Merge pull request #5405 from RoosterDragon/min-max

Added MinBy, MaxBy, etc.
This commit is contained in:
Paul Chote
2014-06-09 17:42:01 +12:00
16 changed files with 90 additions and 47 deletions

View File

@@ -298,7 +298,7 @@ namespace OpenRA.Mods.RA.AI
// Try to maintain 20% excess power
if (!HasAdequatePower())
return buildableThings.Where(a => GetPowerProvidedBy(a) > 0)
.OrderByDescending(a => GetPowerProvidedBy(a)).FirstOrDefault();
.MaxByOrDefault(a => GetPowerProvidedBy(a));
if (playerResource.AlertSilo)
return GetBuildingInfoByCommonName("Silo", p);
@@ -365,9 +365,13 @@ namespace OpenRA.Mods.RA.AI
case BuildingType.Refinery:
var tilesPos = world.FindTilesInCircle(baseCenter, MaxBaseDistance)
.Where(a => resourceTypes.Contains(world.GetTerrainType(new CPos(a.X, a.Y))))
.OrderBy(a => (a.CenterPosition - baseCenter.CenterPosition).LengthSquared);
return tilesPos.Any() ? findPos(tilesPos.First().CenterPosition, baseCenter) : null;
.Where(a => resourceTypes.Contains(world.GetTerrainType(new CPos(a.X, a.Y))));
if (tilesPos.Any())
{
var pos = tilesPos.MinBy(a => (a.CenterPosition - baseCenter.CenterPosition).LengthSquared);
return findPos(pos.CenterPosition, baseCenter);
}
return null;
case BuildingType.Building:
for (var k = 0; k < maxBaseDistance; k++)
@@ -426,8 +430,7 @@ namespace OpenRA.Mods.RA.AI
var leastLikedEnemies = liveEnemies
.GroupBy(e => aggro[e].Aggro)
.OrderByDescending(g => g.Key)
.FirstOrDefault();
.MaxByOrDefault(g => g.Key);
var enemy = (leastLikedEnemies != null) ?
leastLikedEnemies.Random(random) : liveEnemies.FirstOrDefault();
@@ -457,12 +460,9 @@ namespace OpenRA.Mods.RA.AI
{
var allEnemyUnits = world.Actors
.Where(unit => p.Stances[unit.Owner] == Stance.Enemy && !unit.HasTrait<Husk>() &&
unit.HasTrait<ITargetable>()).ToList();
unit.HasTrait<ITargetable>());
if (allEnemyUnits.Count > 0)
return allEnemyUnits.ClosestTo(pos);
return null;
return allEnemyUnits.ClosestTo(pos);
}
internal Actor FindClosestEnemy(WPos pos, WRange radius)

View File

@@ -93,7 +93,7 @@ namespace OpenRA.Mods.RA.Activities
var restrictTo = maximumDistance == null ? null : self.World.FindTilesInCircle(self.Location, maximumDistance.Value);
if (maximumDistance != null)
destination = restrictTo.OrderBy(x => (x - destination).Length).First();
destination = restrictTo.MinBy(x => (x - destination).LengthSquared);
var pos = self.Trait<IPositionable>();
if (pos.CanEnterCell(destination) && self.Owner.Shroud.IsExplored(destination))

View File

@@ -69,8 +69,7 @@ namespace OpenRA.Mods.RA.Air
var side = new WVec(-fwd.Y, fwd.X, fwd.Z);
var approachDelta = self.CenterPosition - approachStart;
var sideTowardBase = new[] { side, -side }
.OrderBy(a => WVec.Dot(a, approachDelta))
.First();
.MinBy(a => WVec.Dot(a, approachDelta));
// Calculate the tangent line that joins the turning circles at the current and approach positions
var cp = self.CenterPosition + turnRadius * sideTowardBase / 1024;

View File

@@ -77,9 +77,7 @@ namespace OpenRA.Mods.RA
? world.SharedRandom.Next(available.Count)
: available // pick the most distant spawnpoint from everyone else
.Select((k, i) => Pair.New(k, i))
.OrderByDescending(a => taken.Sum(t => (t - a.First).LengthSquared))
.Select(a => a.Second)
.First();
.MaxBy(a => taken.Sum(t => (t - a.First).LengthSquared)).Second;
var sp = available[n];
available.RemoveAt(n);

View File

@@ -134,9 +134,8 @@ namespace OpenRA.Mods.RA
var underCursor = world.ScreenMap.ActorsAt(mi)
.Where(a => !world.FogObscures(a))
.OrderByDescending(a => a.Info.Traits.Contains<SelectableInfo>()
? a.Info.Traits.Get<SelectableInfo>().Priority : int.MinValue)
.FirstOrDefault();
.MaxByOrDefault(a => a.Info.Traits.Contains<SelectableInfo>()
? a.Info.Traits.Get<SelectableInfo>().Priority : int.MinValue);
if (mi.Button == Game.mouseButtonPreference.Action && underCursor == null)
{

View File

@@ -132,7 +132,7 @@ namespace OpenRA.Mods.RA
while ((to - z).X > 5 || (to - z).X < -5 || (to - z).Y > 5 || (to - z).Y < -5)
{
var step = steps.Where(t => (to - (z + new float2(t[0], t[1]))).LengthSquared < (to - z).LengthSquared)
.OrderBy(t => Math.Abs(float2.Dot(z + new float2(t[0], t[1]), q) + c)).First();
.MinBy(t => Math.Abs(float2.Dot(z + new float2(t[0], t[1]), q) + c));
var pos = wr.Position((z + new float2(step[2], step[3])).ToInt2());
rs.Add(new SpriteRenderable(s.GetSprite(step[4]), pos, WVec.Zero, 0, pal, 1f, true));