diff options
author | ai4rei <ai4rei@54d463be-8e91-2dee-dedb-b68131a5f0ec> | 2010-12-21 23:23:06 +0000 |
---|---|---|
committer | ai4rei <ai4rei@54d463be-8e91-2dee-dedb-b68131a5f0ec> | 2010-12-21 23:23:06 +0000 |
commit | 537296dd156e22592606eedf539395288855639b (patch) | |
tree | 32b88c8962f5b02e3f243f9461b4cb699ba6f908 /src | |
parent | 8cd45c468ef250dde8ac68e9c9d4ad30da9cf351 (diff) | |
download | hercules-537296dd156e22592606eedf539395288855639b.tar.gz hercules-537296dd156e22592606eedf539395288855639b.tar.bz2 hercules-537296dd156e22592606eedf539395288855639b.tar.xz hercules-537296dd156e22592606eedf539395288855639b.zip |
* Added script_setarray_pc for setting temporary character array variables outside of script.c without requiring them to use script-interal code (add_str and reference_uid).
- Applied script_setarray_pc to assignment of dynamic shop arrays (related r5841).
git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@14613 54d463be-8e91-2dee-dedb-b68131a5f0ec
Diffstat (limited to 'src')
-rw-r--r-- | src/map/npc.c | 26 | ||||
-rw-r--r-- | src/map/script.c | 36 | ||||
-rw-r--r-- | src/map/script.h | 1 |
3 files changed, 54 insertions, 9 deletions
diff --git a/src/map/npc.c b/src/map/npc.c index d210bea94..ee3eb95ed 100644 --- a/src/map/npc.c +++ b/src/map/npc.c @@ -1142,19 +1142,24 @@ static int npc_buylist_sub(struct map_session_data* sd, int n, unsigned short* i { char npc_ev[NAME_LENGTH*2+3]; int i; - int regkey = add_str("@bought_nameid"); - int regkey2 = add_str("@bought_quantity"); + int key_nameid = 0; + int key_amount = 0; // discard old contents script_cleararray_pc(sd, "@bought_nameid", (void*)0); script_cleararray_pc(sd, "@bought_quantity", (void*)0); - snprintf(npc_ev, ARRAYLENGTH(npc_ev), "%s::OnBuyItem", nd->exname); - for(i=0;i<n;i++){ - pc_setreg(sd,regkey+(i<<24),(int)item_list[i*2+1]); - pc_setreg(sd,regkey2+(i<<24),(int)item_list[i*2]); + // save list of bought items + for( i = 0; i < n; i++ ) + { + script_setarray_pc(sd, "@bought_nameid", i, (void*)item_list[i*2+1], &key_nameid); + script_setarray_pc(sd, "@bought_quantity", i, (void*)item_list[i*2], &key_amount); } + + // invoke event + snprintf(npc_ev, ARRAYLENGTH(npc_ev), "%s::OnBuyItem", nd->exname); npc_event(sd, npc_ev, 0); + return 0; } /*========================================== @@ -1367,6 +1372,8 @@ int npc_selllist(struct map_session_data* sd, int n, unsigned short* item_list) { double z; int i,skill; + int key_nameid = 0; + int key_amount = 0; struct npc_data *nd; nullpo_retr(1, sd); @@ -1409,9 +1416,10 @@ int npc_selllist(struct map_session_data* sd, int n, unsigned short* item_list) if(log_config.enable_logs&0x20) //Logs items, Sold to NPC (S)hop [Lupus] log_pick_pc(sd, "S", nameid, -qty, &sd->status.inventory[idx]); - if(nd) { - pc_setreg(sd,add_str("@sold_nameid")+(i<<24),(int)sd->status.inventory[idx].nameid); - pc_setreg(sd,add_str("@sold_quantity")+(i<<24),qty); + if( nd ) + { + script_setarray_pc(sd, "@sold_nameid", i, (void*)sd->status.inventory[idx].nameid, &key_nameid); + script_setarray_pc(sd, "@sold_quantity", i, (void*)qty, &key_amount); } pc_delitem(sd,idx,qty,0,6); } diff --git a/src/map/script.c b/src/map/script.c index 6d0f8d81a..725ef0204 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -3579,6 +3579,42 @@ void script_cleararray_pc(struct map_session_data* sd, const char* varname, void } +/// sets a temporary character array variable element idx to given value +/// @param refcache Pointer to an int variable, which keeps a copy of the reference to varname and must be initialized to 0. Can be NULL if only one element is set. +void script_setarray_pc(struct map_session_data* sd, const char* varname, uint8 idx, void* value, int* refcache) +{ + int key; + + if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) ) + { + ShowError("script_setarray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id); + return; + } + + if( idx >= SCRIPT_MAX_ARRAYSIZE ) + { + ShowError("script_setarray_pc: Variable '%s' has invalid index '%d' (char_id=%d).\n", varname, (int)idx, sd->status.char_id); + return; + } + + key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname); + + if( is_string_variable(varname) ) + { + pc_setregstr(sd, reference_uid(key, idx), (const char*)value); + } + else + { + pc_setreg(sd, reference_uid(key, idx), (int)value); + } + + if( refcache ) + {// save to avoid repeated add_str calls + refcache[0] = key; + } +} + + /*========================================== * I—¹ *------------------------------------------*/ diff --git a/src/map/script.h b/src/map/script.h index d95ecbbb2..d6ede0e8b 100644 --- a/src/map/script.h +++ b/src/map/script.h @@ -168,6 +168,7 @@ struct DBMap* script_get_userfunc_db(void); void script_run_autobonus(const char *autobonus,int id, int pos); void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value); +void script_setarray_pc(struct map_session_data* sd, const char* varname, uint8 idx, void* value, int* refcache); int script_config_read(char *cfgName); int do_init_script(void); |