Monday, June 8, 2009

Zone Events - Part 2

Other common events include when a player gains or loses life. Although more complicated, damage and combat damage events would simplify the coding for cards like Hypnotic Specter. (MTG Forge implements this card through some clever hacking, thanks Dennis!!) Since Control Magic and other cards allow players to steal creatures, you have to carefully update the damage and combat damage events of the stolen creature.

In summary some basic Magic events include when a card is added or removed from a zone, when a player's life changes and damage/combat damage triggered abilities. Additional events include discard for Megrim and "how many card were played this turn" for storm.

To further complicate the situation, technically the stack is an oddball zone that only holds spells and abilities while all of the other zones hold cards. Internally I'm thinking about letting the stack hold card objects so I could use only one method to move a card, moveCard(Card, Zone to, Zone from).

Anytime a card is moved it would have to use this one method. This would also allow me add an event that would fire every time a card changed zones, addEvent_moveCard(Event). Megrim could use this event to see when a player discards a card. Cards that care about the storm count could also use this event to get the information that they need. This last event is very general and very powerful.

Wednesday, June 3, 2009

Zone Events - Part 1

I am slowly working on MTG Forge 2.0 and I'm trying to figure out what events should be generated. Granted that this is just a baseline and more events could be added later but my question is "What events do Magic cards care about?"

Many cards have some effect when another card is added or removed from a zone like Glorious Anthem. A card could even want to know when itself was put into play, like Venerable Monk. The triggered ability "when this card comes into play" is fairly common.

Several cards like Darksteel Colossus and Serra Angel have the replacement effect "If this card would be put into a graveyard from anywhere, reveal this card then shuffle it into its owner's library instead" and need to know if they are in a graveyard. (Technically the card would never be put in the graveyard since it is a replacement effect but let's forget that detail for now.) Remember that replacement effects start with the word "instead", see 419.1a "Effects that use the word 'instead' are replacement effects."

The reader should also note that these basic add and remove card events would not be enough to implement Megrim since it only concerned when an opponent discards a card. A card could be removed from a player's hand if he plays it or if he discards it. It would be best to add a discard event if we want to implement Megrim.

Stay tuned for part 2 on Monday. Same bat-time, same bat-channel :+)

Monday, June 1, 2009

21,000 Lines of Code

