Agent simulation at scale without pairwise checks, grid diffusion and evaporation, conserved counters and emergent trails.
Agent simulation at scale without pairwise checks, grid diffusion and evaporation, conserved counters and emergent trails.
No runs posted yet. Be the first: post yours and tag @wikiwayne.
ANT EMPIRE: 2,000 Ants Foraging on a Real Pheromone Grid with Evaporation and Diffusion
HOOK OBJECT: window.__ants
Build "ANT EMPIRE": a top-down Canvas 2D meadow (no libraries) where exactly 2,000 ants forage for food and carry it home to one nest. Searching ants wander and follow a pheromone trail; ants that carry food walk home and lay that trail on a 160 x 90 grid that spreads out (diffusion) and fades (evaporation) every step. Nothing is scripted: the busy lanes appear by themselves, the nearer food pile wins most of the traffic, and a trail fades when its food is taken away. It is an open top-down field with fixed rules you can check exactly. It is fun to keep watching because glowing magenta lanes grow, pulse and then vanish.
LOOK
- Style: warm top-down toy meadow with a glowing trail overlay. Palette: meadow #6E8F3A with 300 seeded speckles (#5E7D31 and #86A24A, each 0.5 to 1.0 cell wide, drawn once), nest mound #8B5A2B (radius 3.0 cells) with hole #2A180C (radius 1.4), food piles #F2C14E with outline #B8860B, ants #2B1A10 (a body 1.6 cells long and 0.8 wide pointing along its heading), carried crumb #FFE070 (radius 0.45 at the ant's head), pheromone overlay #FF4FD8 at alpha min(0.85, value / 40), HUD strip #141A12 with text #E6F0D0, button #3B4A28.
- World units: 1 cell = 1 unit. The field is 160 x 90 cells, x to the right and y downward. Nest N = (28, 45). Food pile A = (62, 25) and food pile B = (135, 70), each holding 4,000 units of food at the start; a pile is drawn as a disc of radius 1.0 + 2.5 * sqrt(remaining / 4000) cells and disappears at 0. The pheromone grid has one value per cell (column floor(x), row floor(y)), all 0 at the start.
- Screen layout: in landscape (width >= height) a HUD strip 220 px wide on the right and the field area (width - 220) x height; in portrait a HUD strip 150 px high at the bottom and the field area width x (height - 150). Scale s = min(areaWidth / 160, areaHeight / 90) px per cell; the field (160 s x 90 s px) is centered in its area; world (wx, wy) is at screen (field.x + wx s, field.y + wy s). The pheromone overlay is drawn from the grid scaled up with smoothing, over the meadow and under the ants.
- HUD text: 14 px monospace (8.4 px per character), at most 22 characters per line so it fits the 220 px strip (in portrait the lines sit in two columns of about 17 characters): "ANTS 2000", "CARRYING 417", "PICKUPS 1373", "DELIVERED 956", "FOOD LEFT 3797", "PHEROMONE 98934", "TAPS 0 REMOVED 1", "LOOPS 0" (format samples that are consistent with each other: pickups 1373 and food left 3797 mean foodRemoved 2830, since 3797 + 1373 + 2830 = 8000 with one pile removed), and two hint lines "TAP FIELD: ADD FOOD" and "TAP FOOD: TAKE IT". A TRAILS button (at least 56 x 56 CSS px) and the "🔊 TAP FOR SOUND" chip sit in the strip too. Nothing in the strip touches the field.
- FIRST FRAME (t = 0, no input): the whole meadow with the nest, both piles at full size, 2,000 ants scattered evenly over the whole field (each at a seeded random position with a random heading, all searching), no trail yet, HUD "CARRYING 0", "PICKUPS 0". At 1280x720 s = 6.625, the field is 1060 x 596.25 px at x 0, y 61.875, 83% of the window height, and the strip sits beside it. On a phone in portrait (390x844, s = 2.4375) the field is 390 x 219.4 px at y 237.3, only 26% of the height but 100% of the width: that is an accepted exception.
MOTION / PLAY
- Random numbers: one generator, function mulberry32(a){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}}, seeded with 360 at load and again at every cut-back and reset. Start: for ant i = 0 to 1999 in order, x = rand() * 160, y = rand() * 90, heading theta = rand() * 2 pi (three numbers per ant), all searching.
- One step is 1/60 s. In a step the ants are processed in index order, each using exactly one rand(); all ants read the pheromone grid as it was at the start of the step, and deposits are collected and added together after the last ant.
- A searching ant: take three sensor samples at distance 3.0 ahead, at headings theta - 0.6, theta and theta + 0.6, each the grid value of the cell holding that point (0 outside the field), called fl, fc and fr. If max(fl, fc, fr) > 0.05: if fl > fc and fl > fr, theta -= 0.12; else if fr > fc and fr > fl, theta += 0.12. Then theta += (rand() - 0.5) * 0.5.
- A carrying ant: deposits 1.0 into the cell it is in (the cell at the start of its step), then turns toward the nest: d = the angle from theta to atan2(Ny - y, Nx - x), wrapped to -pi..pi; theta += clamp(d, -0.15, 0.15); then theta += (rand() - 0.5) * 0.2.
- Then every ant moves: x += cos(theta) * 0.125, y += sin(theta) * 0.125 (7.5 cells per second). If it would leave the field it is put on the edge (x = 0 or 160 - 1e-6, y = 0 or 90 - 1e-6) and the matching heading is reflected (theta = pi - theta for an x edge, theta = -theta for a y edge).
- After moving: a searching ant picks up one unit from the first pile, in creation order, that still has food and whose center is within 2.5 cells of it: that pile loses 1, the ant starts carrying and theta += pi. A carrying ant within 3.0 cells of the nest delivers: it stops carrying and theta += pi.
- After the last ant: grid[c] = grid[c] + deposits[c] for every cell, then every cell becomes e * ((1 - 0.2) * v + 0.2 * (left + right + up + down) / 4) with e = 0.9961 (half-life about 3 s), where v is that cell's value after adding the deposits and a neighbor outside the field counts as v itself (so the edges reflect and diffusion loses nothing). Total pheromone therefore becomes exactly e * (old total + 1.0 * number of ants that were carrying at the start of the step).
- Taps and clicks: a click or tap (press and release moving 5 px or less) inside the field at world point (wx, wy) first tests the piles in creation order: if the point is within max(the pile's drawn radius, 3.0) cells of the center of a pile that still has food, that pile is removed (its remaining food is thrown away, removed += 1, foodRemoved += the food it had left). Otherwise, if fewer than 3 piles have been placed since the last reset or cut-back and the point is farther than 6 cells from the nest, a new pile of 1,500 units is placed there (taps += 1). Anything else, including taps in the strip, does nothing. The key P or the TRAILS button shows or hides the pheromone overlay (it starts shown).
- ATTRACT MODE: with no input, the colony runs from load in one hands-free loop of exactly 1680 frames (28.0 s): 840 frames (14.0 s) of the colony at work, then after the step of frame 840 the ghost taps the center of pile A (removed += 1) and 840 frames (14.0 s) follow while the lanes to A fade and the ants spread out. Sum: 14.0 + 14.0 = 28.0 s. After frame 1680 the loop cuts back to the t = 0 state: the generator is reseeded with 360, the ants, grid and piles are rebuilt exactly as at load, every counter except loops goes back to 0 and loops goes up by 1 (each run counts for itself). Any input (pointer, key, wheel) takes over: the ghost stops, the colony keeps running, and after 8 s with no input the field cuts back to the t = 0 state and the 28.0 s loop restarts. Hook calls are not user input.
- Time: advance(seconds) runs round(60 * seconds) steps of 1/60 s. The 8 s idle timer counts the same 1/60 s steps as advance() (never wall-clock time). setCameraOrbit(azimuthDeg, elevationDeg, scale) applies the CSS 3D transform perspective(1400px) rotateY(azimuth) rotateX(-elevation) scale(scale) to the canvas, default (0, 0, 1).
- The hook also exposes: attract(on) (false turns the ghost off), reset() (the t = 0 state, every counter 0 including loops), ants() returning {x, y, theta, carrying} as four arrays of 2000 values (copies, in ant order), piles() returning [{x, y, remaining}] for the piles that still have food, in creation order, field(cx, cy) returning the grid value of column cx (0 to 159) and row cy (0 to 89), inject(cx, cy, amount) (adds amount to that cell at once), tap(wx, wy) (the same rule as a click at that world point; returns "placed", "removed" or false), layout() returning {field: {x, y, w, h}, scale, hud: [rects], trailsButton} in CSS px, and view() returning whether the overlay is shown.
SOUND
- Every 25th delivery (the 25th, 50th, ...) -> a tick (sine 1046 Hz, 0.06 s, gain 0.04). A pile placed -> a pop (sine gliding 400 Hz to 800 Hz, 0.1 s, gain 0.05). A pile removed -> a thud (triangle gliding 150 Hz to 70 Hz, 0.15 s, gain 0.06).
- A soft meadow wind: brown noise through a 400 Hz low-pass, gain 0.01, always on after the first tap.
- With no AudioContext (before the first tap, and in ?test=1) nothing is played, but the triggers are still counted.
COUNTERS
stats() returns { fps, ants, carrying, pickups, delivered, foodLeft, foodRemoved, fieldMass, piles, taps, removed, loops, soundTriggers, view, demo }.
- ants: 2000. carrying: the ants carrying food right now. pickups: +1 for each unit an ant picks up. delivered: +1 for each delivery at the nest.
- foodLeft: the food still in the piles. foodRemoved: the food thrown away by removals. fieldMass: the sum of all grid values. piles: the number of piles with food. taps: piles placed. removed: piles removed. loops: cut-backs.
- All of these except loops count the current run and go back to 0 at a cut-back or reset. They always satisfy: pickups = delivered + carrying, and foodLeft + pickups + foodRemoved = 8000 + 1500 * taps.
- soundTriggers: +1 for every tick, pop and thud, whether or not sound is on; so it always equals floor(delivered / 25) + taps + removed.
- view: whether the overlay is shown. demo: whether the ghost is running.
- On screen: the HUD lines above, readable in a paused frame and never over the field.
CHECKS for ?test=1
With ?test=1, run an accelerated self-test that measures real positions and real events (not self-set flags) and logs "TEST n PASS" or "TEST n FAIL: reason" to the console and to window.__testResults:
Each check starts from a fresh load; advance() steps in 1/60 s increments.
1. After attract(false) at t = 0, at 1280x720: stats() has ants 2000, carrying 0, pickups 0, delivered 0, foodLeft 8000, foodRemoved 0, fieldMass 0, piles 2, taps 0, removed 0, loops 0, soundTriggers 0, view true and demo false; piles() is [{62, 25, 4000}, {135, 70, 4000}]; ants() has 2000 entries with every x in [0, 160), every y in [0, 90) and every carrying 0, the mean x between 75 and 85, the mean y between 42 and 48, and each quadrant (x < 80 or not, y < 45 or not) holding between 400 and 600 ants; layout().field is x 0, y 61.875, w 1060, h 596.25 and scale 6.625 (each within 2 px, scale within 0.01), the field height is at least 0.80 of 720, no rectangle in layout().hud overlaps the field and trailsButton is at least 56 x 56. At 390x844: layout().field is w 390, h 219.4, y 237.3 (each within 2) with its height between 0.24 and 0.28 of 844 (the accepted exception) and the hud rectangles start at or below y 694.
2. Exact rules, from fresh loads with attract(false) (use reset() between parts). (a) inject(80, 45, 100): fieldMass is 100; after advance(1/60) fieldMass is 99.61 (within 0.01), field(80, 45) is 79.688, field(79, 45), field(81, 45), field(80, 44) and field(80, 46) are each 4.9805 (each within 0.01), field(82, 45) and field(81, 44) are 0 (within 0.001); after a second advance(1/60) field(80, 45) is 64.494, field(81, 45) 7.9377, field(81, 44) 0.4961, field(82, 45) 0.2481 and field(80, 47) 0.2481 (each within 0.01). (b) inject(0, 0, 100): after advance(1/60) field(0, 0) is 89.649, field(1, 0) and field(0, 1) are 4.9805, field(1, 1) is 0 and fieldMass is 99.61 (within 0.01); the same with inject(159, 89, 100) gives 89.649 at (159, 89) and 4.9805 at (158, 89) and (159, 88). (c) After a reset, frame by frame for 600 frames: on every frame fieldMass after the step equals 0.9961 * (fieldMass before + carrying before) within a relative 1e-4 plus 1e-3, pickups = delivered + carrying, foodLeft + pickups + foodRemoved = 8000, the number of 1s in ants().carrying equals carrying, and ants() always has 2000 entries inside the field. (d) On each of those 600 frames, for every ant that is farther than 0.2 from every field edge both before and after the step: it moved by 0.125 (within 1e-4); an ant that was not carrying before and still is not has |change of theta| at most 0.371 (wrapped to -pi..pi); an ant that was carrying before and still is has |change of theta| at most 0.251; an ant that started carrying this step ended within 2.55 of the center of a pile; an ant that stopped carrying ended within 3.05 of the nest; no ant that is not carrying ended the step within 2.45 of the center of a pile that has food; no carrying ant ended the step within 2.95 of the nest; pickups at frame 1 is between 1 and 25.
3. After attract(false), free-run from a fresh load frame by frame (A pickups = 4000 - piles()[0].remaining, B pickups = 4000 - piles()[1].remaining; the conservation identities of check 2 hold on every frame). At t = 10: pickups between 140 and 480, delivered between 15 and 160, carrying between 70 and 380. At t = 20: pickups between 400 and 1,150, delivered between 200 and 700, carrying between 40 and 560, A pickups at least 0.50 of the A + B pickups. At t = 28: pickups between 550 and 2,000, delivered between 400 and 1,500, carrying between 20 and 900, fieldMass between 12,000 and 200,000, B pickups at least 30, A pickups at least 0.60 of the A + B pickups. The trail test at t = 28: take the six cells at fractions f = 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 of the way from the nest to pile A, column floor(28 + f * 34), row floor(45 - f * 20); their mean field() value is at least 8 and at least 5 times fieldMass / 14400.
4. After attract(false), no advance, using real clicks (pointerdown, pointerup) at the screen points from layout() (world point to screen as in LOOK; positions within 0.2 cells): a click at world (45, 70) places a pile: taps 1, piles 3, foodLeft 9500, soundTriggers 1, and piles()[2] is about {45, 70, 1500}; a second click at (45, 70) removes it: removed 1, foodRemoved 1500, piles 2, foodLeft 8000, taps 1, soundTriggers 2; a click at pile A's center (62, 25) removes A: removed 2, foodRemoved 5500, piles 1, foodLeft 4000, soundTriggers 3; a click at (28, 45) (inside 6 cells of the nest) and one in the HUD strip do nothing; clicks at (100, 45) and (100, 60) place piles (taps 3, soundTriggers 5, piles 3, foodLeft 7000) and a click at (120, 30) does nothing (taps 3); a real keydown "p" makes stats().view false and again true. Then frame by frame for 600 frames: on every frame foodLeft + pickups + foodRemoved = 12500 (= 8000 + 1500 * 3), pickups = delivered + carrying, and soundTriggers = floor(delivered / 25) + 3 + 2; piles() never lists a pile at (62, 25).
5. Fresh load with the demo running, advance() frame by frame to each time (frames = 60 x t): at t = 0.2 take a snapshot of ants(); at t = 13.8: removed 0, piles 2, foodRemoved 0, pickups between 220 and 780, carrying between 100 and 600, and take fieldMass as m1 at t = 14.0 (frame 840); at t = 14.2: removed 1, piles 1, soundTriggers = floor(delivered / 25) + 1, foodRemoved between 3,450 and 3,900, foodLeft + pickups + foodRemoved = 8000 and demo true; at t = 27.0: pickups between 200 and 900, carrying at most 260, B pickups (4000 - remaining of the one pile piles() still lists, which is now B) at least 30, fieldMass between 5,000 and 80,000 and at most 0.95 * m1; at t = 28.2 (after the cut-back): loops 1, taps 0, removed 0, foodRemoved 0, piles 2, foodLeft + pickups = 8000, pickups between 1 and 40, fieldMass at most 300, and ants() equals the snapshot from t = 0.2 (every x, y and theta within 1e-6 and every carrying flag equal). Then a real click at the screen point of world (120, 20) at t = 29.0 gives taps 1, piles 3 and demo false; 7.9 s later (no input) demo is still false and taps 1; 0.3 s after that demo is true, loops 2, taps 0, piles 2, and ants() again equals the snapshot from t = 0.2 (that is, the state of loop frame 12). The run has zero console errors.
KEEP IT SHORT
- Keep the whole file under about 1,500 lines (soft target) so you can write it in one pass and finish well inside 15 minutes.
- Save lines with helpers, loops and instancing, not by dropping features. Everything above must really work: no placeholders, no TODOs.
BUILD RULES (all required):
- Deliver ONE self-contained HTML file: inline CSS and JS, no build step, no local assets. Libraries only from a CDN. If you use Three.js, use exactly this import map: {"imports":{"three":"https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js","three/addons/":"https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/"}}
- It must open straight from disk in desktop Chrome and on an iPhone (Safari), fill the full screen, handle resize and touch, and stay smooth (target 60 fps).
- It must look stunning in the very FIRST frame: no black screen, no loading wait, no tiny subject. The main subject fills at least 50% of the screen height.
- SOUND (required): all sound is made in code with Web Audio (no audio files). Show a small "🔊 TAP FOR SOUND" chip; on the first tap create ONE AudioContext, route everything through a master gain of 0.6 into a DynamicsCompressor, and hide the chip. Also expose enableAudio() on the hook object named at the top.
- Expose the hook object named at the top with: setCameraOrbit(azimuthDeg, elevationDeg, distance) (or an equivalent camera setter), advance(seconds) that steps the simulation exactly that much for still captures, enableAudio(), and stats() returning {fps, ...the counters named above}. Use a fixed random seed of 360 so every run is the same.
- Every on-screen counter must count a real event you can see happen. No fake numbers.
- No text overlapping the subject; any label must be readable in a paused frame.
- No brand logos, no real people, no copyrighted characters, brand names or logos.
FAILURE MODES TO AVOID
- Not exactly 2,000 ants, ants that leave the field, stop, jitter in place or move at a speed other than 0.125 cells per step.
- Ants that do not read the pheromone grid (they just wander), that sense the grid mid-step, or that never turn toward the nest when carrying; a colony that never forms lanes, or one that sits on top of one pile and ignores the other for no reason.
- A pheromone grid that is only a picture: no diffusion, no evaporation (0.9961 per step), edges that leak, or a total that does not equal e * (old total + deposits) every step.
- Deposits made by searching ants, deposits that are not 1.0 per carrying ant per step, or ants that carry food without a pile losing one unit.
- Counters that tick on a timer or miss a pickup, delivery, tap or removal; pickups that do not equal delivered + carrying; food that appears or vanishes without a count.
- Taps that do nothing on touch, taps that place a 4th pile, taps that can both place and remove a pile, or tap targets smaller than 56 px.
- Every ant drawn the same size as a pixel dot, trails drawn on top of the ants, text over the field, or an overlay that cannot be hidden.
- A cut-back at 28 s that does not rebuild the exact t = 0 ants, or counters that do not reset as stated; a demo that crashes in the first 10 s.
- A black first frame, a page that waits before drawing, per-frame work that checks every ant against every other ant (it must stay smooth at 2,000 ants).
- The same variable declared twice, or methods called on objects that don't exist yet.
SELF-TEST LOOP
- If you can run code: build the file, open it, read the console, run ?test=1, and take screenshots at 3 s, 10 s and 30 s at 1280x720 and 390x844. Look at the screenshots and fix what looks wrong. Fix and retest until all 5 checks pass with zero console errors. Mark anything you could not run as UNMEASURED; never invent results.
- If you are chat-only: go through this prompt line by line, check your code against each line, and fix anything that would fail. Then write one corrected, complete file.
- Final answer: ONE complete HTML file in ONE code block, then the 5 checks marked PASS, FAIL or UNMEASURED. No snippets, no "rest unchanged".
Built one? Post it on X and tag @wikiwaynePost on X