Spaces:
Running
Running
Download core.js from CyberMax-tools/plopwick: direct link, hf CLI and curl.
- Browser
- Download file 9.79 kB
-
https://huggingface.co/spaces/CyberMax-tools/plopwick/resolve/main/core.js
- Command line
-
hf download hf://spaces/CyberMax-tools/plopwick/core.js
-
curl -L -o core.js https://huggingface.co/spaces/CyberMax-tools/plopwick/resolve/main/core.js
9.79 kB
| // Gummy Jar core: a merge-drop jar with real circle physics, no DOM (runs in the page and in node tests). | |
| // Drop a gummy, two of the same size touch and plop into the next size up. Fixed 60 Hz ticks, 6 substeps, seeded | |
| // pieces: everyone gets the same 50 drops in the Daily Jar. (c) 2026 CyberMax. All rights reserved. | |
| export const HZ = 60; | |
| export const W = 360; // world units (the canvas scales this to the screen) | |
| export const H = 540; | |
| export const L = 14, R = W - 14; // jar walls | |
| export const FLOOR = H - 14; | |
| export const LINE = 104; // the top line: a settled gummy above it for 2 s ends the round | |
| export const DROP_Y = 58; | |
| export const SUB = 6; // physics substeps per tick | |
| export const G = 1500; // gravity, units/sยฒ | |
| export const COOLDOWN = 24; // ticks between drops (0.4 s) | |
| export const DAILY_DROPS = 50; | |
| export const OVER_TICKS = 120; // 2 s above the line | |
| export const MAX_TIER = 10; | |
| export const RADII = [12, 16, 21, 26, 32, 39, 47, 56, 66, 77, 89]; | |
| export const NAMES = ['Speck', 'Pip', 'Drop', 'Bean', 'Bun', 'Puff', 'Plum', 'Dome', 'Loaf', 'Mallow', 'Sunny']; | |
| export const COLORS = ['#FF6FAE', '#FF9A3C', '#FFC93C', '#86D93F', '#2FD39A', '#38C6F4', '#4A7DFF', '#9A66FF', '#DB5CF2', '#FF5A5F', '#FFB800']; | |
| export const EMOJI = ['๐ฉท', '๐งก', '๐', '๐', '๐ฉต', '๐', '๐', '๐ฎ', '๐ธ', '๐', '๐']; | |
| // points for making tier k (triangular numbers, like the classic merge games): 1, 3, 6, 10, 15 ...; a Sunny pair bursts for 100 | |
| export const PTS = RADII.map((_, k) => ((k + 1) * (k + 2)) / 2); | |
| export const BURST = 100; | |
| const SPAWN_W = [30, 28, 22, 12, 8]; // the first five sizes drop; bigger ones are only made by merging | |
| 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; | |
| }; | |
| } | |
| /** The drop order for a seed: the same list for everyone (the daily jar is `daily:<YYYY-MM-DD>`). */ | |
| export function pieces(seed, n = DAILY_DROPS + 2) { | |
| const r = rng(`plopwick:${seed}`); | |
| const tot = SPAWN_W.reduce((a, b) => a + b, 0); | |
| const out = []; | |
| for (let i = 0; i < n; i++) { | |
| // the first three drops are small so the first merge comes within seconds | |
| let x = r() * tot, k = 0; | |
| while (x >= SPAWN_W[k]) { x -= SPAWN_W[k]; k++; } | |
| out.push(i < 3 ? Math.min(k, 1) : k); | |
| } | |
| return out; | |
| } | |
| export const clampX = (x, tier) => Math.max(L + RADII[tier], Math.min(R - RADII[tier], x)); | |
| /** | |
| * One jar. opts.limit = number of drops (Daily Jar 50; 0 = endless practice). | |
| * Returns an object; the page calls drop(x) on release and tick() 60 times a second. | |
| */ | |
| export function makeGame(seed, opts = {}) { | |
| const limit = opts.limit ?? DAILY_DROPS; | |
| const order = pieces(seed, (limit || 400) + 2); | |
| const g = { | |
| seed, limit, balls: [], t: 0, score: 0, drops: 0, cool: 0, state: 'run', danger: 0, overT: 0, | |
| chain: 0, lastMerge: -999, best: 0, merges: 0, bigChain: 0, bursts: 0, made: new Array(MAX_TIER + 1).fill(0), | |
| events: [], endT: 0, nextId: 1, | |
| cur: () => order[g.drops] ?? 0, | |
| next: () => order[g.drops + 1] ?? 0, | |
| left: () => (limit ? Math.max(0, limit - g.drops) : Infinity), | |
| canDrop: () => g.state === 'run' && g.cool <= 0 && (!limit || g.drops < limit), | |
| }; | |
| g.made[0] = 0; | |
| g.drop = (x) => { | |
| if (!g.canDrop()) return false; | |
| const tier = g.cur(); | |
| const cx = clampX(x, tier); | |
| g.balls.push(ball(cx, DROP_Y, tier)); | |
| g.drops++; g.cool = COOLDOWN; | |
| g.best = Math.max(g.best, tier); | |
| g.made[tier]++; | |
| g.events.push({ type: 'drop', x: cx, tier }); | |
| return true; | |
| }; | |
| function ball(x, y, tier, vx = 0, vy = 0) { | |
| const dt = 1 / (HZ * SUB); | |
| return { id: g.nextId++, x, y, px: x - vx * dt, py: y - vy * dt, r: RADII[tier], tier, age: 0, born: g.t, fresh: true, dead: false }; | |
| } | |
| g.tick = () => { | |
| if (g.state !== 'run' && g.state !== 'settle') return; | |
| g.t++; | |
| if (g.cool > 0) g.cool--; | |
| const dt = 1 / (HZ * SUB); | |
| for (let s = 0; s < SUB; s++) { | |
| for (const b of g.balls) { | |
| let vx = (b.x - b.px) * 0.998, vy = (b.y - b.py) * 0.998; | |
| const max = 1300 * dt; // speed clamp keeps stacks calm | |
| const sp = Math.hypot(vx, vy); | |
| if (sp > max) { vx *= max / sp; vy *= max / sp; } | |
| b.px = b.x; b.py = b.y; | |
| b.x += vx; b.y += vy + G * dt * dt; | |
| } | |
| collide(); | |
| walls(); | |
| } | |
| for (const b of g.balls) { b.age++; if (b.r < RADII[b.tier]) b.r = Math.min(RADII[b.tier], b.r + 1.5); } // a fresh merge grows into its size | |
| g.balls = g.balls.filter((b) => !b.dead); | |
| if (g.t - g.lastMerge > 45) g.chain = 0; | |
| // over the line: any gummy older than 1 s whose top is above the line | |
| const high = g.balls.some((b) => b.age > HZ && b.y - b.r < LINE); | |
| g.overT = high ? g.overT + 1 : Math.max(0, g.overT - 3); | |
| const top = g.balls.reduce((m, b) => (b.age > 20 ? Math.min(m, b.y - b.r) : m), H); | |
| g.danger = Math.max(g.overT / OVER_TICKS, top < LINE + 60 ? (LINE + 60 - top) / 60 * 0.5 : 0); | |
| if (g.overT >= OVER_TICKS) { g.state = 'over'; g.endT = g.t; g.events.push({ type: 'over' }); return; } | |
| if (g.limit && g.drops >= g.limit && g.state === 'run') { g.state = 'settle'; g.endT = g.t; } | |
| if (g.state === 'settle') { | |
| const calm = g.balls.every((b) => Math.hypot(b.x - b.px, b.y - b.py) * HZ * SUB < 30); | |
| if ((calm && g.t - g.endT > 45) || g.t - g.endT > 240) { g.state = 'done'; g.events.push({ type: 'done' }); } | |
| } | |
| }; | |
| function walls() { | |
| for (const b of g.balls) { | |
| if (b.x - b.r < L) { b.x = L + b.r; b.px = b.x + (b.x - b.px) * 0.2; } | |
| if (b.x + b.r > R) { b.x = R - b.r; b.px = b.x + (b.x - b.px) * 0.2; } | |
| if (b.y + b.r > FLOOR) { | |
| const vy = b.y - b.py; | |
| if (b.fresh && vy * HZ * SUB > 120) g.events.push({ type: 'land', x: b.x, y: FLOOR, tier: b.tier, v: vy * HZ * SUB }); | |
| b.fresh = false; | |
| b.y = FLOOR - b.r; | |
| b.py = b.y + vy * 0.15; // small bounce | |
| b.px = b.x - (b.x - b.px) * 0.9; // floor friction | |
| } | |
| } | |
| } | |
| function collide() { | |
| const bs = g.balls; | |
| for (let i = 0; i < bs.length; i++) { | |
| const a = bs[i]; | |
| if (a.dead) continue; | |
| for (let j = i + 1; j < bs.length; j++) { | |
| const b = bs[j]; | |
| if (b.dead) continue; | |
| const dx = b.x - a.x, dy = b.y - a.y, rr = a.r + b.r; | |
| const d2 = dx * dx + dy * dy; | |
| if (d2 >= rr * rr || d2 === 0) continue; | |
| if (a.tier === b.tier) { merge(a, b); if (a.dead) break; continue; } | |
| const d = Math.sqrt(d2), o = Math.min(rr - d, 3) / d; // capped push: a big overlap never launches a gummy | |
| const ma = a.r * a.r, mb = b.r * b.r, k = 0.5; | |
| const fa = (mb / (ma + mb)) * o * k * 2, fb = (ma / (ma + mb)) * o * k * 2; | |
| a.x -= dx * fa * 0.5; a.y -= dy * fa * 0.5; | |
| b.x += dx * fb * 0.5; b.y += dy * fb * 0.5; | |
| if (a.fresh || b.fresh) { | |
| const v = Math.hypot(a.y - a.py, b.y - b.py) * HZ * SUB; | |
| if (v > 120) g.events.push({ type: 'land', x: (a.x + b.x) / 2, y: (a.y + b.y) / 2, tier: Math.max(a.tier, b.tier), v }); | |
| a.fresh = false; b.fresh = false; | |
| } | |
| } | |
| } | |
| } | |
| function merge(a, b) { | |
| a.dead = true; b.dead = true; | |
| const tier = a.tier + 1; | |
| const x = (a.x + b.x) / 2, y = (a.y + b.y) / 2; | |
| g.chain = g.t - g.lastMerge <= 45 ? g.chain + 1 : 1; | |
| g.lastMerge = g.t; | |
| g.bigChain = Math.max(g.bigChain, g.chain); | |
| g.merges++; | |
| const mult = Math.min(g.chain, 5); | |
| if (tier > MAX_TIER) { // two Sunnies: they burst | |
| g.score += BURST * mult; g.bursts++; | |
| g.events.push({ type: 'burst', x, y, tier: MAX_TIER, chain: g.chain, pts: BURST * mult }); | |
| return; | |
| } | |
| const pts = PTS[tier] * mult; | |
| g.score += pts; | |
| g.best = Math.max(g.best, tier); | |
| g.made[tier]++; | |
| const vx = ((a.x - a.px) + (b.x - b.px)) / 2, vy = ((a.y - a.py) + (b.y - b.py)) / 2; | |
| const n = ball(clampX(x, tier), y, tier); | |
| n.r = RADII[tier - 1] + 2; | |
| n.px = n.x - vx; n.py = n.y - vy + 0.8; // a little upward pop | |
| n.fresh = false; n.age = HZ; // a merged gummy counts as settled for the top-line rule | |
| g.balls.push(n); | |
| g.events.push({ type: 'merge', x: n.x, y: n.y, tier, chain: g.chain, pts, id: n.id }); | |
| } | |
| return g; | |
| } | |
| /** Spoiler-free share text: the size ladder you reached (Wordle-style), score and best chain. */ | |
| export function shareText(g, label) { | |
| const ladder = EMOJI.map((e, k) => (k <= g.best ? e : 'โช')).join(''); | |
| const lines = [`Gummy Jar ${label} ยท ${g.score.toLocaleString('en-US')} pts`, ladder]; | |
| const extra = [`Biggest: ${NAMES[g.best]}`]; | |
| if (g.bigChain >= 2) extra.push(`${g.bigChain}x chain`); | |
| if (g.bursts) extra.push(`โ๏ธ burst x${g.bursts}`); | |
| lines.push(extra.join(' ยท ')); | |
| return lines.join('\n'); | |
| } | |
| /** Test/bot helper: a drop x that puts the next gummy on a twin of the same size when one is reachable. */ | |
| export function botX(g) { | |
| const t = g.cur(); | |
| const same = g.balls.filter((b) => b.tier === t).sort((a, b) => a.y - b.y); | |
| if (same.length) return same[0].x; | |
| // else the lowest spot: the column whose highest gummy sits lowest | |
| let bestX = W / 2, bestTop = -1; | |
| for (let x = L + RADII[t]; x <= R - RADII[t]; x += 12) { | |
| const top = g.balls.reduce((m, b) => (Math.abs(b.x - x) < b.r + RADII[t] ? Math.min(m, b.y - b.r) : m), FLOOR); | |
| if (top > bestTop) { bestTop = top; bestX = x; } | |
| } | |
| return bestX; | |
| } | |