Add crew watches, fatigue, action stations and morale

Frigate and crewed boat stand three watches relieved every hour of game
time. Action stations make the crew more alert but tire everybody;
morale moves with kills, damage and HQ tasks. The crew's performance
raises the sonar recognition threshold, needs more lookout/periscope
contrast and slows damage-control work. A fresh crew is exactly the
calibrated crew (77/77 metrics unchanged). Damage page 3, Bridge G,
boat keys B/M, Remote Crew cards and orders, save v21 "watch" and
crew.watch.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019eZphdvoJiBgWqq2zSH4aJ
This commit is contained in:
Claude committed 2026-09-27 17:20:08 +00:00
1 parent de0a9e7497
commit c1946eb75f
49 files changed
+1121 -63

No files matched your search

+1 -1
View File
@@ -207,7 +207,7 @@ def test_damage_control_over_remote_crew_and_projection():
assert sub.damage_control.compartments[_index("engine")].closed
assert apply(game, "uboot_bulkhead", {"compartment": "engine", "closed": False},
"uboot_nav") is False
payload = _uboot_damage(sub)
payload = _uboot_damage(game, game.opfor)
assert [row["name"] for row in payload["compartments"]] == list(COMPARTMENTS)
assert payload["teams"][1]["task"] == "seal" and payload["power"] is True
blob = json.dumps(payload)
+2 -1
View File
@@ -437,7 +437,8 @@ def test_v2_nonlethal_station_controls_are_native_and_exactly_wired():
"engine_set_telegraph", "engine_set_course", "engine_set_speed",
"engine_set_quiet_mode",
"damage_assign_team", "damage_unassign_team", "radio_capture_hfdf",
"radio_task_accept", "radio_task_decline",
"radio_task_accept", "radio_task_decline", "crew_action_stations",
"crew_watch_change",
"eloka_annotate", "eloka_clear_annotation",
"sonar_set_listen_bearing", "sonar_set_focus", "sonar_clear_focus",
"sonar_set_array_mode", "sonar_set_tas", "sonar_set_tow_depth",
+3
View File
@@ -619,6 +619,9 @@ def test_fusion_schema_rejects_duplicate_refs_and_action_role_mismatch(server):
("damage", "damage_unassign_team", {"team": 3, "compartment": "engine"}),
("radio", "radio_capture_hfdf", {"ref": "opaque"}),
("radio", "radio_task_accept", {"task": 1}),
("bridge", "crew_action_stations", {"enabled": True}),
("damage", "crew_action_stations", {"enabled": False}),
("damage", "crew_watch_change", {}),
("radio", "radio_task_decline", {"task": 10_000}),
("eloka", "eloka_annotate", {"ref": "opaque", "candidate_ref": "choice"}),
("eloka", "eloka_clear_annotation", {"ref": "opaque"}),
+2 -2
View File
@@ -109,12 +109,12 @@ def test_role_allowlists_detachment_bounds_and_no_hidden_identifiers(published):
game, _, server = published
expected = {
"bridge": {"navigation", "orders", "threat", "systems",
"tactical_summary", "sightings"},
"tactical_summary", "sightings", "crew"},
"sonar": {"observations", "settings", "visualization"},
"weapons": {"inventory", "readiness", "designated_target", "navigation",
"tactical", "target_choices", "depth_m", "tubes", "settings",
"own_weapons", "active_assets"},
"damage": {"compartments", "teams", "total", "sunk", "stability"},
"damage": {"compartments", "teams", "total", "sunk", "stability", "crew"},
"opz": {"observations", "fusions", "radar", "source_classifications",
"radar_blips", "designated_target_ref", "own_assets", "defense",
"asm_observations"},
+217
View File
@@ -0,0 +1,217 @@
"""Crew watches, fatigue and morale (frigate and crewed boat; save v21)."""
import copy
import json
import sys
from pathlib import Path
import pygame
import pytest
from src.core import config, uboot_local
from src.core.crew import CrewState, sonar_penalty_db
from src.core.game import Game
from src.core.station import Station
from src.sonar import equation
sys.path.insert(0, str(Path(__file__).parent))
from test_opfor_sub import _crewed # noqa: E402
from test_uboot_scope import _key, _local_boat # noqa: E402
def _game(seed=5201):
game = Game(seed=seed, start_menu=False, audio_enabled=False)
game.tasking.next_offer_t = 1e9
return game
def _run(game, seconds, dt=5.0):
for _ in range(int(seconds / dt)):
game.sim_t += dt
game._update_crew(dt)
def test_a_fresh_crew_is_the_calibrated_crew():
game = _game()
assert game.crew_effect() == 1.0
assert game.sonar.operator_dt_db == 0.0 and game.damage.crew_factor == 1.0
# Normal rotation keeps the duty watch below the free fatigue for hours.
_run(game, 4 * config.CREW_WATCH_S + 120.0)
assert game.crew_watch.duty_fatigue() <= config.CREW_FATIGUE_FREE
assert game.crew_effect() == pytest.approx(1.0)
def test_watch_is_relieved_every_hour_with_a_short_turnover():
game = _game()
_run(game, config.CREW_WATCH_S)
watch = game.crew_watch
assert watch.on_watch == 1 and watch.in_turnover(game.sim_t)
assert game.crew_effect() == pytest.approx(config.CREW_TURNOVER_FACTOR)
_run(game, config.CREW_TURNOVER_S + 5.0)
assert not watch.in_turnover(game.sim_t) and game.crew_effect() == pytest.approx(1.0)
def test_action_stations_alert_first_then_tired():
game = _game()
assert game.set_action_stations(True) is True
assert game.set_action_stations(True) == "not_ready"
assert game.crew_effect() == pytest.approx(config.CREW_ACTION_BONUS)
assert game.change_watch() == "not_ready" # nobody to relieve
_run(game, 90 * 60.0)
assert all(value > 0.9 for value in game.crew_watch.fatigue)
assert game.crew_effect() < 0.8
assert game.sonar.operator_dt_db > 1.0
assert game.damage.crew_factor == pytest.approx(game.crew_effect())
assert game.set_action_stations(False) is True
assert game.crew_watch.watch_left_s(game.sim_t) == config.CREW_WATCH_S
_run(game, 3 * config.CREW_WATCH_S + 120.0)
assert game.crew_effect() == pytest.approx(1.0) # rested again
def test_morale_follows_kills_damage_and_tasks():
game = _game()
start = game.crew_watch.morale
task = game._offer_task("emcon")
game.decline_task(task["id"])
assert game.crew_watch.morale < start
hostile = next(sub for sub in game.subs if sub.side == "hostile")
hostile.sunk = True
_run(game, 5.0)
assert game.crew_watch.morale > start
morale = game.crew_watch.morale
game.damage.compartments["engine"].state = "BESCHAEDIGT"
_run(game, 5.0)
assert game.crew_watch.morale < morale
_run(game, 5.0)
assert game.crew_watch.damaged == 1 # credited once
def test_tired_sonar_needs_a_stronger_signal():
terms = dict(frequency_hz=300.0, distance_nm=5.0, target_bonus=1.0,
excess_path_loss_db=0.0, absorption_db_per_km=0.0,
legacy_absorption_db=0.0, own_range_factor=1.0,
array_range_factor=1.0, sea_state=3, rain=0.0, shipping_contacts=0)
fresh = equation.passive_terms(**terms)
tired = equation.passive_terms(**terms, threshold_db=sonar_penalty_db(0.8))
assert fresh.signal_excess_db - tired.signal_excess_db == pytest.approx(2.0)
def test_tired_damage_control_is_slower():
game = _game()
room = game.damage.compartments["engine"]
fresh = game.damage.repair_rates("engine")
game.damage.teams[1] = "engine"
game.damage.team_eta[1] = 0.0
fresh = game.damage.repair_rates("engine")
game.damage.crew_factor = 0.8
tired = game.damage.repair_rates("engine")
assert tired[0] == pytest.approx(0.8 * fresh[0]) and tired[1] == pytest.approx(0.8 * fresh[1])
assert room is game.damage.compartments["engine"]
def test_crew_page_keys_and_draw():
game = _game()
game.station, game.station_page = Station.DAMAGE, 2
_key(game, pygame.K_g)
assert game.crew_watch.action_stations
_key(game, pygame.K_w) # blocked at action stations
assert game.crew_watch.on_watch == 0
game.draw()
game.station, game.station_page = Station.BRIDGE, 0
_key(game, pygame.K_g)
assert not game.crew_watch.action_stations
game.station, game.station_page = Station.DAMAGE, 2
game.crew_watch.turnover_t = None
_key(game, pygame.K_w)
assert game.crew_watch.on_watch != 0
game.station, game.station_page = Station.DAMAGE, 0
before = game.crew_watch.on_watch
_key(game, pygame.K_w) # plan page: no watch order
assert game.crew_watch.on_watch == before
def test_crew_state_schema_is_exact():
state = CrewState(12.0).serialize()
assert CrewState.valid_state(state)
assert CrewState.restore(state).serialize() == state
for key, value in (("morale", 1.5), ("on_watch", 3), ("fatigue", [0.0, 0.0]),
("turnover_t", float("nan")), ("kills", -1), ("version", 2)):
broken = copy.deepcopy(state)
broken[key] = value
assert not CrewState.valid_state(broken)
broken = dict(state, action_stations=True, turnover_t=20.0)
assert not CrewState.valid_state(broken)
broken = dict(state, extra=1)
assert not CrewState.valid_state(broken)
with pytest.raises(ValueError):
CrewState.restore(broken)
def test_save_load_continues_the_watch_bill_identically():
game = _game()
game.set_action_stations(True)
_run(game, 1200.0)
game.damage.compartments["engine"].state = "BESCHAEDIGT"
_run(game, 5.0)
document = json.loads(json.dumps(game.save_state()))
other = _game()
assert other._load_save_data(document)
assert other.damage.crew_factor == game.damage.crew_factor
assert other.sonar.operator_dt_db == game.sonar.operator_dt_db
for runner in (game, other):
runner.set_action_stations(False)
_run(runner, 1800.0)
assert game.crew_watch.serialize() == other.crew_watch.serialize()
for mutate in (lambda doc: doc.pop("watch"),
lambda doc: doc["watch"].update(watch_t=doc["sim_t"] + 10.0),
lambda doc: doc["watch"].update(morale="high")):
broken = copy.deepcopy(document)
mutate(broken)
assert not other._load_save_data(broken)
def test_crewed_boat_has_its_own_watch_bill():
game, _server, bridge = _crewed()
boat = game.opfor
assert boat.watch.watch_t == game.sim_t
apply = bridge._apply_opfor_action
assert apply(game, "uboot_action_stations", {"enabled": True}, "uboot_engine") is True
assert boat.watch.action_stations and not game.crew_watch.action_stations
assert apply(game, "uboot_watch_change", {}, "uboot") == "not_ready"
assert apply(game, "uboot_action_stations", {"enabled": False}, "uboot_sonar") is False
_run(game, 5400.0)
effect = boat.watch.effectiveness(game.sim_t)
assert effect < 0.8
assert boat.sub.damage_control.crew_factor == pytest.approx(effect)
assert boat.station.sonar.operator_dt_db == pytest.approx(sonar_penalty_db(effect))
assert game.sonar.operator_dt_db == 0.0 # the frigate is not tired
document = json.loads(json.dumps(game.save_state()))
assert document["crew"]["watch"] == boat.watch.serialize()
broken = copy.deepcopy(document)
del broken["crew"]["watch"]
assert not game._load_save_data(broken)
game.release_opfor_sub()
assert boat.sub.damage_control.crew_factor == 1.0
def test_boat_keys_order_the_watch_bill():
game, boat = _local_boat()
uboot_local.set_local_station(game, "uboot_engine")
boat.command_page = 3
_key(game, pygame.K_b)
assert boat.watch.action_stations
_key(game, pygame.K_b)
assert not boat.watch.action_stations
boat.watch.turnover_t = None
_key(game, pygame.K_m)
assert boat.watch.on_watch != 0
game.draw()
def test_remote_crew_projection():
from src.commander.projections import _crew
game = _game()
payload = _crew(game)
assert payload["effectiveness"] == 1.0 and payload["on_watch"] == 1
assert [row["index"] for row in payload["watches"]] == [1, 2, 3]
assert json.loads(json.dumps(payload)) == payload
+10 -3
View File
@@ -19,11 +19,11 @@ def _game(seed=1201):
return Game(seed=seed, start_menu=False, audio_enabled=False)
def test_save_is_v20_and_older_documents_are_rejected():
def test_save_is_v21_and_older_documents_are_rejected():
game = _game()
state = json.loads(json.dumps(game.save_state()))
assert (state["version"], state["save_schema"]) == (20, "u-jagd-save-v20")
assert (SAVE_VERSION, SAVE_SCHEMA) == (20, "u-jagd-save-v20")
assert (state["version"], state["save_schema"]) == (21, "u-jagd-save-v21")
assert (SAVE_VERSION, SAVE_SCHEMA) == (21, "u-jagd-save-v21")
assert set(state) == SAVE_ROOT_FIELDS
before = game.save_state()
legacy = copy.deepcopy(state)
@@ -86,6 +86,13 @@ def test_save_is_v20_and_older_documents_are_rejected():
for row in v19["subs"]:
del row["damage_control"]
assert not game._load_save_data(v19)
# v20 differs by HQ tasking and the crew's watch bill.
v20 = copy.deepcopy(state)
v20["version"] = 20
v20["save_schema"] = "u-jagd-save-v20"
del v20["tasking"]
del v20["watch"]
assert not game._load_save_data(v20)
assert game.save_state() == before
+6 -5
View File
@@ -15,7 +15,6 @@ SONAR_PAGES = (
TWO_PAGE_STATIONS = {
Station.WEAPONS: ("WEAPONS_TARGET", "WEAPONS_AMMO"),
Station.DAMAGE: ("DAMAGE_PLAN", "DAMAGE_DETAIL"),
Station.OPZ: ("OPZ_PICTURE", "OPZ_TARGET"),
Station.ENGINE: ("ENGINE_TELEGRAPH", "ENGINE_SYSTEMS"),
Station.ELOKA: ("ELOKA_INTERCEPTS", "ELOKA_EVIDENCE"),
@@ -28,6 +27,7 @@ def test_page_metadata_covers_every_canonical_station():
assert STATION_PAGES[Station.HELICOPTER] == ("HELO_STATUS", "HELO_MISSION", "HELO_SONAR", "HELO_ACOUSTIC")
assert STATION_PAGES[Station.BRIDGE] == ("BRIDGE_NAV", "BRIDGE_MISSION", "BRIDGE_LOOKOUT")
assert STATION_PAGES[Station.RADIO] == ("RADIO_HFDF", "RADIO_MESSAGES", "RADIO_TASKS")
assert STATION_PAGES[Station.DAMAGE] == ("DAMAGE_PLAN", "DAMAGE_DETAIL", "DAMAGE_CREW")
for station, pages in TWO_PAGE_STATIONS.items():
assert STATION_PAGES[station] == pages
@@ -38,7 +38,7 @@ def test_page_metadata_covers_every_canonical_station():
assert all(isinstance(page, str) and page for page in pages)
assert len(pages) == (6 if station is Station.SONAR else
4 if station is Station.HELICOPTER else
3 if station in (Station.BRIDGE, Station.RADIO) else 2)
3 if station in (Station.BRIDGE, Station.RADIO, Station.DAMAGE) else 2)
def test_helicopter_sonar_page_wraps():
@@ -71,9 +71,10 @@ def test_two_page_stations_wrap_within_two_pages(station):
assert station_page_step(station, 0, -1) == 1
def test_radio_tasks_page_wraps():
assert station_page_step(Station.RADIO, 1, 1) == 2
assert station_page_step(Station.RADIO, 2, 1) == 0
@pytest.mark.parametrize("station", (Station.RADIO, Station.DAMAGE))
def test_three_page_stations_wrap(station):
assert station_page_step(station, 1, 1) == 2
assert station_page_step(station, 2, 1) == 0
def test_station_alias_uses_canonical_opz_metadata():