refactor(climate): ♻️ Improve anomaly handling with sorted keys and enforce proper typing for better performance and type safety

Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
autocommit 2026-04-16 14:09:22 -07:00
parent 6e02f0b174
commit 5b13942db8

View file

@ -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