mirror of
https://github.com/db9979/uconsole_asw_game.git
synced 2026-12-06 04:44:18 +01:00
Keyed labels remembered the index of their candidate spot, but the spots beside a contact turn with its course; a course estimate swinging from east to west put the remembered index on the other side, so the name jumped across its symbol (Windows CI, convoy test). The label now first tries the candidate nearest to where it sat last frame. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KEhGaSszU5DteRsG3wNb2R
459 lines
20 KiB
Python
459 lines
20 KiB
Python
"""Chart labels never cover each other or run off the chart.
|
|
|
|
A crowded picture like the one reported from the weapons station: two
|
|
aircraft with speed vectors right beside the frigate, the assigned target
|
|
on a bearing only, a second contact with a TMA fix, the scale line in the
|
|
corner and the grid numbers along the edges. Every text drawn on the
|
|
frigate's chart (Bridge, Weapons, Helicopter) and on the crewed submarine's
|
|
chart is traced with its glyph box; no two may overlap and none may leave
|
|
the chart, in English and German (frigate also with large text).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import itertools
|
|
import math
|
|
|
|
import pygame
|
|
import pytest
|
|
|
|
from src.core import config
|
|
from src.core.game import Game
|
|
from src.core.preferences import Preferences
|
|
from src.core import uboot_local
|
|
from src.core.station import Station
|
|
from src.sonar.sonar import Contact
|
|
from src.ui import layout, map_view
|
|
|
|
|
|
def _game(language: str, large: bool = False) -> Game:
|
|
prefs = Preferences(language=language, fullscreen=False, audio=False,
|
|
large_text=large, tooltips=False)
|
|
game = Game(seed=1234, start_menu=False, show_splash=False,
|
|
audio_enabled=False, preferences=prefs)
|
|
game.msg_until = 0.0
|
|
return game
|
|
|
|
|
|
def _crowd(game: Game, selected_fix: bool) -> None:
|
|
ship = game.ship
|
|
# Two aircraft passing right over the frigate (one leaving the chart's
|
|
# left edge) and a surface radar track: speed labels near other labels.
|
|
tracks = [
|
|
dict(kind="FLG", track_id="T-A1", target_id=1, source="RADAR",
|
|
x=ship.x - .35, y=ship.y - .25, course=300.0, speed_kn=350.0,
|
|
label="1F05A1", quality=1.0),
|
|
dict(kind="FLG", track_id="T-A2", target_id=2, source="RADAR",
|
|
x=ship.x - .30, y=ship.y - .20, course=300.0, speed_kn=330.0,
|
|
label="E1BF75", quality=1.0),
|
|
dict(kind="FLG", track_id="T-A3", target_id=3, source="RADAR",
|
|
x=ship.x - 1.15, y=ship.y - .55, course=280.0, speed_kn=30.0,
|
|
label="77C0DE", quality=1.0),
|
|
dict(kind="SURFACE", track_id="T-S1", target_id=4, source="RADAR",
|
|
x=ship.x + .2, y=ship.y + .1, course=90.0, speed_kn=14.0,
|
|
label="9A2B44", quality=1.0),
|
|
]
|
|
game.radar_tracks = lambda: [dict(track) for track in tracks]
|
|
target = Contact(2, 900, "passiv", "sub")
|
|
target.passive_bearing = target.bearing = 100.0
|
|
target.observer_x, target.observer_y = ship.x, ship.y
|
|
fixed = Contact(3, 901, "passiv", "sub")
|
|
fixed.passive_bearing = fixed.bearing = 140.0
|
|
fixed.observed_x, fixed.observed_y = ship.x + .3, ship.y + .35
|
|
fixed.range_est, fixed.range_source, fixed.range_sigma_nm = .46, "tma", .1
|
|
fixed.tma_course, fixed.tma_speed = 60.0, 8.0
|
|
fixed.observer_x, fixed.observer_y = ship.x, ship.y
|
|
game.sonar.contacts[target.id] = target
|
|
game.sonar.contacts[fixed.id] = fixed
|
|
game.target = target
|
|
game.selected_contact = fixed if selected_fix else None
|
|
game.map_follow = True
|
|
game.map_view.scale = 160.0
|
|
|
|
|
|
def _chart(game: Game) -> pygame.Rect:
|
|
with layout.bottom_panel_regions(game.bottom_panel_mode()):
|
|
return pygame.Rect(config.MAP_RECT)
|
|
|
|
|
|
def _problems(game: Game) -> list:
|
|
with layout.capture_text() as traced:
|
|
game.draw()
|
|
chart = _chart(game)
|
|
inside = [item for item in traced if item["ink"].colliderect(chart)
|
|
and chart.collidepoint(item["ink"].center)]
|
|
problems = [f"off chart: {item['text']!r} {item['ink']}"
|
|
for item in inside if not chart.contains(item["ink"])]
|
|
for a, b in itertools.combinations(inside, 2):
|
|
if a["ink"].colliderect(b["ink"]):
|
|
problems.append(f"overlap: {a['text']!r} {a['ink']} / "
|
|
f"{b['text']!r} {b['ink']}")
|
|
return problems
|
|
|
|
|
|
@pytest.mark.parametrize("language", ["en", "de"])
|
|
@pytest.mark.parametrize("station", [Station.BRIDGE, Station.WEAPONS,
|
|
Station.HELICOPTER])
|
|
@pytest.mark.parametrize("selected_fix", [False, True])
|
|
@pytest.mark.parametrize("large", [False, True])
|
|
def test_frigate_chart_labels_do_not_overlap(language, station, selected_fix, large):
|
|
game = _game(language, large)
|
|
game.station = station
|
|
game.station_page = 0
|
|
_crowd(game, selected_fix)
|
|
assert _problems(game) == []
|
|
|
|
|
|
@pytest.mark.parametrize("language", ["en", "de"])
|
|
def test_weapons_target_is_labelled_once(language):
|
|
game = _game(language)
|
|
game.station = Station.WEAPONS
|
|
_crowd(game, selected_fix=False)
|
|
with layout.capture_text() as traced:
|
|
game.draw()
|
|
chart = _chart(game)
|
|
labels = [item["text"] for item in traced
|
|
if chart.collidepoint(item["ink"].center) and "K02" in item["text"]]
|
|
assert len(labels) == 1, labels
|
|
assert ("Umschalt+A" if language == "de" else "Shift+A") in labels[0]
|
|
|
|
|
|
def test_scale_line_keeps_grid_numbers_out_of_its_band():
|
|
game = _game("de")
|
|
game.station = Station.BRIDGE
|
|
game.draw()
|
|
chart = _chart(game)
|
|
band = map_view.scale_rect(chart)
|
|
for scale in (2.0, 5.0, 12.0, 40.0, 90.0, 160.0, 400.0):
|
|
game.map_view.scale = scale
|
|
for offset in range(0, 40, 4):
|
|
game.map_view.cy = game.ship.y + offset / scale
|
|
game.map_follow = False
|
|
with layout.capture_text() as traced:
|
|
game.draw()
|
|
numbers = [item for item in traced
|
|
if chart.collidepoint(item["ink"].center)
|
|
and item["text"].replace(".", "").isdigit()]
|
|
assert all(not item["ink"].colliderect(band) for item in numbers), \
|
|
(scale, offset, [(item["text"], item["ink"]) for item in numbers])
|
|
|
|
|
|
@pytest.mark.parametrize("language", ["en", "de"])
|
|
@pytest.mark.parametrize("role", ["uboot", "uboot_nav", "uboot_weapons"])
|
|
@pytest.mark.parametrize("scenario", [None, "s5_durchbruch", "s8_meerenge"])
|
|
def test_submarine_chart_labels_do_not_overlap(language, role, scenario):
|
|
game = _game(language)
|
|
game.local_side = "uboot"
|
|
if scenario is not None:
|
|
assert game.start_new_game(scenario, "fixed", seed=61)
|
|
for _ in range(600):
|
|
game.update(.1)
|
|
game.msg_until = 0.0
|
|
uboot_local.set_local_station(game, role)
|
|
assert _problems(game) == []
|
|
|
|
|
|
@pytest.mark.parametrize("language", ["en", "de"])
|
|
def test_opz_chart_labels_do_not_overlap(language):
|
|
from src.ui.stations import opz
|
|
game = _game(language)
|
|
for _ in range(900):
|
|
game.update(.1)
|
|
game.msg_until = 0.0
|
|
game.station = Station.OPZ
|
|
game.station_page = 0
|
|
with layout.capture_text() as traced:
|
|
game.draw()
|
|
with layout.bottom_panel_regions(game.bottom_panel_mode()):
|
|
chart = pygame.Rect(opz.opz_regions(config.OPZ_STATION_RECT)["chart"])
|
|
inside = [item for item in traced if chart.collidepoint(item["ink"].center)]
|
|
assert inside, "the OPZ chart should carry labels"
|
|
overlaps = [(a["text"], b["text"]) for a, b in itertools.combinations(inside, 2)
|
|
if a["ink"].colliderect(b["ink"])]
|
|
assert overlaps == []
|
|
|
|
|
|
@pytest.mark.parametrize("language", ["en", "de"])
|
|
@pytest.mark.parametrize("theme", ["day", "night"])
|
|
def test_opz_bearing_scale_numbers_stay_at_their_bearing(language, theme):
|
|
"""Reported zoomed far out: "000" left of the ring, "030" outside it,
|
|
"180" by the centre, because the scale numbers stepped aside from the
|
|
"40 NM" ring label like movable labels. Every number now sits at its
|
|
bearing and no ring or scale label covers another at any ring size."""
|
|
import math
|
|
from src.ui.stations import opz, opz_display_view
|
|
prefs = Preferences(language=language, fullscreen=False, audio=False,
|
|
large_text=False, tooltips=False, theme=theme)
|
|
game = Game(seed=1234, start_menu=False, show_splash=False,
|
|
audio_enabled=False, preferences=prefs)
|
|
game.msg_until = 0.0
|
|
game.station = Station.OPZ
|
|
game.station_page = 0
|
|
game.surface_radar_on = True
|
|
game.set_opz_range(40.0)
|
|
game.draw()
|
|
with layout.bottom_panel_regions(game.bottom_panel_mode()):
|
|
chart = pygame.Rect(opz.opz_regions(config.OPZ_STATION_RECT)["chart"])
|
|
for radius in (40, 60, 70, 90, 95, 110, 130, 160, 200):
|
|
game.opz_map_view.scale = radius / 40.0
|
|
with layout.capture_text() as traced:
|
|
game.draw()
|
|
view = opz._opz_view(game, chart)
|
|
cx, cy = view.world_to_screen(game.ship.x, game.ship.y)
|
|
unit = " sm" if language == "de" else " NM"
|
|
scale = [item for item in traced if chart.collidepoint(item["ink"].center)
|
|
and (item["text"].endswith(unit) or (
|
|
len(item["text"]) == 3 and item["text"].isdigit()))]
|
|
numbers = {item["text"]: item for item in scale if not item["text"].endswith(unit)}
|
|
if opz_display_view.compass_numbers_shown(radius):
|
|
assert len(numbers) == 12, (radius, sorted(numbers))
|
|
for text, item in numbers.items():
|
|
bearing = math.radians(int(text))
|
|
dx, dy = item["ink"].centerx - cx, item["ink"].centery - cy
|
|
assert abs(math.hypot(dx, dy) - (radius - 20)) < 8, (radius, text, item["ink"])
|
|
assert abs(dx - math.sin(bearing) * math.hypot(dx, dy)) < 8, (radius, text)
|
|
assert abs(dy + math.cos(bearing) * math.hypot(dx, dy)) < 8, (radius, text)
|
|
assert any(item["text"].startswith("40") for item in scale), radius
|
|
overlaps = [(a["text"], b["text"]) for a, b in itertools.combinations(scale, 2)
|
|
if a["ink"].colliderect(b["ink"])]
|
|
assert overlaps == [], (radius, overlaps)
|
|
|
|
|
|
@pytest.mark.parametrize("language", ["en", "de"])
|
|
@pytest.mark.parametrize("station", [Station.BRIDGE, Station.HELICOPTER])
|
|
@pytest.mark.parametrize("hover", [False, True])
|
|
def test_helicopter_label_and_vector_clear_a_plot_circle(language, station, hover):
|
|
"""Reported: "HSP-5" over a plot circle's "SAR 1 R 0.3 NM", the
|
|
helicopter's vector running through the text."""
|
|
game = _game(language)
|
|
game.station = station
|
|
game.station_page = 0
|
|
game.map_follow = True
|
|
game.map_view.scale = 160.0
|
|
helo = game.helo
|
|
helo.launch(game.ship)
|
|
helo.x, helo.y = game.ship.x + .4, game.ship.y + .3
|
|
helo.course = 90.0
|
|
helo.ground_speed_kn = 0.0 if hover else helo.SPEED_KN
|
|
assert game.plot.add(dict(kind="circle", x=helo.x, y=helo.y, radius_nm=.3,
|
|
label="SAR 1", t=0.0)) == 1
|
|
assert game.plot.add(dict(kind="circle", x=helo.x - .4, y=helo.y - .5, radius_nm=1.5,
|
|
label="SAR 2", t=0.0)) == 2
|
|
assert _problems(game) == []
|
|
if not hover:
|
|
# The vector line itself keeps clear of the labels too.
|
|
from src.ui import label_layout
|
|
with layout.capture_text() as traced:
|
|
game.draw()
|
|
view = game.map_view
|
|
px, py = view.world_to_screen(helo.x, helo.y)
|
|
texts = [item["ink"] for item in traced
|
|
if "SAR 1" in item["text"] or item["text"] == "HSP-5"]
|
|
assert texts
|
|
for step in range(8, 60, 4):
|
|
point = (int(px + step), int(py))
|
|
assert not any(rect.collidepoint(point) for rect in texts), (step, texts)
|
|
|
|
|
|
def _off_chart_circles(game: Game, north_nm: float = 12.0) -> None:
|
|
"""Reported: the SAR areas off the top of a deeply zoomed chart; their
|
|
labels were pinned to the same spot on the edge, one over the other."""
|
|
ship = game.ship
|
|
for index, (dx, dy, radius) in enumerate(((.3, 0.0, .3), (-.2, -.4, 1.5),
|
|
(.1, .3, .8)), start=1):
|
|
assert game.plot.add(dict(kind="circle", x=ship.x + dx, y=ship.y + dy - north_nm,
|
|
radius_nm=radius, label=f"SAR {index}", t=0.0)) == index
|
|
|
|
|
|
@pytest.mark.parametrize("language", ["en", "de"])
|
|
@pytest.mark.parametrize("station", [Station.BRIDGE, Station.WEAPONS, Station.HELICOPTER])
|
|
def test_off_chart_plot_labels_sit_beside_each_other_on_the_edge(language, station):
|
|
game = _game(language)
|
|
game.station = station
|
|
game.station_page = 0
|
|
game.map_follow = True
|
|
game.map_view.scale = 160.0
|
|
_off_chart_circles(game)
|
|
assert _problems(game) == []
|
|
with layout.capture_text() as traced:
|
|
game.draw()
|
|
chart = _chart(game)
|
|
shown = [item["text"] for item in traced
|
|
if "SAR" in item["text"] and chart.contains(item["ink"])]
|
|
assert len(shown) == 3, shown
|
|
|
|
|
|
@pytest.mark.parametrize("language", ["en", "de"])
|
|
def test_off_chart_plot_labels_do_not_overlap_on_the_opz_chart(language):
|
|
from src.ui.stations import opz
|
|
game = _game(language)
|
|
game.station = Station.OPZ
|
|
game.station_page = 0
|
|
_off_chart_circles(game, north_nm=60.0)
|
|
game.opz_range_nm = float(min(config.RADAR_RANGE_SCALES_NM))
|
|
with layout.capture_text() as traced:
|
|
game.draw()
|
|
with layout.bottom_panel_regions(game.bottom_panel_mode()):
|
|
chart = pygame.Rect(opz.opz_regions(config.OPZ_STATION_RECT)["chart"])
|
|
inside = [item for item in traced if chart.collidepoint(item["ink"].center)]
|
|
assert len([item for item in inside if "SAR" in item["text"]]) == 3
|
|
overlaps = [(a["text"], b["text"]) for a, b in itertools.combinations(inside, 2)
|
|
if a["ink"].colliderect(b["ink"])]
|
|
assert overlaps == []
|
|
|
|
|
|
def _rect_overlaps(traced, rect):
|
|
inside = [item for item in traced if rect.collidepoint(item["ink"].center)]
|
|
problems = [f"off chart: {item['text']!r}" for item in inside if not rect.contains(item["ink"])]
|
|
problems += [f"overlap: {a['text']!r} / {b['text']!r}"
|
|
for a, b in itertools.combinations(inside, 2) if a["ink"].colliderect(b["ink"])]
|
|
return inside, problems
|
|
|
|
|
|
@pytest.mark.parametrize("language", ["en", "de"])
|
|
@pytest.mark.parametrize("crowded", [False, True])
|
|
def test_radio_cross_fix_chart_degree_labels_do_not_overlap(language, crowded):
|
|
"""The cross-fix chart's graticule numbers, position line, scale and fix
|
|
labels keep clear of each other (uConsole size, empty and with fixes)."""
|
|
from src.ui.stations.radio_chart import draw_hfdf_chart
|
|
game = _game(language)
|
|
game.msg_until = 0.0
|
|
if crowded:
|
|
ship = game.ship
|
|
game.hfdf_log = [dict(track_id="H-1", label="H-ABC123", bearing=40.0 + 3 * index,
|
|
observer_x=ship.x - 2.0 * index, observer_y=ship.y + index,
|
|
t=game.sim_t) for index in range(3)]
|
|
game.hfdf_fixes = {"H-1": dict(label="H-ABC123", x=ship.x + 6.0, y=ship.y - 8.0,
|
|
sigma_nm=1.2, covariance_nm2=None, t=game.sim_t)}
|
|
rect = pygame.Rect(306, 135, 640, 520)
|
|
layout.configure_for(game)
|
|
with layout.capture_text() as traced:
|
|
draw_hfdf_chart(game.screen, game, rect)
|
|
inside, problems = _rect_overlaps(traced, rect)
|
|
assert any(text["text"].endswith(("'N", "°N")) for text in inside)
|
|
assert problems == []
|
|
|
|
|
|
@pytest.mark.parametrize("language", ["en", "de"])
|
|
def test_boat_pilot_chart_degree_labels_do_not_overlap(language):
|
|
from src.ui.uboot_pilot import draw_pilot_chart
|
|
game = _game(language)
|
|
game.local_side = "uboot"
|
|
game.update(.1)
|
|
game.msg_until = 0.0
|
|
boat = uboot_local.boat(game)
|
|
rect = pygame.Rect(664, 282, 592, 192)
|
|
layout.configure_for(game)
|
|
with layout.capture_text() as traced:
|
|
draw_pilot_chart(game.screen, game, boat, rect)
|
|
inside, problems = _rect_overlaps(traced, rect)
|
|
assert any(text["text"].endswith(("'E", "°E")) for text in inside)
|
|
assert problems == []
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def convoy_box():
|
|
"""The convoy of the bug report 2026-10-06: four ships in a box around
|
|
the frigate, all on her course, the chart zoomed to about 6 nm."""
|
|
game = _game("de")
|
|
assert game.start_new_game("s7_geleitzug", "real_fixed", seed=2)
|
|
game.surface_radar_on = True
|
|
for _ in range(300):
|
|
game.update(.1)
|
|
return game
|
|
|
|
|
|
def _draw_with_vectors(game, monkeypatch):
|
|
from src.ui import nato_symbols
|
|
segments = []
|
|
original = nato_symbols.draw_motion_vector
|
|
|
|
def record(surface, center, *args, **kwargs):
|
|
end = original(surface, center, *args, **kwargs)
|
|
if end is not None:
|
|
segments.append(((center[0], center[1]), end))
|
|
return end
|
|
|
|
monkeypatch.setattr(nato_symbols, "draw_motion_vector", record)
|
|
with layout.capture_text() as traced:
|
|
game.draw()
|
|
return traced, segments
|
|
|
|
|
|
@pytest.mark.parametrize("language", ["en", "de"])
|
|
@pytest.mark.parametrize("station", [Station.BRIDGE, Station.WEAPONS, Station.OPZ])
|
|
def test_track_names_never_lie_across_a_motion_vector(convoy_box, language, station,
|
|
monkeypatch):
|
|
"""Bug report 2026-10-06: the course line of every ship ran right
|
|
through its name. Names sit abeam of the course, clear of every vector."""
|
|
from src.core.i18n import Translator
|
|
game = convoy_box
|
|
game.translator = Translator(language)
|
|
game.tr = game.translator.t
|
|
game.station, game.station_page = station, 0
|
|
game.map_view.scale = 117.0
|
|
game.opz_map_view.scale = 90.0
|
|
game.opz_map_view.cx, game.opz_map_view.cy = game.ship.x, game.ship.y
|
|
game.msg_until = 0.0
|
|
traced, segments = _draw_with_vectors(game, monkeypatch)
|
|
names = [str(track["label"]) for track in game.chart_tracks()]
|
|
labels = [item for item in traced
|
|
if any(item["text"].startswith(name) for name in names)]
|
|
assert len(labels) >= 4, [item["text"] for item in traced]
|
|
assert len(segments) >= 4
|
|
for item in labels:
|
|
for start, end in segments:
|
|
assert not item["ink"].clipline(start, end), (item["text"], item["ink"], start, end)
|
|
|
|
|
|
def test_track_names_stay_put_from_frame_to_frame():
|
|
"""Bug report 2026-10-06: names and speeds jumped about between frames.
|
|
Over a minute of the convoy no label leaves its ship's side."""
|
|
game = _game("de")
|
|
assert game.start_new_game("s7_geleitzug", "real_fixed", seed=2)
|
|
game.surface_radar_on = True
|
|
game.station = Station.WEAPONS
|
|
previous, moves, seen = {}, [], 0
|
|
for _ in range(60):
|
|
for _ in range(10):
|
|
game.update(.1)
|
|
game.map_view.scale = 117.0
|
|
game.msg_until = 0.0
|
|
with layout.capture_text() as traced:
|
|
game.draw()
|
|
tracks = {str(track["label"]): track for track in game.chart_tracks()
|
|
if track["x"] is not None}
|
|
current = {}
|
|
for item in traced:
|
|
name = item["text"].split(" ")[0]
|
|
if name in tracks:
|
|
px, py = game.map_view.world_to_screen(tracks[name]["x"], tracks[name]["y"])
|
|
current[name] = (item["ink"].centerx - px, item["ink"].centery - py)
|
|
for name, offset in current.items():
|
|
if name in previous:
|
|
seen += 1
|
|
if math.hypot(offset[0] - previous[name][0],
|
|
offset[1] - previous[name][1]) > 12:
|
|
moves.append((name, previous[name], offset))
|
|
previous = current
|
|
assert seen > 100
|
|
assert moves == []
|
|
|
|
|
|
def test_a_label_keeps_its_side_when_the_course_swings_round():
|
|
"""Windows CI 2026-10-06: a convoy name jumped from above its ship to
|
|
below it. The candidates beside a contact turn with its course, so a
|
|
course estimate swinging from east to west turned the remembered
|
|
candidate onto the other side. The label keeps its spot instead."""
|
|
from src.ui import label_layout
|
|
bounds = pygame.Rect(0, 0, 400, 300)
|
|
size = (80, 12)
|
|
key = ("test", "swing")
|
|
label_layout._MEMORY.pop(key, None)
|
|
with label_layout.label_scope(bounds) as field:
|
|
first = field.place(size, label_layout.beside((200, 150), size, 90.0), key)
|
|
with label_layout.label_scope(bounds) as field:
|
|
second = field.place(size, label_layout.beside((201, 150), size, 270.0), key)
|
|
assert abs(second.centery - first.centery) <= 2
|
|
assert abs(second.centerx - first.centerx) <= 2
|