From e6865c5996bc718e44be0318a237e0099bd49cb0 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 20 Jul 2013 13:53:28 +1200 Subject: [PATCH] Remove PPos overload of FindUnitsInCircle. --- OpenRA.Game/WorldUtils.cs | 27 +++++++++------------- OpenRA.Mods.RA/Activities/FindResources.cs | 18 ++++----------- OpenRA.Mods.RA/Air/Helicopter.cs | 3 ++- OpenRA.Mods.RA/Combat.cs | 3 ++- OpenRA.Mods.RA/ProximityCapturable.cs | 2 +- 5 files changed, 21 insertions(+), 32 deletions(-) diff --git a/OpenRA.Game/WorldUtils.cs b/OpenRA.Game/WorldUtils.cs index a924accbfe..9c866cdbab 100755 --- a/OpenRA.Game/WorldUtils.cs +++ b/OpenRA.Game/WorldUtils.cs @@ -55,25 +55,20 @@ namespace OpenRA return actors.OrderBy(a => (a.CenterPosition - pos).LengthSquared).FirstOrDefault(); } - public static IEnumerable FindUnitsInCircle(this World world, WPos a, WRange r) - { - return world.FindUnitsInCircle(PPos.FromWPos(a), r.Range * Game.CellSize / 1024); - } - - public static IEnumerable FindUnitsInCircle(this World world, PPos a, int r) + public static IEnumerable FindUnitsInCircle(this World world, WPos origin, WRange r) { using (new PerfSample("FindUnitsInCircle")) { - var min = a - PVecInt.FromRadius(r); - var max = a + PVecInt.FromRadius(r); - - var actors = world.FindUnits(min, max); - - var rect = new Rectangle(min.X, min.Y, max.X - min.X, max.Y - min.Y); - - var inBox = actors.Where(x => x.ExtendedBounds.Value.IntersectsWith(rect)); - - return inBox.Where(x => (x.CenterLocation - a).LengthSquared < r * r); + // 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.FindUnits(origin - vec, origin + vec).Where(a => + { + var pos = a.CenterPosition; + var dx = (long)(pos.X - origin.X); + var dy = (long)(pos.Y - origin.Y); + return dx*dx + dy*dy <= rSq; + }); } } diff --git a/OpenRA.Mods.RA/Activities/FindResources.cs b/OpenRA.Mods.RA/Activities/FindResources.cs index 4860ebe0a2..895f3d0354 100755 --- a/OpenRA.Mods.RA/Activities/FindResources.cs +++ b/OpenRA.Mods.RA/Activities/FindResources.cs @@ -52,21 +52,13 @@ namespace OpenRA.Mods.RA.Activities int searchRadiusSquared = searchRadius * searchRadius; // Find harvestable resources nearby: + // Avoid enemy territory: + // TODO: calculate weapons ranges of units and factor those in instead of hard-coding 8. var path = self.World.WorldActor.Trait().FindPath( PathSearch.Search(self.World, mobileInfo, self, true) - .WithCustomCost(loc => - { - // Avoid enemy territory: - int safetycost = ( - // TODO: calculate weapons ranges of units and factor those in instead of hard-coding 8. - from u in self.World.FindUnitsInCircle(loc.ToPPos(), Game.CellSize * 8) - where !u.Destroyed - where self.Owner.Stances[u.Owner] == Stance.Enemy - select Math.Max(0, 64 - (loc - u.Location).LengthSquared) - ).Sum(); - - return safetycost; - }) + .WithCustomCost(loc => self.World.FindUnitsInCircle(loc.CenterPosition, WRange.FromCells(8)) + .Where (u => !u.Destroyed && self.Owner.Stances[u.Owner] == Stance.Enemy) + .Sum(u => Math.Max(0, 64 - (loc - u.Location).LengthSquared))) .WithHeuristic(loc => { // Avoid this cell: diff --git a/OpenRA.Mods.RA/Air/Helicopter.cs b/OpenRA.Mods.RA/Air/Helicopter.cs index d0f18f4bea..698b6b9d4e 100755 --- a/OpenRA.Mods.RA/Air/Helicopter.cs +++ b/OpenRA.Mods.RA/Air/Helicopter.cs @@ -113,7 +113,8 @@ namespace OpenRA.Mods.RA.Air /* repulsion only applies when we're flying */ if (Altitude <= 0) return; - var otherHelis = self.World.FindUnitsInCircle(self.CenterLocation, Info.IdealSeparation) + var separation = new WRange(Info.IdealSeparation * 1024 / Game.CellSize); + var otherHelis = self.World.FindUnitsInCircle(self.CenterPosition, separation) .Where(a => a.HasTrait()); var f = otherHelis diff --git a/OpenRA.Mods.RA/Combat.cs b/OpenRA.Mods.RA/Combat.cs index 956badf6b0..7ccc5577f4 100755 --- a/OpenRA.Mods.RA/Combat.cs +++ b/OpenRA.Mods.RA/Combat.cs @@ -105,7 +105,8 @@ namespace OpenRA.Mods.RA case DamageModel.Normal: { var maxSpread = warhead.Spread * (float)Math.Log(Math.Abs(warhead.Damage), 2); - var hitActors = world.FindUnitsInCircle(args.dest, (int)maxSpread); + var range = new WRange((int)maxSpread * 1024 / Game.CellSize); + var hitActors = world.FindUnitsInCircle(args.dest.ToWPos(0), range); foreach (var victim in hitActors) { diff --git a/OpenRA.Mods.RA/ProximityCapturable.cs b/OpenRA.Mods.RA/ProximityCapturable.cs index 64e46935de..c86aa4a4f8 100644 --- a/OpenRA.Mods.RA/ProximityCapturable.cs +++ b/OpenRA.Mods.RA/ProximityCapturable.cs @@ -109,7 +109,7 @@ namespace OpenRA.Mods.RA IEnumerable UnitsInRange() { - return Self.World.FindUnitsInCircle(Self.CenterLocation, Game.CellSize * Info.Range) + return Self.World.FindUnitsInCircle(Self.CenterPosition, WRange.FromCells(Info.Range)) .Where(a => a.IsInWorld && a != Self && !a.Destroyed) .Where(a => !a.Owner.NonCombatant); }