barkour/js/rng.js
w1n5t0n 2f60136b36 feat: initial Barkour — movement-PvP dog fighting game
5 dogs with distinct movement physics, seeded procedural arenas with
traps and mineable powerups, smash-style knockback/stocks combat,
local 2P and P2P netplay (PeerJS room keys), procedural art and sfx,
headless sim smoke tests.
2026-06-10 14:26:58 +02:00

26 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;
}