Author Topic: Improved inventory listing  (Read 616 times)

Offline z993126

  • BFF
  • ***
  • Posts: 128
    • View Profile
Improved inventory listing
« on: July 20, 2012, 12:20:32 AM »
Improved inventory command that categorises things and now also notes, if you have liquid containers, what the contents are and how much is available.

Code: [Select]
/*  /lib/inventory.c
 *  from the Dead Souls LPC Library
 *  gives a person information about their inventory
 *  created by Descartes of Borg 950412
 *  2012-Jul-19 T. Cook added container capacity/contents information to listing
 *  2012-Feb-25 T. Cook switched to InvStyle property for mode
 *  2012-Feb-02 T. Cook switched to HelpInfo()
 *  2011-Dec-22 T. Cook began modification for categories
 */
#include <lib.h>
#include <rounds.h>
#include <daemons.h>
#include <message_class.h>

inherit LIB_DAEMON;

void eventInventory();

    mixed cmd(string args) {
        if( this_player()->GetInCombat() )
            this_player()->SetAttack(0, (: eventInventory :), ROUND_OTHER);
        else eventInventory();
        return 1;
    }

void eventInventory() {
mapping M_inv = ([ ]);
mixed *ma_temp, *ma_temp2, *ma_temp3;
string s_category, s_resource, s_ret, s_type, s_liquid;
string *sa_resources = ({}), *sa_temp = ({});
object o_item;
int i_resources, i_capacity;

//---- General inventory
foreach( o_item in all_inventory( this_player() ) ){
if( o_item->GetInvis( this_player() ) ){ continue; }
if( sizeof( o_item->GetItemType() ) ){  // the item has a type assigned
if( member_array( "user", keys( o_item->GetItemType() ) ) > -1 ){
s_category = o_item->GetItemType()["user"];
if( !M_inv[s_category] ){ M_inv[s_category] = ({}); }
M_inv[s_category] += ({ o_item });
}else{
s_category = o_item->GetItemType()["default"];
switch( s_category ){
case "weapon":
if( !M_inv["weapon"] ){ M_inv["weapon"] = ({}); }
M_inv["weapons"] += ({ o_item });
break;
case "apparel": case "light armor": case "heavy armor": case "clothing": case "jewelry":
if( !M_inv["apparel"] ){ M_inv["apparel"] = ({}); }
M_inv["apparel"] += ({ o_item });
break;
case "tool": case "key": case "gathering tool": case "basic crafting tool":
case "skilled crafting tool": case "scholar apparatus":
if( !M_inv["tool"] ){ M_inv["tool"] = ({}); }
M_inv["tool"] += ({ o_item });
break;
case "resource": case "vegetable": case "mineral": case "animal product":
if( !M_inv["resource"] ){ M_inv["resource"] = ({}); }
M_inv["resource"] += ({ o_item });
break;
case "literature": case "book": case "scroll":
if( !M_inv["literature"] ){ M_inv["literature"] = ({}); }
M_inv["literature"] += ({ o_item });
break;
case "edible": case "potion": case "poison": case "food": case "drink":
if( !M_inv["edible"] ){ M_inv["edible"] = ({}); }
M_inv["edible"] += ({ o_item });
break;
case "crafted": case "pottery": case "artwork":
if( !M_inv["crafted"] ){ M_inv["crafted"] = ({}); }
M_inv["crafted"] += ({ o_item });
break;
default:
if( !M_inv["miscellany"] ){ M_inv["miscellany"] = ({}); }
M_inv["miscellany"] += ({ o_item });
break;
}
}
}else{  // no item type; try to determine what the item is
if( inherits( LIB_WEAPON, o_item ) && o_item->GetWeaponType() != "blunt" ){
if( !M_inv["weapon"] ){ M_inv["weapon"] = ({}); }
M_inv["weapon"] += ({ o_item });
}else if( inherits( LIB_WEAR,   o_item ) ){
if( !M_inv["apparel"] ){ M_inv["apparel"] = ({}); }
M_inv["apparel"] += ({ o_item });
}else if( member_array( "key", o_item->GetId() ) > -1 ){
if( !M_inv["tool"] ){ M_inv["tool"] = ({}); }
M_inv["tool"] += ({ o_item });
}else if( sizeof( o_item->GetReads() ) > 0 ){
if( !M_inv["literature"] ){ M_inv["literature"] = ({}); }
M_inv["literature"] += ({ o_item });
}else if( inherits( LIB_MEAL,   o_item ) || inherits( LIB_DRINK, o_item ) ){
if( !M_inv["edible"] ){ M_inv["edible"] = ({}); }
M_inv["edible"] += ({ o_item });
}else{
if( !M_inv["miscellany"] ){ M_inv["miscellany"] = ({}); }
M_inv["miscellany"] += ({ o_item });
}
}
}

//---- Resources
/*  (this section has been commented out because my resources daemon is not included in the default DS lib)
sa_resources = this_player()->GetResources();
sa_resources = filter( sa_resources, (: this_player()->GetResource( $1 ) > 0 :) );
foreach( s_resource in sa_resources ){
if( !M_inv["resource"] ){ M_inv["resource"] = ([ ]); }
s_type = RESOURCES_D->Query( s_resource, "type" );
if( !M_inv["resource"][s_type] ){ M_inv["resource"][s_type] = ([ ]); }
M_inv["resource"][s_type][s_resource] = this_player()->GetResource( s_resource );
i_resources++;
}
*/
//---- Money
sa_resources = this_player()->GetCurrencies();
sa_resources = filter( sa_resources, (: this_player()->GetCurrency( $1 ) > 0 :) );
foreach( s_resource in sa_resources ){
if( !M_inv["money"] ){ M_inv["money"] = ([ ]); }
M_inv["money"][s_resource] = this_player()->GetCurrency( s_resource );
}

//---- Construct inventory display
switch( this_player()->GetProperty( "InvStyle" ) ){
case "debug":{
write( sprintf( "%%^BOLD%%^INVENTORY%%^RESET%%^\n%O", M_inv ) );
break;}
case "verbose":{
s_ret = "%^BOLD%^%^CYAN%^%^B_BLUE%^  Inventory  %^RESET%^\n";
if( sizeof( keys( M_inv ) ) == 0 ){
s_ret += "You aren't carrying anything.";
}else{
s_ret += "You are carrying ";
//-- non-resource/money (regular items)
foreach( s_category in keys( M_inv ) - ({ "resource", "money" }) ){
ma_temp = unique_array(
M_inv[s_category],
(: $1->GetEquippedShort() + ( $1->GetFlaskContents() ? "--" + $1->GetFlaskContents() : "" ) :)
);
ma_temp3 = ({});
foreach( ma_temp2 in ma_temp ){
s_liquid = ma_temp2[0]->GetFlaskContents();
i_capacity = 0;
if( s_liquid ){
foreach( o_item in ma_temp2 ){
i_capacity += s_liquid == "empty" ? o_item->GetMaxFlask() : o_item->GetFlaskUses();
}
}
ma_temp3 += ({ s_liquid ?
s_liquid == "empty" ?
consolidate( sizeof( ma_temp2 ), "empty " +
remove_article( ma_temp2[0]->GetEquippedShort() ) ) +
" which can hold a total of " +
consolidate( i_capacity, "unit" ) :
consolidate( sizeof( ma_temp2 ), remove_article( ma_temp2[0]->GetEquippedShort() ) ) +
" which hold a total of " + consolidate( i_capacity, "unit of " + s_liquid )
:
consolidate( sizeof( ma_temp2 ), ma_temp2[0]->GetEquippedShort() )
});
}
sa_temp += ({
( sizeof( ma_temp3 ) == 1 ? "this " + s_category : "these " + pluralize( s_category ) ) +
":  " + item_list( ma_temp3 )
});
}
//-- resource
if( sizeof( M_inv["resource"] ) ){
foreach( s_category in keys( M_inv["resource"] ) ){
ma_temp3 = ({});
foreach( s_resource in keys( M_inv["resource"][s_category] ) ){
ma_temp3 += ({ consolidate(
M_inv["resource"][s_category][s_resource],
RESOURCES_D->Query( s_resource, "shortdesc" )
) });
}
sa_temp += ({
( sizeof( ma_temp3 ) == 1 ? "this " + s_category : "these " + pluralize( s_category ) ) +
":  " + item_list( ma_temp3 )
});
}
}
//-- money
if( sizeof( M_inv["money"] ) ){
ma_temp3 = ({});
foreach( s_category in keys( M_inv["money"] ) ){
ma_temp3 += ({ cardinal( M_inv["money"][s_category] ) + " " + s_category });
}
sa_temp += ({ "this money:  " + item_list( ma_temp3 ) });
}
s_ret += item_list( sa_temp ) + ".";
}
write( s_ret );
break;}
default:{
if( sizeof( keys( M_inv ) ) == 0 ){
s_ret = "You are carrying nothing.";
}else{
if( sizeof( keys( M_inv ) ) == 1 && sizeof( M_inv[keys( M_inv )[0]] ) == 1 ){
s_ret = "%^BOLD%^%^CYAN%^You are carrying just this one item:%^RESET%^\n";
}else{
s_ret = "%^BOLD%^%^CYAN%^You are carrying the following items:%^RESET%^\n";
}
//-- non-resource/money (regular items)
foreach( s_category in keys( M_inv ) - ({ "resource", "money" }) ){
ma_temp = unique_array(
M_inv[s_category],
(: $1->GetEquippedShort() + ( $1->GetFlaskContents() ? "--" + $1->GetFlaskContents() : "" ) :)
);
s_ret += "  %^CYAN%^" +
capitalize( sizeof( ma_temp ) == 1 ? s_category : pluralize( s_category ) ) + ":%^RESET%^\n";
foreach( ma_temp2 in ma_temp ){
s_liquid = ma_temp2[0]->GetFlaskContents();
i_capacity = 0;
if( s_liquid ){
foreach( o_item in ma_temp2 ){
i_capacity += s_liquid == "empty" ? o_item->GetMaxFlask() : o_item->GetFlaskUses();
}
}
s_ret += "    " + consolidate( sizeof( ma_temp2 ),
remove_article( ma_temp2[0]->GetEquippedShort() ) ) +
( s_liquid ?
sprintf(
" %s%d unit%s total)",
s_liquid == "empty" ? "(empty; " : "of " + s_liquid + " (",
i_capacity,
i_capacity == 1 ? "" : "s"
) : ""
) + "\n";
}
}
//-- resources
if( sizeof( M_inv["resource"] ) ){
s_ret += "  %^CYAN%^Resource" + ( i_resources == 1 ? "" : "s" ) + ":%^RESET%^\n";
foreach( s_category in keys( M_inv["resource"] ) ){
s_ret += "    %^BOLD%^" + capitalize( s_category ) + ":%^RESET%^\n";
foreach( s_type in keys( M_inv["resource"][s_category] ) ){
if( member_array( s_type, RESOURCES_D->QueryResources() ) == -1 ){
s_ret += "      %^FLASH%^%^BOLD%^%^RED%^Something's wrong!%^RESET%^  ";
s_ret += "There is no resource called "  + s_type + "!";
continue;
}
s_ret += "      " + M_inv["resource"][s_category][s_type] + " ";
s_ret += M_inv["resource"][s_category][s_type] == 1 ? s_type : pluralize( s_type );
s_ret += "\n";
}
}
}
//-- money
if( sizeof( M_inv["money"] ) ){
s_ret += "  %^CYAN%^Money:%^RESET%^\n";
foreach( s_category in keys( M_inv["money"] ) ){
s_ret += "    " + M_inv["money"][s_category] + " " + s_category + "\n";
}
}
}
message( "look", s_ret, this_player() );
break;}
}
if( !this_player()->GetInvis() && !environment( this_player() )->GetProperty( "meeting room" ) ){
message(
MSG_ANNOYING,
this_player()->GetName() + " checks " + possessive( this_player() ) + " possessions.",
environment( this_player() ), ({ this_player() })
);
}
}

