From: John Evans <evansj@DATAWEST.NET>
Subject: My version of counting objects.

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>
