tags:

views:

44

answers:

1

I am working on a Magic The Gathering game simulator. I haven't found any free systems that allow you to create your own decks which also manage the game (life/toughness, counters, triggered events, etc.).

My problem is that I need to find an efficient way to have triggered events fire for cards which are in play. I am envisioning the card lists being stored in XML format and containing identifiers for the actions they take like follows:

<card>
   <name>Bloodghast</name>
   <cost>BB</cost>
   <type>Creature</type>
   <subtypes>Vampire,Spirit</subtypes>
   <stats>2,1</stats>
   <color>B</color>
   <attributes>
      <attribute>
         <id>C41</id>
         <description>Bloodghast can't block.</description>
      </attribute>
      <attribute>
         <id>C42</id>
         <description>Bloodghast has haste as long as an opponent has 10 or less life/</description>
      </attribute>
      <attribute>
         <id>LF5</id>
         <description>Landfall - Whenever a land enters the battlefield under your control, you may return Bloodghast from your graveyard to the battlefield.</description>
      </attribute>
   </attributes>

Sorry for making that verbose (and probably not understandable for anyone who doesn't really play Magic). So I would read in this tree for each card that is loaded for a deck. I need a way to hook the attributes of the card into the "game engine" so that, for instance, when a player's life total drops below 10 Bloodghast gains haste, or when a land is played, the engine will allow me to grab him out of my graveyard. For those who have not played Magic, other examples would be that some cards trigger an action when a player discards a card or play a spell of a certain color.

I was thinking I would probably have to have an event handler on the engine for each circumstance which would trigger an event (player plays a spell, player life total changes) and that each card would have to somehow add an element to some list allow it to link itself to that event, but I couldn't even start to think of a good way to do it.

Any help would be greatly appreciated. I'm a pretty decent programmer, but I would rather not have to start this project 5 times and re-write the entire thing.

+1  A: 

I would suggest using a proper database such as SQLite or SQLExpress so you can set up relationships and normalize your data (no comma delimited lists, for example).

Coming up with a rules engine is going to be difficult for Magic; Effects can do all sorts of crazy things and your rules engine will have to accommodate all of them. Essentially, you'll be recreating the rules engine from the Magic: The Gathering video games in order to know what is going to happen and when.

Soviut
I had considered using SQL, but I think the query costs on this type of database would be immense compared to search/read times on an XML doc. My reasoning is that I only need to load 40-70 cards per player, not the entire library. I haven't done any math to support the performance claim, though. And yes, I would have to basically re-create the engine, but I think there is an efficient way to do it.
Boerema
Databases are typically way faster than parsing XML. You only have to perform the queries once since you'll essentially turn all the cards into objects in C# so they can be plugged into the rules engine. You should _not_ be reading the data more than once in a situation like this.
Soviut
To come up with the rules engine you basically have to write down every standard rule in the game (when to tap, when to draw, when effects can be triggered, etc.) write them all down and then start adding every possible effect rule to the list as well. Once you've got a complete list, organize them based on characteristics. These characteristics make up the building blocks for your rules. You then string rules together for each of your cards.
Soviut