summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorai4rei <ai4rei@54d463be-8e91-2dee-dedb-b68131a5f0ec>2010-12-26 16:42:39 +0000
committerai4rei <ai4rei@54d463be-8e91-2dee-dedb-b68131a5f0ec>2010-12-26 16:42:39 +0000
commit2091c7772e8adcb05329e5cf816bba4e0c418daf (patch)
treefecb45fc6c21946e9428bd4e9ada846edbe6a4d4
parenta25b1a1f0fb28d4262cdbf22bcc7727b576a876a (diff)
downloadhercules-2091c7772e8adcb05329e5cf816bba4e0c418daf.tar.gz
hercules-2091c7772e8adcb05329e5cf816bba4e0c418daf.tar.bz2
hercules-2091c7772e8adcb05329e5cf816bba4e0c418daf.tar.xz
hercules-2091c7772e8adcb05329e5cf816bba4e0c418daf.zip
* Cleaned up script command checkweight.
- Replaced some code parts with calls to pc_checkadditem and pc_inventoryblank (follow up to r13735). - Fixed item id not getting checked for validity properly. * Moved MAX_AMOUNT check in pc_checkadditem to the top of the function, to prevent unnecessary inventory scan before it. git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@14629 54d463be-8e91-2dee-dedb-b68131a5f0ec
-rw-r--r--Changelog-Trunk.txt4
-rw-r--r--src/map/pc.c5
-rw-r--r--src/map/script.c94
3 files changed, 70 insertions, 33 deletions
diff --git a/Changelog-Trunk.txt b/Changelog-Trunk.txt
index 9ad319bfb..e49dce935 100644
--- a/Changelog-Trunk.txt
+++ b/Changelog-Trunk.txt
@@ -1,6 +1,10 @@
Date Added
2010/12/26
+ * Cleaned up script command checkweight. [Ai4rei]
+ - Replaced some code parts with calls to pc_checkadditem and pc_inventoryblank (follow up to r13735).
+ - Fixed item id not getting checked for validity properly.
+ * Moved MAX_AMOUNT check in pc_checkadditem to the top of the function, to prevent unnecessary inventory scan before it.
* Fixed opening other player's vending terminating currently running NPC conversation. Vending is now unavailable when talking to an NPC (bugreport:2208). [Ai4rei]
* Fixed vending not accepting 0 Zeny as sell price. The client asks for confirmation when you attempt to sell items at 0 Zeny (bugreport:2595, since r918 and r11344).
* Fixed items, that get dropped at 100% chance, being reported in @iteminfo as 'available in the shops only' (since/replaces r3482). [Ai4rei]
diff --git a/src/map/pc.c b/src/map/pc.c
index 45330f343..bb703a208 100644
--- a/src/map/pc.c
+++ b/src/map/pc.c
@@ -3203,6 +3203,9 @@ int pc_checkadditem(struct map_session_data *sd,int nameid,int amount)
nullpo_ret(sd);
+ if(amount > MAX_AMOUNT)
+ return ADDITEM_OVERAMOUNT;
+
if(!itemdb_isstackable(nameid))
return ADDITEM_NEW;
@@ -3214,8 +3217,6 @@ int pc_checkadditem(struct map_session_data *sd,int nameid,int amount)
}
}
- if(amount > MAX_AMOUNT)
- return ADDITEM_OVERAMOUNT;
return ADDITEM_NEW;
}
diff --git a/src/map/script.c b/src/map/script.c
index af723bf28..773f8d6c4 100644
--- a/src/map/script.c
+++ b/src/map/script.c
@@ -5385,48 +5385,80 @@ BUILDIN_FUNC(countitem2)
*------------------------------------------*/
BUILDIN_FUNC(checkweight)
{
- int nameid=0,amount,i;
- unsigned long weight;
- TBL_PC *sd;
- struct script_data *data;
+ int nameid, amount, slots;
+ unsigned int weight;
+ struct item_data* id = NULL;
+ struct map_session_data* sd;
+ struct script_data* data;
- sd = script_rid2sd(st);
- if( sd == NULL )
+ if( ( sd = script_rid2sd(st) ) == NULL )
+ {
return 0;
+ }
- data=script_getdata(st,2);
- get_val(st,data);
- if( data_isstring(data) ){
- const char *name=conv_str(st,data);
- struct item_data *item_data = itemdb_searchname(name);
- if( item_data )
- nameid=item_data->nameid;
- }else
- nameid=conv_num(st,data);
+ data = script_getdata(st,2);
+ get_val(st, data); // convert into value in case of a variable
- amount=script_getnum(st,3);
- if ( amount<=0 || nameid<500 ) { //if get wrong item ID or amount<=0, don't count weight of non existing items
- script_pushint(st,0);
- ShowError("buildin_checkweight: Wrong item ID or amount.\n");
+ 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_checkweight: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
+ return 1;
+ }
+
+ nameid = id->nameid;
+ amount = script_getnum(st,3);
+
+ if( amount < 1 )
+ {
+ ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
return 1;
}
weight = itemdb_weight(nameid)*amount;
- if( amount > MAX_AMOUNT || weight + sd->weight > sd->max_weight )
+
+ if( weight + sd->weight > sd->max_weight )
+ {// too heavy
script_pushint(st,0);
- else if( itemdb_isstackable(nameid) )
- {
- if( (i = pc_search_inventory(sd,nameid)) >= 0 )
- script_pushint(st,amount + sd->status.inventory[i].amount > MAX_AMOUNT ? 0 : 1);
- else
- script_pushint(st,pc_search_inventory(sd,0) >= 0 ? 1 : 0);
+ return 0;
}
- else
+
+ switch( pc_checkadditem(sd, nameid, amount) )
{
- for( i = 0; i < MAX_INVENTORY && amount; ++i )
- if( sd->status.inventory[i].nameid == 0 )
- amount--;
- script_pushint(st,amount ? 0 : 1);
+ case ADDITEM_EXIST:
+ // item is already in inventory, but there is still space for the requested amount
+ break;
+ case ADDITEM_NEW:
+ slots = pc_inventoryblank(sd);
+
+ if( itemdb_isstackable(nameid) )
+ {// stackable
+ if( slots < 1 )
+ {
+ script_pushint(st,0);
+ return 0;
+ }
+ }
+ else
+ {// non-stackable
+ if( slots < amount )
+ {
+ script_pushint(st,0);
+ return 0;
+ }
+ }
+ break;
+ case ADDITEM_OVERAMOUNT:
+ script_pushint(st,0);
+ return 0;
}
return 0;