Files
uconsole_asw_game/src/core/detrand.py
T
db9979andClaude Opus 5.5 bd9260ba09 Phase 0: save v12, persisted AI timers, strict damage block, simulated AIS
- Bump the save format to v12 (u-jagd-save-v12); every v11 save is rejected
  without migration. Remove the same-version v10/v11 upgrader chain, the
  legacy ESM v1/v2 and two-channel ECM restore paths.
- Declare exact save key sets in src/core/save_schema.py.
- Persist Sub._active_ping_cd and SurfaceShip torpedo-evasion state, which
  were silently reset on load and broke deterministic continuation.
- Validate the damage block exactly (keys, teams, bounded flood/fire).
- Add a simulated own-ship AIS receiver (ITU-R M.1371 class-A cadence,
  VHF line of sight). Radar observations of civilians no longer carry the
  true name and course (observation-boundary leak).
- Add src/core/detrand.py for stateless deterministic draws.
- Add tools/calibrate.py with golden 1.0.0 gameplay metrics and a test that
  keeps later physics phases within tolerance.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 13:16:05 +02:00

60 lines
2.0 KiB
Python

"""Stateless, counter-based deterministic randomness.
New simulation draws use these helpers instead of adding more mutable
``random.Random`` streams: a draw is a pure function of ``(seed, tag, key,
counter)``. Nothing has to be saved, the existing saved streams keep their
sequences, and the result never depends on hash randomization, wall clock,
render rate or evaluation order.
"""
from __future__ import annotations
import math
_MASK = (1 << 64) - 1
def _mix(value: int) -> int:
"""splitmix64 finalizer."""
value = (value + 0x9E3779B97F4A7C15) & _MASK
value = ((value ^ (value >> 30)) * 0xBF58476D1CE4E5B9) & _MASK
value = ((value ^ (value >> 27)) * 0x94D049BB133111EB) & _MASK
return value ^ (value >> 31)
def _tag_value(tag: str) -> int:
# FNV-1a 64 over UTF-8: stable across processes (unlike ``hash``).
value = 0xCBF29CE484222325
for byte in tag.encode("utf-8"):
value = ((value ^ byte) * 0x100000001B3) & _MASK
return value
def bits(seed: int, tag: str, *keys: int) -> int:
"""Return 64 deterministic bits for integer ``keys`` in stream ``tag``."""
value = _mix((int(seed) & _MASK) ^ _tag_value(tag))
for key in keys:
value = _mix(value ^ (int(key) & _MASK))
return value
def u01(seed: int, tag: str, *keys: int) -> float:
"""Uniform float in ``[0, 1)`` with 53 bits of resolution."""
return (bits(seed, tag, *keys) >> 11) * (1.0 / (1 << 53))
def uniform(low: float, high: float, seed: int, tag: str, *keys: int) -> float:
return low + (high - low) * u01(seed, tag, *keys)
def normal(seed: int, tag: str, *keys: int) -> float:
"""Standard normal draw (Box-Muller on two independent sub-draws)."""
u1 = max(u01(seed, tag, *keys, 1), 1e-300)
u2 = u01(seed, tag, *keys, 2)
return math.sqrt(-2.0 * math.log(u1)) * math.cos(2.0 * math.pi * u2)
def phase(seed: int, tag: str, *keys: int) -> float:
"""Deterministic phase in ``[0, 2*pi)``."""
return 2.0 * math.pi * u01(seed, tag, *keys)