A million ants.
Two colonies, and every ant is an actor. Drop food, squash ants, and move the slider up to a million. It all runs in this tab.
Watch them.
An ant decides only when something happens: it leaves, finds food, comes home, meets an enemy. Between decisions your browser draws it, not tinyactors.
Two anthills on a field. Each colony's workers fetch food for their queen, who lays eggs with it, and workers of the two colonies fight where they meet.
An ant is a statechart.
An egg hatches, a larva grows, and a worker forages, carries food home and rests. Two fights kill it, and so does old age or your tap.
<?xml version="1.0" encoding="UTF-8"?>
<!-- An ant. An egg hatches into a larva, the larva grows into a worker, and
the worker forages: out to look for food, home with what it found, a
rest in the hill, out again. It decides only when something happens;
the world (urn:world) picks each leg, says when it arrives, and says
when it meets an enemy. Two fights kill it, old age kills it, and so
does a reader's tap. -->
<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0"
datamodel="ecmascript" name="ant" initial="alive">
<datamodel>
<data id="colony" expr="0"/>
<data id="stage" expr="'worker'"/>
<data id="hp" expr="2"/>
</datamodel>
<state id="alive" initial="start">
<transition event="kill" target="dead"/>
<transition event="die" target="dead"/>
<state id="start">
<transition cond="stage === 'egg'" target="egg"/>
<transition target="worker"/>
</state>
<state id="egg">
<onentry><send event="hatch" delay="20s"/></onentry>
<transition event="hatch" target="larva"/>
</state>
<state id="larva">
<onentry><send event="grow" delay="30s"/></onentry>
<transition event="grow" target="worker"/>
</state>
<state id="worker" initial="foraging">
<transition event="fight" cond="hp <= 1" target="dead"/>
<transition event="fight"><assign location="hp" expr="hp - 1"/></transition>
<state id="foraging">
<onentry><send type="urn:world" event="forage"/></onentry>
<transition event="found" target="carrying"/>
<transition event="arrive" target="foraging"/>
</state>
<state id="carrying">
<onentry><send type="urn:world" event="home"/></onentry>
<transition event="arrive" target="resting"/>
</state>
<state id="resting">
<onentry><send type="urn:world" event="rest"/></onentry>
<transition event="arrive" target="foraging"/>
</state>
</state>
</state>
<final id="dead"/>
</scxml>So is a queen.
Workers bring her food. Once a second she lays an egg for every two pieces.
<?xml version="1.0" encoding="UTF-8"?>
<!-- A queen. Her colony's workers bring food home (harvest); once a second
she lays an egg for every two pieces of food she holds, and the world
hatches them as ants, while the colony has room. -->
<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0"
datamodel="ecmascript" name="queen" initial="laying">
<datamodel>
<data id="colony" expr="0"/>
<data id="food" expr="0"/>
</datamodel>
<state id="laying">
<onentry><send event="tick" delay="1s"/></onentry>
<transition event="harvest">
<assign location="food" expr="food + _event.data.food"/>
</transition>
<transition event="tick" cond="food >= 2" target="laying">
<send type="urn:world" event="lay">
<param name="eggs" expr="Math.floor(food / 2)"/>
</send>
<assign location="food" expr="food % 2"/>
</transition>
<transition event="tick" target="laying"/>
</state>
</scxml>The world is an I/O processor.
When an ant asks to forage, the world picks its leg and records two points and two times. It knows when every leg ends.
const world = {
receive(send) {
decisions++;
const session = Number(send.session);
if (send.event === 'lay') {
const colony = queens.indexOf(session);
if (colony >= 0) lay(colony, send.data.eggs);
return;
}
const slot = slotOf[session];
if (slot < 0 || sessionOf[slot] !== session) return;
if (send.event === 'forage') forage(slot);
else if (send.event === 'home') home(slot);
else if (send.event === 'rest') rest(slot);
},
};Each frame moves a simulated clock 50 milliseconds and tells the ants whose legs ended. Enemies whose legs end together may fight.
/** One frame: the clock moves on, the ants whose legs end hear of it. */
async function step() {
clock.increment(FRAME);
const t = now();
const bucket = ring[Math.floor(clock.now / FRAME) % RING];
ring[Math.floor(clock.now / FRAME) % RING] = [];
const items = [];
for (let i = 0; i < bucket.length; i += 2) {
const slot = bucket[i], session = bucket[i + 1];
if (sessionOf[slot] !== session) continue; // ended, or a newer ant in the slot
let kind = pending[slot];
if (kind === FOUND) {
const food = target[slot];
if (food >= 0 && foodLeft[food] > 0) foodLeft[food]--; else kind = ARRIVE;
}
if (kind === ARRIVE && carrying[slot]) { carrying[slot] = 0; colonies[colonyOf[slot]].harvest++; colonies[colonyOf[slot]].stored++; }
items.push({ target: session, event: EVENTS[kind] });
if (outside[slot] && kind !== DIE) meet(slot, t, items);
}
host.sendMany(items);
if (clock.now % (FOOD.every * 1000) === 0) sprout();
if (clock.now % 1000 === 0) {
colonies.forEach((colony, i) => {
if (colony.harvest > 0) host.send(queens[i], 'harvest', { food: colony.harvest });
colony.harvest = 0;
});
}
await host.idle();
}What a million costs.
Drawing is free for tinyactors, so the limit is decisions a second. Above half a million ants the simulation runs slower than real time, and the page says how fast.
| ants | ready | drawn | sim speed | decisions a second |
|---|---|---|---|---|
| 100,000 | 1.1 s | 60 fps | 1.00× | 20,580 |
| 250,000 | 2.5 s | 61 fps | 1.00× | 55,365 |
| 500,000 | 4.9 s | 60 fps | 0.96× | 111,952 |
| 1,000,000 | 9.5 s | 51 fps | 0.58× | 116,264 |