All files / skin-beatoraja/src/scene play-numbers.ts

99.19% Statements 123/124
89.61% Branches 164/183
100% Functions 11/11
99.1% Lines 111/112

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                                                                                                                                                  12x 6x 6x 6x 15x 15x 1x 1x   14x 14x   6x 6x     50x               13x 12x 13x 13x 1x 1x 4x 3x   1x       11x 11x 11x 21x 21x 12x   11x 13x         28x 28x   8x     28x               29x 29x       29x 29x       27x                 17x 17x 2x 2x               15x       8x         3x 2x 2x 2x                                               157x 157x     6x 5x 5x     3x   3x   4x   8x   8x   2x       1x       11x     3x   2x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x     2x   1x   1x   1x   1x   1x   1x   1x   1x   2x       1x                           3x     2x     3x 2x 2x     1x     3x   3x   4x   3x     7x   5x   2x       8x         13x                   1x   2x   1x   2x   2x   1x     1x   13x      
import { clamp } from '@be-music/shared-utils/core';
import { resolveGreenNumber, resolveWhiteNumber, type LaneCoverState } from '@be-music/player-scene-model';
import {
  BEATORAJA_NUM,
  SYNTHETIC_NUM_JUDGE_COMBO_1P,
  SYNTHETIC_NUM_JUDGE_COMBO_2P,
  SYNTHETIC_NUM_JUDGE_COMBO_3P,
} from '../runtime-ids.ts';
import type { BeatorajaWallClock } from './inputs.ts';
import {
  beatorajaNotesJudged,
  beatorajaScoreRate,
  resolveBeatorajaScoreNumber,
  type BeatorajaScoreNumberInput,
} from './score.ts';
 
/** One tempo change of a chart, in chart time: STOPs read as BPM 0. */
export interface BeatorajaBPMSegment {
  readonly timeMs: number;
  readonly bpm: number;
}
 
/** What a play skin's numbers read of the chart. */
export interface BeatorajaPlayChartFacts {
  /** The header BPM. */
  readonly bpm: number;
  /** The BPM most notes sit at (beatoraja's MAINBPM); the header BPM when 0 or absent. */
  readonly mainBPM?: number;
  /** The lowest and highest BPM of the tempo map (0 without a chart). */
  readonly minBPM: number;
  readonly maxBPM: number;
  /** The tempo changes in chart-time order, which the BPM at the playhead is read from. */
  readonly bpmSegments?: readonly BeatorajaBPMSegment[];
  /** `#PLAYLEVEL`, as a number or as written. */
  readonly playLevel?: number | string;
}
 
/** Where the play is: the engine frame's chart time and the chart's length, in seconds. */
export interface BeatorajaPlayClock {
  readonly currentSeconds: number;
  readonly totalSeconds: number;
  /** Chart time of an LR2 negative-BPM reversal, when the chart has one. */
  readonly reversalSeconds?: number;
}
 
/** What a play skin's numbers read: the score, the view (hi-speed and covers), the chart, the clock. */
export interface BeatorajaPlayNumberInput extends BeatorajaScoreNumberInput {
  readonly judgeTimingMs?: number;
  /** The hi-speed the notes scroll at. */
  readonly hiSpeed: number;
  /** The 1P lane covers; none when absent. */
  readonly cover?: LaneCoverState;
  readonly chart: BeatorajaPlayChartFacts;
  /** Absent before the engine's first frame. */
  readonly clock?: BeatorajaPlayClock;
  /** The signed offset (ms, > 0 late) of the latest timed judgment. */
  readonly lastJudgeDeltaMs?: number;
  /** The combo each judge plate showed at its latest judgment: 1P, 2P and POPN 9 KEY's third plate. */
  readonly plateCombos?: readonly [number, number, number];
  /** The target the play paces against (a replay ghost): its EX score over the whole chart, numbers 121-123. */
  readonly targetEXScore?: number;
  readonly wallClock?: BeatorajaWallClock;
  /** How long the player has been running, in ms (numbers 27-29). */
  readonly operatingMs?: number;
  /** The frame rate (number 20). */
  readonly fps?: number;
}
 
/**
 * For each BPM segment, the last positive BPM at or before it (`NaN` before the first one), so the BPM at a time is one
 * binary search away. `undefined` when the segments are not in time order, where only the walk is right.
 */
