All files / player-scene-model/src/play-state hi-speed.ts

92.22% Statements 83/90
87.55% Branches 183/209
100% Functions 8/8
95.77% Lines 68/71

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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262                                                                                                        221x 21x 221x 34x 20x   19x             221x 221x                                 1178x       1178x         1178x 1178x                                                                                     258x 158x 17x 12x 9x               158x 158x     100x 90x   10x 9x 5x 5x 5x       4x 4x 4x 4x                 9x   7x 9x 9x         4x 3x 3x   3x         4x     3x   3x     39x 29x 4x 4x       25x 3x 2x 2x 1x   22x 22x         16x           16x 1x 1x 1x   15x 15x         9x 9x 9x 9x                     27x     1183x     2x 2x 2x    
import {
  captureFloatingHiSpeed,
  clampFloatingHiSpeed,
  resolveFloatingHiSpeed,
  resolveFloatingHiSpeedExit,
  stepFloatingHiSpeed,
  type FloatingHiSpeedAnchor,
} from '../play-options/floating-hi-speed.ts';
import { resolveNormalHiSpeed, resolveNormalHiSpeedLevel } from '../play-options/normal-hi-speed.ts';
import { DEFAULT_HISPEED, snapHiSpeed, snapClassicHiSpeed, stepPlayerHiSpeed } from '../play-options/hi-speed.ts';
import type { HSFix } from '../play-options/options.ts';
 
/**
 * The hi-speed of a play: the player's, on their own scale (what the select panel, the readouts and the host show and
 * the hi-speed keys step), and the drawn one the notes scroll with: the player's times the chart's HS-FIX multiplier.
 * `locked` while a course forbids the player changing it (`no_speed`).
 */
export interface HiSpeedState {
  /** User state hidden by a compulsory authored spacing multiplier; restored when the override ends. */
  readonly chartBase?: HiSpeedState;
  readonly player: number;
  readonly drawn: number;
  /** The chart's HS-FIX multiplier ({@link resolveHSFixMultiplier}); 1 without HS-FIX. */
  readonly multiplier: number;
  readonly locked: boolean;
  readonly classic?: true;
  readonly normalLevel?: number;
  readonly normalBPM?: number;
  readonly floating?: FloatingHiSpeedAnchor;
  readonly floatingInitial?: FloatingHiSpeedAnchor;
}
 
/** The tempo facts HS-FIX reads off a chart. */
export interface HSFixTempo {
  /** The BPM the player's hi-speed is meant at: the chart's `#BASEBPM`, else its start BPM. */
  readonly referenceBPM?: number;
  /** Every BPM of the chart's tempo map (negative ones count at their magnitude). */
  readonly bpms: readonly number[];
  /** The BPM most notes sit at (beatoraja's MAINBPM). */
  readonly mainBPM?: number;
  /** The chart header BPM, independent of the reference (#BASEBPM) and any beat-zero tempo change. */
  readonly startBPM?: number;
}
 
/**
 * The multiplier HS-FIX scales the player's hi-speed by, so the scroll at another BPM of the chart feels like the
 * player's hi-speed at the reference BPM, as beatoraja's fixed hi-speed modes do (`LaneRenderer.init`): `MAXBPM` keeps
 * the maximum BPM at that speed, `MINBPM` the minimum, `AVERAGE` the main BPM (the BPM most notes sit at, which BPM × N
 * + STOP gimmicks do not skew the way a time average is). LR2 `CONSTANT` uses its separate 150-BPM distance map
 * with multiplier 1. Also 1 without HS-FIX, without a reference BPM or without a positive BPM.
 */
export function resolveHSFixMultiplier(mode: HSFix, tempo: HSFixTempo): number {
  if (mode === 'OFF' || mode === 'CONSTANT') return 1;
  const referenceBPM = tempo.referenceBPM !== undefined && tempo.referenceBPM > 0 ? tempo.referenceBPM : undefined;
  if (referenceBPM === undefined) return 1;
  const bpms = tempo.bpms.map((bpm) => Math.abs(bpm)).filter((bpm) => Number.isFinite(bpm) && bpm > 0);
  if (bpms.length === 0) return 1;
  const target =
    mode === 'STARTBPM'
      ? (tempo.startBPM ?? bpms[0]!)
      : mode === 'MAXBPM'
        ? Math.max(...bpms)
        : mode === 'MINBPM'
          ? Math.min(...bpms)
          : (tempo.mainBPM ?? referenceBPM);
  const multiplier = referenceBPM / target;
  return Number.isFinite(multiplier) && multiplier > 0 ? multiplier : 1;
}
 
