Jump to content

Flood: Petra plays differently after a load or rejoin once the water has risen


wasname321
 Share

Recommended Posts

Spoiler

 

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.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...