Item Count

Login to reply  Page: « < 1 of 1 > »
01 Jun 2010 - 03:352655
Item Count
I added this to my CircleMUD 3.1 based MUD years ago. I started long before tbaMUD came out, which is why I've got a modified Circle3.1 instead of a modified tbaMUD, but you guys do great work here and I would like to continue to contribute to the community.

This is why I would suggest the item counting I have in my MUD's inventory command.
This is what my ACMD(do_inventory) looks like:

ACMD(do_inventory)
{
  send_to_char(ch, "You are carrying:\r\n");
  list_obj_to_char(ch->carrying, ch, SHOW_OBJ_SHORT, TRUE);
  send_to_char(ch, "You have %d items including what you are wearing.\r\n", item_count(ch));
}

In addition, the following are required:
In handler.c (I don't remember where I got this originally, possibly the CircleMUD snippets section, but it was buggy and did not count objects inside containers inside containers. I've fixed it to be properly recursive.)
/* count containers and subcontainers recursively */
int parse_container_items(struct obj_data *cont)
{
	int numitems = 0, i;
	struct obj_data *j;

	for (i = 0, j = cont; j; j = j->next_content, i++)
	{
		if (GET_OBJ_TYPE(j) == ITEM_CONTAINER)
			numitems += parse_container_items(j->contains);
	}

	numitems += i;

	return (numitems);
}


/* call this to find out how many items a character has including what he is wearing */
int item_count(struct char_data *ch)
{
	int numitems = 0, i, i2;
	struct obj_data *j;

	/* count items in the inventory first, including the recursive counting of containers and their items */
	for (i = 0, j = ch->carrying; j; j = j->next_content, i++)
	{
		if (GET_OBJ_TYPE(j) == ITEM_CONTAINER)
			numitems += parse_container_items(j->contains);
	}

	numitems += i;

	/* now count equipment, including the recursive counting of containers and their items */
	for (i = 0, i2 = 0; i < NUM_WEARS; i++)
	{
		if (GET_EQ(ch, i))
		{
			if (GET_OBJ_TYPE(GET_EQ(ch, i)) == ITEM_CONTAINER)
				numitems += parse_container_items(GET_EQ(ch, i)->contains);
			i2++;
		}
	}

	numitems += i2;

	return (numitems);
}

This has other uses too, I've used item_count(ch) in many places in my code... mostly in immortal functions. Feel free to use this, but make sure that you make a note in the credits that the original code is by an unknown author and only modified by me... and your staff if you make any further mods to it.

Also, it obviously needs prototypes, you guys know how to make those so I didn't include them ;)

I wish you guys kept the dg scripts updated for stock circle 3.1 with Oasis 2.0.6 too ;)
You've added so much lol... oh well. I'm happy with what I have ;)

Cheers, till next time!


__________________
Owner/Coder/Head Admin
Caer Dubrin
Login to reply  Page: « < 1 of 1 > »