Wednesday, December 16, 2009

The Rules Engine is a Pain in the Neck

We all know that Magic is a great game and one of the reasons why it is so great is because of the complicated (and sometime baffling) rules. And when I’m talking about the rules, I’m not talking about the wimpy basic rulebook that beginner’s use, I’m talking about the monstrous, sometimes incomprehensible comprehensive rulebook.

The comprehensive rulebook is great for answering specific small questions like what is a card’s type and supertype?

204.2a The card types are artifact, creature, enchantment, instant, land, plane, planeswalker, sorcery, tribal, and vanguard.

204.4a A card can also have one or more supertypes. These are printed directly before its card types. The supertypes are basic, legendary, snow, and world.

The comprehensive rules are individually numbered so you can quickly look up the rule again, or just reference it by writing 204.2a (if you have *cough* memorized it).

Obviously you have to know a little about Magic’s rules in order to program them. Some people don’t understand targeted and non-targeted spells and I understand their confusion but if you want to write a program that plays Magic, you have to know the rules as much as I can (without going crazy or getting “analysis paralysis”).

Targeted spells use the word “target” but it gets confusing when a spell affects multiple cards like Wrath of God. It seems like Wrath of God should target all creatures but it doesn’t. And Wrath of God will still destroy cards that have protection from white like Black Knight since protection only protects the creature from being targeted by a spell of that color.

Things get really tricky when you consider Pestilence AND a creature that has protection from black. Protection from black nullifies Pestilence and the creature will not receive any damage. Protection is also another thorny area and is widely misunderstood. Below is taken from this Saturday School article.

Protection from any quality means 4 things, abbreviated D-E-B-T: Damage from that quality is prevented, can’t be Enchanted by enchantments of that quality, can’t be Blocked by creatures of that quality, and can’t be Targeted by spells or effects of that quality. Commander Eesha can’t be blocked by a creature.

Forge supports regular "protection from color" as well as protection from enchantments (Azorius First-Wing), Dragons (Dragonstalker), Goblins (Warren-Scourge Elf), Demons (Baneslayer Angel), everything (Progenitus), artifacts (Nacatl Savage), and creatures (Beloved Chaplain).

My personal mistakes include thinking that Elvish Piper targeted a card in your hand AND that you have to choose the creature that you will put into play when you cast the ability. So I made two mistakes on one simple card. (By the way Elvish Piper is still a little broken because I never fixed it.)

Forge tries to implement as much as the rules as possible but originally Nightmare and Giant Growth didn’t work together. I know Giant Growth is supposed to pump up Nightmare but since creature's power and defense were simple numbers (int), a simple layer system had to be added. (Thanks to Dennis and Rob for the layer system.)

I used to think the hardest part of programming Magic was implementing the cards but I've learned that the rules engine itself is the most complicated part. If you rules engine is sophisticated enough with events and triggers, programming the cards shouldn't be much of a problem.

p.s. The picture for Garruk Wildspeaker is from from the Duel Decks: Garruk vs. Liliana set.


Anonymous said...

What do you mean by the 'layer system' for Nightmare, and how does it work?

Brent S.

Silly Freak said...

The layering system specifies how effects are applied to cards, say you have a Norwood Ranger (1/2) and switch its P/T (2/1), when you now attach a bonesplitter (equipped +2/+0), your ranger is 2/3 and not 4/1

that's because switching is applied in a layer after pumping effects, just like nightmare's ability is applied before pump effects. for a long time, this didn't work in forge

rising fruition said...

For layering details, see rule 612 'Interaction of Continuous Effects' in Magic: The Gathering Comprehensive Rules. (the one effective July 11, 2009, at least.) Even if the numbers are different, the title 'Interaction of Continuous Effects' should stay the same.

Silly Freak said...

613 for 09/10/01

Anonymous said...

I must confess that articles are getting better and better. I realy like this one... thumbs up... and i am already waiting for the next one...

Forge said...

Thanks, sometimes I hit a home run and sometimes I strike out, lol.