/** The hi-speed a play starts with: the player's (the default when none or an invalid one), drawn under HS-FIX. */
export function createHiSpeedState(
  init: {
    hiSpeed?: number;
    multiplier?: number;
    locked?: boolean;
    classic?: boolean;
    normalLevel?: number;
    bpm?: number;
    floating?: boolean;
    visibleFraction?: number;
  } = {},
): HiSpeedState {
  const normal =
    !init.floating && init.normalLevel !== undefined
      ? resolveNormalHiSpeed(init.normalLevel, init.bpm ?? 120)
      : undefined;
  const player =
    normal ??
    (init.floating ? clampFloatingHiSpeed : init.classic ? snapClassicHiSpeed : snapHiSpeed)(
      init.hiSpeed ?? DEFAULT_HISPEED,
    ) ??
    DEFAULT_HISPEED;
  const multiplier = init.multiplier !== undefined && init.multiplier > 0 ? init.multiplier : 1;
  return {
    player,
    drawn: sourceDrawn(player, multiplier, init.classic === true || normal !== undefined || init.floating === true),
    multiplier,
    locked: init.locked === true,
    ...(init.classic ? { classic: true as const } : {}),
    ...(normal !== undefined || (init.floating && (init.normalLevel ?? 0) > 0)
      ? { normalLevel: init.normalLevel!, normalBPM: init.bpm ?? 120 }
      : {}),
    ...(init.floating
      ? {
          floating: captureFloatingHiSpeed(player, init.bpm ?? 120, init.visibleFraction ?? 1),
          floatingInitial: captureFloatingHiSpeed(player, init.bpm ?? 120, init.visibleFraction ?? 1),
        }
      : {}),
  };
}
 
/** A change of the hi-speed during a play. */
export type HiSpeedCommand =
  | { type: 'chart-hi-speed'; value?: number }
  /** The player's hi-speed keys: step the player's hi-speed (ignored while locked). */
  | { type: 'hi-speed-step'; delta: number; bpm?: number }
  /** The host sets the player's hi-speed (a settings menu); HS-FIX applies on top. */
  | { type: 'hi-speed-set'; value: number; bpm?: number }
  | { type: 'hi-speed-scale'; multiplier: number }
  | { type: 'floating-speed-mode'; enabled: boolean; bpm: number; visibleFraction: number }
  | { type: 'floating-speed-restore'; bpm: number; visibleFraction: number }
  | { type: 'floating-speed-recalculate'; bpm: number; visibleFraction: number }
  | { type: 'hi-speed-fine'; direction: number; bpm?: number; visibleFraction?: number }
  /**
   * A drawn hi-speed from elsewhere — a replay's recorded view — taken as it is, the player's worked back from it.
   * Taken while locked too, as the replay's recorded covers are (`cover-set`): the lock stops the player changing the
   * view, and a replay shows the view that was played, which a course's lock held where it started.
   */
  | { type: 'hi-speed-drawn'; value: number };
 
/**
 * Applies a hi-speed change. Returns the same state when nothing changes: a non-finite value, a step past either end
 * of the range, or the player's step while the course locks the hi-speed. The host's own setting and a replay's
 * recorded hi-speed are taken even then.
 */
