Wednesday, July 2, 2008

Compile versus Runtime

First, let me define the terms compile and runtime. Transforming a programming language, like Java, into an executable file is called “compiling.” Runtime is when the program is actually running on your machine. For further details see the Wikipedia for compile time and runtime.

For programmers there is a constant battle going on. The battle is this: should the logic of a program be specified at runtime or compile time? Specifying compile time is easy. Below a tax variable is created, this is an example of compile time.
float tax = 6.5;
Runtime code would read the tax variable from a file. Runtime code is more flexible but harder to write. The runtime code would also require additional error checking to see if the file exists, see if the number is valid, etc...

Most programs use both compile and runtime code. At runtime MTG Forge reads a list of card names, mana costs, and text from the file “cards.txt”. Recently someone e-mailed me and said that Sprout Swarm should be an instant. All I had to do was to change it in the card.txt file, nothing else had to be done.

While it would be easy for a non-programmer to say that all variables and logic should be specified at runtime, it is not feasible. Some parts of a program, like the tax variable, might change in the future so it should be created at runtime. It is a design decision whether to make the code runtime or compile time.

I use the file “cards.txt” for convenience because it allows me to easily add simple creatures. Simple creatures are creatures that have a keyword like flying, haste, or landwalk. MTG Forge uses card.txt to make creatures at runtime. All activated abilities and sorceries/instants are created at compile time.

Keywords that cards.txt recognizes. All the keywords have to have this exact spelling, with Haste being capitilized and "tap: G" having only 1 space after the colon.

Mountainwalk
Forestwalk
Islandwalk
Plainswalk
Swampwalk
Indestructible
Defender
Fear
Flying
Haste
tap: add 1
tap: add B
tap: add G
tap: add R
tap: add U
tap: add W
This creature can block as though it had flying.
This creature cannot block
Trample
Unblockable
Vigilance

8 comments:

Seth said...

It's nice to have the list of keywords that work in the cards file. Some other good ones to include would be the rest of the "evergreen" keywords. Since they are likely to be in almost every set.
DEATHTOUCH
DOUBLE STRIKE
FIRST STRIKE
FLASH
LIFELINK
SHROUD

REACH - to replace "This creature can block as though it had flying."

PROTECTION - just for the 5 colors since it would be impossible to handle all the variations

REGENERATION - not sure if this is doable either

Unknown said...

The game doesn't have any first strike or double strike capability (yet). Flash, maybe. Deathtouch and Lifelink might be doable, somewhere in the combat code except when damage is caused by an ability.... Shadow could be worked into the blocking code. (not on your list, but Shroud reminded me) Shroud and parts of protection isn't, because all the various card code is inconsistent in checking a canTarget() function. Reach could be changed.

Regeneration and many other activated abilities won't be so easy... it's what I've been working on. The game has various standardized effects, like regeneration and firebreath (R:+1/+0), so implementing the code to handle the action is easy. The hard part is parsing the different costs for the abilities. Even limiting costs to mana and/or tapping might not reduce this complexity enough.

Forge said...

Hopefully in the future more keywords will be supported. Keywords simplify adding new creatures.

Cards.txt is great because it lets you easily add new creatures, maybe later it will also let you add spells. (Currently cards.txt only holds the text for spells but none of the functionality.)

Forge said...

Evergreen keywords (keywords that will be used over and over again) should probably be at the top of my list of "things I should add".

Protection is the hardest since the whole program has to support

addDamage(Card target, Card source,
int damage)

And combining keywords like lifelink and trample is tricky.

Anonymous said...

for flash, you could prolly make a new funky card type that works just like an instant and is a creature (InstantCreature! just add water).

Forge said...

Rob Cashwalker just just sent me code that easily adds the flash keyword to cards.txt. When the card is asked, "Are you an instant" it returns true if it has the Flash keyword.

Silly Freak said...

hmm... that "are you" is nothing you can do at the moment? I may be repetant, but a method "what P/T are you" that could take GA into account without ever updating an attribute seems very similar to me. It would also avoid bugs when changing control and so on. the same for changeling. well, you know what I mean it anyway.

Anonymous said...

wellif hiding identity mandatory religions mangalodayam synergies observations deposits halves barometer
masimundus semikonecolori