function bpmLookupFor(segments: readonly BeatorajaBPMSegment[]): Float64Array | undefined {
  if (BPM_LOOKUPS.has(segments)) return BPM_LOOKUPS.get(segments);
  let lookup: Float64Array | undefined = new Float64Array(segments.length);
  let lastPositive = Number.NaN;
  for (let index = 0; index < segments.length; index += 1) {
    const segment = segments[index]!;
    if (index > 0 && segment.timeMs < segments[index - 1]!.timeMs) {
      lookup = undefined;
      break;
    }
    if (segment.bpm > 0) lastPositive = segment.bpm;
    lookup[index] = lastPositive;
  }
  BPM_LOOKUPS.set(segments, lookup);
  return lookup;
}
 
const BPM_LOOKUPS = new WeakMap<readonly BeatorajaBPMSegment[], Float64Array | undefined>();
 
/**
 * The BPM in effect at `timeMs` of chart time (beatoraja's `LaneRenderer.nbpm`): the last positive BPM of a segment at
 * or before it. A STOP (BPM 0) holds the BPM before it, as the readout does; before the first positive segment the
 * first segment's BPM, else the header BPM.
 */
export function beatorajaBPMAt(segments: readonly BeatorajaBPMSegment[], timeMs: number, headerBPM: number): number {
  if (segments.length === 0) return headerBPM;
  const initial = segments[0]!.bpm > 0 ? segments[0]!.bpm : headerBPM;
  const lookup = bpmLookupFor(segments);
  if (lookup === undefined) {
    let lastPositive = initial;
    for (const segment of segments) {
      if (segment.timeMs > timeMs) break;
      Eif (segment.bpm > 0) lastPositive = segment.bpm;
    }
    return lastPositive;
  }
  // The last segment at or before `timeMs` (a binary search), and the last positive BPM up to it: what walking the
  // segments in order until one starts after `timeMs` finds, for a readout asked several times a frame.
  let low = 0;
  let high = segments.length;
  while (low < high) {
    const middle = (low + high) >>> 1;
    if (segments[middle]!.timeMs > timeMs) high = middle;
    else low = middle + 1;
  }
  const lastPositive = low === 0 ? Number.NaN : lookup[low - 1]!;
  return Number.isNaN(lastPositive) ? initial : lastPositive;
}
 
/** The BPM at the playhead: the header BPM before the first frame. */
function playheadBPM(input: BeatorajaPlayNumberInput): number {
  const { clock, chart } = input;
  if (clock === undefined) return chart.bpm;
  const offset =
    typeof input.judgeTimingMs === 'number' && Number.isFinite(input.judgeTimingMs)
      ? Math.trunc(input.judgeTimingMs)
      : 0;
  return beatorajaBPMAt(chart.bpmSegments ?? [], clock.currentSeconds * 1000 + offset, chart.bpm);
}
 
/**
 * The white number (`LaneRenderer.getCurrentDuration`, ms): a lane of four beats at `bpm` and the hi-speed, less the
 * lane cover when `withCover`. The LIFT does not shorten it. Rounded, 0 for a hi-speed or BPM that is not positive.
 */
function whiteDuration(input: BeatorajaPlayNumberInput, bpm: number, withCover: boolean): number {
  const sudden = withCover && input.cover?.suddenOn === true ? beatorajaLaneCover(input.cover) : 0;
  return Math.round(resolveWhiteNumber({ laneBeats: 4, hiSpeed: input.hiSpeed, sudden, bpm }));
}
 
function duration(input: BeatorajaPlayNumberInput, bpm: number, withCover: boolean, green: boolean): number {
  const white = whiteDuration(input, bpm, withCover);
  return green ? resolveGreenNumber(white) : white;
}
 
function clampUnit(value: number): number {
  return Number.isFinite(value) ? clamp(value, 0, 1) : 0;
}
 
/**
 * beatoraja's lane cover (`PlayConfig.lanecover`): the SUDDEN+ height as a fraction of the part of the lane above the
 * LIFT, kept while the cover is off. Be-Music keeps SUDDEN+ so too, except an absolute cover (the floating hi-speed's),
 * a fraction of the whole lane: the same drawn height is `sudden / (1 - lift)` there.
 */
