Remove a bunch of unnecessary caching

This commit is contained in:
Paul Chote
2010-06-23 20:42:45 +12:00
parent b42589b479
commit 3e664779ef
5 changed files with 22 additions and 26 deletions

View File

@@ -60,8 +60,9 @@ namespace OpenRA
List<CachedPath> CachedPaths = new List<CachedPath>(); List<CachedPath> CachedPaths = new List<CachedPath>();
const int MaxPathAge = 50; /* x 40ms ticks */ const int MaxPathAge = 50; /* x 40ms ticks */
public List<int2> FindUnitPath(int2 from, int2 target, UnitMovementType umt, Actor self) public List<int2> FindUnitPath(int2 from, int2 target, Actor self)
{ {
var umt = self.traits.Get<Mobile>().GetMovementType();
using (new PerfSample("find_unit_path")) using (new PerfSample("find_unit_path"))
{ {
var cached = CachedPaths.FirstOrDefault(p => p.from == from && p.to == target && p.umt == umt); var cached = CachedPaths.FirstOrDefault(p => p.from == from && p.to == target && p.umt == umt);
@@ -72,9 +73,9 @@ namespace OpenRA
} }
var pb = FindBidiPath( var pb = FindBidiPath(
PathSearch.FromPoint(self, target, from, umt, true) PathSearch.FromPoint(self, target, from, true)
.WithCustomBlocker(AvoidUnitsNear(from, 4, self)), .WithCustomBlocker(AvoidUnitsNear(from, 4, self)),
PathSearch.FromPoint(self, from, target, umt, true) PathSearch.FromPoint(self, from, target, true)
.WithCustomBlocker(AvoidUnitsNear(from, 4, self)) .WithCustomBlocker(AvoidUnitsNear(from, 4, self))
.InReverse()); .InReverse());
@@ -86,7 +87,7 @@ namespace OpenRA
} }
} }
public List<int2> FindUnitPathToRange( int2 src, int2 target, UnitMovementType umt, int range, Actor self ) public List<int2> FindUnitPathToRange( int2 src, int2 target, int range, Actor self )
{ {
using( new PerfSample( "find_unit_path_multiple_src" ) ) using( new PerfSample( "find_unit_path_multiple_src" ) )
{ {
@@ -94,7 +95,7 @@ namespace OpenRA
var tilesInRange = world.FindTilesInCircle(target, range) var tilesInRange = world.FindTilesInCircle(target, range)
.Where( t => mobile.CanEnterCell(t)); .Where( t => mobile.CanEnterCell(t));
var path = FindPath( PathSearch.FromPoints( self, tilesInRange, src, umt, false ) var path = FindPath( PathSearch.FromPoints( self, tilesInRange, src, false )
.WithCustomBlocker(AvoidUnitsNear(src, 4, self)) .WithCustomBlocker(AvoidUnitsNear(src, 4, self))
.InReverse()); .InReverse());
path.Reverse(); path.Reverse();

View File

@@ -49,6 +49,8 @@ namespace OpenRA
world = self.World; world = self.World;
cellInfo = InitCellInfo(); cellInfo = InitCellInfo();
queue = new PriorityQueue<PathDistance>(); queue = new PriorityQueue<PathDistance>();
umt = self.traits.Get<Mobile>().GetMovementType();
buildingInfluence = world.WorldActor.traits.Get<BuildingInfluence>(); buildingInfluence = world.WorldActor.traits.Get<BuildingInfluence>();
unitInfluence = world.WorldActor.traits.Get<UnitInfluence>(); unitInfluence = world.WorldActor.traits.Get<UnitInfluence>();
@@ -172,31 +174,28 @@ namespace OpenRA
queue.Add( new PathDistance( heuristic( location ), location ) ); queue.Add( new PathDistance( heuristic( location ), location ) );
} }
public static PathSearch Search( Actor self, UnitMovementType umt, bool checkForBlocked ) public static PathSearch Search( Actor self, bool checkForBlocked )
{ {
var search = new PathSearch(self) { var search = new PathSearch(self) {
umt = umt,
checkForBlocked = checkForBlocked }; checkForBlocked = checkForBlocked };
return search; return search;
} }
public static PathSearch FromPoint( Actor self, int2 from, int2 target, UnitMovementType umt, bool checkForBlocked ) public static PathSearch FromPoint( Actor self, int2 from, int2 target, bool checkForBlocked )
{ {
var search = new PathSearch(self) { var search = new PathSearch(self) {
heuristic = DefaultEstimator( target ), heuristic = DefaultEstimator( target ),
umt = umt,
checkForBlocked = checkForBlocked }; checkForBlocked = checkForBlocked };
search.AddInitialCell( self.World, from ); search.AddInitialCell( self.World, from );
return search; return search;
} }
public static PathSearch FromPoints(Actor self, IEnumerable<int2> froms, int2 target, UnitMovementType umt, bool checkForBlocked) public static PathSearch FromPoints(Actor self, IEnumerable<int2> froms, int2 target, bool checkForBlocked)
{ {
var search = new PathSearch(self) var search = new PathSearch(self)
{ {
heuristic = DefaultEstimator(target), heuristic = DefaultEstimator(target),
umt = umt,
checkForBlocked = checkForBlocked checkForBlocked = checkForBlocked
}; };

View File

@@ -32,7 +32,7 @@ namespace OpenRA.Traits.Activities
int2? destination; int2? destination;
int nearEnough; int nearEnough;
public List<int2> path; public List<int2> path;
Func<Actor, Mobile, List<int2>> getPath; Func<Actor, List<int2>> getPath;
public Actor ignoreBuilding; public Actor ignoreBuilding;
MovePart move; MovePart move;
@@ -50,9 +50,8 @@ namespace OpenRA.Traits.Activities
public Move( int2 destination, int nearEnough ) public Move( int2 destination, int nearEnough )
: this() : this()
{ {
this.getPath = ( self, mobile ) => self.World.PathFinder.FindUnitPath( this.getPath = self => self.World.PathFinder.FindUnitPath(
self.Location, destination, self.Location, destination, self );
mobile.GetMovementType(), self );
this.destination = destination; this.destination = destination;
this.nearEnough = nearEnough; this.nearEnough = nearEnough;
} }
@@ -60,9 +59,9 @@ namespace OpenRA.Traits.Activities
public Move(int2 destination, Actor ignoreBuilding) public Move(int2 destination, Actor ignoreBuilding)
: this() : this()
{ {
this.getPath = (self, mobile) => this.getPath = self =>
self.World.PathFinder.FindPath( self.World.PathFinder.FindPath(
PathSearch.FromPoint( self, self.Location, destination, mobile.GetMovementType(), false ) PathSearch.FromPoint( self, self.Location, destination, false )
.WithCustomBlocker( self.World.PathFinder.AvoidUnitsNear( self.Location, 4, self )) .WithCustomBlocker( self.World.PathFinder.AvoidUnitsNear( self.Location, 4, self ))
.WithIgnoredBuilding( ignoreBuilding )); .WithIgnoredBuilding( ignoreBuilding ));
@@ -74,9 +73,9 @@ namespace OpenRA.Traits.Activities
public Move( Actor target, int range ) public Move( Actor target, int range )
: this() : this()
{ {
this.getPath = ( self, mobile ) => self.World.PathFinder.FindUnitPathToRange( this.getPath = self => self.World.PathFinder.FindUnitPathToRange(
self.Location, target.Location, self.Location, target.Location,
mobile.GetMovementType(), range, self ); range, self );
this.destination = null; this.destination = null;
this.nearEnough = range; this.nearEnough = range;
} }
@@ -84,7 +83,7 @@ namespace OpenRA.Traits.Activities
public Move(Func<List<int2>> getPath) public Move(Func<List<int2>> getPath)
: this() : this()
{ {
this.getPath = (_, _2) => getPath(); this.getPath = _ => getPath();
this.destination = null; this.destination = null;
this.nearEnough = 0; this.nearEnough = 0;
} }
@@ -111,7 +110,7 @@ namespace OpenRA.Traits.Activities
return this; return this;
} }
path = getPath( self, mobile ).TakeWhile( a => a != self.Location ).ToList(); path = getPath( self ).TakeWhile( a => a != self.Location ).ToList();
SanityCheckPath( mobile ); SanityCheckPath( mobile );
} }
@@ -187,7 +186,7 @@ namespace OpenRA.Traits.Activities
return null; return null;
self.World.WorldActor.traits.Get<UnitInfluence>().Remove( self, mobile ); self.World.WorldActor.traits.Get<UnitInfluence>().Remove( self, mobile );
var newPath = getPath(self, mobile).TakeWhile(a => a != self.Location).ToList(); var newPath = getPath(self).TakeWhile(a => a != self.Location).ToList();
self.World.WorldActor.traits.Get<UnitInfluence>().Add( self, mobile ); self.World.WorldActor.traits.Get<UnitInfluence>().Add( self, mobile );
if (newPath.Count != 0) if (newPath.Count != 0)

View File

@@ -76,7 +76,7 @@ namespace OpenRA.Mods.RA.Activities
self.QueueActivity(new Move( self.QueueActivity(new Move(
() => () =>
{ {
return self.World.PathFinder.FindPath(PathSearch.Search(self, UnitMovementType.Wheel, true) return self.World.PathFinder.FindPath(PathSearch.Search(self, true)
.WithHeuristic(loc => (res.GetResource(loc) != null && harv.Resources.Contains( res.GetResource(loc).info.Name )) ? 0 : 1) .WithHeuristic(loc => (res.GetResource(loc) != null && harv.Resources.Contains( res.GetResource(loc).info.Name )) ? 0 : 1)
.FromPoint(self.Location)); .FromPoint(self.Location));
})); }));

View File

@@ -66,8 +66,6 @@ namespace OpenRA.Mods.RA
Actor ClosestProc(Actor self, Actor ignore) Actor ClosestProc(Actor self, Actor ignore)
{ {
var mobile = self.traits.Get<Mobile>();
var refs = self.World.Queries.OwnedBy[self.Owner] var refs = self.World.Queries.OwnedBy[self.Owner]
.Where(x => x != ignore && x.traits.Contains<IAcceptOre>()) .Where(x => x != ignore && x.traits.Contains<IAcceptOre>())
.ToList(); .ToList();
@@ -75,7 +73,6 @@ namespace OpenRA.Mods.RA
var path = self.World.PathFinder.FindPath(PathSearch.FromPoints(self, var path = self.World.PathFinder.FindPath(PathSearch.FromPoints(self,
refs.Select(r => r.Location + r.traits.Get<IAcceptOre>().DeliverOffset), refs.Select(r => r.Location + r.traits.Get<IAcceptOre>().DeliverOffset),
self.Location, self.Location,
mobile.GetMovementType(),
false)); false));
path.Reverse(); path.Reverse();
if (path.Count != 0) if (path.Count != 0)