File size: 9,515 Bytes
9d1d0d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Blob Gulp rules (no DOM, unit-tested). Rules: 'timed' = a 60-second arena (biggest at the bell wins); 'last' = Last Blob
// Standing (nobody respawns, the arena shrinks after 40 s, last blob alive wins); 'endless' = one life, bots keep respawning
// (a little bigger over time), play until you are gulped. Steer your jelly blob, gulp snacks to grow, gulp blobs
// smaller than you, dodge bigger ones. Nine bots play the same seeded arena; the biggest blob at the bell wins.
// Fixed 60 Hz simulation, seeded so the Daily Arena starts the same for everyone. (c) 2026 CyberMax. All rights reserved.

export const HZ = 60, WORLD = 1200, ROUND = 60, BOTS = 9, FOOD = 300;
export const START = 22, EAT_RATIO = 1.15, RESPAWN = 1.4;
export const COLORS = ['#3DDC84', '#FF6FAE', '#FFB627', '#4DA3FF', '#B98BFF', '#FF7A59', '#2FD3C0', '#FFD23F', '#7FC8FF', '#FF5A6E'];
const NAMES = ['Pip', 'Moss', 'Juno', 'Bean', 'Taffy', 'Kiwi', 'Mochi', 'Pebble', 'Nori', 'Gus', 'Wren', 'Sprout', 'Bubbles', 'Dot', 'Fizz', 'Olive', 'Plum', 'Ziggy'];

export 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 () => { 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; };
}
export const radius = (m) => 3.4 * Math.sqrt(m);
export const speed = (m) => 230 * Math.pow(START / Math.max(START, m), 0.38);
const clamp = (v, a, b) => Math.max(a, Math.min(b, v));

/** A new arena. opts.bots: number of bots; opts.skill: bot skill 0..1; opts.rule: 'timed' | 'last' | 'endless'. */
export function makeArena(seed, opts = {}) {
  const r = rng(`${seed}:arena`), fr = rng(`${seed}:food`), nb = opts.bots ?? BOTS, skill = opts.skill ?? 0.6;
  const names = NAMES.slice(); for (let i = names.length - 1; i > 0; i--) { const j = Math.floor(r() * (i + 1)); [names[i], names[j]] = [names[j], names[i]]; }
  const blobs = [];
  const spot = () => [200 + r() * (WORLD - 400), 200 + r() * (WORLD - 400)];
  const [px, py] = [WORLD / 2, WORLD / 2];
  blobs.push({ id: 0, me: true, name: 'You', color: COLORS[0], x: px, y: py, vx: 0, vy: 0, m: START, alive: true, dead: 0, gulps: 0, best: START, face: 0 });
  for (let i = 0; i < nb; i++) {
    let x, y; do { [x, y] = spot(); } while (Math.hypot(x - px, y - py) < 260);
    blobs.push({ id: i + 1, me: false, name: names[i], color: COLORS[(i + 1) % COLORS.length], x, y, vx: 0, vy: 0, m: START * (0.7 + r() * 1.1), alive: true, dead: 0, gulps: 0, best: 0, face: 0, think: 0, tx: x, ty: y, wander: r() * Math.PI * 2, brave: 0.4 + r() * 0.6 });
  }
  const food = Array.from({ length: FOOD }, () => ({ x: 30 + fr() * (WORLD - 60), y: 30 + fr() * (WORLD - 60), m: fr() < 0.06 ? 6 : 1 + Math.floor(fr() * 2), c: Math.floor(fr() * COLORS.length) }));
  const rule = opts.rule || 'timed';
  const A = { seed, rule, t: 0, tick: 0, blobs, food, fr, br: rng(`${seed}:bots`), skill, state: 'run', events: [], input: null, lo: 0, hi: WORLD, outs: 0 };
  A.me = () => blobs[0];
  A.left = () => (rule === 'timed' ? Math.max(0, ROUND - A.t) : A.t);
  A.alive = () => blobs.filter((b) => b.alive).length;
  // Last Blob Standing: the living rank above the gulped, and whoever lasted longer ranks higher
  A.ranking = () => blobs.slice().sort(rule === 'last' ? (a, b) => (b.alive - a.alive) || (a.alive ? b.m - a.m : (b.outAt - a.outAt)) || a.id - b.id : (a, b) => b.m - a.m || a.id - b.id);
  A.rank = () => A.ranking().indexOf(blobs[0]) + 1;
  A.step = () => step(A);
  return A;
}

