fixed Building.unitInfo

This commit is contained in:
Bob
2010-01-12 20:18:36 +13:00
parent d5d6812e07
commit 4a004eda0f
9 changed files with 40 additions and 38 deletions

View File

@@ -18,15 +18,15 @@ namespace OpenRa.Game
ChangeInfluence(a, a.traits.Get<Building>(), false); }; ChangeInfluence(a, a.traits.Get<Building>(), 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)) foreach( var u in Footprint.UnpathableTiles( a.Info.Name, a.Info.Traits.Get<BuildingInfo>(), a.Location ) )
if (IsValid(u)) if( IsValid( u ) )
blocked[u.X, u.Y] = isAdd; blocked[ u.X, u.Y ] = isAdd;
foreach (var u in Footprint.Tiles(building.unitInfo, a.Location, false)) foreach( var u in Footprint.Tiles( a.Info.Name, a.Info.Traits.Get<BuildingInfo>(), a.Location, false ) )
if (IsValid(u)) if( IsValid( u ) )
influence[u.X, u.Y] = isAdd ? a : null; influence[ u.X, u.Y ] = isAdd ? a : null;
} }
bool IsValid(int2 t) bool IsValid(int2 t)

View File

@@ -320,15 +320,15 @@ namespace OpenRa.Game
public static Random SharedRandom = new Random(0); /* for things that require sync */ 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 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, t => !Rules.Map.IsInMap(t.X, t.Y) || Rules.Map.ContainsResource(t) || !Game.IsCellBuildable(t,
building.WaterBound ? UnitMovementType.Float : UnitMovementType.Wheel, building.WaterBound ? UnitMovementType.Float : UnitMovementType.Wheel,
toIgnore)); 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; var maxDistance = bi.Adjacent + 1;
@@ -346,7 +346,7 @@ namespace OpenRa.Game
ignoreTerrain = true, 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; return Game.PathFinder.FindPath(search).Count != 0;
} }

View File

@@ -1,17 +1,18 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using OpenRa.Game.Traits;
namespace OpenRa.Game.GameRules namespace OpenRa.Game.GameRules
{ {
static class Footprint static class Footprint
{ {
public static IEnumerable<int2> Tiles( LegacyBuildingInfo buildingInfo, int2 position ) public static IEnumerable<int2> Tiles( string name, BuildingInfo buildingInfo, int2 position )
{ {
return Tiles(buildingInfo, position, true); return Tiles(name, buildingInfo, position, true);
} }
public static IEnumerable<int2> Tiles( LegacyBuildingInfo buildingInfo, int2 position, bool adjustForPlacement ) public static IEnumerable<int2> Tiles( string name, BuildingInfo buildingInfo, int2 position, bool adjustForPlacement )
{ {
var dim = buildingInfo.Dimensions; var dim = buildingInfo.Dimensions;
@@ -24,19 +25,19 @@ namespace OpenRa.Game.GameRules
var adjustment = adjustForPlacement ? AdjustForBuildingSize(buildingInfo) : int2.Zero; 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); return tiles.Select(t => t + position - adjustment);
} }
public static IEnumerable<int2> Tiles(Actor a, Traits.Building building) public static IEnumerable<int2> Tiles(Actor a, Traits.Building building)
{ {
return Tiles( building.unitInfo, a.Location, false ); return Tiles( a.Info.Name, a.Info.Traits.Get<BuildingInfo>(), a.Location, false );
} }
public static IEnumerable<int2> UnpathableTiles( LegacyBuildingInfo buildingInfo, int2 position ) public static IEnumerable<int2> UnpathableTiles( string name, BuildingInfo buildingInfo, int2 position )
{ {
var footprint = buildingInfo.Footprint.Where( x => !char.IsWhiteSpace( x ) ).ToArray(); 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; yield return tile + position;
} }
@@ -52,9 +53,9 @@ namespace OpenRa.Game.GameRules
yield return new int2( x, y ); 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 ); return new int2( dim.X / 2, dim.Y > 1 ? ( dim.Y + 1 ) / 2 : 0 );
} }
} }

View File

