All files / skin-beatoraja/src/elements note.ts

89.41% Statements 76/85
83.75% Branches 67/80
100% Functions 10/10
93.15% Lines 68/73

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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374                                                                                                                                                                                                                                                                                                        67x                                                                               156x 117x   39x     39x 39x 39x 39x 39x 39x 39x 39x 39x 39x     39x 39x 156x   156x 156x 156x 156x 156x 156x                                                                                   39x 4x 4x 39x       8x           7x                 156x                                     12x 22x 10x     2x           702x 29x 29x 410x   29x       39x           23x 23x 13x   10x 10x 16x 16x 16x 16x 16x   10x       39x   39x 39x 39x 39x   29x 29x 412x 412x 412x             29x       16x 12x 12x 11x   12x       1648x    
import { clamp } from '@be-music/shared-utils/core';
// Normalizer for the `skin.note` declaration.
//
// Beatoraja's `note` block defines per-lane note rendering: which `image[]` IDs to use for each note kind
// (regular tap, LN start / body / end / active, hidden charge note variants, mines), plus a `dst[]` block
// that holds per-lane (x, y, w, h) rectangles. The same shape covers JSON skins (where `note` is an object)
// and Lua skins (where `skin.note = {...}` builds the same shape at evaluation time).
//
// The skin author keys per-lane sprite IDs by lane index — entry 0 is lane 1 in the play layout, entry 1
// is lane 2, etc. Lane counts vary by play variant: 8 for 7K (7 keys + 1 scratch), 6 for 5K (5 + scratch),
// 9 for PMS, 16 for double-play, 26 for 24-key (with `note-su` / `note-sd` for the two scratches at the
// trailing end). The renderer uses the `playVariant` to know how many entries to consume and which channel
// → lane mapping to apply.
//
// The `dst[]` block can be a flat array (single layout) or a list of `{ if: [op...], values: [rect...] }`
// gates that the renderer evaluates against `activeOps` to pick the active layout — this is how skins
// support multiple lane geometries from the same author file (e.g., "Half Lane" / "Hybrid Lane" / "Separate
// Lane" in the play24 reference).
 
import { isElementVisible } from './base.ts';
 
/**
 * Per-lane sprite-ID lists for each note kind. Each array's index `i` is the lane index (0 = lane 1, 1 =
 * lane 2, …). Empty / missing arrays are returned as `[]` so the renderer can index without `undefined`
 * checks; missing kinds simply mean the skin doesn't draw that note kind on any lane.
 */
