From e792c9ce1764ccf3c577053c6c37767212856260 Mon Sep 17 00:00:00 2001 From: Bob Date: Sun, 17 Jan 2010 12:50:16 +1300 Subject: [PATCH] moved BIM/UIM to World, and added World.CreateActor --- OpenRa.Game/Game.cs | 24 +++++++------------ OpenRa.Game/Graphics/Minimap.cs | 2 +- OpenRa.Game/Orders/UnitOrders.cs | 2 +- OpenRa.Game/Ore.cs | 2 +- OpenRa.Game/PathFinder.cs | 2 +- OpenRa.Game/PathSearch.cs | 6 ++--- OpenRa.Game/Player.cs | 2 +- OpenRa.Game/Traits/AcceptsOre.cs | 3 +-- OpenRa.Game/Traits/Activities/DeployMcv.cs | 2 +- OpenRa.Game/Traits/Activities/Move.cs | 10 ++++---- OpenRa.Game/Traits/Activities/UndeployMcv.cs | 3 +-- OpenRa.Game/Traits/Activities/UnloadCargo.cs | 2 +- OpenRa.Game/Traits/ConstructionYard.cs | 4 ++-- OpenRa.Game/Traits/Mobile.cs | 12 +++++----- OpenRa.Game/Traits/Production.cs | 6 ++--- .../Traits/RenderBuildingWarFactory.cs | 2 +- OpenRa.Game/UiOverlay.cs | 2 +- OpenRa.Game/World.cs | 15 ++++++++++++ OpenRa.Mods.RA/Mine.cs | 2 +- OpenRa.Mods.RA/Minelayer.cs | 4 ++-- 20 files changed, 55 insertions(+), 52 deletions(-) diff --git a/OpenRa.Game/Game.cs b/OpenRa.Game/Game.cs index c9c6e3d920..20568edba2 100644 --- a/OpenRa.Game/Game.cs +++ b/OpenRa.Game/Game.cs @@ -40,8 +40,6 @@ namespace OpenRa viewport.GoToStartLocation(); } } - public static BuildingInfluenceMap BuildingInfluence; - public static UnitInfluenceMap UnitInfluence; public static bool skipMakeAnims = true; @@ -74,9 +72,6 @@ namespace OpenRa palette = new HardwarePalette(renderer, world.Map); - var worldActor = new Actor("World", new int2(int.MaxValue, int.MaxValue), null); - Game.world.Add(worldActor); - for (int i = 0; i < 8; i++) { var race = players.ContainsKey(i) ? players[i].Race : Race.Allies; @@ -92,12 +87,9 @@ namespace OpenRa minimap = new Minimap(renderer); - BuildingInfluence = new BuildingInfluenceMap(); - UnitInfluence = new UnitInfluenceMap(); - skipMakeAnims = true; foreach (var treeReference in Game.world.Map.Trees) - world.Add(new Actor(treeReference.Image, new int2(treeReference.Location), null)); + world.CreateActor(treeReference.Image, new int2(treeReference.Location), null); LoadMapActors(Rules.AllRules); skipMakeAnims = false; @@ -154,8 +146,8 @@ namespace OpenRa //num=owner,type,health,location,facing,... var parts = s.Value.Split( ',' ); var loc = int.Parse(parts[3]); - world.Add(new Actor(parts[1].ToLowerInvariant(), new int2(loc % 128, loc / 128), - players.Values.FirstOrDefault(p => p.InternalName == parts[0]) ?? players[0])); + world.CreateActor(parts[1].ToLowerInvariant(), new int2(loc % 128, loc / 128), + players.Values.FirstOrDefault(p => p.InternalName == parts[0]) ?? players[0]); } } @@ -209,7 +201,7 @@ namespace OpenRa } world.Tick(); - UnitInfluence.Tick(); + Game.world.UnitInfluence.Tick(); foreach (var player in players.Values) player.Tick(); } @@ -248,8 +240,8 @@ namespace OpenRa public static bool IsCellBuildable(int2 a, UnitMovementType umt, Actor toIgnore) { - if (BuildingInfluence.GetBuildingAt(a) != null) return false; - if (UnitInfluence.GetUnitsAt(a).Any(b => b != toIgnore)) return false; + if (Game.world.BuildingInfluence.GetBuildingAt(a) != null) return false; + if (Game.world.UnitInfluence.GetUnitsAt(a).Any(b => b != toIgnore)) return false; return Game.world.Map.IsInMap(a.X, a.Y) && TerrainCosts.Cost(umt, @@ -350,7 +342,7 @@ namespace OpenRa { heuristic = loc => { - var b = Game.BuildingInfluence.GetBuildingAt(loc); + var b = Game.world.BuildingInfluence.GetBuildingAt(loc); if (b != null && b.Owner == p && b.Info.Traits.Get().BaseNormal) return 0; if ((loc - position).Length > maxDistance) return float.PositiveInfinity; /* not quite right */ @@ -416,7 +408,7 @@ namespace OpenRa // todo: spawn more than one unit, in most cases! var sp = ChooseSpawnPoint(available, taken); - world.Add(new Actor("mcv", sp, players[client.Index])); + world.CreateActor("mcv", sp, players[client.Index]); } Game.viewport.GoToStartLocation(); diff --git a/OpenRa.Game/Graphics/Minimap.cs b/OpenRa.Game/Graphics/Minimap.cs index d9e56fdff5..881ad26f50 100644 --- a/OpenRa.Game/Graphics/Minimap.cs +++ b/OpenRa.Game/Graphics/Minimap.cs @@ -91,7 +91,7 @@ namespace OpenRa.Graphics for (var y = 0; y < 128; y++) for (var x = 0; x < 128; x++) { - var b = Game.BuildingInfluence.GetBuildingAt(new int2(x, y)); + var b = Game.world.BuildingInfluence.GetBuildingAt(new int2(x, y)); if (b != null) *(c + (y * bitmapData.Stride >> 2) + x) = (b.Owner != null ? playerColors[(int)b.Owner.Palette] : terrainTypeColors[4]).ToArgb(); diff --git a/OpenRa.Game/Orders/UnitOrders.cs b/OpenRa.Game/Orders/UnitOrders.cs index 452fbf4011..353be5be0b 100644 --- a/OpenRa.Game/Orders/UnitOrders.cs +++ b/OpenRa.Game/Orders/UnitOrders.cs @@ -22,7 +22,7 @@ namespace OpenRa.Orders if( producing == null || producing.Item != order.TargetString || producing.RemainingTime != 0 ) return; - Game.world.Add( new Actor( order.TargetString, order.TargetLocation - Footprint.AdjustForBuildingSize( unit.Traits.Get() ), order.Player ) ); + Game.world.CreateActor( order.TargetString, order.TargetLocation - Footprint.AdjustForBuildingSize( unit.Traits.Get() ), order.Player ); if (order.Player == Game.LocalPlayer) { Sound.Play("placbldg.aud"); diff --git a/OpenRa.Game/Ore.cs b/OpenRa.Game/Ore.cs index 5e406308c3..78eeaa6bd3 100644 --- a/OpenRa.Game/Ore.cs +++ b/OpenRa.Game/Ore.cs @@ -28,7 +28,7 @@ namespace OpenRa public static bool CanSpreadInto(int i, int j) { - if (Game.BuildingInfluence.GetBuildingAt(new int2(i, j)) != null) + if (Game.world.BuildingInfluence.GetBuildingAt(new int2(i, j)) != null) return false; return TerrainCosts.Cost(UnitMovementType.Wheel, diff --git a/OpenRa.Game/PathFinder.cs b/OpenRa.Game/PathFinder.cs index 427f56f392..fa1f1436a4 100644 --- a/OpenRa.Game/PathFinder.cs +++ b/OpenRa.Game/PathFinder.cs @@ -54,7 +54,7 @@ namespace OpenRa return q => p != q && ((p - q).LengthSquared < dist * dist) && - (Game.UnitInfluence.GetUnitsAt(q).Any()); + (Game.world.UnitInfluence.GetUnitsAt(q).Any()); } public List FindPath( PathSearch search ) diff --git a/OpenRa.Game/PathSearch.cs b/OpenRa.Game/PathSearch.cs index fc1055e1bc..4fb5ba11ff 100755 --- a/OpenRa.Game/PathSearch.cs +++ b/OpenRa.Game/PathSearch.cs @@ -56,15 +56,15 @@ namespace OpenRa { if (passableCost[(int)umt][newHere.X, newHere.Y] == float.PositiveInfinity) continue; - if (!Game.BuildingInfluence.CanMoveHere(newHere) && - Game.BuildingInfluence.GetBuildingAt(newHere) != ignoreBuilding) + if (!Game.world.BuildingInfluence.CanMoveHere(newHere) && + Game.world.BuildingInfluence.GetBuildingAt(newHere) != ignoreBuilding) continue; if (Game.world.Map.IsOverlaySolid(newHere)) continue; } // Replicate real-ra behavior of not being able to enter a cell if there is a mixture of crushable and uncrushable units - if (checkForBlocked && (Game.UnitInfluence.GetUnitsAt(newHere).Any(a => !Game.IsActorPathableToCrush(a, umt)))) + if (checkForBlocked && (Game.world.UnitInfluence.GetUnitsAt(newHere).Any(a => !Game.IsActorPathableToCrush(a, umt)))) continue; if (customBlock != null && customBlock(newHere)) diff --git a/OpenRa.Game/Player.cs b/OpenRa.Game/Player.cs index de92176671..ed04322981 100644 --- a/OpenRa.Game/Player.cs +++ b/OpenRa.Game/Player.cs @@ -32,7 +32,7 @@ namespace OpenRa public Player( int index, Session.Client client ) { Shroud = new Shroud(this); - Game.world.Add(this.PlayerActor = new Actor("Player", new int2(int.MaxValue, int.MaxValue), this)); + this.PlayerActor = Game.world.CreateActor("Player", new int2(int.MaxValue, int.MaxValue), this); this.Index = index; this.InternalName = "Multi{0}".F(index); diff --git a/OpenRa.Game/Traits/AcceptsOre.cs b/OpenRa.Game/Traits/AcceptsOre.cs index 1f7ad2eae3..9b77613595 100644 --- a/OpenRa.Game/Traits/AcceptsOre.cs +++ b/OpenRa.Game/Traits/AcceptsOre.cs @@ -14,12 +14,11 @@ namespace OpenRa.Traits Game.world.AddFrameEndTask( w => { /* create the free harvester! */ - var harvester = new Actor("harv", self.Location + new int2(1, 2), self.Owner); + var harvester = w.CreateActor("harv", self.Location + new int2(1, 2), self.Owner); var unit = harvester.traits.Get(); var mobile = harvester.traits.Get(); unit.Facing = 64; harvester.QueueActivity(new Harvest()); - w.Add(harvester); }); } } diff --git a/OpenRa.Game/Traits/Activities/DeployMcv.cs b/OpenRa.Game/Traits/Activities/DeployMcv.cs index e1d6b85f12..c08de8a759 100755 --- a/OpenRa.Game/Traits/Activities/DeployMcv.cs +++ b/OpenRa.Game/Traits/Activities/DeployMcv.cs @@ -17,7 +17,7 @@ namespace OpenRa.Traits.Activities Sound.Play("placbldg.aud"); Sound.Play("build5.aud"); } - Game.world.Add( new Actor( "fact", self.Location - new int2( 1, 1 ), self.Owner ) ); + Game.world.CreateActor( "fact", self.Location - new int2( 1, 1 ), self.Owner ); } ); return this; } diff --git a/OpenRa.Game/Traits/Activities/Move.cs b/OpenRa.Game/Traits/Activities/Move.cs index 2818eedd18..a421cab39f 100755 --- a/OpenRa.Game/Traits/Activities/Move.cs +++ b/OpenRa.Game/Traits/Activities/Move.cs @@ -57,13 +57,13 @@ namespace OpenRa.Traits.Activities bool CanEnterCell( int2 c, Actor self ) { - if (!Game.BuildingInfluence.CanMoveHere(c) - && Game.BuildingInfluence.GetBuildingAt(c) != ignoreBuilding) + if (!Game.world.BuildingInfluence.CanMoveHere(c) + && Game.world.BuildingInfluence.GetBuildingAt(c) != ignoreBuilding) return false; // Cannot enter a cell if any unit inside is uncrushable // This will need to be updated for multiple-infantry-in-a-cell - return (!Game.UnitInfluence.GetUnitsAt(c).Any(a => a != self && !Game.IsActorCrushableByActor(a, self))); + return (!Game.world.UnitInfluence.GetUnitsAt(c).Any(a => a != self && !Game.IsActorCrushableByActor(a, self))); } public IActivity Tick( Actor self ) @@ -144,10 +144,10 @@ namespace OpenRa.Traits.Activities return null; } - Game.UnitInfluence.Remove( self, mobile ); + Game.world.UnitInfluence.Remove( self, mobile ); var newPath = getPath(self, mobile).TakeWhile(a => a != self.Location).ToList(); - Game.UnitInfluence.Add( self, mobile ); + Game.world.UnitInfluence.Add( self, mobile ); if (newPath.Count != 0) path = newPath; diff --git a/OpenRa.Game/Traits/Activities/UndeployMcv.cs b/OpenRa.Game/Traits/Activities/UndeployMcv.cs index fd40863ea2..54f295d59d 100644 --- a/OpenRa.Game/Traits/Activities/UndeployMcv.cs +++ b/OpenRa.Game/Traits/Activities/UndeployMcv.cs @@ -14,9 +14,8 @@ namespace OpenRa.Traits.Activities ns.Sold(self); w.Remove(self); - var mcv = new Actor("mcv", self.Location + new int2(1, 1), self.Owner); + var mcv = w.CreateActor("mcv", self.Location + new int2(1, 1), self.Owner); mcv.traits.Get().Facing = 96; - w.Add(mcv); } public IActivity Tick(Actor self) diff --git a/OpenRa.Game/Traits/Activities/UnloadCargo.cs b/OpenRa.Game/Traits/Activities/UnloadCargo.cs index 1c077e5a71..f123b2e4ef 100644 --- a/OpenRa.Game/Traits/Activities/UnloadCargo.cs +++ b/OpenRa.Game/Traits/Activities/UnloadCargo.cs @@ -13,7 +13,7 @@ namespace OpenRa.Traits.Activities int2? ChooseExitTile(Actor self) { // is anyone still hogging this tile? - if (Game.UnitInfluence.GetUnitsAt(self.Location).Count() > 1) + if (Game.world.UnitInfluence.GetUnitsAt(self.Location).Count() > 1) return null; for (var i = -1; i < 2; i++) diff --git a/OpenRa.Game/Traits/ConstructionYard.cs b/OpenRa.Game/Traits/ConstructionYard.cs index 23ad56f515..d691dfe85d 100644 --- a/OpenRa.Game/Traits/ConstructionYard.cs +++ b/OpenRa.Game/Traits/ConstructionYard.cs @@ -50,10 +50,10 @@ namespace OpenRa.Traits public bool CanEnterCell(int2 a) { - if (!Game.BuildingInfluence.CanMoveHere(a)) return false; + if (!Game.world.BuildingInfluence.CanMoveHere(a)) return false; var crushable = true; - foreach (Actor actor in Game.UnitInfluence.GetUnitsAt(a)) + foreach (Actor actor in Game.world.UnitInfluence.GetUnitsAt(a)) { if (actor == self) continue; diff --git a/OpenRa.Game/Traits/Mobile.cs b/OpenRa.Game/Traits/Mobile.cs index e062c99c87..60b00d295d 100644 --- a/OpenRa.Game/Traits/Mobile.cs +++ b/OpenRa.Game/Traits/Mobile.cs @@ -21,7 +21,7 @@ namespace OpenRa.Traits public int2 fromCell { get { return __fromCell; } - set { Game.UnitInfluence.Remove(self, this); __fromCell = value; Game.UnitInfluence.Add(self, this); } + set { Game.world.UnitInfluence.Remove(self, this); __fromCell = value; Game.world.UnitInfluence.Add(self, this); } } public int2 toCell { @@ -30,11 +30,11 @@ namespace OpenRa.Traits { if (self.Location != value) { - Game.UnitInfluence.Remove(self, this); + Game.world.UnitInfluence.Remove(self, this); self.Location = value; self.Owner.Shroud.Explore(self); } - Game.UnitInfluence.Add(self, this); + Game.world.UnitInfluence.Add(self, this); } } @@ -42,7 +42,7 @@ namespace OpenRa.Traits { this.self = self; __fromCell = toCell; - Game.UnitInfluence.Add(self, this); + Game.world.UnitInfluence.Add(self, this); } public void TeleportTo(Actor self, int2 xy) @@ -90,10 +90,10 @@ namespace OpenRa.Traits public bool CanEnterCell(int2 a) { - if (!Game.BuildingInfluence.CanMoveHere(a)) return false; + if (!Game.world.BuildingInfluence.CanMoveHere(a)) return false; var crushable = true; - foreach (Actor actor in Game.UnitInfluence.GetUnitsAt(a)) + foreach (Actor actor in Game.world.UnitInfluence.GetUnitsAt(a)) { if (actor == self) continue; diff --git a/OpenRa.Game/Traits/Production.cs b/OpenRa.Game/Traits/Production.cs index afeb6ec3ea..30b1514467 100755 --- a/OpenRa.Game/Traits/Production.cs +++ b/OpenRa.Game/Traits/Production.cs @@ -32,10 +32,10 @@ namespace OpenRa.Traits public bool Produce( Actor self, ActorInfo producee ) { var location = CreationLocation( self, producee ); - if( location == null || Game.UnitInfluence.GetUnitsAt( location.Value ).Any() ) + if( location == null || Game.world.UnitInfluence.GetUnitsAt( location.Value ).Any() ) return false; - var newUnit = new Actor( producee.Name, location.Value, self.Owner ); + var newUnit = Game.world.CreateActor( producee.Name, location.Value, self.Owner ); newUnit.traits.Get().Facing = CreationFacing( self, newUnit ); ; var rp = self.traits.GetOrDefault(); @@ -51,8 +51,6 @@ namespace OpenRa.Traits newUnit.CenterLocation = self.CenterLocation + new float2(pi.SpawnOffset[0], pi.SpawnOffset[1]); - Game.world.Add( newUnit ); - foreach (var t in self.traits.WithInterface()) t.UnitProduced(self, newUnit); diff --git a/OpenRa.Game/Traits/RenderBuildingWarFactory.cs b/OpenRa.Game/Traits/RenderBuildingWarFactory.cs index d070926775..ba0b5cd6ed 100644 --- a/OpenRa.Game/Traits/RenderBuildingWarFactory.cs +++ b/OpenRa.Game/Traits/RenderBuildingWarFactory.cs @@ -47,7 +47,7 @@ namespace OpenRa.Traits if (doneBuilding) roof.Tick(); var b = self.GetBounds(false); - if (isOpen && !Game.UnitInfluence.GetUnitsAt(((1/24f) * self.CenterLocation).ToInt2()).Any()) + if (isOpen && !Game.world.UnitInfluence.GetUnitsAt(((1/24f) * self.CenterLocation).ToInt2()).Any()) { isOpen = false; roof.PlayBackwardsThen(GetPrefix(self) + "build-top", () => roof.Play(GetPrefix(self) + "idle-top")); diff --git a/OpenRa.Game/UiOverlay.cs b/OpenRa.Game/UiOverlay.cs index b9b6fb3ed6..4acaf4a805 100644 --- a/OpenRa.Game/UiOverlay.cs +++ b/OpenRa.Game/UiOverlay.cs @@ -39,7 +39,7 @@ namespace OpenRa if (ShowUnitDebug) for (var j = 0; j < 128; j++) for (var i = 0; i < 128; i++) - if (Game.UnitInfluence.GetUnitsAt(new int2(i, j)).Any()) + if (Game.world.UnitInfluence.GetUnitsAt(new int2(i, j)).Any()) spriteRenderer.DrawSprite(unitDebug, Game.CellSize * new float2(i, j), 0); } diff --git a/OpenRa.Game/World.cs b/OpenRa.Game/World.cs index f69fc254be..7c805d8011 100644 --- a/OpenRa.Game/World.cs +++ b/OpenRa.Game/World.cs @@ -12,6 +12,9 @@ namespace OpenRa List effects = new List(); List> frameEndActions = new List>(); + public readonly BuildingInfluenceMap BuildingInfluence; + public readonly UnitInfluenceMap UnitInfluence; + public readonly Map Map; public readonly TileSet TileSet; @@ -20,6 +23,18 @@ namespace OpenRa Map = new Map( Rules.AllRules ); FileSystem.MountTemporary( new Package( Map.Theater + ".mix" ) ); TileSet = new TileSet( Map.TileSuffix ); + + BuildingInfluence = new BuildingInfluenceMap(); + UnitInfluence = new UnitInfluenceMap(); + + CreateActor("World", new int2(int.MaxValue, int.MaxValue), null); + } + + public Actor CreateActor( string name, int2 location, Player owner ) + { + var a = new Actor( name, location, owner ); + Add( a ); + return a; } public void Add(Actor a) diff --git a/OpenRa.Mods.RA/Mine.cs b/OpenRa.Mods.RA/Mine.cs index e9f85bb1cf..87d6e49d3f 100644 --- a/OpenRa.Mods.RA/Mine.cs +++ b/OpenRa.Mods.RA/Mine.cs @@ -21,7 +21,7 @@ namespace OpenRa.Mods.RA public Mine(Actor self) { this.self = self; - Game.UnitInfluence.Add(self, this); + Game.world.UnitInfluence.Add(self, this); } public void OnCrush(Actor crusher) diff --git a/OpenRa.Mods.RA/Minelayer.cs b/OpenRa.Mods.RA/Minelayer.cs index 3280c3f72f..770772ba69 100644 --- a/OpenRa.Mods.RA/Minelayer.cs +++ b/OpenRa.Mods.RA/Minelayer.cs @@ -17,7 +17,7 @@ namespace OpenRa.Mods.RA return null; // Ensure that the cell is empty except for the minelayer - if (Game.UnitInfluence.GetUnitsAt(xy).Any(a => a != self)) + if (Game.world.UnitInfluence.GetUnitsAt(xy).Any(a => a != self)) return null; if (mi.Button == MouseButton.Right && underCursor == self) @@ -37,7 +37,7 @@ namespace OpenRa.Mods.RA // todo: delay a bit? (req making deploy-mine an activity) Game.world.AddFrameEndTask( - w => w.Add(new Actor(self.Info.Traits.Get().Mine, self.Location, self.Owner))); + w => w.CreateActor(self.Info.Traits.Get().Mine, self.Location, self.Owner)); } } }