← all posts

Digital Garden

Tribes: A Civilisation That Never Stops, Written in C

A persistent civilisation simulation running in C on a Raspberry Pi. Tribes split apart, research forever, burn down, dig underground and go to war, and anyone can watch live in the browser.

Live: simulation.a-kolbinger.net. Open it in another tab while you read. It’s been running the whole time.

The Tribes viewer: a pixel-art map with tribal borders, towns and forests, and a sidebar listing tribes with population, army and research

For a while I’ve wanted something running in the corner of my screen that just keeps going: a small world that makes its own history whether I’m watching or not. So I built Tribes, a persistent civilisation simulation. The server is written in plain C and runs on a Raspberry Pi in my flat. The viewer is a browser tab.

When I took that screenshot, the world was in year 11,181. Sixteen tribes were alive, most of them at war with someone, and a few of them living underground.

The idea

It starts with three peoples on a procedurally generated pixel-art map:

  • Indigo: nature-bound and at home in forests
  • Neros: technological builders
  • Skywires: a flying species that ignores terrain

There’s no player. You watch them gather, farm, trade, research, found towns, starve, catch the plague and declare war. One admin (me) can pause the world, change its speed, drop a meteor or a drought, and tweak parameters. Everyone else just watches.

Why C?

Partly because I wanted to, and partly because the constraints suit it:

  • Single-threaded, one dependency. The server is C11 with mongoose vendored in for HTTP and WebSockets. There’s no framework and no runtime.
  • Deterministic. All randomness comes from a seeded PCG32 generator. The same seed and the same admin inputs always produce the same history, which makes bugs reproducible and balance testable.
  • The whole world is one plain struct. Units, settlements, buildings, caravans and diplomacy live in fixed-size pools inside a single World. There’s no malloc in the tick loop. A save file is that struct, split into tagged sections with a checksum and written atomically (tmp → fsync → rename).
  • It’s fast. One tick is one in-game day and takes about 0.39 ms on the Pi. A headless soak test simulates 100 years in 14 seconds.

The main loop is short enough to fit in one line:

mg_mgr_poll(5 ms) → apply admin queue → run due ticks → broadcast frames (5/s) → stats (1/s) → autosave

The simulation modules never touch the network. proto reads the world and net sends it, as compact binary frames for the map and units and JSON for stats and the chronicle. The browser only gets the units inside its viewport.

The viewer is plain HTML, CSS and ES modules with no build step. Every 16×16 sprite is drawn in code, so there’s no image asset in the whole thing.

Version one was boring

The first version worked, and after a few hours every tribe looked the same. They all climbed the same tech tree, hit the same final era and stopped. Then a run of meteors wiped out the map and nobody recovered.

So most of the work after launch went into making history not converge.

Tribes, not races

Indigo, Neros and Skywires are now species: body plan, home terrain, architecture. A tribe belongs to a species but has its own name, colour, traits, culture, research and relations. Up to 16 can exist at once.

Schisms

Every settlement has unrest. It rises with distance from the capital, famine, plague, war weariness and disasters. It falls in a well-fed, peaceful town with a market or temple. Past a threshold, a town can break away and take unhappy neighbours with it. The new tribe inherits its parent’s knowledge, mutates its traits and culture, and sometimes has to fight a war of independence. That’s how you get the “Sablefolk” from the “Mosswalkers” in the screenshot: Indigo, but a very different people.

No end level

Research never runs out:

  • The base tree has mutually exclusive branches, like Monarchy or Council, and Cybernetics or Bioengineering.
  • After that come endless doctrines with unlimited levels at rising cost: Agronomy, Industry, Warfare, Fortification, Scholarship, Commerce, Medicine and Mobility.
  • A tribe’s culture decides what it prefers, and practice reinforces culture. Two kindred tribes drift further apart every century.
  • Eras are named per species (Seedling → Primeval, Spark → Singularity, Fledgling → Celestial) and keep counting past the last name. That’s why the sidebar shows things like Primeval 15.

Research also changes how a tribe looks and plays. Every tech tier means bigger homes, stronger walls, better farms and new house designs, from huts to neon towers or crystal trees. At the top tier, people glow.

Disasters that matter

  • Wildfire spreads daily with wind and season, burns into towns and kills people. Summer lightning starts fires on its own.
  • Earthquakes collapse mines, and floods wreck lowland farms.
  • Meteor craters now heal back into land within 20–40 years, so impacts can’t pile up forever.
  • If the world ever gets nearly empty, new peoples arrive to resettle it.

Every disaster raises unrest, which feeds back into schisms. A bad decade can split an empire.

Going underground

The most fun addition is a second map layer underneath the first. It has rock, caverns, underground lakes, magma under the mountains, and veins of ore and crystal.

  • Anyone walking below digs a tunnel, and fungus grows in the dark.
  • Tribes that learn Deep Mining send settlers down to found undercities with burrows and fungus gardens.
  • Undercities have no winter hunger and are safe from meteors and wildfire. Earthquakes and floods hit them hard, though.
  • Whether a town is above or below ground is decided per town, so one tribe can live on both layers. A capital hit by a meteor may take its heartland underground while distant towns stay in the open.
  • Surface and underground armies can only fight at an undercity’s gate.

Press U in the viewer to switch layers. Tribes with the ☀⛏ badge live in both worlds.

Reading the chronicle

The best part is the log. Here are a few lines from one minute of a 100-year headless run:

Y100  The Neros conquer Helara from the Volt League
Y100  Fire ravaged Lumopolis: 6 buildings burnt down
Y100  The Indigo and the Neros form an alliance
Y100  Raiders from Octara plunder a caravan of the Hollowwalkers
Y100  The Cobalt Collective reach Scholarship 12
Y101  Miners of the Cipher Accord delve deep and found the hold of Korium
Y101  The Neros declare war on the Hollowweavers

None of that is scripted. These are emergent stories from a few thousand lines of C deciding what happens next.

Hosting

The server runs in Docker on a Raspberry Pi, deployed with Dokploy behind Traefik. The multi-stage Dockerfile builds natively on ARM64 and runs the test suite during the build: determinism, save/load round-trips and corrupt save files. On SIGTERM the server autosaves, so a redeploy never loses the world. It just picks up the same day it stopped on.

Building it

I wrote a detailed spec first: the architecture, data layout, protocol, races, economy and admin API, with every product decision already made. Then I built it together with a coding agent, iterating on what felt wrong once it was live. The spec-first approach paid off. Keeping the world as one plain-data struct, with no allocation and seeded randomness, made every later feature (schisms, endless research, the underground layer) a change to the simulation rather than a fight with the architecture.

What’s next

Mostly watching. The world has been running for over eleven thousand in-game years, and I still find things in the chronicle I didn’t expect. If you open the live view, click a town or a unit, scroll back through history and pick a favourite tribe. Chances are it won’t exist in a few centuries, but something descended from it will.