All files / player-engine/src/core lr2-sp-to-dp-chart.ts

88.31% Statements 68/77
85.39% Branches 76/89
100% Functions 7/7
86.95% Lines 60/69

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                          339x                     31x   31x 31x 2x 29x 31x 25x 518x 25x   25x                 33x       31x 31x 31x 31x 31x 239x 239x 239x 116x 116x 116x 116x       9x 1x 1x   8x                 115x 115x   25x 25x 25x 25x 25x 25x 239x 121x 121x   5x 4x                         4x 4x 4x   5x   116x 1x 1x   115x 115x       239x 239x 239x   25x 168x 25x 25x                 120x     117x 115x            
import {
  getPlayModeLayouts,
  type ResolvedChart,
  type ResolvedNote,
  type TimedSampleTrigger,
} from '@be-music/chart-model';
import type { BeMusicEvent } from '@be-music/chart-source';
import { collectLR2ChartObjects, formatLR2Lane, type LR2ChartObject } from './lr2-chart-objects.ts';
import { resolveLR2SoundFamilies } from './lr2-sound-families.ts';
import { mergeLR2DPToSP, mergeLR2PMSToSP } from './lr2-dp-to-sp.ts';
import { splitLR2SPToDP } from './lr2-sp-to-dp.ts';
import { withChartOverrides } from './resolved-chart-overrides.ts';
 
const OUTPUT_VARIANTS = { '5': '10', '7': '14', '10': '5', '14': '7', '9': '7' } as const;
 
/**
 * Source SP TO DP (`battle` 3): a 5/7KEY chart is split onto both sides (SPtoDP), a 10/14KEY chart merged onto the 1P
 * side (DPtoSP, shown as DP-TO-SP) and a 9KEY chart folded onto seven keys (PMStoSP, 9-TO-7). No copied notes; hold
 * endpoints stay attached to their moved heads; a note with no lane left becomes background sound.
 */
