All files / player-engine/src/core judge-window.ts

100% Statements 52/52
91.42% Branches 64/70
100% Functions 14/14
100% Lines 52/52

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                                                                    423x 423x 423x 423x 423x   423x 423x   423x   423x                                       493x       5962x 5962x 132x     5830x       325x 325x 176x   149x 149x 2x   147x         5952x 5952x                                     423x                   6x       6x 6x                                                   423x                       18x     18x 18x                       3411x                       511x       511x 511x 511x                   25x 25x                   6281x                   31x 6x   25x 25x             3432x     3432x   3432x 3432x 3432x 3432x                         10296x 10296x           11901x    
import type { BeMusicJSON } from '@be-music/chart-source';
 
/**
 * LR2 judge windows.
 *
 * Each compat ruleset owns its judge windows (signed, per lane kind and per note context; the tables live in
 * `src/ruleset/definitions.ts`). This module only resolves LR2's own `#RANK` / `#DEFEXRANK` / `#EXRANKxx` axis, which
 * the LR2 ruleset reads.
 *
 * Source of truth: LR2 measurements collected on hitkey's diary (2015-01-19 "Memo: LR2 timegates",
 * https://hitkey.nekokan.dyndns.info/diary1501.php#D150119) and the machine-readable LR2-compat tables in lr2oraja
 * (`JudgeProperty.java`, `LR2_SCALING`):
 *
 * | #RANK         | PGREAT | GREAT | GOOD  | BAD   |
 * | ------------- | ------ | ----- | ----- | ----- |
 * | 0 VERY HARD   | ±8ms   | ±24ms | ±40ms | ±200ms|
 * | 1 HARD        | ±15ms  | ±30ms | ±60ms | ±200ms|
 * | 2 NORMAL      | ±18ms  | ±40ms | ±100ms| ±200ms|
 * | 3 EASY        | ±21ms  | ±60ms | ±120ms| ±200ms|
 * | 4 VERY EASY   | = NORMAL (LR2 treats #RANK 4 as NORMAL — lr2oraja README) |
 *
 * The BAD window is FIXED at ±200ms for every rank; only PGREAT / GREAT / GOOD scale. Scratch shares the key
 * windows (no widened scratch gate in LR2).
 *
 * Percent axis: ranks sit on a "judgerank percent" axis (VERY HARD = 25, HARD = 50, NORMAL = 75, EASY = 100) and
 * fractional percents (from bmson `judge_rank` and PRACTICE's JUDGERANK) interpolate piecewise-linearly between the
 * measured anchors — the same model lr2oraja uses (`JudgeWindowRule.LR2`). Values beyond EASY extrapolate along the
 * last segment and every scaled window is clamped to the fixed BAD width.
 *
 * `#DEFEXRANK` and `#EXRANKxx` are not on that axis. LR2 has neither command, and nanasigroove defines their value as
 * a percentage of `#RANK 2`, so the LR2 ruleset scales the `#RANK 2` windows by it: `#DEFEXRANK 120` is ±21.6 / ±48 /
 * ±120 ms (#1190). This is a Be-Music extension of LR2, which takes the unit from the command's own definition instead
 * of walking LR2's other ranks.
 */
const LR2_BAD_WINDOW_MS = 200;
const LR2_JUDGE_RANK_ANCHOR_PERCENTS = [0, 25, 50, 75, 100] as const;
const LR2_PGREAT_ANCHORS_MS = [0, 8, 15, 18, 21] as const;
const LR2_GREAT_ANCHORS_MS = [0, 24, 30, 40, 60] as const;
const LR2_GOOD_ANCHORS_MS = [0, 40, 60, 100, 120] as const;
/** `#RANK 0..4` → judgerank percent. `#RANK 4` (VERY EASY) maps onto NORMAL, mirroring LR2. */
const LR2_BMS_RANK_JUDGERANK_PERCENTS = [25, 50, 75, 100, 75] as const;
const LR2_NORMAL_JUDGERANK_PERCENT = LR2_BMS_RANK_JUDGERANK_PERCENTS[2];
/** The `#RANK 2` windows `[PGREAT, GREAT, GOOD]`, which `#DEFEXRANK` and `#EXRANKxx` scale. */
const LR2_NORMAL_WINDOWS_MS = [LR2_PGREAT_ANCHORS_MS[3], LR2_GREAT_ANCHORS_MS[3], LR2_GOOD_ANCHORS_MS[3]] as const;
/** bmson `judge_rank` default per the 1.0.0 spec (100 = the player's default width, i.e. NORMAL here). */
const BMSON_DEFAULT_JUDGERANK = 100;
 
