Fix bot module plumbing

Fixes the issues pointed out after the original harvester module was merged.
Also merges the update rules as discussed on IRC.
This commit is contained in:
reaperrr
2018-11-18 14:58:03 +01:00
committed by Paul Chote
parent 22bece2dc9
commit 67cba65800
14 changed files with 44 additions and 157 deletions

View File

@@ -365,6 +365,7 @@ namespace OpenRA.Traits
public interface IBot public interface IBot
{ {
void Activate(Player p); void Activate(Player p);
void QueueOrder(Order order);
IBotInfo Info { get; } IBotInfo Info { get; }
} }

View File

@@ -1,54 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.AI
{
public sealed class BotOrderManagerInfo : ITraitInfo
{
[Desc("Minimum portion of pending orders to issue each tick (e.g. 5 issues at least 1/5th of all pending orders). Excess orders remain queued for subsequent ticks.")]
public readonly int MinOrderQuotientPerTick = 5;
public object Create(ActorInitializer init) { return new BotOrderManager(this); }
}
public sealed class BotOrderManager : ITick
{
readonly BotOrderManagerInfo info;
readonly Queue<Order> orders = new Queue<Order>();
public BotOrderManager(BotOrderManagerInfo info)
{
this.info = info;
}
public void QueueOrder(Order order)
{
orders.Enqueue(order);
}
void IssueOrders(World world)
{
var ordersToIssueThisTick = Math.Min((orders.Count + info.MinOrderQuotientPerTick - 1) / info.MinOrderQuotientPerTick, orders.Count);
for (var i = 0; i < ordersToIssueThisTick; i++)
world.IssueOrder(orders.Dequeue());
}
void ITick.Tick(Actor self)
{
// Make sure we tick after all of the bot modules so that we don't introduce an additional tick delay
self.World.AddFrameEndTask(IssueOrders);
}
}
}

View File

@@ -44,6 +44,8 @@ namespace OpenRA.Mods.Common.AI
Enabled = true; Enabled = true;
} }
void IBot.QueueOrder(Order order) { }
IBotInfo IBot.Info { get { return info; } } IBotInfo IBot.Info { get { return info; } }
} }
} }

View File

