Re: Class.c

From: Jason Yarber (
Date: 06/20/02

George Greer wrote:
>         NOTE:   'table' is "best case" because of the way my trivial
>                 benchmark ran the loops. The worst case is equal to or
>                 slower than the silly equations I made up.
> So .00022446 seconds per switch saving throw.  You save a whole .00020559
> seconds by using an equation over the switch.  The only place you ever
> might get a (minuscule) measurable difference is the "levels" command.
> I'd use an equation if we had one with a perfect regression.

Oh, boy, we're talking about specialty.  As you can probably
tell from my tablestoformula patch, I've
already outlined the method of changing most of the switches to single line
formulas, that may run faster than the switch.
I'm pretty sure they won't run any slower.  Now your looking for a perfect
formula for the levels command.  In another
word, your trying to find a decent formula to replace the switches in
level_exp.  Well, it's obvious here that you'd rather
not use my previous formula that simply incremented it by 1000(or another
magical number) for every level.  So here
is a re-written formula that I onced used for a graphical RPG game that I
created on GW-Basic.  Notice, I'm re-writing
it for our specific usage here.

// Sample Code

#define XP_MOD_A  10
#define XP_MOD_B   20

/* Function to return the exp required for each class/level */
// Doesn't this system make more sense? - Jason Yarber
int level_exp(int chclass, int level, int chrace)

  if (level > LVL_IMPL || level < 0) {
    log("SYSERR: Requesting exp for invalid level %d!", level);
    return 0;

  // Gods have exp close to EXP_MAX.  This statement should never have to
  // changed, regardless of how many mortal or immortal levels exist.
  if (level > LVL_MAX) {
    return EXP_MAX - ((LVL_IMPL - level) * 1000);

  // Exp required for normal mortals is below
  return ((level * XP_MOD_A)*(level*XP_MOD_B));

// End Sample Code

As you see, unlike the switch code, this is much simpler, and tends to work
just a tad faster, although you will probably never notice
the difference.  However, you'll notice that my level_exp function also has
int chrace as one of it's arguments.  If I was to even attempt to
recreate the switches for this, it'd be a really confusing and jumbled mess,
cause then I'd have one switch statement for every level, for every race,
and for every class.  After you add a few races and classes, that ends up
being a huge switch statement, and it does take a little processor time to
work through, as
well as it's harder for the coder to understand and track down bugs in.
With a simple formula, it makes everything easier.  Here are example returns
from the
previous sample code that I gave.

Level    Experience
1            200
2            800
3            1800
5            5000
6            7200
7            9800
30          18000
31          50000
32          72000

As you can see, this formula does produce an exponential result.  You can
adjust the modifiers to give larger or smaller results.
However, if you were to use any formula in the level_exp function, I'd
highly suggest checking the value that is returned to make
sure it's not over EXP_MAX  as doing so can cause undesired results.

Jason Yarber
"I'm not an expert, but I'm pretty sure I know what I'm doing."

   | FAQ: |
   | Archives: |
   | Newbie List:   |

This archive was generated by hypermail 2b30 : 06/25/03 PDT