Files
uconsole_asw_game/tests/test_hit_view.py
T
Claude 1daf9cf422 Show hits in a small picture and make a homing torpedo's terminal phase audible
Hit picture: when a side's eye sees a hit (fireball, warhead column, a ship
going down) a small eyepiece window trained on it opens for eight seconds at
every station; a hit only heard shows the bearing with the noise instead
(uConsole and browser, display only, from the side's own observations).

Torpedo terminal phase: a seeker pings slowly while searching and four times
faster once locked on, on its measured bearing; the crew reports the lock with
a rough clock to impact (range guessed by ear) and once when the bearing
stands. Frigate and crewed submarine alike; sound and reports only, the
weapon behaves as before.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HQwtegBu8afg7BWtsCx7tt
2026-10-02 08:02:51 +00:00

71 lines
2.8 KiB
Python

"""The hit picture: a hit in sight or only heard opens a small window."""
import sys
from pathlib import Path
import pygame
from src.core import hit_view
from src.core.hit_view import HitTracker
from src.ui import hit_inset
sys.path.insert(0, str(Path(__file__).parent))
from test_opfor_sub import _crewed # noqa: E402
ROOT = Path(__file__).resolve().parents[1]
def _row(at, kind="blast", size=40.0, bearing=45.0):
return dict(kind=kind, bearing=bearing, range_nm=3.0, at_s=at, dur_s=20.0, size_m=size, level=1.0)
def test_a_hit_in_sight_shows_for_a_few_seconds():
tracker = HitTracker()
assert tracker.update("a", 10.0, [_row(5.0)], []) is None # a new world: only marks
view = tracker.update("a", 21.0, [_row(5.0), _row(20.0)], [])
assert view["mode"] == "sight" and view["bearing"] == 45.0 and view["age_s"] == 1.0
assert tracker.update("a", 20.0 + hit_view.SHOW_S + 0.1, [_row(20.0)], []) is None
def test_depth_charge_columns_are_not_hits():
tracker = HitTracker()
tracker.update("a", 0.0, [], [])
assert tracker.update("a", 1.0, [_row(0.5, "column", 70.0)], []) is None
assert tracker.update("a", 2.0, [_row(1.5, "column", 110.0)], [])["mode"] == "sight"
def test_a_heard_hit_shows_the_bearing_unless_the_eye_has_it():
tracker = HitTracker()
tracker.update("a", 0.0, [], [dict(seq=1, key="contact", bearing=10)])
view = tracker.update("a", 3.0, [], [dict(seq=2, key="breakup", bearing=120)])
assert view == dict(mode="sonar", bearing=120.0, kind="breakup", start=3.0, age_s=0.0)
seen = HitTracker()
seen.update("b", 0.0, [], [])
seen.update("b", 4.0, [_row(3.5)], [])
assert seen.update("b", 4.5, [_row(3.5)], [dict(seq=1, key="hit", bearing=45)])["mode"] == "sight"
def test_the_window_draws_and_reaches_the_browser():
game, _server, _bridge = _crewed(seed=71)
ship = game.ship
assert hit_view.current(game, "frigate") is None
game.sight_events.ship_hit(type("Hulk", (), dict(x=ship.x + 2.0, y=ship.y, sunk=False,
hull_length_m=120.0))(), game.sim_t)
game.sim_t += 0.5
surface = pygame.Surface((1280, 720))
surface.fill((0, 0, 0))
hit_inset.draw(game, surface, "frigate")
assert surface.get_at(hit_inset.RECT.center)[:3] != (0, 0, 0)
from src.commander import projections
shown = projections._hit_view(game, "frigate")
assert shown["mode"] == "sight" and round(shown["bearing"]) == 90
assert shown["sky"] is not None and isinstance(shown["events"], list)
script = (ROOT / "data/commander/js/app/bindings.js").read_text(encoding="utf-8")
assert "syncHitView(state)" in script
def test_the_sonar_picture_draws():
surface = pygame.Surface((320, 170))
hit_inset.draw_sonar(surface, surface.get_rect(), 90.0, 2.0)
assert surface.get_at((160, 2))[:3] != hit_inset.BACK