/** LR2's judge windows in milliseconds: each judgment's half-width, the same early and late. */
export interface JudgeWindowsMs {
  pgreat: number;
  great: number;
  good: number;
  bad: number;
}
 
/**
 * Converts a `#DEFEXRANK` / `#EXRANKxx` / bmson `judge_rank` value to the internal judgerank percent.
 *
 * All three share the same unit: a percentage with "100 = NORMAL" (hitkey command memo for `#DEFEXRANK`: "The value
 * 100 corresponds to #RANK 2"; bmson 1.0.0: 100 = the player's default). NORMAL sits at
 * `LR2_NORMAL_JUDGERANK_PERCENT` (= 75) on the internal axis, so the conversion is `value × 75 / 100`. Every
 * consumer that turns such a value into judge windows must route through this function — it is the single place the
 * unit is defined.
 */
export function bmsExRankValueToJudgeRankPercent(value: number): number {
  return (value * LR2_NORMAL_JUDGERANK_PERCENT) / 100;
}
 
function resolveBMSJudgeRankPercent(json: BeMusicJSON): number {
  const defExRank = json.bms.defExRank;
  if (typeof defExRank === 'number' && Number.isFinite(defExRank) && defExRank > 0) {
    return bmsExRankValueToJudgeRankPercent(defExRank);
  }
 
  return resolveBMSRankJudgeRankPercent(json.metadata.rank ?? Number.NaN);
}
 
function resolveBmsonJudgeRankPercent(json: BeMusicJSON): number {
  const judgeRank = json.bmson.info.judgeRank;
  if (Number.isFinite(judgeRank) && (judgeRank ?? 0) > 0) {
    return bmsExRankValueToJudgeRankPercent(judgeRank!);
  }
  const metadataRank = json.metadata.rank;
  if (Number.isFinite(metadataRank) && (metadataRank ?? 0) > 0) {
    return bmsExRankValueToJudgeRankPercent(metadataRank!);
  }
  return bmsExRankValueToJudgeRankPercent(BMSON_DEFAULT_JUDGERANK);
}
 
/** A BMS `#RANK` on the internal judgerank percent axis; `#RANK 4` and anything outside 0-4 read as NORMAL, as in LR2. */
export function resolveBMSRankJudgeRankPercent(rank: number): number {
  const value = Number.isFinite(rank) ? Math.trunc(rank) : Number.NaN;
  return value >= 0 && value < LR2_BMS_RANK_JUDGERANK_PERCENTS.length
    ? LR2_BMS_RANK_JUDGERANK_PERCENTS[value as 0 | 1 | 2 | 3 | 4]
    : LR2_NORMAL_JUDGERANK_PERCENT;
}
 
/**
 * LR2's NONSTOP rank table: the windows its rank object (op 1001) sets when a later NONSTOP stage's `#RANK` takes over
 * (`LR2body.exe` 0x42be2f, jump table 0x42cb34; OpenLR2 `LR2f.cpp` L12831-L12877). It differs from the table a chart
 * opens with (0x4b5835) for VERY HARD and HARD:
 *
 * | `#RANK`     | PGREAT | GREAT | GOOD   |
 * | ----------- | ------ | ----- | ------ |
 * | 0 VERY HARD | ±12ms  | ±24ms | ±60ms  |
 * | 1 HARD      | ±15ms  | ±30ms | ±80ms  |
 * | 2 NORMAL    | ±18ms  | ±40ms | ±100ms |
 * | 3 EASY      | ±21ms  | ±60ms | ±120ms |
 *
 * Any other `#RANK` reads as NORMAL; BAD stays ±200 ms.
 */
const LR2_NONSTOP_RANK_WINDOWS_MS: Readonly<Record<number, readonly [number, number, number]>> = {
  0: [12, 24, 60],
  1: [15, 30, 80],
  2: [18, 40, 100],
  3: [21, 60, 120],
};
 
