Make bots first-class players.
- Bots have their own Clients, with unique ClientIDs - Hosts can set bot team/color in the lobby - Bots are kicked when switching to a smaller map without enough slots - Order validator assumes that only client 0 has permission to issue bot orders
This commit is contained in:
@@ -34,8 +34,7 @@ namespace OpenRA.Network
|
||||
|
||||
public string FirstEmptySlot()
|
||||
{
|
||||
return Slots.FirstOrDefault(s => !s.Value.Closed && ClientInSlot(s.Key) == null
|
||||
&& s.Value.Bot == null).Key;
|
||||
return Slots.FirstOrDefault(s => !s.Value.Closed && ClientInSlot(s.Key) == null).Key;
|
||||
}
|
||||
|
||||
public enum ClientState
|
||||
@@ -55,12 +54,12 @@ namespace OpenRA.Network
|
||||
public ClientState State;
|
||||
public int Team;
|
||||
public string Slot; // slot ID, or null for observer
|
||||
public string Bot; // Bot type, null for real clients
|
||||
}
|
||||
|
||||
public class Slot
|
||||
{
|
||||
public string PlayerReference; // playerReference to bind against.
|
||||
public string Bot; // trait name of the bot to initialize in this slot, or null otherwise.
|
||||
public bool Closed; // host has explicitly closed this slot.
|
||||
|
||||
public bool AllowBots;
|
||||
|
||||
@@ -21,10 +21,9 @@ namespace OpenRA.Network
|
||||
static Player FindPlayerByClient(this World world, Session.Client c)
|
||||
{
|
||||
/* todo: this is still a hack.
|
||||
* the cases we're trying to avoid are the extra players on the host's client -- Neutral, other MapPlayers,
|
||||
* bots,.. */
|
||||
* the cases we're trying to avoid are the extra players on the host's client -- Neutral, other MapPlayers,..*/
|
||||
return world.Players.FirstOrDefault(
|
||||
p => (p.ClientIndex == c.Index && p.PlayerRef.Playable && !p.IsBot));
|
||||
p => (p.ClientIndex == c.Index && p.PlayerRef.Playable));
|
||||
}
|
||||
|
||||
public static void ProcessOrder(OrderManager orderManager, World world, int clientId, Order order)
|
||||
|
||||
@@ -46,11 +46,13 @@ namespace OpenRA
|
||||
PlayerRef = pr;
|
||||
string botType = null;
|
||||
|
||||
// Real player or host-created bot
|
||||
if (client != null)
|
||||
{
|
||||
ClientIndex = client.Index;
|
||||
ColorRamp = client.ColorRamp;
|
||||
PlayerName = client.Name;
|
||||
botType = client.Bot;
|
||||
|
||||
Country = world.GetCountries()
|
||||
.FirstOrDefault(c => client.Country == c.Race)
|
||||
@@ -58,36 +60,21 @@ namespace OpenRA
|
||||
}
|
||||
else
|
||||
{
|
||||
// Map player or bot
|
||||
// Map player
|
||||
ClientIndex = 0; /* it's a map player, "owned" by host */
|
||||
ColorRamp = pr.ColorRamp;
|
||||
PlayerName = pr.Name;
|
||||
NonCombatant = pr.NonCombatant;
|
||||
IsBot = pr.Bot != null;
|
||||
botType = pr.Bot;
|
||||
|
||||
Country = world.GetCountries()
|
||||
.FirstOrDefault(c => pr.Race == c.Race)
|
||||
?? world.GetCountries().Random(world.SharedRandom);
|
||||
|
||||
// Multiplayer bot
|
||||
if (slot != null && slot.Bot != null)
|
||||
{
|
||||
IsBot = true;
|
||||
botType = slot.Bot;
|
||||
PlayerName = slot.Bot;
|
||||
|
||||
// pick a random color for the bot
|
||||
var hue = (byte)world.SharedRandom.Next(255);
|
||||
var sat = (byte)world.SharedRandom.Next(255);
|
||||
var lum = (byte)world.SharedRandom.Next(51,255);
|
||||
ColorRamp = new ColorRamp(hue, sat, lum, 10);
|
||||
}
|
||||
}
|
||||
|
||||
PlayerActor = world.CreateActor("Player", new TypeDictionary { new OwnerInit(this) });
|
||||
|
||||
// Enable the bot logic
|
||||
// Enable the bot logic on the host
|
||||
IsBot = botType != null;
|
||||
if (IsBot && Game.IsHost)
|
||||
{
|
||||
var logic = PlayerActor.TraitsImplementing<IBot>()
|
||||
|
||||
@@ -129,10 +129,11 @@ namespace OpenRA.Server
|
||||
* for manual spawnpoint choosing.
|
||||
* - 256 max players is a dirty hack
|
||||
*/
|
||||
int ChooseFreePlayerIndex()
|
||||
public int ChooseFreePlayerIndex()
|
||||
{
|
||||
for (var i = 0; i < 256; i++)
|
||||
if (conns.All(c => c.PlayerIndex != i) && preConns.All(c => c.PlayerIndex != i))
|
||||
if (conns.All(c => c.PlayerIndex != i) && preConns.All(c => c.PlayerIndex != i)
|
||||
&& lobbyInfo.Clients.All(c => c.Index != i))
|
||||
return i;
|
||||
|
||||
throw new InvalidOperationException("Already got 256 players");
|
||||
|
||||
@@ -18,10 +18,18 @@ namespace OpenRA.Traits
|
||||
{
|
||||
public bool OrderValidation(OrderManager orderManager, World world, int clientId, Order order)
|
||||
{
|
||||
// Drop exploiting orders
|
||||
if (order.Subject != null && order.Subject.Owner.ClientIndex != clientId)
|
||||
if (order.Subject == null || order.Subject.Owner == null)
|
||||
return true;
|
||||
|
||||
var subjectClient = order.Subject.Owner.ClientIndex;
|
||||
|
||||
// Hack: Assumes bots always run on clientId 0.
|
||||
var isBotOrder = orderManager.LobbyInfo.Clients[subjectClient].Bot != null && clientId == 0;
|
||||
|
||||
// Drop exploiting orders
|
||||
if (subjectClient != clientId && !isBotOrder)
|
||||
{
|
||||
Game.Debug("Detected exploit order from {0}: {1}".F(clientId, order.OrderString));
|
||||
Game.Debug("Detected exploit order from client {0}: {1}".F(clientId, order.OrderString));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user