Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 | 45x 45x 45x 35x 35x 35x 10x 10x 10x 10x 5x 5x 5x 5x 5x 3x 3x 3x 2x 3x 5x 126x 126x 126x 126x 1x 126x 2x 2x 2x 2x 2x 1x 1x 2x 141x 141x 141x 141x 141x 141x 141x 141x 141x 141x 141x 141x 4x 141x 141x 141x 140x 140x 1x 1x 1x 9x 9x 28x 52x 52x 3120x 52x 55x 55x 28x 28x 58x 14x 122x 28x 28x 28x 28x 55x 28x 28x 28x 28x 28x 28x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 112x 52x 52x 21x 19x 52x 110x 109x 109x 110x 110x 109x 134x 134x 133x 134x 134x 134x 134x 134x 134x 133x 134x 134x 134x 134x 134x 134x 134x 65x 65x 65x 65x 16x 16x 16x 16x 65x 134x 134x 12x 12x 12x 12x 35x 13x 13x 22x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 7x 7x 7x 7x 4x 4x 4x 1x 1x 1x 1x 4x 4x 4x 4x 1x 1x 21x 13x 13x 13x 4x 13x 13x 2x 2x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 141x 141x 141x 141x 141x 141x 141x 141x 125x 4x 4x 4x 4x 4x 4x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 3x 3x 1x 1x 2x 1x 2x 2x 4x 1x 1x 1x 1x 3x 4x 3x 3x 3x 5x 12x 12x 2x 1x 1x 1x 1x 10x 5x 4x 1x 1x 1x 3x 2x 2x 12x 30x 30x 30x 30x 30x 30x 30x 30x 30x | import type { WebAudioSessionSlicePlayback } from '../../runtime/web-audio-session.ts';
/**
* The engine side of a play on the built-in and LR2 play screens (`PlaySession`): launches the shared engine
* (`runEngineDriver`) on the prepared chart, drains its UI signals each frame into the score, gauge, judgement, combo
* and lane-effect state the scene draws (key beams, bombs, POOR BGA, long-note holds), and starts the ending once
* the chart settles. ESC / F5 / a teardown interrupt the engine here.
*
* The functions read and write the scene's state through {@link PlayEngineContext}; the play session passes itself.
*/
import type { RandomDraws, TimingResolver, ChartPlayVariant } from '@be-music/chart-model';
import type { BeMusicEvent, BeMusicJSON } from '@be-music/chart-source';
import {
PlayerInterruptedError,
type PlayerRulesetInfo,
type PlayerSummary,
type PreparedPlaybackChartData,
} from '@be-music/player-engine';
import { resolveLR2LaneIndex, clampHRandomThresholdBPM } from '@be-music/player-engine/lanes';
import type {
PlayerInputSignalBus,
PlayerGaugeSummary,
PlayerUICommand,
PlayerUIFramePayload,
PlayerUISignalBus,
PlayerJudgeComboSignalState,
PlayerStateSignals,
} from '@be-music/player-engine/signals';
import type { TimedPlayableNote } from '@be-music/player-engine/display';
import type {
BeMusicReplay,
ReplayRecordingOptions,
ReplayViewState,
ReplayOptionState,
} from '@be-music/player-engine/replay';
import {
resolveDisplayedPoor,
type GaugePick,
type JudgmentKind,
type LongNoteStyle,
type ScoreSummary,
} from '@be-music/player-engine/ruleset';
import {
playEventFromUICommand,
reducePlayState,
ReplayViewCursor,
type LibraryChart,
type PlayResult,
} from '@be-music/player-scene-model';
import { resolveScratchAngle } from '@be-music/player-skin-sdk';
import type { BeMusicJudgeWindows } from '@be-music/player-skin-sdk';
import type { AudioBusHandle } from '../../runtime/audio-bus.ts';
import { runEngineDriver } from '../../runtime/engine-driver.ts';
import type { NonstopStageMixer } from '../../runtime/nonstop-audio.ts';
import { supportsLiveTempoChange, type ReplayTempo } from '../../runtime/play-tempo.ts';
import { drainWebUISignals, type WebUIRuntimeCallbacks } from '../../runtime/web-ui-runtime.ts';
import type { PlayChartContext } from './play-chart.ts';
import { resolvePlayerMineModifier, type PlaySessionOptions } from './play-options.ts';
import { toBeMusicJudgeWindows } from './judge-windows.ts';
import type { LongNoteTracker } from './long-note-tracker.ts';
import type { LiveTempo } from './live-tempo.ts';
import { logger } from '../../logger.ts';
import { resolveBeMusicLaneKind } from '../../skin/be-music/registry.ts';
import type { BeMusicJudgmentKind } from '../../skin/be-music/types.ts';
import { beginExitSequence, isEveryNoteJudged, resolvePlayEnding, type PlayEndingContext } from './play-ending.ts';
import { advanceJudgeSideTimings, type PlayPresentation } from './play-presentation.ts';
import { resolveSeekWaiters, settleSeekWaiters } from './play-seek.ts';
import { applyPlayEvent } from './play-state.ts';
import { resolveLaneInputTokens } from './play-cover-keys.ts';
import { shouldSkipPlayKey } from './play-keys.ts';
import { samplePlaybackRate } from './play-media.ts';
import {
flushReplayView,
followTempoChange,
startReplayView,
updateReplayView,
type PlayViewSettingsContext,
} from './play-view-settings.ts';
const log = logger('gameplay');
/** The scene's members the engine side reads and writes: the play session's members of these names. */
export interface PlayEngineContext
extends PlayViewSettingsContext, PlayEndingContext, Omit<PlayPresentation, 'longNotes' | 'playMoments'> {
readonly options: Readonly<
Pick<
PlaySessionOptions,
| 'judgeTimingMs'
| 'judgeTimingAutoAdjust'
| 'onJudgeTimingChange'
| 'autoPlay'
| 'allowSeek'
| 'judgeAlgorithm'
| 'customJudge'
| 'lr2Gambol'
| 'longNoteModifier'
| 'longNoteRate'
| 'sevenToNinePattern'
| 'sevenToNineType'
| 'battleMode'
| 'dBattleScratch'
| 'practiceRange'
| 'lr2ExtraMode'
| 'lr2ExtraLevel'
| 'extraNoteDepth'
| 'extraNoteScratch'
| 'scrollSection'
| 'scrollRate'
| 'scrollModifier'
| 'mineModifier'
| 'crazyAddMine'
| 'gaugeAutoShift'
| 'bmsonTotalMode'
| 'bottomShiftableGauge'
| 'autoScratch1P'
| 'autoScratch2P'
| 'anyKey'
| 'laneKeys'
| 'chartMD5'
| 'chartSHA256'
| 'course'
| 'dpFlip'
| 'gauge'
| 'gauge2P'
| 'judgeRuleset'
| 'nonstop'
| 'onChartFinished'
| 'onExit'
| 'onRestart'
| 'replayRecorder'
| 'replayDeviceNames'
| 'randomSeed1P'
| 'randomSeed2P'
| 'randomFixedLane1P'
| 'randomFixedLane2P'
| 'lr2RandomScratch1P'
| 'lr2RandomScratch2P'
| 'hRandomThresholdBPM'
| 'random1P'
| 'random2P'
| 'replay'
| 'fixedArrangement'
| 'tempoPitchMode'
| 'hsFix'
| 'classicHiSpeed'
| 'floatingHiSpeed'
| 'fixedDisplayArea'
| 'normalHiSpeedLevel'
| 'hiSpeedLocked'
| 'onHiSpeedChange'
| 'onTempoChange'
| 'replayView'
| 'laneCover'
| 'shutter'
| 'hiddenSudden1P'
| 'hiddenSudden2P'
>
>;
readonly chart: LibraryChart | undefined;
readonly resolvedChart: BeMusicJSON | undefined;
readonly randomDraws: RandomDraws | undefined;
readonly randomSeeds?: { p1?: string; p2?: string };
/** The chart's own gameplay options and the lane / mine selection they leave (#844). */
readonly chartPlay?: PlayChartContext['chartPlay'];
readonly preparedChart: PreparedPlaybackChartData | undefined;
readonly notes: TimedPlayableNote[];
readonly timingResolver: TimingResolver | undefined;
readonly chartPlayVariant: ChartPlayVariant;
readonly audioContext: AudioContext | undefined;
readonly audioBus: AudioBusHandle | undefined;
readonly decodedSamples: Map<string, AudioBuffer>;
readonly wavCmdVolumeMultipliers: Map<string, number>;
readonly bmsonSlicePlayback: Map<BeMusicEvent, WebAudioSessionSlicePlayback> | undefined;
readonly nonstopMixer: NonstopStageMixer | undefined;
readonly replayTempo: ReplayTempo | undefined;
readonly liveTempo: LiveTempo;
sharedEnginePromise: Promise<unknown> | undefined;
sharedEngineAbortController: AbortController | undefined;
chartEndHold: AbortController | undefined;
onHeldPlayEnded: ((summary: Readonly<PlayerSummary>) => void) | undefined;
sharedEngineLastJudgeComboTick: number;
sharedEngineSignals: { uiSignals: PlayerUISignalBus; stateSignals?: PlayerStateSignals } | undefined;
sharedEngineInputSignals: PlayerInputSignalBus | undefined;
sharedEngineInterrupt: 'escape' | 'restart' | undefined;
sharedEngineChartSettled: boolean;
engineFrame: Readonly<PlayerUIFramePayload> | undefined;
seekGeneration: number;
practice: boolean;
engineStartRequested: boolean;
readonly paused: boolean;
readonly seekWaiters: Array<{ seconds: number; resolve: () => void }>;
bgaActiveVideos: Record<string, { key: string; cueSeconds: number } | undefined>;
chartEnded: boolean;
chartEndTimeout: number | undefined;
chartEndGate?: (seconds: number) => void;
/** Starts the outro's fade-out at once, skipping what is left of the lead-out; set while the lead-out runs. */
chartEndFade?: (() => void) | undefined;
replay: BeMusicReplay | undefined;
readonly replayWaiters: Array<(replay: BeMusicReplay) => void>;
replayViewCursor: ReplayViewCursor | undefined;
score: ScoreSummary;
gaugeState: PlayerGaugeSummary;
fastCount: number;
slowCount: number;
judgeWindows: BeMusicJudgeWindows | undefined;
longNoteStyle: LongNoteStyle | undefined;
readonly longNotes: LongNoteTracker;
readonly playMoments: PlayPresentation['playMoments'];
pauseAllBGAVideos(): void;
resolveSelectedGauge(): GaugePick;
getResultData(): PlayResult | undefined;
}
/**
* How long a PRACTICE play fades in as it starts, and how long each loop back to its start crossfades: the end of the
* round fades out under the start of the next (#949).
*/
const PRACTICE_LOOP_CROSSFADE_SECONDS = 0.5;
/**
* "Fully on" hold time before {@link flashKeyBeam} hands the lane laser to the release-fade path.
* Without this hold the sprite would only stay at peak alpha for the first frame (≈16 ms at 60 fps) before the fade
* tween kicked in, which made auto-judged short-note flashes look like a single-frame blink. ~60 ms is enough to
* register visually as a deliberate "tap" without lingering through the next note.
*/
const KEY_ON_FLASH_HOLD_MS = 60;
/**
* Hands the play over to the host once ({@link sharedEngineInterrupt}): a play still being judged is interrupted, so
* the engine stops (and, for ESC, records the play as given up); a play whose chart already settled is complete, so
* its hold is let go and it ends with its replay as usual. A no-op before the engine is running.
*/
export function interruptSharedEngine(ctx: PlayEngineContext, reason: 'escape' | 'restart'): void {
Iif (ctx.sharedEngineInterrupt !== undefined) return;
const signals = ctx.sharedEngineInputSignals;
Eif (signals === undefined) return;
ctx.sharedEngineInterrupt = reason;
flushReplayView(ctx);
if (ctx.sharedEngineChartSettled) {
ctx.chartEndHold?.abort();
return;
}
signals.pushCommand({ kind: 'interrupt', reason });
}
/**
* Turns `channel`'s key beam on (`keyBeamStartedAt`, by channel; a view maps the channel onto its own timers, the LR2
* view onto LR2's key-on bank).
*/
function startKeyBeam(ctx: PlayEngineContext, channel: string): void {
const now = ctx.playClock();
ctx.lanePressedAt.set(channel, now);
ctx.keyBeamStartedAt.set(channel, now);
// A fresh press cancels any in-flight release fade on the same lane; the laser is fully on again from this instant.
ctx.keyBeamFadeStartedAt.delete(channel);
}
/**
* Smoothly extinguishes the lane laser when a key (or auto LN head) releases: the lane's key beam stays on for the
* view's key-beam fade (`PlaySessionTheme.keyOnFadeMs`) while a render-time alpha taper drives the visible amplitude
* down to 0, then its entry is deleted. Anchored on `playClock` so the fade pauses cleanly with the rest of the scene.
*
* The taper is what makes manual key-ups, auto-judged short notes (via {@link flashKeyBeam}), and auto-LN releases
* all decay at the same speed without restarting the key-on keyframe (which would blink the LR2 default skin's lane
* laser back to its fade-in origin before the decay).
*/
function releaseKeyBeam(ctx: PlayEngineContext, channel: string): void {
ctx.laneReleasedAt.set(channel, ctx.playClock());
// Stamp the fade origin unconditionally — the render-side cleanup uses it as the authoritative clock for the
// 1 → 0 alpha taper, and an early return on a missing `keyBeamStartedAt` entry would leave the LN-end path stuck at
// full brightness whenever the deferred startKeyBeam / releaseKeyBeam interleaving had already retired the beam (a
// racy setTimeout cleanup from a previous press could have wiped it between the LN's startKeyBeam and this
// release). The `keyBeamStartedAt.has` gate controls only the deferred cleanup below — the fade start is committed
// regardless so the render side (lane beam / skin key-on sprites) can taper any still-visible laser through to
// alpha=0.
ctx.keyBeamFadeStartedAt.set(channel, ctx.playClock());
Iif (!ctx.keyBeamStartedAt.has(channel)) return;
const fadeMs = ctx.theme.keyOnFadeMs(channel);
const timeout = window.setTimeout(() => {
ctx.keyFlashTimeouts.delete(timeout);
Iif (ctx.disposed) return;
// If the player (or autoplay) re-pressed the same lane during the fade window, leave the beam on and skip the
// delete — the new press has its own lifecycle.
if (!ctx.pressedChannels.has(channel)) {
ctx.keyBeamStartedAt.delete(channel);
}
ctx.keyBeamFadeStartedAt.delete(channel);
}, fadeMs);
ctx.keyFlashTimeouts.add(timeout);
}
/**
* Starts a bomb (hit explosion) on `channel`. The core scene draws it from `bombStartedAt`; themes react through the
* `bomb` event (the LR2 family stamps its per-lane bomb timer at the same instant). A chart's `774:CRAZY_DARK2` (#931)
* draws no bombs; the beatoraja family leaves its bomb timers out the same way.
*/
function triggerBomb(ctx: PlayEngineContext, channel: string, judge?: BeMusicJudgmentKind): void {
Iif (ctx.engineFrame?.chartOptions?.crazyDark === 'DARK2') return;
const now = ctx.playClock();
ctx.bombStartedAt.set(channel, now);
if (judge) ctx.bombJudge.set(channel, judge);
else ctx.bombJudge.delete(channel);
ctx.theme.moment({ kind: 'bomb', channel, at: now });
}
/**
* Auto-play loop: when enabled, every playable note is judged as PERFECT exactly at its scheduled time. Background
* lane sounds (`scheduleAutoSamples`) still handle non-input channels separately.
*
* Long notes are NOT judged on the head; instead the head time marks the LN as actively held (sample + bomb +
* sustained key beam for the visual lane laser) and the actual scoreboard / gauge / combo commit is deferred to
* {@link autoFinalizeLongNotes} when chart-time crosses `endSeconds`. This mirrors what real LR2 does (and what
* `@be-music/player-engine`'s engine does via `pendingAutoLongNotes`): one judgement event per LN, fired at the tail timing
* so the combo pulse aligns with the LN visually completing rather than at its start.
*/
/**
* Brief key-on flash. We turn the lane's key beam on and schedule it to clear after a short interval so the laser
* fades like a real keystroke. Used by autoplay (no real keyboard event) so the player still sees the lane / key
* visuals react.
*
* Auto-judged short notes simulate a "press + release" in one tick: the lane laser lights up immediately (stamped at
* `playClock()`, so the LR2 key-on keyframe begins) and stays at peak alpha for {@link KEY_ON_FLASH_HOLD_MS} before
* handing off to the release-fade path. Without that brief hold the sprite would taper from full to invisible across
* the first 1–2 render frames and the press would read as a single-frame blink rather than a deliberate flash.
*/
function flashKeyBeam(ctx: PlayEngineContext, channel: string): void {
startKeyBeam(ctx, channel);
const timeout = window.setTimeout(() => {
ctx.keyFlashTimeouts.delete(timeout);
Iif (ctx.disposed) return;
// A real press took over during the hold — skip the auto-release, the press lifecycle is in charge. (LN
// sustains drive their own `hold-lane-until-beat` timer keep-alive via `applyEngineCommand`.)
if (ctx.pressedChannels.has(channel)) {
return;
}
releaseKeyBeam(ctx, channel);
}, KEY_ON_FLASH_HOLD_MS);
ctx.keyFlashTimeouts.add(timeout);
}
function publishJudge(
ctx: PlayEngineContext,
judge: JudgmentKind,
seconds: number,
state: Readonly<Pick<PlayerJudgeComboSignalState, 'channel' | 'deltaMs' | 'combo'>>,
): void {
const until = seconds + 0.6;
const channel = state.channel;
ctx.lastJudge = judge;
ctx.lastJudgeUntil = until;
ctx.lastJudgeAt = ctx.playClock();
// Themes restart their per-side judge animation on every judgement (LR2 timer 46 / 47 drives the attached
// `#DST_NOWJUDGE` / `#DST_NOWCOMBO` chains from time=0 per hit). When `channel` isn't supplied (legacy callers) we
// default to the 1P side. PMS / 9 KEY is single-side so every judgement collapses onto 1P regardless of the
// sourcing channel — the LR2 9-key skin authors only the 1P judge plate (channels 22..25 belong to the central /
// right half of the Pop'n nine-lane bank, not the second player).
const isPlayer2 = ctx.chartPlayVariant !== '9' && typeof channel === 'string' && channel.startsWith('2');
const side: '1P' | '2P' = isPlayer2 ? '2P' : '1P';
// The judgment goes with the moment, so a view keeps each lane's and each plate's own (a beatoraja skin's).
ctx.theme.moment({
kind: 'judge',
side,
at: ctx.playClock(),
judge,
...(channel !== undefined ? { channel } : {}),
...(typeof state.deltaMs === 'number' && Number.isFinite(state.deltaMs) ? { deltaMs: state.deltaMs } : {}),
combo: state.combo,
});
// Snapshot the verdict + combo for this side so DP rendering shows each lane group's *own* combo number — frozen at
// the moment that side last hit a note — rather than mirroring the global running combo on both sides.
// A course stage also snapshots the course combo, which an LR2 skin shows there during an expert or grade course.
const { combo, courseCombo } = ctx.playState;
const timings = advanceJudgeSideTimings(ctx.judgeSideState[side].timings, judge, channel, state.deltaMs, until);
ctx.judgeSideState[side] = {
judge,
until,
combo,
...(courseCombo !== undefined ? { courseCombo: courseCombo.combo } : {}),
...(timings !== undefined ? { timings } : {}),
};
// POOR / BAD judgements briefly swap the base BGA for the chart's POOR BGA. We trigger the same window for `BAD`
// because the LR2 spec doesn't distinguish the two for the BGA channel.
if (judge === 'POOR' || judge === 'BAD') {
ctx.lastPoorAt = ctx.playClock();
}
maybeFireFullCombo(ctx);
}
/**
* Raises the `full-combo` theme event (LR2 timers 48 = 1P, 49 = 2P) the moment the player's running combo equals the
* chart's playable note count — i.e. every note has been hit GREAT-or-better and the chain never broke. Latches
* `fullComboFired` so subsequent judges don't re-raise it (which would replay the skin's FC slide-in animation from
* time=0).
*
* AUTO mode also reaches this state on the very last note (every judge is auto-PERFECT, so combo === total at the
* end) — the player specifically asked for the FC presentation to fire in AUTO too, which falls out for free since
* the path is the same `publishJudge` chain manual play uses.
*
* One event covers both sides because we don't yet model per-side combos. For DP this is correct (one player, one
* combo). Battle mode would split.
*/
function maybeFireFullCombo(ctx: PlayEngineContext): void {
Iif (ctx.fullComboFired) return;
if (ctx.score.bad > 0 || ctx.score.poor > 0) return;
Iif (ctx.score.total <= 0) return;
if (ctx.playState.combo < ctx.score.total) return;
ctx.fullComboFired = true;
ctx.theme.moment({ kind: 'full-combo', at: ctx.playClock() });
log.info('FULL COMBO');
}
/** Records the latest key press / autoplay hit for input-reactive skin visuals. */
function noteImpulse(ctx: PlayEngineContext, channel: string): void {
ctx.lastImpulseAt = ctx.playClock();
ctx.lastImpulseKind = resolveBeMusicLaneKind(
channel,
resolveLR2LaneIndex(channel, ctx.chartPlayVariant),
ctx.chartPlayVariant,
);
}
/**
* Kicks off the shared-engine playback loop in the background. Called from the play-start gate inside
* {@link start}. The returned promise is captured on `this.sharedEnginePromise` so a defensive re-entry
* doesn't spawn a second engine; we don't `await` it because the chart can run for several minutes and
* `start()` must return synchronously to the host's setVisible chain.
*
* Catches {@link PlayerInterruptedError} so the engine's `interrupt(escape)` / `interrupt(restart)` commands
* route to the host's `onExit` / `onRestart` callbacks the same way the legacy DOM keyhandler did.
*/
/**
* The chart the engine plays in shared-engine mode: the one `prepareChart` resolved. Since the renderer hands the
* engine its own `PreparedPlaybackChartData` instance (which already encodes shuffle / DP-flip), the chart events
* here only feed engine-internal helpers (timing resolver, BGM / BGA / dynamic-volume realtime audio scheduling),
* none of which care about the shuffled lane channels. The resolved chart carries no control flow, so the engine
* plays it as is; {@link randomDraws} travel with it for the RANDOM summary.
*/
function buildSharedEngineChart(ctx: PlayEngineContext): BeMusicJSON {
return ctx.resolvedChart ?? ctx.chart!.chart;
}
/**
* What a recorded play's log says of its settings and chart, for every family: the gauge, the file hashes and the
* recorder the host gave, the lane options and DP FLIP it played with, the view at the start, and the chart path in
* `native` so a dropped log can be matched back to its song. The engine plays the chart the session resolved (and
* flipped); the song's chart is the source both fingerprints describe (`#RANDOM` unresolved, no lane option applied).
*/
export function resolveReplayRecording(
options: Pick<
PlaySessionOptions,
| 'gauge'
| 'chartSHA256'
| 'chartMD5'
| 'replayRecorder'
| 'replayDeviceNames'
| 'randomSeed1P'
| 'randomSeed2P'
| 'randomFixedLane1P'
| 'randomFixedLane2P'
| 'lr2RandomScratch1P'
| 'lr2RandomScratch2P'
| 'hRandomThresholdBPM'
| 'random1P'
| 'random2P'
| 'dpFlip'
| 'recordingOptionState'
| 'autoPlay'
| 'initialHiSpeed'
| 'hsFix'
| 'classicHiSpeed'
| 'floatingHiSpeed'
| 'fixedDisplayArea'
| 'normalHiSpeedLevel'
| 'judgeTimingMs'
| 'judgeTimingAutoAdjust'
| 'visualConstant'
| 'lift1P'
| 'lift2P'
| 'liftOn1P'
| 'liftOn2P'
| 'noteDurationMs'
| 'constantFadeMs'
| 'bpmGuide'
| 'hiddenSudden1P'
| 'hiddenSudden2P'
| 'shutter'
| 'laneCover'
| 'judgeAlgorithm'
| 'customJudge'
| 'longNoteModifier'
| 'longNoteRate'
| 'sevenToNinePattern'
| 'sevenToNineType'
| 'battleMode'
| 'dBattleScratch'
| 'lr2ExtraMode'
| 'lr2ExtraLevel'
| 'extraNoteDepth'
| 'extraNoteScratch'
| 'scrollSection'
| 'scrollRate'
| 'scrollModifier'
| 'mineModifier'
| 'gaugeAutoShift'
| 'bmsonTotalMode'
| 'bottomShiftableGauge'
| 'autoScratch1P'
| 'autoScratch2P'
>,
chartEntry: Pick<LibraryChart, 'chart' | 'chartPath'> | undefined,
view: ReplayViewState,
effectivePlayerHiSpeed?: number,
effectiveRandomSeeds?: { p1?: string; p2?: string },
): ReplayRecordingOptions {
const effectiveOptions: ReplayOptionState = {};
for (const [key, value] of Object.entries({
autoPlay: options.autoPlay,
hiSpeed: effectivePlayerHiSpeed ?? options.initialHiSpeed,
hsFix:
options.floatingHiSpeed || options.classicHiSpeed || (options.normalHiSpeedLevel ?? 0) > 0
? 'OFF'
: options.hsFix,
classicHiSpeed: options.classicHiSpeed,
floatingHiSpeed: options.floatingHiSpeed,
fixedDisplayArea: options.fixedDisplayArea,
...(options.floatingHiSpeed ? { floatingHiSpeedProfile: 'AC32-measured' } : {}),
normalHiSpeedLevel: options.classicHiSpeed ? 0 : options.normalHiSpeedLevel,
...(!options.classicHiSpeed && (options.normalHiSpeedLevel ?? 0) > 0
? { normalHiSpeedProfile: 'AC32-measured' }
: {}),
bpmGuide: options.bpmGuide,
judgeTimingMs: options.judgeTimingMs,
judgeTimingAutoAdjust: options.judgeTimingAutoAdjust,
visualConstant: options.visualConstant,
noteDurationMs: options.noteDurationMs,
lift1P: options.lift1P,
lift2P: options.lift2P,
liftOn1P: options.liftOn1P,
liftOn2P: options.liftOn2P,
constantFadeMs: options.constantFadeMs,
hiddenSudden1P: options.hiddenSudden1P,
hiddenSudden2P: options.hiddenSudden2P,
shutter: options.shutter,
laneCover: options.laneCover,
gauge1P: options.gauge,
random1P: options.random1P,
randomSeed1P: effectiveRandomSeeds?.p1 ?? options.randomSeed1P,
randomSeed2P: effectiveRandomSeeds?.p2 ?? options.randomSeed2P,
randomFixedLane1P: options.randomFixedLane1P,
randomFixedLane2P: options.randomFixedLane2P,
lr2RandomScratch1P: options.lr2RandomScratch1P,
lr2RandomScratch2P: options.lr2RandomScratch2P,
hRandomThresholdBPM:
options.hRandomThresholdBPM === undefined ? undefined : clampHRandomThresholdBPM(options.hRandomThresholdBPM),
random2P: options.random2P,
dpFlip: options.dpFlip,
judgeAlgorithm: options.judgeAlgorithm,
customJudge: options.customJudge === undefined ? undefined : true,
customJudgeKeyPerfectGreat: options.customJudge?.keyPerfectGreat,
customJudgeKeyGreat: options.customJudge?.keyGreat,
customJudgeKeyGood: options.customJudge?.keyGood,
customJudgeScratchPerfectGreat: options.customJudge?.scratchPerfectGreat,
customJudgeScratchGreat: options.customJudge?.scratchGreat,
customJudgeScratchGood: options.customJudge?.scratchGood,
customJudgeLongNoteMargin: options.customJudge?.longNoteMargin,
longNoteModifier: options.longNoteModifier,
longNoteRate: options.longNoteRate,
sevenToNinePattern: options.sevenToNinePattern,
sevenToNineType: options.sevenToNineType,
battleMode: options.battleMode,
dBattleScratch: options.dBattleScratch,
lr2ExtraMode: options.lr2ExtraMode,
lr2ExtraLevel: options.lr2ExtraLevel,
extraNoteDepth: options.extraNoteDepth,
extraNoteScratch: options.extraNoteScratch,
mineModifier: resolvePlayerMineModifier(options),
scrollModifier: options.scrollModifier,
scrollSection: options.scrollSection,
scrollRate: options.scrollRate,
gaugeAutoShift: options.gaugeAutoShift,
bmsonTotalMode: options.bmsonTotalMode,
bottomShiftableGauge: options.bottomShiftableGauge,
autoScratch1P: options.autoScratch1P,
autoScratch2P: options.autoScratch2P,
})) {
if (value !== undefined) effectiveOptions[key] = value;
}
return {
gauge: options.gauge,
options: { ...options.recordingOptionState, ...effectiveOptions },
...(options.chartSHA256 !== undefined ? { chartSHA256: options.chartSHA256 } : {}),
...(options.chartMD5 !== undefined ? { chartMD5: options.chartMD5 } : {}),
...(options.replayRecorder !== undefined ? { recorder: options.replayRecorder } : {}),
...(options.replayDeviceNames === true ? { includeDeviceNames: true } : {}),
...(chartEntry !== undefined ? { sourceChart: chartEntry.chart } : {}),
...(options.random1P !== undefined || options.random2P !== undefined
? {
randomLane: {
...(options.random1P !== undefined ? { p1: options.random1P } : {}),
...(options.random2P !== undefined ? { p2: options.random2P } : {}),
},
}
: {}),
...(options.dpFlip !== undefined ? { dpFlip: options.dpFlip } : {}),
view,
...(chartEntry?.chartPath !== undefined ? { native: { chartPath: chartEntry.chartPath } } : {}),
};
}
export function launchSharedEngine(ctx: PlayEngineContext): void {
Iif (ctx.disposed || ctx.sharedEnginePromise) return;
if (!ctx.chart || !ctx.audioContext || !ctx.audioBus) return;
// Build the chart the engine sees from the view's already-shuffled notes / mines / invisibles. The view's
// `applyRandomMode` mutates `runtimeNote.channel` (NOT `event.channel`), so without this remap the engine
// would extract notes from `chart.events` with their ORIGINAL pre-shuffle channels — its judge would target
// a different lane than the visual the player is reading, so RANDOM / MIRROR / S-RANDOM / SCATTER would
// make the chart unplayable. Rebuilding `events` with the post-shuffle channel keeps the engine and the
// renderer on the same shuffle.
const chart = buildSharedEngineChart(ctx);
const callbacks: WebUIRuntimeCallbacks = {
onFrame: (frame) => applyEngineFrame(ctx, frame),
onCommand: (command) => applyEngineCommand(ctx, command),
onJudgeCombo: (state) => applyEngineJudgeCombo(ctx, state),
onSignalsReady: (signals) => {
ctx.sharedEngineSignals = signals;
},
};
ctx.engineFrame = undefined;
applyPlayEvent(ctx, { type: 'chart-hi-speed' });
// A replay shows its recorded view along the playhead; a recorded replays its own from the start.
ctx.replayViewCoalescer = undefined;
ctx.replayViewCursor =
ctx.options.replay !== undefined
? new ReplayViewCursor(ctx.options.replay.play.view, ctx.options.replay.play.viewChanges)
: undefined;
ctx.sharedEnginePromise = runEngineDriver({
chart,
audio: {
audioContext: ctx.audioContext,
audioBus: ctx.audioBus,
decodedSamples: ctx.decodedSamples,
wavCmdVolumeMultipliers: ctx.wavCmdVolumeMultipliers,
bmsonSlicePlayback: ctx.bmsonSlicePlayback,
// The engine plays through its own session: a stitched NONSTOP chart's stage rates (FIT) and fades go with it.
...(ctx.nonstopMixer !== undefined ? { stageMixer: ctx.nonstopMixer } : {}),
playbackRate: samplePlaybackRate(ctx),
playbackRateFollowsTempo: supportsLiveTempoChange(ctx.options.tempoPitchMode ?? 'pitch'),
tempo: ctx.playbackTempo ?? ctx.tempo,
// A looping PRACTICE play fades in and crossfades back to its start each time round (#949).
...(!ctx.options.replay && ctx.options.practiceRange !== undefined
? { seekCrossfadeSeconds: PRACTICE_LOOP_CROSSFADE_SECONDS }
: {}),
},
mode: ctx.options.autoPlay ? 'auto' : 'manual',
ui: callbacks,
onInputSignalsReady: (signals) => {
ctx.sharedEngineInputSignals = signals.inputSignals;
ctx.sharedEngineInterrupt = undefined;
ctx.sharedEngineChartSettled = false;
// A tempo asked for while the engine was starting.
ctx.liveTempo.attach(signals.inputSignals);
if (ctx.paused) signals.inputSignals.pushCommand({ kind: 'toggle-pause' });
if (ctx.engineStartRequested) signals.inputSignals.pushCommand({ kind: 'start' });
},
// Listen on the window (default) so the runtime sees keyboard events regardless of which DOM element
// currently holds focus — matches the legacy `window.addEventListener('keydown', ...)` behaviour. The
// canvas's `tabIndex = 0` only makes it focusable, not auto-focused, so a canvas-scoped listener would
// only fire after the user clicked the canvas first.
shouldSkipKey: (event) => shouldSkipPlayKey(event, resolveLaneInputTokens(ctx.preparedChart?.laneBindings)),
engineOptions: {
// The draws `resolvedChart` was resolved with; the engine plays that chart as is and names them in its summary.
...(ctx.randomDraws !== undefined ? { randomDraws: ctx.randomDraws } : {}),
// Prepare during the skin intro; chart time and audio wait for its PLAY START command.
leadInMs: 0,
waitForStart: true,
// The play tempo: the engine's clock runs `tempo` times as fast and judges in chart time. A change during the
// play comes back through `onTempoChange`, at the chart second the engine took it.
speed: ctx.tempo,
onTempoChange: (change) => followTempoChange(ctx, change),
// Only the selected side's scratch lanes are assisted by the engine.
autoScratch: ctx.options.autoScratch1P === true || ctx.options.autoScratch2P === true,
bottomShiftableGauge: ctx.options.replay?.play.bottomShiftableGauge ?? ctx.options.bottomShiftableGauge,
gaugeAutoShift:
ctx.options.replay !== undefined ? ctx.options.replay.play.gaugeAutoShift : ctx.options.gaugeAutoShift,
judgeAlgorithm: ctx.options.replay?.play.judgeAlgorithm ?? ctx.options.judgeAlgorithm,
bmsonTotalMode: ctx.options.replay
? (ctx.options.replay.play.bmsonTotalMode ?? 'beatoraja')
: ctx.options.bmsonTotalMode,
customJudge: ctx.options.replay ? ctx.options.replay.play.customJudge : ctx.options.customJudge,
lr2Gambol: ctx.options.replay ? ctx.options.replay.play.lr2Gambol : ctx.options.lr2Gambol,
judgeTimingAutoAdjust: ctx.options.replay === undefined ? (ctx.options.judgeTimingAutoAdjust ?? 'OFF') : 'OFF',
judgeTimingMs: ctx.options.judgeTimingMs,
battleMode: ctx.options.replay
? (ctx.options.replay.play.battleMode ?? 'OFF')
: ctx.options.fixedArrangement
? (ctx.options.fixedArrangement.play.battleMode ?? 'OFF')
: ctx.options.battleMode,
// A PRACTICE play loops its range in place until ESC (#949): the engine goes back to the start where the play would
// end, and the scene takes it as a seek, keeping its samples, BGA and skin.
practice:
ctx.options.replay || ctx.options.practiceRange === undefined
? undefined
: { ...ctx.options.practiceRange, loop: true },
dBattleScratch: ctx.options.replay
? (ctx.options.replay.play.dBattleScratch ?? false)
: ctx.options.fixedArrangement
? (ctx.options.fixedArrangement.play.dBattleScratch ?? false)
: (ctx.preparedChart?.dBattleScratch ?? ctx.options.dBattleScratch),
lr2ExtraMode: ctx.options.replay
? (ctx.options.replay.play.lr2ExtraMode ?? false)
: ctx.options.fixedArrangement
? (ctx.options.fixedArrangement.play.lr2ExtraMode ?? false)
: ctx.options.lr2ExtraMode,
lr2ExtraLevel: ctx.options.replay
? (ctx.options.replay.play.lr2ExtraLevel ?? 0)
: ctx.options.fixedArrangement
? (ctx.options.fixedArrangement.play.lr2ExtraLevel ?? 0)
: ctx.options.lr2ExtraLevel,
extraNoteDepth: ctx.options.replay
? (ctx.options.replay.play.extraNoteDepth ?? 0)
: ctx.options.fixedArrangement
? (ctx.options.fixedArrangement.play.extraNoteDepth ?? 0)
: ctx.options.extraNoteDepth,
extraNoteScratch: ctx.options.replay
? (ctx.options.replay.play.extraNoteScratch ?? false)
: ctx.options.fixedArrangement
? (ctx.options.fixedArrangement.play.extraNoteScratch ?? false)
: ctx.options.extraNoteScratch,
longNoteRate:
ctx.options.replay?.play.longNoteRate ??
ctx.options.fixedArrangement?.play.longNoteRate ??
ctx.options.longNoteRate,
longNoteModifier:
ctx.options.replay?.play.longNoteModifier ??
ctx.options.fixedArrangement?.play.longNoteModifier ??
ctx.options.longNoteModifier,
scrollSection:
ctx.options.replay?.play.scrollSection ??
ctx.options.fixedArrangement?.play.scrollSection ??
ctx.options.scrollSection,
scrollRate:
ctx.options.replay?.play.scrollRate ?? ctx.options.fixedArrangement?.play.scrollRate ?? ctx.options.scrollRate,
scrollModifier:
ctx.options.replay?.play.scrollModifier ??
ctx.options.fixedArrangement?.play.scrollModifier ??
ctx.options.scrollModifier,
mineModifier:
ctx.options.replay?.play.mineModifier ??
ctx.options.fixedArrangement?.play.mineModifier ??
ctx.chartPlay?.selection.mineModifier ??
resolvePlayerMineModifier(ctx.options),
// The chart's gameplay program (#844): a replay's recorded one, or none for an older replay without it.
chartPlayOptionProgram: ctx.options.replay
? (ctx.options.replay.chart.chartPlayOptions ?? { version: 1 as const, options: {} })
: ctx.chartPlay?.program,
autoScratch1P: ctx.options.autoScratch1P === true,
autoScratch2P: ctx.options.autoScratch2P === true,
anyKey: ctx.options.replay ? ctx.options.replay.play.anyKey === true : ctx.options.anyKey === true,
// Hand the engine the same `PreparedPlaybackChartData` instance the renderer is already pointing at
// (`this.notes` / `this.mineNotes` / `this.invisibleNotes` are references into this bundle). The
// engine's `autoPlay` / `manualPlay` use it verbatim instead of running their own
// `preparePlaybackChartData` pass, so the renderer's parallel arrays and the engine's playable note
// arrays are guaranteed to be the same instances — no `inferBmsLNTypeWhenMissing` flag to keep in
// sync, no `laneModeExtension` to forward separately (the bundle already encodes the lane bindings
// computed under the right extension), no `bms.controlFlow` re-resolve to deduplicate, no
// `random1P: 'OFF'` truthy-check to fix. Every "view extracted differently than engine" bug is
// structurally impossible because there is only one extract.
preparedChart: ctx.preparedChart,
sevenToNinePattern: ctx.preparedChart?.sevenToNinePattern,
sevenToNineType: ctx.preparedChart?.sevenToNineType,
hRandomThresholdBPM: ctx.preparedChart?.sevenToNineThresholdBPM,
// The gauge is part of the judging setup, not just a skin colour: the engine resolves it against the active
// ruleset's gauge line-up and runs its curve, so HARD really drains and DEATH really ends the run.
// `resolveSelectedGauge` prefers the replay's recorded pick, so a played-back log runs its original gauge.
gauge: ctx.resolveSelectedGauge(),
...(ctx.options.replay !== undefined
? ctx.options.replay.play.gauge2P !== undefined
? { gauge2P: ctx.options.replay.play.gauge2P }
: {}
: ctx.options.gauge2P !== undefined
? { gauge2P: ctx.options.gauge2P }
: {}),
...(ctx.options.replay === undefined && ctx.options.course !== undefined ? { course: ctx.options.course } : {}),
keyBoundBGACues: ctx.options.nonstop?.keyBoundBGA,
bmsonBGACues: ctx.options.nonstop?.bmsonBGA,
...(ctx.options.replay
? {
chartOptionProgram: ctx.options.replay.chart.chartOptions ?? { version: 1 as const, initial: {}, cues: [] },
}
: {
chartOptionProgram: ctx.options.nonstop?.chartOptions,
chartOptionDiagnostics: [
...(ctx.options.nonstop?.chartOptionDiagnostics ?? []),
...(ctx.chartPlay?.diagnostics ?? []),
],
}),
chartTextStages: ctx.options.nonstop?.chartText,
// Each stitched NONSTOP stage's mines explode with its own #WAV00.
landmineSampleKeys: ctx.options.nonstop?.landmineSampleKeys,
// A stitched NONSTOP chart's later stages take over the judge rank (a replay restores the recorded changes).
...(ctx.options.replay === undefined && ctx.options.nonstop !== undefined
? { stageRanks: ctx.options.nonstop.stageRanks }
: {}),
...(ctx.options.replay?.chart.judgeRank.stageRanks !== undefined
? {
stageRanks: ctx.options.replay.chart.judgeRank.stageRanks.map((change) => ({
...change,
seconds: change.timeUs / 1_000_000,
})),
}
: {}),
// The engine reports the ruleset after applying the judge ruleset, the debug window override, and (for a
// replay) the recorded settings below, so the windows skins see are the ones the run is judged with.
onRulesetResolved: (info) => applyResolvedRuleset(ctx, info),
signal: (ctx.sharedEngineAbortController = new AbortController()).signal,
// Keys stay live after the last note until the fade-out is over; the outro starts when the chart settles.
holdAfterChart: {
signal: (ctx.chartEndHold = new AbortController()).signal,
onSettled: () => {
ctx.sharedEngineChartSettled = true;
drainSharedEngineSignals(ctx);
handleSharedEngineChartFinished(ctx, undefined, true);
},
},
...(ctx.options.replay !== undefined
? {
// Replay playback — feed the recorded input stream; the engine ignores live lane input, and the run
// records no new replay (replaying a replay would only duplicate the file). Auto scratch, the
// judge ruleset, the debug judge-window override, the end-on-failure flag and the course stage are
// restored from the log so the judging setup matches the recording.
replayInputs: ctx.options.replay.inputs,
replayCompletedAtReversal: ctx.options.replay.play.completedAtReversal,
replayReversalActivationUs: ctx.options.replay.chart.reversalActivationUs,
replayInputSources: ctx.options.replay.play.inputSources,
// The tempo changes of the play, followed as the replay reaches them (`resolveReplayTempo`).
...(ctx.replayTempo !== undefined && ctx.replayTempo.changes.length > 0
? { replayTempoChanges: ctx.replayTempo.changes }
: {}),
// A play given up with ESC replays up to where it was given up, as its re-simulations do.
...(ctx.options.replay.play.aborted === true && ctx.options.replay.play.abortedAtUs !== undefined
? {
replayAbortedAtUs: ctx.options.replay.play.abortedAtUs,
replayExitedReversal: ctx.options.replay.play.exitedReversal,
}
: {}),
autoScratch: ctx.options.replay.play.autoScratch,
autoScratch1P: ctx.options.replay.play.autoScratch1P,
autoScratch2P: ctx.options.replay.play.autoScratch2P,
judgeRuleset: ctx.options.replay.play.judgeRuleset ?? 'lr2',
// Logs from before plays ended on failure kept judging to the end; replay them that way.
endOnFailure: ctx.options.replay.play.endOnFailure === true,
...(ctx.options.replay.play.course !== undefined ? { course: ctx.options.replay.play.course } : {}),
...(ctx.options.replay.play.judgeWindowOverrideMs !== undefined
? { judgeWindowMs: ctx.options.replay.play.judgeWindowOverrideMs }
: {}),
}
: {
...(ctx.options.judgeRuleset !== undefined ? { judgeRuleset: ctx.options.judgeRuleset } : {}),
// Replay recording: the engine snapshots the resolved (post-shuffle) chart and the raw input
// replay, then hands the assembled log here right before the play promise settles. The host reads it
// back through `getResultData().replay` for the result screen's auto-save.
// What the player sees at the start; the changes during the play go in as `view` commands. A play that
// seeks records none: the engine discards its recorder at the first seek.
// A chart's RANDOM / ADDMINE (#844) replaced the player's lane and mine options: record what was played.
// The player's own nanasigroove options are recorded apart from the chart's (#1243).
recordReplay: {
...resolveReplayRecording(
{ ...ctx.options, ...ctx.chartPlay?.selection } as typeof ctx.options,
ctx.chart,
startReplayView(ctx),
ctx.playState.view.hiSpeed.player,
ctx.randomSeeds,
),
...(ctx.chartPlay !== undefined && Object.keys(ctx.chartPlay.player).length > 0
? { playerChartPlayOptions: ctx.chartPlay.player }
: {}),
},
onReplayRecorded: ctx.practice
? undefined
: (replay: BeMusicReplay) => {
ctx.replay = replay;
const waiters = ctx.replayWaiters.splice(0);
for (const resolve of waiters) resolve(replay);
},
}),
},
})
.then((summary) => {
log.info('shared engine driver finished');
drainSharedEngineSignals(ctx);
settleSeekWaiters(ctx);
applyFinalSummary(ctx, summary);
// A play the view interrupted (ESC / F5 / teardown) has already handed the host over; it does not finish.
Iif (ctx.sharedEngineInterrupt !== undefined) return;
// A held play ends when its outro lets go; one that ended on its own (a failed gauge, a replay stopping where its
// log was given up) starts it now.
const waiting = ctx.onHeldPlayEnded;
Iif (waiting !== undefined) {
ctx.onHeldPlayEnded = undefined;
waiting(summary);
return;
}
handleSharedEngineChartFinished(ctx, summary);
// A reversed stage has already reached its exit. Its remaining chart time is unreachable.
Eif (hasReachedReversal(ctx.engineFrame)) ctx.chartEndFade?.();
})
.catch((error: unknown) => {
settleSeekWaiters(ctx);
// The view's own ESC / F5 handler (`handleSharedEngineExitKey`) drives the LR2 fadeout animation, calls
// the host's `onExit` / `onRestart` from the fade-completion callback, and then aborts the engine's
// signal — which is what produced this rejection. The view is already disposing (or about to), so the
// catch handler short-circuits to avoid double-firing the host transition. Same logic for any rejection
// landing after `dispose` has flipped `disposed` (e.g. parent component teardown without ESC).
if (ctx.disposed) return;
if (ctx.sharedEngineAbortController?.signal.aborted) return;
if (error instanceof PlayerInterruptedError) {
if (error.reason === 'escape') {
beginExitSequence(ctx, () => ctx.options.onExit?.());
} else if (error.reason === 'restart') {
ctx.options.onRestart?.();
}
return;
}
log.warn('shared engine driver threw', error);
});
}
/**
* Pulls the latest frame snapshot + queued commands + judge-combo state from the engine's signal buses. Called
* from {@link tick} when shared-engine mode is on; the per-call callbacks were registered at engine launch.
*/
export function drainSharedEngineSignals(ctx: PlayEngineContext): void {
if (!ctx.sharedEngineSignals) return;
const result = drainWebUISignals(
ctx.sharedEngineSignals.uiSignals,
{
onFrame: (frame) => applyEngineFrame(ctx, frame),
onCommand: (command) => applyEngineCommand(ctx, command),
onJudgeCombo: (state) => applyEngineJudgeCombo(ctx, state),
},
{
stateSignals: ctx.sharedEngineSignals.stateSignals,
lastJudgeComboTick: ctx.sharedEngineLastJudgeComboTick,
},
);
ctx.sharedEngineLastJudgeComboTick = result.lastJudgeComboTick;
}
/**
* Mirrors the engine's per-frame snapshot into the view's score / gauge / fast-slow fields so the LR2 NUMBER
* elements + score-graph keep rendering from the existing source paths — they don't need to know the engine
* replaced the in-tree judge ladder. The summary's optional `gauge` payload populates `this.gaugeState` so the
* skin's `#DST_GAUGE` chrome animates against the same value the engine considers authoritative. The frame is also
* what the chart's clock runs on from (`PlaySession.currentSeconds`); one of a seek generation older than the last
* `seek` command is stale and dropped.
*/
function applyEngineFrame(ctx: PlayEngineContext, frame: Readonly<PlayerUIFramePayload>): void {
if ((frame.generation ?? 0) < ctx.seekGeneration) return;
// The signal bus reuses its frame object. Keep the applied clock metadata owned by this
// session, so publishing a seek cannot move time before its score snapshot is drained.
ctx.engineFrame = { ...frame };
applyPlayEvent(ctx, { type: 'chart-hi-speed', value: frame.chartOptions?.hiSpeed });
applyEngineSummary(ctx, frame.summary);
Iif (frame.summary2P !== undefined) applySecondPlayerSummary(ctx, frame.summary2P);
ctx.chartEndGate?.(frame.currentSeconds);
}
function applyEngineSummary(ctx: PlayEngineContext, summary: Readonly<PlayerSummary>): void {
// `summary.total` is the engine's authoritative scorable-note count (`scorableNotes.length`, which excludes
// Free-Zone channels). The view's own initial `score.total` is computed independently in `prepareChart` from
// `notes.filter(isPlayableInputChannel).length`, which DOES include Free-Zone — so on charts that use
// channel `17` / `27` as Free-Zone the two diverge by the Free-Zone count. That mismatch makes
// `maybeFireFullCombo`'s `combo === score.total` predicate unreachable (combo can only ever climb to
// the engine's smaller scorable count) and the FC presentation never fires. Sync `score.total` to the
// engine's value here so the view always agrees with the authority on the scorable population.
ctx.score.total = summary.total;
if (summary.fullCombo !== undefined) ctx.score.fullCombo = summary.fullCombo;
else delete ctx.score.fullCombo;
ctx.score.perfect = summary.perfect;
ctx.score.great = summary.great;
ctx.score.good = summary.good;
ctx.score.bad = summary.bad;
ctx.score.emptyPoor = summary.emptyPoor;
if (summary.minePoor !== undefined) ctx.score.minePoor = summary.minePoor;
else delete ctx.score.minePoor;
// `this.score` is the DISPLAY copy: under LR2 the POOR counter folds empty POORs in, matching the real thing
// (OpenLR2 `ApplyJudgeNote` increments `playerstat.poor` for the empty-POOR branch, and LR2 exposes no
// separate stat). Every LR2 read-out — the POOR row, BP, the per-judge rates, the skin's num 84 / 89 / 114 —
// goes through this object, so folding here keeps them all consistent. `summary.poor` itself stays split, and the
// result carries it beside the folded count (`judgedPoor`, `getResultData`) for a record's BP.
ctx.score.poor = resolveDisplayedPoor(summary, ctx.resolveEmptyPoorCountsInPoorDisplay());
ctx.score.exScore = summary.exScore;
ctx.score.score = summary.score;
ctx.fastCount = summary.fast;
ctx.slowCount = summary.slow;
// The play state's gauge and EX score, the graphs' next samples. The graphs run over the scene's own chart length.
applyPlayEvent(ctx, { type: 'frame', frame: { totalSeconds: ctx.resolveChartDurationSeconds(), summary } });
if (summary.gauge) {
const previous = ctx.gaugeState.current;
ctx.gaugeState = { ...summary.gauge };
// Gauge-rise / gauge-max theme feedback (LR2 timers 42 / 44). Mirrors what the legacy `applyGaugeDelta` did —
// raise on every transition so authored skin elements (rise sparkle, max-glow overlay) animate. We compare against the previous frame's value rather than against an "EMPTY_POOR-
// sized delta" because the engine drives gauge updates monotonically through `summary.gauge.current`,
// not through judge deltas.
const next = summary.gauge.current;
if (next !== previous) {
ctx.theme.moment({ kind: 'gauge', previous, next, at: ctx.playClock() });
// Latch the first rise above the starting value and the first time the gauge tops out.
const moments = ctx.playMoments;
if (next > summary.gauge.initial) moments.gaugeRiseAtMs ??= ctx.playClock();
Iif (next >= summary.gauge.max) moments.gaugeMaxAtMs ??= ctx.playClock();
}
Iif (summary.gauge.failedMidPlay === true) ctx.playMoments.failedAtMs ??= ctx.playClock();
}
// `poor` excludes empty POORs, so this counts judged notes only.
const judged = summary.perfect + summary.great + summary.good + summary.bad + summary.poor;
if (summary.total > 0 && judged >= summary.total) ctx.playMoments.allNotesDoneAtMs ??= ctx.playClock();
// Note: the per-note `judged` flag is no longer synced here. The renderer's `this.notes` /
// `this.mineNotes` are references into the same `PreparedPlaybackChartData` instance the engine is
// judging against (handed in via `engineOptions.preparedChart`), so when the engine sets
// `note.judged = true` the renderer sees it on the next paint with no copy step. The fragile
// index-based sync this used to perform was the source of the Phase-4c HIDE-on-judge dropout, the
// mid-chart full-combo cue, and the AUTO PLAY < 200_000 score regressions — every one of them was
// really "engine extract produced a different array than view extract." With the shared instance
// there is no second extract.
}
/**
* The play's final summary, as the engine hands it over: its scores and gauge, and its final judgment counts
* (`finish`): a full combo makes the max combo every note, for any play, even where the engine published no combo for
* some of them (an autoplayed chord).
*/
export function applyFinalSummary(ctx: PlayEngineContext, summary: Readonly<PlayerSummary>): void {
applyEngineSummary(ctx, summary);
applyPlayEvent(ctx, { type: 'finish', summary });
const second = ctx.secondPlayer;
Iif (second !== undefined && summary.secondPlayer !== undefined) {
applySecondPlayerSummary(ctx, summary.secondPlayer);
second.playState = reducePlayState(second.playState, { type: 'finish', summary: summary.secondPlayer });
}
}
/** The 2P player's scores and play state from its summary (a two-player play), as the 1P player's are kept. */
function applySecondPlayerSummary(ctx: PlayEngineContext, summary: Readonly<PlayerSummary>): void {
const second = ctx.secondPlayer;
if (second === undefined) return;
second.score = {
...second.score,
total: summary.total,
perfect: summary.perfect,
great: summary.great,
good: summary.good,
bad: summary.bad,
emptyPoor: summary.emptyPoor,
poor: resolveDisplayedPoor(summary, ctx.resolveEmptyPoorCountsInPoorDisplay()),
exScore: summary.exScore,
score: summary.score,
};
if (summary.gauge !== undefined) second.gauge = { ...summary.gauge };
second.playState = reducePlayState(second.playState, {
type: 'frame',
frame: { totalSeconds: ctx.resolveChartDurationSeconds(), summary },
});
}
/**
* Maps an engine UI command to the matching view-side visual effect. The visual-effect helpers
* (`flashKeyBeam`, `startKeyBeam`, `releaseKeyBeam`, `triggerBomb`) are kept in shared-engine mode because
* they own the per-lane book-keeping the lane beams animate against — only the judge / sample-trigger code
* paths above were removed. Every command is then forwarded to the theme as a `lane-command` event (the LR2 family
* drives its LN-hold timers and turntable from it).
*/
function applyEngineCommand(ctx: PlayEngineContext, command: PlayerUICommand): void {
if (command.kind === 'seek') {
applyEngineSeek(ctx, command);
return;
}
switch (command.kind) {
case 'judge-timing':
// The engine already recorded the accepted update at its judgment timestamp.
(ctx.options as PlaySessionOptions).judgeTimingMs = command.value;
ctx.options.onJudgeTimingChange?.(command.value);
return;
case 'flash-lane':
noteImpulse(ctx, command.channel);
flashKeyBeam(ctx, command.channel);
break;
case 'scratch-spin': {
const atMs = ctx.playClock();
for (const channel of command.channels)
ctx.scratchInputs.set(channel, {
angle: resolveScratchAngle(0, ctx.scratchInputs.get(channel), atMs),
direction: command.spin === 'cw' ? 1 : -1,
atMs,
});
break;
}
case 'press-lane':
noteImpulse(ctx, command.channel);
ctx.pressedChannels.add(command.channel);
startKeyBeam(ctx, command.channel);
break;
case 'release-lane':
ctx.pressedChannels.delete(command.channel);
releaseKeyBeam(ctx, command.channel);
break;
case 'hold-lane-until-beat':
// Mark the lane as held for the LN's duration. Without this, a `flash-lane` command that arrives in
// the same tick (typical for the LN HEAD: autoplay emits BOTH `flash-lane` and `hold-lane-until-beat`
// on every LN start) schedules a `flashKeyBeam` setTimeout that calls `releaseKeyBeam` after
// `KEY_ON_FLASH_HOLD_MS` because `pressedChannels.has(channel)` is `false`, and the lane laser fades
// out ~150 ms into the LN even though the LN body is still scrolling. Adding the channel to
// `pressedChannels` here makes the auto-release skip path fire the same way it does for a real key
// press, and the laser stays lit for the full LN sustain. The matching `release-lane` (emitted by
// `drainPendingAutoLongNotes` / `drainPendingAutoScratchLongNotes` at the LN tail) deletes the
// channel and lets the lane laser fade out at the tail timing.
ctx.pressedChannels.add(command.channel);
startKeyBeam(ctx, command.channel);
// The engine's hold-until-beat command implicitly says "the LN is firing now"; bomb-flash on the head
// mirrors the legacy autoJudge behavior so the lane still blinks a positive-feedback explosion at LN
// start in autoplay.
triggerBomb(ctx, command.channel);
break;
case 'trigger-poor-bga': {
// Commands carry chart seconds, but the shared miss-image window runs on the pause-aware play clock.
ctx.lastPoorAt = ctx.playClock();
// An empty POOR (the engine fires `trigger-poor-bga` for real POOR judgments too, and for empty presses and
// misses past the window) moves the gauge without a judgment: the play state samples its graphs here. A real
// POOR that also publishes a judgment gets a second sample at almost the same progress.
const event = playEventFromUICommand(command);
Eif (event !== undefined) applyPlayEvent(ctx, event);
break;
}
case 'clear-poor-bga':
ctx.lastPoorAt = -Number.POSITIVE_INFINITY;
break;
case 'long-note-start':
ctx.longNotes.start(command.channel, command.beat, command.mode, ctx.playClock());
break;
case 'long-note-end':
ctx.longNotes.end(command.channel, command.outcome, ctx.playClock());
break;
case 'hell-charge':
ctx.longNotes.hellCharge(command.channel, command.beat, command.state);
break;
}
ctx.theme.moment({ kind: 'lane-command', command });
}
/**
* The engine sought: what the play showed of the old position is void — the lane effects, the held long notes, the
* plates, the POOR image and the latched moments ({@link PlayPresentation.resetPresentation}), the lead-out and the
* BGA videos — and the score, the gauge and the play state become the snapshot it rebuilt (the combo, the max combo,
* the graphs and the timings of the judgments before the second sought included). The long notes held across it come
* next, as the commands that follow. The view hears of it (`seek` moment), and the seeks waiting on it settle.
*/
function applyEngineSeek(ctx: PlayEngineContext, command: Extract<PlayerUICommand, { kind: 'seek' }>): void {
const { snapshot, seconds, generation } = command;
ctx.seekGeneration = generation;
if (
(ctx.options.autoPlay !== true && ctx.options.replay === undefined) ||
ctx.options.course !== undefined ||
ctx.options.nonstop !== undefined
)
ctx.practice = true;
ctx.resetPresentation();
// A play sought back out of its lead-out plays on: the engine settles the chart again.
if (ctx.chartEndTimeout !== undefined) {
window.clearTimeout(ctx.chartEndTimeout);
ctx.chartEndTimeout = undefined;
}
ctx.chartEndGate = undefined;
ctx.chartEndFade = undefined;
ctx.chartEnded = false;
ctx.sharedEngineChartSettled = false;
ctx.onHeldPlayEnded = undefined;
// The next BGA sync starts each video at the chart time sought.
ctx.pauseAllBGAVideos();
ctx.bgaActiveVideos = {};
applyEngineSummary(ctx, snapshot.summary);
ctx.displayedScore = ctx.score.score;
applyPlayEvent(ctx, { type: 'seek', snapshot, seconds, atMs: ctx.playClock(), tempo: ctx.tempo });
ctx.lastPoorAt = -Number.POSITIVE_INFINITY;
ctx.theme.moment({
kind: 'seek',
seconds,
gauge: ctx.playState.gauge,
judgments: snapshot.judgments,
at: ctx.playClock(),
});
updateReplayView(ctx, seconds, true);
resolveSeekWaiters(ctx, seconds);
}
/** Keeps the engine's long-note style and its judge windows (converted to ms) for the skin runtime. */
function applyResolvedRuleset(ctx: PlayEngineContext, info: PlayerRulesetInfo): void {
ctx.longNoteStyle = info.longNoteStyle;
ctx.judgeWindows = toBeMusicJudgeWindows(info.windows);
}
/**
* Drives every per-judge visual the LR2 skin animates against from the engine's stateSignals judge-combo bus:
*
* - The play state takes the judgment (`reducePlayState`): the running combo `#SRC_NUMBER,st=70` (NOWCOMBO) renders,
* the max combo and course combo, the timed judgment with its early / late count, and a sample of the graphs.
* - `publishJudge` does the rest of the legacy per-judge work — it stamps timer 46 (1P) or 47 (2P) so the
* `#DST_NOWJUDGE` / `#DST_NOWCOMBO` keyframe chains animate from time=0 on every hit (without this, the
* plate stays invisible because the timer's playhead is parked at scene start), latches `lastJudge` /
* `lastJudgeUntil` so the fallback `#SRC_TEXT` paints the verdict, snapshots `judgeSideState[side]` for DP
* per-side rendering, swaps to the POOR BGA on POOR/BAD, and fires `maybeFireFullCombo` for timers 48/49.
* - On PERFECT / GREAT we additionally fire the lane bomb (the engine's `flash-lane` command is severity-
* independent so we can't piggy-back on it; the bomb is a positive-feedback cue specifically for clean
* hits). The legacy `commitFinalJudge` ran the same gate.
*/
function applyEngineJudgeCombo(ctx: PlayEngineContext, state: Readonly<PlayerJudgeComboSignalState>): void {
Iif (state.judge === 'READY') return;
const judgeKind = state.judge as JudgmentKind;
const seconds = ctx.currentSeconds();
// Autoplay hits, mine BADs, and missed notes carry no `deltaMs`: the play state keeps no timing for them.
const second = ctx.secondPlayer;
Iif (second !== undefined && state.channel?.startsWith('2') === true) {
// A two-player play's 2P judgment is the 2P player's.
second.playState = reducePlayState(second.playState, {
type: 'judge',
judge: state,
seconds,
atMs: ctx.playClock(),
});
} else applyPlayEvent(ctx, { type: 'judge', judge: state, seconds, atMs: ctx.playClock() });
publishJudge(ctx, judgeKind, seconds, state);
if ((judgeKind === 'PERFECT' || judgeKind === 'GREAT') && state.channel) {
triggerBomb(ctx, state.channel, judgeKind);
}
}
/**
* Fires when the engine driver's `manualPlay` / `autoPlay` promise resolves cleanly (= chart played to its end
* without an interrupt). The host's `onChartFinished` callback receives a `PlayResult` snapshot and the theme's exit
* (its fade-out, or a failed play's stop) runs before the result-screen transition. The engine's resolution is the
* authoritative end-of-chart signal — the view's `currentSeconds` clock lags slightly behind the engine's
* own bookkeeping, so we don't gate on it.
*/
/**
* Starts the outro once the chart is over. `held` means the engine's play is still open (keys still live): the fade-out
* runs first, then the play is let go and the result waits for its final summary.
*/
function handleSharedEngineChartFinished(
ctx: PlayEngineContext,
finalSummary: Readonly<PlayerSummary> | undefined,
held = false,
): void {
Iif (ctx.disposed || ctx.chartEnded) return;
ctx.chartEnded = true;
ctx.playMoments.outroAtMs ??= ctx.playClock();
// The end-of-play policy every gameplay family shares (`resolvePlayEnding`): a lead-out after the last note, then the
// fade-out, or — for a play cut short by its gauge emptying — a stop at once (`beginExitSequence`).
const viewTiming = ctx.theme.timing();
const ending = resolvePlayEnding({
summary: finalSummary,
notes: ctx.notes,
timing: ctx.timingResolver,
currentSeconds: ctx.currentSeconds(),
tempo: ctx.tempo,
musicEndDelayMs: viewTiming.musicEndDelayMs,
finishMarginMs: viewTiming.finishMarginMs,
...(ctx.options.autoPlay ? { chartEndSeconds: ctx.resolveChartDurationSeconds() } : {}),
});
// Families without a music-end stage retain the two-measure lead-out. A family with that stage waits on the
// engine's chart clock, then holds its finish margin on the scene clock before fading.
const beginFade = () => {
ctx.chartEndFade = undefined;
ctx.chartEndTimeout = undefined;
Iif (ctx.disposed || ctx.exiting) return;
drainSharedEngineSignals(ctx);
if (finalSummary !== undefined) applyFinalSummary(ctx, finalSummary);
const finish = (): void => {
const result = ctx.getResultData();
Eif (ctx.options.onChartFinished && result) {
ctx.options.onChartFinished(result);
return;
}
ctx.options.onExit?.();
};
beginExitSequence(
ctx,
() => {
if (!held) {
finish();
return;
}
// The outro is over: let the held play end, and show the result with its final summary.
ctx.onHeldPlayEnded = () => {
Eif (!ctx.disposed) finish();
};
flushReplayView(ctx);
ctx.chartEndHold?.abort();
},
// A normal end lets its last keysounds ring through the fade-out; a failure fades them over its close.
{ failed: ending.failed, fadeAudio: false },
);
};
if (ending.musicEndAtSeconds !== undefined) {
const target = ending.musicEndAtSeconds;
const musicEnd = () => {
ctx.chartEndGate = undefined;
if (ctx.disposed || ctx.exiting || !ctx.chartEnded) return;
ctx.theme.moment({ kind: 'music-end', at: ctx.playClock() });
ctx.chartEndTimeout = window.setTimeout(beginFade, ending.finishMarginMs ?? 0);
};
Iif (held) {
// The one engine loop drives this gate: pause, tempo changes and seeks keep their chart-time meaning.
ctx.chartEndGate = (seconds) => {
if (seconds >= target) musicEnd();
};
ctx.chartEndGate(ctx.currentSeconds());
} else {
ctx.chartEndTimeout = window.setTimeout(musicEnd, Math.max(0, ending.leadOutMs - (ending.finishMarginMs ?? 0)));
}
} else {
ctx.chartEndTimeout = window.setTimeout(beginFade, ending.leadOutMs);
}
ctx.chartEndFade = () => {
Eif (ctx.chartEndTimeout !== undefined) window.clearTimeout(ctx.chartEndTimeout);
ctx.chartEndGate = undefined;
beginFade();
};
}
function hasReachedReversal(frame: Readonly<PlayerUIFramePayload> | undefined): boolean {
return frame?.reversalActivation !== undefined && frame.currentSeconds >= frame.reversalActivation.seconds;
}
/**
* Takes an ESC (and the other ways out that act as it) on a play whose notes are all judged: the play finishes instead
* of being given up, so its result shows, as beatoraja's `BMSPlayer.stopPlay` does (once the judged notes reach the
* chart's note count it moves to `STATE_FINISHED` and starts `TIMER_FADEOUT`; a second ESC while that fade-out or the
* failure close runs is ignored). What is left of the lead-out is skipped: the fade-out runs at once, the engine's hold
* is let go, and the play ends complete with its replay as usual. Returns `false` when the ESC is the host's to take
* (a looping PRACTICE play, one already handed over, or an ordinary play with notes still to judge). After a negative
* BPM reversal, ESC instead ends the stage through the judge so outstanding notes count towards the final result.
*/
export function finishPlayOnExit(ctx: PlayEngineContext): boolean {
Iif (ctx.disposed || ctx.sharedEngineInterrupt !== undefined) return false;
if (ctx.chartEnded) {
// The outro is under way: its fade-out (or a failed play's close) leads to the result, which ESC does not cut.
if (ctx.exiting) return true;
const fade = ctx.chartEndFade;
Iif (fade === undefined) return false;
fade();
return true;
}
if (ctx.exiting || ctx.sharedEngineInputSignals === undefined) return false;
// A looping PRACTICE play never reaches its end: ESC leaves it.
if (!ctx.options.replay && ctx.options.practiceRange !== undefined) return false;
if (hasReachedReversal(ctx.engineFrame)) {
// ESC finalizes the reversed stage in the engine, including the gauge and replay. Its
// completion handler owns the transition to the result screen.
flushReplayView(ctx);
ctx.sharedEngineInputSignals.pushCommand({ kind: 'interrupt', reason: 'escape' });
return true;
}
if (!isEveryNoteJudged(ctx.engineFrame)) return false;
handleSharedEngineChartFinished(ctx, undefined, true);
ctx.chartEndFade?.();
return true;
}
/** Stops the shared engine as the play is disposed (`PlaySession.dispose`) and lets go of its buses. */
export function stopSharedEngine(ctx: PlayEngineContext): void {
ctx.chartEndGate = undefined;
ctx.chartEndFade = undefined;
// Abort the shared engine driver BEFORE the audio session tear-down so engine ticks don't fire fresh
// sample triggers onto a session that's about to lose its bus references. The engine catches the abort via
// `throwIfAborted` and unwinds; our launchSharedEngine catch swallows the resulting `AbortError` because
// the host already initiated dispose.
// A play torn down mid-chart is interrupted too, so the engine stops instead of running on to the chart's end.
interruptSharedEngine(ctx, 'escape');
Iif (ctx.sharedEngineAbortController) {
try {
ctx.sharedEngineAbortController.abort();
} catch {
// Already aborted or unsupported environment — swallow.
}
ctx.sharedEngineAbortController = undefined;
}
ctx.sharedEngineSignals = undefined;
ctx.sharedEngineInputSignals = undefined;
ctx.liveTempo.attach(undefined);
ctx.sharedEnginePromise = undefined;
settleSeekWaiters(ctx);
}
|