function respawn(A, b) {
  const r = A.br; let x, y, tries = 0;
  do { x = 150 + r() * (WORLD - 300); y = 150 + r() * (WORLD - 300); tries++; } while (tries < 20 && A.blobs.some((o) => o.alive && o !== b && Math.hypot(o.x - x, o.y - y) < radius(o.m) + 160));
  const grow = A.rule === 'endless' ? 1 + Math.min(2.5, A.t / 90) : 1; // endless: newcomers arrive bigger as time goes on
  Object.assign(b, { x, y, vx: 0, vy: 0, m: A.rule === 'endless' ? START * grow * (0.8 + r() * 0.4) : START, alive: true, dead: 0 });
}

function botBrain(A, b) {
  const r = A.br, R = radius(b.m), see = 380 + R * 2;
  let fx = 0, fy = 0, threat = null, prey = null, td = 1e9, pd = 1e9;
  for (const o of A.blobs) {
    if (o === b || !o.alive) continue;
    const d = Math.hypot(o.x - b.x, o.y - b.y); if (d > see) continue;
    if (o.m > b.m * EAT_RATIO && d < td) { td = d; threat = o; }
    else if (b.m > o.m * EAT_RATIO && d < pd && (!o.me || r() < 0.75 + A.skill * 0.25)) { pd = d; prey = o; }
  }
  if (threat && td < 120 + radius(threat.m) * 2 * (1.2 - b.brave * 0.4)) { fx = b.x - threat.x; fy = b.y - threat.y; b.mood = 'flee'; }
  else if (prey && pd < 300) { fx = prey.x + prey.vx * 0.3 - b.x; fy = prey.y + prey.vy * 0.3 - b.y; b.mood = 'chase'; }
  else {
    let best = null, bd = 1e9;
    for (const f of A.food) { const d = Math.hypot(f.x - b.x, f.y - b.y) - f.m * 20; if (d < bd) { bd = d; best = f; } }
    if (best && bd < 500) { fx = best.x - b.x; fy = best.y - b.y; } else { b.wander += (r() - 0.5) * 0.8; fx = Math.cos(b.wander); fy = Math.sin(b.wander); }
    b.mood = 'eat';
  }
  // stay off the walls
  const m = 120; if (b.x < A.lo + m) fx += 2; if (b.x > A.hi - m) fx -= 2; if (b.y < A.lo + m) fy += 2; if (b.y > A.hi - m) fy -= 2;
  const l = Math.hypot(fx, fy) || 1; b.tx = fx / l; b.ty = fy / l;
}

