All files / player-engine/src/ruleset gauge.ts

98.31% Statements 117/119
98.93% Branches 93/94
100% Functions 15/15
100% Lines 101/101

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            354x 354x 354x 354x 354x 354x                             10631x     10631x       2906057x       10631x 10631x         118779x               10631x 1263x 266x   900x   266x 266x   10631x 10631x 10631x 10631x 11x 11x   10631x         221385x 11065x 2111x 2111x     219274x 221385x 215400x 14x 14x     305370x 221385x 221385x 221385x 44x 215342x 91981x 9269x 9034x 1898x 1898x       215386x 215386x 215386x               4233x 1743x 367x 367x   3866x 4233x 3863x 3863x 3863x 3863x 4233x                 138x 138x 138x 138x       127880x 127878x 15783x   112095x         2744x 2744x 2744x 11987x   2744x 2744x 2744x 2744x 2744x 2744x 2744x         219249x 9x 9x 9x 9x 9x 9x 9x         219249x 219249x 219249x 460x   219249x 1051x 1051x   1x   1050x 1050x     219249x       14x 14x 1x 1x        
import type { GaugeSpec } from './definitions.ts';
 
/**
 * Judgment indices used by every ruleset gauge table, matching beatoraja's ordering:
 * `0` PGREAT / `1` GREAT / `2` GOOD / `3` BAD / `4` missed POOR / `5` empty POOR.
 */
export const GAUGE_JUDGE_PGREAT = 0;
export const GAUGE_JUDGE_GREAT = 1;
export const GAUGE_JUDGE_GOOD = 2;
export const GAUGE_JUDGE_BAD = 3;
export const GAUGE_JUDGE_MISS_POOR = 4;
export const GAUGE_JUDGE_EMPTY_POOR = 5;
 
export type GaugeJudgeIndex = 0 | 1 | 2 | 3 | 4 | 5;
 
/**
 * A gauge running under one ruleset's {@link GaugeSpec}.
 *
 * Shared by the live engine and the replay simulator so a replayed run and the run that produced it agree to the
 * last decimal. The spec already carries the per-judge deltas with the ruleset's TOTAL modifier folded in; this
 * class only applies the runtime parts: low-gauge damage softening (`guts`), clamping, the LR2 death border, and
 * the survival-gauge "0 % is permanent" latch.
 */
export class RulesetGauge {
  value: number;
  /** Set once a survival gauge has bottomed out. The run keeps judging; the gauge stays dead. */
  failedMidPlay = false;
  /** The lives left once a gauge with {@link GaugeSpec.lives} has reached 0; `undefined` before that. */
  livesRemaining: number | undefined;
  private dead = false;
  private activeSpec: GaugeSpec;
 
  get spec(): GaugeSpec {
    return this.activeSpec;
  }
  private fallback: RulesetGauge | undefined;
  private bank: RulesetGauge[] | undefined;
  private selectedIndex = 0;
  private bottomIndex = 0;
  private upperExclusive: number | undefined;
 
  /** Raw units for every parallel gauge; immutable snapshots keep course carries independent. */
  get values(): Record<string, number> | undefined {
    return this.bank && Object.fromEntries(this.bank.map((gauge) => [gauge.spec.id, gauge.value]));
  }
 
  constructor(
    spec: GaugeSpec,
    fallbackSpec?: GaugeSpec,
    bank?: { specs: readonly GaugeSpec[]; bottomIndex: number; upperExclusive?: number },
  ) {
    if (bank !== undefined) {
      this.bank = bank.specs.map((value) => new RulesetGauge(value));
      this.selectedIndex = Math.max(
        0,
        bank.specs.findIndex((value) => value.id === spec.id),
      );
      this.bottomIndex = bank.bottomIndex;
      this.upperExclusive = bank.upperExclusive;
    }
    if (fallbackSpec !== undefined && fallbackSpec.id !== spec.id) this.fallback = new RulesetGauge(fallbackSpec);
    this.activeSpec = spec;
    this.value = spec.float32 === true ? Math.fround(spec.initial) : spec.initial;
    if (spec.survival && spec.initial <= 0) {
      this.dead = true;
      this.failedMidPlay = true;
    }
    if (this.bank !== undefined) this.selectBest();
  }
 
