All files / player-scene-model/src/play-state lane-cover.ts

98.3% Statements 58/59
92.56% Branches 112/121
100% Functions 13/13
100% Lines 49/49

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                                                    1365x                         233x 233x 307x 307x 307x                   307x                 708x 708x                 194x                                         620x 620x 6x 6x 6x                                                       59x               63x         68x 68x 67x 4x         4x     63x     68x 17x 46x 46x 68x 46x       15x 13x 13x 7x                       124x 116x 116x 114x   113x   68x 68x   15x 15x       30x   30x 30x     113x 113x 113x   116x    
import { normalizeFloatingLaneCover } from '../play-options/floating-hi-speed.ts';
import { clamp } from '@be-music/shared-utils/core';
import { SHUTTER_DEFAULT } from '../play-options/choices.ts';
import type { HiddenSuddenMode, PlayOptions } from '../play-options/options.ts';
import type { LaneCover, PlaySide } from './types.ts';
 
/**
 * One play side's lane covers as the player set them: the SUDDEN+ cover over the top of the lanes, the HIDDEN+ cover
 * above the judgement line and the LIFT that raises the line, each with its height kept while it is switched off, so
 * switching it back on needs no redial. Heights are fractions (0..1) of the lane: LIFT of the whole lane,
 * SUDDEN+ and HIDDEN+ of the part above the LIFT (beatoraja's `LaneRenderer` cover).
 */
export interface LaneCoverState {
  readonly side: PlaySide;
  readonly sudden: number;
  readonly suddenOn: boolean;
  readonly hidden: number;
  readonly hiddenOn: boolean;
  readonly lift: number;
  /** Absent on a hand-built state means its lift is already effective. */
  readonly liftOn?: boolean;
  /** AC measured geometry uses the full lane for SUDDEN+ and does not scale its scroll for LIFT. */
  readonly absolute?: true;
}
 
function clampFraction(value: number): number {
  return Number.isFinite(value) ? clamp(value, 0, 1) : 0;
}
 
/**
 * Each side's covers as the select screen's play options set them (LR2's HIDDEN / SUDDEN buttons): the side's mode
 * picks which ends are covered, the one lane cover height (`shutter`, a quarter by default) covers both, and the LANE
 * COVER switch shows them or not. LIFT follows beatoraja, with a retained 0.1 default while off.
 */
export function laneCoversFromPlayOptions(
  options: Pick<PlayOptions, 'hiddenSudden1P' | 'hiddenSudden2P' | 'shutter' | 'laneCover'> &
    Partial<Pick<PlayOptions, 'lift1P' | 'lift2P' | 'liftOn1P' | 'liftOn2P' | 'floatingHiSpeed'>>,
  sides: readonly PlaySide[],
): LaneCoverState[] {
  const height = clampFraction(options.shutter ?? SHUTTER_DEFAULT);
  return sides.map((side): LaneCoverState => {
    const mode: HiddenSuddenMode = (side === '1P' ? options.hiddenSudden1P : options.hiddenSudden2P) ?? 'OFF';
    const shown = options.laneCover !== false;
    const cover: LaneCoverState = {
      side,
      ...(options.floatingHiSpeed ? { absolute: true as const } : {}),
      sudden: height,
      suddenOn: shown && (mode === 'SUDDEN' || mode === 'HID+SUD'),
      hidden: height,
      hiddenOn: shown && (mode === 'HIDDEN' || mode === 'HID+SUD'),
      lift: clampFraction((side === '1P' ? options.lift1P : options.lift2P) ?? 0.1),
      liftOn: (side === '1P' ? options.liftOn1P : options.liftOn2P) === true,
    };
    return options.floatingHiSpeed ? normalizeFloatingLaneCover(cover) : cover;
  });
}
 
/**
 * One side's covers as drawn, as fractions of the whole lane: SUDDEN+ and HIDDEN+ while on, both scaled
 * to the part of the lane above the LIFT, and the LIFT.
 */
export function resolveLaneCover(cover: LaneCoverState): LaneCover {
  const lift = cover.liftOn === false ? 0 : clampFraction(cover.lift);
  return {
    side: cover.side,
    sudden: cover.suddenOn ? clampFraction(cover.sudden) * (cover.absolute ? 1 : 1 - lift) : 0,
    hidden: cover.hiddenOn ? clampFraction(cover.hidden) * (1 - lift) : 0,
    lift,
  };
}
 
/** The highest a chart's LINEMOVE may raise the judgement line with the player's LIFT, as a fraction of the lane. */
export const CHART_LINE_LIFT_MAX = 0.9;
 
/**
 * What a chart-authored display option asks of the lane covers as drawn (#931, `resolveChartPlayfieldVisual` in
 * `@be-music/chart-model`): HIDDEN_M-SUDDEN shows the player's SUDDEN+ cover at its own height whether or not it is
 * switched on, and LINEMOVE raises the judgement line by `lineLift` (a fraction of the lane) on top of the player's
 * LIFT, up to {@link CHART_LINE_LIFT_MAX}.
 */
export interface ChartLaneCoverEffect {
  readonly manualSudden: boolean;
  readonly lineLift: number;
}
 
/**
 * The covers to draw under a chart's {@link ChartLaneCoverEffect}. The player's own covers stay as they are (a replay
 * records those); only the drawing changes. Returns `covers` itself when the chart changes nothing.
 */