MTG Forge has at least 21,000 lines of code. (I say "at least" because that number doesn't include CardFactory which has a lot of redundant code. CardFactory another 16,000 lines itself which makes for a grand total of 37,875 lines.)

It is hard to picture 21,000 lines of code but if you wrote 100 lines of code a day it would take you at 210 days, so you can easily see why MTG Forge took about a year to get it up and running. This doesn't include specific bug fixes which tend to take up tons of time and only account for a tiny bit of code.

MTG Forge’s source code is roughly the size of Oliver Twist by Charles Dickens. When the source code is compressed the size is 350 kb which is the same Oliver Twist. Oliver Twist is about 400 pages long, although the exact number of pages will vary from book to book, and has around 161,000 words. And for what it is worth, the source code is 1.78 MB uncompressed and Oliver Twist is 914 KB, so MTG Forge’s source code is about twice the Oliver Twist.

Computer programming is like writing a novel or building with toothpicks. It can be done, but it takes a lot of time. MTG Forge is by far the longest program I have ever written.

Wednesday, May 27, 2009

Unusual Needs

As a guy who programs Magic, unofficially of course, I want to do things that are unusual. For example, I want to sort all of the cards in MTG Forge by putting the newest cards first. And by newest I mean putting the "Alara Reborn" cards first and then the "Conflux" cards second and so on. I really enjoy playing with the new cards and even though MTG Forge may only have a few cards from a given set, they are still fun to use.

OK, back to my task of putting all of the cards in MTG Forge in print order. My first idea was to put all of the card names for each set into separate files. While this would have worked, I found a better solution. When I was playing around with Nate's Deckbuilder I found out that it could produce a file with all of the card names sorted by the set that it was in. While this isn't perfect because I have to reorder the file by putting Alpha first and Beta second, it does get me closer to my solution.

Now all I have to do is to read the "master file" and then sort the cards according to their relative location, if card A is after card B, I would swap them. Nate's Deckbuilder isn't really designed for what I'm using it for but it is versatile enough to still be helpful. It didn't have an option for saving only the card names but it lets you tinker with how it saves decks so I eventually came up with a working solution.

Sorting cards isn't a monumental task but it is certainly hard enough. Every computer program is an accrual of man-hours, it is easier to modify a 100 line program than a 1,000 line program. MTG Forge has 21,000 lines of code not including CardFactory and 37,000 lines of code with it. CardFactory creates all of the cards but it has a lot of redundant code.

Monday, May 25, 2009

Other Magic Programs

Today I don’t really have anything to write about so I’m going to talk about a few of the various Magic computer programming projects that I know about. (I have created my own zip file for some of the program downloads in order to let you quickly play these programs but they might have had additional updates.)

--These programs lets you play Magic against the AI.

Wagic – is designed for to be played on a Playstation Portable (PSP) but also works on Windows. It has around 1,600 cards. Download

Magma – has a very graphical user interface (maybe too many). The homepage is in Italian I think. Download

Deckbot – is text based but lets you play AI versus AI to test out the strength of a deck. Download

--These programs lets you play Magic against another person on the Internet but they enforce the rules like Magic Online.

Firemox – uses XML for the cards and has been an ongoing project for many years.

Incantus – has a nice user interface and probably has the most cards of any project, 4000 cards and counting

--There are other programs that don’t enforce the rules but let you play against other people. So these programs only function like a virtual tabletop.

Magic Workstation – probably the most well-known.

Apprentice – an oldie but goodie that not too many people seem to use.

OCTGN – this is a newer program that also has a blog

--Other Magic related programs are:

Magic Set Editor – which lets you create your own professional looking cards.

Nate’s Deckbuilder and Rulesviewer – lets you easily create decks for constructed, vintage, or of any other type. It also generates nice text spoilers. Download It uses 7z compression which you can get here.

Shandalar updates – Yes, that old Microprose program is still being updated. Download Part 1 (1.5 MB), Part 2 (79 MB)

Many of these programs are hosted in Slightly Magic's forums.

p.s.
This article has the most links of anything I’ve ever written, lol. And in case you didn’t see the memo, you can play MTG Forge online here.

Friday, May 22, 2009

High Quality Card Scans

Magic has thousands of cards, about 10,500 according to Gatherer, and that number doesn’t include promo cards, alternate art, or tokens. There is a forum that is dedicated to scanning each Magic card ever printed, which is an enormous undertaking.

Each card is scanned in very high quality that lets you really appreciate the artwork (for all of your Vorthos out there). You can download scans of each set and submit scans that they don’t have. Collectively they call themselves the CCGHQ Team (formerly the MWSHQ Team). This is also the same forum that kindly hosts MTG Forge’s forum. The site Slightly Magic also host forums for other Magic projects like Wagic which lets you play Magic on your PSP or on Windows against the computer.

CCGHQ Forum
MTG Forge Forum

Thursday, May 21, 2009

Balanced Booster Pack

In the quest mode you win new cards but balancing the cards between the 5 colors, artifacts, artifact creatures, and lands is very tricky. My idea, which I’ve coded, consists of creating a smaller, balanced "bag" and then choosing randomly from it. The idea is to reduce randomness while still being random.

The colors are usually evenly distributed while allowing some unpredictability. Artifacts, lands and multicolored cards appear less often than any color. If you total the artifacts, lands and multicolored cards you will roughly get a number that is usually close to the number of white cards (or any other one color).

This got me thinking about physical booster packs, how are they created? The cards don’t seem to be randomly mixed together because you don’t seem to get duplicate cards (something that did happen when MTG Forge created purely random packs). The creation of real booster packs seems to be related with how the cards are laid out in sheets. Which also leads to the question of what algorithm does Magic Online use to generate packs?

p.s.
Someone has finally done it and converted MTG Forge into an applet. The link is delvegames.com/solomtg.html The "play the game" option worked for me while the "play with images preloaded" didn’t. You can only play with preconstructed decks, the Deck Editor and Quest mode aren't implemented. Anyways, have fun :)