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 | 2958x 2958x 126246x 126246x 107107x 19139x 2958x 19175x 2958x 2958x 30x 117x 67x 67x 30x 30x 30x 2958x 173390x 385077x 120523x 120523x 86887x 785x 86887x 58x 58x 86829x 86829x 88543x 88543x 86445x 86445x 58x 58x 58x 86387x 86387x 2679x 2679x 88601x 86886x 86886x 86625x 58x 58x 58x 58x 58x 162x 162x 857x 162x 30785x 9269x 9269x 9269x 5955x 5955x 78x 5878x 9269x 9269x 9269x 42x 28x 2x 9269x 2326x 170346x 163541x 163541x 332939x 332939x 332939x 171089x 171089x 171089x 36588x 36588x 1554x 171089x 169535x 169535x 169535x 332939x 84921x 84921x 84921x 84614x 89213x 89213x 89213x 304017x 304017x 304017x 304017x 89213x 86857x 86857x 89213x 214804x 214804x 214804x 214804x 214804x 163541x | import type { ReplayInputEvent } from '../replay/format.ts';
import { createPlugoutLaneSoundState } from './plugout-lane-sound.ts';
import { advanceJudgeState, finishJudgeState, nextTimedJudgmentUs } from './judge-core-advance.ts';
import { markScorableJudged, markLandmineJudged, markInvisibleJudged } from './judge-core-score.ts';
import { handleLanePress, handleLaneRelease, resolveAnyKeyPressChannels } from './judge-core-press.ts';
import { AnyKeyRoutes, anyKeyRouteKeys } from './any-key.ts';
import {
createInputSourceLookup,
inputSourceKey,
type IdentifiedInputSource,
type InputSource,
} from './input-source.ts';
import {
type JudgeCoreConfig,
type JudgeCoreEffect,
type JudgeCoreEffectSink,
type JudgeCoreInput,
type JudgeCoreScore,
type JudgeCoreState,
} from './judge-core-types.ts';
/**
* The deterministic judge core of manual play.
*
* Everything that decides a judgment — note selection, the ruleset windows, long-note holds and tails, the HCN gauge
* ticks, mines, auto-scratch, misses, combo, score and gauge — lives in a {@link JudgeCoreState} that only
* {@link runJudgeCoreStep} changes. A step takes one {@link JudgeCoreInput} stamped with chart time (and the
* millisecond domain the release margins run in) and reports what happened as {@link JudgeCoreEffect}s, in order.
* It takes key-state input only: lanes are down exactly between their `hold` and `release`. A driver whose input
* reports no key releases infers them before they reach the core (`play-key-inference.ts`).
* The core never reads a clock, never touches audio, the UI or the log, and never schedules anything: the same
* state and the same inputs always produce the same effects. Drivers own the rest:
*
* - live play (`manualPlay`) steps it from the playback clock once per frame and on every key event;
* - a replay steps it from one judgment instant to the next, merged with the recorded inputs ({@link advanceJudgeCoreReplay}),
* so its judgments do not depend on tick timing or `speed`.
*
* Effects are delivered synchronously through a sink, so a driver that reads the shared {@link JudgeCoreState.summary}
* while handling one (the no-TUI state logger does) sees it exactly as it was when the effect was produced.
*/
export * from './judge-core-types.ts';
export {
clampToReversal,
isJudgingClosed,
maxJudgeReachSeconds,
noteOwesTailMiss,
toJudgeClock,
} from './judge-core-score.ts';
export { resolveLandmineExplosionEvent, resolveLandmineSampleKey } from './judge-core-advance.ts';
export { hasPressCandidate, resolveAnyKeyPressChannels } from './judge-core-press.ts';
/** Builds the initial judge state of a play. */
export function createJudgeCoreState(config: JudgeCoreConfig, score: JudgeCoreScore): JudgeCoreState {
const scorableNoteSecondsByChannel = new Map<string, number[]>();
for (const note of config.scorableNotes) {
const noteTimes = scorableNoteSecondsByChannel.get(note.channel);
if (noteTimes) {
noteTimes.push(note.seconds);
} else {
scorableNoteSecondsByChannel.set(note.channel, [note.seconds]);
}
}
const state: JudgeCoreState = {
config,
summary: score.summary,
scoreTracker: score.scoreTracker,
applyGaugeJudge: score.applyGaugeJudge,
applyGaugeDelta: score.applyGaugeDelta,
applyGaugeTick: score.applyGaugeTick,
drainGaugeToZero: score.drainGaugeToZero,
combo: 0,
heldChannels: new Set(),
activeLongNotes: new Map(),
longNoteSettleMs: new Map(),
longHoldReleaseSeconds: new Map(),
laneHolds: new Map(),
anyKeyRoutes: new AnyKeyRoutes(),
advancedThroughSeconds: 0,
pendingAutoScratchLongNotes: [],
autoScratchNotes: config.autoScratch
? config.scorableNotes.filter((note) => (config.autoScratchChannels ?? config.scratchChannels).has(note.channel))
: [],
scorableNoteSecondsByChannel,
autoScratchCursor: 0,
scorableMissCursor: 0,
landmineExpireCursor: 0,
landminePhases: new Map(),
hcnPassing: new Map(),
invisibleExpireCursor: 0,
remainingScorableNotes: config.scorableNotes.length,
remainingLandmineNotes: config.landmineNotes.length,
remainingInvisibleNotes: config.invisibleNotes.length,
...(config.ruleset.laneSound === 'plugout' ? { plugoutLaneSounds: createPlugoutLaneSoundState() } : {}),
};
const from = config.practiceFromSeconds;
if (from !== undefined && Number.isFinite(from) && from > 0) {
for (const note of config.scorableNotes) {
if (note.seconds < from) {
markScorableJudged(state, note);
// Hide a skipped long note even when its tail lies after the practice start.
note.visibleUntilBeat = note.beat;
}
}
for (const note of config.landmineNotes) if (note.seconds < from) markLandmineJudged(state, note);
for (const note of config.invisibleNotes) Eif (note.seconds < from) markInvisibleJudged(state, note);
state.advancedThroughSeconds = from;
}
return state;
}
/**
* The keys an input is held and routed by: its source's key when it named one, else its tokens. A live play and a
* replay of it name the same sources, so their lane holds and ANY KEY routes agree.
*/
function inputKeys(tokens: readonly string[] | undefined, source: InputSource | undefined): readonly string[] {
return source === undefined ? (tokens ?? []) : [inputSourceKey(source)];
}
/** Applies one input to the state, delivering the effects to `emit` in the order they happen. */
export function runJudgeCoreStep(state: JudgeCoreState, input: JudgeCoreInput, emit: JudgeCoreEffectSink): void {
switch (input.kind) {
case 'advance':
advanceJudgeState(state, input.nowSec, input.nowMs, emit);
return;
case 'press':
if (input.spin !== undefined)
emit({ kind: 'ui', command: { kind: 'scratch-spin', channels: [...input.channels], spin: input.spin } });
if (state.config.anyKey === true) {
pressAnyKey(state, input, emit);
return;
}
handleLanePress(
state,
input.channels,
inputKeys(input.tokens, input.source),
input.nowMs,
input.nowSec,
emit,
input.channels,
input.source,
input.spin,
input.code,
);
return;
case 'hold':
holdLanes(
state,
state.config.anyKey === true
? state.anyKeyRoutes.routedLanes(anyKeyRouteKeys(inputKeys(input.tokens, input.source), input.channels))
: input.channels,
input.inferred === true,
emit,
);
return;
case 'release': {
const keyed = { ...input, tokens: inputKeys(input.tokens, input.source) };
if (state.config.anyKey === true) {
const keys = anyKeyRouteKeys(keyed.tokens, input.channels);
handleLaneRelease(
state,
{ ...keyed, channels: state.anyKeyRoutes.release(keys, input.channels) },
emit,
input.channels,
);
return;
}
handleLaneRelease(state, keyed, emit);
return;
}
case 'finish':
finishJudgeState(state, input.totalSeconds, emit);
return;
}
}
/** Holds `channels` down until their release; `inferred` holds show no lane cue. */
function holdLanes(
state: JudgeCoreState,
channels: ReadonlySet<string>,
inferred: boolean,
emit: JudgeCoreEffectSink,
): void {
for (const channel of channels) {
state.heldChannels.add(channel);
if (!inferred) {
emit({ kind: 'ui', command: { kind: 'press-lane', channel } });
}
}
}
/**
* An ANY KEY press (#751): routed to the lane of the nearest note in reach (`resolveAnyKeyPressChannels`), which its keys
* then hold down until they come up. The key's own `hold` may arrive before the press (a replay) or after it (live
* input), so the press holds the routed lane itself and a `hold` holds only lanes its keys were already routed to. The
* replay records the keys' own lanes, so a simulation routes the press again.
*/
function pressAnyKey(
state: JudgeCoreState,
input: Extract<JudgeCoreInput, { kind: 'press' }>,
emit: JudgeCoreEffectSink,
): void {
const lanes = resolveAnyKeyPressChannels(state, input.channels, input.nowSec);
const keys = inputKeys(input.tokens, input.source);
state.anyKeyRoutes.route(anyKeyRouteKeys(keys, input.channels), lanes);
holdLanes(state, lanes, false, emit);
handleLanePress(
state,
lanes,
keys,
input.nowMs,
input.nowSec,
emit,
input.channels,
input.source,
input.spin,
input.code,
);
}
/** {@link runJudgeCoreStep} collecting the effects into an array — `step(state, input) → effects`. */
export function stepJudgeCore(state: JudgeCoreState, input: JudgeCoreInput): JudgeCoreEffect[] {
const effects: JudgeCoreEffect[] = [];
runJudgeCoreStep(state, input, (effect) => {
effects.push(effect);
});
return effects;
}
/** True once nothing is left to judge: every note resolved and no long note or auto-scratch tail outstanding. */
export function isJudgeCoreSettled(state: JudgeCoreState): boolean {
return (
state.remainingScorableNotes === 0 &&
state.remainingLandmineNotes === 0 &&
state.remainingInvisibleNotes === 0 &&
state.pendingAutoScratchLongNotes.length === 0 &&
state.activeLongNotes.size === 0
);
}
/**
* Maps key tokens onto the lanes they press, dropping auto-played scratch lanes. Fills and returns `buffer`, which is
* cleared first — the result is only valid until the next call with the same buffer.
*/
export function resolveJudgeInputChannels(
config: Pick<JudgeCoreConfig, 'autoScratch' | 'scratchChannels' | 'autoScratchChannels'>,
inputTokenToChannels: ReadonlyMap<string, readonly string[]>,
buffer: Set<string>,
tokens: readonly string[],
additionalTokens?: readonly string[],
): Set<string> {
buffer.clear();
const collect = (list: readonly string[]): void => {
for (const token of list) {
const mapped = inputTokenToChannels.get(token);
if (!mapped) {
continue;
}
mapped.forEach((channel) => buffer.add(channel));
}
};
collect(tokens);
Iif (additionalTokens && additionalTokens.length > 0) {
collect(additionalTokens);
}
if (config.autoScratch) {
for (const channel of buffer) {
if ((config.autoScratchChannels ?? config.scratchChannels).has(channel)) {
buffer.delete(channel);
}
}
}
return buffer;
}
// ---- replay ---------------------------------------------------------------------------------------------------
/** A recorded input stream being re-driven through the core. */
export interface JudgeCoreReplay {
readonly events: readonly ReplayInputEvent[];
/** The source each event's id names (`play.inputSources`); `undefined` for an id the table does not hold. */
readonly sourceOf: (id: number | undefined) => InputSource | undefined;
cursor: number;
/** The chart second the judge state has been advanced to. */
judgedThroughSeconds: number;
/** The same instant in whole microseconds. */
judgedThroughUs: number;
}
/** Sorts a replay's inputs into replay order; `sources` is its `play.inputSources` table. */
export function createJudgeCoreReplay(
inputs: readonly ReplayInputEvent[],
sources?: readonly IdentifiedInputSource[],
): JudgeCoreReplay {
return {
events: [...inputs].sort((left, right) => left.timeUs - right.timeUs || left.seq - right.seq),
sourceOf: createInputSourceLookup(sources),
cursor: 0,
judgedThroughSeconds: Number.NEGATIVE_INFINITY,
judgedThroughUs: Number.NEGATIVE_INFINITY,
};
}
function processReplayEventsUntil(
state: JudgeCoreState,
replay: JudgeCoreReplay,
untilSec: number,
emit: JudgeCoreEffectSink,
): void {
const { config } = state;
while (replay.cursor < replay.events.length) {
const event = replay.events[replay.cursor]!;
const eventSec = event.timeUs / 1_000_000;
if (eventSec > untilSec) break;
replay.cursor += 1;
const channels = new Set(event.channels);
if (config.autoScratch) {
for (const channel of channels) {
if ((config.autoScratchChannels ?? config.scratchChannels).has(channel)) {
channels.delete(channel);
}
}
}
if (channels.size === 0) continue;
const eventMs = eventSec * config.judgeMsPerChartSecond;
const source = replay.sourceOf(event.source);
// An event whose source the table does not hold is keyed by its id, so its press and release still pair up.
const tokens = source === undefined ? [`#${event.source}`] : [];
if (event.action === 'down') {
runJudgeCoreStep(state, { kind: 'hold', channels, tokens, ...(source !== undefined ? { source } : {}) }, emit);
emit({ kind: 'input-time', seconds: eventSec });
runJudgeCoreStep(
state,
{
kind: 'press',
channels,
tokens,
nowSec: eventSec,
nowMs: eventMs,
...(source !== undefined ? { source } : {}),
...(event.spin !== undefined ? { spin: event.spin } : {}),
...(event.code !== undefined ? { code: event.code } : {}),
},
emit,
);
} else {
runJudgeCoreStep(
state,
{
kind: 'release',
channels,
tokens,
nowSec: eventSec,
nowMs: eventMs,
recordSec: eventSec,
...(source !== undefined ? { source } : {}),
...(event.inferred === true ? { inferred: true } : {}),
...(event.code !== undefined ? { code: event.code } : {}),
},
emit,
);
}
}
}
/**
* Advances a replay's judge state up to `untilSec`, stepping from one instant to the next: to each replay event's own
* microsecond, and to the microsecond after each timed judgment ({@link nextTimedJudgmentUs}) — a tail, a miss
* deadline, an AUTO SCRATCH note, a mine check — so every step settles the judgments of exactly one instant, in the
* order the replay simulator settles them. The state is stepped to each event's instant BEFORE the event is applied,
* so a release resolves its long note before a later press on that lane can refresh the hold, and misses, tails and
* presses reach the gauge in chart-time order — the gauge clamps at its bounds, so the order of judgments is part of
* the result. The steps depend only on the chart and the inputs, never on how the calls are spaced.
*/
export function advanceJudgeCoreReplay(
state: JudgeCoreState,
replay: JudgeCoreReplay,
untilSec: number,
emit: JudgeCoreEffectSink,
): void {
const msPerSecond = state.config.judgeMsPerChartSecond;
const untilUs = Math.round(untilSec * 1e6);
for (;;) {
const nextEvent = replay.events[replay.cursor];
const nextEventUs = nextEvent === undefined ? Number.POSITIVE_INFINITY : nextEvent.timeUs;
const stepUs = Math.min(nextEventUs, nextTimedJudgmentUs(state, replay.judgedThroughUs) + 1);
if (!(stepUs <= untilUs)) {
if (untilUs > replay.judgedThroughUs) {
replay.judgedThroughUs = untilUs;
replay.judgedThroughSeconds = untilSec;
}
return;
}
const stepSec = stepUs / 1e6;
advanceJudgeState(state, stepSec, stepSec * msPerSecond, emit);
replay.judgedThroughUs = stepUs;
replay.judgedThroughSeconds = stepSec;
if (nextEventUs <= stepUs) {
processReplayEventsUntil(state, replay, stepSec, emit);
}
}
}
|