From 532bd42aedc0e791f7b76c0c4d5866945f0f34f7 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Fri, 17 Jun 2011 22:17:03 +1200 Subject: [PATCH] Remove obsolete Player index cruft --- OpenRA.Game/ActorInitializer.cs | 2 +- OpenRA.Game/Network/UnitOrders.cs | 8 ++--- OpenRA.Game/Player.cs | 4 +-- OpenRA.Game/Sync.cs | 2 +- OpenRA.Game/Traits/Render/RenderSimple.cs | 4 +-- .../Traits/World/PlayerColorPalette.cs | 2 +- OpenRA.Game/World.cs | 20 +++++-------- OpenRA.Mods.Cnc/Missions/Gdi01Script.cs | 2 +- OpenRA.Mods.Cnc/SpawnViceroid.cs | 2 +- .../Widgets/Logic/CncDiplomacyLogic.cs | 4 +-- .../Widgets/Logic/CncIngameChromeLogic.cs | 2 +- OpenRA.Mods.RA/ConquestVictoryConditions.cs | 6 ++-- OpenRA.Mods.RA/CreateMPPlayers.cs | 29 ++++++++----------- OpenRA.Mods.RA/HackyAI.cs | 2 +- OpenRA.Mods.RA/MPStartLocations.cs | 3 +- .../Player/SurrenderOnDisconnect.cs | 2 +- OpenRA.Mods.RA/SpawnMapActors.cs | 2 +- .../Strategic/StrategicVictoryConditions.cs | 4 +-- .../Widgets/Logic/DiplomacyLogic.cs | 9 ++---- .../Widgets/StrategicProgressWidget.cs | 4 +-- 20 files changed, 49 insertions(+), 64 deletions(-) diff --git a/OpenRA.Game/ActorInitializer.cs b/OpenRA.Game/ActorInitializer.cs index 06979a729c..9ccf316185 100755 --- a/OpenRA.Game/ActorInitializer.cs +++ b/OpenRA.Game/ActorInitializer.cs @@ -128,7 +128,7 @@ namespace OpenRA { if( player != null ) return player; - return world.players.Values.First( x => x.InternalName == PlayerName ); + return world.Players.First( x => x.InternalName == PlayerName ); } } } diff --git a/OpenRA.Game/Network/UnitOrders.cs b/OpenRA.Game/Network/UnitOrders.cs index f356a3d252..cdba271652 100755 --- a/OpenRA.Game/Network/UnitOrders.cs +++ b/OpenRA.Game/Network/UnitOrders.cs @@ -23,8 +23,8 @@ namespace OpenRA.Network /* 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,.. */ - return world.players.Values.FirstOrDefault( - p => p.ClientIndex == c.Index && p.PlayerName == c.Name); + return world.Players.FirstOrDefault( + p => (p.ClientIndex == c.Index && p.PlayerRef.Playable && !p.IsBot)); } public static void ProcessOrder(OrderManager orderManager, World world, int clientId, Order order) @@ -151,8 +151,8 @@ namespace OpenRA.Network if (Game.orderManager.LobbyInfo.GlobalSettings.LockTeams) return; - var targetPlayer = order.Player.World.players[order.TargetLocation.X]; - var newStance = (Stance)order.TargetLocation.Y; + var targetPlayer = order.Player.World.Players.FirstOrDefault(p => p.InternalName == order.TargetString); + var newStance = (Stance)order.TargetLocation.X; SetPlayerStance(world, order.Player, targetPlayer, newStance); diff --git a/OpenRA.Game/Player.cs b/OpenRA.Game/Player.cs index 6db8f01e79..da61d95bc4 100644 --- a/OpenRA.Game/Player.cs +++ b/OpenRA.Game/Player.cs @@ -31,7 +31,6 @@ namespace OpenRA public readonly string PlayerName; public readonly string InternalName; public readonly CountryInfo Country; - public readonly int Index; public readonly bool NonCombatant = false; public readonly int ClientIndex; public readonly PlayerReference PlayerRef; @@ -40,10 +39,9 @@ namespace OpenRA public Shroud Shroud { get { return World.LocalShroud; }} public World World { get; private set; } - public Player(World world, Session.Client client, PlayerReference pr, int index) + public Player(World world, Session.Client client, PlayerReference pr) { World = world; - Index = index; InternalName = pr.Name; PlayerRef = pr; diff --git a/OpenRA.Game/Sync.cs b/OpenRA.Game/Sync.cs index ebcef9ba1f..4f0bb8a287 100755 --- a/OpenRA.Game/Sync.cs +++ b/OpenRA.Game/Sync.cs @@ -127,7 +127,7 @@ namespace OpenRA public static int hash_player( Player p ) { if( p != null ) - return p.Index * 0x567; + return (int)( p.PlayerActor.ActorID << 16 ) * 0x567; return 0; } diff --git a/OpenRA.Game/Traits/Render/RenderSimple.cs b/OpenRA.Game/Traits/Render/RenderSimple.cs index ea01741595..0787e82158 100755 --- a/OpenRA.Game/Traits/Render/RenderSimple.cs +++ b/OpenRA.Game/Traits/Render/RenderSimple.cs @@ -28,7 +28,7 @@ namespace OpenRA.Traits { var anim = new Animation(RenderSimple.GetImage(building, Tileset), () => 0); anim.PlayRepeating("idle"); - yield return new Renderable(anim.Image, 0.5f * anim.Image.size * (1 - Scale), Palette ?? PlayerPalette + owner.Index, 0, Scale); + yield return new Renderable(anim.Image, 0.5f * anim.Image.size * (1 - Scale), Palette ?? PlayerPalette + owner.InternalName, 0, Scale); } } @@ -70,7 +70,7 @@ namespace OpenRA.Traits Info = self.Info.Traits.Get(); } - public string Palette(Player p) { return Info.Palette ?? Info.PlayerPalette + p.Index; } + public string Palette(Player p) { return Info.Palette ?? Info.PlayerPalette + p.InternalName; } public virtual IEnumerable Render(Actor self) { diff --git a/OpenRA.Game/Traits/World/PlayerColorPalette.cs b/OpenRA.Game/Traits/World/PlayerColorPalette.cs index 6266c42d00..f265db256b 100644 --- a/OpenRA.Game/Traits/World/PlayerColorPalette.cs +++ b/OpenRA.Game/Traits/World/PlayerColorPalette.cs @@ -33,7 +33,7 @@ namespace OpenRA.Traits public void InitPalette( WorldRenderer wr ) { - var paletteName = "{0}{1}".F( info.BaseName, owner.Index ); + var paletteName = "{0}{1}".F( info.BaseName, owner.InternalName ); var newpal = new Palette(wr.GetPalette(info.BasePalette), new PlayerColorRemap(owner.ColorRamp, info.PaletteFormat)); wr.AddPalette(paletteName, newpal); diff --git a/OpenRA.Game/World.cs b/OpenRA.Game/World.cs index 82f3f27c10..cb4bcea63b 100644 --- a/OpenRA.Game/World.cs +++ b/OpenRA.Game/World.cs @@ -10,6 +10,7 @@ using System; using System.Collections.Generic; +using System.Linq; using OpenRA.Collections; using OpenRA.Effects; using OpenRA.FileFormats; @@ -35,21 +36,16 @@ namespace OpenRA public XRandom SharedRandom; - public readonly Dictionary players = new Dictionary(); + public readonly List Players = new List(); - public void AddPlayer(Player p) { players[p.Index] = p; } - - int localPlayerIndex = -999; - public Player LocalPlayer - { - get { return players.ContainsKey(localPlayerIndex) ? players[localPlayerIndex] : null; } - } + public void AddPlayer(Player p) { Players.Add(p); } + public Player LocalPlayer { get; private set; } public readonly Shroud LocalShroud; - public void SetLocalPlayer(int index) + public void SetLocalPlayer(string pr) { if (!(orderManager.Connection is ReplayConnection)) - localPlayerIndex = index; + LocalPlayer = Players.FirstOrDefault(p => p.InternalName == pr); } public readonly Actor WorldActor; @@ -108,8 +104,8 @@ namespace OpenRA cmp.CreatePlayers(this); // Set defaults for any unset stances - foreach (var p in players.Values) - foreach (var q in players.Values) + foreach (var p in Players) + foreach (var q in Players) if (!p.Stances.ContainsKey(q)) p.Stances[q] = Stance.Neutral; diff --git a/OpenRA.Mods.Cnc/Missions/Gdi01Script.cs b/OpenRA.Mods.Cnc/Missions/Gdi01Script.cs index a6de2970f5..14ffbc8e68 100644 --- a/OpenRA.Mods.Cnc/Missions/Gdi01Script.cs +++ b/OpenRA.Mods.Cnc/Missions/Gdi01Script.cs @@ -27,7 +27,7 @@ namespace OpenRA.Mods.Cnc public void WorldLoaded(World w) { - Players = w.players.Values.ToDictionary(p => p.InternalName); + Players = w.Players.ToDictionary(p => p.InternalName); Actors = w.WorldActor.Trait().Actors; var b = w.Map.Bounds; Game.MoveViewport(new int2(b.Left + b.Width/2, b.Top + b.Height/2)); diff --git a/OpenRA.Mods.Cnc/SpawnViceroid.cs b/OpenRA.Mods.Cnc/SpawnViceroid.cs index 6df21a020d..7ce3405c8b 100644 --- a/OpenRA.Mods.Cnc/SpawnViceroid.cs +++ b/OpenRA.Mods.Cnc/SpawnViceroid.cs @@ -43,7 +43,7 @@ namespace OpenRA.Mods.Cnc var td = new TypeDictionary { new LocationInit( self.Location ), - new OwnerInit( self.World.players.Values.First(p => p.InternalName == Info.Owner) ) + new OwnerInit( self.World.Players.First(p => p.InternalName == Info.Owner) ) }; if (self.HasTrait()) diff --git a/OpenRA.Mods.Cnc/Widgets/Logic/CncDiplomacyLogic.cs b/OpenRA.Mods.Cnc/Widgets/Logic/CncDiplomacyLogic.cs index e24a207dc0..7910fb3be7 100644 --- a/OpenRA.Mods.Cnc/Widgets/Logic/CncDiplomacyLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/Logic/CncDiplomacyLogic.cs @@ -32,7 +32,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic var itemTemplate = scrollpanel.GetWidget("PLAYER_TEMPLATE"); scrollpanel.RemoveChildren(); - foreach (var p in world.players.Values.Where(a => a != world.LocalPlayer && !a.NonCombatant)) + foreach (var p in world.Players.Where(a => a != world.LocalPlayer && !a.NonCombatant)) { Player pp = p; var item = itemTemplate.Clone(); @@ -64,7 +64,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic var item = ScrollItemWidget.Setup(template, () => s == world.LocalPlayer.Stances[ pp ], () => world.IssueOrder(new Order("SetStance", world.LocalPlayer.PlayerActor, false) - { TargetLocation = new int2(pp.Index, (int)s) })); + { TargetLocation = new int2((int)s, 0), TargetString = pp.InternalName })); item.GetWidget("LABEL").GetText = () => s.ToString(); return item; diff --git a/OpenRA.Mods.Cnc/Widgets/Logic/CncIngameChromeLogic.cs b/OpenRA.Mods.Cnc/Widgets/Logic/CncIngameChromeLogic.cs index 82df56dfb9..e8c7a33b1d 100644 --- a/OpenRA.Mods.Cnc/Widgets/Logic/CncIngameChromeLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/Logic/CncIngameChromeLogic.cs @@ -49,7 +49,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic widget.GetWidget("PLAYER_WIDGETS").IsVisible = () => true; var diplomacyButton = ingameRoot.GetWidget("DIPLOMACY_BUTTON"); - var diplomacyAvailable = world.players.Values.Any(a => a != world.LocalPlayer && !a.NonCombatant); + var diplomacyAvailable = world.Players.Any(a => a != world.LocalPlayer && !a.NonCombatant); diplomacyButton.IsDisabled = () => !diplomacyAvailable; diplomacyButton.OnClick = () => { diff --git a/OpenRA.Mods.RA/ConquestVictoryConditions.cs b/OpenRA.Mods.RA/ConquestVictoryConditions.cs index 867cf3c0a6..feabe66a4e 100644 --- a/OpenRA.Mods.RA/ConquestVictoryConditions.cs +++ b/OpenRA.Mods.RA/ConquestVictoryConditions.cs @@ -36,12 +36,12 @@ namespace OpenRA.Mods.RA if (!hasAnything && !self.Owner.NonCombatant) Lose(self); - var others = self.World.players.Where( p => !p.Value.NonCombatant - && p.Value != self.Owner && p.Value.Stances[self.Owner] != Stance.Ally ); + var others = self.World.Players.Where( p => !p.NonCombatant + && p != self.Owner && p.Stances[self.Owner] != Stance.Ally ); if (others.Count() == 0) return; - if(others.All(p => p.Value.WinState == WinState.Lost)) + if(others.All(p => p.WinState == WinState.Lost)) Win(self); } diff --git a/OpenRA.Mods.RA/CreateMPPlayers.cs b/OpenRA.Mods.RA/CreateMPPlayers.cs index 5cfb154f2a..d89823f91f 100644 --- a/OpenRA.Mods.RA/CreateMPPlayers.cs +++ b/OpenRA.Mods.RA/CreateMPPlayers.cs @@ -21,13 +21,10 @@ namespace OpenRA.Mods.RA { public void CreatePlayers(World w) { - var playerIndex = 0; - var mapPlayerIndex = -1; // todo: unhack this, but people still rely on it. - // 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, mapPlayerIndex--); + var player = new Player(w, null, kv.Value); w.AddPlayer(player); if (kv.Value.OwnsWorld) w.WorldActor.Owner = player; @@ -40,10 +37,10 @@ namespace OpenRA.Mods.RA if (client != null) { /* spawn a real player in this slot. */ - var player = new Player(w, client, w.Map.Players[slot.MapPlayer], playerIndex++); + var player = new Player(w, client, w.Map.Players[slot.MapPlayer]); w.AddPlayer(player); if (client.Index == Game.LocalClientId) - w.SetLocalPlayer(player.Index); // bind this one to the local player. + 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) @@ -55,8 +52,7 @@ namespace OpenRA.Mods.RA w.Map.Players[slot.MapPlayer].ColorRamp = new ColorRamp(hue, 255, 180, 25); /* todo: pick a random name from the pool */ - - var player = new Player(w, null, w.Map.Players[slot.MapPlayer], playerIndex++); + var player = new Player(w, null, w.Map.Players[slot.MapPlayer]); w.AddPlayer(player); /* activate the bot option that's selected! */ @@ -70,8 +66,8 @@ namespace OpenRA.Mods.RA } } - foreach (var p in w.players.Values) - foreach (var q in w.players.Values) + foreach (var p in w.Players) + foreach (var q in w.Players) { if (!p.Stances.ContainsKey(q)) p.Stances[q] = ChooseInitialStance(p, q); @@ -90,17 +86,16 @@ namespace OpenRA.Mods.RA if (p.World.LobbyInfo.Slots[qc.Slot].Spectator) return Stance.Ally; } + // Stances set via the player reference if (p.PlayerRef.Allies.Contains(q.InternalName)) return Stance.Ally; if (p.PlayerRef.Enemies.Contains(q.InternalName)) return Stance.Enemy; - - // Hack: All map players are neutral wrt everyone else - if (p.Index < 0 || q.Index < 0) return Stance.Neutral; - - if (p.IsBot ^ q.IsBot) - return Stance.Enemy; // bots and humans hate each other - + + // Otherwise, default to neutral for map-players + if (!p.PlayerRef.Playable || !q.PlayerRef.Playable) return Stance.Neutral; + // or enemy for bot vs human + if (p.IsBot ^ q.IsBot) return Stance.Enemy; return pc.Team != 0 && pc.Team == qc.Team ? Stance.Ally : Stance.Enemy; diff --git a/OpenRA.Mods.RA/HackyAI.cs b/OpenRA.Mods.RA/HackyAI.cs index 8966238955..c4a38c0c04 100644 --- a/OpenRA.Mods.RA/HackyAI.cs +++ b/OpenRA.Mods.RA/HackyAI.cs @@ -250,7 +250,7 @@ namespace OpenRA.Mods.RA bool HasHumanPlayers() { - return p.World.players.Any(a => !a.Value.IsBot && !a.Value.NonCombatant); + return p.World.Players.Any(a => !a.IsBot && !a.NonCombatant); } int2? ChooseEnemyTarget() diff --git a/OpenRA.Mods.RA/MPStartLocations.cs b/OpenRA.Mods.RA/MPStartLocations.cs index ed7ffe3727..8a31ffcb93 100755 --- a/OpenRA.Mods.RA/MPStartLocations.cs +++ b/OpenRA.Mods.RA/MPStartLocations.cs @@ -39,7 +39,6 @@ namespace OpenRA.Mods.RA continue; // Skip spectator slots var client = world.LobbyInfo.Clients.FirstOrDefault(c => c.Slot == slot.Index); var player = FindPlayerInSlot(world, slot); - if (player == null) continue; var spid = (client == null || client.SpawnPoint == 0) @@ -62,7 +61,7 @@ namespace OpenRA.Mods.RA static Player FindPlayerInSlot(World world, Session.Slot slot) { - return world.players.Values.FirstOrDefault(p => p.PlayerRef.Name == slot.MapPlayer); + return world.Players.FirstOrDefault(p => p.PlayerRef.Name == slot.MapPlayer); } static int2 ChooseSpawnPoint(World world, List available, List taken) diff --git a/OpenRA.Mods.RA/Player/SurrenderOnDisconnect.cs b/OpenRA.Mods.RA/Player/SurrenderOnDisconnect.cs index 6b4451ba80..78a1b0c7d0 100644 --- a/OpenRA.Mods.RA/Player/SurrenderOnDisconnect.cs +++ b/OpenRA.Mods.RA/Player/SurrenderOnDisconnect.cs @@ -37,7 +37,7 @@ namespace OpenRA.Mods.RA if (client.State == Session.ClientState.Disconnected) { Disconnected = true; /* dont call this multiple times! */ - self.World.players.Do(pl => pl.Value.PlayerActor.TraitsImplementing().Do(t => t.ResolveOrder(pl.Value.PlayerActor, new Order("Surrender", self, false)))); + self.World.Players.Do(pl => pl.PlayerActor.TraitsImplementing().Do(t => t.ResolveOrder(pl.PlayerActor, new Order("Surrender", self, false)))); } } } diff --git a/OpenRA.Mods.RA/SpawnMapActors.cs b/OpenRA.Mods.RA/SpawnMapActors.cs index ba9fc6e655..1e28a81c42 100644 --- a/OpenRA.Mods.RA/SpawnMapActors.cs +++ b/OpenRA.Mods.RA/SpawnMapActors.cs @@ -26,7 +26,7 @@ namespace OpenRA.Mods.RA { // if there is no real player associated, dont spawn it. var ownerName = actorReference.Value.InitDict.Get().PlayerName; - if (!world.players.Values.Any(p => p.InternalName == ownerName)) + if (!world.Players.Any(p => p.InternalName == ownerName)) continue; var initDict = actorReference.Value.InitDict; diff --git a/OpenRA.Mods.RA/Strategic/StrategicVictoryConditions.cs b/OpenRA.Mods.RA/Strategic/StrategicVictoryConditions.cs index 67d989cca5..e000930ec8 100644 --- a/OpenRA.Mods.RA/Strategic/StrategicVictoryConditions.cs +++ b/OpenRA.Mods.RA/Strategic/StrategicVictoryConditions.cs @@ -89,7 +89,7 @@ namespace OpenRA.Mods.RA { int total = 0; - foreach (var p in Self.World.players.Select(k => k.Value)) + foreach (var p in Self.World.Players) { if (p == Self.Owner || (p.Stances[Self.Owner] == Stance.Ally && Self.Owner.Stances[p] == Stance.Ally)) { @@ -195,7 +195,7 @@ namespace OpenRA.Mods.RA public void Won() { // Player has won - foreach (var p in Self.World.players.Select(k => k.Value)) + foreach (var p in Self.World.Players) { var cvc = p.PlayerActor.TraitOrDefault(); diff --git a/OpenRA.Mods.RA/Widgets/Logic/DiplomacyLogic.cs b/OpenRA.Mods.RA/Widgets/Logic/DiplomacyLogic.cs index fdc1b62f88..10a3b9c38e 100644 --- a/OpenRA.Mods.RA/Widgets/Logic/DiplomacyLogic.cs +++ b/OpenRA.Mods.RA/Widgets/Logic/DiplomacyLogic.cs @@ -40,7 +40,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic return true; }; - validPlayers = world.players.Values.Where(a => a != world.LocalPlayer && !a.NonCombatant).Count(); + validPlayers = world.Players.Where(a => a != world.LocalPlayer && !a.NonCombatant).Count(); diplomacy.IsVisible = () => (validPlayers > 0); } @@ -79,13 +79,12 @@ namespace OpenRA.Mods.RA.Widgets.Logic y += 35; - foreach (var p in world.players.Values.Where(a => a != world.LocalPlayer && !a.NonCombatant)) + foreach (var p in world.Players.Where(a => a != world.LocalPlayer && !a.NonCombatant)) { var pp = p; var label = new LabelWidget { Bounds = new Rectangle(margin, y, labelWidth, 25), - Id = "DIPLOMACY_PLAYER_LABEL_{0}".F(p.Index), Text = p.PlayerName, Align = LabelWidget.TextAlign.Left, Font = "Bold", @@ -97,7 +96,6 @@ namespace OpenRA.Mods.RA.Widgets.Logic var theirStance = new LabelWidget { Bounds = new Rectangle( margin + labelWidth + 10, y, labelWidth, 25), - Id = "DIPLOMACY_PLAYER_LABEL_THEIR_{0}".F(p.Index), Text = p.PlayerName, Align = LabelWidget.TextAlign.Left, @@ -110,7 +108,6 @@ namespace OpenRA.Mods.RA.Widgets.Logic var myStance = new DropDownButtonWidget { Bounds = new Rectangle( margin + 2 * labelWidth + 20, y, labelWidth, 25), - Id = "DIPLOMACY_PLAYER_LABEL_MY_{0}".F(p.Index), GetText = () => world.LocalPlayer.Stances[ pp ].ToString(), }; @@ -145,7 +142,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic return; // team changes are banned world.IssueOrder(new Order("SetStance", world.LocalPlayer.PlayerActor, - false) { TargetLocation = new int2(p.Index, (int)ss) }); + false) { TargetLocation = new int2((int)ss, 0), TargetString = p.InternalName }); bw.Text = ss.ToString(); } diff --git a/OpenRA.Mods.RA/Widgets/StrategicProgressWidget.cs b/OpenRA.Mods.RA/Widgets/StrategicProgressWidget.cs index c368075d25..83c4e22462 100644 --- a/OpenRA.Mods.RA/Widgets/StrategicProgressWidget.cs +++ b/OpenRA.Mods.RA/Widgets/StrategicProgressWidget.cs @@ -43,7 +43,7 @@ namespace OpenRA.Mods.RA.Widgets if (!IsVisible()) return; int2 offset = int2.Zero; - var svc = world.players.Select(p => p.Value.PlayerActor.TraitOrDefault()).FirstOrDefault(); + var svc = world.Players.Select(p => p.PlayerActor.TraitOrDefault()).FirstOrDefault(); var totalWidth = (svc.Total + svc.TotalCritical) * 32; int curX = -(totalWidth / 2); @@ -95,7 +95,7 @@ namespace OpenRA.Mods.RA.Widgets int shortest = int.MaxValue; Player shortestPlayer = null; - foreach (var p in world.players.Select(p => p.Value).Where(p => !p.NonCombatant)) + foreach (var p in world.Players.Where(p => !p.NonCombatant)) { var svc = p.PlayerActor.Trait();