Monday, August 27, 2007

Programming Magic 2

Magic is a big game. Chess and checkers is a breeze to program compared to the juggernaut of Magic. Thankfully our brains often divide big tasks into smaller ones and that is how I programmed MTG Forge.

The user interface is one separate part and can be independently changed if needed. Phases are important, so a different part of the code (an object) keeps track of the phases and mostly just advances the phase to the next one. Clicking on a card in your hand usually means that you want to play a spell. The code then lets you tap your land for mana and is called Input_PayManaCost. If you click on a card in play, the card is checked to see if it has any abilities and if it has more than one ability the user is given a choice. If the ability has a mana cost, Input_PayManaCost is used again.

Combat is pretty complicated so different code handles attacking and blocking. Input_Attack lets you declare your attackers and checks for a number of things like is the creature untapped, does not have summoning sickness, not already attacking (it checks for duplicates), and does not have the keyword defender. Input_Block handles blocking and just checks to see if the blocker is untapped and able to block. Input_Block also checks to make sure a flying attacker can only be blocked by another flyer, or a creature like Giant Spider with reach. A few creatures like Daggerclaw Imp cannot block.

Combat is still not done because if there are multiple blockers you get to assign damage. Although this situation is rare it helps to solve the problem that “the biggest creature always wins” like in Yu-Gi-Oh. Once you get to the Combat Damage phase, if your creature is going to receive any damage, it will show how much damage after the “Assigned Damage” label in the upper right of the screen. Bouncing your creature during the Combat Damage step will still allow your creature to deal damage but not to receive any. Giant Growth effects should be played during the “After Declare Blockers” phase.

So MTG Forge is made up of these large pieces paying the mana cost, attacking, blocking, and the user interface. Currently MTG Forge cannot handle any X spell like Fireball or Blaze because the “pay mana cost” part of the program cannot handle it, also the card would have to know what mana was used to pay for it. In order to add First Strike only the combat portion of MTG Forge would have to be changed. Dividing up the program into large modules helps


Nanocore said...

Keep up the good work and keep these blog entries coming. Gives an interesting break to the day of work to read about fun programming. Every time I read these it makes me want to get back to tinkering, I just don't get to take the time. Can't wait to see what you have in store for the next version.

Forge said...

Well I'm glad at least someone is reading my blog :) My readership (I hope thats a word) averages to around 10 hits a day.

I'm still programming on MTG Forge, there is always more to do. Recently I've played Shandalar and re-beat the RPG mode, I like its user interface. Thanks for the feedback.

Anonymous said...

This looks like it was a titanic amount of work, and it is a great way to play the game anytime without a lot of hassle. The AI is weak, but that's obviously the toughest part. The mechanics are really outstanding - excellent job and thanks for being so generous.

Forge said...

The AI is just meant to be a decent opponent. If you and the AI played the same deck you should be able to win 95% percent of the time. I like MTG Forge because it gives me a feel for the cards and sometimes the computer does pull something out of the hat and wins (or at least makes winning harder).