From ab50182c927776092f68868b77b1a72bfa737114 Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Sat, 14 Sep 2024 18:32:54 +0100 Subject: [PATCH] Change ActorIndex to work in terms of TraitInfo, instead of Trait. This allows actor.Info.HasTraitInfo to be used when checking if an actor needs to be added to the index, which is a cheaper call than actor.TraitsImplementing. --- OpenRA.Mods.Common/AIUtils.cs | 3 ++- OpenRA.Mods.Common/ActorIndex.cs | 17 +++++++++-------- .../Traits/BotModules/BaseBuilderBotModule.cs | 16 ++++++++-------- .../BotModules/CaptureManagerBotModule.cs | 4 ++-- .../Traits/BotModules/HarvesterBotModule.cs | 8 ++++---- .../Traits/BotModules/McvManagerBotModule.cs | 12 ++++++------ .../Traits/BotModules/SquadManagerBotModule.cs | 4 ++-- 7 files changed, 33 insertions(+), 31 deletions(-) diff --git a/OpenRA.Mods.Common/AIUtils.cs b/OpenRA.Mods.Common/AIUtils.cs index e9635ba526..6c364414b0 100644 --- a/OpenRA.Mods.Common/AIUtils.cs +++ b/OpenRA.Mods.Common/AIUtils.cs @@ -47,7 +47,8 @@ namespace OpenRA.Mods.Common return owner.World.ActorsHavingTrait().Count(a => a.Owner == owner && a.Info.Name == actorName); } - public static int CountActorByCommonName(ActorIndex.OwnerAndNamesAndTrait actorIndex) + public static int CountActorByCommonName( + ActorIndex.OwnerAndNamesAndTrait actorIndex) where TTraitInfo : ITraitInfoInterface { return actorIndex.Actors.Count(a => !a.IsDead); } diff --git a/OpenRA.Mods.Common/ActorIndex.cs b/OpenRA.Mods.Common/ActorIndex.cs index 4075bbd4de..eee8482f68 100644 --- a/OpenRA.Mods.Common/ActorIndex.cs +++ b/OpenRA.Mods.Common/ActorIndex.cs @@ -12,6 +12,7 @@ using System; using System.Collections.Generic; using System.Linq; +using OpenRA.Traits; namespace OpenRA.Mods.Common { @@ -97,9 +98,9 @@ namespace OpenRA.Mods.Common /// /// Maintains an index of actors in the world that /// have one of the given - /// and have the trait of type . + /// and have the trait with info of type . /// - public sealed class NamesAndTrait : ActorIndex + public sealed class NamesAndTrait : ActorIndex where TTraitInfo : ITraitInfoInterface { readonly HashSet names; @@ -111,12 +112,12 @@ namespace OpenRA.Mods.Common static IEnumerable ActorsToIndex(World world, HashSet names) { - return world.ActorsHavingTrait().Where(a => names.Contains(a.Info.Name)); + return world.Actors.Where(a => names.Contains(a.Info.Name) && a.Info.HasTraitInfo()); } protected override bool ShouldIndexActor(Actor actor) { - return names.Contains(actor.Info.Name) && actor.TraitsImplementing().Any(); + return names.Contains(actor.Info.Name) && actor.Info.HasTraitInfo(); } } @@ -124,9 +125,9 @@ namespace OpenRA.Mods.Common /// Maintains an index of actors in the world that /// are owned by a given , /// have one of the given - /// and have the trait of type . + /// and have the trait with info of type . /// - public sealed class OwnerAndNamesAndTrait : ActorIndex + public sealed class OwnerAndNamesAndTrait : ActorIndex where TTraitInfo : ITraitInfoInterface { readonly HashSet names; readonly Player owner; @@ -140,12 +141,12 @@ namespace OpenRA.Mods.Common static IEnumerable ActorsToIndex(World world, HashSet names, Player owner) { - return world.ActorsHavingTrait().Where(a => a.Owner == owner && names.Contains(a.Info.Name)); + return world.Actors.Where(a => a.Owner == owner && names.Contains(a.Info.Name) && a.Info.HasTraitInfo()); } protected override bool ShouldIndexActor(Actor actor) { - return actor.Owner == owner && names.Contains(actor.Info.Name) && actor.TraitsImplementing().Any(); + return actor.Owner == owner && names.Contains(actor.Info.Name) && actor.Info.HasTraitInfo(); } } } diff --git a/OpenRA.Mods.Common/Traits/BotModules/BaseBuilderBotModule.cs b/OpenRA.Mods.Common/Traits/BotModules/BaseBuilderBotModule.cs index 56c8bd0c0c..81b827e0f5 100644 --- a/OpenRA.Mods.Common/Traits/BotModules/BaseBuilderBotModule.cs +++ b/OpenRA.Mods.Common/Traits/BotModules/BaseBuilderBotModule.cs @@ -173,10 +173,10 @@ namespace OpenRA.Mods.Common.Traits readonly BaseBuilderQueueManager[] builders; int currentBuilderIndex = 0; - readonly ActorIndex.OwnerAndNamesAndTrait refineryBuildings; - readonly ActorIndex.OwnerAndNamesAndTrait powerBuildings; - readonly ActorIndex.OwnerAndNamesAndTrait constructionYardBuildings; - readonly ActorIndex.OwnerAndNamesAndTrait barracksBuildings; + readonly ActorIndex.OwnerAndNamesAndTrait refineryBuildings; + readonly ActorIndex.OwnerAndNamesAndTrait powerBuildings; + readonly ActorIndex.OwnerAndNamesAndTrait constructionYardBuildings; + readonly ActorIndex.OwnerAndNamesAndTrait barracksBuildings; public BaseBuilderBotModule(Actor self, BaseBuilderBotModuleInfo info) : base(info) @@ -184,10 +184,10 @@ namespace OpenRA.Mods.Common.Traits world = self.World; player = self.Owner; builders = new BaseBuilderQueueManager[info.BuildingQueues.Count + info.DefenseQueues.Count]; - refineryBuildings = new ActorIndex.OwnerAndNamesAndTrait(world, info.RefineryTypes, player); - powerBuildings = new ActorIndex.OwnerAndNamesAndTrait(world, info.PowerTypes, player); - constructionYardBuildings = new ActorIndex.OwnerAndNamesAndTrait(world, info.ConstructionYardTypes, player); - barracksBuildings = new ActorIndex.OwnerAndNamesAndTrait(world, info.BarracksTypes, player); + refineryBuildings = new ActorIndex.OwnerAndNamesAndTrait(world, info.RefineryTypes, player); + powerBuildings = new ActorIndex.OwnerAndNamesAndTrait(world, info.PowerTypes, player); + constructionYardBuildings = new ActorIndex.OwnerAndNamesAndTrait(world, info.ConstructionYardTypes, player); + barracksBuildings = new ActorIndex.OwnerAndNamesAndTrait(world, info.BarracksTypes, player); } protected override void Created(Actor self) diff --git a/OpenRA.Mods.Common/Traits/BotModules/CaptureManagerBotModule.cs b/OpenRA.Mods.Common/Traits/BotModules/CaptureManagerBotModule.cs index 3badc02030..351656e6d4 100644 --- a/OpenRA.Mods.Common/Traits/BotModules/CaptureManagerBotModule.cs +++ b/OpenRA.Mods.Common/Traits/BotModules/CaptureManagerBotModule.cs @@ -55,7 +55,7 @@ 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 activeCapturers = new(); - readonly ActorIndex.OwnerAndNamesAndTrait capturingActors; + readonly ActorIndex.OwnerAndNamesAndTrait capturingActors; public CaptureManagerBotModule(Actor self, CaptureManagerBotModuleInfo info) : base(info) @@ -70,7 +70,7 @@ namespace OpenRA.Mods.Common.Traits maximumCaptureTargetOptions = Math.Max(1, Info.MaximumCaptureTargetOptions); - capturingActors = new ActorIndex.OwnerAndNamesAndTrait(world, Info.CapturingActorTypes, player); + capturingActors = new ActorIndex.OwnerAndNamesAndTrait(world, Info.CapturingActorTypes, player); } protected override void TraitEnabled(Actor self) diff --git a/OpenRA.Mods.Common/Traits/BotModules/HarvesterBotModule.cs b/OpenRA.Mods.Common/Traits/BotModules/HarvesterBotModule.cs index d213a05e82..33b1c45764 100644 --- a/OpenRA.Mods.Common/Traits/BotModules/HarvesterBotModule.cs +++ b/OpenRA.Mods.Common/Traits/BotModules/HarvesterBotModule.cs @@ -70,8 +70,8 @@ namespace OpenRA.Mods.Common.Traits readonly Func unitCannotBeOrdered; readonly Dictionary harvesters = new(); readonly Stack harvestersNeedingOrders = new(); - readonly ActorIndex.OwnerAndNamesAndTrait refineries; - readonly ActorIndex.OwnerAndNamesAndTrait harvestersIndex; + readonly ActorIndex.OwnerAndNamesAndTrait refineries; + readonly ActorIndex.OwnerAndNamesAndTrait harvestersIndex; readonly Dictionary resourceTypesByCell = new(); IResourceLayer resourceLayer; @@ -85,8 +85,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(world, info.RefineryTypes, player); - harvestersIndex = new ActorIndex.OwnerAndNamesAndTrait(world, info.HarvesterTypes, player); + refineries = new ActorIndex.OwnerAndNamesAndTrait(world, info.RefineryTypes, player); + harvestersIndex = new ActorIndex.OwnerAndNamesAndTrait(world, info.HarvesterTypes, player); } protected override void Created(Actor self) diff --git a/OpenRA.Mods.Common/Traits/BotModules/McvManagerBotModule.cs b/OpenRA.Mods.Common/Traits/BotModules/McvManagerBotModule.cs index 393e920501..7e46e8b062 100644 --- a/OpenRA.Mods.Common/Traits/BotModules/McvManagerBotModule.cs +++ b/OpenRA.Mods.Common/Traits/BotModules/McvManagerBotModule.cs @@ -60,9 +60,9 @@ namespace OpenRA.Mods.Common.Traits readonly World world; readonly Player player; - readonly ActorIndex.OwnerAndNamesAndTrait mcvs; - readonly ActorIndex.OwnerAndNamesAndTrait constructionYards; - readonly ActorIndex.OwnerAndNamesAndTrait mcvFactories; + readonly ActorIndex.OwnerAndNamesAndTrait mcvs; + readonly ActorIndex.OwnerAndNamesAndTrait constructionYards; + readonly ActorIndex.OwnerAndNamesAndTrait mcvFactories; IBotPositionsUpdated[] notifyPositionsUpdated; IBotRequestUnitProduction[] requestUnitProduction; @@ -76,9 +76,9 @@ namespace OpenRA.Mods.Common.Traits { world = self.World; player = self.Owner; - mcvs = new ActorIndex.OwnerAndNamesAndTrait(world, info.McvTypes, player); - constructionYards = new ActorIndex.OwnerAndNamesAndTrait(world, info.ConstructionYardTypes, player); - mcvFactories = new ActorIndex.OwnerAndNamesAndTrait(world, info.McvFactoryTypes, player); + mcvs = new ActorIndex.OwnerAndNamesAndTrait(world, info.McvTypes, player); + constructionYards = new ActorIndex.OwnerAndNamesAndTrait(world, info.ConstructionYardTypes, player); + mcvFactories = new ActorIndex.OwnerAndNamesAndTrait(world, info.McvFactoryTypes, player); } protected override void Created(Actor self) diff --git a/OpenRA.Mods.Common/Traits/BotModules/SquadManagerBotModule.cs b/OpenRA.Mods.Common/Traits/BotModules/SquadManagerBotModule.cs index 755a6f1b06..d748aedf85 100644 --- a/OpenRA.Mods.Common/Traits/BotModules/SquadManagerBotModule.cs +++ b/OpenRA.Mods.Common/Traits/BotModules/SquadManagerBotModule.cs @@ -126,7 +126,7 @@ namespace OpenRA.Mods.Common.Traits public List Squads = new(); readonly Stack squadsPendingUpdate = new(); - readonly ActorIndex.NamesAndTrait constructionYardBuildings; + readonly ActorIndex.NamesAndTrait constructionYardBuildings; IBot bot; IBotPositionsUpdated[] notifyPositionsUpdated; @@ -146,7 +146,7 @@ namespace OpenRA.Mods.Common.Traits Player = self.Owner; unitCannotBeOrdered = a => a == null || a.Owner != Player || a.IsDead || !a.IsInWorld; - constructionYardBuildings = new ActorIndex.NamesAndTrait(World, info.ConstructionYardTypes); + constructionYardBuildings = new ActorIndex.NamesAndTrait(World, info.ConstructionYardTypes); } // Use for proactive targeting.