Cache trait lookups in MoveAdjacentTo.

This commit is contained in:
Paul Chote
2014-01-09 17:59:20 +13:00
parent 27ef3352f4
commit c333b59eb9
8 changed files with 16 additions and 10 deletions

View File

@@ -34,7 +34,7 @@ namespace OpenRA.Mods.RA.Activities
return NextActivity; return NextActivity;
if (!Util.AdjacentCells(target).Any(c => c == self.Location)) if (!Util.AdjacentCells(target).Any(c => c == self.Location))
return Util.SequenceActivities(new MoveAdjacentTo(target), this); return Util.SequenceActivities(new MoveAdjacentTo(self, target), this);
// Move to the middle of the target, ignoring impassable tiles // Move to the middle of the target, ignoring impassable tiles
var mobile = self.Trait<Mobile>(); var mobile = self.Trait<Mobile>();

View File

@@ -39,7 +39,7 @@ namespace OpenRA.Mods.RA.Activities
var nearest = target.Actor.OccupiesSpace.NearestCellTo(mobile.toCell); var nearest = target.Actor.OccupiesSpace.NearestCellTo(mobile.toCell);
if ((nearest - mobile.toCell).LengthSquared > 2) if ((nearest - mobile.toCell).LengthSquared > 2)
return Util.SequenceActivities(new MoveAdjacentTo(target), this); return Util.SequenceActivities(new MoveAdjacentTo(self, target), this);
if (!capturable.CaptureInProgress) if (!capturable.CaptureInProgress)
capturable.BeginCapture(self); capturable.BeginCapture(self);

View File

@@ -37,7 +37,7 @@ namespace OpenRA.Mods.RA.Activities
return new Wait(20); return new Wait(20);
return Util.SequenceActivities( return Util.SequenceActivities(
new MoveAdjacentTo(Target.FromActor(rearmTarget)), new MoveAdjacentTo(self, Target.FromActor(rearmTarget)),
mobile.MoveTo(rearmTarget.CenterPosition.ToCPos(), rearmTarget), mobile.MoveTo(rearmTarget.CenterPosition.ToCPos(), rearmTarget),
new Rearm(self), new Rearm(self),
new Repair(rearmTarget), new Repair(rearmTarget),

View File

@@ -16,15 +16,21 @@ namespace OpenRA.Mods.RA.Activities
public class MoveAdjacentTo : Activity public class MoveAdjacentTo : Activity
{ {
readonly Target target; readonly Target target;
readonly Mobile mobile;
readonly PathFinder pathFinder;
public MoveAdjacentTo(Target target) { this.target = target; } public MoveAdjacentTo(Actor self, Target target)
{
this.target = target;
mobile = self.Trait<Mobile>();
pathFinder = self.World.WorldActor.Trait<PathFinder>();
}
public override Activity Tick(Actor self) public override Activity Tick(Actor self)
{ {
if (IsCanceled || !target.IsValidFor(self)) if (IsCanceled || !target.IsValidFor(self))
return NextActivity; return NextActivity;
var mobile = self.Trait<Mobile>();
var ps1 = new PathSearch(self.World, mobile.Info, self) var ps1 = new PathSearch(self.World, mobile.Info, self)
{ {
checkForBlocked = true, checkForBlocked = true,
@@ -42,7 +48,7 @@ namespace OpenRA.Mods.RA.Activities
ps1.heuristic = PathSearch.DefaultEstimator(mobile.toCell); ps1.heuristic = PathSearch.DefaultEstimator(mobile.toCell);
var ps2 = PathSearch.FromPoint(self.World, mobile.Info, self, mobile.toCell, target.CenterPosition.ToCPos(), true); var ps2 = PathSearch.FromPoint(self.World, mobile.Info, self, mobile.toCell, target.CenterPosition.ToCPos(), true);
var ret = self.World.WorldActor.Trait<PathFinder>().FindBidiPath(ps1, ps2); var ret = pathFinder.FindBidiPath(ps1, ps2);
return Util.SequenceActivities(mobile.MoveTo(() => ret), this); return Util.SequenceActivities(mobile.MoveTo(() => ret), this);
} }

View File

@@ -65,7 +65,7 @@ namespace OpenRA.Mods.RA
self.CancelActivity(); self.CancelActivity();
self.SetTargetLine(target, Color.Red); self.SetTargetLine(target, Color.Red);
self.QueueActivity(new MoveAdjacentTo(target)); self.QueueActivity(new MoveAdjacentTo(self, target));
self.QueueActivity(new CallFunc(() => Explode(self))); self.QueueActivity(new CallFunc(() => Explode(self)));
} }

View File

@@ -146,7 +146,7 @@ namespace OpenRA.Mods.RA
self.CancelActivity(); self.CancelActivity();
self.SetTargetLine(target, Color.Red); self.SetTargetLine(target, Color.Red);
self.QueueActivity(new MoveAdjacentTo(target)); self.QueueActivity(new MoveAdjacentTo(self, target));
self.QueueActivity(new CallFunc(StartDetonationSequence)); self.QueueActivity(new CallFunc(StartDetonationSequence));
} }

View File

@@ -79,7 +79,7 @@ namespace OpenRA.Mods.RA
self.SetTargetLine(target, Color.Green); self.SetTargetLine(target, Color.Green);
self.CancelActivity(); self.CancelActivity();
self.QueueActivity(new MoveAdjacentTo(target)); self.QueueActivity(new MoveAdjacentTo(self, target));
self.QueueActivity(new EnterTransport(self, order.TargetActor)); self.QueueActivity(new EnterTransport(self, order.TargetActor));
} }
} }

View File

@@ -78,7 +78,7 @@ namespace OpenRA.Mods.RA
self.SetTargetLine(target, Color.Green); self.SetTargetLine(target, Color.Green);
self.CancelActivity(); self.CancelActivity();
self.QueueActivity(new MoveAdjacentTo(target)); self.QueueActivity(new MoveAdjacentTo(self, target));
self.QueueActivity(mobile.MoveTo(order.TargetActor.CenterPosition.ToCPos(), order.TargetActor)); self.QueueActivity(mobile.MoveTo(order.TargetActor.CenterPosition.ToCPos(), order.TargetActor));
self.QueueActivity(new Rearm(self)); self.QueueActivity(new Rearm(self));
self.QueueActivity(new Repair(order.TargetActor)); self.QueueActivity(new Repair(order.TargetActor));