From 6b9dd9670a3c53ca02d8709b53c261f3f6a78eda Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Wed, 22 Jul 2015 22:23:23 +0100 Subject: [PATCH] Avoid multiple AIs all issuing orders in exactly the same tick. Randomize the initial tick used for each AI so they don't cause lag but all flooding the same tick with orders, but instead (try) and use a different tick for each AI. Order processing is expensive due to the need to sync world state, so it is best to spread out the performance cost of issuing these orders over different ticks. --- OpenRA.Mods.Common/AI/HackyAI.cs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/OpenRA.Mods.Common/AI/HackyAI.cs b/OpenRA.Mods.Common/AI/HackyAI.cs index f0cc8edc88..d58cb04d19 100644 --- a/OpenRA.Mods.Common/AI/HackyAI.cs +++ b/OpenRA.Mods.Common/AI/HackyAI.cs @@ -195,11 +195,25 @@ namespace OpenRA.Mods.Common.AI public Map Map { get { return World.Map; } } IBotInfo IBot.Info { get { return this.Info; } } + int rushTicks; + int assignRolesTicks; + int attackForceTicks; + int minAttackForceDelayTicks; + public HackyAI(HackyAIInfo info, ActorInitializer init) { Info = info; World = init.World; + // Avoid all AIs trying to rush in the same tick, randomize their initial rush a little. + var smallFractionOfRushInterval = Info.RushInterval / 20; + rushTicks = World.SharedRandom.Next(Info.RushInterval - smallFractionOfRushInterval, Info.RushInterval + smallFractionOfRushInterval); + + // Avoid all AIs reevaluating assignments on the same tick, randomize their initial evaluation delay. + assignRolesTicks = World.SharedRandom.Next(0, Info.AssignRolesInterval); + attackForceTicks = World.SharedRandom.Next(0, Info.AttackForceInterval); + minAttackForceDelayTicks = World.SharedRandom.Next(0, Info.MinimumAttackForceDelay); + foreach (var decision in info.PowerDecisions) powerDecisions.Add(decision.OrderName, decision); } @@ -520,11 +534,6 @@ namespace OpenRA.Mods.Common.AI return ret; } - int assignRolesTicks = 0; - int rushTicks = 0; - int attackForceTicks = 0; - int minAttackForceDelayTicks = 0; - void AssignRolesToIdleUnits(Actor self) { CleanSquads();