forked from WikiDeck/wikideck
Pure deterministic off-chain engine in shared/game: createMatch, legalActions, applyAction, isOver, result, viewFor, serialize/deserialize, replay. Placeholder Snap-style lanes rules until DESIGN.md v1; keyword hook registry for engine-b. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
74 lines
2.6 KiB
TypeScript
74 lines
2.6 KiB
TypeScript
/**
|
||
* Seeded, serialisable PRNG for the game engine (off-chain only).
|
||
* sfc32 seeded by cyrb128 of a string. The state is four uint32 numbers so it
|
||
* round-trips through JSON and a match can be resumed or replayed exactly.
|
||
*/
|
||
export type RngState = [number, number, number, number];
|
||
|
||
function cyrb128(text: string): RngState {
|
||
let h1 = 1779033703, h2 = 3144134277, h3 = 1013904242, h4 = 2773480762;
|
||
for (let i = 0; i < text.length; i++) {
|
||
const k = text.charCodeAt(i);
|
||
h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
|
||
h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
|
||
h3 = h4 ^ Math.imul(h3 ^ k, 951274213);
|
||
h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);
|
||
}
|
||
h1 = Math.imul(h3 ^ (h1 >>> 18), 597399067);
|
||
h2 = Math.imul(h4 ^ (h2 >>> 22), 2869860233);
|
||
h3 = Math.imul(h1 ^ (h3 >>> 17), 951274213);
|
||
h4 = Math.imul(h2 ^ (h4 >>> 19), 2716044179);
|
||
h1 ^= h2 ^ h3 ^ h4; h2 ^= h1; h3 ^= h1; h4 ^= h1;
|
||
return [h1 >>> 0, h2 >>> 0, h3 >>> 0, h4 >>> 0];
|
||
}
|
||
|
||
export function seedRng(seed: string): RngState {
|
||
const state = cyrb128(seed);
|
||
// Warm up so similar seeds diverge.
|
||
const rng = new Rng(state);
|
||
for (let i = 0; i < 12; i++) rng.nextUint32();
|
||
return rng.state;
|
||
}
|
||
|
||
/** Mutable wrapper over an RngState. The engine keeps `state` inside MatchState. */
|
||
export class Rng {
|
||
state: RngState;
|
||
constructor(state: RngState) { this.state = [state[0] >>> 0, state[1] >>> 0, state[2] >>> 0, state[3] >>> 0]; }
|
||
static fromSeed(seed: string): Rng { return new Rng(seedRng(seed)); }
|
||
|
||
nextUint32(): number {
|
||
let [a, b, c, d] = this.state;
|
||
const t = (((a + b) | 0) + d) | 0;
|
||
d = (d + 1) | 0;
|
||
a = b ^ (b >>> 9);
|
||
b = (c + (c << 3)) | 0;
|
||
c = (c << 21) | (c >>> 11);
|
||
c = (c + t) | 0;
|
||
this.state = [a >>> 0, b >>> 0, c >>> 0, d >>> 0];
|
||
return t >>> 0;
|
||
}
|
||
/** Float in [0, 1). */
|
||
next(): number { return this.nextUint32() / 4294967296; }
|
||
/** Integer in [0, n). Unbiased via rejection sampling. */
|
||
int(n: number): number {
|
||
if (!Number.isInteger(n) || n <= 0 || n > 4294967296) throw new RangeError('Rng.int needs a positive integer bound');
|
||
const limit = 4294967296 - (4294967296 % n);
|
||
let x: number;
|
||
do x = this.nextUint32(); while (x >= limit);
|
||
return x % n;
|
||
}
|
||
pick<T>(items: readonly T[]): T {
|
||
if (!items.length) throw new RangeError('Rng.pick on empty list');
|
||
return items[this.int(items.length)];
|
||
}
|
||
/** Fisher–Yates; returns a new array. */
|
||
shuffle<T>(items: readonly T[]): T[] {
|
||
const out = items.slice();
|
||
for (let i = out.length - 1; i > 0; i--) {
|
||
const j = this.int(i + 1);
|
||
[out[i], out[j]] = [out[j], out[i]];
|
||
}
|
||
return out;
|
||
}
|
||
}
|