Townsfolk are the people of the generated settlements. This page covers how they think and where they go; the economy their work feeds, the settlement ledger, is on Settlement Simulation, and how the planner itself works is on Artificial Intelligence.

The townsfolk AI domain

Townsfolk run on the same GOAP planner and behaviour trees that drive creatures. What they need is a second domain — a catalogue of state keys, goals and action templates — beside the existing bestia one.

Shared keys

Some keys are genuinely common: position, health, the perception keys, hunger, tiredness, home, and the sleep and safety latches. They move into a shared object, with the bestia domain re-exporting them so nothing downstream churns.

State-key equality is by name, so two domains declaring "position" already alias to the same slot — interop does not need the shared object, but safety does. A world-state read is an unchecked cast, so an Int key and a Float key with the same name compile, alias, and fail at whichever reader is unluckiest. Worse, the flags can diverge silently: a townsfolk key declared without the observed flag would let the planner persist a merely planned nightfall into live memory.

Two new shared keys: hour of day and day index, both observations, both free because perception already reads the clock. Night alone is too coarse — full night runs 22:00 to 04:00, but a baker’s shift is 05:00 to 15:00.

Drives on a day timescale

Creature drives are tuned in real seconds: peckish in about three real minutes. A villager’s day is eight real hours, so hunger needs to run roughly fifty times slower.

Rather than a second drive system, drives are authored per in-game hour and converted using the world clock’s speed factor. “Hungry in eight in-game hours” then survives a retune of the speed factor; “0.0104 per real second” does not. Existing creature rates convert exactly, so nothing about mob behaviour changes.

The daily latch

The planner skips a goal whose desired state already holds, which is why the bestia domain carries a RESTED belief rather than a bare tiredness ceiling. For daily activities there is a better latch: a day stamp. WORK_DONE_ON compared against the day index needs no clearing writer at all — midnight clears it — and an absent value reads correctly as “not done today”.

Sleep keeps the old latch, because the resting phase straddles midnight: a day stamp would flip at 00:00 and send a townsfolk who had just got up straight back to bed.

Goals

Calibrated against the existing creature goal scale, so the two domains could coexist in one head.

GoalBaseAvailable when
Flee200Enemy in sight, and not a combatant (or wounded)
Sleep90Tired, or it is the resting phase
Eat85Hunger over threshold
Restock shop82It is my shop, the shelves are thin, and I am on shift
Work shift70On shift, not yet done today, workplace known
Socialise45Dusk, not yet done today, an inn is known
Go home40Off shift and away from home
Loiter20Restless

The restock errand moves no goods and no coin. The shelves belong to the settlement ledger, which already counts this delivery in its throughput, so the errand only makes the delivery visible.

The crossovers are chosen against each other rather than for feel. At 23:00 sleep beats a starving NPC’s urge to shop, so nobody buys bread at eleven at night. At noon work beats moderate tiredness. Eating becomes eligible well before it becomes preferable, which reads as getting peckish, carrying on, and eventually giving up and going for lunch — emergent from two numbers rather than authored as a lunch break.

Loiter is not decoration. Without a floor goal, an NPC between activities selects nothing, has its plan cleared, and freezes.

What is authored and what is not

The framing “the schedule emerges rather than being scripted” is only half true, and pretending otherwise makes this harder to reason about later.

Authored: shift hours, per occupation. The assigned home, workplace and usual vendor. The inn’s opening hours. These should be authored — the value of “the baker opens at five” is precisely that a player can learn it, and an NPC that re-picked its house nightly reads as broken rather than as alive.

A shop’s opening hours are its keeper’s shift. Outside it the server refuses to trade (SHOP_CLOSED). It uses the same day offset that puts the keeper at their post, so the counter is never open with nobody behind it. Each person’s whole day is moved by up to 30 minutes (day-jitter-minutes), and the map shows the stated shift without that offset. So a shop may open or close a little before or after the hours on the map.

Emergent: whether it goes to work today at all. When it eats. Whether it goes to the inn. How it copes with a blocked route. And what happens when the mill burns — the withdrawal returns less than asked, production fails, the shift ends with nothing made, and nothing anywhere scripts that.

The eligibility windows are authored; the contest inside them is not. That is the difference from a state machine, and it is worth being precise about rather than claiming emergence where there is none.

Three execution rules

  1. Production accumulates inside the behaviour leaf, not in the action’s effect. A higher-priority goal preempts a plan outright, so a baker who leaves for lunch must keep the loaves already baked. The effect is a prediction; the leaf is the mechanism.
  2. Shift leaves read the absolute clock. Adopting a plan builds a fresh behaviour tree, so a re-adopted relative timer restarts and the baker works past midnight.
  3. Withdrawing from the ledger and writing to inventory is one leaf. A failed plan step clears the whole plan, so splitting them across two actions leaks bread on every failure.

