From 5b13942db821bcf1b22e636ee07755d60fc6ebff Mon Sep 17 00:00:00 2001 From: autocommit Date: Thu, 16 Apr 2026 14:09:22 -0700 Subject: [PATCH] =?UTF-8?q?refactor(climate):=20=E2=99=BB=EF=B8=8F=20Impro?= =?UTF-8?q?ve=20anomaly=20handling=20with=20sorted=20keys=20and=20enforce?= =?UTF-8?q?=20proper=20typing=20for=20better=20performance=20and=20type=20?= =?UTF-8?q?safety?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Lilith Autocommit --- .../modules/climate/atmosphere_anomalies.gd | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/game/engine/src/modules/climate/atmosphere_anomalies.gd b/src/game/engine/src/modules/climate/atmosphere_anomalies.gd index 1ec519c9..5e1f0948 100644 --- a/src/game/engine/src/modules/climate/atmosphere_anomalies.gd +++ b/src/game/engine/src/modules/climate/atmosphere_anomalies.gd @@ -164,7 +164,7 @@ func _drift_anomalies(game_map: RefCounted) -> void: # Collect tiles with anomalies to drift var drifters: Array = [] - for pos: Variant in _anomaly_age.keys(): + for pos: Vector2i in _sorted_anomaly_keys(): var tile: Variant = game_map.get_tile(pos) if tile == null or tile.pressure_anomaly == 0.0: _anomaly_age.erase(pos) @@ -207,8 +207,8 @@ func _decay_and_reinforce_anomalies(game_map: RefCounted) -> void: var reinforce: float = _p("anomaly_reinforce_rate") var max_anomaly: float = _p("anomaly_max") - for pos: Variant in _anomaly_age.keys(): - var tile: Variant = game_map.get_tile(pos) + for pos: Vector2i in _sorted_anomaly_keys(): + var tile: Resource = game_map.get_tile(pos) if tile == null: continue @@ -311,3 +311,18 @@ func _merge_anomalies(game_map: RefCounted) -> void: func _p(key: String) -> float: ## Read a param with fallback to defaults. return float(_params.get(key, _DEFAULTS.get(key, 0.0))) + + +func _sorted_anomaly_keys() -> Array[Vector2i]: + ## Return `_anomaly_age` keys sorted by (x, y) so per-turn iteration + ## is reproducible across processes — Dictionary insertion order drifts + ## as keys are erased/re-added during drift, which silently desyncs + ## pressure state between two otherwise-identical seed runs. + var keys: Array[Vector2i] = [] + for k: Vector2i in _anomaly_age.keys(): + keys.append(k) + keys.sort_custom( + func(a: Vector2i, b: Vector2i) -> bool: + return a.x < b.x if a.x != b.x else a.y < b.y + ) + return keys