From c4477455fe0dffe7efc436a2c0d83439a2d74a05 Mon Sep 17 00:00:00 2001 From: Bob Date: Sun, 31 Jan 2010 13:55:01 +1300 Subject: [PATCH] more. --- OpenRa.FileFormats/Collections/Set.cs | 13 +++++++++---- OpenRa.FileFormats/Exts.cs | 10 +++++++++- OpenRa.Game/Actor.cs | 3 --- OpenRa.Game/Bridges.cs | 4 ++-- OpenRa.Game/Graphics/Viewport.cs | 3 +-- OpenRa.Game/Traits/UnitInfluence.cs | 9 ++++----- OpenRa.Game/World.cs | 23 ++++++++++++++++++++--- 7 files changed, 45 insertions(+), 20 deletions(-) diff --git a/OpenRa.FileFormats/Collections/Set.cs b/OpenRa.FileFormats/Collections/Set.cs index d69064bf6b..8a1fd8e1ef 100755 --- a/OpenRa.FileFormats/Collections/Set.cs +++ b/OpenRa.FileFormats/Collections/Set.cs @@ -41,14 +41,19 @@ namespace OpenRa.Collections public class CachedView : Set { - public CachedView( Set set, Func include, Func store ) + public CachedView( Set set, Func include, Func store ) + : this( set, include, x => new[] { store( x ) } ) + { + } + + public CachedView( Set set, Func include, Func> store ) { foreach( var t in set ) if( include( t ) ) - Add( store( t ) ); + store( t ).Do( x => Add( x ) ); - set.OnAdd += obj => { if( include( obj ) ) Add( store( obj ) ); }; - set.OnRemove += obj => { if( include( obj ) ) Remove( store( obj ) ); }; + set.OnAdd += obj => { if( include( obj ) ) store( obj ).Do( x => Add( x ) ); }; + set.OnRemove += obj => { if( include( obj ) ) store( obj ).Do( x => Remove( x ) ); }; } } } diff --git a/OpenRa.FileFormats/Exts.cs b/OpenRa.FileFormats/Exts.cs index 34a5cb27c3..62352d8517 100644 --- a/OpenRa.FileFormats/Exts.cs +++ b/OpenRa.FileFormats/Exts.cs @@ -1,4 +1,6 @@ - +using System; +using System.Collections.Generic; + namespace OpenRa { public static class Exts @@ -7,5 +9,11 @@ namespace OpenRa { return string.Format(fmt, args); } + + public static void Do( this IEnumerable e, Action fn ) + { + foreach( var ee in e ) + fn( ee ); + } } } diff --git a/OpenRa.Game/Actor.cs b/OpenRa.Game/Actor.cs index e9d6e271db..ad6b2bd3ad 100755 --- a/OpenRa.Game/Actor.cs +++ b/OpenRa.Game/Actor.cs @@ -52,9 +52,6 @@ namespace OpenRa currentActivity = a.Tick(this) ?? new Idle(); if (a == currentActivity || currentActivity is Idle) break; } - - foreach (var tick in traits.WithInterface()) - tick.Tick(this); } public bool IsIdle diff --git a/OpenRa.Game/Bridges.cs b/OpenRa.Game/Bridges.cs index 42b0daf25e..28d00bcdf0 100644 --- a/OpenRa.Game/Bridges.cs +++ b/OpenRa.Game/Bridges.cs @@ -18,8 +18,8 @@ namespace OpenRa if (IsBridge(w, w.Map.MapTiles[i, j].tile)) ConvertBridgeToActor(w, i, j); - foreach (var br in w.Actors.SelectMany(a => a.traits.WithInterface())) - br.FinalizeBridges(w); + foreach (var br in w.Queries.WithTraitMultiple()) + br.Trait.FinalizeBridges(w); } static void ConvertBridgeToActor(World w, int i, int j) diff --git a/OpenRa.Game/Graphics/Viewport.cs b/OpenRa.Game/Graphics/Viewport.cs index 69398c1972..10328669e2 100644 --- a/OpenRa.Game/Graphics/Viewport.cs +++ b/OpenRa.Game/Graphics/Viewport.cs @@ -45,8 +45,7 @@ namespace OpenRa.Graphics public void DrawRegions( World world ) { - world.WorldRenderer.palette.Update(world.Actors.SelectMany( - a => a.traits.WithInterface())); + world.WorldRenderer.palette.Update(world.Queries.WithTraitMultiple().Select(t=>t.Trait)); float2 r1 = new float2(2, -2) / screenSize; float2 r2 = new float2(-1, 1); diff --git a/OpenRa.Game/Traits/UnitInfluence.cs b/OpenRa.Game/Traits/UnitInfluence.cs index 4d679e186e..0cce565a90 100755 --- a/OpenRa.Game/Traits/UnitInfluence.cs +++ b/OpenRa.Game/Traits/UnitInfluence.cs @@ -62,11 +62,10 @@ namespace OpenRa.Traits if (!a.traits.Get().OccupiedCells().Contains( new int2( x, y ) ) ) throw new InvalidOperationException( "UIM: Sanity check failed A" ); - foreach( Actor a in self.World.Actors ) - foreach( var ios in a.traits.WithInterface() ) - foreach( var cell in ios.OccupiedCells() ) - if (!influence[cell.X, cell.Y].Contains(a)) - throw new InvalidOperationException( "UIM: Sanity check failed B" ); + foreach( var t in self.World.Queries.WithTraitMultiple() ) + foreach( var cell in t.Trait.OccupiedCells() ) + if (!influence[cell.X, cell.Y].Contains(t.Actor)) + throw new InvalidOperationException( "UIM: Sanity check failed B" ); } public IEnumerable GetUnitsAt( int2 a ) diff --git a/OpenRa.Game/World.cs b/OpenRa.Game/World.cs index 41c1ff43d4..6d810e01b8 100644 --- a/OpenRa.Game/World.cs +++ b/OpenRa.Game/World.cs @@ -71,6 +71,9 @@ namespace OpenRa players[i] = new Player(this, i, Game.LobbyInfo.Clients.FirstOrDefault(a => a.Index == i)); Timer.Time( "worldActor, players: {0}" ); + Queries = new AllQueries( this ); + Timer.Time( "queries: {0}" ); + Bridges.MakeBridges(this); PathFinder = new PathFinder(this); Timer.Time( "bridge, pathing: {0}" ); @@ -79,8 +82,6 @@ namespace OpenRa Minimap = new Minimap(this, Game.renderer); Timer.Time( "renderer, minimap: {0}" ); - Queries = new AllQueries( this ); - Timer.Time( "queries: {0}" ); Timer.Time( "----end World.ctor" ); } @@ -124,6 +125,8 @@ namespace OpenRa } foreach (var a in actors) a.Tick(); + Queries.WithTraitMultiple().Do( x => x.Trait.Tick( x.Actor ) ); + foreach (var e in effects) e.Tick( this ); Game.viewport.Tick(); @@ -190,7 +193,21 @@ namespace OpenRa x => x.traits.Contains(), x => new TraitPair { Actor = x, Trait = x.traits.Get() } ); hasTrait.Add( ret ); - return ret; } + return ret; + } + + public CachedView> WithTraitMultiple() + { + var ret = hasTrait.GetOrDefault>>(); + if( ret != null ) + return ret; + ret = new CachedView>( + world.actors, + x => x.traits.Contains(), + x => x.traits.WithInterface().Select( t => new TraitPair { Actor = x, Trait = t } ) ); + hasTrait.Add( ret ); + return ret; + } public struct TraitPair {