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 | 324x 40x 80x 80x 15x 20x 40x 20x 20x 20x 20x 20x 124x 124x 20x 124x 124x 20x 20x 122x 2x 20x 124x 124x 124x 19x 19x 19x 19x 93x 19x 19x 19x 38x 35x 216x 108x 108x 108x 35x 386x 196x 35x 38x 38x 16x 16x 35x 38x 108x 38x 4x 31x 31x 35x 19x 35x 35x 35x 26x 35x 19x 19x 117x 116x 116x 73x 19x 85x 43x 116x 19x 19x 19x 196x 97x 97x 97x 99x 100x 100x 100x 100x 99x 99x 196x 99x 100x 100x 100x 211x 211x 142x 100x 100x 100x 67x 61x 61x 61x 16x 190x 16x 16x | import { shiftChartOptionProgram } from './chart-options.ts';
import { resolveChart, type RandomDraw } from '@be-music/chart-model';
import type { BeMusicJSON } from '@be-music/chart-source';
import type { NonstopConnection } from '../course/definition.ts';
import type { NonstopStitchMap } from '../course/nonstop/compose.ts';
import type { BeMusicReplay, ReplayChart, ReplayInputEvent, ReplayJudgeRank, ReplayNote } from './format.ts';
import { simulateReplay, type SimulateReplayOptions } from './simulate.ts';
/**
* A NONSTOP course run (`BeMusicCourseReplay.nonstop`): the play of the stitched chart, which is what was judged, and
* where each stage sat in it. The course log's `stages` hold each reached stage cut out of it
* ({@link splitNonstopReplay}), so a stage still replays on its own.
*/
export interface ReplayNonstopRun {
connections: NonstopConnection[];
placement: 'intended' | 'lr2';
/**
* The stitching rules the stitched chart was built by (`NONSTOP_STITCH_VERSION`), so it is rebuilt the same way after
* the rules change. Absent in logs from before it was recorded, which read as version 1.
*/
stitch?: number;
/** The stitched play, as recorded: authoritative for the course result. */
replay: BeMusicReplay;
stages: ReplayNonstopStage[];
/** The stage each note of {@link replay} belongs to, by note id. */
noteStages: number[];
}
/** Where one stage sat in a stitched NONSTOP play, in its µs clock. */
export interface ReplayNonstopStage {
/** Stitched time of the stage's own time zero. */
offsetUs: number;
/** The span the stage owned: from the previous boundary to its own (open for the last stage). */
windowStartUs: number;
windowEndUs?: number;
/** FIT's time-stretch the stage was played at (1 without FIT). */
rate: number;
/**
* The `#RANDOM` / `#SWITCH` draws the stage chart was resolved with before stitching, as `play.randomDraws` records a
* single play's. With the stage's source chart they rebuild the stitched chart (the Leaderboard verifies a NONSTOP run
* that way). Absent for a chart without control flow, and in logs from before they were recorded.
*/
randomDraws?: RandomDraw[];
}
const toUs = (seconds: number): number => Math.round(seconds * 1_000_000);
/**
* The {@link ReplayNonstopStage}s of a stitched chart's map, with each stage's `#RANDOM` / `#SWITCH` draws when
* `stageDraws` gives them (one list per stage; an empty list records none).
*/
export function resolveNonstopReplayStages(
map: NonstopStitchMap,
stageDraws: ReadonlyArray<readonly RandomDraw[] | undefined> = [],
): ReplayNonstopStage[] {
return map.stages.map((stage, index) => {
const draws = stageDraws[index];
return {
offsetUs: toUs(stage.placement.offsetSeconds),
windowStartUs: toUs(stage.windowStart),
...(Number.isFinite(stage.windowEnd) ? { windowEndUs: toUs(stage.windowEnd) } : {}),
rate: stage.placement.rate,
...(draws !== undefined && draws.length > 0 ? { randomDraws: draws.map((draw) => ({ ...draw })) } : {}),
};
});
}
/**
* The stage of every note of a stitched replay, by note id. A judged or invisible note belongs to the stage whose
* keysound it plays (the stitched keys are per stage); a mine, whose value is its damage, to the stage whose window it
* falls in. Notes are matched to the stitched chart by time and type, since a replay lists them by time and lane after
* the lane options moved them.
*/
export function resolveNonstopNoteStages(
stitchedChart: BeMusicJSON,
map: NonstopStitchMap,
notes: readonly ReplayNote[],
): number[] {
const stageOfKey = new Map<string, number>();
map.stages.forEach((stage, index) => stage.wavKeys.forEach((key) => stageOfKey.set(key, index)));
const windows = resolveNonstopReplayStages(map);
const stageAt = (timeUs: number): number => {
const index = windows.findIndex((window) => window.windowEndUs === undefined || timeUs < window.windowEndUs);
return index < 0 ? windows.length - 1 : index;
};
const resolved = resolveChart(stitchedChart);
const pending = new Map<string, number[]>();
const add = (timeUs: number, type: string, stage: number): void => {
const slot = `${timeUs}:${type}`;
pending.set(slot, [...(pending.get(slot) ?? []), stage]);
};
for (const note of resolved.notes) {
const type = note.kind === 'ln' ? 'long' : 'normal';
add(toUs(note.seconds), type, stageOfKey.get(note.sampleKey) ?? stageAt(toUs(note.seconds)));
}
for (const note of resolved.invisibleNotes) {
add(toUs(note.seconds), 'invisible', stageOfKey.get(note.sampleKey) ?? stageAt(toUs(note.seconds)));
}
for (const [slot, stages] of pending)
pending.set(
slot,
stages.sort((left, right) => left - right),
);
return notes.map((note) => {
Iif (note.type === 'mine') return stageAt(note.timeUs);
const type = note.type === 'freezone' ? 'long' : note.type;
return pending.get(`${note.timeUs}:${type}`)?.shift() ?? stageAt(note.timeUs);
});
}
/** The facts of a stage chart its cut-out log names: what identifies it and its title. */
export type NonstopStageChartFacts = Pick<
ReplayChart,
'title' | 'subtitle' | 'artist' | 'genre' | 'playLevel' | 'difficulty' | 'sha256' | 'md5' | 'chartFingerprint'
>;
/**
* Cuts each reached stage out of a stitched NONSTOP replay as a self-contained single-chart log:
*
* - its notes, timed from the stage's own zero (`offsetUs`), still at the FIT-stretched speed it was played at
* (`play.course.rate` records the stretch);
* - the inputs that played them: a press goes to the stage of the nearest note on its lanes (to the stage whose window it
* falls in when its lanes have none), a release with the press it ends;
* - the state the stitched play was in when the stage began (`play.course.carryIn`), simulated under `options`.
*
* A stage is reached when the play got to its window. Judgments of a cut-out stage can differ from the stitched play at
* the seams (a press there may have judged the other stage's note); the course result always comes from the stitched
* play.
*/
export function splitNonstopReplay(
run: Pick<ReplayNonstopRun, 'replay' | 'stages' | 'noteStages'>,
stageCharts: readonly NonstopStageChartFacts[],
options: Omit<SimulateReplayOptions, 'course'>,
): BeMusicReplay[] {
const { replay, stages, noteStages } = run;
const endUs = replay.play.aborted === true ? (replay.play.abortedAtUs ?? Number.POSITIVE_INFINITY) : undefined;
let reachedUntil = endUs ?? replay.inputs.at(-1)?.timeUs ?? 0;
if (endUs === undefined) {
for (const note of replay.chart.notes) reachedUntil = Math.max(reachedUntil, note.timeUs);
}
const inputStages = assignInputs(replay, noteStages, stages);
const logs: BeMusicReplay[] = [];
stages.forEach((stage, index) => {
if (index > 0 && stage.windowStartUs > reachedUntil) return;
const notes = replay.chart.notes
.filter((note) => noteStages[note.id] === index)
.map((note, id): ReplayNote => {
const local: ReplayNote = { ...note, id, timeUs: note.timeUs - stage.offsetUs };
if (note.endTimeUs !== undefined) local.endTimeUs = note.endTimeUs - stage.offsetUs;
return local;
});
const inputs = replay.inputs
.filter((_input, position) => inputStages[position] === index)
.map((input, seq): ReplayInputEvent => ({ ...input, seq, timeUs: input.timeUs - stage.offsetUs }));
const course = { ...(replay.play.course ?? { kind: 'nonstop' as const }), rate: stage.rate };
delete course.carryIn;
if (index > 0) {
const before = carryAt(replay, stage.windowStartUs, options);
course.carryIn = before;
}
const facts = stageCharts[index] ?? {};
const stageLog: BeMusicReplay = {
format: replay.format,
version: replay.version,
...(replay.createdAt !== undefined ? { createdAt: replay.createdAt } : {}),
...(replay.recorder !== undefined ? { recorder: replay.recorder } : {}),
clock: replay.clock,
chart: {
...facts,
...(replay.chart.chartOptions
? { chartOptions: shiftChartOptionProgram(replay.chart.chartOptions, stage) }
: {}),
sourceFormat: replay.chart.sourceFormat,
...(replay.chart.totalUnit !== undefined ? { totalUnit: replay.chart.totalUnit } : {}),
laneMode: replay.chart.laneMode,
lnMode: replay.chart.lnMode,
judgeRank: shiftJudgeRank(replay.chart.judgeRank, stage.offsetUs),
noteCount: notes.filter((note) => note.type === 'normal' || note.type === 'long').length,
notes,
...(replay.chart.total !== undefined ? { total: replay.chart.total } : {}),
},
inputs,
play: { ...replay.play, course },
};
if (endUs !== undefined && endUs < (stage.windowEndUs ?? Number.POSITIVE_INFINITY)) {
stageLog.play.abortedAtUs = endUs - stage.offsetUs;
} else {
delete stageLog.play.aborted;
delete stageLog.play.abortedAtUs;
}
logs.push(stageLog);
});
return logs;
}
/** The stitched play's judge rank in a stage's own clock: its rank changes moved by the stage's offset. */
function shiftJudgeRank(judgeRank: ReplayJudgeRank, offsetUs: number): ReplayJudgeRank {
const shifted: ReplayJudgeRank = { ...judgeRank };
Iif (judgeRank.timeline !== undefined) {
shifted.timeline = judgeRank.timeline.map((change) => ({ ...change, timeUs: change.timeUs - offsetUs }));
}
if (judgeRank.stageRanks !== undefined) {
shifted.stageRanks = judgeRank.stageRanks.map((change) => ({ ...change, timeUs: change.timeUs - offsetUs }));
}
return shifted;
}
/** The stage each input of a stitched play goes to (see {@link splitNonstopReplay}). */
function assignInputs(
replay: BeMusicReplay,
noteStages: readonly number[],
stages: readonly ReplayNonstopStage[],
): number[] {
const byChannel = new Map<string, IndexedReplayNote[]>();
replay.chart.notes.forEach((note, order) => {
if (note.type === 'mine' || note.type === 'invisible') return;
const channel = byChannel.get(note.channel);
if (channel === undefined) byChannel.set(note.channel, [{ note, order }]);
else channel.push({ note, order });
});
for (const [channel, notes] of byChannel) {
notes.sort((left, right) => left.note.timeUs - right.note.timeUs);
// At the same time, the first note in the replay wins.
byChannel.set(
channel,
notes.filter(({ note }, index) => note.timeUs !== notes[index - 1]?.note.timeUs),
);
}
const windowStage = (timeUs: number): number => {
const index = stages.findIndex((stage) => stage.windowEndUs === undefined || timeUs < stage.windowEndUs);
return index < 0 ? stages.length - 1 : index;
};
const held = new Map<string, number>();
return replay.inputs.map((input) => {
if (input.action === 'up') {
const stage = input.channels.map((channel) => held.get(channel)).find((value) => value !== undefined);
for (const channel of input.channels) held.delete(channel);
return stage ?? windowStage(input.timeUs);
}
let nearest: ReplayNote | undefined;
for (const channel of input.channels) {
const notes = byChannel.get(channel);
Iif (notes === undefined) continue;
const note = findNearestInputNote(notes, input.timeUs);
if (nearest === undefined || Math.abs(note.timeUs - input.timeUs) < Math.abs(nearest.timeUs - input.timeUs)) {
nearest = note;
}
}
const stage =
nearest !== undefined ? (noteStages[nearest.id] ?? windowStage(input.timeUs)) : windowStage(input.timeUs);
for (const channel of input.channels) held.set(channel, stage);
return stage;
});
}
interface IndexedReplayNote {
note: ReplayNote;
order: number;
}
/** Finds the nearest note in a nonempty lane sorted by time, with one note per timestamp. */
function findNearestInputNote(notes: readonly IndexedReplayNote[], timeUs: number): ReplayNote {
let lo = 0;
let hi = notes.length;
while (lo < hi) {
const mid = (lo + hi) >>> 1;
if (notes[mid]!.note.timeUs < timeUs) lo = mid + 1;
else hi = mid;
}
const before = notes[lo - 1];
const after = notes[lo];
if (before === undefined) return after!.note;
if (after === undefined) return before.note;
const earlierDistance = timeUs - before.note.timeUs;
const laterDistance = after.note.timeUs - timeUs;
return earlierDistance < laterDistance || (earlierDistance === laterDistance && before.order < after.order)
? before.note
: after.note;
}
/** The gauge and combo a stitched play had reached at `timeUs`: the play simulated up to that point. */
function carryAt(
replay: BeMusicReplay,
timeUs: number,
options: Omit<SimulateReplayOptions, 'course'>,
): { gauge: number; combo: number; maxCombo: number } {
const upTo: BeMusicReplay = {
...replay,
inputs: replay.inputs.filter((input) => input.timeUs < timeUs),
play: { ...replay.play, aborted: true, abortedAtUs: timeUs },
};
const result = simulateReplay(upTo, options);
return {
gauge: result.gauge.final,
combo: result.course?.combo ?? 0,
maxCombo: result.course?.maxCombo ?? result.maxCombo,
};
}
|