@@ -1,17 +1,19 @@
using System.Collections.Generic; using System.Collections.Generic;
using OpenRa.Game.GameRules; using OpenRa.Game.GameRules;
using OpenRa.Game.Traits;
namespace OpenRa.Game.Orders namespace OpenRa.Game.Orders
{ {
class PlaceBuildingOrderGenerator : IOrderGenerator class PlaceBuildingOrderGenerator : IOrderGenerator
{ {
readonly Actor Producer; readonly Actor Producer;
readonly LegacyBuildingInfo Building; readonly string Building;
BuildingInfo BuildingInfo { get { return Rules.NewUnitInfo[ Building ].Traits.Get<BuildingInfo>(); } }
public PlaceBuildingOrderGenerator(Actor producer, string name) public PlaceBuildingOrderGenerator(Actor producer, string name)
{ {
Producer = producer; Producer = producer;
Building = (LegacyBuildingInfo)Rules.UnitInfo[ name ]; Building = name;
} }
public IEnumerable<Order> Order(int2 xy, MouseInput mi) public IEnumerable<Order> Order(int2 xy, MouseInput mi)
@@ -26,27 +28,27 @@ namespace OpenRa.Game.Orders
{ {
if (mi.Button == MouseButton.Left) if (mi.Button == MouseButton.Left)
{ {
if (!Game.CanPlaceBuilding(Building, xy, null, true) if (!Game.CanPlaceBuilding( Building, BuildingInfo, xy, null, true)
|| !Game.IsCloseEnoughToBase(Producer.Owner, Building, xy)) || !Game.IsCloseEnoughToBase(Producer.Owner, Building, BuildingInfo, xy))
{ {
Sound.Play("nodeply1.aud"); Sound.Play("nodeply1.aud");
yield break; 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() public void Tick()
{ {
var producing = Producer.traits.Get<Traits.ProductionQueue>().CurrentItem( Rules.UnitCategory[ Building.Name ] ); var producing = Producer.traits.Get<Traits.ProductionQueue>().CurrentItem( Rules.UnitCategory[ Building ] );
if (producing == null || producing.Item != Building.Name || producing.RemainingTime != 0) if (producing == null || producing.Item != Building || producing.RemainingTime != 0)
Game.controller.CancelInputMode(); Game.controller.CancelInputMode();
} }
public void Render() public void Render()
{ {
Game.worldRenderer.uiOverlay.DrawBuildingGrid( Building ); Game.worldRenderer.uiOverlay.DrawBuildingGrid( Building, BuildingInfo );
} }
public Cursor GetCursor(int2 xy, MouseInput mi) public Cursor GetCursor(int2 xy, MouseInput mi)

View File

@@ -69,8 +69,8 @@ namespace OpenRa.Game.Orders
else else
return Cursor.MoveBlocked; return Cursor.MoveBlocked;
case "DeployMcv": case "DeployMcv":
var factBuildingInfo = (LegacyBuildingInfo)Rules.UnitInfo["fact"]; var factBuildingInfo = Rules.NewUnitInfo["fact"].Traits.Get<BuildingInfo>();
if (Game.CanPlaceBuilding(factBuildingInfo, a.Location - new int2(1, 1), a, false)) if (Game.CanPlaceBuilding("fact", factBuildingInfo, a.Location - new int2(1, 1), a, false))
return Cursor.Deploy; return Cursor.Deploy;
else else
return Cursor.DeployBlocked; return Cursor.DeployBlocked;

View File

@@ -20,7 +20,7 @@ namespace OpenRa.Game.Orders
if( producing == null || producing.Item != order.TargetString || producing.RemainingTime != 0 ) if( producing == null || producing.Item != order.TargetString || producing.RemainingTime != 0 )
return; 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<BuildingInfo>() ), order.Player ) );
if (order.Player == Game.LocalPlayer) if (order.Player == Game.LocalPlayer)
{ {
Sound.Play("placbldg.aud"); Sound.Play("placbldg.aud");

View File

@@ -39,7 +39,6 @@ namespace OpenRa.Game.Traits
class Building : INotifyDamage, IResolveOrder, ITick class Building : INotifyDamage, IResolveOrder, ITick
{ {
readonly Actor self; readonly Actor self;
[Obsolete] public readonly LegacyBuildingInfo unitInfo;
public readonly BuildingInfo Info; public readonly BuildingInfo Info;
[Sync] [Sync]
bool isRepairing = false; bool isRepairing = false;
@@ -53,7 +52,6 @@ namespace OpenRa.Game.Traits
{ {
this.self = self; this.self = self;
Info = self.Info.Traits.Get<BuildingInfo>(); Info = self.Info.Traits.Get<BuildingInfo>();
unitInfo = (LegacyBuildingInfo)self.LegacyInfo;
self.CenterLocation = Game.CellSize self.CenterLocation = Game.CellSize
* ((float2)self.Location + .5f * (float2)Info.Dimensions); * ((float2)self.Location + .5f * (float2)Info.Dimensions);
} }

View File

@@ -24,8 +24,8 @@ namespace OpenRa.Game.Traits
{ {
if( order.OrderString == "DeployMcv" ) if( order.OrderString == "DeployMcv" )
{ {
var factBuildingInfo = (LegacyBuildingInfo)Rules.UnitInfo[ "fact" ]; var factBuildingInfo = Rules.NewUnitInfo[ "fact" ].Traits.Get<BuildingInfo>();
if( Game.CanPlaceBuilding( factBuildingInfo, self.Location - new int2( 1, 1 ), self, false ) ) if( Game.CanPlaceBuilding( "fact", factBuildingInfo, self.Location - new int2( 1, 1 ), self, false ) )
{ {
self.CancelActivity(); self.CancelActivity();
self.QueueActivity( new Turn( 96 ) ); self.QueueActivity( new Turn( 96 ) );

View File

@@ -2,6 +2,7 @@ using System.Drawing;
using System.Linq; using System.Linq;
using OpenRa.Game.GameRules; using OpenRa.Game.GameRules;
using OpenRa.Game.Graphics; using OpenRa.Game.Graphics;
using OpenRa.Game.Traits;
namespace OpenRa.Game namespace OpenRa.Game
{ {
@@ -42,12 +43,12 @@ namespace OpenRa.Game
spriteRenderer.DrawSprite(unitDebug, Game.CellSize * new float2(i, j), 0); 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 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 spriteRenderer.DrawSprite( ( isCloseEnough && Game.IsCellBuildable( t, bi.WaterBound
? UnitMovementType.Float : UnitMovementType.Wheel ) && !Rules.Map.ContainsResource( t ) ) ? UnitMovementType.Float : UnitMovementType.Wheel ) && !Rules.Map.ContainsResource( t ) )
? buildOk : buildBlocked, Game.CellSize * t, 0 ); ? buildOk : buildBlocked, Game.CellSize * t, 0 );