remove most of the crap from queries

This commit is contained in:
Chris Forbes
2011-03-17 21:48:50 +13:00
parent eba7641125
commit 149324ea92
21 changed files with 113 additions and 129 deletions

View File

@@ -83,9 +83,8 @@ namespace OpenRA.Orders
public override void Tick(World world)
{
var hasStructure = world.Queries.OwnedBy[world.LocalPlayer]
.WithTrait<T>()
.Any();
var hasStructure = world.Queries.WithTrait<T>()
.Any( a => a.Actor.Owner == world.LocalPlayer );
if (!hasStructure)
world.CancelInputMode();

View File

@@ -132,7 +132,8 @@ namespace OpenRA.Traits
{
var eva = self.World.WorldActor.Info.Traits.Get<EvaAlertsInfo>();
OreCapacity = self.World.Queries.OwnedBy[Owner].WithTrait<IStoreOre>()
OreCapacity = self.World.Queries.WithTrait<IStoreOre>()
.Where(a => a.Actor.Owner == Owner)
.Sum(a => a.Trait.Capacity);
if (Ore > OreCapacity)

View File

@@ -126,7 +126,7 @@ namespace OpenRA.Traits
}
// Is now our ally; add unit vis
if (newStance == Stance.Ally)
foreach (var a in w.Queries.OwnedBy[player])
foreach (var a in w.Actors.Where( a => a.Owner == player ))
AddActor(a);
}

View File

@@ -208,46 +208,15 @@ namespace OpenRA
{
readonly World world;
public readonly Cache<Player, OwnedByCachedView> OwnedBy;
public AllQueries( World world )
{
this.world = world;
OwnedBy = new Cache<Player, OwnedByCachedView>(p => new OwnedByCachedView(world, world.actors, x => x.Owner == p));
}
public IEnumerable<TraitPair<T>> WithTrait<T>()
{
return world.traitDict.ActorsWithTraitMultiple<T>( world );
}
static CachedView<Actor, TraitPair<T>> WithTraitInner<T>( Set<Actor> set, TypeDictionary hasTrait )
{
var ret = hasTrait.GetOrDefault<CachedView<Actor, TraitPair<T>>>();
if( ret != null )
return ret;
ret = new CachedView<Actor, TraitPair<T>>(
set,
x => x.HasTrait<T>(),
x => new TraitPair<T> { Actor = x, Trait = x.Trait<T>() } );
hasTrait.Add( ret );
return ret;
}
public class OwnedByCachedView : CachedView<Actor, Actor>
{
readonly TypeDictionary hasTrait = new TypeDictionary();
public OwnedByCachedView( World world, Set<Actor> set, Func<Actor, bool> include )
: base( set, include, a => a )
{
}
public CachedView<Actor, TraitPair<T>> WithTrait<T>()
{
return WithTraitInner<T>( this, hasTrait );
}
}
}
public AllQueries Queries;

View File

