fixed #999 -- replace order/first[ordefault] with .ClosestTo(point)

This commit is contained in:
Chris Forbes
2011-07-08 23:01:05 +12:00
committed by Paul Chote
parent 5c3a6c81ae
commit e44b965785
7 changed files with 18 additions and 18 deletions

View File

@@ -34,6 +34,11 @@ namespace OpenRA
return world.WorldActor.Trait<SpatialBins>().ActorsInBox(u,v);
}
public static Actor ClosestTo( this IEnumerable<Actor> actors, int2 px )
{
return actors.OrderBy( a => (a.CenterLocation - px).LengthSquared ).FirstOrDefault();
}
public static IEnumerable<Actor> FindUnitsInCircle(this World world, int2 a, int r)
{
using (new PerfSample("FindUnitsInCircle"))

View File

@@ -32,15 +32,13 @@ namespace OpenRA.Mods.RA.Activities
var buildings = self.Info.Traits.Get<MinelayerInfo>().RearmBuildings;
var rearmTarget = self.World.Actors.Where(a => a.Owner != null && self.Owner.Stances[a.Owner] == Stance.Ally
&& buildings.Contains(a.Info.Name))
.OrderBy( a => (a.Location - self.Location).LengthSquared )
.FirstOrDefault();
.ClosestTo( self.CenterLocation );
if (rearmTarget == null)
return new Wait(20);
return Util.SequenceActivities(
new Enter(rearmTarget),
//new Move(Util.CellContaining(rearmTarget.CenterLocation), rearmTarget),
new Rearm(self),
new Repair(rearmTarget),
this );

View File

@@ -25,13 +25,13 @@ namespace OpenRA.Mods.RA.Air
public static Actor ChooseAirfield(Actor self)
{
var rearmBuildings = self.Info.Traits.Get<PlaneInfo>().RearmBuildings;
return self.World.ActorsWithTrait<Reservable>()
.Where(a => a.Actor.Owner == self.Owner)
.Where(a => self.Info.Traits.Get<PlaneInfo>().RearmBuildings.Contains(a.Actor.Info.Name)
.Where(a => rearmBuildings.Contains(a.Actor.Info.Name)
&& !Reservable.IsReserved(a.Actor))
.OrderBy(a => (a.Actor.CenterLocation - self.CenterLocation).LengthSquared)
.Select(a => a.Actor)
.FirstOrDefault();
.ClosestTo( self.CenterLocation );
}
void Calculate(Actor self)

View File

@@ -28,8 +28,7 @@ namespace OpenRA.Mods.RA
.Where(a => a.IsInWorld && !a.IsDead())
.Where(a => a.HasTrait<Health>() && a.GetDamageState() > DamageState.Undamaged)
.Where(a => attack.HasAnyValidWeapons(Target.FromActor(a)))
.OrderBy(a => (a.CenterLocation - self.CenterLocation).LengthSquared)
.FirstOrDefault();
.ClosestTo( self.CenterLocation );
if( target != null )
self.QueueActivity(self.Trait<AttackBase>().GetAttackActivity(self, Target.FromActor( target ), false ));

View File

@@ -101,8 +101,7 @@ namespace OpenRA.Mods.RA
.Where(a => a.Owner != null && a.AppearsHostileTo(self))
.Where(a => !a.HasTrait<AutoTargetIgnore>())
.Where(a => attack.HasAnyValidWeapons(Target.FromActor(a)))
.OrderBy(a => (a.CenterLocation - self.CenterLocation).LengthSquared)
.FirstOrDefault();
.ClosestTo( self.CenterLocation );
}
}

View File

@@ -305,7 +305,7 @@ namespace OpenRA.Mods.RA
{
// Found enemy units nearby.
foundEnemy = true;
var enemy = enemyUnits.OrderBy(e => (e.Location - a1.Location).LengthSquared).First();
var enemy = enemyUnits.ClosestTo( a1.CenterLocation );
// Check how many own units we have gathered nearby...
var ownUnits = world.FindUnitsInCircle(a1.CenterLocation, Game.CellSize * 2)

View File

@@ -174,8 +174,7 @@ namespace OpenRA.Mods.RA
// TODO exclude other NeutralActor that arent permanent
Actor GetInRange(Actor self)
{
return CaptorsInRange(self).OrderBy(a => (a.CenterLocation - self.CenterLocation).LengthSquared)
.FirstOrDefault();
return CaptorsInRange(self).ClosestTo( self.CenterLocation );
}
int CountPlayersNear(Actor self, Player ignoreMe)