All files / player-engine/src/core seven-to-nine.ts

98.64% Statements 73/74
95.23% Branches 60/63
100% Functions 5/5
100% Lines 58/58

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                                          297x   297x                               238x 238x 238x 4x 234x 238x 233x 233x 233x 233x 238x     179x 21x 93x 21x 21x 21x   21x 238x 238x 238x     238x 238x 97x 97x 97x   238x         63x 90x 90x 90x 90x     42x   54x   54x 52x 39x 14x 54x 97x 97x 97x 93x 7x 7x   86x 7x 7x   86x       21x 21x       21x 21x 21x 21x 189x        
import { getPlayModeLayouts } from '@be-music/chart-model';
import type { PreparedPlaybackChartData } from './bootstrap.ts';
import {
  createInputTokenToChannelsMap,
  createLaneBindings,
  resolveLaneDisplayMode,
  type LaneKeyOverrides,
} from './lane-layout.ts';
import { withChartOverrides } from './resolved-chart-overrides.ts';
import type { TimedPlayableNote, TimedLandmineNote } from '../playable-notes.ts';
 
export type SevenToNinePattern = 0 | 1 | 2 | 3 | 4 | 5 | 6;
export type SevenToNineType = 0 | 1 | 2;
export interface SevenToNineOptions {
  sevenToNinePattern?: SevenToNinePattern;
  sevenToNineType?: SevenToNineType;
  hRandomThresholdBPM?: number;
  /** The player's own lane keys, which the nine lanes take (`LaneModeOptions.laneKeys`). */
  laneKeys?: LaneKeyOverrides;
}
 
const SOURCE_CHANNELS = ['11', '12', '13', '14', '15', '18', '19', '16'];
// [first key, scratch, spare], in zero-based POPN lane order.
const PATTERNS = [
  [2, 1, 0],
  [1, 0, 8],
  [2, 0, 1],
  [2, 1, 0],
  [0, 7, 8],
  [0, 8, 7],
  [1, 8, 0],
] as const;
 
/**
 * beatoraja 0.8.9 ModeModifier, after lane randomization. Mutates only the fresh
 * prepared play; source event channels remain the identities recorded by replays.
 * Long-note tails release their reserved destination after that row's mapping.
 */
export function applySevenToNine(chart: PreparedPlaybackChartData, options: SevenToNineOptions = {}): void {
  const pattern = options.sevenToNinePattern ?? 0;
  const type = options.sevenToNineType ?? 0;
  if (!Number.isInteger(pattern) || pattern < 0 || pattern > 6 || !Number.isInteger(type) || type < 0 || type > 2)
    throw new RangeError('Invalid beatoraja 7-to-9 pattern or scratch type.');
  const bpm = options.hRandomThresholdBPM ?? 120;
  if (!Number.isFinite(bpm)) throw new RangeError('Invalid beatoraja 7-to-9 threshold BPM.');
  chart.sevenToNinePattern = pattern;
  chart.sevenToNineType = type;
  chart.sevenToNineThresholdBPM = bpm;
  chart.sevenToNineApplied = pattern > 0 && chart.resolvedChart.playVariant === '7';
  if (!chart.sevenToNineApplied) return;
  // The upstream seven-key decoder has no slot for #17/FREE ZONE. Do not
  // accidentally turn that ignored source object into a judged ninth-mode key.
  const supported = (note: { channel: string }): boolean => SOURCE_CHANNELS.includes(note.channel);
  for (const notes of [chart.notes, chart.landmineNotes, chart.invisibleNotes])
    for (let index = notes.length - 1; index >= 0; index--) if (!supported(notes[index]!)) notes.splice(index, 1);
  chart.laneSoundNotes = chart.laneSoundNotes.filter(supported);
  Eif (chart.shadowedNotes) chart.shadowedNotes = chart.shadowedNotes.filter(supported);
  const [keyLane, scLane, restLane] = PATTERNS[pattern]!;
  // The Java source divides a float, then takes ceil, independent of playback speed.
  const threshold = bpm <= 0 ? 0 : Math.ceil(Math.fround(15000 / Math.fround(bpm)));
  const active = Array<number>(9).fill(-1);
  const ends = Array<number>(9).fill(-1);
  const last = Array<number>(9).fill(-100);
  type Note = TimedPlayableNote | TimedLandmineNote;
  type Cell = { note: Note; tail?: boolean; hidden?: boolean; source: number };
  const rows = new Map<number, Cell[]>();
  const add = (beat: number, cell: Cell): void => {
    const row = rows.get(beat) ?? [];
    row.push(cell);
    rows.set(beat, row);
  };
  for (const [notes, hidden] of [
    [chart.notes, false],
    [chart.landmineNotes, false],
    [chart.invisibleNotes, true],
  ] as const) {
    for (const note of notes) {
      const source = SOURCE_CHANNELS.indexOf(note.channel);
      Iif (source < 0) continue;
      add(note.beat, { note, source, hidden });
      if ('endBeat' in note && note.endBeat !== undefined) add(note.endBeat, { note, source, tail: true });
    }
  }
  for (const [beat, row] of [...rows].sort(([a], [b]) => a - b)) {
    // TimeLine.getTime truncates microseconds to integral milliseconds.
    const now = Math.trunc(chart.resolvedChart.timing.beatToSeconds(beat) * 1000);
    let scratchAtPrimary: boolean;
    if (active[scLane] !== -1 || active[restLane] !== -1) scratchAtPrimary = active[scLane] === 7;
    else if (type === 1) scratchAtPrimary = now - last[scLane]! > threshold || last[scLane]! <= last[restLane]!;
    else if (type === 2) scratchAtPrimary = last[scLane]! <= last[restLane]!;
    else scratchAtPrimary = true;
    for (const cell of row) {
      const destination = cell.source === 7 ? (scratchAtPrimary ? scLane : restLane) : keyLane + cell.source;
      if (!cell.tail) cell.note.channel = `1${destination + 1}`;
      if (cell.hidden) continue;
      if (cell.tail && now === ends[destination]) {
        active[destination] = -1;
        ends[destination] = -1;
      } else {
        if ('endBeat' in cell.note && cell.note.endBeat !== undefined) {
          active[destination] = cell.source;
          Eif (!cell.tail) ends[destination] = Math.trunc(cell.note.endSeconds! * 1000);
        }
        last[destination] = now;
      }
    }
  }
  chart.resolvedChart = withChartOverrides(chart.resolvedChart, { playVariant: '9', layouts: getPlayModeLayouts('9') });
  chart.laneBindings = createLaneBindings(['11', '12', '13', '14', '15', '16', '17', '18', '19'], {
    playVariant: '9',
    laneKeys: options.laneKeys,
  });
  chart.inputTokenToChannels = createInputTokenToChannelsMap(chart.laneBindings);
  chart.activeFreeZoneChannels = new Set();
  chart.scorableNotes = [...chart.notes];
  chart.laneDisplayMode = resolveLaneDisplayMode(
    chart.laneBindings.map((lane) => lane.channel),
    { playVariant: '9' },
  );
}