@@ -46,6 +46,8 @@ namespace OpenRA.Mods.Cnc
}));
}
// THIS IS SHIT
public void OnVictory(World w)
{
started = false;
@@ -54,7 +56,8 @@ namespace OpenRA.Mods.Cnc
w.WorldActor.CancelActivity();
w.WorldActor.QueueActivity(new Wait(125));
w.WorldActor.QueueActivity(new CallFunc(() => Scripting.Media.PlayFMVFullscreen(w, "consyard.vqa", () =>
w.WorldActor.QueueActivity(new CallFunc(
() => Scripting.Media.PlayFMVFullscreen(w, "consyard.vqa", () =>
{
Sound.StopMusic();
Game.Disconnect();
@@ -69,7 +72,8 @@ namespace OpenRA.Mods.Cnc
w.WorldActor.CancelActivity();
w.WorldActor.QueueActivity(new Wait(125));
w.WorldActor.QueueActivity(new CallFunc(() => Scripting.Media.PlayFMVFullscreen(w, "gameover.vqa", () =>
w.WorldActor.QueueActivity(new CallFunc(
() => Scripting.Media.PlayFMVFullscreen(w, "gameover.vqa", () =>
{
Sound.StopMusic();
Game.Disconnect();
@@ -109,7 +113,7 @@ namespace OpenRA.Mods.Cnc
}
// GoodGuy win conditions
// BadGuy is dead
int badcount = self.World.Queries.OwnedBy[Players["BadGuy"]].Count(a => !a.IsDead());
var badcount = self.World.Actors.Count(a => a.Owner == Players["BadGuy"] && !a.IsDead());
if (badcount != lastBadCount)
{
Game.Debug("{0} badguys remain".F(badcount));
@@ -120,7 +124,8 @@ namespace OpenRA.Mods.Cnc
}
//GoodGuy lose conditions
if (self.World.Queries.OwnedBy[Players["GoodGuy"]].Count( a => !a.IsDead()) == 0)
var goodCount = self.World.Actors.Count(a => a.Owner == Players["GoodGuy"] && !a.IsDead());
if (goodCount == 0)
OnLose(self.World);
// GoodGuy reinforcements

View File

@@ -20,7 +20,7 @@ namespace OpenRA.Mods.RA.Air
static Actor ChooseHelipad(Actor self)
{
var rearmBuildings = self.Info.Traits.Get<HelicopterInfo>().RearmBuildings;
return self.World.Queries.OwnedBy[self.Owner].FirstOrDefault(
return self.World.Actors.Where( a => a.Owner == self.Owner ).FirstOrDefault(
a => rearmBuildings.Contains(a.Info.Name) &&
!Reservable.IsReserved(a));
}

View File

@@ -12,6 +12,7 @@ using System;
using System.Linq;
using OpenRA.Traits;
using OpenRA.Traits.Activities;
using OpenRA.Mods.RA.Buildings;
namespace OpenRA.Mods.RA.Air
{
@@ -24,11 +25,13 @@ namespace OpenRA.Mods.RA.Air
public static Actor ChooseAirfield(Actor self)
{
return self.World.Queries.OwnedBy[self.Owner]
.Where(a => self.Info.Traits.Get<PlaneInfo>().RearmBuildings.Contains(a.Info.Name)
&& !Reservable.IsReserved(a))
.OrderBy(a => (a.CenterLocation - self.CenterLocation).LengthSquared)
.FirstOrDefault();
return self.World.Queries.WithTrait<BuildingInfo>()
.Where(a => a.Actor.Owner == self.Owner)
.Where(a => self.Info.Traits.Get<PlaneInfo>().RearmBuildings.Contains(a.Actor.Info.Name)
&& !Reservable.IsReserved(a.Actor))
.OrderBy(a => (a.Actor.CenterLocation - self.CenterLocation).LengthSquared)
.Select(a => a.Actor)
.FirstOrDefault();
}
void Calculate(Actor self)

View File

@@ -60,14 +60,16 @@ namespace OpenRA.Mods.RA.Buildings
if (player == null)
return ret;
foreach( var b in player.World.Queries.OwnedBy[player].Where( x=>x.Info.Traits.Contains<BuildingInfo>() ) )
{
ret[ b.Info.Name ].Add( b );
var tt = b.Info.Traits.GetOrDefault<TooltipInfo>();
if( tt != null )
foreach( var alt in tt.AlternateName )
ret[ alt ].Add( b );
}
foreach (var b in player.World.Queries.WithTrait<BuildingInfo>()
.Where(a => a.Actor.Owner == player).Select(a => a.Actor))
{
ret[b.Info.Name].Add(b);
var tt = b.Info.Traits.GetOrDefault<TooltipInfo>();
if (tt != null)
foreach (var alt in tt.AlternateName)
ret[alt].Add(b);
}
return ret;
}

View File

@@ -21,13 +21,15 @@ namespace OpenRA.Mods.RA
{
if (self.Owner.WinState != WinState.Undefined || self.Owner.NonCombatant) return;
var hasAnything = self.World.Queries.OwnedBy[self.Owner]
.WithTrait<MustBeDestroyed>().Any();
var hasAnything = self.World.Queries.WithTrait<MustBeDestroyed>()
.Any( a => a.Actor.Owner == self.Owner );
if (!hasAnything && !self.Owner.NonCombatant)
Surrender(self);
var others = self.World.players.Where( p => !p.Value.NonCombatant && p.Value != self.Owner && p.Value.Stances[self.Owner] != Stance.Ally );
var others = self.World.players.Where( p => !p.Value.NonCombatant
&& p.Value != self.Owner && p.Value.Stances[self.Owner] != Stance.Ally );
if (others.Count() == 0) return;
if(others.All(p => p.Value.WinState == WinState.Lost))
@@ -46,8 +48,9 @@ namespace OpenRA.Mods.RA
self.Owner.WinState = WinState.Lost;
Game.Debug("{0} is defeated.".F(self.Owner.PlayerName));
foreach (var a in self.World.Queries.OwnedBy[self.Owner])
a.Kill(a);
foreach (var a in self.World.Actors.Where(a => a.Owner == self.Owner))
a.Kill(a);
if (self.Owner == self.World.LocalPlayer)
self.World.LocalShroud.Disabled = true;

View File

@@ -29,8 +29,11 @@ namespace OpenRA.Mods.RA.Crates
if (base.GetSelectionShares(collector) == 0)
return 0; // there's some other really good reason why we shouldn't give this.
var hasBase = self.World.Queries.OwnedBy[collector.Owner].WithTrait<BaseBuilding>().Any();
return hasBase ? info.SelectionShares : (info as GiveMcvCrateActionInfo).NoBaseSelectionShares;
var hasBase = self.World.Queries.WithTrait<BaseBuilding>()
.Any(a => a.Actor.Owner == collector.Owner);
return hasBase ? info.SelectionShares :
(info as GiveMcvCrateActionInfo).NoBaseSelectionShares;
}
}
}

View File

@@ -111,9 +111,9 @@ namespace OpenRA.Mods.RA
this.p = p;
enabled = true;
playerPower = p.PlayerActor.Trait<PowerManager>();
builders = new BaseBuilder[] {
new BaseBuilder( this, "Building", ChooseBuildingToBuild ),
new BaseBuilder( this, "Defense", ChooseDefenseToBuild ) };
builders = new BaseBuilder[] {
new BaseBuilder( this, "Building", q => ChooseBuildingToBuild(q, true) ),
new BaseBuilder( this, "Defense", q => ChooseBuildingToBuild(q, false) ) };
}
int GetPowerProvidedBy(ActorInfo building)
@@ -137,38 +137,23 @@ namespace OpenRA.Mods.RA
playerPower.PowerProvided > playerPower.PowerDrained * 1.2;
}
ActorInfo ChooseBuildingToBuild(ProductionQueue queue)
ActorInfo ChooseBuildingToBuild(ProductionQueue queue, bool buildPower)
{
var buildableThings = queue.BuildableItems();
if (!HasAdequatePower()) /* try to maintain 20% excess power */
{
if (!buildPower) return null;
/* find the best thing we can build which produces power */
return buildableThings.Where(a => GetPowerProvidedBy(a) > 0)
.OrderByDescending(a => GetPowerProvidedBy(a)).FirstOrDefault();
}
var myBuildings = p.World.Queries.OwnedBy[p].WithTrait<Building>()
.Select(a => a.Actor.Info.Name).ToArray();
foreach (var frac in Info.BuildingFractions)
if (buildableThings.Any(b => b.Name == frac.Key))
if (myBuildings.Count(a => a == frac.Key) < frac.Value * myBuildings.Length)
return Rules.Info[frac.Key];
return null;
}
ActorInfo ChooseDefenseToBuild(ProductionQueue queue)
{
if (!HasAdequatePower())
return null;
var buildableThings = queue.BuildableItems();
var myBuildings = p.World.Queries.OwnedBy[p].WithTrait<Building>()
.Select(a => a.Actor.Info.Name).ToArray();
var myBuildings = p.World.Queries
.WithTrait<Building>()
.Where( a => a.Actor.Owner == p )
.Select(a => a.Actor.Info.Name).ToArray();
foreach (var frac in Info.BuildingFractions)
if (buildableThings.Any(b => b.Name == frac.Key))
@@ -287,9 +272,10 @@ namespace OpenRA.Mods.RA
attackForce.RemoveAll(a => a.Destroyed);
// don't select harvesters.
var newUnits = self.World.Queries.OwnedBy[p]
.Where(a => a.HasTrait<IMove>() && a.Info != Rules.Info["harv"]
&& !activeUnits.Contains(a)).ToArray();
var newUnits = self.World.Queries.WithTrait<IMove>()
.Where(a => a.Actor.Owner == p && a.Actor.Info != Rules.Info["harv"]
&& !activeUnits.Contains(a.Actor))
.Select(a => a.Actor).ToArray();
foreach (var a in newUnits)
{
@@ -323,8 +309,9 @@ namespace OpenRA.Mods.RA
void SetRallyPointsForNewProductionBuildings(Actor self)
{
var buildings = self.World.Queries.OwnedBy[p].WithTrait<RallyPoint>()
.Where(rp => !IsRallyPointValid(rp.Trait.rallyPoint)).ToArray();
var buildings = self.World.Queries.WithTrait<RallyPoint>()
.Where(rp => rp.Actor.Owner == p &&
!IsRallyPointValid(rp.Trait.rallyPoint)).ToArray();
if (buildings.Length > 0)
BotDebug("Bot {0} needs to find rallypoints for {1} buildings.",
@@ -396,8 +383,8 @@ namespace OpenRA.Mods.RA
void DeployMcv(Actor self)
{
/* find our mcv and deploy it */
var mcv = self.World.Queries.OwnedBy[p]
.FirstOrDefault(a => a.Info == Rules.Info["mcv"]);
var mcv = self.World.Actors
.FirstOrDefault(a => a.Owner == p && a.Info == Rules.Info["mcv"]);
if (mcv != null)
{

View File

@@ -64,17 +64,18 @@ namespace OpenRA.Mods.RA
Actor ClosestProc(Actor self, Actor ignore)
{
var refs = self.World.Queries.OwnedBy[self.Owner]
.Where(x => x != ignore && x.HasTrait<IAcceptOre>())
.ToList();
var refs = self.World.Queries.WithTrait<IAcceptOre>()
.Where(x => x.Actor != ignore && x.Actor.Owner == self.Owner)
.ToList();
var mi = self.Info.Traits.Get<MobileInfo>();
var path = self.World.WorldActor.Trait<PathFinder>().FindPath(
PathSearch.FromPoints(self.World, mi,
refs.Select(r => r.Location + r.Trait<IAcceptOre>().DeliverOffset),
refs.Select(r => r.Actor.Location + r.Trait.DeliverOffset),
self.Location, false));
path.Reverse();
if (path.Count != 0)
return refs.FirstOrDefault(x => x.Location + x.Trait<IAcceptOre>().DeliverOffset == path[0]);
return refs.Where(x => x.Actor.Location + x.Trait.DeliverOffset == path[0])
.Select(a => a.Actor).FirstOrDefault();
else
return null;
}

View File

@@ -50,7 +50,8 @@ namespace OpenRA.Mods.RA.Orders
public static bool PlayerIsAllowedToRepair( World world )
{
return world.Queries.OwnedBy[ world.LocalPlayer ].WithTrait<AllowsBuildingRepair>().Any();
return world.Queries.WithTrait<AllowsBuildingRepair>()
.Any(a => a.Actor.Owner == world.LocalPlayer);
}
public void RenderAfterWorld( WorldRenderer wr, World world ) { }

View File

@@ -28,7 +28,8 @@ namespace OpenRA.Mods.RA
[Sync] bool QueueActive = false;
public override void Tick( Actor self )
{
QueueActive = self.World.Queries.OwnedBy[self.Owner].WithTrait<Production>()
QueueActive = self.World.Queries.WithTrait<Production>()
.Where(x => x.Actor.Owner == self.Owner)
.Where(x => x.Trait.Info.Produces.Contains(Info.Type))
.Any();
@@ -49,8 +50,9 @@ namespace OpenRA.Mods.RA
protected override bool BuildUnit( string name )
{
// Find a production structure to build this actor
var producers = self.World.Queries.OwnedBy[self.Owner]
var producers = self.World.Queries
.WithTrait<Production>()
.Where(x => x.Actor.Owner == self.Owner)
.Where(x => x.Trait.Info.Produces.Contains(Info.Type))
.OrderByDescending(x => x.Actor.IsPrimaryBuilding() ? 1 : 0 ); // prioritize the primary.

View File

@@ -95,7 +95,8 @@ namespace OpenRA.Mods.RA
if (bi == null)
return;
var producers = self.World.Queries.OwnedBy[ self.Owner ].WithTrait<Production>()
var producers = self.World.Queries.WithTrait<Production>()
.Where( x => x.Actor.Owner == self.Owner )
.Where( x => x.Actor.Info.Traits.Get<ProductionInfo>().Produces.Contains( bi.Queue ) )
.ToList();
var producer = producers.Where( x => x.Actor.IsPrimaryBuilding() ).Concat( producers )

View File

@@ -54,10 +54,12 @@ namespace OpenRA.Mods.RA
return;
}
// THIS IS SHIT
// Cancel existing primaries
foreach (var p in self.Info.Traits.Get<ProductionInfo>().Produces)
foreach (var b in self.World.Queries.OwnedBy[self.Owner]
foreach (var b in self.World.Queries
.WithTrait<PrimaryBuilding>()
.Where(a => a.Actor.Owner == self.Owner)
.Where(x => x.Trait.IsPrimary
&& (x.Actor.Info.Traits.Get<ProductionInfo>().Produces.Contains(p))))
b.Trait.SetPrimaryProducer(b.Actor, false);

View File

@@ -9,6 +9,7 @@
#endregion
using System.Drawing;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Traits;
@@ -30,9 +31,10 @@ namespace OpenRA.Mods.RA
centerLocation,
ai.Traits.Get<AttackBaseInfo>().GetMaximumRange());
foreach (var a in w.Queries.OwnedBy[w.LocalPlayer].WithTrait<RenderRangeCircle>())
if (a.Actor.Info.Traits.Get<RenderRangeCircleInfo>().RangeCircleType == RangeCircleType)
a.Trait.RenderBeforeWorld(wr, a.Actor);
foreach (var a in w.Queries.WithTrait<RenderRangeCircle>())
if (a.Actor.Owner == a.Actor.World.LocalPlayer)
if (a.Actor.Info.Traits.Get<RenderRangeCircleInfo>().RangeCircleType == RangeCircleType)
a.Trait.RenderBeforeWorld(wr, a.Actor);
}
}

View File

@@ -93,7 +93,9 @@ namespace OpenRA.Mods.RA
{
if (p == Self.Owner || (p.Stances[Self.Owner] == Stance.Ally && Self.Owner.Stances[p] == Stance.Ally))
{
total += Self.World.Queries.OwnedBy[p].Where(a => a.HasTrait<StrategicPoint>() && a.TraitOrDefault<StrategicPoint>().Critical == critical).Count();
total += Self.World.Queries.WithTrait<StrategicPoint>()
.Where(a => a.Actor.Owner == p)
.Count(a => a.Trait.Critical == critical);
}
}
return total;

View File

@@ -165,9 +165,9 @@ namespace OpenRA.Mods.RA.Widgets
int updateTicks = 0;
public override void Tick()
{
var hasRadarNew = world.Queries.OwnedBy[world.LocalPlayer]
.WithTrait<ProvidesRadar>()
.Any(a => a.Trait.IsActive);
var hasRadarNew = world.Queries
.WithTrait<ProvidesRadar>()
.Any(a => a.Actor.Owner == world.LocalPlayer && a.Trait.IsActive);
if (hasRadarNew != hasRadar)
radarAnimating = true;

View File

@@ -156,9 +156,9 @@ namespace OpenRA.Mods.RA.Widgets
int updateTicks = 0;
public override void Tick()
{
var hasRadarNew = world.Queries.OwnedBy[world.LocalPlayer]
var hasRadarNew = world.Queries
.WithTrait<ProvidesRadar>()
.Any(a => a.Trait.IsActive);
.Any(a => a.Actor.Owner == world.LocalPlayer && a.Trait.IsActive);
if (hasRadarNew != hasRadar)
{

View File

@@ -101,7 +101,8 @@ namespace OpenRA.Mods.RA.Widgets
bool CycleBases()
{
var bases = World.Queries.OwnedBy[World.LocalPlayer].WithTrait<BaseBuilding>().ToArray();
var bases = World.Queries.WithTrait<BaseBuilding>()
.Where( a => a.Actor.Owner == World.LocalPlayer ).ToArray();
if (!bases.Any()) return true;
var next = bases