Coding Cards in C Tutorial
Before you start coding new cards, make sure you know the basics of how to add non-coded cards. This can all be found in the basic tutorial, level 0.
Contents |
Getting Started
The hardest part about coding cards in C may be getting started! These are the steps I followed to get up and running. Several of them may be unnecessary, but it's he only way I know. (I did this in Win XP)
- It is assumed that your magic game is installed in c:\magic. This is required for the build script to work.
- Download and install Camelbox (http://www.softpedia.com/progDownload/CamelBox-Download-105269.html)
- Choose to install all of the components offered, and make sure camelbox/bin is added to your path.
- Download the latest copy of the manalink distribution.
- Unzip the distribution into c:\magic
- Delete everything from c:\camelbox\bin\cards and c:\camelbox\bin\functions, if they exist.
- Copy everything from c:\magic\src into c:\camelbox\bin (you should now have c:\camelbox\bin\build.pl and c:\camelbox\bin\cards )
- Make sure that yasm.exe and make.exe are in the bin directory.
- Open up the command prompt (programs->accessories->command prompt) and cd c:\camelbox\bin
- Run 'perl build.pl' from the command line. The code should compile without errors. (warnings are OK, though)
Adding a New Card
Now we'll try to add a trivial card, Rorix Bladewing. This same card is added in the tutorial, level 1.
- Open src\cards\red_creatures.c in a text editor.
- Copy and paste any other card function (they all start with card_), and change the name of the function to card_rorix_bladewing.
- Delete all of the code in the card except the function declaration.
- Rorix has 2 coded abilities, haste and legendary. Look at the API for Magic Coding in C and see if those functions are available.
- They are both available in the API, so just add calls to those functions in your function.
- Your code should look like:
int card_rorix_bladewing(int player, int card, event_t event){ check_legend_rule(player, card, event); haste(player, card, event); return 0; }
- Run build.pl from the command line. Since we are creating a card for the first time, also pass to build the name of the card. I.e. build.pl rorix_bladewing
- Note the address at which rorix is added.
- Hopefully everything compiled. If not, fix any syntax errors and try again. When you call build, though, do not pass in the name of the card again. That is only required the first time.
- Open SkyMagic Editor and add your card. The address mentioned earlier is the code pointer for your card.
- Save, run csv2dat, etc. Your card has been added. Congratulations.
Deploying your code
- Create a c:\magic\zips folder if it does not already exist.
- Run c:\camelbox\bin\deploy.bat (if you are on the command line and in that dir, just type deploy).
- A new zip file with today's date will be created in c:\magic\zips
Useful Things to Know
When you look at the source for the game, a few files in particular are very important. They are:
- manalink.h - This holds the constants for things like EVENT CODES and CARD STATES. It also contains the data structure for card instances, which comes up in a LOT of code. Finally, this file
holds the definitions for all the functions used in the program.
- manalink.lds - This holds the memory address for all the functions and global variables in Magic.exe. If you are ever trying to read code from Magic.exe, using this as a translation reference is pretty much essential.
