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); 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) public static IEnumerable<Actor> FindUnitsInCircle(this World world, int2 a, int r)
{ {
using (new PerfSample("FindUnitsInCircle")) using (new PerfSample("FindUnitsInCircle"))

View File

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

View File

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

View File

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

View File

@@ -305,7 +305,7 @@ namespace OpenRA.Mods.RA
{ {
// Found enemy units nearby. // Found enemy units nearby.
foundEnemy = true; 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... // Check how many own units we have gathered nearby...
var ownUnits = world.FindUnitsInCircle(a1.CenterLocation, Game.CellSize * 2) 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 // TODO exclude other NeutralActor that arent permanent
Actor GetInRange(Actor self) Actor GetInRange(Actor self)
{ {
return CaptorsInRange(self).OrderBy(a => (a.CenterLocation - self.CenterLocation).LengthSquared) return CaptorsInRange(self).ClosestTo( self.CenterLocation );
.FirstOrDefault();
} }
int CountPlayersNear(Actor self, Player ignoreMe) int CountPlayersNear(Actor self, Player ignoreMe)