Author Topic: Re structuring the Inventory.  (Read 5587 times)

Offline shigs

  • Acquaintance
  • *
  • Posts: 37
    • View Profile
Re structuring the Inventory.
« on: March 05, 2008, 03:41:19 pm »
I've spent hours trying to re-code the inventory being unimpressed by how it simply listed all the items a player was carrying without it stating what was wielded, what was worn and what the player was actually carrying so I put togther this.

Code: [Select]
//Written by Shigs - 03-05-2008
//Lists items in your inventory

//:PLAYERCOMMAND
//USAGE:  i
//        inventory
//
//Shows you what you have in your inventory.

#include <flags.h>
inherit CMD;

private void main()
{

    string wielded = "";
    string worn = "";
    string carrying = "";
    string ex;
    object ob;
    int a = 0;
    int b = 0;
    int c = 0;
    //Constrcut Wielded string
    //Construct Worn String
    //Construct Carrying String


    object * items = all_inventory(this_body());

    foreach(ob in items)

if (duplicatep(ob))
{
out "there is more than one of" + ob;
    }
if(ob->test_flag(F_WIELDED)){
if(!a){
wielded += " Wielded : " + capitalize(ob->the_short());
a = 1;
            }else{
wielded += "\n\t    " + capitalize(ob->the_short());
        }
             
                   
}else if(ob->test_flag(F_WORN)){
if(!b){
worn += "\n    Worn : " + capitalize(ob->the_short());
b = 1;
            }else{
worn += "\n\t    " + capitalize(ob->the_short());
        }
}else{
if(!c){
carrying += "\nCarrying : " + capitalize(ob->the_short());
c = 1;
            }else{
carrying += "\n\t   " + capitalize(ob->the_short());
        }

}

    }

    ex = wielded + worn + carrying;
    out("%^RED%^===============================%^RESET%^\n");
    out("\tINVENTORY\n");
    out(" You are carrying " + sizeof(items) + " items.\n");
    out("%^RED%^===============================%^RESET%^\n");
    if (ex)
out(ex);
    else
out("You are empty handed.\n");

    out("\n%^RED%^===============================%^RESET%^\n");
    return;
}

Now forgive my slightly hacked toghther code I'm still getting used to things.. this displays as.

Quote
===============================
        INVENTORY
 You are carrying 9 items.
===============================
 Wielded : The Fencing Saber
    Worn : The Alliance Vest
Carrying : The dull sword
           The dull sword
           The dull sword
           The map
           The small leaflet
           The LIMA Guide Book
           The full armor eight
===============================


vs
Quote
You are carrying:
  3 dull swords
  a map
  a Fencing Saber (damaged) (wielded in right hand)
  a small leaflet
  the LIMA Guide Book titled '0'
  a Alliance Vest worn over torso and arms
  a full armor eight


The problem is, as you can see, it dosen't group up duplicate items.

I've trudged through the code and found how the inital inventory code does it by calling inv_list,

Code: [Select]
varargs string inv_list(object array obs, int flag, int depth) {
    string res;
    int j;

    depth++;
    res = "";
    foreach (object ob in obs) {
if (!ob) continue;
if (!ob->is_visible()) continue;
if (!ob->short()) continue;
if (flag && !ob->test_flag(TOUCHED) && ob->untouched_long()) continue;
if (ob->is_attached()) {
    if (ob->inventory_visible() && !ob->query_hide_contents())
res += ob->inventory_recurse(depth);
    continue;
}
if (!duplicatep(ob))
{
    for (j=0; j<depth; j++) res+="  ";
    if ((j=count(ob))>1) {
if (j > 4) res += "many " + ob->plural_short();
else res += j + " " + ob->plural_short();
    } else {
if (ob->is_living()) {
    res += ob->in_room_desc();
} else {
    res += ob->a_short() + ob->get_attributes();
}
    }
    res += "\n";
    if( ob->inventory_visible() && !ob->query_hide_contents())
res += ob->inventory_recurse(depth);
}
    }
    return res == "" ? 0 : res;
}


which uses duplicatep to check if the item is a duplicate.

