All files / player-engine/src/course stage-rules.ts

97.82% Statements 45/46
97.87% Branches 46/47
100% Functions 9/9
100% Lines 39/39

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                    242x       89x 89x 79x 79x 79x   89x                                                                         20x                                   47x 47x 18x 6x   12x 10x           2x                 49x                                                             11x 9x         13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x         82x                                     35x 30x   35x 35x 27x 35x 27x 35x 35x    
import { qualifyGaugePick, splitGaugePick, type GaugePick } from '../ruleset/gauge-type.ts';
import type { ReplayCourseStage } from '../replay/format.ts';
import type { CourseConstraint, CourseDefinition } from './definition.ts';
import { optionValueName, type QualifiedOptionValue } from '../ruleset/option-source.ts';
 
/*
 * The rules a course sets for each of its stages: the course settings the engine plays a stage under, the lane options
 * it allows, the hi-speed lock and LR2's GRADE resets. The long-note constraint is `applyCourseLongNoteConstraint`.
 */
 
const GAUGE_SETS = { gauge_lr2: 'lr2', gauge_5k: '5k', gauge_7k: '7k', gauge_9k: '9k', gauge_24k: '24k' } as const;
 
/** The course settings every stage plays under (`PlayerOptions.course` without the carried-in state). */
export function resolveCourseStageSettings(definition: CourseDefinition): Omit<ReplayCourseStage, 'carryIn'> {
  const settings: Omit<ReplayCourseStage, 'carryIn'> = { kind: definition.kind };
  for (const constraint of definition.constraints) {
    if (Object.hasOwn(GAUGE_SETS, constraint)) settings.gaugeSet = GAUGE_SETS[constraint as keyof typeof GAUGE_SETS];
    Iif (constraint === 'no_good') settings.judge = 'no-good';
    if (constraint === 'no_great') settings.judge = 'no-great';
  }
  return settings;
}
 
/** The lane options a host offers, as the select screen names them; S-RANDOM and CONVERGE may carry a source. */
export type CourseLaneOption =
  | QualifiedOptionValue<'S-RANDOM' | 'CONVERGE'>
  | 'OFF'
  | 'MIRROR'
  | 'RANDOM'
  | 'R-RANDOM'
  | 'S-RANDOM'
  | 'SPIRAL'
  | 'H-RANDOM'
  | 'ALL-SCR'
  | 'RANDOM-EX'
  | 'S-RANDOM-EX'
  // nanasigroove's, which only a chart's `774:RANDOM_RANDOM+` selects (#1244).
  | 'RANDOM+'
  | 'RANDOM_PLAYABLE'
  | 'S_RANDOM_PLAYABLE'
  | 'CONVERGE'
  | 'SCATTER'
  | 'SCATTER-CORRECTED'
  | 'S-RANDOM-CORRECTED'
  | 'CONVERGE-CORRECTED'
  | 'SYNCHRONIZE-RANDOM'
  | 'SYMMETRY-RANDOM';
 
/** The lane options of a play: each side's lane option and DP FLIP. */
export interface CourseLaneOptions {
  random1P: CourseLaneOption;
  random2P: CourseLaneOption;
  dpFlip: boolean;
}
 
/** beatoraja's random options up to index 5 (`grade_random` keeps them): OFF, MIRROR, RANDOM, R-RANDOM S-RANDOM and SPIRAL here. */
function isPlainRandomFamily(option: CourseLaneOption): boolean {
  return ['OFF', 'MIRROR', 'RANDOM', 'R-RANDOM', 'S-RANDOM', 'SPIRAL'].includes(optionValueName(option));
}
 
/**
 * The lane options a course allows in place of the ones the player picked (beatoraja `MusicSelector._readCourse`):
 *
 * - `grade` turns both sides and DP FLIP off;
 * - `grade_mirror` keeps a 1P MIRROR and then plays 2P on MIRROR with FLIP on; any other 1P pick turns both sides and
 *   FLIP off;
 * - `grade_random` keeps the plain random family (beatoraja's options up to index 5: MIRROR, RANDOM, S-RANDOM here) on
 *   each side, turns anything else off, and leaves FLIP alone.
 *
 * A course without a lane-option constraint keeps the picks.
 */
export function resolveCourseLaneOptions(
  constraints: readonly CourseConstraint[],
  picked: CourseLaneOptions,
): CourseLaneOptions {
  const off: CourseLaneOptions = { random1P: 'OFF', random2P: 'OFF', dpFlip: false };
  if (constraints.includes('grade')) return off;
  if (constraints.includes('grade_mirror')) {
    return picked.random1P === 'MIRROR' ? { random1P: 'MIRROR', random2P: 'MIRROR', dpFlip: true } : off;
  }
  if (constraints.includes('grade_random')) {
    return {
      random1P: isPlainRandomFamily(picked.random1P) ? picked.random1P : 'OFF',
      random2P: isPlainRandomFamily(picked.random2P) ? picked.random2P : 'OFF',
      dpFlip: picked.dpFlip,
    };
  }
  return { ...picked };
}
 
