Re: [NEWBIE] Object Wearing/ Object Inventory

From: Thomas Arp (t_arp@stofanet.dk)
Date: 02/11/03


From: "Josh Harris" <jharris2@ACSU.BUFFALO.EDU>
> I am wondering how I can implement it into code to do an if statement.
>
> for example if IS_WEARING was the code for being in equipment
>
> if(IS_WEARING(obj_vnum)) {
>
> }
>
> I don't know how to set up this if statement for if he is wearing
> something?

Quite easy. just check GET_EQ(ch, 0 - NUM_WEARS-1).


> Then I also don't know how to set up the if statement for if it's in his
> inventory?

It's not the easiest thing to do, actually. Since the inventory is a
linked list of object structs, and each of these object structs may
contain a link to another list (contents), which again may have
contents, which again may have contents etc. ad inf. The solution
in this case is recursive functions.

I've whipped up some small functions to check worn positions, and inventory.

struct obj_data *scan_for_obj_by_vnum(struct obj_data *obj, obj_vnum ovnum)
{
  struct obj_data *tmp = obj->contains;

  if (GET_OBJ_VNUM(obj) == ovnum)
    return obj;

  for ( ; tmp ; tmp = tmp->next_content)
    return (scan_for_obj_by_vnum(tmp, ovnum));

  return NULL;
}


struct obj_data *char_has_object(struct char_data *ch, obj_vnum ovnum)
{
  int i;
  struct obj_data *tmp = ch->carrying, *ret;

  for (i = 0; i< NUM_WEARS; i++)
    if (GET_EQ(ch, i) && (ret = scan_for_obj_by_vnum(GET_EQ(ch, i), ovnum)))
      return ret;

  for ( ; tmp ; tmp = tmp->next_content)
    if (ret = scan_for_obj_by_vnum(tmp, ovnum))
      return ret;

  return NULL;
}

This (mailercode) can be called like this;

struct obj_data *obj;
obj = char_has_object(ch, 1201);

if (obj) {
  do_stuff(tm);
}

Welcor

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   | Newbie List:  http://groups.yahoo.com/group/circle-newbies/   |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 06/26/03 PDT