All files / player-engine/src/ruleset chart-judge.ts

100% Statements 27/27
100% Branches 30/30
100% Functions 8/8
100% Lines 22/22

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                                        419x     16x       4x       76x 304x 304x 304x   76x                       9958x 48x 39x 19x                           9x               9x                       33x 33x 24x 24x                   8921x 8921x 8879x 42x                
import {
  isLegacyChartPlayOptionProgram,
  type ChartPlayOptionProgram,
  type ChartPlayOptions,
} from '@be-music/chart-model';
import { applyCourseJudgeConstraint } from './course.ts';
import type { GaugeSpec, JudgeWindowSetUs, RulesetWindowTables } from './definitions.ts';
 
/**
 * The judgment-related chart-authored CRAZY options a ruleset applies (#844). `legacy` marks a program recorded before
 * version 3, whose options keep their earlier readings: HELLJUDGE halves every window, and GOOD2BAD / GREAT2BAD collapse
 * the windows as beatoraja's `NO_GOOD` / `NO_GREAT`, without touching the gauge.
 */
export type ChartJudgeModifiers = Pick<ChartPlayOptions, 'judge' | 'hellJudge'> & { legacy?: true };
 
/**
 * HELLJUDGE's narrowing of each judgment, `[PGREAT, GREAT, GOOD, BAD]`: nanasigroove 1.552 swaps its judge table for one
 * whose COOL and GREAT boundaries are 3/8 of the normal ones, its GOOD boundary 1/3 and its BAD boundary 1/4. Be-Music
 * keeps each ruleset's own windows and narrows them by those ratios (nanasigroove's COOL is PGREAT here).
 */
export const HELLJUDGE_WINDOW_RATIOS: readonly [number, number, number, number] = [3 / 8, 3 / 8, 1 / 3, 1 / 4];
 
function halve(set: JudgeWindowSetUs): JudgeWindowSetUs {
  const half = ([late, early]: readonly [number, number]): readonly [number, number] => [
    Math.round(late / 2),
    Math.round(early / 2),
  ];
  return { ...set, judges: [half(set.judges[0]), half(set.judges[1]), half(set.judges[2]), half(set.judges[3])] };
}
 
function narrow(set: JudgeWindowSetUs): JudgeWindowSetUs {
  const scale = (index: number): readonly [number, number] => {
    const [late, early] = set.judges[index]!;
    const ratio = HELLJUDGE_WINDOW_RATIOS[index]!;
    return [Math.round(late * ratio), Math.round(early * ratio)];
  };
  return { ...set, judges: [scale(0), scale(1), scale(2), scale(3)] };
}
 
/**
 * Applies a chart's HELLJUDGE to every window table of the play's ruleset: each judgment's window narrows by
 * {@link HELLJUDGE_WINDOW_RATIOS}, notes and long-note ends alike, rounded to whole microseconds. The empty-POOR window
 * stays. GOOD2BAD and GREAT2BAD leave the windows alone: they change the gauge ({@link applyChartGaugeModifiers}).
 */
export function applyChartJudgeModifiers(
  tables: RulesetWindowTables,
  modifiers: ChartJudgeModifiers | undefined,
): RulesetWindowTables {
  if (modifiers === undefined) return tables;
  if (modifiers.legacy === true) return applyLegacyChartJudgeModifiers(tables, modifiers);
  if (modifiers.hellJudge !== true) return tables;
  return {
    note: narrow(tables.note),
    scratch: narrow(tables.scratch),
    longNoteEnd: narrow(tables.longNoteEnd),
    longScratchEnd: narrow(tables.longScratchEnd),
  };
}
 
/** The earlier readings a program before version 3 keeps (see {@link ChartJudgeModifiers}). */
function applyLegacyChartJudgeModifiers(
  tables: RulesetWindowTables,
  modifiers: ChartJudgeModifiers,
): RulesetWindowTables {
  const halved: RulesetWindowTables =
    modifiers.hellJudge === true
      ? {
          note: halve(tables.note),
          scratch: halve(tables.scratch),
          longNoteEnd: halve(tables.longNoteEnd),
          longScratchEnd: halve(tables.longScratchEnd),
        }
      : tables;
  return applyCourseJudgeConstraint(
    halved,
    modifiers.judge === 'GREAT2BAD' ? 'no-great' : modifiers.judge === 'GOOD2BAD' ? 'no-good' : undefined,
  );
}
 
/**
 * Applies a chart's GOOD2BAD or GREAT2BAD to a gauge, as nanasigroove does: the judgments stay what they are, but a GOOD
 * (GOOD2BAD), or a GREAT and a GOOD (GREAT2BAD), moves the gauge as a BAD does. The gauge is the ruleset's own, so the
 * BAD delta is that gauge's.
 */
export function applyChartGaugeModifiers(spec: GaugeSpec, modifiers: ChartJudgeModifiers | undefined): GaugeSpec {
  const judge = modifiers?.judge;
  if (judge === undefined || modifiers?.legacy === true) return spec;
  const [pgreat, great, , bad, poor, emptyPoor] = spec.values;
  return {
    ...spec,
    values: [pgreat, judge === 'GREAT2BAD' ? bad : great, bad, bad, poor, emptyPoor],
  };
}
 
/** The `chartJudge` ruleset option of a chart play program: `{}` when it forces no judge modifier. */
export function resolveChartJudgeOption(program: ChartPlayOptionProgram | undefined): {
  chartJudge?: ChartJudgeModifiers;
} {
  const options = program?.options;
  if (program === undefined || options === undefined || (options.judge === undefined && options.hellJudge !== true))
    return {};
  return {
    chartJudge: {
      ...(options.judge !== undefined ? { judge: options.judge } : {}),
      ...(options.hellJudge === true ? { hellJudge: true as const } : {}),
      ...(isLegacyChartPlayOptionProgram(program) ? { legacy: true as const } : {}),
    },
  };
}