All files / player-engine/src/core chart-notes-options.ts

97.36% Statements 74/76
95.45% Branches 63/66
100% Functions 19/19
100% Lines 59/59

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                                                                                        343x     343x       10319x         3569x         76x                         3551x 67x   67x   3551x               214x                           3455x 3455x 32x 32x 32x                     14x 14x 1x         50x                             8x 8x 8x 16x 16x 63x 63x     8x   8x 8x 38x 38x 32x   8x 42x 42x   23x 8x 8x 23x 12x 8x 8x 19x 19x 19x 14x 14x                   14x 14x   51x 8x                             14x 13x 13x 75x 13x 117x 156x   13x    
import {
  isLegacyChartPlayOptionProgram,
  type ChartNotesOption,
  type ChartOptionDiagnostic,
  type ChartPlayOptionProgram,
  type ChartPlayVariant,
  type ResolvedChart,
} from '@be-music/chart-model';
import type { TimedLandmineNote, TimedPlayableNote } from '../playable-notes.ts';
import { duplicateBattleChart } from './battle-chart.ts';
import { resolveLaneOptionChannels, type ChannelLike } from './lane-layout.ts';
import { nanasiDoubleSupports } from './nanasi-notes-double.ts';
 
/**
 * Chart-authored `774:NOTES_*` (#932, #1184; docs/adr/0035-chart-authored-notes-options.md). A version 3 gameplay
 * program plays nanasigroove's own conversions:
 *
 * - `DOUBLE`: nanasigroove's lane copies (`doubleNanasiChartNotes`), on 5 / 7 / 9 / 10 / 14 KEY.
 * - `COPY`: the 5 / 7 KEY chart copied onto the 2P side, mirrored, without its mines; 10 / 14 KEY.
 * - `DBLCOPY`: COPY, then DOUBLE on the double-play chart.
 *
 * Versions 1 and 2 keep Be-Music's earlier readings, which replays and records made under them depend on:
 *
 * - `DOUBLE`: every key note (scratch, wheel and FREE ZONE excluded) gains a partner on its mirror lane of the same side,
 *   key `i` of `n` on key `n + 1 - i`, unless something already occupies the mirror lane over the note's span. The
 *   middle key of an odd side has no mirror lane and stays single.
 * - `COPY`: a 5 / 7 KEY chart is copied onto the 2P side, its keys mirrored and its scratch on the 2P scratch, which
 *   makes it a 10 / 14 KEY (double-play) chart (the catalog's DBM: `[11..15, 16]` → 2P `[15..11, 16]`). Elsewhere, and
 *   in a course, whose stages keep one key mode, it does nothing.
 * - `DBLCOPY`: DOUBLE, then COPY.
 *
 * Under both, COPY does nothing in a course (its stages keep one key mode) and DBLCOPY keeps its DOUBLE where its COPY
 * cannot run.
 */
 
/** Where a chart's options run: the variant its notes were authored for, and whether the play is a course stage. */
export interface ChartPlayOptionContext {
  /** The chart's own play variant, before any conversion. */
  playVariant: ChartPlayVariant;
  /** A course stage (GRADE, EXPERT or NONSTOP): its key mode must stay the course's. */
  course: boolean;
}
 
/** The single-play variants COPY makes double-play, and what they become. */
const COPIED_VARIANTS = { '5': '10', '7': '14' } as const;
 
/** The variants DOUBLE has key lanes for (the generic bmson modes have none). */
const DOUBLED_VARIANTS: ReadonlySet<string> = new Set(['5', '7', '9', '10', '14', '24', '48']);
 
/** Whether `option` copies the chart onto the 2P side. */
export function chartNotesCopies(option: ChartNotesOption | undefined): boolean {
  return option === 'COPY' || option === 'DBLCOPY';
}
 
/** Whether `option` doubles the key notes. */
export function chartNotesDoubles(option: ChartNotesOption | undefined): boolean {
  return option === 'DOUBLE' || option === 'DBLCOPY';
}
 
/** Whether a declared NOTES conversion's COPY takes effect: on a 5 / 7 KEY chart outside a course. */
export function chartNotesCopyRuns(option: ChartNotesOption | undefined, context: ChartPlayOptionContext): boolean {
  return chartNotesCopies(option) && !context.course && context.playVariant in COPIED_VARIANTS;
}
 
/**
 * The part of a declared NOTES conversion that takes effect: COPY only on a 5 / 7 KEY chart outside a course, DOUBLE on
 * the variants nanasigroove defines it for (`legacy`, a version 1 / 2 program: every variant with key lanes). DBLCOPY
 * keeps its DOUBLE where its COPY cannot run.
 */
export function resolveChartNotesOption(
  option: ChartNotesOption | undefined,
  context: ChartPlayOptionContext,
  legacy = false,
): ChartNotesOption | undefined {
  if (option === undefined) return undefined;
  const copies = chartNotesCopyRuns(option, context);
  const doubles =
    chartNotesDoubles(option) &&
    (legacy ? DOUBLED_VARIANTS.has(context.playVariant) : nanasiDoubleSupports(context.playVariant));
  return copies ? (doubles ? 'DBLCOPY' : 'COPY') : doubles ? 'DOUBLE' : undefined;
}
 
/** The play variant a chart plays as under its effective NOTES conversion. */
export function resolveChartNotesPlayVariant(
  playVariant: ChartPlayVariant,
  option: ChartNotesOption | undefined,
): ChartPlayVariant {
  return chartNotesCopies(option) && playVariant in COPIED_VARIANTS
    ? COPIED_VARIANTS[playVariant as keyof typeof COPIED_VARIANTS]
    : playVariant;
}
 