export function convertLR2SPToDPChart(
  source: ResolvedChart,
  options: { tempo?: number; assist1P?: boolean; assist2P?: boolean } = {},
): { chart: ResolvedChart; changedObjectCount: number; droppedObjectCount: number } {
  Iif (source.sourceFormat !== 'bms' || source.idBase !== 36)
    throw new RangeError('LR2 SP TO DP requires a base-36 BMS source.');
  const sourceVariant = source.playVariant;
  if (sourceVariant.startsWith('generic-') || sourceVariant === '24' || sourceVariant === '48')
    throw new RangeError('SP TO DP requires a 5, 7, 9, 10 or 14KEY source chart.');
  const tempo = options.tempo ?? 1;
  if (!Number.isFinite(tempo) || tempo <= 0) throw new RangeError('SP TO DP requires a positive finite tempo.');
  const objects = collectLR2ChartObjects(source, { tempo, nativeLeadIn: true });
  objects.sort((a, b) => (a.timeMs === b.timeMs ? a.op - b.op : Math.trunc(a.timeMs - b.timeMs)));
  const initialBPM = source.source.metadata.bpm * tempo;
  const ops =
    sourceVariant === '5' || sourceVariant === '7'
      ? splitLR2SPToDP(objects, {
          soundFamilies: resolveLR2SoundFamilies(source.source, source.randomDraws),
          playLevel: Number.parseInt(String(source.source.metadata.playLevel ?? '0'), 10) || 0,
          assist1P: options.assist1P,
          assist2P: options.assist2P,
        })
      : sourceVariant === '9'
        ? mergeLR2PMSToSP(
            objects.map((object) => ({ ...object, mine: object.note?.kind === 'mine' })),
            { initialBPM },
          )
        : mergeLR2DPToSP(objects, { initialBPM, keymode: sourceVariant === '10' ? 10 : 14 });
  const accepted = new Set<number>();
  const lastByLane = new Map<string, { index: number; object: LR2ChartObject }>();
  let changedObjectCount = 0;
  let droppedObjectCount = 0;
  for (const [index, object] of objects.entries()) {
    const op = ops[index]!;
    if (object.note && op !== object.op && (op === 1 || op === -1)) changedObjectCount++;
    if (!object.note || op < 10 || op >= 30) continue;
    if (op !== object.op) changedObjectCount++;
    const channel = formatLR2Lane(op - 10, false);
    const previous = lastByLane.get(channel);
    if (previous !== undefined) {
      // ParseBmsFile's final intArr pass suppresses heads on/inside a prior hold.
      // A normal head's native realTiming_ln is zero; an LN at the same instant
      // can replace that normal head, while two ordinary heads retain the first.
      if (object.timeMs <= (previous.object.holdEndMs ?? 0)) {
        droppedObjectCount++;
        continue;
      }
      Iif (object.timeMs === previous.object.timeMs) {
        if ((object.holdEndMs ?? 0) <= object.timeMs) {
          droppedObjectCount++;
          continue;
        }
        accepted.delete(previous.index);
        droppedObjectCount++;
      }
    }
    accepted.add(index);
    lastByLane.set(channel, { index, object });
  }
  const notes: ResolvedNote[] = [],
    mines: ResolvedNote[] = [];
  const replacements = new Map<BeMusicEvent, TimedSampleTrigger>();
  const appended: TimedSampleTrigger[] = [];
  const droppedEvents = new Set<BeMusicEvent>();
  for (const [index, object] of objects.entries()) {
    if (!object.note || !object.event || object.op < 10 || object.op >= 30) continue;
    const op = ops[index]!;
    if (op === 1 || op === -1) {
      // A merged note with no free lane plays as BGM (a mine's value is zeroed: WAV00); a PMS mine is dropped.
      if (op === -1) droppedEvents.add(object.event);
      else Iif (object.note.kind === 'mine') {
        const event = { ...object.event, channel: '01', value: '00' };
        replacements.set(object.event, {
          event,
          channel: '01',
          sampleKey: '00',
          samplePath: source.source.resources.wav['00'],
          sampleOffsetSeconds: 0,
          beat: object.note.beat,
          seconds: object.note.seconds,
        });
        // A mine has no trigger of its own; the WAV00 sound is a new one.
        appended.push(replacements.get(object.event)!);
      } else Eif (object.trigger) {
        const event = { ...object.event, channel: '01' };
        replacements.set(object.event, { ...object.trigger, channel: '01', event });
      }
      continue;
    }
    if (!accepted.has(index)) {
      droppedEvents.add(object.event);
      continue;
    }
    const channel = formatLR2Lane(ops[index]! - 10, false);
    const event = {
      ...object.event,
      channel: object.note.kind === 'mine' ? `${channel[0] === '1' ? 'D' : 'E'}${channel[1]}` : channel,
    };
    const note = { ...object.note, channel, event };
    (note.kind === 'mine' ? mines : notes).push(note);
    if (object.trigger) replacements.set(object.event, { ...object.trigger, channel, event });
  }
  const compare = (a: ResolvedNote, b: ResolvedNote): number =>
    a.seconds - b.seconds || a.channel.localeCompare(b.channel);
  const playVariant = OUTPUT_VARIANTS[sourceVariant as keyof typeof OUTPUT_VARIANTS];
  return {
    chart: withChartOverrides(source, {
      playVariant,
      layouts: getPlayModeLayouts(playVariant),
      notes: notes.sort(compare),
      mines: mines.sort(compare),
      shadowedNotes: [],
      sampleTriggers: [
        ...source.sampleTriggers.flatMap((trigger) =>
          droppedEvents.has(trigger.event) ? [] : [replacements.get(trigger.event) ?? trigger],
        ),
        ...appended,
      ].sort((a, b) => a.seconds - b.seconds),
      lastNoteSeconds: notes.reduce((last, note) => Math.max(last, note.endSeconds ?? note.seconds), 0),
    }),
    changedObjectCount,
    droppedObjectCount,
  };
}