MTG Arena Rule Engine Overview

BoingBoomTschak1 pts1 comments

On Whiteboards, Naps, and Living BreakthroughSkip to main content

Locator

Card Database

Accounts

News

MTG Arena

Hello, internet! My name is Alex Werner, and I've been a developer on the rule team of Magic: The Gathering Arena since September of 2021. I thought you all might enjoy a peek into the MTG Arena rules engine and into the job of those of us who make sure that hundreds of new Magic cards added each year work with the thousands of cards that already exist.

MTG Arena Is Just GRE-at!

So first, a quick summary of how the rules engine works. When a game of Magic is in progress on MTG Arena, the program that is tracking the state of the game and enforcing all the rules-correct card interactions is called the Game Rules Engine (GRE). It's one of the two main programs that we work on. It's written in a combination of C++ and a language called CLIPS, which is a variant of LISP.

What does the GRE do? Well, for the most part, it just knows the basic rules of Magic:

It knows which player gets priority and when.

It knows that creatures die when they have lethal damage.

It knows how to move between the phases of a turn.

It knows the steps of casting a spell.

What it does not know is what any of the thousands of individual Magic cards do.

You can imagine writing a rules engine for Magic that actually knows about all the cards … and how it would get out of control quickly. Imagine a function determining whether a player can play a particular land. So that one function would have to have code for Fastbond. And code for Explore. And code for Solfatara. And code for Crucible of Worlds. All those bits of code would have to interact with each other properly. Every new card printed that had anything to do with what lands are or are not allowed to be played with would make that function even more complicated. It would be a nightmare.

Instead, MTG Arena's model is what I like to think of as "naps and whiteboards." I envision the GRE as a very industrious little worker who takes frequent naps. For instance, let's say that Day of Judgment starts resolving. The GRE goes to their whiteboard and writes:

TO DO

Destroy Darksteel Colossus.

Destroy Cityscape Leveler (that was unearthed).

Destroy Grizzly Bears.

Destroy Sanctuary Warden.

Then, the GRE goes off and takes a nap.

While they're napping, the "CLIPS rules" leap into action. These are the little bits of code that make all the specific abilities of individual Magic cards work. They are in the CLIPS language, because this sort of flexibility is precisely what CLIPS is designed for.

So first, the CLIPS rule for indestructible comes in and erases "Destroy Darksteel Colossus" off the whiteboard entirely. Then, the CLIPS rule for unearth comes in and replaces "Destroy Cityscape Leveler" with "Exile Cityscape Leveler." Then the CLIPS rule for shield counters comes in and replaces "Destroy Sanctuary Warden" with "Remove a shield counter from Sanctuary Warden."

Then, the GRE wakes up from its nap and looks at the whiteboard, which now reads:

TO DO

Exile Cityscape Leveler (that was unearthed).

Destroy Grizzly Bears.

Remove a shield counter from Sanctuary Warden.

And it does those things. The key here is that the GRE neither knows nor cares how many CLIPS rules came along and modified the contents of the whiteboard while it was napping. If we print a Magic card next year which reads, "When this creature would be destroyed, instead you win the game," and the GRE comes back from its nap to see "Player 1 wins the game," it will just do that. It doesn't care.

Let's look at one more example, which will be more relevant to the story I'm telling. In addition to to-do lists while resolving spells or abilities, one of the things the GRE is constantly doing is assembling a list of available actions for a player who has priority.

So, Player 1 has priority. The GRE writes on the whiteboard:

AVAILABLE ACTIONS FOR PLAYER 1

Cast Lightning Bolt (from hand).

Play Swamp from hand.

Activate Icy Manipulator.

And then the GRE takes a nap. Again, the CLIPS rules go to work. It turns out that Player 1 cast Yawgmoth's Will earlier this turn. So, the CLIPS rule for Yawgmoth's Will shows up and adds "Play Forest (from graveyard)" and "Cast Lightning Bolt (from graveyard)" to the whiteboard. But … Meddling Mage naming Lightning Bolt is in play, so the CLIPS rule for Meddling Mage comes up and erases "Cast Lightning Bolt (from hand)" and "Cast Lightning Bolt (from graveyard)" from the whiteboard.

And then the GRE wakes back up from their nap, sees the finalized list of available actions, and sends it off to Player 1, who sees the Forest in their graveyard highlighted and playable, but neither of their Lightning Bolts are playable.

What's important about this is that the GRE has no idea that either Yawgmoth's Will or Meddling Mage exists, and neither one of them knows anything about the other. There's no special case code to make sure Meddling Mage and Yawgmoth's Will work...

from clips rules magic knows player

Related Articles