Using Locomotor instead of Info for pathfinding

This commit is contained in:
teinarss
2019-07-07 19:44:39 +02:00
committed by reaperrr
parent c00b13a18e
commit 2ddf9fa826
9 changed files with 41 additions and 29 deletions

View File

@@ -44,14 +44,15 @@ namespace OpenRA.Mods.Common.Traits
public readonly Actor Actor;
public readonly Harvester Harvester;
public readonly Parachutable Parachutable;
public readonly LocomotorInfo LocomotorInfo;
public readonly Locomotor Locomotor;
public HarvesterTraitWrapper(Actor actor)
{
Actor = actor;
Harvester = actor.Trait<Harvester>();
Parachutable = actor.TraitOrDefault<Parachutable>();
LocomotorInfo = actor.Info.TraitInfo<MobileInfo>().LocomotorInfo;
var mobile = actor.Trait<Mobile>();
Locomotor = mobile.Locomotor;
}
}
@@ -141,12 +142,12 @@ namespace OpenRA.Mods.Common.Traits
Target FindNextResource(Actor actor, HarvesterTraitWrapper harv)
{
Func<CPos, bool> isValidResource = cell =>
domainIndex.IsPassable(actor.Location, cell, harv.LocomotorInfo) &&
domainIndex.IsPassable(actor.Location, cell, harv.Locomotor.Info) &&
harv.Harvester.CanHarvestCell(actor, cell) &&
claimLayer.CanClaimCell(actor, cell);
var path = pathfinder.FindPath(
PathSearch.Search(world, harv.LocomotorInfo, actor, true, isValidResource)
PathSearch.Search(world, harv.Locomotor, actor, true, isValidResource)
.WithCustomCost(loc => world.FindActorsInCircle(world.Map.CenterOfCell(loc), Info.HarvesterEnemyAvoidanceRadius)
.Where(u => !u.IsDead && actor.Owner.Stances[u.Owner] == Stance.Enemy)
.Sum(u => Math.Max(WDist.Zero.Length, Info.HarvesterEnemyAvoidanceRadius.Length - (world.Map.CenterOfCell(loc) - u.CenterPosition).Length)))

View File

@@ -201,8 +201,8 @@ namespace OpenRA.Mods.Common.Traits
// Start a search from each refinery's delivery location:
List<CPos> path;
var li = self.Info.TraitInfo<MobileInfo>().LocomotorInfo;
using (var search = PathSearch.FromPoints(self.World, li, self, refs.Values.Select(r => r.Location), self.Location, false)
using (var search = PathSearch.FromPoints(self.World, mobile.Locomotor, self, refs.Values.Select(r => r.Location), self.Location, false)
.WithCustomCost(loc =>
{
if (!refs.ContainsKey(loc))

View File

@@ -185,6 +185,8 @@ namespace OpenRA.Mods.Common.Traits
[Sync]
public int PathHash; // written by Move.EvalPath, to temporarily debug this crap.
public Locomotor Locomotor { get; private set; }
#region IOccupySpace
[Sync]
@@ -235,6 +237,8 @@ namespace OpenRA.Mods.Common.Traits
notifyMoving = self.TraitsImplementing<INotifyMoving>().ToArray();
notifyFinishedMoving = self.TraitsImplementing<INotifyFinishedMoving>().ToArray();
moveWrappers = self.TraitsImplementing<IWrapMove>().ToArray();
Locomotor = self.World.WorldActor.TraitsImplementing<Locomotor>()
.Single(l => l.Info.Name == Info.Locomotor);
base.Created(self);
}
@@ -704,7 +708,7 @@ namespace OpenRA.Mods.Common.Traits
var pathFinder = self.World.WorldActor.Trait<IPathFinder>();
List<CPos> path;
using (var search = PathSearch.Search(self.World, Info.LocomotorInfo, self, true,
using (var search = PathSearch.Search(self.World, Locomotor, self, true,
loc => loc.Layer == 0 && CanEnterCell(loc))
.FromPoint(self.Location))
path = pathFinder.FindPath(search);

View File

@@ -64,7 +64,9 @@ namespace OpenRA.Mods.Common.Traits
public List<CPos> FindUnitPath(CPos source, CPos target, Actor self, Actor ignoreActor)
{
var li = self.Info.TraitInfo<MobileInfo>().LocomotorInfo;
var mobile = self.Trait<Mobile>();
var locomotor = mobile.Locomotor;
if (!cached)
{
domainIndex = world.WorldActor.TraitOrDefault<DomainIndex>();
@@ -72,7 +74,7 @@ namespace OpenRA.Mods.Common.Traits
}
// If a water-land transition is required, bail early
if (domainIndex != null && !domainIndex.IsPassable(source, target, li))
if (domainIndex != null && !domainIndex.IsPassable(source, target, locomotor.Info))
return EmptyPath;
var distance = source - target;
@@ -80,8 +82,9 @@ namespace OpenRA.Mods.Common.Traits
return new List<CPos> { target };
List<CPos> pb;
using (var fromSrc = PathSearch.FromPoint(world, li, self, target, source, true).WithIgnoredActor(ignoreActor))
using (var fromDest = PathSearch.FromPoint(world, li, self, source, target, true).WithIgnoredActor(ignoreActor).Reverse())
using (var fromSrc = PathSearch.FromPoint(world, locomotor, self, target, source, true).WithIgnoredActor(ignoreActor))
using (var fromDest = PathSearch.FromPoint(world, locomotor, self, source, target, true).WithIgnoredActor(ignoreActor).Reverse())
pb = FindBidiPath(fromSrc, fromDest);
return pb;
@@ -95,7 +98,8 @@ namespace OpenRA.Mods.Common.Traits
cached = true;
}
var mi = self.Info.TraitInfo<MobileInfo>();
var mobile = self.Trait<Mobile>();
var mi = mobile.Info;
var li = mi.LocomotorInfo;
var targetCell = world.Map.CellContaining(target);
@@ -117,8 +121,10 @@ namespace OpenRA.Mods.Common.Traits
return EmptyPath;
}
using (var fromSrc = PathSearch.FromPoints(world, li, self, tilesInRange, source, true))
using (var fromDest = PathSearch.FromPoint(world, li, self, source, targetCell, true).Reverse())
var locomotor = mobile.Locomotor;
using (var fromSrc = PathSearch.FromPoints(world, locomotor, self, tilesInRange, source, true))
using (var fromDest = PathSearch.FromPoint(world, locomotor, self, source, targetCell, true).Reverse())
return FindBidiPath(fromSrc, fromDest);
}