export interface BeatorajaNoteSection {
  /** Image-element id this note layer mounts onto (the destination block uses `id = "notes"` to hint placement). */
  id: string;
  /** Per-lane regular-note image IDs. */
  note: ReadonlyArray<string>;
  /** Per-lane long-note start image IDs. */
  lnstart: ReadonlyArray<string>;
  /** Per-lane long-note end image IDs. */
  lnend: ReadonlyArray<string>;
  /** Per-lane hold-charge-note start image IDs (PMS hcn variant). */
  hcnstart: ReadonlyArray<string>;
  /** Per-lane hold-charge-note end image IDs. */
  hcnend: ReadonlyArray<string>;
  /**
   * Raw `lnbody` field. Beatoraja's loader interprets this differently depending on whether
   * the skin uses **legacy** mode or **modern** mode (the latter introduced `lnbodyActive`):
   *
   * - **Legacy** (when `lnbodyActive` is empty): `lnbody` = the "held" body sprite.
   * - **Modern** (when `lnbodyActive` is non-empty): `lnbody` = the "unheld" body sprite.
   *
   * Renderers should consume {@link lnBodyHeld} / {@link lnBodyUnheld} instead of this field
   * directly — they already encode the mode-flip and present a stable per-state slot.
   */
  lnbody: ReadonlyArray<string>;
  /**
   * Raw `lnactive` field. Legacy mode only — beatoraja maps it to the "unheld / failed" slot
   * (`lns[3]`). Renamed to {@link lnbodyActive} in modern mode (where it instead drives the
   * "held" slot). Renderers should use {@link lnBodyHeld} / {@link lnBodyUnheld}.
   */
  lnactive: ReadonlyArray<string>;
  /**
   * Modern-mode "held" body field (audit 1.10). Authored by community skins that follow
   * beatoraja's newer naming convention. When non-empty, the loader switches into modern
   * mode where `lnbody` becomes the "unheld" sprite and `lnbodyActive` is the "held" one.
   */
  lnbodyActive: ReadonlyArray<string>;
  /**
   * Resolved "held" LN body — mirrors beatoraja's `lns[2]`. Modern mode: `lnbodyActive`.
   * Legacy mode: `lnbody`. Use this in the renderer; it already absorbs the mode flip.
   */
  lnBodyHeld: ReadonlyArray<string>;
  /**
   * Resolved "unheld" LN body — mirrors beatoraja's `lns[3]`. Modern mode: `lnbody`.
   * Legacy mode: `lnactive`. Used to paint the body when the player is NOT currently
   * holding the LN (typically the "missed / fading" look).
   */
  lnBodyUnheld: ReadonlyArray<string>;
  /**
   * Raw `hcnbody` field. HCN (hold-charge note, PMS-style) has 4 body states; the field's
   * meaning flips between legacy and modern modes the same way `lnbody` does. See the
   * resolved `hcnBody*` fields below for the post-flip per-state arrays.
   */
  hcnbody: ReadonlyArray<string>;
  /** Legacy "held" HCN — beatoraja's lns[7] in legacy mode. Maps to "unheld" in modern mode. */
  hcnactive: ReadonlyArray<string>;
  /** Legacy "damage" HCN — beatoraja's lns[8] in legacy mode. */
  hcndamage: ReadonlyArray<string>;
  /** Legacy "reactive (after damage)" HCN — beatoraja's lns[9] in legacy mode. */
  hcnreactive: ReadonlyArray<string>;
  /** Modern-mode "held" HCN body field (audit 1.10). When non-empty, switches into modern mode. */
  hcnbodyActive: ReadonlyArray<string>;
  /** Modern-mode "missed / damaged" HCN body field. */
  hcnbodyMiss: ReadonlyArray<string>;
  /** Modern-mode "reactive (recovered after damage)" HCN body field. */
  hcnbodyReactive: ReadonlyArray<string>;
  /**
   * Resolved per-state HCN body slots after the mode flip. Mirrors beatoraja's `lns[6..9]`:
   *
   *   - {@link hcnBodyHeld}     — lns[6]: actively held (modern: `hcnbodyActive`, legacy: `hcnbody`)
   *   - {@link hcnBodyUnheld}   — lns[7]: not held       (modern: `hcnbody`, legacy: `hcnactive`)
   *   - {@link hcnBodyReactive} — lns[8]: recovered      (modern: `hcnbodyReactive`, legacy: `hcndamage`)
   *   - {@link hcnBodyMiss}     — lns[9]: damaged / missed (modern: `hcnbodyMiss`, legacy: `hcnreactive`)
   */
  hcnBodyHeld: ReadonlyArray<string>;
  hcnBodyUnheld: ReadonlyArray<string>;
  hcnBodyReactive: ReadonlyArray<string>;
  hcnBodyMiss: ReadonlyArray<string>;
  /** Per-lane mine image IDs. */
  mine: ReadonlyArray<string>;
  /** Per-lane "hidden cover" image IDs (lift / hidden mode chrome). */
  hidden: ReadonlyArray<string>;
  /** Per-lane "already-judged" note image IDs (used by skins that fade processed notes in place). */
  processed: ReadonlyArray<string>;
  /** Conditional per-lane geometry blocks. Authors may emit a single block (no `if`) or several gated layouts. */
  dst: ReadonlyArray<BeatorajaNoteDestinationBlock>;
  /**
   * Per-marker destination prototypes. Each kind references one or more `image[].id` and an `(x,
   * y, w, h)` rect that defines the marker's visual size on the lane. The renderer paints copies
   * of this rect at every chart-time marker (measure boundary / BPM change / STOP / time tick),
   * scrolled to the matching beat with the same hispeed math the note layer uses. Stored as raw
   * destination records so the renderer's existing destination pipeline can ingest them.
   */
  group: ReadonlyArray<Readonly<Record<string, unknown>>>;
  bpm: ReadonlyArray<Readonly<Record<string, unknown>>>;
  stop: ReadonlyArray<Readonly<Record<string, unknown>>>;
  time: ReadonlyArray<Readonly<Record<string, unknown>>>;
  /**
   * Optional pair of integer percentages `[xPct, yPct]` that scale every painted note past the
   * authored `dst[]` rect's natural size. Default 9K's `play9.json` authors `[115, 112]` (= 1.15×
   * width, 1.12× height) for popn-style chunkier notes. `[100, 100]` (or omission) leaves notes
   * at their authored size. Values are clamped to `[1, 1000]` to defensively reject NaN /
   * negative entries.
   */
  expansionRate: { x: number; y: number };
}
 
/** One `dst` group from the note section — gated by `op` codes that must all be active. */
export interface BeatorajaNoteDestinationBlock {
  /** `op` codes evaluated against `activeOps`. Empty array = always active. Negation via negative codes. */
  ifCodes: ReadonlyArray<number>;
  /** Per-lane rectangles. Index 0 = lane 1, index 1 = lane 2, etc. */
  rects: ReadonlyArray<BeatorajaNoteRect>;
}
 
/** A single per-lane rect inside `note.dst[]`. */
export interface BeatorajaNoteRect {
  x: number;
  y: number;
  w: number;
  h: number;
}
 
