From 10b7ece62e2533d2e0cdbd7fbf8dae566ab593d1 Mon Sep 17 00:00:00 2001 From: Bob Date: Sun, 1 Aug 2010 10:31:23 +1200 Subject: [PATCH] new mapactor init stuff --- OpenRA.Editor/Form1.cs | 1 + OpenRA.FileFormats/Map/MapStub.cs | 5 +- OpenRA.FileFormats/OpenRA.FileFormats.csproj | 3 +- OpenRA.FileFormats/TypeDictionary.cs | 5 +- OpenRA.Game/Actor.cs | 29 +++---- OpenRA.Game/ActorInitializer.cs | 83 +++++++++++++++++++ .../Map => OpenRA.Game}/Map.cs | 17 ++-- OpenRA.Game/OpenRA.Game.csproj | 2 + OpenRA.Game/Traits/Building.cs | 2 +- OpenRA.Game/Traits/Mobile.cs | 2 +- OpenRA.Game/Widgets/MapPreviewWidget.cs | 2 +- OpenRA.Game/World.cs | 31 +++++-- OpenRA.Mods.Cnc/ProductionAirdrop.cs | 2 +- OpenRA.Mods.RA/Aircraft.cs | 2 +- OpenRA.Mods.RA/Crate.cs | 2 +- OpenRA.Mods.RA/CrateDrop.cs | 2 +- OpenRA.Mods.RA/Crates/GiveUnitCrateAction.cs | 2 +- OpenRA.Mods.RA/Husk.cs | 2 +- OpenRA.Mods.RA/Mine.cs | 2 +- .../SupportPowers/ParatroopersPower.cs | 2 +- 20 files changed, 149 insertions(+), 49 deletions(-) create mode 100755 OpenRA.Game/ActorInitializer.cs rename {OpenRA.FileFormats/Map => OpenRA.Game}/Map.cs (92%) mode change 100644 => 100755 diff --git a/OpenRA.Editor/Form1.cs b/OpenRA.Editor/Form1.cs index 36f1f15e90..e165fbaeed 100644 --- a/OpenRA.Editor/Form1.cs +++ b/OpenRA.Editor/Form1.cs @@ -152,6 +152,7 @@ namespace OpenRA.Editor try { var info = Rules.Info[a]; + if( !info.Traits.Contains() ) continue; var template = RenderActor(info, tileset, palette); var ibox = new PictureBox { diff --git a/OpenRA.FileFormats/Map/MapStub.cs b/OpenRA.FileFormats/Map/MapStub.cs index d3b33ae073..c31bce0fae 100644 --- a/OpenRA.FileFormats/Map/MapStub.cs +++ b/OpenRA.FileFormats/Map/MapStub.cs @@ -17,10 +17,10 @@ namespace OpenRA.FileFormats { public class MapStub { - public IFolder Package; + public readonly IFolder Package; // Yaml map data - public string Uid; + public readonly string Uid; public bool Selectable; public string Title; @@ -35,7 +35,6 @@ namespace OpenRA.FileFormats public int2 BottomRight; public int Width { get { return BottomRight.X - TopLeft.X; } } public int Height { get { return BottomRight.Y - TopLeft.Y; } } - public Map Map { get { return new Map(Package); }} static List Fields = new List() { "Selectable", "Title", "Description", "Author", "PlayerCount", "Tileset", "TopLeft", "BottomRight" diff --git a/OpenRA.FileFormats/OpenRA.FileFormats.csproj b/OpenRA.FileFormats/OpenRA.FileFormats.csproj index d919f4a3e0..6024cedd55 100644 --- a/OpenRA.FileFormats/OpenRA.FileFormats.csproj +++ b/OpenRA.FileFormats/OpenRA.FileFormats.csproj @@ -1,4 +1,4 @@ - + Debug @@ -77,7 +77,6 @@ - diff --git a/OpenRA.FileFormats/TypeDictionary.cs b/OpenRA.FileFormats/TypeDictionary.cs index cfcef67dae..b7f86ef4b1 100644 --- a/OpenRA.FileFormats/TypeDictionary.cs +++ b/OpenRA.FileFormats/TypeDictionary.cs @@ -9,12 +9,13 @@ #endregion using System; +using System.Collections; using System.Collections.Generic; using System.Linq; namespace OpenRA.FileFormats { - public class TypeDictionary + public class TypeDictionary : IEnumerable { Dictionary dataSingular = new Dictionary(); Dictionary> dataMultiple = new Dictionary>(); @@ -85,7 +86,7 @@ namespace OpenRA.FileFormats return new T[ 0 ]; } - public IEnumerator GetEnumerator() + public IEnumerator GetEnumerator() { return WithInterface().GetEnumerator(); } diff --git a/OpenRA.Game/Actor.cs b/OpenRA.Game/Actor.cs index 572a190541..30b91880a9 100755 --- a/OpenRA.Game/Actor.cs +++ b/OpenRA.Game/Actor.cs @@ -35,13 +35,14 @@ namespace OpenRA IActivity currentActivity; public Group Group; - public Actor(World world, string name, int2 location, Player owner) + internal Actor(World world, string name, TypeDictionary initDict ) { - World = world; - ActorID = world.NextAID(); - Owner = owner; + var init = new ActorInitializer( this, initDict ); - var init = new ActorInitializer( this, location ); + World = world; + ActorID = world.NextAID(); + if( initDict.Contains() ) + Owner = init.Get(); if (name != null) { @@ -182,18 +183,10 @@ namespace OpenRA var o = obj as Actor; return ( o != null && o.ActorID == ActorID ); } - } - - public class ActorInitializer - { - public readonly Actor self; - public World world { get { return self.World; } } - public readonly int2 location; - - public ActorInitializer( Actor actor, int2 location ) + + public override string ToString() { - this.self = actor; - this.location = location; - } - } + return "{0} {1}{2}".F( Info.Name, ActorID, IsInWorld ? "" : " (not in world)" ); + } + } } diff --git a/OpenRA.Game/ActorInitializer.cs b/OpenRA.Game/ActorInitializer.cs new file mode 100755 index 0000000000..b42753b7b1 --- /dev/null +++ b/OpenRA.Game/ActorInitializer.cs @@ -0,0 +1,83 @@ +#region Copyright & License Information +/* + * Copyright 2007-2010 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation. For more information, + * see LICENSE. + */ +#endregion + +using System.Linq; +using OpenRA.FileFormats; + +namespace OpenRA +{ + public class ActorInitializer + { + public readonly Actor self; + public World world { get { return self.World; } } + internal TypeDictionary dict; + + public ActorInitializer( Actor actor, TypeDictionary dict ) + { + this.self = actor; + this.dict = dict; + } + + public T Get() + where T : IActorInit + { + return dict.Get(); + } + + public U Get() + where T : IActorInit + { + return dict.Get().Value( world ); + } + } + + public interface IActorInit + { + } + + public interface IActorInit : IActorInit + { + T Value( World world ); + } + + public class LocationInit : IActorInit + { + public readonly int2 value = int2.Zero; + + public LocationInit( int2 init ) + { + value = init; + } + + public int2 Value( World world ) + { + return value; + } + } + + public class OwnerInit : IActorInit + { + public readonly string PlayerName = "Neutral"; + Player player; + + public OwnerInit( Player player ) + { + this.player = player; + this.PlayerName = player.InternalName; + } + + public Player Value( World world ) + { + if( player != null ) + return player; + return world.players.First( x => x.Value.InternalName == PlayerName ).Value; + } + } +} diff --git a/OpenRA.FileFormats/Map/Map.cs b/OpenRA.Game/Map.cs old mode 100644 new mode 100755 similarity index 92% rename from OpenRA.FileFormats/Map/Map.cs rename to OpenRA.Game/Map.cs index 19885e9ec5..4791bf4261 --- a/OpenRA.FileFormats/Map/Map.cs +++ b/OpenRA.Game/Map.cs @@ -15,8 +15,9 @@ using System.IO; using System.Linq; using System.Reflection; using System.Security.Cryptography; +using OpenRA.FileFormats; -namespace OpenRA.FileFormats +namespace OpenRA { public class Map { @@ -118,14 +119,14 @@ namespace OpenRA.FileFormats // Actors if (MapFormat == 1) - { + { int actors = 0; foreach (var kv in yaml["Actors"].Nodes) { - string[] vals = kv.Value.Value.Split(' '); - string[] loc = vals[2].Split(','); - var a = new ActorReference("Actor"+actors++, vals[0], new int2(int.Parse(loc[0]), int.Parse(loc[1])), "Neutral"); - Actors.Add(a.Id, a); + string[] vals = kv.Value.Value.Split(' '); + string[] loc = vals[2].Split(','); + var a = new ActorReference("Actor"+actors++, vals[0], new int2(int.Parse(loc[0]), int.Parse(loc[1])), "Neutral"); + Actors.Add(a.Id, a); } } else @@ -274,8 +275,8 @@ namespace OpenRA.FileFormats { // UID is calculated by taking an SHA1 of the yaml and binary data // Read the relevant data into a buffer - var data = Exts.ReadAllBytes(Package.GetContent("map.yaml")) - .Concat(Exts.ReadAllBytes(Package.GetContent("map.bin"))).ToArray(); + var data = Package.GetContent("map.yaml").ReadAllBytes() + .Concat(Package.GetContent("map.bin").ReadAllBytes()).ToArray(); // Take the SHA1 using (var csp = SHA1.Create()) diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index 644b0db4bf..f6ab5a911c 100755 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -227,6 +227,8 @@ {BDAEAB25-991E-46A7-AF1E-4F0E03358DAA} OpenRA.FileFormats + + diff --git a/OpenRA.Game/Traits/Building.cs b/OpenRA.Game/Traits/Building.cs index 154f085bbf..c10e4ba2b4 100644 --- a/OpenRA.Game/Traits/Building.cs +++ b/OpenRA.Game/Traits/Building.cs @@ -53,7 +53,7 @@ namespace OpenRA.Traits public Building(ActorInitializer init) { this.self = init.self; - this.topLeft = init.location; + this.topLeft = init.Get(); Info = self.Info.Traits.Get(); self.CenterLocation = Game.CellSize * ((float2)topLeft + .5f * (float2)Info.Dimensions); diff --git a/OpenRA.Game/Traits/Mobile.cs b/OpenRA.Game/Traits/Mobile.cs index 3642e52877..53dafb47b7 100644 --- a/OpenRA.Game/Traits/Mobile.cs +++ b/OpenRA.Game/Traits/Mobile.cs @@ -76,7 +76,7 @@ namespace OpenRA.Traits { this.self = init.self; this.Info = info; - this.__fromCell = this.__toCell = init.location; + this.__fromCell = this.__toCell = init.Get(); shroud = self.World.WorldActor.traits.Get(); uim = self.World.WorldActor.traits.Get(); diff --git a/OpenRA.Game/Widgets/MapPreviewWidget.cs b/OpenRA.Game/Widgets/MapPreviewWidget.cs index ac69031d14..1814ceea39 100644 --- a/OpenRA.Game/Widgets/MapPreviewWidget.cs +++ b/OpenRA.Game/Widgets/MapPreviewWidget.cs @@ -24,7 +24,7 @@ namespace OpenRA.Widgets public Func Map = () => null; public Action OnSpawnClick = spawn => {}; public Func> SpawnColors = () => new Dictionary(); - static Cache PreviewCache = new Cache(stub => Minimap.RenderMapPreview(stub.Map)); + static Cache PreviewCache = new Cache(stub => Minimap.RenderMapPreview( new Map( stub.Package ))); public MapPreviewWidget() : base() { } protected MapPreviewWidget(MapPreviewWidget other) diff --git a/OpenRA.Game/World.cs b/OpenRA.Game/World.cs index 95fb163802..733bc8e579 100644 --- a/OpenRA.Game/World.cs +++ b/OpenRA.Game/World.cs @@ -90,8 +90,8 @@ namespace OpenRA WorldRenderer = new WorldRenderer(this); Timer.Time("renderer: {0}"); - - WorldActor = CreateActor("World", new int2(int.MaxValue, int.MaxValue), null); + + WorldActor = CreateActor( "World", new TypeDictionary() ); // Add Map Players int mapPlayerIndex = -1; @@ -124,11 +124,32 @@ namespace OpenRA Timer.Time( "----end World.ctor" ); } - + public Actor CreateActor( string name, int2 location, Player owner ) { - var a = new Actor( this, name, location, owner ); - Add( a ); + return CreateActor( true, name, location, owner ); + } + + public Actor CreateActor( bool addToWorld, string name, int2 location, Player owner ) + { + var initDict = new TypeDictionary + { + new LocationInit( location ), + new OwnerInit( owner ), + }; + return CreateActor( addToWorld, name, initDict ); + } + + public Actor CreateActor( string name, TypeDictionary initDict ) + { + return CreateActor( true, name, initDict ); + } + + public Actor CreateActor( bool addToWorld, string name, TypeDictionary initDict ) + { + var a = new Actor( this, name, initDict ); + if( addToWorld ) + Add( a ); return a; } diff --git a/OpenRA.Mods.Cnc/ProductionAirdrop.cs b/OpenRA.Mods.Cnc/ProductionAirdrop.cs index e8a55892d3..73dc254070 100644 --- a/OpenRA.Mods.Cnc/ProductionAirdrop.cs +++ b/OpenRA.Mods.Cnc/ProductionAirdrop.cs @@ -43,7 +43,7 @@ namespace OpenRA.Mods.Cnc a.traits.Get().Facing = 64; a.traits.Get().Altitude = a.Info.Traits.Get().CruiseAltitude; - var newUnit = new Actor(self.World, producee.Name, new int2(0, 0), self.Owner); + var newUnit = self.World.CreateActor(false, producee.Name, new int2(0, 0), self.Owner); cargo.Load(a, newUnit); a.CancelActivity(); diff --git a/OpenRA.Mods.RA/Aircraft.cs b/OpenRA.Mods.RA/Aircraft.cs index d847878e7d..60c2f580cc 100755 --- a/OpenRA.Mods.RA/Aircraft.cs +++ b/OpenRA.Mods.RA/Aircraft.cs @@ -41,7 +41,7 @@ namespace OpenRA.Mods.RA public Aircraft( ActorInitializer init , AircraftInfo info) { - this.Location = init.location; + this.Location = init.Get(); Info = info; } diff --git a/OpenRA.Mods.RA/Crate.cs b/OpenRA.Mods.RA/Crate.cs index 1324490c16..ec9bc351a5 100644 --- a/OpenRA.Mods.RA/Crate.cs +++ b/OpenRA.Mods.RA/Crate.cs @@ -49,7 +49,7 @@ namespace OpenRA.Mods.RA public Crate(ActorInitializer init, CrateInfo info) { this.self = init.self; - this.Location = init.location; + this.Location = init.Get(); this.Info = info; self.World.WorldActor.traits.Get().Add(self, this); diff --git a/OpenRA.Mods.RA/CrateDrop.cs b/OpenRA.Mods.RA/CrateDrop.cs index dec5bcdb14..cd855741aa 100644 --- a/OpenRA.Mods.RA/CrateDrop.cs +++ b/OpenRA.Mods.RA/CrateDrop.cs @@ -67,7 +67,7 @@ namespace OpenRA.Mods.RA self.World.AddFrameEndTask(w => { - var crate = new Actor(w, "crate", new int2(0, 0), w.WorldActor.Owner); + var crate = w.CreateActor(false, "crate", new int2(0, 0), w.WorldActor.Owner); crates.Add(crate); var startPos = w.ChooseRandomEdgeCell(); diff --git a/OpenRA.Mods.RA/Crates/GiveUnitCrateAction.cs b/OpenRA.Mods.RA/Crates/GiveUnitCrateAction.cs index f51a78d005..053fc303a3 100644 --- a/OpenRA.Mods.RA/Crates/GiveUnitCrateAction.cs +++ b/OpenRA.Mods.RA/Crates/GiveUnitCrateAction.cs @@ -41,7 +41,7 @@ namespace OpenRA.Mods.RA.Crates var location = ChooseEmptyCellNear(collector); if (location != null) collector.World.AddFrameEndTask( - w => w.Add(new Actor(w, Info.Unit, location.Value, collector.Owner))); + w => w.CreateActor(Info.Unit, location.Value, collector.Owner)); base.Activate(collector); } diff --git a/OpenRA.Mods.RA/Husk.cs b/OpenRA.Mods.RA/Husk.cs index 9a64a2705c..64eff0e1ae 100644 --- a/OpenRA.Mods.RA/Husk.cs +++ b/OpenRA.Mods.RA/Husk.cs @@ -32,7 +32,7 @@ namespace OpenRA.Mods.RA public Husk(ActorInitializer init) { this.self = init.self; - this.location = init.location; + this.location = init.Get(); self.World.WorldActor.traits.Get().Add(self, this); } diff --git a/OpenRA.Mods.RA/Mine.cs b/OpenRA.Mods.RA/Mine.cs index 30f19b7e3f..63ca6588b8 100644 --- a/OpenRA.Mods.RA/Mine.cs +++ b/OpenRA.Mods.RA/Mine.cs @@ -35,7 +35,7 @@ namespace OpenRA.Mods.RA { this.self = init.self; this.info = info; - this.location = init.location; + this.location = init.Get(); self.World.WorldActor.traits.Get().Add(self, this); } diff --git a/OpenRA.Mods.RA/SupportPowers/ParatroopersPower.cs b/OpenRA.Mods.RA/SupportPowers/ParatroopersPower.cs index 5de8cf607d..c924667023 100755 --- a/OpenRA.Mods.RA/SupportPowers/ParatroopersPower.cs +++ b/OpenRA.Mods.RA/SupportPowers/ParatroopersPower.cs @@ -70,7 +70,7 @@ namespace OpenRA.Mods.RA var cargo = a.traits.Get(); foreach (var i in items) - cargo.Load(a, new Actor(owner.World, i.ToLowerInvariant(), + cargo.Load(a, owner.World.CreateActor(false, i.ToLowerInvariant(), new int2(0,0), a.Owner)); }); }