ModMox
ModMox is going to be a roguelike RPG where you fight your way through a run with a deck of Magic: the Gathering cards, and the cards you earn along the way can be changed. A spell might lose a point of its cost, a creature might pick up flying, a burn spell might stop targeting and hit everything instead. Underneath it is a rules engine for Magic, written in GDScript as a self-contained Godot addon that knows nothing about the game around it, with an AI that plays it and a screen to play on. The roguelike around that has started to exist too.
The engine handles the pregame and London mulligans, every phase and step of a turn, priority and the stack, lands and mana, casting with targets, combat with first strike, double strike and trample, state-based actions, triggered abilities, the layer system for static abilities, replacement and prevention effects, and abilities activated from Emblems and Potions. The current card pool is 121 cards from 45 sets, from Grizzly Bears and Lightning Bolt to Oblivion Ring and Brimaz, King of Oreskos, and every one of them is plain data. A whole encounter against the AI is playable on screen, from the opening hand to the last point of damage, and a debug run strings encounters together: build a deck, walk a Stage’s map, win cards from the Villains you beat, lose Hearts, beat the Boss.
Why follow the real rules
The obvious way to build a card game is to write each card as a little script. That falls apart the moment players can modify cards, because a modified card is a card nobody wrote. So the engine follows the actual structure of the Comprehensive Rules from the start, priority, the stack, state-based actions, layers with timestamps and replacement effects, even though the card pool is tiny. Scope gets cut by leaving out whole mechanics, never by simplifying the architecture, and a rule that only matters for one mechanic isn’t written until a card needs it.
It follows current rules and current Oracle wording, even for Alpha cards. There’s no mana burn and no damage assignment order. If I ever want an old rule back for flavor, it comes in as a named house rule rather than mixed into the core. Encounters are always one on one, and there will never be planeswalkers.
Cards are data
Card Definitions are Godot Resources assembled from a fixed library of primitives: costs, effects, triggers, static abilities, replacement effects, Values, Filters and Selections. Every number in an ability is a Value, so “deal 3 damage” can become “deal damage equal to the number of Goblins you control” without new code. Every effect reaches its targets through a Selection, so turning “target creature” into “each creature” is a data change too. If a card can’t be built from the primitives, the fix is to add a primitive, not to script the card.
Rules classifications are worked out from a card’s contents instead of being set by
hand. Whether an ability is a mana ability follows CR 605.1a, so a modification that
adds a target to {T}: Add {R} correctly turns it into an ability that uses the
stack. A card the player has earned is stored as an untouched base definition plus an
ordered list of Modifications, and the engine flattens it at the start of each
encounter into a printed card that simply is what it is.
Decisions, replays and undo
The engine never waits for input. Whenever someone has a choice, it stops and returns a Pending Decision listing the legal options, and it resumes when it gets an answer. The GUI and the AI answer exactly the same decisions, which keeps everything testable headless. Priority is answered with an index into the list of legal Actions, so an illegal play can’t even be expressed.
Every encounter is deterministic from its setup and seed, with separate random number streams for the game and the AI so that tuning the AI never changes a shuffle. That means a mid-encounter save is just the setup plus the list of answers, and loading it replays them. Undo works the same way. The engine keeps a checkpoint at each point where taking something back would be unfair, like a card being drawn, and undoing restores the checkpoint and replays the few decisions since.
An AI that doesn’t cheat
The AI is its own addon that depends on the engine and is never referenced by it. It holds no card names and no authored hints. To judge a play, it copies the game, tries the play on the copy, and scores the board that comes out, so a card a player has rewritten needs no new AI code. It never sees the real game either. It gets what its player could legally see, plus a copy in which every hidden card has been dealt out again at random, so it knows what could be in your hand and never what is.
On top of its heuristics sits Flat Monte Carlo, which plays the best few candidates forward through the next turn on the same imagined worlds. Difficulty only sets how much it searches. It never adds mistakes, and search budgets are counted in samples instead of milliseconds, so the AI plays the same on any machine and after any reload. The search didn’t buy much strength at first. Normal beat Easy 52.5% of the time over 200 games, short of the 55% I was aiming for, mostly because combat is planned the same way at every level. Since then it has learned to judge a combat trick by what’s left after the turn ends, which stopped it from spending mana on pumps that do nothing.
The screen
Exactly one object in the GUI is allowed to touch the engine. The Encounter Presenter runs the encounter, keeps the AI on a worker thread, and hands the screen a snapshot of what the player may see after every change. The screen draws from the snapshot and nothing else, so it can’t leak a hidden card it was never given.
Cards are drawn from their characteristics, with frames baked from layers, so a card that changes color mid-run gets the right frame without anyone drawing it. What a click means is worked out in one place, and the glow that marks what you can choose is drawn from the same answer. Every kind of decision has an input of its own, from dragging a card out of your hand to pairing blockers with attackers, and a test fails if the engine ever gains a decision without one.
Rules text is generated from a card’s abilities each time it’s drawn, in current Oracle wording, so a modified card always says what it does. The screen still rebuilds its board from every snapshot, and animation sits on top: an object outside the board remembers where each card was, and cards ease to their new places or fly between zones. The Villain’s answers are paced so you can read them, the game taps your lands for you with the same planner the AI uses, and right-clicking a card pins it in an inspector that shows everything currently affecting it.
Emblems and Potions
Emblems are how a character’s lasting effects will reach the engine, and they can have abilities you activate, some of them only once per game. A Potion is the thing that gets used up. It’s an object with one ability whose cost is the Potion itself, so it uses the stack like any other ability and is gone even if it’s countered.
The run
A run is one plain data object holding its seed, your Collection of cards, your saved decks, three Hearts and your gold. It knows nothing about screens, and a test fails if anything on the run side names a file in the GUI. The Collection owns every card and a deck only points at cards in it, so a card carries its Mods into every deck it’s in.
Decks are built in the Deckbuilder, where each card has three Mod Slots, and they’re kept in the Deckbox, which is the only way into the Deckbuilder. The Deckbox opens by itself before each fight, once the Villain is revealed, and its Start button is the one place a deck’s legality is checked.
Each Stage’s map is generated from the run’s seed. The generator rolls a map, checks the whole thing against its rules, and throws it away and rolls again if it fails, instead of repairing it, which keeps the odds honest and the map exactly replayable. Every kind of random choice in a run draws from a stream of its own, so adding a new one never reshuffles the old ones. Fights are the only stops on the map that do anything yet. Stores, Workshops and treasure are still to come.
Winning a fight pays Spoils: gold, and one to three cards drawn evenly from the Villain’s own deck, so the decks themselves are the tuning. You take all of it, and a loss or a draw pays nothing.
Stages and Villains
A Stage lists its Villains by Rank, Lesser, Elite and Boss, and a Villain’s Rank comes from the list it was taken from, so the same Villain can be an Elite early and a Lesser later. The first real Stage is Kitty Keep, white’s first, with eight cat Villains that each have a deck of their own. It has a backdrop as tall as its whole map that scrolls as you climb, and scenery placed at random from the run’s seed. The other colors are still placeholders.
Each Villain’s portrait is drawn by hand at 32 by 32 pixels after the art of a real Magic card, along with a second picture of it wincing and a third with its eyes closed, for showing damage and blinking later.
Adding cards
Cards are added on a track of their own, outside the milestones. Each one starts from its current Oracle data and is sorted by what the engine can already express: ready to add, blocked on a new primitive, set aside because it needs something new on screen, or unsuited to the game. A blocked card’s primitive is agreed and built test-first before the card goes in. That’s how Equipment, counters, tokens, modal spells, flashback, Auras, ward and surveil arrived, each for a specific card.
How it’s tested
Each milestone starts by agreeing on its scope and test scenarios, gets built test-first with GUT, and isn’t done until everything is green. There are around 3,790 tests across the engine, the AI, the run and the GUI, run headless from one script, with a check for leaked objects. To make sure the engine’s tests actually pin the rules down, I break the rules on purpose at the end of each engine milestone and count how many of those mutations the suite catches. Milestone 10 caught 35 of 36. The AI gets a soak test that plays whole seeded encounters against itself and checks that each one ends and replays exactly, and tournaments between AI settings to measure which plays better.
The architecture
- Engine.
addons/modmox_rules/, built fromRefCountedandResourceclasses with no scene tree dependency, and no game content inside it. - AI.
addons/modmox_ai/, which reads only its player’s view and looks ahead on re-dealt copies of the game. - GUI.
ui/, game-side, where a test fails if any file but the Encounter Presenter mentions the engine. - Run.
run/, a run’s state with no nodes in it, which never names anything in the GUI and answers which screen comes next with a plain value. - Content. Card Definitions live game-side in
content/cards/as.tresfiles, with a test that checks no two cards share a sub-resource. - Events. Every change to the game goes through one pipeline: proposed, offered to replacement effects, applied, logged, then checked for triggers.
- Layers. Characteristics are always read through one query, and the layer engine works out every object on the battlefield at once, layer by layer, and caches the result until the next event.
- Typing. Static typing is enforced everywhere, with untyped declarations treated as compile errors, and a test fails if that setting is ever undone.
- Decision records. Sixty-nine ADRs cover the design, alongside two glossaries and a milestone roadmap.
Next up is making a run its full length, five Stages of rising Tier in five different colors. I’ve written up the work so far in five posts: A modular Magic engine on the first nine milestones, ModMox gets an opponent on the AI and the first of the GUI, ModMox: Encounter-Enabled on finishing the encounter, ModMox: On the Map on the Deckbuilder and the Stage Map, and ModMox: Kitty Keep on the first real Stage, its Villains and the card track.