diff --git a/OpenRA.Game/Traits/Target.cs b/OpenRA.Game/Traits/Target.cs index a8e73f99ef..7ab4337e38 100644 --- a/OpenRA.Game/Traits/Target.cs +++ b/OpenRA.Game/Traits/Target.cs @@ -146,8 +146,7 @@ namespace OpenRA.Traits return false; // Target ranges are calculated in 2D, so ignore height differences - var rangeSquared = range.Range * range.Range; - return Positions.Any(t => (t - origin).HorizontalLengthSquared <= rangeSquared); + return Positions.Any(t => (t - origin).HorizontalLengthSquared <= range.RangeSquared); } public override string ToString() diff --git a/OpenRA.Game/Traits/World/ActorMap.cs b/OpenRA.Game/Traits/World/ActorMap.cs index 0572725cc5..bfee3448dd 100644 --- a/OpenRA.Game/Traits/World/ActorMap.cs +++ b/OpenRA.Game/Traits/World/ActorMap.cs @@ -132,7 +132,7 @@ namespace OpenRA.Traits var oldActors = currentActors; var delta = new WVec(Range, Range, WRange.Zero); currentActors = am.ActorsInBox(Position - delta, Position + delta) - .Where(a => (a.CenterPosition - Position).HorizontalLengthSquared < Range.Range * Range.Range) + .Where(a => (a.CenterPosition - Position).HorizontalLengthSquared < Range.RangeSquared) .ToList(); var entered = currentActors.Except(oldActors); diff --git a/OpenRA.Game/Traits/World/Shroud.cs b/OpenRA.Game/Traits/World/Shroud.cs index b3c6746fbe..93587afc35 100644 --- a/OpenRA.Game/Traits/World/Shroud.cs +++ b/OpenRA.Game/Traits/World/Shroud.cs @@ -109,7 +109,7 @@ namespace OpenRA.Traits { var map = world.Map; var r = (radius.Range + 1023) / 1024; - var limit = radius.Range * radius.Range; + var limit = radius.RangeSquared; var pos = map.CenterOfCell(position); foreach (var cell in map.FindTilesInCircle(position, r)) diff --git a/OpenRA.Game/WRange.cs b/OpenRA.Game/WRange.cs index 5004eec776..5255930c47 100644 --- a/OpenRA.Game/WRange.cs +++ b/OpenRA.Game/WRange.cs @@ -23,6 +23,7 @@ namespace OpenRA public struct WRange : IComparable, IComparable, IEquatable, IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaEqualityBinding, ILuaTableBinding { public readonly int Range; + public long RangeSquared { get { return (long)Range * (long)Range; } } public WRange(int r) { Range = r; } public static readonly WRange Zero = new WRange(0); diff --git a/OpenRA.Game/WorldUtils.cs b/OpenRA.Game/WorldUtils.cs index d3cd04cda8..84caa4b046 100644 --- a/OpenRA.Game/WorldUtils.cs +++ b/OpenRA.Game/WorldUtils.cs @@ -36,9 +36,8 @@ namespace OpenRA { // Target ranges are calculated in 2D, so ignore height differences var vec = new WVec(r, r, WRange.Zero); - var rSq = r.Range * r.Range; return world.ActorMap.ActorsInBox(origin - vec, origin + vec).Where( - a => (a.CenterPosition - origin).HorizontalLengthSquared <= rSq); + a => (a.CenterPosition - origin).HorizontalLengthSquared <= r.RangeSquared); } } diff --git a/OpenRA.Mods.Common/Activities/Enter.cs b/OpenRA.Mods.Common/Activities/Enter.cs index 46215d2e25..cccbb2c7ea 100644 --- a/OpenRA.Mods.Common/Activities/Enter.cs +++ b/OpenRA.Mods.Common/Activities/Enter.cs @@ -54,11 +54,10 @@ namespace OpenRA.Mods.Common.Activities protected bool TryGetAlternateTargetInCircle( Actor self, WRange radius, Action update, Func primaryFilter, Func[] preferenceFilters = null) { - var radiusSquared = radius.Range * radius.Range; var diff = new WVec(radius, radius, WRange.Zero); var candidates = self.World.ActorMap.ActorsInBox(self.CenterPosition - diff, self.CenterPosition + diff) .Where(primaryFilter).Select(a => new { Actor = a, Ls = (self.CenterPosition - a.CenterPosition).HorizontalLengthSquared }) - .Where(p => p.Ls <= radiusSquared).OrderBy(p => p.Ls).Select(p => p.Actor); + .Where(p => p.Ls <= radius.RangeSquared).OrderBy(p => p.Ls).Select(p => p.Actor); if (preferenceFilters != null) foreach (var filter in preferenceFilters) { diff --git a/OpenRA.Mods.Common/Activities/Move/MoveWithinRange.cs b/OpenRA.Mods.Common/Activities/Move/MoveWithinRange.cs index e91407ecfa..7b6e99fac4 100644 --- a/OpenRA.Mods.Common/Activities/Move/MoveWithinRange.cs +++ b/OpenRA.Mods.Common/Activities/Move/MoveWithinRange.cs @@ -48,8 +48,8 @@ namespace OpenRA.Mods.Common.Activities var maxCells = (maxRange.Range + 1023) / 1024; var minCells = minRange.Range / 1024; - var outerSq = maxRange.Range * maxRange.Range; - var innerSq = minRange.Range * minRange.Range; + var outerSq = maxRange.RangeSquared; + var innerSq = minRange.RangeSquared; var center = Target.CenterPosition; return map.FindTilesInAnnulus(targetPosition, minCells + 1, maxCells).Where(c => diff --git a/OpenRA.Mods.Common/Effects/Missile.cs b/OpenRA.Mods.Common/Effects/Missile.cs index b1ff707e90..be726e5cfc 100644 --- a/OpenRA.Mods.Common/Effects/Missile.cs +++ b/OpenRA.Mods.Common/Effects/Missile.cs @@ -181,7 +181,7 @@ namespace OpenRA.Mods.Common.Effects var cell = world.Map.CellContaining(pos); var shouldExplode = (pos.Z < 0) // Hit the ground - || (dist.LengthSquared < info.CloseEnough.Range * info.CloseEnough.Range) // Within range + || (dist.LengthSquared < info.CloseEnough.RangeSquared) // Within range || (info.RangeLimit != 0 && ticks > info.RangeLimit) // Ran out of fuel || (info.Blockable && world.ActorMap.GetUnitsAt(cell).Any(a => a.HasTrait())) // Hit a wall or other blocking obstacle || !world.Map.Contains(cell) // This also avoids an IndexOutOfRangeException in GetTerrainInfo below. diff --git a/OpenRA.Mods.Common/Traits/Air/Aircraft.cs b/OpenRA.Mods.Common/Traits/Air/Aircraft.cs index fbe23c3a0b..a1c723fdc0 100644 --- a/OpenRA.Mods.Common/Traits/Air/Aircraft.cs +++ b/OpenRA.Mods.Common/Traits/Air/Aircraft.cs @@ -110,7 +110,7 @@ namespace OpenRA.Mods.Common.Traits var d = self.CenterPosition - other.CenterPosition; var distSq = d.HorizontalLengthSquared; - if (distSq > info.IdealSeparation.Range * info.IdealSeparation.Range) + if (distSq > info.IdealSeparation.RangeSquared) return WVec.Zero; if (distSq < 1) diff --git a/OpenRA.Mods.Common/Traits/Attack/AttackBase.cs b/OpenRA.Mods.Common/Traits/Attack/AttackBase.cs index 83057ff250..39efb4e5bb 100644 --- a/OpenRA.Mods.Common/Traits/Attack/AttackBase.cs +++ b/OpenRA.Mods.Common/Traits/Attack/AttackBase.cs @@ -262,9 +262,8 @@ namespace OpenRA.Mods.Common.Traits if (modifiers.HasModifier(TargetModifiers.ForceAttack)) { - var maxRange = ab.GetMaximumRange().Range; var targetRange = (self.World.Map.CenterOfCell(location) - self.CenterPosition).HorizontalLengthSquared; - if (targetRange > maxRange * maxRange) + if (targetRange > ab.GetMaximumRange().RangeSquared) cursor = ab.Info.OutsideRangeCursor; return true; diff --git a/OpenRA.Mods.Common/Traits/Repairable.cs b/OpenRA.Mods.Common/Traits/Repairable.cs index bd3d2c4648..665432616c 100644 --- a/OpenRA.Mods.Common/Traits/Repairable.cs +++ b/OpenRA.Mods.Common/Traits/Repairable.cs @@ -141,7 +141,7 @@ namespace OpenRA.Mods.Common.Traits return; var targetCell = self.World.Map.CellContaining(target.CenterPosition); - if ((self.CenterPosition - target.CenterPosition).LengthSquared < transport.MinimumDistance.Range * transport.MinimumDistance.Range) + if ((self.CenterPosition - target.CenterPosition).LengthSquared < transport.MinimumDistance.RangeSquared) return; transport.RequestTransport(targetCell, nextActivity); diff --git a/OpenRA.Mods.Common/Traits/Upgrades/UpgradeActorsNear.cs b/OpenRA.Mods.Common/Traits/Upgrades/UpgradeActorsNear.cs index 7ff4abf0ff..5a95443dc0 100644 --- a/OpenRA.Mods.Common/Traits/Upgrades/UpgradeActorsNear.cs +++ b/OpenRA.Mods.Common/Traits/Upgrades/UpgradeActorsNear.cs @@ -106,7 +106,7 @@ namespace OpenRA.Mods.Common.Traits public void UnitProducedByOther(Actor self, Actor producer, Actor produced) { // Work around for actors produced within the region not triggering until the second tick - if ((produced.CenterPosition - self.CenterPosition).HorizontalLengthSquared <= info.Range.Range * info.Range.Range) + if ((produced.CenterPosition - self.CenterPosition).HorizontalLengthSquared <= info.Range.RangeSquared) { var stance = self.Owner.Stances[produced.Owner]; if (!info.ValidStances.HasFlag(stance)) diff --git a/OpenRA.Mods.Common/Traits/World/PathFinder.cs b/OpenRA.Mods.Common/Traits/World/PathFinder.cs index 332aaa6b12..f1890e6b8f 100644 --- a/OpenRA.Mods.Common/Traits/World/PathFinder.cs +++ b/OpenRA.Mods.Common/Traits/World/PathFinder.cs @@ -85,7 +85,6 @@ namespace OpenRA.Mods.Common.Traits { var mi = self.Info.Traits.Get(); var targetCell = world.Map.CellContaining(target); - var rangeSquared = range.Range * range.Range; // Correct for SubCell offset target -= world.Map.OffsetOfSubCell(srcSub); @@ -93,7 +92,7 @@ namespace OpenRA.Mods.Common.Traits // Select only the tiles that are within range from the requested SubCell // This assumes that the SubCell does not change during the path traversal var tilesInRange = world.Map.FindTilesInCircle(targetCell, range.Range / 1024 + 1) - .Where(t => (world.Map.CenterOfCell(t) - target).LengthSquared <= rangeSquared + .Where(t => (world.Map.CenterOfCell(t) - target).LengthSquared <= range.RangeSquared && mi.CanEnterCell(self.World as World, self as Actor, t)); // See if there is any cell within range that does not involve a cross-domain request diff --git a/OpenRA.Mods.Common/Warheads/CreateEffectWarhead.cs b/OpenRA.Mods.Common/Warheads/CreateEffectWarhead.cs index 968bf0150f..610f9a8eeb 100644 --- a/OpenRA.Mods.Common/Warheads/CreateEffectWarhead.cs +++ b/OpenRA.Mods.Common/Warheads/CreateEffectWarhead.cs @@ -66,7 +66,7 @@ namespace OpenRA.Mods.Common.Warheads continue; // If the impact position is within any actor's health radius, we have a direct hit - if ((unit.CenterPosition - pos).LengthSquared <= healthInfo.Radius.Range * healthInfo.Radius.Range) + if ((unit.CenterPosition - pos).LengthSquared <= healthInfo.Radius.RangeSquared) return true; } diff --git a/OpenRA.Mods.D2k/Traits/Carryable.cs b/OpenRA.Mods.D2k/Traits/Carryable.cs index 651cdc3afd..30302305b5 100644 --- a/OpenRA.Mods.D2k/Traits/Carryable.cs +++ b/OpenRA.Mods.D2k/Traits/Carryable.cs @@ -56,7 +56,7 @@ namespace OpenRA.Mods.D2k.Traits public void RequestTransport(CPos destination, Activity afterLandActivity) { var destPos = self.World.Map.CenterOfCell(destination); - if (destination == CPos.Zero || (self.CenterPosition - destPos).LengthSquared < info.MinDistance.Range * info.MinDistance.Range) + if (destination == CPos.Zero || (self.CenterPosition - destPos).LengthSquared < info.MinDistance.RangeSquared) { WantsTransport = false; // Be sure to cancel any pending transports return; @@ -120,7 +120,7 @@ namespace OpenRA.Mods.D2k.Traits return false; var destPos = self.World.Map.CenterOfCell(Destination); - if ((self.CenterPosition - destPos).LengthSquared < info.MinDistance.Range * info.MinDistance.Range) + if ((self.CenterPosition - destPos).LengthSquared < info.MinDistance.RangeSquared) { MovementCancelled(self); return false; @@ -148,7 +148,7 @@ namespace OpenRA.Mods.D2k.Traits // Last change to change our mind... var destPos = self.World.Map.CenterOfCell(Destination); - if ((self.CenterPosition - destPos).LengthSquared < info.MinDistance.Range * info.MinDistance.Range) + if ((self.CenterPosition - destPos).LengthSquared < info.MinDistance.RangeSquared) { MovementCancelled(self); return false;