Add ActorsHavingTrait<TTrait>([Func<TTrait, bool])

This commit is contained in:
atlimit8
2015-10-25 04:45:53 -05:00
parent 0997f5b52f
commit b6f17df260
27 changed files with 132 additions and 125 deletions

View File

@@ -36,14 +36,12 @@ namespace OpenRA.Mods.Common.Traits
// nowhere to land, pick something friendly and circle over it.
// i'd prefer something we own
var someBuilding = self.World.ActorsWithTrait<Building>()
.Select(a => a.Actor)
var someBuilding = self.World.ActorsHavingTrait<Building>()
.FirstOrDefault(a => a.Owner == self.Owner);
// failing that, something unlikely to shoot at us
if (someBuilding == null)
someBuilding = self.World.ActorsWithTrait<Building>()
.Select(a => a.Actor)
someBuilding = self.World.ActorsHavingTrait<Building>()
.FirstOrDefault(a => self.Owner.Stances[a.Owner] == Stance.Ally);
if (someBuilding == null)

View File

@@ -72,10 +72,9 @@ namespace OpenRA.Mods.Common.Traits
continue; // Cell is empty; continue search
// Cell contains an actor. Is it the type we want?
if (world.ActorsWithTrait<LineBuildNode>().Any(a =>
(a.Actor.Location == cell &&
a.Actor.Info.TraitInfo<LineBuildNodeInfo>()
.Types.Overlaps(lbi.NodeTypes))))
if (world.ActorsHavingTrait<LineBuildNode>()
.Any(a => a.Location == cell
&& a.Info.TraitInfo<LineBuildNodeInfo>().Types.Overlaps(lbi.NodeTypes)))
dirs[d] = i; // Cell contains actor of correct type
else
dirs[d] = -1; // Cell is blocked by another actor type

View File

@@ -36,8 +36,8 @@ namespace OpenRA.Mods.Common.Traits
if (!CanGiveTo(collector))
return 0;
var hasBase = collector.World.ActorsWithTrait<BaseBuilding>()
.Any(a => a.Actor.Owner == collector.Owner);
var hasBase = collector.World.ActorsHavingTrait<BaseBuilding>()
.Any(a => a.Owner == collector.Owner);
return hasBase ? info.SelectionShares : info.NoBaseSelectionShares;
}

View File

@@ -114,9 +114,8 @@ namespace OpenRA.Mods.Common.Traits
if (proc == null) return;
if (proc.Disposed) return;
var linkedHarvs = proc.World.ActorsWithTrait<Harvester>()
.Where(a => a.Trait.LinkedProc == proc)
.Select(a => Target.FromActor(a.Actor))
var linkedHarvs = proc.World.ActorsHavingTrait<Harvester>(h => h.LinkedProc == proc)
.Select(a => Target.FromActor(a))
.ToList();
proc.SetTargetLines(linkedHarvs, Color.Gold);
@@ -163,7 +162,7 @@ namespace OpenRA.Mods.Common.Traits
.Select(r => new {
Location = r.Actor.Location + r.Trait.DeliveryOffset,
Actor = r.Actor,
Occupancy = self.World.ActorsWithTrait<Harvester>().Count(a => a.Trait.LinkedProc == r.Actor) })
Occupancy = self.World.ActorsHavingTrait<Harvester>(h => h.LinkedProc == r.Actor).Count() })
.ToDictionary(r => r.Location);
// Start a search from each refinery's delivery location:

View File

@@ -133,10 +133,10 @@ namespace OpenRA.Mods.Common.Traits
{
var type = bi.BuildAtProductionType ?? info.Type;
var selfsameBuildingsCount = self.World.ActorsWithTrait<Production>()
var selfsameProductionsCount = self.World.ActorsWithTrait<Production>()
.Count(p => p.Actor.Owner == self.Owner && p.Trait.Info.Produces.Contains(type));
var speedModifier = selfsameBuildingsCount.Clamp(1, info.BuildTimeSpeedReduction.Length) - 1;
var speedModifier = selfsameProductionsCount.Clamp(1, info.BuildTimeSpeedReduction.Length) - 1;
time = (time * info.BuildTimeSpeedReduction[speedModifier]) / 100;
}

View File

@@ -266,7 +266,7 @@ namespace OpenRA.Mods.Common.Traits
if (!developerMode.AllTech && bi.BuildLimit > 0)
{
var inQueue = queue.Count(pi => pi.Item == order.TargetString);
var owned = self.Owner.World.ActorsWithTrait<Buildable>().Count(a => a.Actor.Info.Name == order.TargetString && a.Actor.Owner == self.Owner);
var owned = self.Owner.World.ActorsHavingTrait<Buildable>().Count(a => a.Info.Name == order.TargetString && a.Owner == self.Owner);
fromLimit = bi.BuildLimit - (inQueue + owned);
if (fromLimit <= 0)

View File

@@ -56,13 +56,13 @@ namespace OpenRA.Mods.Common.Traits
mo = self.Trait<MissionObjectives>();
}
public IEnumerable<TraitPair<StrategicPoint>> AllPoints
public IEnumerable<Actor> AllPoints
{
get { return player.World.ActorsWithTrait<StrategicPoint>(); }
get { return player.World.ActorsHavingTrait<StrategicPoint>(); }
}
public int Total { get { return AllPoints.Count(); } }
int Owned { get { return AllPoints.Count(a => WorldUtils.AreMutualAllies(player, a.Actor.Owner)); } }
int Owned { get { return AllPoints.Count(a => WorldUtils.AreMutualAllies(player, a.Owner)); } }
public bool Holding { get { return Owned >= info.RatioRequired * Total; } }

View File

@@ -138,8 +138,7 @@ namespace OpenRA.Mods.Common.Traits
void UpdatePowerOutageActors()
{
var actors = self.World.ActorsWithTrait<AffectedByPowerOutage>()
.Select(tp => tp.Actor)
var actors = self.World.ActorsHavingTrait<AffectedByPowerOutage>()
.Where(a => !a.IsDead && a.IsInWorld && a.Owner == self.Owner);
foreach (var a in actors)