Files
uconsole_asw_game/tests/test_runtime_observation_followup.py
T
db9979andClaude Opus 5.5 2f3b0f21b2 Phase 11: radar equation, antenna scan, ESM amplitude, HFDF, lookout
Radar: SNR ~ sigma/R^4 anchored so the nominal range is the Pd = 0.5 point
for a broadside target; Swerling-1 behind a 16-cell CA-CFAR; GIT-type sea
clutter (+3 dB per sea state, CNR ~ R^-3); two-way rain attenuation; aspect
as RCS. The antenna turns in simulation time (phase and pending sweep are
saved) and a contact gets one deterministic look only when the beam swept
it. Missile noise jammers are solved from their burn-through range (JNR ~
R^-2). The CIWS search/track radar holds missiles inside 3 NM. Hostile
platform radars use the same Pd = 0.5 range.

ESM: one-way level vs power class, main beam once per revolution and side
lobes at -25 dB, peak level, measured scan period (drives the LIVE window)
and a range estimate from the best candidate's power class (ELOKA page and
Remote Crew projection).

HFDF: boats pick OWF = 0.85 MUF for their shore path; ground-wave range
falls with frequency, sky wave beyond the skip distance with doubled
bearing error; frequency and mode are published on the observation.

Lookout: Koschmieder contrast, Blackwell threshold vs angular size, optical
horizon, whitecap clutter for sea-level targets and a moon-phase night.

Calibration gains an air-defence section (impact time, CIWS and chaff leak)
recorded on the 1.0.0 tree; all 77 metrics are within tolerance.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 20:57:07 +02:00

422 lines
17 KiB
Python

