Re: [CODE] Introduction Snippet

From: Peter Ajamian (peter@pajamian.dhs.org)
Date: 11/13/00


"Lewis, Jason" wrote:
>
> If you need some code for this then reply back to the list and I'll be
> glad to help.
>
> I am gonna take you up on this Peter and I appreciate the help.

Alright...

You need to add the array to struct char_data (in structs.h) first,
something like this...

unsigned long *knows; /* Array that holds the info of who is known to
char */
long knows_length;    /* length of array */

Then when a player is loaded you will need to allocate space for the
array and load it up, use something like this...

long l;

ch->knows_length = top_idnum/32 + 1;
CREATE(ch->knows, unsigned long, ch->knows_length);
for (l = 0; l < ch->knows_length; l++)
  ch->knows[l] = 0;

l is simply a counter shich needs to be of type long and defined at the
top of the function.

knows_length is so that you know how long your array is so that you know
when you need to resize it.

The CREATE line tells your program to allocate space for
ch->knows_length unsigned longs and place a pointer to that in
ch->knows.

The for loop simply zeros out the memory that was just allocated.

We'll get back to loading up the array later on.

Okay, you'll need the following to access it...

void resize_knows(struct char_data *ch, struct char_data *tch)
{
  long l;

  if (IS_NPC(ch) || IS_NPC(tch))
    return;

  if (GET_IDNUM(tch)/32 >= ch->knows_length) {
    l = ch->knows_length;
    ch->knows_length = top_idnum/32 + 1;
    RECREATE(ch->knows, unsigned long, ch->knows_length);
    for (;l < knows_length; l++)
      ch->knows[l] = 0;
  }
}

#define IS_KNOWN(ch,tch) \
  (CHECK_PLAYER_SPECIAL((ch),CHECK_PLAYER_SPECIAL((tch), \
  (GET_IDNUM(tch)/32 < ch->knows_length) && \
  ((ch)->knows[GET_IDNUM(tch)/32]&(GET_IDNUM(tch)%32)))))

void make_known(struct char_data *ch, struct_char_data *tch)
{
  if (IS_NPC(ch) || IS_NPC(tch))
    return;

  resize_knows(ch, tch);
  ch->knows[GET_IDNUM(tch)/32] |= (1<<GET_IDNUM(tch)%32);
}

void make_known(struct char_data *ch, struct_char_data *tch)
{
  if (IS_NPC(ch) || IS_NPC(tch))
    return;

  if (GET_IDNUM(tch)/32 >= ch->knows_length)
    return;

  ch->knows[GET_IDNUM(tch)/32] &= ~(1<<GET_IDNUM(tch)%32);
}


As far as loading and saving to file is concerned, you have several
options which have thier own benefits, or lack thereof...

Binary has the downside of being fixed length, but you can vary the
length simply by saving the length and offset in a fixed location within
the file, then you know how far to advance to get to the entry you're
reading in.  Binary is also the most efficient when it comes to storage
space.

ASCII is less efficient in storage space and reading and writing, but it
is easier to work with.

In both forms you also have the choice of either saving data on all the
players or just for those players who are known.  Saving data only for
players who are known is more costly in terms of the amount of space per
entry (approx 32 times the space) but requires fewer entries.

You also have the choice of either using a separate file for each player
or saving all the data to one file.  Saving to a separate file makes for
faster loading and saving but is less efficient on disk space.  If you
are using ASCII pfiles then the choice is already made for you as ASCII
pfiles save in a separate file for each player anyways.

Lemmie know what you decide and we'll go from there.

Regards, Peter


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



This archive was generated by hypermail 2b30 : 04/11/01 PDT