string GetHelp() {
return "syntax:  'inventory'\n\nLists all items you are currently carrying.  If you are fighting, this command " +
"will take up one round of combat.";
}
/* EOF */
« Last Edit: July 20, 2012, 12:26:31 AM by z993126 »

Offline Newt

  • Acquaintance
  • *
  • Posts: 14
    • View Profile
Re: Improved inventory listing
« Reply #1 on: July 20, 2012, 12:51:34 PM »
pics or it didn't happen

Offline z993126

  • BFF
  • ***
  • Posts: 128
    • View Profile
Re: Improved inventory listing
« Reply #2 on: July 24, 2012, 10:21:42 AM »
An image of this command's output is at http://i.imgur.com/bplnE.png  ...complete with an invalid resource caused by my removing and adding a differently-named version of the resource, which affects line 234 of the code above; to correct, replace line 234 with the following code:

Code: [Select]
if( !( !stringp( s_category ) || ( stringp( s_category ) && !sizeof( s_category ) ) ) ){
s_ret += "    %^BOLD%^" + capitalize( s_category ) + ":%^RESET%^\n";
}else{
s_ret += "    %^BOLD%^Invalid resources:%^RESET%^\n";
}

and the line about 'there is no resource called...' needs a \n at the end.

Offline detah

  • BFF
  • ***
  • Posts: 182
  • Ruler of 2D
    • View Profile
Re: Improved inventory listing
« Reply #3 on: July 24, 2012, 10:42:53 AM »
I think it will serve you better if you put those error results in a error() function. Then when a player discovers an error it will get logged in the error log and should also broadcast on the error channel.