Skip to content

Commit

Permalink
Cache unchanging values for MoveWithinRange
Browse files Browse the repository at this point in the history
In theory, CandidateMovementCells could be called
every tick, so let's avoid creating the vars
every time.
  • Loading branch information
reaperrr authored and teinarss committed Aug 10, 2021
1 parent 0249116 commit 777a927
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions OpenRA.Mods.Common/Activities/Move/MoveWithinRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ public class MoveWithinRange : MoveAdjacentTo
{
readonly WDist maxRange;
readonly WDist minRange;
readonly Map map;
readonly int maxCells;
readonly int minCells;

public MoveWithinRange(Actor self, in Target target, WDist minRange, WDist maxRange,
WPos? initialTargetPosition = null, Color? targetLineColor = null)
: base(self, target, initialTargetPosition, targetLineColor)
{
this.minRange = minRange;
this.maxRange = maxRange;
map = self.World.Map;
maxCells = (maxRange.Length + 1023) / 1024;
minCells = minRange.Length / 1024;
}

protected override bool ShouldStop(Actor self)
Expand All @@ -44,10 +50,6 @@ protected override bool ShouldRepath(Actor self, CPos targetLocation)

protected override IEnumerable<CPos> CandidateMovementCells(Actor self)
{
var map = self.World.Map;
var maxCells = (maxRange.Length + 1023) / 1024;
var minCells = minRange.Length / 1024;

return map.FindTilesInAnnulus(lastVisibleTargetLocation, minCells, maxCells)
.Where(c => Mobile.CanStayInCell(c) && AtCorrectRange(map.CenterOfSubCell(c, Mobile.FromSubCell)));
}
Expand Down

0 comments on commit 777a927

Please sign in to comment.