Opened 6 years ago

Closed 6 years ago

#4907 closed defect (fixed)

AIManager must initialize RNG with the AISeed immediately

Reported by: elexis Owned by: elexis
Priority: Must Have Milestone: Alpha 23
Component: AI Keywords:
Cc: Patch: Phab:D1178

Description

As noticed by mimo in Phab:D1159, the AIManager does not initialize the RNG of the AI Manager with the AISeed determined in the gamesetup soon enough.

It executes cmpAIManager.AddPlayer in InitGame.js prior to cmpAIManager.SetRNGSeed, so the Config function in config.js of the bot may not use any randomization functions if it wants to stay in sync.

It's the case since the introduction of the AISeed in r15973.

Change History (5)

comment:1 by elexis, 6 years ago

Moving the line a bit upwards might already fix the issue:

Index: binaries/data/mods/public/simulation/helpers/InitGame.js
===================================================================
--- binaries/data/mods/public/simulation/helpers/InitGame.js	(revision 20670)
+++ binaries/data/mods/public/simulation/helpers/InitGame.js	(working copy)
@@ -44,6 +44,7 @@
 	// Sandbox, Very Easy, Easy, Medium, Hard, Very Hard
 	let rate = [ 0.50, 0.64, 0.80, 1.00, 1.25, 1.56 ];
 	let cmpAIManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_AIManager);
+	cmpAIManager.SetRNGSeed(settings.AISeed || 0);
 	for (let i = 0; i < settings.PlayerData.length; ++i)
 	{
 		let cmpPlayer = QueryPlayerIDInterface(i);
@@ -72,8 +73,6 @@
 	// Map or player data (handicap...) dependent initialisations of components (i.e. garrisoned units)
 	Engine.BroadcastMessage(MT_InitGame, {});
 
-	let seed = settings.AISeed ? settings.AISeed : 0;
-	cmpAIManager.SetRNGSeed(seed);
 	cmpAIManager.TryLoadSharedComponent();
 	cmpAIManager.RunGamestateInit();
 }

It might be better to not give that freedom to break the RNG in JS but init it in C++ first.

However by design the AI is supposed to run in a custom thread and cannot easily access the ComponentManager that stores the initial gamesettings that contain the Seed.

(Also these cmpPlayer calls ought to be in simulation/helper/Player.js, but on the other hand currently need to be called before RunGamestateInit as far as I see. If that is true, the AI doesn't support changing playersettings after the init which might be supported non-AI playersettings after init.)

Last edited 6 years ago by elexis (previous) (diff)

comment:2 by mimo, 6 years ago

The patch is ok, and solves the problem in my tests.

comment:3 by elexis, 6 years ago

Milestone: BacklogAlpha 23

comment:4 by elexis, 6 years ago

Patch: Phab:D1178

A C++ analog to the other Seed seems safer: Phab:D1178

comment:5 by elexis, 6 years ago

Owner: set to elexis
Resolution: fixed
Status: newclosed

In 20700:

Initialize the AIManager AISeed immediately in C++ rather than too late and in JS.

Fixes #4907
Differential Revision: https://code.wildfiregames.com/D1178
Reviewed By: mimo
Refs D1159, rP15973

Note: See TracTickets for help on using tickets.