export function applyChartLaneCovers(
  covers: readonly LaneCoverState[],
  effect: ChartLaneCoverEffect,
): readonly LaneCoverState[] {
  const lineLift = Number.isFinite(effect.lineLift) ? Math.max(0, effect.lineLift) : 0;
  if (!effect.manualSudden && lineLift === 0) return covers;
  return covers.map((cover) => {
    const playerLift = cover.liftOn === false ? 0 : clampFraction(cover.lift);
    return {
      ...cover,
      ...(effect.manualSudden ? { suddenOn: true } : {}),
      ...(lineLift > 0 ? { lift: Math.min(CHART_LINE_LIFT_MAX, playerLift + lineLift), liftOn: true } : {}),
    };
  });
}
 
/** The part of a lane cover a command changes. */
export type LaneCoverPart = 'sudden' | 'hidden' | 'lift';
 
/** A change of the lane covers during a play; `side` limits it to one side, else it changes every side. */
export type LaneCoverCommand =
  /**
   * Raise or lower a cover by `delta` of the lane (the lane cover keys, the mouse wheel, a skin's cover button). A
   * SUDDEN+ or HIDDEN+ switched off comes on when raised, from the height it keeps; one lowered to nothing goes off. A
   * LIFT switched off comes on when raised from the judgement line (none of it shows), and lowering it does nothing.
   */
  | { type: 'cover-adjust'; part: LaneCoverPart; delta: number; side?: PlaySide; bpm?: number }
  /**
   * Switch a cover on or off, keeping its height; SUDDEN+/HIDDEN+ without a height start at a quarter of
   * the lane.
   */
  | { type: 'cover-toggle'; part: LaneCoverPart; side?: PlaySide; bpm?: number }
  /** Set the covers outright: what a replay recorded, or a host's setting. */
  | { type: 'cover-set'; covers: readonly Partial<LaneCoverState>[]; side?: PlaySide; recorded?: true };
 
function onKey(part: 'sudden' | 'hidden'): 'suddenOn' | 'hiddenOn' {
  return part === 'sudden' ? 'suddenOn' : 'hiddenOn';
}
 
/**
 * A cover height after a step, on the 1/10000 grid of the replay (so repeated 0.05 or 0.005 steps land on round
 * values instead of drifting through float rounding), clamped to the lane.
 */
function stepHeight(height: number, delta: number): number {
  return clampFraction(Math.round((height + delta) * 10_000) / 10_000);
}
 
function adjust(cover: LaneCoverState, part: LaneCoverPart, delta: number): LaneCoverState {
  // A LIFT switched off shows none, so a step moves it from the judgement line, not from the height it keeps.
  const liftOff = part === 'lift' && cover.liftOn === false;
  if (liftOff && delta <= 0) return cover;
  if (cover.absolute && (part === 'sudden' || part === 'lift')) {
    const next = normalizeFloatingLaneCover({
      ...cover,
      [part]: (liftOff ? 0 : cover[part]) + delta,
      ...(part === 'lift' ? { liftOn: true } : {}),
    });
    return next.sudden === cover.sudden && next.lift === cover.lift && next.liftOn === cover.liftOn ? cover : next;
  }
  const height =
    cover.absolute && part === 'sudden'
      ? Math.max(30 / 723, stepHeight(cover.sudden, delta))
      : stepHeight(liftOff ? 0 : cover[part], delta);
  if (part === 'lift')
    return height === cover.lift && cover.liftOn !== false ? cover : { ...cover, lift: height, liftOn: true };
  const on = onKey(part);
  const next = height <= 0 ? false : delta > 0 ? true : cover[on];
  Iif (height === cover[part] && next === cover[on]) return cover;
  return { ...cover, [part]: height, [on]: next };
}
 
function toggle(cover: LaneCoverState, part: LaneCoverPart): LaneCoverState {
  if (part === 'lift') return { ...cover, liftOn: !(cover.liftOn ?? cover.lift > 0) };
  const on = onKey(part);
  if (cover[on]) return { ...cover, [on]: false };
  return { ...cover, [on]: true, [part]: cover[part] > 0 ? cover[part] : SHUTTER_DEFAULT };
}
 
/**
 * Applies a lane cover change to every side it names. Returns the same array when nothing changes. A course that locks
 * the hi-speed locks the covers too: the caller passes `locked` and every change is ignored.
 */
export function reduceLaneCovers(
  covers: readonly LaneCoverState[],
  command: LaneCoverCommand,
  locked = false,
): readonly LaneCoverState[] {
  if (locked || (command.type === 'cover-adjust' && !Number.isFinite(command.delta))) return covers;
  let changed = false;
  const next = covers.map((cover, index) => {
    if (command.side !== undefined && cover.side !== command.side) return cover;
    let updated: LaneCoverState;
    switch (command.type) {
      case 'cover-adjust':
        updated = adjust(cover, command.part, command.delta);
        break;
      case 'cover-toggle':
        updated = toggle(cover, command.part);
        break;
      case 'cover-set': {
        // An entry that names a side sets that side; one that names none sets the side at its index.
        const set =
          command.covers.find((entry) => entry.side === cover.side) ??
          (command.covers[index]?.side === undefined ? command.covers[index] : undefined);
        updated = set === undefined ? cover : { ...cover, ...set, side: cover.side };
        break;
      }
    }
    if (updated !== cover && updated.absolute) updated = normalizeFloatingLaneCover(updated);
    if (updated !== cover) changed = true;
    return updated;
  });
  return changed ? next : covers;
}