function step(A) {
  if (A.state !== 'run') return;
  const dt = 1 / HZ; A.tick++; A.t += dt;
  if (A.rule === 'last') { // the arena closes in from 40 s to 160 s, down to a 360-wide ring in the middle
    const k = Math.min(1, Math.max(0, (A.t - 40) / 120)), half = WORLD / 2 - k * (WORLD / 2 - 180);
    A.lo = WORLD / 2 - half; A.hi = WORLD / 2 + half;
  }
  for (const b of A.blobs) {
    if (!b.alive) {
      if (A.rule === 'last' || (A.rule === 'endless' && b.me)) continue; // no comebacks
      b.dead += dt; if (b.dead >= RESPAWN) { respawn(A, b); A.events.push({ type: 'spawn', id: b.id }); } continue;
    }
    let dx = 0, dy = 0;
    if (b.me) { if (A.input) { dx = A.input.dx; dy = A.input.dy; } }
    else { if (--b.think <= 0) { botBrain(A, b); b.think = 6 + Math.floor(A.br() * 6); } dx = b.tx * (0.75 + A.skill * 0.25); dy = b.ty * (0.75 + A.skill * 0.25); }
    const v = speed(b.m), k = 1 - Math.exp(-dt * 7);
    b.vx += (dx * v - b.vx) * k; b.vy += (dy * v - b.vy) * k;
    const R = radius(b.m);
    b.x = clamp(b.x + b.vx * dt, A.lo + R * 0.6, A.hi - R * 0.6); b.y = clamp(b.y + b.vy * dt, A.lo + R * 0.6, A.hi - R * 0.6);
    // snacks
    for (const f of A.food) {
      if (Math.abs(f.x - b.x) > R || Math.abs(f.y - b.y) > R) continue;
      if (Math.hypot(f.x - b.x, f.y - b.y) < R) {
        b.m += f.m; if (b.me) A.events.push({ type: 'snack', x: f.x, y: f.y, m: f.m, c: f.c });
        f.x = A.lo + 30 + A.fr() * (A.hi - A.lo - 60); f.y = A.lo + 30 + A.fr() * (A.hi - A.lo - 60); f.m = A.fr() < 0.06 ? 6 : 1 + Math.floor(A.fr() * 2);
      }
    }
    b.m = Math.max(START * 0.6, b.m * (1 - dt * 0.0035 * Math.max(0, b.m - 120) / 120)); // very big blobs slowly shrink: no runaway
    b.best = Math.max(b.best, b.m);
  }
  // gulps: a blob 15% bigger swallows one whose centre it covers
  for (const a of A.blobs) {
    if (!a.alive) continue;
    for (const o of A.blobs) {
      if (o === a || !o.alive || a.m < o.m * EAT_RATIO) continue;
      if (Math.hypot(a.x - o.x, a.y - o.y) < radius(a.m) - radius(o.m) * 0.4) {
        a.m += o.m * 0.8; a.gulps++; o.alive = false; o.dead = 0; o.vx = o.vy = 0; o.outAt = A.t; o.outN = ++A.outs;
        A.events.push({ type: 'gulp', by: a.id, of: o.id, x: o.x, y: o.y, m: o.m });
      }
    }
  }
  if (A.rule !== 'timed') { // snacks outside a shrinking arena come back inside it
    for (const f of A.food) if (f.x < A.lo + 10 || f.x > A.hi - 10 || f.y < A.lo + 10 || f.y > A.hi - 10) { f.x = A.lo + 30 + A.fr() * (A.hi - A.lo - 60); f.y = A.lo + 30 + A.fr() * (A.hi - A.lo - 60); }
  }
  if (A.rule === 'timed') { if (A.t >= ROUND) { A.t = ROUND; A.state = 'over'; A.events.push({ type: 'bell' }); } }
  else if (!A.me().alive) { A.state = 'over'; A.events.push({ type: 'out' }); }
  else if (A.rule === 'last' && A.alive() === 1) { A.state = 'over'; A.events.push({ type: 'bell' }); }
}

/** A simple player bot for tests and clips: chase snacks and smaller blobs, flee bigger ones. */
export function autoInput(A) {
  const me = A.me(); if (!me.alive) return null;
  const save = { tx: me.tx, ty: me.ty, think: me.think, mood: me.mood, wander: me.wander ?? 0, brave: me.brave };
  me.brave = 0.6; me.wander = me.wander ?? 0; botBrain(A, me);
  const out = { dx: me.tx, dy: me.ty };
  Object.assign(me, save);
  return out;
}

export const MEDALS = ['🥇', '🥈', '🥉'];
export const place = (n) => `${n}${n === 1 ? 'st' : n === 2 ? 'nd' : n === 3 ? 'rd' : 'th'}`;
/** Share line: place, size and gulps; a bar of the top 5 with you marked. */
export function shareText(A, label) {
  const me = A.me(), rk = A.rank(), top = A.ranking().slice(0, 5).map((b) => (b.me ? '🟢' : '⚪')).join('');
  if (A.rule === 'endless') return `Blob Gulp endless 🫧 lasted ${Math.floor(A.t)}s\nbiggest ${Math.round(me.best)} · gulped ${me.gulps}`;
  return `Blob Gulp ${label} ${MEDALS[rk - 1] || '🫧'} ${place(rk)} of ${A.blobs.length}\nsize ${Math.round(me.m)} · biggest ${Math.round(me.best)} · gulped ${me.gulps} ${top}`;
}