const EMPTY_SECTION: BeatorajaNoteSection = Object.freeze({
  id: '',
  note: [],
  lnstart: [],
  lnbody: [],
  lnend: [],
  lnactive: [],
  lnbodyActive: [],
  lnBodyHeld: [],
  lnBodyUnheld: [],
  hcnstart: [],
  hcnbody: [],
  hcnend: [],
  hcnactive: [],
  hcndamage: [],
  hcnreactive: [],
  hcnbodyActive: [],
  hcnbodyMiss: [],
  hcnbodyReactive: [],
  hcnBodyHeld: [],
  hcnBodyUnheld: [],
  hcnBodyReactive: [],
  hcnBodyMiss: [],
  mine: [],
  hidden: [],
  processed: [],
  dst: [],
  group: [],
  bpm: [],
  stop: [],
  time: [],
  expansionRate: { x: 100, y: 100 },
});
 
/**
 * Normalize the raw `skin.note` block into a typed structure. Returns an empty section (every list `[]`)
 * when the skin doesn't author a `note` block — the renderer treats that as "no notes drawn", which lets
 * a skin omit lane notes entirely (e.g., a key-config preview skin).
 */
export function normalizeBeatorajaNote(input: unknown): BeatorajaNoteSection {
  if (input === null || input === undefined || typeof input !== 'object' || Array.isArray(input)) {
    return EMPTY_SECTION;
  }
  const obj = input as Readonly<Record<string, unknown>>;
  // Read every raw field first; the resolved per-state slots are then derived from these via
  // beatoraja's mode-flip logic (see comments on `BeatorajaNoteSection`).
  const lnbody = stringArray(obj.lnbody);
  const lnactive = stringArray(obj.lnactive);
  const lnbodyActive = stringArray(obj.lnbodyActive);
  const hcnbody = stringArray(obj.hcnbody);
  const hcnactive = stringArray(obj.hcnactive);
  const hcndamage = stringArray(obj.hcndamage);
  const hcnreactive = stringArray(obj.hcnreactive);
  const hcnbodyActive = stringArray(obj.hcnbodyActive);
  const hcnbodyMiss = stringArray(obj.hcnbodyMiss);
  const hcnbodyReactive = stringArray(obj.hcnbodyReactive);
  // LN mode flip — mirrors `JsonPlaySkinObjectLoader.java:38-43`. The presence of any
  // `lnbodyActive` entry switches the loader into modern mode.
  const lnModern = lnbodyActive.length > 0;
  const lnBodyHeld = lnModern ? lnbodyActive : lnbody;
  const lnBodyUnheld = lnModern ? lnbody : lnactive;
  // HCN mode flip — mirrors `JsonPlaySkinObjectLoader.java:44-54`.
  const hcnModern = hcnbodyActive.length > 0;
  const hcnBodyHeld = hcnModern ? hcnbodyActive : hcnbody;
  const hcnBodyUnheld = hcnModern ? hcnbody : hcnactive;
  const hcnBodyReactive = hcnModern ? hcnbodyReactive : hcndamage;
  const hcnBodyMiss = hcnModern ? hcnbodyMiss : hcnreactive;
  return {
    id: typeof obj.id === 'string' ? obj.id : '',
    note: stringArray(obj.note),
    lnstart: stringArray(obj.lnstart),
    lnbody,
    lnend: stringArray(obj.lnend),
    lnactive,
    lnbodyActive,
    lnBodyHeld,
    lnBodyUnheld,
    hcnstart: stringArray(obj.hcnstart),
    hcnbody,
    hcnend: stringArray(obj.hcnend),
    hcnactive,
    hcndamage,
    hcnreactive,
    hcnbodyActive,
    hcnbodyMiss,
    hcnbodyReactive,
    hcnBodyHeld,
    hcnBodyUnheld,
    hcnBodyReactive,
    hcnBodyMiss,
    mine: stringArray(obj.mine),
    hidden: stringArray(obj.hidden),
    processed: stringArray(obj.processed),
    dst: normalizeNoteDst(obj.dst),
    group: rawObjectArray(obj.group),
    bpm: rawObjectArray(obj.bpm),
    stop: rawObjectArray(obj.stop),
    time: rawObjectArray(obj.time),
    expansionRate: parseExpansionRate(obj.expansionrate),
  };
}
 
/**
 * Parse the `expansionrate` array. Beatoraja authors a 2-element `[xPct, yPct]` (default 9K
 * authors `[115, 112]`). Single-element arrays apply uniformly. Anything malformed / missing
 * collapses to `[100, 100]` (= no scaling), matching the renderer's "no-op when factor is 1"
 * fast path.
 */