export function reduceHiSpeed(state: HiSpeedState, command: HiSpeedCommand): HiSpeedState {
  if (command.type === 'chart-hi-speed') {
    if (command.value === undefined) return state.chartBase ?? state;
    if (!Number.isFinite(command.value) || command.value < 0.25 || command.value > 100) return state;
    if (state.chartBase && state.drawn === command.value) return state;
    const base = state.chartBase ?? state;
    const {
      classic: _classic,
      normalLevel: _level,
      normalBPM: _bpm,
      floating: _floating,
      floatingInitial: _initial,
      ...kept
    } = base;
    return { ...kept, chartBase: base, player: command.value, drawn: command.value, multiplier: 1, locked: true };
  }
  // Mandatory chart settings own display while active, including over course/user/replay view changes.
  if (state.chartBase) return state;
  switch (command.type) {
    case 'floating-speed-mode': {
      if (state.locked || command.enabled === (state.floating !== undefined)) return state;
      if (command.enabled) {
        const floating = captureFloatingHiSpeed(state.player, command.bpm, command.visibleFraction);
        const player = clampFloatingHiSpeed(state.player);
        return !floating || player === undefined
          ? state
          : { ...state, floating, floatingInitial: floating, player, drawn: player * state.multiplier };
      }
      const next = resolveFloatingHiSpeedExit(state.player, command.bpm, state.classic === true);
      Iif (!next) return state;
      const { floating: _floating, floatingInitial: _initial, normalLevel: _normal, ...kept } = state;
      return {
        ...kept,
        player: next.hiSpeed,
        drawn: next.hiSpeed * state.multiplier,
        ...(next.normalLevel !== undefined ? { normalLevel: next.normalLevel, normalBPM: command.bpm } : {}),
      };
    }
    case 'floating-speed-restore':
    case 'floating-speed-recalculate': {
      if (state.locked || !state.floating) return state;
      const anchor =
        command.type === 'floating-speed-restore' ? (state.floatingInitial ?? state.floating) : state.floating;
      const player = resolveFloatingHiSpeed(anchor, command.bpm, command.visibleFraction);
      return player === undefined || player === state.player
        ? state
        : { ...state, player, drawn: player * state.multiplier };
    }
    case 'hi-speed-fine': {
      if (state.locked || !state.floating) return state;
      const player = stepFloatingHiSpeed(state.player, command.direction, true);
      Iif (player === undefined || player === state.player) return state;
      // An uncovered scratch edits the saved reference, unlike a keyboard step.
      const floating = captureFloatingHiSpeed(
        player,
        command.bpm ?? state.floating.bpm,
        command.visibleFraction ?? state.floating.visibleFraction,
      );
      return { ...state, player, drawn: player * state.multiplier, floating: floating ?? state.floating };
    }
    case 'hi-speed-scale': {
      Iif (!(command.multiplier > 0) || !Number.isFinite(command.multiplier) || command.multiplier === state.multiplier)
        return state;
      return { ...state, multiplier: command.multiplier, drawn: sourceDrawn(state.player, command.multiplier, true) };
    }
    case 'hi-speed-step': {
      if (state.locked) return state;
      if (state.floating) {
        const player = stepFloatingHiSpeed(state.player, command.delta);
        return player === undefined || player === state.player
          ? state
          : { ...state, player, drawn: player * state.multiplier };
      }
      if (state.normalLevel !== undefined) {
        if (!Number.isFinite(command.delta) || command.delta === 0) return state;
        const level = Math.max(1, Math.min(20, state.normalLevel + Math.sign(command.delta)));
        if (level === state.normalLevel) return state;
        return setNormalLevel(state, level, command.bpm ?? state.normalBPM ?? 120);
      }
      const next = stepPlayerHiSpeed(state.player, command.delta, state.multiplier, state.classic);
      return next === undefined || (next.player === state.player && next.drawn === state.drawn)
        ? state
        : { ...state, player: next.player, drawn: next.drawn };
    }
    case 'hi-speed-set': {
      Iif (state.floating) {
        const player = clampFloatingHiSpeed(command.value);
        return player === undefined || player === state.player
          ? state
          : { ...state, player, drawn: player * state.multiplier };
      }
      if (state.normalLevel !== undefined) {
        const bpm = command.bpm ?? state.normalBPM ?? 120;
        const level = resolveNormalHiSpeedLevel(command.value, bpm);
        return level === undefined ? state : setNormalLevel(state, level, bpm);
      }
      const next = stepPlayerHiSpeed(command.value, 0, state.multiplier, state.classic);
      return next === undefined || (next.player === state.player && next.drawn === state.drawn)
        ? state
        : { ...state, player: next.player, drawn: next.drawn };
    }
    case 'hi-speed-drawn': {
      const source = state.floating !== undefined || state.classic || state.normalLevel !== undefined;
      const drawn = source ? validSourceSpeed(command.value) : snapHiSpeed(command.value);
      Iif (drawn === undefined || drawn === state.drawn) return state;
      return {
        ...state,
        drawn,
        player:
          (source ? validSourceSpeed(drawn / state.multiplier) : snapHiSpeed(drawn / state.multiplier)) ?? state.player,
      };
    }
  }
}
 
function validSourceSpeed(value: number): number | undefined {
  return Number.isFinite(value) && value > 0 ? value : undefined;
}
function sourceDrawn(player: number, multiplier: number, source: boolean): number {
  return (source ? validSourceSpeed(player * multiplier) : snapHiSpeed(player * multiplier)) ?? player;
}
function setNormalLevel(state: HiSpeedState, level: number, bpm: number): HiSpeedState {
  const player = resolveNormalHiSpeed(level, bpm);
  Iif (player === undefined) return state;
  return { ...state, normalLevel: level, normalBPM: bpm, player, drawn: sourceDrawn(player, state.multiplier, true) };
}