@@ -19,7 +19,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.AI namespace OpenRA.Mods.Common.AI
{ {
public sealed class HackyAIInfo : IBotInfo, ITraitInfo, Requires<BotOrderManagerInfo> public sealed class HackyAIInfo : IBotInfo, ITraitInfo
{ {
public class UnitCategories public class UnitCategories
{ {
@@ -71,6 +71,9 @@ namespace OpenRA.Mods.Common.AI
[Desc("Minimum delay (in ticks) between creating squads.")] [Desc("Minimum delay (in ticks) between creating squads.")]
public readonly int MinimumAttackForceDelay = 0; public readonly int MinimumAttackForceDelay = 0;
[Desc("Minimum portion of pending orders to issue each tick (e.g. 5 issues at least 1/5th of all pending orders). Excess orders remain queued for subsequent ticks.")]
public readonly int MinOrderQuotientPerTick = 5;
[Desc("Minimum excess power the AI should try to maintain.")] [Desc("Minimum excess power the AI should try to maintain.")]
public readonly int MinimumExcessPower = 0; public readonly int MinimumExcessPower = 0;
@@ -258,10 +261,12 @@ namespace OpenRA.Mods.Common.AI
public List<Squad> Squads = new List<Squad>(); public List<Squad> Squads = new List<Squad>();
public Player Player { get; private set; } public Player Player { get; private set; }
readonly Queue<Order> orders = new Queue<Order>();
readonly Func<Actor, bool> isEnemyUnit; readonly Func<Actor, bool> isEnemyUnit;
readonly Predicate<Actor> unitCannotBeOrdered; readonly Predicate<Actor> unitCannotBeOrdered;
BotOrderManager botOrderManager; IBotTick[] tickModules;
CPos initialBaseCenter; CPos initialBaseCenter;
PowerManager playerPower; PowerManager playerPower;
@@ -317,7 +322,7 @@ namespace OpenRA.Mods.Common.AI
IsEnabled = true; IsEnabled = true;
playerPower = p.PlayerActor.TraitOrDefault<PowerManager>(); playerPower = p.PlayerActor.TraitOrDefault<PowerManager>();
playerResource = p.PlayerActor.Trait<PlayerResources>(); playerResource = p.PlayerActor.Trait<PlayerResources>();
botOrderManager = p.PlayerActor.Trait<BotOrderManager>(); tickModules = p.PlayerActor.TraitsImplementing<IBotTick>().ToArray();
supportPowerManager = new AISupportPowerManager(this, p); supportPowerManager = new AISupportPowerManager(this, p);
@@ -344,10 +349,15 @@ namespace OpenRA.Mods.Common.AI
resourceTypeIndices.Set(tileset.GetTerrainIndex(t.TerrainType), true); resourceTypeIndices.Set(tileset.GetTerrainIndex(t.TerrainType), true);
} }
// DEPRECATED: Bot modules should queue orders directly. void IBot.QueueOrder(Order order)
{
orders.Enqueue(order);
}
// DEPRECATED: Modules should use IBot.QueueOrder instead
public void QueueOrder(Order order) public void QueueOrder(Order order)
{ {
botOrderManager.QueueOrder(order); orders.Enqueue(order);
} }
ActorInfo ChooseRandomUnitToBuild(ProductionQueue queue) ActorInfo ChooseRandomUnitToBuild(ProductionQueue queue)
@@ -535,6 +545,18 @@ namespace OpenRA.Mods.Common.AI
foreach (var b in builders) foreach (var b in builders)
b.Tick(); b.Tick();
// TODO: Add an option to include this in CheckSyncAroundUnsyncedCode.
// Checking sync for this is too expensive to include it by default,
// so it should be implemented as separate sub-option checkbox.
using (new PerfSample("tick_bots"))
foreach (var t in tickModules)
if (t.IsTraitEnabled())
t.BotTick(this);
var ordersToIssueThisTick = Math.Min((orders.Count + Info.MinOrderQuotientPerTick - 1) / Info.MinOrderQuotientPerTick, orders.Count);
for (var i = 0; i < ordersToIssueThisTick; i++)
World.IssueOrder(orders.Dequeue());
} }
internal Actor FindClosestEnemy(WPos pos) internal Actor FindClosestEnemy(WPos pos)

View File

@@ -123,7 +123,6 @@
<Compile Include="AI\AIUtils.cs" /> <Compile Include="AI\AIUtils.cs" />
<Compile Include="AI\AttackOrFleeFuzzy.cs" /> <Compile Include="AI\AttackOrFleeFuzzy.cs" />
<Compile Include="AI\BaseBuilder.cs" /> <Compile Include="AI\BaseBuilder.cs" />
<Compile Include="AI\BotOrderManager.cs" />
<Compile Include="AI\HackyAI.cs" /> <Compile Include="AI\HackyAI.cs" />
<Compile Include="Traits\BotModules\HarvesterBotModule.cs" /> <Compile Include="Traits\BotModules\HarvesterBotModule.cs" />
<Compile Include="AI\AISupportPowerManager.cs" /> <Compile Include="AI\AISupportPowerManager.cs" />
@@ -942,8 +941,7 @@
<Compile Include="UpdateRules\Rules\20180923\RemoveRepairBuildingsFromAircraft.cs" /> <Compile Include="UpdateRules\Rules\20180923\RemoveRepairBuildingsFromAircraft.cs" />
<Compile Include="UpdateRules\Rules\20180923\AddRearmable.cs" /> <Compile Include="UpdateRules\Rules\20180923\AddRearmable.cs" />
<Compile Include="UpdateRules\Rules\20180923\MergeAttackPlaneAndHeli.cs" /> <Compile Include="UpdateRules\Rules\20180923\MergeAttackPlaneAndHeli.cs" />
<Compile Include="UpdateRules\Rules\20180923\AddBotOrderManager.cs" /> <Compile Include="UpdateRules\Rules\20180923\ExtractHackyAIModules.cs" />
<Compile Include="UpdateRules\Rules\20180923\AddHarvesterBotModule.cs" />
<Compile Include="Traits\Player\PlayerResources.cs" /> <Compile Include="Traits\Player\PlayerResources.cs" />
<Compile Include="UtilityCommands\DumpSequenceSheetsCommand.cs" /> <Compile Include="UtilityCommands\DumpSequenceSheetsCommand.cs" />
<Compile Include="Traits\Render\WithBuildingRepairDecoration.cs" /> <Compile Include="Traits\Render\WithBuildingRepairDecoration.cs" />

View File

@@ -20,7 +20,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits 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.")] [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, Requires<BotOrderManagerInfo> public class HarvesterBotModuleInfo : ConditionalTraitInfo
{ {
[Desc("Interval (in ticks) between giving out orders to idle harvesters.")] [Desc("Interval (in ticks) between giving out orders to idle harvesters.")]
public readonly int ScanForIdleHarvestersInterval = 20; public readonly int ScanForIdleHarvestersInterval = 20;
@@ -31,7 +31,7 @@ namespace OpenRA.Mods.Common.Traits
public override object Create(ActorInitializer init) { return new HarvesterBotModule(init.Self, this); } public override object Create(ActorInitializer init) { return new HarvesterBotModule(init.Self, this); }
} }
public class HarvesterBotModule : ConditionalTrait<HarvesterBotModuleInfo>, ITick public class HarvesterBotModule : ConditionalTrait<HarvesterBotModuleInfo>, IBotTick
{ {
readonly World world; readonly World world;
readonly Player player; readonly Player player;
@@ -40,7 +40,6 @@ namespace OpenRA.Mods.Common.Traits
DomainIndex domainIndex; DomainIndex domainIndex;
ResourceLayer resLayer; ResourceLayer resLayer;
ResourceClaimLayer claimLayer; ResourceClaimLayer claimLayer;
BotOrderManager botOrderManager;
List<Actor> harvesters = new List<Actor>(); List<Actor> harvesters = new List<Actor>();
int scanForIdleHarvestersTicks; int scanForIdleHarvestersTicks;
@@ -58,15 +57,11 @@ namespace OpenRA.Mods.Common.Traits
domainIndex = world.WorldActor.Trait<DomainIndex>(); domainIndex = world.WorldActor.Trait<DomainIndex>();
resLayer = world.WorldActor.TraitOrDefault<ResourceLayer>(); resLayer = world.WorldActor.TraitOrDefault<ResourceLayer>();
claimLayer = world.WorldActor.TraitOrDefault<ResourceClaimLayer>(); claimLayer = world.WorldActor.TraitOrDefault<ResourceClaimLayer>();
botOrderManager = self.Owner.PlayerActor.Trait<BotOrderManager>();
scanForIdleHarvestersTicks = Info.ScanForIdleHarvestersInterval; scanForIdleHarvestersTicks = Info.ScanForIdleHarvestersInterval;
} }
void ITick.Tick(Actor self) void IBotTick.BotTick(IBot bot)
{ {
if (IsTraitDisabled)
return;
if (resLayer == null || resLayer.IsResourceLayerEmpty) if (resLayer == null || resLayer.IsResourceLayerEmpty)
return; return;
@@ -103,7 +98,7 @@ namespace OpenRA.Mods.Common.Traits
// Tell the idle harvester to quit slacking: // Tell the idle harvester to quit slacking:
var newSafeResourcePatch = FindNextResource(harvester, harv); var newSafeResourcePatch = FindNextResource(harvester, harv);
AIUtils.BotDebug("AI: Harvester {0} is idle. Ordering to {1} in search for new resources.".F(harvester, newSafeResourcePatch)); AIUtils.BotDebug("AI: Harvester {0} is idle. Ordering to {1} in search for new resources.".F(harvester, newSafeResourcePatch));
botOrderManager.QueueOrder(new Order("Harvest", harvester, Target.FromCell(world, newSafeResourcePatch), false)); bot.QueueOrder(new Order("Harvest", harvester, Target.FromCell(world, newSafeResourcePatch), false));
} }
} }