/** The windows LR2's NONSTOP rank object sets for a later stage's `#RANK` (see `LR2_NONSTOP_RANK_WINDOWS_MS`). */
export function resolveLR2NonstopRankWindowsMs(rank: number, debugBadWindowMs?: number): JudgeWindowsMs {
  const bad =
    typeof debugBadWindowMs === 'number' && Number.isFinite(debugBadWindowMs) && debugBadWindowMs > 0
      ? debugBadWindowMs
      : LR2_BAD_WINDOW_MS;
  const [pgreat, great, good] =
    LR2_NONSTOP_RANK_WINDOWS_MS[Number.isFinite(rank) ? Math.trunc(rank) : 2] ?? LR2_NONSTOP_RANK_WINDOWS_MS[2]!;
  return {
    pgreat: clampWindow(pgreat, bad),
    great: clampWindow(great, bad),
    good: clampWindow(good, bad),
    bad,
  };
}
 
/** LR2's mania option GAMBOL: LEVEL 1 or LEVEL 2. */
export type LR2GambolLevel = 1 | 2;
 
/**
 * GAMBOL's windows (OpenLR2 `LR2f.cpp` L27612-L27625 as a chart opens, L12863-L12876 at the NONSTOP rank object). LEVEL
 * 1 is the table's own VERY HARD row whatever the chart's rank; LEVEL 2 keeps its PGREAT and narrows GREAT and GOOD to
 * 12 ms. The two tables differ because LR2's NONSTOP rank object has its own VERY HARD (see
 * `LR2_NONSTOP_RANK_WINDOWS_MS`).
 *
 * | Table   | Level | PGREAT | GREAT | GOOD  |
 * | ------- | ----- | ------ | ----- | ----- |
 * | opening | 1     | ±8ms   | ±24ms | ±40ms |
 * | opening | 2     | ±8ms   | ±12ms | ±12ms |
 * | nonstop | 1     | ±12ms  | ±24ms | ±60ms |
 * | nonstop | 2     | ±12ms  | ±12ms | ±12ms |
 */
const LR2_GAMBOL_WINDOWS_MS: Readonly<
  Record<'opening' | 'nonstop', Readonly<Record<LR2GambolLevel, readonly [number, number, number]>>>
> = {
  opening: { 1: [8, 24, 40], 2: [8, 12, 12] },
  nonstop: { 1: [12, 24, 60], 2: [12, 12, 12] },
};
 
/** The windows GAMBOL fixes for the play: `opening` as the chart opens, `nonstop` from a later NONSTOP stage's rank on. */
export function resolveLR2GambolWindowsMs(
  level: LR2GambolLevel,
  table: 'opening' | 'nonstop',
  debugBadWindowMs?: number,
): JudgeWindowsMs {
  const bad =
    typeof debugBadWindowMs === 'number' && Number.isFinite(debugBadWindowMs) && debugBadWindowMs > 0
      ? debugBadWindowMs
      : LR2_BAD_WINDOW_MS;
  const [pgreat, great, good] = LR2_GAMBOL_WINDOWS_MS[table][level];
  return {
    pgreat: clampWindow(pgreat, bad),
    great: clampWindow(great, bad),
    good: clampWindow(good, bad),
    bad,
  };
}
 
export function resolveBMSJudgeWindowsMsForPercent(
  judgeRankPercent: number,
  debugBadWindowMs?: number,
): JudgeWindowsMs {
  return scaleJudgeWindowsMs(judgeRankPercent, debugBadWindowMs);
}
 
/**
 * Resolves the LR2 judge windows for a `#DEFEXRANK` value, or a dynamic `#EXRANKxx` value applied via channel `A0`
 * mid-chart: the `#RANK 2` windows scaled by `value / 100`, BAD fixed, nothing wider than BAD.
 *
 * Both commands share one unit (`RANK 2 = 100`, hitkey command memo), so `#EXRANK 100` lands on exactly the same
 * windows as `#DEFEXRANK 100` (NORMAL), and `50` halves PGREAT, GREAT and GOOD.
 */
export function resolveBMSJudgeWindowsMsForExRankValue(exRankValue: number, debugBadWindowMs?: number): JudgeWindowsMs {
  const bad =
    typeof debugBadWindowMs === 'number' && Number.isFinite(debugBadWindowMs) && debugBadWindowMs > 0
      ? debugBadWindowMs
      : LR2_BAD_WINDOW_MS;
  // A value that is not a positive number changes nothing: the `#RANK 2` windows as they are.
  const percent = Number.isFinite(exRankValue) && exRankValue > 0 ? exRankValue : 100;
  const [pgreat, great, good] = LR2_NORMAL_WINDOWS_MS;
  return {
    pgreat: clampWindow((pgreat * percent) / 100, bad),
    great: clampWindow((great * percent) / 100, bad),
    good: clampWindow((good * percent) / 100, bad),
    bad,
  };
}
 
