/*==========================================
* countitem(nameID)
* returns number of items in inventory
*==========================================
* countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
* returns number of items that meet the conditions
*------------------------------------------*/
BUILDIN_FUNC(countitem)
{
int nameid, i;
int count = 0;
struct item_data* id = NULL;
struct script_data* data;
TBL_PC* sd = script_rid2sd(st);
if (!sd) {
script_pushint(st,0);
return 0;
}
data = script_getdata(st,2);
get_val(st, data); // convert into value in case of a variable
if( data_isstring(data) ) // item name
id = itemdb_searchname(conv_str(st, data));
else // item id
id = itemdb_exists(conv_num(st, data));
if( id == NULL ) {
ShowError("buildin_countitem: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
script_pushint(st,0);
return 1;
}
nameid = id->nameid;
if(!strcmpi(script_getfuncname(st),"countitem")) {
for(i = 0; i < MAX_INVENTORY; i++)
if(sd->status.inventory[i].nameid == nameid)
count += sd->status.inventory[i].amount;
} else {
int iden, ref, attr, c1, c2, c3, c4;
iden = script_getnum(st,3);
ref = script_getnum(st,4);
attr = script_getnum(st,5);
c1 = (short)script_getnum(st,6);
c2 = (short)script_getnum(st,7);
c3 = (short)script_getnum(st,8);
c4 = (short)script_getnum(st,9);
for(i = 0; i < MAX_INVENTORY; i++)
if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
sd->status.inventory[i].card[3] == c4
)
count += sd->status.inventory[i].amount;
}
script_pushint(st,count);
return 0;
}