View File

@@ -14,6 +14,7 @@ using System.Drawing;
using OpenRA.Activities; using OpenRA.Activities;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Mods.Common.Activities; using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.AI;
using OpenRA.Mods.Common.Graphics; using OpenRA.Mods.Common.Graphics;
using OpenRA.Primitives; using OpenRA.Primitives;
using OpenRA.Traits; using OpenRA.Traits;
@@ -446,4 +447,7 @@ namespace OpenRA.Mods.Common.Traits
[RequireExplicitImplementation] [RequireExplicitImplementation]
public interface IPreventsShroudReset { bool PreventShroudReset(Actor self); } public interface IPreventsShroudReset { bool PreventShroudReset(Actor self); }
[RequireExplicitImplementation]
public interface IBotTick { void BotTick(IBot bot); }
} }

View File

@@ -1,76 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Linq;
namespace OpenRA.Mods.Common.UpdateRules.Rules
{
public class AddBotOrderManager : UpdateRule
{
public override string Name { get { return "Split bot order management from HackyAI to BotOrderManager"; } }
public override string Description
{
get
{
return "The MinOrderQuotientPerTick property and all bot order handling have been moved from HackyAI\n" +
"to the new BotOrderManager.";
}
}
bool showMessage;
bool messageShown;
public override IEnumerable<string> AfterUpdate(ModData modData)
{
var message = "You may want to manually change MinOrderQuotientPerTick on BotOrderManager,\n" +
"if you were using a custom value on any AI.";
if (showMessage && !messageShown)
yield return message;
messageShown = true;
}
public override IEnumerable<string> UpdateActorNode(ModData modData, MiniYamlNode actorNode)
{
if (actorNode.Key != "Player")
yield break;
var hackyAIs = actorNode.ChildrenMatching("HackyAI");
if (!hackyAIs.Any())
yield break;
foreach (var hackyAINode in hackyAIs)
{
// We no longer support individual values for each AI,
// and in practice the default of 5 has proven to be a solid middle-ground,
// so just removing any custom value and notifying the modder about it should suffice.
var minQuotient = hackyAINode.LastChildMatching("MinOrderQuotientPerTick");
if (minQuotient != null)
{
hackyAINode.RemoveNode(minQuotient);
if (!showMessage)
showMessage = true;
}
}
var botOrderManager = actorNode.LastChildMatching("BotOrderManager");
if (botOrderManager == null)
{
var addBotOrderManager = new MiniYamlNode("BotOrderManager", "");
actorNode.AddNode(addBotOrderManager);
}
yield break;
}
}
}

