← ritwiksharma.com
Work
Project Zomboid Build 42 · NPCs and AI

Zombita NPC System

Survivors for Project Zomboid who live their own lives. They move into empty houses, cook, sleep, loot and hide from the dead. They remember what you did, tell each other about it, and can become your friends, companions or partners. The mod is built to run on a busy multiplayer server, and part of how they move and fight is learned from real players.

Role
Solo
Status
In testing with my server's admins since Nov 2026. Public Workshop release planned.
Engine
Project Zomboid Build 42 (Kahlua Lua) + Python
Size
387 Lua files, 106 offline Lua test suites

What they are

Each NPC is a person with their own body, needs, mood, memories and plans. They are not zombies in disguise. People walk in from outside the map over the first days, find an empty house and settle. They never claim a player base or safehouse.

Eight of the NPCs' baked looks shown front and back in a T-pose: tee and jeans, cap and tee, hoodie and cargo pants, flannel and overalls, leather jacket, jumper and chinos, a woman's tee and cap, and a woman's hoodie.
8 of the 49 baked looks, front and back, from an offline preview render in a T-pose, not a game screenshot.
The talk window, from a test run. She stays "Stranger" until she gives her name, Iris Mason. The header shows how she feels about you, what she is wearing, how well you know each other and her mood, and she goes from unsure to wary as you talk. She answers from her own life ("Back then I was a vet's assistant"). Ask if she needs a hand and she gives you a job, a notebook to carry to Muldraugh, which goes into the task tracker with the distance and time left. The box at the bottom takes your own typed lines. 63 seconds, two short cuts, no sound.
The same job, finished. In Muldraugh, Iris Shaw is very scared and on edge by a fence. She takes the notebook ("From Iris Mason? Thank you. Tell them I said thanks"), and helping her earns you a new title, "A friendly face", which changes how people see you. Back at Iris Mason's house she asks how it went, hands you a reward ("Here, this is yours"), and is now friendly and very happy: "helped with a problem". Then she asks her own question. 66 seconds, one cut for the walk back, no sound.

How they are built

The record and the body. An NPC lives as a data record on the server with its own id. A body only exists near players: it appears when a player is 60 tiles away and goes at 90, so it does not flicker at the edge. Far from players the record keeps living in a cheap off-screen simulation, so needs, home, loot and travel move on. The body can be removed and rebuilt at any time without losing anything. The body is a Build 42 animal set up as a human, not a dressed-up zombie.

Reflexes stay in Lua. Senses, fear, fleeing and fighting always run in the mod. Both brains, the Lua planner and the optional Python one, speak only in intents, a fixed list of 44 actions such as GO_TO, HIDE, LOOT, TALK and DRIVE_TO. Python never moves a body. Lua checks every intent before it runs and refuses one that is out of date, impossible or planned on old information. Reflexes always win over a plan.

The NPC test menu in Project Zomboid's debug mode: admin panel, NPC Creator, People, bug report, spawn, walk points and one submenu per system, with the Driving submenu open: drive nearest NPC to here, spawn a test car, physical driving switch, get in or out, driving status.
The NPC test menu I built for testing in game: one submenu per system. Here Driving, which is how the driving clip further down was started.
The Switches submenu: toggle fear of zombies, zombies hunting NPCs, the NPC brain living on its own, the Python brain bridge, and showing NPC awareness.
Live switches: fear of zombies, zombies hunting NPCs, the NPC brain living on its own, the Python brain bridge, and showing who notices you.
-- Zombita NPC System: the intent list. Both brains (Lua planner, optional Python) speak only this.
-- Python never moves a body: it sends intents, Lua checks them here and runs them.
...
-- may `new` replace `current`? Reflexes (high prio) always win; equal prio only if current is finished/expired.
From shared/ZNS/ZNS_Intents.lua.

Safe on a multiplayer server. The server decides everything. Clients show bodies and send requests. Before the multiplayer test I fired all 161 client-to-server commands from three seats: a player 300 tiles away, a plain player next to the NPC, and staff. A far player changes no NPC with any of them. Every command has a rate cap (a bucket of 30 per player, refilled 12 a second) and a payload cap (no text over 4,000 bytes). The audit found and fixed two holes: a test command any player could use, and a reply that leaked an NPC's health to a far player.

A load governor. The mod watches the real gap between server ticks, so it sees load from the engine and every other mod, not only its own. When the server is busy it slows only what nobody is watching: gossip, holidays, news, letters and off-screen life. Fights, senses, zombies and anything at a player's side never wait. Staff can also flip any system off live, or pause new NPCs, without a restart.

"What is it doing and why". The admin panel lists every NPC, on screen or off, with what it is doing and why, its needs and its last decisions. Testers can take a snapshot with /npcreport: for each NPC nearby it writes the current intent and where it came from (Lua or Python), mood, needs, health and its last 10 decisions.

Fights and companions

Footwork against zombies leans on a model trained on real players (next section). The fight's own safety rules still win over it. Hurt bodies look hurt, with limps, blood, bandages and splints.

