From c285f1d21043c21bc6c7cc6aa8cb4ca402f94e8b Mon Sep 17 00:00:00 2001 From: Bob Date: Sat, 14 Nov 2009 02:57:52 +1300 Subject: [PATCH] Cleaned up some casts. (BuildingInfo) --- OpenRa.Game/BuildingInfluenceMap.cs | 26 ++++++++++-------- OpenRa.Game/Game.cs | 13 +++++---- OpenRa.Game/GameRules/Footprint.cs | 24 ++++++----------- OpenRa.Game/PlaceBuilding.cs | 15 +++++------ OpenRa.Game/Traits/Activities/DeliverOre.cs | 2 +- OpenRa.Game/Traits/Building.cs | 8 +++--- OpenRa.Game/Traits/McvDeploy.cs | 30 ++++++++++----------- OpenRa.Game/Traits/Mobile.cs | 17 +++++++++--- OpenRa.Game/Traits/RallyPoint.cs | 2 +- OpenRa.Game/Traits/RenderBuilding.cs | 2 +- OpenRa.Game/UiOverlay.cs | 2 +- OpenRa.Game/UnitOrders.cs | 7 ++--- 12 files changed, 76 insertions(+), 72 deletions(-) diff --git a/OpenRa.Game/BuildingInfluenceMap.cs b/OpenRa.Game/BuildingInfluenceMap.cs index aa483f00b9..460fd9bdbd 100644 --- a/OpenRa.Game/BuildingInfluenceMap.cs +++ b/OpenRa.Game/BuildingInfluenceMap.cs @@ -25,14 +25,14 @@ namespace OpenRa.Game influence[i, j] = NoClaim; Game.world.ActorAdded += - a => { if (a.traits.Contains()) AddInfluence(a); }; + a => { if (a.traits.Contains()) AddInfluence(a, a.traits.Get()); }; Game.world.ActorRemoved += - a => { if (a.traits.Contains()) RemoveInfluence(a); }; + a => { if (a.traits.Contains()) RemoveInfluence(a, a.traits.Get()); }; } - void AddInfluence(Actor a) + void AddInfluence(Actor a, Traits.Building building) { - var tiles = Footprint.Tiles(a).ToArray(); + var tiles = Footprint.Tiles(a, building).ToArray(); var min = int2.Max(new int2(0, 0), tiles.Aggregate(int2.Min) - new int2(maxDistance, maxDistance)); var max = int2.Min(new int2(128, 128), @@ -42,7 +42,7 @@ namespace OpenRa.Game var initialTileCount = 0; - foreach (var u in Footprint.UnpathableTiles(a.unitInfo, a.Location)) + foreach (var u in Footprint.UnpathableTiles(building.unitInfo, a.Location)) if (IsValid(u)) blocked[u.X, u.Y] = true; @@ -53,7 +53,7 @@ namespace OpenRa.Game ++initialTileCount; } - if (!((UnitInfo.BuildingInfo)a.unitInfo).BaseNormal) + if (!building.unitInfo.BaseNormal) { while (!pq.Empty) { @@ -101,15 +101,15 @@ namespace OpenRa.Game Log.Write("Finished recalculating region. {0} cells updated.", updatedCells); } - void RemoveInfluence(Actor a) + void RemoveInfluence(Actor a, Traits.Building building) { - var tiles = Footprint.Tiles(a).ToArray(); + var tiles = Footprint.Tiles(a, building).ToArray(); var min = int2.Max(new int2(0, 0), tiles.Aggregate(int2.Min) - new int2(maxDistance, maxDistance)); var max = int2.Min(new int2(128, 128), tiles.Aggregate(int2.Max) + new int2(maxDistance, maxDistance)); - foreach (var u in Footprint.UnpathableTiles(a.unitInfo, a.Location)) + foreach (var u in Footprint.UnpathableTiles(building.unitInfo, a.Location)) if (IsValid(u)) blocked[u.X, u.Y] = false; @@ -130,8 +130,12 @@ namespace OpenRa.Game Log.Write("Finished collecting candidates for evacuated region = {0}", actors.Count); - foreach (var b in actors) - AddInfluence(b); /* we can actually safely constrain this a bit more... */ + foreach( var b in actors ) + { + var bb = a.traits.GetOrDefault(); + if( bb != null ) + AddInfluence( b, bb ); + } } bool IsValid(int2 t) diff --git a/OpenRa.Game/Game.cs b/OpenRa.Game/Game.cs index c7e1d09d61..26bc780818 100644 --- a/OpenRa.Game/Game.cs +++ b/OpenRa.Game/Game.cs @@ -271,7 +271,7 @@ namespace OpenRa.Game static int2? FindAdjacentTile(Actor a, UnitMovementType umt) { - var tiles = Footprint.Tiles(a); + var tiles = Footprint.Tiles(a, a.traits.Get()); var min = tiles.Aggregate(int2.Min) - new int2(1, 1); var max = tiles.Aggregate(int2.Max) + new int2(1, 1); @@ -283,18 +283,17 @@ namespace OpenRa.Game return null; } - public static bool CanPlaceBuilding(string name, int2 xy, Actor toIgnore, bool adjust) + public static bool CanPlaceBuilding(UnitInfo.BuildingInfo building, int2 xy, Actor toIgnore, bool adjust) { - var bi = (UnitInfo.BuildingInfo)Rules.UnitInfo[name]; - return !Footprint.Tiles(bi, xy, adjust).Any( + return !Footprint.Tiles(building, xy, adjust).Any( t => Game.map.ContainsResource(t) || !Game.IsCellBuildable(t, - bi.WaterBound ? UnitMovementType.Float : UnitMovementType.Wheel, + building.WaterBound ? UnitMovementType.Float : UnitMovementType.Wheel, toIgnore)); } - public static bool CanPlaceBuilding(string name, int2 xy, bool adjust) + public static bool CanPlaceBuilding( UnitInfo.BuildingInfo building, int2 xy, bool adjust ) { - return CanPlaceBuilding(name, xy, null, adjust); + return CanPlaceBuilding(building, xy, null, adjust); } public static void BuildUnit(Player player, string name) diff --git a/OpenRa.Game/GameRules/Footprint.cs b/OpenRa.Game/GameRules/Footprint.cs index 47ae6fd693..8842af3d2c 100644 --- a/OpenRa.Game/GameRules/Footprint.cs +++ b/OpenRa.Game/GameRules/Footprint.cs @@ -9,14 +9,13 @@ namespace OpenRa.Game.GameRules { static class Footprint { - public static IEnumerable Tiles( UnitInfo unitInfo, int2 position ) + public static IEnumerable Tiles( UnitInfo.BuildingInfo buildingInfo, int2 position ) { - return Tiles(unitInfo, position, true); + return Tiles(buildingInfo, position, true); } - public static IEnumerable Tiles(UnitInfo unitInfo, int2 position, bool adjustForPlacement) + public static IEnumerable Tiles( UnitInfo.BuildingInfo buildingInfo, int2 position, bool adjustForPlacement ) { - var buildingInfo = unitInfo as UnitInfo.BuildingInfo; var dim = buildingInfo.Dimensions; var footprint = buildingInfo.Footprint.Where(x => !char.IsWhiteSpace(x)); @@ -28,21 +27,19 @@ namespace OpenRa.Game.GameRules var adjustment = adjustForPlacement ? AdjustForBuildingSize(buildingInfo) : int2.Zero; - var tiles = TilesWhere(unitInfo.Name, dim, footprint.ToArray(), a => a != '_'); + var tiles = TilesWhere(buildingInfo.Name, dim, footprint.ToArray(), a => a != '_'); return tiles.Select(t => t + position - adjustment); } - public static IEnumerable Tiles(Actor a) + public static IEnumerable Tiles(Actor a, Traits.Building building) { - return Tiles(a.unitInfo, a.Location, false); + return Tiles( building.unitInfo, a.Location, false ); } - public static IEnumerable UnpathableTiles( UnitInfo unitInfo, int2 position ) + public static IEnumerable UnpathableTiles( UnitInfo.BuildingInfo buildingInfo, int2 position ) { - var buildingInfo = unitInfo as UnitInfo.BuildingInfo; - var footprint = buildingInfo.Footprint.Where( x => !char.IsWhiteSpace( x ) ).ToArray(); - foreach( var tile in TilesWhere( unitInfo.Name, buildingInfo.Dimensions, footprint, a => a == 'x' ) ) + foreach( var tile in TilesWhere( buildingInfo.Name, buildingInfo.Dimensions, footprint, a => a == 'x' ) ) yield return tile + position; } @@ -58,11 +55,6 @@ namespace OpenRa.Game.GameRules yield return new int2( x, y ); } - public static int2 AdjustForBuildingSize( string name ) - { - return AdjustForBuildingSize( Rules.UnitInfo[ name ] as UnitInfo.BuildingInfo ); - } - public static int2 AdjustForBuildingSize( UnitInfo.BuildingInfo unitInfo ) { var dim = unitInfo.Dimensions; diff --git a/OpenRa.Game/PlaceBuilding.cs b/OpenRa.Game/PlaceBuilding.cs index 53d53adf3c..bd55f2716d 100644 --- a/OpenRa.Game/PlaceBuilding.cs +++ b/OpenRa.Game/PlaceBuilding.cs @@ -9,28 +9,27 @@ namespace OpenRa.Game class PlaceBuilding : IOrderGenerator { public readonly Player Owner; - public readonly string Name; + public readonly UnitInfo.BuildingInfo Building; public PlaceBuilding(Player owner, string name) { Owner = owner; - Name = name; + Building = (UnitInfo.BuildingInfo)Rules.UnitInfo[ name ]; } public IEnumerable Order(int2 xy, bool lmb) { if( lmb ) { - if (!Game.CanPlaceBuilding(Name, xy, true)) + if( !Game.CanPlaceBuilding( Building, xy, true ) ) yield break; - var bi = (UnitInfo.BuildingInfo)Rules.UnitInfo[Name]; - var maxDistance = bi.Adjacent + 2; /* real-ra is weird. this is 1 GAP. */ - if( !Footprint.Tiles( bi, xy ).Any( + var maxDistance = Building.Adjacent + 2; /* real-ra is weird. this is 1 GAP. */ + if( !Footprint.Tiles( Building, xy ).Any( t => Game.GetDistanceToBase( t, Owner ) < maxDistance ) ) yield break; - yield return OpenRa.Game.Order.PlaceBuilding( Owner, xy, Name ); + yield return OpenRa.Game.Order.PlaceBuilding( Owner, xy, Building.Name ); } else // rmb { @@ -41,7 +40,7 @@ namespace OpenRa.Game public void Tick() { var producing = Owner.Producing( "Building" ); - if( producing == null || producing.Item != Name || producing.RemainingTime != 0 ) + if( producing == null || producing.Item != Building.Name || producing.RemainingTime != 0 ) Game.world.AddFrameEndTask( _ => { Game.controller.orderGenerator = null; } ); } } diff --git a/OpenRa.Game/Traits/Activities/DeliverOre.cs b/OpenRa.Game/Traits/Activities/DeliverOre.cs index af8837acfd..26c45e227a 100644 --- a/OpenRa.Game/Traits/Activities/DeliverOre.cs +++ b/OpenRa.Game/Traits/Activities/DeliverOre.cs @@ -42,7 +42,7 @@ namespace OpenRa.Game.Traits.Activities umt = mobile.GetMovementType(), checkForBlocked = false, }; - var refineries = Game.world.Actors.Where( x => x.unitInfo == Rules.UnitInfo[ "proc" ] ).ToList(); + var refineries = Game.world.Actors.Where( x => x.unitInfo.Name == "proc" ).ToList(); if( refinery != null ) search.AddInitialCell( refinery.Location + refineryDeliverOffset ); else diff --git a/OpenRa.Game/Traits/Building.cs b/OpenRa.Game/Traits/Building.cs index 0894e00baf..d6585296f8 100644 --- a/OpenRa.Game/Traits/Building.cs +++ b/OpenRa.Game/Traits/Building.cs @@ -8,8 +8,11 @@ namespace OpenRa.Game.Traits { class Building : ITick, INotifyBuildComplete { + public readonly UnitInfo.BuildingInfo unitInfo; + public Building(Actor self) { + unitInfo = (UnitInfo.BuildingInfo)self.unitInfo; } bool first = true; @@ -23,10 +26,7 @@ namespace OpenRa.Game.Traits public void BuildingComplete(Actor self) { - UnitInfo.BuildingInfo bi = self.unitInfo as UnitInfo.BuildingInfo; - if (bi == null) return; - - self.Owner.ChangePower(bi.Power); + self.Owner.ChangePower(unitInfo.Power); } } } diff --git a/OpenRa.Game/Traits/McvDeploy.cs b/OpenRa.Game/Traits/McvDeploy.cs index 875ab743a5..06a2866b98 100644 --- a/OpenRa.Game/Traits/McvDeploy.cs +++ b/OpenRa.Game/Traits/McvDeploy.cs @@ -1,23 +1,23 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using System; +using System.Collections.Generic; +using System.Linq; using System.Text; -using OpenRa.Game.GameRules; - -namespace OpenRa.Game.Traits -{ - class McvDeploy : IOrder +using OpenRa.Game.GameRules; + +namespace OpenRa.Game.Traits +{ + class McvDeploy : IOrder { - public McvDeploy(Actor self) { } + public McvDeploy(Actor self) { } public Order Order(Actor self, int2 xy, bool lmb, Actor underCursor) { if (lmb) return null; - if (xy == self.Location) - return OpenRa.Game.Order.DeployMcv(self, !Game.CanPlaceBuilding("fact", xy - new int2(1,1), self, false)); + if( xy != self.Location ) return null; - return null; - } - } -} + var factBuildingInfo = (UnitInfo.BuildingInfo)Rules.UnitInfo[ "fact" ]; + return OpenRa.Game.Order.DeployMcv(self, !Game.CanPlaceBuilding(factBuildingInfo, xy - new int2(1,1), self, false)); + } + } +} diff --git a/OpenRa.Game/Traits/Mobile.cs b/OpenRa.Game/Traits/Mobile.cs index f31410a6e8..3bddabd6ea 100644 --- a/OpenRa.Game/Traits/Mobile.cs +++ b/OpenRa.Game/Traits/Mobile.cs @@ -84,10 +84,19 @@ namespace OpenRa.Game.Traits public UnitMovementType GetMovementType() { - var vi = self.unitInfo as UnitInfo.VehicleInfo; - if (vi == null) return UnitMovementType.Foot; - if (vi.WaterBound) return UnitMovementType.Float; - return vi.Tracked ? UnitMovementType.Track : UnitMovementType.Wheel; + switch( Rules.UnitCategory[ self.unitInfo.Name ] ) + { + case "Infantry": + return UnitMovementType.Foot; + case "Vehicle": + return ( self.unitInfo as UnitInfo.VehicleInfo ).Tracked ? UnitMovementType.Track : UnitMovementType.Wheel; + case "Ship": + return UnitMovementType.Float; + case "Plane": + return UnitMovementType.Track; // FIXME: remove this when planes actually fly. + default: + throw new InvalidOperationException( "GetMovementType on unit that shouldn't be aable to move." ); + } } public IEnumerable GetCurrentPath() diff --git a/OpenRa.Game/Traits/RallyPoint.cs b/OpenRa.Game/Traits/RallyPoint.cs index c2527f29b9..a5dce9541d 100644 --- a/OpenRa.Game/Traits/RallyPoint.cs +++ b/OpenRa.Game/Traits/RallyPoint.cs @@ -15,7 +15,7 @@ namespace OpenRa.Game.Traits public RallyPoint(Actor self) { - var bi = (UnitInfo.BuildingInfo)self.unitInfo; + var bi = self.traits.Get().unitInfo; rallyPoint = self.Location + new int2(bi.RallyPoint[0], bi.RallyPoint[1]); anim = new Animation("flagfly"); anim.PlayRepeating("idle"); diff --git a/OpenRa.Game/Traits/RenderBuilding.cs b/OpenRa.Game/Traits/RenderBuilding.cs index b8f46e28b6..3c0e449a16 100644 --- a/OpenRa.Game/Traits/RenderBuilding.cs +++ b/OpenRa.Game/Traits/RenderBuilding.cs @@ -43,7 +43,7 @@ namespace OpenRa.Game.Traits void DoBib(Actor self, bool isRemove) { - var buildingInfo = (UnitInfo.BuildingInfo)self.unitInfo; + var buildingInfo = self.traits.Get().unitInfo; if (buildingInfo.Bib) { var size = buildingInfo.Dimensions.X; diff --git a/OpenRa.Game/UiOverlay.cs b/OpenRa.Game/UiOverlay.cs index d7e5f7be2b..b672e0701c 100644 --- a/OpenRa.Game/UiOverlay.cs +++ b/OpenRa.Game/UiOverlay.cs @@ -47,7 +47,7 @@ namespace OpenRa.Game var position = Game.controller.MousePosition.ToInt2(); - var bi = (UnitInfo.BuildingInfo)Rules.UnitInfo[placeBuilding.Name]; + var bi = placeBuilding.Building; var maxDistance = bi.Adjacent + 2; /* real-ra is weird. this is 1 GAP. */ diff --git a/OpenRa.Game/UnitOrders.cs b/OpenRa.Game/UnitOrders.cs index b46bd07844..86734dff3c 100755 --- a/OpenRa.Game/UnitOrders.cs +++ b/OpenRa.Game/UnitOrders.cs @@ -49,7 +49,8 @@ namespace OpenRa.Game } case "DeployMcv": { - if (!Game.CanPlaceBuilding("fact", order.Subject.Location - new int2(1,1), order.Subject, false)) + var factBuildingInfo = (UnitInfo.BuildingInfo)Rules.UnitInfo[ "fact" ]; + if( !Game.CanPlaceBuilding( factBuildingInfo, order.Subject.Location - new int2( 1, 1 ), order.Subject, false ) ) break; /* throw the order on the floor */ var mobile = order.Subject.traits.Get(); @@ -77,14 +78,14 @@ namespace OpenRa.Game { Game.world.AddFrameEndTask( _ => { - var building = Rules.UnitInfo[ order.TargetString ]; + var building = (UnitInfo.BuildingInfo)Rules.UnitInfo[ order.TargetString ]; var producing = order.Player.Producing( "Building" ); if( producing == null || producing.Item != order.TargetString || producing.RemainingTime != 0 ) return; Log.Write( "Player \"{0}\" builds {1}", order.Player.PlayerName, building.Name ); - Game.world.Add( new Actor( building.Name, order.TargetLocation - GameRules.Footprint.AdjustForBuildingSize( building.Name ), order.Player ) ); + Game.world.Add( new Actor( building.Name, order.TargetLocation - GameRules.Footprint.AdjustForBuildingSize( building ), order.Player ) ); order.Player.FinishProduction(Rules.UnitCategory[building.Name]); } );