27 lines
784 B
JavaScript
27 lines
784 B
JavaScript
|
|
// Seeded RNG (mulberry32). All sim/mapgen randomness goes through this so a
|
||
|
|
// shared seed produces identical maps on both peers.
|
||
|
|
export function mulberry32(seed) {
|
||
|
|
let a = seed >>> 0;
|
||
|
|
return function () {
|
||
|
|
a |= 0; a = (a + 0x6D2B79F5) | 0;
|
||
|
|
let t = Math.imul(a ^ (a >>> 15), 1 | a);
|
||
|
|
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
||
|
|
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
export function makeRng(seed) {
|
||
|
|
const f = mulberry32(seed);
|
||
|
|
return {
|
||
|
|
f,
|
||
|
|
int: (a, b) => a + Math.floor(f() * (b - a + 1)),
|
||
|
|
pick: (arr) => arr[Math.floor(f() * arr.length)],
|
||
|
|
chance: (p) => f() < p,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
// UI-side only (room keys, match seeds) — never inside the sim.
|
||
|
|
export function randomSeed() {
|
||
|
|
return Math.floor(Math.random() * 0xffffffff) >>> 0;
|
||
|
|
}
|