One survivor's bad day, unpolished and from the test phase. Zombies swarm her in a parking lot; checking her wounds shows a bite on the arm and more; she runs through the gardens with the dead behind her; and her body is left with her own things: her ID card, a party flyer and a printed selfie. 51 seconds, one stretch sped up, no sound.
Sent somewhere by an admin test command, an NPC gets in the car, drives off down the road, then arrives, parks and gets out. Same motion code on the server and every client. Test server, 20 seconds, no sound.

Learning from real players: Zombita Pulse

Pulse is a separate server mod, with a small part on each player's PC, that records how real players move, search, fight and drive on my server, so the NPCs can learn to act like players.

What it records. Walking four times a second, every door, window and fence crossed, timed actions like eating, cooking and building, items taken and stored, each hit and a summary of each fight, and driving. While a zombie is within 6 tiles it also records footwork ten times a second: position, facing, and where the nearest zombies are. No keys, mouse or screen are recorded.

Nobody is recorded by name. Each player gets a code made from a secret that lives only in the server's memory. The secret changes every 24 hours and at every restart, so a code only links one person's data for that stretch. No Steam IDs, character names, chat or IPs are kept, and players agree to it in the server's terms. Pulse runs inside a cost budget and can be switched between full, light and paused from the website.

The analyzer. A Python package turns the logs into training tables, proven routes and crossings for the NPC walker, a driving map, fight styles, and models for the Python brain. A newly trained model only replaces the old one when it does better on the same held-out players.

The fight forest. The first model in the mod picks an NPC's next footwork move against zombies from 8 choices: back, forward, left, right, shove, stay, swing or turn. It is a boosted forest of 160 trees, compiled from Python into plain Kahlua Lua tables. The tests check, row by row, that the Lua picks exactly what the Python model picks. Tested on 51,856 fight moments from players it never saw, it picks the move the real player made 72.6% of the time, with its mix of moves within 3.6% of the players' mix. Set to weigh every move equally, its balanced accuracy is 0.57, where chance is 0.125.

-- ZNS_FightTree.decide(f): a boosted forest made by pulse_analysis.forest_lua (160 trees, 8 classes).
-- Held-out (51856 test rows): with PRIOR_W 0.50 the right move 0.726, move mix within 0.036 of the players';
-- PRIOR_W 0: balanced accuracy 0.570 (chance 0.125).
-- returns one of: back, forward, left, right, shove, stay, swing, turn
-- GENERATED: do not edit, retrain. Kahlua-safe: tables, loops and if only, no main-body locals.
Header of server/ZNS/ZNS_FightTree.lua (line breaks added). The file ships with PRIOR_W 0.5.
The Zombita Pulse page: running full with 17 players online, its cost per second against a budget, data written today, no errors, and the switch between pause, light, full, automatic and by players.
The Pulse page: running full with 17 players, its cost against the budget, today's data, and the switch.
Pulse's lines today by kind, such as path, act, loot, foot, open, drive, swing and hit, and details on the current file, client part and drops.
What it recorded today by kind (paths, actions, loot, footwork, doors, driving, swings) and how the client part is doing.

The Python brain

An optional program that runs next to the game and thinks for the NPCs over a file bridge. It keeps long memory in SQLite, tracks relationships, makes life choices like moving out or pairing up, and plans for people off-screen. It uses the models the Pulse analyzer exports. With an API key it can also run free talk, where a guard checks what the player types going in and what the model says coming out, and swaps a bad line for a written one.

It is never required. If it goes quiet for 5 seconds, every NPC falls back to the Lua brain and nothing breaks. It is not part of the Workshop item, and it needs only Python's standard library, plus one optional package for free talk. For testers without Python there is a one-click build of it.

Testing

Most of this was built and checked offline before anyone played it. The mod has 106 Lua test suites that load the real mod files under Python, against strict fakes of the game's Java: calling a method the fake doesn't know is an error, as it is in Kahlua. The Python brain and analyzer have 27 more test files. Every install runs all the Lua suites, plus checks for Kahlua's limits, and stops if one fails.

The stress test. One suite loads the whole mod as a dedicated server with 30 players in 6 groups, 40 NPC records, 12 bodies and 3,000 zombies, and counts every call into Java per server tick. Kahlua takes roughly a microsecond per Java call, so this is close to the real cost. The first audit cut the NPC systems from 96,346 Java calls a tick to 3,679, 26 times fewer, mostly by scanning the zombie list once instead of once per NPC per system. After the multiplayer hardening, with every newer system on, a busy run averages 2,375 calls a tick and its worst tick is 8,331, inside the limits of 3,000 and 10,000.

What is still to come. The stress test is offline, not a real server. Another admin and I have run the NPCs together on a multiplayer test server, and they work there. They stay off on the live server while testing goes on, and hostile groups, raids and NPC car fights stay switched off by default until it is done.

What this demonstrates

Related work