wasname321
Community Newbie-
Posts
4 -
Joined
-
Last visited
About wasname321
Contact Methods
-
Lobby Name
wasdfffas
wasname321's Achievements
Tiro (1/14)
0
Reputation
-
Running a replay with -serializationtest=N segfaults at turn N+1 for me, in every game with Petra AIs I tried. For example skirmishes/acropolis_bay_2p with two Petra AIs and N = 200, on a clean release build of main 47f5fb4f (no local changes). The commits since then don't touch the simulation code. Backtrace, shortened: ``` JS::shadow::Zone::needsIncrementalBarrier (this=0x0) js::gc::PreWriteBarrierImpl JS::Heap<JS::Value>::~Heap ICmpTemplateManager::~ICmpTemplateManager (m_CachedInstance) CCmpTemplateManager::Deallocate CComponentManager::ResetState CComponentManager::~CComponentManager CSimulation2Impl::Update (the secondary simulation is rebuilt every turn) ``` What I think happens: 1. The serialization test destroys the previous turn's secondary CComponentManager every turn. 2. ~CComponentManager first removes its extra GC roots tracer, and ResetState() clears m_TraceCache, so the components' cached JS instances aren't traced any more. 3. ResetState() then deletes the components in reverse order. Deleting the AIManager destroys the AI's script interface, and Context::UnRegisterRealm calls JS::FinishIncrementalGC if an incremental GC is running. MaybeIncrementalGC() at the end of each turn usually leaves one running. 4. That GC sweeps the cached instances of the components that haven't been deleted yet. 5. The next destructor, the TemplateManager's JS::Heap<JS::Value> m_CachedInstance, runs a write barrier on the dead cell. Finishing any incremental GC at the start of ~CComponentManager, before anything stops being traced, fixed it for me: ``` CComponentManager::~CComponentManager() { JSContext* cx = m_ScriptInterface.GetGeneralJSContext(); if (JS::IsIncrementalGCInProgress(cx)) JS::FinishIncrementalGC(cx, JS::GCReason::API); JS_RemoveExtraGCRootsTracer(cx, Trace, (void*)this); ResetState(); } ``` With it, Acropolis Bay ran 100 turns of the serialization test without problems, and a random map game ran 1,000 turns. Something else I noticed while looking at this, though it wasn't the cause: Update assigns a new m_SecondaryTerrain before it replaces the context and component manager that still point to the old terrain. Resetting the component manager first, then the context, then the terrain avoids that.
-
On Survival of the Fittest, if you save the game and load it again, no more enemy waves come. attackerUnitTemplates in maps/random/survivalofthefittest_triggers.js is a script global that only InitSurvival fills (on OnInitGame). Script globals aren't saved and OnInitGame doesn't run on load, so after a load the table is empty and the next wave fails with: ``` ERROR: Error in timer on entity 1, IID104, function DoAction: TypeError: can't access property "heroes", attackerUnitTemplates[civ] is undefined ``` After that no further waves spawn. To reproduce: 1. Start Survival of the Fittest. 2. Save before the first wave and load the save. 3. Wait for the wave: the error above shows up and no attackers come. A rejoin is not affected, because a rejoining client runs InitGame before it receives the state. I checked that with two peers. Fix: reload the templates if the table is empty when a wave starts: ``` Trigger.prototype.StartAnEnemyWave = function() { + if (!Object.keys(attackerUnitTemplates).length) + this.LoadAttackerTemplates(); ``` With this, loaded games stay identical to the running game across the waves I tested. Tested on main 47f5fb4f;
-
On random/wall_demo with Petra AIs, a client that rejoins goes out of sync, because a loaded AI has a base manager that the running AI never created. On Wall Demo neither player starts with a civic centre or a unit in an accessible land region, so Petra's region analysis fails at game start (the "does not know how to interpret this map" case from #3110). gameAnalysis then returns before basesManager.init, so noBase stays undefined. BasesManager.Serialize already handles that (noBase?.Serialize()). But on load, HQ.Deserialize calls this.basesManager.init(gameState, true) unconditionally, which creates noBase, and BasesManager.Deserialize keeps it even though the save has none. So every loaded or rejoined AI has one more base manager than the AI in the running game. AI state is part of the full state hash, so a rejoin goes out of sync. I tested it with two peers: a host playing Wall Demo with two Petra AIs (seed 31), and an observer that rejoined and got the state of turn 600. Both peers wrote OOS dumps at turn 880. With the fix below there was no OOS up to turn 1,099. To reproduce: 1. Host a game on random/wall_demo with two Petra AIs. 2. Let an observer join after turn 500. 3. The joiner goes out of sync a few hundred turns later (turn 880 in my test). Single player is affected too: loading a save gives the AI the extra noBase. In my headless runs every restore from turns 500, 1,000 and 1,500 differed from the running game at the first comparison. Fix: in BasesManager.Deserialize, leave noBase undefined when the save doesn't have one. With that, loads from turns 500, 1,000 and 1,500 matched the running game at every compared turn up to 2,000. I tested it as a small mod; the change is just clearing this.noBase when the saved data has no noBase entry: ``` --- a/binaries/data/mods/public/simulation/ai/petra/basesManager.js +++ b/binaries/data/mods/public/simulation/ai/petra/basesManager.js @@ -796,6 +796,10 @@ this.noBase.init(gameState, BaseManager.STATE_WITH_ANCHOR); this.noBase.Deserialize(gameState, data.noBase); } + else + // init() (called by HQ.Deserialize) created a noBase that the saved game never had: + // the region analysis failed at game start, so basesManager.init was never called. + this.noBase = undefined; this.baseManagers = []; for (const basedata of data.baseManagers) ``` Tested on main 47f5fb4f.
-
On the Flood map, a game with Petra AIs plays differently after a load (or a rejoin) once the water has started rising. Petra builds its accessibility data (the land and water regions) once, in init (simulation/ai/common-api/shared.js), and nothing tells it when the water level changes. So in the running game the AI keeps using the regions from before the flood. An AI that is loaded after the first rise, from a save or on a rejoin, builds them again on the flooded terrain and makes different decisions from the first turn (different subroles and gather orders). Since AIs run on every client, a rejoin after the rise goes out of sync. I tested this with two peers: a host playing Flood with two Petra AIs (seed 21), and an observer joining after turn 1,500. The observer was reported out of sync at turn 1,824. To reproduce without a network game: 1. Run Flood headless with two Petra AIs and a fixed seed. 2. Save at turns 1,200, 1,300 and 1,400 (the first rise is at 260 seconds). 3. Load each save and compare with the game that kept running. The load from turn 1,200 (before the rise) stays identical. The loads from 1,300 and 1,400 differ within 100 turns. I see two ways to fix it: - Save the terrain analysis and regions in the shared script's Serialize() and restore them on load instead of recomputing them. This keeps the current behaviour and makes a loaded game match the running one. I tried this: on Flood, 330 of 330 compared states were equal (125 of 330 without it). - Tell the AIs about water and terrain changes and rebuild the accessibility data when they happen. That would change how the AI plays tho Here is the patch for the first option, against current main: ``` --- a/binaries/data/mods/public/simulation/ai/common-api/shared.js +++ b/binaries/data/mods/public/simulation/ai/common-api/shared.js @@ -32,14 +32,39 @@ /** Return a simple object (using no classes etc) that will be serialized into saved games */ Serialize() { + // loaded but not yet initialized (e.g. serialized again right after a load): write the loaded copy back + if (this.savedTerrain) + return { + "players": this._players, + "templatesModifications": this._templatesModifications, + "entitiesModifications": this._entitiesModifications, + "metadata": this._entityMetadata, + "terrainAnalyzer": this.savedTerrain.terrainAnalyzer, + "accessibility": this.savedTerrain.accessibility + }; return { "players": this._players, "templatesModifications": this._templatesModifications, "entitiesModifications": this._entitiesModifications, - "metadata": this._entityMetadata + "metadata": this._entityMetadata, + // the terrain analysis and regions are built once, at initialization, and kept while the + // game runs; save them so that a load restores them instead of recomputing them from the current terrain + "terrainAnalyzer": SharedScript.plainFields(this.terrainAnalyzer), + // (the accessibility map is the analyzer's map, the same array: saved once and relinked on load) + "accessibility": SharedScript.plainFields(this.accessibility, this.terrainAnalyzer && this.terrainAnalyzer.map) }; } + static plainFields(obj, shared) + { + const out = {}; + if (obj) + for (const key of Object.keys(obj)) + if (key != "sharedScript" && !(shared && obj[key] === shared)) + out[key] = obj[key]; + return out; + } + /** * Called after the constructor when loading a saved game, with 'data' being * whatever Serialize() returned @@ -50,6 +75,7 @@ this._templatesModifications = data.templatesModifications; this._entitiesModifications = data.entitiesModifications; this._entityMetadata = data.metadata; + this.savedTerrain = { "terrainAnalyzer": data.terrainAnalyzer, "accessibility": data.accessibility }; this.isDeserialized = true; } @@ -117,6 +143,13 @@ // create the terrain analyzer this.terrainAnalyzer = new TerrainAnalysis(this, state); this.accessibility = new Accessibility(state, this.terrainAnalyzer); + if (deserialization && this.savedTerrain && this.savedTerrain.accessibility) + { + Object.assign(this.terrainAnalyzer, this.savedTerrain.terrainAnalyzer); + Object.assign(this.accessibility, this.savedTerrain.accessibility); + this.accessibility.map = this.terrainAnalyzer.map; + } + this.savedTerrain = undefined; // Resource types: ignore = not used for resource maps // abundant = abundant resource with small amount each ``` Tested on main 47f5fb4f.