View File

@@ -14,15 +14,15 @@ using System.Linq;
namespace OpenRA.Mods.Common.UpdateRules.Rules namespace OpenRA.Mods.Common.UpdateRules.Rules
{ {
public class AddHarvesterBotModule : UpdateRule public class ExtractHackyAIModules : UpdateRule
{ {
public override string Name { get { return "Split HackyAI harvester handling to HarvesterBotModule"; } } public override string Name { get { return "Split HackyAI logic handling to BotModules"; } }
public override string Description public override string Description
{ {
get get
{ {
return "Some properties and all harvester handling have been moved from HackyAI\n" + return "Most properties and logic are being moved from HackyAI\n" +
"to the new HarvesterBotModule."; "to *BotModules.";
} }
} }

View File

@@ -105,8 +105,7 @@ namespace OpenRA.Mods.Common.UpdateRules
new RemovedDemolishLocking(), new RemovedDemolishLocking(),
new RequireProductionType(), new RequireProductionType(),
new CloakRequiresConditionToPause(), new CloakRequiresConditionToPause(),
new AddBotOrderManager(), new ExtractHackyAIModules(),
new AddHarvesterBotModule(),
new RemoveNegativeDamageFullHealthCheck(), new RemoveNegativeDamageFullHealthCheck(),
new RemoveResourceExplodeModifier(), new RemoveResourceExplodeModifier(),
}) })

View File

@@ -1,5 +1,4 @@
Player: Player:
BotOrderManager:
HackyAI@Cabal: HackyAI@Cabal:
Name: Cabal Name: Cabal
Type: cabal Type: cabal

View File

@@ -1,5 +1,4 @@
Player: Player:
BotOrderManager:
HackyAI@Omnius: HackyAI@Omnius:
Name: Omnius Name: Omnius
Type: omnius Type: omnius

View File

@@ -1,5 +1,4 @@
Player: Player:
BotOrderManager:
HackyAI@RushAI: HackyAI@RushAI:
Name: Rush AI Name: Rush AI
Type: rush Type: rush

View File

@@ -1,5 +1,4 @@
Player: Player:
BotOrderManager:
HackyAI@TestAI: HackyAI@TestAI:
Name: Test AI Name: Test AI
Type: test Type: test