All files / player-engine/src/ruleset gauge-type.ts

100% Statements 35/35
96.55% Branches 28/29
100% Functions 11/11
100% Lines 30/30

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                                                                    383x     383x                   383x 383x 383x   383x                         6189x   1656x   1661x   1222x   1650x           16x     383x         383x                                             383x 4596x   9958x           1470x         944x                 891x 23x         16x 7x         10120x                   383x                                     8811x 2x     8809x 7603x 214x                
/**
 * The gauge the player picks, spelled in LR2's names because that is what the skins' `#SRC_BUTTON,type=40 / 41`
 * cycling exposes. Each judge ruleset plays it on a gauge of its own line-up, its gauge id (`resolveRuleset`; a course
 * stage's by `resolveCourseGaugeId`):
 *
 * - `'GROOVE'` — the default recovery gauge; beatoraja and That-Which-Must-Not-Be-Named call the same one `NORMAL`.
 * - `'ASSIST-EASY'` — beatoraja ASSIST EASY / AC ASSISTED EASY recovery gauge.
 * - `'EASY'` — gentler recovery with a lower clear threshold.
 * - `'HARD'` — starts full, drains on misses, fails at the ruleset's death border.
 * - `'EX-HARD'` — independent beatoraja / AC survival gauge, without HARD's low-life softening.
 * - `'G-ATTACK'` — LR2 GOOD ATTACK: GOOD recovers; PGREAT costs the same as missed POOR.
 * - `'P-ATTACK'` — LR2 PERFECT ATTACK: GOOD or worse kills; GREAT drains one percent.
 * - `'DEATH'` — starts full and ends the run on the first break; beatoraja calls it `HAZARD`. LR2 (its HAZARD) also
 *   clears a single chart only with HP 80 or more at the end, as it checks GROOVE (OpenLR2 `LR2f.cpp` L1842-L1862).
 * - `'DEATH-CORRECTED'` — LR2's HAZARD that clears by surviving, without that 80 border. Under the other rulesets it
 *   reads as their DEATH.
 * - `'EX-GROOVE'` / `'EX-EASY'` / `'EX-DEATH'` — plugout's EXNORMAL (clears only at 100 %), EX EASY and EXHAZARD; only
 *   plugout has them. plugout's EXHARD is `'EX-HARD'`.
 */
export type GaugeType =
  | 'GROOVE'
  | 'HARD'
  | 'DEATH'
  | 'DEATH-CORRECTED'
  | 'EASY'
  | 'EX-HARD'
  | 'ASSIST-EASY'
  | 'P-ATTACK'
  | 'G-ATTACK'
  | 'EX-GROOVE'
  | 'EX-EASY'
  | 'EX-DEATH';
 
/** Every gauge type, in the order LR2's gauge button cycles them (`#SRC_BUTTON,type=40 / 41` cells 0..5). */
export const LR2_GAUGE_TYPES: readonly GaugeType[] = ['GROOVE', 'HARD', 'DEATH', 'EASY', 'P-ATTACK', 'G-ATTACK'];
 
/** Every accepted gauge pick; source-specific menus use resolveGaugeTypes. */
export const GAUGE_TYPES: readonly GaugeType[] = [
  ...LR2_GAUGE_TYPES,
  'EX-HARD',
  'ASSIST-EASY',
  'DEATH-CORRECTED',
  'EX-GROOVE',
  'EX-EASY',
  'EX-DEATH',
];
/** LR2's menu: its own six gauges, then the corrected HAZARD. */
const LR2_MENU_GAUGE_TYPES: readonly GaugeType[] = [...LR2_GAUGE_TYPES, 'DEATH-CORRECTED'];
const BEATORAJA_GAUGE_TYPES: readonly GaugeType[] = ['GROOVE', 'HARD', 'EX-HARD', 'DEATH', 'EASY', 'ASSIST-EASY'];
const ____GAUGE_TYPES: readonly GaugeType[] = ['GROOVE', 'HARD', 'EX-HARD', 'EASY', 'ASSIST-EASY'];
/** plugout's four gauges, then their EX variants (NORMAL / HARD / HAZARD / EASY, the select skin's order). */
const PLUGOUT_GAUGE_TYPES: readonly GaugeType[] = [
  'GROOVE',
  'HARD',
  'DEATH',
  'EASY',
  'EX-GROOVE',
  'EX-HARD',
  'EX-DEATH',
  'EX-EASY',
];
 
/** Source menu choices: LR2 has no EX-HARD; the AC menu has no DEATH pick; plugout has no ASSIST EASY. */
export function resolveGaugeTypes(ruleset: GaugeSource): readonly GaugeType[] {
  switch (ruleset) {
    case 'lr2':
      return LR2_MENU_GAUGE_TYPES;
    case 'beatoraja':
      return BEATORAJA_GAUGE_TYPES;
    case 'plugout':
      return PLUGOUT_GAUGE_TYPES;
    default:
      return ____GAUGE_TYPES;
  }
}
 
/** Whether `value` names a gauge type. */
export function isGaugeType(value: unknown): value is GaugeType {
  return typeof value === 'string' && GAUGE_TYPE_SET.has(value);
}
 
const GAUGE_TYPE_SET: ReadonlySet<string> = new Set(GAUGE_TYPES);
 
/** The sources a gauge pick can name: the judge rulesets, whose gauge line-ups differ. */
type GaugeSource = 'lr2' | 'beatoraja' | '____' | 'plugout';
 
const GAUGE_SOURCES: readonly GaugeSource[] = ['lr2', 'beatoraja', '____', 'plugout'];
 