function beatorajaLaneCover(cover: LaneCoverState): number {
  const sudden = clampUnit(cover.sudden);
  if (cover.absolute !== true) return sudden;
  const above = 1 - clampUnit(cover.lift);
  return above > 0 ? Math.min(1, sudden / above) : 0;
}
 
/**
 * `(int) (value * 1000)` on beatoraja's `float`s (`IntegerPropertyFactory` 14 / 314), so a cover moved up ten 0.01
 * steps (0.09999999999999999 as a double) reads 100, not 99.
 */
function beatorajaPerMille(value: number): number {
  return Math.trunc(Math.fround(Math.fround(value) * 1000));
}
 
function mainBPM(chart: BeatorajaPlayChartFacts): number {
  return chart.mainBPM !== undefined && chart.mainBPM > 0 ? chart.mainBPM : chart.bpm;
}
 
/** `#PLAYLEVEL` as a whole number: a number truncated, text parsed, 0 without one. */
function playLevelNumber(level: number | string | undefined): number {
  if (typeof level === 'number') return Math.trunc(level);
  Eif (typeof level === 'string') {
    const parsed = Number.parseInt(level, 10);
    return Number.isFinite(parsed) ? parsed : 0;
  }
  return 0;
}
 
/**
 * A play skin's number (`value[].ref`, `IntegerPropertyFactory`) for the play as it is, or `undefined` for an id the
 * play does not show. Besides the score numbers ({@link resolveBeatorajaScoreNumber}):
 *
 * - the score rate (102 / 103) over the notes judged so far, 100 % before the first (beatoraja's `nowrate`; 115 / 116
 *   are over every note);
 * - the hi-speed (310 its integer part, 311 two decimals, 10 times 100, all truncated as beatoraja's `(int)` casts),
 *   the lane cover (of the lane above the LIFT, kept while it is off) and the LIFT in per mille (14, 314);
 * - the white and green numbers (312 / 313, and 1312-1327 at the main, lowest and highest BPM, with and without the
 *   lane cover) from {@link resolveWhiteNumber};
 * - the BPMs (90-92, and 160 at the playhead, held at an LR2 negative-BPM reversal), `#PLAYLEVEL` (96);
 * - the play, remaining and song time (161-164, 1163 / 1164), loading done (165);
 * - the clock (21-26), the time running (27-29) and the frame rate (20) as given;
 * - the max combo also as number 75, the latest judgment's offset (525) and each judge plate's combo for the judge
 *   popup (the synthetic 20101-20103);
 * - 0 for the best record, play counts, target and rival numbers, the play time and the judge timing and HIDDEN+
 *   readouts, which nothing feeds yet.
 */
