Log in

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.


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)

  1. It is assumed that your magic game is installed in c:\magic. This is required for the build script to work.
  2. Download and install Camelbox (http://www.softpedia.com/progDownload/CamelBox-Download-105269.html)
  3. Choose to install all of the components offered, and make sure camelbox/bin is added to your path.
  4. Download the latest copy of the manalink distribution.
  5. Unzip the distribution into c:\magic
  6. Delete everything from c:\camelbox\bin\cards and c:\camelbox\bin\functions, if they exist.
  7. Copy everything from c:\magic\src into c:\camelbox\bin (you should now have c:\camelbox\bin\build.pl and c:\camelbox\bin\cards )
  8. Make sure that yasm.exe and make.exe are in the bin directory.
  9. Open up the command prompt (programs->accessories->command prompt) and cd c:\camelbox\bin
  10. 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.

  1. Open src\cards\red_creatures.c in a text editor.
  2. Copy and paste any other card function (they all start with card_), and change the name of the function to card_rorix_bladewing.
  3. Delete all of the code in the card except the function declaration.
  4. Rorix has 2 coded abilities, haste and legendary. Look at the API for Magic Coding in C and see if those functions are available.
  5. They are both available in the API, so just add calls to those functions in your function.
  6. 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;
}
  1. 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
  2. Note the address at which rorix is added.
  3. 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.
  4. Open SkyMagic Editor and add your card. The address mentioned earlier is the code pointer for your card.
  5. Save, run csv2dat, etc. Your card has been added. Congratulations.

Deploying your code

  1. Create a c:\magic\zips folder if it does not already exist.
  2. Run c:\camelbox\bin\deploy.bat (if you are on the command line and in that dir, just type deploy).
  3. A new zip file with today's date will be created in c:\magic\zips