mirror of
https://github.com/db9979/uconsole_asw_game.git
synced 2026-12-05 12:24:25 +01:00
The ships of Tiefenkontakt (the browser submarine game, Three.js) come over as polygon meshes for our painter: tools/tiefenkontakt/export_models.mts writes the opaque triangles of 22 designs from a Tiefenkontakt checkout, tools/build_tk_models.py snaps, decimates the smooth submarine hulls, joins coplanar faces of one colour, shares a 40-colour palette and keeps the navigation lamps (data/unit_models/tiefenkontakt.json.gz, commit embedded). src/ui/tk_models.py maps catalog types to designs: corvettes, frigates and destroyers up to 160 m, container ships, tankers, bulk carriers, reefers, RoRo/car carriers, general cargo, heavy lift, ferries, tugs, trawlers and all submarines (79 of 111 types); carriers, cruisers, big destroyers, cruise ships, auxiliaries and aircraft keep their own models. Each is stretched to its type's beam and draught; light positions come from the design. The browser loads one module per design (views/tk/<name>.js) and stretches it the same way (parity test in Chromium). uConsole: a detailed partition is built in a background thread on first sight while the flat silhouette shows (model_bsp.tree_if_ready), and eyepiece sprites grow in ~4 % steps (scene_length). By day the models show their paint (scene_vivid), fading to the steel outline at night and in haze, on both renderers. Docs: manual (bridge, editors), changelog, README, notices, AGENTS; screenshots and manual figures regenerated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUgHR8PXDGer54DH6ie
332 lines
16 KiB
Python
332 lines
16 KiB
Python
#!/usr/bin/env python3
|
|
"""Render the browser schema's allowlists from the Python source of truth.
|
|
|
|
It also writes the colour themes of ``src/ui/theme.py`` (``css_tokens()``)
|
|
into the generated block of ``data/commander/css/tokens.css``.
|
|
|
|
``data/commander/js/state/schema.js`` keeps its hand-written validation
|
|
logic; the role shapes and row field arrays between the ``GENERATED``
|
|
markers are rendered from ``src/commander/v2/schema.py``. ``--check``
|
|
(CI) fails when the file differs from a fresh render; without it the block
|
|
is rewritten in place. Output is deterministic (sorted roles, fixed
|
|
formatting, LF line endings).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import os
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
os.environ.setdefault("PYGAME_HIDE_SUPPORT_PROMPT", "1")
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
from src.commander.v2 import schema # noqa: E402
|
|
|
|
SCHEMA_JS = ROOT / "data" / "commander" / "js" / "state" / "schema.js"
|
|
PROFILES_JS = ROOT / "data" / "commander" / "js" / "views" / "silhouette-profiles.js"
|
|
MODELS_JS = ROOT / "data" / "commander" / "js" / "views" / "unit-models.js"
|
|
TOKENS_CSS = ROOT / "data" / "commander" / "css" / "tokens.css"
|
|
VARIANT_GROUPS = ("naval", "civil", "subs")
|
|
CSS_BEGIN = "/* BEGIN GENERATED THEME TOKENS (tools/gen_web_schema.py; do not edit by hand) */\n"
|
|
CSS_END = "/* END GENERATED THEME TOKENS */\n"
|
|
|
|
|
|
def variants_js(group: str):
|
|
return ROOT / "data" / "commander" / "js" / "views" / f"unit-variants-{group}.js"
|
|
|
|
|
|
def tk_js(name: str):
|
|
return ROOT / "data" / "commander" / "js" / "views" / "tk" / f"{name}.js"
|
|
BEGIN = " // BEGIN GENERATED (tools/gen_web_schema.py; do not edit by hand)\n"
|
|
END = " // END GENERATED\n"
|
|
|
|
|
|
def _array(values) -> str:
|
|
return "[" + ", ".join(json.dumps(value) for value in values) + "]"
|
|
|
|
|
|
def render_block() -> str:
|
|
lines = [BEGIN, " const shapes = {\n"]
|
|
for role in sorted(schema.ROLE_SHAPES):
|
|
lines.append(f" {role}: {_array(schema.ROLE_SHAPES[role])},\n")
|
|
lines.append(" };\n")
|
|
lines.append(f" const tacticalFields = {_array(schema.TACTICAL_FIELDS)};\n")
|
|
lines.append(f" const sonarFields = {_array(schema.SONAR_FIELDS)};\n")
|
|
lines.append(f" const radioFields = {_array(schema.RADIO_FIELDS)};\n")
|
|
lines.append(" const radioTaskFields = {\n")
|
|
lines.append(f" row: {_array(schema.RADIO_TASK_FIELDS)},\n")
|
|
lines.append(f" kinds: {_array(schema.RADIO_TASK_KINDS)},\n")
|
|
lines.append(f" states: {_array(schema.RADIO_TASK_STATES)},\n")
|
|
lines.append(" };\n")
|
|
lines.append(" const radioReportFields = {\n")
|
|
lines.append(f" row: {_array(schema.RADIO_REPORT_FIELDS)},\n")
|
|
lines.append(f" states: {_array(schema.RADIO_REPORT_STATES)},\n")
|
|
lines.append(f" reasons: {_array(schema.RADIO_REPORT_REASONS)},\n")
|
|
lines.append(" };\n")
|
|
lines.append(" const crewFields = {\n")
|
|
lines.append(f" row: {_array(schema.CREW_FIELDS)},\n")
|
|
lines.append(f" watch: {_array(schema.CREW_WATCH_FIELDS)},\n")
|
|
lines.append(f" casualties: {_array(schema.CASUALTY_FIELDS)},\n")
|
|
lines.append(f" casualtyStation: {_array(schema.CASUALTY_STATION_FIELDS)},\n")
|
|
lines.append(f" stations: {_array(schema.CASUALTY_STATIONS)},\n")
|
|
lines.append(" };\n")
|
|
lines.append(" const mpaFields = {\n")
|
|
lines.append(f" row: {_array(schema.MPA_FIELDS)},\n")
|
|
lines.append(f" states: {_array(schema.MPA_STATES)},\n")
|
|
lines.append(" };\n")
|
|
lines.append(" const consortFields = {\n")
|
|
lines.append(f" row: {_array(schema.CONSORT_FIELDS)},\n")
|
|
lines.append(f" bearing: {_array(schema.CONSORT_BEARING_FIELDS)},\n")
|
|
lines.append(f" modes: {_array(schema.CONSORT_MODES)},\n")
|
|
lines.append(f" stations: {_array(schema.CONSORT_STATIONS)},\n")
|
|
lines.append(" };\n")
|
|
lines.append(" const opzSuggestionFields = "
|
|
f"{_array(schema.OPZ_SUGGESTION_FIELDS)};\n")
|
|
lines.append(" const opzTrailFields = {\n")
|
|
lines.append(f" row: {_array(schema.OPZ_TRAIL_FIELDS)},\n")
|
|
lines.append(f" max: {schema.OPZ_TRAIL_MAX},\n")
|
|
lines.append(f" points: {schema.OPZ_TRAIL_POINTS},\n")
|
|
lines.append(" };\n")
|
|
lines.append(" const helicopterTacticalFields = "
|
|
f"{_array(schema.HELICOPTER_TACTICAL_FIELDS)};\n")
|
|
lines.append(" const weatherFields = {\n")
|
|
lines.append(f" atmosphere: {_array(schema.WEATHER_ATMOSPHERE_FIELDS)},\n")
|
|
lines.append(f" boatAtmosphere: {_array(schema.WEATHER_BOAT_ATMOSPHERE_FIELDS)},\n")
|
|
lines.append(f" boat: {_array(schema.WEATHER_BOAT_FIELDS)},\n")
|
|
lines.append(" };\n")
|
|
lines.append(" const sightFields = {\n")
|
|
lines.append(f" sky: {_array(schema.SKY_FIELDS)},\n")
|
|
lines.append(f" glasses: {_array(schema.LOOKOUT_GLASSES_FIELDS)},\n")
|
|
lines.append(f" outline: {_array(schema.LOOKOUT_OUTLINE_FIELDS)},\n")
|
|
lines.append(f" classes: {_array(schema.SIGHT_CLASSES)},\n")
|
|
lines.append(f" event: {_array(schema.SIGHT_EVENT_FIELDS)},\n")
|
|
lines.append(f" eventKinds: {_array(schema.SIGHT_EVENT_KINDS)},\n")
|
|
lines.append(f" phone: {_array(schema.LOOKOUT_PHONE_FIELDS)},\n")
|
|
lines.append(f" phoneOutline: {_array(schema.LOOKOUT_PHONE_OUTLINE_FIELDS)},\n")
|
|
lines.append(f" call: {_array(schema.LOOKOUT_CALL_FIELDS)},\n")
|
|
lines.append(f" callCategories: {_array(schema.LOOKOUT_CALL_CATEGORIES)},\n")
|
|
lines.append(" };\n")
|
|
lines.append(" const routeFields = {\n")
|
|
lines.append(f" row: {_array(schema.BRIDGE_ROUTE_FIELDS)},\n")
|
|
lines.append(f" point: {_array(schema.BRIDGE_ROUTE_POINT_FIELDS)},\n")
|
|
lines.append(f" patterns: {_array(schema.BRIDGE_ROUTE_PATTERNS)},\n")
|
|
lines.append(" };\n")
|
|
lines.append(" const boatFields = {\n")
|
|
lines.append(f" plant: {_array(schema.UBOOT_PLANT_FIELDS)},\n")
|
|
lines.append(f" air: {_array(schema.UBOOT_AIR_FIELDS)},\n")
|
|
lines.append(f" ballast: {_array(schema.UBOOT_BALLAST_FIELDS)},\n")
|
|
lines.append(f" ballastFlags: {_array(schema.UBOOT_BALLAST_FLAGS)},\n")
|
|
lines.append(f" damage: {_array(schema.UBOOT_DAMAGE_FIELDS)},\n")
|
|
lines.append(f" compartment: {_array(schema.UBOOT_COMPARTMENT_FIELDS)},\n")
|
|
lines.append(f" dcTeam: {_array(schema.UBOOT_DC_TEAM_FIELDS)},\n")
|
|
lines.append(f" compartments: {_array(schema.UBOOT_COMPARTMENTS)},\n")
|
|
lines.append(f" dcTasks: {_array(schema.UBOOT_DC_TASKS)},\n")
|
|
lines.append(f" esm: {_array(schema.UBOOT_ESM_FIELDS)},\n")
|
|
lines.append(f" esmEmitter: {_array(schema.UBOOT_ESM_EMITTER_FIELDS)},\n")
|
|
lines.append(f" esmHistory: {_array(schema.UBOOT_ESM_HISTORY_FIELDS)},\n")
|
|
lines.append(f" esmFix: {_array(schema.UBOOT_ESM_FIX_FIELDS)},\n")
|
|
lines.append(f" esmCandidate: {_array(schema.UBOOT_ESM_CANDIDATE_FIELDS)},\n")
|
|
lines.append(f" threat: {_array(schema.UBOOT_THREAT_FIELDS)},\n")
|
|
lines.append(f" intercept: {_array(schema.UBOOT_INTERCEPT_FIELDS)},\n")
|
|
lines.append(f" interceptKinds: {_array(schema.UBOOT_INTERCEPT_KINDS)},\n")
|
|
lines.append(f" advice: {_array(schema.UBOOT_THREAT_ADVICE)},\n")
|
|
lines.append(f" evadePlan: {_array(schema.UBOOT_EVADE_PLAN_FIELDS)},\n")
|
|
lines.append(f" radio: {_array(schema.UBOOT_RADIO_FIELDS)},\n")
|
|
lines.append(f" radioLog: {_array(schema.UBOOT_RADIO_LOG_FIELDS)},\n")
|
|
lines.append(f" radioLogKinds: {_array(schema.UBOOT_RADIO_LOG_KINDS)},\n")
|
|
lines.append(f" radioBuoyStates: {_array(schema.UBOOT_RADIO_BUOY_STATES)},\n")
|
|
lines.append(f" radioReport: {_array(schema.UBOOT_RADIO_REPORT_FIELDS)},\n")
|
|
lines.append(f" radioOrder: {_array(schema.UBOOT_RADIO_ORDER_FIELDS)},\n")
|
|
lines.append(f" radioOrderKinds: {_array(schema.UBOOT_RADIO_ORDER_KINDS)},\n")
|
|
lines.append(" };\n")
|
|
lines.append(END)
|
|
return "".join(lines)
|
|
|
|
|
|
def _rounded(value):
|
|
if isinstance(value, float):
|
|
return round(value, 6)
|
|
if isinstance(value, (list, tuple)):
|
|
return [_rounded(item) for item in value]
|
|
if isinstance(value, dict):
|
|
return {key: _rounded(item) for key, item in sorted(value.items())}
|
|
return value
|
|
|
|
|
|
def render_profiles() -> str:
|
|
"""The silhouettes of ``src/ui/silhouettes.py`` as a browser module, so
|
|
the browser eyepieces draw exactly the uConsole's shapes."""
|
|
from src.ui import silhouettes
|
|
body = json.dumps(_rounded(silhouettes.PROFILES), separators=(",", ":"), sort_keys=True)
|
|
return ("// GENERATED by tools/gen_web_schema.py from src/ui/silhouettes.py;"
|
|
" do not edit by hand.\n"
|
|
f"export const PROFILES = {body};\n"
|
|
f"export const DETAIL_MIN_PX = {silhouettes.DETAIL_MIN_PX};\n"
|
|
f"export const FOAM = {_array(silhouettes.FOAM)};\n"
|
|
"export const NAV_LIGHT = {%s};\n" % ", ".join(
|
|
f"{name}: {_array(color)}" for name, color in sorted(silhouettes.NAV_LIGHT.items())))
|
|
|
|
|
|
def render_models() -> str:
|
|
"""The analyzer's 3D models of ``src/ui/unit_models.py`` as a browser
|
|
module (meshes, materials and each catalog profile's model class)."""
|
|
from src.data.catalog import CATALOG
|
|
from src.ui import unit_models as um
|
|
meshes = {cls: um.mesh_for(cls).to_json() for cls in um.MODEL_CLASSES}
|
|
classes = um.catalog_model_classes(CATALOG)
|
|
light = [round(float(c), 6) for c in um._LIGHT]
|
|
from src.ui import unit_variants as uv
|
|
groups = {key: uv.group_of(key) for key in uv.specs()}
|
|
return ("// GENERATED by tools/gen_web_schema.py from src/ui/unit_models.py;"
|
|
" do not edit by hand.\n"
|
|
f"export const MATERIALS = {_array([list(c) for c in um.MATERIALS.values()])};\n"
|
|
f"export const WATER = {_array(um.WATER)};\n"
|
|
f"export const WATER_RING = {um.WATER_RING};\n"
|
|
f"export const ELEVATION_RAD = {round(um.ELEVATION_RAD, 6)};\n"
|
|
f"export const TURN_RAD_S = {um.TURN_RAD_S};\n"
|
|
f"export const LIGHT = {_array(light)};\n"
|
|
f"export const SCENE_ELEVATION_RAD = {round(um.SCENE_ELEVATION_RAD, 6)};\n"
|
|
f"export const SCENE_MIN_PX = {um.SCENE_MIN_PX};\n"
|
|
f"export const SCENE_CACHE_SIZE = {um.SCENE_CACHE_SIZE};\n"
|
|
f"export const SCENE_CLASSES = {json.dumps(list(um.SCENE_CLASSES))};\n"
|
|
f"export const AIRCRAFT_LIFT = {um.AIRCRAFT_LIFT};\n"
|
|
"export const CLASSES = "
|
|
f"{json.dumps(classes, separators=(',', ':'), sort_keys=True)};\n"
|
|
"export const VARIANT_GROUPS = "
|
|
f"{json.dumps(groups, separators=(',', ':'), sort_keys=True)};\n"
|
|
"export const MODELS = "
|
|
f"{json.dumps(meshes, separators=(',', ':'), sort_keys=True)};\n")
|
|
|
|
|
|
def render_variants(group: str) -> str:
|
|
"""The per-type models of one browser group (loaded only when needed)
|
|
with the navigation light positions of the ship variants."""
|
|
from src.ui import unit_variants as uv
|
|
keys = [key for key in uv.specs() if uv.group_of(key) == group]
|
|
# A type drawn as a Tiefenkontakt design names the design and its
|
|
# stretch; the design's mesh is its own module (views/tk/<name>.js).
|
|
meshes = {key: uv.tk_ref(key) or uv.variant_mesh(key).to_json() for key in keys}
|
|
nav = {key: uv.ship_nav(key) for key in keys if uv.ship_nav(key)}
|
|
return ("// GENERATED by tools/gen_web_schema.py from src/ui/unit_variants.py;"
|
|
" do not edit by hand.\n"
|
|
"export const VARIANTS = "
|
|
f"{json.dumps(meshes, separators=(',', ':'), sort_keys=True)};\n"
|
|
"export const NAV = "
|
|
f"{json.dumps(nav, separators=(',', ':'), sort_keys=True)};\n")
|
|
|
|
|
|
def render_tk(name: str) -> str:
|
|
"""One Tiefenkontakt design as a browser module (src/ui/tk_models.py)."""
|
|
from src.ui import tk_models
|
|
return ("// GENERATED by tools/gen_web_schema.py from data/unit_models/tiefenkontakt.json.gz;"
|
|
" do not edit by hand.\n"
|
|
"export const MODEL = "
|
|
f"{json.dumps(tk_models.browser_model(name), separators=(',', ':'))};\n")
|
|
|
|
|
|
def tk_names() -> tuple:
|
|
from src.ui import tk_models
|
|
return tk_models.names()
|
|
|
|
|
|
def render(text: str) -> str:
|
|
start = text.index(BEGIN)
|
|
end = text.index(END) + len(END)
|
|
return text[:start] + render_block() + text[end:]
|
|
|
|
|
|
def render_tokens(text: str) -> str:
|
|
"""tokens.css with its generated block replaced by the theme tokens."""
|
|
from src.ui import theme
|
|
start = text.index(CSS_BEGIN) + len(CSS_BEGIN)
|
|
end = text.index(CSS_END, start)
|
|
return text[:start] + theme.css_tokens() + text[end:]
|
|
|
|
|
|
def main(argv=None) -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--check", action="store_true",
|
|
help="fail when schema.js differs from a fresh render")
|
|
args = parser.parse_args(argv)
|
|
current = SCHEMA_JS.read_text(encoding="utf-8")
|
|
try:
|
|
rendered = render(current)
|
|
except ValueError:
|
|
print("schema.js: generated block markers missing", file=sys.stderr)
|
|
return 2
|
|
profiles = render_profiles()
|
|
profiles_current = PROFILES_JS.read_text(encoding="utf-8") if PROFILES_JS.exists() else ""
|
|
models = render_models()
|
|
models_current = MODELS_JS.read_text(encoding="utf-8") if MODELS_JS.exists() else ""
|
|
tokens_current = TOKENS_CSS.read_text(encoding="utf-8")
|
|
try:
|
|
tokens = render_tokens(tokens_current)
|
|
except ValueError:
|
|
print("tokens.css: generated block markers missing", file=sys.stderr)
|
|
return 2
|
|
if args.check:
|
|
if rendered != current:
|
|
print("schema.js: generated block out of date; run tools/gen_web_schema.py",
|
|
file=sys.stderr)
|
|
return 1
|
|
if profiles != profiles_current:
|
|
print("silhouette-profiles.js out of date; run tools/gen_web_schema.py",
|
|
file=sys.stderr)
|
|
return 1
|
|
if models != models_current:
|
|
print("unit-models.js out of date; run tools/gen_web_schema.py",
|
|
file=sys.stderr)
|
|
return 1
|
|
if tokens != tokens_current:
|
|
print("tokens.css: theme tokens out of date; run tools/gen_web_schema.py",
|
|
file=sys.stderr)
|
|
return 1
|
|
for group in VARIANT_GROUPS:
|
|
path = variants_js(group)
|
|
if not path.exists() or path.read_text(encoding="utf-8") != render_variants(group):
|
|
print(f"{path.name} out of date; run tools/gen_web_schema.py", file=sys.stderr)
|
|
return 1
|
|
for name in tk_names():
|
|
path = tk_js(name)
|
|
if not path.exists() or path.read_text(encoding="utf-8") != render_tk(name):
|
|
print(f"tk/{path.name} out of date; run tools/gen_web_schema.py",
|
|
file=sys.stderr)
|
|
return 1
|
|
print("web schema up to date")
|
|
return 0
|
|
if profiles != profiles_current:
|
|
PROFILES_JS.write_text(profiles, encoding="utf-8", newline="\n")
|
|
print(f"wrote {PROFILES_JS.relative_to(ROOT)}")
|
|
if models != models_current:
|
|
MODELS_JS.write_text(models, encoding="utf-8", newline="\n")
|
|
print(f"wrote {MODELS_JS.relative_to(ROOT)}")
|
|
if tokens != tokens_current:
|
|
TOKENS_CSS.write_text(tokens, encoding="utf-8", newline="\n")
|
|
print(f"wrote {TOKENS_CSS.relative_to(ROOT)}")
|
|
for group in VARIANT_GROUPS:
|
|
path, text = variants_js(group), render_variants(group)
|
|
if not path.exists() or path.read_text(encoding="utf-8") != text:
|
|
path.write_text(text, encoding="utf-8", newline="\n")
|
|
print(f"wrote {path.relative_to(ROOT)}")
|
|
for name in tk_names():
|
|
path, text = tk_js(name), render_tk(name)
|
|
if not path.exists() or path.read_text(encoding="utf-8") != text:
|
|
path.parent.mkdir(exist_ok=True)
|
|
path.write_text(text, encoding="utf-8", newline="\n")
|
|
print(f"wrote {path.relative_to(ROOT)}")
|
|
if rendered != current:
|
|
SCHEMA_JS.write_text(rendered, encoding="utf-8", newline="\n")
|
|
shown = SCHEMA_JS.relative_to(ROOT) if SCHEMA_JS.is_relative_to(ROOT) else SCHEMA_JS
|
|
print(f"wrote {shown}")
|
|
else:
|
|
print("web schema unchanged")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|