Thursday, August 30, 2007

Programming Magic 3

The AI (artificial intelligence) is the most important aspect of MTG Forge. Yes, Magic is a fun game, but you need to have a challenging opponent.

I’ll let you in on a little secret. I don’t have the Internet at home. Are some of you shocked? I don’t have cable either, ha. Double shock!! But don’t worry I have used Magic Online and at first I was very impressed with everything. The games played very smoothly and I understood the phase stops and everything. Unfortunately after awhile I got irritated waiting on my opponents and “random” disconnects when I was winning. The high card prices also frustrated me, so MTG Forge was born. My goal was to provide fast, fun games of Magic. I am very happy that MTG Forge is able to simulate drafting and sealed games. Currently sealed is my favorite and I often make 2-3 decks with the same pool of cards in order to see which configuration works the best. Sometimes I play “speed Magic” and see how fast I can click and still win.

OK, back to the nitty-gritty of the AI. The AI of course has many different parts. The main skeleton is named ComputerAI_General and it calls the other parts of the AI depending on the phase. This part of the AI has code which handles playing a land, paying for a card, attacking, and blocking. Each of these separate parts has its own code, for instance ComputerUtil_Attack2 decides which creatures the computer will attack with.

The AI is roughly divided up into 2 parts. Code that is built into each specific card and code that attacks, blocks, plays a land, and pays for cards. Some AI is built into each card. For instance, the computer will target creatures with a defense of 2 or less when choosing targets for Shock. The AI code in Shock chooses the targets and decides when the computer should play it. All of the cards and their AI is crammed in CardFactory, which is roughly 10,000 lines long. Yes it is really, really long.

The AI is sub-optimal sometimes when playing cards like Wrath of God and Giant Growth. When playing Giant Growth, the computer will target a creature that will attack but won’t target a 2/2 if you have a 2/3 blocker. The computer will also not use regeneration effects or counter spells unlike the old Magic game Duels of the Planeswalkers, also known as Shandalar. I find it impressive that Shandalar did do some things right and occasionally even pulls off some pretty good 2-card combos. I am very pleased how MTG Forge’s AI worked out. It is smart enough to provide a good game which was my goal. I have played MTG Forge more than probably anyone else, I’m guessing 100+ hours, but I still love it. Maybe next version I’ll add something that shows the “total time played” so you could see how much you really love Magic and of course for bragging rights. :)

p.s. Sid Meier is credited as both a game designer and programmer in Shandalar.


Chad said...

Hey bro, if you would like some help programming this thing, let me know. I'm a Computer Science major from Westminster College. Hit me up sometime!!!

Forge said...

Hi thanks, the hard part of programming Magic is just trying to encode the logic of a card into Java (or any other programming language). Version 2.0, which only has 10 cards working, now supports creatures like Hypnotic Spector that have abilities that trigger on damage or combat damage.

If I needed help on anything, it would be on how to make a better user interface.

Doug S. said...

You could try some various heuristics for the computer to use when deciding to play Wrath of God or not.

A rather crude one would be "play it if I have fewer creatures than my opponent".

Forge said...

I could do that. Wrath of God is hard for the computer to use because it has a downside of losing all your creatures. The AI has to be reduced to something really, really simple like "if the computer has less creatures than the human" play Wrath. Thanks for the comments.

Luca said...

hi forge,
great work whith MTGForge!
sorry for the OT, but i noticed some refresh problems in the tables of the deck editor, during the makeof of a deck. I'd like to correct them and try to experiment with the GUI part of the game if it's ok with you. Why don' t you try to set up the CVS enviroment that Sourceforge offers so that the code sharing and versioning can be better managed? Luca

Forge said...

A CVS would be fine. I'm sort of working on 2.0, but I still do some programming on the old stuff (I'm adding planeswalkers) and some new stuff.