From b792d07c8c0e4f04d85637ad4fbd709e07be789b Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Sat, 21 May 2011 12:05:26 +1200 Subject: [PATCH] start reworking player setup --- OpenRA.Editor/Form1.cs | 43 +++---------------- OpenRA.Game/Map.cs | 93 ++++++++++++++++++------------------------ 2 files changed, 45 insertions(+), 91 deletions(-) diff --git a/OpenRA.Editor/Form1.cs b/OpenRA.Editor/Form1.cs index ad08af801d..9ebf4896ab 100755 --- a/OpenRA.Editor/Form1.cs +++ b/OpenRA.Editor/Form1.cs @@ -87,13 +87,7 @@ namespace OpenRA.Editor // upgrade maps that have no player definitions. editor doesnt care, // but this breaks the game pretty badly. if (map.Players.Count == 0) - map.Players.Add("Neutral", new PlayerReference - { - Name = "Neutral", - Race = Rules.Info["world"].Traits.WithInterface().First().Race, - OwnsWorld = true, - NonCombatant = true - }); + map.MakeDefaultPlayers(); PrepareMapResources(Game.modData.Manifest, map); @@ -326,21 +320,8 @@ namespace OpenRA.Editor map.Resize((int)nmd.width.Value, (int)nmd.height.Value); map.ResizeCordon((int)nmd.cordonLeft.Value, (int)nmd.cordonTop.Value, (int)nmd.cordonRight.Value, (int)nmd.cordonBottom.Value); - - map.Players.Add("Neutral", new PlayerReference - { - Name = "Neutral", - Race = Rules.Info["world"].Traits.WithInterface().First().Race, - OwnsWorld = true, - NonCombatant = true - }); - - map.Players.Add("Creeps", new PlayerReference - { - Name = "Creeps", - Race = Rules.Info["world"].Traits.WithInterface().First().Race, - NonCombatant = true - }); + + map.MakeDefaultPlayers(); NewMap(map); } @@ -395,22 +376,8 @@ namespace OpenRA.Editor if (errors.Count > 0) using (var eld = new ErrorListDialog(errors)) eld.ShowDialog(); - - if (!map.Players.ContainsKey("Neutral")) - map.Players.Add("Neutral", new PlayerReference - { - Name = "Neutral", - Race = Rules.Info["world"].Traits.WithInterface().First().Race, - OwnsWorld = true, - NonCombatant = true - }); - - map.Players.Add("Creeps", new PlayerReference - { - Name = "Creeps", - Race = Rules.Info["world"].Traits.WithInterface().First().Race, - NonCombatant = true - }); + + map.MakeDefaultPlayers(); map.Save(savePath); LoadMap(savePath); diff --git a/OpenRA.Game/Map.cs b/OpenRA.Game/Map.cs index 78b03d2056..8e6024a162 100644 --- a/OpenRA.Game/Map.cs +++ b/OpenRA.Game/Map.cs @@ -16,6 +16,7 @@ using System.Linq; using System.Security.Cryptography; using System.Text; using OpenRA.FileFormats; +using OpenRA.Traits; namespace OpenRA { @@ -118,7 +119,8 @@ namespace OpenRA // Support for formats 1-3 dropped 2011-02-11. // Use release-20110207 to convert older maps to format 4 - if (MapFormat < 4) + // Use release-20110511 to convert older maps to format 5 + if (MapFormat < 5) throw new InvalidDataException("Map format {0} is not supported.\n File: {1}".F(MapFormat, path)); // Load players @@ -136,60 +138,8 @@ namespace OpenRA ret.Add(kv.Key, new ActorReference(kv.Value.Value, kv.Value.NodesDict)); // Add waypoint actors - - if (MapFormat < 5) - foreach( var wp in yaml.NodesDict[ "Waypoints" ].NodesDict ) - { - string[] loc = wp.Value.Value.Split( ',' ); - var a = new ActorReference("mpspawn"); - a.Add(new LocationInit(new int2( int.Parse( loc[ 0 ] ), int.Parse( loc[ 1 ] ) ))); - a.Add(new OwnerInit(Players.First(p => p.Value.OwnsWorld).Key)); - ret.Add(wp.Key, a); - } - return ret; }); - - - /* hack: make some slots. */ - if (!Players.Any(p => p.Value.Playable)) - { - for (int index = 0; index < SpawnPoints.Count(); index++) - { - var p = new PlayerReference - { - Name = "Multi{0}".F(index), - Race = "Random", - Playable = true, - DefaultStartingUnits = true, - Enemies = new[] { "Creeps" } - }; - Players.Add(p.Name, p); - } - } - - // Upgrade map to format 5 - if (MapFormat < 5) - { - // Define RequiresMod for map installer - RequiresMod = Game.CurrentMods.Keys.First(); - - var TopLeft = (int2)FieldLoader.GetValue( "", typeof(int2), yaml.NodesDict["TopLeft"].Value); - var BottomRight = (int2)FieldLoader.GetValue( "", typeof(int2), yaml.NodesDict["BottomRight"].Value); - Bounds = Rectangle.FromLTRB(TopLeft.X, TopLeft.Y, BottomRight.X, BottomRight.Y); - - // Creep player - foreach (var mp in Players.Where(p => !p.Value.NonCombatant && !p.Value.Enemies.Contains("Creeps"))) - mp.Value.Enemies = mp.Value.Enemies.Concat(new[] {"Creeps"}).ToArray(); - - Players.Add("Creeps", new PlayerReference - { - Name = "Creeps", - Race = "Random", - NonCombatant = true, - Enemies = Players.Where(p => p.Value.Playable).Select(p => p.Key).ToArray() - }); - } // Smudges Smudges = Lazy.New(() => @@ -446,5 +396,42 @@ namespace OpenRA using (var csp = SHA1.Create()) return new string(csp.ComputeHash(data).SelectMany(a => a.ToString("x2")).ToArray()); } + + public void MakeDefaultPlayers() + { + Players.Clear(); + + var firstRace = OpenRA.Rules.Info["world"].Traits + .WithInterface().First().Race; + + Players.Add("Neutral", new PlayerReference + { + Name = "Neutral", + Race = firstRace, + OwnsWorld = true, + NonCombatant = true + }); + + for (int index = 0; index < SpawnPoints.Count(); index++) + { + var p = new PlayerReference + { + Name = "Multi{0}".F(index), + Race = "Random", + Playable = true, + DefaultStartingUnits = true, + Enemies = new[] { "Creeps" } + }; + Players.Add(p.Name, p); + } + + Players.Add("Creeps", new PlayerReference + { + Name = "Creeps", + Race = firstRace, + NonCombatant = true, + Enemies = Players.Where(p => p.Value.Playable).Select(p => p.Key).ToArray() + }); + } } }