hrxdev
English

#1 A deterministic core, enforced by the compiler

· Nick

There is nothing to look at yet, so this first entry is about the part nobody will see: the simulation core. The game is meant to run up to 1,000 inmates and staff at 20 ticks per second and to explain why anyone did anything. That only works if the same inputs always give the same world. I decided to make that a property the machine checks, from day one.

Where things stand

I am in the first milestone, the core skeleton. The simulation is a plain .NET library with no Godot code in it, and the Godot 4 client so far opens an empty 3D scene. There is no world model, no tiles, no characters, no saves. The 1,000-character budget is a target; I have not measured anything at that scale.

The tick

Time in the core is an integer tick count, 20 per second. There is no floating-point time. A tick runs three phases: apply outside commands, run the systems in an order written down in one place, then commit deferred changes and advance the counter. If an error escapes a tick, the world is marked faulted and stops. Today the list of systems is empty; the loop exists so that the first system has a place to go.

Commands and a log

The world changes only through commands. A command is a small record with a stable type id and a hand-written binary encoding. For each one the world encodes it, stamps it with the tick and a sequence number, validates it (read-only), writes it to the log, and only then applies it. Rejected commands are logged as well, which should help with bug reports. Seed plus log is meant to reproduce a run exactly. The replay file format is not written yet.

Random numbers

I wrote the random number generators myself (SplitMix64 and xoshiro256**) and banned the standard library’s Random in the core. The world has four named streams (commands, needs, AI, events), derived from the seed and stored in the world state, so they are saved and hashed. For work where order must not matter there is a stateless keyed generator based on domain, entity and tick. The outputs for seed 42 are pinned, because that is effectively the replay format: change the generator and every old replay breaks.

A hash of the world

A small order-sensitive hasher folds the seed, the tick, the random streams and a digest of the command log into 64 bits. Two runs that should be identical can be compared with one number. Right now it covers only those parts, because the world has nothing else in it. Comparing hashes every N ticks during a run is the next step.

Making nondeterminism a build error

I did not want to rely on remembering the rules. The core is built with banned-API analyzers: wall-clock time, unseeded random numbers, random GUIDs, randomized string hashes, threads and parallel helpers, SIMD, and culture-dependent string APIs all fail the build. Further checks verify that there is no mutable static state and scan the compiled code for anything that slipped past. My machine uses a Russian locale with a decimal comma, which is a good reason to ban culture-dependent APIs.

Then I went further and banned the standard hash collections (Dictionary, HashSet and friends) from the core, since their iteration order is a classic source of drift. The core gets arrays, lists and two small collections of its own with a documented order. It is not airtight: an API that hands back a hash collection through a generic interface can still slip through, and part of the protection works only in tests.

How it is built

The tooling around the code is automated too, and the same scripts run on Windows and macOS. The general approach is on How it’s built.

Next

Still open in this milestone: the world model with floors, save and replay files, the determinism test, and a headless scenario runner with a benchmark baseline. After that comes the Godot building view. See the roadmap.

← All devlog entries