/** A gauge of one source, whatever ruleset judges the play: `lr2:HARD`, `beatoraja:EX-HARD`, `____:EASY` (ADR 0019). */
export type QualifiedGaugeType = `${GaugeSource}:${GaugeType}`;
 
/**
 * The gauge a play runs on. A bare {@link GaugeType} is the judge ruleset's own gauge of that name. A
 * {@link QualifiedGaugeType} keeps its source under every judge ruleset: that source's gauge table, TOTAL reading,
 * auto-shift ladder, fallback gauge and course gauges (#1171).
 */
export type GaugePick = GaugeType | QualifiedGaugeType;
 
/** A gauge pick's source (`undefined` for a bare pick) and its gauge type. */
export interface GaugePickParts {
  source: GaugeSource | undefined;
  type: GaugeType;
}
 
/**
 * Every gauge pick and its parts: each bare gauge type, and each source's menu gauges qualified with it. A qualified pick
 * names a gauge its source's own menu offers; it is never mapped to a lookalike. The table is fixed, so a pick is split
 * by one lookup rather than by parsing it on every call.
 */
const GAUGE_PICK_PARTS: ReadonlyMap<string, Readonly<GaugePickParts>> = new Map<string, Readonly<GaugePickParts>>([
  ...GAUGE_TYPES.map((type) => [type, Object.freeze({ source: undefined, type })] as const),
  ...GAUGE_SOURCES.flatMap((source) =>
    resolveGaugeTypes(source).map((type) => [qualifyGaugePick(source, type), Object.freeze({ source, type })] as const),
  ),
]);
 
/** Splits a gauge pick into its source and type, or `undefined` when `value` is no gauge pick. */
export function splitGaugePick(value: unknown): Readonly<GaugePickParts> | undefined {
  return typeof value === 'string' ? GAUGE_PICK_PARTS.get(value) : undefined;
}
 
/** Whether `value` is a gauge pick: a bare gauge type, or one qualified with a source that defines it. */
export function isGaugePick(value: unknown): value is GaugePick {
  return splitGaugePick(value) !== undefined;
}
 
/**
 * The gauge type of a pick, without its source (`lr2:HARD` is HARD): what a skin's label reads. A value that is no
 * gauge pick (an unvalidated log) is returned as it is.
 */
export function gaugePickType(pick: GaugePick): GaugeType {
  // A pick without a source is its own type, whether or not it names one.
  if (typeof pick === 'string' && !pick.includes(':')) return pick as GaugeType;
  return splitGaugePick(pick)?.type ?? (pick as GaugeType);
}
 
/** The source whose gauge `pick` runs under the judge ruleset `ruleset`: its own source, or the ruleset for a bare pick. */
export function resolveGaugePickSource(pick: GaugePick, ruleset: GaugeSource): GaugeSource {
  if (typeof pick === 'string' && !pick.includes(':')) return ruleset;
  return splitGaugePick(pick)?.source ?? ruleset;
}
 
/** `type` as `source`'s gauge. */
export function qualifyGaugePick(source: GaugeSource, type: GaugeType): QualifiedGaugeType {
  return `${source}:${type}`;
}
 
/**
 * Gauge auto-shift policies, beatoraja 0.8.9's (`BMSPlayer`), usable under every ruleset (#933): each shifts along the
 * judge ruleset's own gauge ladder (`resolveGaugeShiftLadder`).
 */
export type GaugeAutoShift = 'NONE' | 'CONTINUE' | 'SURVIVAL_TO_GROOVE' | 'BEST_CLEAR' | 'SELECT_TO_UNDER';
 
/** Every gauge auto-shift policy, in beatoraja's button order. */
export const GAUGE_AUTO_SHIFTS: readonly GaugeAutoShift[] = [
  'NONE',
  'CONTINUE',
  'SURVIVAL_TO_GROOVE',
  'BEST_CLEAR',
  'SELECT_TO_UNDER',
];
 
/**
 * Whether a play ends when its survival gauge empties. NONE stops the play; every other policy keeps judging to the end:
 * CONTINUE on the emptied gauge, SURVIVAL TO GROOVE on the ruleset's default recovery gauge, BEST CLEAR / SELECT TO
 * UNDER on the parallel gauges of the ruleset's ladder. An omitted policy keeps the host-level `endOnFailure` contract.
 * A single play on plugout's gauges (`singlePlayGaugeSource`) always plays on, as plugout does.
 */
export function resolveGaugeEndOnFailure(
  policy: GaugeAutoShift | undefined,
  fallback = true,
  singlePlayGaugeSource?: string,
): boolean {
  if (policy !== undefined && !GAUGE_AUTO_SHIFTS.includes(policy))
    throw new RangeError(`Unknown gauge auto-shift policy: ${String(policy)}`);
  // plugout plays a single chart to its end on an emptied survival gauge: the gauge stays at 0 and nothing stops the
  // play (`plugout4.exe` 0x43a4f1; the single-play gauge path raises no event, unlike the course gauges').
  if (singlePlayGaugeSource === 'plugout') return false;
  if (policy === undefined) return fallback;
  return policy === 'NONE';
}
 
/**
 * The lowest gauge BEST CLEAR / SELECT TO UNDER shift to, in ASSIST EASY / EASY / NORMAL order (beatoraja's), spelled in
 * LR2's names like {@link GaugeType}; each ruleset reads it on its own ladder (`resolveGaugeShiftBottomIndex`).
 */
export type GaugeShiftBottom = 'ASSIST-EASY' | 'EASY' | 'GROOVE';