A
Argha SenReflections & Devlogs
2 min readBy Argha Sen

Screeps AI: Building an Operating System

📚Part of the Screeps Autonomous AI OS Series
  1. #1Screeps: An MMO for Programmers
  2. #2Screeps AI: First Rewrite and Moving to TypeScript
  3. #3Screeps AI: Building an Operating System Current
  4. #4Screeps AI: State of the World at the Beginning of 2023
Table of Contents (4 sections)

After playing Screeps for some time, it became clear that CPU budget management is critical. While early rooms rarely threaten the 20ms limit, scaling across multiple rooms makes CPU optimization an essential architectural requirement.

Following architectural discussions in the Screeps community, I decided to build a custom cooperative operating system for the bot.

Operating System Architecture

The OS architecture centers around three core components:

  1. Kernel: The execution engine that boots the base system, tracks CPU consumption, and runs processes scheduled by the scheduler.
  2. Scheduler: Manages process states and execution priority across ticks.
  3. Process Framework: Abstract base classes and life-cycle definitions for tasks.

Kernel

The Kernel is responsible for orchestrating the main game loop. It monitors remaining tick CPU, invokes processes according to schedule, and gracefully suspends tasks when CPU budgets are exhausted.

Scheduler

Inspired by the open-source bot Quorum, our scheduler manages task execution across discrete queues:

  • Ready Queue: Processes waiting to execute this tick.
  • Running Queue: Actively executing processes.
  • Completed Queue: Finished processes staged for the next tick cycle.

Features

  • Launches parent and child processes with structured PID tracking.
  • Preserves persistent process metadata across tick boundaries.
  • Re-schedules periodic background processes automatically.

Process Life Cycle

Processes inherit from a base abstract Process class implementing a standard run() method. This abstraction decouples high-level bot strategy from low-level execution logic, allowing individual tasks (e.g. market trading, creep spawning, tower control) to run independently.

The code up to this point is available on GitHub here.

Enjoyed this article? Share it:
Share on X
💬

Discussion & Comments

Loading comments...
Fetching discussion...

Join the conversation

Share thoughts, ask questions, or discuss technical insights. No account required.