[snippet] My version of counting objects.

From: John Evans (evansj@DATAWEST.NET)
Date: 06/08/98


This is a simple drop-in tid-bit of code that returns the number of items
that someone is in possession of. It counts through containers recursively
so that if someone puts 14 things in a sack that is put in a sack that is
put in a sack that is put in a sack that is put in a sack that is put in a
sack that is put in a sack, it will still count the 14 items and all of
the sacks.

----------
File: utils.h
----------
int     item_count(struct char_data *ch);


----------
File: utils.c
----------
/* Will return the number of items in the container. */
int count_contents(struct obj_data *container)
{
  int count = 0;
  struct obj_data *obj;

  if (container->contains)
    for (obj = container->contains; obj; obj = obj->next_content, count++)
      if (GET_OBJ_TYPE(obj) == ITEM_CONTAINER && obj->contains)
        count += count_contents(obj);

  return(count);
}

/* Will return the number of items that the character owns. */
int item_count(struct char_data *ch)
{
  int i, count = 0;
  struct obj_data *obj;

  for (i = 0; i < NUM_WEARS; i++) {
    if (GET_EQ(ch, i)) {
      count++;
      if (GET_OBJ_TYPE(GET_EQ(ch, i)) == ITEM_CONTAINER &&
          GET_EQ(ch, i)->contains)
        count += count_contents(GET_EQ(ch, i));
    }
  }

  if (ch->carrying)
    for (obj = ch->carrying; obj; obj = obj->next_content, count++)
      if (GET_OBJ_TYPE(obj) == ITEM_CONTAINER && obj->contains)
        count += count_contents(obj);

  return(count);
}


That allows you to do something similar to the following in do_score():

  sprintf(buf, "You possess %d items.\r\n", item_count(ch));
  send_to_char(buf, ch);


Easy enough, eh?

John Evans <evansj@datawest.net>
http://www.hi-line.net/~evansj/              telnet://spear.gator.net:1066

Any sufficiently advanced technology is indistinguishable from magic.
--Arthur C. Clarke


     +------------------------------------------------------------+
     | 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/15/00 PST