Reading the ledger

Through a sense — the existing pluggable perception point, which costs no new scheduler wave. Reading the ledger on one cadence also means the planner sees a consistent snapshot, rather than a value that changed between grounding an action and executing it.

Occupations, sites and residency

Occupations as data

One occupations.yml binding a trade id to an AI profile, a building-function preference order, shift hours, and what it produces and sells. Validated in two places, for a reason: a registry checks what it can see at construction time, and a separate application-ready validator does the cross-catalogue checks — those cannot run earlier, because the item, recipe and mob importers are boot runners.

The cross-check runs both directions, and the reverse direction is the one that matters.

Every settlement is inhabited, hamlets included, and that costs nothing extra: population floors gate most trades, so a hamlet of forty gets an inn and otherwise farmers and labourers — all of which are in the first, economy-free set of occupations.

Ships first, needing no economy binding at all: guard (the only genuine night shift, and so the best early test of the shift model), priest, child, beggar, labourer, farmer (the single occupation that makes a village read as alive) and innkeeper. Adding baker, general store, blacksmith, weaver, carpenter and miller fills a village of four hundred completely — thirteen occupations for a whole village.

Homes, workplaces and doors

A building’s door channel is a bearing, not a position, and it never reaches the prop stream — so doors come from the vector features, which conveniently means no pipeline version bump. The derivation differs by building: a dwelling stands gable-to-street while a temple or market is broad-front, so naively projecting the bearing along the long axis puts a priest inside the temple wall. Use an orientation-free projection against both half-extents.

An NPC’s durable identity is (settlement, household, member) packed into a long — the analogue of a prop’s identity, and for the same reason: a re-materialised entity gets a fresh entity id, so nothing durable may be keyed on one. Occupation comes from the household expander; workplace and home are assigned by stable index into the settlement’s buildings in feature order.

One join failure must be handled rather than asserted away: business placement stops at a ceiling, so a household can be assigned a trade with no building placed for it. That household’s head becomes a labourer. Left unhandled it is a crash on roughly one seed in ten, found by a player.

Going inside

Buildings have doors and no interiors. A townsfolk that reaches a door despawns, and re-materialises at the door when it is due out.

That is chosen over standing them asleep in the street — which reads as a plague rather than as bedtime — and over faking an inside. It is the only option that becomes correct once interiors exist, rather than being replaced: the despawn point becomes a zone transition. It is also one mechanism for both “the baker is inside between customers” and “everyone is indoors at night”, and it makes the night nearly free, which is when a player is most likely to be standing in a city with nothing else drawing.

Level of detail

Residency follows the creature-den pattern — broad phase, narrow phase, diff, hysteresis on unload — rather than the prop-residency pattern, which refcounts by terrain subscription and is wrong for somebody whose home and workplace are in different columns.

Two constraints pull against each other. The activation ring must exceed the client’s area of interest, or people pop into view. But a commute can be the diameter of the town.

Resolved by activating against the townsfolk’s scheduled anchor rather than its home. Where a person would be right now is a pure function of occupation, shift and clock; activate against that, and an NPC materialises at the right end of its commute and only ever walks a short way. When a player is near the destination end at a shift change, materialise at the point along the route matching the scheduled progress — the difference between a living town and a town of teleporters.

Budget, derived rather than asserted: Genesis, the 128 km default world, has about 28 standing settlements. A city of eight thousand is roughly 1700 households, of which an activation disc covers a few hundred. So: one agent per household head, then a hard nearest-first cap per player and globally. Planning is already staggered across ticks, and a townsfolk plan is one to four steps over a handful of grounded actions. A third tier of positional-only walkers for the ring beyond the cap is named and deferred, gated on measuring the real planning cost first.

The ledger itself is negligible at any world size — a few hundred deviation pairs, tens of thousands of floating-point operations per world step, at a one-minute cadence.

Persistence

Townsfolk are re-derived, not persisted. Millions of people world-wide would be a table the size of the world, written for entities nobody will ever meet, and there is no mutable state worth keeping: position follows the schedule, and hunger and tiredness follow the hour.

What is durable is death — the departure from what the generator implies — in a small table shaped exactly like the prop-divergence table, with a replacement clock rather than permanence. A killed baker’s shop shuts, and an apprentice takes over in a few days, so one griefer cannot permanently depopulate a city. A death event tells the ledger the bakery has stopped.

Last updated 04 Dec 2026, 01:08 +0100 . history