  /** Applies one judgment. `rate` scales the delta (HCN hold ticks use a fraction of a GREAT / BAD). */
  applyJudge(judge: GaugeJudgeIndex, rate = 1): void {
    if (this.bank !== undefined) {
      for (const gauge of this.bank) gauge.applyJudge(judge, rate);
      this.selectBest();
      return;
    }
    // beatoraja GrooveGauge.update updates the hidden gauge too, including on the fatal judgment.
    this.fallback?.applyJudge(judge, rate);
    if (this.dead) return;
    if (this.livesRemaining !== undefined) {
      this.spendLives(this.spec.lives!.costs[judge]! * rate);
      return;
    }
    // beatoraja computes `gauge[judge] * rate` and the guts product in float (`GrooveGauge.Gauge.update`).
    const f32 = this.spec.float32 === true ? Math.fround : (value: number) => value;
    let delta = f32(this.spec.values[judge]! * f32(rate));
    const damageScale = this.spec.damageScale;
    if (delta < 0 && damageScale !== undefined) {
      delta *= damageScale.base + damageScale.perGauge * this.value;
    } else if (delta < 0) {
      for (const step of this.spec.guts) {
        if (step.judges !== undefined && !step.judges.includes(judge)) continue;
        if (step.inclusive === true ? this.value <= step.threshold : this.value < step.threshold) {
          delta = f32(delta * step.multiplier);
          break;
        }
      }
    }
    Iif (this.spec.recoveryDisabled === true && delta > 0) delta = 0;
    this.set(this.value + delta);
    this.selectFallback();
  }
 
  /**
   * Applies a raw percentage delta that bypasses the per-judge table, the guts softening, and the TOTAL modifier —
   * landmine damage is specified directly as a gauge percentage in every ruleset.
   */
  applyRawDelta(delta: number): void {
    if (this.bank !== undefined) {
      for (const gauge of this.bank) gauge.applyRawDelta(delta);
      this.selectBest();
      return;
    }
    this.fallback?.applyRawDelta(delta);
    if (this.dead) return;
    if (this.spec.recoveryDisabled === true && delta > 0) delta = 0;
    if (this.spec.float32 === true) delta = Math.fround(delta);
    const floor = this.spec.mineFloor;
    this.set(delta < 0 && floor !== undefined ? Math.max(floor, this.value + delta) : this.value + delta);
    this.selectFallback();
  }
 
  /**
   * Empties the gauge for good — below the spec's floor — and ignores every later judgment. LR2 does this when a play
   * ends with notes still unjudged after a negative-BPM reversal (`HP = 0`, OpenLR2 `LR2f.cpp:12980-12982`), so the
   * run is always FAILED. Not a mid-play survival death: `failedMidPlay` stays as it was.
   */
  drainToZero(): void {
    this.fallback = undefined;
    for (const gauge of this.bank ?? []) gauge.drainToZero();
    this.value = 0;
    this.dead = true;
  }
 
  cleared(): boolean {
    if (this.livesRemaining !== undefined) return !this.failedMidPlay && this.livesRemaining > 0;
    if (this.spec.survival) {
      return !this.failedMidPlay && this.value > 0 && this.value >= (this.spec.clearBorder ?? 0);
    }
    return this.value >= this.spec.border;
  }
 
  /** BMSPlayer 0.8.9: highest positive qualified gauge; retain the lower-bound gauge when none qualifies. */
  private selectBest(): void {
    const bank = this.bank!;
    let selected = Math.min(this.selectedIndex, this.bottomIndex);
    for (let index = selected; index < (this.upperExclusive ?? bank.length); index++) {
      if (bank[index]!.value > 0 && bank[index]!.cleared()) selected = index;
    }
    this.selectedIndex = selected;
    const gauge = bank[selected]!;
    this.activeSpec = gauge.spec;
    this.value = gauge.value;
    this.dead = gauge.dead;
    this.failedMidPlay = gauge.failedMidPlay;
    this.livesRemaining = gauge.livesRemaining;
  }
 
  /** Source BMSPlayer: at zero switch to NORMAL's existing value, never refill it from survival HP. */
  private selectFallback(): void {
    if (this.value !== 0 || this.fallback === undefined) return;
    const fallback = this.fallback;
    this.fallback = undefined;
    this.activeSpec = fallback.spec;
    this.value = fallback.value;
    this.dead = fallback.dead;
    this.failedMidPlay = fallback.failedMidPlay;
    this.livesRemaining = fallback.livesRemaining;
  }
 
  private set(next: number): void {
    // beatoraja adds in float before it clamps (`GrooveGauge.Gauge.setValue`).
    const sum = this.spec.float32 === true ? Math.fround(next) : next;
    let value = Math.min(this.spec.max, Math.max(this.spec.min, sum));
    if (this.spec.death !== undefined && value < this.spec.death) {
      value = 0;
    }
    if (this.spec.survival && value <= 0) {
      value = 0;
      if (this.spec.lives !== undefined) {
        // plugout's grade NORMAL / EASY: the stage plays on lives instead of failing.
        this.livesRemaining = this.spec.lives.initial;
      } else {
        this.dead = true;
        this.failedMidPlay = true;
      }
    }
    this.value = value;
  }
 
  private spendLives(cost: number): void {
    this.livesRemaining = Math.max(0, this.livesRemaining! - cost);
    if (this.livesRemaining <= 0) {
      this.dead = true;
      this.failedMidPlay = true;
    }
  }
}