Hilbert World
If you wish to bake an apple pie from scratch, you must first invent the
universe.
—Carl Sagan, The Lives of the Stars, Cosmos: A Personal Voyage
Stephen Wolfram may or may not be right that our universe could be modeled as a cellular automaton with a sufficiently large and fast computer to run it. Scientists have lofty notions of creating models that predict the behavior of the universe, I assume, we both live in.
I have no such notions.
A grand enough notion is to create a simulation that is compelling, one in which many near or distant friends might wander and explore, imagining they are in together. To be compelling, the world needs a life of its own and to be governed by rules that these friends can manipulate to deform the world in interesting ways, sometimes with exciting, cascading consequences. Ideally these rules smack of familiar phenomena like rivers flowing, weather forming, grass growing, predators preying, and also of systems that intrigue like the manipulation of magic.
Simulations like Minecraft produce compelling worlds in snapshot. Players enter a familiar world and then lay waste to it, harvesting its resources and undermining the sense the world usually has from its inception. The world is open though, so new colonies to exploit are just over the horizon and only the spawn point compels players to pool their resources and garden the world.
Because the world is open, there are hard limits to how much sense the world generator can impose. Sea level, for example, is universal and there are no high-elevation lakes. This is because forming and flowing water is difficult to coördinate across chunk boundaries, and the engine needs to be able to create a chunk only knowing the seams with its immediate neighbors. Minecraft also makes the simplifying assumption that only the chunks with active players in them are simulated. With a sufficiently dynamic cellular world, many interesting simulations would “clump” at the boundaries of live chunks. For example, a chunk boundary would serve as a dam until the neighboring chunk wakens and floods.
An alternative approach is to create a closed world. However, to create a compelling closed world, the entire world must be both large and always alive.
The first cellular automaton, Conway’s Game of Life, models animal populations in the most simple terms. Life begets life. Overpopulation leads to starvation. A slight variation ensures that the simulation remains interesting and slightly irregular indefinitely, by introducing some arbitrary bias toward life or death as well as genesis that is most likely to occur when there are no living cells. Genesis bootstraps the simulation.
Cellular automata are also a great tool for modeling watersheds, erosion, and weather. This is a simple simulation where the world has some given topography (grayscale, lighter is higher) and on that topography we lay an even sheet of water, like a sudden great flood over the whole world. The simulation redistributes water between adjacent cells to level it out. What emerges from the simulation are seas and lakes (where water becomes trapped in the topography) and rivers. The rivers eventually drain, but adding an atmosphere to convey evaporation and precipitation around the world would sustain them.
Weather ultimately derives from the strobe of a celestial heat source. Simulating atmospheric convection involves adding layers to the automaton.
A circle of life can be in turn modeled as a cellular automaton with a layer for each link in the chain, where life on each layer emerges and consumes from the prior.
This returns us to the problem of making a large shared simulation. This turns out to be an embarrassingly parallel problem. Each chunk of the world can run in isolation, needing the adjacent chunks only to compute the edges. We shard the world and distribute the chunk assignments over however many processes as we have running.
In a naïve implementation, each shard would request all of the chunks from the prior generation for the neighbors of the chunks it owns. In a more sophisticated implementation, each shard would simulate all of its own chunks and all of its neighboring chunks locally. Since information can only propagate one cell per generation, we run all neighboring chunks on stale information until corruption from their neighboring voids traverses their entire length.
A clever implementation can minimize the number of neighbors between shards using a Hilbert curve.
The Hilbert curve is a space-filling curve that is conveniently square, recursively partitions like a quadtree, and is continuous. That is, every cell along a Hilbert curve is adjacent to its two neighbors. Consider a square. If we recursively divide each of those squares into quadrants down to some depth, we have organized our world into cells. We can infer the position of that cell along the Hilbert curve by mixing up the bits of its coördinate.
In the following figure, the top row shows numbers and the bottom shows bits as black and white fields. The Hilbert curve numbers quadrants of a cell 0, 1, 2, 3 in a U and orients the U by conditionally rotating and transposing the parent to ensure that the ends of the neighbors connect. The four numbers 0–3 are expressed as two bits of binary.

The rightmost figure in the top row depicts the Hilbert number for each cell as a block of four binary bits. The top two are the address of the quadrant, and the bottom two are the address of the inner quadrant.
We assemble the Hilbert number by concatenating these bits, where the outermost quadrants are the most significant bits and the innermost quadrants provide the least significant bits.

So the important consequence of this bit manipulation is that every cell number is adjacent along the Hilbert curve to the next and previous number, forming a winding maze.

The orderly maze of a Hilbert curve so closely resembles the space filling curve of a Scandinavian Home Furnishings Labyrinth, one could even make a game that plays satire over designing spaces to force customers to visit every square of your showroom.

So, now that we have transformed a two dimensional space into a continuous number line, we can slice that line into ranges of chunks, one for each task, to run the simulation. The next figure depicts a Hilbert curve divided into three slices, 0–4, 5–A, B–F, noted in hexadecimal since a single hex digit is conveniently sufficient to uniquely address 16 cells.

The Hilbert curve then minimizes the number of neighbors we need to periodically fetch from other shards. The next figure shows the second shard covering 5–A in magenta, and also shows in red the six cells that are neighbors for the purposes of a 3⨉3 stencil.

Try it below at other sizes and with more shards. Choose a shard by clicking a cell. Beside the Hilbert curve, you can number the same world in a Z-order, which interleaves the same bits without the turns and so jumps across the world between quadrants, or row by row, which keeps every shard a band of whole rows.
A couple of years ago, I took a stab at sharding a Hilbert curve using Uber’s Ringpop. This is a depiction of slicing a proportional range of the world to six workers. Unfortunately, Ringpop was a weak fit for the problem since we ought to have a strongly consistent coördinator to signal the beat between generations, redistribute ranges based on observed speed, and a database to serve as a backstop for prior generations of each chunk if the simulation loses a worker.

This kind of simulation is out of scope for the games I’m working on today (BØRK and Die as in Dice), and likely the next three after that (a couch coöp in space, one like Missile Command but in space, and Spreadsheets in Space), but along the way I intend to make this kind of clockwork world.
Since then #
Ringpop was not the right consensus algorithm for generational cellular automata, which need to synchronize generations: Ringpop is built on SWIM’s gossip-based membership protocol. If I revisit this, I will likely use Raft instead of SWIM. Indeed, I would probably use CASK, which is a story for another day and another article.
First published on Patreon, December 14, 2018.
- Previous: The Faces of Daia
- Next: Terminal Rivers