Files
uconsole_asw_game/tests/test_same_seed_same_process.py
T
Claude ed2627efa3 Core stability: same-seed draws, checked saves, DR objective line, dead thresholds
- C16: stateless draws keyed on the entity's saved sensor_seed instead of the
  process-global entity id (MPA MAD/radar, helicopter radar, sub radar alert,
  frigate HF/DF noise, raid, scope phase, sub contact report, boat ESM level),
  so a second game with the same seed in one process runs identically.
- C18: save slots, autosave and the crash recovery autosave check the JSON
  text with the loader's size limit and strict validator before writing; a
  rejected document keeps the previous file, is recorded in the crash log and
  flashes a save error.
- C20: the crewed boat's objective lines (custom reach, breakthrough, strait,
  swimmers, homecoming, pickup, datum) use the dead-reckoned position.
- C22: drop thresholds that never trigger (HEARD_S, SUB_REPORT_CONTACT_S);
  manual states the effective 4 minutes for AI contact reports.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CW6FkwLLc6KvPHvCs5KTRo
2026-10-03 16:57:30 +00:00

97 lines
3.5 KiB
Python

"""Same seed, second game in the same process: identical course.
Entity ids come from process-global class counters that ``reset()`` does not
rewind, so a stateless draw keyed on an entity id differs between two games
with the same seed. Draws are keyed on the entity's saved ``sensor_seed``."""
import re
from pathlib import Path
from src.core.game import Game
ROOT = Path(__file__).resolve().parents[1]
SEED = 77
def _game(scenario="s2_doppeljagd"):
game = Game(seed=SEED, start_menu=False, audio_enabled=False)
game.reset(SEED, scenario)
return game
def _run(game, seconds, dt=0.25):
end = game.sim_t + seconds
while game.sim_t < end - 1e-9 and not game.game_over:
game._update_sim(dt)
def _snap(game):
def r(value):
return round(float(value), 5)
return dict(
ship=(r(game.ship.x), r(game.ship.y), r(game.ship.course), r(game.ship.speed)),
subs=[(s.sensor_seed, r(s.x), r(s.y), r(s.depth), s.state, s.torpedoes_left)
for s in game.subs],
civilians=[(c.sensor_seed, r(c.x), r(c.y), c.sunk) for c in game.civilians],
air=sorted((t.kind, t.source, r(t.bearing), r(t.range_nm or -1.0))
for t in game.air_picture.tracks(game.sim_t)),
sonar=sorted(r(c.bearing) for c in game.sonar.contacts.values()),
score=game.score,
)
def _sweep(game):
for number, ship in enumerate(game.civilians + game.warships):
game.sim_t = 8.0 + 2.0 * number
game._airborne_radar_sweep(2.0, ship.x + 8.0, ship.y, 3000.0, 60.0,
"mpa-radar-", "M-", "RADAR-MPA")
return sorted((round(t.bearing, 6), round(t.range_nm, 6))
for t in game.air_picture.tracks(game.sim_t)
if t.source == "RADAR-MPA")
def test_second_game_with_same_seed_runs_identically():
first = _game()
first.toggle_crew_assist()
first.request_mpa()
_run(first, 120.0)
second = _game()
second.toggle_crew_assist()
second.request_mpa()
_run(second, 120.0)
assert [s.id for s in second.subs] != [s.id for s in first.subs]
assert _snap(first) == _snap(second)
def test_airborne_radar_draws_do_not_depend_on_entity_ids():
first, second = _game(), _game()
assert [c.id for c in second.civilians] != [c.id for c in first.civilians]
tracks = _sweep(first)
assert tracks and tracks == _sweep(second)
def test_boat_draws_do_not_depend_on_entity_ids():
first, second = _game(), _game()
for a, b in zip(first.subs, second.subs):
assert a.id != b.id and a.sensor_seed == b.sensor_seed
a.memory["contact"] = b.memory["contact"] = object()
a.memory["contact_age"] = b.memory["contact_age"] = 0.0
a.depth = b.depth = 10.0
for now in range(0, 7200, 50):
assert (a.contact_report_on_air(SEED, float(now))
== b.contact_report_on_air(SEED, float(now)))
def test_no_stateless_draw_is_keyed_on_an_entity_id():
"""Lint: ``detrand.*(..., x.id, ...)`` would bring the bug back. Sonar
contact ids are numbered per game (``Sonar._next_contact_id``)."""
allowed = {"contact.id"}
pattern = re.compile(r"detrand\.\w+\((?:[^()]|\([^()]*\))*?\b\w+\.id\b", re.S)
offenders = []
for path in sorted((ROOT / "src").rglob("*.py")):
text = path.read_text(encoding="utf-8")
offenders += [f"{path.relative_to(ROOT)}:{text.count(chr(10), 0, m.start()) + 1}"
for m in pattern.finditer(text)
if m.group(0).rsplit(",", 1)[-1].strip() not in allowed]
assert offenders == []