Monday, November 29, 2010

AI - Continuing saga

(Here is an article written by lazylockie, which is his username on the forums.  You may think this is a cop-out and that I'm extremely lazy and you would be correct on both accounts.  So without further ado...)

In this article I’ll talk about Forge – yeah, obviously – but more specifically about some aspects of Forge’s AI.

Why is the AI weak?
I think the main reason sometimes the AI is so weak is because it doesn’t interact the human on any other phase other Main. This limits a lot the card pool they can effectively use. We know the developers are working on this, but it’s a nice advantage that we players have against them.

For example stuff like Giant Growth, Basking Rootwalla and Sorceress Queen are usually the Achilles Tendon of AI. AI happily attacks with a White Knight onto your Basking Rootwalla and suddenly you’ve gained advantage on a situation that in real life wouldn’t have happened. Stalemates are more easily broken and often lead into human victory.


AI needs to be generic
How is your general behavior when playing with Jace, the Mind Sculptor? I assume it would be something like: If you’re not playing against burn or there’s no threat on opponent’s field, Brainstorm. This could be, in pseudo code – albeit probably codeable on Forge:

Jace, the Mind Sculptor behavior
if (there’s a “dangerous” creature on opponent’s field)
  -1 ability targeting that creature

if (loyalty == 1) OR (opponent has a basic land AND loyalty <= 3)
   +2 ability targeting opponent
else
   +0 ability //Brainstorm for card advantage

 The pseudocode above isn’t perfect, but practically covers all common sense functionality of Jace. However, as mtgrares (the author of this bog) already mentioned on past articles, designing an AI behavior for each card created is hard/confusing and even then it might not work as expected, since Magic involves interactions between many cards at same time.

Another example that is often seen is Baneslayer Angel. The little lady is almost auto-win on the hands of a human player if it connects, because common sense says that that gal must attack every turn, even when on board disadvantage, because it has evasion and lifelink. However, AI doesn’t comprehend that Baneslayer has a 10 point life swing, so in dire situations he’ll often leave it for blocking, when he could have attacked and won the battle.

Why the AI prefers simple decks
If there’s one thing that AI is good at is brute force. Anything that is big and cheap is often a good deal for AI. That’s why Affinity decks are so hard on the hands of AI, especially at early levels of Quest mode: big stuff like Myr Enforcer is free and the deck is pretty straightforward. The fewer decisions the deck needs to make, the better AI will handle that deck. Tarmogoyf, considered one of the best creatures on Legacy, fits this function of brute force quite well: easily splashable, low mana investment, grows quickly as game progresses. It amuses me why Tarmogoyf isn’t widely used on AI decks.

(This is mtgrares talking now)
Let me briefly critique the pseudocode.   The AI has a hard time determining if you (the opponent) has a dangerous creature on the field.  How dangerous is dangerous?  Is a flying 2/2 dangerous or does it have to be a flying 4/4?  Royal Assassin is very dangerous bit the AI won't recognize it has dangerous, since it is only a 1/1 and the AI cannot "see" activated abilities.

I would suggest using Jace to look at his opponents library.  If the opponent has less than 6 lands the AI would put basic lands on the bottom of your library and if the opponent has more than 6 lands, do the opposite, put any spell on the bottom of your library.  Then use Jace's ultimate ability to win the game.

And to finish the article on a funny note, "I am the mustard seed of your doom" which is taken from the videogame Mario and Luigi: Bowser's Inside Story. 

--mtgrares

Monday, November 22, 2010

Generate Deck - Play by Play


I love Forge's generate deck option.  It was something that occurred to me randomly and has grown to be one of my favorite ways to play Magic.  Today, I'm going to play through one game against the AI and describe all of the wonderful, exciting stuff that happens.

I lost the coin toss and I'm going second.  My opening hand has 4 lands, 3 cost creature, 6 cost creature, and an creature enchantment (aura).  I should win if I can play Plasma Elemental (cost: 5U, 4/1) since it is unblockable, if the AI doesn't weenie rush me.
The AI plays a land.

I draw the nearly worthless Demolish (cost: 3R, sorcery) which says "Destroy target artifact or land."  I play an Island.

The AI plays a different land, so he has access to both of his colors.

I draw another Island and play a Mountain.

AI plays another Plains.  (Those Zendikar full-frame lands do look great.  I was hoping that Wizards would print all new lands like that.)

I play a 2nd Island and cast my Vodalian Knights (cost: 1UU, 2/2) which has first strike and gains flying for U.  I caught a big break since Vodalian Knights can't attack unless the AI has an Island, which it does.

The AI plays a 4th Island and plays Cessation (cost: 2W, enchant creature, enchanted creature can't block) on my Knights.  At least Vodalian Knights can still block.

I draw the insane Phantasmal Forces (cost: 3U, 4/1, flying) "At the beginning of your upkeep, sacrifice Phantasmal Forces unless you pay U" and cast it.

The AI plays another land (now he has 5) and Psionic Blast's my Phantasmal Forces.

I draw and play another land so now I have 5.  I don't have any creatures that I can play yet so I destroy one of the AI's land with Demolish and I play Brainstorm (cost: U, instant, Draw three cards, then put two cards from your hand on top of your library in any order.) Brainstorm doesn't show me any creatures.

The computer plays Keeneye Aven (cost: 3U, creature, 2/3, flying).

I cautiously play my game winning Plasma Elemental (4/1, unblockable) and hope the AI doesn't notice.

The AI plays Diplomatic Immunity (cost: 1U, enchant creature) which gives his Keeneye Aven shroud and swings for 2. 
AI - 18 life, Me - 18 life

I enchant Plasma Elemental with Volcanic Strength which increases Plasma's stats by +2/+2 and swing for 6.
AI - 12 life, Me - 18 life

The AI destroys Volcanic Strength with Frantic Purification (cost: 2W, instant, Destroy target enchantment) and attacks with Keeneye Aven for 2.  AI - 12 life, Me - 16 life

I draw the obscure card Onslaught (cost: R, enchantment, Whenever you cast a creature spell, tap target creature) which doesn't help me at all but I play it anyways.  I attack with Plasma Elemental for 4.
AI - 8 life, Me - 16 life

The AI coolly attacks for 2 and then plays the game changing Meloku the Clouded Mirror which has the ability to turn all of your lands into 1/1 flyers.  Lets hope the AI is dumb and doesn't use Meloku's ability.
AI - 8 life, Me - 14 life

I draw another odd Magic card, Bringer of the Blue Dawn which costs an insane 7UU.  I only have 7 lands so Bringer is unplayable.  I swing for 4 with Plasma Elemental.
AI - 4 life, Me - 14 life

The AI swings for 2 with Keeneye Aven and only creates one flying token.  The AI seems smart and plays Shu Soldier-Farmers (cost: 4W, 2/4) which has a triggered ability, so the AI gains 4 life.  That extra 4 life means that the AI can live an extra turn.
AI - 8 life, Me - 12 life

I draw and play another land.  I swing again for 4 with Plasma Elemental.
AI - 4 life, Me - 12 life

The AI creates one more token with Meloku and attacks with almost everything, for a total attack of 6.  I sacrificially use Vodalian Knights to reduce the attack to 4.
AI - 4 life, Me - 8 life

Finally victory is within my grasp.  I attack with Plasma Elemental and win the game.  All in all it was a good game.  The AI whittled my life down to 8 and threw me a few curveballs.  Plasma Elemental was an all-star and I would have certainly lost the game with any other creature.  In this format the creatures win the game.  Generated decks feel similar to sealed decks because they are a little random and not very focused.

Playing a quick game like this is very satisfying.  Magic is a fun game with lots of variety.  I love not knowing which cards are in my deck.  I love that feeling of surprise and of course winning occasionally doesn't hurt. :)  I'm glad that the AI put up a good fight and didn't seem to do anything stupid.

Keep tapping,
mtgrares

p.s.
--The generate deck code that I wrote still has some major flaws in it: it doesn't generate a mana curve and it could exclude some awful cards which are only useful if you build a whole deck around them.  I prefer generated decks without creatures with a low power (0 or 1) and without artifacts.  The menu options are in the New Game screen, under Options.

--Writing is hard work.  It took me an hour to write the 900 or so words above.  I have no idea how the guys at Star City Games churn out so many pages each week.

Friday, November 19, 2010

Jesus Follow-up

I figured my Jesus post would generate a number of comments.  Most personal blogs are so random that they are unreadable so I have intentionally focused on making all of my posts about Magic or programming.  So rather than diluting this blog, maybe I’ll start another blog where I talk only about Jesus (but I’ll probably sneak in some Magic stuff).

I realize that religion is a volatile topic and everyone has a strong opinion.  One of my favorite shows is House and he despises religion.  I also know that Karl Marx said that “Religion is the opiate of the masses”.  But neither House nor Karl Marx have changed my stance on Jesus.

The truth about blogging is that I have no idea what I’m doing even though I’ve written more than 100,000 words.  I’m just a guy who writes stuff for an unseen audience.  Yes I can look at the hit count and say that the average number of views in a day is 500 to 800 but those are just numbers and people are different than numbers.  To me blogging is just a big experiment and the only reason that I blog is to “advertise” Forge.

Thanks for your patience,
mtgrares

p.s.
The new opiate of the masses is the Internet and before that was the TV.

Thursday, November 18, 2010

Do you know Jesus?

I wanted to briefly talk about something more important than Magic.  I believe that heaven and hell are real, literal places and that the only way to get into heaven is to ask Jesus into your heart.  The common belief is that a person has to be “good” or do a few “good works” to get into heaven but the Bible says our good works are as “filthy rags”.  Nothing you can do (without Jesus) will get you into heaven. 

The only way to get into heaven is to ask Jesus into your heart and to ask him to forgive your sins.  If you have never read the Bible, start with Matthew which describes the life of Jesus.

p.s.
I have been reluctant to talk about Jesus on my blog but since Jesus is the most important part of my life, I should talk about him more. 

Monday, November 15, 2010

How to NOT Write a Magic Clone


Let's say that you wanted to write your own implementation of Magic (for some unknown reason). Naturally you would create a Card class which mimics a real, cardboard card. And for some crazy reason you wanted to add your favorite card from Planeshift Singe (R, Instant, "Deals 1 damage to target creature"). Obviously you are not a "power gamer" but that is ok, because you like who you are....maybe.

The simplest way to implement effects like Singe is to allow a Card object to deal damage to another Card object. The code for Singe would look something like this:
Singe.resolve()
{
  getTargetCard().addDamage(1)
}
So far so good, one card down, a bazillion to go. Now you want to code a card that says, "Whenever you deal damage, you draw 1 card". Suddenly your world comes crashing down because your simple implementation doesn't work. Card objects should not deal damage to each other, so you use a layer of indirection. Singe could be written as:
Singe.resolve()
{
  RulesEngine.addDamage(getTargetCard(), 1)
}
Now you want to add another card, Awe Strike (W, Instant, "The next time target creature would deal damage this turn, prevent that damage. You gain life equal to the damage prevented this way").

The question is this, "Can we add Awe Strike using the method above?" Truthfully the answer is maybe. If every card uses the rule engine hopefully you should be able to code Awe Strike. Unfortunately the devil is always in the details.

The goal when programming a Magic clone is to give as much information as possible to the rules engine so it can do the insane things that Magic cards do like Painter's Servant

If the rules engine isn't given enough information eventually your Magic clone will hit the wall and not be able to implement a card or keyword.

Keep on tapping,
mtgrares

p.s.
The craziest card I know of is Warp World (5RRR, sorcery) which dumps all of your permanents into your graveyard and then you draw that many cards and put those permanents onto the battlefield. The Magic 2010 set faq lists 6 separate sub-steps.


--“All problems in computer science can be solved by another level of indirection.”

--You can read more about my idea of making the rules engine object huge because it has to handle everything in the whole game.  (Most of the RulesEngine object would just act as a "wrapper" for other objects/methods.)  Big objects are usually to be avoided because they have too much complexity but I don't see another way to handle a game like Magic.  You can read a previous post about this subject here.

Wednesday, November 10, 2010

Forge Download - With Decks

I forgot to include decks in the most recent verison of Forge.  That is fixed now.

You can download more decks from Forge's deck forum here.

Monday, November 8, 2010

Forge’s Strengths

Some people have accused me of talking more about Forge’s weaknesses than strengths, so I decided to remedy (fix) the situation. Forge has a number of strong points such as: good architecture, quest mode, and a number of great volunteers.

Talking about Forge’s software architecture is a little abstract so let me provide a comparison. A program’s architecture is the foundation that the rest of the program is built on. The foundation can be marginally improved but basically the foundation restricts what the program can and cannot do.

Forge’s architecture isn’t perfect but I would give it a good B. I assumed most costs would be mana costs which is true 90% of the time but this decision makes adding cards with additional costs (like sacrificing a permanent) more complicated. Looking back it would have been better to make cost an abstract class (or interface in Java terms) so that the cost could be anything. On the positive side Forge’s architecture lets you basically do (almost) anything, so you can hack it do death in order to support weird/unusual cards. Most of Forge’s architecture was built by me but other people have improved my code. I remember trying to code Time Walk and Forge just wouldn’t support it, but now Forge has Time Walk, so I am very impressed.

Forge’s greatest feature is the fantasy quest mode. I like to think of it as a poor man’s Shandalar since it is only menu based. In the beginning you start out with a random cardpool and you buy or win more cards. Quests can be as short as 10 wins or as long as you want it to be. You have 200 different decks that you may face.

The fantasy quest mode also allows you to buy more life, since you start at with 15. You can also purchase a free mulligan (you draw 7 cards) or a creature to begin the game with (either a plant wall or a wolf). You can also buy a boost that adds 10% to all of your match winnings as well as improving your selling percentage to the card shop by 1% (which is a lot since the card shop usually only buys at 10%). After 20 wins you may also get to face specialized AI opponents that have an unfair advantage. To add some tention, some of these special opponents can only be played once, so you better make sure to win. (A big thanks to Dennis for the fantasy quest mode.)

And lastly Forge is great because it has a good number of volunteers. Without these volunteers Forge would have probably been stuck with only 1,000 cards. These guys have improved the user interface, added the fantasy quest mode, and added/fixed numerous cards and bugs in Forge. While Forge didn’t have any documentation (which is my fault) the code seems to be very readable and I’m glad that these guys can figure out my (insane) code. Nowadays they know more about Forge than I do.

I never dreamed Forge would have over 5,000 cards or be downloaded 1,500 times a month. Forge has been the biggest project I have worked on and I’m just glad that Forge’s architecture didn’t implode because of all of the hacks. (Earlier versions of Forge did sort of collapse under their own weight because the foundation was not good enough.)

“Never debug standing up”

--mtgrares

p.s.
I hate to explain this quote because then it isn’t funny but I don’t want to leave anyone out. “Never debug standing up” is like saying, “No matter how long you think a job will take, it will always take more time” similar to Murphy’s Law.

p.p.s.
A clone of Shandalar is being worked on. You will walk around and do similar quests.

Monday, November 1, 2010

New Version

This new version of Forge has almost 800 new cards and a world-bashing total of 4,890 cards. (P.S. Don’t ask me what “world-bashing” means, I just made it up.)

I’m just going to list some of the new cards without any rhyme or reason. Bubble Matrix (4, artifact) says “Prevent all damage dealt to creatures” so you will probably need an alternate win condition or try burning your opponent off the face of the planet. Claws of Gix (0, artifact) “1, sacrifice a permanent: You gain 1 life” will interest some deck builders like moths to a nuclear bomb.

Fortune Thief (4R, creature, 0/1) will always keep your life at 1 like Ali from Cairo. Fortune Thief does have morph and can be flipped for RR. Some people may remember Arcbound Ravager (2, artifact creature, 1/1) combine him with Disciple of the Vault to damage your opponent when artifacts go to the graveyard. Umbra Stalker (4BBB, creature, */*) its power and toughness are equal to the number of black mana symbols on the cards in your graveyard. At least it gives you one reason to mill yourself.

Download Forge (10 MB) – Runs on Windows, Mac, Linux and requires Java 1.5 or higher.  This is the 10/19 version because that is when it was released on the forums.

Download (109 MB) - LQ (low quality) Card Pictures - delete your /res/pic/ directory because this file creates a new directory named "pic"

I’m sure there are other mind-blowing cards but you will have to find them for yourself. Download and enjoy.  --mtgrares

(For some reason Word 2010 wants to change "artifact" to "artefact", go figure.  Computers are .... weird sometimes.)

p.s.
Many people helped with this version. Programming can be very hard and frustrating. A big THANK YOU goes out to them:

Dennis Bergkamp
Rob Cashwalker
Friar Sol
Slapshot5
Sloth
Zerker2000
Gofishus
Hellfish
Nerzahd
PhoenixAvenger
Chris H