Skip to content

Commit

Permalink
Remove unnecessary trait queries from HarvesterBotModule
Browse files Browse the repository at this point in the history
  • Loading branch information
pchote committed Dec 31, 2018
1 parent 63f76fc commit 39d6a39
Showing 1 changed file with 38 additions and 22 deletions.
60 changes: 38 additions & 22 deletions OpenRA.Mods.Common/Traits/BotModules/HarvesterBotModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,31 @@ public class HarvesterBotModuleInfo : ConditionalTraitInfo

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 Player player;
readonly Predicate<Actor> unitCannotBeOrdered;
readonly Func<Actor, bool> unitCannotBeOrdered;
readonly Dictionary<Actor, HarvesterTraitWrapper> harvesters = new Dictionary<Actor, HarvesterTraitWrapper>();

IPathFinder pathfinder;
DomainIndex domainIndex;
ResourceLayer resLayer;
ResourceClaimLayer claimLayer;
List<Actor> harvesters = new List<Actor>();
int scanForIdleHarvestersTicks;

public HarvesterBotModule(Actor self, HarvesterBotModuleInfo info)
Expand Down Expand Up @@ -67,51 +84,50 @@ void IBotTick.BotTick(IBot bot)
if (--scanForIdleHarvestersTicks > 0)
return;

harvesters.RemoveAll(unitCannotBeOrdered);
var toRemove = harvesters.Keys.Where(unitCannotBeOrdered).ToList();
foreach (var a in toRemove)
harvesters.Remove(a);

scanForIdleHarvestersTicks = Info.ScanForIdleHarvestersInterval;

// Find new harvesters
// 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)
harvesters.Add(a);
harvesters[a] = new HarvesterTraitWrapper(a);

// Find idle harvesters and give them orders:
foreach (var harvester in harvesters)
foreach (var h in harvesters)
{
var harv = harvester.Trait<Harvester>();
if (!harv.IsEmpty)
if (!h.Value.Harvester.IsEmpty)
continue;

if (!harvester.IsIdle)
if (!h.Key.IsIdle)
{
var act = harvester.CurrentActivity;
if (!harv.LastSearchFailed || act.NextActivity == null || act.NextActivity.GetType() != typeof(FindResources))
var act = h.Key.CurrentActivity;
if (!h.Value.Harvester.LastSearchFailed || act.NextActivity == null || act.NextActivity.GetType() != typeof(FindResources))
continue;
}

var para = harvester.TraitOrDefault<Parachutable>();
if (para != null && para.IsInAir)
if (h.Value.Parachutable != null && h.Value.Parachutable.IsInAir)
continue;

// Tell the idle harvester to quit slacking:
var newSafeResourcePatch = FindNextResource(harvester, harv);
AIUtils.BotDebug("AI: Harvester {0} is idle. Ordering to {1} in search for new resources.".F(harvester, newSafeResourcePatch));
bot.QueueOrder(new Order("Harvest", harvester, Target.FromCell(world, newSafeResourcePatch), false));
var newSafeResourcePatch = FindNextResource(h.Key, h.Value);
AIUtils.BotDebug("AI: Harvester {0} is idle. Ordering to {1} in search for new resources.".F(h.Key, newSafeResourcePatch));
bot.QueueOrder(new Order("Harvest", h.Key, Target.FromCell(world, newSafeResourcePatch), false));
}
}

CPos FindNextResource(Actor actor, Harvester harv)
CPos FindNextResource(Actor actor, HarvesterTraitWrapper harv)
{
var locomotorInfo = actor.Info.TraitInfo<MobileInfo>().LocomotorInfo;

Func<CPos, bool> isValidResource = cell =>
domainIndex.IsPassable(actor.Location, cell, locomotorInfo) &&
harv.CanHarvestCell(actor, cell) &&
domainIndex.IsPassable(actor.Location, cell, harv.LocomotorInfo) &&
harv.Harvester.CanHarvestCell(actor, cell) &&
claimLayer.CanClaimCell(actor, cell);

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)
.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)))
Expand Down

0 comments on commit 39d6a39

Please sign in to comment.