delvewren / core.js
CyberMax-tools's picture
Deploy delvewren
5f9262c verified
Raw History Blame Contribute Delete
9.87 kB
// Dig Drone core: a dig-down upgrade game, no DOM (runs in the page and in node tests).
// Hold to drill down, slide to steer, ores pay coins, coins buy upgrades between runs, a drone digs while you're away.
// Fixed 60 Hz ticks; the ground is a pure function of (seed, row), so the Daily Dig is the same for everyone.
// (c) 2026 CyberMax. All rights reserved.
export const HZ = 60;
export const COLS = 9;
export const TS = 40; // tile size in world units (the view is 360 wide)
export const VIEW_W = COLS * TS, VIEW_H = 540;
export const BIOME_ROWS = 40;
export const T = { AIR: 0, SOIL: 1, ROCK: 2, BOULDER: 3, COPPER: 4, SILVER: 5, GOLD: 6, GEM: 7, FUEL: 8 };
export const ORE = { 4: 1, 5: 3, 6: 8, 7: 25 }; // coins per ore (before the Value upgrade)
const HP = { 1: 1, 2: 3, 3: 9, 4: 1.4, 5: 1.6, 6: 1.8, 7: 2, 8: 0.8 };
export const BIOMES = [
{ name: 'Meadow Soil', soil: '#C98A4B', rock: '#9C7A5B', deep: '#8A5A2E' },
{ name: 'Clay Bank', soil: '#E58A55', rock: '#B66A45', deep: '#A2502A' },
{ name: 'Sandstone', soil: '#EFC266', rock: '#C9A15A', deep: '#B9822E' },
{ name: 'Slate Deep', soil: '#7FA9CC', rock: '#5E7F9E', deep: '#47668A' },
{ name: 'Crystal Caves', soil: '#B49BF5', rock: '#8C74D6', deep: '#6F55BE' },
{ name: 'Ember Rock', soil: '#F2836B', rock: '#C95F4E', deep: '#A8433A' },
{ name: 'Golden Core', soil: '#F7CF5C', rock: '#E0A93B', deep: '#C98A1E' },
];
export const LAYER_EMOJI = ['๐ŸŸซ', '๐ŸŸง', '๐ŸŸจ', '๐ŸŸฆ', '๐ŸŸช', '๐ŸŸฅ', 'โญ'];
export const biomeOf = (row) => Math.min(BIOMES.length - 1, Math.max(0, Math.floor((row - 1) / BIOME_ROWS)));
function hash(str) {
let h = 0x811c9dc5;
for (let i = 0; i < str.length; i++) { h ^= str.charCodeAt(i); h = Math.imul(h, 0x01000193); }
return h >>> 0;
}
export function rng(seed) {
let a = typeof seed === 'string' ? hash(seed) : seed >>> 0;
return function () {
a = (a + 0x6d2b79f5) >>> 0;
let t = a;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
/** One row of ground: COLS tile types. Row 0 is the grassy surface. Pure: the same (seed, row) always gives the same row. */
export function genRow(seed, row) {
if (row <= 0) return new Array(COLS).fill(T.SOIL);
const r = rng(`delvewren:${seed}:${row}`);
const b = biomeOf(row);
const out = [];
for (let c = 0; c < COLS; c++) {
const x = r();
let p = 0, t = T.SOIL;
const pick = (q, type) => { if (t === T.SOIL && x >= p && x < p + q) t = type; p += q; };
pick(row > 3 ? 0.05 : 0, T.AIR);
pick(row > 6 ? 0.03 + 0.02 * b : 0, T.BOULDER);
pick(0.14 + 0.03 * b, T.ROCK);
pick(0.07, T.COPPER);
pick(b >= 1 ? 0.035 : 0.01, T.SILVER);
pick(b >= 2 ? 0.025 : 0, T.GOLD);
pick(b >= 3 ? 0.014 : 0, T.GEM);
pick(row > 8 ? 0.009 : 0, T.FUEL);
out.push(t);
}
// fairness: never more than 3 boulders in a row, so there is always a way down
let bould = out.reduce((n, t) => n + (t === T.BOULDER), 0);
for (let c = 0; c < COLS && bould > 3; c++) if (out[c] === T.BOULDER) { out[c] = T.ROCK; bould--; }
return out;
}
export const hpOf = (type, row) => (HP[type] || 0) * (1 + 0.35 * biomeOf(row));
// ------------------------------------------------------------------ upgrades (the "idle" depth between runs)
export const UPGRADES = {
drill: { name: 'Drill', icon: 'โ›๏ธ', what: 'Digs faster, cracks boulders', base: 15, mult: 1.62, max: 25 },
tank: { name: 'Fuel tank', icon: 'โ›ฝ', what: 'Longer runs', base: 15, mult: 1.62, max: 25 },
magnet: { name: 'Magnet', icon: '๐Ÿงฒ', what: 'Pulls ores from side columns', base: 60, mult: 2.4, max: 3 },
value: { name: 'Polish', icon: '๐Ÿ’Ž', what: '+25% coins per ore', base: 30, mult: 1.7, max: 10 },
};
export const DAILY_RIG = { drill: 4, tank: 4, magnet: 1, value: 0 }; // the Daily Dig: the same rig for everyone
export const upgradeCost = (kind, lvl) => (lvl >= UPGRADES[kind].max ? Infinity : Math.round(UPGRADES[kind].base * Math.pow(UPGRADES[kind].mult, lvl)));
export function rig(lv = {}) {
const l = (k) => Math.max(0, Math.min(UPGRADES[k].max, lv[k] | 0));
return { power: 5 * Math.pow(1.22, l('drill')), fuel: 20 + 3 * l('tank'), magnet: l('magnet'), value: 1 + 0.25 * l('value') };
}
/** Coins the helper drone digs while you're away: up to 8 hours, more the deeper your best dig. Nothing under 10 minutes. */
export function idleCoins(ms, bestDepth = 0) {
if (!(ms > 10 * 60e3)) return 0;
const h = Math.min(8, ms / 3.6e6);
return Math.floor(h * (6 + bestDepth * 0.4));
}
// ------------------------------------------------------------------ one run
/**
* makeRun(seed, levels) -> run. The page calls run.tick({ hold, col }) 60 times a second:
* hold = finger/mouse/key down, col = the column under the finger (0..COLS-1, float) or null.
*/
export function makeRun(seed, levels = {}) {
const R = rig(levels);
const rows = new Map(); // row -> { t: types[], hp: number[] }
const run = {
seed, rig: R, t: 0, x: Math.floor(COLS / 2), y: 0, tx: null, vy: 0, fuel: R.fuel, maxFuel: R.fuel,
coins: 0, depth: 0, ores: { 4: 0, 5: 0, 6: 0, 7: 0 }, fuelCans: 0, layers: [0], state: 'run', events: [], streak: 0, lastOre: -999,
holding: false, digging: null, stall: 0,
};
run.row = (r) => {
let o = rows.get(r);
if (!o) { const t = genRow(seed, r); o = { t, hp: t.map((k) => hpOf(k, r)) }; rows.set(r, o); }
return o;
};
run.tile = (c, r) => (c < 0 || c >= COLS ? -1 : r < 0 ? T.AIR : run.row(r).t[c]);
run.crack = (c, r) => { if (c < 0 || c >= COLS || r < 0) return 0; const o = run.row(r); const full = hpOf(o.t[c], r); return full ? 1 - o.hp[c] / full : 0; };
run.cell = () => Math.floor(run.y + 1e-6);
run.meters = () => run.depth;
function collect(c, r, type, how) {
const o = run.row(r);
o.t[c] = T.AIR; o.hp[c] = 0;
if (ORE[type]) {
const v = Math.max(1, Math.round(ORE[type] * R.value));
run.coins += v; run.ores[type]++;
run.streak = run.t - run.lastOre < 90 ? run.streak + 1 : 1; run.lastOre = run.t;
run.events.push({ type: 'ore', c, r, ore: type, v, how, streak: run.streak });
} else if (type === T.FUEL) {
run.fuel = Math.min(run.maxFuel, run.fuel + 3); run.fuelCans++;
run.events.push({ type: 'fuel', c, r });
}
}
function damage(c, r, dt) {
const o = run.row(r), type = o.t[c];
if (type === T.AIR || type < 0) return true;
o.hp[c] -= R.power * dt;
run.digging = { c, r, type };
if (o.hp[c] <= 0) {
run.events.push({ type: 'break', c, r, tile: type, biome: biomeOf(r) });
collect(c, r, type, 'dig');
return true;
}
return false;
}
function enterRow(r) {
if (r > run.depth) {
const b0 = biomeOf(run.depth), b1 = biomeOf(r);
run.depth = r;
if (b1 !== b0) { run.layers.push(b1); run.events.push({ type: 'biome', biome: b1, row: r }); }
}
for (let d = 1; d <= R.magnet; d++) for (const c of [run.x - d, run.x + d]) {
const k = run.tile(c, r);
if (ORE[k]) collect(c, r, k, 'magnet');
}
}
run.tick = (inp = {}) => {
if (run.state !== 'run') return;
const dt = 1 / HZ;
run.t++;
run.digging = null;
run.holding = !!inp.hold && run.fuel > 0;
const r0 = run.cell();
const below = run.tile(run.x, r0 + 1);
const aligned = Math.abs(run.y - r0) < 1e-6 && run.tx === null;
// 1) falling through air
if (below === T.AIR || !aligned && run.tx === null) {
run.vy = Math.min(14, run.vy + 40 * dt);
let ny = run.y + run.vy * dt;
const nr = Math.floor(ny + 1e-6);
for (let r = r0 + 1; r <= nr; r++) enterRow(r);
if (run.tile(run.x, nr + 1) !== T.AIR) {
if (ny > nr) { ny = nr; if (run.vy > 6) run.events.push({ type: 'thud', v: run.vy }); run.vy = 0; }
}
run.y = ny;
} else if (run.tx !== null) { // 2) sliding sideways into an open cell
const d = run.tx - run.x;
run.sx = (run.sx || 0) + Math.sign(d) * 7 * dt;
if (Math.abs(run.sx) >= 1) { run.x = run.tx; run.tx = null; run.sx = 0; enterRow(r0); }
} else if (run.holding) { // 3) drilling
const want = inp.col == null ? run.x : Math.max(0, Math.min(COLS - 1, Math.round(inp.col)));
const dir = Math.sign(want - run.x);
if (dir !== 0) {
if (damage(run.x + dir, r0, dt)) { run.tx = run.x + dir; run.sx = 0; }
} else if (damage(run.x, r0 + 1, dt)) {
run.vy = 2;
}
}
// fuel: drilling burns 1/s, waiting 0.25/s
run.fuel = Math.max(0, run.fuel - (run.holding ? 1 : 0.25) * dt);
if (run.fuel <= 0 && run.tx === null && run.tile(run.x, run.cell() + 1) !== T.AIR && Math.abs(run.y - run.cell()) < 1e-6) {
run.state = 'done'; run.events.push({ type: 'done' });
}
};
return run;
}
/** Spoiler-free share text: depth, the layers you passed (one emoji each) and the ore haul. */
export function shareText(run, label) {
const lines = [`Dig Drone ${label} ยท โ›๏ธ ${run.depth} m`, run.layers.map((b) => LAYER_EMOJI[b]).join('')];
const haul = [];
if (run.ores[7]) haul.push(`๐Ÿ’Žร—${run.ores[7]}`);
if (run.ores[6]) haul.push(`๐Ÿฅ‡ร—${run.ores[6]}`);
haul.push(`๐Ÿช™ ${run.coins}`);
lines.push(haul.join(' '));
return lines.join('\n');
}
/** Test/bot helper: the column to steer to (an ore in the next rows if close, else the softest way down). */
export function botCol(run) {
const r = run.cell();
let best = run.x, score = -Infinity;
for (let c = 0; c < COLS; c++) {
let s = -Math.abs(c - run.x) * 1.2;
for (let d = 1; d <= 4; d++) {
const k = run.tile(c, r + d);
s += (ORE[k] || 0) / d + (k === T.FUEL ? 3 / d : 0) - (k === T.BOULDER ? 4 / d : k === T.ROCK ? 0.8 / d : 0) + (k === T.AIR ? 0.6 / d : 0);
}
if (s > score) { score = s; best = c; }
}
return best;
}