mirror of
https://github.com/db9979/uconsole_asw_game.git
synced 2026-12-06 12:54:18 +01:00
28 KiB
28 KiB
Coding Agent Guide
Authority and scope
- This is U-Jagd 1.3.124 (
src/core/version.py); current saves are v43-only. Treat these as compatibility contracts, not changelog entries. - Resolve conflicts in this order: executable code and focused tests; packaged JSON/runtime resources;
pyproject.tomland provenance/license notices;README.md; design/history documents underdocs/. A plan or old comment is not an implementation contract. - Preserve explicit compatibility tests and user data unless a task intentionally changes the contract. Add a regression test for behavior changes.
- Older phase/milestone labels under
docs/GDD.md,docs/implementation-plan.md,docs/plan-0.1.6.md, anddocs/plan-0.1.7.mdare historical. Current resumable work is tracked indocs/plan-1.3.mdanddocs/resume.md.
Architecture
main.py: CLI and saved-preference/CLI override wiring.src/core/game.py: composition root only;Gameis assembled from mixins with one responsibility each:game_events.py(event precedence and input ownership),game_sim.py(update/_update_simand the frozenSIM_ORDER),game_radar.py(radar, air, ECM, ESM and radio pictures, missiles and raiders; stages of the sameSIM_ORDER),game_operator.py(station commands shared by keys and Remote Crew),game_pictures.py(OPZ/ELOKA pictures, plot layer, weather, SimLog),game_draw.py(frame, overlays, main loop),mission_bridge.py(scenario start and the custom-mission runtime bridge),game_tasking.py(HQ radio tasks, model intasking.py),game_crew.py(watches, fatigue, morale; model increw.py),game_mpa.py(the on-call patrol aircraft, flight model insrc/air/mpa.py),game_bugreport.py(main-menu bug report page; links and report file inbugreport.py),game_save.py(save/load, slots) withsave_validate.py(pure document validator) andlimits.py(shared bounds);game_shared.pyholds help/display constants. Move code between them verbatim;Gamere-exports the names tests import fromsrc.core.game.src/core/config.py: gameplay constants, units/conversions, scenarios, layout, and timing.src/core/mission*.py,commands.py,station.py,preferences.py,i18n.py: mission models, shared commands, UI state, settings, and localization.src/world/: coastline/bathymetry, real-sector loading, projection, terrain queries, weather/time.src/sonar/andsrc/sensors/: measurements, receiver/TMA, and persistent observation tracks.src/ship/,src/enemies/,src/air/,src/weapons/: simulation entities and mechanics.- Crewed submarine:
src/core/opfor.py(crew orders, sightings, periscope, stadimeter, wires),src/core/boat_radio.py(radio room),src/core/uboot_local.py(uConsole keys of the boat side),src/ui/uboot_view.pyandsrc/ui/uboot_scope.py(boat stations and the periscope page). src/ui/: rendering, hit testing, viewport, tooltips, and editors. Station views live insrc/ui/stations/(one module per station,common.pyshared);src/ui/stations_view.pyis the facade with the station-wide hit test and re-exports. Views should consume game-facing observations, not discover hidden entity state.src/audio/: bounded NumPy synthesis/analysis and non-blocking Pygame playback.src/data/: packaged catalog loading, validation, fingerprints, and secure user-content persistence.data/contains package resources;tests/is the behavioral contract;tools/contains validators/reproducible generators.
Simulation invariants
- World position/range is nautical miles, speed is knots, simulation duration is seconds, depth is metres, frequency is Hz, and headings/bearings are nautical degrees: 0 north, clockwise; movement uses
x += sin(course),y -= cos(course). - At 1x, one real second is one simulation second. Convert knots with
kn / 3600; do not add hidden tactical compression.GAME_TIME_PER_SECadvances the 24-hour clock separately. - The game always runs in real time: there is no time acceleration and no pause. Menus and overlays opened over a mission (help/manual, options, save/load, quit confirmation, nations, F8 analyzer, F9 administration, live traffic) and focus loss own input only and never stop the simulation.
Game.update()subdivides each frame to at mostPHYS_SUBSTEP_Ssubject toPHYS_SUBSTEP_MAX; the main loop (Game._frame_dt) never advances a frame by more thanSIM_FRAME_DT_MAX(0.1 s) but catches up slow frames from a bounded debt (SIM_CATCHUP_MAX_S, 2.5 s), and never after the main menu, load or reset, so sim time (and the sonar audio produced in it) keeps pace with wall-clock playback. Preserve swept collision behavior and the update ordering in_update_sim()(SIM_ORDERinsrc/core/game_sim.py, frozen bytests/test_sim_order.py).tests/test_module_size.pykeeps every source module under 2500 lines. - Only states without a running mission (splash, main menu and its editors, game over) do not advance simulation. Cosmetic/UI timers and audio cadence use wall time where the code says so.
- Same seed, world mode, and input/update sequence must produce the same state. Use explicit local
random.Randomstreams, stable hashes, stable iteration/order, and saved RNG states. Never introduce simulation dependence on global randomness, hash randomization, rendering frequency, audio availability, or wall clock. - Save/load must preserve deterministic continuation, entity ID progression, generated coastline/bathymetry snapshots, pending sensor/projectile state, and operator focus. When adding random draws, consider stream/order compatibility and update determinism and round-trip tests.
- New random draws should use the stateless counter-based
src/core/detrand.py(u01/normalkeyed by seed, tag, entity and tick) so they need no saved stream and never shift existingrandom.Randomsequences. - Physics lives in pure modules (
src/physics/,src/sonar/equation.py/raytrace.py,src/sensors/radar.py/visual.py/hfdf.py,src/weapons/ciws.py) anchored so the 1.0.0 behaviour appears at its reference point.python tools/calibrate.py --checkcompares 77 gameplay metrics withtests/calibration/golden.json(recorded on 1.0.0); intentional deviations go intests/calibration/deviations.jsonwith a reason. Caches in these paths must be pure and bounded (results may never depend on call order).
Input ownership and precedence
- Keep
Game.handle_event()single-owner semantics: splash, editor, pinned-tooltip escape, administrative overlay, menu, numeric entry, then live global/station controls.Alt+Enteris intercepted before contextual confirmation; key-repeatKEYDOWNis ignored. - Only one administrative overlay owns input, clears held/joystick/drag/audio state, and blocks background devices; the simulation keeps running behind it. Focus loss clears controls only.
Escfirst clears a pinned tooltip, then cancels the active owner, then opens quit confirmation. Numeric entry is intentionally live while simulation runs, but held controls are cleared and invalid input remains editable.- Station keys are contextual and must not leak across stations. Clear held controls on station/administration transitions. Trackball axes are station-specific; steering is Bridge-only. Map zoom/pan/follow only operate where a map is visible.
- Transform display coordinates through
_window_to_canvas()and reject letterbox space before hit testing. Keep draw and hit-test geometry aligned.
Observation boundary
- The player has no general ground-truth access. Sonar exposes noisy bearing-only contacts unless a current ping/TMA/fused observation supplies range/depth/course; stale fixes decay or disappear. Radar, AIS, ESM, HFDF, and radio publish
SensorTrackobservations with age/quality. - Workstations, targeting, weapon guidance, tooltips, and logs must use contacts/tracks/datalink data, not live enemy
x,y,depth, class, hostility, or object references. Do not put simulation objects into serialized/public tracks. - Legitimate truth is limited to own ship and commanded own assets, known geography/synthetic chart depth, authored/static preview data, and explicitly modeled friendly datalink truth. Physics, AI, collision, sensor generation, and terminal weapon seekers may use underlying entities internally.
- Manual classification and NATO affiliation are operator annotations, not inferred truth. A sensor domain comes from the observation. Weapons require the modeled track/fix freshness and engagement envelope; pre-terminal torpedo guidance uses the commanded datum, not a hidden target.
Data and localization
data/contacts/*.jsonis the single source of truth for built-in platform/acoustic/torpedo/decoy profile values. Load throughsrc/data/catalog.py; do not duplicate JSON-owned values in Python.tools/gen_contacts.pyis a strict validator despite its historical name; it never generates files.data/editor_templates/*.jsonsupplies editor templates, while Python validators define acceptance and runtime code defines effectiveness. Validation success does not imply runtime support.- All new user-visible prose needs matching keys in
data/i18n/en.jsonandde.json. English is the reference/fallback; catalogs require exact key parity and identical named placeholders. Use named placeholders only. - Route rendering through
Translator,localize,translation_scope, and bounded layout helpers. Do not add directFont.renderprose; the test allowlist is technical labels only. Verify English, German, large text where relevant, and the pseudolocale/layout tests.
Player documentation
src/core/help.pyis the single source for key bindings (STATION_HELP,_GLOBAL_HELP,_WEB_HELP) and per-station standard procedures (STATION_SOP,help.sop.*keys). The F1 overlay, the in-game manual reader (F1 category 4), the Remote Crew/manual-en|depages, anddocs/manual/all render from it.- Manual prose lives in
data/manual/<nn>-<chapter>.{en,de}.md(small Markdown subset parsed bysrc/core/manual.py; every heading needs an explicit{#anchor}; key tables and procedures only via<!-- keys:… -->/<!-- sop:… -->markers). EN and DE must keep identical block structure, anchors, and markers. - Every change to controls, stations, mechanics, or documented numbers updates
help.py, both manual languages, and the catalogs in the same change, then runspython tools/build_manual.py(CI:--check) andpytest tests/test_manual.py. Document only implemented behavior; list gaps under "Not modelled".
Persistence and user content
- Runtime root is
~/.u-jagd/:settings.json;slot1.jsonthroughslot5.json;autosave.json(a v43 save of the running mission everyAUTOSAVE_INTERVAL_Sand on a normal quit, deleted when a mission ends or a new one starts; "Continue" in the main menu loads it);logbook.json(the service record: finished missions, best scores, awards; never part of a save); editor files inmissions/andunits/. Tests replace save paths with temporary directories; never write real user paths from tests..u-jagd/, build products, caches, and egg metadata stay untracked. - Preferences persist language/fullscreen/audio/large text/tooltips in
settings.json; v43 saves also retain the mission's tooltip state and the uConsole's local side. CLI--windowedand--no-audiooverride saved values for one launch; no inverse fullscreen or CLI-language switch exists. - Write and load save v43 only. Require the exact
u-jagd-save-v43schema (exact key sets live insrc/core/save_schema.py) and the complete current catalog, platform, ESM, ASW, AIS (with each report's reported position), RNG and crewed-boat crew state (crewblock: orders with the towed buoy antennabuoy, the attack computer's stadimeter markstdcand each tube's flooding statetubes, wires, plot, ESM picture with each emitter's last main-beam hitlast_peak, radio room with its HQ orders, feed, sonar station; a loaded boat keeps its binding forUBOOT_RESTORE_HOLD_Suntil its crew returns), plus foreign active pings still travelling to the frigate (ping_intercepts) and each submarine's tube flooding and flood transient (ai_tube_left,ai_fire_pending,flood_*) and each conventional boat's diesel, charge rate and air stores (endurance state version 2) and every submarine's ballast, trim and high-pressure air (ballast) and its compartments, leaks, fires, gas and damage-control teams (damage_control), plus the HQ task board (tasking), the frigate's watch bill (watch; the boat's iscrew.watch), the patrol aircraft (mpa) and each buoy'sowner, plus the frigate's variable-depth sonar (vds_state, payout, depth and target), plus the frigate's radar mast blips and the OPZ's marks the AI hunters act on (radar_marks), plus the Bridge's autopilot route (route: waypoints, current index, manual or pattern), plus the frigate's depth charges in the water and own ASROC/depth-charge stores (asw.depth_charges,asw.own_stores), plus the helicopter's radar switch (radar_on), the patrol aircraft's MAD passes (mad_mode) and each submarine's hold on snorkeling after hearing an aircraft radar (radar_hold_s), plus the incidents at sea (incidents: drift nets, weather fronts, merchants without AIS, whales, and emergencies aboard: man overboard, steering failure, snorkel valve, battery gas), plus the Bridge's baffle clearing (baffle_clear; the crewed boat's iscrew.orders.baffle_clear), plus the radio room's own calls to HQ (hq_reports: the call on the air, the next allowed time and the log with each contact report's hidden accuracy), plus the ASW rocket launcher (rbu: rounds in flight and sinking, rockets left, reload), plus the wounded crew of the frigate and each submarine (casualties: light and serious per station, exposure, medical team, re-manning), plus the AI hunters' ESM bearing lines for a cross-fix (hunter_esm), plus the crew assist that lets the AI man every free station (autocrew.assist), plus the knuckles (bubble slicks of hard turns at speed,knuckles) and how long the frigate's flight deck has been quiet (ship.deck_quiet_s), plus the combat swimmers' lock-out so far (swimmer_hold_s). Reject malformed, non-integer, older, newer, incomplete, or unknown schemas without migration. - Writes must stage beside the destination, flush and
fsync, then atomicallyos.replace; failure leaves the prior file intact and save-and-quit quits only after success. Load into a candidate state and commit only after complete validation/restoration; failure must leave the live game and global ID counters unchanged. - Treat imported/editor JSON as hostile. Require finite typed/bounded values, strict schemas,
user.<lowercase/digit/_/->keys, confined destinations, and no symlinked root/file. Never interpret a logical reference as a filesystem path. - Bundle import validates every item and collision before the first visible write, stages all files, rechecks confinement/symlinks, and rolls back the whole commit on failure. Preserve backups if rollback itself fails. JSON output must reject NaN/Infinity.
Remote Crew multiplayer
- Remote Crew is authoritative-host multiplayer for multiple authenticated browser clients. The uConsole process remains the sole simulation authority. Browsers never run simulation, advance time, resolve physics, read live entities, or mutate game state from transport threads.
- Multiplayer protocol versions are independent of application and save versions. Remote Crew is protocol v2-only; every
/api/v1/*route is retired and returns 404. Saves remain exact v43; credentials, clients, station leases, network queues, drafts, and unaccepted commands are transient and never enter saves or settings. - Support a hard-bounded client count. Each client has an independent cryptographic session, expiry, request namespace, and revocation state. The host explicitly grants one exclusive station role per client and can revoke or take over any role. Reconnect never inherits another client's authority.
- Role capabilities are allowlisted. A granted browser may directly operate its station, including weapons when the host separately enables direct fire, but every action must pass the same observation freshness, damage, inventory, ROE, envelope, and readiness checks as local input. Save/load, reset, editors, options, quit, network administration, and credentials remain host-only, with one explicit exception: solo mode (below).
- The transport is
src/commander/server.py(CommanderServer: sessions, leases, publications) withsrc/commander/v2/(wire.pyconstants and framing,commands.pythe closed action registry and validators,routes.pyHTTP and WebSocket handlers);server.pyre-exports the v2 names.src/commander/bridge.py(CommanderBridge: projection pump and command application) applies station actions through the closed handler tables insrc/commander/actions.py, which it re-exports./ws/v2/statepushes the active role's projection (the same bytes asGET /api/v2/state, at most 4 Hz, heartbeat 2 s, latest state only) and the browser falls back to polling; both paths share one allowlist:src/commander/v2/schema.pyis rendered into the generated block ofdata/commander/js/state/schema.jsbytools/gen_web_schema.py(--checkin CI). Transport handlers accept only strict, finite, size-bounded, versioned messages and enqueue detached envelopes. The main thread revalidates client, role, lease generation, world epoch, resource revision, freshness, and readiness immediately before applying each command. Never synthesize Pygame events or dispatch arbitrary method names from network data. - Apply accepted remote commands exactly once in deterministic station order and per-client FIFO order before
Game.update(). Wall clock, packet scheduling, polling frequency, browser rendering, and hash iteration must not affect simulation results. Role loss, disconnect, load/new game, and world replacement reject unsafe queued commands and clear held remote controls; local overlays and focus loss do not. - Each normal role view receives only an allowlisted detached projection of own ship and commanded assets, published observations, annotations, and known geography. Never export simulation objects, hidden IDs, undiscovered positions, true hostile identity, RNG state, or save dumps through station state. The explicit host-granted SimLog capability is the sole diagnostic exception: its read-only history may include detached full-truth snapshots, but never simulation objects, RNG state, credentials, settings, or save dumps.
- Observers are a host-granted, never-persisted read-only pseudo-role (at most
OBSERVER_MAX= 2): no lease, any station viewable throughstations/activatewith generation 0, every command rejected, SimLog included; occupancy andstation_leased()ignore observers. Voice starts enabled in the web-host room and the host switches it off. - Solo mode is a per-launch, never-persisted host decision (
--solo-crewor the F9 "Crew mode" row; changing it revokes every session and rotates the code). One paired session then holds all nine stations and may use the closed host command surface: save/load slots 1-5, new game and the instructor environment, sent as pseudo-rolehostover the normal command pipeline (same CSRF/Origin, exactly-once, phase and world-session checks; rejected in crew mode). There is no pause or time-scale command. Editors, options, quit, network administration and credentials stay host-only, saves stay exact v43, and a world replacement keeps the solo session but re-leases every station and drops queued commands. The browser runs on a desktop PC, so the web UI targets large screens;data/commander/keeps the accessible single-column fallback for narrow windows only. - Phone lookouts (
LOOKOUT_ROLES:lookout,uboot_lookout) are Remote Crew roles outsideSTATIONS: a phone pairs through/lookoutstraight onto one (pair bodyrole), never takes another station, and in solo mode may join beside the solo session (one phone per role). Their calls (lookout_call) are confirmed only against the host's own lookout/periscope sightings (src/core/phone_lookout.py); while a phone holdslookoutthe automatic lookout only feedsgame.lookout_eye. All of this is transient, never saved. For the phone's gyroscope and microphone the F9 listener also serves HTTPS on port+1 with a self-signed certificate made in pure Python (src/commander/tls.py,~/.u-jagd/tls/); the pairing code is typed on the phone and never put in the QR URL. - Browser sessions may use host-only HttpOnly SameSite cookies for reload recovery. Credentials never enter JavaScript, URLs, DOM, logs, settings, or saves. State-changing requests also require exact Origin and a separate CSRF token. Plain HTTP remains trusted-LAN-only; Internet-capable deployment requires a separately reviewed encrypted endpoint or reverse proxy. The local F9 listener accepts only its exact LAN
Host/http://origin unless launched with--public-origin https://…; then it also accepts exactly that proxy host (plus explicit:443) and origin, and marks cookiesSecureonly on the HTTPS path. The--web-hostroom accepts its public origin only. - Keep clients, histories, queues, projections, polling, and render work bounded for the uConsole. Cache immutable projections shared by clients with identical visibility, apply backpressure, and verify maximum-client CPU, memory, bandwidth, frame time, reconnect, and thermal behavior on real hardware.
- New multiplayer prose uses exact EN/DE catalog parity and safe DOM insertion. Browser controls require keyboard and touch access, bounded responsive layouts, explicit stale/revoked states, and tests at desktop, mobile, large zoom, and pseudolocale where applicable.
Coastline provenance
data/coastlines/region.jsonis the hand-maintained stylized legacy map.data/coastlines/real_sectors.json.gzis a generated runtime artifact: exactly 128 distinct, validated 500 NM sectors selected byseed % 128.- Never hand-edit, casually recompress, or replace the generated catalog.
tools/build_coastline_sectors.pyis offline and deterministic (sorted JSON, gzipmtime=0) and accepts only the pinned Natural Earth and Wikidata snapshots with exact SHA-256 values embedded in the tool. - The source snapshots are not runtime assets. Regenerate only from rights-cleared pinned inputs, then verify embedded provenance,
THIRD_PARTY_NOTICES.md, loader/integrity tests, package inclusion, deterministic seed mapping, and geographic disclaimers together. Gameplay roles and synthetic bathymetry are not claims about real states or real seabed. - New third-party assets require source, author, license, modification terms, attribution, and redistribution rights documented before release.
Current intentional limits
- Editor authoring/validation is broader than runtime. Unit-editor profiles take effect only in custom missions that reference them (
src/data/user_profiles.pyadds them to that mission's runtime catalog and save snapshot; a user submarine clones the v2 systems of the built-in boat of its propulsion); Wikipedia-import extras stay descriptive. - A user mission starts only via
F5from the Mission Editor browser and only with a 500 NM world:fixed(the game's current world mode) orreferencenaming a packaged real sector assector:<0..127>(Coastline.generate(sector_index=...)). Forsink, targets must exactly equal all placed hostile submarines;protecttargets are placed friendly/neutral units;reachneedsobjective.reach(x, y, radius_nm). - Runtime-effective mission fields are seed/name/description, player pose/speed, sea state/start time/thermocline/weather (an authored weather kind is held as
world.weather_override, saved), exact units of every kind, built-in or user profile (submarines, surface ships, aircraft at profile speed from the nearest charted airbase, animals, static decoys, hostile enemy torpedoes already running without a launching boat) with placement/course/speed/depth, seeded random groups (MISSION_GROUP_SPEED_KN/_DEPTH_M, a spawn event defers its group), events in time order (message verbatim, spawn, weather, objective; pending ids are the save root fieldmission_events), and objective/time limit. Placed units are remembered asmission_runtime.units(mission id → entity id). Other world sizes, frigate/helicopter torpedoes and missing user profiles are rejected, never silently ignored. Mission world definitions do not replace the packaged coastline dataset. - Audio is optional and synthesized at runtime; no device, disabled audio, or an incompatible shared mixer must degrade to silence without changing simulation. The only external assets are the OFL fonts in
data/commander/fonts/(web) anddata/fonts/(uConsole face, loaded bysrc/ui/layout.pywith a system monospace fallback); there are no external image or audio assets.
uConsole and performance
- The canonical virtual canvas is 1280x720. The frame-rate cap is the
frame_ratepreference (30 or 60 FPS, default 30 to save uConsole CPU);config.FPS= 60 stays the reference maximum. Preserve semantic station layout and bounded text/tooltips at that size. Other windows use aspect-correct letterboxing (FILL_SCREEN=False); do not distort tactical geometry. - The uConsole is a low-power target. Keep render paths free of disk I/O and hidden simulation mutation; cache metadata/surfaces, cull by bounds before point transforms, bound histories/queues/caches, and use NumPy/vectorized processing for signal/waterfall work. Avoid per-frame synthesis and expensive full-world scans.
- Audio updates at 0.25 s cadence. Engine and sonar streams generate short continuous blocks and retain at most the current plus one queued block. Buffered sonar playback (local mixer worker and browser AudioWorklet) is elastic: it steers its queue by resampling at most 2 % faster/slower, conceals underruns with non-periodic granular audio and refills before resuming; never reintroduce plain block repetition. Retuning the listening bearing continues the stream (sequence gap), it does not hard-stop it. Preserve their phase/filter state, independent reserved channels, bounded queues, and an already initialized compatible shared mixer.
- Headless CI is not hardware acceptance. Changes affecting frame time, display scaling, controls, mixer behavior, or readability still need testing on the real 1280x720 uConsole target when available.
Build and verification
Use Python 3.11+ from the repository root:
python -m pip install -e '.[dev]'
pytest
python tools/gen_contacts.py --check
python tools/build_manual.py --check
python tools/gen_web_schema.py --check
python tools/calibrate.py --check
python tools/smoke_full.py
python -m build
pytestconfigures SDL dummy video/audio and isolates saves. Run focused tests while iterating, then the full suite for cross-system changes.python -m buildrequires thebuildextra/package and produces both sdist and wheel. Packaging tests build/install artifacts and verify package resources.- Release notes:
README.md/README.de.mddescribe only the current release (one paragraph each); every release adds its entry at the top ofCHANGELOG.mdandCHANGELOG.de.md(## x.y.z), which also becomes the GitHub release text (tools/changelog_notes.py,--checkintests/test_changelog.py). After visible changes regenerate the README screenshots withpython tools/capture_screenshots.pyandpython tools/capture_commander.py(Chromium onPATH). - For releases, verify
src/core/version.py, save compatibility, README release statement, changelog entries, package metadata, wheel/sdist resources, notices/licenses, full tests, catalog check, smoke test, and a clean install. Do not include caches, local saves/settings, build trees, source snapshots, private references, or unlicensed assets. - Keep commits narrowly scoped; inspect status/diff before staging, never overwrite unrelated work, never commit secrets or local user data, and do not rewrite history or force-push without explicit approval. Generated artifacts must be reproducible and accompanied by their provenance updates.