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:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -44,6 +44,8 @@ namespace OpenRA.Mods.Common.AI
|
||||
Enabled = true;
|
||||
}
|
||||
|
||||
void IBot.QueueOrder(Order order) { }
|
||||
|
||||
IBotInfo IBot.Info { get { return info; } }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.AI
|
||||
{
|
||||
public sealed class HackyAIInfo : IBotInfo, ITraitInfo, Requires<BotOrderManagerInfo>
|
||||
public sealed class HackyAIInfo : IBotInfo, ITraitInfo
|
||||
{
|
||||
public class UnitCategories
|
||||
{
|
||||
@@ -71,6 +71,9 @@ namespace OpenRA.Mods.Common.AI
|
||||
[Desc("Minimum delay (in ticks) between creating squads.")]
|
||||
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.")]
|
||||
public readonly int MinimumExcessPower = 0;
|
||||
|
||||
@@ -258,10 +261,12 @@ namespace OpenRA.Mods.Common.AI
|
||||
public List<Squad> Squads = new List<Squad>();
|
||||
public Player Player { get; private set; }
|
||||
|
||||
readonly Queue<Order> orders = new Queue<Order>();
|
||||
|
||||
readonly Func<Actor, bool> isEnemyUnit;
|
||||
readonly Predicate<Actor> unitCannotBeOrdered;
|
||||
|
||||
BotOrderManager botOrderManager;
|
||||
IBotTick[] tickModules;
|
||||
|
||||
CPos initialBaseCenter;
|
||||
PowerManager playerPower;
|
||||
@@ -317,7 +322,7 @@ namespace OpenRA.Mods.Common.AI
|
||||
IsEnabled = true;
|
||||
playerPower = p.PlayerActor.TraitOrDefault<PowerManager>();
|
||||
playerResource = p.PlayerActor.Trait<PlayerResources>();
|
||||
botOrderManager = p.PlayerActor.Trait<BotOrderManager>();
|
||||
tickModules = p.PlayerActor.TraitsImplementing<IBotTick>().ToArray();
|
||||
|
||||
supportPowerManager = new AISupportPowerManager(this, p);
|
||||
|
||||
@@ -344,10 +349,15 @@ namespace OpenRA.Mods.Common.AI
|
||||
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)
|
||||
{
|
||||
botOrderManager.QueueOrder(order);
|
||||
orders.Enqueue(order);
|
||||
}
|
||||
|
||||
ActorInfo ChooseRandomUnitToBuild(ProductionQueue queue)
|
||||
@@ -535,6 +545,18 @@ namespace OpenRA.Mods.Common.AI
|
||||
|
||||
foreach (var b in builders)
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user