Spaces:
Running
Running
File size: 9,865 Bytes
5f9262c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | // 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;
}
|