Refactor UnitInfluence trait -> world.ActorMap

This commit is contained in:
Paul Chote
2011-05-22 13:06:14 +12:00
parent d6496cb5be
commit d4baf2d757
25 changed files with 49 additions and 73 deletions

View File

@@ -83,15 +83,15 @@ namespace OpenRA.Mods.RA.Move
{SubCell.FullCell, new int2(0,0)},
};
public bool CanEnterCell(World world, UnitInfluence uim, int2 cell, Actor ignoreActor, bool checkTransientActors)
public bool CanEnterCell(World world, int2 cell, Actor ignoreActor, bool checkTransientActors)
{
if (MovementCostForCell(world, cell) == int.MaxValue)
return false;
if (SharesCell && uim.HasFreeSubCell(cell))
if (SharesCell && world.ActorMap.HasFreeSubCell(cell))
return true;
var blockingActors = uim.GetUnitsAt(cell).Where(x => x != ignoreActor).ToList();
var blockingActors = world.ActorMap.GetUnitsAt(cell).Where(x => x != ignoreActor).ToList();
if (checkTransientActors && blockingActors.Count > 0)
{
// Non-sharable unit can enter a cell with shareable units only if it can crush all of them
@@ -105,12 +105,6 @@ namespace OpenRA.Mods.RA.Move
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, ISync
@@ -163,8 +157,6 @@ namespace OpenRA.Mods.RA.Move
AddInfluence();
}
UnitInfluence uim;
const int avgTicksBeforePathing = 5;
const int spreadTicksBeforePathing = 5;
internal int ticksBeforePathing = 0;
@@ -174,7 +166,6 @@ namespace OpenRA.Mods.RA.Move
this.self = init.self;
this.Info = info;
uim = self.World.WorldActor.Trait<UnitInfluence>();
__toSubCell = __fromSubCell = info.SharesCell ? SubCell.Center : SubCell.FullCell;
if (init.Contains<SubCellInit>())
{
@@ -321,7 +312,7 @@ namespace OpenRA.Mods.RA.Move
return new[]{ __fromSubCell, SubCell.TopLeft, SubCell.TopRight, SubCell.Center,
SubCell.BottomLeft, SubCell.BottomRight}.First(b =>
{
var blockingActors = uim.GetUnitsAt(a,b).Where(c => c != ignoreActor);
var blockingActors = self.World.ActorMap.GetUnitsAt(a,b).Where(c => c != ignoreActor);
if (blockingActors.Count() > 0)
{
// Non-sharable unit can enter a cell with shareable units only if it can crush all of them
@@ -343,13 +334,12 @@ namespace OpenRA.Mods.RA.Move
public bool CanEnterCell(int2 cell, Actor ignoreActor, bool checkTransientActors)
{
var uim = self.World.WorldActor.Trait<UnitInfluence>();
return Info.CanEnterCell(self.World, uim, cell, ignoreActor, checkTransientActors);
return Info.CanEnterCell(self.World, cell, ignoreActor, checkTransientActors);
}
public void FinishedMoving(Actor self)
{
var crushable = uim.GetUnitsAt(toCell).Where(a => a != self && a.HasTrait<ICrushable>());
var crushable = self.World.ActorMap.GetUnitsAt(toCell).Where(a => a != self && a.HasTrait<ICrushable>());
foreach (var a in crushable)
{
var crushActions = a.TraitsImplementing<ICrushable>().Where(b => b.CrushClasses.Intersect(Info.Crushes).Any());
@@ -374,13 +364,13 @@ namespace OpenRA.Mods.RA.Move
public void AddInfluence()
{
if (self.IsInWorld)
uim.Add(self, this);
self.World.ActorMap.Add(self, this);
}
public void RemoveInfluence()
{
if (self.IsInWorld)
uim.Remove(self, this);
self.World.ActorMap.Remove(self, this);
}
public void OnNudge(Actor self, Actor nudger)

View File

@@ -162,7 +162,7 @@ namespace OpenRA.Mods.RA.Move
void NudgeBlocker(Actor self, int2 nextCell)
{
var blocker = self.World.WorldActor.Trait<UnitInfluence>().GetUnitsAt(nextCell).FirstOrDefault();
var blocker = self.World.ActorMap.GetUnitsAt(nextCell).FirstOrDefault();
if (blocker == null) return;
Log.Write("debug", "NudgeBlocker #{0} nudges #{1} at {2} from {3}",

View File

@@ -27,12 +27,10 @@ namespace OpenRA.Mods.RA.Move
public bool inReverse;
MobileInfo mobileInfo;
UnitInfluence uim;
public PathSearch(World world, MobileInfo mobileInfo)
{
this.world = world;
uim = world.WorldActor.Trait<UnitInfluence>();
cellInfo = InitCellInfo();
this.mobileInfo = mobileInfo;
queue = new PriorityQueue<PathDistance>();
@@ -105,7 +103,7 @@ namespace OpenRA.Mods.RA.Move
if (costHere == int.MaxValue)
continue;
if (!mobileInfo.CanEnterCell(world, uim, newHere, ignoreBuilding, checkForBlocked))
if (!mobileInfo.CanEnterCell(world, newHere, ignoreBuilding, checkForBlocked))
continue;
if (customBlock != null && customBlock(newHere))