All files / player-engine/src lane-converge.ts

96.66% Statements 58/60
94.11% Branches 48/51
100% Functions 9/9
97.91% Lines 47/48

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                                                    66x 30x 30x 1x 29x 10x         19x 19x 19x 40x 39x 39x 30x   19x 30x 30x 30x     30x 39x 39x 1x 1x 1x 1x   30x 38x 38x 37x   12x 49x   12x 12x 12x   12x   37x 37x   38x 3x 38x   30x 39x 8x 36x 30x 39x 39x     19x    
import { randomizeLR2DoubleNotes, type LR2DoubleRandomOptions } from './lane-lr2-double.ts';
import type { SRandomNote } from './lane-random.ts';
 
/**
 * LR2 beta3 CONVERGE: try the selected lane first for each note; if occupied by
 * a chord or LN (including its tail time), fall back to a random free key lane.
 * Scratch remains fixed unless it is the selected destination. The caller passes
 * the complete side key domain and the source mode's default/selected target.
 */
export function randomizeLR2ConvergeNotes(
  notes: readonly SRandomNote[],
  lanes: readonly string[],
  options: {
    target: string;
    scratch?: string;
    random: () => number;
    /**
     * LR2's nine-key CONVERGE: its fallback draw also reaches raw lane 10, the 2P scratch, outside the playfield
     * (inclusive `GetRand(9)`, OpenLR2 `LR2f.cpp` L27354, L27372), as SCATTER's does.
     */
    outside?: string;
    /** LR2's 2P target may point one lane past the keys, where nothing can hit the note. */
    targetOutside?: boolean;
    lr2Double?: LR2DoubleRandomOptions;
  },
): string[] {
  const channels = notes.map((note) => note.channel);
  const domain = new Set(lanes);
  if (!domain.has(options.target) && options.target !== options.scratch && options.targetOutside !== true)
    throw new RangeError('CONVERGE target must be a key or scratch of this side.');
  if (options.lr2Double !== undefined)
    return randomizeLR2DoubleNotes(notes, lanes, {
      ...options.lr2Double,
      random: options.random,
      target: options.target,
    });
  const held = new Map<string, { source: string; endMs: number }>();
  const rows = new Map<number, number[]>();
  notes.forEach((note, index) => {
    if (!domain.has(note.channel) && note.channel !== options.scratch) return;
    const row = rows.get(note.timeMs);
    if (row) row.push(index);
    else rows.set(note.timeMs, [index]);
  });
  for (const [timeMs, indices] of [...rows].sort(([a], [b]) => a - b)) {
    for (const [target, hold] of held) if (hold.endMs < timeMs) held.delete(target);
    const used = new Set(held.keys());
    const map = new Map<string, string>();
    // Native scratch events are fixed and must reserve the scratch before keys
    // try to converge there. Scratch LNs also reserve it on later timelines.
    for (const index of indices) {
      const note = notes[index]!;
      if (note.channel !== options.scratch || note.kind === 'invisible') continue;
      used.add(note.channel);
      map.set(note.channel, note.channel);
      Eif (note.endTimeMs !== undefined && note.endTimeMs > timeMs)
        held.set(note.channel, { source: note.channel, endMs: note.endTimeMs });
    }
    const place = (note: SRandomNote, visible: boolean): string => {
      let target = map.get(note.channel) ?? [...held].find(([, hold]) => hold.source === note.channel)?.[0];
      if (visible || target === undefined) {
        if (!used.has(options.target)) target = options.target;
        else {
          const free = (options.outside === undefined ? lanes : [...lanes, options.outside]).filter(
            (lane) => !used.has(lane),
          );
          Iif (free.length === 0) throw new RangeError('CONVERGE has more simultaneous events than available lanes.');
          const draw = options.random();
          Iif (!Number.isFinite(draw) || draw < 0 || draw >= 1)
            throw new RangeError('Lane randomness must be in [0, 1).');
          target = free[Math.floor(draw * free.length)]!;
        }
        used.add(target);
        map.set(note.channel, target);
      }
      if (visible && note.endTimeMs !== undefined && note.endTimeMs > timeMs)
        held.set(target, { source: note.channel, endMs: note.endTimeMs });
      return target;
    };
    const visible = indices
      .filter((index) => domain.has(notes[index]!.channel) && notes[index]!.kind !== 'invisible')
      .sort((a, b) => lanes.indexOf(notes[a]!.channel) - lanes.indexOf(notes[b]!.channel));
    for (const index of visible) channels[index] = place(notes[index]!, true);
    for (const index of indices) {
      const note = notes[index]!;
      if (domain.has(note.channel) && note.kind === 'invisible') channels[index] = place(note, false);
    }
  }
  return channels;
}