From 137d3be3465b71ef3f4ff10f269df1408cc48351 Mon Sep 17 00:00:00 2001 From: reaperrr Date: Thu, 3 Jan 2019 18:06:48 +0100 Subject: [PATCH] Add plumbing for bots auto-replacing harvesters If their number drops below refinery count. --- .../Traits/BotModules/HarvesterBotModule.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/OpenRA.Mods.Common/Traits/BotModules/HarvesterBotModule.cs b/OpenRA.Mods.Common/Traits/BotModules/HarvesterBotModule.cs index 2146bf6457..1798b81dad 100644 --- a/OpenRA.Mods.Common/Traits/BotModules/HarvesterBotModule.cs +++ b/OpenRA.Mods.Common/Traits/BotModules/HarvesterBotModule.cs @@ -21,6 +21,13 @@ namespace OpenRA.Mods.Common.Traits [Desc("Put this on the Player actor. Manages bot harvesters to ensure they always continue harvesting as long as there are resources on the map.")] public class HarvesterBotModuleInfo : ConditionalTraitInfo { + [Desc("Actor types that are considered harvesters. If harvester count drops below RefineryTypes count, a new harvester is built.", + "Leave empty to disable harvester replacement. Currently only needed by harvester replacement system.")] + public readonly HashSet HarvesterTypes = new HashSet(); + + [Desc("Actor types that are counted as refineries. Currently only needed by harvester replacement system.")] + public readonly HashSet RefineryTypes = new HashSet(); + [Desc("Interval (in ticks) between giving out orders to idle harvesters.")] public readonly int ScanForIdleHarvestersInterval = 50; @@ -35,6 +42,7 @@ namespace OpenRA.Mods.Common.Traits readonly World world; readonly Player player; readonly Predicate unitCannotBeOrdered; + IBotRequestUnitProduction[] requestUnitProduction; IPathFinder pathfinder; DomainIndex domainIndex; ResourceLayer resLayer; @@ -52,6 +60,7 @@ namespace OpenRA.Mods.Common.Traits protected override void TraitEnabled(Actor self) { + requestUnitProduction = player.PlayerActor.TraitsImplementing().ToArray(); pathfinder = world.WorldActor.Trait(); domainIndex = world.WorldActor.Trait(); resLayer = world.WorldActor.TraitOrDefault(); @@ -99,6 +108,16 @@ namespace OpenRA.Mods.Common.Traits 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)); } + + // Less harvesters than refineries - build a new harvester + var unitBuilder = requestUnitProduction.FirstOrDefault(Exts.IsTraitEnabled); + if (unitBuilder != null && Info.HarvesterTypes.Any()) + { + 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); + } } CPos FindNextResource(Actor actor, Harvester harv)