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:
@@ -44,7 +44,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
public override object Create(ActorInitializer init) { return new CaptureManagerBotModule(init.Self, this); }
|
||||
}
|
||||
|
||||
public class CaptureManagerBotModule : ConditionalTrait<CaptureManagerBotModuleInfo>, IBotTick
|
||||
public class CaptureManagerBotModule : ConditionalTrait<CaptureManagerBotModuleInfo>, IBotTick, INotifyActorDisposing
|
||||
{
|
||||
readonly World world;
|
||||
readonly Player player;
|
||||
@@ -55,6 +55,8 @@ namespace OpenRA.Mods.Common.Traits
|
||||
// Units that the bot already knows about and has given a capture order. Any unit not on this list needs to be given a new order.
|
||||
readonly List<Actor> activeCapturers = new();
|
||||
|
||||
readonly ActorIndex.OwnerAndNamesAndTrait<Captures> capturingActors;
|
||||
|
||||
public CaptureManagerBotModule(Actor self, CaptureManagerBotModuleInfo info)
|
||||
: base(info)
|
||||
{
|
||||
@@ -67,6 +69,8 @@ namespace OpenRA.Mods.Common.Traits
|
||||
unitCannotBeOrderedOrIsIdle = a => a.Owner != player || a.IsDead || !a.IsInWorld || a.IsIdle;
|
||||
|
||||
maximumCaptureTargetOptions = Math.Max(1, Info.MaximumCaptureTargetOptions);
|
||||
|
||||
capturingActors = new ActorIndex.OwnerAndNamesAndTrait<Captures>(world, Info.CapturingActorTypes, player);
|
||||
}
|
||||
|
||||
protected override void TraitEnabled(Actor self)
|
||||
@@ -105,11 +109,8 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
activeCapturers.RemoveAll(unitCannotBeOrderedOrIsIdle);
|
||||
|
||||
var newUnits = world.ActorsHavingTrait<IPositionable>()
|
||||
.Where(a => a.Owner == player && !activeCapturers.Contains(a));
|
||||
|
||||
var capturers = newUnits
|
||||
.Where(a => a.IsIdle && Info.CapturingActorTypes.Contains(a.Info.Name) && a.Info.HasTraitInfo<CapturesInfo>())
|
||||
var capturers = capturingActors.Actors
|
||||
.Where(a => a.IsIdle && a.Info.HasTraitInfo<IPositionableInfo>() && !activeCapturers.Contains(a))
|
||||
.Select(a => new TraitPair<CaptureManager>(a, a.TraitOrDefault<CaptureManager>()))
|
||||
.Where(tp => tp.Trait != null)
|
||||
.ToArray();
|
||||
@@ -154,5 +155,10 @@ namespace OpenRA.Mods.Common.Traits
|
||||
activeCapturers.Add(capturer.Actor);
|
||||
}
|
||||
}
|
||||
|
||||
void INotifyActorDisposing.Disposing(Actor self)
|
||||
{
|
||||
capturingActors.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user