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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
"""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)