"""Observation boundaries, swept civilian impacts, and local unit navigation."""
import copy
import random
from types import SimpleNamespace
import pytest
from src.air.asm import ASM
from src.air.flights import Flight
from src.core import config
from src.core.game import Game
from src.data.catalog import CATALOG
from src.enemies.animal import Animal
from src.enemies.decoy import Decoy
from src.enemies.sub import Sub
from src.enemies.surface import SurfaceShip
from src.sonar.sonar import Contact
from src.weapons.torpedo import Torpedo
def water(**overrides):
values = dict(size_nm=500, on_land=lambda x, y: False,
depth_m=lambda x, y: 1000,
thermocline_depth_m=lambda x, y: 100,
land_blocks_line=lambda *args: False)
values.update(overrides)
return SimpleNamespace(**values)
@pytest.fixture
def game(monkeypatch):
game = Game(seed=733, start_menu=False, audio_enabled=False)
game.subs, game.animals, game.civilians, game.warships, game.decoys = [], [], [], [], []
game.asms, game.essms, game.torpedoes = [], [], []
game.flights.flights = []
game.air_picture._tracks.clear()
game.mission.asm_count = 0
for name in ("on_land", "land_blocks_line", "sonar_path_blocked"):
monkeypatch.setattr(game.world, name, lambda *args: False)
monkeypatch.setattr(game.world, "depth_m", lambda *args: 1000)
game.msg = "unchanged"
return game
def observed_target(game, target_id):
contact = Contact(17, target_id, "ping", "sub")
contact.update_ping(90, 5, 50, 1, game.sim_t)
contact.player_class = "U_BOOT"
contact.observed_x, contact.observed_y = game.ship.x + 5, game.ship.y
game.sonar.contacts[target_id] = contact
game.target = contact
return contact
def esm_revolution(game, steps=12):
"""Rotating search radars reach the ESM mast with their main beam only
once per revolution (up to 5 s); sample the picture over one."""
for _ in range(steps):
game.sim_t += .5
game._update_esm_picture()
@pytest.mark.parametrize("kind", ["sub", "civilian", "animal", "decoy", "missing"])
@pytest.mark.parametrize("launcher", ["launch_torpedo", "launch_helo_torpedo"])
def test_launch_authorization_is_observation_equivalent_across_hidden_categories(game, kind, launcher):
rng = random.Random(1)
entity = None
if kind == "sub":
entity = Sub(100, 100, 50, 0, "diesel_alt", rng)
game.subs.append(entity)
elif kind == "civilian":
entity = SurfaceShip(100, 100, rng)
game.civilians.append(entity)
elif kind == "animal":
entity = Animal(100, 100, "whale", rng)
game.animals.append(entity)
elif kind == "decoy":
entity = Decoy(100, 100, 50, rng)
game.decoys.append(entity)
contact = observed_target(game, entity.id if entity else 999999)
game.helo.launch(game.ship)
assert game.torpedo_readiness()[0] == "FEUER FREI"
getattr(game, launcher)()
assert len(game.torpedoes) == 1
assert (game.torpedoes[0].guidance_x, game.torpedoes[0].guidance_y) == (
contact.observed_x, contact.observed_y)
assert not hasattr(game, "_target_ids")
@pytest.mark.parametrize("affiliation", ["FRIEND", "NEUTRAL"])
@pytest.mark.parametrize("launcher", ["launch_torpedo", "launch_helo_torpedo"])
def test_empty_datum_shots_still_respect_protected_annotations(game, affiliation, launcher):
observed_target(game, 999999)
game.opz_affiliations["U-999999"] = affiliation
game.helo.launch(game.ship)
getattr(game, launcher)()
assert not game.torpedoes
@pytest.mark.parametrize("depth,incident", [(5, True), (100, False)])
def test_civilian_incident_uses_swept_path_and_depth(game, depth, incident):
civilian = SurfaceShip(100.5, 100, random.Random(1))
game.civilians = [civilian]
torpedo = Torpedo(100, 100, 90, depth, None, 1, speed_kn=3600,
guidance_x=130, guidance_y=100)
torpedo.depth = depth
game.torpedoes = [torpedo]
game._update_player_torpedoes(1)
assert game.incident is incident
assert civilian.sunk is incident
def test_civilian_depth_must_overlap_at_collision_time_not_frame_end(game):
civilian = SurfaceShip(100.2, 100, random.Random(1))
game.civilians = [civilian]
torpedo = Torpedo(100, 100, 90, 5, None, 1, speed_kn=360,
guidance_x=130, guidance_y=100)
torpedo.depth = 105
game.torpedoes = [torpedo]
game._update_player_torpedoes(10)
assert torpedo.depth < 20
assert not game.incident and not civilian.sunk
def test_civilian_hit_cannot_cross_intervening_terrain(game, monkeypatch):
game.civilians = [SurfaceShip(100.8, 100, random.Random(1))]
monkeypatch.setattr(game.world, "sonar_path_blocked",
lambda x0, y0, d0, x1, y1, d1: min(x0, x1) <= 100.5 <= max(x0, x1))
game.torpedoes = [Torpedo(100, 100, 90, 5, None, 1, speed_kn=3600,
guidance_x=130, guidance_y=100)]
game._update_player_torpedoes(1)
assert not game.incident and not game.civilians[0].sunk
def test_biological_candidate_can_be_acquired_without_revealing_hidden_identity(game):
animal = Animal(100.5, 100, "whale", random.Random(1), depth_m=50)
game.animals = [animal]
torpedo = Torpedo(100, 100, 90, 50, None, 1, speed_kn=3600,
guidance_x=100.5, guidance_y=100)
torpedo.depth = 50
game.torpedoes = [torpedo]
before = len(game.feed.entries)
game._update_player_torpedoes(1)
assert animal.dead and torpedo.target is animal
assert not game.incident
assert game.msg == "unchanged" and len(game.feed.entries) == before
def test_r10_batch5_profiles_add_no_hidden_observation_or_weapon_capabilities():
for key in (*CATALOG.animals, *CATALOG.decoys):
systems = CATALOG.profile_systems[key]
assert systems.sensor_keys == systems.emitter_keys == ()
assert systems.launcher_keys == systems.magazine_keys == \
systems.countermeasure_keys == ()
animal = Animal(100, 100, "whale", random.Random(81), depth_m=50)
decoy = Decoy(100, 100, 50, random.Random(82))
assert not hasattr(animal, "sensor_suite")
assert not hasattr(decoy, "sensor_suite")
def test_decoy_and_salvo_spawn_callbacks_do_not_publish_hidden_notifications(game):
sub = Sub(100, 100, 50, 0, "diesel_alt", random.Random(1))
sub.pending_decoys = [(100, 100)]
game.subs = [sub]
warship = SurfaceShip(100, 100, random.Random(2), hostile=True)
warship.sensor_contact = (120, 100)
warship.sensor_contact_age = 0
warship.pending_asm = [(100, 100, 2)]
game.warships = [warship]
before = len(game.feed.entries), list(game.messages)
game._drain_warship_asm()
assert all(asm.course == 90 for asm in game.asms)
game._update_underwater_entities(.1)
game.mission.asm_count = 1
game.mission_time = config.ASM_SPAWN_FIRST_S
game._maybe_spawn_asm()
assert game.decoys and len(game.asms) == 3
assert game.msg == "unchanged"
assert (len(game.feed.entries), game.messages) == before
def test_air_threat_report_requires_a_modeled_track_and_is_not_repeated(game):
game.radar_on = False
missile = ASM(game.ship.x + 10, game.ship.y, 270, 1, game.rng_asm)
missile.jammer = False
game.asms = [missile]
game._update_air_defense(.1)
assert game.msg == "unchanged" and not game.air_threat_reported
game.air_picture.observe(track_id="M-1", kind="ASM", target_id=1,
source="RADAR-L", bearing=90, range_nm=10,
observer_x=game.ship.x, observer_y=game.ship.y,
course=None, quality=1, now=game.sim_t, label="M1")
game._update_air_defense(.1)
assert game.msg["__u_jagd_i18n__"] == "runtime.threat.air"
count = len(game.feed.entries)
game._update_air_defense(.1)
assert len(game.feed.entries) == count
def test_surface_own_radar_contact_freezes_on_loss_then_expires():
warship = SurfaceShip(100, 100, random.Random(1), hostile=True)
player = SimpleNamespace(x=110, y=100)
warship.emitter = False
warship.attack_left = 0
warship.update(.1, player, water())
assert warship.sensor_contact is None and not warship.pending_asm
warship.emitter = True
warship.update(.1, player, water())
assert warship.sensor_contact == (110, 100) and warship.pending_asm
player.x, player.y = 90, 120
warship.update(1, player, water(land_blocks_line=lambda *args: True))
assert warship.sensor_contact == (110, 100)
warship.emitter = False
warship.pending_asm.clear()
warship.attack_left = 0
warship.update(config.RADAR_TRACK_STALE_S, player, water())
assert warship.sensor_contact is None and not warship.pending_asm
def test_surface_coast_avoidance_wins_over_tactical_orders():
warship = SurfaceShip(100, 100, random.Random(1), hostile=True)
warship.course = warship.target_course = 90
warship.speed = 6
warship.orbit_direction = 1
player = SimpleNamespace(x=90, y=100) # Tactical evasion orders east, into land.
coast = water(on_land=lambda x, y: x >= 100.1)
# Use the endpoint fallback for this deliberately minimal world.
del coast.land_blocks_line
warship.update(1, player, coast)
assert warship.course > 90 and warship.target_course != 90
def patrol():
flight = Flight("military", dict(id="base", x=100, y=100, nation="BOREN"),
rng=random.Random(1))
flight.x, flight.y = 100, 100
return flight
def test_aircraft_emission_doctrine_covers_patrol_and_transit():
base = dict(id="base", x=100, y=100, nation="BOREN")
dest = dict(id="dest", x=200, y=100, nation="BOREN")
military_patrol = Flight("military", base, rng=random.Random(1))
military_transit = Flight("military", base, dest=dest, rng=random.Random(2))
civil_transit = Flight("civil", base, dest=dest, rng=random.Random(3))
assert military_patrol.radar_emitting
assert military_transit.radar_emitting
assert not civil_transit.radar_emitting
civil_transit.esm = True
military_transit.esm = False
assert not civil_transit.radar_emitting
assert military_transit.radar_emitting
def test_aircraft_esm_requires_emission_and_keeps_only_aged_bearing():
flight = patrol()
player = SimpleNamespace(x=110, y=100)
flight.update(.1, player, world=water())
assert flight.sensor_bearing is None
flight.update(.1, player, ship_emitting=True, world=water())
bearing = flight.sensor_bearing
assert bearing is not None
player.x, player.y = 90, 130
flight.update(1, player, ship_emitting=False, world=water())
assert flight.sensor_bearing == bearing
flight.update(config.RADAR_TRACK_STALE_S, player, ship_emitting=True,
world=water(land_blocks_line=lambda *args: True))
assert flight.sensor_bearing is None
def test_aircraft_receiver_capability_does_not_make_it_an_emitter(game):
base = dict(id="base", x=100, y=100, nation="HANSE")
dest = dict(id="dest", x=200, y=100, nation="HANSE")
civil = Flight("civil", base, dest=dest, rng=random.Random(1))
civil.x, civil.y = game.ship.x + 50, game.ship.y
civil.esm = True
game.flights.flights = [civil]
game.radar_on = False
game._update_air_picture(full_scan=True)
game._update_esm_picture()
assert not game.air_picture.tracks(game.sim_t)
assert not game.eloka_tracks()
military = Flight("military", base, dest=dest, rng=random.Random(2))
military.x, military.y = game.ship.x + 50, game.ship.y
military.esm = False
military.esm_range_nm = 1 # Its receiver range is not the player's receiver range.
game.flights.flights = [military]
esm_revolution(game)
assert len(game.eloka_tracks()) == 1
assert not game.air_picture.tracks(game.sim_t)
@pytest.mark.parametrize("state,rate", [("PATROLLE", .5), ("EVADE", 1.5),
("LAUER", .5)])
def test_submarine_safe_command_never_teleports_depth(state, rate):
sub = Sub(100, 100, 70, 0, "diesel_alt", random.Random(1))
sub.state, sub.evac_left, sub.target_depth = state, 100, 1000
sub.rng.random = lambda: 1.0
player = SimpleNamespace(x=400, y=400, noise_level=lambda: 0)
sub.update(.1, player, water(depth_m=lambda x, y: 80))
assert sub.target_depth <= 55
assert sub.depth > 55
assert abs(sub.depth - 70) <= rate * .1 + 1e-9
def test_submarine_depth_change_has_inertia_not_an_instant_full_rate():
"""W2: depth approaches the target with acceleration-limited inertia -
a single short tick moves less than the old instant-full-rate model
would (rate * dt), because depth_rate_mps must first ramp up from 0."""
sub = Sub(100, 100, 50, 0, "diesel_alt", random.Random(1))
sub.state, sub.evac_left, sub.target_depth = "PATROLLE", 100, 200
player = SimpleNamespace(x=400, y=400, noise_level=lambda: 0)
sub.update(.1, player, water(depth_m=lambda x, y: 1000))
assert 0.0 < sub.depth - 50.0 < sub.motion.depth_rate_m_s * .1
assert sub.depth_rate_mps > 0.0
def test_submarine_depth_response_is_dt_agnostic_within_one_physics_substep():
"""Closed-form acceleration integration is exact within a single segment
where the rate does not saturate - matching at any dt up to the game's
own PHYS_SUBSTEP_S/PHYS_SUBSTEP_MAX bound (sub.update() is never called
with a larger single dt in practice, see Game._update_sim's substepping;
the rate-saturation case beyond one segment is intentionally out of
scope, matching how it is actually driven)."""
big = Sub(100, 100, 50, 0, "diesel_alt", random.Random(1))
big.state, big.evac_left, big.target_depth = "PATROLLE", 1e9, 200
small = Sub(100, 100, 50, 0, "diesel_alt", random.Random(1))
small.state, small.evac_left, small.target_depth = "PATROLLE", 1e9, 200
player = SimpleNamespace(x=400, y=400, noise_level=lambda: 0)
world = water(depth_m=lambda x, y: 1000)
big.update(1.0, player, world)
for _ in range(10):
small.update(0.1, player, world)
assert big.depth == pytest.approx(small.depth, abs=1e-6)
assert big.depth_rate_mps == pytest.approx(small.depth_rate_mps, abs=1e-6)
def test_submarine_depth_converges_without_overshoot_and_does_not_cross_shoal():
sub = Sub(100, 100, 50, 90, "diesel_alt", random.Random(1))
sub.state, sub.evac_left = "EVADE", 100
player = SimpleNamespace(x=400, y=400, noise_level=lambda: 0)
sub.update(10, player, water(depth_m=lambda x, y: 80))
assert sub.depth == sub.target_depth == 55
sub.course = 90
before = sub.x
sub.update(60, player, water(depth_m=lambda x, y: 30 if x > before + .001 else 80))
assert sub.x == before
def test_animal_target_depth_respects_seabed_and_rate():
animal = Animal(100, 100, "whale", random.Random(1), depth_m=45)
animal.target_depth = 200
world = water(depth_m=lambda x, y: 50)
animal.update(1, world)
assert animal.target_depth == 49 and animal.depth == 46
animal.update(10, world)
assert animal.depth == 49
def test_decoy_terrain_collision_expires_without_crossing_land():
decoy = Decoy(100, 100, 50, random.Random(1))
decoy.course, decoy.speed = 90, 1
decoy.update(1, water(on_land=lambda x, y: 100.2 <= x <= 100.4))
assert decoy.dead and (decoy.x, decoy.y) == (100, 100)
def test_new_ai_observations_and_alert_state_roundtrip(game):
warship = SurfaceShip(100, 100, random.Random(1), hostile=True)
warship.sensor_contact, warship.sensor_contact_age = (110, 105), 2
game.warships = [warship]
flight = patrol()
flight.base_id = game.world.coast.airbases[0]["id"]
flight.sensor_bearing, flight.sensor_age = 123, 4
game.flights.flights = [flight]
game.air_threat_reported = True
game.load_state(game.save_state())
assert game.warships[0].sensor_contact == (110, 105)
assert game.warships[0].sensor_contact_age == 2
assert game.flights.flights[0].radar_emitting
assert game.flights.flights[0].sensor_bearing == 123
assert game.flights.flights[0].sensor_age == 4
assert game.air_threat_reported
@pytest.mark.parametrize("field,value", [("sensor_contact", [1]),
("sensor_contact", [1, float("nan")]),
("sensor_contact_age", 31)])
def test_invalid_surface_observations_are_rejected_transactionally(game, field, value):
game.warships = [SurfaceShip(100, 100, random.Random(1), hostile=True)]
before = game.save_state()
data = copy.deepcopy(before)
data["warships"][0][field] = value
assert not game._load_save_data(data)
assert game.save_state() == before
@pytest.mark.parametrize("field,value", [("radar_emitting", "yes"),
("sensor_age", -1), ("sensor_age", 31),
("sensor_bearing", 360),
("sensor_bearing", float("inf"))])
def test_invalid_aircraft_observations_are_rejected_transactionally(game, field, value):
flight = patrol()
flight.base_id = game.world.coast.airbases[0]["id"]
game.flights.flights = [flight]
before = game.save_state()
data = copy.deepcopy(before)
data["flights"]["items"][0][field] = value
assert not game._load_save_data(data)
assert game.save_state() == before
def test_invalid_air_threat_notice_state_is_rejected_transactionally(game):
before = game.save_state()
data = copy.deepcopy(before)
data["air_threat_reported"] = 1
assert not game._load_save_data(data)
assert game.save_state() == before