/** A chart's positive `#DEFEXRANK`, or `undefined` for bmson and for a BMS chart without one. */
function resolveBMSDefExRank(json: BeMusicJSON): number | undefined {
  const defExRank = json.sourceFormat === 'bmson' ? undefined : json.bms.defExRank;
  return typeof defExRank === 'number' && Number.isFinite(defExRank) && defExRank > 0 ? defExRank : undefined;
}
 
/**
 * Resolves the chart's initial judgerank on the internal percent axis (VERY HARD = 25 / HARD = 50 / NORMAL = 75 /
 * EASY = 100) — the same value {@link resolveJudgeWindowsMs} scales its windows from. Exposed so consumers that
 * persist the resolved rank (e.g. the replay recorder) share the exact resolution chain (`#DEFEXRANK` →
 * `metadata.rank` → default for BMS; `info.judge_rank` → `metadata.rank` → default for bmson).
 */
export function resolveJudgeRankPercent(json: BeMusicJSON): number {
  return json.sourceFormat === 'bmson' ? resolveBmsonJudgeRankPercent(json) : resolveBMSJudgeRankPercent(json);
}
 
/**
 * The LR2 judge windows a chart opens with: a BMS chart's positive `#DEFEXRANK` scales the `#RANK 2` windows
 * ({@link resolveBMSJudgeWindowsMsForExRankValue}); any other chart's judgerank ({@link resolveJudgeRankPercent}) is
 * scaled over LR2's measured anchors. `debugBadWindowMs` replaces the fixed ±200 ms BAD window, and no other window
 * grows past it.
 */
export function resolveJudgeWindowsMs(json: BeMusicJSON, debugBadWindowMs?: number): JudgeWindowsMs {
  if (json.sourceFormat === 'bmson') {
    return scaleJudgeWindowsMs(resolveBmsonJudgeRankPercent(json), debugBadWindowMs);
  }
  const defExRank = resolveBMSDefExRank(json);
  return defExRank !== undefined
    ? resolveBMSJudgeWindowsMsForExRankValue(defExRank, debugBadWindowMs)
    : scaleJudgeWindowsMs(resolveBMSRankJudgeRankPercent(json.metadata.rank ?? Number.NaN), debugBadWindowMs);
}
 
function scaleJudgeWindowsMs(judgeRankPercent: number, debugBadWindowMs?: number): JudgeWindowsMs {
  const bad =
    typeof debugBadWindowMs === 'number' && Number.isFinite(debugBadWindowMs) && debugBadWindowMs > 0
      ? debugBadWindowMs
      : LR2_BAD_WINDOW_MS;
  const percent = Number.isFinite(judgeRankPercent) ? Math.max(0, judgeRankPercent) : LR2_NORMAL_JUDGERANK_PERCENT;
  // The anchors are 25 points apart. The final interval also handles extrapolation beyond EASY.
  const step = LR2_JUDGE_RANK_ANCHOR_PERCENTS[1];
  const interval = Math.min(LR2_JUDGE_RANK_ANCHOR_PERCENTS.length - 2, Math.floor(percent / step));
  const offset = percent - interval * step;
  return {
    pgreat: clampWindow(interpolateWindow(LR2_PGREAT_ANCHORS_MS, interval, offset, step), bad),
    great: clampWindow(interpolateWindow(LR2_GREAT_ANCHORS_MS, interval, offset, step), bad),
    good: clampWindow(interpolateWindow(LR2_GOOD_ANCHORS_MS, interval, offset, step), bad),
    bad,
  };
}
 
/**
 * Piecewise-linear interpolation of an LR2 window over the judgerank percent anchors; percents beyond the EASY
 * anchor (100) extrapolate along the final segment, matching lr2oraja's `JudgeWindowRule.LR2`.
 */
function interpolateWindow(anchors: readonly number[], interval: number, offset: number, step: number): number {
  const start = anchors[interval]!;
  return start + (offset * (anchors[interval + 1]! - start)) / step;
}
 
function clampWindow(value: number, badWindowMs: number): number {
  // LR2 never lets a scaled window exceed the fixed BAD gate (ralba: "#DEFEXRANK でどれだけ拡げても BAD の判定幅を
  // 超えない"), and a negative width is meaningless.
  return Math.min(Math.max(0, value), badWindowMs);
}