All files / chart-model/src chart-play-options.ts

100% Statements 72/72
100% Branches 66/66
100% Functions 17/17
100% Lines 56/56

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                              600x     600x     600x                                                               600x       3239x           725x 1717x 725x                   3476x 3157x 3476x 30x 30x   3127x 3476x 301x 300x 173x     2826x 3476x 1283x 899x 626x     1543x 3476x     25x   18x   608x   673x   219x           1288x                 41810x 41810x   2179x 2179x     41810x       41810x                                   3457x 197x 197x 197x 4x 4x   197x 7x 7x   197x 13x 13x   197x                         3455x 3455x 3455x 3455x 3455x         2972x    
import type { ChartOptionTimeline } from './chart-options.ts';
import { resolveChartLegacyOptionAlias } from './chart-option-legacy.ts';
 
/**
 * Chart-authored gameplay options (#844): nanasigroove's `774:RANDOM_*` arrangement and the judgment-related
 * `774:CRAZY_*` options (`GOOD2BAD`, `GREAT2BAD`, `HELLJUDGE`, `ADDMINE`). The source makes them initial-only
 * (`#OPTION`, never A6), so they compile to one fixed set for the whole play, not to a timed program. A chart that sets
 * one wins over the player's own setting of the same item (docs/adr/0033-chart-authored-gameplay-options.md).
 *
 * `774:NOTES_*` (DOUBLE / COPY / DBLCOPY, #932) is compiled here too: DOUBLE adds a mirrored partner to the key notes,
 * COPY copies a single-play chart onto the 2P side as its mirror image (a double-play chart), DBLCOPY does both
 * (docs/adr/0035-chart-authored-notes-options.md). Where they take effect is the engine's to decide
 * (`resolveEffectiveChartPlayOptions` in `@be-music/player-engine`).
 */
 
export const CHART_RANDOM_OPTIONS = ['MIRROR', 'RANDOM', 'RANDOM+', 'ALL', 'ALL+'] as const;
export type ChartRandomOption = (typeof CHART_RANDOM_OPTIONS)[number];
/** `GOOD2BAD` makes the GOOD window a BAD; `GREAT2BAD` the GREAT and GOOD windows. A later one replaces an earlier one. */
export const CHART_JUDGE_MODIFIERS = ['GOOD2BAD', 'GREAT2BAD'] as const;
export type ChartJudgeModifier = (typeof CHART_JUDGE_MODIFIERS)[number];
/** `DOUBLE` doubles the key notes, `COPY` copies the chart onto the 2P side mirrored, `DBLCOPY` does both. */
export const CHART_NOTES_OPTIONS = ['DOUBLE', 'COPY', 'DBLCOPY'] as const;
export type ChartNotesOption = (typeof CHART_NOTES_OPTIONS)[number];
 
/** The effective chart-authored gameplay options of one play. */
export interface ChartPlayOptions {
  /** The lane arrangement the chart forces, replacing the player's lane options on both sides. */
  random?: ChartRandomOption;
  /** The judge-window modifier the chart forces. */
  judge?: ChartJudgeModifier;
  /** `HELLJUDGE`: every judge window at half its width. */
  hellJudge?: true;
  /** `ADDMINE`: landmines added at random blank cells, replacing the player's mine option. */
  addMine?: true;
  /** The note conversion the chart forces, replacing the player's battle conversion when it copies the chart (#932). */
  notes?: ChartNotesOption;
  /** Legacy CRA-RHS sets the chart’s standard playback rate. */
  baseSpeed?: 1.2 | 1.5 | 2;
}
 
/**
 * The recorded form: versioned, so a later profile never reinterprets an older replay. `notes` joined version 1 without a
 * bump. Version 2 records the chart’s BASE-SPEED while keeping the player tempo relative to it. Version 3 reads the
 * CRAZY gameplay options as nanasigroove 1.552 plays them: GOOD2BAD / GREAT2BAD change the gauge instead of the judge
 * windows, HELLJUDGE narrows each judgment by its own ratio, ADDMINE places nanasigroove's mines, and NOTES DOUBLE /
 * COPY / DBLCOPY convert the chart as nanasigroove does. Versions 1 and 2
 * keep their earlier readings (`isLegacyChartPlayOptionProgram`).
 */
export interface ChartPlayOptionProgram {
  version: 1 | 2 | 3;
  options: ChartPlayOptions;
}
 
export const CHART_PLAY_OPTION_PROGRAM_VERSION = 3;
 
/** Whether `program` predates version 3, so its CRAZY gameplay options keep their earlier Be-Music readings. */
export function isLegacyChartPlayOptionProgram(program: ChartPlayOptionProgram): boolean {
  return program.version < 3;
}
 
type Patch = (options: ChartPlayOptions) => ChartPlayOptions;
 
function without<K extends keyof ChartPlayOptions>(options: ChartPlayOptions, ...keys: K[]): ChartPlayOptions {
  const next = { ...options };
  for (const key of keys) delete next[key];
  return next;
}
 
/**
 * How one exact-case modern declaration changes the gameplay options, or `undefined` when it is not one of these.
 * `RANDOM_OFF` clears the arrangement; `CRAZY_OFF` clears every gameplay CRAZY declared before it; `NOTES_OFF` clears the
 * note conversion (`CRAZY_OFF` does not, as in nanasigroove).
 */
