From e934baa6e971217153cf262a3f540b6632471eee Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Fri, 17 Jun 2011 22:50:05 +1200 Subject: [PATCH] Better bot spawning code; better random color and proper player names. --- OpenRA.Game/Player.cs | 33 +++++++++++++++++++++++----- OpenRA.Mods.RA/CreateMPPlayers.cs | 36 ++++++------------------------- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/OpenRA.Game/Player.cs b/OpenRA.Game/Player.cs index da61d95bc4..22ca713a28 100644 --- a/OpenRA.Game/Player.cs +++ b/OpenRA.Game/Player.cs @@ -39,11 +39,12 @@ namespace OpenRA public Shroud Shroud { get { return World.LocalShroud; }} public World World { get; private set; } - public Player(World world, Session.Client client, PlayerReference pr) + public Player(World world, Session.Client client, Session.Slot slot, PlayerReference pr) { World = world; InternalName = pr.Name; PlayerRef = pr; + string botType = null; if (client != null) { @@ -57,23 +58,45 @@ namespace OpenRA } else { + // Map player or bot 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 = pr.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) }); - if (IsBot) - PlayerActor.TraitsImplementing() - .Single(b => b.Info.Name == pr.Bot) - .Activate(this); + // Enable the bot logic + if (IsBot && Game.IsHost) + { + var logic = PlayerActor.TraitsImplementing() + .FirstOrDefault(b => b.Info.Name == botType); + if (logic == null) + Log.Write("debug", "Invalid bot type: {0}", botType); + else + logic.Activate(this); + } } public void GiveAdvice(string advice) diff --git a/OpenRA.Mods.RA/CreateMPPlayers.cs b/OpenRA.Mods.RA/CreateMPPlayers.cs index d89823f91f..dec748eae7 100644 --- a/OpenRA.Mods.RA/CreateMPPlayers.cs +++ b/OpenRA.Mods.RA/CreateMPPlayers.cs @@ -24,7 +24,7 @@ namespace OpenRA.Mods.RA // create the unplayable map players -- neutral, shellmap, scripted, etc. foreach (var kv in w.Map.Players.Where(p => !p.Value.Playable)) { - var player = new Player(w, null, kv.Value); + var player = new Player(w, null, null, kv.Value); w.AddPlayer(player); if (kv.Value.OwnsWorld) w.WorldActor.Owner = player; @@ -34,36 +34,14 @@ namespace OpenRA.Mods.RA foreach (var slot in w.LobbyInfo.Slots) { var client = w.LobbyInfo.Clients.FirstOrDefault(c => c.Slot == slot.Index && slot.MapPlayer != null); - if (client != null) - { - /* spawn a real player in this slot. */ - var player = new Player(w, client, w.Map.Players[slot.MapPlayer]); - w.AddPlayer(player); - if (client.Index == Game.LocalClientId) - w.SetLocalPlayer(player.InternalName); // bind this one to the local player. - } - // TODO: This is shit. Merge it up into Player ctor? - else if (slot.Bot != null && slot.MapPlayer != null) - { - /* spawn a bot in this slot, "owned" by the host */ + if (client == null && slot.Bot == null) + continue; - /* pick a random color for the bot */ - var hue = (byte)w.SharedRandom.Next(256); - w.Map.Players[slot.MapPlayer].ColorRamp = new ColorRamp(hue, 255, 180, 25); + var player = new Player(w, client, slot, w.Map.Players[slot.MapPlayer]); + w.AddPlayer(player); - /* todo: pick a random name from the pool */ - var player = new Player(w, null, w.Map.Players[slot.MapPlayer]); - w.AddPlayer(player); - - /* activate the bot option that's selected! */ - if (Game.IsHost) - player.PlayerActor.TraitsImplementing() - .Single(b => b.Info.Name == slot.Bot) - .Activate(player); - - /* a bit of a hack */ - player.IsBot = true; - } + if (client != null && client.Index == Game.LocalClientId) + w.SetLocalPlayer(player.InternalName); } foreach (var p in w.Players)