Re: array question

From: Andrey Fidrya (andrey@ALEX-UA.COM)
Date: 10/05/97


> Okay, what I mean is.. I have an array in the pfile.. int memorized[100];
> which stores spellnums, if they forget a spell, I want it to set the
> spellnum to 0, and if there is a 0 in the array, I want it to move all
> entries to the left one after the zero, so there are no more zeros until
> the end of the array
> i.e.
> 1, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 5
> would equal this:
> 1, 2, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

I had almost same situation.
Best solution was to call sorting function only after "forget" and "cast"
to remove the "0" hole from array.
Instead of sorting, just move other spells 1 position left.
Function below will sort only those spells, which are located _after_ the
forgotten one. (to save time)

#define GET_SLOT(ch, i) ((ch)->char_specials.saved.memorized[i-1])

void mem_sort(struct char_data *ch, int starting_slot)
{
  int current_slot = starting_slot;

  for (; current_slot < 100; ++current_slot) {
    if (GET_SLOT(ch, current_slot) == 0) { /* no spell here */
      if (GET_SLOT(ch, current_slot+1) == 0)
        break; /* other slots are empty */
      GET_SLOT(ch, current_slot) = GET_SLOT(ch, current_slot+1);
      GET_SLOT(ch, current_slot+1) = 0;
    }
  }
}

To save speed you can comment out the
 if (GET_SLOT(ch, current_slot) == 0) {} check.
Just pass correct parameters to function.

Call this function from function like:

/* forget memorized spell. used by do_cast */
int mem_forget(struct char_data *ch, int spellnum)
{
  int current_slot = 1;

  for (; current_slot <= 100; ++current_slot) {
    if (GET_SLOT(ch, current_slot) == spellnum) {
      /* forget the spell */
      GET_SLOT(ch, current_slot) = 0;
      /* move other spells 1 position left */
      mem_sort(ch, current_slot);
      return TRUE;
    }
  }
  return FALSE;
}

Function above should be called from do_cast or do_forget,
for example:

if (!mem_forget(ch, spellnum)) {
  send_to_char("You haven't that spell memorized", ch);
  return;
} else
  do_whatever_spell_does;


I hope this helps.
Btw, this is a mailer code and not guaranteed to work, but it should. :)

In RMUD I'm using slightly different system:
player or mobile can have up to 9 spell circles, each circle
can contain up to 14 spells. (I'm using 2d array for slots).
While player gains levels, more and more spell circles and slots
being made available to him.

  Andrey (andrey@alex-ua.com)      Kiev, Ukraine
    aka Zmey//RMUD (Russian MUD Project)


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/08/00 PST