Improve BotModule performance.
Several parts of bot module logic, often through the AIUtils helper class, will query or count over all actors in the world. This is not a fast operation and the AI tends to repeat it often. Introduce some ActorIndex classes that can maintain an index of actors in the world that match a query based on a mix of actor name, owner or trait. These indexes introduce some overhead to maintain, but allow the queries or counts that bot modules needs to perform to be greatly sped up, as the index means there is a much smaller starting set of actors to consider. This is beneficial to the bot logic as the TraitDictionary index maintained by the world works only in terms of traits and doesn't allow the bot logic to perform a sufficiently selective lookup. This is because the bot logic is usually defined in terms of actor names rather than traits.
This commit is contained in:
@@ -37,7 +37,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
public override object Create(ActorInitializer init) { return new HarvesterBotModule(init.Self, this); }
|
||||
}
|
||||
|
||||
public class HarvesterBotModule : ConditionalTrait<HarvesterBotModuleInfo>, IBotTick
|
||||
public class HarvesterBotModule : ConditionalTrait<HarvesterBotModuleInfo>, IBotTick, INotifyActorDisposing
|
||||
{
|
||||
sealed class HarvesterTraitWrapper
|
||||
{
|
||||
@@ -59,6 +59,8 @@ namespace OpenRA.Mods.Common.Traits
|
||||
readonly Player player;
|
||||
readonly Func<Actor, bool> unitCannotBeOrdered;
|
||||
readonly Dictionary<Actor, HarvesterTraitWrapper> harvesters = new();
|
||||
readonly ActorIndex.OwnerAndNamesAndTrait<Building> refineries;
|
||||
readonly ActorIndex.OwnerAndNamesAndTrait<Harvester> harvestersIndex;
|
||||
|
||||
IResourceLayer resourceLayer;
|
||||
ResourceClaimLayer claimLayer;
|
||||
@@ -71,6 +73,8 @@ namespace OpenRA.Mods.Common.Traits
|
||||
world = self.World;
|
||||
player = self.Owner;
|
||||
unitCannotBeOrdered = a => a.Owner != self.Owner || a.IsDead || !a.IsInWorld;
|
||||
refineries = new ActorIndex.OwnerAndNamesAndTrait<Building>(world, info.RefineryTypes, player);
|
||||
harvestersIndex = new ActorIndex.OwnerAndNamesAndTrait<Harvester>(world, info.HarvesterTypes, player);
|
||||
}
|
||||
|
||||
protected override void Created(Actor self)
|
||||
@@ -102,7 +106,6 @@ namespace OpenRA.Mods.Common.Traits
|
||||
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 => !unitCannotBeOrdered(a) && !harvesters.ContainsKey(a));
|
||||
foreach (var a in newHarvesters)
|
||||
harvesters[a] = new HarvesterTraitWrapper(a);
|
||||
@@ -133,10 +136,15 @@ namespace OpenRA.Mods.Common.Traits
|
||||
var unitBuilder = requestUnitProduction.FirstEnabledTraitOrDefault();
|
||||
if (unitBuilder != null && Info.HarvesterTypes.Count > 0)
|
||||
{
|
||||
var harvInfo = AIUtils.GetInfoByCommonName(Info.HarvesterTypes, player);
|
||||
var harvCountTooLow = AIUtils.CountActorByCommonName(Info.HarvesterTypes, player) < AIUtils.CountBuildingByCommonName(Info.RefineryTypes, player);
|
||||
if (harvCountTooLow && unitBuilder.RequestedProductionCount(bot, harvInfo.Name) == 0)
|
||||
unitBuilder.RequestUnitProduction(bot, harvInfo.Name);
|
||||
var harvCountTooLow =
|
||||
AIUtils.CountActorByCommonName(harvestersIndex) <
|
||||
AIUtils.CountActorByCommonName(refineries);
|
||||
if (harvCountTooLow)
|
||||
{
|
||||
var harvesterType = Info.HarvesterTypes.Random(world.LocalRandom);
|
||||
if (unitBuilder.RequestedProductionCount(bot, harvesterType) == 0)
|
||||
unitBuilder.RequestUnitProduction(bot, harvesterType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,5 +165,11 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
return Target.FromCell(world, path[0]);
|
||||
}
|
||||
|
||||
void INotifyActorDisposing.Disposing(Actor self)
|
||||
{
|
||||
refineries.Dispose();
|
||||
harvestersIndex.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user