Files
drip-runner/lib/spawn.js
T

44 lines
2.6 KiB
JavaScript

// The deterministic spawn schedule, shared by the browser and the server (served at /spawn.js).
//
// A run is seeded once by the server. Every drip the courier can ever collect in that run comes
// from this schedule, so the server knows when drip #i appeared and roughly when it could have
// been reached, and can refuse a collection that the schedule says was impossible. Bots and
// firewall gates come from their own streams (seed+1, seed+2): they matter to the player, not
// to the payout, so the server never regenerates them.
(function (root, factory) {
if (typeof module === 'object' && module.exports) module.exports = factory(); else root.DRSpawn = factory();
})(typeof self !== 'undefined' ? self : this, function () {
'use strict';
// mulberry32: tiny, good enough, identical in every JS engine
function rng(seed) {
let a = (seed >>> 0) || 1;
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; };
}
const FLOOR = 92; // px from the bottom to the rail
const PX = 0.27; // the courier's x as a fraction of the width
// drip groups: every call returns the next group { t, drips: [{ i, x0, y }] }
function drips(seed, W, H) {
const r = rng(seed); let t = 0, i = 0; const floorY = H - FLOOR;
return function next() {
t += 0.5 + r() * 0.3;
const arc = r() < 0.35, n = arc ? 4 : 1, y0 = 120 + r() * (floorY - 170);
const g = { t: Math.round(t * 1000) / 1000, drips: [] };
for (let k = 0; k < n; k++) g.drips.push({ i: i++, x0: W + 40 + k * 34, y: y0 - (arc ? Math.sin(k / 3 * Math.PI) * 46 : 0) });
return g;
};
}
function bots(seed, chapter, H) {
const r = rng(seed + 1); let t = 0.8; const floorY = H - FLOOR;
return function () { t += Math.max(0.75, 1.7 - chapter * 0.08) + r() * 0.5; const y = 110 + r() * (floorY - 150); return { t, y, amp: r() < 0.5 ? 0 : 40 + r() * 50, ph: r() * 6 }; };
}
function gates(seed, chapter, H) {
const r = rng(seed + 2); let t = 3; const floorY = H - FLOOR;
return function () { t += 4 + r() * 3; const gap = 190 - Math.min(60, chapter * 6); return { t, y: 140 + r() * (floorY - 160 - gap), gap }; };
}
function goalFor(ch) { return 20 + (ch - 1) * 12; }
function speedFor(ch) { return 230 + (ch - 1) * 14; }
// when drip { x0 } spawned at time s reaches the courier's x at a constant speed
function arrival(s, x0, W, speed) { return s + (x0 - W * PX) / speed; }
return { rng, drips, bots, gates, goalFor, speedFor, arrival, PX, FLOOR };
});