From c539b9fcb4ae0489c546ff0b7e12d7ca45e13388 Mon Sep 17 00:00:00 2001 From: Oliver Brakmann Date: Sat, 1 Aug 2015 15:59:12 +0200 Subject: [PATCH] Fix AI using SharedRandom values The AI code runs on only one hosts, so by having the AI use SharedRandom values, the host's random gets out of sync with the other players' and crashes the game. --- OpenRA.Mods.Common/AI/BaseBuilder.cs | 2 +- OpenRA.Mods.Common/AI/HackyAI.cs | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/OpenRA.Mods.Common/AI/BaseBuilder.cs b/OpenRA.Mods.Common/AI/BaseBuilder.cs index 137014e4ae..79f269fe05 100644 --- a/OpenRA.Mods.Common/AI/BaseBuilder.cs +++ b/OpenRA.Mods.Common/AI/BaseBuilder.cs @@ -116,7 +116,7 @@ namespace OpenRA.Mods.Common.AI // Add a random factor so not every AI produces at the same tick early in the game. // Minimum should not be negative as delays in HackyAI could be zero. - var randomFactor = world.SharedRandom.Next(0, ai.Info.StructureProductionRandomBonusDelay); + var randomFactor = ai.Random.Next(0, ai.Info.StructureProductionRandomBonusDelay); waitTicks = active ? ai.Info.StructureProductionActiveDelay + randomFactor : ai.Info.StructureProductionInactiveDelay + randomFactor; } diff --git a/OpenRA.Mods.Common/AI/HackyAI.cs b/OpenRA.Mods.Common/AI/HackyAI.cs index dd1cfeed5f..4a9bac7ccd 100644 --- a/OpenRA.Mods.Common/AI/HackyAI.cs +++ b/OpenRA.Mods.Common/AI/HackyAI.cs @@ -230,15 +230,6 @@ namespace OpenRA.Mods.Common.AI 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); } @@ -265,6 +256,15 @@ namespace OpenRA.Mods.Common.AI Random = new MersenneTwister((int)p.PlayerActor.ActorID); + // Avoid all AIs trying to rush in the same tick, randomize their initial rush a little. + var smallFractionOfRushInterval = Info.RushInterval / 20; + rushTicks = Random.Next(Info.RushInterval - smallFractionOfRushInterval, Info.RushInterval + smallFractionOfRushInterval); + + // Avoid all AIs reevaluating assignments on the same tick, randomize their initial evaluation delay. + assignRolesTicks = Random.Next(0, Info.AssignRolesInterval); + attackForceTicks = Random.Next(0, Info.AttackForceInterval); + minAttackForceDelayTicks = Random.Next(0, Info.MinimumAttackForceDelay); + resourceTypeIndices = new BitArray(World.TileSet.TerrainInfo.Length); // Big enough foreach (var t in Map.Rules.Actors["world"].Traits.WithInterface()) resourceTypeIndices.Set(World.TileSet.GetTerrainIndex(t.TerrainType), true);