export function resolveBeatorajaPlayNumber(id: number, input: BeatorajaPlayNumberInput): number | undefined {
  const { chart, clock, hiSpeed } = input;
  switch (id) {
    case BEATORAJA_NUM.SCORE_RATE:
    case BEATORAJA_NUM.SCORE_RATE_AFTERDOT: {
      if (input.score === undefined) return 0;
      const rate = beatorajaScoreRate(input.score.exScore, beatorajaNotesJudged(input.score));
      return id === BEATORAJA_NUM.SCORE_RATE ? rate.whole : rate.afterDot;
    }
    case BEATORAJA_NUM.HISPEED:
      return Math.trunc(hiSpeed);
    case BEATORAJA_NUM.HISPEED_LR2:
      return Math.trunc(hiSpeed * 100);
    case BEATORAJA_NUM.HISPEED_AFTERDOT:
      return Math.trunc(hiSpeed * 100) % 100;
    case BEATORAJA_NUM.LANECOVER1:
      return input.cover !== undefined ? beatorajaPerMille(beatorajaLaneCover(input.cover)) : 0;
    case BEATORAJA_NUM.LIFT1:
      return input.cover !== undefined ? beatorajaPerMille(clampUnit(input.cover.lift)) : 0;
    case BEATORAJA_NUM.JUDGETIMING:
      return typeof input.judgeTimingMs === 'number' && Number.isFinite(input.judgeTimingMs)
        ? Math.trunc(input.judgeTimingMs)
        : 0;
    case BEATORAJA_NUM.HIDDEN1:
      return 0;
 
    case BEATORAJA_NUM.DURATION:
    case BEATORAJA_NUM.DURATION_LANECOVER_ON:
      return duration(input, playheadBPM(input), true, false);
    case BEATORAJA_NUM.DURATION_GREEN:
    case BEATORAJA_NUM.DURATION_GREEN_LANECOVER_ON:
      return duration(input, playheadBPM(input), true, true);
    case BEATORAJA_NUM.DURATION_LANECOVER_OFF:
      return duration(input, playheadBPM(input), false, false);
    case BEATORAJA_NUM.DURATION_GREEN_LANECOVER_OFF:
      return duration(input, playheadBPM(input), false, true);
    case BEATORAJA_NUM.MAINBPM_DURATION_LANECOVER_ON:
      return duration(input, mainBPM(chart), true, false);
    case BEATORAJA_NUM.MAINBPM_DURATION_GREEN_LANECOVER_ON:
      return duration(input, mainBPM(chart), true, true);
    case BEATORAJA_NUM.MAINBPM_DURATION_LANECOVER_OFF:
      return duration(input, mainBPM(chart), false, false);
    case BEATORAJA_NUM.MAINBPM_DURATION_GREEN_LANECOVER_OFF:
      return duration(input, mainBPM(chart), false, true);
    case BEATORAJA_NUM.MINBPM_DURATION_LANECOVER_ON:
      return duration(input, chart.minBPM, true, false);
    case BEATORAJA_NUM.MINBPM_DURATION_GREEN_LANECOVER_ON:
      return duration(input, chart.minBPM, true, true);
    case BEATORAJA_NUM.MINBPM_DURATION_LANECOVER_OFF:
      return duration(input, chart.minBPM, false, false);
    case BEATORAJA_NUM.MINBPM_DURATION_GREEN_LANECOVER_OFF:
      return duration(input, chart.minBPM, false, true);
    case BEATORAJA_NUM.MAXBPM_DURATION_LANECOVER_ON:
      return duration(input, chart.maxBPM, true, false);
    case BEATORAJA_NUM.MAXBPM_DURATION_GREEN_LANECOVER_ON:
      return duration(input, chart.maxBPM, true, true);
    case BEATORAJA_NUM.MAXBPM_DURATION_LANECOVER_OFF:
      return duration(input, chart.maxBPM, false, false);
    case BEATORAJA_NUM.MAXBPM_DURATION_GREEN_LANECOVER_OFF:
      return duration(input, chart.maxBPM, false, true);
 
    case BEATORAJA_NUM.TIME_YEAR:
      return input.wallClock?.year;
    case BEATORAJA_NUM.TIME_MONTH:
      return input.wallClock?.month;
    case BEATORAJA_NUM.TIME_DAY:
      return input.wallClock?.day;
    case BEATORAJA_NUM.TIME_HOUR:
      return input.wallClock?.hour;
    case BEATORAJA_NUM.TIME_MINUTE:
      return input.wallClock?.minute;
    case BEATORAJA_NUM.TIME_SECOND:
      return input.wallClock?.second;
    case BEATORAJA_NUM.OPERATING_TIME_HOUR:
      return Math.floor((input.operatingMs ?? 0) / 3_600_000);
    case BEATORAJA_NUM.OPERATING_TIME_MINUTE:
      return Math.floor(((input.operatingMs ?? 0) / 60_000) % 60);
    case BEATORAJA_NUM.OPERATING_TIME_SECOND:
      return Math.floor(((input.operatingMs ?? 0) / 1_000) % 60);
    case BEATORAJA_NUM.CURRENT_FPS:
      return input.fps ?? 0;
    case BEATORAJA_NUM.TOTALPLAYTIME_HOUR:
    case BEATORAJA_NUM.TOTALPLAYTIME_MINUTE:
    case BEATORAJA_NUM.TOTALPLAYTIME_SECOND:
      return 0;
 
    case BEATORAJA_NUM.BEST_SCORE:
    case BEATORAJA_NUM.BEST_MAXSCORE:
    case BEATORAJA_NUM.BEST_MISSCOUNT:
    case BEATORAJA_NUM.PLAYCOUNT:
    case BEATORAJA_NUM.CLEARCOUNT:
    case BEATORAJA_NUM.FAILCOUNT:
    case BEATORAJA_NUM.BEST_PERFECT:
    case BEATORAJA_NUM.BEST_GREAT:
    case BEATORAJA_NUM.BEST_GOOD:
    case BEATORAJA_NUM.BEST_BAD:
    case BEATORAJA_NUM.BEST_POOR:
    case BEATORAJA_NUM.DIFF_EXSCORE:
      return 0;
    // The target's EX score over the whole chart and its rate (beatoraja `ScoreDataProperty.rivalscore` / `rivalrate`).
    case BEATORAJA_NUM.TARGET_SCORE:
      return input.targetEXScore ?? 0;
    case BEATORAJA_NUM.TARGET_SCORE_RATE:
    case BEATORAJA_NUM.TARGET_SCORE_RATE_AFTERDOT: {
      if (input.targetEXScore === undefined || input.score === undefined) return 0;
      const rate = beatorajaScoreRate(input.targetEXScore, input.score.total);
      return id === BEATORAJA_NUM.TARGET_SCORE_RATE ? rate.whole : rate.afterDot;
    }
    case BEATORAJA_NUM.BEST_MAXCOMBO:
      return input.maxCombo;
 
    case BEATORAJA_NUM.MAXBPM:
      return Math.round(chart.maxBPM);
    case BEATORAJA_NUM.MINBPM:
      return Math.round(chart.minBPM);
    case BEATORAJA_NUM.MAINBPM:
      return Math.round(mainBPM(chart));
    case BEATORAJA_NUM.PLAYLEVEL:
      return playLevelNumber(chart.playLevel);
 
    case SYNTHETIC_NUM_JUDGE_COMBO_1P:
      return input.plateCombos?.[0] ?? 0;
    case SYNTHETIC_NUM_JUDGE_COMBO_2P:
      return input.plateCombos?.[1] ?? 0;
    case SYNTHETIC_NUM_JUDGE_COMBO_3P:
      return input.plateCombos?.[2] ?? 0;
    case BEATORAJA_NUM.JUDGE_1P_OFFSET_MS:
      // beatoraja's `getRecentJudgeTiming` is the note's time minus the press's (`JudgeManager` L690), positive when
      // early: the opposite sign of the offset kept here.
      return input.lastJudgeDeltaMs !== undefined ? Math.trunc(-input.lastJudgeDeltaMs) || 0 : 0;
 
    // Past an LR2 negative-BPM reversal the readout holds the value at the reversal: LR2's event pump is frozen there
    // and never applies a later BPM event (the reverse scroll itself keeps following the tempo map).
    case BEATORAJA_NUM.NOWBPM:
      return Math.round(
        clock !== undefined && clock.reversalSeconds !== undefined
          ? beatorajaBPMAt(
              chart.bpmSegments ?? [],
              Math.min(clock.currentSeconds, clock.reversalSeconds) * 1000,
              chart.bpm,
            )
          : playheadBPM(input),
      );
    case BEATORAJA_NUM.PLAYTIME_MINUTE:
      return clock !== undefined ? Math.floor(clock.currentSeconds / 60) : 0;
    case BEATORAJA_NUM.PLAYTIME_SECOND:
      return clock !== undefined ? Math.floor(clock.currentSeconds % 60) : 0;
    case BEATORAJA_NUM.TIMELEFT_MINUTE:
      return clock !== undefined ? Math.floor(Math.max(0, clock.totalSeconds - clock.currentSeconds) / 60) : 0;
    case BEATORAJA_NUM.TIMELEFT_SECOND:
      return clock !== undefined ? Math.floor(Math.max(0, clock.totalSeconds - clock.currentSeconds) % 60) : 0;
    case BEATORAJA_NUM.SONGLENGTH_MINUTE:
      return clock !== undefined ? Math.floor(clock.totalSeconds / 60) : 0;
    case BEATORAJA_NUM.SONGLENGTH_SECOND:
      return clock !== undefined ? Math.floor(clock.totalSeconds % 60) : 0;
    // The play scene mounts once its assets are decoded, so loading is done.
    case BEATORAJA_NUM.LOADING_PROGRESS:
      return 100;
    default:
      return resolveBeatorajaScoreNumber(id, input);
  }
}