function parseExpansionRate(input: unknown): { x: number; y: number } {
  if (!Array.isArray(input)) return { x: 100, y: 100 };
  const x = clampPercent(input[0], 100);
  const y = clampPercent(input[1] ?? input[0], 100);
  return { x, y };
}
 
function clampPercent(value: unknown, fallback: number): number {
  if (typeof value !== 'number' || !Number.isFinite(value)) return fallback;
  // Audit 3.2 — allow `0` so authors can set `expansionrate = [0, 0]` to hide notes entirely
  // (a "preview without notes" debug mode some skins use). The previous `value < 1 → 1`
  // clamp silently lifted 0 to 1, which made an authored-zero scale render at 1% (= almost
  // invisible but not actually invisible). Negative values still clamp up to 0 since they're
  // never meaningful here.
  return clamp(value, 0, 1000);
}
 
/**
 * Coerce a possibly-array of objects to a frozen-shape `Record<string, unknown>[]`. Drops
 * non-object entries — typically a defensive shape check, since the marker arrays are author-
 * shaped destinations and we want them as opaque records.
 */
function rawObjectArray(input: unknown): ReadonlyArray<Readonly<Record<string, unknown>>> {
  Eif (!Array.isArray(input)) return [];
  const out: Array<Readonly<Record<string, unknown>>> = [];
  for (const v of input) {
    if (v !== null && typeof v === 'object' && !Array.isArray(v)) {
      out.push(v as Readonly<Record<string, unknown>>);
    }
  }
  return out;
}
 
/**
 * Pick the per-lane rect block whose `if` codes are satisfied by `activeOps`. Returns the first match in
 * declaration order, falling back to the first block with no `if` (the "default" layout). Returns `[]`
 * when nothing matches — the renderer treats that as "lane geometry undefined" and skips note drawing.
 */
export function pickBeatorajaNoteRects(
  section: BeatorajaNoteSection,
  activeOps: ReadonlySet<number>,
): ReadonlyArray<BeatorajaNoteRect> {
  for (const block of section.dst) {
    if (isElementVisible(block.ifCodes, activeOps)) {
      return block.rects;
    }
  }
  return [];
}
 
// ─── Internals ────────────────────────────────────────────────────────────────────────────────
 
function stringArray(input: unknown): ReadonlyArray<string> {
  if (!Array.isArray(input)) return [];
  const out: string[] = [];
  for (const v of input) {
    out.push(typeof v === 'string' ? v : '');
  }
  return out;
}
 
function normalizeNoteDst(input: unknown): ReadonlyArray<BeatorajaNoteDestinationBlock> {
  if (!Array.isArray(input)) return [];
  // Two valid shapes:
  //   1. Flat list of rect objects → one block with no `if` codes.
  //   2. List of `{ if: [...], values: [...] }` gated blocks.
  // We disambiguate by sniffing the first entry: if it has `values`, the whole list is gated blocks; if
  // it has rect-shaped fields (`x`/`y`/`w`/`h`), the whole list is flat rects.
  const flatBlock = collectFlatRects(input);
  if (flatBlock !== undefined) {
    return [{ ifCodes: [], rects: flatBlock }];
  }
  const blocks: BeatorajaNoteDestinationBlock[] = [];
  for (const entry of input) {
    Iif (entry === null || entry === undefined || typeof entry !== 'object') continue;
    const obj = entry as Readonly<Record<string, unknown>>;
    Iif (!Array.isArray(obj.values)) continue;
    const rects = collectFlatRects(obj.values) ?? [];
    blocks.push({ ifCodes: collectIfCodes(obj.if), rects });
  }
  return blocks;
}
 
function collectFlatRects(input: ReadonlyArray<unknown>): BeatorajaNoteRect[] | undefined {
  Iif (input.length === 0) return undefined;
  // Sniff first entry to distinguish flat from gated.
  const first = input[0];
  Eif (first !== null && typeof first === 'object' && !Array.isArray(first)) {
    const obj = first as Readonly<Record<string, unknown>>;
    if ('values' in obj || 'if' in obj) return undefined;
  }
  const out: BeatorajaNoteRect[] = [];
  for (const entry of input) {
    Iif (entry === null || entry === undefined || typeof entry !== 'object') continue;
    const obj = entry as Readonly<Record<string, unknown>>;
    out.push({
      x: toNumber(obj.x, 0),
      y: toNumber(obj.y, 0),
      w: toNumber(obj.w, 0),
      h: toNumber(obj.h, 0),
    });
  }
  return out;
}
 
function collectIfCodes(input: unknown): ReadonlyArray<number> {
  if (!Array.isArray(input)) return [];
  const out: number[] = [];
  for (const v of input) {
    Eif (typeof v === 'number' && Number.isFinite(v)) out.push(v);
  }
  return out;
}
 
function toNumber(input: unknown, fallback: number): number {
  return typeof input === 'number' && Number.isFinite(input) ? input : fallback;
}