export function parseChartPlayOption(declaration: string): Patch | undefined {
  // Every declaration here is a `774:` one; the check keeps the others off the patterns below.
  if (!declaration.startsWith('774:')) return undefined;
  const baseSpeed = declaration.startsWith('CRA-RHS', 4) ? /^774:CRA-RHS([123])$/.exec(declaration) : null;
  if (baseSpeed !== null) {
    const rate = ([1.2, 1.5, 2] as const)[Number(baseSpeed[1]) - 1]!;
    return (options) => ({ ...options, baseSpeed: rate });
  }
  const random = /^774:RANDOM_(.+)$/.exec(declaration)?.[1];
  if (random !== undefined) {
    if (random === 'OFF') return (options) => without(options, 'random');
    return (CHART_RANDOM_OPTIONS as readonly string[]).includes(random)
      ? (options) => ({ ...options, random: random as ChartRandomOption })
      : undefined;
  }
  const notes = /^774:NOTES_(.+)$/.exec(declaration)?.[1];
  if (notes !== undefined) {
    if (notes === 'OFF') return (options) => without(options, 'notes');
    return (CHART_NOTES_OPTIONS as readonly string[]).includes(notes)
      ? (options) => ({ ...options, notes: notes as ChartNotesOption })
      : undefined;
  }
  const crazy = /^774:CRAZY_(.+)$/.exec(declaration)?.[1];
  switch (crazy) {
    case 'GOOD2BAD':
    case 'GREAT2BAD':
      return (options) => ({ ...options, judge: crazy });
    case 'HELLJUDGE':
      return (options) => ({ ...options, hellJudge: true });
    case 'ADDMINE':
      return (options) => ({ ...options, addMine: true });
    case 'OFF':
      return (options) => without(options, 'judge', 'hellJudge', 'addMine');
    default:
      return undefined;
  }
}
 
/** True for a declaration this module consumes (so the display compiler does not diagnose it as unsupported). */
export function isChartPlayOptionDeclaration(declaration: string): boolean {
  return parseChartPlayOption(declaration) !== undefined;
}
 
/**
 * Compile the initial gameplay declarations in source order, later ones winning within a family. Timed (A6) uses are
 * the display compiler's to diagnose as `initial-only-option` (`initial-only-alias` for a legacy ID); they never change
 * this set.
 */
export function compileChartPlayOptions(timeline: Pick<ChartOptionTimeline, 'initial'>): ChartPlayOptionProgram {
  let options: ChartPlayOptions = {};
  for (const declaration of timeline.initial) {
    // The legacy CRA-* judgment, note and NORMAL IDs stand for their modern IDs here (#931).
    const patch = parseChartPlayOption(resolveChartLegacyOptionAlias(declaration) ?? declaration);
    if (patch !== undefined) options = patch(options);
  }
  const crazy =
    options.judge !== undefined ||
    options.hellJudge === true ||
    options.addMine === true ||
    options.notes !== undefined;
  return { version: crazy ? CHART_PLAY_OPTION_PROGRAM_VERSION : options.baseSpeed === undefined ? 1 : 2, options };
}
 
/**
 * The CRAZY gameplay options a player can select in place of a chart's (#1177, #1184, #1185). The player's ADDMINE is
 * the mine option `NANASI_ADD` in `@be-music/player-engine`.
 */
export type PlayerChartPlayOptions = Pick<ChartPlayOptions, 'judge' | 'hellJudge' | 'notes'>;
 
/**
 * `program` with the player's CRAZY gameplay selections added where the chart sets none: the chart's own choice wins
 * within each family (GOOD2BAD / GREAT2BAD, HELLJUDGE, NOTES), as nanasigroove lets a chart's `#OPTION` override the
 * player's. A program that gains a selection is version 3, whose readings are nanasigroove's.
 */
export function withPlayerChartPlayOptions(
  program: ChartPlayOptionProgram,
  player: PlayerChartPlayOptions | undefined,
): ChartPlayOptionProgram {
  if (player === undefined) return program;
  const options: ChartPlayOptions = { ...program.options };
  let changed = false;
  if (player.judge !== undefined && options.judge === undefined) {
    options.judge = player.judge;
    changed = true;
  }
  if (player.hellJudge === true && options.hellJudge !== true) {
    options.hellJudge = true;
    changed = true;
  }
  if (player.notes !== undefined && options.notes === undefined) {
    options.notes = player.notes;
    changed = true;
  }
  return changed ? { version: CHART_PLAY_OPTION_PROGRAM_VERSION, options } : program;
}
 
/**
 * The selections in `effective` that came from the player (#1243): the CRAZY gameplay options a play ran with that the
 * chart's own `declared` options do not set, as {@link withPlayerChartPlayOptions} adds them. `effective` is the program
 * narrowed to the play (a course stage leaves COPY out), so a selection that took no effect is not listed, and a value
 * the play narrowed (DBLCOPY that kept only its DOUBLE) is listed as it ran.
 */
export function playerChartPlayOptionsOf(
  declared: ChartPlayOptions,
  effective: ChartPlayOptions,
): PlayerChartPlayOptions {
  const player: PlayerChartPlayOptions = {};
  if (effective.judge !== undefined && declared.judge === undefined) player.judge = effective.judge;
  if (effective.hellJudge === true && declared.hellJudge !== true) player.hellJudge = true;
  if (effective.notes !== undefined && declared.notes === undefined) player.notes = effective.notes;
  return player;
}
 
/** True when `program` forces anything. */
export function hasChartPlayOptions(program: ChartPlayOptionProgram | undefined): boolean {
  return program !== undefined && Object.keys(program.options).length > 0;
}