All files / chart-model/src note-rules.ts

100% Statements 22/22
100% Branches 28/28
100% Functions 2/2
100% Lines 20/20

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                              597x       69433x 68773x 68773x 58062x   660x 660x       597x 597x                                                 23209x 23209x 556x   22653x 22653x 22640x 5x   22635x 22635x 2x   22633x    
import { resolveHDXLongNoteMode } from './hdx-long-note-mode.ts';
import {
  normalizeObjectKey,
  type BeMusicEvent,
  type BeMusicJSON,
  type BeMusicSourceFormat,
} from '@be-music/chart-source';
 
/** Long-note mode: 1 = LN (no tail judgment), 2 = CN (tail judged), 3 = HCN (held, gauge drains while released). */
export type LongNoteMode = 1 | 2 | 3;
 
/**
 * Long-note mode when the chart names none: LN, the LR2-aligned default for a missing BMS `#LNMODE`. beatoraja's bmson
 * extension leaves an unspecified `info.ln_type` to the player's default mode, which is the same LN here.
 */
export const DEFAULT_LONG_NOTE_MODE: LongNoteMode = 1;
 
/** Chart-level mode: valid #LNMODE > initial HDX LONGMODE > LN; bmson retains its own ln_type rules. */
export function resolveChartLongNoteMode(json: BeMusicJSON): LongNoteMode {
  if (json.sourceFormat === 'bms') {
    const mode = json.bms.lnMode;
    if (mode === 1 || mode === 2 || mode === 3) return mode;
    return resolveHDXLongNoteMode(json) ?? DEFAULT_LONG_NOTE_MODE;
  }
  const lnType = json.bmson.info.lnType;
  return lnType === 2 || lnType === 3 ? lnType : DEFAULT_LONG_NOTE_MODE;
}
 
/** Gauge damage (a percentage) of a mine whose value carries no usable damage. */
export const DEFAULT_LANDMINE_DAMAGE = 4;
const BASE36_OBJECT_KEY_PATTERN = /^[0-9A-Z]{2}$/;
 
/**
 * The gauge damage (a percentage) a landmine object deals when hit.
 *
 * Mine damage encodes the value in base-36 regardless of the chart's `#BASE` setting (the damage encoding is a
 * chart-format constant, not an indexed-resource lookup), so the ID is normalized under the chart's base only to
 * match the rest of the resource-key handling. LR2 and beatoraja both interpret the value DIRECTLY as the gauge-damage
 * percentage (losak's LR2 mine writeup; jbms-parser passes the raw base-36 value into `MineNote`) — the nanasi-era
 * `value / 2` rule in hitkey's memo is a different lineage and is NOT what LR2 does. `ZZ` (= 1295) therefore wipes any
 * gauge: survival gauges die instantly, GROOVE / EASY hit their 2 % floor.
 *
 * bmson `mine_channels[].notes[].damage` (beatoraja extension) is an explicit per-mine gauge percentage; when present
 * it wins over the BMS value rule because the event value there is the WAV slot, not a damage encoding. `damage: 0` is
 * a valid authored value (a no-damage decoration mine), so the guard checks finiteness rather than truthiness.
 *
 * For a bmson chart (`sourceFormat: 'bmson'`) the value rule never applies: the parser numbers each `mine_channels`
 * WAV slot after the sound channels, so reading it as damage would make the damage depend on how many sound channels
 * the chart has. A bmson mine without a usable `damage` deals zero damage, matching beatoraja.
 */
export function resolveLandmineDamage(
  landmineEvent: Pick<BeMusicEvent, 'value' | 'bmson'>,
  base: 36 | 62 = 36,
  sourceFormat: BeMusicSourceFormat = 'bms',
): number {
  const bmsonDamage = landmineEvent.bmson?.damage;
  if (typeof bmsonDamage === 'number' && Number.isFinite(bmsonDamage) && bmsonDamage >= 0) {
    return bmsonDamage;
  }
  const objectValue = normalizeObjectKey(landmineEvent.value, base);
  if (sourceFormat === 'bmson') return 0;
  if (!BASE36_OBJECT_KEY_PATTERN.test(objectValue)) {
    return DEFAULT_LANDMINE_DAMAGE;
  }
  const parsedDamage = Number.parseInt(objectValue, 36);
  if (!Number.isFinite(parsedDamage) || parsedDamage <= 0) {
    return DEFAULT_LANDMINE_DAMAGE;
  }
  return parsedDamage;
}