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 | 46x 46x 10x 10x 23x 23x 13x 10x 33x 33x 12x 7x 5x 4x 12x 4x 4x 240x 240x 33x 4x 7x 7x 7x 7x 7x 7x 7x 8x 8x 20x 20x 8x 8x 8x 20x 12x 20x 20x 8x 20x 20x 6x 8x 7x 8x 11x 11x 11x 11x 38x 21x 11x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 17x 7x 8x 8x 8x 8x 8x 7x 7x 8x 8x 7x 7x 7x 7x 7x 7x 4x 2x 2x 2x 3x 3x 8x 8x 7x 7x 7x 4x 4x 2x 2x 2x 8x 7x 4x 7x 7x 7x 3x 2x 3x 7x 4x 7x 6x 2x 2x 2x 2x 16x 16x 16x 16x 16x 16x 2x 2x 14x 14x 14x 14x 14x 14x | /**
* The ghost as a second playfield (the first {@link GhostPresentation}): the recorded play's notes scroll on the 2P side
* of a single-play chart, using the two-player layout and the combined lane model (`ghost-side.ts`), and go as its replay
* settled them. Its key beams, bombs and judgment plate follow its recorded inputs and judgments, and its score, combo
* and gauge show as the second player's HUD. Its notes scroll with the live play's hi-speed, so a note of the ghost
* and the live note it matches cross their judgment lines together.
*
* It writes its lane effects into the play's own maps under its 2P lanes (which a single-play chart never uses), and
* gives the view a playfield that adds its lanes, notes and HUD to the live play's ({@link SidePlayfieldGhost.present}).
* The live play's arrays and state are never touched, so the engine judges and records only the live notes.
*/
import type { TimingResolver } from '@be-music/chart-model';
import type { TimedLandmineNote, TimedPlayableNote } from '@be-music/player-engine/display';
import { resolveLR2MoneyScore, createEmptyScore, type ScoreSummary } from '@be-music/player-engine/ruleset';
import { createPlayState, type PlayState } from '@be-music/player-scene-model';
import type { BeMusicJudgmentKind } from '../../../skin/be-music/types.ts';
import type { PlayfieldRuntime, RuntimeMineNote, RuntimeNote } from '../../../view/shared/playfield/context.ts';
import type { PlayJudgeSideState } from '../play-presentation.ts';
import type { GhostPresentation } from './ghost.ts';
import { toGhostChannel, type GhostSide } from './ghost-side.ts';
import { findGhostStepAt, type GhostTimeline } from './ghost-timeline.ts';
import { PresentedPlayfield } from './presented-playfield.ts';
/** How long a judgment plate stays up, in chart seconds (as the live play's). */
const PLATE_SECONDS = 0.6;
/** A step further behind the playhead than this is caught up silently: no bomb, no key beam for it. */
const EFFECT_WINDOW_SECONDS = 0.25;
/** The play's lane effects and plates the ghost writes into, under its own lanes. */
export interface SidePlayfieldGhostTarget {
readonly keyBeamStartedAt: Map<string, number>;
readonly keyBeamFadeStartedAt: Map<string, number>;
readonly bombStartedAt: Map<string, number>;
readonly bombJudge: Map<string, BeMusicJudgmentKind>;
readonly lanePressedAt: Map<string, number>;
readonly laneReleasedAt: Map<string, number>;
judgeSideState: Record<'1P' | '2P', PlayJudgeSideState>;
}
/** The live chart the ghost's notes are placed on: its notes (for their beats) and its timing. */
export interface GhostFieldChart {
readonly notes: readonly TimedPlayableNote[];
readonly mineNotes: readonly TimedLandmineNote[];
readonly timingResolver: Pick<TimingResolver, 'beatToSeconds'> | undefined;
}
/**
* Chart second → beat on the live chart: a second a live note sits at gives that note's beat; any other second is
* found on the chart's timing (the first beat at or after it), or spread between the known notes without one.
*/
export function createSecondsToBeat(
points: readonly { readonly seconds: number; readonly beat: number }[],
timing?: Pick<TimingResolver, 'beatToSeconds'>,
): (seconds: number) => number {
const exact = new Map<number, number>();
for (const point of points) {
const key = Math.round(point.seconds * 1_000_000);
Eif (!exact.has(key)) exact.set(key, point.beat);
}
const sorted = [...points].sort((left, right) => left.seconds - right.seconds || left.beat - right.beat);
return (seconds) => {
const hit = exact.get(Math.round(seconds * 1_000_000));
if (hit !== undefined) return hit;
if (timing !== undefined) return invertBeatToSeconds(timing, seconds);
return interpolateBeat(sorted, seconds);
};
}
function invertBeatToSeconds(timing: Pick<TimingResolver, 'beatToSeconds'>, seconds: number): number {
if (seconds <= timing.beatToSeconds(0)) return 0;
let high = 1;
while (timing.beatToSeconds(high) < seconds && high < 1e7) high *= 2;
let low = 0;
for (let step = 0; step < 60; step += 1) {
const middle = (low + high) / 2;
if (timing.beatToSeconds(middle) < seconds) low = middle;
else high = middle;
}
return high;
}
function interpolateBeat(sorted: readonly { seconds: number; beat: number }[], seconds: number): number {
Iif (sorted.length === 0) return 0;
Iif (sorted.length === 1) return sorted[0]!.beat;
const index = Math.min(Math.max(1, findGhostStepAt(sorted, seconds) + 1), sorted.length - 1);
const left = sorted[index - 1]!;
const right = sorted[index]!;
const span = right.seconds - left.seconds;
return span > 0 ? left.beat + ((seconds - left.seconds) / span) * (right.beat - left.beat) : left.beat;
}
/** The ghost's notes and mines, ready to draw on its lanes: copies on the 2P channels, none judged yet. */
export interface GhostField {
/** In beat order, like the live notes. */
readonly notes: readonly RuntimeNote[];
readonly mines: readonly RuntimeMineNote[];
/** `notes` indices in the order the replay settled them (unsettled notes left out). */
readonly settleOrder: readonly number[];
/** When each of `notes` was settled, by index. */
readonly settledAt: readonly number[];
}
/** Places `timeline`'s notes and mines on the ghost's lanes of the live chart `chart`. */
export function buildGhostField(timeline: GhostTimeline, chart: GhostFieldChart): GhostField {
const points: { seconds: number; beat: number }[] = [];
for (const note of chart.notes) {
points.push({ seconds: note.seconds, beat: note.beat });
Iif (note.endSeconds !== undefined && note.endBeat !== undefined)
points.push({ seconds: note.endSeconds, beat: note.endBeat });
}
for (const mine of chart.mineNotes) points.push({ seconds: mine.seconds, beat: mine.beat });
const beatAt = createSecondsToBeat(points, chart.timingResolver);
// The view reads nothing of a note's source event; any of the chart's keeps the type whole.
const event = chart.notes[0]?.event ?? chart.mineNotes[0]?.event;
const placed = timeline.notes.map((timing) => ({
timing,
note: {
event: event!,
channel: toGhostChannel(timing.channel),
beat: beatAt(timing.seconds),
seconds: timing.seconds,
judged: false,
...(timing.endSeconds !== undefined ? { endSeconds: timing.endSeconds, endBeat: beatAt(timing.endSeconds) } : {}),
...(timing.longNoteMode !== undefined ? { longNoteMode: timing.longNoteMode } : {}),
} satisfies RuntimeNote,
}));
placed.sort((left, right) => left.note.beat - right.note.beat || left.note.seconds - right.note.seconds);
const notes = placed.map((entry) => entry.note);
const settledAt = placed.map((entry) => entry.timing.settledAtSeconds ?? Number.POSITIVE_INFINITY);
const settleOrder = settledAt
.map((_, index) => index)
.filter((index) => Number.isFinite(settledAt[index]!))
.sort((left, right) => settledAt[left]! - settledAt[right]! || left - right);
const mines = timeline.mines
.map((mine): RuntimeMineNote => ({
event: event!,
channel: toGhostChannel(mine.channel),
beat: beatAt(mine.seconds),
seconds: mine.seconds,
judged: false,
mine: true,
}))
.sort((left, right) => left.beat - right.beat);
return { notes, mines, settleOrder, settledAt };
}
/** Merges two beat-ordered note arrays into one, `left` first on equal beats. */
export function mergeByBeat<T extends { readonly beat: number }>(left: readonly T[], right: readonly T[]): T[] {
const merged: T[] = [];
let i = 0;
let j = 0;
while (i < left.length || j < right.length) {
if (j >= right.length || (i < left.length && left[i]!.beat <= right[j]!.beat)) merged.push(left[i++]!);
else merged.push(right[j++]!);
}
return merged;
}
/** The second playfield. See the module comment. */
export class SidePlayfieldGhost implements GhostPresentation {
public readonly drawsOnPlayfield = true;
private readonly timeline: GhostTimeline;
private readonly side: GhostSide;
private readonly field: GhostField;
private readonly target: SidePlayfieldGhostTarget;
private readonly maxLongNoteBeatSpan: number;
private seconds = Number.NEGATIVE_INFINITY;
private settleCursor = 0;
private judgmentCursor = 0;
private inputCursor = 0;
private secondPlayer: { score: ScoreSummary; playState: PlayState } | undefined;
private merged: { notes: RuntimeNote[]; liveNotes: readonly RuntimeNote[] } | undefined;
private mergedDraw: { notes: RuntimeNote[]; liveNotes: readonly RuntimeNote[] } | undefined;
private mergedMines: { mines: RuntimeMineNote[]; liveMines: readonly RuntimeMineNote[] } | undefined;
private readonly presented = new PresentedPlayfield();
public constructor(timeline: GhostTimeline, side: GhostSide, field: GhostField, target: SidePlayfieldGhostTarget) {
this.timeline = timeline;
this.side = side;
this.field = field;
this.target = target;
this.maxLongNoteBeatSpan = field.notes.reduce(
(max, note) => Math.max(max, (note.endBeat ?? note.beat) - note.beat),
0,
);
this.secondPlayer = this.resolveSecondPlayer(-1);
}
public update(seconds: number, nowMs: number): void {
if (seconds < this.seconds) this.rewind();
this.seconds = seconds;
const { notes } = this.field;
const { settleOrder, settledAt } = this.field;
while (this.settleCursor < settleOrder.length && settledAt[settleOrder[this.settleCursor]!]! <= seconds) {
notes[settleOrder[this.settleCursor]!]!.judged = true;
this.settleCursor += 1;
}
const { inputs, judgments } = this.timeline;
while (this.inputCursor < inputs.length && inputs[this.inputCursor]!.seconds <= seconds) {
const input = inputs[this.inputCursor]!;
this.inputCursor += 1;
const channel = toGhostChannel(input.channel);
Iif (!this.side.ghostChannels.has(channel)) continue;
const recent = seconds - input.seconds <= EFFECT_WINDOW_SECONDS;
if (input.pressed) {
if (!recent) continue;
this.target.lanePressedAt.set(channel, nowMs);
this.target.keyBeamStartedAt.set(channel, nowMs);
this.target.keyBeamFadeStartedAt.delete(channel);
} else {
this.target.laneReleasedAt.set(channel, nowMs);
if (this.target.keyBeamStartedAt.has(channel)) this.target.keyBeamFadeStartedAt.set(channel, nowMs);
}
}
const before = this.judgmentCursor;
while (this.judgmentCursor < judgments.length && judgments[this.judgmentCursor]!.seconds <= seconds) {
const step = judgments[this.judgmentCursor]!;
this.judgmentCursor += 1;
if (seconds - step.seconds > EFFECT_WINDOW_SECONDS) continue;
this.target.judgeSideState['2P'] = {
judge: step.judgment,
until: step.seconds + PLATE_SECONDS,
combo: step.combo,
};
if (step.channel !== undefined && (step.judgment === 'PERFECT' || step.judgment === 'GREAT')) {
const channel = toGhostChannel(step.channel);
this.target.bombStartedAt.set(channel, nowMs);
this.target.bombJudge.set(channel, step.judgment);
}
}
if (this.judgmentCursor !== before) this.secondPlayer = this.resolveSecondPlayer(this.judgmentCursor - 1);
}
public present(play: PlayfieldRuntime): PlayfieldRuntime {
if (this.merged?.liveNotes !== play.notes)
this.merged = { notes: mergeByBeat(play.notes, this.field.notes), liveNotes: play.notes };
let drawNotes = this.merged.notes;
// A seamless PRACTICE loop also draws its next round. Keep that preview in the renderer's list.
const liveDrawNotes = play.drawNotes;
if (liveDrawNotes !== play.notes) {
if (this.mergedDraw?.liveNotes !== liveDrawNotes)
this.mergedDraw = { notes: mergeByBeat(liveDrawNotes, this.field.notes), liveNotes: liveDrawNotes };
drawNotes = this.mergedDraw.notes;
}
if (this.mergedMines?.liveMines !== play.mineNotes)
this.mergedMines = { mines: mergeByBeat(play.mineNotes, this.field.mines), liveMines: play.mineNotes };
return this.presented.present(play, {
laneChannels: this.side.laneChannels,
chartPlayVariant: this.side.playVariant,
notes: this.merged.notes,
drawNotes,
maxLongNoteBeatSpan: Math.max(play.maxLongNoteBeatSpan, this.maxLongNoteBeatSpan),
mineNotes: this.mergedMines.mines,
secondPlayer: this.secondPlayer,
ghostSide: '2P',
});
}
/** Back to the chart's start: no note settled, no effect of the ghost's left on its lanes. */
private rewind(): void {
for (const note of this.field.notes) note.judged = false;
this.settleCursor = 0;
this.judgmentCursor = 0;
this.inputCursor = 0;
for (const channel of this.side.ghostChannels) {
this.target.keyBeamStartedAt.delete(channel);
this.target.keyBeamFadeStartedAt.delete(channel);
this.target.bombStartedAt.delete(channel);
this.target.bombJudge.delete(channel);
this.target.lanePressedAt.delete(channel);
this.target.laneReleasedAt.delete(channel);
}
this.target.judgeSideState['2P'] = { judge: '', until: 0, combo: 0 };
this.secondPlayer = this.resolveSecondPlayer(-1);
}
private resolveSecondPlayer(index: number): { score: ScoreSummary; playState: PlayState } {
const step = index >= 0 ? this.timeline.judgments[index] : undefined;
const counts = step?.counts;
const score: ScoreSummary = {
...createEmptyScore(this.timeline.noteCount),
...(counts !== undefined
? {
perfect: counts.perfect,
great: counts.great,
good: counts.good,
bad: counts.bad,
poor: counts.poor,
emptyPoor: counts.emptyPoor,
}
: {}),
exScore: step?.exScore ?? 0,
};
score.score = this.timeline.ruleset === 'lr2' ? resolveLR2MoneyScore(score) : score.exScore;
const playState: PlayState = {
...createPlayState(),
gaugeType: this.timeline.gaugeType,
gauge: step?.gauge ?? this.timeline.judgments[0]?.gauge ?? 0,
exScore: score.exScore,
combo: step?.combo ?? 0,
maxCombo: step?.maxCombo ?? 0,
};
return { score, playState };
}
}
|