This article describes the basics of representing and using Magic Spells in roguelikes.
The general techniques are very similar to those used in my article on Object
representation.
For starts, we need a class to hold the information for a specific spell.
class Spell {
public:
OK. Now, let's give the spell a name.
int MPCost;
Also, every spell has a level: how difficult a spell it is. Only more powerful casters can
use more powerful spells.
int Level;
There. Now all we need to know is what in Gehenna the spell does. We'll make a
sub-class called Effect to store this information.
class Effect: {
friend Spell;
OK, so what does a specific spell do? We'll make a simple int to describe what a specific
Effect describes.
Effect_Type Type;
So what does Type mean? We'll use an enumeration to specify.
enum Effect_Type
{
HEAL,
SCORCH,
TICKLE,
CREATE_OBJ
};
You can of course add as many as you want. Now, we know what an Effect does, but
that's not enough. For example, for a HEAL Effect, how much HP does it heal? We
could base everything of level (making a rigid and uniform magic system, which may
be what you want: predictability), or we could give each Effect a set of arguments to
define in more detial what it does. We'll do this through the use of 5 int's.
int Args[5];
What do the fields of Args mean? That's based on what Type is equal to. For an Effect
with Type HEAL, we might say that:
Args[0] is Number of Dice
Args[1] is Sides per Die
Args[3] is Roll Mod
So an Effect of type HEAL with Args[0] = 2, Args[1] = 6, Args[3] = 2 would heal 2d6+2
HP. Pretty simple, eh?
Anyways, we can close of the Effect class. We want each Spell to have 5 Effect's (so
every spell can have lots of flexibility).
} Effects[5];
We can now close of the Spell class.
};
So that's all there is to a basic magic class.
Casting the spell is just as simple. Make a function called Cast or whatever (I used an
object method inside the Spell class, since I'm a C++ OOP freak). The function would
take as arguments the spell to cast, the target, etc.
void Cast ( Spell *SpellToCast, int TX, int TY );
Then we go with a big, evil, switch statement based on effect. This actually works
VERY well. The flexibility is astounding...
Of course, how each spell takes effect is based on how you've programmed the rest
of your roguelike. Because of the OO nature of WotR, I found it very easy to create
simple object Methods for spell effects.
For the HEAL Spell Effect Type, you might to do something as simple as loop through
all the Characters (NPC's and Players) in the target loc (defined by TX and TY), and
heal them based on the arguments listed above... 2d6+2, or whatever.
Anyways, this is just the basics. The advanced stuff all depends on your magic
system and how you programmed the rest of the game.
The complete source for the Spell class is:
enum Effect_Type
{
HEAL,
SCORCH,
TICKLE,
CREATE_OBJ
};
class Spell {
public:
char *Name;
int MPCost;
int Level;
class Effect: {
friend Spell;
Effect_Type Type;
int Args[5];
} Effects[5];
};
Any questions, comments, threats, etc., e-mail me at
sean.middleditch@iname.com
Well, I don't really want any threats.
The End
Sean Middleditch