smart queries for World.Actors

This commit is contained in:
Bob
2010-01-31 01:27:50 +13:00
parent c012cf3c7f
commit 73c16d5d9d
23 changed files with 201 additions and 64 deletions

View File

@@ -42,8 +42,9 @@ namespace OpenRa.Traits.Activities
umt = mobile.GetMovementType(),
checkForBlocked = false,
};
var refineries = self.World.Actors.Where( x => x.traits.Contains<AcceptsOre>()
&& x.Owner == self.Owner ).ToList();
var refineries = self.World.Queries.OwnedBy[self.Owner]
.Where( x => x.traits.Contains<AcceptsOre>())
.ToList();
if( refinery != null )
search.AddInitialCell( self.World, refinery.Location + refineryDeliverOffset );
else

View File

@@ -9,9 +9,8 @@ namespace OpenRa.Traits.Activities
static Actor ChooseHelipad(Actor self)
{
return self.World.Actors.FirstOrDefault(
return self.World.Queries.OwnedBy[self.Owner].FirstOrDefault(
a => a.Info.Name == "hpad" &&
a.Owner == self.Owner &&
!Reservable.IsReserved(a));
}

View File

@@ -18,9 +18,8 @@ namespace OpenRa.Traits.Activities
Actor ChooseAirfield(Actor self)
{
var airfield = self.World.Actors
var airfield = self.World.Queries.OwnedBy[self.Owner]
.Where(a => a.Info.Name == "afld"
&& a.Owner == self.Owner
&& !Reservable.IsReserved(a))
.FirstOrDefault();

View File

@@ -46,8 +46,10 @@ namespace OpenRa.Traits
if (!movement.CanEnterCell(order.TargetLocation))
return;
var chronosphere = self.World.Actors.Where(a => a.Owner == self.Owner
&& a.traits.Contains<Chronosphere>()).FirstOrDefault();
var chronosphere = self.World.Queries
.OwnedBy[self.Owner]
.WithTrait<Chronosphere>()
.Select(x=>x.Actor).FirstOrDefault();
bool success = order.TargetActor.traits.Get<Chronoshiftable>().Activate(order.TargetActor,
order.TargetLocation,
@@ -60,8 +62,8 @@ namespace OpenRa.Traits
Sound.Play("chrono2.aud");
// Trigger screen desaturate effect
foreach (var a in self.World.Actors.Where(a => a.traits.Contains<ChronoshiftPaletteEffect>()))
a.traits.Get<ChronoshiftPaletteEffect>().DoChronoshift();
foreach (var a in self.World.Queries.WithTrait<ChronoshiftPaletteEffect>())
a.Trait.DoChronoshift();
if (chronosphere != null)
chronosphere.traits.Get<RenderBuilding>().PlayCustomAnim(chronosphere, "active");
@@ -98,8 +100,9 @@ namespace OpenRa.Traits
public void Tick( World world )
{
var hasChronosphere = world.Actors
.Any(a => a.Owner == world.LocalPlayer && a.traits.Contains<Chronosphere>());
var hasChronosphere = world.Queries.OwnedBy[world.LocalPlayer]
.WithTrait<Chronosphere>()
.Any();
if (!hasChronosphere)
Game.controller.CancelInputMode();
@@ -135,8 +138,9 @@ namespace OpenRa.Traits
public void Tick(World world)
{
var hasChronosphere = world.Actors
.Any(a => a.Owner == world.LocalPlayer && a.traits.Contains<Chronosphere>());
var hasChronosphere = world.Queries.OwnedBy[world.LocalPlayer]
.WithTrait<Chronosphere>()
.Any();
if (!hasChronosphere)
Game.controller.CancelInputMode();

View File

@@ -26,8 +26,9 @@ namespace OpenRa.Traits
{
if (order.OrderString == "NuclearMissile")
{
var silo = self.World.Actors.Where(a => a.Owner == self.Owner
&& a.traits.Contains<NukeSilo>()).FirstOrDefault();
var silo = self.World.Queries.OwnedBy[self.Owner]
.Where(a => a.traits.Contains<NukeSilo>())
.FirstOrDefault();
if (silo != null)
silo.traits.Get<RenderBuilding>().PlayCustomAnim(silo, "active");
@@ -70,8 +71,9 @@ namespace OpenRa.Traits
public void Tick(World world)
{
var hasStructure = world.Actors
.Any(a => a.Owner == world.LocalPlayer && a.traits.Contains<NukeSilo>());
var hasStructure = world.Queries.OwnedBy[world.LocalPlayer]
.WithTrait<NukeSilo>()
.Any();
if (!hasStructure)
Game.controller.CancelInputMode();

View File

@@ -86,12 +86,12 @@ namespace OpenRa.Traits
// Cancel existing primaries
foreach (var p in self.Info.Traits.Get<ProductionInfo>().Produces)
{
foreach (var b in self.World.Actors.Where(x => x.traits.Contains<Production>()
&& x.Owner == self.Owner
&& x.traits.Get<Production>().IsPrimary == true
&& (x.Info.Traits.Get<ProductionInfo>().Produces.Contains(p))))
foreach (var b in self.World.Queries.OwnedBy[self.Owner]
.WithTrait<Production>()
.Where(x => x.Trait.IsPrimary
&& (x.Actor.Info.Traits.Get<ProductionInfo>().Produces.Contains(p))))
{
b.traits.Get<Production>().SetPrimaryProducer(b, false);
b.Trait.SetPrimaryProducer(b.Actor, false);
}
}
isPrimary = true;

View File

@@ -131,18 +131,17 @@ namespace OpenRa.Traits
Actor producer = null;
// Prioritise primary structure in build order
var primaryProducers = self.World.Actors
.Where(x => x.traits.Contains<Production>()
&& producerTypes.Contains(x.Info)
&& x.Owner == self.Owner
&& x.traits.Get<Production>().IsPrimary == true);
var primaryProducers = self.World.Queries.OwnedBy[self.Owner]
.WithTrait<Production>()
.Where(x => producerTypes.Contains(x.Actor.Info)
&& x.Trait.IsPrimary);
foreach (var p in primaryProducers)
{
// Ignore buildings that are disabled
if (p.traits.Contains<Building>() && p.traits.Get<Building>().Disabled)
if (p.Actor.traits.Contains<Building>() && p.Actor.traits.Get<Building>().Disabled)
continue;
producer = p;
producer = p.Actor;
break;
}
@@ -152,8 +151,8 @@ namespace OpenRa.Traits
// Pick the first available producer
if (producer == null)
{
producer = self.World.Actors
.Where( x => producerTypes.Contains( x.Info ) && x.Owner == self.Owner )
producer = self.World.Queries.OwnedBy[self.Owner]
.Where( x => producerTypes.Contains( x.Info ) )
.FirstOrDefault();
}

View File

@@ -16,9 +16,8 @@ namespace OpenRa.Traits
var b = self.traits.Get<Building>();
if (b != null && b.Disabled) return false;
var isJammed = self.World.Actors.Any(a => a.traits.Contains<JamsRadar>()
&& self.Owner != a.Owner
&& (self.Location - a.Location).Length < a.Info.Traits.Get<JamsRadarInfo>().Range);
var isJammed = self.World.Queries.WithTrait<JamsRadar>().Any(a => self.Owner != a.Actor.Owner
&& (self.Location - a.Actor.Location).Length < a.Actor.Info.Traits.Get<JamsRadarInfo>().Range);
return !isJammed;
}

View File

@@ -30,8 +30,9 @@ namespace OpenRa.Traits
// Does this belong here? NO, but it's your mess.
// Get the crushable actors
foreach (var a in self.World.Actors.Where(b => b.traits.Contains<ICrushable>()))
foreach (var aa in self.World.Queries.WithTrait<ICrushable>())
{
var a = aa.Actor;
// Are there any units in the same cell that can crush this?
foreach( var ios in a.traits.WithInterface<IOccupySpace>() )
foreach( var cell in ios.OccupiedCells() )