move some MobileInfo-related bits onto MobileInfo

This commit is contained in:
Chris Forbes
2010-12-27 18:41:07 +13:00
parent 31b98c0eb4
commit 9d768fa1c1
5 changed files with 337 additions and 330 deletions

View File

@@ -63,7 +63,7 @@ namespace OpenRA.Mods.RA.Crates
var mi = Rules.Info[Info.Unit].Traits.GetOrDefault<MobileInfo>();
for (var i = -1; i < 2; i++)
for (var j = -1; j < 2; j++)
if (Mobile.CanEnterCell(self.World, mi, near + new int2(i, j), null, true))
if (mi.CanEnterCell(self.World, near + new int2(i, j), null, true))
yield return near + new int2(i, j);
}

View File

@@ -24,26 +24,33 @@ namespace OpenRA.Mods.RA.Move
{
public class MobileInfo : ITraitInfo
{
[FieldLoader.LoadUsing( "LoadSpeeds" )]
[FieldLoader.LoadUsing("LoadSpeeds")]
public readonly Dictionary<string, TerrainInfo> TerrainSpeeds;
[FieldLoader.Load] public readonly string[] Crushes;
[FieldLoader.Load] public readonly int WaitAverage = 60;
[FieldLoader.Load] public readonly int WaitSpread = 20;
[FieldLoader.Load] public readonly int InitialFacing = 128;
[FieldLoader.Load] public readonly int ROT = 255;
[FieldLoader.Load] public readonly int Speed = 1;
[FieldLoader.Load] public readonly bool OnRails = false;
[FieldLoader.Load]
public readonly string[] Crushes;
[FieldLoader.Load]
public readonly int WaitAverage = 60;
[FieldLoader.Load]
public readonly int WaitSpread = 20;
[FieldLoader.Load]
public readonly int InitialFacing = 128;
[FieldLoader.Load]
public readonly int ROT = 255;
[FieldLoader.Load]
public readonly int Speed = 1;
[FieldLoader.Load]
public readonly bool OnRails = false;
public virtual object Create(ActorInitializer init) { return new Mobile(init, this); }
static object LoadSpeeds( MiniYaml y )
static object LoadSpeeds(MiniYaml y)
{
Dictionary<string,TerrainInfo> ret = new Dictionary<string, TerrainInfo>();
Dictionary<string, TerrainInfo> ret = new Dictionary<string, TerrainInfo>();
foreach (var t in y.NodesDict["TerrainSpeeds"].Nodes)
{
var speed = (decimal)FieldLoader.GetValue("speed", typeof(decimal),t.Value.Value);
var cost = t.Value.NodesDict.ContainsKey("PathingCost") ? (int)FieldLoader.GetValue("cost", typeof(int), t.Value.NodesDict["PathingCost"].Value) : (int)(10000/speed);
ret.Add(t.Key, new TerrainInfo{Speed = speed, Cost = cost});
var speed = (decimal)FieldLoader.GetValue("speed", typeof(decimal), t.Value.Value);
var cost = t.Value.NodesDict.ContainsKey("PathingCost") ? (int)FieldLoader.GetValue("cost", typeof(int), t.Value.NodesDict["PathingCost"].Value) : (int)(10000 / speed);
ret.Add(t.Key, new TerrainInfo { Speed = speed, Cost = cost });
}
return ret;
@@ -54,6 +61,44 @@ namespace OpenRA.Mods.RA.Move
public int Cost = int.MaxValue;
public decimal Speed = 0;
}
public int MovementCostForCell(World world, int2 cell)
{
if (!world.Map.IsInMap(cell.X, cell.Y))
return int.MaxValue;
var type = world.GetTerrainType(cell);
if (!TerrainSpeeds.ContainsKey(type))
return int.MaxValue;
return TerrainSpeeds[type].Cost;
}
public bool CanEnterCell(World world, UnitInfluence uim, int2 cell, Actor ignoreActor, bool checkTransientActors)
{
if (MovementCostForCell(world, cell) == int.MaxValue)
return false;
var blockingActors = uim.GetUnitsAt(cell).Where(x => x != ignoreActor).ToList();
if (checkTransientActors && blockingActors.Count > 0)
{
// We can enter a cell with nonshareable units only if we can crush all of them
if (Crushes == null)
return false;
if (blockingActors.Any(a => !(a.HasTrait<ICrushable>() &&
a.TraitsImplementing<ICrushable>().Any(b => b.CrushClasses.Intersect(Crushes).Any()))))
return false;
}
return true;
}
public bool CanEnterCell(World world, int2 cell, Actor ignoreActor, bool checkTransientActors)
{
var uim = world.WorldActor.Trait<UnitInfluence>();
return CanEnterCell(world, uim, cell, ignoreActor, checkTransientActors);
}
}
public class Mobile : IIssueOrder, IResolveOrder, IOrderVoice, IOccupySpace, IMove, IFacing, INudge
@@ -117,38 +162,38 @@ namespace OpenRA.Mods.RA.Move
if (init.Contains<LocationInit>())
{
this.__fromCell = this.__toCell = init.Get<LocationInit,int2>();
this.PxPosition = Util.CenterOfCell( fromCell );
this.__fromCell = this.__toCell = init.Get<LocationInit, int2>();
this.PxPosition = Util.CenterOfCell(fromCell);
}
this.Facing = init.Contains<FacingInit>() ? init.Get<FacingInit,int>() : info.InitialFacing;
this.Altitude = init.Contains<AltitudeInit>() ? init.Get<AltitudeInit,int>() : 0;
this.Facing = init.Contains<FacingInit>() ? init.Get<FacingInit, int>() : info.InitialFacing;
this.Altitude = init.Contains<AltitudeInit>() ? init.Get<AltitudeInit, int>() : 0;
}
public void SetPosition(Actor self, int2 cell)
{
SetLocation( cell, cell );
SetLocation(cell, cell);
PxPosition = Util.CenterOfCell(fromCell);
FinishedMoving(self);
}
public void SetPxPosition( Actor self, int2 px )
public void SetPxPosition(Actor self, int2 px)
{
var cell = Util.CellContaining( px );
SetLocation( cell, cell );
var cell = Util.CellContaining(px);
SetLocation(cell, cell);
PxPosition = px;
FinishedMoving(self);
}
public IEnumerable<IOrderTargeter> Orders { get { yield return new MoveOrderTargeter( Info ); } }
public IEnumerable<IOrderTargeter> Orders { get { yield return new MoveOrderTargeter(Info); } }
// Note: Returns a valid order even if the unit can't move to the target
public Order IssueOrder( Actor self, IOrderTargeter order, Target target, bool queued )
public Order IssueOrder(Actor self, IOrderTargeter order, Target target, bool queued)
{
if( order is MoveOrderTargeter )
if (order is MoveOrderTargeter)
{
if( Info.OnRails ) return null;
return new Order( "Move", self, queued ) { TargetLocation = Util.CellContaining( target.CenterLocation ) };
if (Info.OnRails) return null;
return new Order("Move", self, queued) { TargetLocation = Util.CellContaining(target.CenterLocation) };
}
return null;
}
@@ -158,10 +203,10 @@ namespace OpenRA.Mods.RA.Move
if (CanEnterCell(target))
return target;
var searched = new List<int2>(){};
var searched = new List<int2>() { };
// Limit search to a radius of 10 tiles
for (int r = 1; r < 10; r++)
foreach (var tile in self.World.FindTilesInCircle(target,r).Except(searched))
foreach (var tile in self.World.FindTilesInCircle(target, r).Except(searched))
{
if (CanEnterCell(tile))
return tile;
@@ -232,39 +277,13 @@ namespace OpenRA.Mods.RA.Move
public bool CanEnterCell(int2 p)
{
return CanEnterCell( p, null, true);
return CanEnterCell(p, null, true);
}
public static bool CanEnterCell( World world, MobileInfo mi, int2 cell, Actor ignoreActor, bool checkTransientActors )
{
var uim = world.WorldActor.Trait<UnitInfluence>();
return Mobile.CanEnterCell( mi, world, uim, cell, ignoreActor, checkTransientActors );
}
public bool CanEnterCell( int2 cell, Actor ignoreActor, bool checkTransientActors )
public bool CanEnterCell(int2 cell, Actor ignoreActor, bool checkTransientActors)
{
var uim = self.World.WorldActor.Trait<UnitInfluence>();
return CanEnterCell( Info, self.World, uim, cell, ignoreActor, checkTransientActors );
}
public static bool CanEnterCell( MobileInfo mobileInfo, World world, UnitInfluence uim, int2 cell, Actor ignoreActor, bool checkTransientActors )
{
if (MovementCostForCell(mobileInfo, world, cell) == int.MaxValue)
return false;
var blockingActors = uim.GetUnitsAt( cell ).Where( x => x != ignoreActor ).ToList();
if (checkTransientActors && blockingActors.Count > 0)
{
// We can enter a cell with nonshareable units only if we can crush all of them
if (mobileInfo.Crushes == null)
return false;
if (blockingActors.Any(a => !(a.HasTrait<ICrushable>() &&
a.TraitsImplementing<ICrushable>().Any(b => b.CrushClasses.Intersect(mobileInfo.Crushes).Any()))))
return false;
}
return true;
return Info.CanEnterCell(self.World, uim, cell, ignoreActor, checkTransientActors);
}
public void FinishedMoving(Actor self)
@@ -278,18 +297,6 @@ namespace OpenRA.Mods.RA.Move
}
}
public static int MovementCostForCell(MobileInfo info, World world, int2 cell)
{
if (!world.Map.IsInMap(cell.X,cell.Y))
return int.MaxValue;
var type = world.GetTerrainType(cell);
if (!info.TerrainSpeeds.ContainsKey(type))
return int.MaxValue;
return info.TerrainSpeeds[type].Cost;
}
public int MovementSpeedForCell(Actor self, int2 cell)
{
var type = self.World.GetTerrainType(cell);
@@ -298,21 +305,21 @@ namespace OpenRA.Mods.RA.Move
return 0;
decimal speed = Info.Speed * Info.TerrainSpeeds[type].Speed;
foreach( var t in self.TraitsImplementing<ISpeedModifier>() )
foreach (var t in self.TraitsImplementing<ISpeedModifier>())
speed *= t.GetSpeedModifier();
return (int)(speed / 100);
}
public void AddInfluence()
{
if( self.IsInWorld )
uim.Add( self, this );
if (self.IsInWorld)
uim.Add(self, this);
}
public void RemoveInfluence()
{
if( self.IsInWorld )
uim.Remove( self, this );
if (self.IsInWorld)
uim.Remove(self, this);
}
public void OnNudge(Actor self, Actor nudger)
@@ -330,7 +337,7 @@ namespace OpenRA.Mods.RA.Move
var availCells = new List<int2>();
var notStupidCells = new List<int2>();
for( var i = -1; i < 2; i++ )
for (var i = -1; i < 2; i++)
for (var j = -1; j < 2; j++)
{
var p = toCell + new int2(i, j);
@@ -362,7 +369,7 @@ namespace OpenRA.Mods.RA.Move
{
readonly MobileInfo unitType;
public MoveOrderTargeter( MobileInfo unitType )
public MoveOrderTargeter(MobileInfo unitType)
{
this.unitType = unitType;
}
@@ -379,19 +386,19 @@ namespace OpenRA.Mods.RA.Move
{
IsQueued = forceQueued;
cursor = "move";
if( !self.World.Map.IsInMap(location) || (self.World.LocalPlayer.Shroud.IsExplored( location ) &&
Mobile.MovementCostForCell(self.Trait<Mobile>().Info, self.World, location) == int.MaxValue))
if (!self.World.Map.IsInMap(location) || (self.World.LocalPlayer.Shroud.IsExplored(location) &&
self.Trait<Mobile>().Info.MovementCostForCell(self.World, location) == int.MaxValue))
cursor = "move-blocked";
return true;
}
}
public IActivity ScriptedMove( int2 cell ) { return new Move( cell ); }
public IActivity MoveTo( int2 cell, int nearEnough ) { return new Move( cell, nearEnough ); }
public IActivity MoveTo( int2 cell, Actor ignoredActor ) { return new Move( cell, ignoredActor ); }
public IActivity MoveWithinRange( Actor target, int range ) { return new Move( target, range ); }
public IActivity MoveWithinRange( Target target, int range ) { return new Move( target, range ); }
public IActivity MoveTo( Func<List<int2>> pathFunc ) { return new Move( pathFunc ); }
public IActivity ScriptedMove(int2 cell) { return new Move(cell); }
public IActivity MoveTo(int2 cell, int nearEnough) { return new Move(cell, nearEnough); }
public IActivity MoveTo(int2 cell, Actor ignoredActor) { return new Move(cell, ignoredActor); }
public IActivity MoveWithinRange(Actor target, int range) { return new Move(target, range); }
public IActivity MoveWithinRange(Target target, int range) { return new Move(target, range); }
public IActivity MoveTo(Func<List<int2>> pathFunc) { return new Move(pathFunc); }
}
}

