From 4a004eda0f1516e3f6532a5cee3bb87828cc8419 Mon Sep 17 00:00:00 2001 From: Bob Date: Tue, 12 Jan 2010 20:18:36 +1300 Subject: [PATCH] fixed Building.unitInfo --- OpenRa.Game/BuildingInfluenceMap.cs | 14 +++++++------- OpenRa.Game/Game.cs | 8 ++++---- OpenRa.Game/GameRules/Footprint.cs | 19 ++++++++++--------- .../Orders/PlaceBuildingOrderGenerator.cs | 18 ++++++++++-------- OpenRa.Game/Orders/UnitOrderGenerator.cs | 4 ++-- OpenRa.Game/Orders/UnitOrders.cs | 2 +- OpenRa.Game/Traits/Building.cs | 2 -- OpenRa.Game/Traits/McvDeploy.cs | 4 ++-- OpenRa.Game/UiOverlay.cs | 7 ++++--- 9 files changed, 40 insertions(+), 38 deletions(-) diff --git a/OpenRa.Game/BuildingInfluenceMap.cs b/OpenRa.Game/BuildingInfluenceMap.cs index 636a75bdd9..4d8f7bfb54 100644 --- a/OpenRa.Game/BuildingInfluenceMap.cs +++ b/OpenRa.Game/BuildingInfluenceMap.cs @@ -18,15 +18,15 @@ namespace OpenRa.Game ChangeInfluence(a, a.traits.Get(), false); }; } - void ChangeInfluence(Actor a, Building building, bool isAdd) + void ChangeInfluence( Actor a, Building building, bool isAdd ) { - foreach (var u in Footprint.UnpathableTiles(building.unitInfo, a.Location)) - if (IsValid(u)) - blocked[u.X, u.Y] = isAdd; + foreach( var u in Footprint.UnpathableTiles( a.Info.Name, a.Info.Traits.Get(), a.Location ) ) + if( IsValid( u ) ) + blocked[ u.X, u.Y ] = isAdd; - foreach (var u in Footprint.Tiles(building.unitInfo, a.Location, false)) - if (IsValid(u)) - influence[u.X, u.Y] = isAdd ? a : null; + foreach( var u in Footprint.Tiles( a.Info.Name, a.Info.Traits.Get(), a.Location, false ) ) + if( IsValid( u ) ) + influence[ u.X, u.Y ] = isAdd ? a : null; } bool IsValid(int2 t) diff --git a/OpenRa.Game/Game.cs b/OpenRa.Game/Game.cs index 0fa9eaf020..3f052a1f1a 100644 --- a/OpenRa.Game/Game.cs +++ b/OpenRa.Game/Game.cs @@ -320,15 +320,15 @@ namespace OpenRa.Game public static Random SharedRandom = new Random(0); /* for things that require sync */ public static Random CosmeticRandom = new Random(); /* for things that are just fluff */ - public static bool CanPlaceBuilding(LegacyBuildingInfo building, int2 xy, Actor toIgnore, bool adjust) + public static bool CanPlaceBuilding(string name, BuildingInfo building, int2 xy, Actor toIgnore, bool adjust) { - return !Footprint.Tiles(building, xy, adjust).Any( + return !Footprint.Tiles(name, building, xy, adjust).Any( t => !Rules.Map.IsInMap(t.X, t.Y) || Rules.Map.ContainsResource(t) || !Game.IsCellBuildable(t, building.WaterBound ? UnitMovementType.Float : UnitMovementType.Wheel, toIgnore)); } - public static bool IsCloseEnoughToBase(Player p, LegacyBuildingInfo bi, int2 position) + public static bool IsCloseEnoughToBase(Player p, string buildingName, BuildingInfo bi, int2 position) { var maxDistance = bi.Adjacent + 1; @@ -346,7 +346,7 @@ namespace OpenRa.Game ignoreTerrain = true, }; - foreach (var t in Footprint.Tiles(bi, position)) search.AddInitialCell(t); + foreach (var t in Footprint.Tiles(buildingName, bi, position)) search.AddInitialCell(t); return Game.PathFinder.FindPath(search).Count != 0; } diff --git a/OpenRa.Game/GameRules/Footprint.cs b/OpenRa.Game/GameRules/Footprint.cs index 3d740fd1d2..63d1306ab8 100644 --- a/OpenRa.Game/GameRules/Footprint.cs +++ b/OpenRa.Game/GameRules/Footprint.cs @@ -1,17 +1,18 @@ using System; using System.Collections.Generic; using System.Linq; +using OpenRa.Game.Traits; namespace OpenRa.Game.GameRules { static class Footprint { - public static IEnumerable Tiles( LegacyBuildingInfo buildingInfo, int2 position ) + public static IEnumerable Tiles( string name, BuildingInfo buildingInfo, int2 position ) { - return Tiles(buildingInfo, position, true); + return Tiles(name, buildingInfo, position, true); } - public static IEnumerable Tiles( LegacyBuildingInfo buildingInfo, int2 position, bool adjustForPlacement ) + public static IEnumerable Tiles( string name, BuildingInfo buildingInfo, int2 position, bool adjustForPlacement ) { var dim = buildingInfo.Dimensions; @@ -24,19 +25,19 @@ namespace OpenRa.Game.GameRules var adjustment = adjustForPlacement ? AdjustForBuildingSize(buildingInfo) : int2.Zero; - var tiles = TilesWhere(buildingInfo.Name, dim, footprint.ToArray(), a => a != '_'); + var tiles = TilesWhere(name, dim, footprint.ToArray(), a => a != '_'); return tiles.Select(t => t + position - adjustment); } public static IEnumerable Tiles(Actor a, Traits.Building building) { - return Tiles( building.unitInfo, a.Location, false ); + return Tiles( a.Info.Name, a.Info.Traits.Get(), a.Location, false ); } - public static IEnumerable UnpathableTiles( LegacyBuildingInfo buildingInfo, int2 position ) + public static IEnumerable UnpathableTiles( string name, BuildingInfo buildingInfo, int2 position ) { var footprint = buildingInfo.Footprint.Where( x => !char.IsWhiteSpace( x ) ).ToArray(); - foreach( var tile in TilesWhere( buildingInfo.Name, buildingInfo.Dimensions, footprint, a => a == 'x' ) ) + foreach( var tile in TilesWhere( name, buildingInfo.Dimensions, footprint, a => a == 'x' ) ) yield return tile + position; } @@ -52,9 +53,9 @@ namespace OpenRa.Game.GameRules yield return new int2( x, y ); } - public static int2 AdjustForBuildingSize( LegacyBuildingInfo unitInfo ) + public static int2 AdjustForBuildingSize( BuildingInfo buildingInfo ) { - var dim = unitInfo.Dimensions; + var dim = buildingInfo.Dimensions; return new int2( dim.X / 2, dim.Y > 1 ? ( dim.Y + 1 ) / 2 : 0 ); } } diff --git a/OpenRa.Game/Orders/PlaceBuildingOrderGenerator.cs b/OpenRa.Game/Orders/PlaceBuildingOrderGenerator.cs index 193f5872d0..1377bbc5ec 100644 --- a/OpenRa.Game/Orders/PlaceBuildingOrderGenerator.cs +++ b/OpenRa.Game/Orders/PlaceBuildingOrderGenerator.cs @@ -1,17 +1,19 @@ using System.Collections.Generic; using OpenRa.Game.GameRules; +using OpenRa.Game.Traits; namespace OpenRa.Game.Orders { class PlaceBuildingOrderGenerator : IOrderGenerator { readonly Actor Producer; - readonly LegacyBuildingInfo Building; + readonly string Building; + BuildingInfo BuildingInfo { get { return Rules.NewUnitInfo[ Building ].Traits.Get(); } } public PlaceBuildingOrderGenerator(Actor producer, string name) { Producer = producer; - Building = (LegacyBuildingInfo)Rules.UnitInfo[ name ]; + Building = name; } public IEnumerable Order(int2 xy, MouseInput mi) @@ -26,27 +28,27 @@ namespace OpenRa.Game.Orders { if (mi.Button == MouseButton.Left) { - if (!Game.CanPlaceBuilding(Building, xy, null, true) - || !Game.IsCloseEnoughToBase(Producer.Owner, Building, xy)) + if (!Game.CanPlaceBuilding( Building, BuildingInfo, xy, null, true) + || !Game.IsCloseEnoughToBase(Producer.Owner, Building, BuildingInfo, xy)) { Sound.Play("nodeply1.aud"); yield break; } - yield return new Order("PlaceBuilding", Producer.Owner.PlayerActor, null, xy, Building.Name); + yield return new Order("PlaceBuilding", Producer.Owner.PlayerActor, null, xy, Building); } } public void Tick() { - var producing = Producer.traits.Get().CurrentItem( Rules.UnitCategory[ Building.Name ] ); - if (producing == null || producing.Item != Building.Name || producing.RemainingTime != 0) + var producing = Producer.traits.Get().CurrentItem( Rules.UnitCategory[ Building ] ); + if (producing == null || producing.Item != Building || producing.RemainingTime != 0) Game.controller.CancelInputMode(); } public void Render() { - Game.worldRenderer.uiOverlay.DrawBuildingGrid( Building ); + Game.worldRenderer.uiOverlay.DrawBuildingGrid( Building, BuildingInfo ); } public Cursor GetCursor(int2 xy, MouseInput mi) diff --git a/OpenRa.Game/Orders/UnitOrderGenerator.cs b/OpenRa.Game/Orders/UnitOrderGenerator.cs index a3455ae861..a7800482f7 100644 --- a/OpenRa.Game/Orders/UnitOrderGenerator.cs +++ b/OpenRa.Game/Orders/UnitOrderGenerator.cs @@ -69,8 +69,8 @@ namespace OpenRa.Game.Orders else return Cursor.MoveBlocked; case "DeployMcv": - var factBuildingInfo = (LegacyBuildingInfo)Rules.UnitInfo["fact"]; - if (Game.CanPlaceBuilding(factBuildingInfo, a.Location - new int2(1, 1), a, false)) + var factBuildingInfo = Rules.NewUnitInfo["fact"].Traits.Get(); + if (Game.CanPlaceBuilding("fact", factBuildingInfo, a.Location - new int2(1, 1), a, false)) return Cursor.Deploy; else return Cursor.DeployBlocked; diff --git a/OpenRa.Game/Orders/UnitOrders.cs b/OpenRa.Game/Orders/UnitOrders.cs index 327f1e3a05..177a48386b 100644 --- a/OpenRa.Game/Orders/UnitOrders.cs +++ b/OpenRa.Game/Orders/UnitOrders.cs @@ -20,7 +20,7 @@ namespace OpenRa.Game.Orders if( producing == null || producing.Item != order.TargetString || producing.RemainingTime != 0 ) return; - Game.world.Add( new Actor( order.TargetString, order.TargetLocation - Footprint.AdjustForBuildingSize( (LegacyBuildingInfo)Rules.UnitInfo[ order.TargetString ] ), order.Player ) ); + Game.world.Add( new Actor( order.TargetString, order.TargetLocation - Footprint.AdjustForBuildingSize( Rules.NewUnitInfo[ order.TargetString ].Traits.Get() ), order.Player ) ); if (order.Player == Game.LocalPlayer) { Sound.Play("placbldg.aud"); diff --git a/OpenRa.Game/Traits/Building.cs b/OpenRa.Game/Traits/Building.cs index b4816bb971..f80deff35e 100644 --- a/OpenRa.Game/Traits/Building.cs +++ b/OpenRa.Game/Traits/Building.cs @@ -39,7 +39,6 @@ namespace OpenRa.Game.Traits class Building : INotifyDamage, IResolveOrder, ITick { readonly Actor self; - [Obsolete] public readonly LegacyBuildingInfo unitInfo; public readonly BuildingInfo Info; [Sync] bool isRepairing = false; @@ -53,7 +52,6 @@ namespace OpenRa.Game.Traits { this.self = self; Info = self.Info.Traits.Get(); - unitInfo = (LegacyBuildingInfo)self.LegacyInfo; self.CenterLocation = Game.CellSize * ((float2)self.Location + .5f * (float2)Info.Dimensions); } diff --git a/OpenRa.Game/Traits/McvDeploy.cs b/OpenRa.Game/Traits/McvDeploy.cs index 20221549a3..ac6c0ce924 100644 --- a/OpenRa.Game/Traits/McvDeploy.cs +++ b/OpenRa.Game/Traits/McvDeploy.cs @@ -24,8 +24,8 @@ namespace OpenRa.Game.Traits { if( order.OrderString == "DeployMcv" ) { - var factBuildingInfo = (LegacyBuildingInfo)Rules.UnitInfo[ "fact" ]; - if( Game.CanPlaceBuilding( factBuildingInfo, self.Location - new int2( 1, 1 ), self, false ) ) + var factBuildingInfo = Rules.NewUnitInfo[ "fact" ].Traits.Get(); + if( Game.CanPlaceBuilding( "fact", factBuildingInfo, self.Location - new int2( 1, 1 ), self, false ) ) { self.CancelActivity(); self.QueueActivity( new Turn( 96 ) ); diff --git a/OpenRa.Game/UiOverlay.cs b/OpenRa.Game/UiOverlay.cs index 7a0e4c8c29..4c60fd59d3 100644 --- a/OpenRa.Game/UiOverlay.cs +++ b/OpenRa.Game/UiOverlay.cs @@ -2,6 +2,7 @@ using System.Drawing; using System.Linq; using OpenRa.Game.GameRules; using OpenRa.Game.Graphics; +using OpenRa.Game.Traits; namespace OpenRa.Game { @@ -42,12 +43,12 @@ namespace OpenRa.Game spriteRenderer.DrawSprite(unitDebug, Game.CellSize * new float2(i, j), 0); } - public void DrawBuildingGrid( LegacyBuildingInfo bi ) + public void DrawBuildingGrid( string name, BuildingInfo bi ) { var position = Game.controller.MousePosition.ToInt2(); - var isCloseEnough = Game.IsCloseEnoughToBase(Game.LocalPlayer, bi, position); + var isCloseEnough = Game.IsCloseEnoughToBase(Game.LocalPlayer, name, bi, position); - foreach( var t in Footprint.Tiles( bi, position ) ) + foreach( var t in Footprint.Tiles( name, bi, position ) ) spriteRenderer.DrawSprite( ( isCloseEnough && Game.IsCellBuildable( t, bi.WaterBound ? UnitMovementType.Float : UnitMovementType.Wheel ) && !Rules.Map.ContainsResource( t ) ) ? buildOk : buildBlocked, Game.CellSize * t, 0 );