Cleaned up some casts. (BuildingInfo)

This commit is contained in:
Bob
2009-11-14 02:57:52 +13:00
parent f4e55572d6
commit c285f1d210
12 changed files with 76 additions and 72 deletions

View File

@@ -25,14 +25,14 @@ namespace OpenRa.Game
influence[i, j] = NoClaim;
Game.world.ActorAdded +=
a => { if (a.traits.Contains<Traits.Building>()) AddInfluence(a); };
a => { if (a.traits.Contains<Traits.Building>()) AddInfluence(a, a.traits.Get<Traits.Building>()); };
Game.world.ActorRemoved +=
a => { if (a.traits.Contains<Traits.Building>()) RemoveInfluence(a); };
a => { if (a.traits.Contains<Traits.Building>()) RemoveInfluence(a, a.traits.Get<Traits.Building>()); };
}
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<Traits.Building>();
if( bb != null )
AddInfluence( b, bb );
}
}
bool IsValid(int2 t)

View File

@@ -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<Traits.Building>());
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)

View File

@@ -9,14 +9,13 @@ namespace OpenRa.Game.GameRules
{
static class Footprint
{
public static IEnumerable<int2> Tiles( UnitInfo unitInfo, int2 position )
public static IEnumerable<int2> Tiles( UnitInfo.BuildingInfo buildingInfo, int2 position )
{
return Tiles(unitInfo, position, true);
return Tiles(buildingInfo, position, true);
}
public static IEnumerable<int2> Tiles(UnitInfo unitInfo, int2 position, bool adjustForPlacement)
public static IEnumerable<int2> 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<int2> Tiles(Actor a)
public static IEnumerable<int2> Tiles(Actor a, Traits.Building building)
{
return Tiles(a.unitInfo, a.Location, false);
return Tiles( building.unitInfo, a.Location, false );
}
public static IEnumerable<int2> UnpathableTiles( UnitInfo unitInfo, int2 position )
public static IEnumerable<int2> 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;

View File

@@ -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> 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; } );
}
}

View File

@@ -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

View File

@@ -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);
}
}
}

View File

@@ -14,10 +14,10 @@ namespace OpenRa.Game.Traits
{
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));
}
}
}

View File

@@ -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<int2> GetCurrentPath()

View File

@@ -15,7 +15,7 @@ namespace OpenRa.Game.Traits
public RallyPoint(Actor self)
{
var bi = (UnitInfo.BuildingInfo)self.unitInfo;
var bi = self.traits.Get<Building>().unitInfo;
rallyPoint = self.Location + new int2(bi.RallyPoint[0], bi.RallyPoint[1]);
anim = new Animation("flagfly");
anim.PlayRepeating("idle");

View File

@@ -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<Building>().unitInfo;
if (buildingInfo.Bib)
{
var size = buildingInfo.Dimensions.X;

View File

@@ -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. */

View File

@@ -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<Mobile>();
@@ -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]);
} );