/**
 * The chart's gameplay program as it runs in `context`, the form a play records (a replay keeps only what took effect):
 * the NOTES conversion narrowed by {@link resolveChartNotesOption}. A declared conversion that lost its effect is
 * diagnosed `unsupported-option` with its declaration.
 */
export function resolveEffectiveChartPlayOptions(
  program: ChartPlayOptionProgram,
  context: ChartPlayOptionContext,
): { program: ChartPlayOptionProgram; diagnostics: ChartOptionDiagnostic[] } {
  const declared = program.options.notes;
  if (declared === undefined) return { program, diagnostics: [] };
  const effective = resolveChartNotesOption(declared, context, isLegacyChartPlayOptionProgram(program));
  const { notes: _declared, ...options } = program.options;
  return {
    program: {
      version: program.version,
      options: effective === undefined ? options : { ...options, notes: effective },
    },
    diagnostics: effective === declared ? [] : [{ code: 'unsupported-option', declaration: `774:NOTES_${declared}` }],
  };
}
 
/** A copied event's channel: the source event's kind (visible / long note) on `lane` (`1x` / `2x`). */
function channelOnLane(sourceChannel: string, lane: string): string {
  const longNote = sourceChannel[0] === '5' || sourceChannel[0] === '6';
  if (!longNote) return lane;
  return `${lane[0] === '1' ? '5' : '6'}${lane.slice(1)}`;
}
 
/** Whole microseconds, the unit replays and lane options group objects by. */
function toMicroseconds(seconds: number): number {
  return Math.round(seconds * 1_000_000);
}
 
/**
 * The version 1 / 2 DOUBLE in place: adds each key note's mirror-lane partner where the mirror lane is free over the note's span (no note,
 * long note or mine from its head to its tail, ends included). The partners are new notes with their own event identity
 * on the mirror lane (so a replay tells them apart), the same keysound and timing, and no long-note modifier mark (the
 * modifier's placements stay the originals'). Returns the number of notes added.
 */
export function doubleChartNotes(
  notes: TimedPlayableNote[],
  mines: readonly TimedLandmineNote[],
  playVariant: ChartPlayVariant,
  channels: ReadonlyArray<ChannelLike> = notes,
): number {
  Iif (!DOUBLED_VARIANTS.has(playVariant)) return 0;
  const mirror = new Map<string, string>();
  for (const side of ['1P', '2P'] as const) {
    const keys = resolveLaneOptionChannels(side, playVariant, channels);
    keys.forEach((key, index) => {
      const partner = keys[keys.length - 1 - index]!;
      if (partner !== key) mirror.set(key, partner);
    });
  }
  Iif (mirror.size === 0) return 0;
  // Occupied spans per lane in µs, inclusive; a tap is a point.
  const spans = new Map<string, Array<[number, number]>>();
  const occupy = (channel: string, start: number, end: number): void => {
    const list = spans.get(channel);
    if (list) list.push([start, end]);
    else spans.set(channel, [[start, end]]);
  };
  const spanOf = (note: TimedPlayableNote): [number, number] => {
    const start = toMicroseconds(note.seconds);
    return [start, note.endSeconds === undefined ? start : Math.max(start, toMicroseconds(note.endSeconds))];
  };
  for (const note of notes) occupy(note.channel, ...spanOf(note));
  for (const mine of mines) occupy(mine.channel, toMicroseconds(mine.seconds), toMicroseconds(mine.seconds));
  const originals = notes
    .filter((note) => mirror.has(note.channel))
    .sort((a, b) => a.seconds - b.seconds || a.channel.localeCompare(b.channel));
  let added = 0;
  for (const original of originals) {
    const partner = mirror.get(original.channel)!;
    const [start, end] = spanOf(original);
    if ((spans.get(partner) ?? []).some(([from, to]) => from <= end && start <= to)) continue;
    const { generatedLongNote: _generated, ...fields } = original;
    notes.push({
      ...fields,
      channel: partner,
      judged: false,
      event: {
        ...original.event,
        position: [...original.event.position] as [number, number],
        channel: channelOnLane(original.event.channel, partner),
      },
    });
    occupy(partner, start, end);
    added++;
  }
  if (added > 0) notes.sort((a, b) => a.seconds - b.seconds || a.channel.localeCompare(b.channel));
  return added;
}
 
/**
 * COPY: the 5 / 7 KEY chart copied onto the 2P side (`duplicateBattleChart`), then the 2P keys mirrored, so the 2P side
 * is the 1P side's mirror image with the scratch on the 2P scratch. The chart becomes 10 / 14 KEY. nanasigroove leaves
 * the mines on 1P; the version 1 / 2 reading (`legacy`) copies them too.
 */
export function copyChartNotes(
  source: ResolvedChart,
  notes: TimedPlayableNote[],
  mines: TimedLandmineNote[],
  invisible: TimedPlayableNote[],
  legacy = false,
): { chart: ResolvedChart; changedObjectCount: number } {
  if (!(source.playVariant in COPIED_VARIANTS)) throw new RangeError('NOTES COPY requires a 5KEY or 7KEY chart.');
  const copied = duplicateBattleChart(source, notes, mines, invisible, { duplicateMines: legacy });
  const keys = resolveLaneOptionChannels('2P', copied.chart.playVariant);
  const mirror = new Map(keys.map((key, index) => [key, keys[keys.length - 1 - index]!]));
  for (const objects of [notes, mines, invisible] as Array<Array<{ channel: string; seconds: number }>>) {
    for (const object of objects) object.channel = mirror.get(object.channel) ?? object.channel;
    objects.sort((a, b) => a.seconds - b.seconds || a.channel.localeCompare(b.channel));
  }
  return copied;
}