"""Beat scripts for the in-client instruction tutorials. Pure Python on purpose — no NiceGUI, no PIL — so `smoke_test.py` and pytest can check every caption, gate and quiz link headlessly. Drawing lives in `maze_render.py`; the driver that waits on keypresses lives in `maze_stages.py`. The tutorials replaced the old prose instruction pages (`_INTRO_PRO_*`, `_INTRO_REA_MD`), the two standalone quiz pages, the standalone avatar page and the two "Practice complete!" pages. One stage per leg now covers briefing, practice and (for the reactive leg) avatar choice — see `docs/superpowers/specs/2026-09-09-interactive-instructions.md`. Every number in a caption is interpolated from `maze_design.CONFIG`. Nothing here states a decay rate, a per-freeze point cost or a dollar amount: the researcher's decision is that captions stay qualitative about cost (spec §10f). Captions are drawn with PIL, so they stay plain ASCII — no emoji, no en/em dashes. """ from dataclasses import dataclass import maze_design as D import maze_grid as G import orders import questionnaires #: Fixed participant id used to build the demo frames. Not a real session — it #: only exists so the instruction frames are identical for every participant #: (and reproducible for `generate_instructions_doc.py`). DEMO_PID = "0000" #: Longest a caption may be. Enforced by smoke_test.py — a caption that wraps #: past three lines starts to look like the prose page this replaced. MAX_CAPTION = 120 # --------------------------------------------------------------------------- # Beats # --------------------------------------------------------------------------- #: Beat kinds. SAY = "say" # advance on Space / Enter PRESS = "press" # advance only on one of `keys` MOVE = "move" # advance after `moves` legal arrow-key steps ASK = "ask" # advance on the correct option number PICK = "pick" # avatar chooser: arrows to browse, Space to confirm @dataclass(frozen=True) class Beat: id: str kind: str caption: str scene: str target: str | None = None keys: tuple = () #: Noise-blast level to play the moment the beat's key is pressed. play_level: int | None = None #: Show the real OUTCOME frame for this level after the key is pressed. outcome_level: int | None = None #: MOVE only — how many legal steps end the beat. moves: int = 0 #: ASK only — the questionnaires.py item id being asked. quiz_id: str | None = None def _ask_beats(items, scene: str) -> list: return [ Beat( id=f"ask_{q['id']}", kind=ASK, caption=q["question"], scene=scene, quiz_id=q["id"], ) for q in items ] # --------------------------------------------------------------------------- # Scene keys — one rendered game state each, built by build_scene() # --------------------------------------------------------------------------- PRO_START = "pro_start" PRO_WALK = "pro_walk" PRO_DECIDE = "pro_decide" PRO_OUTCOME = "pro_outcome_4" REA_START = "rea_start" REA_PROVOCATION = "rea_provocation" REA_DECIDE = "rea_decide" REA_OUTCOME = "rea_outcome_4" AVATAR_PICK = "avatar_pick" # --------------------------------------------------------------------------- # Part 1 — proactive # --------------------------------------------------------------------------- def proactive_beats() -> list: """Briefing beats for Part 1, in order. Practice follows the last one.""" pts = f"{D.CONFIG['sp_start']:,}" n_cp = D.CONFIG["trials_per_run"] n_runs = D.CONFIG["main_runs"] return [ Beat( id="pro_goal", kind=SAY, caption=( "Your goal is to end up with as many points " "as possible." ), scene=PRO_START, ), Beat( id="pro_partner", kind=SAY, caption="This is your partner's avatar. They are about to walk their maze.", scene=PRO_START, target="sp_avatar", ), Beat( id="pro_points", kind=SAY, caption=( f"They start with {pts} points, but they lose points for every " "second it takes them to escape." ), scene=PRO_START, ), Beat( id="pro_watch", kind=SAY, caption="In this first part of the task, you only watch your partner navigate the maze.", scene=PRO_WALK, ), Beat( id="pro_popup", kind=SAY, caption=( f"During each run, a pop-up will appear below {n_cp} times. " "You choose 1, 2, 3 or 4." ), scene=PRO_DECIDE, target="popup_band", ), Beat( id="pro_drain", kind=SAY, caption=( "Levels 2, 3 and 4 blast them with noise and freeze them - " "the longer the freeze, the more points they lose." ), scene=PRO_OUTCOME, ), Beat( id="pro_earn", kind=SAY, caption=( "You earn every point they lose, and those points become " "real money at the end." ), scene=PRO_OUTCOME, ), Beat( id="pro_level1", kind=PRESS, caption=( "Level 1 does nothing at all - no sound, no freeze. " "Press 1 to try it." ), scene=PRO_DECIDE, target="popup_btn_1", keys=("1",), outcome_level=1, ), Beat( id="pro_level2", kind=PRESS, caption=( "Levels 2, 3 and 4 fire a noise blast at them. " "Press 2 to hear the quietest." ), scene=PRO_DECIDE, target="popup_btn_2", keys=("2",), play_level=2, outcome_level=2, ), Beat( id="pro_level3", kind=PRESS, caption=( f"Level 3 is louder, and freezes them for {D.freeze_s(3)} " "seconds. Press 3." ), scene=PRO_DECIDE, target="popup_btn_3", keys=("3",), play_level=3, outcome_level=3, ), Beat( id="pro_level4", kind=PRESS, caption=( f"Level 4 is the loudest - {D.freeze_s(4)} seconds frozen. " "Press 4." ), scene=PRO_DECIDE, target="popup_btn_4", keys=("4",), play_level=4, outcome_level=4, ), Beat( id="pro_structure", kind=SAY, caption=( f"You will watch {n_runs} runs, {n_cp} pop-ups each. " "A random round will be chosen to count toward your bonus payment." ), scene=PRO_START, ), *_ask_beats(questionnaires.QUIZ_PROACTIVE, PRO_START), Beat( id="pro_to_practice", kind=SAY, caption=( "Let's do a practice run first. Press the spacebar to start." ), scene=PRO_START, ), ] def proactive_closing_beat() -> "Beat": """Shown once the practice run is over, before the matching screen.""" return Beat( id="pro_done", kind=SAY, caption=( "That was practice. Next, you will be matched " "with your partner for today." ), scene=PRO_START, ) # --------------------------------------------------------------------------- # Part 2 — reactive # --------------------------------------------------------------------------- def reactive_beats() -> list: """Avatar choice, then the Part 2 briefing. Practice follows the last one.""" pts = f"{D.CONFIG['pp_start']:,}" n_runs = D.CONFIG["main_runs"] return [ Beat( id="rea_avatar", kind=PICK, caption="Pick your avatar for the next part of the task. Use the left and right arrows to browse, and press Space to choose.", scene=AVATAR_PICK, ), Beat( id="rea_you", kind=SAY, caption="This time, you will navigate the maze. Your avatar is ready.", scene=REA_START, target="pp_avatar", ), Beat( id="rea_exit", kind=SAY, caption="Your job is simple: reach the EXIT as fast as you can.", scene=REA_START, target="exit_tile", ), Beat( id="rea_points", kind=SAY, caption=( f"You start with {pts} points too, draining every second " "until you are out. What is left is your bonus." ), scene=REA_START, ), Beat( id="rea_move", kind=MOVE, caption="Use the arrow keys to move. Take four steps.", scene=REA_START, moves=4, ), Beat( id="rea_frozen", kind=SAY, caption=( "Your partner can hit you with the same noise blast. " "You will freeze while your points keep draining." ), scene=REA_PROVOCATION, target="prov_banner", play_level=4, ), Beat( id="rea_partner", kind=SAY, caption=( "This is your partner. They are running their own maze at the " "same time, losing points too." ), scene=REA_PROVOCATION, target="partner_panel", ), Beat( id="rea_popup", kind=SAY, caption="After each freeze, you get the same 1-4 choice.", scene=REA_DECIDE, target="popup_band", ), Beat( id="rea_no_gain", kind=SAY, caption=( "Hitting back costs them points, but it adds nothing to yours." ), scene=REA_OUTCOME, ), Beat( id="rea_structure", kind=SAY, caption=( "Your bonus here depends only on how fast you finish. " f"{n_runs} runs, and one is drawn at random to pay your bonus." ), scene=REA_START, ), *_ask_beats(questionnaires.QUIZ_REACTIVE, REA_START), Beat( id="rea_to_practice", kind=SAY, caption=( "Let's do a practice run first. Press the spacebar to start." ), scene=REA_START, ), ] def reactive_closing_beat() -> "Beat": """Shown once the practice run is over, before the paying runs.""" return Beat( id="rea_done", kind=SAY, caption=( f"That was practice. The next {D.CONFIG['main_runs']} runs count " "- one of them pays your bonus." ), scene=REA_START, ) def beats_for(leg: str) -> list: return proactive_beats() if leg == "proactive" else reactive_beats() def closing_beat_for(leg: str) -> "Beat": return ( proactive_closing_beat() if leg == "proactive" else reactive_closing_beat() ) # --------------------------------------------------------------------------- # Practice coach marks # --------------------------------------------------------------------------- # # Fired once each, the first time the mechanic they describe appears in the # practice run. Main runs are never interrupted. COACH_MARKS = { "pro_first_popup": "Here it comes. Pick 1 to 4.", "rea_run_start": "Arrow keys. Get to the EXIT.", "rea_first_popup": "You can hit back, or press 1 to let it go.", } # --------------------------------------------------------------------------- # Demo states # --------------------------------------------------------------------------- def build_scene(key: str, avatar: str = "walker_a", sp_avatar: str = "partner_a"): """Build the MazeState one scene key renders from. Deterministic: every scene comes from DEMO_PID, so the instruction frames are the same for every participant and can be regenerated for the docs. Returns None for AVATAR_PICK, which is not a game state. """ if key == AVATAR_PICK: return None if key.startswith("pro"): run = orders.runs_for(DEMO_PID, "proactive", False)[0] s = G.begin_leg("proactive", [run], False, avatar=avatar, sp_avatar=sp_avatar) if key == PRO_START: return s if key == PRO_WALK: s.mode = G.MODE_WATCHING return s # Both DECIDE and OUTCOME open the pop-up first. G.start_deciding(s) if key == PRO_DECIDE: return s level = int(key.rsplit("_", 1)[1]) G.choose(s, level, rt_ms=0) return s run = orders.runs_for(DEMO_PID, "reactive", False)[-1] s = G.begin_leg("reactive", [run], False, avatar=avatar, sp_avatar=sp_avatar) s.mode = G.MODE_WALKING if key == REA_START: return s # Park on a trial whose scripted provocation is the loudest, so the # freeze the participant is shown is the one worth showing. for i, spec in enumerate(run): if spec.provocation == 4: s.trial_idx = i break G.begin_provocation(s) if key == REA_PROVOCATION: return s G.end_provocation(s) if key == REA_DECIDE: return s level = int(key.rsplit("_", 1)[1]) G.choose(s, level, rt_ms=0) return s def quiz_item(quiz_id: str) -> dict: """The questionnaires.py item behind an ASK beat.""" for q in questionnaires.QUIZ_PROACTIVE + questionnaires.QUIZ_REACTIVE: if q["id"] == quiz_id: return q raise KeyError(quiz_id)