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>(); var mi = Rules.Info[Info.Unit].Traits.GetOrDefault<MobileInfo>();
for (var i = -1; i < 2; i++) for (var i = -1; i < 2; i++)
for (var j = -1; j < 2; j++) 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); yield return near + new int2(i, j);
} }

View File

@@ -26,13 +26,20 @@ namespace OpenRA.Mods.RA.Move
{ {
[FieldLoader.LoadUsing("LoadSpeeds")] [FieldLoader.LoadUsing("LoadSpeeds")]
public readonly Dictionary<string, TerrainInfo> TerrainSpeeds; public readonly Dictionary<string, TerrainInfo> TerrainSpeeds;
[FieldLoader.Load] public readonly string[] Crushes; [FieldLoader.Load]
[FieldLoader.Load] public readonly int WaitAverage = 60; public readonly string[] Crushes;
[FieldLoader.Load] public readonly int WaitSpread = 20; [FieldLoader.Load]
[FieldLoader.Load] public readonly int InitialFacing = 128; public readonly int WaitAverage = 60;
[FieldLoader.Load] public readonly int ROT = 255; [FieldLoader.Load]
[FieldLoader.Load] public readonly int Speed = 1; public readonly int WaitSpread = 20;
[FieldLoader.Load] public readonly bool OnRails = false; [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); } public virtual object Create(ActorInitializer init) { return new Mobile(init, this); }
@@ -54,6 +61,44 @@ namespace OpenRA.Mods.RA.Move
public int Cost = int.MaxValue; public int Cost = int.MaxValue;
public decimal Speed = 0; 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 public class Mobile : IIssueOrder, IResolveOrder, IOrderVoice, IOccupySpace, IMove, IFacing, INudge
@@ -235,36 +280,10 @@ namespace OpenRA.Mods.RA.Move
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>(); var uim = self.World.WorldActor.Trait<UnitInfluence>();
return CanEnterCell( Info, self.World, uim, cell, ignoreActor, checkTransientActors ); return Info.CanEnterCell(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;
} }
public void FinishedMoving(Actor self) 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) public int MovementSpeedForCell(Actor self, int2 cell)
{ {
var type = self.World.GetTerrainType(cell); var type = self.World.GetTerrainType(cell);
@@ -380,7 +387,7 @@ namespace OpenRA.Mods.RA.Move
IsQueued = forceQueued; IsQueued = forceQueued;
cursor = "move"; cursor = "move";
if (!self.World.Map.IsInMap(location) || (self.World.LocalPlayer.Shroud.IsExplored(location) && if (!self.World.Map.IsInMap(location) || (self.World.LocalPlayer.Shroud.IsExplored(location) &&
Mobile.MovementCostForCell(self.Trait<Mobile>().Info, self.World, location) == int.MaxValue)) self.Trait<Mobile>().Info.MovementCostForCell(self.World, location) == int.MaxValue))
cursor = "move-blocked"; cursor = "move-blocked";
return true; return true;

View File

@@ -74,7 +74,7 @@ namespace OpenRA.Mods.RA.Move
{ {
var mi = self.Info.Traits.Get<MobileInfo>(); var mi = self.Info.Traits.Get<MobileInfo>();
var tilesInRange = world.FindTilesInCircle(target, range) 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( var path = FindBidiPath(
PathSearch.FromPoints(world, mi, tilesInRange, src, true) 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; 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) if (thisCost == int.MaxValue)
return p.Location; return p.Location;
@@ -100,12 +100,12 @@ namespace OpenRA.Mods.RA.Move
if( cellInfo[ newHere.X, newHere.Y ].Seen ) if( cellInfo[ newHere.X, newHere.Y ].Seen )
continue; continue;
var costHere = Mobile.MovementCostForCell(mobileInfo, world, newHere); var costHere = mobileInfo.MovementCostForCell(world, newHere);
if (costHere == int.MaxValue) if (costHere == int.MaxValue)
continue; continue;
if (!Mobile.CanEnterCell(mobileInfo, world, uim, newHere, ignoreBuilding, checkForBlocked)) if (!mobileInfo.CanEnterCell(world, uim, newHere, ignoreBuilding, checkForBlocked))
continue; continue;
if (customBlock != null && customBlock(newHere)) if (customBlock != null && customBlock(newHere))

View File

@@ -94,7 +94,7 @@ namespace OpenRA.Mods.RA
// Pick a spawn/exit point pair // Pick a spawn/exit point pair
// Todo: Reorder in a synced random way // Todo: Reorder in a synced random way
foreach (var s in self.Info.Traits.WithInterface<ExitInfo>()) 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); DoProduction(self, producee, s);
return true; return true;