Remove unnecessary trait queries from HarvesterBotModule

This commit is contained in:
Paul Chote
2018-12-31 21:06:59 +00:00
committed by reaperrr
parent 4da2d32bc5
commit fd013ad9d0

View File

@@ -39,15 +39,32 @@ namespace OpenRA.Mods.Common.Traits
public class HarvesterBotModule : ConditionalTrait<HarvesterBotModuleInfo>, IBotTick public class HarvesterBotModule : ConditionalTrait<HarvesterBotModuleInfo>, IBotTick
{ {
class HarvesterTraitWrapper
{
public readonly Actor Actor;
public readonly Harvester Harvester;
public readonly Parachutable Parachutable;
public readonly LocomotorInfo LocomotorInfo;
public HarvesterTraitWrapper(Actor actor)
{
Actor = actor;
Harvester = actor.Trait<Harvester>();
Parachutable = actor.TraitOrDefault<Parachutable>();
LocomotorInfo = actor.Info.TraitInfo<MobileInfo>().LocomotorInfo;
}
}
readonly World world; readonly World world;
readonly Player player; readonly Player player;
readonly Predicate<Actor> unitCannotBeOrdered; readonly Func<Actor, bool> unitCannotBeOrdered;
IBotRequestUnitProduction[] requestUnitProduction; readonly Dictionary<Actor, HarvesterTraitWrapper> harvesters = new Dictionary<Actor, HarvesterTraitWrapper>();
IPathFinder pathfinder; IPathFinder pathfinder;
DomainIndex domainIndex; DomainIndex domainIndex;
ResourceLayer resLayer; ResourceLayer resLayer;
ResourceClaimLayer claimLayer; ResourceClaimLayer claimLayer;
List<Actor> harvesters = new List<Actor>(); IBotRequestUnitProduction[] requestUnitProduction;
int scanForIdleHarvestersTicks; int scanForIdleHarvestersTicks;
public HarvesterBotModule(Actor self, HarvesterBotModuleInfo info) public HarvesterBotModule(Actor self, HarvesterBotModuleInfo info)
@@ -76,37 +93,38 @@ namespace OpenRA.Mods.Common.Traits
if (--scanForIdleHarvestersTicks > 0) if (--scanForIdleHarvestersTicks > 0)
return; return;
harvesters.RemoveAll(unitCannotBeOrdered); var toRemove = harvesters.Keys.Where(unitCannotBeOrdered).ToList();
foreach (var a in toRemove)
harvesters.Remove(a);
scanForIdleHarvestersTicks = Info.ScanForIdleHarvestersInterval; scanForIdleHarvestersTicks = Info.ScanForIdleHarvestersInterval;
// Find new harvesters // Find new harvesters
// TODO: Look for a more performance-friendly way to update this list // TODO: Look for a more performance-friendly way to update this list
var newHarvesters = world.ActorsHavingTrait<Harvester>().Where(a => a.Owner == player && !harvesters.Contains(a)); var newHarvesters = world.ActorsHavingTrait<Harvester>().Where(a => a.Owner == player && !harvesters.ContainsKey(a));
foreach (var a in newHarvesters) foreach (var a in newHarvesters)
harvesters.Add(a); harvesters[a] = new HarvesterTraitWrapper(a);
// Find idle harvesters and give them orders: // Find idle harvesters and give them orders:
foreach (var harvester in harvesters) foreach (var h in harvesters)
{ {
var harv = harvester.Trait<Harvester>(); if (!h.Value.Harvester.IsEmpty)
if (!harv.IsEmpty)
continue; continue;
if (!harvester.IsIdle) if (!h.Key.IsIdle)
{ {
var act = harvester.CurrentActivity; var act = h.Key.CurrentActivity;
if (!harv.LastSearchFailed || act.NextActivity == null || act.NextActivity.GetType() != typeof(FindResources)) if (!h.Value.Harvester.LastSearchFailed || act.NextActivity == null || act.NextActivity.GetType() != typeof(FindResources))
continue; continue;
} }
var para = harvester.TraitOrDefault<Parachutable>(); if (h.Value.Parachutable != null && h.Value.Parachutable.IsInAir)
if (para != null && para.IsInAir)
continue; continue;
// Tell the idle harvester to quit slacking: // Tell the idle harvester to quit slacking:
var newSafeResourcePatch = FindNextResource(harvester, harv); var newSafeResourcePatch = FindNextResource(h.Key, h.Value);
AIUtils.BotDebug("AI: Harvester {0} is idle. Ordering to {1} in search for new resources.".F(harvester, newSafeResourcePatch)); AIUtils.BotDebug("AI: Harvester {0} is idle. Ordering to {1} in search for new resources.".F(h.Key, newSafeResourcePatch));
bot.QueueOrder(new Order("Harvest", harvester, Target.FromCell(world, newSafeResourcePatch), false)); bot.QueueOrder(new Order("Harvest", h.Key, Target.FromCell(world, newSafeResourcePatch), false));
} }
// Less harvesters than refineries - build a new harvester // Less harvesters than refineries - build a new harvester
@@ -120,17 +138,15 @@ namespace OpenRA.Mods.Common.Traits
} }
} }
CPos FindNextResource(Actor actor, Harvester harv) CPos FindNextResource(Actor actor, HarvesterTraitWrapper harv)
{ {
var locomotorInfo = actor.Info.TraitInfo<MobileInfo>().LocomotorInfo;
Func<CPos, bool> isValidResource = cell => Func<CPos, bool> isValidResource = cell =>
domainIndex.IsPassable(actor.Location, cell, locomotorInfo) && domainIndex.IsPassable(actor.Location, cell, harv.LocomotorInfo) &&
harv.CanHarvestCell(actor, cell) && harv.Harvester.CanHarvestCell(actor, cell) &&
claimLayer.CanClaimCell(actor, cell); claimLayer.CanClaimCell(actor, cell);
var path = pathfinder.FindPath( var path = pathfinder.FindPath(
PathSearch.Search(world, locomotorInfo, actor, true, isValidResource) PathSearch.Search(world, harv.LocomotorInfo, actor, true, isValidResource)
.WithCustomCost(loc => world.FindActorsInCircle(world.Map.CenterOfCell(loc), Info.HarvesterEnemyAvoidanceRadius) .WithCustomCost(loc => world.FindActorsInCircle(world.Map.CenterOfCell(loc), Info.HarvesterEnemyAvoidanceRadius)
.Where(u => !u.IsDead && actor.Owner.Stances[u.Owner] == Stance.Enemy) .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))) .Sum(u => Math.Max(WDist.Zero.Length, Info.HarvesterEnemyAvoidanceRadius.Length - (world.Map.CenterOfCell(loc) - u.CenterPosition).Length)))