Monday, June 29, 2009

Xbox AI

I wanted to briefly talk about the two articles that Wizards posted about the new Xbox game. (The game doesn't have a deck editor if you can believe it and is an introduction to the game of Magic.) The AI seems pretty good. The game starts on medium difficulty but the user can change it to make it more challenging.

The article mentions that the AI will attack with a 1/1 into a 2/2 if it is holding Giant Growth and an untapped Forest. And "there's the time it attacked with a bunch of green creatures, let me block, and only then used Elvish Piper to drop a Roughshod Mentor into play and give everything trample." While these examples wouldn't win a pro tournament they are still impressive for a computer. While effectively using Giant Growth isn't rocket science the key idea is that the Xbox AI can use other cards like Giant Growth without additional programming.

One of the articles mentions that the AI can only look ahead for one combat phase. For example the computer might attack with all of its creatures just to lose when the human player attacks with his creatures. I find that this scenario occurs often in the games that I play. MTG Forge's AI also makes this error because the combat logic is relatively primitive.

The user interface is separate from the rules engine which is always good. One of the articles even shows the internal representation of Angel of Mercy. While I think using XML for Magic cards is cumbersome, it is interesting to see how a "commercial Magic" project does it. (You can right click and open the gif below in a new window in order to see it more clearly.)

p.s. It is great to see Lightning Bolt again in Magic 2010. I didn't think it would ever be reprinted.

Friday, June 26, 2009

Mana Burn Controversy

I never considered mana burn to be an important part of Magic and I was surprised that both Mark Rosewater and Abe Sargent wrote about mana burn this week. I considered the new combat rules to be ten times more important. (In case you missed the memo, Magic will no longer have mana burn. Mana pools are emptied at the end of every step and phase.)

MTG Forge has never had mana burn and I've never missed it. Sometimes I had to pay attention to mana burn when I was playing Shandalar but usually I only burned myself because of a mistaken mouse click and not a tactical error.

The only card that I like that is influenced by this change is Su-Chi. It is a 4 mana, 4/4 creature that makes 4 mana when it is destroyed. The trick is trying not to be mana burned. I thought the card was just great because it sort of gives you too much mana.

The truth is that most people won't notice that the mana burn rule has been removed. Yes the change affects a bunch of old cards, but it doesn't really affect much that has been printed within the last 5 years. Supposedly Michelangelo was asked how he carves such beautiful marble statues and he says, "The statue is there within the block of marble, I just have to remove the rest." One less rule = better Magic.

What is Forge?

My computer program Forge lets you play the card game Magic: The Gathering against a computer opponent using all of the rules. This is very different than playing solitare Magic with Apprentice or Magic Workstation because the AI will attack and play spells. Currently there are over 1,800 cards ranging from old power cards like Ancestral Recall and Lightning Bolt to newer cards like planeswalkers. The user interface is basic, but it does let you download all of the cards pictures and view them while playing the game.

(If you don't know Magic, the easiest way to learn is to download this demo from here or here for Windows, it is 85 MB. Also checkout the YouTube videos at playmagic.com.)

You can also play sealed deck, draft, or the new quest mode. In draft mode the AI drafts for the 7 other opponents and builds the decks from the cards that it chooses. In the quest mode you start out with a few cards and the more games that you play the more that you win. As you get more cards you can update and change your deck.

There is also a "quest mode" where you start out with a small, random cardpool and you win more cards by playing against opponents. This is a scaled down version of Shandalar that is only menu based. On easy mode your goal is to win 10 matches. Easy mode is well easy because you always win cards after the match whether you win or lose. On some of the harder modes you only win cards if you win. Quest mode is challenging since the computer uses cards that are better than the stinky ones that you have. The computer opponents are divided into three categories so they get progressively harder.

Forge supports a number of keywords such as exalted, fear, flying, shadow, flanking, protection, wither, flash, defender, reach, and haste. Forge now also has a mana pool and supports creature enchantments.

The AI is very simple and does the best that it can. It tries to do reasonable actions. Sometimes the computer seems smart and sometimes it doesn't. Truthfully the AI plays random cards from its hand and each card has some AI code programmed into it. The computer can play cards like Counterspell but in all other cases the computer will never respond to a spell or ability on the stack. Also the computer will only play spells during its Main phase.

Forge runs on Windows, Linux, and Macs. You can download Forge from the panel on the right.

If you need to download Java, you can get it from here.

Wednesday, June 24, 2009

Booster Draft AI - Part 2

It would also help if the drafting AI considered the deck's mana curve. Having a smooth mana curve will improve any deck's performance. Below is a general outline for a good curve for a draft deck.

2 mana: 3 cards
3 mana: 5 cards
4 mana: 4 cards
5 mana: 2 cards
6 mana: 2 cards

One rule of thumb for draft decks is to have at most five spells that cost five or more mana. Also a draft deck should have a few powerful win conditions like Might of Oaks or Shivan Dragon. (In case you don't know Might of Oaks is a +7/+7 Giant Growth.)

I would love to draft against some sort of "reactive AI" that reads signals and everything but it doesn't seem very realistic. How does a real human player draft and how could I put that logic into a computer program?

While drafting in MTG Forge is easy compared to real life, it does provide the "drafting experience" that other games don't have.

Monday, June 22, 2009

Booster Draft AI - Part 1

In case you didn't know, MTG Forge supports booster drafts. The computer AI simulates the other seven players that you are drafting against. Before the draft starts each of the AI players chooses the two colors that they will draft. Each AI player picks creatures first and then spells. Each AI deck has 12-20 creatures, various spells and 18 land, 9 of each color.

The drafting AI is very weak since it only chooses a random card in one of its colors. The simplest way to improve the AI is to have a pick order for all of the cards. Each color could have a separate file and the cards at the top of the list would be chosen first. (You could have a "global pick order" by putting all of the cards into one file and but it would be hard to compare the power levels of various cards across all of the colors.)

This is a sample of what would be put into the "red-draft" file.

Lightning Bolt
Shock
etc...
2/2 creature with haste
2/2 creature

Since archetypes do exist in drafting, it would be nice if the AI could draft them. One way to implement this is to create a file that lists all of the cards that work well for a specific archetype. Another idea is that the AI could draft using a simple plan. For example an aggro AI drafter would pick many 2/2 creatures.
This article talks about both pick orders and archetypes, you can read it here.

"While I am not completely opposed to the idea of having a pick order for a specific archetype, or ranking cards for first-pick, first-pack purposes, having a "pick order" for the entire set strikes me as being more harmful than helpful.

The key to successful archetype drafting is to take cards that not only work well with what you've already drafted, but will allow you to maximize the value of cards that you will draft. So if you're trying to draft an awesome Esper deck, you need to draft artifacts—and lots of 'em."

p.s.
I'm a huge believer of using plain text files like the above example.

Friday, June 19, 2009

Xbox Game

(The Xbox videogame was supposed by to released but Wizards made an announcement that the game is delayed.)

I just wanted to tell everyone that there is now an Xbox Magic game. “Duels of the Planeswalkers” from the Xbox LIVE Arcade (XBLA) network. You don’t buy booster packs like Magic Online, it has around 280 cards and a 40 hour single player campaign. You can also play against someone else over the Internet or in a Two-Headed Giant game. It also supports free-for-all multiplayer with up to three other players.

If you have played this game please leave a comment. I am especially curious to know how good is the AI? I am guessing that each deck has an AI designed especially for that deck which should make the AI relatively smart. MTG Forge has a more general AI that can use any deck.

Wizards Announcement

You can also read an article written by the lead developer, here.

Wednesday, June 17, 2009

New Version

I’m glad to announce that there is a new version of MTG Forge that has 90 new cards which brings the grand total number of cards up to 1691. New features include land stacking, equipment, persist, and flashback. Multiple lands are grouped together making the user interface less cluttered. Equipment cards include Loxodon Warhammer, Lightning Greaves and the insane Skullclamp which should spice up your favorite deck.

Other changes include "champion" creatures like Wren’s Run Packmaster. The changeling champion cards are also included: Changeling Hero, Changeling Berserker, and Changeling Titan which can fit into almost any white, red, or green deck. Blue mages can control the board with Capsize while green mages can hold their own with Survival of the Fittest. (I love blue versus green.)

And as a hint, in the deck editor you can put the new card first by selecting the menu option "Sort By" then "Newer cards first". This puts the cards that were recently added first in the lineup. This option doesn’t sort by the date the card was printed but by the location that the card is in according to the file cards.txt. New cards are added to the beginning of cards.txt. (I keep meaning to add a sort option that sorts cards by their set: Alara Reborn, Conflux, Shard of Alara, etc…)

All of the credit for this new version goes to Dennis and Rob from MTG Forge's forum. Thanks and keep up the good work.

MTG Forge 06-12
Readme (also included in the above file)

p.s.
You may also want to download the official card token artwork. It isn't necessary but most of them look pretty good.
Tokens (1.5 MB)

And if you can't download the card pictures, this file has most of them.
Card Pictures (41 MB)

Monday, June 15, 2009

Rules Changes - My Opinion

The biggest rules change is combat. (In case you missed the memo, Wizards has updated a few things.) Last night I read and re-read the combat section but I’m not sure that I fully understand it. While the old way of doing combat was more complicated, I had spent many hours fully understanding all of the intricacies. Much as if the rules of chess were changed and pawns could now move diagonally, I’m not sure how the new combat rules differ greatly from the old ones.

The new combat rules deal damage as the damage is assigned, so there is not a step between assigning and dealing damage. That seems simple enough but the rules about multiple blockers is still fuzzy in my mind. The new rules state that the attacking players puts the blocking creatures in order, assigning them 1,2,3 and so on and then deals damage to them in that order.

Personally I don’t see a difference between putting the blockers in order (1,2,3) and not putting them in order. You will still kill the creatures that you want to. Hopefully the nuances of this new combat scheme will be explained in further detail. I would love a computerized demo like the one that is packaged with the 10th Edition starter kit and hopefully Wizards will put out a new version when Magic 2010 comes out. You can download the Windows demo here (85 MB).

Thursday, June 11, 2009

Magic 2010 Rules Changes

There have been rumors about rules changes for the upcoming Magic 2010 set and Wizards has finally put up an announcement. I’ll go over the high points.

1. Simultaneous Mulligans – because technically one player mulliganed and then the second player mulliganed.

2. Terminology Changes – "In Play" becomes "Battlefield", "Removed from the game" becomes "Exile"

"Cast" is being reinstated as the verb used when referring to the act of playing spells or types of spells. "Play" is being kept as the verb associated with lands (and with cards of unspecified types). Activated abilities are also no longer "played" but rather "activated.

3. Changing "end-of-turn step" to the "end step" – to help clarify the difference between "at end of turn" and "until end of turn" effects.

4. Mana Pools now empty at the end of every step and phases, instead of only at the end of every phase. And mana burn is no more which helps a few cards and hurts other cards like Spectral Searchlight.

5. Token Ownership was changed from "the controller of the effect that put it into play" to "the player under whose control the token entered the battlefield".

6. Combat Damage No Longer Uses the Stack - As soon as damage is assigned in the combat damage step, it is dealt. This is probably the biggest change.

7. Deathtouch and Lifelink are being changed from a triggered ability into a static ability.

Hopefully MTG Forge can implement all of these changes. The biggest difficulty will be changing how combat damage is assigned. What do you think of these rule changes?

Personally I don't have much to say about the rules changes. Some of them will help out new players. I don't think the rules changes will really improve the game but every little bump in the right direction (if this is the right direction) is a good thing. The weather changes, the winds blow, and Wizards changes a few rules, nothing more, nothing less.

Wednesday, June 10, 2009

What is a Card?

MTG Forge has a card object which represents a real-world, cardboard card. This sound simple but it gets a little bit complicated. Let's say that I want to destroy one of your Forest's but you want to bounce it, so you have to know which specific Forest card that I am targeting. In the real world you can just point to the specific Forest, while a computerized game of Magic has to show the user. Magic Online uses nicely drawn arrows while MTG Forge just tells you the targeting card name and its unique number.

At the beginning of the game MTG Forge gives each card a unique number much like the old Shandalar game. Whenever a card is copied, the card's unique number is copied also. In MTG Forge every time a card's attributes need to be reset, like when a creature is going to the graveyard, a new copy of the card is made that has the same unique number. In MTG Forge, every time a card changes zone a new copy is made. I think that making a new copy of a card is easier than saving all of the base attributes and then resetting them, Card.reset().

The card name is important but so is the unique number. Ideally the unique number would not be shown to the user and little arrows would be drawn to show targets.

p.s.
Nothing I write about it really rocket science just one guys opinion about programming Magic. Just finding the subject is the hard part, the rest is usually straightforward.

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.