/**
 * Whether a course is an LR2 GRADE course: one read from a `.lr2crs` file or made with the course maker. LR2 resets
 * the player's play options when such a course starts ({@link resolveLR2GradeCoursePlaySettings}); a difficulty
 * table's grade or a beatoraja course is not one.
 */
export function isLR2GradeCourse(definition: Pick<CourseDefinition, 'kind' | 'source'>): boolean {
  return definition.kind === 'grade' && definition.source.format === 'lr2crs';
}
 
/** The play settings LR2 resets when a GRADE course starts. */
export interface LR2GradeResetSettings {
  /** The gauge pick, bare or qualified with its source (`lr2:EASY`). */
  gauge: GaugePick;
  /** Each side's lane cover, as the select screen names it. */
  hiddenSudden1P: 'OFF' | 'HIDDEN' | 'SUDDEN' | 'HID+SUD';
  hiddenSudden2P: 'OFF' | 'HIDDEN' | 'SUDDEN' | 'HID+SUD';
  autoScratch1P: boolean;
  autoScratch2P: boolean;
  dpFlip: boolean;
  /** The play tempo (1 = the chart's own). */
  tempo: number;
}
 
/**
 * The settings an LR2 GRADE course plays its stages with (OpenLR2 `Main.cpp` M1124-M1170, which LR2 applies when the
 * course starts): EASY and G.ATTACK gauge picks play on GROOVE, a source-qualified one on its own source's
 * (`beatoraja:EASY` on `beatoraja:GROOVE`; the reset reads the gauge type, a Be-Music choice no reference defines);
 * HIDDEN / SUDDEN, the assists (auto scratch), DP FLIP and the play tempo (LR2's pitch) are turned off. A course that is not an LR2 GRADE course
 * ({@link isLR2GradeCourse}) keeps the settings. The lane options are reset by the course's `grade` constraint
 * ({@link resolveCourseLaneOptions}). LR2 writes the resets into its config; a host applies them to the course's
 * stages only, so the player's own settings come back afterwards.
 */
export function resolveLR2GradeCoursePlaySettings<T extends LR2GradeResetSettings>(
  definition: Pick<CourseDefinition, 'kind' | 'source'>,
  settings: T,
): T {
  // Spread over `settings` so the other fields a host carries along pass through.
  if (!isLR2GradeCourse(definition)) return settings;
  return resetLR2GradePlaySettings({ ...settings, gauge: settings.gauge });
}
 
function resetLR2GradePlaySettings<T extends LR2GradeResetSettings>(settings: T): T {
  // A bare pick, the common case, needs no split.
  const pick = settings.gauge.includes(':') ? splitGaugePick(settings.gauge) : undefined;
  const type = pick?.type ?? settings.gauge;
  const reset = type === 'EASY' || type === 'G-ATTACK';
  if (reset) settings.gauge = pick?.source === undefined ? 'GROOVE' : qualifyGaugePick(pick.source, 'GROOVE');
  settings.hiddenSudden1P = 'OFF';
  settings.hiddenSudden2P = 'OFF';
  settings.autoScratch1P = false;
  settings.autoScratch2P = false;
  settings.dpFlip = false;
  settings.tempo = 1;
  return settings;
}
 
/** Whether the course forbids changing the hi-speed during play (`no_speed`). */
export function isCourseHiSpeedLocked(constraints: readonly CourseConstraint[]): boolean {
  return constraints.includes('no_speed');
}
 
/** The player's picks a course may override for a stage: the lane options and what LR2 resets on a GRADE course. */
export interface CourseStagePlaySettings extends CourseLaneOptions, LR2GradeResetSettings {
  lr2RandomScratch1P?: boolean;
  lr2RandomScratch2P?: boolean;
}
 
/**
 * The settings a stage plays with in place of the player's picks: the lane options the course allows
 * ({@link resolveCourseLaneOptions}), then, on an LR2 GRADE course, LR2's resets ({@link resolveLR2GradeCoursePlaySettings}:
 * EASY gauge to GROOVE; HIDDEN / SUDDEN, auto scratch, FLIP and the tempo off). The other fields a host carries along
 * pass through. The player's own settings are left as they are, so they come back after the course.
 */
export function resolveCourseStagePlaySettings<T extends CourseStagePlaySettings>(
  definition: Pick<CourseDefinition, 'kind' | 'source' | 'constraints'>,
  picked: T,
): T {
  const allowScratchRandom = !definition.constraints.some(
    (constraint) => constraint === 'grade' || constraint === 'grade_mirror' || constraint === 'grade_random',
  );
  const settings = { ...picked };
  if (picked.lr2RandomScratch1P !== undefined)
    settings.lr2RandomScratch1P = allowScratchRandom && picked.lr2RandomScratch1P;
  if (picked.lr2RandomScratch2P !== undefined)
    settings.lr2RandomScratch2P = allowScratchRandom && picked.lr2RandomScratch2P;
  if (!allowScratchRandom) Object.assign(settings, resolveCourseLaneOptions(definition.constraints, picked));
  return isLR2GradeCourse(definition) ? resetLR2GradePlaySettings(settings) : settings;
}