View File

@@ -74,7 +74,7 @@ namespace OpenRA.Mods.RA.Move
{
var mi = self.Info.Traits.Get<MobileInfo>();
var tilesInRange = world.FindTilesInCircle(target, range)
.Where( t => Mobile.CanEnterCell(self.World, mi, t, null, true));
.Where( t => mi.CanEnterCell(self.World, t, null, true));
var path = FindBidiPath(
PathSearch.FromPoints(world, mi, tilesInRange, src, true)

View File

@@ -87,7 +87,7 @@ namespace OpenRA.Mods.RA.Move
cellInfo[p.Location.X, p.Location.Y].Seen = true;
var thisCost = Mobile.MovementCostForCell(mobileInfo, world, p.Location);
var thisCost = mobileInfo.MovementCostForCell(world, p.Location);
if (thisCost == int.MaxValue)
return p.Location;
@@ -100,12 +100,12 @@ namespace OpenRA.Mods.RA.Move
if( cellInfo[ newHere.X, newHere.Y ].Seen )
continue;
var costHere = Mobile.MovementCostForCell(mobileInfo, world, newHere);
var costHere = mobileInfo.MovementCostForCell(world, newHere);
if (costHere == int.MaxValue)
continue;
if (!Mobile.CanEnterCell(mobileInfo, world, uim, newHere, ignoreBuilding, checkForBlocked))
if (!mobileInfo.CanEnterCell(world, uim, newHere, ignoreBuilding, checkForBlocked))
continue;
if (customBlock != null && customBlock(newHere))

View File

@@ -94,7 +94,7 @@ namespace OpenRA.Mods.RA
// Pick a spawn/exit point pair
// Todo: Reorder in a synced random way
foreach (var s in self.Info.Traits.WithInterface<ExitInfo>())
if( Mobile.CanEnterCell( mobileInfo, self.World, uim, self.Location + s.ExitCell,self,true ) )
if( mobileInfo.CanEnterCell( self.World, uim, self.Location + s.ExitCell,self,true ) )
{
DoProduction(self, producee, s);
return true;