Part 3 of the series Building a Screeps AI. Previous part: A Tick in the Life: How My Bot Rebuilds Itself Every Few Seconds
For quite a while my economy ran on fumes — and the reason was a single line that, at the end of every tick, threw away every job the bot had just handed out.
Before I tell that story, let me explain what those jobs are, and why my bot has something that looks suspiciously like a ticket system.
The problem: creeps are simple, empires are not
A single creep is a very simple machine. A harvester sits next to an energy source and mines. A filler carries energy to the spawn. That kind of work fits perfectly into a role: the creep looks around, sees what to do, does it.
But an empire has work that doesn’t fit into one creep’s head:
- Work across rooms. Building up a freshly claimed room, tearing down an abandoned base, guarding a remote mining room. No single creep sees all of that.
- Work that must not be done twice. If a container is full, I want one hauler to empty it, not five that all arrive at the same moment and four of them leave empty-handed.
- Work with a priority. A container that is full is more pressing than one that is half full, because the full one is already wasting energy. A rampart that is about to fall is far more important than raising a wall that already has a million hit points.
- Work that should not cost the world. If every creep scans the whole room for something to do, every tick, the CPU bill explodes with the size of the empire.
So instead of letting every creep look for work on its own, the bot writes the work down.
Tickets, queues and schedulers
In my day job I work with ticket systems, schedulers and message queues all the time. A ticket describes a piece of work, has a priority, and is assigned to exactly one person. A queue holds work until a worker is free to pick it up. A scheduler decides who gets what first.
The task system of my bot is all three in a very small form. A task is a little record in the bot’s persistent memory:
- what kind of work it is (repair, collect energy, build up a room, guard, dismantle …),
- where it is (a room, and usually a target object),
- and, once it is taken, which creep owns it.
The life of a task always follows the same path:
- A producer creates it. The economy of a room notices a full container and writes a “collect energy” task. The expansion logic decides a new room needs help and writes “build up this room”. The defence notices a weak rampart.
- The task system assigns it. When a creep needs something to do, it asks the task system: “I am a worker in this room — what is there for me?”
- The creep executes it and keeps it until the work is done, the target disappears, or the creep dies.
- The task is closed or released so that it can be handed to someone else.

Because tasks live in memory and not in the heap, they survive anything: a deploy, a server restart, the heap being thrown away. A creep that was halfway through repairing a wall picks up exactly where it left off.
Priority, then distance
The assignment step is where the “scheduler” part lives. Simplified, it looks like this:
function fetchTask(creep: Creep, role: string): Task | undefined {
const mine = findTaskOwnedBy(creep);
if (mine) return mine; // keep doing what you were doing
const candidates = tasksInRoom(creep.memory.room)
.filter(t => t.role === role)
.sort(byPriority); // most urgent task types first
let best: Task | undefined;
for (const task of candidates) {
if (task.creepId) continue; // already taken
if (!best) { best = task; continue; }
if (task.type !== best.type) break; // only less urgent work follows
if (distance(creep, task) < distance(creep, best)) best = task;
}
if (best) best.creepId = creep.id;
return best;
}
The order of the task types is the hierarchy: some kinds of work always come first. Within the same kind, the closest free task wins, so creeps don’t cross the whole room while there is the same job next to them.

Some priorities needed a special rule. For walls and ramparts, the weakest one always comes first, no matter how big the others are. A rampart with a single hit point must never wait behind a wall that already has fifteen million.
The other important detail is the very first line. A creep that already owns a task keeps it. Without that rule, creeps would jump between jobs every tick, and nothing would ever get finished.
The invisible bug
Which brings me back to the story at the start.
In 2021 I noticed that my creeps were standing around. Not all of them, and not all the time — just often enough that the economy felt slower than it should be. Nothing crashed, there was no error in the console. The bot simply looked a bit lazy.
So I followed a few of those idle creeps through their ticks. They had a task, then they didn’t. They got a new one, then lost it again. Eventually I found the reason, and it was almost embarrassingly small: at the end of every tick, after saving the task list, the shutdown step cleared it. Every tick the bot started with an empty list. The producers dutifully wrote the work down again, creeps were assigned again — and at the end of the tick everything was thrown away again.
In effect, every task lived for exactly one tick. Deleting that line was the whole fix. The commit message I wrote that day still sums it up: “basically … we ran on fumes.”
I don’t know any more how long the bot ran like that. What I do know is what it taught me: a bug in the coordination layer doesn’t look like a crash. It looks like a slightly weaker economy. You only find it if you actually watch individual creeps and ask why they are doing nothing.
Keeping the queue clean
A queue that only grows becomes a problem of its own. Targets disappear: a container is destroyed, a construction site is finished, a creep dies while it still owns a task. If nobody cleans up, tasks point into the void, or stay assigned to a creep that no longer exists.
Side note: the stomped construction site. Have you ever watched an enemy creep stroll across your construction sites? In Screeps, a hostile creep that steps on a construction site destroys it. Your own creeps, however, still hold their “build this” task, and they keep walking to a site that no longer exists, determined to finish it. Without a cleanup step they would go on like that until they die of old age.
That’s why the task system has a regular health check. Tasks whose target is gone are removed. Tasks whose creep has died are released on the tick the creep dies, so someone else can pick them up immediately. Tasks for rooms that have turned dangerous are closed, and new ones aren’t created there at all — the bot learned where it should not send its workers.
The health check itself is not cheap, so the full sweep runs only every few ticks. A finished repair task that lingers a few ticks longer than necessary doesn’t hurt anyone; a dead creep’s task, on the other hand, is released right away.
Memory is never free
All of this lives in the bot’s persistent memory, and memory has a price. Every tick, the game hands your code its memory as a JSON string, and the bot has to parse it before it can do anything — and serialise it again at the end.
Honestly, memory has never been a real crisis for my bot. But it does grow with the empire. Today it is around 800 kilobytes. The largest parts are the memory of the creeps themselves, the knowledge about other rooms (who owns them, are they dangerous, which of my rooms mines them), room plans, and the history of how much the remote mining rooms yielded. The tasks are a surprisingly small part of it.

So I try to keep it small before it becomes a problem:
- Index instead of scan. Tasks are indexed by room, so a creep only looks at the tasks of its own room instead of the whole list.
- Keep hot data out of memory. Data that changes every tick and is only needed for a short time lives in the heap as a cache, not in memory, where it would be serialised over and over.
- Clean up. Old entries for rooms the bot no longer cares about are removed.
None of this is spectacular. But a memory that grows by a few kilobytes a week ends up as a CPU bill that you pay every single tick.
What’s next
With tasks, the bot knows what needs to be done and who does it. What it doesn’t know yet is where things should be built in the first place.
Next time: the room planner — how the bot decides where its spawns, storage, extensions and towers go, and why it puts some of them in places that look odd at first.






