From e026a0f00f1a655f52a84ebadc29f1f5165391e0 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Wed, 13 Jan 2016 21:45:15 +0000 Subject: [PATCH] Create proper data structures for hardcoded AI classes. --- OpenRA.Mods.Common/AI/BaseBuilder.cs | 19 +++---- OpenRA.Mods.Common/AI/HackyAI.cs | 78 ++++++++++++++++------------ 2 files changed, 52 insertions(+), 45 deletions(-) diff --git a/OpenRA.Mods.Common/AI/BaseBuilder.cs b/OpenRA.Mods.Common/AI/BaseBuilder.cs index ae7e359276..ba4341c7c7 100644 --- a/OpenRA.Mods.Common/AI/BaseBuilder.cs +++ b/OpenRA.Mods.Common/AI/BaseBuilder.cs @@ -170,12 +170,8 @@ namespace OpenRA.Mods.Common.AI return true; } - ActorInfo GetProducibleBuilding(string commonName, IEnumerable buildables, Func orderBy = null) + ActorInfo GetProducibleBuilding(HashSet actors, IEnumerable buildables, Func orderBy = null) { - HashSet actors; - if (!ai.Info.BuildingCommonNames.TryGetValue(commonName, out actors)) - return null; - var available = buildables.Where(actor => { // Are we able to build this? @@ -205,7 +201,7 @@ namespace OpenRA.Mods.Common.AI var buildableThings = queue.BuildableItems(); // This gets used quite a bit, so let's cache it here - var power = GetProducibleBuilding("Power", buildableThings, + var power = GetProducibleBuilding(ai.Info.BuildingCommonNames.Power, buildableThings, a => a.TraitInfos().Where(i => i.UpgradeMinEnabledLevel < 1).Sum(p => p.Amount)); // First priority is to get out of a low power situation @@ -221,7 +217,7 @@ namespace OpenRA.Mods.Common.AI // Next is to build up a strong economy if (!ai.HasAdequateProc() || !ai.HasMinimumProc()) { - var refinery = GetProducibleBuilding("Refinery", buildableThings); + var refinery = GetProducibleBuilding(ai.Info.BuildingCommonNames.Refinery, buildableThings); if (refinery != null && HasSufficientPowerForActor(refinery)) { HackyAI.BotDebug("AI: {0} decided to build {1}: Priority override (refinery)", queue.Actor.Owner, refinery.Name); @@ -238,7 +234,7 @@ namespace OpenRA.Mods.Common.AI // Make sure that we can spend as fast as we are earning if (ai.Info.NewProductionCashThreshold > 0 && playerResources.Resources > ai.Info.NewProductionCashThreshold) { - var production = GetProducibleBuilding("Production", buildableThings); + var production = GetProducibleBuilding(ai.Info.BuildingCommonNames.Production, buildableThings); if (production != null && HasSufficientPowerForActor(production)) { HackyAI.BotDebug("AI: {0} decided to build {1}: Priority override (production)", queue.Actor.Owner, production.Name); @@ -257,7 +253,7 @@ namespace OpenRA.Mods.Common.AI && playerResources.Resources > ai.Info.NewProductionCashThreshold && ai.CloseEnoughToWater()) { - var navalproduction = GetProducibleBuilding("NavalProduction", buildableThings); + var navalproduction = GetProducibleBuilding(ai.Info.BuildingCommonNames.NavalProduction, buildableThings); if (navalproduction != null && HasSufficientPowerForActor(navalproduction)) { HackyAI.BotDebug("AI: {0} decided to build {1}: Priority override (navalproduction)", queue.Actor.Owner, navalproduction.Name); @@ -274,7 +270,7 @@ namespace OpenRA.Mods.Common.AI // Create some head room for resource storage if we really need it if (playerResources.Resources > 0.8 * playerResources.ResourceCapacity) { - var silo = GetProducibleBuilding("Silo", buildableThings); + var silo = GetProducibleBuilding(ai.Info.BuildingCommonNames.Silo, buildableThings); if (silo != null && HasSufficientPowerForActor(silo)) { HackyAI.BotDebug("AI: {0} decided to build {1}: Priority override (silo)", queue.Actor.Owner, silo.Name); @@ -308,8 +304,7 @@ namespace OpenRA.Mods.Common.AI // If we're considering to build a naval structure, check whether there is enough water inside the base perimeter // and any structure providing buildable area close enough to that water. // TODO: Extend this check to cover any naval structure, not just production. - if (ai.Info.BuildingCommonNames.ContainsKey("NavalProduction") - && ai.Info.BuildingCommonNames["NavalProduction"].Contains(name) + if (ai.Info.BuildingCommonNames.NavalProduction.Contains(name) && (waterState == Water.NotEnoughWater || !ai.CloseEnoughToWater())) continue; diff --git a/OpenRA.Mods.Common/AI/HackyAI.cs b/OpenRA.Mods.Common/AI/HackyAI.cs index 3730d2307f..bc3331e274 100644 --- a/OpenRA.Mods.Common/AI/HackyAI.cs +++ b/OpenRA.Mods.Common/AI/HackyAI.cs @@ -23,6 +23,23 @@ namespace OpenRA.Mods.Common.AI { public sealed class HackyAIInfo : IBotInfo, ITraitInfo { + public class UnitCategories + { + public readonly HashSet Mcv = new HashSet(); + } + + public class BuildingCategories + { + public readonly HashSet ConstructionYard = new HashSet(); + public readonly HashSet VehiclesFactory = new HashSet(); + public readonly HashSet Refinery = new HashSet(); + public readonly HashSet Power = new HashSet(); + public readonly HashSet Barracks = new HashSet(); + public readonly HashSet Production = new HashSet(); + public readonly HashSet NavalProduction = new HashSet(); + public readonly HashSet Silo = new HashSet(); + } + [Desc("Ingame name this bot uses.")] public readonly string Name = "Unnamed Bot"; @@ -133,11 +150,13 @@ namespace OpenRA.Mods.Common.AI public readonly Dictionary BuildingFractions = null; [Desc("Tells the AI what unit types fall under the same common name. Only supported entry is Mcv.")] - public readonly Dictionary> UnitsCommonNames = null; + [FieldLoader.LoadUsing("LoadUnitCategories", true)] + public readonly UnitCategories UnitsCommonNames; [Desc("Tells the AI what building types fall under the same common name.", "Possible keys are ConstructionYard, Power, Refinery, Silo , Barracks, Production, VehiclesFactory, NavalProduction.")] - public readonly Dictionary> BuildingCommonNames = null; + [FieldLoader.LoadUsing("LoadBuildingCategories", true)] + public readonly BuildingCategories BuildingCommonNames; [Desc("What buildings should the AI have a maximum limit to build.")] public readonly Dictionary BuildingLimits = null; @@ -147,6 +166,18 @@ namespace OpenRA.Mods.Common.AI [FieldLoader.LoadUsing("LoadDecisions")] public readonly List PowerDecisions = new List(); + static object LoadUnitCategories(MiniYaml yaml) + { + var categories = yaml.Nodes.First(n => n.Key == "UnitsCommonNames"); + return FieldLoader.Load(categories.Value); + } + + static object LoadBuildingCategories(MiniYaml yaml) + { + var categories = yaml.Nodes.First(n => n.Key == "BuildingCommonNames"); + return FieldLoader.Load(categories.Value); + } + static object LoadDecisions(MiniYaml yaml) { var ret = new List(); @@ -378,57 +409,38 @@ namespace OpenRA.Mods.Common.AI return World.ActorsHavingTrait().Count(a => a.Owner == owner && a.Info.Name == unit); } - int? CountBuildingByCommonName(string commonName, Player owner) + int CountBuildingByCommonName(HashSet buildings, Player owner) { - if (!Info.BuildingCommonNames.ContainsKey(commonName)) - return null; - return World.ActorsHavingTrait() - .Count(a => a.Owner == owner && Info.BuildingCommonNames[commonName].Contains(a.Info.Name)); + .Count(a => a.Owner == owner && buildings.Contains(a.Info.Name)); } - public ActorInfo GetBuildingInfoByCommonName(string commonName, Player owner) + public ActorInfo GetInfoByCommonName(HashSet names, Player owner) { - if (commonName == "ConstructionYard") - return Map.Rules.Actors.Where(k => Info.BuildingCommonNames[commonName].Contains(k.Key)).Random(Random).Value; - - return GetInfoByCommonName(Info.BuildingCommonNames, commonName, owner); - } - - public ActorInfo GetUnitInfoByCommonName(string commonName, Player owner) - { - return GetInfoByCommonName(Info.UnitsCommonNames, commonName, owner); - } - - public ActorInfo GetInfoByCommonName(Dictionary> names, string commonName, Player owner) - { - if (!names.Any() || !names.ContainsKey(commonName)) - throw new InvalidOperationException("Can't find {0} in the HackyAI UnitsCommonNames definition.".F(commonName)); - - return Map.Rules.Actors.Where(k => names[commonName].Contains(k.Key)).Random(Random).Value; + return Map.Rules.Actors.Where(k => names.Contains(k.Key)).Random(Random).Value; } public bool HasAdequateFact() { // Require at least one construction yard, unless we have no vehicles factory (can't build it). - return CountBuildingByCommonName("ConstructionYard", Player) > 0 || - CountBuildingByCommonName("VehiclesFactory", Player) == 0; + return CountBuildingByCommonName(Info.BuildingCommonNames.ConstructionYard, Player) > 0 || + CountBuildingByCommonName(Info.BuildingCommonNames.VehiclesFactory, Player) == 0; } public bool HasAdequateProc() { // Require at least one refinery, unless we have no power (can't build it). - return CountBuildingByCommonName("Refinery", Player) > 0 || - CountBuildingByCommonName("Power", Player) == 0; + return CountBuildingByCommonName(Info.BuildingCommonNames.Refinery, Player) > 0 || + CountBuildingByCommonName(Info.BuildingCommonNames.Power, Player) == 0; } public bool HasMinimumProc() { // Require at least two refineries, unless we have no power (can't build it) // or barracks (higher priority?) - return CountBuildingByCommonName("Refinery", Player) >= 2 || - CountBuildingByCommonName("Power", Player) == 0 || - CountBuildingByCommonName("Barracks", Player) == 0; + return CountBuildingByCommonName(Info.BuildingCommonNames.Refinery, Player) >= 2 || + CountBuildingByCommonName(Info.BuildingCommonNames.Power, Player) == 0 || + CountBuildingByCommonName(Info.BuildingCommonNames.Barracks, Player) == 0; } // For mods like RA (number of building must match the number of aircraft) @@ -1014,7 +1026,7 @@ namespace OpenRA.Mods.Common.AI // No construction yards - Build a new MCV if (!HasAdequateFact() && !self.World.ActorsHavingTrait() .Any(a => a.Owner == Player && a.Info.HasTraitInfo())) - BuildUnit("Vehicle", GetUnitInfoByCommonName("Mcv", Player).Name); + BuildUnit("Vehicle", GetInfoByCommonName(Info.UnitsCommonNames.Mcv, Player).Name); foreach (var q in Info.UnitQueues) BuildUnit(q, unitsHangingAroundTheBase.Count < Info.IdleBaseUnitsMaximum);