Code: [Select]
varargs int duplicatep( object o ) {
    int i;
    object *obs;

    if (!o)
o = previous_object();

    obs = all_inventory(environment(o));
    for (i=0; i<sizeof(obs); i++) {
if (obs[i]==o) return 0;
if (compare_objects(obs[i], o)) return 1;
    }
}

I sort of understand the code, but it might as well be in German for all my 'sort of understanding 'it goes.. because I can't for the slightest figure out how to implement anything passed checking for duplicate objects.

any suggestions, Gents?


Offline shigs

  • Acquaintance
  • *
  • Posts: 37
    • View Profile
Re: Re structuring the Inventory.
« Reply #1 on: March 05, 2008, 05:39:28 pm »
Thanks to the suggestions on icode/intermud and some help lots of guidance from Amok, I managed to sort it out, by creating another copy of the array and taking away all the wielded and worn items and then passing it to a modified version of inv_list.

Quote
./inv
===============================
        INVENTORY
 You are carrying 9 items.
===============================
 Wielded : The Fencing Saber
    Worn : The Alliance Vest
Carrying :
          3 dull swords
          a map
          a small leaflet
          the LIMA Guide Book titled '0'
          a full armor eight

Code
Code: [Select]
//Written by Shigs - 03-05-2008
//Lists items in your inventory

//:PLAYERCOMMAND
//USAGE:  i
//        inventory
//
//Shows you what you have in your inventory.

#include <flags.h>
inherit CMD;
varargs string inv_list2(object array obs, int flag, int depth) {
    string res;
    int j;

    depth++;
    res = "";
    foreach (object ob in obs) {
if (!ob) continue;
if (!ob->is_visible()) continue;
if (!ob->short()) continue;
if (flag && !ob->test_flag(TOUCHED) && ob->untouched_long()) continue;
if (ob->is_attached()) {
    if (ob->inventory_visible() && !ob->query_hide_contents())
res += ob->inventory_recurse(depth);
    continue;
}
if (!duplicatep(ob))
{
    for (j=0; j<depth; j++) res+="          ";
    if ((j=count(ob))>1) {
if (j > 4) res += "many " + ob->plural_short();
else res += j + " " + ob->plural_short();
    } else {
if (ob->is_living()) {
    res += ob->in_room_desc();
} else {
    res +=  ob->a_short() + ob->get_attributes();
}
    }
    res += "\n";
    if( ob->inventory_visible() && !ob->query_hide_contents())
res += ob->inventory_recurse(depth);
}
    }
    return res == "" ? 0 : res;
}
private void main()
{

    string wielded = "";
    string worn = "";
    string carrying = "";
    string ex;
    object ob;
    int a = 0;
    int b = 0;
    int c = 0;
int d = 1;
    //Constrcut Wielded string
    //Construct Worn String
    //Construct Carrying String


    object * items = all_inventory(this_body());
    object * carrying_items = all_inventory(this_body());
    foreach(ob in items){


if(ob->test_flag(F_WIELDED)){
if(!a){
wielded += " Wielded : " + capitalize(ob->the_short());
a = 1;
                carrying_items -= ({ob});
            }else{
wielded += "\n\t    " + capitalize(ob->the_short());
                carrying_items -= ({ob});
        }
             
                   
}else if(ob->test_flag(F_WORN)){
if(!b){
worn += "\n    Worn : " + capitalize(ob->the_short());
b = 1;
                carrying_items -= ({ob});
            }else{
worn += "\n\t    " + capitalize(ob->the_short());
carrying_items -= ({ob});
        }
}else{


if(!c){
carrying += "\nCarrying : " + capitalize(ob->the_short());
c = 1;
            }else{
carrying += "\n\t   " + capitalize(ob->the_short());
        }

   


}

    }

    ex = wielded + worn + "\nCarrying :\n" + inv_list2(carrying_items);
    out("%^RED%^===============================%^RESET%^\n");
    out("\tINVENTORY\n");
    out(" You are carrying " + sizeof(items) + " items.\n");
    out("%^RED%^===============================%^RESET%^\n");
    if (ex)
out(ex);
    else
out("You are empty handed.\n");

    out("\n%^RED%^===============================%^RESET%^\n");
}


Thanks for your help guys.