diff options
author | Happy <markaizer@gmail.com> | 2014-08-21 04:50:46 +0800 |
---|---|---|
committer | Happy <markaizer@gmail.com> | 2014-08-21 04:50:46 +0800 |
commit | f52e1007fe08c67003c0bc4c78231904dd3fd5cc (patch) | |
tree | 99907d827264e501774e58ab4630e41fa7103c02 /src/map/script.c | |
parent | 2410110dece79b4598c12f1c953219f1d0d1904a (diff) | |
parent | 769b1d05aa5cfa8cddfe7d21b35d5c5e4da3bbd6 (diff) | |
download | hercules-f52e1007fe08c67003c0bc4c78231904dd3fd5cc.tar.gz hercules-f52e1007fe08c67003c0bc4c78231904dd3fd5cc.tar.bz2 hercules-f52e1007fe08c67003c0bc4c78231904dd3fd5cc.tar.xz hercules-f52e1007fe08c67003c0bc4c78231904dd3fd5cc.zip |
Merge pull request #1 from HerculesWS/master
Update from original
Diffstat (limited to 'src/map/script.c')
-rw-r--r-- | src/map/script.c | 14083 |
1 files changed, 7929 insertions, 6154 deletions
diff --git a/src/map/script.c b/src/map/script.c index 682faa42b..ecd12a3c1 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -2,250 +2,78 @@ // See the LICENSE file // Portions Copyright (c) Athena Dev Teams -//#define DEBUG_DISP -//#define DEBUG_DISASM -//#define DEBUG_RUN -//#define DEBUG_HASH -//#define DEBUG_DUMP_STACK +#define HERCULES_CORE -#include "../common/cbasetypes.h" -#include "../common/malloc.h" -#include "../common/md5calc.h" -#include "../common/nullpo.h" -#include "../common/random.h" -#include "../common/showmsg.h" -#include "../common/socket.h" // usage: getcharip -#include "../common/strlib.h" -#include "../common/timer.h" -#include "../common/utils.h" +#include "../config/core.h" // RENEWAL, RENEWAL_ASPD, RENEWAL_CAST, RENEWAL_DROP, RENEWAL_EDP, RENEWAL_EXP, RENEWAL_LVDMG, SCRIPT_CALLFUNC_CHECK, SECURE_NPCTIMEOUT, SECURE_NPCTIMEOUT_INTERVAL +#include "script.h" -#include "map.h" -#include "path.h" -#include "clif.h" +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +#include "atcommand.h" +#include "battle.h" +#include "battleground.h" +#include "chat.h" #include "chrif.h" +#include "clif.h" +#include "elemental.h" +#include "guild.h" +#include "homunculus.h" +#include "instance.h" +#include "intif.h" #include "itemdb.h" -#include "pc.h" -#include "status.h" -#include "storage.h" +#include "log.h" +#include "mail.h" +#include "map.h" +#include "mapreg.h" +#include "mercenary.h" #include "mob.h" #include "npc.h" +#include "party.h" +#include "path.h" +#include "pc.h" #include "pet.h" -#include "mapreg.h" -#include "homunculus.h" -#include "instance.h" -#include "mercenary.h" -#include "intif.h" +#include "pet.h" +#include "quest.h" #include "skill.h" #include "status.h" -#include "chat.h" -#include "battle.h" -#include "battleground.h" -#include "party.h" -#include "guild.h" -#include "atcommand.h" -#include "log.h" +#include "status.h" +#include "storage.h" #include "unit.h" -#include "pet.h" -#include "mail.h" -#include "script.h" -#include "quest.h" -#include "elemental.h" -#include "../config/core.h" +#include "../common/cbasetypes.h" +#include "../common/malloc.h" +#include "../common/md5calc.h" +#include "../common/mmo.h" // NEW_CARTS +#include "../common/nullpo.h" +#include "../common/random.h" +#include "../common/showmsg.h" +#include "../common/socket.h" // usage: getcharip +#include "../common/strlib.h" +#include "../common/sysinfo.h" +#include "../common/timer.h" +#include "../common/utils.h" -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <math.h> #ifndef WIN32 #include <sys/time.h> #endif -#include <time.h> -#include <setjmp.h> -#include <errno.h> - -#define FETCH(n, t) \ - if( script_hasdata(st,n) ) \ - (t)=script_getnum(st,n); - -/// Maximum amount of elements in script arrays -#define SCRIPT_MAX_ARRAYSIZE 128 -#define SCRIPT_BLOCK_SIZE 512 -enum { LABEL_NEXTLINE=1,LABEL_START }; - -/// temporary buffer for passing around compiled bytecode -/// @see add_scriptb, set_label, parse_script -static unsigned char* script_buf = NULL; -static int script_pos = 0, script_size = 0; - -static inline int GETVALUE(const unsigned char* buf, int i) -{ +static inline int GETVALUE(const unsigned char* buf, int i) { return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0)); } -static inline void SETVALUE(unsigned char* buf, int i, int n) -{ +static inline void SETVALUE(unsigned char* buf, int i, int n) { buf[i] = GetByte(n, 0); buf[i+1] = GetByte(n, 1); buf[i+2] = GetByte(n, 2); } -// Using a prime number for SCRIPT_HASH_SIZE should give better distributions -#define SCRIPT_HASH_SIZE 1021 -int str_hash[SCRIPT_HASH_SIZE]; -// Specifies which string hashing method to use -//#define SCRIPT_HASH_DJB2 -//#define SCRIPT_HASH_SDBM -#define SCRIPT_HASH_ELF - -static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos -static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code* -static int parse_options=0; -DBMap* script_get_label_db(void){ return scriptlabel_db; } -DBMap* script_get_userfunc_db(void){ return userfunc_db; } - -// important buildin function references for usage in scripts -static int buildin_set_ref = 0; -static int buildin_callsub_ref = 0; -static int buildin_callfunc_ref = 0; -static int buildin_getelementofarray_ref = 0; - -// Caches compiled autoscript item code. -// Note: This is not cleared when reloading itemdb. -static DBMap* autobonus_db=NULL; // char* script -> char* bytecode - -struct Script_Config script_config = { - 1, // warn_func_mismatch_argtypes - 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount - 0, INT_MAX, // input_min_value/input_max_value - "OnPCDieEvent", //die_event_name - "OnPCKillEvent", //kill_pc_event_name - "OnNPCKillEvent", //kill_mob_event_name - "OnPCLoginEvent", //login_event_name - "OnPCLogoutEvent", //logout_event_name - "OnPCLoadMapEvent", //loadmap_event_name - "OnPCBaseLvUpEvent", //baselvup_event_name - "OnPCJobLvUpEvent", //joblvup_event_name - "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves) - "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area) -}; - -static jmp_buf error_jump; -static char* error_msg; -static const char* error_pos; -static int error_report; // if the error should produce output - -// for advanced scripting support ( nested if, switch, while, for, do-while, function, etc ) -// [Eoe / jA 1080, 1081, 1094, 1164] -enum curly_type { - TYPE_NULL = 0, - TYPE_IF, - TYPE_SWITCH, - TYPE_WHILE, - TYPE_FOR, - TYPE_DO, - TYPE_USERFUNC, - TYPE_ARGLIST // function argument list -}; - -enum e_arglist -{ - ARGLIST_UNDEFINED = 0, - ARGLIST_NO_PAREN = 1, - ARGLIST_PAREN = 2, -}; - -static struct { - struct { - enum curly_type type; - int index; - int count; - int flag; - struct linkdb_node *case_label; - } curly[256]; // Information right parenthesis - int curly_count; // The number of right brackets - int index; // Number of the syntax used in the script -} syntax; +struct script_interface script_s; -const char* parse_curly_close(const char* p); -const char* parse_syntax_close(const char* p); -const char* parse_syntax_close_sub(const char* p,int* flag); -const char* parse_syntax(const char* p); -static int parse_syntax_for_flag = 0; - -extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus] -int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex] -int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0; -int potion_target=0; - - -c_op get_com(unsigned char *script,int *pos); -int get_num(unsigned char *script,int *pos); - -/*========================================== - * (Only those needed) local declaration prototype - *------------------------------------------*/ -const char* parse_subexpr(const char* p,int limit); -int run_func(struct script_state *st); - -enum { - MF_NOMEMO, //0 - MF_NOTELEPORT, - MF_NOSAVE, - MF_NOBRANCH, - MF_NOPENALTY, - MF_NOZENYPENALTY, - MF_PVP, - MF_PVP_NOPARTY, - MF_PVP_NOGUILD, - MF_GVG, - MF_GVG_NOPARTY, //10 - MF_NOTRADE, - MF_NOSKILL, - MF_NOWARP, - MF_PARTYLOCK, - MF_NOICEWALL, - MF_SNOW, - MF_FOG, - MF_SAKURA, - MF_LEAVES, - /* 21 - 22 free */ - MF_CLOUDS = 23, - MF_CLOUDS2, - MF_FIREWORKS, - MF_GVG_CASTLE, - MF_GVG_DUNGEON, - MF_NIGHTENABLED, - MF_NOBASEEXP, - MF_NOJOBEXP, //30 - MF_NOMOBLOOT, - MF_NOMVPLOOT, - MF_NORETURN, - MF_NOWARPTO, - MF_NIGHTMAREDROP, - MF_ZONE, - MF_NOCOMMAND, - MF_NODROP, - MF_JEXP, - MF_BEXP, //40 - MF_NOVENDING, - MF_LOADEVENT, - MF_NOCHAT, - MF_NOEXPPENALTY, - MF_GUILDLOCK, - MF_TOWN, - MF_AUTOTRADE, - MF_ALLOWKS, - MF_MONSTER_NOTELEPORT, - MF_PVP_NOCALCRANK, //50 - MF_BATTLEGROUND, - MF_RESET -}; - -const char* script_op2name(int op) -{ +const char* script_op2name(int op) { #define RETURN_OP_NAME(type) case type: return #type - switch( op ) - { + switch( op ) { RETURN_OP_NAME(C_NOP); RETURN_OP_NAME(C_POS); RETURN_OP_NAME(C_INT); @@ -260,6 +88,8 @@ const char* script_op2name(int op) RETURN_OP_NAME(C_USERFUNC); RETURN_OP_NAME(C_USERFUNC_POS); + RETURN_OP_NAME(C_REF); + // operators RETURN_OP_NAME(C_OP3); RETURN_OP_NAME(C_LOR); @@ -283,6 +113,14 @@ const char* script_op2name(int op) RETURN_OP_NAME(C_NOT); RETURN_OP_NAME(C_R_SHIFT); RETURN_OP_NAME(C_L_SHIFT); + RETURN_OP_NAME(C_ADD_POST); + RETURN_OP_NAME(C_SUB_POST); + RETURN_OP_NAME(C_ADD_PRE); + RETURN_OP_NAME(C_SUB_PRE); +#ifdef PCRE_SUPPORT + RETURN_OP_NAME(C_RE_EQ); + RETURN_OP_NAME(C_RE_NE); +#endif // PCRE_SUPPORT default: ShowDebug("script_op2name: unexpected op=%d\n", op); @@ -291,7 +129,7 @@ const char* script_op2name(int op) #undef RETURN_OP_NAME } -#ifdef DEBUG_DUMP_STACK +#ifdef SCRIPT_DEBUG_DUMP_STACK static void script_dump_stack(struct script_state* st) { int i; @@ -322,7 +160,7 @@ static void script_dump_stack(struct script_state* st) case C_RETINFO: { struct script_retinfo* ri = data->u.ri; - ShowMessage(" %p {var_function=%p, script=%p, pos=%d, nargs=%d, defsp=%d}\n", ri, ri->var_function, ri->script, ri->pos, ri->nargs, ri->defsp); + ShowMessage(" %p {scope.vars=%p, scope.arrays=%p, script=%p, pos=%d, nargs=%d, defsp=%d}\n", ri, ri->scope.vars, ri->scope.arrays, ri->script, ri->pos, ri->nargs, ri->defsp); } break; default: @@ -334,35 +172,34 @@ static void script_dump_stack(struct script_state* st) #endif /// Reports on the console the src of a script error. -static void script_reportsrc(struct script_state *st) -{ +void script_reportsrc(struct script_state *st) { struct block_list* bl; if( st->oid == 0 ) return; //Can't report source. - bl = iMap->id2bl(st->oid); + bl = map->id2bl(st->oid); if( bl == NULL ) return; switch( bl->type ) { case BL_NPC: if( bl->m >= 0 ) - ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y); + ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map->list[bl->m].name, bl->x, bl->y); else ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name); break; default: if( bl->m >= 0 ) - ShowDebug("Source (Non-NPC type %d): name %s at %s (%d,%d)\n", bl->type, status_get_name(bl), map[bl->m].name, bl->x, bl->y); + ShowDebug("Source (Non-NPC type %d): name %s at %s (%d,%d)\n", bl->type, status->get_name(bl), map->list[bl->m].name, bl->x, bl->y); else - ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl)); + ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status->get_name(bl)); break; } } /// Reports on the console information about the script data. -static void script_reportdata(struct script_data* data) +void script_reportdata(struct script_data* data) { if( data == NULL ) return; @@ -371,7 +208,7 @@ static void script_reportdata(struct script_data* data) ShowDebug("Data: nothing (nil)\n"); break; case C_INT:// number - ShowDebug("Data: number value=%d\n", data->u.num); + ShowDebug("Data: number value=%"PRId64"\n", data->u.num); break; case C_STR: case C_CONSTSTR:// string @@ -384,31 +221,28 @@ static void script_reportdata(struct script_data* data) case C_NAME:// reference if( reference_tovariable(data) ) {// variable const char* name = reference_getname(data); - if( not_array_variable(*name) ) - ShowDebug("Data: variable name='%s'\n", name); - else - ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data)); + ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data)); } else if( reference_toconstant(data) ) {// constant ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data)); } else if( reference_toparam(data) ) {// param ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data)); } else {// ??? - ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type)); - ShowDebug("Please report this!!! - script->str_data.type=%s\n", script_op2name(script->str_data[reference_getid(data)].type)); + ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script->op2name(data->type)); + ShowDebug("Please report this!!! - script->str_data.type=%s\n", script->op2name(script->str_data[reference_getid(data)].type)); } break; case C_POS:// label - ShowDebug("Data: label pos=%d\n", data->u.num); + ShowDebug("Data: label pos=%"PRId64"\n", data->u.num); break; default: - ShowDebug("Data: %s\n", script_op2name(data->type)); + ShowDebug("Data: %s\n", script->op2name(data->type)); break; } } /// Reports on the console information about the current built-in function. -static void script_reportfunc(struct script_state* st) +void script_reportfunc(struct script_state* st) { int i, params, id; struct script_data* data; @@ -430,16 +264,16 @@ static void script_reportfunc(struct script_state* st) if( params > 0 ) { - ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s"); + ShowDebug("Function: %s (%d parameter%s):\n", script->get_str(id), params, ( params == 1 ) ? "" : "s"); for( i = 2; i <= script_lastdata(st); i++ ) { - script_reportdata(script_getdata(st,i)); + script->reportdata(script_getdata(st,i)); } } else { - ShowDebug("Function: %s (no parameters)\n", get_str(id)); + ShowDebug("Function: %s (no parameters)\n", script->get_str(id)); } } @@ -447,35 +281,73 @@ static void script_reportfunc(struct script_state* st) /*========================================== * Output error message *------------------------------------------*/ -static void disp_error_message2(const char *mes,const char *pos,int report) -{ - error_msg = aStrdup(mes); - error_pos = pos; - error_report = report; - longjmp( error_jump, 1 ); +static void disp_error_message2(const char *mes,const char *pos,int report) analyzer_noreturn; +static void disp_error_message2(const char *mes,const char *pos,int report) { + script->error_msg = aStrdup(mes); + script->error_pos = pos; + script->error_report = report; + longjmp( script->error_jump, 1 ); +} +#define disp_error_message(mes,pos) (disp_error_message2((mes),(pos),1)) + +void disp_warning_message(const char *mes, const char *pos) { + script->warning(script->parser_current_src,script->parser_current_file,script->parser_current_line,mes,pos); } -#define disp_error_message(mes,pos) disp_error_message2(mes,pos,1) /// Checks event parameter validity -static void check_event(struct script_state *st, const char *evt) +void check_event(struct script_state *st, const char *evt) { if( evt && evt[0] && !stristr(evt, "::On") ) { ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt); - script_reportsrc(st); + script->reportsrc(st); } } /*========================================== * Hashes the input string *------------------------------------------*/ -static unsigned int calc_hash(const char* p) -{ +unsigned int calc_hash(const char* p) { unsigned int h; #if defined(SCRIPT_HASH_DJB2) h = 5381; while( *p ) // hash*33 + c + h = ( h << 5 ) + h + ((unsigned char)(*p++)); +#elif defined(SCRIPT_HASH_SDBM) + h = 0; + while( *p ) // hash*65599 + c + h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)(*p++)); +#elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash + h = 0; + while( *p ) { + unsigned int g; + h = ( h << 4 ) + ((unsigned char)(*p++)); + g = h & 0xF0000000; + if( g ) { + h ^= g >> 24; + h &= ~g; + } + } +#else // athena hash + h = 0; + while( *p ) + h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)(*p++); +#endif + + return h % SCRIPT_HASH_SIZE; +} + +/*========================================== + * Hashes the input string in a case insensitive way + *------------------------------------------*/ +unsigned int calc_hash_ci(const char* p) { + unsigned int h = 0; +#ifdef ENABLE_CASE_CHECK + +#if defined(SCRIPT_HASH_DJB2) + h = 5381; + while( *p ) // hash*33 + c h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++)); #elif defined(SCRIPT_HASH_SDBM) h = 0; @@ -483,12 +355,11 @@ static unsigned int calc_hash(const char* p) h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++)); #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash h = 0; - while( *p ){ + while( *p ) { unsigned int g; h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++)); g = h & 0xF0000000; - if( g ) - { + if( g ) { h ^= g >> 24; h &= ~g; } @@ -499,6 +370,7 @@ static unsigned int calc_hash(const char* p) h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++); #endif +#endif // ENABLE_CASE_CHECK return h % SCRIPT_HASH_SIZE; } @@ -508,39 +380,129 @@ static unsigned int calc_hash(const char* p) *------------------------------------------*/ /// Looks up string using the provided id. -const char* get_str(int id) +const char* script_get_str(int id) { Assert( id >= LABEL_START && id < script->str_size ); return script->str_buf+script->str_data[id].str; } /// Returns the uid of the string, or -1. -static int search_str(const char* p) +int script_search_str(const char* p) { int i; - for( i = str_hash[calc_hash(p)]; i != 0; i = script->str_data[i].next ) - if( strcasecmp(get_str(i),p) == 0 ) + for( i = script->str_hash[script->calc_hash(p)]; i != 0; i = script->str_data[i].next ) { + if( strcmp(script->get_str(i),p) == 0 ) { return i; + } + } return -1; } +void script_casecheck_clear_sub(struct casecheck_data *ccd) { +#ifdef ENABLE_CASE_CHECK + if (ccd->str_data) { + aFree(ccd->str_data); + ccd->str_data = NULL; + } + ccd->str_data_size = 0; + ccd->str_num = 1; + if (ccd->str_buf) { + aFree(ccd->str_buf); + ccd->str_buf = NULL; + } + ccd->str_pos = 0; + ccd->str_size = 0; + memset(ccd->str_hash, 0, sizeof(ccd->str_hash)); +#endif // ENABLE_CASE_CHECK +} + +void script_global_casecheck_clear(void) { + script_casecheck_clear_sub(&script->global_casecheck); +} + +void script_local_casecheck_clear(void) { + script_casecheck_clear_sub(&script->local_casecheck); +} + +const char *script_casecheck_add_str_sub(struct casecheck_data *ccd, const char *p) { +#ifdef ENABLE_CASE_CHECK + int len, i; + int h = script->calc_hash_ci(p); + if( ccd->str_hash[h] == 0 ) { //empty bucket, add new node here + ccd->str_hash[h] = ccd->str_num; + } else { + const char *s = NULL; + for( i = ccd->str_hash[h]; ; i = ccd->str_data[i].next ) { + Assert( i >= 0 && i < ccd->str_size ); + s = ccd->str_buf+ccd->str_data[i].str; + if( strcasecmp(s,p) == 0 ) { + return s; // string already in list + } + if( ccd->str_data[i].next == 0 ) + break; // reached the end + } + + // append node to end of list + ccd->str_data[i].next = ccd->str_num; + } + + // grow list if neccessary + if( ccd->str_num >= ccd->str_data_size ) { + ccd->str_data_size += 1280; + RECREATE(ccd->str_data,struct str_data_struct,ccd->str_data_size); + memset(ccd->str_data + (ccd->str_data_size - 1280), '\0', 1280); + } + + len=(int)strlen(p); + + // grow string buffer if neccessary + while( ccd->str_pos+len+1 >= ccd->str_size ) { + ccd->str_size += 10240; + RECREATE(ccd->str_buf,char,ccd->str_size); + memset(ccd->str_buf + (ccd->str_size - 10240), '\0', 10240); + } + + safestrncpy(ccd->str_buf+ccd->str_pos, p, len+1); + ccd->str_data[ccd->str_num].type = C_NOP; + ccd->str_data[ccd->str_num].str = ccd->str_pos; + ccd->str_data[ccd->str_num].val = 0; + ccd->str_data[ccd->str_num].next = 0; + ccd->str_data[ccd->str_num].func = NULL; + ccd->str_data[ccd->str_num].backpatch = -1; + ccd->str_data[ccd->str_num].label = -1; + ccd->str_pos += len+1; + + ccd->str_num++; +#endif // ENABLE_CASE_CHECK + return NULL; +} + +const char *script_global_casecheck_add_str(const char *p) { + return script_casecheck_add_str_sub(&script->global_casecheck, p); +} + +const char *script_local_casecheck_add_str(const char *p) { + return script_casecheck_add_str_sub(&script->local_casecheck, p); +} + /// Stores a copy of the string and returns its id. /// If an identical string is already present, returns its id instead. -int add_str(const char* p) +int script_add_str(const char* p) { - int i, h; - int len; + int i, len, h = script->calc_hash(p); +#ifdef ENABLE_CASE_CHECK + const char *existingentry = NULL; +#endif // ENABLE_CASE_CHECK - h = calc_hash(p); - - if( str_hash[h] == 0 ) {// empty bucket, add new node here - str_hash[h] = script->str_num; + if( script->str_hash[h] == 0 ) {// empty bucket, add new node here + script->str_hash[h] = script->str_num; } else {// scan for end of list, or occurence of identical string - for( i = str_hash[h]; ; i = script->str_data[i].next ) { - if( strcasecmp(get_str(i),p) == 0 ) + for( i = script->str_hash[h]; ; i = script->str_data[i].next ) { + if( strcmp(script->get_str(i),p) == 0 ) { return i; // string already in list + } if( script->str_data[i].next == 0 ) break; // reached the end } @@ -549,6 +511,25 @@ int add_str(const char* p) script->str_data[i].next = script->str_num; } +#ifdef ENABLE_CASE_CHECK + if( (strncmp(p, ".@", 2) == 0) ) // Local scope vars are checked separately to decrease false positives + existingentry = script->local_casecheck.add_str(p); + else { + existingentry = script->global_casecheck.add_str(p); + if( existingentry ) { + if( strcasecmp(p, "disguise") == 0 || strcasecmp(p, "Poison_Spore") == 0 + || strcasecmp(p, "PecoPeco_Egg") == 0 || strcasecmp(p, "Soccer_Ball") == 0 + || strcasecmp(p, "Horn") == 0 || strcasecmp(p, "Treasure_Box_") == 0 + || strcasecmp(p, "Lord_of_Death") == 0 + ) // Known duplicates, don't bother warning the user + existingentry = NULL; + } + } + if( existingentry ) { + DeprecationWarning2("script_add_str", p, existingentry, script->parser_current_file); // TODO + } +#endif // ENABLE_CASE_CHECK + // grow list if neccessary if( script->str_num >= script->str_data_size ) { script->str_data_size += 1280; @@ -568,6 +549,7 @@ int add_str(const char* p) safestrncpy(script->str_buf+script->str_pos, p, len+1); script->str_data[script->str_num].type = C_NOP; script->str_data[script->str_num].str = script->str_pos; + script->str_data[script->str_num].val = 0; script->str_data[script->str_num].next = 0; script->str_data[script->str_num].func = NULL; script->str_data[script->str_num].backpatch = -1; @@ -579,43 +561,43 @@ int add_str(const char* p) /// Appends 1 byte to the script buffer. -static void add_scriptb(int a) +void add_scriptb(int a) { - if( script_pos+1 >= script_size ) + if( script->pos+1 >= script->size ) { - script_size += SCRIPT_BLOCK_SIZE; - RECREATE(script_buf,unsigned char,script_size); + script->size += SCRIPT_BLOCK_SIZE; + RECREATE(script->buf,unsigned char,script->size); } - script_buf[script_pos++] = (uint8)(a); + script->buf[script->pos++] = (uint8)(a); } /// Appends a c_op value to the script buffer. /// The value is variable-length encoded into 8-bit blocks. /// The encoding scheme is ( 01?????? )* 00??????, LSB first. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries). -static void add_scriptc(int a) +void add_scriptc(int a) { while( a >= 0x40 ) { - add_scriptb((a&0x3f)|0x40); + script->addb((a&0x3f)|0x40); a = (a - 0x40) >> 6; } - add_scriptb(a); + script->addb(a); } /// Appends an integer value to the script buffer. /// The value is variable-length encoded into 8-bit blocks. /// The encoding scheme is ( 11?????? )* 10??????, LSB first. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries). -static void add_scripti(int a) +void add_scripti(int a) { while( a >= 0x40 ) { - add_scriptb((a&0x3f)|0xc0); + script->addb((a&0x3f)|0xc0); a = (a - 0x40) >> 6; } - add_scriptb(a|0x80); + script->addb(a|0x80); } /// Appends a script->str_data object (label/function/variable/integer) to the script buffer. @@ -623,37 +605,37 @@ static void add_scripti(int a) /// /// @param l The id of the script->str_data entry // Maximum up to 16M -static void add_scriptl(int l) +void add_scriptl(int l) { int backpatch = script->str_data[l].backpatch; - switch(script->str_data[l].type){ + switch(script->str_data[l].type) { case C_POS: case C_USERFUNC_POS: - add_scriptc(C_POS); - add_scriptb(script->str_data[l].label); - add_scriptb(script->str_data[l].label>>8); - add_scriptb(script->str_data[l].label>>16); + script->addc(C_POS); + script->addb(script->str_data[l].label); + script->addb(script->str_data[l].label>>8); + script->addb(script->str_data[l].label>>16); break; case C_NOP: case C_USERFUNC: // Embedded data backpatch there is a possibility of label - add_scriptc(C_NAME); - script->str_data[l].backpatch = script_pos; - add_scriptb(backpatch); - add_scriptb(backpatch>>8); - add_scriptb(backpatch>>16); + script->addc(C_NAME); + script->str_data[l].backpatch = script->pos; + script->addb(backpatch); + script->addb(backpatch>>8); + script->addb(backpatch>>16); break; case C_INT: - add_scripti(abs(script->str_data[l].val)); + script->addi(abs(script->str_data[l].val)); if( script->str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce) - add_scriptc(C_NEG); + script->addc(C_NEG); break; default: // assume C_NAME - add_scriptc(C_NAME); - add_scriptb(l); - add_scriptb(l>>8); - add_scriptb(l>>16); + script->addc(C_NAME); + script->addb(l); + script->addb(l>>8); + script->addb(l>>16); break; } } @@ -665,27 +647,27 @@ void set_label(int l,int pos, const char* script_pos) { int i,next; - if(script->str_data[l].type==C_INT || script->str_data[l].type==C_PARAM || script->str_data[l].type==C_FUNC) - { //Prevent overwriting constants values, parameters and built-in functions [Skotlex] + if(script->str_data[l].type==C_INT || script->str_data[l].type==C_PARAM || script->str_data[l].type==C_FUNC) { + //Prevent overwriting constants values, parameters and built-in functions [Skotlex] disp_error_message("set_label: invalid label name",script_pos); return; } - if(script->str_data[l].label!=-1){ + if(script->str_data[l].label!=-1) { disp_error_message("set_label: dup label ",script_pos); return; } script->str_data[l].type=(script->str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS); script->str_data[l].label=pos; - for(i=script->str_data[l].backpatch;i>=0 && i!=0x00ffffff;){ - next=GETVALUE(script_buf,i); - script_buf[i-1]=(script->str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS); - SETVALUE(script_buf,i,pos); + for(i=script->str_data[l].backpatch;i>=0 && i!=0x00ffffff;) { + next=GETVALUE(script->buf,i); + script->buf[i-1]=(script->str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS); + SETVALUE(script->buf,i,pos); i=next; } } /// Skips spaces and/or comments. -const char* skip_space(const char* p) +const char* script_skip_space(const char* p) { if( p == NULL ) return NULL; @@ -703,8 +685,10 @@ const char* skip_space(const char* p) p += 2; for(;;) { - if( *p == '\0' ) - return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p); + if( *p == '\0' ) { + script->disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p); + return p; + } if( *p == '*' && p[1] == '/' ) {// end of block comment p += 2; @@ -722,7 +706,7 @@ const char* skip_space(const char* p) /// Skips a word. /// A word consists of undercores and/or alphanumeric characters, /// and valid variable prefixes/postfixes. -static const char* skip_word(const char* p) { +const char* skip_word(const char* p) { // prefix switch( *p ) { case '@':// temporary char variable @@ -737,7 +721,7 @@ static const char* skip_word(const char* p) { p += ( p[1] == '@' ? 2 : 1 ); break; } - while( ISALNUM(*p) || *p == '_' ) + while( ISALNUM(*p) || *p == '_' || *p == '\'' ) ++p; // postfix @@ -748,26 +732,26 @@ static const char* skip_word(const char* p) { } /// Adds a word to script->str_data. /// @see skip_word -/// @see add_str -static int add_word(const char* p) { - int len; +/// @see script->add_str +int add_word(const char* p) { + size_t len; int i; // Check for a word - len = skip_word(p) - p; + len = script->skip_word(p) - p; if( len == 0 ) disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p); // Duplicate the word if( len+1 > script->word_size ) RECREATE(script->word_buf, char, (script->word_size = (len+1))); - + memcpy(script->word_buf, p, len); script->word_buf[len] = 0; // add the word - i = add_str(script->word_buf); - + i = script->add_str(script->word_buf); + return i; } @@ -779,109 +763,109 @@ const char* parse_callfunc(const char* p, int require_paren, int is_custom) { const char *p2; char *arg = NULL; + char null_arg = '\0'; int func; - func = add_word(p); - if( script->str_data[func].type == C_FUNC ){ - char argT = 0; + func = script->add_word(p); + if( script->str_data[func].type == C_FUNC ) { // buildin function - add_scriptl(func); - add_scriptc(C_ARG); + script->addl(func); + script->addc(C_ARG); arg = script->buildin[script->str_data[func].val]; - if( !arg ) arg = &argT; - } else if( script->str_data[func].type == C_USERFUNC || script->str_data[func].type == C_USERFUNC_POS ){ + if( !arg ) arg = &null_arg; // Use a dummy, null string + } else if( script->str_data[func].type == C_USERFUNC || script->str_data[func].type == C_USERFUNC_POS ) { // script defined function - add_scriptl(buildin_callsub_ref); - add_scriptc(C_ARG); - add_scriptl(func); - arg = script->buildin[script->str_data[buildin_callsub_ref].val]; + script->addl(script->buildin_callsub_ref); + script->addc(C_ARG); + script->addl(func); + arg = script->buildin[script->str_data[script->buildin_callsub_ref].val]; if( *arg == 0 ) disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p); if( *arg != '*' ) ++arg; // count func as argument } else { #ifdef SCRIPT_CALLFUNC_CHECK - const char* name = get_str(func); - if( !is_custom && strdb_get(userfunc_db, name) == NULL ) { + const char* name = script->get_str(func); + if( !is_custom && strdb_get(script->userfunc_db, name) == NULL ) { #endif disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p); #ifdef SCRIPT_CALLFUNC_CHECK } else {; - add_scriptl(buildin_callfunc_ref); - add_scriptc(C_ARG); - add_scriptc(C_STR); - while( *name ) add_scriptb(*name ++); - add_scriptb(0); - arg = script->buildin[script->str_data[buildin_callfunc_ref].val]; + script->addl(script->buildin_callfunc_ref); + script->addc(C_ARG); + script->addc(C_STR); + while( *name ) script->addb(*name ++); + script->addb(0); + arg = script->buildin[script->str_data[script->buildin_callfunc_ref].val]; if( *arg != '*' ) ++ arg; } #endif } - p = skip_word(p); - p = skip_space(p); - syntax.curly[syntax.curly_count].type = TYPE_ARGLIST; - syntax.curly[syntax.curly_count].count = 0; + p = script->skip_word(p); + p = script->skip_space(p); + script->syntax.curly[script->syntax.curly_count].type = TYPE_ARGLIST; + script->syntax.curly[script->syntax.curly_count].count = 0; if( *p == ';' ) {// <func name> ';' - syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN; - } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' ) + script->syntax.curly[script->syntax.curly_count].flag = ARGLIST_NO_PAREN; + } else if( *p == '(' && *(p2=script->skip_space(p+1)) == ')' ) {// <func name> '(' ')' - syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN; + script->syntax.curly[script->syntax.curly_count].flag = ARGLIST_PAREN; p = p2; /* } else if( 0 && require_paren && *p != '(' ) {// <func name> - syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN; + script->syntax.curly[script->syntax.curly_count].flag = ARGLIST_NO_PAREN; */ } else {// <func name> <arg list> - if( require_paren ){ + if( require_paren ) { if( *p != '(' ) disp_error_message("need '('",p); ++p; // skip '(' - syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN; - } else if( *p == '(' ){ - syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED; + script->syntax.curly[script->syntax.curly_count].flag = ARGLIST_PAREN; + } else if( *p == '(' ) { + script->syntax.curly[script->syntax.curly_count].flag = ARGLIST_UNDEFINED; } else { - syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN; + script->syntax.curly[script->syntax.curly_count].flag = ARGLIST_NO_PAREN; } - ++syntax.curly_count; + ++script->syntax.curly_count; while( *arg ) { - p2=parse_subexpr(p,-1); + p2=script->parse_subexpr(p,-1); if( p == p2 ) break; // not an argument if( *arg != '*' ) ++arg; // next argument - p=skip_space(p2); + p=script->skip_space(p2); if( *arg == 0 || *p != ',' ) break; // no more arguments ++p; // skip comma } - --syntax.curly_count; + --script->syntax.curly_count; } if( arg && *arg && *arg != '?' && *arg != '*' ) - disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum); - if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST ) + disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script->config.warn_func_mismatch_paramnum); + if( script->syntax.curly[script->syntax.curly_count].type != TYPE_ARGLIST ) disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p); - if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){ + if( script->syntax.curly[script->syntax.curly_count].flag == ARGLIST_PAREN ) { if( *p != ')' ) disp_error_message("parse_callfunc: expected ')' to close argument list",p); ++p; } - add_scriptc(C_FUNC); + script->addc(C_FUNC); return p; } /// Processes end of logical script line. /// @param first When true, only fix up scheduling data is initialized /// @param p Script position for error reporting in set_label -static void parse_nextline(bool first, const char* p) +void parse_nextline(bool first, const char* p) { if( !first ) { - add_scriptc(C_EOL); // mark end of line for stack cleanup - set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels + script->addc(C_EOL); // mark end of line for stack cleanup + script->set_label(LABEL_NEXTLINE, script->pos, p); // fix up '-' labels } // initialize data for new '-' label fix up scheduling @@ -890,6 +874,38 @@ static void parse_nextline(bool first, const char* p) script->str_data[LABEL_NEXTLINE].label = -1; } +/** + * Pushes a variable into stack, processing its array index if needed. + * @see parse_variable + */ +void parse_variable_sub_push(int word, const char *p2) { + const char* p3 = NULL; + + if( p2 ) { + // process the variable index + + // push the getelementofarray method into the stack + script->addl(script->buildin_getelementofarray_ref); + script->addc(C_ARG); + script->addl(word); + + // process the sub-expression for this assignment + p3 = script->parse_subexpr(p2 + 1, 1); + p3 = script->skip_space(p3); + + if( *p3 != ']' ) {// closing parenthesis is required for this script + disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3); + } + + // push the closing function stack operator onto the stack + script->addc(C_FUNC); + p3++; + } else { + // No array index, simply push the variable or value onto the stack + script->addl(word); + } +} + /// Parse a variable assignment using the direct equals operator /// @param p script position where the function should run from /// @return NULL if not a variable assignment, the new position otherwise @@ -899,21 +915,30 @@ const char* parse_variable(const char* p) { const char *p2 = NULL; const char *var = p; + if( ( p[0] == '+' && p[1] == '+' && (type = C_ADD_PRE) ) // pre ++ + || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_PRE) ) // pre -- + ) { + var = p = script->skip_space(&p[2]); + } + // skip the variable where applicable - p = skip_word(p); - p = skip_space(p); + p = script->skip_word(p); + p = script->skip_space(p); - if( p == NULL ) {// end of the line or invalid buffer + if( p == NULL ) { + // end of the line or invalid buffer return NULL; } - if( *p == '[' ) {// array variable so process the array as appropriate + if( *p == '[' ) { + // array variable so process the array as appropriate for( p2 = p, i = 0, j = 1; p; ++ i ) { if( *p ++ == ']' && --(j) == 0 ) break; if( *p == '[' ) ++ j; } - if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining + if( !(p = script->skip_space(p)) ) { + // end of line or invalid characters remaining disp_error_message("Missing right expression or closing bracket for variable.", p); } } @@ -928,9 +953,8 @@ const char* parse_variable(const char* p) { || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *= || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /= || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %= - || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~= - || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_PP) ) // ++ - || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_PP) ) // -- + || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_POST) ) // post ++ + || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_POST) ) // post -- || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<= || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>= ) ) @@ -939,188 +963,229 @@ const char* parse_variable(const char* p) { } switch( type ) { - case C_EQ: {// incremental modifier - p = skip_space( &p[1] ); - } - break; + case C_ADD_PRE: // pre ++ + case C_SUB_PRE: // pre -- + // (nothing more to skip) + break; - case C_L_SHIFT: - case C_R_SHIFT: {// left or right shift modifier - p = skip_space( &p[3] ); - } - break; + case C_EQ: // = + p = script->skip_space( &p[1] ); + break; - default: {// normal incremental command - p = skip_space( &p[2] ); - } + case C_L_SHIFT: // <<= + case C_R_SHIFT: // >>= + p = script->skip_space( &p[3] ); + break; + + default: // everything else + p = script->skip_space( &p[2] ); } - if( p == NULL ) {// end of line or invalid buffer + if( p == NULL ) { + // end of line or invalid buffer return NULL; } // push the set function onto the stack - add_scriptl(buildin_set_ref); - add_scriptc(C_ARG); + script->addl(script->buildin_set_ref); + script->addc(C_ARG); // always append parenthesis to avoid errors - syntax.curly[syntax.curly_count].type = TYPE_ARGLIST; - syntax.curly[syntax.curly_count].count = 0; - syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN; + script->syntax.curly[script->syntax.curly_count].type = TYPE_ARGLIST; + script->syntax.curly[script->syntax.curly_count].count = 0; + script->syntax.curly[script->syntax.curly_count].flag = ARGLIST_PAREN; // increment the total curly count for the position in the script - ++ syntax.curly_count; + ++script->syntax.curly_count; // parse the variable currently being modified - word = add_word(var); + word = script->add_word(var); - if( script->str_data[word].type == C_FUNC || script->str_data[word].type == C_USERFUNC || script->str_data[word].type == C_USERFUNC_POS ) - {// cannot assign a variable which exists as a function or label + if( script->str_data[word].type == C_FUNC + || script->str_data[word].type == C_USERFUNC + || script->str_data[word].type == C_USERFUNC_POS + ) { + // cannot assign a variable which exists as a function or label disp_error_message("Cannot modify a variable which has the same name as a function or label.", p); } - if( p2 ) {// process the variable index - const char* p3 = NULL; - - // push the getelementofarray method into the stack - add_scriptl(buildin_getelementofarray_ref); - add_scriptc(C_ARG); - add_scriptl(word); - - // process the sub-expression for this assignment - p3 = parse_subexpr(p2 + 1, 1); - p3 = skip_space(p3); + parse_variable_sub_push(word, p2); // Push variable onto the stack - if( *p3 != ']' ) {// closing parenthesis is required for this script - disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3); - } - - // push the closing function stack operator onto the stack - add_scriptc(C_FUNC); - p3 ++; - } else {// simply push the variable or value onto the stack - add_scriptl(word); + if( type != C_EQ ) { + parse_variable_sub_push(word, p2); // Push variable onto the stack once again (first argument of setr) } - if( type != C_EQ ) - add_scriptc(C_REF); + if( type == C_ADD_POST || type == C_SUB_POST ) { // post ++ / -- + script->addi(1); + script->addc(type == C_ADD_POST ? C_ADD : C_SUB); - if( type == C_ADD_PP || type == C_SUB_PP ) {// incremental operator for the method - add_scripti(1); - add_scriptc(type == C_ADD_PP ? C_ADD : C_SUB); - } else {// process the value as an expression - p = parse_subexpr(p, -1); + parse_variable_sub_push(word, p2); // Push variable onto the stack (third argument of setr) + } else if( type == C_ADD_PRE || type == C_SUB_PRE ) { // pre ++ / -- + script->addi(1); + script->addc(type == C_ADD_PRE ? C_ADD : C_SUB); + } else { + // process the value as an expression + p = script->parse_subexpr(p, -1); - if( type != C_EQ ) - {// push the type of modifier onto the stack - add_scriptc(type); + if( type != C_EQ ) { + // push the type of modifier onto the stack + script->addc(type); } } // decrement the curly count for the position within the script - -- syntax.curly_count; + --script->syntax.curly_count; // close the script by appending the function operator - add_scriptc(C_FUNC); + script->addc(C_FUNC); // push the buffer from the method return p; } +/* + * Checks whether the gives string is a number literal + * + * Mainly necessary to differentiate between number literals and NPC name + * constants, since several of those start with a digit. + * + * All this does is to check if the string begins with an optional + or - sign, + * followed by a hexadecimal or decimal number literal literal and is NOT + * followed by a underscore or letter. + * + * @param p Pointer to the string to check + * @return Whether the string is a number literal + */ +bool is_number(const char *p) { + const char *np; + if (!p) + return false; + if (*p == '-' || *p == '+') + p++; + np = p; + if (*p == '0' && p[1] == 'x') { + p+=2; + np = p; + // Hexadecimal + while (ISXDIGIT(*np)) + np++; + } else { + // Decimal + while (ISDIGIT(*np)) + np++; + } + if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _ + return true; + return false; +} + /*========================================== * Analysis section *------------------------------------------*/ -const char* parse_simpleexpr(const char *p) -{ +const char* parse_simpleexpr(const char *p) { int i; - p=skip_space(p); + p=script->skip_space(p); if(*p==';' || *p==',') disp_error_message("parse_simpleexpr: unexpected end of expression",p); - if(*p=='('){ - if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST ) - ++syntax.curly[i].count; - p=parse_subexpr(p+1,-1); - p=skip_space(p); - if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST && - syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0 - ){ - if( *p == ',' ){ - syntax.curly[i].flag = ARGLIST_PAREN; + if(*p=='(') { + if( (i=script->syntax.curly_count-1) >= 0 && script->syntax.curly[i].type == TYPE_ARGLIST ) + ++script->syntax.curly[i].count; + p=script->parse_subexpr(p+1,-1); + p=script->skip_space(p); + if( (i=script->syntax.curly_count-1) >= 0 && script->syntax.curly[i].type == TYPE_ARGLIST + && script->syntax.curly[i].flag == ARGLIST_UNDEFINED && --script->syntax.curly[i].count == 0 + ) { + if( *p == ',' ) { + script->syntax.curly[i].flag = ARGLIST_PAREN; return p; - } else - syntax.curly[i].flag = ARGLIST_NO_PAREN; + } else { + script->syntax.curly[i].flag = ARGLIST_NO_PAREN; + } } if( *p != ')' ) disp_error_message("parse_simpleexpr: unmatched ')'",p); ++p; - } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){ + } else if(is_number(p)) { char *np; - while(*p == '0' && ISDIGIT(p[1])) p++; - i=strtoul(p,&np,0); - add_scripti(i); + long long lli; + while(*p == '0' && ISDIGIT(p[1])) p++; // Skip leading zeros, we don't support octal literals + lli=strtoll(p,&np,0); + if( lli < INT_MIN ) { + lli = INT_MIN; + script->disp_warning_message("parse_simpleexpr: underflow detected, capping value to INT_MIN",p); + } else if( lli > INT_MAX ) { + lli = INT_MAX; + script->disp_warning_message("parse_simpleexpr: overflow detected, capping value to INT_MAX",p); + } + script->addi((int)lli); // Cast is safe, as it's already been checked for overflows p=np; - } else if(*p=='"'){ - add_scriptc(C_STR); - p++; - while( *p && *p != '"' ){ - if( (unsigned char)p[-1] <= 0x7e && *p == '\\' ) { - char buf[8]; - size_t len = sv->skip_escaped_c(p) - p; - size_t n = sv->unescape_c(buf, p, len); - if( n != 1 ) - ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf); - p += len; - add_scriptb(*buf); - continue; - } else if( *p == '\n' ) - disp_error_message("parse_simpleexpr: unexpected newline @ string",p); - add_scriptb(*p++); - } - if(!*p) - disp_error_message("parse_simpleexpr: unexpected end of file @ string",p); - add_scriptb(0); - p++; //'"' + } else if(*p=='"') { + script->addc(C_STR); + do { + p++; + while( *p && *p != '"' ) { + if( (unsigned char)p[-1] <= 0x7e && *p == '\\' ) { + char buf[8]; + size_t len = sv->skip_escaped_c(p) - p; + size_t n = sv->unescape_c(buf, p, len); + if( n != 1 ) + ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf); + p += len; + script->addb(*buf); + continue; + } else if( *p == '\n' ) { + disp_error_message("parse_simpleexpr: unexpected newline @ string",p); + } + script->addb(*p++); + } + if(!*p) + disp_error_message("parse_simpleexpr: unexpected end of file @ string",p); + p++; //'"' + p = script->skip_space(p); + } while( *p && *p == '"' ); + script->addb(0); } else { int l; const char* pv; // label , register , function etc - if(skip_word(p)==p) + if(script->skip_word(p)==p) disp_error_message("parse_simpleexpr: unexpected character",p); - l=add_word(p); - if( script->str_data[l].type == C_FUNC || script->str_data[l].type == C_USERFUNC || script->str_data[l].type == C_USERFUNC_POS) - return parse_callfunc(p,1,0); + l=script->add_word(p); + if( script->str_data[l].type == C_FUNC || script->str_data[l].type == C_USERFUNC || script->str_data[l].type == C_USERFUNC_POS) { + return script->parse_callfunc(p,1,0); #ifdef SCRIPT_CALLFUNC_CHECK - else { - const char* name = get_str(l); - if( strdb_get(userfunc_db,name) != NULL ) { - return parse_callfunc(p,1,1); + } else { + const char* name = script->get_str(l); + if( strdb_get(script->userfunc_db,name) != NULL ) { + return script->parse_callfunc(p,1,1); } - } #endif + } - if( (pv = parse_variable(p)) ) - {// successfully processed a variable assignment + if( (pv = script->parse_variable(p)) ) { + // successfully processed a variable assignment return pv; } - p=skip_word(p); - if( *p == '[' ){ + p=script->skip_word(p); + if( *p == '[' ) { // array(name[i] => getelementofarray(name,i) ) - add_scriptl(buildin_getelementofarray_ref); - add_scriptc(C_ARG); - add_scriptl(l); + script->addl(script->buildin_getelementofarray_ref); + script->addc(C_ARG); + script->addl(l); - p=parse_subexpr(p+1,-1); - p=skip_space(p); + p=script->parse_subexpr(p+1,-1); + p=script->skip_space(p); if( *p != ']' ) disp_error_message("parse_simpleexpr: unmatched ']'",p); ++p; - add_scriptc(C_FUNC); - }else - add_scriptl(l); + script->addc(C_FUNC); + } else { + script->addl(l); + } } @@ -1130,60 +1195,67 @@ const char* parse_simpleexpr(const char *p) /*========================================== * Analysis of the expression *------------------------------------------*/ -const char* parse_subexpr(const char* p,int limit) -{ +const char* script_parse_subexpr(const char* p,int limit) { int op,opl,len; const char* tmpp; - p=skip_space(p); + p=script->skip_space(p); - if( *p == '-' ){ - tmpp = skip_space(p+1); - if( *tmpp == ';' || *tmpp == ',' ){ - add_scriptl(LABEL_NEXTLINE); + if( *p == '-' ) { + tmpp = script->skip_space(p+1); + if( *tmpp == ';' || *tmpp == ',' ) { + script->addl(LABEL_NEXTLINE); p++; return p; } } - if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){ - p=parse_subexpr(p+1,10); - add_scriptc(op); - } else - p=parse_simpleexpr(p); - p=skip_space(p); + if( (p[0]=='+' && p[1]=='+') /* C_ADD_PRE */ || (p[0]=='-'&&p[1]=='-') /* C_SUB_PRE */ ) { // Pre ++ -- operators + p=script->parse_variable(p); + } else if( (op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~') ) { // Unary - ! ~ operators + p=script->parse_subexpr(p+1,11); + script->addc(op); + } else { + p=script->parse_simpleexpr(p); + } + p=script->skip_space(p); while(( - (op=C_OP3,opl=0,len=1,*p=='?') || - (op=C_ADD,opl=8,len=1,*p=='+') || - (op=C_SUB,opl=8,len=1,*p=='-') || - (op=C_MUL,opl=9,len=1,*p=='*') || - (op=C_DIV,opl=9,len=1,*p=='/') || - (op=C_MOD,opl=9,len=1,*p=='%') || - (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') || - (op=C_AND,opl=6,len=1,*p=='&') || - (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') || - (op=C_OR,opl=5,len=1,*p=='|') || - (op=C_XOR,opl=4,len=1,*p=='^') || - (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') || - (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') || - (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') || - (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') || - (op=C_GT,opl=3,len=1,*p=='>') || - (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') || - (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') || - (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){ + (op=C_OP3, opl=0, len=1,*p=='?') // ?: + || (op=C_ADD, opl=9, len=1,*p=='+') // + + || (op=C_SUB, opl=9, len=1,*p=='-') // - + || (op=C_MUL, opl=10,len=1,*p=='*') // * + || (op=C_DIV, opl=10,len=1,*p=='/') // / + || (op=C_MOD, opl=10,len=1,*p=='%') // % + || (op=C_LAND, opl=2, len=2,*p=='&' && p[1]=='&') // && + || (op=C_AND, opl=5, len=1,*p=='&') // & + || (op=C_LOR, opl=1, len=2,*p=='|' && p[1]=='|') // || + || (op=C_OR, opl=3, len=1,*p=='|') // | + || (op=C_XOR, opl=4, len=1,*p=='^') // ^ + || (op=C_EQ, opl=6, len=2,*p=='=' && p[1]=='=') // == + || (op=C_NE, opl=6, len=2,*p=='!' && p[1]=='=') // != +#ifdef PCRE_SUPPORT + || (op=C_RE_EQ, opl=6, len=2,*p=='~' && p[1]=='=') // ~= + || (op=C_RE_NE, opl=6, len=2,*p=='~' && p[1]=='!') // ~! +#endif // PCRE_SUPPORT + || (op=C_R_SHIFT,opl=8, len=2,*p=='>' && p[1]=='>') // >> + || (op=C_GE, opl=7, len=2,*p=='>' && p[1]=='=') // >= + || (op=C_GT, opl=7, len=1,*p=='>') // > + || (op=C_L_SHIFT,opl=8, len=2,*p=='<' && p[1]=='<') // << + || (op=C_LE, opl=7, len=2,*p=='<' && p[1]=='=') // <= + || (op=C_LT, opl=7, len=1,*p=='<') // < + ) && opl>limit) { p+=len; if(op == C_OP3) { - p=parse_subexpr(p,-1); - p=skip_space(p); + p=script->parse_subexpr(p,-1); + p=script->skip_space(p); if( *(p++) != ':') disp_error_message("parse_subexpr: need ':'", p-1); - p=parse_subexpr(p,-1); + p=script->parse_subexpr(p,-1); } else { - p=parse_subexpr(p,opl); + p=script->parse_subexpr(p,opl); } - add_scriptc(op); - p=skip_space(p); + script->addc(op); + p=script->skip_space(p); } return p; /* return first untreated operator */ @@ -1194,12 +1266,12 @@ const char* parse_subexpr(const char* p,int limit) *------------------------------------------*/ const char* parse_expr(const char *p) { - switch(*p){ + switch(*p) { case ')': case ';': case ':': case '[': case ']': case '}': disp_error_message("parse_expr: unexpected char",p); } - p=parse_subexpr(p,-1); + p=script->parse_subexpr(p,-1); return p; } @@ -1210,43 +1282,43 @@ const char* parse_line(const char* p) { const char* p2; - p=skip_space(p); + p=script->skip_space(p); if(*p==';') { //Close decision for if(); for(); while(); - p = parse_syntax_close(p + 1); + p = script->parse_syntax_close(p + 1); return p; } - if(*p==')' && parse_syntax_for_flag) + if(*p==')' && script->parse_syntax_for_flag) return p+1; - p = skip_space(p); + p = script->skip_space(p); if(p[0] == '{') { - syntax.curly[syntax.curly_count].type = TYPE_NULL; - syntax.curly[syntax.curly_count].count = -1; - syntax.curly[syntax.curly_count].index = -1; - syntax.curly_count++; + script->syntax.curly[script->syntax.curly_count].type = TYPE_NULL; + script->syntax.curly[script->syntax.curly_count].count = -1; + script->syntax.curly[script->syntax.curly_count].index = -1; + script->syntax.curly_count++; return p + 1; } else if(p[0] == '}') { - return parse_curly_close(p); + return script->parse_curly_close(p); } // Syntax-related processing - p2 = parse_syntax(p); + p2 = script->parse_syntax(p); if(p2 != NULL) return p2; // attempt to process a variable assignment - p2 = parse_variable(p); + p2 = script->parse_variable(p); if( p2 != NULL ) {// variable assignment processed so leave the method - return parse_syntax_close(p2 + 1); + return script->parse_syntax_close(p2 + 1); } - p = parse_callfunc(p,0,0); - p = skip_space(p); + p = script->parse_callfunc(p,0,0); + p = script->skip_space(p); - if(parse_syntax_for_flag) { + if(script->parse_syntax_for_flag) { if( *p != ')' ) disp_error_message("parse_line: need ')'",p); } else { @@ -1255,7 +1327,7 @@ const char* parse_line(const char* p) } //Binding decision for if(), for(), while() - p = parse_syntax_close(p+1); + p = script->parse_syntax_close(p+1); return p; } @@ -1263,52 +1335,52 @@ const char* parse_line(const char* p) // { ... } Closing process const char* parse_curly_close(const char* p) { - if(syntax.curly_count <= 0) { + if(script->syntax.curly_count <= 0) { disp_error_message("parse_curly_close: unexpected string",p); return p + 1; - } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) { - syntax.curly_count--; + } else if(script->syntax.curly[script->syntax.curly_count-1].type == TYPE_NULL) { + script->syntax.curly_count--; //Close decision if, for , while - p = parse_syntax_close(p + 1); + p = script->parse_syntax_close(p + 1); return p; - } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) { + } else if(script->syntax.curly[script->syntax.curly_count-1].type == TYPE_SWITCH) { //Closing switch() - int pos = syntax.curly_count-1; + int pos = script->syntax.curly_count-1; char label[256]; int l; // Remove temporary variables - sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index); - syntax.curly[syntax.curly_count++].type = TYPE_NULL; - parse_line(label); - syntax.curly_count--; + sprintf(label,"set $@__SW%x_VAL,0;",script->syntax.curly[pos].index); + script->syntax.curly[script->syntax.curly_count++].type = TYPE_NULL; + script->parse_line(label); + script->syntax.curly_count--; // Go to the end pointer unconditionally - sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index); - syntax.curly[syntax.curly_count++].type = TYPE_NULL; - parse_line(label); - syntax.curly_count--; + sprintf(label,"goto __SW%x_FIN;",script->syntax.curly[pos].index); + script->syntax.curly[script->syntax.curly_count++].type = TYPE_NULL; + script->parse_line(label); + script->syntax.curly_count--; // You are here labeled - sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count); - l=add_str(label); - set_label(l,script_pos, p); + sprintf(label,"__SW%x_%x",script->syntax.curly[pos].index,script->syntax.curly[pos].count); + l=script->add_str(label); + script->set_label(l,script->pos, p); - if(syntax.curly[pos].flag) { + if(script->syntax.curly[pos].flag) { //Exists default - sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index); - syntax.curly[syntax.curly_count++].type = TYPE_NULL; - parse_line(label); - syntax.curly_count--; + sprintf(label,"goto __SW%x_DEF;",script->syntax.curly[pos].index); + script->syntax.curly[script->syntax.curly_count++].type = TYPE_NULL; + script->parse_line(label); + script->syntax.curly_count--; } // Label end - sprintf(label,"__SW%x_FIN",syntax.curly[pos].index); - l=add_str(label); - set_label(l,script_pos, p); - linkdb_final(&syntax.curly[pos].case_label); // free the list of case label - syntax.curly_count--; + sprintf(label,"__SW%x_FIN",script->syntax.curly[pos].index); + l=script->add_str(label); + script->set_label(l,script->pos, p); + linkdb_final(&script->syntax.curly[pos].case_label); // free the list of case label + script->syntax.curly_count--; //Closing decision if, for , while - p = parse_syntax_close(p + 1); + p = script->parse_syntax_close(p + 1); return p; } else { disp_error_message("parse_curly_close: unexpected string",p); @@ -1321,27 +1393,27 @@ const char* parse_curly_close(const char* p) // if, switch, while ? will handle this internally. const char* parse_syntax(const char* p) { - const char *p2 = skip_word(p); + const char *p2 = script->skip_word(p); switch(*p) { case 'B': case 'b': - if(p2 - p == 5 && !strncasecmp(p,"break",5)) { + if( p2 - p == 5 && strncmp(p,"break",5) == 0 ) { // break Processing char label[256]; - int pos = syntax.curly_count - 1; + int pos = script->syntax.curly_count - 1; while(pos >= 0) { - if(syntax.curly[pos].type == TYPE_DO) { - sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index); + if(script->syntax.curly[pos].type == TYPE_DO) { + sprintf(label,"goto __DO%x_FIN;",script->syntax.curly[pos].index); break; - } else if(syntax.curly[pos].type == TYPE_FOR) { - sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index); + } else if(script->syntax.curly[pos].type == TYPE_FOR) { + sprintf(label,"goto __FR%x_FIN;",script->syntax.curly[pos].index); break; - } else if(syntax.curly[pos].type == TYPE_WHILE) { - sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index); + } else if(script->syntax.curly[pos].type == TYPE_WHILE) { + sprintf(label,"goto __WL%x_FIN;",script->syntax.curly[pos].index); break; - } else if(syntax.curly[pos].type == TYPE_SWITCH) { - sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index); + } else if(script->syntax.curly[pos].type == TYPE_SWITCH) { + sprintf(label,"goto __SW%x_FIN;",script->syntax.curly[pos].index); break; } pos--; @@ -1349,107 +1421,113 @@ const char* parse_syntax(const char* p) if(pos < 0) { disp_error_message("parse_syntax: unexpected 'break'",p); } else { - syntax.curly[syntax.curly_count++].type = TYPE_NULL; - parse_line(label); - syntax.curly_count--; + script->syntax.curly[script->syntax.curly_count++].type = TYPE_NULL; + script->parse_line(label); + script->syntax.curly_count--; } - p = skip_space(p2); + p = script->skip_space(p2); if(*p != ';') disp_error_message("parse_syntax: need ';'",p); // Closing decision if, for , while - p = parse_syntax_close(p + 1); + p = script->parse_syntax_close(p + 1); return p; +#ifdef ENABLE_CASE_CHECK + } else if( p2 - p == 5 && strncasecmp(p, "break", 5) == 0 ) { + disp_deprecation_message("parse_syntax", "break", p); // TODO +#endif // ENABLE_CASE_CHECK } break; case 'c': case 'C': - if(p2 - p == 4 && !strncasecmp(p,"case",4)) { + if( p2 - p == 4 && strncmp(p, "case", 4) == 0 ) { //Processing case - int pos = syntax.curly_count-1; - if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) { + int pos = script->syntax.curly_count-1; + if(pos < 0 || script->syntax.curly[pos].type != TYPE_SWITCH) { disp_error_message("parse_syntax: unexpected 'case' ",p); return p+1; } else { char label[256]; int l,v; char *np; - if(syntax.curly[pos].count != 1) { + if(script->syntax.curly[pos].count != 1) { //Jump for FALLTHRU - sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count); - syntax.curly[syntax.curly_count++].type = TYPE_NULL; - parse_line(label); - syntax.curly_count--; + sprintf(label,"goto __SW%x_%xJ;",script->syntax.curly[pos].index,script->syntax.curly[pos].count); + script->syntax.curly[script->syntax.curly_count++].type = TYPE_NULL; + script->parse_line(label); + script->syntax.curly_count--; // You are here labeled - sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count); - l=add_str(label); - set_label(l,script_pos, p); + sprintf(label,"__SW%x_%x",script->syntax.curly[pos].index,script->syntax.curly[pos].count); + l=script->add_str(label); + script->set_label(l,script->pos, p); } //Decision statement switch - p = skip_space(p2); + p = script->skip_space(p2); if(p == p2) { disp_error_message("parse_syntax: expect space ' '",p); } // check whether case label is integer or not - v = strtol(p,&np,0); - if(np == p) { //Check for constants - p2 = skip_word(p); - v = p2-p; // length of word at p2 - memcpy(label,p,v); - label[v]='\0'; - if( !script_get_constant(label, &v) ) - disp_error_message("parse_syntax: 'case' label is not an integer",p); - p = skip_word(p); - } else { //Numeric value - if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word + if(is_number(p)) { + //Numeric value + v = (int)strtol(p,&np,0); + if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word p++; - p = skip_word(p); + p = script->skip_word(p); if(np != p) disp_error_message("parse_syntax: 'case' label is not an integer",np); + } else { + //Check for constants + p2 = script->skip_word(p); + v = (int)(size_t) (p2-p); // length of word at p2 + memcpy(label,p,v); + label[v]='\0'; + if( !script->get_constant(label, &v) ) + disp_error_message("parse_syntax: 'case' label is not an integer",p); + p = script->skip_word(p); } - p = skip_space(p); + p = script->skip_space(p); if(*p != ':') disp_error_message("parse_syntax: expect ':'",p); sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;", - v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1); - syntax.curly[syntax.curly_count++].type = TYPE_NULL; + v,script->syntax.curly[pos].index,script->syntax.curly[pos].index,script->syntax.curly[pos].count+1); + script->syntax.curly[script->syntax.curly_count++].type = TYPE_NULL; // Bad I do not parse twice - p2 = parse_line(label); - parse_line(p2); - syntax.curly_count--; - if(syntax.curly[pos].count != 1) { + p2 = script->parse_line(label); + script->parse_line(p2); + script->syntax.curly_count--; + if(script->syntax.curly[pos].count != 1) { // Label after the completion of FALLTHRU - sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count); - l=add_str(label); - set_label(l,script_pos,p); + sprintf(label,"__SW%x_%xJ",script->syntax.curly[pos].index,script->syntax.curly[pos].count); + l=script->add_str(label); + script->set_label(l,script->pos,p); } // check duplication of case label [Rayce] - if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPTRSIZE(v)) != NULL) + if(linkdb_search(&script->syntax.curly[pos].case_label, (void*)h64BPTRSIZE(v)) != NULL) disp_error_message("parse_syntax: dup 'case'",p); - linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPTRSIZE(v), (void*)1); + linkdb_insert(&script->syntax.curly[pos].case_label, (void*)h64BPTRSIZE(v), (void*)1); - sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index); - syntax.curly[syntax.curly_count++].type = TYPE_NULL; + sprintf(label,"set $@__SW%x_VAL,0;",script->syntax.curly[pos].index); + script->syntax.curly[script->syntax.curly_count++].type = TYPE_NULL; - parse_line(label); - syntax.curly_count--; - syntax.curly[pos].count++; + script->parse_line(label); + script->syntax.curly_count--; + script->syntax.curly[pos].count++; } return p + 1; - } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) { + } else if( p2 - p == 8 && strncmp(p, "continue", 8) == 0 ) { // Processing continue char label[256]; - int pos = syntax.curly_count - 1; + int pos = script->syntax.curly_count - 1; while(pos >= 0) { - if(syntax.curly[pos].type == TYPE_DO) { - sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index); - syntax.curly[pos].flag = 1; //Flag put the link for continue + if(script->syntax.curly[pos].type == TYPE_DO) { + sprintf(label,"goto __DO%x_NXT;",script->syntax.curly[pos].index); + script->syntax.curly[pos].flag = 1; //Flag put the link for continue break; - } else if(syntax.curly[pos].type == TYPE_FOR) { - sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index); + } else if(script->syntax.curly[pos].type == TYPE_FOR) { + sprintf(label,"goto __FR%x_NXT;",script->syntax.curly[pos].index); break; - } else if(syntax.curly[pos].type == TYPE_WHILE) { - sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index); + } else if(script->syntax.curly[pos].type == TYPE_WHILE) { + sprintf(label,"goto __WL%x_NXT;",script->syntax.curly[pos].index); break; } pos--; @@ -1457,162 +1535,173 @@ const char* parse_syntax(const char* p) if(pos < 0) { disp_error_message("parse_syntax: unexpected 'continue'",p); } else { - syntax.curly[syntax.curly_count++].type = TYPE_NULL; - parse_line(label); - syntax.curly_count--; + script->syntax.curly[script->syntax.curly_count++].type = TYPE_NULL; + script->parse_line(label); + script->syntax.curly_count--; } - p = skip_space(p2); + p = script->skip_space(p2); if(*p != ';') disp_error_message("parse_syntax: need ';'",p); //Closing decision if, for , while - p = parse_syntax_close(p + 1); + p = script->parse_syntax_close(p + 1); return p; +#ifdef ENABLE_CASE_CHECK + } else if( p2 - p == 4 && strncasecmp(p, "case", 4) == 0 ) { + disp_deprecation_message("parse_syntax", "case", p); // TODO + } else if( p2 - p == 8 && strncasecmp(p, "continue", 8) == 0 ) { + disp_deprecation_message("parse_syntax", "continue", p); // TODO +#endif // ENABLE_CASE_CHECK } break; case 'd': case 'D': - if(p2 - p == 7 && !strncasecmp(p,"default",7)) { + if( p2 - p == 7 && strncmp(p, "default", 7) == 0 ) { // Switch - default processing - int pos = syntax.curly_count-1; - if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) { + int pos = script->syntax.curly_count-1; + if(pos < 0 || script->syntax.curly[pos].type != TYPE_SWITCH) { disp_error_message("parse_syntax: unexpected 'default'",p); - } else if(syntax.curly[pos].flag) { + } else if(script->syntax.curly[pos].flag) { disp_error_message("parse_syntax: dup 'default'",p); } else { char label[256]; int l; // Put the label location - p = skip_space(p2); + p = script->skip_space(p2); if(*p != ':') { disp_error_message("parse_syntax: need ':'",p); } - sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count); - l=add_str(label); - set_label(l,script_pos,p); + sprintf(label,"__SW%x_%x",script->syntax.curly[pos].index,script->syntax.curly[pos].count); + l=script->add_str(label); + script->set_label(l,script->pos,p); // Skip to the next link w/o condition - sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1); - syntax.curly[syntax.curly_count++].type = TYPE_NULL; - parse_line(label); - syntax.curly_count--; + sprintf(label,"goto __SW%x_%x;",script->syntax.curly[pos].index,script->syntax.curly[pos].count+1); + script->syntax.curly[script->syntax.curly_count++].type = TYPE_NULL; + script->parse_line(label); + script->syntax.curly_count--; // The default label - sprintf(label,"__SW%x_DEF",syntax.curly[pos].index); - l=add_str(label); - set_label(l,script_pos,p); + sprintf(label,"__SW%x_DEF",script->syntax.curly[pos].index); + l=script->add_str(label); + script->set_label(l,script->pos,p); - syntax.curly[syntax.curly_count - 1].flag = 1; - syntax.curly[pos].count++; + script->syntax.curly[script->syntax.curly_count - 1].flag = 1; + script->syntax.curly[pos].count++; } return p + 1; - } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) { + } else if( p2 - p == 2 && strncmp(p, "do", 2) == 0 ) { int l; char label[256]; - p=skip_space(p2); + p=script->skip_space(p2); - syntax.curly[syntax.curly_count].type = TYPE_DO; - syntax.curly[syntax.curly_count].count = 1; - syntax.curly[syntax.curly_count].index = syntax.index++; - syntax.curly[syntax.curly_count].flag = 0; + script->syntax.curly[script->syntax.curly_count].type = TYPE_DO; + script->syntax.curly[script->syntax.curly_count].count = 1; + script->syntax.curly[script->syntax.curly_count].index = script->syntax.index++; + script->syntax.curly[script->syntax.curly_count].flag = 0; // Label of the (do) form here - sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index); - l=add_str(label); - set_label(l,script_pos,p); - syntax.curly_count++; + sprintf(label,"__DO%x_BGN",script->syntax.curly[script->syntax.curly_count].index); + l=script->add_str(label); + script->set_label(l,script->pos,p); + script->syntax.curly_count++; return p; +#ifdef ENABLE_CASE_CHECK + } else if( p2 - p == 7 && strncasecmp(p, "default", 7) == 0 ) { + disp_deprecation_message("parse_syntax", "default", p); // TODO + } else if( p2 - p == 2 && strncasecmp(p, "do", 2) == 0 ) { + disp_deprecation_message("parse_syntax", "do", p); // TODO +#endif // ENABLE_CASE_CHECK } break; case 'f': case 'F': - if(p2 - p == 3 && !strncasecmp(p,"for",3)) { + if( p2 - p == 3 && strncmp(p, "for", 3) == 0 ) { int l; char label[256]; - int pos = syntax.curly_count; - syntax.curly[syntax.curly_count].type = TYPE_FOR; - syntax.curly[syntax.curly_count].count = 1; - syntax.curly[syntax.curly_count].index = syntax.index++; - syntax.curly[syntax.curly_count].flag = 0; - syntax.curly_count++; + int pos = script->syntax.curly_count; + script->syntax.curly[script->syntax.curly_count].type = TYPE_FOR; + script->syntax.curly[script->syntax.curly_count].count = 1; + script->syntax.curly[script->syntax.curly_count].index = script->syntax.index++; + script->syntax.curly[script->syntax.curly_count].flag = 0; + script->syntax.curly_count++; - p=skip_space(p2); + p=script->skip_space(p2); if(*p != '(') disp_error_message("parse_syntax: need '('",p); p++; // Execute the initialization statement - syntax.curly[syntax.curly_count++].type = TYPE_NULL; - p=parse_line(p); - syntax.curly_count--; + script->syntax.curly[script->syntax.curly_count++].type = TYPE_NULL; + p=script->parse_line(p); + script->syntax.curly_count--; // Form the start of label decision - sprintf(label,"__FR%x_J",syntax.curly[pos].index); - l=add_str(label); - set_label(l,script_pos,p); + sprintf(label,"__FR%x_J",script->syntax.curly[pos].index); + l=script->add_str(label); + script->set_label(l,script->pos,p); - p=skip_space(p); + p=script->skip_space(p); if(*p == ';') { // For (; Because the pattern of always true ;) ; } else { // Skip to the end point if the condition is false - sprintf(label,"__FR%x_FIN",syntax.curly[pos].index); - add_scriptl(add_str("jump_zero")); - add_scriptc(C_ARG); - p=parse_expr(p); - p=skip_space(p); - add_scriptl(add_str(label)); - add_scriptc(C_FUNC); + sprintf(label,"__FR%x_FIN",script->syntax.curly[pos].index); + script->addl(script->add_str("jump_zero")); + script->addc(C_ARG); + p=script->parse_expr(p); + p=script->skip_space(p); + script->addl(script->add_str(label)); + script->addc(C_FUNC); } if(*p != ';') disp_error_message("parse_syntax: need ';'",p); p++; // Skip to the beginning of the loop - sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index); - syntax.curly[syntax.curly_count++].type = TYPE_NULL; - parse_line(label); - syntax.curly_count--; + sprintf(label,"goto __FR%x_BGN;",script->syntax.curly[pos].index); + script->syntax.curly[script->syntax.curly_count++].type = TYPE_NULL; + script->parse_line(label); + script->syntax.curly_count--; // Labels to form the next loop - sprintf(label,"__FR%x_NXT",syntax.curly[pos].index); - l=add_str(label); - set_label(l,script_pos,p); + sprintf(label,"__FR%x_NXT",script->syntax.curly[pos].index); + l=script->add_str(label); + script->set_label(l,script->pos,p); // Process the next time you enter the loop // A ')' last for; flag to be treated as' - parse_syntax_for_flag = 1; - syntax.curly[syntax.curly_count++].type = TYPE_NULL; - p=parse_line(p); - syntax.curly_count--; - parse_syntax_for_flag = 0; + script->parse_syntax_for_flag = 1; + script->syntax.curly[script->syntax.curly_count++].type = TYPE_NULL; + p=script->parse_line(p); + script->syntax.curly_count--; + script->parse_syntax_for_flag = 0; // Skip to the determination process conditions - sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index); - syntax.curly[syntax.curly_count++].type = TYPE_NULL; - parse_line(label); - syntax.curly_count--; + sprintf(label,"goto __FR%x_J;",script->syntax.curly[pos].index); + script->syntax.curly[script->syntax.curly_count++].type = TYPE_NULL; + script->parse_line(label); + script->syntax.curly_count--; // Loop start labeling - sprintf(label,"__FR%x_BGN",syntax.curly[pos].index); - l=add_str(label); - set_label(l,script_pos,p); + sprintf(label,"__FR%x_BGN",script->syntax.curly[pos].index); + l=script->add_str(label); + script->set_label(l,script->pos,p); return p; - } - else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 ) - {// internal script function + } else if( p2 - p == 8 && strncmp(p, "function", 8) == 0 ) { + // internal script function const char *func_name; - func_name = skip_space(p2); - p = skip_word(func_name); + func_name = script->skip_space(p2); + p = script->skip_word(func_name); if( p == func_name ) disp_error_message("parse_syntax:function: function name is missing or invalid", p); - p2 = skip_space(p); + p2 = script->skip_space(p); if( *p2 == ';' ) {// function <name> ; // function declaration - just register the name int l; - l = add_word(func_name); + l = script->add_word(func_name); if( script->str_data[l].type == C_NOP )// register only, if the name was not used by something else script->str_data[l].type = C_USERFUNC; else if( script->str_data[l].type == C_USERFUNC ) @@ -1621,7 +1710,7 @@ const char* parse_syntax(const char* p) disp_error_message("parse_syntax:function: function name is invalid", func_name); // Close condition of if, for, while - p = parse_syntax_close(p2 + 1); + p = script->parse_syntax_close(p2 + 1); return p; } else if(*p2 == '{') @@ -1629,118 +1718,136 @@ const char* parse_syntax(const char* p) char label[256]; int l; - syntax.curly[syntax.curly_count].type = TYPE_USERFUNC; - syntax.curly[syntax.curly_count].count = 1; - syntax.curly[syntax.curly_count].index = syntax.index++; - syntax.curly[syntax.curly_count].flag = 0; - ++syntax.curly_count; + script->syntax.curly[script->syntax.curly_count].type = TYPE_USERFUNC; + script->syntax.curly[script->syntax.curly_count].count = 1; + script->syntax.curly[script->syntax.curly_count].index = script->syntax.index++; + script->syntax.curly[script->syntax.curly_count].flag = 0; + ++script->syntax.curly_count; // Jump over the function code - sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index); - syntax.curly[syntax.curly_count].type = TYPE_NULL; - ++syntax.curly_count; - parse_line(label); - --syntax.curly_count; + sprintf(label, "goto __FN%x_FIN;", script->syntax.curly[script->syntax.curly_count-1].index); + script->syntax.curly[script->syntax.curly_count].type = TYPE_NULL; + ++script->syntax.curly_count; + script->parse_line(label); + --script->syntax.curly_count; // Set the position of the function (label) - l=add_word(func_name); + l=script->add_word(func_name); if( script->str_data[l].type == C_NOP || script->str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else { script->str_data[l].type = C_USERFUNC; - set_label(l, script_pos, p); - if( parse_options&SCRIPT_USE_LABEL_DB ) - strdb_iput(scriptlabel_db, get_str(l), script_pos); + script->set_label(l, script->pos, p); + if( script->parse_options&SCRIPT_USE_LABEL_DB ) + script->label_add(l,script->pos); } else disp_error_message("parse_syntax:function: function name is invalid", func_name); - return skip_space(p); + return script->skip_space(p); } else { disp_error_message("expect ';' or '{' at function syntax",p); } +#ifdef ENABLE_CASE_CHECK + } else if( p2 - p == 3 && strncasecmp(p, "for", 3) == 0 ) { + disp_deprecation_message("parse_syntax", "for", p); // TODO + } else if( p2 - p == 8 && strncasecmp(p, "function", 8) == 0 ) { + disp_deprecation_message("parse_syntax", "function", p); // TODO +#endif // ENABLE_CASE_CHECK } break; case 'i': case 'I': - if(p2 - p == 2 && !strncasecmp(p,"if",2)) { + if( p2 - p == 2 && strncmp(p, "if", 2) == 0 ) { // If process char label[256]; - p=skip_space(p2); - if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA) + p=script->skip_space(p2); + if(*p != '(') { //Prevent if this {} non-c script->syntax. from Rayce (jA) disp_error_message("need '('",p); } - syntax.curly[syntax.curly_count].type = TYPE_IF; - syntax.curly[syntax.curly_count].count = 1; - syntax.curly[syntax.curly_count].index = syntax.index++; - syntax.curly[syntax.curly_count].flag = 0; - sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count); - syntax.curly_count++; - add_scriptl(add_str("jump_zero")); - add_scriptc(C_ARG); - p=parse_expr(p); - p=skip_space(p); - add_scriptl(add_str(label)); - add_scriptc(C_FUNC); + script->syntax.curly[script->syntax.curly_count].type = TYPE_IF; + script->syntax.curly[script->syntax.curly_count].count = 1; + script->syntax.curly[script->syntax.curly_count].index = script->syntax.index++; + script->syntax.curly[script->syntax.curly_count].flag = 0; + sprintf(label,"__IF%x_%x",script->syntax.curly[script->syntax.curly_count].index,script->syntax.curly[script->syntax.curly_count].count); + script->syntax.curly_count++; + script->addl(script->add_str("jump_zero")); + script->addc(C_ARG); + p=script->parse_expr(p); + p=script->skip_space(p); + script->addl(script->add_str(label)); + script->addc(C_FUNC); return p; +#ifdef ENABLE_CASE_CHECK + } else if( p2 - p == 2 && strncasecmp(p, "if", 2) == 0 ) { + disp_deprecation_message("parse_syntax", "if", p); // TODO +#endif // ENABLE_CASE_CHECK } break; case 's': case 'S': - if(p2 - p == 6 && !strncasecmp(p,"switch",6)) { + if( p2 - p == 6 && strncmp(p, "switch", 6) == 0 ) { // Processing of switch () char label[256]; - p=skip_space(p2); + p=script->skip_space(p2); if(*p != '(') { disp_error_message("need '('",p); } - syntax.curly[syntax.curly_count].type = TYPE_SWITCH; - syntax.curly[syntax.curly_count].count = 1; - syntax.curly[syntax.curly_count].index = syntax.index++; - syntax.curly[syntax.curly_count].flag = 0; - sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index); - syntax.curly_count++; - add_scriptl(add_str("set")); - add_scriptc(C_ARG); - add_scriptl(add_str(label)); - p=parse_expr(p); - p=skip_space(p); + script->syntax.curly[script->syntax.curly_count].type = TYPE_SWITCH; + script->syntax.curly[script->syntax.curly_count].count = 1; + script->syntax.curly[script->syntax.curly_count].index = script->syntax.index++; + script->syntax.curly[script->syntax.curly_count].flag = 0; + sprintf(label,"$@__SW%x_VAL",script->syntax.curly[script->syntax.curly_count].index); + script->syntax.curly_count++; + script->addl(script->add_str("set")); + script->addc(C_ARG); + script->addl(script->add_str(label)); + p=script->parse_expr(p); + p=script->skip_space(p); if(*p != '{') { disp_error_message("parse_syntax: need '{'",p); } - add_scriptc(C_FUNC); + script->addc(C_FUNC); return p + 1; +#ifdef ENABLE_CASE_CHECK + } else if( p2 - p == 6 && strncasecmp(p, "switch", 6) == 0 ) { + disp_deprecation_message("parse_syntax", "switch", p); // TODO +#endif // ENABLE_CASE_CHECK } break; case 'w': case 'W': - if(p2 - p == 5 && !strncasecmp(p,"while",5)) { + if( p2 - p == 5 && strncmp(p, "while", 5) == 0 ) { int l; char label[256]; - p=skip_space(p2); + p=script->skip_space(p2); if(*p != '(') { disp_error_message("need '('",p); } - syntax.curly[syntax.curly_count].type = TYPE_WHILE; - syntax.curly[syntax.curly_count].count = 1; - syntax.curly[syntax.curly_count].index = syntax.index++; - syntax.curly[syntax.curly_count].flag = 0; + script->syntax.curly[script->syntax.curly_count].type = TYPE_WHILE; + script->syntax.curly[script->syntax.curly_count].count = 1; + script->syntax.curly[script->syntax.curly_count].index = script->syntax.index++; + script->syntax.curly[script->syntax.curly_count].flag = 0; // Form the start of label decision - sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index); - l=add_str(label); - set_label(l,script_pos,p); + sprintf(label,"__WL%x_NXT",script->syntax.curly[script->syntax.curly_count].index); + l=script->add_str(label); + script->set_label(l,script->pos,p); // Skip to the end point if the condition is false - sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index); - syntax.curly_count++; - add_scriptl(add_str("jump_zero")); - add_scriptc(C_ARG); - p=parse_expr(p); - p=skip_space(p); - add_scriptl(add_str(label)); - add_scriptc(C_FUNC); + sprintf(label,"__WL%x_FIN",script->syntax.curly[script->syntax.curly_count].index); + script->syntax.curly_count++; + script->addl(script->add_str("jump_zero")); + script->addc(C_ARG); + p=script->parse_expr(p); + p=script->skip_space(p); + script->addl(script->add_str(label)); + script->addc(C_FUNC); return p; +#ifdef ENABLE_CASE_CHECK + } else if( p2 - p == 5 && strncasecmp(p, "while", 5) == 0 ) { + disp_deprecation_message("parse_syntax", "while", p); // TODO +#endif // ENABLE_CASE_CHECK } break; } @@ -1752,183 +1859,190 @@ const char* parse_syntax_close(const char *p) { int flag; do { - p = parse_syntax_close_sub(p,&flag); + p = script->parse_syntax_close_sub(p,&flag); } while(flag); return p; } // Close judgment if, for, while, of do -// flag == 1 : closed -// flag == 0 : not closed +// flag == 1 : closed +// flag == 0 : not closed const char* parse_syntax_close_sub(const char* p,int* flag) { char label[256]; - int pos = syntax.curly_count - 1; + int pos = script->syntax.curly_count - 1; int l; *flag = 1; - if(syntax.curly_count <= 0) { + if(script->syntax.curly_count <= 0) { *flag = 0; return p; - } else if(syntax.curly[pos].type == TYPE_IF) { + } else if(script->syntax.curly[pos].type == TYPE_IF) { const char *bp = p; const char *p2; // if-block and else-block end is a new line - parse_nextline(false, p); + script->parse_nextline(false, p); // Skip to the last location if - sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index); - syntax.curly[syntax.curly_count++].type = TYPE_NULL; - parse_line(label); - syntax.curly_count--; + sprintf(label,"goto __IF%x_FIN;",script->syntax.curly[pos].index); + script->syntax.curly[script->syntax.curly_count++].type = TYPE_NULL; + script->parse_line(label); + script->syntax.curly_count--; // Put the label of the location - sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count); - l=add_str(label); - set_label(l,script_pos,p); - - syntax.curly[pos].count++; - p = skip_space(p); - p2 = skip_word(p); - if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) { + sprintf(label,"__IF%x_%x",script->syntax.curly[pos].index,script->syntax.curly[pos].count); + l=script->add_str(label); + script->set_label(l,script->pos,p); + + script->syntax.curly[pos].count++; + p = script->skip_space(p); + p2 = script->skip_word(p); + if( !script->syntax.curly[pos].flag && p2 - p == 4 && strncmp(p, "else", 4) == 0 ) { // else or else - if - p = skip_space(p2); - p2 = skip_word(p); - if(p2 - p == 2 && !strncasecmp(p,"if",2)) { + p = script->skip_space(p2); + p2 = script->skip_word(p); + if( p2 - p == 2 && strncmp(p, "if", 2) == 0 ) { // else - if - p=skip_space(p2); + p=script->skip_space(p2); if(*p != '(') { disp_error_message("need '('",p); } - sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count); - add_scriptl(add_str("jump_zero")); - add_scriptc(C_ARG); - p=parse_expr(p); - p=skip_space(p); - add_scriptl(add_str(label)); - add_scriptc(C_FUNC); + sprintf(label,"__IF%x_%x",script->syntax.curly[pos].index,script->syntax.curly[pos].count); + script->addl(script->add_str("jump_zero")); + script->addc(C_ARG); + p=script->parse_expr(p); + p=script->skip_space(p); + script->addl(script->add_str(label)); + script->addc(C_FUNC); *flag = 0; return p; +#ifdef ENABLE_CASE_CHECK + } else if( p2 - p == 2 && strncasecmp(p, "if", 2) == 0 ) { + disp_deprecation_message("parse_syntax", "if", p); // TODO +#endif // ENABLE_CASE_CHECK } else { // else - if(!syntax.curly[pos].flag) { - syntax.curly[pos].flag = 1; + if(!script->syntax.curly[pos].flag) { + script->syntax.curly[pos].flag = 1; *flag = 0; return p; } } +#ifdef ENABLE_CASE_CHECK + } else if( !script->syntax.curly[pos].flag && p2 - p == 4 && strncasecmp(p, "else", 4) == 0 ) { + disp_deprecation_message("parse_syntax", "else", p); // TODO +#endif // ENABLE_CASE_CHECK } // Close if - syntax.curly_count--; + script->syntax.curly_count--; // Put the label of the final location - sprintf(label,"__IF%x_FIN",syntax.curly[pos].index); - l=add_str(label); - set_label(l,script_pos,p); - if(syntax.curly[pos].flag == 1) { + sprintf(label,"__IF%x_FIN",script->syntax.curly[pos].index); + l=script->add_str(label); + script->set_label(l,script->pos,p); + if(script->syntax.curly[pos].flag == 1) { // Because the position of the pointer is the same if not else for this return bp; } return p; - } else if(syntax.curly[pos].type == TYPE_DO) { - int l; - char label[256]; + } else if(script->syntax.curly[pos].type == TYPE_DO) { const char *p2; - if(syntax.curly[pos].flag) { + if(script->syntax.curly[pos].flag) { // (Come here continue) to form the label here - sprintf(label,"__DO%x_NXT",syntax.curly[pos].index); - l=add_str(label); - set_label(l,script_pos,p); + sprintf(label,"__DO%x_NXT",script->syntax.curly[pos].index); + l=script->add_str(label); + script->set_label(l,script->pos,p); } // Skip to the end point if the condition is false - p = skip_space(p); - p2 = skip_word(p); - if(p2 - p != 5 || strncasecmp(p,"while",5)) + p = script->skip_space(p); + p2 = script->skip_word(p); + if( p2 - p != 5 || strncmp(p, "while", 5) != 0 ) { +#ifdef ENABLE_CASE_CHECK + if( p2 - p == 5 && strncasecmp(p, "while", 5) == 0 ) disp_deprecation_message("parse_syntax", "while", p); // TODO +#endif // ENABLE_CASE_CHECK disp_error_message("parse_syntax: need 'while'",p); + } - p = skip_space(p2); + p = script->skip_space(p2); if(*p != '(') { disp_error_message("need '('",p); } // do-block end is a new line - parse_nextline(false, p); + script->parse_nextline(false, p); - sprintf(label,"__DO%x_FIN",syntax.curly[pos].index); - add_scriptl(add_str("jump_zero")); - add_scriptc(C_ARG); - p=parse_expr(p); - p=skip_space(p); - add_scriptl(add_str(label)); - add_scriptc(C_FUNC); + sprintf(label,"__DO%x_FIN",script->syntax.curly[pos].index); + script->addl(script->add_str("jump_zero")); + script->addc(C_ARG); + p=script->parse_expr(p); + p=script->skip_space(p); + script->addl(script->add_str(label)); + script->addc(C_FUNC); // Skip to the starting point - sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index); - syntax.curly[syntax.curly_count++].type = TYPE_NULL; - parse_line(label); - syntax.curly_count--; + sprintf(label,"goto __DO%x_BGN;",script->syntax.curly[pos].index); + script->syntax.curly[script->syntax.curly_count++].type = TYPE_NULL; + script->parse_line(label); + script->syntax.curly_count--; // Form label of the end point conditions - sprintf(label,"__DO%x_FIN",syntax.curly[pos].index); - l=add_str(label); - set_label(l,script_pos,p); - p = skip_space(p); + sprintf(label,"__DO%x_FIN",script->syntax.curly[pos].index); + l=script->add_str(label); + script->set_label(l,script->pos,p); + p = script->skip_space(p); if(*p != ';') { disp_error_message("parse_syntax: need ';'",p); return p+1; } p++; - syntax.curly_count--; + script->syntax.curly_count--; return p; - } else if(syntax.curly[pos].type == TYPE_FOR) { + } else if(script->syntax.curly[pos].type == TYPE_FOR) { // for-block end is a new line - parse_nextline(false, p); + script->parse_nextline(false, p); // Skip to the next loop - sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index); - syntax.curly[syntax.curly_count++].type = TYPE_NULL; - parse_line(label); - syntax.curly_count--; + sprintf(label,"goto __FR%x_NXT;",script->syntax.curly[pos].index); + script->syntax.curly[script->syntax.curly_count++].type = TYPE_NULL; + script->parse_line(label); + script->syntax.curly_count--; // End for labeling - sprintf(label,"__FR%x_FIN",syntax.curly[pos].index); - l=add_str(label); - set_label(l,script_pos,p); - syntax.curly_count--; + sprintf(label,"__FR%x_FIN",script->syntax.curly[pos].index); + l=script->add_str(label); + script->set_label(l,script->pos,p); + script->syntax.curly_count--; return p; - } else if(syntax.curly[pos].type == TYPE_WHILE) { + } else if(script->syntax.curly[pos].type == TYPE_WHILE) { // while-block end is a new line - parse_nextline(false, p); + script->parse_nextline(false, p); // Skip to the decision while - sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index); - syntax.curly[syntax.curly_count++].type = TYPE_NULL; - parse_line(label); - syntax.curly_count--; + sprintf(label,"goto __WL%x_NXT;",script->syntax.curly[pos].index); + script->syntax.curly[script->syntax.curly_count++].type = TYPE_NULL; + script->parse_line(label); + script->syntax.curly_count--; // End while labeling - sprintf(label,"__WL%x_FIN",syntax.curly[pos].index); - l=add_str(label); - set_label(l,script_pos,p); - syntax.curly_count--; + sprintf(label,"__WL%x_FIN",script->syntax.curly[pos].index); + l=script->add_str(label); + script->set_label(l,script->pos,p); + script->syntax.curly_count--; return p; - } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) { - int pos = syntax.curly_count-1; - char label[256]; - int l; + } else if(script->syntax.curly[pos].type == TYPE_USERFUNC) { // Back sprintf(label,"return;"); - syntax.curly[syntax.curly_count++].type = TYPE_NULL; - parse_line(label); - syntax.curly_count--; + script->syntax.curly[script->syntax.curly_count++].type = TYPE_NULL; + script->parse_line(label); + script->syntax.curly_count--; // Put the label of the location - sprintf(label,"__FN%x_FIN",syntax.curly[pos].index); - l=add_str(label); - set_label(l,script_pos,p); - syntax.curly_count--; + sprintf(label,"__FN%x_FIN",script->syntax.curly[pos].index); + l=script->add_str(label); + script->set_label(l,script->pos,p); + script->syntax.curly_count--; return p; } else { *flag = 0; @@ -1939,7 +2053,7 @@ const char* parse_syntax_close_sub(const char* p,int* flag) /// Retrieves the value of a constant. bool script_get_constant(const char* name, int* value) { - int n = search_str(name); + int n = script->search_str(name); if( n == -1 || script->str_data[n].type != C_INT ) {// not found or not a constant @@ -1951,287 +2065,380 @@ bool script_get_constant(const char* name, int* value) } /// Creates new constant or parameter with given value. -void script_set_constant(const char* name, int value, bool isparameter) -{ - int n = add_str(name); +void script_set_constant(const char* name, int value, bool isparameter) { + int n = script->add_str(name); - if( script->str_data[n].type == C_NOP ) - {// new + if( script->str_data[n].type == C_NOP ) {// new script->str_data[n].type = isparameter ? C_PARAM : C_INT; script->str_data[n].val = value; - } - else if( script->str_data[n].type == C_PARAM || script->str_data[n].type == C_INT ) - {// existing parameter or constant + } else if( script->str_data[n].type == C_PARAM || script->str_data[n].type == C_INT ) {// existing parameter or constant ShowError("script_set_constant: Attempted to overwrite existing %s '%s' (old value=%d, new value=%d).\n", ( script->str_data[n].type == C_PARAM ) ? "parameter" : "constant", name, script->str_data[n].val, value); - } - else - {// existing name - ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(script->str_data[n].type)); + } else {// existing name + ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script->op2name(script->str_data[n].type)); } } +/* adds data to a existent constant in the database, inserted normally via parse */ +void script_set_constant2(const char *name, int value, bool isparameter) { + int n = script->add_str(name); + if( script->str_data[n].type == C_PARAM ) { + ShowError("script_set_constant2: Attempted to overwrite existing parameter '%s' with a constant (value=%d).\n", name, value); + return; + } + + if( script->str_data[n].type == C_NAME && script->str_data[n].val ) { + ShowWarning("script_set_constant2: Attempted to overwrite existing variable '%s' with a constant (value=%d).\n", name, value); + return; + } + + if( script->str_data[n].type == C_INT && value && value != script->str_data[n].val ) { // existing constant + ShowWarning("script_set_constant2: Attempted to overwrite existing constant '%s' (old value=%d, new value=%d).\n", name, script->str_data[n].val, value); + return; + } + + if( script->str_data[n].type != C_NOP ) { + script->str_data[n].func = NULL; + script->str_data[n].backpatch = -1; + script->str_data[n].label = -1; + } + + script->str_data[n].type = isparameter ? C_PARAM : C_INT; + script->str_data[n].val = value; + +} /*========================================== * Reading constant databases * const.txt *------------------------------------------*/ -static void read_constdb(void) -{ +void read_constdb(void) { FILE *fp; char line[1024],name[1024],val[1024]; int type; - sprintf(line, "%s/const.txt", iMap->db_path); + sprintf(line, "%s/const.txt", map->db_path); fp=fopen(line, "r"); - if(fp==NULL){ + if(fp==NULL) { ShowError("can't read %s\n", line); return ; } - while(fgets(line, sizeof(line), fp)) - { - if(line[0]=='/' && line[1]=='/') + while (fgets(line, sizeof(line), fp)) { + if (line[0] == '/' && line[1] == '/') continue; - type=0; - if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 || - sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){ - script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type); + type = 0; + if (sscanf(line, "%1023[A-Za-z0-9_],%1023[-0-9xXA-Fa-f],%d", name, val, &type) >=2 + || sscanf(line, "%1023[A-Za-z0-9_] %1023[-0-9xXA-Fa-f] %d", name, val, &type) >=2 + ) { + script->set_constant(name, (int)strtol(val, NULL, 0), (bool)type); } } fclose(fp); } +// Standard UNIX tab size is 8 +#define TAB_SIZE 8 +#define update_tabstop(tabstop,chars) \ + do { \ + (tabstop) -= (chars); \ + while ((tabstop) <= 0) (tabstop) += TAB_SIZE; \ + } while (false) + /*========================================== * Display emplacement line of script *------------------------------------------*/ -static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line) +const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line) { - int i; + int i, mark_pos = 0, tabstop = TAB_SIZE; if( p == NULL || !p[0] ) return NULL; if( line < 0 ) - StrBuf->Printf(buf, "*% 5d : ", -line); + StrBuf->Printf(buf, "*%5d: ", -line); // len = 8 else - StrBuf->Printf(buf, " % 5d : ", line); - for(i=0;p[i] && p[i] != '\n';i++){ - if(p + i != mark) - StrBuf->Printf(buf, "%c", p[i]); + StrBuf->Printf(buf, " %5d: ", line); // len = 8 + update_tabstop(tabstop,8); // len = 8 + for( i=0; p[i] && p[i] != '\n'; i++ ) { + char c = p[i]; + int w = 1; + // Like Clang does, let's print the code with tabs expanded to spaces to ensure that the marker will be under the right character + if( c == '\t' ) { + c = ' '; + w = tabstop; + } + update_tabstop(tabstop, w); + if( p + i < mark) + mark_pos += w; + if( p + i != mark) + StrBuf->Printf(buf, "%*c", w, c); else - StrBuf->Printf(buf, "\'%c\'", p[i]); + StrBuf->Printf(buf, CL_BT_RED"%*c"CL_RESET, w, c); } StrBuf->AppendStr(buf, "\n"); + if( mark ) { + StrBuf->AppendStr(buf, " "CL_BT_CYAN); // len = 8 + for( ; mark_pos > 0; mark_pos-- ) { + StrBuf->AppendStr(buf, "~"); + } + StrBuf->AppendStr(buf, CL_RESET CL_BT_GREEN"^"CL_RESET"\n"); + } return p+i+(p[i] == '\n' ? 1 : 0); } +#undef TAB_SIZE +#undef update_tabstop -void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos) -{ +#define CONTEXTLINES 3 +void script_errorwarning_sub(StringBuf *buf, const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos) { // Find the line where the error occurred int j; int line = start_line; - const char *p; - const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL }; - StringBuf buf; + const char *p, *error_linepos; + const char *linestart[CONTEXTLINES] = { NULL }; - for(p=src;p && *p;line++){ + for(p=src;p && *p;line++) { const char *lineend=strchr(p,'\n'); - if(lineend==NULL || error_pos<lineend){ + if(lineend==NULL || error_pos<lineend) { break; } - for( j = 0; j < 4; j++ ) { + for( j = 0; j < CONTEXTLINES-1; j++ ) { linestart[j] = linestart[j+1]; } - linestart[4] = p; - p=lineend+1; + linestart[CONTEXTLINES-1] = p; + p = lineend+1; } + error_linepos = p; - StrBuf->Init(&buf); - StrBuf->AppendStr(&buf, "\a\n"); if( line >= 0 ) - StrBuf->Printf(&buf, "script error on %s line %d\n", file, line); + StrBuf->Printf(buf, "script error in file '%s' line %d column %"PRIdPTR"\n", file, line, error_pos-error_linepos+1); else - StrBuf->Printf(&buf, "script error on %s item ID %d\n", file, -line); + StrBuf->Printf(buf, "script error in file '%s' item ID %d\n", file, -line); - StrBuf->Printf(&buf, " %s\n", error_msg); - for(j = 0; j < 5; j++ ) { - script_print_line(&buf, linestart[j], NULL, line + j - 5); + StrBuf->Printf(buf, " %s\n", error_msg); + for(j = 0; j < CONTEXTLINES; j++ ) { + script->print_line(buf, linestart[j], NULL, line + j - CONTEXTLINES); } - p = script_print_line(&buf, p, error_pos, -line); - for(j = 0; j < 5; j++) { - p = script_print_line(&buf, p, NULL, line + j + 1 ); + p = script->print_line(buf, p, error_pos, -line); + for(j = 0; j < CONTEXTLINES; j++) { + p = script->print_line(buf, p, NULL, line + j + 1 ); } +} +#undef CONTEXTLINES + +void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos) { + StringBuf buf; + + StrBuf->Init(&buf); + StrBuf->AppendStr(&buf, "\a"); + + script->errorwarning_sub(&buf, src, file, start_line, error_msg, error_pos); + ShowError("%s", StrBuf->Value(&buf)); StrBuf->Destroy(&buf); } +void script_warning(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos) { + StringBuf buf; + + StrBuf->Init(&buf); + + script->errorwarning_sub(&buf, src, file, start_line, error_msg, error_pos); + + ShowWarning("%s", StrBuf->Value(&buf)); + StrBuf->Destroy(&buf); +} + /*========================================== * Analysis of the script *------------------------------------------*/ -struct script_code* parse_script(const char *src,const char *file,int line,int options) -{ +struct script_code* parse_script(const char *src,const char *file,int line,int options, int *retval) { const char *p,*tmpp; int i; struct script_code* code = NULL; char end; bool unresolved_names = false; + script->parser_current_src = src; + script->parser_current_file = file; + script->parser_current_line = line; + if( src == NULL ) return NULL;// empty script - memset(&syntax,0,sizeof(syntax)); + memset(&script->syntax,0,sizeof(script->syntax)); - script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char)); - script_pos=0; - script_size=SCRIPT_BLOCK_SIZE; - parse_nextline(true, NULL); + script->buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char)); + script->pos=0; + script->size=SCRIPT_BLOCK_SIZE; + script->parse_nextline(true, NULL); // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here if( options&SCRIPT_USE_LABEL_DB ) - db_clear(scriptlabel_db); - parse_options = options; + script->label_count = 0; + script->parse_options = options; - if( setjmp( error_jump ) != 0 ) { + if( setjmp( script->error_jump ) != 0 ) { //Restore program state when script has problems. [from jA] - int i; - const int size = ARRAYLENGTH(syntax.curly); - if( error_report ) - script_error(src,file,line,error_msg,error_pos); - aFree( error_msg ); - aFree( script_buf ); - script_pos = 0; - script_size = 0; - script_buf = NULL; + const int size = ARRAYLENGTH(script->syntax.curly); + if( script->error_report ) + script->error(src,file,line,script->error_msg,script->error_pos); + aFree( script->error_msg ); + aFree( script->buf ); + script->pos = 0; + script->size = 0; + script->buf = NULL; for(i=LABEL_START;i<script->str_num;i++) if(script->str_data[i].type == C_NOP) script->str_data[i].type = C_NAME; for(i=0; i<size; i++) - linkdb_final(&syntax.curly[i].case_label); + linkdb_final(&script->syntax.curly[i].case_label); +#ifdef ENABLE_CASE_CHECK + script->local_casecheck.clear(); + script->parser_current_src = NULL; + script->parser_current_file = NULL; + script->parser_current_line = 0; +#endif // ENABLE_CASE_CHECK + if (retval) *retval = EXIT_FAILURE; return NULL; } - parse_syntax_for_flag=0; + script->parse_syntax_for_flag=0; p=src; - p=skip_space(p); + p=script->skip_space(p); if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS ) {// does not require brackets around the script if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) ) {// empty script and can return NULL - aFree( script_buf ); - script_pos = 0; - script_size = 0; - script_buf = NULL; + aFree( script->buf ); + script->pos = 0; + script->size = 0; + script->buf = NULL; +#ifdef ENABLE_CASE_CHECK + script->local_casecheck.clear(); + script->parser_current_src = NULL; + script->parser_current_file = NULL; + script->parser_current_line = 0; +#endif // ENABLE_CASE_CHECK return NULL; } end = '\0'; } else {// requires brackets around the script - if( *p != '{' ) + if( *p != '{' ) { disp_error_message("not found '{'",p); - p = skip_space(p+1); + if (retval) *retval = EXIT_FAILURE; + } + p = script->skip_space(p+1); if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) ) {// empty script and can return NULL - aFree( script_buf ); - script_pos = 0; - script_size = 0; - script_buf = NULL; + aFree( script->buf ); + script->pos = 0; + script->size = 0; + script->buf = NULL; +#ifdef ENABLE_CASE_CHECK + script->local_casecheck.clear(); + script->parser_current_src = NULL; + script->parser_current_file = NULL; + script->parser_current_line = 0; +#endif // ENABLE_CASE_CHECK return NULL; } end = '}'; } // clear references of labels, variables and internal functions - for(i=LABEL_START;i<script->str_num;i++){ + for(i=LABEL_START;i<script->str_num;i++) { if( script->str_data[i].type==C_POS || script->str_data[i].type==C_NAME || script->str_data[i].type==C_USERFUNC || script->str_data[i].type == C_USERFUNC_POS - ){ + ) { script->str_data[i].type=C_NOP; script->str_data[i].backpatch=-1; script->str_data[i].label=-1; } } - while( syntax.curly_count != 0 || *p != end ) + while( script->syntax.curly_count != 0 || *p != end ) { if( *p == '\0' ) disp_error_message("unexpected end of script",p); // Special handling only label - tmpp=skip_space(skip_word(p)); - if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){ - i=add_word(p); - set_label(i,script_pos,p); - if( parse_options&SCRIPT_USE_LABEL_DB ) - strdb_iput(scriptlabel_db, get_str(i), script_pos); + tmpp=script->skip_space(script->skip_word(p)); + if(*tmpp==':' && !(strncmp(p,"default:",8) == 0 && p + 7 == tmpp)) { + i=script->add_word(p); + script->set_label(i,script->pos,p); + if( script->parse_options&SCRIPT_USE_LABEL_DB ) + script->label_add(i,script->pos); p=tmpp+1; - p=skip_space(p); + p=script->skip_space(p); continue; } // All other lumped - p=parse_line(p); - p=skip_space(p); + p=script->parse_line(p); + p=script->skip_space(p); - parse_nextline(false, p); + script->parse_nextline(false, p); } - add_scriptc(C_NOP); + script->addc(C_NOP); // trim code to size - script_size = script_pos; - RECREATE(script_buf,unsigned char,script_pos); + script->size = script->pos; + RECREATE(script->buf,unsigned char,script->pos); // default unknown references to variables - for(i=LABEL_START;i<script->str_num;i++){ - if(script->str_data[i].type==C_NOP){ + for(i=LABEL_START;i<script->str_num;i++) { + if(script->str_data[i].type==C_NOP) { int j,next; script->str_data[i].type=C_NAME; script->str_data[i].label=i; - for(j=script->str_data[i].backpatch;j>=0 && j!=0x00ffffff;){ - next=GETVALUE(script_buf,j); - SETVALUE(script_buf,j,i); + for(j=script->str_data[i].backpatch;j>=0 && j!=0x00ffffff;) { + next=GETVALUE(script->buf,j); + SETVALUE(script->buf,j,i); j=next; } } else if( script->str_data[i].type == C_USERFUNC ) {// 'function name;' without follow-up code ShowError("parse_script: function '%s' declared but not defined.\n", script->str_buf+script->str_data[i].str); + if (retval) *retval = EXIT_FAILURE; unresolved_names = true; } } - if( unresolved_names ) - { + if( unresolved_names ) { disp_error_message("parse_script: unresolved function references", p); + if (retval) *retval = EXIT_FAILURE; } -#ifdef DEBUG_DISP - for(i=0;i<script_pos;i++){ +#ifdef SCRIPT_DEBUG_DISP + for(i=0;i<script->pos;i++) { if((i&15)==0) ShowMessage("%04x : ",i); - ShowMessage("%02x ",script_buf[i]); + ShowMessage("%02x ",script->buf[i]); if((i&15)==15) ShowMessage("\n"); } ShowMessage("\n"); #endif -#ifdef DEBUG_DISASM +#ifdef SCRIPT_DEBUG_DISASM { int i = 0,j; - while(i < script_pos) { - c_op op = get_com(script_buf,&i); + while(i < script->pos) { + c_op op = script->get_com(script->buf,&i); - ShowMessage("%06x %s", i, script_op2name(op)); + ShowMessage("%06x %s", i, script->op2name(op)); j = i; switch(op) { case C_INT: - ShowMessage(" %d", get_num(script_buf,&i)); + ShowMessage(" %d", script->get_num(script->buf,&i)); break; case C_POS: - ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff); + ShowMessage(" 0x%06x", *(int*)(script->buf+i)&0xffffff); i += 3; break; case C_NAME: - j = (*(int*)(script_buf+i)&0xffffff); - ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j)); + j = (*(int*)(script->buf+i)&0xffffff); + ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : script->get_str(j)); i += 3; break; case C_STR: - j = strlen(script_buf + i); - ShowMessage(" %s", script_buf + i); + j = strlen((char*)script->buf + i); + ShowMessage(" %s", script->buf + i); i += j+1; break; } @@ -2241,9 +2448,16 @@ struct script_code* parse_script(const char *src,const char *file,int line,int o #endif CREATE(code,struct script_code,1); - code->script_buf = script_buf; - code->script_size = script_size; - code->script_vars = NULL; + code->script_buf = script->buf; + code->script_size = script->size; + code->local.vars = NULL; + code->local.arrays = NULL; +#ifdef ENABLE_CASE_CHECK + script->local_casecheck.clear(); + script->parser_current_src = NULL; + script->parser_current_file = NULL; + script->parser_current_line = 0; +#endif // ENABLE_CASE_CHECK return code; } @@ -2251,28 +2465,30 @@ struct script_code* parse_script(const char *src,const char *file,int line,int o /// If there is no player attached, the script is terminated. TBL_PC *script_rid2sd(struct script_state *st) { TBL_PC *sd; - if( !( sd = iMap->id2sd(st->rid) ) ){ + if( !( sd = map->id2sd(st->rid) ) ) { ShowError("script_rid2sd: fatal error ! player not attached!\n"); - script_reportfunc(st); - script_reportsrc(st); + script->reportfunc(st); + script->reportsrc(st); st->state = END; } return sd; } -/// Dereferences a variable/constant, replacing it with a copy of the value. -/// -/// @param st Script state -/// @param data Variable/constant -void get_val(struct script_state* st, struct script_data* data) -{ +/** + * Dereferences a variable/constant, replacing it with a copy of the value. + * + * @param st[in] script state. + * @param data[in,out] variable/constant. + * @return pointer to data, for convenience. + */ +struct script_data *get_val(struct script_state* st, struct script_data* data) { const char* name; char prefix; char postfix; TBL_PC* sd = NULL; if( !data_isreference(data) ) - return;// not a variable/constant + return data;// not a variable/constant name = reference_getname(data); prefix = name[0]; @@ -2280,18 +2496,18 @@ void get_val(struct script_state* st, struct script_data* data) //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS] if( !reference_toconstant(data) && not_server_variable(prefix) ) { - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) {// needs player attached if( postfix == '$' ) {// string variable - ShowWarning("script:script->get_val: cannot access player variable '%s', defaulting to \"\"\n", name); + ShowWarning("script_get_val: cannot access player variable '%s', defaulting to \"\"\n", name); data->type = C_CONSTSTR; data->u.str = ""; } else {// integer variable - ShowWarning("script:script->get_val: cannot access player variable '%s', defaulting to 0\n", name); + ShowWarning("script_get_val: cannot access player variable '%s', defaulting to 0\n", name); data->type = C_INT; data->u.num = 0; } - return; + return data; } } @@ -2302,36 +2518,36 @@ void get_val(struct script_state* st, struct script_data* data) data->u.str = pc->readregstr(sd, data->u.num); break; case '$': - data->u.str = mapreg_readregstr(data->u.num); + data->u.str = mapreg->readregstr(data->u.num); break; case '#': if( name[1] == '#' ) - data->u.str = pc_readaccountreg2str(sd, name);// global + data->u.str = pc_readaccountreg2str(sd, data->u.num);// global else - data->u.str = pc_readaccountregstr(sd, name);// local + data->u.str = pc_readaccountregstr(sd, data->u.num);// local break; case '.': { - struct DBMap* n = - data->ref ? *data->ref: - name[1] == '@' ? st->stack->var_function:// instance/scope variable - st->script->script_vars;// npc variable + struct DBMap* n = data->ref ? + data->ref->vars : name[1] == '@' ? + st->stack->scope.vars : // instance/scope variable + st->script->local.vars; // npc variable if( n ) - data->u.str = (char*)idb_get(n,reference_getuid(data)); + data->u.str = (char*)i64db_get(n,reference_getuid(data)); else data->u.str = NULL; } break; case '\'': if ( st->instance_id >= 0 ) { - data->u.str = (char*)idb_get(instances[st->instance_id].vars,reference_getuid(data)); + data->u.str = (char*)i64db_get(instance->list[st->instance_id].regs.vars, reference_getuid(data)); } else { - ShowWarning("script:script->get_val: cannot access instance variable '%s', defaulting to \"\"\n", name); + ShowWarning("script_get_val: cannot access instance variable '%s', defaulting to \"\"\n", name); data->u.str = NULL; } break; default: - data->u.str = pc_readglobalreg_str(sd, name); + data->u.str = pc_readglobalreg_str(sd, data->u.num); break; } @@ -2357,106 +2573,362 @@ void get_val(struct script_state* st, struct script_data* data) data->u.num = pc->readreg(sd, data->u.num); break; case '$': - data->u.num = mapreg_readreg(data->u.num); + data->u.num = mapreg->readreg(data->u.num); break; case '#': if( name[1] == '#' ) - data->u.num = pc_readaccountreg2(sd, name);// global + data->u.num = pc_readaccountreg2(sd, data->u.num);// global else - data->u.num = pc_readaccountreg(sd, name);// local + data->u.num = pc_readaccountreg(sd, data->u.num);// local break; case '.': { - struct DBMap* n = - data->ref ? *data->ref: - name[1] == '@' ? st->stack->var_function:// instance/scope variable - st->script->script_vars;// npc variable + struct DBMap* n = data->ref ? + data->ref->vars : name[1] == '@' ? + st->stack->scope.vars : // instance/scope variable + st->script->local.vars; // npc variable if( n ) - data->u.num = (int)idb_iget(n,reference_getuid(data)); + data->u.num = (int)i64db_iget(n,reference_getuid(data)); else data->u.num = 0; } break; case '\'': if( st->instance_id >= 0 ) - data->u.num = (int)idb_iget(instances[st->instance_id].vars,reference_getuid(data)); + data->u.num = (int)i64db_iget(instance->list[st->instance_id].regs.vars, reference_getuid(data)); else { - ShowWarning("script:script->get_val: cannot access instance variable '%s', defaulting to 0\n", name); + ShowWarning("script_get_val: cannot access instance variable '%s', defaulting to 0\n", name); data->u.num = 0; } break; default: - data->u.num = pc_readglobalreg(sd, name); + data->u.num = pc_readglobalreg(sd, data->u.num); break; } } + data->ref = NULL; - return; + return data; } -/// Retrieves the value of a reference identified by uid (variable, constant, param) -/// The value is left in the top of the stack and needs to be removed manually. -void* get_val2(struct script_state* st, int uid, struct DBMap** ref) { +/** + * Retrieves the value of a reference identified by uid (variable, constant, param) + * + * The value is left in the top of the stack and needs to be removed manually. + * + * @param st[in] script state. + * @param uid[in] reference identifier. + * @param ref[in] the container to look up the reference into. + * @return the retrieved value of the reference. + */ +void* get_val2(struct script_state* st, int64 uid, struct reg_db *ref) { struct script_data* data; script->push_val(st->stack, C_NAME, uid, ref); data = script_getdatatop(st, -1); script->get_val(st, data); - return (data->type == C_INT ? (void*)__64BPTRSIZE(data->u.num) : (void*)__64BPTRSIZE(data->u.str)); + return (data->type == C_INT ? (void*)h64BPTRSIZE((int32)data->u.num) : (void*)h64BPTRSIZE(data->u.str)); // u.num is int32 because it comes from script->get_val +} +/** + * Because, currently, array members with key 0 are indifferenciable from normal variables, we should ensure its actually in + * Will be gone as soon as undefined var feature is implemented + **/ +void script_array_ensure_zero(struct script_state *st, struct map_session_data *sd, int64 uid, struct reg_db *ref) { + const char *name = script->get_str(script_getvarid(uid)); + struct reg_db *src = script->array_src(st, sd ? sd : st->rid ? map->id2sd(st->rid) : NULL, name, ref); + struct script_array *sa = NULL; + bool insert = false; + + if( sd && !st ) /* when sd comes, st isn't available */ + insert = true; + else { + if( is_string_variable(name) ) { + char* str = (char*)script->get_val2(st, uid, ref); + if( str && *str ) + insert = true; + script_removetop(st, -1, 0); + } else { + int32 num = (int32)h64BPTRSIZE(script->get_val2(st, uid, ref)); + if( num ) + insert = true; + script_removetop(st, -1, 0); + } + } + + if( src && src->arrays ) { + if( (sa = idb_get(src->arrays, script_getvarid(uid)) ) ) { + unsigned int i; + + ARR_FIND(0, sa->size, i, sa->members[i] == 0); + if( i != sa->size ) { + if( !insert ) + script->array_remove_member(src,sa,i); + return; + } + + script->array_add_member(sa,0); + } else if( insert ) { + script->array_update(src,reference_uid(script_getvarid(uid), 0),false); + } + } +} +/** + * Returns array size by ID + **/ +unsigned int script_array_size(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref) { + struct script_array *sa = NULL; + struct reg_db *src = script->array_src(st, sd, name, ref); + + if( src && src->arrays ) + sa = idb_get(src->arrays, script->search_str(name)); + + return sa ? sa->size : 0; +} +/** + * Returns array's highest key (for that awful getarraysize implementation that doesn't really gets the array size) + **/ +unsigned int script_array_highest_key(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref) { + struct script_array *sa = NULL; + struct reg_db *src = script->array_src(st, sd, name, ref); + + + if( src && src->arrays ) { + int key = script->add_word(name); + + script->array_ensure_zero(st,sd,reference_uid(key, 0),ref); + + if( ( sa = idb_get(src->arrays, key) ) ) { + unsigned int i, highest_key = 0; + + for(i = 0; i < sa->size; i++) { + if( sa->members[i] > highest_key ) + highest_key = sa->members[i]; + } + + return sa->size ? highest_key + 1 : 0; + } + } + + return 0; +} +int script_free_array_db(DBKey key, DBData *data, va_list ap) { + struct script_array *sa = DB->data2ptr(data); + aFree(sa->members); + ers_free(script->array_ers, sa); + return 0; +} +/** + * Clears script_array and removes it from script->array_db + **/ +void script_array_delete(struct reg_db *src, struct script_array *sa) { + aFree(sa->members); + idb_remove(src->arrays, sa->id); + ers_free(script->array_ers, sa); +} +/** + * Removes a member from a script_array list + * + * @param idx the index of the member in script_array struct list, not of the actual array member + **/ +void script_array_remove_member(struct reg_db *src, struct script_array *sa, unsigned int idx) { + unsigned int i, cursor; + + /* its the only member left, no need to do anything other than delete the array data */ + if( sa->size == 1 ) { + script->array_delete(src,sa); + return; + } + + sa->members[idx] = UINT_MAX; + + for(i = 0, cursor = 0; i < sa->size; i++) { + if( sa->members[i] == UINT_MAX ) + continue; + if( i != cursor ) + sa->members[cursor] = sa->members[i]; + cursor++; + } + + sa->size = cursor; } +/** + * Appends a new array index to the list in script_array + * + * @param idx the index of the array member being inserted + **/ +void script_array_add_member(struct script_array *sa, unsigned int idx) { + + RECREATE(sa->members, unsigned int, ++sa->size); + + sa->members[sa->size - 1] = idx; -/*========================================== +} +/** + * Obtains the source of the array database for this type and scenario + * Initializes such database when not yet initialized. + **/ +struct reg_db *script_array_src(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref) { + struct reg_db *src = NULL; + + switch( name[0] ) { + /* from player */ + default: /* char reg */ + case '@':/* temp char reg */ + case '#':/* account reg */ + src = &sd->regs; + break; + case '$':/* map reg */ + src = &mapreg->regs; + break; + case '.':/* npc/script */ + if( ref ) + src = ref; + else + src = (name[1] == '@') ? &st->stack->scope : &st->script->local; + break; + case '\'':/* instance */ + if( st->instance_id >= 0 ) { + src = &instance->list[st->instance_id].regs; + } + break; + } + + if( src ) { + if( !src->arrays ) + src->arrays = idb_alloc(DB_OPT_BASE); + return src; + } + + return NULL; +} + +/** + * Processes a array member modification, and update data accordingly + * + * @param src[in,out] Variable source database. If the array database doesn't exist, it is created. + * @param num[in] Variable ID + * @param empty[in] Whether the modified member is empty (needs to be removed) + **/ +void script_array_update(struct reg_db *src, int64 num, bool empty) { + struct script_array *sa = NULL; + int id = script_getvarid(num); + unsigned int index = script_getvaridx(num); + + if (!src->arrays) { + src->arrays = idb_alloc(DB_OPT_BASE); + } else { + sa = idb_get(src->arrays, id); + } + + if( sa ) { + unsigned int i; + + /* search */ + for(i = 0; i < sa->size; i++) { + if( sa->members[i] == index ) + break; + } + + /* if existent */ + if( i != sa->size ) { + /* if empty, we gotta remove it */ + if( empty ) { + script->array_remove_member(src, sa, i); + } + } else if( !empty ) { /* new entry */ + script->array_add_member(sa,index); + /* we do nothing if its empty, no point in modifying array data for a new empty member */ + } + } else if ( !empty ) {/* we only move to create if not empty */ + sa = ers_alloc(script->array_ers, struct script_array); + sa->id = id; + sa->members = NULL; + sa->size = 0; + script->array_add_member(sa,index); + idb_put(src->arrays, id, sa); + } +} + +/** * Stores the value of a script variable - * Return value is 0 on fail, 1 on success. + * + * @param st current script state. + * @param sd current character data. + * @param num variable identifier. + * @param name variable name. + * @param value new value. + * @param ref variable container, in case of a npc/scope variable reference outside the current scope. + * @retval 0 failure. + * @retval 1 success. + * + * TODO: return values are screwed up, have been for some time (reaad: years), e.g. some functions return 1 failure and success. *------------------------------------------*/ -static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct DBMap** ref) -{ +int set_reg(struct script_state* st, TBL_PC* sd, int64 num, const char* name, const void* value, struct reg_db *ref) { char prefix = name[0]; + + if( is_string_variable(name) ) {// string variable + const char *str = (const char*)value; - if( is_string_variable(name) ) - {// string variable - const char* str = (const char*)value; switch (prefix) { - case '@': - return pc->setregstr(sd, num, str); - case '$': - return mapreg_setregstr(num, str); - case '#': - return (name[1] == '#') ? - pc_setaccountreg2str(sd, name, str) : - pc_setaccountregstr(sd, name, str); - case '.': - { - struct DBMap* n; - n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars; - if( n ) { - idb_remove(n, num); - if (str[0]) idb_put(n, num, aStrdup(str)); + case '@': + pc->setregstr(sd, num, str); + return 1; + case '$': + return mapreg->setregstr(num, str); + case '#': + return (name[1] == '#') ? + pc_setaccountreg2str(sd, num, str) : + pc_setaccountregstr(sd, num, str); + case '.': + { + struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local; + if( n ) { + if (str[0]) { + i64db_put(n->vars, num, aStrdup(str)); + if( script_getvaridx(num) ) + script->array_update(n, num, false); + } else { + i64db_remove(n->vars, num); + if( script_getvaridx(num) ) + script->array_update(n, num, true); + } + } } - } - return 1; - case '\'': - if( st->instance_id >= 0 ) { - idb_remove(instances[st->instance_id].vars, num); - if( str[0] ) idb_put(instances[st->instance_id].vars, num, aStrdup(str)); - } - return 1; - default: - return pc_setglobalreg_str(sd, name, str); + return 1; + case '\'': + if( st->instance_id >= 0 ) { + if( str[0] ) { + i64db_put(instance->list[st->instance_id].regs.vars, num, aStrdup(str)); + if( script_getvaridx(num) ) + script->array_update(&instance->list[st->instance_id].regs, num, false); + } else { + i64db_remove(instance->list[st->instance_id].regs.vars, num); + if( script_getvaridx(num) ) + script->array_update(&instance->list[st->instance_id].regs, num, true); + } + } else { + ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name); + script_reportsrc(st); + } + return 1; + default: + return pc_setglobalreg_str(sd, num, str); } - } - else - {// integer variable - int val = (int)__64BPTRSIZE(value); - if(script->str_data[num&0x00ffffff].type == C_PARAM) - { - if( pc->setparam(sd, script->str_data[num&0x00ffffff].val, val) == 0 ) - { - if( st != NULL ) - { + } else {// integer variable + // FIXME: This isn't safe, in 32bits systems we're converting a 64bit pointer + // to a 32bit int, this will lead to overflows! [Panikon] + int val = (int)h64BPTRSIZE(value); + + if(script->str_data[script_getvarid(num)].type == C_PARAM) { + if( pc->setparam(sd, script->str_data[script_getvarid(num)].val, val) == 0 ) { + if( st != NULL ) { ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val); - script_reportsrc(st); - st->state = END; + script->reportsrc(st); + // Instead of just stop the script execution we let the character close + // the window if it was open. + st->state = (sd->state.dialog) ? CLOSE : END; + if( st->state == CLOSE ) + clif->scriptclose(sd, st->oid); } return 0; } @@ -2464,46 +2936,59 @@ static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* nam } switch (prefix) { - case '@': - return pc->setreg(sd, num, val); - case '$': - return mapreg_setreg(num, val); - case '#': - return (name[1] == '#') ? - pc_setaccountreg2(sd, name, val) : - pc_setaccountreg(sd, name, val); - case '.': - { - struct DBMap* n; - n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars; - if( n ) { - idb_remove(n, num); - if( val != 0 ) - idb_iput(n, num, val); + case '@': + pc->setreg(sd, num, val); + return 1; + case '$': + return mapreg->setreg(num, val); + case '#': + return (name[1] == '#') ? + pc_setaccountreg2(sd, num, val) : + pc_setaccountreg(sd, num, val); + case '.': + { + struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local; + if( n ) { + if( val != 0 ) { + i64db_iput(n->vars, num, val); + if( script_getvaridx(num) ) + script->array_update(n, num, false); + } else { + i64db_remove(n->vars, num); + if( script_getvaridx(num) ) + script->array_update(n, num, true); + } + } } - } - return 1; - case '\'': - if( st->instance_id >= 0 ) { - idb_remove(instances[st->instance_id].vars, num); - if( val != 0 ) - idb_iput(instances[st->instance_id].vars, num, val); - } - return 1; - default: - return pc_setglobalreg(sd, name, val); + return 1; + case '\'': + if( st->instance_id >= 0 ) { + if( val != 0 ) { + i64db_iput(instance->list[st->instance_id].regs.vars, num, val); + if( script_getvaridx(num) ) + script->array_update(&instance->list[st->instance_id].regs, num, false); + } else { + i64db_remove(instance->list[st->instance_id].regs.vars, num); + if( script_getvaridx(num) ) + script->array_update(&instance->list[st->instance_id].regs, num, true); + } + } else { + ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name); + script_reportsrc(st); + } + return 1; + default: + return pc_setglobalreg(sd, num, val); } } } -int set_var(TBL_PC* sd, char* name, void* val) -{ - return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL); +int set_var(TBL_PC* sd, char* name, void* val) { + return script->set_reg(NULL, sd, reference_uid(script->add_str(name),0), name, val, NULL); } -void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct DBMap **ref) -{ - set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref); +void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct reg_db *ref) { + script->set_reg(st, sd, reference_uid(script->add_str(varname),elem), varname, value, ref); } /// Converts the data to a string @@ -2518,7 +3003,7 @@ const char* conv_str(struct script_state* st, struct script_data* data) else if( data_isint(data) ) {// int -> string CREATE(p, char, ITEM_NAME_LENGTH); - snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num); + snprintf(p, ITEM_NAME_LENGTH, "%"PRId64"", data->u.num); p[ITEM_NAME_LENGTH-1] = '\0'; data->type = C_STR; data->u.str = p; @@ -2532,8 +3017,8 @@ const char* conv_str(struct script_state* st, struct script_data* data) else {// unsupported data type ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n"); - script_reportdata(data); - script_reportsrc(st); + script->reportdata(data); + script->reportsrc(st); data->type = C_CONSTSTR; data->u.str = ""; } @@ -2572,8 +3057,8 @@ int conv_num(struct script_state* st, struct script_data* data) { num = INT_MAX; ShowError("script:conv_num: overflow detected, capping to %ld\n", num); } - script_reportdata(data); - script_reportsrc(st); + script->reportdata(data); + script->reportsrc(st); } if( data->type == C_STR ) aFree(p); @@ -2586,13 +3071,13 @@ int conv_num(struct script_state* st, struct script_data* data) { else {// unsupported data type ShowError("script:conv_num: cannot convert to number, defaulting to 0\n"); - script_reportdata(data); - script_reportsrc(st); + script->reportdata(data); + script->reportsrc(st); data->type = C_INT; data->u.num = 0; } #endif - return data->u.num; + return (int)data->u.num; } // @@ -2609,9 +3094,9 @@ void stack_expand(struct script_stack* stack) { } /// Pushes a value into the stack (with reference) -struct script_data* push_val(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref) { +struct script_data* push_val(struct script_stack* stack, enum c_op type, int64 val, struct reg_db *ref) { if( stack->sp >= stack->sp_max ) - stack_expand(stack); + script->stack_expand(stack); stack->stack_data[stack->sp].type = type; stack->stack_data[stack->sp].u.num = val; stack->stack_data[stack->sp].ref = ref; @@ -2623,7 +3108,7 @@ struct script_data* push_val(struct script_stack* stack, enum c_op type, int val struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str) { if( stack->sp >= stack->sp_max ) - stack_expand(stack); + script->stack_expand(stack); stack->stack_data[stack->sp].type = type; stack->stack_data[stack->sp].u.str = str; stack->stack_data[stack->sp].ref = NULL; @@ -2632,10 +3117,9 @@ struct script_data* push_str(struct script_stack* stack, enum c_op type, char* s } /// Pushes a retinfo into the stack -struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, DBMap **ref) -{ +struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, struct reg_db *ref) { if( stack->sp >= stack->sp_max ) - stack_expand(stack); + script->stack_expand(stack); stack->stack_data[stack->sp].type = C_RETINFO; stack->stack_data[stack->sp].u.ri = ri; stack->stack_data[stack->sp].ref = ref; @@ -2668,8 +3152,7 @@ struct script_data* push_copy(struct script_stack* stack, int pos) { /// Removes the values in indexes [start,end[ from the stack. /// Adjusts all stack pointers. -void pop_stack(struct script_state* st, int start, int end) -{ +void pop_stack(struct script_state* st, int start, int end) { struct script_stack* stack = st->stack; struct script_data* data; int i; @@ -2690,8 +3173,18 @@ void pop_stack(struct script_state* st, int start, int end) if( data->type == C_RETINFO ) { struct script_retinfo* ri = data->u.ri; - if( ri->var_function ) - script_free_vars(ri->var_function); + if( ri->scope.vars ) { + // Note: This is necessary evern if we're also doing it in run_func + // (in the RETFUNC block) because not all functions return. If a + // function (or a sub) has an 'end' or a 'close', it'll reach this + // block with its scope vars still to be freed. + script->free_vars(ri->scope.vars); + ri->scope.vars = NULL; + } + if( ri->scope.arrays ) { + ri->scope.arrays->destroy(ri->scope.arrays,script->array_free_db); + ri->scope.arrays = NULL; + } if( data->ref ) aFree(data->ref); aFree(ri); @@ -2706,12 +3199,18 @@ void pop_stack(struct script_state* st, int start, int end) stack->stack_data[i].type = C_NOP; } // adjust stack pointers - if( st->start > end ) st->start -= end - start; - else if( st->start > start ) st->start = start; - if( st->end > end ) st->end -= end - start; - else if( st->end > start ) st->end = start; - if( stack->defsp > end ) stack->defsp -= end - start; - else if( stack->defsp > start ) stack->defsp = start; + if( st->start > end ) + st->start -= end - start; + else if( st->start > start ) + st->start = start; + if( st->end > end ) + st->end -= end - start; + else if( st->end > start ) + st->end = start; + if( stack->defsp > end ) + stack->defsp -= end - start; + else if( stack->defsp > start ) + stack->defsp = start; stack->sp -= end - start; } @@ -2722,17 +3221,24 @@ void pop_stack(struct script_state* st, int start, int end) /*========================================== * Release script dependent variable, dependent variable of function *------------------------------------------*/ -void script_free_vars(struct DBMap* storage) -{ - if( storage ) - {// destroy the storage construct containing the variables - db_destroy(storage); +void script_free_vars(struct DBMap* var_storage) { + if( var_storage ) { + // destroy the storage construct containing the variables + db_destroy(var_storage); } } void script_free_code(struct script_code* code) { - script_free_vars( code->script_vars ); + nullpo_retv(code); + + if( code->instances ) + script->stop_instances(code); + else { + script->free_vars(code->local.vars); + if( code->local.arrays ) + code->local.arrays->destroy(code->local.arrays,script->array_free_db); + } aFree( code->script_buf ); aFree( code ); } @@ -2746,14 +3252,17 @@ void script_free_code(struct script_code* code) /// @return Script state struct script_state* script_alloc_state(struct script_code* rootscript, int pos, int rid, int oid) { struct script_state* st; - + st = ers_alloc(script->st_ers, struct script_state); st->stack = ers_alloc(script->stack_ers, struct script_stack); + st->pending_refs = NULL; + st->pending_ref_count = 0; st->stack->sp = 0; st->stack->sp_max = 64; CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max); st->stack->defsp = st->stack->sp; - st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA); + st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA); + st->stack->scope.arrays = NULL; st->state = RUN; st->script = rootscript; st->pos = pos; @@ -2762,91 +3271,125 @@ struct script_state* script_alloc_state(struct script_code* rootscript, int pos, st->sleep.timer = INVALID_TIMER; st->npc_item_flag = battle_config.item_enabled_npc; - if( !st->script->script_vars ) - st->script->script_vars = idb_alloc(DB_OPT_RELEASE_DATA); - + if( st->script->instances != USHRT_MAX ) + st->script->instances++; + else { + struct npc_data *nd = map->id2nd(oid); + ShowError("over 65k instances of '%s' script are being run\n",nd ? nd->name : "unknown"); + } + + if( !st->script->local.vars ) + st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA); + st->id = script->next_id++; script->active_scripts++; idb_put(script->st_db, st->id, st); - + return st; } /// Frees a script state. /// /// @param st Script state -void script_free_state(struct script_state* st) -{ - if(st->bk_st) {// backup was not restored - ShowDebug("script_free_state: Previous script state lost (rid=%d, oid=%d, state=%d, bk_npcid=%d).\n", st->bk_st->rid, st->bk_st->oid, st->bk_st->state, st->bk_npcid); - } +void script_free_state(struct script_state* st) { + if( idb_exists(script->st_db,st->id) ) { + struct map_session_data *sd = st->rid ? map->id2sd(st->rid) : NULL; + + if(st->bk_st) {// backup was not restored + ShowDebug("script_free_state: Previous script state lost (rid=%d, oid=%d, state=%d, bk_npcid=%d).\n", st->bk_st->rid, st->bk_st->oid, st->bk_st->state, st->bk_npcid); + } + + if(sd && sd->st == st) { //Current script is aborted. + if(sd->state.using_fake_npc){ + clif->clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd); + sd->state.using_fake_npc = 0; + } + sd->st = NULL; + sd->npc_id = 0; + } - if( st->sleep.timer != INVALID_TIMER ) - iTimer->delete_timer(st->sleep.timer, run_script_timer); - script_free_vars(st->stack->var_function); - script->pop_stack(st, 0, st->stack->sp); - aFree(st->stack->stack_data); - ers_free(script->stack_ers, st->stack); - if( st->script && st->script->script_vars && !db_size(st->script->script_vars) ) { - script_free_vars(st->script->script_vars); - st->script->script_vars = NULL; - } - st->stack = NULL; - st->pos = -1; - idb_remove(script->st_db, st->id); - ers_free(script->st_ers, st); - if( --script->active_scripts == 0 ) { - script->next_id = 0; + if( st->sleep.timer != INVALID_TIMER ) + timer->delete(st->sleep.timer, script->run_timer); + if( st->stack ) { + script->free_vars(st->stack->scope.vars); + if( st->stack->scope.arrays ) + st->stack->scope.arrays->destroy(st->stack->scope.arrays,script->array_free_db); + script->pop_stack(st, 0, st->stack->sp); + aFree(st->stack->stack_data); + ers_free(script->stack_ers, st->stack); + st->stack = NULL; + } + if( st->script && st->script->instances != USHRT_MAX && --st->script->instances == 0 ) { + if( st->script->local.vars && !db_size(st->script->local.vars) ) { + script->free_vars(st->script->local.vars); + st->script->local.vars = NULL; + } + if( st->script->local.arrays && !db_size(st->script->local.arrays) ) { + st->script->local.arrays->destroy(st->script->local.arrays,script->array_free_db); + st->script->local.arrays = NULL; + } + } + st->pos = -1; + if (st->pending_ref_count > 0) { + while (st->pending_ref_count > 0) + aFree(st->pending_refs[--st->pending_ref_count]); + aFree(st->pending_refs); + st->pending_refs = NULL; + } + idb_remove(script->st_db, st->id); + ers_free(script->st_ers, st); + if( --script->active_scripts == 0 ) { + script->next_id = 0; + } } } +/** + * Adds a pending reference entry to the current script. + * + * @see struct script_state::pending_refs + * + * @param st[in] Script state. + * @param ref[in] Reference to be added. + */ +void script_add_pending_ref(struct script_state *st, struct reg_db *ref) { + RECREATE(st->pending_refs, struct reg_db*, ++st->pending_ref_count); + st->pending_refs[st->pending_ref_count-1] = ref; +} + // // Main execution unit // /*========================================== * Read command *------------------------------------------*/ -c_op get_com(unsigned char *script,int *pos) +c_op get_com(unsigned char *scriptbuf,int *pos) { int i = 0, j = 0; - if(script[*pos]>=0x80){ + if(scriptbuf[*pos]>=0x80) { return C_INT; } - while(script[*pos]>=0x40){ - i=script[(*pos)++]<<j; + while(scriptbuf[*pos]>=0x40) { + i=scriptbuf[(*pos)++]<<j; j+=6; } - return (c_op)(i+(script[(*pos)++]<<j)); + return (c_op)(i+(scriptbuf[(*pos)++]<<j)); } /*========================================== * Income figures *------------------------------------------*/ -int get_num(unsigned char *script,int *pos) +int get_num(unsigned char *scriptbuf,int *pos) { int i,j; i=0; j=0; - while(script[*pos]>=0xc0){ - i+=(script[(*pos)++]&0x7f)<<j; + while(scriptbuf[*pos]>=0xc0) { + i+=(scriptbuf[(*pos)++]&0x7f)<<j; j+=6; } - return i+((script[(*pos)++]&0x7f)<<j); -} - -/*========================================== - * Remove the value from the stack - *------------------------------------------*/ -int pop_val(struct script_state* st) -{ - if(st->stack->sp<=0) - return 0; - st->stack->sp--; - script->get_val(st,&(st->stack->stack_data[st->stack->sp])); - if(st->stack->stack_data[st->stack->sp].type==C_INT) - return st->stack->stack_data[st->stack->sp].u.num; - return 0; + return i+((scriptbuf[(*pos)++]&0x7f)<<j); } /// Ternary operators @@ -2862,12 +3405,12 @@ void op_3(struct script_state* st, int op) if( data_isstring(data) ) flag = data->u.str[0];// "" -> false else if( data_isint(data) ) - flag = data->u.num;// 0 -> false + flag = data->u.num == 0 ? 0 : 1;// 0 -> false else { ShowError("script:op_3: invalid data for the ternary operator test\n"); - script_reportdata(data); - script_reportsrc(st); + script->reportdata(data); + script->reportsrc(st); script_removetop(st, -3, 0); script_pushnil(st); return; @@ -2886,18 +3429,86 @@ void op_3(struct script_state* st, int op) /// s1 GE s2 -> i /// s1 LT s2 -> i /// s1 LE s2 -> i +/// s1 RE_EQ s2 -> i +/// s1 RE_NE s2 -> i /// s1 ADD s2 -> s void op_2str(struct script_state* st, int op, const char* s1, const char* s2) { int a = 0; - switch(op){ + switch(op) { case C_EQ: a = (strcmp(s1,s2) == 0); break; case C_NE: a = (strcmp(s1,s2) != 0); break; case C_GT: a = (strcmp(s1,s2) > 0); break; case C_GE: a = (strcmp(s1,s2) >= 0); break; case C_LT: a = (strcmp(s1,s2) < 0); break; case C_LE: a = (strcmp(s1,s2) <= 0); break; +#ifdef PCRE_SUPPORT + case C_RE_EQ: + case C_RE_NE: + { + int inputlen = (int)strlen(s1); + pcre *compiled_regex; + pcre_extra *extra_regex; + const char *pcre_error, *pcre_match; + int pcre_erroroffset, offsetcount, i; + int offsets[256*3]; // (max_capturing_groups+1)*3 + + compiled_regex = libpcre->compile(s2, 0, &pcre_error, &pcre_erroroffset, NULL); + + if( compiled_regex == NULL ) { + ShowError("script:op2_str: Invalid regex '%s'.\n", s2); + script->reportsrc(st); + script_pushnil(st); + st->state = END; + return; + } + + extra_regex = libpcre->study(compiled_regex, 0, &pcre_error); + + if( pcre_error != NULL ) { + libpcre->free(compiled_regex); + ShowError("script:op2_str: Unable to optimize the regex '%s': %s\n", s2, pcre_error); + script->reportsrc(st); + script_pushnil(st); + st->state = END; + return; + } + + offsetcount = libpcre->exec(compiled_regex, extra_regex, s1, inputlen, 0, 0, offsets, 256*3); + + if( offsetcount == 0 ) { + offsetcount = 256; + } else if( offsetcount == PCRE_ERROR_NOMATCH ) { + offsetcount = 0; + } else if( offsetcount < 0 ) { + libpcre->free(compiled_regex); + if( extra_regex != NULL ) + libpcre->free(extra_regex); + ShowWarning("script:op2_str: Unable to process the regex '%s'.\n", s2); + script->reportsrc(st); + script_pushnil(st); + st->state = END; + return; + } + + if( op == C_RE_EQ ) { + for( i = 0; i < offsetcount; i++ ) { + libpcre->get_substring(s1, offsets, offsetcount, i, &pcre_match); + mapreg->setregstr(reference_uid(script->add_str("$@regexmatch$"), i), pcre_match); + libpcre->free_substring(pcre_match); + } + mapreg->setreg(script->add_str("$@regexmatchcount"), i); + a = offsetcount; + } else { // C_RE_NE + a = (offsetcount == 0); + } + libpcre->free(compiled_regex); + if( extra_regex != NULL ) + libpcre->free(extra_regex); + } + break; +#endif // PCRE_SUPPORT case C_ADD: { char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char)); @@ -2907,8 +3518,8 @@ void op_2str(struct script_state* st, int op, const char* s1, const char* s2) return; } default: - ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op)); - script_reportsrc(st); + ShowError("script:op2_str: unexpected string operator %s\n", script->op2name(op)); + script->reportsrc(st); script_pushnil(st); st->state = END; return; @@ -2924,27 +3535,26 @@ void op_2num(struct script_state* st, int op, int i1, int i2) int ret; double ret_double; - switch( op ) - { - case C_AND: ret = i1 & i2; break; - case C_OR: ret = i1 | i2; break; - case C_XOR: ret = i1 ^ i2; break; - case C_LAND: ret = (i1 && i2); break; - case C_LOR: ret = (i1 || i2); break; - case C_EQ: ret = (i1 == i2); break; - case C_NE: ret = (i1 != i2); break; - case C_GT: ret = (i1 > i2); break; - case C_GE: ret = (i1 >= i2); break; - case C_LT: ret = (i1 < i2); break; - case C_LE: ret = (i1 <= i2); break; - case C_R_SHIFT: ret = i1>>i2; break; - case C_L_SHIFT: ret = i1<<i2; break; + switch( op ) { + case C_AND: ret = i1 & i2; break; + case C_OR: ret = i1 | i2; break; + case C_XOR: ret = i1 ^ i2; break; + case C_LAND: ret = (i1 && i2); break; + case C_LOR: ret = (i1 || i2); break; + case C_EQ: ret = (i1 == i2); break; + case C_NE: ret = (i1 != i2); break; + case C_GT: ret = (i1 > i2); break; + case C_GE: ret = (i1 >= i2); break; + case C_LT: ret = (i1 < i2); break; + case C_LE: ret = (i1 <= i2); break; + case C_R_SHIFT: ret = i1>>i2; break; + case C_L_SHIFT: ret = i1<<i2; break; case C_DIV: case C_MOD: if( i2 == 0 ) { - ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2); - script_reportsrc(st); + ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script->op2name(op), i1, i2); + script->reportsrc(st); script_pushnil(st); st->state = END; return; @@ -2961,21 +3571,21 @@ void op_2num(struct script_state* st, int op, int i1, int i2) case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break; case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break; default: - ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2); - script_reportsrc(st); + ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script->op2name(op), i1, i2); + script->reportsrc(st); script_pushnil(st); return; } if( ret_double < (double)INT_MIN ) { - ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2); - script_reportsrc(st); + ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script->op2name(op), i1, i2); + script->reportsrc(st); ret = INT_MIN; } else if( ret_double > (double)INT_MAX ) { - ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2); - script_reportsrc(st); + ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script->op2name(op), i1, i2); + script->reportsrc(st); ret = INT_MAX; } } @@ -3021,32 +3631,33 @@ void op_2(struct script_state *st, int op) if( data_isstring(left) && data_isstring(right) ) {// ss => op_2str - op_2str(st, op, left->u.str, right->u.str); + script->op_2str(st, op, left->u.str, right->u.str); script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one if (leftref.type != C_NOP) { - aFree(left->u.str); + if (left->type == C_STR) // don't free C_CONSTSTR + aFree(left->u.str); *left = leftref; } } else if( data_isint(left) && data_isint(right) ) {// ii => op_2num - int i1 = left->u.num; - int i2 = right->u.num; + int i1 = (int)left->u.num; + int i2 = (int)right->u.num; script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0); - op_2num(st, op, i1, i2); + script->op_2num(st, op, i1, i2); if (leftref.type != C_NOP) *left = leftref; } else {// invalid argument - ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op)); - script_reportdata(left); - script_reportdata(right); - script_reportsrc(st); + ShowError("script:op_2: invalid data for operator %s\n", script->op2name(op)); + script->reportdata(left); + script->reportdata(right); + script->reportsrc(st); script_removetop(st, -2, 0); script_pushnil(st); st->state = END; @@ -3067,27 +3678,26 @@ void op_1(struct script_state* st, int op) if( !data_isint(data) ) {// not a number - ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op)); - script_reportdata(data); - script_reportsrc(st); + ShowError("script:op_1: argument is not a number (op=%s)\n", script->op2name(op)); + script->reportdata(data); + script->reportsrc(st); script_pushnil(st); st->state = END; return; } - i1 = data->u.num; + i1 = (int)data->u.num; script_removetop(st, -1, 0); - switch( op ) - { - case C_NEG: i1 = -i1; break; - case C_NOT: i1 = ~i1; break; - case C_LNOT: i1 = !i1; break; - default: - ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1); - script_reportsrc(st); - script_pushnil(st); - st->state = END; - return; + switch( op ) { + case C_NEG: i1 = -i1; break; + case C_NOT: i1 = ~i1; break; + case C_LNOT: i1 = !i1; break; + default: + ShowError("script:op_1: unexpected operator %s i1=%d\n", script->op2name(op), i1); + script->reportsrc(st); + script_pushnil(st); + st->state = END; + return; } script_pushint(st, i1); } @@ -3097,7 +3707,7 @@ void op_1(struct script_state* st, int op) /// /// @param st Script state whose stack arguments should be inspected. /// @param func Built-in function for which the arguments are intended. -static void script_check_buildin_argtype(struct script_state* st, int func) +void script_check_buildin_argtype(struct script_state* st, int func) { char type; int idx, invalid = 0; @@ -3107,11 +3717,11 @@ static void script_check_buildin_argtype(struct script_state* st, int func) struct script_data* data = script_getdata(st, idx); type = sf[idx-2]; - + if( type == '?' || type == '*' ) {// optional argument or unknown number of optional parameters ( no types are after this ) break; } else if( type == 0 ) {// more arguments than necessary ( should not happen, as it is checked before ) - ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type)); + ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script->op2name(data->type)); invalid++; break; } else { @@ -3127,7 +3737,7 @@ static void script_check_buildin_argtype(struct script_state* st, int func) if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) ) {// variant ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1); - script_reportdata(data); + script->reportdata(data); invalid++; } break; @@ -3135,7 +3745,7 @@ static void script_check_buildin_argtype(struct script_state* st, int func) if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) ) {// string ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1); - script_reportdata(data); + script->reportdata(data); invalid++; } break; @@ -3143,23 +3753,23 @@ static void script_check_buildin_argtype(struct script_state* st, int func) if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) ) {// int ( params and constants are always int ) ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1); - script_reportdata(data); + script->reportdata(data); invalid++; } break; case 'r': - if( !data_isreference(data) ) + if( !data_isreference(data) || reference_toconstant(data) ) {// variables - ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type)); - script_reportdata(data); + ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script->op2name(data->type)); + script->reportdata(data); invalid++; } break; case 'l': if( !data_islabel(data) && !data_isfunclabel(data) ) {// label - ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type)); - script_reportdata(data); + ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script->op2name(data->type)); + script->reportdata(data); invalid++; } break; @@ -3168,8 +3778,8 @@ static void script_check_buildin_argtype(struct script_state* st, int func) } if(invalid) { - ShowDebug("Function: %s\n", get_str(func)); - script_reportsrc(st); + ShowDebug("Function: %s\n", script->get_str(func)); + script->reportsrc(st); } } @@ -3189,7 +3799,7 @@ int run_func(struct script_state *st) { ShowError("script:run_func: C_ARG not found. please report this!!!\n"); st->state = END; - script_reportsrc(st); + script->reportsrc(st); return 1; } start_sp = i-1;// C_NAME of the command @@ -3198,27 +3808,27 @@ int run_func(struct script_state *st) data = &st->stack->stack_data[st->start]; if( data->type == C_NAME && script->str_data[data->u.num].type == C_FUNC ) - func = data->u.num; + func = (int)data->u.num; else { ShowError("script:run_func: not a buildin command.\n"); - script_reportdata(data); - script_reportsrc(st); + script->reportdata(data); + script->reportsrc(st); st->state = END; return 1; } - if( script_config.warn_func_mismatch_argtypes ) - { - script_check_buildin_argtype(st, func); + if( script->config.warn_func_mismatch_argtypes ) { + script->check_buildin_argtype(st, func); } - if(script->str_data[func].func){ + if(script->str_data[func].func) { if (!(script->str_data[func].func(st))) //Report error - script_reportsrc(st); + script->reportsrc(st); } else { - ShowError("script:run_func: '%s' (id=%d type=%s) has no C function. please report this!!!\n", get_str(func), func, script_op2name(script->str_data[func].type)); - script_reportsrc(st); + ShowError("script:run_func: '%s' (id=%d type=%s) has no C function. please report this!!!\n", + script->get_str(func), func, script->op2name(script->str_data[func].type)); + script->reportsrc(st); st->state = END; } @@ -3237,17 +3847,19 @@ int run_func(struct script_state *st) if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO ) { ShowWarning("script:run_func: return without callfunc or callsub!\n"); - script_reportsrc(st); + script->reportsrc(st); st->state = END; return 1; } - script_free_vars( st->stack->var_function ); + script->free_vars(st->stack->scope.vars); + st->stack->scope.arrays->destroy(st->stack->scope.arrays,script->array_free_db); ri = st->stack->stack_data[st->stack->defsp-1].u.ri; nargs = ri->nargs; st->pos = ri->pos; st->script = ri->script; - st->stack->var_function = ri->var_function; + st->stack->scope.vars = ri->scope.vars; + st->stack->scope.arrays = ri->scope.arrays; st->stack->defsp = ri->defsp; memset(ri, 0, sizeof(struct script_retinfo)); @@ -3267,48 +3879,50 @@ void run_script(struct script_code *rootscript,int pos,int rid,int oid) { if( rootscript == NULL || pos < 0 ) return; - + // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS] // It is unclear how that can be triggered, so it needs the be traced/checked in more detail. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set. - st = script_alloc_state(rootscript, pos, rid, oid); - - run_script_main(st); + st = script->alloc_state(rootscript, pos, rid, oid); + + script->run_main(st); } -void script_stop_instances(int id) { +void script_stop_instances(struct script_code *code) { DBIterator *iter; struct script_state* st; - + if( !script->active_scripts ) return;//dont even bother. - + iter = db_iterator(script->st_db); - + for( st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter) ) { - if( st->oid == id ) { - script_free_state(st); + if( st->script == code ) { + script->free_state(st); } } - + dbi_destroy(iter); } /*========================================== * Timer function for sleep *------------------------------------------*/ -int run_script_timer(int tid, unsigned int tick, int id, intptr_t data) { - struct script_state *st = (struct script_state *)data; - TBL_PC *sd = iMap->id2sd(st->rid); +int run_script_timer(int tid, int64 tick, int id, intptr_t data) { + struct script_state *st = idb_get(script->st_db,(int)data); + if( st ) { + TBL_PC *sd = map->id2sd(st->rid); - if((sd && sd->status.char_id != id) || (st->rid && !sd)) { //Character mismatch. Cancel execution. - st->rid = 0; - st->state = END; + if((sd && sd->status.char_id != id) || (st->rid && !sd)) { //Character mismatch. Cancel execution. + st->rid = 0; + st->state = END; + } + st->sleep.timer = INVALID_TIMER; + if(st->state != RERUNLINE) + st->sleep.tick = 0; + script->run_main(st); } - st->sleep.timer = INVALID_TIMER; - if(st->state != RERUNLINE) - st->sleep.tick = 0; - run_script_main(st); return 0; } @@ -3316,11 +3930,10 @@ int run_script_timer(int tid, unsigned int tick, int id, intptr_t data) { /// /// @param st Script state to detach. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script. -static void script_detach_state(struct script_state* st, bool dequeue_event) -{ +void script_detach_state(struct script_state* st, bool dequeue_event) { struct map_session_data* sd; - if(st->rid && (sd = iMap->id2sd(st->rid))!=NULL) { + if(st->rid && (sd = map->id2sd(st->rid))!=NULL) { sd->st = st->bk_st; sd->npc_id = st->bk_npcid; sd->state.dialog = 0; @@ -3329,25 +3942,21 @@ static void script_detach_state(struct script_state* st, bool dequeue_event) st->bk_st = NULL; st->bk_npcid = 0; } else if(dequeue_event) { - /** - * For the Secure NPC Timeout option (check config/Secure.h) [RR] - **/ + // For the Secure NPC Timeout option (check config/Secure.h) [RR] #ifdef SECURE_NPCTIMEOUT - /** - * We're done with this NPC session, so we cancel the timer (if existent) and move on - **/ + // We're done with this NPC session, so we cancel the timer (if existent) and move on if( sd->npc_idle_timer != INVALID_TIMER ) { - iTimer->delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer); + timer->delete(sd->npc_idle_timer,npc->secure_timeout_timer); sd->npc_idle_timer = INVALID_TIMER; } #endif - npc_event_dequeue(sd); + npc->event_dequeue(sd); } } else if(st->bk_st) { // rid was set to 0, before detaching the script state ShowError("script_detach_state: Found previous script state without attached player (rid=%d, oid=%d, state=%d, bk_npcid=%d)\n", st->bk_st->rid, st->bk_st->oid, st->bk_st->state, st->bk_npcid); - script_reportsrc(st->bk_st); + script->reportsrc(st->bk_st); - script_free_state(st->bk_st); + script->free_state(st->bk_st); st->bk_st = NULL; } } @@ -3355,11 +3964,10 @@ static void script_detach_state(struct script_state* st, bool dequeue_event) /// Attaches script state to possibly attached character and backups it's previous script, if any. /// /// @param st Script state to attach. -static void script_attach_state(struct script_state* st) -{ +void script_attach_state(struct script_state* st) { struct map_session_data* sd; - if(st->rid && (sd = iMap->id2sd(st->rid))!=NULL) + if(st->rid && (sd = map->id2sd(st->rid))!=NULL) { if(st!=sd->st) { @@ -3378,8 +3986,8 @@ static void script_attach_state(struct script_state* st) **/ #ifdef SECURE_NPCTIMEOUT if( sd->npc_idle_timer == INVALID_TIMER ) - sd->npc_idle_timer = iTimer->add_timer(iTimer->gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0); - sd->npc_idle_tick = iTimer->gettick(); + sd->npc_idle_timer = timer->add(timer->gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc->secure_timeout_timer,sd->bl.id,0); + sd->npc_idle_tick = timer->gettick(); #endif } } @@ -3387,32 +3995,31 @@ static void script_attach_state(struct script_state* st) /*========================================== * The main part of the script execution *------------------------------------------*/ -void run_script_main(struct script_state *st) -{ - int cmdcount = script_config.check_cmdcount; - int gotocount = script_config.check_gotocount; +void run_script_main(struct script_state *st) { + int cmdcount = script->config.check_cmdcount; + int gotocount = script->config.check_gotocount; TBL_PC *sd; struct script_stack *stack = st->stack; struct npc_data *nd; - script_attach_state(st); + script->attach_state(st); - nd = iMap->id2nd(st->oid); + nd = map->id2nd(st->oid); if( nd && nd->bl.m >= 0 ) - st->instance_id = map[nd->bl.m].instance_id; + st->instance_id = map->list[nd->bl.m].instance_id; else st->instance_id = -1; if(st->state == RERUNLINE) { - run_func(st); + script->run_func(st); if(st->state == GOTO) st->state = RUN; } else if(st->state != END) st->state = RUN; - while( st->state == RUN ){ - enum c_op c = get_com(st->script->script_buf,&st->pos); - switch(c){ + while( st->state == RUN ) { + enum c_op c = script->get_com(st->script->script_buf,&st->pos); + switch(c) { case C_EOL: if( stack->defsp > stack->sp ) ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp); @@ -3420,7 +4027,7 @@ void run_script_main(struct script_state *st) script->pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value) break; case C_INT: - script->push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos),NULL); + script->push_val(stack,C_INT,script->get_num(st->script->script_buf,&st->pos),NULL); break; case C_POS: case C_NAME: @@ -3435,12 +4042,12 @@ void run_script_main(struct script_state *st) while(st->script->script_buf[st->pos++]); break; case C_FUNC: - run_func(st); - if(st->state==GOTO){ + script->run_func(st); + if(st->state==GOTO) { st->state = RUN; - if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){ + if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ) { ShowError("run_script: infinity loop !\n"); - script_reportsrc(st); + script->reportsrc(st); st->state=END; } } @@ -3453,7 +4060,7 @@ void run_script_main(struct script_state *st) case C_NEG: case C_NOT: case C_LNOT: - op_1(st ,c); + script->op_1(st ,c); break; case C_ADD: @@ -3474,11 +4081,15 @@ void run_script_main(struct script_state *st) case C_LOR: case C_R_SHIFT: case C_L_SHIFT: - op_2(st, c); +#ifdef PCRE_SUPPORT + case C_RE_EQ: + case C_RE_NE: +#endif // PCRE_SUPPORT + script->op_2(st, c); break; case C_OP3: - op_3(st, c); + script->op_3(st, c); break; case C_NOP: @@ -3490,93 +4101,88 @@ void run_script_main(struct script_state *st) st->state=END; break; } - if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){ - ShowError("run_script: infinity loop !\n"); - script_reportsrc(st); + if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ) { + ShowError("run_script: too many opeartions being processed non-stop !\n"); + script->reportsrc(st); st->state=END; } } if(st->sleep.tick > 0) { //Restore previous script - script_detach_state(st, false); + script->detach_state(st, false); //Delay execution - sd = iMap->id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish] + sd = map->id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish] st->sleep.charid = sd?sd->status.char_id:0; - st->sleep.timer = iTimer->add_timer(iTimer->gettick()+st->sleep.tick, - run_script_timer, st->sleep.charid, (intptr_t)st); - } else if(st->state != END && st->rid){ + st->sleep.timer = timer->add(timer->gettick()+st->sleep.tick, + script->run_timer, st->sleep.charid, (intptr_t)st->id); + } else if(st->state != END && st->rid) { //Resume later (st is already attached to player). if(st->bk_st) { ShowWarning("Unable to restore stack! Double continuation!\n"); //Report BOTH scripts to see if that can help somehow. ShowDebug("Previous script (lost):\n"); - script_reportsrc(st->bk_st); + script->reportsrc(st->bk_st); ShowDebug("Current script:\n"); - script_reportsrc(st); + script->reportsrc(st); - script_free_state(st->bk_st); + script->free_state(st->bk_st); st->bk_st = NULL; } } else { //Dispose of script. - if ((sd = iMap->id2sd(st->rid))!=NULL) { //Restore previous stack and save char. - if(sd->state.using_fake_npc){ + if ((sd = map->id2sd(st->rid))!=NULL) { //Restore previous stack and save char. + if(sd->state.using_fake_npc) { clif->clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd); sd->state.using_fake_npc = 0; } //Restore previous script if any. - script_detach_state(st, true); - if (sd->state.reg_dirty&2) - intif_saveregistry(sd,2); - if (sd->state.reg_dirty&1) - intif_saveregistry(sd,1); + script->detach_state(st, true); + if (sd->vars_dirty) + intif->saveregistry(sd); } - script_free_state(st); + script->free_state(st); st = NULL; } } -int script_config_read(char *cfgName) -{ +int script_config_read(char *cfgName) { int i; char line[1024],w1[1024],w2[1024]; FILE *fp; - fp=fopen(cfgName,"r"); - if(fp==NULL){ + if( !( fp = fopen(cfgName,"r") ) ) { ShowError("File not found: %s\n", cfgName); return 1; } - while(fgets(line, sizeof(line), fp)) - { - if(line[0] == '/' && line[1] == '/') + while (fgets(line, sizeof(line), fp)) { + if (line[0] == '/' && line[1] == '/') continue; - i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2); + i = sscanf(line,"%1023[^:]: %1023[^\r\n]", w1, w2); if(i!=2) continue; if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) { - script_config.warn_func_mismatch_paramnum = config_switch(w2); + script->config.warn_func_mismatch_paramnum = config_switch(w2); } else if(strcmpi(w1,"check_cmdcount")==0) { - script_config.check_cmdcount = config_switch(w2); + script->config.check_cmdcount = config_switch(w2); } else if(strcmpi(w1,"check_gotocount")==0) { - script_config.check_gotocount = config_switch(w2); + script->config.check_gotocount = config_switch(w2); } else if(strcmpi(w1,"input_min_value")==0) { - script_config.input_min_value = config_switch(w2); + script->config.input_min_value = config_switch(w2); } else if(strcmpi(w1,"input_max_value")==0) { - script_config.input_max_value = config_switch(w2); + script->config.input_max_value = config_switch(w2); } else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) { - script_config.warn_func_mismatch_argtypes = config_switch(w2); + script->config.warn_func_mismatch_argtypes = config_switch(w2); } - else if(strcmpi(w1,"import")==0){ - script_config_read(w2); + else if(strcmpi(w1,"import")==0) { + script->config_read(w2); } else { ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName); @@ -3590,102 +4196,132 @@ int script_config_read(char *cfgName) /** * @see DBApply */ -static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap) +int db_script_free_code_sub(DBKey key, DBData *data, va_list ap) { struct script_code *code = DB->data2ptr(data); if (code) - script_free_code(code); + script->free_code(code); return 0; } void script_run_autobonus(const char *autobonus, int id, int pos) { - struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus); + struct script_code *scriptroot = (struct script_code *)strdb_get(script->autobonus_db, autobonus); - if( script ) - { - current_equip_item_index = pos; - run_script(script,0,id,0); + if( scriptroot ) { + status->current_equip_item_index = pos; + script->run(scriptroot,0,id,0); } } void script_add_autobonus(const char *autobonus) { - if( strdb_get(autobonus_db, autobonus) == NULL ) - { - struct script_code *script = parse_script(autobonus, "autobonus", 0, 0); + if( strdb_get(script->autobonus_db, autobonus) == NULL ) { + struct script_code *scriptroot = script->parse(autobonus, "autobonus", 0, 0, NULL); - if( script ) - strdb_put(autobonus_db, autobonus, script); + if( scriptroot ) + strdb_put(script->autobonus_db, autobonus, scriptroot); } } /// resets a temporary character array variable to given value -void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value) -{ +void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value) { + struct script_array *sa = NULL; + struct reg_db *src = NULL; + unsigned int i, *list = NULL, size = 0; int key; - uint8 idx; - if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) ) - { - ShowError("script_cleararray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id); + key = script->add_str(varname); + + if( !(src = script->array_src(NULL,sd,varname,NULL) ) ) return; - } - - key = add_str(varname); - - if( is_string_variable(varname) ) - { - for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ ) - { - pc->setregstr(sd, reference_uid(key, idx), (const char*)value); - } - } - else - { - for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ ) - { - pc->setreg(sd, reference_uid(key, idx), (int)__64BPTRSIZE(value)); - } + + if( value ) + script->array_ensure_zero(NULL,sd,reference_uid(key,0),NULL); + + if( !(sa = idb_get(src->arrays, key)) ) /* non-existent array, nothing to empty */ + return; + + size = sa->size; + list = script->array_cpy_list(sa); + + for(i = 0; i < size; i++) { + script->set_reg(NULL,sd,reference_uid(key, list[i]),varname,value,NULL); } } /// 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) -{ +void script_setarray_pc(struct map_session_data* sd, const char* varname, uint32 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); + + if( idx >= SCRIPT_MAX_ARRAYSIZE ) { + ShowError("script_setarray_pc: Variable '%s' has invalid index '%u' (char_id=%d).\n", varname, 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); + key = ( refcache && refcache[0] ) ? refcache[0] : script->add_str(varname); + + script->set_reg(NULL,sd,reference_uid(key, idx),varname,value,NULL); + + if( refcache ) + {// save to avoid repeated script->add_str calls + refcache[0] = key; } - else - { - pc->setreg(sd, reference_uid(key, idx), (int)__64BPTRSIZE(value)); +} +/** + * Clears persistent variables from memory + **/ +int script_reg_destroy(DBKey key, DBData *data, va_list ap) { + struct script_reg_state *src; + + if( data->type != DB_DATA_PTR )/* got no need for those! */ + return 0; + + src = DB->data2ptr(data); + + if( src->type ) { + struct script_reg_str *p = (struct script_reg_str *)src; + + if( p->value ) + aFree(p->value); + + ers_free(pc->str_reg_ers,p); + } else { + ers_free(pc->num_reg_ers,(struct script_reg_num*)src); } + + return 0; +} +/** + * Clears a single persistent variable + **/ +void script_reg_destroy_single(struct map_session_data *sd, int64 reg, struct script_reg_state *data) { + i64db_remove(sd->regs.vars, reg); - if( refcache ) - {// save to avoid repeated add_str calls - refcache[0] = key; + if( data->type ) { + struct script_reg_str *p = (struct script_reg_str*)data; + + if( p->value ) + aFree(p->value); + + ers_free(pc->str_reg_ers,p); + } else { + ers_free(pc->num_reg_ers,(struct script_reg_num*)data); } } +unsigned int *script_array_cpy_list(struct script_array *sa) { + if( sa->size > script->generic_ui_array_size ) + script->generic_ui_array_expand(sa->size); + memcpy(script->generic_ui_array, sa->members, sizeof(unsigned int)*sa->size); + return script->generic_ui_array; +} +void script_generic_ui_array_expand (unsigned int plus) { + script->generic_ui_array_size += plus + 100; + RECREATE(script->generic_ui_array, unsigned int, script->generic_ui_array_size); +} /*========================================== * Destructor *------------------------------------------*/ @@ -3693,8 +4329,8 @@ void do_final_script(void) { int i; DBIterator *iter; struct script_state *st; - -#ifdef DEBUG_HASH + +#ifdef SCRIPT_DEBUG_HASH if (battle_config.etc_log) { FILE *fp = fopen("hash_dump.txt","wt"); @@ -3711,8 +4347,8 @@ void do_final_script(void) { fprintf(fp,"num : hash : data_name\n"); fprintf(fp,"---------------------------------------------------------------\n"); for(i=LABEL_START; i<script->str_num; i++) { - unsigned int h = calc_hash(get_str(i)); - fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i)); + unsigned int h = script->calc_hash(script->get_str(i)); + fprintf(fp,"%04d : %4u : %s\n",i,h, script->get_str(i)); ++count[h]; } fprintf(fp,"--------------------\n\n"); @@ -3720,19 +4356,19 @@ void do_final_script(void) { for(i=0; i<SCRIPT_HASH_SIZE; i++) { fprintf(fp," hash %3d = %d\n",i,count[i]); if(min > count[i]) - min = count[i]; // minimun count of collision + min = count[i]; // minimun count of collision if(max < count[i]) - max = count[i]; // maximun count of collision + max = count[i]; // maximun count of collision if(count[i] == 0) zero++; ++count2[count[i]]; } fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n"); - for( i=min; i <= max; ++i ){ + for( i=min; i <= max; ++i ) { fprintf(fp," %5d : %7d\n",i,count2[i]); mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets> } - for( i=min; i <= max; ++i ){ + for( i=min; i <= max; ++i ) { n += count2[i]; if( n*2 >= SCRIPT_HASH_SIZE ) { @@ -3749,20 +4385,19 @@ void do_final_script(void) { } #endif - mapreg_final(); - - db_destroy(scriptlabel_db); - userfunc_db->destroy(userfunc_db, db_script_free_code_sub); - autobonus_db->destroy(autobonus_db, db_script_free_code_sub); - iter = db_iterator(script->st_db); - + for( st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter) ) { - script_free_state(st); + script->free_state(st); } - + dbi_destroy(iter); - + + mapreg->final(); + + script->userfunc_db->destroy(script->userfunc_db, script->db_free_code_sub); + script->autobonus_db->destroy(script->autobonus_db, script->db_free_code_sub); + if (script->str_data) aFree(script->str_data); if (script->str_buf) @@ -3774,7 +4409,7 @@ void do_final_script(void) { if( atcommand->binding_count != 0 ) aFree(atcommand->binding); - + for( i = 0; i < script->buildin_count; i++) { if( script->buildin[i] ) { aFree(script->buildin[i]); @@ -3783,7 +4418,7 @@ void do_final_script(void) { } aFree(script->buildin); - + if( script->hqs ) { for( i = 0; i < script->hqs; i++ ) { if( script->hq[i].item != NULL ) @@ -3802,39 +4437,69 @@ void do_final_script(void) { aFree(script->hqi); if( script->word_buf != NULL ) aFree(script->word_buf); - + +#ifdef ENABLE_CASE_CHECK + script->global_casecheck.clear(); + script->local_casecheck.clear(); +#endif // ENABLE_CASE_CHECK + ers_destroy(script->st_ers); ers_destroy(script->stack_ers); - + db_destroy(script->st_db); + + if( script->labels != NULL ) + aFree(script->labels); + + ers_destroy(script->array_ers); + + if( script->generic_ui_array ) + aFree(script->generic_ui_array); } /*========================================== * Initialization *------------------------------------------*/ -void do_init_script(void) { +void do_init_script(bool minimal) { script->st_db = idb_alloc(DB_OPT_BASE); - userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0); - scriptlabel_db = strdb_alloc(DB_OPT_DUP_KEY,50); - autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0); + script->userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0); + script->autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0); - script->st_ers = ers_new(sizeof(struct script_state), "script.c::st_ers", ERS_OPT_NONE); - script->stack_ers = ers_new(sizeof(struct script_stack), "script.c::script_stack", ERS_OPT_NONE); + script->st_ers = ers_new(sizeof(struct script_state), "script.c::st_ers", ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK); + script->stack_ers = ers_new(sizeof(struct script_stack), "script.c::script_stack", ERS_OPT_NONE|ERS_OPT_FLEX_CHUNK); + script->array_ers = ers_new(sizeof(struct script_array), "script.c::array_ers", ERS_OPT_CLEAN|ERS_OPT_CLEAR); ers_chunk_size(script->st_ers, 10); ers_chunk_size(script->stack_ers, 10); - + script->parse_builtin(); - read_constdb(); - mapreg_init(); + script->read_constdb(); + script->hardcoded_constants(); + + if (minimal) + return; + + mapreg->init(); } -int script_reload() { +int script_reload(void) { int i; DBIterator *iter; struct script_state *st; - userfunc_db->clear(userfunc_db, db_script_free_code_sub); - db_clear(scriptlabel_db); +#ifdef ENABLE_CASE_CHECK + script->global_casecheck.clear(); +#endif // ENABLE_CASE_CHECK + + iter = db_iterator(script->st_db); + + for( st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter) ) { + script->free_state(st); + } + + dbi_destroy(iter); + + script->userfunc_db->clear(script->userfunc_db, script->db_free_code_sub); + script->label_count = 0; for( i = 0; i < atcommand->binding_count; i++ ) { aFree(atcommand->binding[i]); @@ -3845,27 +4510,32 @@ int script_reload() { atcommand->binding_count = 0; - iter = db_iterator(script->st_db); - - for( st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter) ) { - script_free_state(st); - } - - dbi_destroy(iter); - db_clear(script->st_db); - - mapreg_reload(); + + mapreg->reload(); + + itemdb->name_constants(); + + sysinfo->vcsrevision_reload(); + return 0; } +/* returns name of current function being run, from within the stack [Ind/Hercules] */ +const char *script_getfuncname(struct script_state *st) { + struct script_data *data; + + data = &st->stack->stack_data[st->start]; + + if( data->type == C_NAME && script->str_data[data->u.num].type == C_FUNC ) + return script->get_str(script_getvarid(data->u.num)); + + return NULL; +} //----------------------------------------------------------------------------- // buildin functions // -#define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args } -#define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args } - ///////////////////////////////////////////////////////////////////// // NPC interaction // @@ -3875,21 +4545,21 @@ int script_reload() { /// /// mes "<message>"; BUILDIN(mes) { - TBL_PC* sd = script_rid2sd(st); + TBL_PC* sd = script->rid2sd(st); if( sd == NULL ) return true; - + if( !script_hasdata(st, 3) ) {// only a single line detected in the script clif->scriptmes(sd, st->oid, script_getstr(st, 2)); } else {// parse multiple lines as they exist int i; - + for( i = 2; script_hasdata(st, i); i++ ) { // send the message to the client clif->scriptmes(sd, st->oid, script_getstr(st, i)); } } - + return true; } @@ -3900,8 +4570,8 @@ BUILDIN(mes) { BUILDIN(next) { TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; #ifdef SECURE_NPCTIMEOUT @@ -3918,11 +4588,11 @@ BUILDIN(next) /// close; BUILDIN(close) { TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - + st->state = sd->state.dialog == 1 ? CLOSE : END; clif->scriptclose(sd, st->oid); return true; @@ -3934,12 +4604,18 @@ BUILDIN(close) { /// close2; BUILDIN(close2) { TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - - st->state = STOP; + + if( sd->state.dialog == 1 ) + st->state = STOP; + else { + ShowWarning("misuse of 'close2'! trying to use it without prior dialog! skipping...\n"); + script->reportsrc(st); + } + clif->scriptclose(sd, st->oid); return true; } @@ -3947,15 +4623,15 @@ BUILDIN(close2) { /// Counts the number of valid and total number of options in 'str' /// If max_count > 0 the counting stops when that valid option is reached /// total is incremented for each option (NULL is supported) -static int menu_countoptions(const char* str, int max_count, int* total) +int menu_countoptions(const char* str, int max_count, int* total) { int count = 0; int bogus_total; - + if( total == NULL ) total = &bogus_total; ++(*total); - + // initial empty options while( *str == ':' ) { @@ -4001,62 +4677,60 @@ BUILDIN(menu) int i; const char* text; TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - + #ifdef SECURE_NPCTIMEOUT sd->npc_idle_type = NPCT_MENU; #endif - + // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens if( sd->state.menu_or_input == 0 ) { struct StringBuf buf; struct script_data* data; - + if( script_lastdata(st) % 2 == 0 ) {// argument count is not even (1st argument is at index 2) ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1)); st->state = END; return false; } - + StrBuf->Init(&buf); sd->npc_menu = 0; for( i = 2; i < script_lastdata(st); i += 2 ) { // menu options text = script_getstr(st, i); - + // target label data = script_getdata(st, i+1); if( !data_islabel(data) ) {// not a label StrBuf->Destroy(&buf); ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i); - script_reportdata(data); + script->reportdata(data); st->state = END; return false; } - + // append option(s) if( text[0] == '\0' ) continue;// empty string, ignore if( sd->npc_menu > 0 ) StrBuf->AppendStr(&buf, ":"); StrBuf->AppendStr(&buf, text); - sd->npc_menu += menu_countoptions(text, 0, NULL); + sd->npc_menu += script->menu_countoptions(text, 0, NULL); } st->state = RERUNLINE; sd->state.menu_or_input = 1; - - /** - * menus beyond this length crash the client (see bugreport:6402) - **/ + + /* menus beyond this length crash the client (see bugreport:6402) */ if( StrBuf->Length(&buf) >= 2047 ) { - struct npc_data * nd = iMap->id2nd(st->oid); + struct npc_data * nd = map->id2nd(st->oid); char* menu; CREATE(menu, char, 2048); safestrncpy(menu, StrBuf->Value(&buf), 2047); @@ -4065,13 +4739,13 @@ BUILDIN(menu) aFree(menu); } else clif->scriptmenu(sd, st->oid, StrBuf->Value(&buf)); - + StrBuf->Destroy(&buf); - + if( sd->npc_menu >= 0xff ) {// client supports only up to 254 entries; 0 is not used and 255 is reserved for cancel; excess entries are displayed but cause 'uint8' overflow ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu); - script_reportsrc(st); + script->reportsrc(st); } } else if( sd->npc_menu == 0xff ) @@ -4082,7 +4756,7 @@ BUILDIN(menu) else {// goto target label int menu = 0; - + sd->state.menu_or_input = 0; if( sd->npc_menu <= 0 ) { @@ -4090,12 +4764,12 @@ BUILDIN(menu) st->state = END; return false; } - + // get target label for( i = 2; i < script_lastdata(st); i += 2 ) { text = script_getstr(st, i); - sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu); + sd->npc_menu -= script->menu_countoptions(text, sd->npc_menu, &menu); if( sd->npc_menu <= 0 ) break;// entry found } @@ -4108,11 +4782,11 @@ BUILDIN(menu) if( !data_islabel(script_getdata(st, i + 1)) ) {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input) ShowError("script:menu: unexpected data in label argument\n"); - script_reportdata(script_getdata(st, i + 1)); + script->reportdata(script_getdata(st, i + 1)); st->state = END; return false; } - pc->setreg(sd, add_str("@menu"), menu); + pc->setreg(sd, script->add_str("@menu"), menu); st->pos = script_getnum(st, i + 1); st->state = GOTO; } @@ -4130,38 +4804,36 @@ BUILDIN(select) int i; const char* text; TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - + #ifdef SECURE_NPCTIMEOUT sd->npc_idle_type = NPCT_MENU; #endif - + if( sd->state.menu_or_input == 0 ) { struct StringBuf buf; - + StrBuf->Init(&buf); sd->npc_menu = 0; for( i = 2; i <= script_lastdata(st); ++i ) { text = script_getstr(st, i); - + if( sd->npc_menu > 0 ) StrBuf->AppendStr(&buf, ":"); - + StrBuf->AppendStr(&buf, text); - sd->npc_menu += menu_countoptions(text, 0, NULL); + sd->npc_menu += script->menu_countoptions(text, 0, NULL); } - + st->state = RERUNLINE; sd->state.menu_or_input = 1; - - /** - * menus beyond this length crash the client (see bugreport:6402) - **/ + + /* menus beyond this length crash the client (see bugreport:6402) */ if( StrBuf->Length(&buf) >= 2047 ) { - struct npc_data * nd = iMap->id2nd(st->oid); + struct npc_data * nd = map->id2nd(st->oid); char* menu; CREATE(menu, char, 2048); safestrncpy(menu, StrBuf->Value(&buf), 2047); @@ -4171,25 +4843,25 @@ BUILDIN(select) } else clif->scriptmenu(sd, st->oid, StrBuf->Value(&buf)); StrBuf->Destroy(&buf); - + if( sd->npc_menu >= 0xff ) { ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu); - script_reportsrc(st); + script->reportsrc(st); } } else if( sd->npc_menu == 0xff ) {// Cancel was pressed sd->state.menu_or_input = 0; st->state = END; } else {// return selected option int menu = 0; - + sd->state.menu_or_input = 0; for( i = 2; i <= script_lastdata(st); ++i ) { text = script_getstr(st, i); - sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu); + sd->npc_menu -= script->menu_countoptions(text, sd->npc_menu, &menu); if( sd->npc_menu <= 0 ) break;// entry found } - pc->setreg(sd, add_str("@menu"), menu); + pc->setreg(sd, script->add_str("@menu"), menu); script_pushint(st, menu); st->state = RUN; } @@ -4209,19 +4881,19 @@ BUILDIN(prompt) int i; const char *text; TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - + #ifdef SECURE_NPCTIMEOUT sd->npc_idle_type = NPCT_MENU; #endif - + if( sd->state.menu_or_input == 0 ) { struct StringBuf buf; - + StrBuf->Init(&buf); sd->npc_menu = 0; for( i = 2; i <= script_lastdata(st); ++i ) @@ -4230,17 +4902,15 @@ BUILDIN(prompt) if( sd->npc_menu > 0 ) StrBuf->AppendStr(&buf, ":"); StrBuf->AppendStr(&buf, text); - sd->npc_menu += menu_countoptions(text, 0, NULL); + sd->npc_menu += script->menu_countoptions(text, 0, NULL); } - + st->state = RERUNLINE; sd->state.menu_or_input = 1; - - /** - * menus beyond this length crash the client (see bugreport:6402) - **/ + + /* menus beyond this length crash the client (see bugreport:6402) */ if( StrBuf->Length(&buf) >= 2047 ) { - struct npc_data * nd = iMap->id2nd(st->oid); + struct npc_data * nd = map->id2nd(st->oid); char* menu; CREATE(menu, char, 2048); safestrncpy(menu, StrBuf->Value(&buf), 2047); @@ -4250,33 +4920,33 @@ BUILDIN(prompt) } else clif->scriptmenu(sd, st->oid, StrBuf->Value(&buf)); StrBuf->Destroy(&buf); - + if( sd->npc_menu >= 0xff ) { ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu); - script_reportsrc(st); + script->reportsrc(st); } } else if( sd->npc_menu == 0xff ) {// Cancel was pressed sd->state.menu_or_input = 0; - pc->setreg(sd, add_str("@menu"), 0xff); + pc->setreg(sd, script->add_str("@menu"), 0xff); script_pushint(st, 0xff); st->state = RUN; } else {// return selected option int menu = 0; - + sd->state.menu_or_input = 0; for( i = 2; i <= script_lastdata(st); ++i ) { text = script_getstr(st, i); - sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu); + sd->npc_menu -= script->menu_countoptions(text, sd->npc_menu, &menu); if( sd->npc_menu <= 0 ) break;// entry found } - pc->setreg(sd, add_str("@menu"), menu); + pc->setreg(sd, script->add_str("@menu"), menu); script_pushint(st, menu); st->state = RUN; } @@ -4295,11 +4965,11 @@ BUILDIN(goto) if( !data_islabel(script_getdata(st,2)) ) { ShowError("script:goto: not a label\n"); - script_reportdata(script_getdata(st,2)); + script->reportdata(script_getdata(st,2)); st->state = END; return false; } - + st->pos = script_getnum(st,2); st->state = GOTO; return true; @@ -4314,44 +4984,55 @@ BUILDIN(callfunc) struct script_retinfo* ri; struct script_code* scr; const char* str = script_getstr(st,2); - DBMap **ref = NULL; - - scr = (struct script_code*)strdb_get(userfunc_db, str); + struct reg_db *ref = NULL; + + scr = (struct script_code*)strdb_get(script->userfunc_db, str); if( !scr ) { ShowError("script:callfunc: function not found! [%s]\n", str); st->state = END; return false; } - - for( i = st->start+3, j = 0; i < st->end; i++, j++ ) - { + + ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 2); + ref[0].vars = st->stack->scope.vars; + if (!st->stack->scope.arrays) + st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when? + ref[0].arrays = st->stack->scope.arrays; + ref[1].vars = st->script->local.vars; + if (!st->script->local.arrays) + st->script->local.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when? + ref[1].arrays = st->script->local.arrays; + + for( i = st->start+3, j = 0; i < st->end; i++, j++ ) { struct script_data* data = script->push_copy(st->stack,i); - if( data_isreference(data) && !data->ref ) - { + if( data_isreference(data) && !data->ref ) { const char* name = reference_getname(data); if( name[0] == '.' ) { - ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1); - ref[0] = (name[1] == '@' ? st->stack->var_function : st->script->script_vars); - data->ref = ref; + data->ref = (name[1] == '@' ? &ref[0] : &ref[1]); } } } - + CREATE(ri, struct script_retinfo, 1); - ri->script = st->script;// script code - ri->var_function = st->stack->var_function;// scope variables - ri->pos = st->pos;// script location - ri->nargs = j;// argument count - ri->defsp = st->stack->defsp;// default stack pointer - push_retinfo(st->stack, ri, ref); - + ri->script = st->script; // script code + ri->scope.vars = st->stack->scope.vars; // scope variables + ri->scope.arrays = st->stack->scope.arrays; // scope arrays + ri->pos = st->pos; // script location + ri->nargs = j; // argument count + ri->defsp = st->stack->defsp; // default stack pointer + script->push_retinfo(st->stack, ri, ref); + st->pos = 0; st->script = scr; st->stack->defsp = st->stack->sp; st->state = GOTO; - st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA); - + st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA); + st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); + + if( !st->script->local.vars ) + st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA); + return true; } /*========================================== @@ -4362,45 +5043,47 @@ BUILDIN(callsub) int i,j; struct script_retinfo* ri; int pos = script_getnum(st,2); - DBMap **ref = NULL; - + struct reg_db *ref = NULL; + if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) ) { ShowError("script:callsub: argument is not a label\n"); - script_reportdata(script_getdata(st,2)); + script->reportdata(script_getdata(st,2)); st->state = END; return false; } - - for( i = st->start+3, j = 0; i < st->end; i++, j++ ) - { + + ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1); + ref[0].vars = st->stack->scope.vars; + if (!st->stack->scope.arrays) + st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when? + ref[0].arrays = st->stack->scope.arrays; + + for( i = st->start+3, j = 0; i < st->end; i++, j++ ) { struct script_data* data = script->push_copy(st->stack,i); - if( data_isreference(data) && !data->ref ) - { + if( data_isreference(data) && !data->ref ) { const char* name = reference_getname(data); if( name[0] == '.' && name[1] == '@' ) { - if ( !ref ) { - ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1); - ref[0] = st->stack->var_function; - } - data->ref = ref; + data->ref = &ref[0]; } } } - + CREATE(ri, struct script_retinfo, 1); - ri->script = st->script;// script code - ri->var_function = st->stack->var_function;// scope variables - ri->pos = st->pos;// script location - ri->nargs = j;// argument count - ri->defsp = st->stack->defsp;// default stack pointer - push_retinfo(st->stack, ri, ref); - + ri->script = st->script; // script code + ri->scope.vars = st->stack->scope.vars; // scope variables + ri->scope.arrays = st->stack->scope.arrays; // scope arrays + ri->pos = st->pos; // script location + ri->nargs = j; // argument count + ri->defsp = st->stack->defsp; // default stack pointer + script->push_retinfo(st->stack, ri, ref); + st->pos = pos; st->stack->defsp = st->stack->sp; st->state = GOTO; - st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA); - + st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA); + st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); + return true; } @@ -4412,7 +5095,7 @@ BUILDIN(getarg) { struct script_retinfo* ri; int idx; - + if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) { ShowError("script:getarg: no callfunc or callsub!\n"); @@ -4420,9 +5103,9 @@ BUILDIN(getarg) return false; } ri = st->stack->stack_data[st->stack->defsp - 1].u.ri; - + idx = script_getnum(st,2); - + if( idx >= 0 && idx < ri->nargs ) script->push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx); else if( script_hasdata(st,3) ) @@ -4433,7 +5116,7 @@ BUILDIN(getarg) st->state = END; return false; } - + return true; } @@ -4442,24 +5125,30 @@ BUILDIN(getarg) /// /// return; /// return <value>; -BUILDIN(return) -{ +BUILDIN(return) { if( script_hasdata(st,2) ) {// return value struct script_data* data; script_pushcopy(st, 2); data = script_getdatatop(st, -1); - if( data_isreference(data) ) - { + if( data_isreference(data) ) { const char* name = reference_getname(data); - if( name[0] == '.' && name[1] == '@' ) - {// scope variable - if( !data->ref || data->ref == (DBMap**)&st->stack->var_function ) + if( name[0] == '.' && name[1] == '@' ) { + // scope variable + if( !data->ref || data->ref->vars == st->stack->scope.vars ) script->get_val(st, data);// current scope, convert to value - } - else if( name[0] == '.' && !data->ref ) - {// script variable, link to current script - data->ref = &st->script->script_vars; + if( data->ref && data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->scope.vars ) + data->ref = NULL; // Reference to the parent scope, remove reference pointer + } else if( name[0] == '.' && !data->ref ) { + // script variable without a reference set, link to current script + data->ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1); + script->add_pending_ref(st, data->ref); + data->ref->vars = st->script->local.vars; + if( !st->script->local.arrays ) + st->script->local.arrays = idb_alloc(DB_OPT_BASE); + data->ref->arrays = st->script->local.arrays; + } else if ( name[0] == '.' /* && data->ref != NULL */ ) { + data->ref = NULL; // Reference to the parent scope's script, remove reference pointer. } } } @@ -4481,7 +5170,7 @@ BUILDIN(rand) int range; int min; int max; - + if( script_hasdata(st,3) ) {// min,max min = script_getnum(st,2); @@ -4499,7 +5188,7 @@ BUILDIN(rand) script_pushint(st, min); else script_pushint(st, rnd()%range + min); - + return true; } @@ -4512,59 +5201,59 @@ BUILDIN(warp) int x,y; const char* str; TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - + str = script_getstr(st,2); x = script_getnum(st,3); y = script_getnum(st,4); - + if(strcmp(str,"Random")==0) ret = pc->randomwarp(sd,CLR_TELEPORT); else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0) ret = pc->setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT); else - ret = pc->setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT); - + ret = pc->setpos(sd,script->mapindexname2id(st,str),x,y,CLR_OUTSIGHT); + if( ret ) { ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y); - script_reportsrc(st); + script->reportsrc(st); } - + return true; } /*========================================== * Warp a specified area *------------------------------------------*/ -static int buildin_areawarp_sub(struct block_list *bl,va_list ap) +int buildin_areawarp_sub(struct block_list *bl,va_list ap) { int x2,y2,x3,y3; unsigned int index; - + index = va_arg(ap,unsigned int); x2 = va_arg(ap,int); y2 = va_arg(ap,int); x3 = va_arg(ap,int); y3 = va_arg(ap,int); - + if(index == 0) pc->randomwarp((TBL_PC *)bl,CLR_TELEPORT); else if(x3 && y3) { int max, tx, ty, j = 0; - + // choose a suitable max number of attempts if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 ) max = 1000; - + // find a suitable map cell do { tx = rnd()%(x3-x2+1)+x2; ty = rnd()%(y3-y2+1)+y2; j++; - } while( iMap->getcell(index,tx,ty,CELL_CHKNOPASS) && j < max ); - + } while( map->getcell(index,tx,ty,CELL_CHKNOPASS) && j < max ); + pc->setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT); } else @@ -4577,7 +5266,7 @@ BUILDIN(areawarp) unsigned int index; const char *str; const char *mapname; - + mapname = script_getstr(st,2); x0 = script_getnum(st,3); y0 = script_getnum(st,4); @@ -4586,9 +5275,9 @@ BUILDIN(areawarp) str = script_getstr(st,7); x2 = script_getnum(st,8); y2 = script_getnum(st,9); - + if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area - if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){ + if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ) { x3 = 0; y3 = 0; } else if( x3 && y3 ) { @@ -4597,23 +5286,23 @@ BUILDIN(areawarp) if( y3 < y2 ) swap(y3,y2); } } - - if( (m = iMap->mapname2mapid(mapname)) < 0 ) + + if( (m = map->mapname2mapid(mapname)) < 0 ) return true; - + if( strcmp(str,"Random") == 0 ) index = 0; - else if( !(index=mapindex_name2id(str)) ) + else if( !(index=script->mapindexname2id(st,str)) ) return true; - - iMap->foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3); + + map->foreachinarea(script->buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3); return true; } /*========================================== * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp> *------------------------------------------*/ -static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap) +int buildin_areapercentheal_sub(struct block_list *bl,va_list ap) { int hp, sp; hp = va_arg(ap, int); @@ -4621,12 +5310,11 @@ static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap) pc->percentheal((TBL_PC *)bl,hp,sp); return 0; } -BUILDIN(areapercentheal) -{ +BUILDIN(areapercentheal) { int hp,sp,m; const char *mapname; int x0,y0,x1,y1; - + mapname=script_getstr(st,2); x0=script_getnum(st,3); y0=script_getnum(st,4); @@ -4634,11 +5322,11 @@ BUILDIN(areapercentheal) y1=script_getnum(st,6); hp=script_getnum(st,7); sp=script_getnum(st,8); - - if( (m=iMap->mapname2mapid(mapname))< 0) + + if( (m=map->mapname2mapid(mapname))< 0) return true; - - iMap->foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp); + + map->foreachinarea(script->buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp); return true; } @@ -4648,29 +5336,28 @@ BUILDIN(areapercentheal) * another player npc-session. * Using: warpchar "mapname",x,y,Char_ID; *------------------------------------------*/ -BUILDIN(warpchar) -{ +BUILDIN(warpchar) { int x,y,a; const char *str; TBL_PC *sd; - + str=script_getstr(st,2); x=script_getnum(st,3); y=script_getnum(st,4); a=script_getnum(st,5); - - sd = iMap->charid2sd(a); + + sd = map->charid2sd(a); if( sd == NULL ) return true; - + if(strcmp(str, "Random") == 0) pc->randomwarp(sd, CLR_TELEPORT); else if(strcmp(str, "SavePoint") == 0) pc->setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT); else - pc->setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT); - + pc->setpos(sd, script->mapindexname2id(st,str), x, y, CLR_TELEPORT); + return true; } /*========================================== @@ -4684,9 +5371,9 @@ BUILDIN(warpparty) TBL_PC *pl_sd; struct party_data* p; int type; - int mapindex; + int map_index; int i; - + const char* str = script_getstr(st,2); int x = script_getnum(st,3); int y = script_getnum(st,4); @@ -4694,17 +5381,17 @@ BUILDIN(warpparty) const char* str2 = NULL; if ( script_hasdata(st,6) ) str2 = script_getstr(st,6); - + p = party->search(p_id); if(!p) return true; - + type = ( strcmp(str,"Random")==0 ) ? 0 : ( strcmp(str,"SavePointAll")==0 ) ? 1 : ( strcmp(str,"SavePoint")==0 ) ? 2 : ( strcmp(str,"Leader")==0 ) ? 3 : 4; - + switch (type) { case 3: @@ -4712,55 +5399,54 @@ BUILDIN(warpparty) if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online return true; pl_sd = p->data[i].sd; - mapindex = pl_sd->mapindex; + map_index = pl_sd->mapindex; x = pl_sd->bl.x; y = pl_sd->bl.y; break; case 4: - mapindex = mapindex_name2id(str); + map_index = script->mapindexname2id(st,str); break; case 2: //"SavePoint" uses save point of the currently attached player - if (( sd = script_rid2sd(st) ) == NULL ) + if (( sd = script->rid2sd(st) ) == NULL ) return true; default: - mapindex = 0; + map_index = 0; break; } - - for (i = 0; i < MAX_PARTY; i++) - { + + for (i = 0; i < MAX_PARTY; i++) { if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id ) continue; - - if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 ) + + if( str2 && strcmp(str2, map->list[pl_sd->bl.m].name) != 0 ) continue; - + if( pc_isdead(pl_sd) ) continue; - + switch( type ) { case 0: // Random - if(!map[pl_sd->bl.m].flag.nowarp) + if(!map->list[pl_sd->bl.m].flag.nowarp) pc->randomwarp(pl_sd,CLR_TELEPORT); break; case 1: // SavePointAll - if(!map[pl_sd->bl.m].flag.noreturn) + if(!map->list[pl_sd->bl.m].flag.noreturn) pc->setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT); break; case 2: // SavePoint - if(!map[pl_sd->bl.m].flag.noreturn) + if(!map->list[pl_sd->bl.m].flag.noreturn) pc->setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT); break; case 3: // Leader case 4: // m,x,y - if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp) - pc->setpos(pl_sd,mapindex,x,y,CLR_TELEPORT); + if(!map->list[pl_sd->bl.m].flag.noreturn && !map->list[pl_sd->bl.m].flag.nowarp) + pc->setpos(pl_sd,map_index,x,y,CLR_TELEPORT); break; } } - + return true; } /*========================================== @@ -4774,70 +5460,69 @@ BUILDIN(warpguild) struct guild* g; struct s_mapiterator* iter; int type; - + const char* str = script_getstr(st,2); int x = script_getnum(st,3); int y = script_getnum(st,4); int gid = script_getnum(st,5); - + g = guild->search(gid); if( g == NULL ) return true; - + type = ( strcmp(str,"Random")==0 ) ? 0 : ( strcmp(str,"SavePointAll")==0 ) ? 1 : ( strcmp(str,"SavePoint")==0 ) ? 2 : 3; - - if( type == 2 && ( sd = script_rid2sd(st) ) == NULL ) + + if( type == 2 && ( sd = script->rid2sd(st) ) == NULL ) {// "SavePoint" uses save point of the currently attached player return true; } - + iter = mapit_getallusers(); for( pl_sd = (TBL_PC*)mapit->first(iter); mapit->exists(iter); pl_sd = (TBL_PC*)mapit->next(iter) ) { if( pl_sd->status.guild_id != gid ) continue; - + switch( type ) { case 0: // Random - if(!map[pl_sd->bl.m].flag.nowarp) + if(!map->list[pl_sd->bl.m].flag.nowarp) pc->randomwarp(pl_sd,CLR_TELEPORT); break; case 1: // SavePointAll - if(!map[pl_sd->bl.m].flag.noreturn) + if(!map->list[pl_sd->bl.m].flag.noreturn) pc->setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT); break; case 2: // SavePoint - if(!map[pl_sd->bl.m].flag.noreturn) + if(!map->list[pl_sd->bl.m].flag.noreturn) pc->setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT); break; case 3: // m,x,y - if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp) - pc->setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT); + if(!map->list[pl_sd->bl.m].flag.noreturn && !map->list[pl_sd->bl.m].flag.nowarp) + pc->setpos(pl_sd,script->mapindexname2id(st,str),x,y,CLR_TELEPORT); break; } } mapit->free(iter); - + return true; } /*========================================== * Force Heal a player (hp and sp) *------------------------------------------*/ -BUILDIN(heal) -{ +BUILDIN(heal) { TBL_PC *sd; int hp,sp; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if (!sd) return true; - + hp=script_getnum(st,2); sp=script_getnum(st,3); - status_heal(&sd->bl, hp, sp, 1); + status->heal(&sd->bl, hp, sp, 1); return true; } /*========================================== @@ -4847,17 +5532,17 @@ BUILDIN(itemheal) { TBL_PC *sd; int hp,sp; - + hp=script_getnum(st,2); sp=script_getnum(st,3); - - if(potion_flag==1) { - potion_hp = hp; - potion_sp = sp; + + if(script->potion_flag==1) { + script->potion_hp = hp; + script->potion_sp = sp; return true; } - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if (!sd) return true; pc->itemheal(sd,sd->itemid,hp,sp); return true; @@ -4869,17 +5554,17 @@ BUILDIN(percentheal) { int hp,sp; TBL_PC* sd; - + hp=script_getnum(st,2); sp=script_getnum(st,3); - - if(potion_flag==1) { - potion_per_hp = hp; - potion_per_sp = sp; + + if(script->potion_flag==1) { + script->potion_per_hp = hp; + script->potion_per_sp = sp; return true; } - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; #ifdef RENEWAL @@ -4896,22 +5581,22 @@ BUILDIN(percentheal) BUILDIN(jobchange) { int job, upper=-1; - + job=script_getnum(st,2); if( script_hasdata(st,3) ) upper=script_getnum(st,3); - + if (pcdb_checkid(job)) { TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - + pc->jobchange(sd, job, upper); } - + return true; } @@ -4936,53 +5621,52 @@ BUILDIN(input) { TBL_PC* sd; struct script_data* data; - int uid; + int64 uid; const char* name; int min; int max; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - + data = script_getdata(st,2); - if( !data_isreference(data) ){ + if( !data_isreference(data) ) { ShowError("script:input: not a variable\n"); - script_reportdata(data); + script->reportdata(data); st->state = END; return false; } uid = reference_getuid(data); name = reference_getname(data); - min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value); - max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value); - + min = (script_hasdata(st,3) ? script_getnum(st,3) : script->config.input_min_value); + max = (script_hasdata(st,4) ? script_getnum(st,4) : script->config.input_max_value); + #ifdef SECURE_NPCTIMEOUT sd->npc_idle_type = NPCT_WAIT; #endif - - if( !sd->state.menu_or_input ) - { // first invocation, display npc input box + + if( !sd->state.menu_or_input ) { + // first invocation, display npc input box sd->state.menu_or_input = 1; st->state = RERUNLINE; if( is_string_variable(name) ) clif->scriptinputstr(sd,st->oid); else clif->scriptinput(sd,st->oid); - } - else - { // take received text/value and store it in the designated variable + } else { + // take received text/value and store it in the designated variable sd->state.menu_or_input = 0; if( is_string_variable(name) ) { int len = (int)strlen(sd->npc_str); - set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2)); + script->set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2)); script_pushint(st, (len > max ? 1 : len < min ? -1 : 0)); } else { int amount = sd->npc_amount; - set_reg(st, sd, uid, name, (void*)__64BPTRSIZE(cap_value(amount,min,max)), script_getref(st,2)); + script->set_reg(st, sd, uid, name, (void*)h64BPTRSIZE(cap_value(amount,min,max)), script_getref(st,2)); script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0)); } st->state = RUN; @@ -4997,75 +5681,81 @@ BUILDIN(copyarray); /// The value is converted to the type of the variable. /// /// set(<variable>,<value>) -> <variable> -BUILDIN(set) -{ +BUILDIN(setr) { TBL_PC* sd = NULL; struct script_data* data; //struct script_data* datavalue; - int num; + int64 num; const char* name; char prefix; - + data = script_getdata(st,2); //datavalue = script_getdata(st,3); - if( !data_isreference(data) ) - { + if( !data_isreference(data) || reference_toconstant(data) ) { ShowError("script:set: not a variable\n"); - script_reportdata(script_getdata(st,2)); + script->reportdata(script_getdata(st,2)); st->state = END; return false; } - + num = reference_getuid(data); name = reference_getname(data); prefix = *name; - - if( not_server_variable(prefix) ) - { - sd = script_rid2sd(st); - if( sd == NULL ) - { + + if( not_server_variable(prefix) ) { + sd = script->rid2sd(st); + if( sd == NULL ) { ShowError("script:set: no player attached for player variable '%s'\n", name); return true; } } - + #if 0 - if( data_isreference(datavalue) ) - {// the value being referenced is a variable + // TODO: see de43fa0f73be01080bd11c08adbfb7c158324c81 + if( data_isreference(datavalue) ) { + // the value being referenced is a variable const char* namevalue = reference_getname(datavalue); - - if( !not_array_variable(*namevalue) ) - {// array variable being copied into another array variable - if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) ) - {// player must be attached in order to copy a player variable + + if( !not_array_variable(*namevalue) ) { + // array variable being copied into another array variable + if( sd == NULL && not_server_variable(*namevalue) && !(sd = script->rid2sd(st)) ) { + // player must be attached in order to copy a player variable ShowError("script:set: no player attached for player variable '%s'\n", namevalue); return true; } - - if( is_string_variable(namevalue) != is_string_variable(name) ) - {// non-matching array value types + + if( is_string_variable(namevalue) != is_string_variable(name) ) { + // non-matching array value types ShowWarning("script:set: two array variables do not match in type.\n"); return true; } - + // push the maximum number of array values to the stack script->push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE,NULL); - + // call the copy array method directly return buildin_copyarray(st); } } #endif - + + if( script_hasdata(st, 4) ) { + // Optional argument used by post-increment/post-decrement constructs to return the previous value + if( is_string_variable(name) ) { + script_pushstrcopy(st, script_getstr(st, 4)); + } else { + script_pushint(st, script_getnum(st, 4)); + } + } else { + // return a copy of the variable reference + script_pushcopy(st,2); + } + if( is_string_variable(name) ) - set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2)); + script->set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2)); else - set_reg(st,sd,num,name,(void*)__64BPTRSIZE(script_getnum(st,3)),script_getref(st,2)); - - // return a copy of the variable reference - script_pushcopy(st,2); - + script->set_reg(st,sd,num,name,(void*)h64BPTRSIZE(script_getnum(st,3)),script_getref(st,2)); + return true; } @@ -5073,34 +5763,6 @@ BUILDIN(set) /// Array variables /// -/// Returns the size of the specified array -static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct DBMap** ref) -{ - int32 ret = idx; - - if( isstring ) - { - for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx ) - { - char* str = (char*)script->get_val2(st, reference_uid(id, idx), ref); - if( str && *str ) - ret = idx + 1; - script_removetop(st, -1, 0); - } - } - else - { - for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx ) - { - int32 num = (int32)__64BPTRSIZE(script->get_val2(st, reference_uid(id, idx), ref)); - if( num ) - ret = idx + 1; - script_removetop(st, -1, 0); - } - } - return ret; -} - /// Sets values of an array, from the starting index. /// ex: setarray arr[1],1,2,3; /// @@ -5109,52 +5771,45 @@ BUILDIN(setarray) { struct script_data* data; const char* name; - int32 start; - int32 end; + uint32 start; + uint32 end; int32 id; int32 i; TBL_PC* sd = NULL; - + data = script_getdata(st, 2); - if( !data_isreference(data) ) + if( !data_isreference(data) || reference_toconstant(data) ) { ShowError("script:setarray: not a variable\n"); - script_reportdata(data); + script->reportdata(data); st->state = END; return false;// not a variable } - + id = reference_getid(data); start = reference_getindex(data); name = reference_getname(data); - if( not_array_variable(*name) ) - { - ShowError("script:setarray: illegal scope\n"); - script_reportdata(data); - st->state = END; - return false;// not supported - } - + if( not_server_variable(*name) ) { - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached } - + end = start + script_lastdata(st) - 2; if( end > SCRIPT_MAX_ARRAYSIZE ) end = SCRIPT_MAX_ARRAYSIZE; - + if( is_string_variable(name) ) {// string array for( i = 3; start < end; ++start, ++i ) - set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data)); + script->set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data)); } else {// int array for( i = 3; start < end; ++start, ++i ) - set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPTRSIZE(script_getnum(st,i)), reference_getref(data)); + script->set_reg(st, sd, reference_uid(id, start), name, (void*)h64BPTRSIZE(script_getnum(st,i)), reference_getref(data)); } return true; } @@ -5167,50 +5822,43 @@ BUILDIN(cleararray) { struct script_data* data; const char* name; - int32 start; - int32 end; + uint32 start; + uint32 end; int32 id; void* v; TBL_PC* sd = NULL; - + data = script_getdata(st, 2); if( !data_isreference(data) ) { ShowError("script:cleararray: not a variable\n"); - script_reportdata(data); + script->reportdata(data); st->state = END; return false;// not a variable } - + id = reference_getid(data); start = reference_getindex(data); name = reference_getname(data); - if( not_array_variable(*name) ) - { - ShowError("script:cleararray: illegal scope\n"); - script_reportdata(data); - st->state = END; - return false;// not supported - } - + if( not_server_variable(*name) ) { - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached } - + if( is_string_variable(name) ) v = (void*)script_getstr(st, 3); else - v = (void*)__64BPTRSIZE(script_getnum(st, 3)); - + v = (void*)h64BPTRSIZE(script_getnum(st, 3)); + end = start + script_getnum(st, 4); if( end > SCRIPT_MAX_ARRAYSIZE ) end = SCRIPT_MAX_ARRAYSIZE; - + for( ; start < end; ++start ) - set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2)); + script->set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2)); return true; } @@ -5230,78 +5878,67 @@ BUILDIN(copyarray) int32 id2; void* v; int32 i; - int32 count; + uint32 count; TBL_PC* sd = NULL; - + data1 = script_getdata(st, 2); data2 = script_getdata(st, 3); if( !data_isreference(data1) || !data_isreference(data2) ) { ShowError("script:copyarray: not a variable\n"); - script_reportdata(data1); - script_reportdata(data2); + script->reportdata(data1); + script->reportdata(data2); st->state = END; return false;// not a variable } - + id1 = reference_getid(data1); id2 = reference_getid(data2); idx1 = reference_getindex(data1); idx2 = reference_getindex(data2); name1 = reference_getname(data1); name2 = reference_getname(data2); - if( not_array_variable(*name1) || not_array_variable(*name2) ) - { - ShowError("script:copyarray: illegal scope\n"); - script_reportdata(data1); - script_reportdata(data2); - st->state = END; - return false;// not supported - } - + if( is_string_variable(name1) != is_string_variable(name2) ) { ShowError("script:copyarray: type mismatch\n"); - script_reportdata(data1); - script_reportdata(data2); + script->reportdata(data1); + script->reportdata(data2); st->state = END; return false;// data type mismatch } - + if( not_server_variable(*name1) || not_server_variable(*name2) ) { - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached } - + count = script_getnum(st, 4); if( count > SCRIPT_MAX_ARRAYSIZE - idx1 ) count = SCRIPT_MAX_ARRAYSIZE - idx1; - if( count <= 0 || (id1 == id2 && idx1 == idx2) ) + if( count <= 0 || (idx1 == idx2 && is_same_reference(data1, data2)) ) return true;// nothing to copy - - if( id1 == id2 && idx1 > idx2 ) - {// destination might be overlapping the source - copy in reverse order - for( i = count - 1; i >= 0; --i ) - { + + if( is_same_reference(data1, data2) && idx1 > idx2 ) { + // destination might be overlapping the source - copy in reverse order + for( i = count - 1; i >= 0; --i ) { v = script->get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2)); - set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1)); + script->set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1)); script_removetop(st, -1, 0); } - } - else - {// normal copy - for( i = 0; i < count; ++i ) - { - if( idx2 + i < SCRIPT_MAX_ARRAYSIZE ) - { + } else { + // normal copy + for( i = 0; i < count; ++i ) { + if( idx2 + i < SCRIPT_MAX_ARRAYSIZE ) { v = script->get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2)); - set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1)); + script->set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1)); script_removetop(st, -1, 0); + } else { + // out of range - assume ""/0 + script->set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1)); } - else// out of range - assume ""/0 - set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1)); } } return true; @@ -5315,31 +5952,23 @@ BUILDIN(copyarray) BUILDIN(getarraysize) { struct script_data* data; - const char* name; - + data = script_getdata(st, 2); if( !data_isreference(data) ) { ShowError("script:getarraysize: not a variable\n"); - script_reportdata(data); + script->reportdata(data); script_pushnil(st); st->state = END; return false;// not a variable } - - name = reference_getname(data); - if( not_array_variable(*name) ) - { - ShowError("script:getarraysize: illegal scope\n"); - script_reportdata(data); - script_pushnil(st); - st->state = END; - return false;// not supported - } - - script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data))); + + script_pushint(st, script->array_highest_key(st,st->rid ? script->rid2sd(st) : NULL,reference_getname(data),reference_getref(data))); return true; } +int script_array_index_cmp(const void *a, const void *b) { + return ( *(unsigned int*)a - *(unsigned int*)b ); +} /// Deletes count or all the elements in an array, from the starting index. /// ex: deletearray arr[4],2; @@ -5350,71 +5979,109 @@ BUILDIN(deletearray) { struct script_data* data; const char* name; - int start; - int end; + unsigned int start, end, i; int id; TBL_PC *sd = NULL; - + struct script_array *sa = NULL; + struct reg_db *src = NULL; + void *value; + data = script_getdata(st, 2); if( !data_isreference(data) ) { ShowError("script:deletearray: not a variable\n"); - script_reportdata(data); + script->reportdata(data); st->state = END; return false;// not a variable } - + id = reference_getid(data); start = reference_getindex(data); name = reference_getname(data); - if( not_array_variable(*name) ) - { - ShowError("script:deletearray: illegal scope\n"); - script_reportdata(data); - st->state = END; - return false;// not supported - } - + if( not_server_variable(*name) ) { - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached } + + if( !(src = script->array_src(st,sd,name, reference_getref(data)) ) ) { + ShowError("script:deletearray: not a array\n"); + script->reportdata(data); + st->state = END; + return false;// not a variable + } - end = SCRIPT_MAX_ARRAYSIZE; + script->array_ensure_zero(st,NULL,data->u.num,reference_getref(data)); + + if ( !(sa = idb_get(src->arrays, id)) ) { /* non-existent array, nothing to empty */ + return true;// not a variable + } + + end = script->array_highest_key(st,sd,name,reference_getref(data)); if( start >= end ) return true;// nothing to free + + if( is_string_variable(name) ) + value = (void *)""; + else + value = (void *)0; - if( script_hasdata(st,3) ) - { - int count = script_getnum(st, 3); + if( script_hasdata(st,3) ) { + unsigned int count = script_getnum(st, 3); if( count > end - start ) count = end - start; if( count <= 0 ) return true;// nothing to free + + if( end - start < sa->size ) { + // Better to iterate directly on the array, no speed-up from using sa + for( ; start + count < end; ++start ) { + // Compact and overwrite + void* v = script->get_val2(st, reference_uid(id, start + count), reference_getref(data)); + script->set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data)); + script_removetop(st, -1, 0); + } + for( ; start < end; start++ ) { + // Clean up any leftovers that weren't overwritten + script->set_reg(st, sd, reference_uid(id, start), name, value, reference_getref(data)); + } + } else { + // using sa to speed up + unsigned int *list = NULL, size = 0; + list = script->array_cpy_list(sa); + size = sa->size; + qsort(list, size, sizeof(unsigned int), script_array_index_cmp); + + ARR_FIND(0, size, i, list[i] >= start); + + for( ; i < size && list[i] < start + count; i++ ) { + // Clear any entries between start and start+count, if they exist + script->set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data)); + } + + for( ; i < size && list[i] < end; i++ ) { + // Move back count positions any entries between start+count to fill the gaps + void* v = script->get_val2(st, reference_uid(id, list[i]), reference_getref(data)); + script->set_reg(st, sd, reference_uid(id, list[i]-count), name, v, reference_getref(data)); + script_removetop(st, -1, 0); + // Clear their originals + script->set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data)); + } + } + } else { + unsigned int *list = NULL, size = 0; + list = script->array_cpy_list(sa); + size = sa->size; - // move rest of the elements backward - for( ; start + count < end; ++start ) - { - void* v = script->get_val2(st, reference_uid(id, start + count), reference_getref(data)); - set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data)); - script_removetop(st, -1, 0); + for(i = 0; i < size; i++) { + if( list[i] >= start ) // Less expensive than sorting it, most likely + script->set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data)); } } - - // clear the rest of the array - if( is_string_variable(name) ) - { - for( ; start < end; ++start ) - set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data)); - } - else - { - for( ; start < end; ++start ) - set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data)); - } + return true; } @@ -5425,42 +6092,31 @@ BUILDIN(deletearray) BUILDIN(getelementofarray) { struct script_data* data; - const char* name; int32 id; - int i; - + int64 i; + data = script_getdata(st, 2); if( !data_isreference(data) ) { ShowError("script:getelementofarray: not a variable\n"); - script_reportdata(data); + script->reportdata(data); script_pushnil(st); st->state = END; return false;// not a variable } - + id = reference_getid(data); - name = reference_getname(data); - if( not_array_variable(*name) ) - { - ShowError("script:getelementofarray: illegal scope\n"); - script_reportdata(data); - script_pushnil(st); - st->state = END; - return false;// not supported - } - + i = script_getnum(st, 3); - if( i < 0 || i >= SCRIPT_MAX_ARRAYSIZE ) - { - ShowWarning("script:getelementofarray: index out of range (%d)\n", i); - script_reportdata(data); + if (i < 0 || i >= SCRIPT_MAX_ARRAYSIZE) { + ShowWarning("script:getelementofarray: index out of range (%"PRId64")\n", i); + script->reportdata(data); script_pushnil(st); st->state = END; return false;// out of range } - - script->push_val(st->stack, C_NAME, reference_uid(id, i), reference_getref(data)); + + script->push_val(st->stack, C_NAME, reference_uid(id, (unsigned int)i), reference_getref(data)); return true; } @@ -5475,16 +6131,16 @@ BUILDIN(setlook) { int type,val; TBL_PC* sd; - + type=script_getnum(st,2); val=script_getnum(st,3); - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - + pc->changelook(sd,type,val); - + return true; } @@ -5492,16 +6148,16 @@ BUILDIN(changelook) { // As setlook but only client side int type,val; TBL_PC* sd; - + type=script_getnum(st,2); val=script_getnum(st,3); - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - + clif->changelook(&sd->bl,type,val); - + return true; } @@ -5511,11 +6167,11 @@ BUILDIN(changelook) BUILDIN(cutin) { TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - + clif->cutin(sd,script_getstr(st,2),script_getnum(st,3)); return true; } @@ -5527,104 +6183,86 @@ BUILDIN(viewpoint) { int type,x,y,id,color; TBL_PC* sd; - + type=script_getnum(st,2); x=script_getnum(st,3); y=script_getnum(st,4); id=script_getnum(st,5); color=script_getnum(st,6); - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - + clif->viewpoint(sd,st->oid,type,x,y,id,color); - + return true; } /*========================================== * *------------------------------------------*/ -BUILDIN(countitem) -{ +BUILDIN(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); + + TBL_PC* sd = script->rid2sd(st); + if( !sd ) return true; + + if( script_isstringtype(st, 2) ) { + // item name + id = itemdb->search_name(script_getstr(st, 2)); + } else { + // item id + id = itemdb->exists(script_getnum(st, 2)); } - - data = script_getdata(st,2); - script->get_val(st, data); // convert into value in case of a variable - - if( data_isstring(data) ) - {// item name - id = itemdb_searchname(script->conv_str(st, data)); - } - else - {// item id - id = itemdb_exists(script->conv_num(st, data)); - } - - if( id == NULL ) - { + + 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 false; } - + nameid = id->nameid; - + for(i = 0; i < MAX_INVENTORY; i++) if(sd->status.inventory[i].nameid == nameid) count += sd->status.inventory[i].amount; - + script_pushint(st,count); return true; } /*========================================== - * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus] - * returns number of items that meet the conditions + * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus] + * returns number of items that meet the conditions *------------------------------------------*/ -BUILDIN(countitem2) -{ +BUILDIN(countitem2) { int nameid, iden, ref, attr, c1, c2, c3, c4; int count = 0; int i; struct item_data* id = NULL; - struct script_data* data; - - TBL_PC* sd = script_rid2sd(st); - if (!sd) { - script_pushint(st,0); + + TBL_PC* sd = script->rid2sd(st); + if( !sd ) return true; + + if( script_isstringtype(st, 2) ) { + // item name + id = itemdb->search_name(script_getstr(st, 2)); + } else { + // item id + id = itemdb->exists(script_getnum(st, 2)); } - - data = script_getdata(st,2); - script->get_val(st, data); // convert into value in case of a variable - - if( data_isstring(data) ) - {// item name - id = itemdb_searchname(script->conv_str(st, data)); - } - else - {// item id - id = itemdb_exists(script->conv_num(st, data)); - } - - if( id == NULL ) - { + + if( id == NULL ) { ShowError("buildin_countitem2: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was script_pushint(st,0); return false; } - + nameid = id->nameid; iden = script_getnum(st,3); ref = script_getnum(st,4); @@ -5633,7 +6271,7 @@ BUILDIN(countitem2) 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 && @@ -5643,7 +6281,7 @@ BUILDIN(countitem2) sd->status.inventory[i].card[3] == c4 ) count += sd->status.inventory[i].amount; - + script_pushint(st,count); return true; } @@ -5652,8 +6290,8 @@ BUILDIN(countitem2) * Check if item with this amount can fit in inventory * Checking : weight, stack amount >32k, slots amount >(MAX_INVENTORY) * Return - * 0 : fail - * 1 : success (npc side only) + * 0 : fail + * 1 : success (npc side only) *------------------------------------------*/ BUILDIN(checkweight) { @@ -5661,73 +6299,76 @@ BUILDIN(checkweight) unsigned int weight=0, i, nbargs; struct item_data* id = NULL; struct map_session_data* sd; - struct script_data* data; - - if( ( sd = script_rid2sd(st) ) == NULL ){ + + if( ( sd = script->rid2sd(st) ) == NULL ) { return true; } nbargs = script_lastdata(st)+1; - if(nbargs%2){ - ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n"); - script_pushint(st,0); - return false; + if(nbargs%2) { + ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n"); + script_pushint(st,0); + return false; } slots = pc->inventoryblank(sd); //nb of empty slot - - for(i=2; i<nbargs; i=i+2){ - data = script_getdata(st,i); - script->get_val(st, data); // convert into value in case of a variable - if( data_isstring(data) ){// item name - id = itemdb_searchname(script->conv_str(st, data)); - } else {// item id - id = itemdb_exists(script->conv_num(st, data)); - } - if( id == NULL ) { - ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was - script_pushint(st,0); - return false; - } - nameid = id->nameid; - - amount = script_getnum(st,i+1); - if( amount < 1 ) { - ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount); - script_pushint(st,0); - return false; - } - - weight += itemdb_weight(nameid)*amount; //total weight for all chk - if( weight + sd->weight > sd->max_weight ) - {// too heavy - script_pushint(st,0); - return true; - } - - switch( pc->checkadditem(sd, nameid, amount) ) - { - case ADDITEM_EXIST: - // item is already in inventory, but there is still space for the requested amount - break; - case ADDITEM_NEW: - if( itemdb_isstackable(nameid) ) {// stackable - amount2++; - if( slots < amount2 ) { - script_pushint(st,0); - return true; - } - } - else {// non-stackable - amount2 += amount; - if( slots < amount2){ - script_pushint(st,0); - return true; - } - } - break; - case ADDITEM_OVERAMOUNT: - script_pushint(st,0); - return true; - } + + for( i = 2; i < nbargs; i += 2 ) { + if( script_isstringtype(st, i) ) { + // item name + id = itemdb->search_name(script_getstr(st, i)); + } else if ( script_isinttype(st, i) ) { + // item id + id = itemdb->exists(script_getnum(st, i)); + } else { + ShowError("buildin_checkweight: invalid type for argument '%d'.\n", i); + script_pushint(st,0); + return false; + } + if( id == NULL ) { + ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was + script_pushint(st,0); + return false; + } + nameid = id->nameid; + + amount = script_getnum(st,i+1); + if( amount < 1 ) { + ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount); + script_pushint(st,0); + return false; + } + + weight += itemdb_weight(nameid)*amount; //total weight for all chk + if( weight + sd->weight > sd->max_weight ) + {// too heavy + script_pushint(st,0); + return true; + } + + switch( pc->checkadditem(sd, nameid, amount) ) { + case ADDITEM_EXIST: + // item is already in inventory, but there is still space for the requested amount + break; + case ADDITEM_NEW: + if( itemdb->isstackable(nameid) ) { + // stackable + amount2++; + if( slots < amount2 ) { + script_pushint(st,0); + return true; + } + } else { + // non-stackable + amount2 += amount; + if( slots < amount2) { + script_pushint(st,0); + return true; + } + } + break; + case ADDITEM_OVERAMOUNT: + script_pushint(st,0); + return true; + } } script_pushint(st,1); return true; @@ -5739,7 +6380,7 @@ BUILDIN(checkweight2) int32 nameid=-1, amount=-1; int i=0, amount2=0, slots=0, weight=0; short fail=0; - + //variable for array parsing struct script_data* data_it; struct script_data* data_nb; @@ -5748,13 +6389,15 @@ BUILDIN(checkweight2) int32 id_it, id_nb; int32 idx_it, idx_nb; int nb_it, nb_nb; //array size - - TBL_PC *sd = script_rid2sd(st); - nullpo_retr(1,sd); - + + TBL_PC *sd = script->rid2sd(st); + + if( sd == NULL ) + return false; + data_it = script_getdata(st, 2); data_nb = script_getdata(st, 3); - + if( !data_isreference(data_it) || !data_isreference(data_nb)) { ShowError("script:checkweight2: parameter not a variable\n"); @@ -5767,70 +6410,64 @@ BUILDIN(checkweight2) idx_nb = reference_getindex(data_nb); name_it = reference_getname(data_it); name_nb = reference_getname(data_nb); - - if( not_array_variable(*name_it) || not_array_variable(*name_nb)) - { - ShowError("script:checkweight2: illegal scope\n"); - script_pushint(st,0); - return false;// not supported - } - if(is_string_variable(name_it) || is_string_variable(name_nb)){ + + if(is_string_variable(name_it) || is_string_variable(name_nb)) { ShowError("script:checkweight2: illegal type, need int\n"); script_pushint(st,0); return false;// not supported } - nb_it = getarraysize(st, id_it, idx_it, 0, reference_getref(data_it)); - nb_nb = getarraysize(st, id_nb, idx_nb, 0, reference_getref(data_nb)); - if(nb_it != nb_nb){ + nb_it = script->array_highest_key(st,sd,reference_getname(data_it),reference_getref(data_it)); + nb_nb = script->array_highest_key(st,sd,reference_getname(data_nb),reference_getref(data_nb)); + if(nb_it != nb_nb) { ShowError("Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb); fail = 1; } - + slots = pc->inventoryblank(sd); - for(i=0; i<nb_it; i++){ - nameid = (int32)__64BPTRSIZE(script->get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it))); - script_removetop(st, -1, 0); - amount = (int32)__64BPTRSIZE(script->get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb))); - script_removetop(st, -1, 0); - if(fail) continue; //cpntonie to depop rest - - if(itemdb_exists(nameid) == NULL ){ + for(i=0; i<nb_it; i++) { + nameid = (int32)h64BPTRSIZE(script->get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it))); + script_removetop(st, -1, 0); + amount = (int32)h64BPTRSIZE(script->get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb))); + script_removetop(st, -1, 0); + if(fail) continue; //cpntonie to depop rest + + if(itemdb->exists(nameid) == NULL ) { ShowError("buildin_checkweight2: Invalid item '%d'.\n", nameid); fail=1; continue; } - if(amount < 0 ){ + if(amount < 0 ) { ShowError("buildin_checkweight2: Invalid amount '%d'.\n", amount); fail = 1; continue; } - weight += itemdb_weight(nameid)*amount; - if( weight + sd->weight > sd->max_weight ){ + weight += itemdb_weight(nameid)*amount; + if( weight + sd->weight > sd->max_weight ) { fail = 1; continue; - } - switch( pc->checkadditem(sd, nameid, amount) ) { - case ADDITEM_EXIST: + } + switch( pc->checkadditem(sd, nameid, amount) ) { + case ADDITEM_EXIST: // item is already in inventory, but there is still space for the requested amount - break; - case ADDITEM_NEW: - if( itemdb_isstackable(nameid) ){// stackable - amount2++; - if( slots < amount2 ) - fail = 1; - } - else {// non-stackable - amount2 += amount; - if( slots < amount2 ){ - fail = 1; - } - } - break; - case ADDITEM_OVERAMOUNT: - fail = 1; - } //end switch + break; + case ADDITEM_NEW: + if( itemdb->isstackable(nameid) ) {// stackable + amount2++; + if( slots < amount2 ) + fail = 1; + } + else {// non-stackable + amount2 += amount; + if( slots < amount2 ) { + fail = 1; + } + } + break; + case ADDITEM_OVERAMOUNT: + fail = 1; + } //end switch } //end loop DO NOT break it prematurly we need to depop all stack - + fail?script_pushint(st,0):script_pushint(st,1); return true; } @@ -5838,115 +6475,128 @@ BUILDIN(checkweight2) /*========================================== * getitem <item id>,<amount>{,<account ID>}; * getitem "<item name>",<amount>{,<account ID>}; + * + * getitembound <item id>,<amount>,<type>{,<account ID>}; + * getitembound "<item id>",<amount>,<type>{,<account ID>}; *------------------------------------------*/ -BUILDIN(getitem) -{ - int nameid,amount,get_count,i,flag = 0; +BUILDIN(getitem) { + int nameid,amount,get_count,i,flag = 0, offset = 0; struct item it; TBL_PC *sd; - struct script_data *data; struct item_data *item_data; - - data=script_getdata(st,2); - script->get_val(st,data); - if( data_isstring(data) ) - {// "<item name>" - const char *name=script->conv_str(st,data); - if( (item_data = itemdb_searchname(name)) == NULL ){ - ShowError("buildin_getitem: Nonexistant item %s requested.\n", name); + + if( script_isstringtype(st, 2) ) { + // "<item name>" + const char *name = script_getstr(st, 2); + if( (item_data = itemdb->search_name(name)) == NULL ) { + ShowError("buildin_%s: Nonexistant item %s requested.\n", script->getfuncname(st), name); return false; //No item created. } nameid=item_data->nameid; - } else if( data_isint(data) ) {// <item id> - nameid=script->conv_num(st,data); + } else { + // <item id> + nameid = script_getnum(st, 2); //Violet Box, Blue Box, etc - random item pick if( nameid < 0 ) { nameid = -nameid; flag = 1; } - if( nameid <= 0 || !(item_data = itemdb_exists(nameid)) ){ - ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid); + if( nameid <= 0 || !(item_data = itemdb->exists(nameid)) ) { + ShowError("buildin_%s: Nonexistant item %d requested.\n", script->getfuncname(st), nameid); return false; //No item created. } - } else { - ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type); - return false; } - + // <amount> if( (amount=script_getnum(st,3)) <= 0) return true; //return if amount <=0, skip the useles iteration - + memset(&it,0,sizeof(it)); it.nameid=nameid; + if(!flag) it.identify=1; else - it.identify=itemdb_isidentified2(item_data); - - if( script_hasdata(st,4) ) - sd=iMap->id2sd(script_getnum(st,4)); // <Account ID> + it.identify=itemdb->isidentified2(item_data); + + if( !strcmp(script->getfuncname(st),"getitembound") ) { + int bound = script_getnum(st,4); + if( bound < IBT_MIN || bound > IBT_MAX ) { //Not a correct bound type + ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound); + return false; + } + if( item_data->type == IT_PETEGG || item_data->type == IT_PETARMOR ) { + ShowError("script_getitembound: can't bind a pet egg/armor! Type=%d\n",bound); + return false; + } + it.bound = (unsigned char)bound; + offset += 1; + } + + if( script_hasdata(st,4+offset) ) + sd=map->id2sd(script_getnum(st,4+offset)); // <Account ID> else - sd=script_rid2sd(st); // Attached player - + sd=script->rid2sd(st); // Attached player + if( sd == NULL ) // no target return true; - + //Check if it's stackable. - if (!itemdb_isstackable(nameid)) + if (!itemdb->isstackable(nameid)) get_count = 1; else get_count = amount; - - for (i = 0; i < amount; i += get_count) - { + + for (i = 0; i < amount; i += get_count) { // if not pet egg - if (!pet_create_egg(sd, nameid)) - { - if ((flag = pc->additem(sd, &it, get_count, LOG_TYPE_SCRIPT))) - { + if (!pet->create_egg(sd, nameid)) { + if ((flag = pc->additem(sd, &it, get_count, LOG_TYPE_SCRIPT))) { clif->additem(sd, 0, 0, flag); if( pc->candrop(sd,&it) ) - iMap->addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0); + map->addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0); } } } - + return true; } /*========================================== * *------------------------------------------*/ -BUILDIN(getitem2) -{ - int nameid,amount,get_count,i,flag = 0; - int iden,ref,attr,c1,c2,c3,c4; - struct item_data *item_data; - struct item item_tmp; +BUILDIN(getitem2) { + int nameid,amount,i,flag = 0, offset = 0; + int iden,ref,attr,c1,c2,c3,c4, bound = 0; TBL_PC *sd; - struct script_data *data; - - if( script_hasdata(st,11) ) - sd=iMap->id2sd(script_getnum(st,11)); // <Account ID> + + if( !strcmp(script->getfuncname(st),"getitembound2") ) { + bound = script_getnum(st,11); + if( bound < IBT_MIN || bound > IBT_MAX ) { //Not a correct bound type + ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound); + return false; + } + offset += 1; + } + + if( script_hasdata(st,11+offset) ) + sd=map->id2sd(script_getnum(st,11+offset)); // <Account ID> else - sd=script_rid2sd(st); // Attached player - + sd=script->rid2sd(st); // Attached player + if( sd == NULL ) // no target return true; - - data=script_getdata(st,2); - script->get_val(st,data); - if( data_isstring(data) ){ - const char *name=script->conv_str(st,data); - struct item_data *item_data = itemdb_searchname(name); + + if( script_isstringtype(st, 2) ) { + const char *name = script_getstr(st, 2); + struct item_data *item_data = itemdb->search_name(name); if( item_data ) nameid=item_data->nameid; else nameid=UNKNOWN_ITEM_ID; - }else - nameid=script->conv_num(st,data); - + } else { + nameid = script_getnum(st, 2); + } + amount=script_getnum(st,3); iden=script_getnum(st,4); ref=script_getnum(st,5); @@ -5955,18 +6605,25 @@ BUILDIN(getitem2) c2=(short)script_getnum(st,8); c3=(short)script_getnum(st,9); c4=(short)script_getnum(st,10); - + + if (bound && (itemdb_type(nameid) == IT_PETEGG || itemdb_type(nameid) == IT_PETARMOR)) { + ShowError("script_getitembound2: can't bind a pet egg/armor! Type=%d\n",bound); + return false; + } + if(nameid<0) { // Invalide nameid nameid = -nameid; flag = 1; } - + if(nameid > 0) { + struct item item_tmp; + struct item_data *item_data = itemdb->exists(nameid); + int get_count; memset(&item_tmp,0,sizeof(item_tmp)); - item_data=itemdb_exists(nameid); if (item_data == NULL) return -1; - if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){ + if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR) { if(ref > MAX_REFINE) ref = MAX_REFINE; } else if(item_data->type==IT_PETEGG) { @@ -5977,7 +6634,7 @@ BUILDIN(getitem2) iden = 1; ref = attr = 0; } - + item_tmp.nameid=nameid; if(!flag) item_tmp.identify=iden; @@ -5985,32 +6642,30 @@ BUILDIN(getitem2) item_tmp.identify=0; item_tmp.refine=ref; item_tmp.attribute=attr; + item_tmp.bound=(unsigned char)bound; item_tmp.card[0]=(short)c1; item_tmp.card[1]=(short)c2; item_tmp.card[2]=(short)c3; item_tmp.card[3]=(short)c4; - + //Check if it's stackable. - if (!itemdb_isstackable(nameid)) + if (!itemdb->isstackable(nameid)) get_count = 1; else get_count = amount; - - for (i = 0; i < amount; i += get_count) - { + + for (i = 0; i < amount; i += get_count) { // if not pet egg - if (!pet_create_egg(sd, nameid)) - { - if ((flag = pc->additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT))) - { + if (!pet->create_egg(sd, nameid)) { + if ((flag = pc->additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT))) { clif->additem(sd, 0, 0, flag); if( pc->candrop(sd,&item_tmp) ) - iMap->addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0); + map->addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0); } } } } - + return true; } @@ -6018,58 +6673,45 @@ BUILDIN(getitem2) * rentitem <item id>,<seconds> * rentitem "<item name>",<seconds> *------------------------------------------*/ -BUILDIN(rentitem) -{ +BUILDIN(rentitem) { struct map_session_data *sd; - struct script_data *data; struct item it; int seconds; int nameid = 0, flag; - - data = script_getdata(st,2); - script->get_val(st,data); - - if( (sd = script_rid2sd(st)) == NULL ) + + if( (sd = script->rid2sd(st)) == NULL ) return true; - - if( data_isstring(data) ) - { - const char *name = script->conv_str(st,data); - struct item_data *itd = itemdb_searchname(name); + + if( script_isstringtype(st, 2) ) { + const char *name = script_getstr(st, 2); + struct item_data *itd = itemdb->search_name(name); if( itd == NULL ) { ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name); return false; } nameid = itd->nameid; - } - else if( data_isint(data) ) - { - nameid = script->conv_num(st,data); - if( nameid <= 0 || !itemdb_exists(nameid) ) - { + } else { + nameid = script_getnum(st, 2); + if( nameid <= 0 || !itemdb->exists(nameid) ) { ShowError("buildin_rentitem: Nonexistant item %d requested.\n", nameid); return false; } } - else - { - ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type); - return false; - } - + seconds = script_getnum(st,3); memset(&it, 0, sizeof(it)); it.nameid = nameid; it.identify = 1; it.expire_time = (unsigned int)(time(NULL) + seconds); - + it.bound = 0; + if( (flag = pc->additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) { clif->additem(sd, 0, 0, flag); return false; } - + return true; } @@ -6079,53 +6721,45 @@ BUILDIN(rentitem) * Returned Qty is always 1, only works on equip-able * equipment *------------------------------------------*/ -BUILDIN(getnameditem) -{ +BUILDIN(getnameditem) { int nameid; struct item item_tmp; TBL_PC *sd, *tsd; - struct script_data *data; - - sd = script_rid2sd(st); - if (sd == NULL) - { //Player not attached! - script_pushint(st,0); + + sd = script->rid2sd(st); + if (sd == NULL) // Player not attached! return true; - } - - data=script_getdata(st,2); - script->get_val(st,data); - if( data_isstring(data) ){ - const char *name=script->conv_str(st,data); - struct item_data *item_data = itemdb_searchname(name); - if( item_data == NULL) - { //Failed + + if( script_isstringtype(st, 2) ) { + const char *name = script_getstr(st, 2); + struct item_data *item_data = itemdb->search_name(name); + if( item_data == NULL) { + //Failed script_pushint(st,0); return true; } nameid = item_data->nameid; - }else - nameid = script->conv_num(st,data); - - if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/) - { //Even though named stackable items "could" be risky, they are required for certain quests. + } else { + nameid = script_getnum(st, 2); + } + + if(!itemdb->exists(nameid)/* || itemdb->isstackable(nameid)*/) { + //Even though named stackable items "could" be risky, they are required for certain quests. script_pushint(st,0); return true; } - - data=script_getdata(st,3); - script->get_val(st,data); - if( data_isstring(data) ) //Char Name - tsd=iMap->nick2sd(script->conv_str(st,data)); - else //Char Id was given - tsd=iMap->charid2sd(script->conv_num(st,data)); - - if( tsd == NULL ) - { //Failed + + if( script_isstringtype(st, 3) ) //Char Name + tsd=map->nick2sd(script_getstr(st, 3)); + else //Char Id was given + tsd=map->charid2sd(script_getnum(st, 3)); + + if( tsd == NULL ) { + //Failed script_pushint(st,0); return true; } - + memset(&item_tmp,0,sizeof(item_tmp)); item_tmp.nameid=nameid; item_tmp.amount=1; @@ -6135,9 +6769,9 @@ BUILDIN(getnameditem) item_tmp.card[3]=tsd->status.char_id >> 16; if(pc->additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) { script_pushint(st,0); - return true; //Failed to add item, we will not drop if they don't fit + return true; //Failed to add item, we will not drop if they don't fit } - + script_pushint(st,1); return true; } @@ -6146,12 +6780,30 @@ BUILDIN(getnameditem) * gets a random item ID from an item group [Skotlex] * groupranditem group_num *------------------------------------------*/ -BUILDIN(grouprandomitem) -{ - int group; - - group = script_getnum(st,2); - script_pushint(st,itemdb_searchrandomid(group)); +BUILDIN(grouprandomitem) { + struct item_data *data; + int nameid; + + if( script_hasdata(st, 2) ) + nameid = script_getnum(st, 2); + else if ( script->current_item_id ) + nameid = script->current_item_id; + else { + ShowWarning("buildin_grouprandomitem: no item id provided and no item attached\n"); + script_pushint(st, 0); + return true; + } + + if( !(data = itemdb->exists(nameid)) ) { + ShowWarning("buildin_grouprandomitem: unknown item id %d\n",nameid); + script_pushint(st, 0); + } else if ( !data->group ) { + ShowWarning("buildin_grouprandomitem: item '%s' (%d) isn't a group!\n",data->name,nameid); + script_pushint(st, 0); + } else { + script_pushint(st, itemdb->group_item(data->group)); + } + return true; } @@ -6164,47 +6816,49 @@ BUILDIN(makeitem) int x,y,m; const char *mapname; struct item item_tmp; - struct script_data *data; struct item_data *item_data; - data=script_getdata(st,2); - script->get_val(st,data); - if( data_isstring(data) ){ - const char *name=script->conv_str(st,data); - if( (item_data = itemdb_searchname(name)) ) + if( script_isstringtype(st, 2) ) { + const char *name = script_getstr(st, 2); + if( (item_data = itemdb->search_name(name)) ) nameid=item_data->nameid; else nameid=UNKNOWN_ITEM_ID; } else { - nameid=script->conv_num(st,data); - if( nameid <= 0 || !(item_data = itemdb_exists(nameid)) ){ + nameid = script_getnum(st, 2); + if( nameid <= 0 || !(item_data = itemdb->exists(nameid)) ) { ShowError("makeitem: Nonexistant item %d requested.\n", nameid); return false; //No item created. } } - amount=script_getnum(st,3); - mapname =script_getstr(st,4); - x =script_getnum(st,5); - y =script_getnum(st,6); - - if(strcmp(mapname,"this")==0) - { + amount = script_getnum(st,3); + mapname = script_getstr(st,4); + x = script_getnum(st,5); + y = script_getnum(st,6); + + if(strcmp(mapname,"this")==0) { TBL_PC *sd; - sd = script_rid2sd(st); + sd = script->rid2sd(st); if (!sd) return true; //Failed... m=sd->bl.m; } else - m=iMap->mapname2mapid(mapname); - + m=map->mapname2mapid(mapname); + + if( m == -1 ) { + ShowError("makeitem: creating map on unexistent map '%s'!\n", mapname); + return false; + } + + memset(&item_tmp,0,sizeof(item_tmp)); item_tmp.nameid=nameid; if(!flag) item_tmp.identify=1; else - item_tmp.identify=itemdb_isidentified2(item_data); - - iMap->addflooritem(&item_tmp,amount,m,x,y,0,0,0,0); - + item_tmp.identify=itemdb->isidentified2(item_data); + + map->addflooritem(&item_tmp,amount,m,x,y,0,0,0,0); + return true; } @@ -6212,22 +6866,22 @@ BUILDIN(makeitem) /// Counts / deletes the current item given by idx. /// Used by buildin_delitem_search /// Relies on all input data being already fully valid. -static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, bool delete_items) +void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, bool delete_items) { int delamount; struct item* inv = &sd->status.inventory[idx]; - + delamount = ( amount[0] < inv->amount ) ? amount[0] : inv->amount; - + if( delete_items ) { if( sd->inventory_data[idx]->type == IT_PETEGG && inv->card[0] == CARD0_PET ) {// delete associated pet - intif_delete_petdata(MakeDWord(inv->card[1], inv->card[2])); + intif->delete_petdata(MakeDWord(inv->card[1], inv->card[2])); } pc->delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT); } - + amount[0]-= delamount; } @@ -6237,15 +6891,15 @@ static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* am /// Relies on all input data being already fully valid. /// @param exact_match will also match item attributes and cards, not just name id /// @return true when all items could be deleted, false when there were not enough items to delete -static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match) +bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match) { bool delete_items = false; int i, amount, important; struct item* inv; - + // prefer always non-equipped items it->equip = 0; - + // when searching for nameid only, prefer additionally if( !exact_match ) { @@ -6254,28 +6908,28 @@ static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, // card-less items memset(it->card, 0, sizeof(it->card)); } - + for(;;) { amount = it->amount; important = 0; - + // 1st pass -- less important items / exact match for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ ) { inv = &sd->status.inventory[i]; - + if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid ) {// wrong/invalid item continue; } - + if( inv->equip != it->equip || inv->refine != it->refine ) {// not matching attributes important++; continue; } - + if( exact_match ) { if( inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) ) @@ -6287,7 +6941,7 @@ static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, { if( sd->inventory_data[i]->type == IT_PETEGG ) { - if( inv->card[0] == CARD0_PET && CheckForCharServer() ) + if( inv->card[0] == CARD0_PET && intif->CheckForCharServer() ) {// pet which cannot be deleted continue; } @@ -6298,11 +6952,11 @@ static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, continue; } } - + // count / delete item - buildin_delitem_delete(sd, i, &amount, delete_items); + script->buildin_delitem_delete(sd, i, &amount, delete_items); } - + // 2nd pass -- any matching item if( amount == 0 || important == 0 ) {// either everything was already consumed or no items were skipped @@ -6311,17 +6965,17 @@ static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, else for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ ) { inv = &sd->status.inventory[i]; - + if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid ) {// wrong/invalid item continue; } - - if( sd->inventory_data[i]->type == IT_PETEGG && inv->card[0] == CARD0_PET && CheckForCharServer() ) + + if( sd->inventory_data[i]->type == IT_PETEGG && inv->card[0] == CARD0_PET && intif->CheckForCharServer() ) {// pet which cannot be deleted continue; } - + if( exact_match ) { if( inv->refine != it->refine || inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) ) @@ -6329,11 +6983,11 @@ static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, continue; } } - + // count / delete item - buildin_delitem_delete(sd, i, &amount, delete_items); + script->buildin_delitem_delete(sd, i, &amount, delete_items); } - + if( amount ) {// not enough items return false; @@ -6355,16 +7009,14 @@ static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, /// /// delitem <item id>,<amount>{,<account id>} /// delitem "<item name>",<amount>{,<account id>} -BUILDIN(delitem) -{ +BUILDIN(delitem) { TBL_PC *sd; struct item it; - struct script_data *data; - + if( script_hasdata(st,4) ) { int account_id = script_getnum(st,4); - sd = iMap->id2sd(account_id); // <account id> + sd = map->id2sd(account_id); // <account id> if( sd == NULL ) { ShowError("script:delitem: player not found (AID=%d).\n", account_id); @@ -6374,46 +7026,40 @@ BUILDIN(delitem) } else { - sd = script_rid2sd(st);// attached player + sd = script->rid2sd(st);// attached player if( sd == NULL ) return true; } - - data = script_getdata(st,2); - script->get_val(st,data); - if( data_isstring(data) ) - { - const char* item_name = script->conv_str(st,data); - struct item_data* id = itemdb_searchname(item_name); - if( id == NULL ) - { + + if( script_isstringtype(st, 2) ) { + const char* item_name = script_getstr(st, 2); + struct item_data* id = itemdb->search_name(item_name); + if( id == NULL ) { ShowError("script:delitem: unknown item \"%s\".\n", item_name); st->state = END; return false; } it.nameid = id->nameid;// "<item name>" - } - else - { - it.nameid = script->conv_num(st,data);// <item id> - if( !itemdb_exists( it.nameid ) ) + } else { + it.nameid = script_getnum(st, 2);// <item id> + if( !itemdb->exists( it.nameid ) ) { ShowError("script:delitem: unknown item \"%d\".\n", it.nameid); st->state = END; return false; } } - + it.amount=script_getnum(st,3); - + if( it.amount <= 0 ) return true;// nothing to do - - if( buildin_delitem_search(sd, &it, false) ) + + if( script->buildin_delitem_search(sd, &it, false) ) {// success return true; } - + ShowError("script:delitem: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid); st->state = END; clif->scriptclose(sd, st->oid); @@ -6424,18 +7070,14 @@ BUILDIN(delitem) /// /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>} /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>} -BUILDIN(delitem2) -{ +BUILDIN(delitem2) { TBL_PC *sd; struct item it; - struct script_data *data; - - if( script_hasdata(st,11) ) - { + + if( script_hasdata(st,11) ) { int account_id = script_getnum(st,11); - sd = iMap->id2sd(account_id); // <account id> - if( sd == NULL ) - { + sd = map->id2sd(account_id); // <account id> + if( sd == NULL ) { ShowError("script:delitem2: player not found (AID=%d).\n", account_id); st->state = END; return false; @@ -6443,36 +7085,29 @@ BUILDIN(delitem2) } else { - sd = script_rid2sd(st);// attached player + sd = script->rid2sd(st);// attached player if( sd == NULL ) return true; } - - data = script_getdata(st,2); - script->get_val(st,data); - if( data_isstring(data) ) - { - const char* item_name = script->conv_str(st,data); - struct item_data* id = itemdb_searchname(item_name); - if( id == NULL ) - { + + if( script_isstringtype(st, 2) ) { + const char* item_name = script_getstr(st, 2); + struct item_data* id = itemdb->search_name(item_name); + if( id == NULL ) { ShowError("script:delitem2: unknown item \"%s\".\n", item_name); st->state = END; return false; } it.nameid = id->nameid;// "<item name>" - } - else - { - it.nameid = script->conv_num(st,data);// <item id> - if( !itemdb_exists( it.nameid ) ) - { + } else { + it.nameid = script_getnum(st, 2);// <item id> + if( !itemdb->exists( it.nameid ) ) { ShowError("script:delitem: unknown item \"%d\".\n", it.nameid); st->state = END; return false; } } - + it.amount=script_getnum(st,3); it.identify=script_getnum(st,4); it.refine=script_getnum(st,5); @@ -6481,15 +7116,15 @@ BUILDIN(delitem2) it.card[1]=(short)script_getnum(st,8); it.card[2]=(short)script_getnum(st,9); it.card[3]=(short)script_getnum(st,10); - + if( it.amount <= 0 ) return true;// nothing to do - - if( buildin_delitem_search(sd, &it, true) ) + + if( script->buildin_delitem_search(sd, &it, true) ) {// success return true; } - + ShowError("script:delitem2: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid); st->state = END; clif->scriptclose(sd, st->oid); @@ -6502,7 +7137,7 @@ BUILDIN(delitem2) BUILDIN(enableitemuse) { TBL_PC *sd; - sd=script_rid2sd(st); + sd=script->rid2sd(st); if (sd) st->npc_item_flag = sd->npc_item_flag = 1; return true; @@ -6511,7 +7146,7 @@ BUILDIN(enableitemuse) BUILDIN(disableitemuse) { TBL_PC *sd; - sd=script_rid2sd(st); + sd=script->rid2sd(st); if (sd) st->npc_item_flag = sd->npc_item_flag = 0; return true; @@ -6521,52 +7156,50 @@ BUILDIN(disableitemuse) * return the basic stats of sd * chk pc->readparam for available type *------------------------------------------*/ -BUILDIN(readparam) -{ +BUILDIN(readparam) { int type; TBL_PC *sd; - + type=script_getnum(st,2); if( script_hasdata(st,3) ) - sd=iMap->nick2sd(script_getstr(st,3)); + sd=map->nick2sd(script_getstr(st,3)); else - sd=script_rid2sd(st); - - if(sd==NULL){ + sd=script->rid2sd(st); + + if(sd==NULL) { script_pushint(st,-1); return true; } - + script_pushint(st,pc->readparam(sd,type)); - + return true; } /*========================================== * Return charid identification * return by @num : - * 0 : char_id - * 1 : party_id - * 2 : guild_id - * 3 : account_id - * 4 : bg_id + * 0 : char_id + * 1 : party_id + * 2 : guild_id + * 3 : account_id + * 4 : bg_id *------------------------------------------*/ -BUILDIN(getcharid) -{ +BUILDIN(getcharid) { int num; TBL_PC *sd; - + num = script_getnum(st,2); if( script_hasdata(st,3) ) - sd=iMap->nick2sd(script_getstr(st,3)); + sd=map->nick2sd(script_getstr(st,3)); else - sd=script_rid2sd(st); - - if(sd==NULL){ - script_pushint(st,0); //return 0, according docs + sd=script->rid2sd(st); + + if(sd==NULL) { + script_pushint(st,0); //return 0, according docs return true; } - + switch( num ) { case 0: script_pushint(st,sd->status.char_id); break; case 1: script_pushint(st,sd->status.party_id); break; @@ -6578,7 +7211,7 @@ BUILDIN(getcharid) script_pushint(st,0); break; } - + return true; } /*========================================== @@ -6588,17 +7221,17 @@ BUILDIN(getnpcid) { int num = script_getnum(st,2); struct npc_data* nd = NULL; - + if( script_hasdata(st,3) ) {// unique npc name - if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL ) + if( ( nd = npc->name2id(script_getstr(st,3)) ) == NULL ) { ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3)); script_pushint(st,0); return false; } } - + switch (num) { case 0: script_pushint(st,nd ? nd->bl.id : st->oid); @@ -6608,7 +7241,7 @@ BUILDIN(getnpcid) script_pushint(st,0); return false; } - + return true; } @@ -6620,9 +7253,9 @@ BUILDIN(getpartyname) { int party_id; struct party_data* p; - + party_id = script_getnum(st,2); - + if( ( p = party->search(party_id) ) != NULL ) { script_pushstrcopy(st,p->party.name); @@ -6638,39 +7271,39 @@ BUILDIN(getpartyname) * Get the information of the members of a party by type * @party_id, @type * return by @type : - * - : nom des membres - * 1 : char_id des membres - * 2 : account_id des membres + * - : nom des membres + * 1 : char_id des membres + * 2 : account_id des membres *------------------------------------------*/ BUILDIN(getpartymember) { struct party_data *p; int i,j=0,type=0; - + p=party->search(script_getnum(st,2)); - + if( script_hasdata(st,3) ) - type=script_getnum(st,3); - - if(p!=NULL){ - for(i=0;i<MAX_PARTY;i++){ - if(p->party.member[i].account_id){ + type=script_getnum(st,3); + + if(p!=NULL) { + for(i=0;i<MAX_PARTY;i++) { + if(p->party.member[i].account_id) { switch (type) { case 2: - mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id); + mapreg->setreg(reference_uid(script->add_str("$@partymemberaid"), j),p->party.member[i].account_id); break; case 1: - mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id); + mapreg->setreg(reference_uid(script->add_str("$@partymembercid"), j),p->party.member[i].char_id); break; default: - mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name); + mapreg->setregstr(reference_uid(script->add_str("$@partymembername$"), j),p->party.member[i].name); } j++; } } } - mapreg_setreg(add_str("$@partymembercount"),j); - + mapreg->setreg(script->add_str("$@partymembercount"),j); + return true; } @@ -6682,16 +7315,16 @@ BUILDIN(getpartyleader) { int party_id, type = 0, i=0; struct party_data *p; - + party_id=script_getnum(st,2); if( script_hasdata(st,3) ) - type=script_getnum(st,3); - + type=script_getnum(st,3); + p=party->search(party_id); - + if (p) //Search leader for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++); - + if (!p || i == MAX_PARTY) { //leader not found if (type) script_pushint(st,-1); @@ -6699,7 +7332,7 @@ BUILDIN(getpartyleader) script_pushconststr(st,"null"); return true; } - + switch (type) { case 1: script_pushint(st,p->party.member[i].account_id); break; case 2: script_pushint(st,p->party.member[i].char_id); break; @@ -6719,9 +7352,9 @@ BUILDIN(getguildname) { int guild_id; struct guild* g; - + guild_id = script_getnum(st,2); - + if( ( g = guild->search(guild_id) ) != NULL ) { script_pushstrcopy(st,g->name); @@ -6741,9 +7374,9 @@ BUILDIN(getguildmaster) { int guild_id; struct guild* g; - + guild_id = script_getnum(st,2); - + if( ( g = guild->search(guild_id) ) != NULL ) { script_pushstrcopy(st,g->member[0].name); @@ -6759,9 +7392,9 @@ BUILDIN(getguildmasterid) { int guild_id; struct guild* g; - + guild_id = script_getnum(st,2); - + if( ( g = guild->search(guild_id) ) != NULL ) { script_pushint(st,g->member[0].char_id); @@ -6776,11 +7409,11 @@ BUILDIN(getguildmasterid) /*========================================== * Get char string information by type : * Return by @type : - * 0 : char_name - * 1 : party_name or "" - * 2 : guild_name or "" - * 3 : map_name - * - : "" + * 0 : char_name + * 1 : party_name or "" + * 2 : guild_name or "" + * 3 : map_name + * - : "" *------------------------------------------*/ BUILDIN(strcharinfo) { @@ -6788,14 +7421,13 @@ BUILDIN(strcharinfo) int num; struct guild* g; struct party_data* p; - - sd=script_rid2sd(st); - if (!sd) { //Avoid crashing.... - script_pushconststr(st,""); + + sd=script->rid2sd(st); + if (!sd) //Avoid crashing.... return true; - } + num=script_getnum(st,2); - switch(num){ + switch(num) { case 0: script_pushstrcopy(st,sd->status.name); break; @@ -6814,40 +7446,39 @@ BUILDIN(strcharinfo) } break; case 3: - script_pushconststr(st,map[sd->bl.m].name); + script_pushconststr(st,map->list[sd->bl.m].name); break; default: ShowWarning("buildin_strcharinfo: unknown parameter.\n"); script_pushconststr(st,""); break; } - + return true; } /*========================================== * Get npc string information by type * return by @type: - * 0 : name - * 1 : str# - * 2 : #str - * 3 : ::str - * 4 : map name + * 0 : name + * 1 : str# + * 2 : #str + * 3 : ::str + * 4 : map name *------------------------------------------*/ -BUILDIN(strnpcinfo) -{ +BUILDIN(strnpcinfo) { TBL_NPC* nd; int num; char *buf,*name=NULL; - - nd = iMap->id2nd(st->oid); + + nd = map->id2nd(st->oid); if (!nd) { script_pushconststr(st, ""); return true; } - + num = script_getnum(st,2); - switch(num){ + switch(num) { case 0: // display name name = aStrdup(nd->name); break; @@ -6867,56 +7498,53 @@ BUILDIN(strnpcinfo) name = aStrdup(nd->exname); break; case 4: // map name - name = aStrdup(map[nd->bl.m].name); + if( nd->bl.m >= 0 ) // Only valid map indexes allowed (issue:8034) + name = aStrdup(map->list[nd->bl.m].name); break; } - + if(name) script_pushstr(st, name); else script_pushconststr(st, ""); - + return true; } - -// aegis->athena slot position conversion table -static unsigned int equip[] = {EQP_HEAD_TOP,EQP_ARMOR,EQP_HAND_L,EQP_HAND_R,EQP_GARMENT,EQP_SHOES,EQP_ACC_L,EQP_ACC_R,EQP_HEAD_MID,EQP_HEAD_LOW,EQP_COSTUME_HEAD_LOW,EQP_COSTUME_HEAD_MID,EQP_COSTUME_HEAD_TOP,EQP_COSTUME_GARMENT}; - /*========================================== - * GetEquipID(Pos); Pos: 1-10 + * GetEquipID(Pos); Pos: 1-SCRIPT_EQUIP_TABLE_SIZE *------------------------------------------*/ BUILDIN(getequipid) { int i, num; TBL_PC* sd; struct item_data* item; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - + num = script_getnum(st,2) - 1; - if( num < 0 || num >= ARRAYLENGTH(equip) ) + if( num < 0 || num >= ARRAYLENGTH(script->equip) ) { script_pushint(st,-1); return true; } - + // get inventory position of item - i = pc->checkequip(sd,equip[num]); + i = pc->checkequip(sd,script->equip[num]); if( i < 0 ) { script_pushint(st,-1); return true; } - + item = sd->inventory_data[i]; if( item != 0 ) script_pushint(st,item->nameid); else script_pushint(st,0); - + return true; } @@ -6929,32 +7557,32 @@ BUILDIN(getequipname) int i, num; TBL_PC* sd; struct item_data* item; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - + num = script_getnum(st,2) - 1; - if( num < 0 || num >= ARRAYLENGTH(equip) ) + if( num < 0 || num >= ARRAYLENGTH(script->equip) ) { script_pushconststr(st,""); return true; } - + // get inventory position of item - i = pc->checkequip(sd,equip[num]); + i = pc->checkequip(sd,script->equip[num]); if( i < 0 ) { script_pushconststr(st,""); return true; } - + item = sd->inventory_data[i]; if( item != 0 ) script_pushstrcopy(st,item->jname); else script_pushconststr(st,""); - + return true; } @@ -6965,24 +7593,24 @@ BUILDIN(getbrokenid) { int i,num,id=0,brokencounter=0; TBL_PC *sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - + num=script_getnum(st,2); for(i=0; i<MAX_INVENTORY; i++) { - if(sd->status.inventory[i].attribute){ + if(sd->status.inventory[i].attribute) { brokencounter++; - if(num==brokencounter){ + if(num==brokencounter) { id=sd->status.inventory[i].nameid; break; } } } - + script_pushint(st,id); - + return true; } @@ -6994,16 +7622,16 @@ BUILDIN(repair) int i,num; int repaircounter=0; TBL_PC *sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - + num=script_getnum(st,2); for(i=0; i<MAX_INVENTORY; i++) { - if(sd->status.inventory[i].attribute){ + if(sd->status.inventory[i].attribute) { repaircounter++; - if(num==repaircounter){ + if(num==repaircounter) { sd->status.inventory[i].attribute=0; clif->equiplist(sd); clif->produce_effect(sd, 0, sd->status.inventory[i].nameid); @@ -7012,7 +7640,7 @@ BUILDIN(repair) } } } - + return true; } @@ -7023,11 +7651,11 @@ BUILDIN(repairall) { int i, repaircounter = 0; TBL_PC *sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if(sd == NULL) return true; - + for(i = 0; i < MAX_INVENTORY; i++) { if(sd->status.inventory[i].nameid && sd->status.inventory[i].attribute) @@ -7037,13 +7665,13 @@ BUILDIN(repairall) repaircounter++; } } - + if(repaircounter) { clif->misceffect(&sd->bl, 3); clif->equiplist(sd); } - + return true; } @@ -7054,15 +7682,15 @@ BUILDIN(getequipisequiped) { int i = -1,num; TBL_PC *sd; - + num = script_getnum(st,2); - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) return true; - - if (num > 0 && num <= ARRAYLENGTH(equip)) - i=pc->checkequip(sd,equip[num-1]); - + + if (num > 0 && num <= ARRAYLENGTH(script->equip)) + i=pc->checkequip(sd,script->equip[num-1]); + if(i >= 0) script_pushint(st,1); else @@ -7074,78 +7702,78 @@ BUILDIN(getequipisequiped) * Chk if the player have something equiped at pos * if so chk if this item ain't marked not refinable or rental * return (npc) - * 1 : true - * 0 : false + * 1 : true + * 0 : false *------------------------------------------*/ BUILDIN(getequipisenableref) { int i = -1,num; TBL_PC *sd; - + num = script_getnum(st,2); - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) return true; - - if( num > 0 && num <= ARRAYLENGTH(equip) ) - i = pc->checkequip(sd,equip[num-1]); + + if( num > 0 && num <= ARRAYLENGTH(script->equip) ) + i = pc->checkequip(sd,script->equip[num-1]); if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time ) script_pushint(st,1); else script_pushint(st,0); - + return true; } /*========================================== * Chk if the item equiped at pos is identify (huh ?) * return (npc) - * 1 : true - * 0 : false + * 1 : true + * 0 : false *------------------------------------------*/ BUILDIN(getequipisidentify) { int i = -1,num; TBL_PC *sd; - + num = script_getnum(st,2); - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) return true; - - if (num > 0 && num <= ARRAYLENGTH(equip)) - i=pc->checkequip(sd,equip[num-1]); + + if (num > 0 && num <= ARRAYLENGTH(script->equip)) + i=pc->checkequip(sd,script->equip[num-1]); if(i >= 0) script_pushint(st,sd->status.inventory[i].identify); else script_pushint(st,0); - + return true; } /*========================================== * Get the item refined value at pos * return (npc) - * x : refine amount - * 0 : false (not refined) + * x : refine amount + * 0 : false (not refined) *------------------------------------------*/ BUILDIN(getequiprefinerycnt) { int i = -1,num; TBL_PC *sd; - + num = script_getnum(st,2); - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) return true; - - if (num > 0 && num <= ARRAYLENGTH(equip)) - i=pc->checkequip(sd,equip[num-1]); + + if (num > 0 && num <= ARRAYLENGTH(script->equip)) + i=pc->checkequip(sd,script->equip[num-1]); if(i >= 0) script_pushint(st,sd->status.inventory[i].refine); else script_pushint(st,0); - + return true; } @@ -7153,96 +7781,98 @@ BUILDIN(getequiprefinerycnt) * Get the weapon level value at pos * (pos should normally only be EQI_HAND_L or EQI_HAND_R) * return (npc) - * x : weapon level - * 0 : false + * x : weapon level + * 0 : false *------------------------------------------*/ BUILDIN(getequipweaponlv) { int i = -1,num; TBL_PC *sd; - + num = script_getnum(st,2); - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) return true; - - if (num > 0 && num <= ARRAYLENGTH(equip)) - i=pc->checkequip(sd,equip[num-1]); + + if (num > 0 && num <= ARRAYLENGTH(script->equip)) + i=pc->checkequip(sd,script->equip[num-1]); if(i >= 0 && sd->inventory_data[i]) script_pushint(st,sd->inventory_data[i]->wlv); else script_pushint(st,0); - + return true; } /*========================================== * Get the item refine chance (from refine.txt) for item at pos * return (npc) - * x : refine chance - * 0 : false (max refine level or unequip..) + * x : refine chance + * 0 : false (max refine level or unequip..) *------------------------------------------*/ -BUILDIN(getequippercentrefinery) -{ +BUILDIN(getequippercentrefinery) { int i = -1,num; TBL_PC *sd; - + num = script_getnum(st,2); - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) return true; - - if (num > 0 && num <= ARRAYLENGTH(equip)) - i=pc->checkequip(sd,equip[num-1]); + + if (num > 0 && num <= ARRAYLENGTH(script->equip)) + i=pc->checkequip(sd,script->equip[num-1]); if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE) - script_pushint(st,status_get_refine_chance(itemdb_wlv(sd->status.inventory[i].nameid), (int)sd->status.inventory[i].refine)); + script_pushint(st,status->get_refine_chance(itemdb_wlv(sd->status.inventory[i].nameid), (int)sd->status.inventory[i].refine)); else script_pushint(st,0); - + return true; } /*========================================== * Refine +1 item at pos and log and display refine *------------------------------------------*/ -BUILDIN(successrefitem) -{ - int i=-1,num,ep; +BUILDIN(successrefitem) { + int i = -1 , num, ep, up = 1; TBL_PC *sd; - + num = script_getnum(st,2); - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) return true; + + if( script_hasdata(st, 3) ) + up = script_getnum(st, 3); - if (num > 0 && num <= ARRAYLENGTH(equip)) - i=pc->checkequip(sd,equip[num-1]); + if (num > 0 && num <= ARRAYLENGTH(script->equip)) + i=pc->checkequip(sd,script->equip[num-1]); if(i >= 0) { ep=sd->status.inventory[i].equip; - + //Logs items, got from (N)PC scripts [Lupus] logs->pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i],sd->inventory_data[i]); - + if (sd->status.inventory[i].refine >= MAX_REFINE) return true; - - sd->status.inventory[i].refine++; + + sd->status.inventory[i].refine += up; + sd->status.inventory[i].refine = cap_value( sd->status.inventory[i].refine, 0, MAX_REFINE); pc->unequipitem(sd,i,2); // status calc will happen in pc->equipitem() below - + clif->refine(sd->fd,0,i,sd->status.inventory[i].refine); clif->delitem(sd,i,1,3); - + //Logs items, got from (N)PC scripts [Lupus] logs->pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i],sd->inventory_data[i]); - + clif->additem(sd,i,1,0); pc->equipitem(sd,i,ep); clif->misceffect(&sd->bl,3); if(sd->status.inventory[i].refine == 10 && sd->status.inventory[i].card[0] == CARD0_FORGE && sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3]) - ){ // Fame point system [DracoRPG] - switch (sd->inventory_data[i]->wlv){ + ) { // Fame point system [DracoRPG] + switch (sd->inventory_data[i]->wlv) { case 1: pc->addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point break; @@ -7255,7 +7885,7 @@ BUILDIN(successrefitem) } } } - + return true; } @@ -7266,24 +7896,24 @@ BUILDIN(failedrefitem) { int i=-1,num; TBL_PC *sd; - + num = script_getnum(st,2); - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) return true; - - if (num > 0 && num <= ARRAYLENGTH(equip)) - i=pc->checkequip(sd,equip[num-1]); + + if (num > 0 && num <= ARRAYLENGTH(script->equip)) + i=pc->checkequip(sd,script->equip[num-1]); if(i >= 0) { sd->status.inventory[i].refine = 0; pc->unequipitem(sd,i,3); //recalculate bonus clif->refine(sd->fd,1,i,sd->status.inventory[i].refine); //notify client of failure - + pc->delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT); - - clif->misceffect(&sd->bl,2); // display failure effect + + clif->misceffect(&sd->bl,2); // display failure effect } - + return true; } @@ -7292,36 +7922,39 @@ BUILDIN(failedrefitem) *------------------------------------------*/ BUILDIN(downrefitem) { - int i = -1,num,ep; + int i = -1,num,ep, down = 1; TBL_PC *sd; - - num = script_getnum(st,2); - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - - if (num > 0 && num <= ARRAYLENGTH(equip)) - i = pc->checkequip(sd,equip[num-1]); + num = script_getnum(st,2); + if( script_hasdata(st, 3) ) + down = script_getnum(st, 3); + + if (num > 0 && num <= ARRAYLENGTH(script->equip)) + i = pc->checkequip(sd,script->equip[num-1]); if(i >= 0) { ep = sd->status.inventory[i].equip; - + //Logs items, got from (N)PC scripts [Lupus] logs->pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i],sd->inventory_data[i]); - - sd->status.inventory[i].refine++; + pc->unequipitem(sd,i,2); // status calc will happen in pc->equipitem() below - - clif->refine(sd->fd,2,i,sd->status.inventory[i].refine = sd->status.inventory[i].refine - 2); + sd->status.inventory[i].refine -= down; + sd->status.inventory[i].refine = cap_value( sd->status.inventory[i].refine, 0, MAX_REFINE); + + clif->refine(sd->fd,2,i,sd->status.inventory[i].refine); clif->delitem(sd,i,1,3); - + //Logs items, got from (N)PC scripts [Lupus] logs->pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i],sd->inventory_data[i]); - + clif->additem(sd,i,1,0); pc->equipitem(sd,i,ep); clif->misceffect(&sd->bl,2); } - + return true; } @@ -7332,37 +7965,39 @@ BUILDIN(delequip) { int i=-1,num; TBL_PC *sd; - + num = script_getnum(st,2); - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) return true; - - if (num > 0 && num <= ARRAYLENGTH(equip)) - i=pc->checkequip(sd,equip[num-1]); + + if (num > 0 && num <= ARRAYLENGTH(script->equip)) + i=pc->checkequip(sd,script->equip[num-1]); if(i >= 0) { pc->unequipitem(sd,i,3); //recalculate bonus pc->delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT); + script_pushint(st,1); + } else { + script_pushint(st,0); } - + return true; } /*========================================== * *------------------------------------------*/ -BUILDIN(statusup) -{ +BUILDIN(statusup) { int type; TBL_PC *sd; - + type=script_getnum(st,2); - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) return true; - - pc->statusup(sd,type); - + + pc->statusup(sd, type, 1); + return true; } /*========================================== @@ -7372,15 +8007,15 @@ BUILDIN(statusup2) { int type,val; TBL_PC *sd; - + type=script_getnum(st,2); val=script_getnum(st,3); - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) return true; - + pc->statusup2(sd,type,val); - + return true; } @@ -7391,8 +8026,7 @@ BUILDIN(statusup2) /// bonus3 <bonus type>,<val1>,<val2>,<val3>; /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>; /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>; -BUILDIN(bonus) -{ +BUILDIN(bonus) { int type; int val1; int val2 = 0; @@ -7400,11 +8034,11 @@ BUILDIN(bonus) int val4 = 0; int val5 = 0; TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; // no player attached - + type = script_getnum(st,2); switch( type ) { case SP_AUTOSPELL: @@ -7424,13 +8058,16 @@ BUILDIN(bonus) case SP_FIXCASTRATE: case SP_SKILL_USE_SP: // these bonuses support skill names - val1 = ( script_isstring(st,3) ? skill->name2id(script_getstr(st,3)) : script_getnum(st,3) ); - break; + if (script_isstringtype(st, 3)) { + val1 = skill->name2id(script_getstr(st, 3)); + break; + } + // else fall through default: val1 = script_getnum(st,3); break; } - + switch( script_lastdata(st)-2 ) { case 1: pc->bonus(sd, type, val1); @@ -7445,21 +8082,21 @@ BUILDIN(bonus) pc->bonus3(sd, type, val1, val2, val3); break; case 4: - if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) ) + if( type == SP_AUTOSPELL_ONSKILL && script_isstringtype(st,4) ) val2 = skill->name2id(script_getstr(st,4)); // 2nd value can be skill name else val2 = script_getnum(st,4); - + val3 = script_getnum(st,5); val4 = script_getnum(st,6); pc->bonus4(sd, type, val1, val2, val3, val4); break; case 5: - if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) ) + if( type == SP_AUTOSPELL_ONSKILL && script_isstringtype(st,4) ) val2 = skill->name2id(script_getstr(st,4)); // 2nd value can be skill name else val2 = script_getnum(st,4); - + val3 = script_getnum(st,5); val4 = script_getnum(st,6); val5 = script_getnum(st,7); @@ -7469,116 +8106,113 @@ BUILDIN(bonus) ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1)); break; } - + return true; } -BUILDIN(autobonus) -{ +BUILDIN(autobonus) { unsigned int dur; short rate; short atk_type = 0; TBL_PC* sd; const char *bonus_script, *other_script = NULL; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; // no player attached - - if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip ) + + if( sd->state.autobonus&sd->status.inventory[status->current_equip_item_index].equip ) return true; - + rate = script_getnum(st,3); dur = script_getnum(st,4); bonus_script = script_getstr(st,2); if( !rate || !dur || !bonus_script ) return true; - + if( script_hasdata(st,5) ) atk_type = script_getnum(st,5); if( script_hasdata(st,6) ) other_script = script_getstr(st,6); - - if( pc->addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus), - bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) ) - { - script_add_autobonus(bonus_script); + + if( pc->addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),bonus_script,rate,dur,atk_type,other_script, + sd->status.inventory[status->current_equip_item_index].equip,false) + ) { + script->add_autobonus(bonus_script); if( other_script ) - script_add_autobonus(other_script); + script->add_autobonus(other_script); } - + return true; } -BUILDIN(autobonus2) -{ +BUILDIN(autobonus2) { unsigned int dur; short rate; short atk_type = 0; TBL_PC* sd; const char *bonus_script, *other_script = NULL; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; // no player attached - - if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip ) + + if( sd->state.autobonus&sd->status.inventory[status->current_equip_item_index].equip ) return true; - + rate = script_getnum(st,3); dur = script_getnum(st,4); bonus_script = script_getstr(st,2); if( !rate || !dur || !bonus_script ) return true; - + if( script_hasdata(st,5) ) atk_type = script_getnum(st,5); if( script_hasdata(st,6) ) other_script = script_getstr(st,6); - - if( pc->addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2), - bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) ) - { - script_add_autobonus(bonus_script); + + if( pc->addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),bonus_script,rate,dur,atk_type,other_script, + sd->status.inventory[status->current_equip_item_index].equip,false) + ) { + script->add_autobonus(bonus_script); if( other_script ) - script_add_autobonus(other_script); + script->add_autobonus(other_script); } - + return true; } -BUILDIN(autobonus3) -{ +BUILDIN(autobonus3) { unsigned int dur; short rate,atk_type; TBL_PC* sd; const char *bonus_script, *other_script = NULL; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; // no player attached - - if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip ) + + if( sd->state.autobonus&sd->status.inventory[status->current_equip_item_index].equip ) return true; - + rate = script_getnum(st,3); dur = script_getnum(st,4); - atk_type = ( script_isstring(st,5) ? skill->name2id(script_getstr(st,5)) : script_getnum(st,5) ); + atk_type = ( script_isstringtype(st,5) ? skill->name2id(script_getstr(st,5)) : script_getnum(st,5) ); bonus_script = script_getstr(st,2); if( !rate || !dur || !atk_type || !bonus_script ) return true; - + if( script_hasdata(st,6) ) other_script = script_getstr(st,6); - - if( pc->addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3), - bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,true) ) - { - script_add_autobonus(bonus_script); + + if( pc->addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),bonus_script,rate,dur,atk_type,other_script, + sd->status.inventory[status->current_equip_item_index].equip,true) + ) { + script->add_autobonus(bonus_script); if( other_script ) - script_add_autobonus(other_script); + script->add_autobonus(other_script); } - + return true; } @@ -7592,23 +8226,22 @@ BUILDIN(autobonus3) /// skill <skill id>,<level> /// skill "<skill name>",<level>,<flag> /// skill "<skill name>",<level> -BUILDIN(skill) -{ +BUILDIN(skill) { int id; int level; int flag = 1; TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached, report source - - id = ( script_isstring(st,2) ? skill->name2id(script_getstr(st,2)) : script_getnum(st,2) ); + + id = ( script_isstringtype(st,2) ? skill->name2id(script_getstr(st,2)) : script_getnum(st,2) ); level = script_getnum(st,3); if( script_hasdata(st,4) ) flag = script_getnum(st,4); pc->skill(sd, id, level, flag); - + return true; } @@ -7621,23 +8254,22 @@ BUILDIN(skill) /// addtoskill "<skill name>",<amount> /// /// @see skill -BUILDIN(addtoskill) -{ +BUILDIN(addtoskill) { int id; int level; int flag = 2; TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached, report source - - id = ( script_isstring(st,2) ? skill->name2id(script_getstr(st,2)) : script_getnum(st,2) ); + + id = ( script_isstringtype(st,2) ? skill->name2id(script_getstr(st,2)) : script_getnum(st,2) ); level = script_getnum(st,3); if( script_hasdata(st,4) ) flag = script_getnum(st,4); pc->skill(sd, id, level, flag); - + return true; } @@ -7645,22 +8277,37 @@ BUILDIN(addtoskill) /// /// guildskill <skill id>,<amount>; /// guildskill "<skill name>",<amount>; -BUILDIN(guildskill) -{ - int id; +BUILDIN(guildskill) { + int skill_id, id, max_points; int level; + TBL_PC* sd; - int i; - - sd = script_rid2sd(st); + struct guild *gd; + struct guild_skill gd_skill; + + sd = script->rid2sd(st); if( sd == NULL ) - return true;// no player attached, report source - - id = ( script_isstring(st,2) ? skill->name2id(script_getstr(st,2)) : script_getnum(st,2) ); + return false; // no player attached, report source + + if( (gd = sd->guild) == NULL ) + return true; + + skill_id = ( script_isstringtype(st,2) ? skill->name2id(script_getstr(st,2)) : script_getnum(st,2) ); level = script_getnum(st,3); - for( i=0; i < level; i++ ) - guild->skillup(sd, id); - + + id = skill_id - GD_SKILLBASE; + max_points = guild->skill_get_max(skill_id); + + if( (gd->skill[id].lv + level) > max_points ) + level = max_points - gd->skill[id].lv; + + if( level <= 0 ) + return true; + + memcpy(&gd_skill, &(gd->skill[id]), sizeof(gd->skill[id])); + gd_skill.lv += level; + + intif->guild_change_basicinfo( gd->guild_id, GBI_SKILLLV, &(gd_skill), sizeof(gd_skill) ); return true; } @@ -7668,18 +8315,17 @@ BUILDIN(guildskill) /// /// getskilllv(<skill id>) -> <level> /// getskilllv("<skill name>") -> <level> -BUILDIN(getskilllv) -{ +BUILDIN(getskilllv) { int id; TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached, report source - - id = ( script_isstring(st,2) ? skill->name2id(script_getstr(st,2)) : script_getnum(st,2) ); + + id = ( script_isstringtype(st,2) ? skill->name2id(script_getstr(st,2)) : script_getnum(st,2) ); script_pushint(st, pc->checkskill(sd,id)); - + return true; } @@ -7687,20 +8333,19 @@ BUILDIN(getskilllv) /// /// getgdskilllv(<guild id>,<skill id>) -> <level> /// getgdskilllv(<guild id>,"<skill name>") -> <level> -BUILDIN(getgdskilllv) -{ +BUILDIN(getgdskilllv) { int guild_id; uint16 skill_id; struct guild* g; - + guild_id = script_getnum(st,2); - skill_id = ( script_isstring(st,3) ? skill->name2id(script_getstr(st,3)) : script_getnum(st,3) ); + skill_id = ( script_isstringtype(st,3) ? skill->name2id(script_getstr(st,3)) : script_getnum(st,3) ); g = guild->search(guild_id); if( g == NULL ) script_pushint(st, -1); else script_pushint(st, guild->checkskill(g,skill_id)); - + return true; } @@ -7721,13 +8366,13 @@ BUILDIN(basicskillcheck) BUILDIN(getgmlevel) { TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached, report source - - script_pushint(st, pc->get_group_level(sd)); - + + script_pushint(st, pc_get_group_level(sd)); + return true; } @@ -7737,12 +8382,12 @@ BUILDIN(getgmlevel) BUILDIN(getgroupid) { TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if (sd == NULL) return false; // no player attached, report source script_pushint(st, pc_get_group_id(sd)); - + return true; } @@ -7751,6 +8396,18 @@ BUILDIN(getgroupid) /// end BUILDIN(end) { st->state = END; + + /* are we stopping inside a function? */ + if( st->stack->defsp >= 1 && st->stack->stack_data[st->stack->defsp-1].type == C_RETINFO ) { + int i; + for(i = 0; i < st->stack->sp; i++) { + if( st->stack->stack_data[i].type == C_RETINFO ) {/* grab the first, aka the original */ + struct script_retinfo *ri = st->stack->stack_data[i].u.ri; + st->script = ri->script; + break; + } + } + } return true; } @@ -7761,17 +8418,17 @@ BUILDIN(checkoption) { int option; TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached, report source - + option = script_getnum(st,2); if( sd->sc.option&option ) script_pushint(st, 1); else script_pushint(st, 0); - + return true; } @@ -7782,17 +8439,17 @@ BUILDIN(checkoption1) { int opt1; TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached, report source - + opt1 = script_getnum(st,2); if( sd->sc.opt1 == opt1 ) script_pushint(st, 1); else script_pushint(st, 0); - + return true; } @@ -7803,17 +8460,17 @@ BUILDIN(checkoption2) { int opt2; TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached, report source - + opt2 = script_getnum(st,2); if( sd->sc.opt2&opt2 ) script_pushint(st, 1); else script_pushint(st, 0); - + return true; } @@ -7829,28 +8486,28 @@ BUILDIN(setoption) int option; int flag = 1; TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached, report source - + option = script_getnum(st,2); if( script_hasdata(st,3) ) flag = script_getnum(st,3); - else if( !option ){// Request to remove everything. + else if( !option ) {// Request to remove everything. flag = 0; option = OPTION_FALCON|OPTION_RIDING; #ifndef NEW_CARTS option |= OPTION_CART; #endif } - if( flag ){// Add option + if( flag ) {// Add option if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay ) option &= ~OPTION_WEDDING;// Do not show the wedding sprites pc->setoption(sd, sd->sc.option|option); } else// Remove option pc->setoption(sd, sd->sc.option&~option); - + return true; } @@ -7862,16 +8519,16 @@ BUILDIN(setoption) BUILDIN(checkcart) { TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached, report source - + if( pc_iscarton(sd) ) script_pushint(st, 1); else script_pushint(st, 0); - + return true; } @@ -7890,15 +8547,15 @@ BUILDIN(setcart) { int type = 1; TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached, report source - + if( script_hasdata(st,2) ) type = script_getnum(st,2); pc->setcart(sd, type); - + return true; } @@ -7910,16 +8567,16 @@ BUILDIN(setcart) BUILDIN(checkfalcon) { TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached, report source - + if( pc_isfalcon(sd) ) script_pushint(st, 1); else script_pushint(st, 0); - + return true; } @@ -7932,16 +8589,16 @@ BUILDIN(setfalcon) { int flag = 1; TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached, report source - + if( script_hasdata(st,2) ) flag = script_getnum(st,2); - + pc->setfalcon(sd, flag); - + return true; } @@ -7953,16 +8610,16 @@ BUILDIN(setfalcon) BUILDIN(checkriding) { TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached, report source - + if( pc_isriding(sd) || pc_isridingwug(sd) || pc_isridingdragon(sd) ) script_pushint(st, 1); else script_pushint(st, 0); - + return true; } @@ -7975,15 +8632,15 @@ BUILDIN(setriding) { int flag = 1; TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached, report source - + if( script_hasdata(st,2) ) flag = script_getnum(st,2); pc->setriding(sd, flag); - + return true; } @@ -7994,16 +8651,16 @@ BUILDIN(setriding) BUILDIN(checkwug) { TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached, report source - + if( pc_iswug(sd) || pc_isridingwug(sd) ) script_pushint(st, 1); else script_pushint(st, 0); - + return true; } @@ -8014,16 +8671,16 @@ BUILDIN(checkwug) BUILDIN(checkmadogear) { TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached, report source - + if( pc_ismadogear(sd) ) script_pushint(st, 1); else script_pushint(st, 0); - + return true; } @@ -8036,15 +8693,15 @@ BUILDIN(setmadogear) { int flag = 1; TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached, report source - + if( script_hasdata(st,2) ) flag = script_getnum(st,2); pc->setmadogear(sd, flag); - + return true; } @@ -8052,40 +8709,38 @@ BUILDIN(setmadogear) /// /// save "<map name>",<x>,<y> /// savepoint "<map name>",<x>,<y> -BUILDIN(savepoint) -{ +BUILDIN(savepoint) { int x; int y; - short map; + short mapid; const char* str; TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) - return true;// no player attached, report source - - str = script_getstr(st, 2); - x = script_getnum(st,3); - y = script_getnum(st,4); - map = mapindex_name2id(str); - if( map ) - pc->setsavepoint(sd, map, x, y); - + return false;// no player attached, report source + + str = script_getstr(st,2); + x = script_getnum(st,3); + y = script_getnum(st,4); + mapid = script->mapindexname2id(st,str); + if( mapid ) + pc->setsavepoint(sd, mapid, x, y); + return true; } /*========================================== * GetTimeTick(0: System Tick, 1: Time Second Tick) *------------------------------------------*/ -BUILDIN(gettimetick) /* Asgard Version */ -{ +BUILDIN(gettimetick) { /* Asgard Version */ int type; - time_t timer; + time_t clock; struct tm *t; - + type=script_getnum(st,2); - - switch(type){ + + switch(type) { case 2: //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC // from the system clock.) @@ -8093,37 +8748,35 @@ BUILDIN(gettimetick) /* Asgard Version */ break; case 1: //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59) - time(&timer); - t=localtime(&timer); + time(&clock); + t=localtime(&clock); script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec)); break; case 0: default: //type 0:(System Ticks) - script_pushint(st,iTimer->gettick()); + script_pushint(st,(int)timer->gettick()); // TODO: change this to int64 when we'll support 64 bit script values break; } return true; } - /*========================================== * GetTime(Type); * 1: Sec 2: Min 3: Hour * 4: WeekDay 5: MonthDay 6: Month * 7: Year *------------------------------------------*/ -BUILDIN(gettime) /* Asgard Version */ -{ +BUILDIN(gettime) { /* Asgard Version */ int type; - time_t timer; + time_t clock; struct tm *t; - + type=script_getnum(st,2); - - time(&timer); - t=localtime(&timer); - - switch(type){ + + time(&clock); + t=localtime(&clock); + + switch(type) { case 1://Sec(0~59) script_pushint(st,t->tm_sec); break; @@ -8164,14 +8817,14 @@ BUILDIN(gettimestr) const char *fmtstr; int maxlen; time_t now = time(NULL); - + fmtstr=script_getstr(st,2); maxlen=script_getnum(st,3); - + tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char)); strftime(tmpstr,maxlen,fmtstr,localtime(&now)); tmpstr[maxlen]='\0'; - + script_pushstr(st,tmpstr); return true; } @@ -8179,28 +8832,26 @@ BUILDIN(gettimestr) /*========================================== * Open player storage *------------------------------------------*/ -BUILDIN(openstorage) -{ +BUILDIN(openstorage) { TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - - storage_storageopen(sd); + + storage->open(sd); return true; } -BUILDIN(guildopenstorage) -{ +BUILDIN(guildopenstorage) { TBL_PC* sd; int ret; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - - ret = storage_guild_storageopen(sd); + + ret = gstorage->open(sd); script_pushint(st,ret); return true; } @@ -8214,12 +8865,12 @@ BUILDIN(itemskill) { int id; int lv; TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER ) return true; - - id = ( script_isstring(st,2) ? skill->name2id(script_getstr(st,2)) : script_getnum(st,2) ); + + id = ( script_isstringtype(st,2) ? skill->name2id(script_getstr(st,2)) : script_getnum(st,2) ); lv = script_getnum(st,3); /* temporarily disabled, awaiting for kenpachi to detail this so we can make it work properly */ #if 0 @@ -8240,11 +8891,11 @@ BUILDIN(produce) { int trigger; TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - + trigger=script_getnum(st,2); clif->skill_produce_mix_list(sd, -1, trigger); return true; @@ -8256,11 +8907,11 @@ BUILDIN(cooking) { int trigger; TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - + trigger=script_getnum(st,2); clif->cooking_list(sd, trigger, AM_PHARMACY, 1, 1); return true; @@ -8272,25 +8923,24 @@ BUILDIN(makepet) { TBL_PC* sd; int id,pet_id; - + id=script_getnum(st,2); - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) return true; - - pet_id = search_petDB_index(id, PET_CLASS); - + + pet_id = pet->search_petDB_index(id, PET_CLASS); + if (pet_id < 0) - pet_id = search_petDB_index(id, PET_EGG); + pet_id = pet->search_petDB_index(id, PET_EGG); if (pet_id >= 0 && sd) { - sd->catch_target_class = pet_db[pet_id].class_; - intif_create_pet( - sd->status.account_id, sd->status.char_id, - (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv, - (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate, - 100, 0, 1, pet_db[pet_id].jname); + sd->catch_target_class = pet->db[pet_id].class_; + intif->create_pet(sd->status.account_id, sd->status.char_id, + (short)pet->db[pet_id].class_, (short)mob->db(pet->db[pet_id].class_)->lv, + (short)pet->db[pet_id].EggID, 0, (short)pet->db[pet_id].intimate, + 100, 0, 1, pet->db[pet_id].jname); } - + return true; } /*========================================== @@ -8301,23 +8951,23 @@ BUILDIN(getexp) TBL_PC* sd; int base=0,job=0; double bonus; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - + base=script_getnum(st,2); job =script_getnum(st,3); if(base<0 || job<0) return true; - + // bonus for npc-given exp bonus = battle_config.quest_exp_rate / 100.; base = (int) cap_value(base * bonus, 0, INT_MAX); job = (int) cap_value(job * bonus, 0, INT_MAX); - - pc->gainexp(sd, NULL, base, job, true); - + + pc->gainexp(sd, &sd->bl, base, job, true); + return true; } @@ -8328,71 +8978,70 @@ BUILDIN(guildgetexp) { TBL_PC* sd; int exp; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - + exp = script_getnum(st,2); if(exp < 0) return true; if(sd && sd->status.guild_id > 0) guild->getexp (sd, exp); - + return true; } /*========================================== * Changes the guild master of a guild [Skotlex] *------------------------------------------*/ -BUILDIN(guildchangegm) -{ +BUILDIN(guildchangegm) { TBL_PC *sd; int guild_id; const char *name; - + guild_id = script_getnum(st,2); name = script_getstr(st,3); - sd=iMap->nick2sd(name); - + sd=map->nick2sd(name); + if (!sd) script_pushint(st,0); else script_pushint(st,guild->gm_change(guild_id, sd)); - + return true; } /*========================================== * Spawn a monster : - @mapn,x,y : location - @str : monster name - @class_ : mob_id - @amount : nb to spawn - @event : event to attach to mob + * @mapn,x,y : location + * @str : monster name + * @class_ : mob_id + * @amount : nb to spawn + * @event : event to attach to mob *------------------------------------------*/ BUILDIN(monster) { - const char* mapn = script_getstr(st,2); - int x = script_getnum(st,3); - int y = script_getnum(st,4); - const char* str = script_getstr(st,5); - int class_ = script_getnum(st,6); - int amount = script_getnum(st,7); - const char* event = ""; - unsigned int size = SZ_SMALL; - unsigned int ai = AI_NONE; + const char *mapn = script_getstr(st,2); + int x = script_getnum(st,3); + int y = script_getnum(st,4); + const char *str = script_getstr(st,5); + int class_ = script_getnum(st,6); + int amount = script_getnum(st,7); + const char *event = ""; + unsigned int size = SZ_MEDIUM; + unsigned int ai = AI_NONE; int mob_id; - + struct map_session_data* sd; int16 m; - + if (script_hasdata(st, 8)) { event = script_getstr(st, 8); - check_event(st, event); + script->check_event(st, event); } - + if (script_hasdata(st, 9)) { size = script_getnum(st, 9); @@ -8402,7 +9051,7 @@ BUILDIN(monster) return false; } } - + if (script_hasdata(st, 10)) { ai = script_getnum(st, 10); @@ -8412,28 +9061,32 @@ BUILDIN(monster) return false; } } - - if (class_ >= 0 && !mobdb_checkid(class_)) + + if (class_ >= 0 && !mob->db_checkid(class_)) { ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_); return false; } - - sd = iMap->id2sd(st->rid); - + + sd = map->id2sd(st->rid); + if (sd && strcmp(mapn, "this") == 0) m = sd->bl.m; else { - m = iMap->mapname2mapid(mapn); - if (map[m].flag.src4instance && st->instance_id >= 0) { // Try to redirect to the instance map, not the src map + if ( ( m = map->mapname2mapid(mapn) ) == -1 ) { + ShowWarning("buildin_monster: Attempted to spawn monster class %d on non-existing map '%s'\n",class_, mapn); + return false; + } + + if (map->list[m].flag.src4instance && st->instance_id >= 0) { // Try to redirect to the instance map, not the src map if ((m = instance->mapid2imapid(m, st->instance_id)) < 0) { ShowError("buildin_monster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn); return false; } } } - - mob_id = mob_once_spawn(sd, m, x, y, str, class_, amount, event, size, ai); + + mob_id = mob->once_spawn(sd, m, x, y, str, class_, amount, event, size, ai); script_pushint(st, mob_id); return true; } @@ -8444,60 +9097,59 @@ BUILDIN(getmobdrops) { int class_ = script_getnum(st,2); int i, j = 0; - struct mob_db *mob; - - if( !mobdb_checkid(class_) ) + struct mob_db *monster; + + if( !mob->db_checkid(class_) ) { script_pushint(st, 0); return true; } - - mob = mob_db(class_); - + + monster = mob->db(class_); + for( i = 0; i < MAX_MOB_DROP; i++ ) { - if( mob->dropitem[i].nameid < 1 ) + if( monster->dropitem[i].nameid < 1 ) continue; - if( itemdb_exists(mob->dropitem[i].nameid) == NULL ) + if( itemdb->exists(monster->dropitem[i].nameid) == NULL ) continue; - - mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid); - mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p); - + + mapreg->setreg(reference_uid(script->add_str("$@MobDrop_item"), j), monster->dropitem[i].nameid); + mapreg->setreg(reference_uid(script->add_str("$@MobDrop_rate"), j), monster->dropitem[i].p); + j++; } - - mapreg_setreg(add_str("$@MobDrop_count"), j); + + mapreg->setreg(script->add_str("$@MobDrop_count"), j); script_pushint(st, 1); - + return true; } /*========================================== * Same as monster but randomize location in x0,x1,y0,y1 area *------------------------------------------*/ -BUILDIN(areamonster) -{ - const char* mapn = script_getstr(st,2); - int x0 = script_getnum(st,3); - int y0 = script_getnum(st,4); - int x1 = script_getnum(st,5); - int y1 = script_getnum(st,6); - const char* str = script_getstr(st,7); - int class_ = script_getnum(st,8); - int amount = script_getnum(st,9); - const char* event = ""; - unsigned int size = SZ_SMALL; - unsigned int ai = AI_NONE; +BUILDIN(areamonster) { + const char *mapn = script_getstr(st,2); + int x0 = script_getnum(st,3); + int y0 = script_getnum(st,4); + int x1 = script_getnum(st,5); + int y1 = script_getnum(st,6); + const char *str = script_getstr(st,7); + int class_ = script_getnum(st,8); + int amount = script_getnum(st,9); + const char *event = ""; + unsigned int size = SZ_MEDIUM; + unsigned int ai = AI_NONE; int mob_id; - + struct map_session_data* sd; int16 m; - + if (script_hasdata(st,10)) { event = script_getstr(st, 10); - check_event(st, event); + script->check_event(st, event); } - + if (script_hasdata(st, 11)) { size = script_getnum(st, 11); if (size > 3) { @@ -8505,7 +9157,7 @@ BUILDIN(areamonster) return false; } } - + if (script_hasdata(st, 12)) { ai = script_getnum(st, 12); if (ai > 4) { @@ -8513,64 +9165,66 @@ BUILDIN(areamonster) return false; } } - - sd = iMap->id2sd(st->rid); - + + sd = map->id2sd(st->rid); + if (sd && strcmp(mapn, "this") == 0) m = sd->bl.m; else { - m = iMap->mapname2mapid(mapn); - if (map[m].flag.src4instance && st->instance_id >= 0) { // Try to redirect to the instance map, not the src map + if ( ( m = map->mapname2mapid(mapn) ) == -1 ) { + ShowWarning("buildin_areamonster: Attempted to spawn monster class %d on non-existing map '%s'\n",class_, mapn); + return false; + } + if (map->list[m].flag.src4instance && st->instance_id >= 0) { // Try to redirect to the instance map, not the src map if ((m = instance->mapid2imapid(m, st->instance_id)) < 0) { ShowError("buildin_areamonster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn); return false; } } } - - mob_id = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, amount, event, size, ai); + + mob_id = mob->once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, amount, event, size, ai); script_pushint(st, mob_id); - + return true; } /*========================================== * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag *------------------------------------------*/ -static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap) +int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap) { //same fix but with killmonster instead - stripping events from mobs. TBL_MOB* md = (TBL_MOB*)bl; char *event=va_arg(ap,char *); int allflag=va_arg(ap,int); - + md->state.npc_killmonster = 1; - - if(!allflag){ + + if(!allflag) { if(strcmp(event,md->npc_event)==0) status_kill(bl); - }else{ + } else { if(!md->spawn) status_kill(bl); } md->state.npc_killmonster = 0; return 0; } -static int buildin_killmonster_sub(struct block_list *bl,va_list ap) +int buildin_killmonster_sub(struct block_list *bl,va_list ap) { TBL_MOB* md = (TBL_MOB*)bl; char *event=va_arg(ap,char *); int allflag=va_arg(ap,int); - - if(!allflag){ + + if(!allflag) { if(strcmp(event,md->npc_event)==0) status_kill(bl); - }else{ + } else { if(!md->spawn) status_kill(bl); } return 0; } -BUILDIN(killmonster) -{ +BUILDIN(killmonster) { const char *mapname,*event; int16 m,allflag=0; mapname=script_getstr(st,2); @@ -8578,63 +9232,62 @@ BUILDIN(killmonster) if(strcmp(event,"All")==0) allflag = 1; else - check_event(st, event); - - if( (m=iMap->mapname2mapid(mapname))<0 ) + script->check_event(st, event); + + if( (m=map->mapname2mapid(mapname))<0 ) return true; - - if( map[m].flag.src4instance && st->instance_id >= 0 && (m = instance->mapid2imapid(m, st->instance_id)) < 0 ) + + if( map->list[m].flag.src4instance && st->instance_id >= 0 && (m = instance->mapid2imapid(m, st->instance_id)) < 0 ) return true; - + if( script_hasdata(st,4) ) { if ( script_getnum(st,4) == 1 ) { - iMap->foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag); + map->foreachinmap(script->buildin_killmonster_sub, m, BL_MOB, event ,allflag); return true; } } - - iMap->freeblock_lock(); - iMap->foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag); - iMap->freeblock_unlock(); + + map->freeblock_lock(); + map->foreachinmap(script->buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag); + map->freeblock_unlock(); return true; } -static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap) +int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap) { //Strips the event from the mob if it's killed the old method. struct mob_data *md; - + md = BL_CAST(BL_MOB, bl); if (md->npc_event[0]) md->npc_event[0] = 0; - + status_kill(bl); return 0; } -static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap) +int buildin_killmonsterall_sub(struct block_list *bl,va_list ap) { status_kill(bl); return 0; } -BUILDIN(killmonsterall) -{ +BUILDIN(killmonsterall) { const char *mapname; int16 m; mapname=script_getstr(st,2); - - if( (m = iMap->mapname2mapid(mapname))<0 ) + + if( (m = map->mapname2mapid(mapname))<0 ) return true; - - if( map[m].flag.src4instance && st->instance_id >= 0 && (m = instance->mapid2imapid(m, st->instance_id)) < 0 ) + + if( map->list[m].flag.src4instance && st->instance_id >= 0 && (m = instance->mapid2imapid(m, st->instance_id)) < 0 ) return true; - + if( script_hasdata(st,3) ) { if ( script_getnum(st,3) == 1 ) { - iMap->foreachinmap(buildin_killmonsterall_sub,m,BL_MOB); + map->foreachinmap(script->buildin_killmonsterall_sub,m,BL_MOB); return true; } } - - iMap->foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB); + + map->foreachinmap(script->buildin_killmonsterall_sub_strip,m,BL_MOB); return true; } @@ -8642,50 +9295,49 @@ BUILDIN(killmonsterall) * Creates a clone of a player. * clone map, x, y, event, char_id, master_id, mode, flag, duration *------------------------------------------*/ -BUILDIN(clone) -{ +BUILDIN(clone) { TBL_PC *sd, *msd=NULL; int char_id,master_id=0,x,y, mode = 0, flag = 0, m; unsigned int duration = 0; - const char *map,*event=""; - - map=script_getstr(st,2); + const char *mapname, *event=""; + + mapname=script_getstr(st,2); x=script_getnum(st,3); y=script_getnum(st,4); event=script_getstr(st,5); char_id=script_getnum(st,6); - + if( script_hasdata(st,7) ) master_id=script_getnum(st,7); - + if( script_hasdata(st,8) ) mode=script_getnum(st,8); - + if( script_hasdata(st,9) ) flag=script_getnum(st,9); - + if( script_hasdata(st,10) ) duration=script_getnum(st,10); - - check_event(st, event); - - m = iMap->mapname2mapid(map); + + script->check_event(st, event); + + m = map->mapname2mapid(mapname); if (m < 0) return true; - - sd = iMap->charid2sd(char_id); - + + sd = map->charid2sd(char_id); + if (master_id) { - msd = iMap->charid2sd(master_id); + msd = map->charid2sd(master_id); if (msd) master_id = msd->bl.id; else master_id = 0; } if (sd) //Return ID of newly crafted clone. - script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration)); + script_pushint(st,mob->clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration)); else //Failed to create clone. script_pushint(st,0); - + return true; } /*========================================== @@ -8694,14 +9346,14 @@ BUILDIN(doevent) { const char* event = script_getstr(st,2); struct map_session_data* sd; - - if( ( sd = script_rid2sd(st) ) == NULL ) + + if( ( sd = script->rid2sd(st) ) == NULL ) { return true; } - - check_event(st, event); - npc_event(sd, event, 0); + + script->check_event(st, event); + npc->event(sd, event, 0); return true; } /*========================================== @@ -8709,9 +9361,9 @@ BUILDIN(doevent) BUILDIN(donpcevent) { const char* event = script_getstr(st,2); - check_event(st, event); - if( !npc_event_do(event) ) { - struct npc_data * nd = iMap->id2nd(st->oid); + script->check_event(st, event); + if( !npc->event_do(event) ) { + struct npc_data * nd = map->id2nd(st->oid); ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown"); script_pushint(st, 0); } else @@ -8720,15 +9372,14 @@ BUILDIN(donpcevent) } /// for Aegis compatibility -/// basically a specialized 'donpcevent', with the event specified as two arguments instead of one -BUILDIN(cmdothernpc) // Added by RoVeRT -{ - const char* npc = script_getstr(st,2); +/// basically a specialized 'donpcevent', with the event specified as two arguments instead of one [RoVeRT] +BUILDIN(cmdothernpc) { + const char* npc_name = script_getstr(st,2); const char* command = script_getstr(st,3); char event[EVENT_NAME_LENGTH]; - snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command); - check_event(st, event); - npc_event_do(event); + snprintf(event, sizeof(event), "%s::OnCommand%s", npc_name, command); + script->check_event(st, event); + npc->event_do(event); return true; } @@ -8739,13 +9390,16 @@ BUILDIN(addtimer) int tick = script_getnum(st,2); const char* event = script_getstr(st, 3); TBL_PC* sd; - - check_event(st, event); - sd = script_rid2sd(st); + + script->check_event(st, event); + sd = script->rid2sd(st); if( sd == NULL ) return true; - - pc->addeventtimer(sd,tick,event); + + if (!pc->addeventtimer(sd,tick,event)) { + ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event); + return false; + } return true; } /*========================================== @@ -8754,13 +9408,13 @@ BUILDIN(deltimer) { const char *event; TBL_PC* sd; - + event=script_getstr(st, 2); - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) return true; - - check_event(st, event); + + script->check_event(st, event); pc->deleventtimer(sd,event); return true; } @@ -8771,14 +9425,14 @@ BUILDIN(addtimercount) const char *event; int tick; TBL_PC* sd; - + event=script_getstr(st, 2); tick=script_getnum(st,3); - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) return true; - - check_event(st, event); + + script->check_event(st, event); pc->addeventtimercount(sd,event,tick); return true; } @@ -8789,46 +9443,42 @@ BUILDIN(initnpctimer) { struct npc_data *nd; int flag = 0; - - if( script_hasdata(st,3) ) - { //Two arguments: NPC name and attach flag. - nd = npc_name2id(script_getstr(st, 2)); + + if( script_hasdata(st,3) ) { + //Two arguments: NPC name and attach flag. + nd = npc->name2id(script_getstr(st, 2)); flag = script_getnum(st,3); - } - else if( script_hasdata(st,2) ) - { //Check if argument is numeric (flag) or string (npc name) + } else if( script_hasdata(st,2) ) { + //Check if argument is numeric (flag) or string (npc name) struct script_data *data; data = script_getdata(st,2); - script->get_val(st,data); + script->get_val(st,data); // dereference if it's a variable if( data_isstring(data) ) //NPC name - nd = npc_name2id(script->conv_str(st, data)); - else if( data_isint(data) ) //Flag - { - nd = (struct npc_data *)iMap->id2bl(st->oid); + nd = npc->name2id(script->conv_str(st, data)); + else if( data_isint(data) ) { + //Flag + nd = (struct npc_data *)map->id2bl(st->oid); flag = script->conv_num(st,data); - } - else - { + } else { ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n"); return false; } - } - else - nd = (struct npc_data *)iMap->id2bl(st->oid); - + } else + nd = (struct npc_data *)map->id2bl(st->oid); + if( !nd ) return true; if( flag ) //Attach { - TBL_PC* sd = script_rid2sd(st); + TBL_PC* sd = script->rid2sd(st); if( sd == NULL ) return true; nd->u.scr.rid = sd->bl.id; } - + nd->u.scr.timertick = 0; - npc_settimerevent_tick(nd,0); - npc_timerevent_start(nd, st->rid); + npc->settimerevent_tick(nd,0); + npc->timerevent_start(nd, st->rid); return true; } /*========================================== @@ -8837,117 +9487,104 @@ BUILDIN(startnpctimer) { struct npc_data *nd; int flag = 0; - - if( script_hasdata(st,3) ) - { //Two arguments: NPC name and attach flag. - nd = npc_name2id(script_getstr(st, 2)); + + if( script_hasdata(st,3) ) { + //Two arguments: NPC name and attach flag. + nd = npc->name2id(script_getstr(st, 2)); flag = script_getnum(st,3); - } - else if( script_hasdata(st,2) ) - { //Check if argument is numeric (flag) or string (npc name) + } else if( script_hasdata(st,2) ) { + //Check if argument is numeric (flag) or string (npc name) struct script_data *data; data = script_getdata(st,2); - script->get_val(st,data); + script->get_val(st,data); // dereference if it's a variable if( data_isstring(data) ) //NPC name - nd = npc_name2id(script->conv_str(st, data)); - else if( data_isint(data) ) //Flag - { - nd = (struct npc_data *)iMap->id2bl(st->oid); + nd = npc->name2id(script->conv_str(st, data)); + else if( data_isint(data) ) { + //Flag + nd = (struct npc_data *)map->id2bl(st->oid); flag = script->conv_num(st,data); - } - else - { + } else { ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n"); return false; } - } - else - nd=(struct npc_data *)iMap->id2bl(st->oid); - + } else + nd=(struct npc_data *)map->id2bl(st->oid); + if( !nd ) return true; if( flag ) //Attach { - TBL_PC* sd = script_rid2sd(st); + TBL_PC* sd = script->rid2sd(st); if( sd == NULL ) return true; nd->u.scr.rid = sd->bl.id; } - - npc_timerevent_start(nd, st->rid); + + npc->timerevent_start(nd, st->rid); return true; } /*========================================== *------------------------------------------*/ -BUILDIN(stopnpctimer) -{ +BUILDIN(stopnpctimer) { struct npc_data *nd; int flag = 0; - - if( script_hasdata(st,3) ) - { //Two arguments: NPC name and attach flag. - nd = npc_name2id(script_getstr(st, 2)); + + if( script_hasdata(st,3) ) { + //Two arguments: NPC name and attach flag. + nd = npc->name2id(script_getstr(st, 2)); flag = script_getnum(st,3); - } - else if( script_hasdata(st,2) ) - { //Check if argument is numeric (flag) or string (npc name) + } else if( script_hasdata(st,2) ) { + //Check if argument is numeric (flag) or string (npc name) struct script_data *data; data = script_getdata(st,2); - script->get_val(st,data); + script->get_val(st,data); // Dereference if it's a variable if( data_isstring(data) ) //NPC name - nd = npc_name2id(script->conv_str(st, data)); - else if( data_isint(data) ) //Flag - { - nd = (struct npc_data *)iMap->id2bl(st->oid); + nd = npc->name2id(script->conv_str(st, data)); + else if( data_isint(data) ) { + //Flag + nd = (struct npc_data *)map->id2bl(st->oid); flag = script->conv_num(st,data); - } - else - { + } else { ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n"); return false; } - } - else - nd=(struct npc_data *)iMap->id2bl(st->oid); - + } else + nd=(struct npc_data *)map->id2bl(st->oid); + if( !nd ) return true; if( flag ) //Detach nd->u.scr.rid = 0; - - npc_timerevent_stop(nd); + + npc->timerevent_stop(nd); return true; } /*========================================== *------------------------------------------*/ -BUILDIN(getnpctimer) -{ +BUILDIN(getnpctimer) { struct npc_data *nd; TBL_PC *sd; int type = script_getnum(st,2); int val = 0; - + if( script_hasdata(st,3) ) - nd = npc_name2id(script_getstr(st,3)); + nd = npc->name2id(script_getstr(st,3)); else - nd = (struct npc_data *)iMap->id2bl(st->oid); - + nd = (struct npc_data *)map->id2bl(st->oid); + if( !nd || nd->bl.type != BL_NPC ) { script_pushint(st,0); ShowError("getnpctimer: Invalid NPC.\n"); return false; } - - switch( type ) - { - case 0: val = npc_gettimerevent_tick(nd); break; + + switch( type ) { + case 0: val = (int)npc->gettimerevent_tick(nd); break; // FIXME: change this to int64 when we'll support 64 bit script values case 1: - if( nd->u.scr.rid ) - { - sd = iMap->id2sd(nd->u.scr.rid); - if( !sd ) - { + if( nd->u.scr.rid ) { + sd = map->id2sd(nd->u.scr.rid); + if( !sd ) { ShowError("buildin_getnpctimer: Attached player not found!\n"); break; } @@ -8958,7 +9595,7 @@ BUILDIN(getnpctimer) break; case 2: val = nd->u.scr.timeramount; break; } - + script_pushint(st,val); return true; } @@ -8968,21 +9605,20 @@ BUILDIN(setnpctimer) { int tick; struct npc_data *nd; - + tick = script_getnum(st,2); if( script_hasdata(st,3) ) - nd = npc_name2id(script_getstr(st,3)); + nd = npc->name2id(script_getstr(st,3)); else - nd = (struct npc_data *)iMap->id2bl(st->oid); - - if( !nd || nd->bl.type != BL_NPC ) - { + nd = (struct npc_data *)map->id2bl(st->oid); + + if( !nd || nd->bl.type != BL_NPC ) { script_pushint(st,1); ShowError("setnpctimer: Invalid NPC.\n"); return false; } - - npc_settimerevent_tick(nd,tick); + + npc->settimerevent_tick(nd,tick); script_pushint(st,0); return true; } @@ -8990,30 +9626,29 @@ BUILDIN(setnpctimer) /*========================================== * attaches the player rid to the timer [Celest] *------------------------------------------*/ -BUILDIN(attachnpctimer) -{ +BUILDIN(attachnpctimer) { TBL_PC *sd; - struct npc_data *nd = (struct npc_data *)iMap->id2bl(st->oid); - + struct npc_data *nd = (struct npc_data *)map->id2bl(st->oid); + if( !nd || nd->bl.type != BL_NPC ) { script_pushint(st,1); ShowError("setnpctimer: Invalid NPC.\n"); return false; } - + if( script_hasdata(st,2) ) - sd = iMap->nick2sd(script_getstr(st,2)); + sd = map->nick2sd(script_getstr(st,2)); else - sd = script_rid2sd(st); - + sd = script->rid2sd(st); + if( !sd ) { script_pushint(st,1); ShowWarning("attachnpctimer: Invalid player.\n"); return false; } - + nd->u.scr.rid = sd->bl.id; script_pushint(st,0); return true; @@ -9022,22 +9657,21 @@ BUILDIN(attachnpctimer) /*========================================== * detaches a player rid from the timer [Celest] *------------------------------------------*/ -BUILDIN(detachnpctimer) -{ +BUILDIN(detachnpctimer) { struct npc_data *nd; - + if( script_hasdata(st,2) ) - nd = npc_name2id(script_getstr(st,2)); + nd = npc->name2id(script_getstr(st,2)); else - nd = (struct npc_data *)iMap->id2bl(st->oid); - + nd = (struct npc_data *)map->id2bl(st->oid); + if( !nd || nd->bl.type != BL_NPC ) { script_pushint(st,1); ShowError("detachnpctimer: Invalid NPC.\n"); return false; } - + nd->u.scr.rid = 0; script_pushint(st,0); return true; @@ -9048,9 +9682,8 @@ BUILDIN(detachnpctimer) * it checks if there is a player attached to the current script. [Skotlex] * If no, returns 0, if yes, returns the account_id of the attached player. *------------------------------------------*/ -BUILDIN(playerattached) -{ - if(st->rid == 0 || iMap->id2sd(st->rid) == NULL) +BUILDIN(playerattached) { + if(st->rid == 0 || map->id2sd(st->rid) == NULL) script_pushint(st,0); else script_pushint(st,st->rid); @@ -9059,8 +9692,7 @@ BUILDIN(playerattached) /*========================================== *------------------------------------------*/ -BUILDIN(announce) -{ +BUILDIN(announce) { const char *mes = script_getstr(st,2); int flag = script_getnum(st,3); const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL; @@ -9068,36 +9700,36 @@ BUILDIN(announce) int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY - - if (flag&0x0f) // Broadcast source or broadcast region defined - { + + if( flag&(BC_TARGET_MASK|BC_SOURCE_MASK) ) { + // Broadcast source or broadcast region defined send_target target; - struct block_list *bl = (flag&0x08) ? iMap->id2bl(st->oid) : (struct block_list *)script_rid2sd(st); // If bc_npc flag is set, use NPC as broadcast source + struct block_list *bl = (flag&BC_NPC) ? map->id2bl(st->oid) : (struct block_list *)script->rid2sd(st); // If bc_npc flag is set, use NPC as broadcast source if (bl == NULL) return true; - - flag &= 0x07; - target = (flag == 1) ? ALL_SAMEMAP : - (flag == 2) ? AREA : - (flag == 3) ? SELF : - ALL_CLIENT; + + switch( flag&BC_TARGET_MASK ) { + case BC_MAP: target = ALL_SAMEMAP; break; + case BC_AREA: target = AREA; break; + case BC_SELF: target = SELF; break; + default: target = ALL_CLIENT; break; // BC_ALL + } + if (fontColor) - clif->broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target); + clif->broadcast2(bl, mes, (int)strlen(mes)+1, (unsigned int)strtoul(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target); else - clif->broadcast(bl, mes, (int)strlen(mes)+1, flag&0xf0, target); - } - else - { + clif->broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target); + } else { if (fontColor) - intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY); + intif->broadcast2(mes, (int)strlen(mes)+1, (unsigned int)strtoul(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY); else - intif_broadcast(mes, (int)strlen(mes)+1, flag&0xf0); + intif->broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK); } return true; } /*========================================== *------------------------------------------*/ -static int buildin_announce_sub(struct block_list *bl, va_list ap) +int buildin_announce_sub(struct block_list *bl, va_list ap) { char *mes = va_arg(ap, char *); int len = va_arg(ap, int); @@ -9108,7 +9740,7 @@ static int buildin_announce_sub(struct block_list *bl, va_list ap) short fontAlign = (short)va_arg(ap, int); short fontY = (short)va_arg(ap, int); if (fontColor) - clif->broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF); + clif->broadcast2(bl, mes, len, (unsigned int)strtoul(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF); else clif->broadcast(bl, mes, len, type, SELF); return 0; @@ -9119,41 +9751,38 @@ static int buildin_announce_sub(struct block_list *bl, va_list ap) BUILDIN(itemeffect) { TBL_NPC *nd; TBL_PC *sd; - struct script_data *data; struct item_data *item_data; - - nullpo_retr( 1, ( sd = script_rid2sd( st ) ) ); - nullpo_retr( 1, ( nd = (TBL_NPC *)iMap->id2bl( sd->npc_id ) ) ); - - data = script_getdata( st, 2 ); - script->get_val( st, data ); - - if( data_isstring( data ) ){ - const char *name = script->conv_str( st, data ); - - if( ( item_data = itemdb_searchname( name ) ) == NULL ){ + + sd = script->rid2sd(st); + if( sd == NULL ) + return false; + + nd = (TBL_NPC *)map->id2bl(sd->npc_id); + if( nd == NULL ) + return false; + + if( script_isstringtype(st, 2) ) { + const char *name = script_getstr(st, 2); + + if( ( item_data = itemdb->search_name( name ) ) == NULL ) { ShowError( "buildin_itemeffect: Nonexistant item %s requested.\n", name ); return false; } - } else if( data_isint( data ) ){ - int nameid = script->conv_num( st, data ); - - if( ( item_data = itemdb_exists( nameid ) ) == NULL ){ + } else { + int nameid = script_getnum(st, 2); + + if( ( item_data = itemdb->exists( nameid ) ) == NULL ) { ShowError("buildin_itemeffect: Nonexistant item %d requested.\n", nameid ); return false; } - } else { - ShowError("buildin_itemeffect: invalid data type for argument #1 (%d).", data->type ); - return false; } - - run_script( item_data->script, 0, sd->bl.id, nd->bl.id ); - + + script->run( item_data->script, 0, sd->bl.id, nd->bl.id ); + return true; } -BUILDIN(mapannounce) -{ +BUILDIN(mapannounce) { const char *mapname = script_getstr(st,2); const char *mes = script_getstr(st,3); int flag = script_getnum(st,4); @@ -9163,18 +9792,17 @@ BUILDIN(mapannounce) int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY int16 m; - - if ((m = iMap->mapname2mapid(mapname)) < 0) + + if ((m = map->mapname2mapid(mapname)) < 0) return true; - - iMap->foreachinmap(buildin_announce_sub, m, BL_PC, - mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY); + + map->foreachinmap(script->buildin_announce_sub, m, BL_PC, + mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY); return true; } /*========================================== *------------------------------------------*/ -BUILDIN(areaannounce) -{ +BUILDIN(areaannounce) { const char *mapname = script_getstr(st,2); int x0 = script_getnum(st,3); int y0 = script_getnum(st,4); @@ -9188,51 +9816,47 @@ BUILDIN(areaannounce) int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY int16 m; - - if ((m = iMap->mapname2mapid(mapname)) < 0) + + if ((m = map->mapname2mapid(mapname)) < 0) return true; - - iMap->foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC, - mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY); + + map->foreachinarea(script->buildin_announce_sub, m, x0, y0, x1, y1, BL_PC, + mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY); return true; } /*========================================== *------------------------------------------*/ -BUILDIN(getusers) -{ +BUILDIN(getusers) { int flag, val = 0; struct map_session_data* sd; struct block_list* bl = NULL; - + flag = script_getnum(st,2); - - switch(flag&0x07) - { + + switch(flag&0x07) { case 0: - if(flag&0x8) - {// npc - bl = iMap->id2bl(st->oid); - } - else if((sd = script_rid2sd(st))!=NULL) - {// pc + if(flag&0x8) { + // npc + bl = map->id2bl(st->oid); + } else if((sd = script->rid2sd(st))!=NULL) { + // pc bl = &sd->bl; } - - if(bl) - { - val = map[bl->m].users; + + if(bl) { + val = map->list[bl->m].users; } break; case 1: - val = iMap->getusers(); + val = map->getusers(); break; default: ShowWarning("buildin_getusers: Unknown type %d.\n", flag); script_pushint(st,0); return false; } - + script_pushint(st,val); return true; } @@ -9244,17 +9868,17 @@ BUILDIN(getusersname) TBL_PC *sd, *pl_sd; int /*disp_num=1,*/ group_level = 0; struct s_mapiterator* iter; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if (!sd) return true; - - group_level = pc->get_group_level(sd); + + group_level = pc_get_group_level(sd); iter = mapit_getallusers(); for( pl_sd = (TBL_PC*)mapit->first(iter); mapit->exists(iter); pl_sd = (TBL_PC*)mapit->next(iter) ) { - if (pc_has_permission(pl_sd, PC_PERM_HIDE_SESSION) && pc->get_group_level(pl_sd) > group_level) + if (pc_has_permission(pl_sd, PC_PERM_HIDE_SESSION) && pc_get_group_level(pl_sd) > group_level) continue; // skip hidden sessions - + /* Temporary fix for bugreport:1023. * Do not uncomment unless you want thousands of 'next' buttons. if((disp_num++)%10==0) @@ -9262,7 +9886,7 @@ BUILDIN(getusersname) clif->scriptmes(sd,st->oid,pl_sd->status.name); } mapit->free(iter); - + return true; } /*========================================== @@ -9277,40 +9901,39 @@ BUILDIN(getmapguildusers) struct guild *g = NULL; str=script_getstr(st,2); gid=script_getnum(st,3); - if ((m = iMap->mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server) + if ((m = map->mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server) script_pushint(st,-1); return true; } g = guild->search(gid); - - if (g){ + + if (g) { for(i = 0; i < g->max_member; i++) { if (g->member[i].sd && g->member[i].sd->bl.m == m) c++; } } - + script_pushint(st,c); return true; } /*========================================== *------------------------------------------*/ -BUILDIN(getmapusers) -{ +BUILDIN(getmapusers) { const char *str; int16 m; str=script_getstr(st,2); - if( (m=iMap->mapname2mapid(str))< 0){ + if( (m=map->mapname2mapid(str))< 0) { script_pushint(st,-1); return true; } - script_pushint(st,map[m].users); + script_pushint(st,map->list[m].users); return true; } /*========================================== *------------------------------------------*/ -static int buildin_getareausers_sub(struct block_list *bl,va_list ap) +int buildin_getareausers_sub(struct block_list *bl,va_list ap) { int *users=va_arg(ap,int *); (*users)++; @@ -9325,59 +9948,56 @@ BUILDIN(getareausers) y0=script_getnum(st,4); x1=script_getnum(st,5); y1=script_getnum(st,6); - if( (m=iMap->mapname2mapid(str))< 0){ + if( (m=map->mapname2mapid(str))< 0) { script_pushint(st,-1); return true; } - iMap->foreachinarea(buildin_getareausers_sub, - m,x0,y0,x1,y1,BL_PC,&users); + map->foreachinarea(script->buildin_getareausers_sub, + m,x0,y0,x1,y1,BL_PC,&users); script_pushint(st,users); return true; } /*========================================== *------------------------------------------*/ -static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap) +int buildin_getareadropitem_sub(struct block_list *bl,va_list ap) { int item=va_arg(ap,int); int *amount=va_arg(ap,int *); struct flooritem_data *drop=(struct flooritem_data *)bl; - + if(drop->item_data.nameid==item) (*amount)+=drop->item_data.amount; - + return 0; } -BUILDIN(getareadropitem) -{ +BUILDIN(getareadropitem) { const char *str; int16 m,x0,y0,x1,y1; int item,amount=0; - struct script_data *data; - + str=script_getstr(st,2); x0=script_getnum(st,3); y0=script_getnum(st,4); x1=script_getnum(st,5); y1=script_getnum(st,6); - - data=script_getdata(st,7); - script->get_val(st,data); - if( data_isstring(data) ){ - const char *name=script->conv_str(st,data); - struct item_data *item_data = itemdb_searchname(name); + + if( script_isstringtype(st, 7) ) { + const char *name = script_getstr(st, 7); + struct item_data *item_data = itemdb->search_name(name); item=UNKNOWN_ITEM_ID; if( item_data ) item=item_data->nameid; - }else - item=script->conv_num(st,data); - - if( (m=iMap->mapname2mapid(str))< 0){ + } else { + item=script_getnum(st, 7); + } + + if( (m=map->mapname2mapid(str))< 0) { script_pushint(st,-1); return true; } - iMap->foreachinarea(buildin_getareadropitem_sub, - m,x0,y0,x1,y1,BL_ITEM,item,&amount); + map->foreachinarea(script->buildin_getareadropitem_sub, + m,x0,y0,x1,y1,BL_ITEM,item,&amount); script_pushint(st,amount); return true; } @@ -9387,7 +10007,7 @@ BUILDIN(enablenpc) { const char *str; str=script_getstr(st,2); - npc_enable(str,1); + npc->enable(str,1); return true; } /*========================================== @@ -9396,7 +10016,7 @@ BUILDIN(disablenpc) { const char *str; str=script_getstr(st,2); - npc_enable(str,0); + npc->enable(str,0); return true; } @@ -9406,7 +10026,7 @@ BUILDIN(hideoffnpc) { const char *str; str=script_getstr(st,2); - npc_enable(str,2); + npc->enable(str,2); return true; } /*========================================== @@ -9415,91 +10035,88 @@ BUILDIN(hideonnpc) { const char *str; str=script_getstr(st,2); - npc_enable(str,4); + npc->enable(str,4); return true; } /// Starts a status effect on the target unit or on the attached player. /// /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>}; -BUILDIN(sc_start) -{ +BUILDIN(sc_start) { struct block_list* bl; enum sc_type type; int tick; int val1; int val4 = 0; - + type = (sc_type)script_getnum(st,2); tick = script_getnum(st,3); val1 = script_getnum(st,4); if( script_hasdata(st,5) ) - bl = iMap->id2bl(script_getnum(st,5)); + bl = map->id2bl(script_getnum(st,5)); else - bl = iMap->id2bl(st->rid); - - if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 ) - {// When there isn't a duration specified, try to get it from the skill_db - tick = skill->get_time(status_sc2skill(type), val1); + bl = map->id2bl(st->rid); + + if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status->sc2skill(type) != 0 ) { + // When there isn't a duration specified, try to get it from the skill_db + tick = skill->get_time(status->sc2skill(type), val1); } - - if( potion_flag == 1 && potion_target ) - { //skill.c set the flags before running the script, this must be a potion-pitched effect. - bl = iMap->id2bl(potion_target); + + if( script->potion_flag == 1 && script->potion_target ) { + //skill.c set the flags before running the script, this must be a potion-pitched effect. + bl = map->id2bl(script->potion_target); tick /= 2;// Thrown potions only last half. val4 = 1;// Mark that this was a thrown sc_effect } - + if( bl ) - status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 2); - + status->change_start(NULL, bl, type, 10000, val1, 0, 0, val4, tick, 2); + return true; } /// Starts a status effect on the target unit or on the attached player. /// /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>}; -BUILDIN(sc_start2) -{ +BUILDIN(sc_start2) { struct block_list* bl; enum sc_type type; int tick; int val1; int val4 = 0; int rate; - + type = (sc_type)script_getnum(st,2); tick = script_getnum(st,3); val1 = script_getnum(st,4); rate = script_getnum(st,5); if( script_hasdata(st,6) ) - bl = iMap->id2bl(script_getnum(st,6)); + bl = map->id2bl(script_getnum(st,6)); else - bl = iMap->id2bl(st->rid); - - if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 ) - {// When there isn't a duration specified, try to get it from the skill_db - tick = skill->get_time(status_sc2skill(type), val1); + bl = map->id2bl(st->rid); + + if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status->sc2skill(type) != 0 ) { + // When there isn't a duration specified, try to get it from the skill_db + tick = skill->get_time(status->sc2skill(type), val1); } - - if( potion_flag == 1 && potion_target ) - { //skill.c set the flags before running the script, this must be a potion-pitched effect. - bl = iMap->id2bl(potion_target); + + if( script->potion_flag == 1 && script->potion_target ) { + //skill.c set the flags before running the script, this must be a potion-pitched effect. + bl = map->id2bl(script->potion_target); tick /= 2;// Thrown potions only last half. val4 = 1;// Mark that this was a thrown sc_effect } - + if( bl ) - status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 2); - + status->change_start(NULL, bl, type, rate, val1, 0, 0, val4, tick, 2); + return true; } /// Starts a status effect on the target unit or on the attached player. /// /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>}; -BUILDIN(sc_start4) -{ +BUILDIN(sc_start4) { struct block_list* bl; enum sc_type type; int tick; @@ -9507,7 +10124,7 @@ BUILDIN(sc_start4) int val2; int val3; int val4; - + type = (sc_type)script_getnum(st,2); tick = script_getnum(st,3); val1 = script_getnum(st,4); @@ -9515,96 +10132,91 @@ BUILDIN(sc_start4) val3 = script_getnum(st,6); val4 = script_getnum(st,7); if( script_hasdata(st,8) ) - bl = iMap->id2bl(script_getnum(st,8)); + bl = map->id2bl(script_getnum(st,8)); else - bl = iMap->id2bl(st->rid); - - if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 ) - {// When there isn't a duration specified, try to get it from the skill_db - tick = skill->get_time(status_sc2skill(type), val1); + bl = map->id2bl(st->rid); + + if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status->sc2skill(type) != 0 ) { + // When there isn't a duration specified, try to get it from the skill_db + tick = skill->get_time(status->sc2skill(type), val1); } - - if( potion_flag == 1 && potion_target ) - { //skill.c set the flags before running the script, this must be a potion-pitched effect. - bl = iMap->id2bl(potion_target); + + if( script->potion_flag == 1 && script->potion_target ) { + //skill.c set the flags before running the script, this must be a potion-pitched effect. + bl = map->id2bl(script->potion_target); tick /= 2;// Thrown potions only last half. } - + if( bl ) - status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 2); - + status->change_start(NULL, bl, type, 10000, val1, val2, val3, val4, tick, 2); + return true; } /// Ends one or all status effects on the target unit or on the attached player. /// /// sc_end <effect_id>{,<unit_id>}; -BUILDIN(sc_end) -{ +BUILDIN(sc_end) { struct block_list* bl; int type; - + type = script_getnum(st, 2); if (script_hasdata(st, 3)) - bl = iMap->id2bl(script_getnum(st, 3)); + bl = map->id2bl(script_getnum(st, 3)); else - bl = iMap->id2bl(st->rid); - - if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS] - bl = iMap->id2bl(potion_target); - + bl = map->id2bl(st->rid); + + if (script->potion_flag == 1 && script->potion_target) //##TODO how does this work [FlavioJS] + bl = map->id2bl(script->potion_target); + if (!bl) return true; - - if (type >= 0 && type < SC_MAX) - { - struct status_change *sc = status_get_sc(bl); + + if (type >= 0 && type < SC_MAX) { + struct status_change *sc = status->get_sc(bl); struct status_change_entry *sce = sc ? sc->data[type] : NULL; - + if (!sce) return true; - - - switch (type) - { + + /* status that can't be individually removed (TODO sc_config option?) */ + switch (type) { case SC_WEIGHTOVER50: case SC_WEIGHTOVER90: case SC_NOCHAT: case SC_PUSH_CART: return true; - default: break; } - + //This should help status_change_end force disabling the SC in case it has no limit. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0; status_change_end(bl, (sc_type)type, INVALID_TIMER); } else - status_change_clear(bl, 3); // remove all effects - + status->change_clear(bl, 3); // remove all effects + return true; } /*========================================== * @FIXME atm will return reduced tick, 0 immune, 1 no tick *------------------------------------------*/ -BUILDIN(getscrate) -{ +BUILDIN(getscrate) { struct block_list *bl; int type,rate; - + type=script_getnum(st,2); rate=script_getnum(st,3); if( script_hasdata(st,4) ) //get for the bl assigned - bl = iMap->id2bl(script_getnum(st,4)); + bl = map->id2bl(script_getnum(st,4)); else - bl = iMap->id2bl(st->rid); - + bl = map->id2bl(st->rid); + if (bl) - rate = status_get_sc_def(bl, (sc_type)type, 10000, 10000, 0); - + rate = status->get_sc_def(bl, bl, (sc_type)type, 10000, 10000, 0); + script_pushint(st,rate); return true; } @@ -9615,47 +10227,46 @@ BUILDIN(getscrate) BUILDIN(getstatus) { int id, type; - struct map_session_data* sd = script_rid2sd(st); - + struct map_session_data* sd = script->rid2sd(st); + if( sd == NULL ) {// no player attached return true; } - + id = script_getnum(st, 2); type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0; - + if( id <= SC_NONE || id >= SC_MAX ) {// invalid status type given ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id); return true; } - + if( sd->sc.count == 0 || !sd->sc.data[id] ) {// no status is active script_pushint(st, 0); return true; } - - switch( type ) - { - case 1: script_pushint(st, sd->sc.data[id]->val1); break; - case 2: script_pushint(st, sd->sc.data[id]->val2); break; - case 3: script_pushint(st, sd->sc.data[id]->val3); break; - case 4: script_pushint(st, sd->sc.data[id]->val4); break; + + switch( type ) { + case 1: script_pushint(st, sd->sc.data[id]->val1); break; + case 2: script_pushint(st, sd->sc.data[id]->val2); break; + case 3: script_pushint(st, sd->sc.data[id]->val3); break; + case 4: script_pushint(st, sd->sc.data[id]->val4); break; case 5: { - struct TimerData* timer = (struct TimerData*)iTimer->get_timer(sd->sc.data[id]->timer); - - if( timer ) - {// return the amount of time remaining - script_pushint(st, timer->tick - iTimer->gettick()); + struct TimerData* td = (struct TimerData*)timer->get(sd->sc.data[id]->timer); + + if( td ) { + // return the amount of time remaining + script_pushint(st, (int)(td->tick - timer->gettick())); // TODO: change this to int64 when we'll support 64 bit script values } } break; default: script_pushint(st, 1); break; } - + return true; } @@ -9676,13 +10287,13 @@ BUILDIN(catchpet) { int pet_id; TBL_PC *sd; - + pet_id= script_getnum(st,2); - sd=script_rid2sd(st); + sd=script->rid2sd(st); if( sd == NULL ) return true; - - pet_catch_process1(sd,pet_id); + + pet->catch_process1(sd,pet_id); return true; } @@ -9692,11 +10303,11 @@ BUILDIN(catchpet) BUILDIN(homunculus_evolution) { TBL_PC *sd; - - sd=script_rid2sd(st); + + sd=script->rid2sd(st); if( sd == NULL ) return true; - + if(homun_alive(sd->hd)) { if (sd->hd->homunculus.intimacy > 91000) homun->evolve(sd->hd); @@ -9708,44 +10319,82 @@ BUILDIN(homunculus_evolution) /*========================================== * [Xantara] + * Checks for vaporized morph state + * and deletes ITEMID_STRANGE_EMBRYO. *------------------------------------------*/ BUILDIN(homunculus_mutate) { int homun_id; enum homun_type m_class, m_id; TBL_PC *sd; - - sd = script_rid2sd(st); + bool success = false; + + sd = script->rid2sd(st); if( sd == NULL || sd->hd == NULL ) return true; - - if(script_hasdata(st,2)) - homun_id = script_getnum(st,2); - else - homun_id = 6048 + (rnd() % 4); - - if(homun_alive(sd->hd)) { + + if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) { + int i = pc->search_inventory(sd, ITEMID_STRANGE_EMBRYO); + if( script_hasdata(st,2) ) + homun_id = script_getnum(st,2); + else + homun_id = 6048 + (rnd() % 4); + m_class = homun->class2type(sd->hd->homunculus.class_); m_id = homun->class2type(homun_id); - - if ( m_class != -1 && m_id != -1 && m_class == HT_EVO && m_id == HT_S && sd->hd->homunculus.level >= 99 ) + + if( m_class == HT_EVO && m_id == HT_S && + sd->hd->homunculus.level >= 99 && i != INDEX_NOT_FOUND && + !pc->delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT) ) { + sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state. + homun->call(sd); // Respawn homunculus. homun->mutate(sd->hd, homun_id); - else + success = true; + } else clif->emotion(&sd->hd->bl, E_SWT); - } + } else + clif->emotion(&sd->hd->bl, E_SWT); + + script_pushint(st,success?1:0); return true; } -// [Zephyrus] -BUILDIN(homunculus_shuffle) { +/*========================================== + * Puts homunculus into morph state + * and gives ITEMID_STRANGE_EMBRYO. + *------------------------------------------*/ +BUILDIN(homunculus_morphembryo) { + enum homun_type m_class; + int i = 0; TBL_PC *sd; - - sd=script_rid2sd(st); - if( sd == NULL ) + bool success = false; + + sd = script->rid2sd(st); + if( sd == NULL || sd->hd == NULL ) return true; - - if(homun_alive(sd->hd)) - homun->shuffle(sd->hd); - + + if( homun_alive(sd->hd) ) { + m_class = homun->class2type(sd->hd->homunculus.class_); + + if ( m_class == HT_EVO && sd->hd->homunculus.level >= 99 ) { + struct item item_tmp; + + memset(&item_tmp, 0, sizeof(item_tmp)); + item_tmp.nameid = ITEMID_STRANGE_EMBRYO; + item_tmp.identify = 1; + + if( (i = pc->additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) { + clif->additem(sd, 0, 0, i); + clif->emotion(&sd->hd->bl, E_SWT); + } else { + homun->vaporize(sd, HOM_ST_MORPH); + success = true; + } + } else + clif->emotion(&sd->hd->bl, E_SWT); + } else + clif->emotion(&sd->hd->bl, E_SWT); + + script_pushint(st, success?1:0); return true; } @@ -9756,20 +10405,28 @@ BUILDIN(homunculus_shuffle) { * 1 = Homunculus is vaporized (rest) * 2 = Homunculus is in morph state *------------------------------------------*/ -BUILDIN(checkhomcall) -{ - TBL_PC *sd = script_rid2sd(st); - TBL_HOM *hd; +BUILDIN(homunculus_checkcall) { + TBL_PC *sd = script->rid2sd(st); - if( sd == NULL ) - return false; - - hd = sd->hd; - - if( !hd ) + if( sd == NULL || !sd->hd ) script_pushint(st, -1); else - script_pushint(st, hd->homunculus.vaporize); + script_pushint(st, sd->hd->homunculus.vaporize); + + return true; +} + + +// [Zephyrus] +BUILDIN(homunculus_shuffle) { + TBL_PC *sd; + + sd=script->rid2sd(st); + if( sd == NULL ) + return true; + + if(homun_alive(sd->hd)) + homun->shuffle(sd->hd); return true; } @@ -9782,11 +10439,9 @@ BUILDIN(eaclass) class_ = script_getnum(st,2); else { TBL_PC *sd; - sd=script_rid2sd(st); - if (!sd) { - script_pushint(st,-1); + sd=script->rid2sd(st); + if( !sd ) return true; - } class_ = sd->status.class_; } script_pushint(st,pc->jobid2mapid(class_)); @@ -9801,7 +10456,7 @@ BUILDIN(roclass) sex = script_getnum(st,3); else { TBL_PC *sd; - if (st->rid && (sd=script_rid2sd(st))) + if (st->rid && (sd=script->rid2sd(st))) sex = sd->status.sex; else sex = 1; //Just use male when not found. @@ -9816,15 +10471,15 @@ BUILDIN(roclass) BUILDIN(birthpet) { TBL_PC *sd; - sd=script_rid2sd(st); + sd=script->rid2sd(st); if( sd == NULL ) return true; - + if( sd->status.pet_id ) {// do not send egg list, when you already have a pet return true; } - + clif->sendegg(sd); return true; } @@ -9832,21 +10487,21 @@ BUILDIN(birthpet) /*========================================== * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes) * @type - * 1 : make like after rebirth - * 2 : blvl,jlvl=1, skillpoint=0 - * 3 : don't reset skill, blvl=1 - * 4 : jlvl=0 + * 1 : make like after rebirth + * 2 : blvl,jlvl=1, skillpoint=0 + * 3 : don't reset skill, blvl=1 + * 4 : jlvl=0 *------------------------------------------*/ BUILDIN(resetlvl) { TBL_PC *sd; - + int type=script_getnum(st,2); - - sd=script_rid2sd(st); + + sd=script->rid2sd(st); if( sd == NULL ) return true; - + pc->resetlvl(sd,type); return true; } @@ -9856,7 +10511,9 @@ BUILDIN(resetlvl) BUILDIN(resetstatus) { TBL_PC *sd; - sd=script_rid2sd(st); + sd=script->rid2sd(st); + if( sd == NULL ) + return false; pc->resetstate(sd); return true; } @@ -9867,7 +10524,9 @@ BUILDIN(resetstatus) BUILDIN(resetskill) { TBL_PC *sd; - sd=script_rid2sd(st); + sd=script->rid2sd(st); + if( sd == NULL ) + return false; pc->resetskill(sd,1); return true; } @@ -9878,7 +10537,9 @@ BUILDIN(resetskill) BUILDIN(skillpointcount) { TBL_PC *sd; - sd=script_rid2sd(st); + sd=script->rid2sd(st); + if( sd == NULL ) + return false; script_pushint(st,sd->status.skill_point + pc->resetskill(sd,2)); return true; } @@ -9886,19 +10547,18 @@ BUILDIN(skillpointcount) /*========================================== * *------------------------------------------*/ -BUILDIN(changebase) -{ +BUILDIN(changebase) { TBL_PC *sd=NULL; int vclass; - + if( script_hasdata(st,3) ) - sd=iMap->id2sd(script_getnum(st,3)); + sd=map->id2sd(script_getnum(st,3)); else - sd=script_rid2sd(st); - + sd=script->rid2sd(st); + if(sd == NULL) return true; - + vclass = script_getnum(st,2); if(vclass == JOB_WEDDING) { @@ -9907,9 +10567,9 @@ BUILDIN(changebase) ) return true; } - + if(sd->disguise == -1 && vclass != sd->vd.class_) { - status_set_viewdata(&sd->bl, vclass); + status->set_viewdata(&sd->bl, vclass); //Updated client view. Base, Weapon and Cloth Colors. clif->changelook(&sd->bl,LOOK_BASE,sd->vd.class_); clif->changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon); @@ -9917,7 +10577,7 @@ BUILDIN(changebase) clif->changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color); clif->skillinfoblock(sd); } - + return true; } @@ -9928,36 +10588,39 @@ BUILDIN(changesex) { int i; TBL_PC *sd = NULL; - sd = script_rid2sd(st); - + sd = script->rid2sd(st); + + if( sd == NULL ) + return false; + pc->resetskill(sd,4); // to avoid any problem with equipment and invalid sex, equipment is unequiped. for( i=0; i<EQI_MAX; i++ ) if( sd->equip_index[i] >= 0 ) pc->unequipitem(sd, sd->equip_index[i], 3); - chrif_changesex(sd); + chrif->changesex(sd); return true; } /*========================================== * Works like 'announce' but outputs in the common chat window *------------------------------------------*/ -BUILDIN(globalmes) -{ - struct block_list *bl = iMap->id2bl(st->oid); +BUILDIN(globalmes) { + struct block_list *bl = map->id2bl(st->oid); struct npc_data *nd = (struct npc_data *)bl; const char *name=NULL,*mes; - + mes=script_getstr(st,2); if(mes==NULL) return true; - - if(script_hasdata(st,3)){ // npc name to display + + if(script_hasdata(st,3)) { + // npc name to display name=script_getstr(st,3); } else { name=nd->name; //use current npc name } - - npc_globalmessage(name,mes); // broadcast to all players connected - + + npc->globalmessage(name,mes); // broadcast to all players connected + return true; } @@ -9968,8 +10631,7 @@ BUILDIN(globalmes) /// Creates a waiting room (chat room) for this npc. /// /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}}; -BUILDIN(waitingroom) -{ +BUILDIN(waitingroom) { struct npc_data* nd; int pub = 1; const char* title = script_getstr(st, 2); @@ -9979,11 +10641,11 @@ BUILDIN(waitingroom) int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0; int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1; int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL; - - nd = (struct npc_data *)iMap->id2bl(st->oid); + + nd = (struct npc_data *)map->id2bl(st->oid); if( nd != NULL ) - chat_createnpcchat(nd, title, limit, pub, trigger, ev, zeny, minLvl, maxLvl); - + chat->create_npc_chat(nd, title, limit, pub, trigger, ev, zeny, minLvl, maxLvl); + return true; } @@ -9991,15 +10653,14 @@ BUILDIN(waitingroom) /// /// delwaitingroom "<npc_name>"; /// delwaitingroom; -BUILDIN(delwaitingroom) -{ +BUILDIN(delwaitingroom) { struct npc_data* nd; if( script_hasdata(st,2) ) - nd = npc_name2id(script_getstr(st, 2)); + nd = npc->name2id(script_getstr(st, 2)); else - nd = (struct npc_data *)iMap->id2bl(st->oid); + nd = (struct npc_data *)map->id2bl(st->oid); if( nd != NULL ) - chat_deletenpcchat(nd); + chat->delete_npc_chat(nd); return true; } @@ -10007,18 +10668,17 @@ BUILDIN(delwaitingroom) /// /// kickwaitingroomall "<npc_name>"; /// kickwaitingroomall; -BUILDIN(waitingroomkickall) -{ +BUILDIN(waitingroomkickall) { struct npc_data* nd; struct chat_data* cd; - + if( script_hasdata(st,2) ) - nd = npc_name2id(script_getstr(st,2)); + nd = npc->name2id(script_getstr(st,2)); else - nd = (struct npc_data *)iMap->id2bl(st->oid); - - if( nd != NULL && (cd=(struct chat_data *)iMap->id2bl(nd->chat_id)) != NULL ) - chat_npckickall(cd); + nd = (struct npc_data *)map->id2bl(st->oid); + + if( nd != NULL && (cd=(struct chat_data *)map->id2bl(nd->chat_id)) != NULL ) + chat->npc_kick_all(cd); return true; } @@ -10026,18 +10686,17 @@ BUILDIN(waitingroomkickall) /// /// enablewaitingroomevent "<npc_name>"; /// enablewaitingroomevent; -BUILDIN(enablewaitingroomevent) -{ +BUILDIN(enablewaitingroomevent) { struct npc_data* nd; struct chat_data* cd; - + if( script_hasdata(st,2) ) - nd = npc_name2id(script_getstr(st, 2)); + nd = npc->name2id(script_getstr(st, 2)); else - nd = (struct npc_data *)iMap->id2bl(st->oid); - - if( nd != NULL && (cd=(struct chat_data *)iMap->id2bl(nd->chat_id)) != NULL ) - chat_enableevent(cd); + nd = (struct npc_data *)map->id2bl(st->oid); + + if( nd != NULL && (cd=(struct chat_data *)map->id2bl(nd->chat_id)) != NULL ) + chat->enable_event(cd); return true; } @@ -10045,18 +10704,17 @@ BUILDIN(enablewaitingroomevent) /// /// disablewaitingroomevent "<npc_name>"; /// disablewaitingroomevent; -BUILDIN(disablewaitingroomevent) -{ +BUILDIN(disablewaitingroomevent) { struct npc_data *nd; struct chat_data *cd; - + if( script_hasdata(st,2) ) - nd = npc_name2id(script_getstr(st, 2)); + nd = npc->name2id(script_getstr(st, 2)); else - nd = (struct npc_data *)iMap->id2bl(st->oid); - - if( nd != NULL && (cd=(struct chat_data *)iMap->id2bl(nd->chat_id)) != NULL ) - chat_disableevent(cd); + nd = (struct npc_data *)map->id2bl(st->oid); + + if( nd != NULL && (cd=(struct chat_data *)map->id2bl(nd->chat_id)) != NULL ) + chat->disable_event(cd); return true; } @@ -10074,26 +10732,23 @@ BUILDIN(disablewaitingroomevent) /// /// getwaitingroomstate(<type>,"<npc_name>") -> <info> /// getwaitingroomstate(<type>) -> <info> -BUILDIN(getwaitingroomstate) -{ +BUILDIN(getwaitingroomstate) { struct npc_data *nd; struct chat_data *cd; int type; - + type = script_getnum(st,2); if( script_hasdata(st,3) ) - nd = npc_name2id(script_getstr(st, 3)); + nd = npc->name2id(script_getstr(st, 3)); else - nd = (struct npc_data *)iMap->id2bl(st->oid); - - if( nd == NULL || (cd=(struct chat_data *)iMap->id2bl(nd->chat_id)) == NULL ) - { + nd = (struct npc_data *)map->id2bl(st->oid); + + if( nd == NULL || (cd=(struct chat_data *)map->id2bl(nd->chat_id)) == NULL ) { script_pushint(st, -1); return true; } - - switch(type) - { + + switch(type) { case 0: script_pushint(st, cd->users); break; case 1: script_pushint(st, cd->limit); break; case 2: script_pushint(st, cd->trigger&0x7f); break; @@ -10120,8 +10775,7 @@ BUILDIN(getwaitingroomstate) /// /// warpwaitingpc "<map name>",<x>,<y>,<number of players>; /// warpwaitingpc "<map name>",<x>,<y>; -BUILDIN(warpwaitingpc) -{ +BUILDIN(warpwaitingpc) { int x; int y; int i; @@ -10130,47 +10784,46 @@ BUILDIN(warpwaitingpc) struct npc_data* nd; struct chat_data* cd; TBL_PC* sd; - - nd = (struct npc_data *)iMap->id2bl(st->oid); - if( nd == NULL || (cd=(struct chat_data *)iMap->id2bl(nd->chat_id)) == NULL ) + + nd = (struct npc_data *)map->id2bl(st->oid); + if( nd == NULL || (cd=(struct chat_data *)map->id2bl(nd->chat_id)) == NULL ) return true; - + map_name = script_getstr(st,2); x = script_getnum(st,3); y = script_getnum(st,4); n = cd->trigger&0x7f; - + if( script_hasdata(st,5) ) n = script_getnum(st,5); - - for( i = 0; i < n && cd->users > 0; i++ ) - { + + for( i = 0; i < n && cd->users > 0; i++ ) { sd = cd->usersd[0]; - - if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport ) - {// can't teleport on this map + + if( strcmp(map_name,"SavePoint") == 0 && map->list[sd->bl.m].flag.noteleport ) { + // can't teleport on this map break; } - - if( cd->zeny ) - {// fee set - if( (uint32)sd->status.zeny < cd->zeny ) - {// no zeny to cover set fee + + if( cd->zeny ) { + // fee set + if( (uint32)sd->status.zeny < cd->zeny ) { + // no zeny to cover set fee break; } pc->payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL); } - - mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id); - + + mapreg->setreg(reference_uid(script->add_str("$@warpwaitingpc"), i), sd->bl.id); + if( strcmp(map_name,"Random") == 0 ) pc->randomwarp(sd,CLR_TELEPORT); else if( strcmp(map_name,"SavePoint") == 0 ) pc->setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT); else - pc->setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT); + pc->setpos(sd, script->mapindexname2id(st,map_name), x, y, CLR_OUTSIGHT); } - mapreg_setreg(add_str("$@warpwaitingpcnum"), i); + mapreg->setreg(script->add_str("$@warpwaitingpcnum"), i); return true; } @@ -10183,7 +10836,7 @@ BUILDIN(warpwaitingpc) /// @param st Script state to detach the character from. void script_detach_rid(struct script_state* st) { if(st->rid) { - script_detach_state(st, false); + script->detach_state(st, false); st->rid = 0; } } @@ -10191,15 +10844,14 @@ void script_detach_rid(struct script_state* st) { /*========================================== * Attach sd char id to script and detach current one if any *------------------------------------------*/ -BUILDIN(attachrid) -{ +BUILDIN(attachrid) { int rid = script_getnum(st,2); - - if (iMap->id2sd(rid) != NULL) { - script_detach_rid(st); - + + if (map->id2sd(rid) != NULL) { + script->detach_rid(st); + st->rid = rid; - script_attach_state(st); + script->attach_state(st); script_pushint(st,1); } else script_pushint(st,0); @@ -10210,15 +10862,14 @@ BUILDIN(attachrid) *------------------------------------------*/ BUILDIN(detachrid) { - script_detach_rid(st); + script->detach_rid(st); return true; } /*========================================== * Chk if account connected, (and charid from account if specified) *------------------------------------------*/ -BUILDIN(isloggedin) -{ - TBL_PC* sd = iMap->id2sd(script_getnum(st,2)); +BUILDIN(isloggedin) { + TBL_PC* sd = map->id2sd(script_getnum(st,2)); if (script_hasdata(st,3) && sd && sd->status.char_id != script_getnum(st,3)) sd = NULL; @@ -10230,26 +10881,25 @@ BUILDIN(isloggedin) /*========================================== * *------------------------------------------*/ -BUILDIN(setmapflagnosave) -{ +BUILDIN(setmapflagnosave) { int16 m,x,y; - unsigned short mapindex; + unsigned short map_index; const char *str,*str2; - + str=script_getstr(st,2); str2=script_getstr(st,3); x=script_getnum(st,4); y=script_getnum(st,5); - m = iMap->mapname2mapid(str); - mapindex = mapindex_name2id(str2); - - if(m >= 0 && mapindex) { - map[m].flag.nosave=1; - map[m].save.map=mapindex; - map[m].save.x=x; - map[m].save.y=y; + m = map->mapname2mapid(str); + map_index = script->mapindexname2id(st,str2); + + if(m >= 0 && map_index) { + map->list[m].flag.nosave=1; + map->list[m].save.map=map_index; + map->list[m].save.x=x; + map->list[m].save.y=y; } - + return true; } @@ -10257,72 +10907,74 @@ BUILDIN(getmapflag) { int16 m,i; const char *str; - + str=script_getstr(st,2); i=script_getnum(st,3); - - m = iMap->mapname2mapid(str); + + m = map->mapname2mapid(str); if(m >= 0) { switch(i) { - case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break; - case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break; - case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break; - case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break; - case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break; - case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break; - case MF_PVP: script_pushint(st,map[m].flag.pvp); break; - case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break; - case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break; - case MF_GVG: script_pushint(st,map[m].flag.gvg); break; - case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break; - case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break; - case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break; - case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break; - case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break; - case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break; - case MF_SNOW: script_pushint(st,map[m].flag.snow); break; - case MF_FOG: script_pushint(st,map[m].flag.fog); break; - case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break; - case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break; - case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break; - case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break; - case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break; - case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break; - case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break; - case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break; - case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break; - case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break; - case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break; - case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break; - case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break; - case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break; - case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break; - case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break; - case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break; - case MF_JEXP: script_pushint(st,map[m].jexp); break; - case MF_BEXP: script_pushint(st,map[m].bexp); break; - case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break; - case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break; - case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break; - case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break; - case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break; - case MF_TOWN: script_pushint(st,map[m].flag.town); break; - case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break; - case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break; - case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break; - case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break; - case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break; - case MF_RESET: script_pushint(st,map[m].flag.reset); break; + case MF_NOMEMO: script_pushint(st,map->list[m].flag.nomemo); break; + case MF_NOTELEPORT: script_pushint(st,map->list[m].flag.noteleport); break; + case MF_NOSAVE: script_pushint(st,map->list[m].flag.nosave); break; + case MF_NOBRANCH: script_pushint(st,map->list[m].flag.nobranch); break; + case MF_NOPENALTY: script_pushint(st,map->list[m].flag.noexppenalty); break; + case MF_NOZENYPENALTY: script_pushint(st,map->list[m].flag.nozenypenalty); break; + case MF_PVP: script_pushint(st,map->list[m].flag.pvp); break; + case MF_PVP_NOPARTY: script_pushint(st,map->list[m].flag.pvp_noparty); break; + case MF_PVP_NOGUILD: script_pushint(st,map->list[m].flag.pvp_noguild); break; + case MF_GVG: script_pushint(st,map->list[m].flag.gvg); break; + case MF_GVG_NOPARTY: script_pushint(st,map->list[m].flag.gvg_noparty); break; + case MF_NOTRADE: script_pushint(st,map->list[m].flag.notrade); break; + case MF_NOSKILL: script_pushint(st,map->list[m].flag.noskill); break; + case MF_NOWARP: script_pushint(st,map->list[m].flag.nowarp); break; + case MF_PARTYLOCK: script_pushint(st,map->list[m].flag.partylock); break; + case MF_NOICEWALL: script_pushint(st,map->list[m].flag.noicewall); break; + case MF_SNOW: script_pushint(st,map->list[m].flag.snow); break; + case MF_FOG: script_pushint(st,map->list[m].flag.fog); break; + case MF_SAKURA: script_pushint(st,map->list[m].flag.sakura); break; + case MF_LEAVES: script_pushint(st,map->list[m].flag.leaves); break; + case MF_CLOUDS: script_pushint(st,map->list[m].flag.clouds); break; + case MF_CLOUDS2: script_pushint(st,map->list[m].flag.clouds2); break; + case MF_FIREWORKS: script_pushint(st,map->list[m].flag.fireworks); break; + case MF_GVG_CASTLE: script_pushint(st,map->list[m].flag.gvg_castle); break; + case MF_GVG_DUNGEON: script_pushint(st,map->list[m].flag.gvg_dungeon); break; + case MF_NIGHTENABLED: script_pushint(st,map->list[m].flag.nightenabled); break; + case MF_NOBASEEXP: script_pushint(st,map->list[m].flag.nobaseexp); break; + case MF_NOJOBEXP: script_pushint(st,map->list[m].flag.nojobexp); break; + case MF_NOMOBLOOT: script_pushint(st,map->list[m].flag.nomobloot); break; + case MF_NOMVPLOOT: script_pushint(st,map->list[m].flag.nomvploot); break; + case MF_NORETURN: script_pushint(st,map->list[m].flag.noreturn); break; + case MF_NOWARPTO: script_pushint(st,map->list[m].flag.nowarpto); break; + case MF_NIGHTMAREDROP: script_pushint(st,map->list[m].flag.pvp_nightmaredrop); break; + case MF_NOCOMMAND: script_pushint(st,map->list[m].nocommand); break; + case MF_NODROP: script_pushint(st,map->list[m].flag.nodrop); break; + case MF_JEXP: script_pushint(st,map->list[m].jexp); break; + case MF_BEXP: script_pushint(st,map->list[m].bexp); break; + case MF_NOVENDING: script_pushint(st,map->list[m].flag.novending); break; + case MF_LOADEVENT: script_pushint(st,map->list[m].flag.loadevent); break; + case MF_NOCHAT: script_pushint(st,map->list[m].flag.nochat); break; + case MF_NOEXPPENALTY: script_pushint(st,map->list[m].flag.noexppenalty ); break; + case MF_GUILDLOCK: script_pushint(st,map->list[m].flag.guildlock); break; + case MF_TOWN: script_pushint(st,map->list[m].flag.town); break; + case MF_AUTOTRADE: script_pushint(st,map->list[m].flag.autotrade); break; + case MF_ALLOWKS: script_pushint(st,map->list[m].flag.allowks); break; + case MF_MONSTER_NOTELEPORT: script_pushint(st,map->list[m].flag.monster_noteleport); break; + case MF_PVP_NOCALCRANK: script_pushint(st,map->list[m].flag.pvp_nocalcrank); break; + case MF_BATTLEGROUND: script_pushint(st,map->list[m].flag.battleground); break; + case MF_RESET: script_pushint(st,map->list[m].flag.reset); break; + case MF_NOTOMB: script_pushint(st,map->list[m].flag.notomb); break; + case MF_NOCASHSHOP: script_pushint(st,map->list[m].flag.nocashshop); break; } } - + return true; } /* pvp timer handling */ -static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) { +int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) { TBL_PC* sd = (TBL_PC*)bl; if (sd->pvp_timer == INVALID_TIMER) { - sd->pvp_timer = iTimer->add_timer(iTimer->gettick() + 200, pc->calc_pvprank_timer, sd->bl.id, 0); + sd->pvp_timer = timer->add(timer->gettick() + 200, pc->calc_pvprank_timer, sd->bl.id, 0); sd->pvp_rank = 0; sd->pvp_lastusers = 0; sd->pvp_point = 5; @@ -10333,227 +10985,229 @@ static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) { clif->maptypeproperty2(&sd->bl,SELF); return 0; } -BUILDIN(setmapflag) -{ +BUILDIN(setmapflag) { int16 m,i; const char *str, *val2 = NULL; - struct script_data* data; int val=0; - + str=script_getstr(st,2); - + i = script_getnum(st, 3); - - if(script_hasdata(st,4)){ - data = script_getdata(st,4); - script->get_val(st, data); - - - if( data_isstring(data) ) + + if (script_hasdata(st,4)) { + if (script_isstringtype(st, 4)) { val2 = script_getstr(st, 4); - else + } else if (script_isinttype(st, 4)) { val = script_getnum(st, 4); - + } else { + ShowError("buildin_setmapflag: invalid data type for argument 3.\n"); + return false; + } } - m = iMap->mapname2mapid(str); - + m = map->mapname2mapid(str); + if(m >= 0) { switch(i) { - case MF_NOMEMO: map[m].flag.nomemo = 1; break; - case MF_NOTELEPORT: map[m].flag.noteleport = 1; break; - case MF_NOSAVE: map[m].flag.nosave = 1; break; - case MF_NOBRANCH: map[m].flag.nobranch = 1; break; - case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break; - case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break; + case MF_NOMEMO: map->list[m].flag.nomemo = 1; break; + case MF_NOTELEPORT: map->list[m].flag.noteleport = 1; break; + case MF_NOSAVE: map->list[m].flag.nosave = 1; break; + case MF_NOBRANCH: map->list[m].flag.nobranch = 1; break; + case MF_NOPENALTY: map->list[m].flag.noexppenalty = 1; map->list[m].flag.nozenypenalty = 1; break; + case MF_NOZENYPENALTY: map->list[m].flag.nozenypenalty = 1; break; case MF_PVP: - map[m].flag.pvp = 1; + map->list[m].flag.pvp = 1; if( !battle_config.pk_mode ) { - iMap->foreachinmap(script_mapflag_pvp_sub,m,BL_PC); + map->foreachinmap(script->mapflag_pvp_sub,m,BL_PC); } break; - case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break; - case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break; + case MF_PVP_NOPARTY: map->list[m].flag.pvp_noparty = 1; break; + case MF_PVP_NOGUILD: map->list[m].flag.pvp_noguild = 1; break; case MF_GVG: { struct block_list bl; - map[m].flag.gvg = 1; + map->list[m].flag.gvg = 1; clif->map_property_mapall(m, MAPPROPERTY_AGITZONE); bl.type = BL_NUL; bl.m = m; clif->maptypeproperty2(&bl,ALL_SAMEMAP); } break; - case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break; - case MF_NOTRADE: map[m].flag.notrade = 1; break; - case MF_NOSKILL: map[m].flag.noskill = 1; break; - case MF_NOWARP: map[m].flag.nowarp = 1; break; - case MF_PARTYLOCK: map[m].flag.partylock = 1; break; - case MF_NOICEWALL: map[m].flag.noicewall = 1; break; - case MF_SNOW: map[m].flag.snow = 1; break; - case MF_FOG: map[m].flag.fog = 1; break; - case MF_SAKURA: map[m].flag.sakura = 1; break; - case MF_LEAVES: map[m].flag.leaves = 1; break; - case MF_CLOUDS: map[m].flag.clouds = 1; break; - case MF_CLOUDS2: map[m].flag.clouds2 = 1; break; - case MF_FIREWORKS: map[m].flag.fireworks = 1; break; - case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break; - case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break; - case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break; - case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break; - case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break; - case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break; - case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break; - case MF_NORETURN: map[m].flag.noreturn = 1; break; - case MF_NOWARPTO: map[m].flag.nowarpto = 1; break; - case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break; - case MF_ZONE: { - char zone[6] = "zone\0"; - char empty[1] = "\0"; - char params[MAP_ZONE_MAPFLAG_LENGTH]; - memcpy(params, val2, MAP_ZONE_MAPFLAG_LENGTH); - npc_parse_mapflag(map[m].name, empty, zone, params, empty, empty, empty); - } + case MF_GVG_NOPARTY: map->list[m].flag.gvg_noparty = 1; break; + case MF_NOTRADE: map->list[m].flag.notrade = 1; break; + case MF_NOSKILL: map->list[m].flag.noskill = 1; break; + case MF_NOWARP: map->list[m].flag.nowarp = 1; break; + case MF_PARTYLOCK: map->list[m].flag.partylock = 1; break; + case MF_NOICEWALL: map->list[m].flag.noicewall = 1; break; + case MF_SNOW: map->list[m].flag.snow = 1; break; + case MF_FOG: map->list[m].flag.fog = 1; break; + case MF_SAKURA: map->list[m].flag.sakura = 1; break; + case MF_LEAVES: map->list[m].flag.leaves = 1; break; + case MF_CLOUDS: map->list[m].flag.clouds = 1; break; + case MF_CLOUDS2: map->list[m].flag.clouds2 = 1; break; + case MF_FIREWORKS: map->list[m].flag.fireworks = 1; break; + case MF_GVG_CASTLE: map->list[m].flag.gvg_castle = 1; break; + case MF_GVG_DUNGEON: map->list[m].flag.gvg_dungeon = 1; break; + case MF_NIGHTENABLED: map->list[m].flag.nightenabled = 1; break; + case MF_NOBASEEXP: map->list[m].flag.nobaseexp = 1; break; + case MF_NOJOBEXP: map->list[m].flag.nojobexp = 1; break; + case MF_NOMOBLOOT: map->list[m].flag.nomobloot = 1; break; + case MF_NOMVPLOOT: map->list[m].flag.nomvploot = 1; break; + case MF_NORETURN: map->list[m].flag.noreturn = 1; break; + case MF_NOWARPTO: map->list[m].flag.nowarpto = 1; break; + case MF_NIGHTMAREDROP: map->list[m].flag.pvp_nightmaredrop = 1; break; + case MF_ZONE: + if( val2 ) { + char zone[6] = "zone\0"; + char empty[1] = "\0"; + char params[MAP_ZONE_MAPFLAG_LENGTH]; + memcpy(params, val2, MAP_ZONE_MAPFLAG_LENGTH); + npc->parse_mapflag(map->list[m].name, empty, zone, params, empty, empty, empty, NULL); + } break; - case MF_NOCOMMAND: map[m].nocommand = (val <= 0) ? 100 : val; break; - case MF_NODROP: map[m].flag.nodrop = 1; break; - case MF_JEXP: map[m].jexp = (val <= 0) ? 100 : val; break; - case MF_BEXP: map[m].bexp = (val <= 0) ? 100 : val; break; - case MF_NOVENDING: map[m].flag.novending = 1; break; - case MF_LOADEVENT: map[m].flag.loadevent = 1; break; - case MF_NOCHAT: map[m].flag.nochat = 1; break; - case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break; - case MF_GUILDLOCK: map[m].flag.guildlock = 1; break; - case MF_TOWN: map[m].flag.town = 1; break; - case MF_AUTOTRADE: map[m].flag.autotrade = 1; break; - case MF_ALLOWKS: map[m].flag.allowks = 1; break; - case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break; - case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break; - case MF_BATTLEGROUND: map[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break; - case MF_RESET: map[m].flag.reset = 1; break; + case MF_NOCOMMAND: map->list[m].nocommand = (val <= 0) ? 100 : val; break; + case MF_NODROP: map->list[m].flag.nodrop = 1; break; + case MF_JEXP: map->list[m].jexp = (val <= 0) ? 100 : val; break; + case MF_BEXP: map->list[m].bexp = (val <= 0) ? 100 : val; break; + case MF_NOVENDING: map->list[m].flag.novending = 1; break; + case MF_LOADEVENT: map->list[m].flag.loadevent = 1; break; + case MF_NOCHAT: map->list[m].flag.nochat = 1; break; + case MF_NOEXPPENALTY: map->list[m].flag.noexppenalty = 1; break; + case MF_GUILDLOCK: map->list[m].flag.guildlock = 1; break; + case MF_TOWN: map->list[m].flag.town = 1; break; + case MF_AUTOTRADE: map->list[m].flag.autotrade = 1; break; + case MF_ALLOWKS: map->list[m].flag.allowks = 1; break; + case MF_MONSTER_NOTELEPORT: map->list[m].flag.monster_noteleport = 1; break; + case MF_PVP_NOCALCRANK: map->list[m].flag.pvp_nocalcrank = 1; break; + case MF_BATTLEGROUND: map->list[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break; + case MF_RESET: map->list[m].flag.reset = 1; break; + case MF_NOTOMB: map->list[m].flag.notomb = 1; break; + case MF_NOCASHSHOP: map->list[m].flag.nocashshop = 1; break; } } - + return true; } -BUILDIN(removemapflag) -{ +BUILDIN(removemapflag) { int16 m,i; const char *str; - int val=0; - + str=script_getstr(st,2); i=script_getnum(st,3); - if(script_hasdata(st,4)){ - val=script_getnum(st,4); - } - m = iMap->mapname2mapid(str); + + m = map->mapname2mapid(str); if(m >= 0) { switch(i) { - case MF_NOMEMO: map[m].flag.nomemo = 0; break; - case MF_NOTELEPORT: map[m].flag.noteleport = 0; break; - case MF_NOSAVE: map[m].flag.nosave = 0; break; - case MF_NOBRANCH: map[m].flag.nobranch = 0; break; - case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break; - case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break; + case MF_NOMEMO: map->list[m].flag.nomemo = 0; break; + case MF_NOTELEPORT: map->list[m].flag.noteleport = 0; break; + case MF_NOSAVE: map->list[m].flag.nosave = 0; break; + case MF_NOBRANCH: map->list[m].flag.nobranch = 0; break; + case MF_NOPENALTY: map->list[m].flag.noexppenalty = 0; map->list[m].flag.nozenypenalty = 0; break; + case MF_NOZENYPENALTY: map->list[m].flag.nozenypenalty = 0; break; case MF_PVP: { struct block_list bl; bl.type = BL_NUL; bl.m = m; - map[m].flag.pvp = 0; + map->list[m].flag.pvp = 0; clif->map_property_mapall(m, MAPPROPERTY_NOTHING); clif->maptypeproperty2(&bl,ALL_SAMEMAP); } break; - case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break; - case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break; + case MF_PVP_NOPARTY: map->list[m].flag.pvp_noparty = 0; break; + case MF_PVP_NOGUILD: map->list[m].flag.pvp_noguild = 0; break; case MF_GVG: { struct block_list bl; bl.type = BL_NUL; bl.m = m; - map[m].flag.gvg = 0; + map->list[m].flag.gvg = 0; clif->map_property_mapall(m, MAPPROPERTY_NOTHING); clif->maptypeproperty2(&bl,ALL_SAMEMAP); } break; - case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break; - case MF_NOTRADE: map[m].flag.notrade = 0; break; - case MF_NOSKILL: map[m].flag.noskill = 0; break; - case MF_NOWARP: map[m].flag.nowarp = 0; break; - case MF_PARTYLOCK: map[m].flag.partylock = 0; break; - case MF_NOICEWALL: map[m].flag.noicewall = 0; break; - case MF_SNOW: map[m].flag.snow = 0; break; - case MF_FOG: map[m].flag.fog = 0; break; - case MF_SAKURA: map[m].flag.sakura = 0; break; - case MF_LEAVES: map[m].flag.leaves = 0; break; - case MF_CLOUDS: map[m].flag.clouds = 0; break; - case MF_CLOUDS2: map[m].flag.clouds2 = 0; break; - case MF_FIREWORKS: map[m].flag.fireworks = 0; break; - case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break; - case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break; - case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break; - case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break; - case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break; - case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break; - case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break; - case MF_NORETURN: map[m].flag.noreturn = 0; break; - case MF_NOWARPTO: map[m].flag.nowarpto = 0; break; - case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break; + case MF_GVG_NOPARTY: map->list[m].flag.gvg_noparty = 0; break; + case MF_NOTRADE: map->list[m].flag.notrade = 0; break; + case MF_NOSKILL: map->list[m].flag.noskill = 0; break; + case MF_NOWARP: map->list[m].flag.nowarp = 0; break; + case MF_PARTYLOCK: map->list[m].flag.partylock = 0; break; + case MF_NOICEWALL: map->list[m].flag.noicewall = 0; break; + case MF_SNOW: map->list[m].flag.snow = 0; break; + case MF_FOG: map->list[m].flag.fog = 0; break; + case MF_SAKURA: map->list[m].flag.sakura = 0; break; + case MF_LEAVES: map->list[m].flag.leaves = 0; break; + case MF_CLOUDS: map->list[m].flag.clouds = 0; break; + case MF_CLOUDS2: map->list[m].flag.clouds2 = 0; break; + case MF_FIREWORKS: map->list[m].flag.fireworks = 0; break; + case MF_GVG_CASTLE: map->list[m].flag.gvg_castle = 0; break; + case MF_GVG_DUNGEON: map->list[m].flag.gvg_dungeon = 0; break; + case MF_NIGHTENABLED: map->list[m].flag.nightenabled = 0; break; + case MF_NOBASEEXP: map->list[m].flag.nobaseexp = 0; break; + case MF_NOJOBEXP: map->list[m].flag.nojobexp = 0; break; + case MF_NOMOBLOOT: map->list[m].flag.nomobloot = 0; break; + case MF_NOMVPLOOT: map->list[m].flag.nomvploot = 0; break; + case MF_NORETURN: map->list[m].flag.noreturn = 0; break; + case MF_NOWARPTO: map->list[m].flag.nowarpto = 0; break; + case MF_NIGHTMAREDROP: map->list[m].flag.pvp_nightmaredrop = 0; break; case MF_ZONE: - iMap->zone_change2(m, map[m].prev_zone); + map->zone_change2(m, map->list[m].prev_zone); break; - case MF_NOCOMMAND: map[m].nocommand = 0; break; - case MF_NODROP: map[m].flag.nodrop = 0; break; - case MF_JEXP: map[m].jexp = 0; break; - case MF_BEXP: map[m].bexp = 0; break; - case MF_NOVENDING: map[m].flag.novending = 0; break; - case MF_LOADEVENT: map[m].flag.loadevent = 0; break; - case MF_NOCHAT: map[m].flag.nochat = 0; break; - case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break; - case MF_GUILDLOCK: map[m].flag.guildlock = 0; break; - case MF_TOWN: map[m].flag.town = 0; break; - case MF_AUTOTRADE: map[m].flag.autotrade = 0; break; - case MF_ALLOWKS: map[m].flag.allowks = 0; break; - case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break; - case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break; - case MF_BATTLEGROUND: map[m].flag.battleground = 0; break; - case MF_RESET: map[m].flag.reset = 0; break; + case MF_NOCOMMAND: map->list[m].nocommand = 0; break; + case MF_NODROP: map->list[m].flag.nodrop = 0; break; + case MF_JEXP: map->list[m].jexp = 0; break; + case MF_BEXP: map->list[m].bexp = 0; break; + case MF_NOVENDING: map->list[m].flag.novending = 0; break; + case MF_LOADEVENT: map->list[m].flag.loadevent = 0; break; + case MF_NOCHAT: map->list[m].flag.nochat = 0; break; + case MF_NOEXPPENALTY: map->list[m].flag.noexppenalty = 0; break; + case MF_GUILDLOCK: map->list[m].flag.guildlock = 0; break; + case MF_TOWN: map->list[m].flag.town = 0; break; + case MF_AUTOTRADE: map->list[m].flag.autotrade = 0; break; + case MF_ALLOWKS: map->list[m].flag.allowks = 0; break; + case MF_MONSTER_NOTELEPORT: map->list[m].flag.monster_noteleport = 0; break; + case MF_PVP_NOCALCRANK: map->list[m].flag.pvp_nocalcrank = 0; break; + case MF_BATTLEGROUND: map->list[m].flag.battleground = 0; break; + case MF_RESET: map->list[m].flag.reset = 0; break; + case MF_NOTOMB: map->list[m].flag.notomb = 0; break; + case MF_NOCASHSHOP: map->list[m].flag.nocashshop = 0; break; } } - + return true; } -BUILDIN(pvpon) -{ +BUILDIN(pvpon) { int16 m; const char *str; TBL_PC* sd = NULL; struct s_mapiterator* iter; struct block_list bl; - + str = script_getstr(st,2); - m = iMap->mapname2mapid(str); - if( m < 0 || map[m].flag.pvp ) + m = map->mapname2mapid(str); + if( m < 0 || map->list[m].flag.pvp ) return true; // nothing to do - - iMap->zone_change2(m, strdb_get(zone_db, MAP_ZONE_PVP_NAME)); - map[m].flag.pvp = 1; + + if( !strdb_exists(map->zone_db,MAP_ZONE_PVP_NAME) ) { + ShowError("buildin_pvpon: zone_db missing '%s'\n",MAP_ZONE_PVP_NAME); + return true; + } + + map->zone_change2(m, strdb_get(map->zone_db, MAP_ZONE_PVP_NAME)); + map->list[m].flag.pvp = 1; clif->map_property_mapall(m, MAPPROPERTY_FREEPVPZONE); bl.type = BL_NUL; bl.m = m; clif->maptypeproperty2(&bl,ALL_SAMEMAP); - - + + if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris] return true; - + iter = mapit_getallusers(); for( sd = (TBL_PC*)mapit->first(iter); mapit->exists(iter); sd = (TBL_PC*)mapit->next(iter) ) { if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER ) continue; // not applicable - - sd->pvp_timer = iTimer->add_timer(iTimer->gettick()+200,pc->calc_pvprank_timer,sd->bl.id,0); + + sd->pvp_timer = timer->add(timer->gettick()+200,pc->calc_pvprank_timer,sd->bl.id,0); sd->pvp_rank = 0; sd->pvp_lastusers = 0; sd->pvp_point = 5; @@ -10561,227 +11215,218 @@ BUILDIN(pvpon) sd->pvp_lost = 0; } mapit->free(iter); - + return true; } -static int buildin_pvpoff_sub(struct block_list *bl,va_list ap) +int buildin_pvpoff_sub(struct block_list *bl,va_list ap) { TBL_PC* sd = (TBL_PC*)bl; clif->pvpset(sd, 0, 0, 2); if (sd->pvp_timer != INVALID_TIMER) { - iTimer->delete_timer(sd->pvp_timer, pc->calc_pvprank_timer); + timer->delete(sd->pvp_timer, pc->calc_pvprank_timer); sd->pvp_timer = INVALID_TIMER; } return 0; } -BUILDIN(pvpoff) -{ +BUILDIN(pvpoff) { int16 m; const char *str; struct block_list bl; - + str=script_getstr(st,2); - m = iMap->mapname2mapid(str); - if(m < 0 || !map[m].flag.pvp) + m = map->mapname2mapid(str); + if(m < 0 || !map->list[m].flag.pvp) return true; //fixed Lupus - - iMap->zone_change2(m, map[m].prev_zone); - map[m].flag.pvp = 0; + + map->zone_change2(m, map->list[m].prev_zone); + map->list[m].flag.pvp = 0; clif->map_property_mapall(m, MAPPROPERTY_NOTHING); bl.type = BL_NUL; bl.m = m; clif->maptypeproperty2(&bl,ALL_SAMEMAP); - + if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris] return true; - - iMap->foreachinmap(buildin_pvpoff_sub, m, BL_PC); + + map->foreachinmap(script->buildin_pvpoff_sub, m, BL_PC); return true; } -BUILDIN(gvgon) -{ +BUILDIN(gvgon) { int16 m; const char *str; - + str=script_getstr(st,2); - m = iMap->mapname2mapid(str); - if(m >= 0 && !map[m].flag.gvg) { + m = map->mapname2mapid(str); + if(m >= 0 && !map->list[m].flag.gvg) { struct block_list bl; - iMap->zone_change2(m, strdb_get(zone_db, MAP_ZONE_GVG_NAME)); - map[m].flag.gvg = 1; + + if( !strdb_exists(map->zone_db,MAP_ZONE_GVG_NAME) ) { + ShowError("buildin_gvgon: zone_db missing '%s'\n",MAP_ZONE_GVG_NAME); + return true; + } + + map->zone_change2(m, strdb_get(map->zone_db, MAP_ZONE_GVG_NAME)); + map->list[m].flag.gvg = 1; clif->map_property_mapall(m, MAPPROPERTY_AGITZONE); bl.type = BL_NUL; bl.m = m; clif->maptypeproperty2(&bl,ALL_SAMEMAP); } - + return true; } -BUILDIN(gvgoff) -{ +BUILDIN(gvgoff) { int16 m; const char *str; - + str=script_getstr(st,2); - m = iMap->mapname2mapid(str); - if(m >= 0 && map[m].flag.gvg) { + m = map->mapname2mapid(str); + if(m >= 0 && map->list[m].flag.gvg) { struct block_list bl; - iMap->zone_change2(m, map[m].prev_zone); - map[m].flag.gvg = 0; + map->zone_change2(m, map->list[m].prev_zone); + map->list[m].flag.gvg = 0; clif->map_property_mapall(m, MAPPROPERTY_NOTHING); bl.type = BL_NUL; bl.m = m; clif->maptypeproperty2(&bl,ALL_SAMEMAP); } - + return true; } /*========================================== - * Shows an emoticon on top of the player/npc - * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name> + * Shows an emoticon on top of the player/npc + * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name> *------------------------------------------*/ //Optional second parameter added by [Skotlex] -BUILDIN(emotion) -{ +BUILDIN(emotion) { int type; int player=0; - + type=script_getnum(st,2); if(type < 0 || type > 100) return true; - + if( script_hasdata(st,3) ) player=script_getnum(st,3); - + if (player) { TBL_PC *sd = NULL; if( script_hasdata(st,4) ) - sd = iMap->nick2sd(script_getstr(st,4)); + sd = map->nick2sd(script_getstr(st,4)); else - sd = script_rid2sd(st); + sd = script->rid2sd(st); if (sd) clif->emotion(&sd->bl,type); + } else if( script_hasdata(st,4) ) { + TBL_NPC *nd = npc->name2id(script_getstr(st,4)); + if(nd) + clif->emotion(&nd->bl,type); } else - if( script_hasdata(st,4) ) - { - TBL_NPC *nd = npc_name2id(script_getstr(st,4)); - if(nd) - clif->emotion(&nd->bl,type); - } - else - clif->emotion(iMap->id2bl(st->oid),type); + clif->emotion(map->id2bl(st->oid),type); return true; } -static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap) +int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap) { int16 m=va_arg(ap,int); int g_id=va_arg(ap,int); int flag=va_arg(ap,int); - + if(!sd || sd->bl.m != m) return 0; if( - (sd->status.guild_id == g_id && flag&1) || //Warp out owners - (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders - (sd->status.guild_id == 0) // Warp out players not in guild [Valaris] - ) + (sd->status.guild_id == g_id && flag&1) //Warp out owners + || (sd->status.guild_id != g_id && flag&2) //Warp out outsiders + || (sd->status.guild_id == 0) // Warp out players not in guild [Valaris] + ) pc->setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT); return 1; } -static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap) +int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap) { struct mob_data *md=(struct mob_data *)bl; - + if(!md->guardian_data && md->class_ != MOBID_EMPERIUM) status_kill(bl); - + return 0; } -BUILDIN(maprespawnguildid) -{ +BUILDIN(maprespawnguildid) { const char *mapname=script_getstr(st,2); int g_id=script_getnum(st,3); int flag=script_getnum(st,4); - - int16 m=iMap->mapname2mapid(mapname); - + + int16 m=map->mapname2mapid(mapname); + if(m == -1) return true; - + //Catch ALL players (in case some are 'between maps' on execution time) - iMap->map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag); + map->foreachpc(script->buildin_maprespawnguildid_sub_pc,m,g_id,flag); if (flag&4) //Remove script mobs. - iMap->foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB); + map->foreachinmap(script->buildin_maprespawnguildid_sub_mob,m,BL_MOB); return true; } -BUILDIN(agitstart) -{ - if(iMap->agit_flag==1) return true; // Agit already Start. - iMap->agit_flag=1; +BUILDIN(agitstart) { + if(map->agit_flag==1) return true; // Agit already Start. + map->agit_flag=1; guild->agit_start(); return true; } -BUILDIN(agitend) -{ - if(iMap->agit_flag==0) return true; // Agit already End. - iMap->agit_flag=0; +BUILDIN(agitend) { + if(map->agit_flag==0) return true; // Agit already End. + map->agit_flag=0; guild->agit_end(); return true; } -BUILDIN(agitstart2) -{ - if(iMap->agit2_flag==1) return true; // Agit2 already Start. - iMap->agit2_flag=1; +BUILDIN(agitstart2) { + if(map->agit2_flag==1) return true; // Agit2 already Start. + map->agit2_flag=1; guild->agit2_start(); return true; } -BUILDIN(agitend2) -{ - if(iMap->agit2_flag==0) return true; // Agit2 already End. - iMap->agit2_flag=0; +BUILDIN(agitend2) { + if(map->agit2_flag==0) return true; // Agit2 already End. + map->agit2_flag=0; guild->agit2_end(); return true; } /*========================================== - * Returns whether woe is on or off. // choice script + * Returns whether woe is on or off. *------------------------------------------*/ -BUILDIN(agitcheck) -{ - script_pushint(st,iMap->agit_flag); +BUILDIN(agitcheck) { + script_pushint(st,map->agit_flag); return true; } /*========================================== - * Returns whether woese is on or off. // choice script + * Returns whether woese is on or off. *------------------------------------------*/ -BUILDIN(agitcheck2) -{ - script_pushint(st,iMap->agit2_flag); +BUILDIN(agitcheck2) { + script_pushint(st,map->agit2_flag); return true; } /// Sets the guild_id of this npc. /// /// flagemblem <guild_id>; -BUILDIN(flagemblem) -{ +BUILDIN(flagemblem) { TBL_NPC* nd; int g_id = script_getnum(st,2); - + if(g_id < 0) return true; - - nd = (TBL_NPC*)iMap->id2nd(st->oid); + + nd = (TBL_NPC*)map->id2nd(st->oid); if( nd == NULL ) { ShowError("script:flagemblem: npc %d not found\n", st->oid); } else if( nd->subtype != SCRIPT ) { @@ -10801,7 +11446,7 @@ BUILDIN(flagemblem) BUILDIN(getcastlename) { - const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL); + const char* mapname = mapindex->getmapname(script_getstr(st,2),NULL); struct guild_castle* gc = guild->mapname2gc(mapname); const char* name = (gc) ? gc->castle_name : ""; script_pushstrcopy(st,name); @@ -10810,16 +11455,16 @@ BUILDIN(getcastlename) BUILDIN(getcastledata) { - const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL); + const char *mapname = mapindex->getmapname(script_getstr(st,2),NULL); int index = script_getnum(st,3); struct guild_castle *gc = guild->mapname2gc(mapname); - + if (gc == NULL) { script_pushint(st,0); ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname); return false; } - + switch (index) { case 1: script_pushint(st,gc->guild_id); break; @@ -10853,21 +11498,21 @@ BUILDIN(getcastledata) BUILDIN(setcastledata) { - const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL); + const char *mapname = mapindex->getmapname(script_getstr(st,2),NULL); int index = script_getnum(st,3); int value = script_getnum(st,4); struct guild_castle *gc = guild->mapname2gc(mapname); - + if (gc == NULL) { ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname); return false; } - + if (index <= 0 || index > 9+MAX_GUARDIANS) { ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index); return false; } - + guild->castledatasave(gc->castle_id, index, value); return true; } @@ -10878,12 +11523,12 @@ BUILDIN(requestguildinfo) { int guild_id=script_getnum(st,2); const char *event=NULL; - - if( script_hasdata(st,3) ){ + + if( script_hasdata(st,3) ) { event=script_getstr(st,3); - check_event(st, event); + script->check_event(st, event); } - + if(guild_id>0) guild->npc_request_info(guild_id,event); return true; @@ -10896,28 +11541,32 @@ BUILDIN(getequipcardcnt) int i=-1,j,num; TBL_PC *sd; int count; - + num=script_getnum(st,2); - sd=script_rid2sd(st); - if (num > 0 && num <= ARRAYLENGTH(equip)) - i=pc->checkequip(sd,equip[num-1]); - + sd=script->rid2sd(st); + + if( sd == NULL ) + return false; + + if (num > 0 && num <= ARRAYLENGTH(script->equip)) + i=pc->checkequip(sd,script->equip[num-1]); + if (i < 0 || !sd->inventory_data[i]) { script_pushint(st,0); return true; } - + if(itemdb_isspecial(sd->status.inventory[i].card[0])) { script_pushint(st,0); return true; } - + count = 0; for( j = 0; j < sd->inventory_data[i]->slot; j++ ) if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD ) count++; - + script_pushint(st,count); return true; } @@ -10927,20 +11576,23 @@ BUILDIN(getequipcardcnt) /// successremovecards <slot>; BUILDIN(successremovecards) { int i=-1,j,c,cardflag=0; - - TBL_PC* sd = script_rid2sd(st); + + TBL_PC* sd = script->rid2sd(st); int num = script_getnum(st,2); - - if (num > 0 && num <= ARRAYLENGTH(equip)) - i=pc->checkequip(sd,equip[num-1]); - + + if( sd == NULL ) + return false; + + if (num > 0 && num <= ARRAYLENGTH(script->equip)) + i=pc->checkequip(sd,script->equip[num-1]); + if (i < 0 || !sd->inventory_data[i]) { return true; } - + if(itemdb_isspecial(sd->status.inventory[i].card[0])) return true; - + for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) { if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {// extract this card from the item int flag; @@ -10949,34 +11601,37 @@ BUILDIN(successremovecards) { cardflag = 1; item_tmp.nameid = sd->status.inventory[i].card[c]; item_tmp.identify = 1; - - if((flag=pc->additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory + + if((flag=pc->additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))) { + // get back the cart in inventory clif->additem(sd,0,0,flag); - iMap->addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0); + map->addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0); } } } - + if(cardflag == 1) {//if card was remove remplace item with no card int flag; struct item item_tmp; memset(&item_tmp,0,sizeof(item_tmp)); - + item_tmp.nameid = sd->status.inventory[i].nameid; item_tmp.identify = 1; item_tmp.refine = sd->status.inventory[i].refine; item_tmp.attribute = sd->status.inventory[i].attribute; item_tmp.expire_time = sd->status.inventory[i].expire_time; - + item_tmp.bound = sd->status.inventory[i].bound; + for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++) item_tmp.card[j]=sd->status.inventory[i].card[j]; - + pc->delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT); - if((flag=pc->additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor + if((flag=pc->additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))) { + //chk if can be spawn in inventory otherwise put on floor clif->additem(sd,0,0,flag); - iMap->addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0); + map->addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0); } - + clif->misceffect(&sd->bl,3); } return true; @@ -10990,80 +11645,86 @@ BUILDIN(successremovecards) { /// <type>=? : will just display the failure effect. BUILDIN(failedremovecards) { int i=-1,j,c,cardflag=0; - - TBL_PC* sd = script_rid2sd(st); + + TBL_PC* sd = script->rid2sd(st); int num = script_getnum(st,2); int typefail = script_getnum(st,3); - - if (num > 0 && num <= ARRAYLENGTH(equip)) - i=pc->checkequip(sd,equip[num-1]); - + + if( sd == NULL ) + return false; + + if (num > 0 && num <= ARRAYLENGTH(script->equip)) + i=pc->checkequip(sd,script->equip[num-1]); + if (i < 0 || !sd->inventory_data[i]) return true; - + if(itemdb_isspecial(sd->status.inventory[i].card[0])) return true; - + for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) { if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) { cardflag = 1; - + if(typefail == 2) {// add cards to inventory, clear int flag; struct item item_tmp; - + memset(&item_tmp,0,sizeof(item_tmp)); - + item_tmp.nameid = sd->status.inventory[i].card[c]; item_tmp.identify = 1; - - if((flag=pc->additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ + + if((flag=pc->additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))) { clif->additem(sd,0,0,flag); - iMap->addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0); + map->addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0); } } } } - + if(cardflag == 1) { - if(typefail == 0 || typefail == 2){ // destroy the item + if(typefail == 0 || typefail == 2) { + // destroy the item pc->delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT); } - if(typefail == 1){ // destroy the card + if(typefail == 1) { + // destroy the card int flag; struct item item_tmp; - + memset(&item_tmp,0,sizeof(item_tmp)); - + item_tmp.nameid = sd->status.inventory[i].nameid; item_tmp.identify = 1; item_tmp.refine = sd->status.inventory[i].refine; item_tmp.attribute = sd->status.inventory[i].attribute; item_tmp.expire_time = sd->status.inventory[i].expire_time; - + item_tmp.bound = sd->status.inventory[i].bound; + for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++) item_tmp.card[j]=sd->status.inventory[i].card[j]; - + pc->delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT); - - if((flag=pc->additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ + + if((flag=pc->additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))) { clif->additem(sd,0,0,flag); - iMap->addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0); + map->addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0); } } clif->misceffect(&sd->bl,2); } - + return true; } /* ================================================================ * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>; - * type: 0=everyone, 1=guild, 2=party; [Reddozen] + * type: 0=everyone, 1=guild, 2=party; [Reddozen] * improved by [Lance] * ================================================================*/ -BUILDIN(mapwarp) // Added by RoVeRT -{ +// Added by RoVeRT +BUILDIN(mapwarp) { int x,y,m,check_val=0,check_ID=0,i=0; struct guild *g = NULL; struct party_data *p = NULL; @@ -11074,24 +11735,24 @@ BUILDIN(mapwarp) // Added by RoVeRT str=script_getstr(st,3); x=script_getnum(st,4); y=script_getnum(st,5); - if(script_hasdata(st,7)){ + if(script_hasdata(st,7)) { check_val=script_getnum(st,6); check_ID=script_getnum(st,7); } - - if((m=iMap->mapname2mapid(mapname))< 0) + + if((m=map->mapname2mapid(mapname))< 0) return true; - - if(!(index=mapindex_name2id(str))) + + if(!(index=script->mapindexname2id(st,str))) return true; - - switch(check_val){ + + switch(check_val) { case 1: g = guild->search(check_ID); - if (g){ + if (g) { for( i=0; i < g->max_member; i++) { - if(g->member[i].sd && g->member[i].sd->bl.m==m){ + if(g->member[i].sd && g->member[i].sd->bl.m==m) { pc->setpos(g->member[i].sd,index,x,y,CLR_TELEPORT); } } @@ -11099,24 +11760,24 @@ BUILDIN(mapwarp) // Added by RoVeRT break; case 2: p = party->search(check_ID); - if(p){ - for(i=0;i<MAX_PARTY; i++){ - if(p->data[i].sd && p->data[i].sd->bl.m == m){ + if(p) { + for(i=0;i<MAX_PARTY; i++) { + if(p->data[i].sd && p->data[i].sd->bl.m == m) { pc->setpos(p->data[i].sd,index,x,y,CLR_TELEPORT); } } } break; default: - iMap->foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0); + map->foreachinmap(script->buildin_areawarp_sub,m,BL_PC,index,x,y,0,0); break; } - + return true; } -static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT -{ +// Added by RoVeRT +int buildin_mobcount_sub(struct block_list *bl,va_list ap) { char *event=va_arg(ap,char *); struct mob_data *md = ((struct mob_data *)bl); if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) ) @@ -11124,70 +11785,67 @@ static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by Ro return 0; } -BUILDIN(mobcount) // Added by RoVeRT -{ +// Added by RoVeRT +BUILDIN(mobcount) { const char *mapname,*event; int16 m; mapname=script_getstr(st,2); event=script_getstr(st,3); - + if( strcmp(event, "all") == 0 ) event = NULL; else - check_event(st, event); + script->check_event(st, event); if( strcmp(mapname, "this") == 0 ) { - struct map_session_data *sd = script_rid2sd(st); - if( sd ) - m = sd->bl.m; - else { - script_pushint(st,-1); - return true; - } - } else if( (m = iMap->mapname2mapid(mapname)) < 0 ) { + struct map_session_data *sd = script->rid2sd(st); + + if( sd == NULL ) + return false; + + m = sd->bl.m; + } else if( (m = map->mapname2mapid(mapname)) < 0 ) { script_pushint(st,-1); return true; } - if( map[m].flag.src4instance && map[m].instance_id == -1 && st->instance_id >= 0 && (m = instance->mapid2imapid(m, st->instance_id)) < 0 ) { + if( map->list[m].flag.src4instance && map->list[m].instance_id == -1 && st->instance_id >= 0 && (m = instance->mapid2imapid(m, st->instance_id)) < 0 ) { script_pushint(st,-1); return true; } - script_pushint(st,iMap->foreachinmap(buildin_mobcount_sub, m, BL_MOB, event)); - + script_pushint(st,map->foreachinmap(script->buildin_mobcount_sub, m, BL_MOB, event)); + return true; } -BUILDIN(marriage) -{ +BUILDIN(marriage) { const char *partner=script_getstr(st,2); - TBL_PC *sd=script_rid2sd(st); - TBL_PC *p_sd=iMap->nick2sd(partner); - - if(sd==NULL || p_sd==NULL || pc->marriage(sd,p_sd) < 0){ + TBL_PC *sd=script->rid2sd(st); + TBL_PC *p_sd=map->nick2sd(partner); + + if(sd==NULL || p_sd==NULL || pc->marriage(sd,p_sd) < 0) { script_pushint(st,0); return true; } script_pushint(st,1); return true; } -BUILDIN(wedding_effect) -{ - TBL_PC *sd=script_rid2sd(st); +BUILDIN(wedding_effect) { + TBL_PC *sd=script->rid2sd(st); struct block_list *bl; - - if(sd==NULL) { - bl=iMap->id2bl(st->oid); - } else - bl=&sd->bl; + + if( sd == NULL ) + return false; //bl=map->id2bl(st->oid); + + bl=&sd->bl; clif->wedding_effect(bl); return true; } BUILDIN(divorce) { - TBL_PC *sd=script_rid2sd(st); - if(sd==NULL || pc->divorce(sd) < 0){ + TBL_PC *sd=script->rid2sd(st); + if(sd==NULL || pc->divorce(sd) < 0) { script_pushint(st,0); return true; } @@ -11195,89 +11853,76 @@ BUILDIN(divorce) return true; } -BUILDIN(ispartneron) -{ - TBL_PC *sd=script_rid2sd(st); - - if(sd==NULL || !pc->ismarried(sd) || - iMap->charid2sd(sd->status.partner_id) == NULL) { +BUILDIN(ispartneron) { + TBL_PC *sd=script->rid2sd(st); + + if (sd==NULL || !pc->ismarried(sd) + || map->charid2sd(sd->status.partner_id) == NULL) { script_pushint(st,0); return true; } - + script_pushint(st,1); return true; } -BUILDIN(getpartnerid) -{ - TBL_PC *sd=script_rid2sd(st); - if (sd == NULL) { - script_pushint(st,0); - return true; - } - - script_pushint(st,sd->status.partner_id); - return true; +BUILDIN(getpartnerid) { + TBL_PC *sd=script->rid2sd(st); + if( sd == NULL ) + return false; + + script_pushint(st,sd->status.partner_id); + return true; } -BUILDIN(getchildid) -{ - TBL_PC *sd=script_rid2sd(st); - if (sd == NULL) { - script_pushint(st,0); - return true; - } - - script_pushint(st,sd->status.child); - return true; +BUILDIN(getchildid) { + TBL_PC *sd=script->rid2sd(st); + if( sd == NULL ) + return false; + + script_pushint(st,sd->status.child); + return true; } -BUILDIN(getmotherid) -{ - TBL_PC *sd=script_rid2sd(st); - if (sd == NULL) { - script_pushint(st,0); - return true; - } - - script_pushint(st,sd->status.mother); - return true; +BUILDIN(getmotherid) { + TBL_PC *sd=script->rid2sd(st); + if( sd == NULL ) + return false; + + script_pushint(st,sd->status.mother); + return true; } -BUILDIN(getfatherid) -{ - TBL_PC *sd=script_rid2sd(st); - if (sd == NULL) { - script_pushint(st,0); - return true; - } - - script_pushint(st,sd->status.father); - return true; +BUILDIN(getfatherid) { + TBL_PC *sd=script->rid2sd(st); + if( sd == NULL ) + return false; + + script_pushint(st,sd->status.father); + return true; } BUILDIN(warppartner) { int x,y; - unsigned short mapindex; + unsigned short map_index; const char *str; - TBL_PC *sd=script_rid2sd(st); + TBL_PC *sd=script->rid2sd(st); TBL_PC *p_sd=NULL; - - if(sd==NULL || !pc->ismarried(sd) || - (p_sd=iMap->charid2sd(sd->status.partner_id)) == NULL) { + + if ( sd==NULL || !pc->ismarried(sd) + || (p_sd=map->charid2sd(sd->status.partner_id)) == NULL) { script_pushint(st,0); return true; } - + str=script_getstr(st,2); x=script_getnum(st,3); y=script_getnum(st,4); - - mapindex = mapindex_name2id(str); - if (mapindex) { - pc->setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT); + + map_index = script->mapindexname2id(st,str); + if (map_index) { + pc->setpos(p_sd,map_index,x,y,CLR_OUTSIGHT); script_pushint(st,1); } else script_pushint(st,0); @@ -11289,11 +11934,11 @@ BUILDIN(warppartner) *------------------------------------------------*/ BUILDIN(strmobinfo) { - + int num=script_getnum(st,2); int class_=script_getnum(st,3); - - if(!mobdb_checkid(class_)) + + if(!mob->db_checkid(class_)) { if (num < 3) //requested a string script_pushconststr(st,""); @@ -11301,15 +11946,15 @@ BUILDIN(strmobinfo) script_pushint(st,0); return true; } - + switch (num) { - case 1: script_pushstrcopy(st,mob_db(class_)->name); break; - case 2: script_pushstrcopy(st,mob_db(class_)->jname); break; - case 3: script_pushint(st,mob_db(class_)->lv); break; - case 4: script_pushint(st,mob_db(class_)->status.max_hp); break; - case 5: script_pushint(st,mob_db(class_)->status.max_sp); break; - case 6: script_pushint(st,mob_db(class_)->base_exp); break; - case 7: script_pushint(st,mob_db(class_)->job_exp); break; + case 1: script_pushstrcopy(st,mob->db(class_)->name); break; + case 2: script_pushstrcopy(st,mob->db(class_)->jname); break; + case 3: script_pushint(st,mob->db(class_)->lv); break; + case 4: script_pushint(st,mob->db(class_)->status.max_hp); break; + case 5: script_pushint(st,mob->db(class_)->status.max_sp); break; + case 6: script_pushint(st,mob->db(class_)->base_exp); break; + case 7: script_pushint(st,mob->db(class_)->job_exp); break; default: script_pushint(st,0); break; @@ -11321,74 +11966,70 @@ BUILDIN(strmobinfo) * Summon guardians [Valaris] * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id> *------------------------------------------*/ -BUILDIN(guardian) -{ - int class_=0,x=0,y=0,guardian=0; - const char *str,*map,*evt=""; - struct script_data *data; +BUILDIN(guardian) { + int class_ = 0, x = 0, y = 0, guardian = 0; + const char *str, *mapname, *evt=""; bool has_index = false; - - map =script_getstr(st,2); - x =script_getnum(st,3); - y =script_getnum(st,4); - str =script_getstr(st,5); - class_=script_getnum(st,6); - + + mapname = script_getstr(st,2); + x = script_getnum(st,3); + y = script_getnum(st,4); + str = script_getstr(st,5); + class_ = script_getnum(st,6); + if( script_hasdata(st,8) ) {// "<event label>",<guardian index> evt=script_getstr(st,7); guardian=script_getnum(st,8); has_index = true; - } else if( script_hasdata(st,7) ){ - data=script_getdata(st,7); - script->get_val(st,data); - if( data_isstring(data) ) - {// "<event label>" + } else if( script_hasdata(st,7) ) { + struct script_data *data = script_getdata(st,7); + script->get_val(st,data); // Dereference if it's a variable + if( data_isstring(data) ) { + // "<event label>" evt=script_getstr(st,7); - } else if( data_isint(data) ) - {// <guardian index> + } else if( data_isint(data) ) { + // <guardian index> guardian=script_getnum(st,7); has_index = true; } else { ShowError("script:guardian: invalid data type for argument #6 (from 1)\n"); - script_reportdata(data); + script->reportdata(data); return false; } } - - check_event(st, evt); - script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index)); - + + script->check_event(st, evt); + script_pushint(st, mob->spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index)); + return true; } /*========================================== * Invisible Walls [Zephyrus] *------------------------------------------*/ -BUILDIN(setwall) -{ - const char *map, *name; +BUILDIN(setwall) { + const char *mapname, *name; int x, y, m, size, dir; bool shootable; - - map = script_getstr(st,2); - x = script_getnum(st,3); - y = script_getnum(st,4); - size = script_getnum(st,5); - dir = script_getnum(st,6); + + mapname = script_getstr(st,2); + x = script_getnum(st,3); + y = script_getnum(st,4); + size = script_getnum(st,5); + dir = script_getnum(st,6); shootable = script_getnum(st,7); - name = script_getstr(st,8); - - if( (m = iMap->mapname2mapid(map)) < 0 ) + name = script_getstr(st,8); + + if( (m = map->mapname2mapid(mapname)) < 0 ) return true; // Invalid Map - - iMap->iwall_set(m, x, y, size, dir, shootable, name); + + map->iwall_set(m, x, y, size, dir, shootable, name); return true; } -BUILDIN(delwall) -{ +BUILDIN(delwall) { const char *name = script_getstr(st,2); - iMap->iwall_remove(name); - + map->iwall_remove(name); + return true; } @@ -11399,69 +12040,59 @@ BUILDIN(delwall) /// 1 - maximum hp /// 2 - current hp /// -BUILDIN(guardianinfo) -{ - const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL); +BUILDIN(guardianinfo) { + const char* mapname = mapindex->getmapname(script_getstr(st,2),NULL); int id = script_getnum(st,3); int type = script_getnum(st,4); - + struct guild_castle* gc = guild->mapname2gc(mapname); struct mob_data* gd; - - if( gc == NULL || id < 0 || id >= MAX_GUARDIANS ) - { + + if( gc == NULL || id < 0 || id >= MAX_GUARDIANS ) { script_pushint(st,-1); return true; } - + if( type == 0 ) script_pushint(st, gc->guardian[id].visible); + else if( !gc->guardian[id].visible ) + script_pushint(st,-1); + else if( (gd = map->id2md(gc->guardian[id].id)) == NULL ) + script_pushint(st,-1); + else if( type == 1 ) + script_pushint(st,gd->status.max_hp); + else if( type == 2 ) + script_pushint(st,gd->status.hp); else - if( !gc->guardian[id].visible ) - script_pushint(st,-1); - else - if( (gd = iMap->id2md(gc->guardian[id].id)) == NULL ) - script_pushint(st,-1); - else - { - if ( type == 1 ) script_pushint(st,gd->status.max_hp); - else if( type == 2 ) script_pushint(st,gd->status.hp); - else - script_pushint(st,-1); - } - + script_pushint(st,-1); + return true; } /*========================================== * Get the item name by item_id or null *------------------------------------------*/ -BUILDIN(getitemname) -{ +BUILDIN(getitemname) { int item_id=0; struct item_data *i_data; char *item_name; - struct script_data *data; - - data=script_getdata(st,2); - script->get_val(st,data); - - if( data_isstring(data) ){ - const char *name=script->conv_str(st,data); - struct item_data *item_data = itemdb_searchname(name); + + if( script_isstringtype(st, 2) ) { + const char *name = script_getstr(st, 2); + struct item_data *item_data = itemdb->search_name(name); if( item_data ) item_id=item_data->nameid; - }else - item_id=script->conv_num(st,data); - - i_data = itemdb_exists(item_id); - if (i_data == NULL) - { + } else { + item_id = script_getnum(st, 2); + } + + i_data = itemdb->exists(item_id); + if (i_data == NULL) { script_pushconststr(st,"null"); return true; } item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char)); - + memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH); script_pushstr(st,item_name); return true; @@ -11473,11 +12104,11 @@ BUILDIN(getitemslots) { int item_id; struct item_data *i_data; - + item_id=script_getnum(st,2); - - i_data = itemdb_exists(item_id); - + + i_data = itemdb->exists(item_id); + if (i_data) script_pushint(st,i_data->slot); else @@ -11490,35 +12121,35 @@ BUILDIN(getitemslots) /*========================================== * Returns some values of an item [Lupus] * Price, Weight, etc... - getiteminfo(itemID,n), where n - 0 value_buy; - 1 value_sell; - 2 type; - 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc.. - if = 0, then monsters don't drop it at all (rare or a quest item) - if = -1, then this item is sold in NPC shops only - 4 sex; - 5 equip; - 6 weight; - 7 atk; - 8 def; - 9 range; - 10 slot; - 11 look; - 12 elv; - 13 wlv; - 14 view id + * getiteminfo(itemID,n), where n + * 0 value_buy; + * 1 value_sell; + * 2 type; + * 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc.. + * if = 0, then monsters don't drop it at all (rare or a quest item) + * if = -1, then this item is sold in NPC shops only + * 4 sex; + * 5 equip; + * 6 weight; + * 7 atk; + * 8 def; + * 9 range; + * 10 slot; + * 11 look; + * 12 elv; + * 13 wlv; + * 14 view id *------------------------------------------*/ BUILDIN(getiteminfo) { int item_id,n; int *item_arr; struct item_data *i_data; - - item_id = script_getnum(st,2); - n = script_getnum(st,3); - i_data = itemdb_exists(item_id); - + + item_id = script_getnum(st,2); + n = script_getnum(st,3); + i_data = itemdb->exists(item_id); + if (i_data && n>=0 && n<=14) { item_arr = (int*)&i_data->value_buy; script_pushint(st,item_arr[n]); @@ -11530,24 +12161,24 @@ BUILDIN(getiteminfo) /*========================================== * Set some values of an item [Lupus] * Price, Weight, etc... - setiteminfo(itemID,n,Value), where n - 0 value_buy; - 1 value_sell; - 2 type; - 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc.. - if = 0, then monsters don't drop it at all (rare or a quest item) - if = -1, then this item is sold in NPC shops only - 4 sex; - 5 equip; - 6 weight; - 7 atk; - 8 def; - 9 range; - 10 slot; - 11 look; - 12 elv; - 13 wlv; - 14 view id + * setiteminfo(itemID,n,Value), where n + * 0 value_buy; + * 1 value_sell; + * 2 type; + * 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc.. + * if = 0, then monsters don't drop it at all (rare or a quest item) + * if = -1, then this item is sold in NPC shops only + * 4 sex; + * 5 equip; + * 6 weight; + * 7 atk; + * 8 def; + * 9 range; + * 10 slot; + * 11 look; + * 12 elv; + * 13 wlv; + * 14 view id * Returns Value or -1 if the wrong field's been set *------------------------------------------*/ BUILDIN(setiteminfo) @@ -11555,12 +12186,12 @@ BUILDIN(setiteminfo) int item_id,n,value; int *item_arr; struct item_data *i_data; - - item_id = script_getnum(st,2); - n = script_getnum(st,3); - value = script_getnum(st,4); - i_data = itemdb_exists(item_id); - + + item_id = script_getnum(st,2); + n = script_getnum(st,3); + value = script_getnum(st,4); + i_data = itemdb->exists(item_id); + if (i_data && n>=0 && n<=14) { item_arr = (int*)&i_data->value_buy; item_arr[n] = value; @@ -11572,31 +12203,35 @@ BUILDIN(setiteminfo) /*========================================== * Returns value from equipped item slot n [Lupus] - getequpcardid(num,slot) - where - num = eqip position slot - slot = 0,1,2,3 (Card Slot N) - - This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced) - it's useful when you want to check item cards or if it's signed - Useful for such quests as "Sign this refined item with players name" etc - Hat[0] +4 -> Player's Hat[0] +4 + * getequpcardid(num,slot) + * where + * num = eqip position slot + * slot = 0,1,2,3 (Card Slot N) + * + * This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced) + * it's useful when you want to check item cards or if it's signed + * Useful for such quests as "Sign this refined item with players name" etc + * Hat[0] +4 -> Player's Hat[0] +4 *------------------------------------------*/ BUILDIN(getequipcardid) { int i=-1,num,slot; TBL_PC *sd; - + num=script_getnum(st,2); slot=script_getnum(st,3); - sd=script_rid2sd(st); - if (num > 0 && num <= ARRAYLENGTH(equip)) - i=pc->checkequip(sd,equip[num-1]); + sd=script->rid2sd(st); + + if( sd == NULL ) + return false; + + if (num > 0 && num <= ARRAYLENGTH(script->equip)) + i=pc->checkequip(sd,script->equip[num-1]); if(i >= 0 && slot>=0 && slot<4) script_pushint(st,sd->status.inventory[i].card[slot]); else script_pushint(st,0); - + return true; } @@ -11606,34 +12241,34 @@ BUILDIN(getequipcardid) BUILDIN(petskillbonus) { struct pet_data *pd; - - TBL_PC *sd=script_rid2sd(st); - + + TBL_PC *sd=script->rid2sd(st); + if(sd==NULL || sd->pd==NULL) return true; - + pd=sd->pd; if (pd->bonus) { //Clear previous bonus if (pd->bonus->timer != INVALID_TIMER) - iTimer->delete_timer(pd->bonus->timer, pet_skill_bonus_timer); + timer->delete(pd->bonus->timer, pet->skill_bonus_timer); } else //init pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus)); - + pd->bonus->type=script_getnum(st,2); pd->bonus->val=script_getnum(st,3); pd->bonus->duration=script_getnum(st,4); pd->bonus->delay=script_getnum(st,5); - + if (pd->state.skillbonus == 1) - pd->state.skillbonus=0; // waiting state - + pd->state.skillbonus=0; // waiting state + // wait for timer to start if (battle_config.pet_equip_required && pd->pet.equip == 0) pd->bonus->timer = INVALID_TIMER; else - pd->bonus->timer = iTimer->add_timer(iTimer->gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0); - + pd->bonus->timer = timer->add(timer->gettick()+pd->bonus->delay*1000, pet->skill_bonus_timer, sd->bl.id, 0); + return true; } @@ -11644,33 +12279,33 @@ BUILDIN(petloot) { int max; struct pet_data *pd; - TBL_PC *sd=script_rid2sd(st); - + TBL_PC *sd=script->rid2sd(st); + if(sd==NULL || sd->pd==NULL) return true; - + max=script_getnum(st,2); - + if(max < 1) - max = 1; //Let'em loot at least 1 item. + max = 1; //Let'em loot at least 1 item. else if (max > MAX_PETLOOT_SIZE) max = MAX_PETLOOT_SIZE; - + pd = sd->pd; - if (pd->loot != NULL) - { //Release whatever was there already and reallocate memory - pet_lootitem_drop(pd, pd->msd); + if (pd->loot != NULL) { + //Release whatever was there already and reallocate memory + pet->lootitem_drop(pd, pd->msd); aFree(pd->loot->item); } else pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot)); - + pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item)); - + pd->loot->max=max; pd->loot->count = 0; pd->loot->weight = 0; - + return true; } /*========================================== @@ -11682,52 +12317,53 @@ BUILDIN(petloot) *------------------------------------------*/ BUILDIN(getinventorylist) { - TBL_PC *sd=script_rid2sd(st); + TBL_PC *sd=script->rid2sd(st); char card_var[NAME_LENGTH]; - + int i,j=0,k; if(!sd) return true; - for(i=0;i<MAX_INVENTORY;i++){ - if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){ - pc->setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid); - pc->setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount); - pc->setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip); - pc->setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine); - pc->setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify); - pc->setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute); + for(i=0;i<MAX_INVENTORY;i++) { + if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0) { + pc->setreg(sd,reference_uid(script->add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid); + pc->setreg(sd,reference_uid(script->add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount); + pc->setreg(sd,reference_uid(script->add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip); + pc->setreg(sd,reference_uid(script->add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine); + pc->setreg(sd,reference_uid(script->add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify); + pc->setreg(sd,reference_uid(script->add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute); for (k = 0; k < MAX_SLOTS; k++) { sprintf(card_var, "@inventorylist_card%d",k+1); - pc->setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]); + pc->setreg(sd,reference_uid(script->add_str(card_var), j),sd->status.inventory[i].card[k]); } - pc->setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time); + pc->setreg(sd,reference_uid(script->add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time); + pc->setreg(sd,reference_uid(script->add_str("@inventorylist_bound"), j),sd->status.inventory[i].bound); j++; } } - pc->setreg(sd,add_str("@inventorylist_count"),j); + pc->setreg(sd,script->add_str("@inventorylist_count"),j); return true; } BUILDIN(getskilllist) { - TBL_PC *sd=script_rid2sd(st); + TBL_PC *sd=script->rid2sd(st); int i,j=0; if(!sd) return true; - for(i=0;i<MAX_SKILL;i++){ - if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){ - pc->setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id); - pc->setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv); - pc->setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag); + for(i=0;i<MAX_SKILL;i++) { + if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0) { + pc->setreg(sd,reference_uid(script->add_str("@skilllist_id"), j),sd->status.skill[i].id); + pc->setreg(sd,reference_uid(script->add_str("@skilllist_lv"), j),sd->status.skill[i].lv); + pc->setreg(sd,reference_uid(script->add_str("@skilllist_flag"), j),sd->status.skill[i].flag); j++; } } - pc->setreg(sd,add_str("@skilllist_count"),j); + pc->setreg(sd,script->add_str("@skilllist_count"),j); return true; } BUILDIN(clearitem) { - TBL_PC *sd=script_rid2sd(st); + TBL_PC *sd=script->rid2sd(st); int i; if(sd==NULL) return true; for (i=0; i<MAX_INVENTORY; i++) { @@ -11744,17 +12380,17 @@ BUILDIN(clearitem) BUILDIN(disguise) { int id; - TBL_PC* sd = script_rid2sd(st); + TBL_PC* sd = script->rid2sd(st); if (sd == NULL) return true; - + id = script_getnum(st,2); - - if (mobdb_checkid(id) || npcdb_checkid(id)) { + + if (mob->db_checkid(id) || npcdb_checkid(id)) { pc->disguise(sd, id); script_pushint(st,id); } else script_pushint(st,0); - + return true; } @@ -11763,9 +12399,9 @@ BUILDIN(disguise) *------------------------------------------*/ BUILDIN(undisguise) { - TBL_PC* sd = script_rid2sd(st); + TBL_PC* sd = script->rid2sd(st); if (sd == NULL) return true; - + if (sd->disguise != -1) { pc->disguise(sd, -1); script_pushint(st,0); @@ -11776,19 +12412,18 @@ BUILDIN(undisguise) } /*========================================== - * Transform a bl to another _class, + * Transform a bl to another class, * @type unused *------------------------------------------*/ -BUILDIN(classchange) -{ - int _class,type; - struct block_list *bl=iMap->id2bl(st->oid); - +BUILDIN(classchange) { + int class_,type; + struct block_list *bl=map->id2bl(st->oid); + if(bl==NULL) return true; - - _class=script_getnum(st,2); + + class_=script_getnum(st,2); type=script_getnum(st,3); - clif->class_change(bl,_class,type); + clif->class_change(bl,class_,type); return true; } @@ -11798,14 +12433,14 @@ BUILDIN(classchange) BUILDIN(misceffect) { int type; - + type=script_getnum(st,2); - if(st->oid && st->oid != fake_nd->bl.id) { - struct block_list *bl = iMap->id2bl(st->oid); + if(st->oid && st->oid != npc->fake_nd->bl.id) { + struct block_list *bl = map->id2bl(st->oid); if (bl) clif->specialeffect(bl,type,AREA); - } else{ - TBL_PC *sd=script_rid2sd(st); + } else { + TBL_PC *sd=script->rid2sd(st); if(sd) clif->specialeffect(&sd->bl,type,AREA); } @@ -11814,34 +12449,34 @@ BUILDIN(misceffect) /*========================================== * Play a BGM on a single client [Rikter/Yommy] *------------------------------------------*/ -BUILDIN(playBGM) +BUILDIN(playbgm) { const char* name; struct map_session_data* sd; - - if( ( sd = script_rid2sd(st) ) != NULL ) + + if( ( sd = script->rid2sd(st) ) != NULL ) { name = script_getstr(st,2); - + clif->playBGM(sd, name); } - + return true; } -static int playBGM_sub(struct block_list* bl,va_list ap) +int playbgm_sub(struct block_list* bl,va_list ap) { const char* name = va_arg(ap,const char*); - + clif->playBGM(BL_CAST(BL_PC, bl), name); - + return 0; } -static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args) +int playbgm_foreachpc_sub(struct map_session_data* sd, va_list args) { const char* name = va_arg(args, const char*); - + clif->playBGM(sd, name); return 0; } @@ -11849,33 +12484,42 @@ static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args) /*========================================== * Play a BGM on multiple client [Rikter/Yommy] *------------------------------------------*/ -BUILDIN(playBGMall) -{ +BUILDIN(playbgmall) { const char* name; - + name = script_getstr(st,2); - - if( script_hasdata(st,7) ) - {// specified part of map - const char* map = script_getstr(st,3); + + if( script_hasdata(st,7) ) { + // specified part of map + const char *mapname = script_getstr(st,3); int x0 = script_getnum(st,4); int y0 = script_getnum(st,5); int x1 = script_getnum(st,6); int y1 = script_getnum(st,7); - - iMap->foreachinarea(playBGM_sub, iMap->mapname2mapid(map), x0, y0, x1, y1, BL_PC, name); - } - else if( script_hasdata(st,3) ) - {// entire map - const char* map = script_getstr(st,3); - - iMap->foreachinmap(playBGM_sub, iMap->mapname2mapid(map), BL_PC, name); - } - else - {// entire server - iMap->map_foreachpc(&playBGM_foreachpc_sub, name); + int m; + + if ( ( m = map->mapname2mapid(mapname) ) == -1 ) { + ShowWarning("playbgmall: Attempted to play song '%s' on non-existent map '%s'\n",name, mapname); + return true; + } + + map->foreachinarea(script->playbgm_sub, m, x0, y0, x1, y1, BL_PC, name); + } else if( script_hasdata(st,3) ) { + // entire map + const char* mapname = script_getstr(st,3); + int m; + + if ( ( m = map->mapname2mapid(mapname) ) == -1 ) { + ShowWarning("playbgmall: Attempted to play song '%s' on non-existent map '%s'\n",name, mapname); + return true; + } + + map->foreachinmap(script->playbgm_sub, m, BL_PC, name); + } else { + // entire server + map->foreachpc(script->playbgm_foreachpc_sub, name); } - + return true; } @@ -11884,10 +12528,10 @@ BUILDIN(playBGMall) *------------------------------------------*/ BUILDIN(soundeffect) { - TBL_PC* sd = script_rid2sd(st); + TBL_PC* sd = script->rid2sd(st); const char* name = script_getstr(st,2); int type = script_getnum(st,3); - + if(sd) { clif->soundeffect(sd,&sd->bl,name,type); @@ -11899,9 +12543,9 @@ int soundeffect_sub(struct block_list* bl,va_list ap) { char* name = va_arg(ap,char*); int type = va_arg(ap,int); - + clif->soundeffect((TBL_PC *)bl, bl, name, type); - + return true; } @@ -11909,46 +12553,52 @@ int soundeffect_sub(struct block_list* bl,va_list ap) * Play a sound effect (.wav) on multiple clients * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>}; *------------------------------------------*/ -BUILDIN(soundeffectall) -{ +BUILDIN(soundeffectall) { struct block_list* bl; const char* name; int type; - - bl = (st->rid) ? &(script_rid2sd(st)->bl) : iMap->id2bl(st->oid); + + bl = (st->rid) ? &(script->rid2sd(st)->bl) : map->id2bl(st->oid); if (!bl) return true; - + name = script_getstr(st,2); type = script_getnum(st,3); - - //FIXME: enumerating map squares (iMap->foreach) is slower than enumerating the list of online players (iMap->foreachpc?) [ultramage] - - if(!script_hasdata(st,4)) - { // area around + + //FIXME: enumerating map squares (map->foreach) is slower than enumerating the list of online players (map->foreachpc?) [ultramage] + + if(!script_hasdata(st,4)) { // area around clif->soundeffectall(bl, name, type, AREA); - } - else - if(!script_hasdata(st,5)) - { // entire map - const char* map = script_getstr(st,4); - iMap->foreachinmap(soundeffect_sub, iMap->mapname2mapid(map), BL_PC, name, type); - } - else - if(script_hasdata(st,8)) - { // specified part of map - const char* map = script_getstr(st,4); - int x0 = script_getnum(st,5); - int y0 = script_getnum(st,6); - int x1 = script_getnum(st,7); - int y1 = script_getnum(st,8); - iMap->foreachinarea(soundeffect_sub, iMap->mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type); + } else { + if(!script_hasdata(st,5)) { // entire map + const char *mapname = script_getstr(st,4); + int m; + + if ( ( m = map->mapname2mapid(mapname) ) == -1 ) { + ShowWarning("soundeffectall: Attempted to play song '%s' (type %d) on non-existent map '%s'\n",name,type, mapname); + return true; } - else - { - ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n"); + + map->foreachinmap(script->soundeffect_sub, m, BL_PC, name, type); + } else if(script_hasdata(st,8)) { // specified part of map + const char *mapname = script_getstr(st,4); + int x0 = script_getnum(st,5); + int y0 = script_getnum(st,6); + int x1 = script_getnum(st,7); + int y1 = script_getnum(st,8); + int m; + + if ( ( m = map->mapname2mapid(mapname) ) == -1 ) { + ShowWarning("soundeffectall: Attempted to play song '%s' (type %d) on non-existent map '%s'\n",name,type, mapname); + return true; } - + + map->foreachinarea(script->soundeffect_sub, m, x0, y0, x1, y1, BL_PC, name, type); + } else { + ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n"); + } + } + return true; } /*========================================== @@ -11957,24 +12607,24 @@ BUILDIN(soundeffectall) BUILDIN(petrecovery) { struct pet_data *pd; - TBL_PC *sd=script_rid2sd(st); - + TBL_PC *sd=script->rid2sd(st); + if(sd==NULL || sd->pd==NULL) return true; - + pd=sd->pd; - + if (pd->recovery) { //Halt previous bonus if (pd->recovery->timer != INVALID_TIMER) - iTimer->delete_timer(pd->recovery->timer, pet_recovery_timer); + timer->delete(pd->recovery->timer, pet->recovery_timer); } else //Init pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery)); - + pd->recovery->type = (sc_type)script_getnum(st,2); pd->recovery->delay = script_getnum(st,3); pd->recovery->timer = INVALID_TIMER; - + return true; } @@ -11984,37 +12634,37 @@ BUILDIN(petrecovery) BUILDIN(petheal) { struct pet_data *pd; - TBL_PC *sd=script_rid2sd(st); - + TBL_PC *sd=script->rid2sd(st); + if(sd==NULL || sd->pd==NULL) return true; - + pd=sd->pd; if (pd->s_skill) { //Clear previous skill if (pd->s_skill->timer != INVALID_TIMER) { if (pd->s_skill->id) - iTimer->delete_timer(pd->s_skill->timer, pet_skill_support_timer); + timer->delete(pd->s_skill->timer, pet->skill_support_timer); else - iTimer->delete_timer(pd->s_skill->timer, pet_heal_timer); + timer->delete(pd->s_skill->timer, pet->heal_timer); } } else //init memory pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support)); - + pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport //Use the lv as the amount to heal pd->s_skill->lv=script_getnum(st,2); pd->s_skill->delay=script_getnum(st,3); pd->s_skill->hp=script_getnum(st,4); pd->s_skill->sp=script_getnum(st,5); - + //Use delay as initial offset to avoid skill/heal exploits if (battle_config.pet_equip_required && pd->pet.equip == 0) pd->s_skill->timer = INVALID_TIMER; else - pd->s_skill->timer = iTimer->add_timer(iTimer->gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0); - + pd->s_skill->timer = timer->add(timer->gettick()+pd->s_skill->delay*1000,pet->heal_timer,sd->bl.id,0); + return true; } @@ -12023,24 +12673,23 @@ BUILDIN(petheal) *------------------------------------------*/ /// petskillattack <skill id>,<level>,<rate>,<bonusrate> /// petskillattack "<skill name>",<level>,<rate>,<bonusrate> -BUILDIN(petskillattack) -{ +BUILDIN(petskillattack) { struct pet_data *pd; - TBL_PC *sd=script_rid2sd(st); - + TBL_PC *sd=script->rid2sd(st); + if(sd==NULL || sd->pd==NULL) return true; - + pd=sd->pd; if (pd->a_skill == NULL) pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack)); - - pd->a_skill->id=( script_isstring(st,2) ? skill->name2id(script_getstr(st,2)) : script_getnum(st,2) ); + + pd->a_skill->id=( script_isstringtype(st,2) ? skill->name2id(script_getstr(st,2)) : script_getnum(st,2) ); pd->a_skill->lv=script_getnum(st,3); pd->a_skill->div_ = 0; pd->a_skill->rate=script_getnum(st,4); pd->a_skill->bonusrate=script_getnum(st,5); - + return true; } @@ -12049,24 +12698,23 @@ BUILDIN(petskillattack) *------------------------------------------*/ /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate> /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate> -BUILDIN(petskillattack2) -{ +BUILDIN(petskillattack2) { struct pet_data *pd; - TBL_PC *sd=script_rid2sd(st); - + TBL_PC *sd=script->rid2sd(st); + if(sd==NULL || sd->pd==NULL) return true; - + pd=sd->pd; if (pd->a_skill == NULL) pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack)); - - pd->a_skill->id=( script_isstring(st,2) ? skill->name2id(script_getstr(st,2)) : script_getnum(st,2) ); + + pd->a_skill->id=( script_isstringtype(st,2) ? skill->name2id(script_getstr(st,2)) : script_getnum(st,2) ); pd->a_skill->lv=script_getnum(st,3); pd->a_skill->div_ = script_getnum(st,4); pd->a_skill->rate=script_getnum(st,5); pd->a_skill->bonusrate=script_getnum(st,6); - + return true; } @@ -12075,39 +12723,38 @@ BUILDIN(petskillattack2) *------------------------------------------*/ /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp> /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp> -BUILDIN(petskillsupport) -{ +BUILDIN(petskillsupport) { struct pet_data *pd; - TBL_PC *sd=script_rid2sd(st); - + TBL_PC *sd=script->rid2sd(st); + if(sd==NULL || sd->pd==NULL) return true; - + pd=sd->pd; if (pd->s_skill) { //Clear previous skill if (pd->s_skill->timer != INVALID_TIMER) { if (pd->s_skill->id) - iTimer->delete_timer(pd->s_skill->timer, pet_skill_support_timer); + timer->delete(pd->s_skill->timer, pet->skill_support_timer); else - iTimer->delete_timer(pd->s_skill->timer, pet_heal_timer); + timer->delete(pd->s_skill->timer, pet->heal_timer); } } else //init memory pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support)); - - pd->s_skill->id=( script_isstring(st,2) ? skill->name2id(script_getstr(st,2)) : script_getnum(st,2) ); + + pd->s_skill->id=( script_isstringtype(st,2) ? skill->name2id(script_getstr(st,2)) : script_getnum(st,2) ); pd->s_skill->lv=script_getnum(st,3); pd->s_skill->delay=script_getnum(st,4); pd->s_skill->hp=script_getnum(st,5); pd->s_skill->sp=script_getnum(st,6); - + //Use delay as initial offset to avoid skill/heal exploits if (battle_config.pet_equip_required && pd->pet.equip == 0) pd->s_skill->timer = INVALID_TIMER; else - pd->s_skill->timer = iTimer->add_timer(iTimer->gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0); - + pd->s_skill->timer = timer->add(timer->gettick()+pd->s_skill->delay*1000,pet->skill_support_timer,sd->bl.id,0); + return true; } @@ -12116,16 +12763,25 @@ BUILDIN(petskillsupport) *------------------------------------------*/ /// skilleffect <skill id>,<level> /// skilleffect "<skill name>",<level> -BUILDIN(skilleffect) -{ +BUILDIN(skilleffect) { TBL_PC *sd; - - uint16 skill_id=( script_isstring(st,2) ? skill->name2id(script_getstr(st,2)) : script_getnum(st,2) ); + + uint16 skill_id=( script_isstringtype(st,2) ? skill->name2id(script_getstr(st,2)) : script_getnum(st,2) ); uint16 skill_lv=script_getnum(st,3); - sd=script_rid2sd(st); - + sd=script->rid2sd(st); + + if( sd == NULL ) + return false; + + /* ensure we're standing because the following packet causes the client to virtually set the char to stand, + * which leaves the server thinking it still is sitting. */ + if( pc_issit(sd) ) { + pc->setstand(sd); + skill->sit(sd,0); + clif->standing(&sd->bl); + } clif->skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1); - + return true; } @@ -12134,65 +12790,64 @@ BUILDIN(skilleffect) *------------------------------------------*/ /// npcskilleffect <skill id>,<level>,<x>,<y> /// npcskilleffect "<skill name>",<level>,<x>,<y> -BUILDIN(npcskilleffect) -{ - struct block_list *bl= iMap->id2bl(st->oid); - - uint16 skill_id=( script_isstring(st,2) ? skill->name2id(script_getstr(st,2)) : script_getnum(st,2) ); +BUILDIN(npcskilleffect) { + struct block_list *bl= map->id2bl(st->oid); + + uint16 skill_id=( script_isstringtype(st,2) ? skill->name2id(script_getstr(st,2)) : script_getnum(st,2) ); uint16 skill_lv=script_getnum(st,3); int x=script_getnum(st,4); int y=script_getnum(st,5); - + if (bl) - clif->skill_poseffect(bl,skill_id,skill_lv,x,y,iTimer->gettick()); - + clif->skill_poseffect(bl,skill_id,skill_lv,x,y,timer->gettick()); + return true; } /*========================================== * Special effects [Valaris] *------------------------------------------*/ -BUILDIN(specialeffect) -{ - struct block_list *bl=iMap->id2bl(st->oid); +BUILDIN(specialeffect) { + struct block_list *bl=map->id2bl(st->oid); int type = script_getnum(st,2); enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA; - + if(bl==NULL) return true; - + if( script_hasdata(st,4) ) { - TBL_NPC *nd = npc_name2id(script_getstr(st,4)); + TBL_NPC *nd = npc->name2id(script_getstr(st,4)); if(nd) clif->specialeffect(&nd->bl, type, target); } else { if (target == SELF) { - TBL_PC *sd=script_rid2sd(st); + TBL_PC *sd=script->rid2sd(st); if (sd) clif->specialeffect_single(bl,type,sd->fd); } else { clif->specialeffect(bl, type, target); } } - + return true; } -BUILDIN(specialeffect2) -{ - TBL_PC *sd=script_rid2sd(st); +BUILDIN(specialeffect2) { + TBL_PC *sd; int type = script_getnum(st,2); enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA; - + if( script_hasdata(st,4) ) - sd = iMap->nick2sd(script_getstr(st,4)); - + sd = map->nick2sd(script_getstr(st,4)); + else + sd = script->rid2sd(st); + if (sd) clif->specialeffect(&sd->bl, type, target); - + return true; } @@ -12201,12 +12856,12 @@ BUILDIN(specialeffect2) *------------------------------------------*/ BUILDIN(nude) { - TBL_PC *sd = script_rid2sd(st); + TBL_PC *sd = script->rid2sd(st); int i, calcflag = 0; - + if( sd == NULL ) return true; - + for( i = 0 ; i < EQI_MAX; i++ ) { if( sd->equip_index[ i ] >= 0 ) { if( !calcflag ) @@ -12214,49 +12869,48 @@ BUILDIN(nude) pc->unequipitem( sd , sd->equip_index[ i ] , 2); } } - + if( calcflag ) - status_calc_pc(sd,0); - + status_calc_pc(sd,SCO_NONE); + return true; } /*========================================== * gmcommand [MouseJstr] *------------------------------------------*/ -BUILDIN(atcommand) -{ - TBL_PC dummy_sd; - TBL_PC* sd; +BUILDIN(atcommand) { + TBL_PC *sd, *dummy_sd = NULL; int fd; const char* cmd; - + bool ret = true; + cmd = script_getstr(st,2); - + if (st->rid) { - sd = script_rid2sd(st); + sd = script->rid2sd(st); + if( sd == NULL ) + return false; fd = sd->fd; } else { //Use a dummy character. - sd = &dummy_sd; + sd = dummy_sd = pc->get_dummy_sd(); fd = 0; - - memset(&dummy_sd, 0, sizeof(TBL_PC)); - if (st->oid) - { - struct block_list* bl = iMap->id2bl(st->oid); - memcpy(&dummy_sd.bl, bl, sizeof(struct block_list)); + + if (st->oid) { + struct block_list* bl = map->id2bl(st->oid); + memcpy(&sd->bl, bl, sizeof(struct block_list)); if (bl->type == BL_NPC) - safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH); + safestrncpy(sd->status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH); } } - - if (!atcommand->parse(fd, sd, cmd, 0)) { + + if (!atcommand->exec(fd, sd, cmd, false)) { ShowWarning("script: buildin_atcommand: failed to execute command '%s'\n", cmd); - script_reportsrc(st); - return false; + script->reportsrc(st); + ret = false; } - - return true; + if (dummy_sd) aFree(dummy_sd); + return ret; } /*========================================== @@ -12264,11 +12918,11 @@ BUILDIN(atcommand) *------------------------------------------*/ BUILDIN(dispbottom) { - TBL_PC *sd=script_rid2sd(st); + TBL_PC *sd=script->rid2sd(st); const char *message; message=script_getstr(st,2); if(sd) - clif->disp_onlyself(sd,message,(int)strlen(message)); + clif_disp_onlyself(sd,message,(int)strlen(message)); return true; } @@ -12280,15 +12934,15 @@ BUILDIN(recovery) { TBL_PC* sd; struct s_mapiterator* iter; - + iter = mapit_getallusers(); for( sd = (TBL_PC*)mapit->first(iter); mapit->exists(iter); sd = (TBL_PC*)mapit->next(iter) ) { if(pc_isdead(sd)) - status_revive(&sd->bl, 100, 100); + status->revive(&sd->bl, 100, 100); else status_percent_heal(&sd->bl, 100, 100); - clif->message(sd->fd,msg_txt(680)); + clif->message(sd->fd,msg_txt(880)); // "You have been recovered!" } mapit->free(iter); return true; @@ -12300,10 +12954,10 @@ BUILDIN(recovery) *------------------------------------------*/ BUILDIN(getpetinfo) { - TBL_PC *sd=script_rid2sd(st); + TBL_PC *sd=script->rid2sd(st); TBL_PET *pd; int type=script_getnum(st,2); - + if(!sd || !sd->pd) { if (type == 2) script_pushconststr(st,"null"); @@ -12312,7 +12966,7 @@ BUILDIN(getpetinfo) return true; } pd = sd->pd; - switch(type){ + switch(type) { case 0: script_pushint(st,pd->pet.pet_id); break; case 1: script_pushint(st,pd->pet.class_); break; case 2: script_pushstrcopy(st,pd->pet.name); break; @@ -12334,28 +12988,25 @@ BUILDIN(getpetinfo) *------------------------------------------*/ BUILDIN(gethominfo) { - TBL_PC *sd=script_rid2sd(st); - TBL_HOM *hd; - int type=script_getnum(st,2); - - hd = sd?sd->hd:NULL; - if(!homun_alive(hd)) - { + TBL_PC *sd=script->rid2sd(st); + int type = script_getnum(st,2); + + if(!sd || !sd->hd) { if (type == 2) script_pushconststr(st,"null"); else script_pushint(st,0); return true; } - - switch(type){ - case 0: script_pushint(st,hd->homunculus.hom_id); break; - case 1: script_pushint(st,hd->homunculus.class_); break; - case 2: script_pushstrcopy(st,hd->homunculus.name); break; - case 3: script_pushint(st,hd->homunculus.intimacy); break; - case 4: script_pushint(st,hd->homunculus.hunger); break; - case 5: script_pushint(st,hd->homunculus.rename_flag); break; - case 6: script_pushint(st,hd->homunculus.level); break; + + switch(type) { + case 0: script_pushint(st,sd->hd->homunculus.hom_id); break; + case 1: script_pushint(st,sd->hd->homunculus.class_); break; + case 2: script_pushstrcopy(st,sd->hd->homunculus.name); break; + case 3: script_pushint(st,sd->hd->homunculus.intimacy); break; + case 4: script_pushint(st,sd->hd->homunculus.hunger); break; + case 5: script_pushint(st,sd->hd->homunculus.rename_flag); break; + case 6: script_pushint(st,sd->hd->homunculus.level); break; default: script_pushint(st,0); break; @@ -12365,36 +13016,28 @@ BUILDIN(gethominfo) /// Retrieves information about character's mercenary /// getmercinfo <type>[,<char id>]; -BUILDIN(getmercinfo) -{ +BUILDIN(getmercinfo) { int type, char_id; struct map_session_data* sd; struct mercenary_data* md; - + type = script_getnum(st,2); - - if( script_hasdata(st,3) ) - { + + if( script_hasdata(st,3) ) { char_id = script_getnum(st,3); - - if( ( sd = iMap->charid2sd(char_id) ) == NULL ) - { + + if( ( sd = map->charid2sd(char_id) ) == NULL ) { ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id); script_pushnil(st); return false; } - } - else - { - if( ( sd = script_rid2sd(st) ) == NULL ) - { - script_pushnil(st); + } else { + if( ( sd = script->rid2sd(st) ) == NULL ) return true; - } } - + md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL; - + switch( type ) { case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break; @@ -12405,46 +13048,48 @@ BUILDIN(getmercinfo) else script_pushconststr(st,""); break; - case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break; - case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break; + case 3: script_pushint(st,md ? mercenary->get_faith(md) : 0); break; + case 4: script_pushint(st,md ? mercenary->get_calls(md) : 0); break; case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break; - case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break; + case 6: script_pushint(st,md ? mercenary->get_lifetime(md) : 0); break; case 7: script_pushint(st,md ? md->db->lv : 0); break; default: ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id); script_pushnil(st); return false; } - + return true; } /*========================================== * Shows wether your inventory(and equips) contain - selected card or not. - checkequipedcard(4001); + * selected card or not. + * checkequipedcard(4001); *------------------------------------------*/ BUILDIN(checkequipedcard) { - TBL_PC *sd=script_rid2sd(st); - - if(sd){ - int n,i,c=0; - c=script_getnum(st,2); - - for(i=0;i<MAX_INVENTORY;i++){ - if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){ - if (itemdb_isspecial(sd->status.inventory[i].card[0])) - continue; - for(n=0;n<sd->inventory_data[i]->slot;n++){ - if(sd->status.inventory[i].card[n]==c){ - script_pushint(st,1); - return true; - } + int n,i,c=0; + TBL_PC *sd=script->rid2sd(st); + + if( sd == NULL ) + return false; + + c = script_getnum(st,2); + + for( i=0; i<MAX_INVENTORY; i++) { + if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]) { + if (itemdb_isspecial(sd->status.inventory[i].card[0])) + continue; + for(n=0;n<sd->inventory_data[i]->slot;n++) { + if(sd->status.inventory[i].card[n]==c) { + script_pushint(st,1); + return true; } } } } + script_pushint(st,0); return true; } @@ -12455,12 +13100,12 @@ BUILDIN(jump_zero) sel=script_getnum(st,2); if(!sel) { int pos; - if( !data_islabel(script_getdata(st,3)) ){ + if( !data_islabel(script_getdata(st,3)) ) { ShowError("script: jump_zero: not label !\n"); st->state=END; return false; } - + pos=script_getnum(st,3); st->pos=pos; st->state=GOTO; @@ -12473,51 +13118,49 @@ BUILDIN(jump_zero) *------------------------------------------*/ BUILDIN(movenpc) { TBL_NPC *nd = NULL; - const char *npc; + const char *npc_name; int x,y; - - npc = script_getstr(st,2); + + npc_name = script_getstr(st,2); x = script_getnum(st,3); y = script_getnum(st,4); - - if ((nd = npc_name2id(npc)) == NULL) + + if ((nd = npc->name2id(npc_name)) == NULL) return -1; - + if (script_hasdata(st,5)) nd->dir = script_getnum(st,5) % 8; - npc_movenpc(nd, x, y); + npc->movenpc(nd, x, y); return true; } /*========================================== * message [MouseJstr] *------------------------------------------*/ -BUILDIN(message) -{ +BUILDIN(message) { const char *msg,*player; TBL_PC *pl_sd = NULL; - + player = script_getstr(st,2); msg = script_getstr(st,3); - - if((pl_sd=iMap->nick2sd((char *) player)) == NULL) + + if((pl_sd=map->nick2sd((char *) player)) == NULL) return true; clif->message(pl_sd->fd, msg); - + return true; } /*========================================== * npctalk (sends message to surrounding area) *------------------------------------------*/ -BUILDIN(npctalk) -{ +BUILDIN(npctalk) { const char* str; char name[NAME_LENGTH], message[256]; - - struct npc_data* nd = (struct npc_data *)iMap->id2bl(st->oid); + + struct npc_data* nd = (struct npc_data *)map->id2bl(st->oid); str = script_getstr(st,2); - + if(nd) { safestrncpy(name, nd->name, sizeof(name)); @@ -12525,65 +13168,55 @@ BUILDIN(npctalk) safesnprintf(message, sizeof(message), "%s : %s", name, str); clif->disp_overhead(&nd->bl, message); } - + return true; } // change npc walkspeed [Valaris] -BUILDIN(npcspeed) -{ +BUILDIN(npcspeed) { struct npc_data* nd; int speed; - + speed = script_getnum(st,2); - nd =(struct npc_data *)iMap->id2bl(st->oid); - + nd = (struct npc_data *)map->id2bl(st->oid); + if( nd ) { - if( nd->ud == &npc_base_ud ) { - nd->ud = NULL; - CREATE(nd->ud, struct unit_data, 1); - unit_dataset(&nd->bl); - } + unit->bl2ud2(&nd->bl); // ensure nd->ud is safe to edit nd->speed = speed; nd->ud->state.speed_changed = 1; } - + return true; } // make an npc walk to a position [Valaris] BUILDIN(npcwalkto) { - struct npc_data *nd=(struct npc_data *)iMap->id2bl(st->oid); + struct npc_data *nd=(struct npc_data *)map->id2bl(st->oid); int x=0,y=0; - + x=script_getnum(st,2); y=script_getnum(st,3); - - if(nd) { - if( nd->ud == &npc_base_ud ) { - nd->ud = NULL; - CREATE(nd->ud, struct unit_data, 1); - unit_dataset(&nd->bl); - } - + + if( nd ) { + unit->bl2ud2(&nd->bl); // ensure nd->ud is safe to edit if (!nd->status.hp) { - status_calc_npc(nd, true); + status_calc_npc(nd, SCO_FIRST); } else { - status_calc_npc(nd, false); + status_calc_npc(nd, SCO_NONE); } - unit_walktoxy(&nd->bl,x,y,0); + unit->walktoxy(&nd->bl,x,y,0); } - + return true; } // stop an npc's movement [Valaris] -BUILDIN(npcstop) -{ - struct npc_data *nd=(struct npc_data *)iMap->id2bl(st->oid); - - if(nd) { - unit_stop_walking(&nd->bl,1|4); +BUILDIN(npcstop) { + struct npc_data *nd = (struct npc_data *)map->id2bl(st->oid); + + if( nd ) { + unit->bl2ud2(&nd->bl); // ensure nd->ud is safe to edit + unit->stop_walking(&nd->bl,1|4); } - + return true; } @@ -12595,23 +13228,25 @@ BUILDIN(getlook) { int type,val; TBL_PC *sd; - sd=script_rid2sd(st); - + sd=script->rid2sd(st); + if( sd == NULL ) + return false; + type=script_getnum(st,2); - val=-1; + val = -1; switch(type) { - case LOOK_HAIR: val=sd->status.hair; break; //1 - case LOOK_WEAPON: val=sd->status.weapon; break; //2 - case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3 - case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4 - case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5 - case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6 - case LOOK_CLOTHES_COLOR:val=sd->status.clothes_color; break; //7 - case LOOK_SHIELD: val=sd->status.shield; break; //8 - case LOOK_SHOES: break; //9 - case LOOK_ROBE: val=sd->status.robe; break; //12 + case LOOK_HAIR: val = sd->status.hair; break; //1 + case LOOK_WEAPON: val = sd->status.weapon; break; //2 + case LOOK_HEAD_BOTTOM: val = sd->status.head_bottom; break; //3 + case LOOK_HEAD_TOP: val = sd->status.head_top; break; //4 + case LOOK_HEAD_MID: val = sd->status.head_mid; break; //5 + case LOOK_HAIR_COLOR: val = sd->status.hair_color; break; //6 + case LOOK_CLOTHES_COLOR: val = sd->status.clothes_color; break; //7 + case LOOK_SHIELD: val = sd->status.shield; break; //8 + case LOOK_SHOES: break; //9 + case LOOK_ROBE: val = sd->status.robe; break; //12 } - + script_pushint(st,val); return true; } @@ -12623,15 +13258,13 @@ BUILDIN(getsavepoint) { TBL_PC* sd; int type; - - sd = script_rid2sd(st); - if (sd == NULL) { - script_pushint(st,0); - return true; - } - + + sd = script->rid2sd(st); + if( sd == NULL ) + return false; + type = script_getnum(st,2); - + switch(type) { case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break; case 1: script_pushint(st,sd->status.save_point.x); break; @@ -12669,88 +13302,106 @@ BUILDIN(getmapxy) { struct block_list *bl = NULL; TBL_PC *sd=NULL; - - int num; + + int64 num; const char *name; char prefix; - + int x,y,type; char mapname[MAP_NAME_LENGTH]; - - if( !data_isreference(script_getdata(st,2)) ){ + + if( !data_isreference(script_getdata(st,2)) ) { ShowWarning("script: buildin_getmapxy: not mapname variable\n"); script_pushint(st,-1); return false; } - if( !data_isreference(script_getdata(st,3)) ){ + if( !data_isreference(script_getdata(st,3)) ) { ShowWarning("script: buildin_getmapxy: not mapx variable\n"); script_pushint(st,-1); return false; } - if( !data_isreference(script_getdata(st,4)) ){ + if( !data_isreference(script_getdata(st,4)) ) { ShowWarning("script: buildin_getmapxy: not mapy variable\n"); script_pushint(st,-1); return false; } + if( !is_string_variable(reference_getname(script_getdata(st, 2))) ) { + ShowWarning("script: buildin_getmapxy: %s is not a string variable\n",reference_getname(script_getdata(st, 2))); + script_pushint(st,-1); + return false; + } + + if( is_string_variable(reference_getname(script_getdata(st, 3))) ) { + ShowWarning("script: buildin_getmapxy: %s is a string variable, should be int\n",reference_getname(script_getdata(st, 3))); + script_pushint(st,-1); + return false; + } + + if( is_string_variable(reference_getname(script_getdata(st, 4))) ) { + ShowWarning("script: buildin_getmapxy: %s is a string variable, should be int\n",reference_getname(script_getdata(st, 4))); + script_pushint(st,-1); + return false; + } + // Possible needly check function parameters on C_STR,C_INT,C_INT type=script_getnum(st,5); - - switch (type){ - case 0: //Get Character Position + + switch (type) { + case 0: //Get Character Position if( script_hasdata(st,6) ) - sd=iMap->nick2sd(script_getstr(st,6)); + sd=map->nick2sd(script_getstr(st,6)); else - sd=script_rid2sd(st); - + sd=script->rid2sd(st); + if (sd) bl = &sd->bl; break; - case 1: //Get NPC Position + case 1: //Get NPC Position if( script_hasdata(st,6) ) { struct npc_data *nd; - nd=npc_name2id(script_getstr(st,6)); + nd=npc->name2id(script_getstr(st,6)); if (nd) bl = &nd->bl; } else //In case the origin is not an npc? - bl=iMap->id2bl(st->oid); + bl=map->id2bl(st->oid); break; - case 2: //Get Pet Position + case 2: //Get Pet Position if(script_hasdata(st,6)) - sd=iMap->nick2sd(script_getstr(st,6)); + sd=map->nick2sd(script_getstr(st,6)); else - sd=script_rid2sd(st); - + sd=script->rid2sd(st); + if (sd && sd->pd) bl = &sd->pd->bl; break; - case 3: //Get Mob Position + case 3: //Get Mob Position break; //Not supported? - case 4: //Get Homun Position + case 4: //Get Homun Position if(script_hasdata(st,6)) - sd=iMap->nick2sd(script_getstr(st,6)); + sd=map->nick2sd(script_getstr(st,6)); else - sd=script_rid2sd(st); - + sd=script->rid2sd(st); + if (sd && sd->hd) bl = &sd->hd->bl; break; case 5: //Get Mercenary Position if(script_hasdata(st,6)) - sd=iMap->nick2sd(script_getstr(st,6)); + sd=map->nick2sd(script_getstr(st,6)); else - sd=script_rid2sd(st); - + sd=script->rid2sd(st); + if (sd && sd->md) bl = &sd->md->bl; break; case 6: //Get Elemental Position if(script_hasdata(st,6)) - sd=iMap->nick2sd(script_getstr(st,6)); + sd=map->nick2sd(script_getstr(st,6)); else - sd=script_rid2sd(st); - + sd=script->rid2sd(st); + if (sd && sd->ed) bl = &sd->ed->bl; break; @@ -12763,44 +13414,44 @@ BUILDIN(getmapxy) script_pushint(st,-1); return true; } - + x= bl->x; y= bl->y; - safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH); - + safestrncpy(mapname, map->list[bl->m].name, MAP_NAME_LENGTH); + //Set MapName$ num=st->stack->stack_data[st->start+2].u.num; - name=get_str(num&0x00ffffff); + name=script->get_str(script_getvarid(num)); prefix=*name; if(not_server_variable(prefix)) - sd=script_rid2sd(st); + sd=script->rid2sd(st); else sd=NULL; - set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2)); - + script->set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2)); + //Set MapX num=st->stack->stack_data[st->start+3].u.num; - name=get_str(num&0x00ffffff); + name=script->get_str(script_getvarid(num)); prefix=*name; - + if(not_server_variable(prefix)) - sd=script_rid2sd(st); + sd=script->rid2sd(st); else sd=NULL; - set_reg(st,sd,num,name,(void*)__64BPTRSIZE(x),script_getref(st,3)); - + script->set_reg(st,sd,num,name,(void*)h64BPTRSIZE(x),script_getref(st,3)); + //Set MapY num=st->stack->stack_data[st->start+4].u.num; - name=get_str(num&0x00ffffff); + name=script->get_str(script_getvarid(num)); prefix=*name; - + if(not_server_variable(prefix)) - sd=script_rid2sd(st); + sd=script->rid2sd(st); else sd=NULL; - set_reg(st,sd,num,name,(void*)__64BPTRSIZE(y),script_getref(st,4)); - + script->set_reg(st,sd,num,name,(void*)h64BPTRSIZE(y),script_getref(st,4)); + //Return Success value script_pushint(st,0); return true; @@ -12813,11 +13464,11 @@ BUILDIN(logmes) { const char *str; TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return false; - + str = script_getstr(st,2); logs->npc(sd,str); return true; @@ -12825,36 +13476,36 @@ BUILDIN(logmes) BUILDIN(summon) { - int _class, timeout=0; + int class_, timeout=0; const char *str,*event=""; TBL_PC *sd; struct mob_data *md; - int tick = iTimer->gettick(); - - sd=script_rid2sd(st); + int64 tick = timer->gettick(); + + sd=script->rid2sd(st); if (!sd) return true; - - str =script_getstr(st,2); - _class=script_getnum(st,3); + + str = script_getstr(st,2); + class_ = script_getnum(st,3); if( script_hasdata(st,4) ) timeout=script_getnum(st,4); - if( script_hasdata(st,5) ){ + if( script_hasdata(st,5) ) { event=script_getstr(st,5); - check_event(st, event); + script->check_event(st, event); } - + clif->skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick); - - md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE); + + md = mob->once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, class_, event, SZ_MEDIUM, AI_NONE); if (md) { md->master_id=sd->bl.id; md->special_state.ai = AI_ATTACK; if( md->deletetimer != INVALID_TIMER ) - iTimer->delete_timer(md->deletetimer, mob_timer_delete); - md->deletetimer = iTimer->add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0); - mob_spawn (md); //Now it is ready for spawning. + timer->delete(md->deletetimer, mob->timer_delete); + md->deletetimer = timer->add(tick+(timeout>0?timeout*1000:60000),mob->timer_delete,md->bl.id,0); + mob->spawn (md); //Now it is ready for spawning. clif->specialeffect(&md->bl,344,AREA); - sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000); + sc_start4(NULL, &md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000); } return true; } @@ -12862,15 +13513,13 @@ BUILDIN(summon) /*========================================== * Checks whether it is daytime/nighttime *------------------------------------------*/ -BUILDIN(isnight) -{ - script_pushint(st,(iMap->night_flag == 1)); +BUILDIN(isnight) { + script_pushint(st,(map->night_flag == 1)); return true; } -BUILDIN(isday) -{ - script_pushint(st,(iMap->night_flag == 0)); +BUILDIN(isday) { + script_pushint(st,(map->night_flag == 0)); return true; } @@ -12883,18 +13532,16 @@ BUILDIN(isequippedcnt) TBL_PC *sd; int i, j, k, id = 1; int ret = 0; - - sd = script_rid2sd(st); - if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing... - script_pushint(st,0); - return true; - } - + + sd = script->rid2sd(st); + if( sd == NULL ) + return false; + for (i=0; id!=0; i++) { - FETCH (i+2, id) else id = 0; + script_fetch(st,i+2, id); if (id <= 0) continue; - + for (j=0; j<EQI_MAX; j++) { int index; index = sd->equip_index[j]; @@ -12902,10 +13549,10 @@ BUILDIN(isequippedcnt) if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue; if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue; if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue; - + if(!sd->inventory_data[index]) continue; - + if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory. if (sd->inventory_data[index]->nameid == id) ret+= sd->status.inventory[index].amount; @@ -12919,7 +13566,7 @@ BUILDIN(isequippedcnt) } } } - + script_pushint(st,ret); return true; } @@ -12938,18 +13585,16 @@ BUILDIN(isequipped) int ret = -1; //Original hash to reverse it when full check fails. unsigned int setitem_hash = 0, setitem_hash2 = 0; - - sd = script_rid2sd(st); - - if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing... - script_pushint(st,0); - return true; - } - + + sd = script->rid2sd(st); + + if( sd == NULL ) + return false; + setitem_hash = sd->bonus.setitem_hash; setitem_hash2 = sd->bonus.setitem_hash2; for (i=0; id!=0; i++) { - FETCH (i+2, id) else id = 0; + script_fetch(st,i+2, id); if (id <= 0) continue; flag = 0; @@ -12959,10 +13604,10 @@ BUILDIN(isequipped) if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue; if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue; if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue; - + if(!sd->inventory_data[index]) continue; - + if (itemdb_type(id) != IT_CARD) { if (sd->inventory_data[index]->nameid != id) continue; @@ -12972,18 +13617,18 @@ BUILDIN(isequipped) if (sd->inventory_data[index]->slot == 0 || itemdb_isspecial(sd->status.inventory[index].card[0])) continue; - - for (k = 0; k < sd->inventory_data[index]->slot; k++) - { //New hash system which should support up to 4 slots on any equipment. [Skotlex] + + for (k = 0; k < sd->inventory_data[index]->slot; k++) { + //New hash system which should support up to 4 slots on any equipment. [Skotlex] unsigned int hash = 0; if (sd->status.inventory[index].card[k] != id) continue; - + hash = 1<<((j<5?j:j-5)*4 + k); // check if card is already used by another set if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash) continue; - + // We have found a match flag = 1; // Set hash so this card cannot be used by another @@ -13014,26 +13659,28 @@ BUILDIN(isequipped) * Check how many given inserted cards in the CURRENT * weapon - used for 2/15's cards patch [Lupus] *------------------------------------------------*/ -BUILDIN(cardscnt) -{ +BUILDIN(cardscnt) { TBL_PC *sd; int i, k, id = 1; int ret = 0; int index; - - sd = script_rid2sd(st); - + + sd = script->rid2sd(st); + + if( sd == NULL ) + return false; + for (i=0; id!=0; i++) { - FETCH (i+2, id) else id = 0; + script_fetch(st,i+2, id); if (id <= 0) continue; - - index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus] + + index = status->current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus] if(index < 0) continue; - + if(!sd->inventory_data[index]) continue; - + if(itemdb_type(id) != IT_CARD) { if (sd->inventory_data[index]->nameid == id) ret+= sd->status.inventory[index].amount; @@ -13047,7 +13694,7 @@ BUILDIN(cardscnt) } } script_pushint(st,ret); - // script_pushint(st,current_equip_item_index); + // script_pushint(st,status->current_equip_item_index); return true; } @@ -13055,27 +13702,26 @@ BUILDIN(cardscnt) * Returns the refined number of the current item, or an * item with inventory index specified *-------------------------------------------------------*/ -BUILDIN(getrefine) -{ +BUILDIN(getrefine) { TBL_PC *sd; - if ((sd = script_rid2sd(st))!= NULL) - script_pushint(st,sd->status.inventory[current_equip_item_index].refine); - else - script_pushint(st,0); + + sd = script->rid2sd(st); + if( sd == NULL ) + return false; + + script_pushint(st,sd->status.inventory[status->current_equip_item_index].refine); return true; } /*======================================================= * Day/Night controls *-------------------------------------------------------*/ -BUILDIN(night) -{ - if (iMap->night_flag != 1) pc->map_night_timer(pc->night_timer_tid, 0, 0, 1); +BUILDIN(night) { + if (map->night_flag != 1) pc->map_night_timer(pc->night_timer_tid, 0, 0, 1); return true; } -BUILDIN(day) -{ - if (iMap->night_flag != 0) pc->map_day_timer(pc->day_timer_tid, 0, 0, 1); +BUILDIN(day) { + if (map->night_flag != 0) pc->map_day_timer(pc->day_timer_tid, 0, 0, 1); return true; } @@ -13087,12 +13733,12 @@ BUILDIN(unequip) int i; size_t num; TBL_PC *sd; - + num = script_getnum(st,2); - sd = script_rid2sd(st); - if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) ) + sd = script->rid2sd(st); + if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(script->equip) ) { - i = pc->checkequip(sd,equip[num-1]); + i = pc->checkequip(sd,script->equip[num-1]); if (i >= 0) pc->unequipitem(sd,i,1|2); } @@ -13104,19 +13750,21 @@ BUILDIN(equip) int nameid=0,i; TBL_PC *sd; struct item_data *item_data; - - sd = script_rid2sd(st); - + + sd = script->rid2sd(st); + if( sd == NULL ) + return false; + nameid=script_getnum(st,2); - if((item_data = itemdb_exists(nameid)) == NULL) + if((item_data = itemdb->exists(nameid)) == NULL) { ShowError("wrong item ID : equipitem(%i)\n",nameid); return false; } - ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid ); + ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid && sd->status.inventory[i].equip == 0 ); if( i < MAX_INVENTORY ) pc->equipitem(sd,i,item_data->equip); - + return true; } @@ -13126,19 +13774,19 @@ BUILDIN(autoequip) struct item_data *item_data; nameid=script_getnum(st,2); flag=script_getnum(st,3); - - if( ( item_data = itemdb_exists(nameid) ) == NULL ) + + if( ( item_data = itemdb->exists(nameid) ) == NULL ) { ShowError("buildin_autoequip: Invalid item '%d'.\n", nameid); return false; } - - if( !itemdb_isequip2(item_data) ) + + if( !itemdb->isequip2(item_data) ) { ShowError("buildin_autoequip: Item '%d' cannot be equipped.\n", nameid); return false; } - + item_data->flag.autoequip = flag>0?1:0; return true; } @@ -13146,15 +13794,15 @@ BUILDIN(autoequip) BUILDIN(setbattleflag) { const char *flag, *value; - + flag = script_getstr(st,2); value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value - + if (battle->config_set_value(flag, value) == 0) ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag); else ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value); - + return true; } @@ -13171,10 +13819,10 @@ BUILDIN(getbattleflag) //------------------------------------------------------- BUILDIN(getstrlen) { - + const char *str = script_getstr(st,2); int len = (str) ? (int)strlen(str) : 0; - + script_pushint(st,len); return true; } @@ -13186,9 +13834,9 @@ BUILDIN(charisalpha) { const char *str=script_getstr(st,2); int pos=script_getnum(st,3); - + int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0; - + script_pushint(st,val); return true; } @@ -13200,9 +13848,9 @@ BUILDIN(charisupper) { const char *str = script_getstr(st,2); int pos = script_getnum(st,3); - + int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0; - + script_pushint(st,val); return true; } @@ -13214,9 +13862,9 @@ BUILDIN(charislower) { const char *str = script_getstr(st,2); int pos = script_getnum(st,3); - + int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0; - + script_pushint(st,val); return true; } @@ -13227,7 +13875,7 @@ BUILDIN(charislower) BUILDIN(charat) { const char *str = script_getstr(st,2); int pos = script_getnum(st,3); - + if( pos >= 0 && (unsigned int)pos < strlen(str) ) { char output[2]; output[0] = str[pos]; @@ -13247,10 +13895,10 @@ BUILDIN(setchar) const char *c = script_getstr(st,3); int index = script_getnum(st,4); char *output = aStrdup(str); - + if(index >= 0 && index < strlen(output)) output[index] = *c; - + script_pushstr(st, output); return true; } @@ -13265,19 +13913,19 @@ BUILDIN(insertchar) int index = script_getnum(st,4); char *output; size_t len = strlen(str); - + if(index < 0) index = 0; else if(index > len) - index = len; - + index = (int)len; + output = (char*)aMalloc(len + 2); - + memcpy(output, str, index); output[index] = c[0]; memcpy(&output[index+1], &str[index], len - index); output[len+1] = '\0'; - + script_pushstr(st, output); return true; } @@ -13291,19 +13939,19 @@ BUILDIN(delchar) int index = script_getnum(st,3); char *output; size_t len = strlen(str); - + if(index < 0 || index > len) { //return original output = aStrdup(str); script_pushstr(st, output); return true; } - + output = (char*)aMalloc(len); - + memcpy(output, str, index); memcpy(&output[index], &str[index+1], len - index); - + script_pushstr(st, output); return true; } @@ -13316,12 +13964,12 @@ BUILDIN(strtoupper) const char *str = script_getstr(st,2); char *output = aStrdup(str); char *cursor = output; - + while (*cursor != '\0') { *cursor = TOUPPER(*cursor); cursor++; } - + script_pushstr(st, output); return true; } @@ -13334,12 +13982,12 @@ BUILDIN(strtolower) const char *str = script_getstr(st,2); char *output = aStrdup(str); char *cursor = output; - + while (*cursor != '\0') { *cursor = TOLOWER(*cursor); cursor++; } - + script_pushstr(st, output); return true; } @@ -13353,18 +14001,18 @@ BUILDIN(substr) char *output; int start = script_getnum(st,3); int end = script_getnum(st,4); - + int len = 0; - + if(start >= 0 && end < strlen(str) && start <= end) { len = end - start + 1; output = (char*)aMalloc(len + 1); memcpy(output, &str[start], len); } else output = (char*)aMalloc(1); - + output[len] = '\0'; - + script_pushstr(st, output); return true; } @@ -13382,54 +14030,46 @@ BUILDIN(explode) size_t len = strlen(str); int i = 0, j = 0; int start; - - + + char *temp; const char* name; - + TBL_PC* sd = NULL; - + temp = (char*)aMalloc(len + 1); - + if( !data_isreference(data) ) { ShowError("script:explode: not a variable\n"); - script_reportdata(data); + script->reportdata(data); st->state = END; return false;// not a variable } - + id = reference_getid(data); start = reference_getindex(data); name = reference_getname(data); - - if( not_array_variable(*name) ) - { - ShowError("script:explode: illegal scope\n"); - script_reportdata(data); - st->state = END; - return false;// not supported - } - + if( !is_string_variable(name) ) { ShowError("script:explode: not string array\n"); - script_reportdata(data); + script->reportdata(data); st->state = END; return false;// data type mismatch } - + if( not_server_variable(*name) ) { - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached } - + while(str[i] != '\0') { if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index temp[j] = '\0'; - set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data)); + script->set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data)); j = 0; ++i; } else { @@ -13438,8 +14078,8 @@ BUILDIN(explode) } //set last string temp[j] = '\0'; - set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data)); - + script->set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data)); + aFree(temp); return true; } @@ -13452,63 +14092,55 @@ BUILDIN(implode) { struct script_data* data = script_getdata(st, 2); const char *glue = NULL, *name, *temp; - int32 glue_len = 0, array_size, id; - size_t len = 0; - int i, k = 0; - + uint32 array_size, id; + size_t len = 0, glue_len = 0, k = 0; + int i; + TBL_PC* sd = NULL; - + char *output; - + if( !data_isreference(data) ) { ShowError("script:implode: not a variable\n"); - script_reportdata(data); + script->reportdata(data); st->state = END; return false;// not a variable } - + id = reference_getid(data); name = reference_getname(data); - - if( not_array_variable(*name) ) - { - ShowError("script:implode: illegal scope\n"); - script_reportdata(data); - st->state = END; - return false;// not supported - } - + if( !is_string_variable(name) ) { ShowError("script:implode: not string array\n"); - script_reportdata(data); + script->reportdata(data); st->state = END; return false;// data type mismatch } - + if( not_server_variable(*name) ) { - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached } - + //count chars - array_size = getarraysize(st, id, reference_getindex(data), is_string_variable(name), reference_getref(data)) - 1; - - if(array_size == -1) //empty array check (AmsTaff) - { - ShowWarning("script:implode: array length = 0\n"); - output = (char*)aMalloc(sizeof(char)*5); - sprintf(output,"%s","NULL"); + array_size = script->array_highest_key(st,sd,name,reference_getref(data)) - 1; + + if(array_size == -1) { + //empty array check (AmsTaff) + ShowWarning("script:implode: array length = 0\n"); + output = (char*)aMalloc(sizeof(char)*5); + sprintf(output,"%s","NULL"); } else { for(i = 0; i <= array_size; ++i) { temp = (char*) script->get_val2(st, reference_uid(id, i), reference_getref(data)); len += strlen(temp); script_removetop(st, -1, 0); } - + //allocate mem if( script_hasdata(st,3) ) { glue = script_getstr(st,3); @@ -13516,7 +14148,7 @@ BUILDIN(implode) len += glue_len * (array_size); } output = (char*)aMalloc(len + 1); - + //build output for(i = 0; i < array_size; ++i) { temp = (char*) script->get_val2(st, reference_uid(id, i), reference_getref(data)); @@ -13534,10 +14166,10 @@ BUILDIN(implode) memcpy(&output[k], temp, len); k += len; script_removetop(st, -1, 0); - + output[k] = '\0'; } - + script_pushstr(st, output); return true; } @@ -13547,232 +14179,242 @@ BUILDIN(implode) // Implements C sprintf, except format %n. The resulting string is // returned, instead of being saved in variable by reference. //------------------------------------------------------- -BUILDIN(sprintf) -{ - unsigned int len, argc = 0, arg = 0, buf2_len = 0; - const char* format; - char* p; - char* q; - char* buf = NULL; - char* buf2 = NULL; - struct script_data* data; - StringBuf final_buf; - - // Fetch init data - format = script_getstr(st, 2); - argc = script_lastdata(st)-2; - len = strlen(format); - - // Skip parsing, where no parsing is required. - if(len==0){ - script_pushconststr(st,""); - return true; - } - - // Pessimistic alloc - CREATE(buf, char, len+1); - - // Need not be parsed, just solve stuff like %%. - if(argc==0){ +BUILDIN(sprintf) { + unsigned int argc = 0, arg = 0; + const char* format; + char* p; + char* q; + char* buf = NULL; + char* buf2 = NULL; + struct script_data* data; + size_t len, buf2_len = 0; + StringBuf final_buf; + + // Fetch init data + format = script_getstr(st, 2); + argc = script_lastdata(st)-2; + len = strlen(format); + + // Skip parsing, where no parsing is required. + if(len==0) { + script_pushconststr(st,""); + return true; + } + + // Pessimistic alloc + CREATE(buf, char, len+1); + + // Need not be parsed, just solve stuff like %%. + if(argc==0) { memcpy(buf,format,len+1); - script_pushstrcopy(st, buf); - aFree(buf); - return true; - } - - safestrncpy(buf, format, len+1); - - // Issue sprintf for each parameter - StrBuf->Init(&final_buf); - q = buf; - while((p = strchr(q, '%'))!=NULL){ - if(p!=q){ - len = p-q+1; - if(buf2_len<len){ - RECREATE(buf2, char, len); - buf2_len = len; - } - safestrncpy(buf2, q, len); - StrBuf->AppendStr(&final_buf, buf2); - q = p; - } - p = q+1; - if(*p=='%'){ // %% - StrBuf->AppendStr(&final_buf, "%"); - q+=2; - continue; - } - if(*p=='n'){ // %n - ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n"); - script_reportsrc(st); - q+=2; - continue; - } - if(arg>=argc){ - ShowError("buildin_sprintf: Not enough arguments passed!\n"); - if(buf) aFree(buf); - if(buf2) aFree(buf2); - StrBuf->Destroy(&final_buf); - script_pushconststr(st,""); - return false; - } - if((p = strchr(q+1, '%'))==NULL){ - p = strchr(q, 0); // EOS - } - len = p-q+1; - if(buf2_len<len){ - RECREATE(buf2, char, len); - buf2_len = len; - } - safestrncpy(buf2, q, len); - q = p; - - // Note: This assumes the passed value being the correct - // type to the current format specifier. If not, the server - // probably crashes or returns anything else, than expected, - // but it would behave in normal code the same way so it's - // the scripter's responsibility. - data = script_getdata(st, arg+3); - if(data_isstring(data)){ // String - StrBuf->Printf(&final_buf, buf2, script_getstr(st, arg+3)); - }else if(data_isint(data)){ // Number - StrBuf->Printf(&final_buf, buf2, script_getnum(st, arg+3)); - }else if(data_isreference(data)){ // Variable - char* name = reference_getname(data); - if(name[strlen(name)-1]=='$'){ // var Str - StrBuf->Printf(&final_buf, buf2, script_getstr(st, arg+3)); - }else{ // var Int - StrBuf->Printf(&final_buf, buf2, script_getnum(st, arg+3)); - } - }else{ // Unsupported type - ShowError("buildin_sprintf: Unknown argument type!\n"); - if(buf) aFree(buf); - if(buf2) aFree(buf2); - StrBuf->Destroy(&final_buf); - script_pushconststr(st,""); - return false; - } - arg++; - } - - // Append anything left - if(*q){ - StrBuf->AppendStr(&final_buf, q); - } - - // Passed more, than needed - if(arg<argc){ - ShowWarning("buildin_sprintf: Unused arguments passed.\n"); - script_reportsrc(st); - } - - script_pushstrcopy(st, StrBuf->Value(&final_buf)); - - if(buf) aFree(buf); - if(buf2) aFree(buf2); - StrBuf->Destroy(&final_buf); - - return true; + script_pushstrcopy(st, buf); + aFree(buf); + return true; + } + + safestrncpy(buf, format, len+1); + + // Issue sprintf for each parameter + StrBuf->Init(&final_buf); + q = buf; + while((p = strchr(q, '%'))!=NULL) { + if(p!=q) { + len = p-q+1; + if(buf2_len<len) { + RECREATE(buf2, char, len); + buf2_len = len; + } + safestrncpy(buf2, q, len); + StrBuf->AppendStr(&final_buf, buf2); + q = p; + } + p = q+1; + if(*p=='%') { // %% + StrBuf->AppendStr(&final_buf, "%"); + q+=2; + continue; + } + if(*p=='n') { // %n + ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n"); + script->reportsrc(st); + q+=2; + continue; + } + if(arg>=argc) { + ShowError("buildin_sprintf: Not enough arguments passed!\n"); + if(buf) aFree(buf); + if(buf2) aFree(buf2); + StrBuf->Destroy(&final_buf); + script_pushconststr(st,""); + return false; + } + if((p = strchr(q+1, '%'))==NULL) { + p = strchr(q, 0); // EOS + } + len = p-q+1; + if(buf2_len<len) { + RECREATE(buf2, char, len); + buf2_len = len; + } + safestrncpy(buf2, q, len); + q = p; + + // Note: This assumes the passed value being the correct + // type to the current format specifier. If not, the server + // probably crashes or returns anything else, than expected, + // but it would behave in normal code the same way so it's + // the scripter's responsibility. + data = script_getdata(st, arg+3); + if(data_isstring(data)) { // String + StrBuf->Printf(&final_buf, buf2, script_getstr(st, arg+3)); + } else if(data_isint(data)) { // Number + StrBuf->Printf(&final_buf, buf2, script_getnum(st, arg+3)); + } else if(data_isreference(data)) { // Variable + char* name = reference_getname(data); + if(name[strlen(name)-1]=='$') { // var Str + StrBuf->Printf(&final_buf, buf2, script_getstr(st, arg+3)); + } else { // var Int + StrBuf->Printf(&final_buf, buf2, script_getnum(st, arg+3)); + } + } else { // Unsupported type + ShowError("buildin_sprintf: Unknown argument type!\n"); + if(buf) aFree(buf); + if(buf2) aFree(buf2); + StrBuf->Destroy(&final_buf); + script_pushconststr(st,""); + return false; + } + arg++; + } + + // Append anything left + if(*q) { + StrBuf->AppendStr(&final_buf, q); + } + + // Passed more, than needed + if(arg<argc) { + ShowWarning("buildin_sprintf: Unused arguments passed.\n"); + script->reportsrc(st); + } + + script_pushstrcopy(st, StrBuf->Value(&final_buf)); + + if(buf) aFree(buf); + if(buf2) aFree(buf2); + StrBuf->Destroy(&final_buf); + + return true; } //======================================================= // sscanf(<str>, <format>, ...); // Implements C sscanf. //------------------------------------------------------- -BUILDIN(sscanf){ - unsigned int argc, arg = 0, len; - struct script_data* data; - struct map_session_data* sd = NULL; - const char* str; - const char* format; - const char* p; - const char* q; - char* buf = NULL; - char* buf_p; - char* ref_str = NULL; - int ref_int; - - // Get data - str = script_getstr(st, 2); - format = script_getstr(st, 3); - argc = script_lastdata(st)-3; - - len = strlen(format); - CREATE(buf, char, len*2+1); - - // Issue sscanf for each parameter - *buf = 0; - q = format; - while((p = strchr(q, '%'))){ - if(p!=q){ - strncat(buf, q, (size_t)(p-q)); - q = p; - } - p = q+1; - if(*p=='*' || *p=='%'){ // Skip - strncat(buf, q, 2); - q+=2; - continue; - } - if(arg>=argc){ - ShowError("buildin_sscanf: Not enough arguments passed!\n"); - script_pushint(st, -1); - if(buf) aFree(buf); - if(ref_str) aFree(ref_str); - return false; - } - if((p = strchr(q+1, '%'))==NULL){ - p = strchr(q, 0); // EOS - } - len = p-q; - strncat(buf, q, len); - q = p; - - // Validate output - data = script_getdata(st, arg+4); - if(!data_isreference(data) || !reference_tovariable(data)){ - ShowError("buildin_sscanf: Target argument is not a variable!\n"); - script_pushint(st, -1); - if(buf) aFree(buf); - if(ref_str) aFree(ref_str); - return false; - } - buf_p = reference_getname(data); - if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){ - script_pushint(st, -1); - if(buf) aFree(buf); - if(ref_str) aFree(ref_str); - return true; - } - - // Save value if any - if(buf_p[strlen(buf_p)-1]=='$'){ // String - if(ref_str==NULL){ - CREATE(ref_str, char, strlen(str)+1); - } - if(sscanf(str, buf, ref_str)==0){ - break; - } - set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data)); - } else { // Number - if(sscanf(str, buf, &ref_int)==0){ - break; - } - set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPTRSIZE(ref_int), reference_getref(data)); - } - arg++; - - // Disable used format (%... -> %*...) - buf_p = strchr(buf, 0); - memmove(buf_p-len+2, buf_p-len+1, len); - *(buf_p-len+1) = '*'; - } - - script_pushint(st, arg); - if(buf) aFree(buf); - if(ref_str) aFree(ref_str); - - return true; +BUILDIN(sscanf) { + unsigned int argc, arg = 0; + struct script_data* data; + struct map_session_data* sd = NULL; + const char* str; + const char* format; + const char* p; + const char* q; + char* buf = NULL; + char* buf_p; + char* ref_str = NULL; + int ref_int; + size_t len; + + // Get data + str = script_getstr(st, 2); + format = script_getstr(st, 3); + argc = script_lastdata(st)-3; + + len = strlen(format); + + if (len != 0 && strlen(str) == 0) { + // If the source string is empty but the format string is not, we return -1 + // according to the C specs. (if the format string is also empty, we shall + // continue and return 0: 0 conversions took place out of the 0 attempted.) + script_pushint(st, -1); + return true; + } + + CREATE(buf, char, len*2+1); + + // Issue sscanf for each parameter + *buf = 0; + q = format; + while((p = strchr(q, '%'))) { + if(p!=q) { + strncat(buf, q, (size_t)(p-q)); + q = p; + } + p = q+1; + if(*p=='*' || *p=='%') { // Skip + strncat(buf, q, 2); + q+=2; + continue; + } + if(arg>=argc) { + ShowError("buildin_sscanf: Not enough arguments passed!\n"); + script_pushint(st, -1); + if(buf) aFree(buf); + if(ref_str) aFree(ref_str); + return false; + } + if((p = strchr(q+1, '%'))==NULL) { + p = strchr(q, 0); // EOS + } + len = p-q; + strncat(buf, q, len); + q = p; + + // Validate output + data = script_getdata(st, arg+4); + if(!data_isreference(data) || !reference_tovariable(data)) { + ShowError("buildin_sscanf: Target argument is not a variable!\n"); + script_pushint(st, -1); + if(buf) aFree(buf); + if(ref_str) aFree(ref_str); + return false; + } + buf_p = reference_getname(data); + if(not_server_variable(*buf_p) && (sd = script->rid2sd(st))==NULL) { + script_pushint(st, -1); + if(buf) aFree(buf); + if(ref_str) aFree(ref_str); + return true; + } + + // Save value if any + if(buf_p[strlen(buf_p)-1]=='$') { // String + if(ref_str==NULL) { + CREATE(ref_str, char, strlen(str)+1); + } + if(sscanf(str, buf, ref_str)==0) { + break; + } + script->set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data)); + } else { // Number + if(sscanf(str, buf, &ref_int)==0) { + break; + } + script->set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)h64BPTRSIZE(ref_int), reference_getref(data)); + } + arg++; + + // Disable used format (%... -> %*...) + buf_p = strchr(buf, 0); + memmove(buf_p-len+2, buf_p-len+1, len); + *(buf_p-len+1) = '*'; + } + + script_pushint(st, arg); + if(buf) aFree(buf); + if(ref_str) aFree(ref_str); + + return true; } //======================================================= @@ -13787,17 +14429,17 @@ BUILDIN(strpos) { const char *needle = script_getstr(st,3); int i; size_t len; - + if( script_hasdata(st,4) ) i = script_getnum(st,4); else i = 0; - + if (needle[0] == '\0') { script_pushint(st, -1); return true; } - + len = strlen(haystack); for ( ; i < len; ++i ) { if ( haystack[i] == *needle ) { @@ -13835,48 +14477,47 @@ BUILDIN(replacestr) size_t findlen = strlen(find); struct StringBuf output; bool usecase = true; - + int count = 0; int numFinds = 0; int i = 0, f = 0; - + if(findlen == 0) { ShowError("script:replacestr: Invalid search length.\n"); st->state = END; return false; } - + if(script_hasdata(st, 5)) { - if( !script_isstring(st,5) ) + if( script_isinttype(st,5) ) { usecase = script_getnum(st, 5) != 0; - else { - ShowError("script:replacestr: Invalid usecase value. Expected int got string\n"); + } else { + ShowError("script:replacestr: Invalid usecase value. Expected int.\n"); st->state = END; return false; } } - + if(script_hasdata(st, 6)) { - count = script_getnum(st, 6); - if(count == 0) { - ShowError("script:replacestr: Invalid count value. Expected int got string\n"); + if (!script_isinttype(st, 5) || (count = script_getnum(st, 6) == 0)) { + ShowError("script:replacestr: Invalid count value. Expected int.\n"); st->state = END; return false; } } - + StrBuf->Init(&output); - + for(; i < inputlen; i++) { - if(count && count == numFinds) { //found enough, stop looking - break; + if(count && count == numFinds) { + break; //found enough, stop looking } - + for(f = 0; f <= findlen; f++) { if(f == findlen) { //complete match numFinds++; StrBuf->AppendStr(&output, replace); - + i += findlen - 1; break; } else { @@ -13894,11 +14535,11 @@ BUILDIN(replacestr) } } } - + //append excess after enough found if(i < inputlen) StrBuf->AppendStr(&output, &(input[i])); - + script_pushstrcopy(st, StrBuf->Value(&output)); StrBuf->Destroy(&output); return true; @@ -13917,26 +14558,26 @@ BUILDIN(countstr) size_t inputlen = strlen(input); size_t findlen = strlen(find); bool usecase = true; - + int numFinds = 0; int i = 0, f = 0; - + if(findlen == 0) { ShowError("script:countstr: Invalid search length.\n"); st->state = END; return false; } - + if(script_hasdata(st, 4)) { - if( !script_isstring(st,4) ) + if( script_isinttype(st,4) ) usecase = script_getnum(st, 4) != 0; else { - ShowError("script:countstr: Invalid usecase value. Expected int got string\n"); + ShowError("script:countstr: Invalid usecase value. Expected int.\n"); st->state = END; return false; } } - + for(; i < inputlen; i++) { for(f = 0; f <= findlen; f++) { if(f == findlen) { //complete match @@ -13968,70 +14609,80 @@ BUILDIN(countstr) /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int> /// setnpcdisplay("<npc name>", "<new display name>") -> <int> /// setnpcdisplay("<npc name>", <new class id>) -> <int> -BUILDIN(setnpcdisplay) -{ +BUILDIN(setnpcdisplay) { const char* name; const char* newname = NULL; int class_ = -1, size = -1; - struct script_data* data; struct npc_data* nd; - + name = script_getstr(st,2); - data = script_getdata(st,3); - + if( script_hasdata(st,4) ) class_ = script_getnum(st,4); if( script_hasdata(st,5) ) size = script_getnum(st,5); - - script->get_val(st, data); - if( data_isstring(data) ) - newname = script->conv_str(st,data); - else if( data_isint(data) ) - class_ = script->conv_num(st,data); + + if( script_isstringtype(st, 3) ) + newname = script_getstr(st, 3); else - { - ShowError("script:setnpcdisplay: expected string or number\n"); - script_reportdata(data); - return false; - } - - nd = npc_name2id(name); + class_ = script_getnum(st, 3); + + nd = npc->name2id(name); if( nd == NULL ) {// not found script_pushint(st,1); return true; } - + // update npc if( newname ) - npc_setdisplayname(nd, newname); - + npc->setdisplayname(nd, newname); + if( size != -1 && size != (int)nd->size ) nd->size = size; else size = -1; - + if( class_ != -1 && nd->class_ != class_ ) - npc_setclass(nd, class_); + npc->setclass(nd, class_); else if( size != -1 ) { // Required to update the visual size clif->clearunit_area(&nd->bl, CLR_OUTSIGHT); clif->spawn(&nd->bl); } - + script_pushint(st,0); return true; } -BUILDIN(atoi) -{ +BUILDIN(atoi) { const char *value; value = script_getstr(st,2); script_pushint(st,atoi(value)); return true; } +BUILDIN(axtoi) { + const char *hex = script_getstr(st,2); + long value = strtol(hex, NULL, 16); +#if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN + value = cap_value(value, INT_MIN, INT_MAX); +#endif + script_pushint(st, (int)value); + return true; +} + +BUILDIN(strtol) { + const char *string = script_getstr(st, 2); + int base = script_getnum(st, 3); + long value = strtol(string, NULL, base); +#if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN + value = cap_value(value, INT_MIN, INT_MAX); +#endif + script_pushint(st, (int)value); + return true; +} + // case-insensitive substring search [lordalfa] BUILDIN(compare) { @@ -14066,12 +14717,12 @@ BUILDIN(pow) BUILDIN(distance) { int x0, y0, x1, y1; - + x0 = script_getnum(st,2); y0 = script_getnum(st,3); x1 = script_getnum(st,4); y1 = script_getnum(st,5); - + script_pushint(st,distance_xy(x0,y0,x1,y1)); return true; } @@ -14082,7 +14733,7 @@ BUILDIN(md5) { const char *tmpstr; char *md5str; - + tmpstr = script_getstr(st,2); md5str = (char *)aMalloc((32+1)*sizeof(char)); MD5_String(tmpstr, md5str); @@ -14099,26 +14750,26 @@ BUILDIN(setd) const char *buffer; int elem; buffer = script_getstr(st, 2); - + if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2) elem = 0; - + if( not_server_variable(*varname) ) { - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) { ShowError("script:setd: no player attached for player variable '%s'\n", buffer); return true; } } - + if( is_string_variable(varname) ) { - setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL); + script->setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL); } else { - setd_sub(st, sd, varname, elem, (void *)__64BPTRSIZE(script_getnum(st, 3)), NULL); + script->setd_sub(st, sd, varname, elem, (void *)h64BPTRSIZE(script_getnum(st, 3)), NULL); } - + return true; } @@ -14129,97 +14780,92 @@ int buildin_query_sql_sub(struct script_state* st, Sql* handle) const char* query; struct script_data* data; const char* name; - int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows + unsigned int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows int num_vars; int num_cols; - + // check target variables for( i = 3; script_hasdata(st,i); ++i ) { data = script_getdata(st, i); if( data_isreference(data) ) { // it's a variable name = reference_getname(data); if( not_server_variable(*name) && sd == NULL ) { // requires a player - sd = script_rid2sd(st); - if( sd == NULL ) { // no player attached - script_reportdata(data); - st->state = END; + sd = script->rid2sd(st); + if( sd == NULL )// no player attached return false; - } } - if( not_array_variable(*name) ) - max_rows = 1;// not an array, limit to one row } else { ShowError("script:query_sql: not a variable\n"); - script_reportdata(data); + script->reportdata(data); st->state = END; return false; } } num_vars = i - 3; - + // Execute the query query = script_getstr(st,2); - + if( SQL_ERROR == SQL->QueryStr(handle, query) ) { Sql_ShowDebug(handle); - script_pushint(st, 0); + st->state = END; return false; } - + if( SQL->NumRows(handle) == 0 ) { // No data received SQL->FreeResult(handle); script_pushint(st, 0); return true; } - + // Count the number of columns to store num_cols = SQL->NumColumns(handle); if( num_vars < num_cols ) { ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars)); - script_reportsrc(st); + script->reportsrc(st); } else if( num_vars > num_cols ) { ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols)); - script_reportsrc(st); + script->reportsrc(st); } - + // Store data for( i = 0; i < max_rows && SQL_SUCCESS == SQL->NextRow(handle); ++i ) { for( j = 0; j < num_vars; ++j ) { char* str = NULL; - + if( j < num_cols ) SQL->GetData(handle, j, &str, NULL); - + data = script_getdata(st, j+3); name = reference_getname(data); if( is_string_variable(name) ) - setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data)); + script->setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data)); else - setd_sub(st, sd, name, i, (void *)__64BPTRSIZE((str?atoi(str):0)), reference_getref(data)); + script->setd_sub(st, sd, name, i, (void *)h64BPTRSIZE((str?atoi(str):0)), reference_getref(data)); } } if( i == max_rows && max_rows < SQL->NumRows(handle) ) { ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)SQL->NumRows(handle)); - script_reportsrc(st); + script->reportsrc(st); } - + // Free data SQL->FreeResult(handle); script_pushint(st, i); - + return true; } BUILDIN(query_sql) { - return buildin_query_sql_sub(st, mmysql_handle); + return script->buildin_query_sql_sub(st, map->mysql_handle); } BUILDIN(query_logsql) { - if( !logs->config.sql_logs ) {// logmysql_handle == NULL + if( !logs->config.sql_logs ) {// logs->mysql_handle == NULL ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2)); script_pushint(st,-1); return false; } - return buildin_query_sql_sub(st, logmysql_handle); + return script->buildin_query_sql_sub(st, logs->mysql_handle); } //Allows escaping of a given string. @@ -14228,29 +14874,28 @@ BUILDIN(escape_sql) const char *str; char *esc_str; size_t len; - + str = script_getstr(st,2); len = strlen(str); esc_str = (char*)aMalloc(len*2+1); - SQL->EscapeStringLen(mmysql_handle, esc_str, str, len); + SQL->EscapeStringLen(map->mysql_handle, esc_str, str, len); script_pushstr(st, esc_str); return true; } -BUILDIN(getd) -{ +BUILDIN(getd) { char varname[100]; const char *buffer; int elem; - + buffer = script_getstr(st, 2); - - if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2) + + if (sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2) elem = 0; - + // Push the 'pointer' so it's more flexible [Lance] - script->push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem),NULL); - + script->push_val(st->stack, C_NAME, reference_uid(script->add_str(varname), elem),NULL); + return true; } @@ -14261,8 +14906,8 @@ BUILDIN(petstat) TBL_PC *sd = NULL; struct pet_data *pd; int flag = script_getnum(st,2); - sd = script_rid2sd(st); - if(!sd || !sd->status.pet_id || !sd->pd){ + sd = script->rid2sd(st); + if(!sd || !sd->status.pet_id || !sd->pd) { if(flag == 2) script_pushconststr(st, ""); else @@ -14270,7 +14915,7 @@ BUILDIN(petstat) return true; } pd = sd->pd; - switch(flag){ + switch(flag) { case 1: script_pushint(st,(int)pd->pet.class_); break; case 2: script_pushstrcopy(st, pd->pet.name); break; case 3: script_pushint(st,(int)pd->pet.level); break; @@ -14289,37 +14934,35 @@ BUILDIN(callshop) struct npc_data *nd; const char *shopname; int flag = 0; - sd = script_rid2sd(st); - if (!sd) { - script_pushint(st,0); - return true; - } + sd = script->rid2sd(st); + if( sd == NULL ) + return false; shopname = script_getstr(st, 2); if( script_hasdata(st,3) ) flag = script_getnum(st,3); - nd = npc_name2id(shopname); + nd = npc->name2id(shopname); if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP) ) { ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname); script_pushint(st,0); return false; } - + if( nd->subtype == SHOP ) { // flag the user as using a valid script call for opening the shop (for floating NPCs) sd->state.callshop = 1; - + switch( flag ) { - case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window - case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window + case 1: npc->buysellsel(sd,nd->bl.id,0); break; //Buy window + case 2: npc->buysellsel(sd,nd->bl.id,1); break; //Sell window default: clif->npcbuysell(sd,nd->bl.id); break; //Show menu } } else clif->cashshop_show(sd, nd); - + sd->npc_shopid = nd->bl.id; script_pushint(st,1); return true; @@ -14328,19 +14971,19 @@ BUILDIN(callshop) BUILDIN(npcshopitem) { const char* npcname = script_getstr(st, 2); - struct npc_data* nd = npc_name2id(npcname); + struct npc_data* nd = npc->name2id(npcname); int n, i; int amount; - - if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) ) - { //Not found. + + if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) ) { + //Not found. script_pushint(st,0); return true; } - + // get the count of new entries amount = (script_lastdata(st)-2)/2; - + // generate new shop item list RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount); for( n = 0, i = 3; n < amount; n++, i+=2 ) @@ -14349,7 +14992,7 @@ BUILDIN(npcshopitem) nd->u.shop.shop_item[n].value = script_getnum(st,i+1); } nd->u.shop.count = n; - + script_pushint(st,1); return true; } @@ -14357,19 +15000,19 @@ BUILDIN(npcshopitem) BUILDIN(npcshopadditem) { const char* npcname = script_getstr(st,2); - struct npc_data* nd = npc_name2id(npcname); + struct npc_data* nd = npc->name2id(npcname); int n, i; int amount; - - if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) ) - { //Not found. + + if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) ) { + //Not found. script_pushint(st,0); return true; } - + // get the count of new entries amount = (script_lastdata(st)-2)/2; - + // append new items to existing shop item list RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount); for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 ) @@ -14378,7 +15021,7 @@ BUILDIN(npcshopadditem) nd->u.shop.shop_item[n].value = script_getnum(st,i+1); } nd->u.shop.count = n; - + script_pushint(st,1); return true; } @@ -14386,26 +15029,26 @@ BUILDIN(npcshopadditem) BUILDIN(npcshopdelitem) { const char* npcname = script_getstr(st,2); - struct npc_data* nd = npc_name2id(npcname); + struct npc_data* nd = npc->name2id(npcname); unsigned int nameid; int n, i; int amount; int size; - - if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) ) - { //Not found. + + if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) ) { + //Not found. script_pushint(st,0); return true; } - + amount = script_lastdata(st)-2; size = nd->u.shop.count; - + // remove specified items from the shop item list for( i = 3; i < 3 + amount; i++ ) { nameid = script_getnum(st,i); - + ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid ); if( n < size ) { @@ -14413,35 +15056,34 @@ BUILDIN(npcshopdelitem) size--; } } - + RECREATE(nd->u.shop.shop_item, struct npc_item_list, size); nd->u.shop.count = size; - + script_pushint(st,1); return true; } //Sets a script to attach to a shop npc. -BUILDIN(npcshopattach) -{ +BUILDIN(npcshopattach) { const char* npcname = script_getstr(st,2); - struct npc_data* nd = npc_name2id(npcname); + struct npc_data* nd = npc->name2id(npcname); int flag = 1; - + if( script_hasdata(st,3) ) flag = script_getnum(st,3); - - if( !nd || nd->subtype != SHOP ) - { //Not found. + + if( !nd || nd->subtype != SHOP ) { + //Not found. script_pushint(st,0); return true; } - + if (flag) - nd->master_nd = ((struct npc_data *)iMap->id2bl(st->oid)); + nd->master_nd = ((struct npc_data *)map->id2bl(st->oid)); else nd->master_nd = NULL; - + script_pushint(st,1); return true; } @@ -14449,11 +15091,11 @@ BUILDIN(npcshopattach) /*========================================== * Returns some values of an item [Lupus] * Price, Weight, etc... - setitemscript(itemID,"{new item bonus script}",[n]); - Where n: - 0 - script - 1 - Equip script - 2 - Unequip script + * setitemscript(itemID,"{new item bonus script}",[n]); + * Where n: + * 0 - script + * 1 - Equip script + * 2 - Unequip script *------------------------------------------*/ BUILDIN(setitemscript) { @@ -14461,13 +15103,13 @@ BUILDIN(setitemscript) const char *new_bonus_script; struct item_data *i_data; struct script_code **dstscript; - - item_id = script_getnum(st,2); + + item_id = script_getnum(st,2); new_bonus_script = script_getstr(st,3); if( script_hasdata(st,4) ) n=script_getnum(st,4); - i_data = itemdb_exists(item_id); - + i_data = itemdb->exists(item_id); + if (!i_data || new_bonus_script==NULL || ( new_bonus_script[0] && new_bonus_script[0]!='{' )) { script_pushint(st,0); return true; @@ -14484,52 +15126,132 @@ BUILDIN(setitemscript) break; } if(*dstscript) - script_free_code(*dstscript); - - *dstscript = new_bonus_script[0] ? parse_script(new_bonus_script, "script_setitemscript", 0, 0) : NULL; + script->free_code(*dstscript); + + *dstscript = new_bonus_script[0] ? script->parse(new_bonus_script, "script_setitemscript", 0, 0, NULL) : NULL; script_pushint(st,1); return true; } -/* Work In Progress [Lupus] - BUILDIN(addmonsterdrop) - { - int class_,item_id,chance; - class_=script_getnum(st,2); - item_id=script_getnum(st,3); - chance=script_getnum(st,4); - if(class_>1000 && item_id>500 && chance>0) { - script_pushint(st,1); - } else { - script_pushint(st,0); - } - } - - BUILDIN(delmonsterdrop) - { - int class_,item_id; - class_=script_getnum(st,2); - item_id=script_getnum(st,3); - if(class_>1000 && item_id>500) { - script_pushint(st,1); - } else { - script_pushint(st,0); - } - } - */ +/*======================================================= + * Temporarily add or update a mob drop + * Original Idea By: [Lupus], [Akinari] + * + * addmonsterdrop <mob_id or name>,<item_id>,<rate>; + * + * If given an item the mob already drops, the rate + * is updated to the new rate. Rate must be in the range [1:10000] + * Returns 1 if succeeded (added/updated a mob drop) + *-------------------------------------------------------*/ +BUILDIN(addmonsterdrop) { + struct mob_db *monster; + int item_id, rate, i, c = MAX_MOB_DROP; + + if( script_isstringtype(st,2) ) + monster = mob->db(mob->db_searchname(script_getstr(st,2))); + else + monster = mob->db(script_getnum(st,2)); + + if( monster == mob->dummy ) { + if( script_isstringtype(st,2) ) { + ShowError("buildin_addmonsterdrop: invalid mob name: '%s'.\n", script_getstr(st,2)); + } else { + ShowError("buildin_addmonsterdrop: invalid mob id: '%d'.\n", script_getnum(st,2)); + } + return false; + } + + item_id = script_getnum(st,3); + if( !itemdb->exists(item_id) ) { + ShowError("buildin_addmonsterdrop: Invalid item ID: '%d'.\n", item_id); + return false; + } + + rate = script_getnum(st,4); + if( rate < 1 || rate > 10000 ) { + ShowWarning("buildin_addmonsterdrop: Invalid drop rate '%d'. Capping to the [1:10000] range.\n", rate); + rate = cap_value(rate,1,10000); + } + + for( i = 0; i < MAX_MOB_DROP; i++ ) { + if( monster->dropitem[i].nameid == item_id ) // Item ID found + break; + if( c == MAX_MOB_DROP && monster->dropitem[i].nameid < 1 ) // First empty slot + c = i; + } + if( i < MAX_MOB_DROP ) // If the item ID was found, prefer it + c = i; + + if( c < MAX_MOB_DROP ) { + // Fill in the slot with the item and rate + monster->dropitem[c].nameid = item_id; + monster->dropitem[c].p = rate; + script_pushint(st,1); + } else { + //No place to put the new drop + script_pushint(st,0); + } + + return true; +} + +/*======================================================= + * Temporarily remove a mob drop + * Original Idea By: [Lupus], [Akinari] + * + * delmonsterdrop <mob_id or name>,<item_id>; + * + * Returns 1 if succeeded (deleted a mob drop) + *-------------------------------------------------------*/ +BUILDIN(delmonsterdrop) { + struct mob_db *monster; + int item_id, i; + + if( script_isstringtype(st, 2) ) + monster = mob->db(mob->db_searchname(script_getstr(st, 2))); + else + monster = mob->db(script_getnum(st, 2)); + + if( monster == mob->dummy ) { + if( script_isstringtype(st, 2) ) { + ShowError("buildin_delmonsterdrop: invalid mob name: '%s'.\n", script_getstr(st,2)); + } else { + ShowError("buildin_delmonsterdrop: invalid mob id: '%d'.\n", script_getnum(st,2)); + } + return false; + } + + item_id = script_getnum(st,3); + if( !itemdb->exists(item_id) ) { + ShowError("buildin_delmonsterdrop: Invalid item ID: '%d'.\n", item_id); + return false; + } + + for( i = 0; i < MAX_MOB_DROP; i++ ) { + if( monster->dropitem[i].nameid == item_id ) { + monster->dropitem[i].nameid = 0; + monster->dropitem[i].p = 0; + script_pushint(st,1); + return true; + } + } + // No drop on that monster + script_pushint(st,0); + return true; +} /*========================================== * Returns some values of a monster [Lupus] * Name, Level, race, size, etc... - getmonsterinfo(monsterID,queryIndex); + * getmonsterinfo(monsterID,queryIndex); *------------------------------------------*/ BUILDIN(getmonsterinfo) { - struct mob_db *mob; + struct mob_db *monster; int mob_id; - - mob_id = script_getnum(st,2); - if (!mobdb_checkid(mob_id)) { + + mob_id = script_getnum(st,2); + if (!mob->db_checkid(mob_id)) { ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id); if ( !script_getnum(st,3) ) //requested a string script_pushconststr(st,"null"); @@ -14537,31 +15259,31 @@ BUILDIN(getmonsterinfo) script_pushint(st,-1); return -1; } - mob = mob_db(mob_id); + monster = mob->db(mob_id); switch ( script_getnum(st,3) ) { - case 0: script_pushstrcopy(st,mob->jname); break; - case 1: script_pushint(st,mob->lv); break; - case 2: script_pushint(st,mob->status.max_hp); break; - case 3: script_pushint(st,mob->base_exp); break; - case 4: script_pushint(st,mob->job_exp); break; - case 5: script_pushint(st,mob->status.rhw.atk); break; - case 6: script_pushint(st,mob->status.rhw.atk2); break; - case 7: script_pushint(st,mob->status.def); break; - case 8: script_pushint(st,mob->status.mdef); break; - case 9: script_pushint(st,mob->status.str); break; - case 10: script_pushint(st,mob->status.agi); break; - case 11: script_pushint(st,mob->status.vit); break; - case 12: script_pushint(st,mob->status.int_); break; - case 13: script_pushint(st,mob->status.dex); break; - case 14: script_pushint(st,mob->status.luk); break; - case 15: script_pushint(st,mob->status.rhw.range); break; - case 16: script_pushint(st,mob->range2); break; - case 17: script_pushint(st,mob->range3); break; - case 18: script_pushint(st,mob->status.size); break; - case 19: script_pushint(st,mob->status.race); break; - case 20: script_pushint(st,mob->status.def_ele); break; - case 21: script_pushint(st,mob->status.mode); break; - case 22: script_pushint(st,mob->mexp); break; + case 0: script_pushstrcopy(st,monster->jname); break; + case 1: script_pushint(st,monster->lv); break; + case 2: script_pushint(st,monster->status.max_hp); break; + case 3: script_pushint(st,monster->base_exp); break; + case 4: script_pushint(st,monster->job_exp); break; + case 5: script_pushint(st,monster->status.rhw.atk); break; + case 6: script_pushint(st,monster->status.rhw.atk2); break; + case 7: script_pushint(st,monster->status.def); break; + case 8: script_pushint(st,monster->status.mdef); break; + case 9: script_pushint(st,monster->status.str); break; + case 10: script_pushint(st,monster->status.agi); break; + case 11: script_pushint(st,monster->status.vit); break; + case 12: script_pushint(st,monster->status.int_); break; + case 13: script_pushint(st,monster->status.dex); break; + case 14: script_pushint(st,monster->status.luk); break; + case 15: script_pushint(st,monster->status.rhw.range); break; + case 16: script_pushint(st,monster->range2); break; + case 17: script_pushint(st,monster->range3); break; + case 18: script_pushint(st,monster->status.size); break; + case 19: script_pushint(st,monster->status.race); break; + case 20: script_pushint(st,monster->status.def_ele); break; + case 21: script_pushint(st,monster->status.mode); break; + case 22: script_pushint(st,monster->mexp); break; default: script_pushint(st,-1); //wrong Index } return true; @@ -14570,52 +15292,51 @@ BUILDIN(getmonsterinfo) BUILDIN(checkvending) // check vending [Nab4] { TBL_PC *sd = NULL; - + if(script_hasdata(st,2)) - sd = iMap->nick2sd(script_getstr(st,2)); + sd = map->nick2sd(script_getstr(st,2)); else - sd = script_rid2sd(st); - + sd = script->rid2sd(st); + if(sd) script_pushint(st, sd->state.autotrade ? 2 : sd->state.vending); else script_pushint(st,0); - + return true; } -BUILDIN(checkchatting) // check chatting [Marka] -{ +// check chatting [Marka] +BUILDIN(checkchatting) { TBL_PC *sd = NULL; - + if(script_hasdata(st,2)) - sd = iMap->nick2sd(script_getstr(st,2)); + sd = map->nick2sd(script_getstr(st,2)); else - sd = script_rid2sd(st); - + sd = script->rid2sd(st); + if(sd) script_pushint(st,(sd->chatID != 0)); else script_pushint(st,0); - + return true; } -BUILDIN(checkidle) -{ +BUILDIN(checkidle) { TBL_PC *sd = NULL; - + if (script_hasdata(st, 2)) - sd = iMap->nick2sd(script_getstr(st, 2)); + sd = map->nick2sd(script_getstr(st, 2)); else - sd = script_rid2sd(st); - + sd = script->rid2sd(st); + if (sd) - script_pushint(st, DIFF_TICK(last_tick, sd->idletime)); + script_pushint(st, DIFF_TICK32(sockt->last_tick, sd->idletime)); // TODO: change this to int64 when we'll support 64 bit script values else script_pushint(st, 0); - + return true; } @@ -14625,117 +15346,67 @@ BUILDIN(searchitem) const char *itemname = script_getstr(st,3); struct item_data *items[MAX_SEARCH]; int count; - + char* name; int32 start; int32 id; int32 i; TBL_PC* sd = NULL; - - if ((items[0] = itemdb_exists(atoi(itemname)))) + + if ((items[0] = itemdb->exists(atoi(itemname)))) count = 1; else { - count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname); + count = itemdb->search_name_array(items, ARRAYLENGTH(items), itemname, 0); if (count > MAX_SEARCH) count = MAX_SEARCH; } - + if (!count) { script_pushint(st, 0); return true; } - + if( !data_isreference(data) ) { ShowError("script:searchitem: not a variable\n"); - script_reportdata(data); + script->reportdata(data); st->state = END; return false;// not a variable } - + id = reference_getid(data); start = reference_getindex(data); name = reference_getname(data); - if( not_array_variable(*name) ) - { - ShowError("script:searchitem: illegal scope\n"); - script_reportdata(data); - st->state = END; - return false;// not supported - } - + if( not_server_variable(*name) ) { - sd = script_rid2sd(st); + sd = script->rid2sd(st); if( sd == NULL ) return true;// no player attached } - + if( is_string_variable(name) ) {// string array ShowError("script:searchitem: not an integer array reference\n"); - script_reportdata(data); + script->reportdata(data); st->state = END; return false;// not supported } - + for( i = 0; i < count; ++start, ++i ) {// Set array - void* v = (void*)__64BPTRSIZE((int)items[i]->nameid); - set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data)); + void* v = (void*)h64BPTRSIZE((int)items[i]->nameid); + script->set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data)); } - - script_pushint(st, count); - return true; -} -int axtoi(const char *hexStg) -{ - int n = 0; // position in string - int16 m = 0; // position in digit[] to shift - int count; // loop index - int intValue = 0; // integer value of hex string - int digit[11]; // hold values to convert - while (n < 10) { - if (hexStg[n]=='\0') - break; - if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9 - digit[n] = hexStg[n] & 0x0f; //convert to int - else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f - digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int - else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F - digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int - else break; - n++; - } - count = n; - m = n - 1; - n = 0; - while(n < count) { - // digit[n] is value of hex digit at position n - // (m << 2) is the number of positions to shift - // OR the bits into return value - intValue = intValue | (digit[n] << (m << 2)); - m--; // adjust the position to set - n++; // next digit to process - } - return (intValue); -} - -// [Lance] Hex string to integer converter -BUILDIN(axtoi) -{ - const char *hex = script_getstr(st,2); - script_pushint(st,axtoi(hex)); + script_pushint(st, count); return true; } // [zBuffer] List of player cont commands ---> -BUILDIN(rid2name) -{ +BUILDIN(rid2name) { struct block_list *bl = NULL; int rid = script_getnum(st,2); - if((bl = iMap->id2bl(rid))) - { + if((bl = map->id2bl(rid))) { switch(bl->type) { case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break; case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break; @@ -14755,93 +15426,91 @@ BUILDIN(rid2name) return true; } -BUILDIN(pcblockmove) -{ +BUILDIN(pcblockmove) { int id, flag; TBL_PC *sd = NULL; - + id = script_getnum(st,2); flag = script_getnum(st,3); - + if(id) - sd = iMap->id2sd(id); + sd = map->id2sd(id); else - sd = script_rid2sd(st); - + sd = script->rid2sd(st); + if(sd) sd->state.blockedmove = flag > 0; - + return true; } -BUILDIN(pcfollow) -{ +BUILDIN(pcfollow) { int id, targetid; TBL_PC *sd = NULL; - - + + id = script_getnum(st,2); targetid = script_getnum(st,3); - + if(id) - sd = iMap->id2sd(id); + sd = map->id2sd(id); else - sd = script_rid2sd(st); - + sd = script->rid2sd(st); + if(sd) pc->follow(sd, targetid); - - return true; + + return true; } BUILDIN(pcstopfollow) { int id; TBL_PC *sd = NULL; - - + + id = script_getnum(st,2); - + if(id) - sd = iMap->id2sd(id); + sd = map->id2sd(id); else - sd = script_rid2sd(st); - + sd = script->rid2sd(st); + if(sd) pc->stop_following(sd); - + return true; } // <--- [zBuffer] List of player cont commands // [zBuffer] List of mob control commands ---> //## TODO always return if the request/whatever was successfull [FlavioJS] -/// Makes the unit walk to target position or map +/// Makes the unit walk to target position or target id /// Returns if it was successfull /// /// unitwalk(<unit_id>,<x>,<y>) -> <bool> -/// unitwalk(<unit_id>,<map_id>) -> <bool> -BUILDIN(unitwalk) -{ +/// unitwalk(<unit_id>,<target_id>) -> <bool> +BUILDIN(unitwalk) { struct block_list* bl; - - bl = iMap->id2bl(script_getnum(st,2)); - if( bl == NULL ) - { + + bl = map->id2bl(script_getnum(st,2)); + if( bl == NULL ) { script_pushint(st, 0); + return true; } - else if( script_hasdata(st,4) ) - { + + if( bl->type == BL_NPC ) { + unit->bl2ud2(bl); // ensure the ((TBL_NPC*)bl)->ud is safe to edit + } + if( script_hasdata(st,4) ) { int x = script_getnum(st,3); int y = script_getnum(st,4); - script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations. - } - else - { - int map_id = script_getnum(st,3); - script_pushint(st, unit_walktobl(bl,iMap->id2bl(map_id),65025,1)); + script_pushint(st, unit->walktoxy(bl,x,y,0));// We'll use harder calculations. + } else { + int target_id = script_getnum(st,3); + script_pushint(st, unit->walktobl(bl,map->id2bl(target_id),1,1)); } - + return true; } @@ -14850,10 +15519,10 @@ BUILDIN(unitwalk) /// unitkill <unit_id>; BUILDIN(unitkill) { - struct block_list* bl = iMap->id2bl(script_getnum(st,2)); + struct block_list* bl = map->id2bl(script_getnum(st,2)); if( bl != NULL ) status_kill(bl); - + return true; } @@ -14861,35 +15530,36 @@ BUILDIN(unitkill) /// Returns if it was successfull /// /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool> -BUILDIN(unitwarp) -{ +BUILDIN(unitwarp) { int unit_id; - int map; + int mapid; short x; short y; struct block_list* bl; const char *mapname; - + unit_id = script_getnum(st,2); mapname = script_getstr(st, 3); x = (short)script_getnum(st,4); y = (short)script_getnum(st,5); - + if (!unit_id) //Warp the script's runner - bl = iMap->id2bl(st->rid); + bl = map->id2bl(st->rid); else - bl = iMap->id2bl(unit_id); - + bl = map->id2bl(unit_id); + if( strcmp(mapname,"this") == 0 ) - map = bl?bl->m:-1; - else - map = iMap->mapname2mapid(mapname); - - if( map >= 0 && bl != NULL ) - script_pushint(st, unit_warp(bl,map,x,y,CLR_OUTSIGHT)); + mapid = bl?bl->m:-1; else + mapid = map->mapname2mapid(mapname); + + if( mapid >= 0 && bl != NULL ) { + unit->bl2ud2(bl); // ensure ((TBL_NPC*)bl)->ud is safe to edit + script_pushint(st, unit->warp(bl,mapid,x,y,CLR_OUTSIGHT)); + } else { script_pushint(st, 0); - + } + return true; } @@ -14900,44 +15570,39 @@ BUILDIN(unitwarp) /// /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool> /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool> -BUILDIN(unitattack) -{ +BUILDIN(unitattack) { struct block_list* unit_bl; struct block_list* target_bl = NULL; - struct script_data* data; int actiontype = 0; - + // get unit - unit_bl = iMap->id2bl(script_getnum(st,2)); + unit_bl = map->id2bl(script_getnum(st,2)); if( unit_bl == NULL ) { script_pushint(st, 0); return true; } - - data = script_getdata(st, 3); - script->get_val(st, data); - if( data_isstring(data) ) - { - TBL_PC* sd = iMap->nick2sd(script->conv_str(st, data)); + + if( script_isstringtype(st, 3) ) { + TBL_PC* sd = map->nick2sd(script_getstr(st, 3)); if( sd != NULL ) target_bl = &sd->bl; } else - target_bl = iMap->id2bl(script->conv_num(st, data)); + target_bl = map->id2bl(script_getnum(st, 3)); // request the attack if( target_bl == NULL ) { script_pushint(st, 0); return true; } - + // get actiontype if( script_hasdata(st,4) ) actiontype = script_getnum(st,4); - + switch( unit_bl->type ) { case BL_PC: - clif->pActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, iTimer->gettick()); + clif->pActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, timer->gettick()); script_pushint(st, 1); return true; case BL_MOB: @@ -14951,56 +15616,53 @@ BUILDIN(unitattack) script_pushint(st, 0); return false; } - script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2)); + script_pushint(st, unit->walktobl(unit_bl, target_bl, 65025, 2)); return true; } /// Makes the unit stop attacking and moving /// /// unitstop <unit_id>; -BUILDIN(unitstop) -{ +BUILDIN(unitstop) { int unit_id; struct block_list* bl; - + unit_id = script_getnum(st,2); - - bl = iMap->id2bl(unit_id); - if( bl != NULL ) - { - unit_stop_attack(bl); - unit_stop_walking(bl,4); + + bl = map->id2bl(unit_id); + if( bl != NULL ) { + unit->bl2ud2(bl); // ensure ((TBL_NPC*)bl)->ud is safe to edit + unit->stop_attack(bl); + unit->stop_walking(bl,4); if( bl->type == BL_MOB ) ((TBL_MOB*)bl)->target_id = 0; } - + return true; } /// Makes the unit say the message /// /// unittalk <unit_id>,"<message>"; -BUILDIN(unittalk) -{ +BUILDIN(unittalk) { int unit_id; const char* message; struct block_list* bl; - + unit_id = script_getnum(st,2); message = script_getstr(st, 3); - - bl = iMap->id2bl(unit_id); - if( bl != NULL ) - { + + bl = map->id2bl(unit_id); + if( bl != NULL ) { struct StringBuf sbuf; StrBuf->Init(&sbuf); - StrBuf->Printf(&sbuf, "%s : %s", status_get_name(bl), message); + StrBuf->Printf(&sbuf, "%s : %s", status->get_name(bl), message); clif->disp_overhead(bl, StrBuf->Value(&sbuf)); if( bl->type == BL_PC ) clif->message(((TBL_PC*)bl)->fd, StrBuf->Value(&sbuf)); StrBuf->Destroy(&sbuf); } - + return true; } @@ -15009,18 +15671,17 @@ BUILDIN(unittalk) /// unitemote <unit_id>,<emotion>; /// /// @see e_* in const.txt -BUILDIN(unitemote) -{ +BUILDIN(unitemote) { int unit_id; int emotion; struct block_list* bl; - + unit_id = script_getnum(st,2); emotion = script_getnum(st,3); - bl = iMap->id2bl(unit_id); + bl = map->id2bl(unit_id); if( bl != NULL ) clif->emotion(bl, emotion); - + return true; } @@ -15028,23 +15689,31 @@ BUILDIN(unitemote) /// /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>}; /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>}; -BUILDIN(unitskilluseid) -{ +BUILDIN(unitskilluseid) { int unit_id; uint16 skill_id; uint16 skill_lv; int target_id; struct block_list* bl; - + unit_id = script_getnum(st,2); - skill_id = ( script_isstring(st,3) ? skill->name2id(script_getstr(st,3)) : script_getnum(st,3) ); + skill_id = ( script_isstringtype(st, 3) ? skill->name2id(script_getstr(st, 3)) : script_getnum(st, 3) ); skill_lv = script_getnum(st,4); target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id ); - - bl = iMap->id2bl(unit_id); - if( bl != NULL ) - unit_skilluse_id(bl, target_id, skill_id, skill_lv); - + + bl = map->id2bl(unit_id); + + if( bl != NULL ) { + if( bl->type == BL_NPC ) { + if (!((TBL_NPC*)bl)->status.hp) { + status_calc_npc(((TBL_NPC*)bl), SCO_FIRST); + } else { + status_calc_npc(((TBL_NPC*)bl), SCO_NONE); + } + } + unit->skilluse_id(bl, target_id, skill_id, skill_lv); + } + return true; } @@ -15052,25 +15721,33 @@ BUILDIN(unitskilluseid) /// /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>; /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>; -BUILDIN(unitskillusepos) -{ +BUILDIN(unitskillusepos) { int unit_id; uint16 skill_id; uint16 skill_lv; int skill_x; int skill_y; struct block_list* bl; - + unit_id = script_getnum(st,2); - skill_id = ( script_isstring(st,3) ? skill->name2id(script_getstr(st,3)) : script_getnum(st,3) ); + skill_id = ( script_isstringtype(st, 3) ? skill->name2id(script_getstr(st, 3)) : script_getnum(st, 3) ); skill_lv = script_getnum(st,4); skill_x = script_getnum(st,5); skill_y = script_getnum(st,6); - - bl = iMap->id2bl(unit_id); - if( bl != NULL ) - unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv); - + + bl = map->id2bl(unit_id); + + if( bl != NULL ) { + if( bl->type == BL_NPC ) { + if (!((TBL_NPC*)bl)->status.hp) { + status_calc_npc(((TBL_NPC*)bl), SCO_FIRST); + } else { + status_calc_npc(((TBL_NPC*)bl), SCO_NONE); + } + } + unit->skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv); + } + return true; } @@ -15082,12 +15759,12 @@ BUILDIN(unitskillusepos) BUILDIN(sleep) { int ticks; - + ticks = script_getnum(st,2); - + // detach the player - script_detach_rid(st); - + script->detach_rid(st); + if( ticks <= 0 ) {// do nothing } @@ -15108,26 +15785,23 @@ BUILDIN(sleep) /// Returns if a player is still attached /// /// sleep2(<mili secconds>) -> <bool> -BUILDIN(sleep2) -{ +BUILDIN(sleep2) { int ticks; - + ticks = script_getnum(st,2); - - if( ticks <= 0 ) - {// do nothing - script_pushint(st, (iMap->id2sd(st->rid)!=NULL)); - } - else if( !st->sleep.tick ) - {// sleep for the target amount of time + + if( ticks <= 0 ) { + // do nothing + script_pushint(st, (map->id2sd(st->rid)!=NULL)); + } else if( !st->sleep.tick ) { + // sleep for the target amount of time st->state = RERUNLINE; st->sleep.tick = ticks; - } - else - {// sleep time is over + } else { + // sleep time is over st->state = RUN; st->sleep.tick = 0; - script_pushint(st, (iMap->id2sd(st->rid)!=NULL)); + script_pushint(st, (map->id2sd(st->rid)!=NULL)); } return true; } @@ -15139,18 +15813,18 @@ BUILDIN(awake) { DBIterator *iter; struct script_state *tst; struct npc_data* nd; - - if( ( nd = npc_name2id(script_getstr(st, 2)) ) == NULL ) { + + if( ( nd = npc->name2id(script_getstr(st, 2)) ) == NULL ) { ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2)); return false; } - + iter = db_iterator(script->st_db); - + for( tst = dbi_first(iter); dbi_exists(iter); tst = dbi_next(iter) ) { if( tst->oid == nd->bl.id ) { - TBL_PC* sd = iMap->id2sd(tst->rid); - + TBL_PC* sd = map->id2sd(tst->rid); + if( tst->sleep.timer == INVALID_TIMER ) {// already awake ??? continue; } @@ -15159,17 +15833,17 @@ BUILDIN(awake) { tst->state = END; tst->rid = 0; } - - iTimer->delete_timer(tst->sleep.timer, run_script_timer); + + timer->delete(tst->sleep.timer, script->run_timer); tst->sleep.timer = INVALID_TIMER; if(tst->state != RERUNLINE) tst->sleep.tick = 0; - run_script_main(tst); + script->run_main(tst); } } - + dbi_destroy(iter); - + return true; } @@ -15182,28 +15856,28 @@ BUILDIN(getvariableofnpc) struct script_data* data; const char* name; struct npc_data* nd; - + data = script_getdata(st,2); if( !data_isreference(data) ) {// Not a reference (aka varaible name) ShowError("script:getvariableofnpc: not a variable\n"); - script_reportdata(data); + script->reportdata(data); script_pushnil(st); st->state = END; return false; } - + name = reference_getname(data); if( *name != '.' || name[1] == '@' ) {// not a npc variable ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n"); - script_reportdata(data); + script->reportdata(data); script_pushnil(st); st->state = END; return false; } - - nd = npc_name2id(script_getstr(st,3)); + + nd = npc->name2id(script_getstr(st,3)); if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL ) {// NPC not found or has no script ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3)); @@ -15211,8 +15885,11 @@ BUILDIN(getvariableofnpc) st->state = END; return false; } + + if( !nd->u.scr.script->local.vars ) + nd->u.scr.script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA); - script->push_val(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars ); + script->push_val(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->local); return true; } @@ -15222,65 +15899,64 @@ BUILDIN(getvariableofnpc) /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>; /// /// @author blackhole89 -BUILDIN(warpportal) -{ +BUILDIN(warpportal) { int spx; int spy; - unsigned short mapindex; + unsigned short map_index; int tpx; int tpy; struct skill_unit_group* group; struct block_list* bl; - - bl = iMap->id2bl(st->oid); + + bl = map->id2bl(st->oid); if( bl == NULL ) { ShowError("script:warpportal: npc is needed\n"); return false; } - + spx = script_getnum(st,2); spy = script_getnum(st,3); - mapindex = mapindex_name2id(script_getstr(st, 4)); + map_index = script->mapindexname2id(st,script_getstr(st, 4)); tpx = script_getnum(st,5); tpy = script_getnum(st,6); - - if( mapindex == 0 ) + + if( map_index == 0 ) return true;// map not found - + group = skill->unitsetting(bl, AL_WARP, 4, spx, spy, 0); if( group == NULL ) return true;// failed group->val1 = (group->val1<<16)|(short)0; group->val2 = (tpx<<16) | tpy; - group->val3 = mapindex; - + group->val3 = map_index; + return true; } BUILDIN(openmail) { TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - + mail->openmail(sd); - + return true; } BUILDIN(openauction) { TBL_PC* sd; - - sd = script_rid2sd(st); + + sd = script->rid2sd(st); if( sd == NULL ) return true; - + clif->auction_openwindow(sd); - + return true; } @@ -15289,15 +15965,19 @@ BUILDIN(openauction) /// checkcell("<map name>",<x>,<y>,<type>) -> <bool> /// /// @see cell_chk* constants in const.txt for the types -BUILDIN(checkcell) -{ - int16 m = iMap->mapname2mapid(script_getstr(st,2)); +BUILDIN(checkcell) { + int16 m = map->mapname2mapid(script_getstr(st,2)); int16 x = script_getnum(st,3); int16 y = script_getnum(st,4); cell_chk type = (cell_chk)script_getnum(st,5); - - script_pushint(st, iMap->getcell(m, x, y, type)); - + + if ( m == -1 ) { + ShowWarning("checkcell: Attempted to run on unexsitent map '%s', type %d, x/y %d,%d\n",script_getstr(st,2),type,x,y); + return true; + } + + script_pushint(st, map->getcell(m, x, y, type)); + return true; } @@ -15306,25 +15986,29 @@ BUILDIN(checkcell) /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>; /// /// @see cell_* constants in const.txt for the types -BUILDIN(setcell) -{ - int16 m = iMap->mapname2mapid(script_getstr(st,2)); +BUILDIN(setcell) { + int16 m = map->mapname2mapid(script_getstr(st,2)); int16 x1 = script_getnum(st,3); int16 y1 = script_getnum(st,4); int16 x2 = script_getnum(st,5); int16 y2 = script_getnum(st,6); cell_t type = (cell_t)script_getnum(st,7); bool flag = (bool)script_getnum(st,8); - + int x,y; - + + if ( m == -1 ) { + ShowWarning("setcell: Attempted to run on unexistent map '%s', type %d, x1/y1 - %d,%d | x2/y2 - %d,%d\n",script_getstr(st, 2),type,x1,y1,x2,y2); + return true; + } + if( x1 > x2 ) swap(x1,x2); if( y1 > y2 ) swap(y1,y2); - + for( y = y1; y <= y2; ++y ) for( x = x1; x <= x2; ++x ) - map[m].setcell(m, x, y, type, flag); - + map->list[m].setcell(m, x, y, type, flag); + return true; } @@ -15335,62 +16019,58 @@ BUILDIN(mercenary_create) { struct map_session_data *sd; int class_, contract_time; - - if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 ) + + if( (sd = script->rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 ) return true; - + class_ = script_getnum(st,2); - - if( !merc_class(class_) ) + + if( !mercenary->class(class_) ) return true; - + contract_time = script_getnum(st,3); - merc_create(sd, class_, contract_time); + mercenary->create(sd, class_, contract_time); return true; } -BUILDIN(mercenary_heal) -{ - struct map_session_data *sd = script_rid2sd(st); +BUILDIN(mercenary_heal) { + struct map_session_data *sd = script->rid2sd(st); int hp, sp; - + if( sd == NULL || sd->md == NULL ) return true; hp = script_getnum(st,2); sp = script_getnum(st,3); - - status_heal(&sd->md->bl, hp, sp, 0); + + status->heal(&sd->md->bl, hp, sp, 0); return true; } -BUILDIN(mercenary_sc_start) -{ - struct map_session_data *sd = script_rid2sd(st); +BUILDIN(mercenary_sc_start) { + struct map_session_data *sd = script->rid2sd(st); enum sc_type type; int tick, val1; - + if( sd == NULL || sd->md == NULL ) return true; - + type = (sc_type)script_getnum(st,2); tick = script_getnum(st,3); val1 = script_getnum(st,4); - - status_change_start(&sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2); + + status->change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2); return true; } -BUILDIN(mercenary_get_calls) -{ - struct map_session_data *sd = script_rid2sd(st); - int guild; - +BUILDIN(mercenary_get_calls) { + struct map_session_data *sd = script->rid2sd(st); + int guild_id; + if( sd == NULL ) return true; - - guild = script_getnum(st,2); - switch( guild ) - { + + guild_id = script_getnum(st,2); + switch( guild_id ) { case ARCH_MERC_GUILD: script_pushint(st,sd->status.arch_calls); break; @@ -15404,23 +16084,21 @@ BUILDIN(mercenary_get_calls) script_pushint(st,0); break; } - + return true; } -BUILDIN(mercenary_set_calls) -{ - struct map_session_data *sd = script_rid2sd(st); - int guild, value, *calls; - +BUILDIN(mercenary_set_calls) { + struct map_session_data *sd = script->rid2sd(st); + int guild_id, value, *calls; + if( sd == NULL ) return true; - - guild = script_getnum(st,2); + + guild_id = script_getnum(st,2); value = script_getnum(st,3); - - switch( guild ) - { + + switch( guild_id ) { case ARCH_MERC_GUILD: calls = &sd->status.arch_calls; break; @@ -15433,24 +16111,22 @@ BUILDIN(mercenary_set_calls) default: return true; // Invalid Guild } - + *calls += value; *calls = cap_value(*calls, 0, INT_MAX); - + return true; } -BUILDIN(mercenary_get_faith) -{ - struct map_session_data *sd = script_rid2sd(st); - int guild; - +BUILDIN(mercenary_get_faith) { + struct map_session_data *sd = script->rid2sd(st); + int guild_id; + if( sd == NULL ) return true; - - guild = script_getnum(st,2); - switch( guild ) - { + + guild_id = script_getnum(st,2); + switch( guild_id ) { case ARCH_MERC_GUILD: script_pushint(st,sd->status.arch_faith); break; @@ -15464,23 +16140,21 @@ BUILDIN(mercenary_get_faith) script_pushint(st,0); break; } - + return true; } -BUILDIN(mercenary_set_faith) -{ - struct map_session_data *sd = script_rid2sd(st); - int guild, value, *calls; - +BUILDIN(mercenary_set_faith) { + struct map_session_data *sd = script->rid2sd(st); + int guild_id, value, *calls; + if( sd == NULL ) return true; - - guild = script_getnum(st,2); + + guild_id = script_getnum(st,2); value = script_getnum(st,3); - - switch( guild ) - { + + switch( guild_id ) { case ARCH_MERC_GUILD: calls = &sd->status.arch_faith; break; @@ -15493,12 +16167,12 @@ BUILDIN(mercenary_set_faith) default: return true; // Invalid Guild } - + *calls += value; *calls = cap_value(*calls, 0, INT_MAX); - if( mercenary_get_guild(sd->md) == guild ) + if( mercenary->get_guild(sd->md) == guild_id ) clif->mercenary_updatestatus(sd,SP_MERCFAITH); - + return true; } @@ -15509,337 +16183,419 @@ BUILDIN(readbook) { struct map_session_data *sd; int book_id, page; - - if( (sd = script_rid2sd(st)) == NULL ) + + if( (sd = script->rid2sd(st)) == NULL ) return true; - + book_id = script_getnum(st,2); page = script_getnum(st,3); - + clif->readbook(sd->fd, book_id, page); return true; } -/****************** - Questlog script commands - *******************/ +/**************************** + * Questlog script commands * + ****************************/ -BUILDIN(setquest) +BUILDIN(questinfo) { - struct map_session_data *sd = script_rid2sd(st); - nullpo_ret(sd); - - quest_add(sd, script_getnum(st, 2)); + struct npc_data *nd = map->id2nd(st->oid); + int quest_id, icon, job, color = 0; + struct questinfo qi; + + if( nd == NULL || nd->bl.m == -1 ) + return true; + + quest_id = script_getnum(st, 2); + icon = script_getnum(st, 3); + + #if PACKETVER >= 20120410 + if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7) + icon = 9999; // Default to nothing if icon id is invalid. + #else + if(icon < 0 || icon > 7) + icon = 0; + else + icon = icon + 1; + #endif + + qi.quest_id = quest_id; + qi.icon = (unsigned char)icon; + qi.nd = nd; + + if( script_hasdata(st, 4) ) { + color = script_getnum(st, 4); + if( color < 0 || color > 3 ) { + ShowWarning("buildin_questinfo: invalid color '%d', changing to 0\n",color); + script->reportfunc(st); + color = 0; + } + qi.color = (unsigned char)color; + } + + qi.hasJob = false; + + if(script_hasdata(st, 5)) { + job = script_getnum(st, 5); + + if (!pcdb_checkid(job)) + ShowError("buildin_questinfo: Nonexistant Job Class.\n"); + else { + qi.hasJob = true; + qi.job = (unsigned short)job; + } + } + + map->add_questinfo(nd->bl.m,&qi); + return true; } -BUILDIN(erasequest) -{ - struct map_session_data *sd = script_rid2sd(st); - nullpo_ret(sd); - - quest_delete(sd, script_getnum(st, 2)); +BUILDIN(setquest) { + struct map_session_data *sd = script->rid2sd(st); + unsigned short i; + int quest_id; + + if( sd == NULL ) + return false; + + quest_id = script_getnum(st, 2); + + quest->add(sd, quest_id); + + // If questinfo is set, remove quest bubble once quest is set. + for(i = 0; i < map->list[sd->bl.m].qi_count; i++) { + struct questinfo *qi = &map->list[sd->bl.m].qi_data[i]; + if( qi->quest_id == quest_id ) { +#if PACKETVER >= 20120410 + clif->quest_show_event(sd, &qi->nd->bl, 9999, 0); +#else + clif->quest_show_event(sd, &qi->nd->bl, 0, 0); +#endif + } + } + return true; } -BUILDIN(completequest) -{ - struct map_session_data *sd = script_rid2sd(st); - nullpo_ret(sd); - - quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE); +BUILDIN(erasequest) { + struct map_session_data *sd = script->rid2sd(st); + + if( sd == NULL ) + return false; + + quest->delete(sd, script_getnum(st, 2)); return true; } -BUILDIN(changequest) -{ - struct map_session_data *sd = script_rid2sd(st); - nullpo_ret(sd); - - quest_change(sd, script_getnum(st, 2),script_getnum(st, 3)); +BUILDIN(completequest) { + struct map_session_data *sd = script->rid2sd(st); + + if( sd == NULL ) + return false; + + quest->update_status(sd, script_getnum(st, 2), Q_COMPLETE); return true; } -BUILDIN(checkquest) -{ - struct map_session_data *sd = script_rid2sd(st); - quest_check_type type = HAVEQUEST; - - nullpo_ret(sd); - +BUILDIN(changequest) { + struct map_session_data *sd = script->rid2sd(st); + + if( sd == NULL ) + return false; + + quest->change(sd, script_getnum(st, 2),script_getnum(st, 3)); + return true; +} + +BUILDIN(checkquest) { + struct map_session_data *sd = script->rid2sd(st); + enum quest_check_type type = HAVEQUEST; + + if( sd == NULL ) + return false; + if( script_hasdata(st, 3) ) - type = (quest_check_type)script_getnum(st, 3); - - script_pushint(st, quest_check(sd, script_getnum(st, 2), type)); - + type = (enum quest_check_type)script_getnum(st, 3); + + script_pushint(st, quest->check(sd, script_getnum(st, 2), type)); + return true; } -BUILDIN(showevent) -{ - TBL_PC *sd = script_rid2sd(st); - struct npc_data *nd = iMap->id2nd(st->oid); - int state, color; - +BUILDIN(showevent) { + TBL_PC *sd = script->rid2sd(st); + struct npc_data *nd = map->id2nd(st->oid); + int icon, color = 0; + if( sd == NULL || nd == NULL ) return true; - state = script_getnum(st, 2); - color = script_getnum(st, 3); - - if( color < 0 || color > 3 ) - color = 0; // set default color - - clif->quest_show_event(sd, &nd->bl, state, color); + + icon = script_getnum(st, 2); + if( script_hasdata(st, 3) ) { + color = script_getnum(st, 3); + if( color < 0 || color > 3 ) { + ShowWarning("buildin_showevent: invalid color '%d', changing to 0\n",color); + script->reportfunc(st); + color = 0; + } + } + + #if PACKETVER >= 20120410 + if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7) + icon = 9999; // Default to nothing if icon id is invalid. + #else + if(icon < 0 || icon > 7) + icon = 0; + else + icon = icon + 1; + #endif + + clif->quest_show_event(sd, &nd->bl, icon, color); return true; } /*========================================== * BattleGround System *------------------------------------------*/ -BUILDIN(waitingroom2bg) -{ +BUILDIN(waitingroom2bg) { struct npc_data *nd; struct chat_data *cd; const char *map_name, *ev = "", *dev = ""; - int x, y, i, mapindex = 0, bg_id, n; + int x, y, i, map_index = 0, bg_id, n; struct map_session_data *sd; - + if( script_hasdata(st,7) ) - nd = npc_name2id(script_getstr(st,7)); + nd = npc->name2id(script_getstr(st,7)); else - nd = (struct npc_data *)iMap->id2bl(st->oid); - - if( nd == NULL || (cd = (struct chat_data *)iMap->id2bl(nd->chat_id)) == NULL ) - { + nd = (struct npc_data *)map->id2bl(st->oid); + + if( nd == NULL || (cd = (struct chat_data *)map->id2bl(nd->chat_id)) == NULL ) { script_pushint(st,0); return true; } - + map_name = script_getstr(st,2); if( strcmp(map_name,"-") != 0 ) { - mapindex = mapindex_name2id(map_name); - if( mapindex == 0 ) + map_index = script->mapindexname2id(st,map_name); + if( map_index == 0 ) { // Invalid Map script_pushint(st,0); return true; } } - + x = script_getnum(st,3); y = script_getnum(st,4); ev = script_getstr(st,5); // Logout Event dev = script_getstr(st,6); // Die Event - - if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 ) + + if( (bg_id = bg->create(map_index, x, y, ev, dev)) == 0 ) { // Creation failed script_pushint(st,0); return true; } - + n = cd->users; for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ ) { - if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) ) - mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), sd->bl.id); + if( (sd = cd->usersd[i]) != NULL && bg->team_join(bg_id, sd) ) + mapreg->setreg(reference_uid(script->add_str("$@arenamembers"), i), sd->bl.id); else - mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), 0); + mapreg->setreg(reference_uid(script->add_str("$@arenamembers"), i), 0); } - - mapreg_setreg(add_str("$@arenamembersnum"), i); + + mapreg->setreg(script->add_str("$@arenamembersnum"), i); script_pushint(st,bg_id); return true; } -BUILDIN(waitingroom2bg_single) -{ +BUILDIN(waitingroom2bg_single) { const char* map_name; struct npc_data *nd; struct chat_data *cd; struct map_session_data *sd; - int x, y, mapindex, bg_id; - + int x, y, map_index, bg_id; + bg_id = script_getnum(st,2); map_name = script_getstr(st,3); - if( (mapindex = mapindex_name2id(map_name)) == 0 ) + if( (map_index = script->mapindexname2id(st,map_name)) == 0 ) return true; // Invalid Map - + x = script_getnum(st,4); y = script_getnum(st,5); - nd = npc_name2id(script_getstr(st,6)); - - if( nd == NULL || (cd = (struct chat_data *)iMap->id2bl(nd->chat_id)) == NULL || cd->users <= 0 ) + nd = npc->name2id(script_getstr(st,6)); + + if( nd == NULL || (cd = (struct chat_data *)map->id2bl(nd->chat_id)) == NULL || cd->users <= 0 ) return true; - + if( (sd = cd->usersd[0]) == NULL ) return true; - - if( bg_team_join(bg_id, sd) ) + + if( bg->team_join(bg_id, sd) ) { - pc->setpos(sd, mapindex, x, y, CLR_TELEPORT); + pc->setpos(sd, map_index, x, y, CLR_TELEPORT); script_pushint(st,1); } else script_pushint(st,0); - + return true; } BUILDIN(bg_team_setxy) { - struct battleground_data *bg; + struct battleground_data *bgd; int bg_id; - + bg_id = script_getnum(st,2); - if( (bg = bg_team_search(bg_id)) == NULL ) + if( (bgd = bg->team_search(bg_id)) == NULL ) return true; - - bg->x = script_getnum(st,3); - bg->y = script_getnum(st,4); + + bgd->x = script_getnum(st,3); + bgd->y = script_getnum(st,4); return true; } BUILDIN(bg_warp) { - int x, y, mapindex, bg_id; + int x, y, map_index, bg_id; const char* map_name; - + bg_id = script_getnum(st,2); map_name = script_getstr(st,3); - if( (mapindex = mapindex_name2id(map_name)) == 0 ) + if( (map_index = script->mapindexname2id(st,map_name)) == 0 ) return true; // Invalid Map x = script_getnum(st,4); y = script_getnum(st,5); - bg_team_warp(bg_id, mapindex, x, y); + bg->team_warp(bg_id, map_index, x, y); return true; } BUILDIN(bg_monster) { int class_ = 0, x = 0, y = 0, bg_id = 0; - const char *str,*map, *evt=""; - - bg_id = script_getnum(st,2); - map = script_getstr(st,3); - x = script_getnum(st,4); - y = script_getnum(st,5); - str = script_getstr(st,6); - class_ = script_getnum(st,7); + const char *str, *mapname, *evt=""; + + bg_id = script_getnum(st,2); + mapname = script_getstr(st,3); + x = script_getnum(st,4); + y = script_getnum(st,5); + str = script_getstr(st,6); + class_ = script_getnum(st,7); if( script_hasdata(st,8) ) evt = script_getstr(st,8); - check_event(st, evt); - script_pushint(st, mob_spawn_bg(map,x,y,str,class_,evt,bg_id)); + script->check_event(st, evt); + script_pushint(st, mob->spawn_bg(mapname,x,y,str,class_,evt,bg_id)); return true; } -BUILDIN(bg_monster_set_team) -{ +BUILDIN(bg_monster_set_team) { struct mob_data *md; struct block_list *mbl; int id = script_getnum(st,2), bg_id = script_getnum(st,3); - - if( (mbl = iMap->id2bl(id)) == NULL || mbl->type != BL_MOB ) + + if( (mbl = map->id2bl(id)) == NULL || mbl->type != BL_MOB ) return true; md = (TBL_MOB *)mbl; md->bg_id = bg_id; - + mob_stop_attack(md); mob_stop_walking(md, 0); md->target_id = md->attacked_id = 0; clif->charnameack(0, &md->bl); - + return true; } BUILDIN(bg_leave) { - struct map_session_data *sd = script_rid2sd(st); + struct map_session_data *sd = script->rid2sd(st); if( sd == NULL || !sd->bg_id ) return true; - - bg_team_leave(sd,0); + + bg->team_leave(sd,BGTL_LEFT); return true; } BUILDIN(bg_destroy) { int bg_id = script_getnum(st,2); - bg_team_delete(bg_id); + bg->team_delete(bg_id); return true; } -BUILDIN(bg_getareausers) -{ +BUILDIN(bg_getareausers) { const char *str; int16 m, x0, y0, x1, y1; int bg_id; int i = 0, c = 0; - struct battleground_data *bg = NULL; + struct battleground_data *bgd = NULL; struct map_session_data *sd; - + bg_id = script_getnum(st,2); str = script_getstr(st,3); - - if( (bg = bg_team_search(bg_id)) == NULL || (m = iMap->mapname2mapid(str)) < 0 ) - { + + if( (bgd = bg->team_search(bg_id)) == NULL || (m = map->mapname2mapid(str)) < 0 ) { script_pushint(st,0); return true; } - + x0 = script_getnum(st,4); y0 = script_getnum(st,5); x1 = script_getnum(st,6); y1 = script_getnum(st,7); - + for( i = 0; i < MAX_BG_MEMBERS; i++ ) { - if( (sd = bg->members[i].sd) == NULL ) + if( (sd = bgd->members[i].sd) == NULL ) continue; if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 ) continue; c++; } - + script_pushint(st,c); return true; } -BUILDIN(bg_updatescore) -{ +BUILDIN(bg_updatescore) { const char *str; int16 m; - + str = script_getstr(st,2); - if( (m = iMap->mapname2mapid(str)) < 0 ) + if( (m = map->mapname2mapid(str)) < 0 ) return true; - - map[m].bgscore_lion = script_getnum(st,3); - map[m].bgscore_eagle = script_getnum(st,4); - + + map->list[m].bgscore_lion = script_getnum(st,3); + map->list[m].bgscore_eagle = script_getnum(st,4); + clif->bg_updatescore(m); return true; } BUILDIN(bg_get_data) { - struct battleground_data *bg; + struct battleground_data *bgd; int bg_id = script_getnum(st,2), type = script_getnum(st,3); - - if( (bg = bg_team_search(bg_id)) == NULL ) + + if( (bgd = bg->team_search(bg_id)) == NULL ) { script_pushint(st,0); return true; } - + switch( type ) { - case 0: script_pushint(st, bg->count); break; + case 0: script_pushint(st, bgd->count); break; default: ShowError("script:bg_get_data: unknown data identifier %d\n", type); break; } - + return true; } @@ -15851,7 +16607,7 @@ BUILDIN(instance_create) { const char *name; int owner_id, res; int type = IOT_PARTY; - + name = script_getstr(st, 2); owner_id = script_getnum(st, 3); if( script_hasdata(st,4) ) { @@ -15861,9 +16617,9 @@ BUILDIN(instance_create) { return true; } } - + res = instance->create(owner_id, name, (enum instance_owner_type) type); - if( res == -4 ) { // Already exists + if( res == -4 ) { // Already exists script_pushint(st, -1); return true; } else if( res < 0 ) { @@ -15878,25 +16634,25 @@ BUILDIN(instance_create) { script_pushint(st, -2); return true; } - + script_pushint(st, res); return true; } BUILDIN(instance_destroy) { int instance_id = -1; - + if( script_hasdata(st, 2) ) instance_id = script_getnum(st, 2); else if( st->instance_id >= 0 ) instance_id = st->instance_id; else return true; - + if( !instance->valid(instance_id) ) { ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id); return true; } - + instance->destroy(instance_id); return true; } @@ -15906,22 +16662,22 @@ BUILDIN(instance_attachmap) { int16 m; int instance_id = -1; bool usebasename = false; - + name = script_getstr(st,2); instance_id = script_getnum(st,3); if( script_hasdata(st,4) && script_getnum(st,4) > 0 ) usebasename = true; - + if( script_hasdata(st, 5) ) map_name = script_getstr(st, 5); - + if( (m = instance->add_map(name, instance_id, usebasename, map_name)) < 0 ) { // [Saithis] ShowError("buildin_instance_attachmap: instance creation failed (%s): %d\n", name, m); script_pushconststr(st, ""); return true; } - script_pushconststr(st, map[m].name); - + script_pushconststr(st, map->list[m].name); + return true; } @@ -15929,30 +16685,30 @@ BUILDIN(instance_detachmap) { const char *str; int16 m; int instance_id = -1; - + str = script_getstr(st, 2); if( script_hasdata(st, 3) ) instance_id = script_getnum(st, 3); else if( st->instance_id >= 0 ) instance_id = st->instance_id; else return true; - - if( (m = iMap->mapname2mapid(str)) < 0 || (m = instance->map2imap(m,instance_id)) < 0 ) { + + if( (m = map->mapname2mapid(str)) < 0 || (m = instance->map2imap(m,instance_id)) < 0 ) { ShowError("buildin_instance_detachmap: Trying to detach invalid map %s\n", str); return true; } - + instance->del_map(m); return true; } BUILDIN(instance_attach) { int instance_id = -1; - + instance_id = script_getnum(st, 2); if( !instance->valid(instance_id) ) return true; - + st->instance_id = instance_id; return true; } @@ -15966,35 +16722,35 @@ BUILDIN(instance_set_timeout) { int progress_timeout, idle_timeout; int instance_id = -1; - + progress_timeout = script_getnum(st, 2); idle_timeout = script_getnum(st, 3); - + if( script_hasdata(st, 4) ) instance_id = script_getnum(st, 4); else if( st->instance_id >= 0 ) instance_id = st->instance_id; else return true; - + if( instance_id >= 0 ) instance->set_timeout(instance_id, progress_timeout, idle_timeout); - + return true; } BUILDIN(instance_init) { int instance_id = script_getnum(st, 2); - + if( !instance->valid(instance_id) ) { ShowError("instance_init: invalid instance id %d.\n",instance_id); return true; } - - if( instances[instance_id].state != INSTANCE_IDLE ) { + + if( instance->list[instance_id].state != INSTANCE_IDLE ) { ShowError("instance_init: instance already initialized.\n"); return true; } - + instance->start(instance_id); return true; } @@ -16008,23 +16764,23 @@ BUILDIN(instance_announce) { int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY - + int i; - + if( instance_id == -1 ) { if( st->instance_id >= 0 ) instance_id = st->instance_id; else return true; } - + if( !instance->valid(instance_id) ) return true; - - for( i = 0; i < instances[instance_id].num_map; i++ ) - iMap->foreachinmap(buildin_announce_sub, instances[instance_id].map[i], BL_PC, - mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY); - + + for( i = 0; i < instance->list[instance_id].num_map; i++ ) + map->foreachinmap(script->buildin_announce_sub, instance->list[instance_id].map[i], BL_PC, + mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY); + return true; } @@ -16032,91 +16788,107 @@ BUILDIN(instance_npcname) { const char *str; int instance_id = -1; struct npc_data *nd; - + str = script_getstr(st, 2); if( script_hasdata(st, 3) ) instance_id = script_getnum(st, 3); else if( st->instance_id >= 0 ) instance_id = st->instance_id; - - if( instance_id >= 0 && (nd = npc_name2id(str)) != NULL ) { + + if( instance_id >= 0 && (nd = npc->name2id(str)) != NULL ) { static char npcname[NAME_LENGTH]; snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id); - script_pushconststr(st,npcname); + script_pushconststr(st,npcname); } else { ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str); st->state = END; return false; } - + return true; } BUILDIN(has_instance) { struct map_session_data *sd; - const char *str; + const char *str; int16 m; int instance_id = -1; + bool type = strcmp(script->getfuncname(st),"has_instance2") == 0 ? true : false; - str = script_getstr(st, 2); - - if( (m = iMap->mapname2mapid(str)) < 0 ) { - script_pushconststr(st, ""); + str = script_getstr(st, 2); + + if( (m = map->mapname2mapid(str)) < 0 ) { + if( type ) + script_pushint(st, -1); + else + script_pushconststr(st, ""); return true; } - + if( script_hasdata(st, 3) ) instance_id = script_getnum(st, 3); else if( st->instance_id >= 0 ) instance_id = st->instance_id; - else if( (sd = script_rid2sd(st)) != NULL ) { + else if( (sd = script->rid2sd(st)) != NULL ) { struct party_data *p; int i = 0, j = 0; if( sd->instances ) { for( i = 0; i < sd->instances; i++ ) { - ARR_FIND(0, instances[sd->instance[i]].num_map, j, map[instances[sd->instance[i]].map[j]].instance_src_map == m); - if( j != instances[sd->instance[i]].num_map ) - break; + if( sd->instance[i] >= 0 ) { + ARR_FIND(0, instance->list[sd->instance[i]].num_map, j, map->list[instance->list[sd->instance[i]].map[j]].instance_src_map == m); + if( j != instance->list[sd->instance[i]].num_map ) + break; + } } if( i != sd->instances ) instance_id = sd->instance[i]; } if( instance_id == -1 && sd->status.party_id && (p = party->search(sd->status.party_id)) && p->instances ) { for( i = 0; i < p->instances; i++ ) { - ARR_FIND(0, instances[p->instance[i]].num_map, j, map[instances[p->instance[i]].map[j]].instance_src_map == m); - if( j != instances[p->instance[i]].num_map ) - break; + if( p->instance[i] >= 0 ) { + ARR_FIND(0, instance->list[p->instance[i]].num_map, j, map->list[instance->list[p->instance[i]].map[j]].instance_src_map == m); + if( j != instance->list[p->instance[i]].num_map ) + break; + } } if( i != p->instances ) instance_id = p->instance[i]; } if( instance_id == -1 && sd->guild && sd->guild->instances ) { for( i = 0; i < sd->guild->instances; i++ ) { - ARR_FIND(0, instances[sd->guild->instance[i]].num_map, j, map[instances[sd->guild->instance[i]].map[j]].instance_src_map == m); - if( j != instances[sd->guild->instance[i]].num_map ) - break; + if( sd->guild->instance[i] >= 0 ) { + ARR_FIND(0, instance->list[sd->guild->instance[i]].num_map, j, map->list[instance->list[sd->guild->instance[i]].map[j]].instance_src_map == m); + if( j != instance->list[sd->guild->instance[i]].num_map ) + break; + } } if( i != sd->guild->instances ) instance_id = sd->guild->instance[i]; } } - + if( !instance->valid(instance_id) || (m = instance->map2imap(m, instance_id)) < 0 ) { - script_pushconststr(st, ""); + if( type ) + script_pushint(st, -1); + else + script_pushconststr(st, ""); return true; } - - script_pushconststr(st, map[m].name); + + if( type ) + script_pushint(st, instance_id); + else + script_pushconststr(st, map->list[m].name); return true; } -static int buildin_instance_warpall_sub(struct block_list *bl,va_list ap) { +int buildin_instance_warpall_sub(struct block_list *bl,va_list ap) { struct map_session_data *sd = ((TBL_PC*)bl); - int mapindex = va_arg(ap,int); + int map_index = va_arg(ap,int); int x = va_arg(ap,int); int y = va_arg(ap,int); - - pc->setpos(sd,mapindex,x,y,CLR_TELEPORT); - + + pc->setpos(sd,map_index,x,y,CLR_TELEPORT); + return 0; } BUILDIN(instance_warpall) { @@ -16124,25 +16896,25 @@ BUILDIN(instance_warpall) { int instance_id = -1; const char *mapn; int x, y; - int mapindex; - + int map_index; + mapn = script_getstr(st,2); x = script_getnum(st,3); y = script_getnum(st,4); - + if( script_hasdata(st,5) ) instance_id = script_getnum(st,5); else if( st->instance_id >= 0 ) instance_id = st->instance_id; else return true; - - if( (m = iMap->mapname2mapid(mapn)) < 0 || (map[m].flag.src4instance && (m = instance->mapid2imapid(m, instance_id)) < 0) ) + + if( (m = map->mapname2mapid(mapn)) < 0 || (map->list[m].flag.src4instance && (m = instance->mapid2imapid(m, instance_id)) < 0) ) return true; - - mapindex = map_id2index(m); - - iMap->foreachininstance(buildin_instance_warpall_sub, instance_id, BL_PC,mapindex,x,y); + + map_index = map_id2index(m); + + map->foreachininstance(script->buildin_instance_warpall_sub, instance_id, BL_PC,map_index,x,y); return true; } @@ -16161,46 +16933,46 @@ BUILDIN(instance_check_party) { struct map_session_data *pl_sd; int amount, min, max, i, party_id, c = 0; struct party_data *p = NULL; - + amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance. - - if( min < 1 || min > MAX_LEVEL){ + + if( min < 1 || min > MAX_LEVEL) { ShowError("instance_check_party: Invalid min level, %d\n", min); return true; - } else if( max < 1 || max > MAX_LEVEL){ + } else if( max < 1 || max > MAX_LEVEL) { ShowError("instance_check_party: Invalid max level, %d\n", max); return true; } - + if( script_hasdata(st,2) ) party_id = script_getnum(st,2); else return true; - - if( !(p = party->search(party_id)) ){ + + if( !(p = party->search(party_id)) ) { script_pushint(st, 0); // Returns false if party does not exist. return true; } - + for( i = 0; i < MAX_PARTY; i++ ) if( (pl_sd = p->data[i].sd) ) - if(iMap->id2bl(pl_sd->bl.id)){ - if(pl_sd->status.base_level < min){ + if(map->id2bl(pl_sd->bl.id)) { + if(pl_sd->status.base_level < min) { script_pushint(st, 0); return true; - }else if(pl_sd->status.base_level > max){ + } else if(pl_sd->status.base_level > max) { script_pushint(st, 0); return true; } c++; } - - if(c < amount){ + + if(c < amount) { script_pushint(st, 0); // Not enough Members in the Party to join Instance. - }else + } else script_pushint(st, 1); - + return true; } @@ -16209,113 +16981,112 @@ BUILDIN(instance_check_party) { *------------------------------------------*/ BUILDIN(setfont) { - struct map_session_data *sd = script_rid2sd(st); + struct map_session_data *sd = script->rid2sd(st); int font = script_getnum(st,2); if( sd == NULL ) return true; - - if( sd->user_font != font ) - sd->user_font = font; + + if( sd->status.font != font ) + sd->status.font = font; else - sd->user_font = 0; - + sd->status.font = 0; + clif->font(sd); return true; } -static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap) +int buildin_mobuseskill_sub(struct block_list *bl,va_list ap) { - TBL_MOB* md = (TBL_MOB*)bl; + TBL_MOB* md = (TBL_MOB*)bl; struct block_list *tbl; - int mobid = va_arg(ap,int); - uint16 skill_id = va_arg(ap,int); - uint16 skill_lv = va_arg(ap,int); - int casttime = va_arg(ap,int); - int cancel = va_arg(ap,int); - int emotion = va_arg(ap,int); - int target = va_arg(ap,int); - + int mobid = va_arg(ap,int); + uint16 skill_id = va_arg(ap,int); + uint16 skill_lv = va_arg(ap,int); + int casttime = va_arg(ap,int); + int cancel = va_arg(ap,int); + int emotion = va_arg(ap,int); + int target = va_arg(ap,int); + if( md->class_ != mobid ) return 0; - + // 0:self, 1:target, 2:master, default:random switch( target ) { - case 0: tbl = iMap->id2bl(md->bl.id); break; - case 1: tbl = iMap->id2bl(md->target_id); break; - case 2: tbl = iMap->id2bl(md->master_id); break; - default:tbl = battle->get_enemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill->get_range2(&md->bl, skill_id, skill_lv)); break; + case 0: tbl = map->id2bl(md->bl.id); break; + case 1: tbl = map->id2bl(md->target_id); break; + case 2: tbl = map->id2bl(md->master_id); break; + default: tbl = battle->get_enemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill->get_range2(&md->bl, skill_id, skill_lv)); break; } - + if( !tbl ) return 0; - + if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill. - unit_skillcastcancel(bl,0); - + unit->skillcastcancel(bl,0); + if( skill->get_casttype(skill_id) == CAST_GROUND ) - unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel); + unit->skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel); else - unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel); - + unit->skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel); + clif->emotion(&md->bl, emotion); - + return 0; } /*========================================== * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>; *------------------------------------------*/ -BUILDIN(areamobuseskill) -{ +BUILDIN(areamobuseskill) { struct block_list center; int16 m; int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel; - - if( (m = iMap->mapname2mapid(script_getstr(st,2))) < 0 ) { + + if( (m = map->mapname2mapid(script_getstr(st,2))) < 0 ) { ShowError("areamobuseskill: invalid map name.\n"); return true; } - - if( map[m].flag.src4instance && st->instance_id >= 0 && (m = instance->mapid2imapid(m, st->instance_id)) < 0 ) + + if( map->list[m].flag.src4instance && st->instance_id >= 0 && (m = instance->mapid2imapid(m, st->instance_id)) < 0 ) return true; - + center.m = m; center.x = script_getnum(st,3); center.y = script_getnum(st,4); range = script_getnum(st,5); mobid = script_getnum(st,6); - skill_id = ( script_isstring(st,7) ? skill->name2id(script_getstr(st,7)) : script_getnum(st,7) ); + skill_id = ( script_isstringtype(st, 7) ? skill->name2id(script_getstr(st, 7)) : script_getnum(st, 7) ); skill_lv = script_getnum(st,8); casttime = script_getnum(st,9); cancel = script_getnum(st,10); emotion = script_getnum(st,11); target = script_getnum(st,12); - - iMap->foreachinrange(buildin_mobuseskill_sub, ¢er, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target); + + map->foreachinrange(script->buildin_mobuseskill_sub, ¢er, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target); return true; } BUILDIN(progressbar) { - struct map_session_data * sd = script_rid2sd(st); + struct map_session_data * sd = script->rid2sd(st); const char * color; unsigned int second; - + if( !st || !sd ) return true; - + st->state = STOP; - + color = script_getstr(st,2); second = script_getnum(st,3); - + sd->progressbar.npc_id = st->oid; - sd->progressbar.timeout = iTimer->gettick() + second*1000; + sd->progressbar.timeout = timer->gettick() + second*1000; sd->state.workinprogress = 3; - - clif->progressbar(sd, strtol(color, (char **)NULL, 0), second); - return true; + + clif->progressbar(sd, (unsigned int)strtoul(color, (char **)NULL, 0), second); + return true; } BUILDIN(pushpc) @@ -16323,23 +17094,23 @@ BUILDIN(pushpc) uint8 dir; int cells, dx, dy; struct map_session_data* sd; - - if((sd = script_rid2sd(st))==NULL) + + if((sd = script->rid2sd(st))==NULL) { return true; } - + dir = script_getnum(st,2); cells = script_getnum(st,3); - + if(dir>7) { ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir); - script_reportsrc(st); - + script->reportsrc(st); + dir%= 8; // trim spin-over } - + if(!cells) {// zero distance return true; @@ -16349,11 +17120,11 @@ BUILDIN(pushpc) dir = (dir+4)%8; // turn around cells = -cells; } - + dx = dirx[dir]; dy = diry[dir]; - - unit_blown(&sd->bl, dx, dy, cells, 0); + + unit->blown(&sd->bl, dx, dy, cells, 0); return true; } @@ -16363,11 +17134,11 @@ BUILDIN(pushpc) BUILDIN(buyingstore) { struct map_session_data* sd; - - if( ( sd = script_rid2sd(st) ) == NULL ) { + + if( ( sd = script->rid2sd(st) ) == NULL ) { return true; } - + buyingstore->setup(sd, script_getnum(st,2)); return true; } @@ -16380,27 +17151,27 @@ BUILDIN(searchstores) unsigned short effect; unsigned int uses; struct map_session_data* sd; - - if( ( sd = script_rid2sd(st) ) == NULL ) + + if( ( sd = script->rid2sd(st) ) == NULL ) { return true; } - + uses = script_getnum(st,2); effect = script_getnum(st,3); - + if( !uses ) { ShowError("buildin_searchstores: Amount of uses cannot be zero.\n"); return false; } - + if( effect > 1 ) { ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect); return false; } - + searchstore->open(sd, uses, effect); return true; } @@ -16411,25 +17182,25 @@ BUILDIN(showdigit) unsigned int type = 0; int value; struct map_session_data* sd; - - if( ( sd = script_rid2sd(st) ) == NULL ) + + if( ( sd = script->rid2sd(st) ) == NULL ) { return true; } - + value = script_getnum(st,2); - + if( script_hasdata(st,3) ) { type = script_getnum(st,3); - + if( type > 3 ) { ShowError("buildin_showdigit: Invalid type %u.\n", type); return false; } } - + clif->showdigit(sd, (unsigned char)type, value); return true; } @@ -16438,7 +17209,7 @@ BUILDIN(showdigit) **/ BUILDIN(makerune) { TBL_PC* sd; - if( (sd = script_rid2sd(st)) == NULL ) + if( (sd = script->rid2sd(st)) == NULL ) return true; clif->skill_produce_mix_list(sd,RK_RUNEMASTERY,24); sd->itemid = script_getnum(st,2); @@ -16449,7 +17220,7 @@ BUILDIN(makerune) { **/ BUILDIN(checkdragon) { TBL_PC* sd; - if( (sd = script_rid2sd(st)) == NULL ) + if( (sd = script->rid2sd(st)) == NULL ) return true; if( pc_isridingdragon(sd) ) script_pushint(st,1); @@ -16470,8 +17241,8 @@ BUILDIN(checkdragon) { BUILDIN(setdragon) { TBL_PC* sd; int color = script_hasdata(st,2) ? script_getnum(st,2) : 0; - - if( (sd = script_rid2sd(st)) == NULL ) + + if( (sd = script->rid2sd(st)) == NULL ) return true; if( !pc->checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT ) script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight @@ -16482,10 +17253,10 @@ BUILDIN(setdragon) { unsigned int option = OPTION_DRAGON1; if( color ) { option = ( color == 1 ? OPTION_DRAGON1 : - color == 2 ? OPTION_DRAGON2 : - color == 3 ? OPTION_DRAGON3 : - color == 4 ? OPTION_DRAGON4 : - color == 5 ? OPTION_DRAGON5 : 0); + color == 2 ? OPTION_DRAGON2 : + color == 3 ? OPTION_DRAGON3 : + color == 4 ? OPTION_DRAGON4 : + color == 5 ? OPTION_DRAGON5 : 0); if( !option ) { ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color); option = OPTION_DRAGON1; @@ -16502,7 +17273,7 @@ BUILDIN(setdragon) { **/ BUILDIN(ismounting) { TBL_PC* sd; - if( (sd = script_rid2sd(st)) == NULL ) + if( (sd = script->rid2sd(st)) == NULL ) return true; if( sd->sc.data[SC_ALL_RIDING] ) script_pushint(st,1); @@ -16519,16 +17290,16 @@ BUILDIN(ismounting) { **/ BUILDIN(setmounting) { TBL_PC* sd; - if( (sd = script_rid2sd(st)) == NULL ) + if( (sd = script->rid2sd(st)) == NULL ) return true; - if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ){ + if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) { clif->msgtable(sd->fd, 0X78b); script_pushint(st,0);//can't mount with one of these - }else { + } else { if( sd->sc.data[SC_ALL_RIDING] ) status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER); else - sc_start(&sd->bl, SC_ALL_RIDING, 100, 0, -1); + sc_start(NULL,&sd->bl, SC_ALL_RIDING, 100, 0, -1); script_pushint(st,1);//in both cases, return 1. } return true; @@ -16539,60 +17310,56 @@ BUILDIN(setmounting) { **/ BUILDIN(getargcount) { struct script_retinfo* ri; - + if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) { ShowError("script:getargcount: used out of function or callsub label!\n"); st->state = END; return false; } ri = st->stack->stack_data[st->stack->defsp - 1].u.ri; - + script_pushint(st, ri->nargs); - + return true; } /** * getcharip(<account ID>/<character ID>/<character name>) **/ -BUILDIN(getcharip) -{ +BUILDIN(getcharip) { struct map_session_data* sd = NULL; - + /* check if a character name is specified */ - if( script_hasdata(st, 2) ) - { - if (script_isstring(st, 2)) - sd = iMap->nick2sd(script_getstr(st, 2)); - else if (script_isint(st, 2) || script_getnum(st, 2)) - { - int id; - id = script_getnum(st, 2); - sd = (iMap->id2sd(id) ? iMap->id2sd(id) : iMap->charid2sd(id)); + if( script_hasdata(st, 2) ) { + if (script_isstringtype(st, 2)) { + sd = map->nick2sd(script_getstr(st, 2)); + } else { + int id = script_getnum(st, 2); + sd = (map->id2sd(id) ? map->id2sd(id) : map->charid2sd(id)); } + } else { + sd = script->rid2sd(st); } - else - sd = script_rid2sd(st); - + /* check for sd and IP */ if (!sd || !session[sd->fd]->client_addr) { script_pushconststr(st, ""); return true; } - + /* return the client ip_addr converted for output */ if (sd && sd->fd && session[sd->fd]) { /* initiliaze */ const char *ip_addr = NULL; uint32 ip; - + /* set ip, ip_addr and convert to ip and push str */ ip = session[sd->fd]->client_addr; ip_addr = ip2str(ip, NULL); script_pushstrcopy(st, ip_addr); } - + return true; } /** @@ -16600,39 +17367,84 @@ BUILDIN(getcharip) **/ BUILDIN(is_function) { const char* str = script_getstr(st,2); - - if( strdb_exists(userfunc_db, str) ) + + if( strdb_exists(script->userfunc_db, str) ) script_pushint(st,1); else script_pushint(st,0); - - return true; -} -/** - * get_revision() -> retrieves the current svn revision (if available) - **/ -BUILDIN(get_revision) { - const char *svn = get_svn_revision(); - - if ( svn[0] != HERC_UNKNOWN_VER ) - script_pushint(st,atoi(svn)); - else - script_pushint(st,-1);//unknown - + return true; } /** * freeloop(<toggle>) -> toggles this script instance's looping-check ability **/ BUILDIN(freeloop) { - + if( script_getnum(st,2) ) st->freeloop = 1; else st->freeloop = 0; - + script_pushint(st, st->freeloop); - + + return true; +} + +BUILDIN(sit) { + struct map_session_data *sd = NULL; + + if( script_hasdata(st, 2) ) + sd = map->nick2sd(script_getstr(st, 2)); + else + sd = script->rid2sd(st); + + if( sd == NULL ) + return false; + + if (!pc_issit(sd)) + { + pc_setsit(sd); + skill->sit(sd,1); + clif->sitting(&sd->bl); + } + return true; +} + +BUILDIN(stand) { + struct map_session_data *sd = NULL; + + if( script_hasdata(st, 2) ) + sd = map->nick2sd(script_getstr(st, 2)); + else + sd = script->rid2sd(st); + + if( sd == NULL ) + return false; + + if (pc_issit(sd)) + { + pc->setstand(sd); + skill->sit(sd,0); + clif->standing(&sd->bl); + } + return true; +} + +BUILDIN(issit) { + struct map_session_data *sd = NULL; + + if( script_hasdata(st, 2) ) + sd = map->nick2sd(script_getstr(st, 2)); + else + sd = script->rid2sd(st); + + if( sd == NULL ) + return false; + + if (pc_issit(sd)) + script_pushint(st, 1); + else + script_pushint(st, 0); return true; } @@ -16645,21 +17457,21 @@ BUILDIN(bindatcmd) { int i, group_lv = 0, group_lv_char = 99; bool log = false; bool create = false; - + atcmd = script_getstr(st,2); eventName = script_getstr(st,3); - + if( *atcmd == atcommand->at_symbol || *atcmd == atcommand->char_symbol ) atcmd++; - + if( script_hasdata(st,4) ) group_lv = script_getnum(st,4); if( script_hasdata(st,5) ) group_lv_char = script_getnum(st,5); if( script_hasdata(st,6) ) log = script_getnum(st,6) ? true : false; - + if( atcommand->binding_count == 0 ) { CREATE(atcommand->binding,struct atcmd_binding_data*,1); - + create = true; } else { ARR_FIND(0, atcommand->binding_count, i, strcmp(atcommand->binding[i]->command,atcmd) == 0); @@ -16671,39 +17483,39 @@ BUILDIN(bindatcmd) { } else create = true; } - + if( create ) { i = atcommand->binding_count; - + if( atcommand->binding_count++ != 0 ) RECREATE(atcommand->binding,struct atcmd_binding_data*,atcommand->binding_count); - + CREATE(atcommand->binding[i],struct atcmd_binding_data,1); - + safestrncpy(atcommand->binding[i]->command, atcmd, 50); safestrncpy(atcommand->binding[i]->npc_event, eventName, 50); atcommand->binding[i]->group_lv = group_lv; atcommand->binding[i]->group_lv_char = group_lv_char; atcommand->binding[i]->log = log; } - + return true; } BUILDIN(unbindatcmd) { const char* atcmd; int i = 0; - + atcmd = script_getstr(st, 2); - + if( *atcmd == atcommand->at_symbol || *atcmd == atcommand->char_symbol ) atcmd++; - + if( atcommand->binding_count == 0 ) { script_pushint(st, 0); return true; } - + ARR_FIND(0, atcommand->binding_count, i, strcmp(atcommand->binding[i]->command, atcmd) == 0); if( i < atcommand->binding_count ) { int cursor = 0; @@ -16713,70 +17525,67 @@ BUILDIN(unbindatcmd) { for( i = 0, cursor = 0; i < atcommand->binding_count; i++ ) { if( atcommand->binding[i] == NULL ) continue; - + if( cursor != i ) { memmove(&atcommand->binding[cursor], &atcommand->binding[i], sizeof(struct atcmd_binding_data*)); } - + cursor++; } - + if( (atcommand->binding_count = cursor) == 0 ) aFree(atcommand->binding); - + script_pushint(st, 1); } else script_pushint(st, 0);/* not found */ - + return true; } -BUILDIN(useatcmd) -{ - TBL_PC dummy_sd; - TBL_PC* sd; +BUILDIN(useatcmd) { + TBL_PC *sd, *dummy_sd = NULL; int fd; const char* cmd; - + cmd = script_getstr(st,2); - - if( st->rid ) - { - sd = script_rid2sd(st); + + if( st->rid ) { + sd = script->rid2sd(st); + if( sd == NULL ) + return false; fd = sd->fd; - } - else - { // Use a dummy character. - sd = &dummy_sd; + } else { + // Use a dummy character. + sd = dummy_sd = pc->get_dummy_sd(); fd = 0; - - memset(&dummy_sd, 0, sizeof(TBL_PC)); - if( st->oid ) - { - struct block_list* bl = iMap->id2bl(st->oid); - memcpy(&dummy_sd.bl, bl, sizeof(struct block_list)); + + if( st->oid ) { + struct block_list* bl = map->id2bl(st->oid); + memcpy(&sd->bl, bl, sizeof(struct block_list)); if( bl->type == BL_NPC ) - safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH); + safestrncpy(sd->status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH); } } - + // compatibility with previous implementation (deprecated!) if( cmd[0] != atcommand->at_symbol ) { cmd += strlen(sd->status.name); while( *cmd != atcommand->at_symbol && *cmd != 0 ) cmd++; } - - atcommand->parse(fd, sd, cmd, 1); + + atcommand->exec(fd, sd, cmd, true); + if (dummy_sd) aFree(dummy_sd); return true; } BUILDIN(checkre) { int num; - + num=script_getnum(st,2); - switch(num){ + switch(num) { case 0: #ifdef RENEWAL script_pushint(st, 1); @@ -16833,112 +17642,112 @@ BUILDIN(checkre) return true; } -/* getrandgroupitem <group_id>,<quantity> */ +/* getrandgroupitem <container_item_id>,<quantity> */ BUILDIN(getrandgroupitem) { - TBL_PC* sd; - int i, get_count = 0, flag, nameid, group = script_getnum(st, 2), qty = script_getnum(st,3); - struct item item_tmp; - - if( !( sd = script_rid2sd(st) ) ) - return true; - - if( qty <= 0 ) { - ShowError("getrandgroupitem: qty is <= 0!\n"); - return false; - } - - if(group < 1 || group >= MAX_ITEMGROUP) { - ShowError("getrandgroupitem: Invalid group id %d\n", group); - return false; - } - if (!itemgroup_db[group].qty) { - ShowError("getrandgroupitem: group id %d is empty!\n", group); - return false; - } - - nameid = itemdb_searchrandomid(group); - memset(&item_tmp,0,sizeof(item_tmp)); - - item_tmp.nameid = nameid; - item_tmp.identify = itemdb_isidentified(nameid); - - //Check if it's stackable. - if (!itemdb_isstackable(nameid)) - get_count = 1; - else - get_count = qty; - - for (i = 0; i < qty; i += get_count) { - // if not pet egg - if (!pet_create_egg(sd, nameid)) { - if ((flag = pc->additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT))) { - clif->additem(sd, 0, 0, flag); - if( pc->candrop(sd,&item_tmp) ) - iMap->addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0); + struct item_data *data = NULL; + struct map_session_data *sd = NULL; + int nameid = script_getnum(st, 2); + int count = script_getnum(st, 3); + + if( !(data = itemdb->exists(nameid)) ) { + ShowWarning("buildin_getrandgroupitem: unknown item id %d\n",nameid); + script_pushint(st, 1); + } else if ( count <= 0 ) { + ShowError("buildin_getrandgroupitem: qty is <= 0!\n"); + script_pushint(st, 1); + } else if ( !data->group ) { + ShowWarning("buildin_getrandgroupitem: item '%s' (%d) isn't a group!\n",data->name,nameid); + script_pushint(st, 1); + } else if( !( sd = script->rid2sd(st) ) ) { + ShowWarning("buildin_getrandgroupitem: no player attached!! (item %s (%d))\n",data->name,nameid); + script_pushint(st, 1); + } else { + int i, get_count, flag; + struct item it; + + memset(&it,0,sizeof(it)); + + nameid = itemdb->group_item(data->group); + + it.nameid = nameid; + it.identify = itemdb->isidentified(nameid); + + if (!itemdb->isstackable(nameid)) + get_count = 1; + else + get_count = count; + + for (i = 0; i < count; i += get_count) { + // if not pet egg + if (!pet->create_egg(sd, nameid)) { + if ((flag = pc->additem(sd, &it, get_count, LOG_TYPE_SCRIPT))) { + clif->additem(sd, 0, 0, flag); + if( pc->candrop(sd,&it) ) + map->addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0); + } } } + + script_pushint(st, 0); } - + return true; } /* cleanmap <map_name>; * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */ -static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap) -{ +int script_cleanfloor_sub(struct block_list *bl, va_list ap) { nullpo_ret(bl); - iMap->clearflooritem(bl); - + map->clearflooritem(bl); + return 0; } -BUILDIN(cleanmap) -{ - const char *map; - int16 m = -1; - int16 x0 = 0, y0 = 0, x1 = 0, y1 = 0; - - map = script_getstr(st, 2); - m = iMap->mapname2mapid(map); - if (!m) - return false; - +BUILDIN(cleanmap) { + const char *mapname; + int16 m = -1; + int16 x0 = 0, y0 = 0, x1 = 0, y1 = 0; + + mapname = script_getstr(st, 2); + m = map->mapname2mapid(mapname); + if ( m == -1 ) + return false; + if ((script_lastdata(st) - 2) < 4) { - iMap->foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM); + map->foreachinmap(script->cleanfloor_sub, m, BL_ITEM); } else { x0 = script_getnum(st, 3); y0 = script_getnum(st, 4); x1 = script_getnum(st, 5); y1 = script_getnum(st, 6); if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) { - iMap->foreachinarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM); + map->foreachinarea(script->cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM); } else { ShowError("cleanarea: invalid coordinate defined!\n"); return false; } } - + return true; } /* Cast a skill on the attached player. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>; * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */ -BUILDIN(npcskill) -{ +BUILDIN(npcskill) { uint16 skill_id; unsigned short skill_level; unsigned int stat_point; unsigned int npc_level; struct npc_data *nd; struct map_session_data *sd; - - skill_id = script_isstring(st, 2) ? skill->name2id(script_getstr(st, 2)) : script_getnum(st, 2); - skill_level = script_getnum(st, 3); - stat_point = script_getnum(st, 4); - npc_level = script_getnum(st, 5); - sd = script_rid2sd(st); - nd = (struct npc_data *)iMap->id2bl(sd->npc_id); - + + skill_id = script_isstringtype(st, 2) ? skill->name2id(script_getstr(st, 2)) : script_getnum(st, 2); + skill_level = script_getnum(st, 3); + stat_point = script_getnum(st, 4); + npc_level = script_getnum(st, 5); + sd = script->rid2sd(st); + nd = (struct npc_data *)map->id2bl(sd->npc_id); + if (stat_point > battle_config.max_third_parameter) { ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter ); return false; @@ -16947,112 +17756,190 @@ BUILDIN(npcskill) ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL); return false; } - if (sd == NULL || nd == NULL) { //ain't possible, but I don't trust people. + if (sd == NULL || nd == NULL) { return false; } - + nd->level = npc_level; nd->stat_point = stat_point; - + if (!nd->status.hp) { - status_calc_npc(nd, true); + status_calc_npc(nd, SCO_FIRST); } else { - status_calc_npc(nd, false); + status_calc_npc(nd, SCO_NONE); } - + if (skill->get_inf(skill_id)&INF_GROUND_SKILL) { - unit_skilluse_pos(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level); + unit->skilluse_pos(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level); } else { - unit_skilluse_id(&nd->bl, sd->bl.id, skill_id, skill_level); + unit->skilluse_id(&nd->bl, sd->bl.id, skill_id, skill_level); + } + + return true; +} + +/* Turns a player into a monster and grants SC attribute effect. [malufett/Hercules] + * montransform <monster name/id>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>; */ +BUILDIN(montransform) { + int tick; + enum sc_type type; + struct block_list* bl; + char msg[CHAT_SIZE_MAX]; + int mob_id, val1, val2, val3, val4; + + if( (bl = map->id2bl(st->rid)) == NULL ) + return true; + + if( script_isstringtype(st, 2) ) { + mob_id = mob->db_searchname(script_getstr(st, 2)); + } else { + mob_id = mob->db_checkid(script_getnum(st, 2)); + } + + if( mob_id == 0 ) { + if( script_isstringtype(st, 2) ) + ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2)); + else + ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2)); + return false; + } + + tick = script_getnum(st, 3); + type = (sc_type)script_getnum(st, 4); + val1 = val2 = val3 = val4 = 0; + + if( !(type > SC_NONE && type < SC_MAX) ) { + ShowWarning("buildin_montransform: Unsupported status change id %d\n", type); + return false; + } + + if (script_hasdata(st, 5)) + val1 = script_getnum(st, 5); + + if (script_hasdata(st, 6)) + val2 = script_getnum(st, 6); + + if (script_hasdata(st, 7)) + val3 = script_getnum(st, 7); + + if (script_hasdata(st, 8)) + val4 = script_getnum(st, 8); + + if( tick != 0 ) { + struct map_session_data *sd = map->id2sd(bl->id); + struct mob_db *monster = mob->db(mob_id); + + if( !sd ) + return true; + + if( battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m) ) { + clif->message(sd->fd, msg_txt(1488)); // Transforming into monster is not allowed in Guild Wars. + return true; + } + + if( sd->disguise != -1 ) { + clif->message(sd->fd, msg_txt(1486)); // Cannot transform into monster while in disguise. + return true; + } + + sprintf(msg, msg_txt(1485), monster->name); // Traaaansformation-!! %s form!! + clif->ShowScript(&sd->bl, msg); + status_change_end(bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous + sc_start2(NULL, bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick); + sc_start4(NULL, bl, type, 100, val1, val2, val3, val4, tick); } - return true; } + struct hQueue *script_hqueue_get(int idx) { - if( idx < 0 || idx >= script->hqs || script->hq[idx].items == -1 ) + if( idx < 0 || idx >= script->hqs || script->hq[idx].size == -1 ) return NULL; return &script->hq[idx]; } -/* set .@id,queue(); */ -/* creates queue, returns created queue id */ -BUILDIN(queue) { +int script_hqueue_create(void) { int idx = script->hqs; int i; - + for(i = 0; i < script->hqs; i++) { - if( script->hq[i].items == -1 ) { + if( script->hq[i].size == -1 ) { break; } } - + if( i == script->hqs ) { RECREATE(script->hq, struct hQueue, ++script->hqs); script->hq[ idx ].item = NULL; } else idx = i; - + script->hq[ idx ].id = idx; + script->hq[ idx ].size = 0; script->hq[ idx ].items = 0; script->hq[ idx ].onDeath[0] = '\0'; script->hq[ idx ].onLogOut[0] = '\0'; script->hq[ idx ].onMapChange[0] = '\0'; - - script_pushint(st,idx); + return idx; +} +/* set .@id,queue(); */ +/* creates queue, returns created queue id */ +BUILDIN(queue) { + script_pushint(st,script->queue_create()); return true; } /* set .@length,queuesize(.@queue_id); */ /* returns queue length */ BUILDIN(queuesize) { int idx = script_getnum(st, 2); - - if( idx < 0 || idx >= script->hqs || script->hq[idx].items == -1 ) { + + if( idx < 0 || idx >= script->hqs || script->hq[idx].size == -1 ) { ShowWarning("buildin_queuesize: unknown queue id %d\n",idx); script_pushint(st, 0); - } else - script_pushint(st, script->hq[ idx ].items ); - + } else { + script_pushint(st, script->hq[ idx ].items); + } + return true; } bool script_hqueue_add(int idx, int var) { - if( idx < 0 || idx >= script->hqs || script->hq[idx].items == -1 ) { + if( idx < 0 || idx >= script->hqs || script->hq[idx].size == -1 ) { ShowWarning("script_hqueue_add: unknown queue id %d\n",idx); return true; } else { struct map_session_data *sd; int i; - - for(i = 0; i < script->hq[idx].items; i++) { + + for(i = 0; i < script->hq[idx].size; i++) { if( script->hq[idx].item[i] == var ) { return true; } } - - if( i == script->hq[idx].items ) { - - for(i = 0; i < script->hq[idx].items; i++) { + + if( i == script->hq[idx].size ) { + + for(i = 0; i < script->hq[idx].size; i++) { if( script->hq[idx].item[i] == 0 ) { break; } } - - if( i == script->hq[idx].items ) - RECREATE(script->hq[idx].item, int, ++script->hq[idx].items); - + + if( i == script->hq[idx].size ) + RECREATE(script->hq[idx].item, int, ++script->hq[idx].size); + script->hq[idx].item[i] = var; - - if( var >= START_ACCOUNT_NUM && (sd = iMap->id2sd(var)) ) { + script->hq[idx].items++; + if( var >= START_ACCOUNT_NUM && (sd = map->id2sd(var)) ) { for(i = 0; i < sd->queues_count; i++) { if( sd->queues[i] == -1 ) { break; } } - + if( i == sd->queues_count ) RECREATE(sd->queues, int, ++sd->queues_count); - + sd->queues[i] = idx; } - + } } return false; @@ -17062,39 +17949,41 @@ bool script_hqueue_add(int idx, int var) { BUILDIN(queueadd) { int idx = script_getnum(st, 2); int var = script_getnum(st, 3); - + script_pushint(st,script->queue_add(idx,var)?1:0); - + return true; } bool script_hqueue_remove(int idx, int var) { - if( idx < 0 || idx >= script->hqs || script->hq[idx].items == -1 ) { + if( idx < 0 || idx >= script->hqs || script->hq[idx].size == -1 ) { ShowWarning("script_hqueue_remove: unknown queue id %d (used with var %d)\n",idx,var); return true; } else { int i; - - for(i = 0; i < script->hq[idx].items; i++) { + + for(i = 0; i < script->hq[idx].size; i++) { if( script->hq[idx].item[i] == var ) { - return true; + break; } } - - if( i != script->hq[idx].items ) { + + if( i != script->hq[idx].size ) { struct map_session_data *sd; - script->hq[idx].item[i] = 0; - - if( var >= START_ACCOUNT_NUM && (sd = iMap->id2sd(var)) ) { + + script->hq[idx].item[i] = -1; + script->hq[idx].items--; + + if( var >= START_ACCOUNT_NUM && (sd = map->id2sd(var)) ) { for(i = 0; i < sd->queues_count; i++) { - if( sd->queues[i] == var ) { + if( sd->queues[i] == idx ) { break; } } - + if( i != sd->queues_count ) sd->queues[i] = -1; } - + } } return false; @@ -17106,7 +17995,7 @@ BUILDIN(queueremove) { int var = script_getnum(st, 3); script_pushint(st, script->queue_remove(idx,var)?1:0); - + return true; } @@ -17117,8 +18006,8 @@ BUILDIN(queueremove) { BUILDIN(queueopt) { int idx = script_getnum(st, 2); int var = script_getnum(st, 3); - - if( idx < 0 || idx >= script->hqs || script->hq[idx].items == -1 ) { + + if( idx < 0 || idx >= script->hqs || script->hq[idx].size == -1 ) { ShowWarning("buildin_queueopt: unknown queue id %d\n",idx); script_pushint(st, 1); } else if( var <= HQO_NONE || var >= HQO_MAX ) { @@ -17150,32 +18039,34 @@ BUILDIN(queueopt) { break; } } - + return true; } bool script_hqueue_del(int idx) { - if( idx < 0 || idx >= script->hqs || script->hq[idx].items == -1 ) { + if( idx < 0 || idx >= script->hqs || script->hq[idx].size == -1 ) { ShowWarning("script_queue_del: unknown queue id %d\n",idx); return true; } else { struct map_session_data *sd; int i; - - for(i = 0; i < script->hq[idx].items; i++) { - if( script->hq[idx].item[i] >= START_ACCOUNT_NUM && (sd = iMap->id2sd(script->hq[idx].item[i])) ) { + + for(i = 0; i < script->hq[idx].size; i++) { + if( script->hq[idx].item[i] >= START_ACCOUNT_NUM && (sd = map->id2sd(script->hq[idx].item[i])) ) { int j; for(j = 0; j < sd->queues_count; j++) { if( sd->queues[j] == script->hq[idx].item[i] ) { break; } } - + if( j != sd->queues_count ) sd->queues[j] = -1; } + script->hq[idx].item[i] = 0; } - - script->hq[idx].items = -1; + + script->hq[idx].size = -1; + script->hq[idx].items = 0; } return false; } @@ -17183,12 +18074,39 @@ bool script_hqueue_del(int idx) { /* deletes queue of id .@queue_id, returns 1 if id not found, 0 otherwise */ BUILDIN(queuedel) { int idx = script_getnum(st, 2); - + script_pushint(st,script->queue_del(idx)?1:0); - + return true; } +void script_hqueue_clear(int idx) { + if( idx < 0 || idx >= script->hqs || script->hq[idx].size == -1 ) { + ShowWarning("script_hqueue_clear: unknown queue id %d\n",idx); + return; + } else { + struct map_session_data *sd; + int i, j; + + for(i = 0; i < script->hq[idx].size; i++) { + if( script->hq[idx].item[i] > 0 ) { + + if( script->hq[idx].item[i] >= START_ACCOUNT_NUM && (sd = map->id2sd(script->hq[idx].item[i])) ) { + for(j = 0; j < sd->queues_count; j++) { + if( sd->queues[j] == idx ) { + break; + } + } + if( j != sd->queues_count ) + sd->queues[j] = -1; + } + script->hq[idx].item[i] = 0; + } + } + script->hq[idx].items = 0; + } + return; +} /* set .@id, queueiterator(.@queue_id); */ /* creates a new queue iterator, returns its id */ BUILDIN(queueiterator) { @@ -17196,30 +18114,37 @@ BUILDIN(queueiterator) { struct hQueue *queue = NULL; int idx = script->hqis; int i; - - if( qid < 0 || qid >= script->hqs || script->hq[idx].items == -1 || !(queue = script->queue(qid)) ) { + + if( qid < 0 || qid >= script->hqs || script->hq[qid].size == -1 || !(queue = script->queue(qid)) ) { ShowWarning("queueiterator: invalid queue id %d\n",qid); return true; } - + + /* what if queue->size is 0? (iterating a empty queue?) */ + if( queue->size <= 0 ) { + ShowWarning("queueiterator: attempting to iterate on on empty queue id %d!\n",qid); + return true; + } + for(i = 0; i < script->hqis; i++) { if( script->hqi[i].items == -1 ) { break; } } - - if( i == script->hqis ) + + if( i == script->hqis ) { RECREATE(script->hqi, struct hQueueIterator, ++script->hqis); - else + script->hqi[ idx ].item = NULL; + } else idx = i; - - RECREATE(script->hqi[ idx ].item, int, queue->items); - - memcpy(&script->hqi[idx].item, &queue->item, sizeof(int)*queue->items); - - script->hqi[ idx ].items = queue->items; + + RECREATE(script->hqi[ idx ].item, int, queue->size); + + memcpy(script->hqi[idx].item, queue->item, sizeof(int)*queue->size); + + script->hqi[ idx ].items = queue->size; script->hqi[ idx ].pos = 0; - + script_pushint(st,idx); return true; } @@ -17227,11 +18152,11 @@ BUILDIN(queueiterator) { /* returns next/first member in the iterator, 0 if none */ BUILDIN(qiget) { int idx = script_getnum(st, 2); - + if( idx < 0 || idx >= script->hqis ) { ShowWarning("buildin_qiget: unknown queue iterator id %d\n",idx); script_pushint(st, 0); - } else if ( script->hqi[idx].pos == script->hqi[idx].items ) { + } else if ( script->hqi[idx].pos -1 == script->hqi[idx].items ) { script_pushint(st, 0); } else { struct hQueueIterator *it = &script->hqi[idx]; @@ -17244,22 +18169,22 @@ BUILDIN(qiget) { /* returns 1:0 if there is a next member in the iterator */ BUILDIN(qicheck) { int idx = script_getnum(st, 2); - + if( idx < 0 || idx >= script->hqis ) { ShowWarning("buildin_qicheck: unknown queue iterator id %d\n",idx); script_pushint(st, 0); - } else if ( script->hqi[idx].pos == script->hqi[idx].items ) { + } else if ( script->hqi[idx].pos -1 == script->hqi[idx].items ) { script_pushint(st, 0); } else { script_pushint(st, 1); } - + return true; } /* Queue Iterator Check */ BUILDIN(qiclear) { int idx = script_getnum(st, 2); - + if( idx < 0 || idx >= script->hqis ) { ShowWarning("buildin_qiclear: unknown queue iterator id %d\n",idx); script_pushint(st, 1); @@ -17267,57 +18192,589 @@ BUILDIN(qiclear) { script->hqi[idx].items = -1; script_pushint(st, 0); } - + + return true; +} +/** + * packageitem({<optional container_item_id>}) + * when no item id is provided it tries to assume it comes from the current item id being processed (if any) + **/ +BUILDIN(packageitem) { + struct item_data *data = NULL; + struct map_session_data *sd = NULL; + int nameid; + + if( script_hasdata(st, 2) ) + nameid = script_getnum(st, 2); + else if ( script->current_item_id ) + nameid = script->current_item_id; + else { + ShowWarning("buildin_packageitem: no item id provided and no item attached\n"); + script_pushint(st, 1); + return true; + } + + if( !(data = itemdb->exists(nameid)) ) { + ShowWarning("buildin_packageitem: unknown item id %d\n",nameid); + script_pushint(st, 1); + } else if ( !data->package ) { + ShowWarning("buildin_packageitem: item '%s' (%d) isn't a package!\n",data->name,nameid); + script_pushint(st, 1); + } else if( !( sd = script->rid2sd(st) ) ) { + ShowWarning("buildin_packageitem: no player attached!! (item %s (%d))\n",data->name,nameid); + script_pushint(st, 1); + } else { + itemdb->package_item(sd,data->package); + script_pushint(st, 0); + } + + return true; +} +/* New Battlegrounds Stuff */ +/* bg_team_create(map_name,respawn_x,respawn_y) */ +/* returns created team id or -1 when fails */ +BUILDIN(bg_create_team) { + const char *map_name, *ev = "", *dev = "";//ev and dev will be dropped. + int x, y, map_index = 0, bg_id; + + map_name = script_getstr(st,2); + if( strcmp(map_name,"-") != 0 ) { + map_index = script->mapindexname2id(st,map_name); + if( map_index == 0 ) { // Invalid Map + script_pushint(st,0); + return true; + } + } + + x = script_getnum(st,3); + y = script_getnum(st,4); + + if( (bg_id = bg->create(map_index, x, y, ev, dev)) == 0 ) { // Creation failed + script_pushint(st,-1); + } else + script_pushint(st,bg_id); + + return true; + +} +/* bg_join_team(team_id{,optional account id}) */ +/* when account id is not present it tries to autodetect from the attached player (if any) */ +/* returns 0 when successful, 1 otherwise */ +BUILDIN(bg_join_team) { + struct map_session_data *sd; + int team_id = script_getnum(st, 2); + + if( script_hasdata(st, 3) ) + sd = map->id2sd(script_getnum(st, 3)); + else + sd = script->rid2sd(st); + + if( !sd ) + script_pushint(st, 1); + else + script_pushint(st,bg->team_join(team_id, sd)?0:1); + + return true; +} +/*==============[Mhalicot]================== + * countbound {<type>}; + * Creates an array of bounded item IDs + * Returns amount of items found + * Type: + * 1 - Account Bound + * 2 - Guild Bound + * 3 - Party Bound + * 4 - Character Bound + *------------------------------------------*/ +BUILDIN(countbound) +{ + int i, type, j=0, k=0; + TBL_PC *sd; + + if( (sd = script->rid2sd(st)) == NULL ) + return false; + + type = script_hasdata(st,2)?script_getnum(st,2):0; + + for(i=0;i<MAX_INVENTORY;i++) { + if(sd->status.inventory[i].nameid > 0 && ( + (!type && sd->status.inventory[i].bound > 0) || + (type && sd->status.inventory[i].bound == type) + )) { + pc->setreg(sd,reference_uid(script->add_str("@bound_items"), k),sd->status.inventory[i].nameid); + k++; + j += sd->status.inventory[i].amount; + } + } + + script_pushint(st,j); + return 0; +} + +/* bg_match_over( arena_name {, optional canceled } ) */ +/* returns 0 when successful, 1 otherwise */ +BUILDIN(bg_match_over) { + bool canceled = script_hasdata(st,3) ? true : false; + struct bg_arena *arena = bg->name2arena((char*)script_getstr(st, 2)); + + if( arena ) { + bg->match_over(arena,canceled); + script_pushint(st, 0); + } else + script_pushint(st, 1); + + return true; +} + +BUILDIN(instance_mapname) { + const char *map_name; + int m; + short instance_id = -1; + + map_name = script_getstr(st,2); + + if( script_hasdata(st,3) ) + instance_id = script_getnum(st,3); + else + instance_id = st->instance_id; + + // Check that instance mapname is a valid map + if( instance_id == -1 || (m = instance->mapname2imap(map_name,instance_id)) == -1 ) + script_pushconststr(st, ""); + else + script_pushconststr(st, map->list[m].name); + + return true; +} +/* modify an instances' reload-spawn point */ +/* instance_set_respawn <map_name>,<x>,<y>{,<instance_id>} */ +/* returns 1 when successful, 0 otherwise. */ +BUILDIN(instance_set_respawn) { + const char *map_name; + short instance_id = -1; + short mid; + short x,y; + + map_name = script_getstr(st,2); + x = script_getnum(st, 3); + y = script_getnum(st, 4); + + if( script_hasdata(st, 5) ) + instance_id = script_getnum(st, 5); + else + instance_id = st->instance_id; + + if( instance_id == -1 || !instance->valid(instance_id) ) + script_pushint(st, 0); + else if( (mid = map->mapname2mapid(map_name)) == -1 ) { + ShowError("buildin_instance_set_respawn: unknown map '%s'\n",map_name); + script_pushint(st, 0); + } else { + int i; + + for(i = 0; i < instance->list[instance_id].num_map; i++) { + if( map->list[instance->list[instance_id].map[i]].m == mid ) { + instance->list[instance_id].respawn.map = map_id2index(mid); + instance->list[instance_id].respawn.x = x; + instance->list[instance_id].respawn.y = y; + break; + } + } + + if( i != instance->list[instance_id].num_map ) + script_pushint(st, 1); + else { + ShowError("buildin_instance_set_respawn: map '%s' not part of instance '%s'\n",map_name,instance->list[instance_id].name); + script_pushint(st, 0); + } + } + + + return true; +} +/** + * @call openshop({NPC Name}); + * + * @return 1 on success, 0 otherwise. + **/ +BUILDIN(openshop) { + struct npc_data *nd; + struct map_session_data *sd; + const char *name = NULL; + + if( script_hasdata(st, 2) ) { + name = script_getstr(st, 2); + if( !(nd = npc->name2id(name)) || nd->subtype != SCRIPT ) { + ShowWarning("buildin_openshop(\"%s\"): trying to run without a proper NPC!\n",name); + return false; + } + } else if( !(nd = map->id2nd(st->oid)) ) { + ShowWarning("buildin_openshop: trying to run without a proper NPC!\n"); + return false; + } + if( !( sd = script->rid2sd(st) ) ) { + ShowWarning("buildin_openshop: trying to run without a player attached!\n"); + return false; + } else if ( !nd->u.scr.shop || !nd->u.scr.shop->items ) { + ShowWarning("buildin_openshop: trying to open without any items!\n"); + return false; + } + + if( !npc->trader_open(sd,nd) ) + script_pushint(st, 0); + else + script_pushint(st, 1); + + return true; +} +/** + * @call sellitem <Item_ID>,{,price{,qty}}; + * + * adds <Item_ID> (or modifies if present) to shop + * if price not provided (or -1) uses the item's value_sell + **/ +BUILDIN(sellitem) { + struct npc_data *nd; + struct item_data *it; + int i = 0, id = script_getnum(st,2); + int value = 0; + int qty = 0; + + if( !(nd = map->id2nd(st->oid)) ) { + ShowWarning("buildin_sellitem: trying to run without a proper NPC!\n"); + return false; + } else if ( !(it = itemdb->exists(id)) ) { + ShowWarning("buildin_sellitem: unknown item id '%d'!\n",id); + return false; + } + + value = script_hasdata(st,3) ? script_getnum(st, 3) : it->value_buy; + if( value == -1 ) + value = it->value_buy; + + if( !nd->u.scr.shop ) + npc->trader_update(nd->src_id?nd->src_id:nd->bl.id); + else {/* no need to run this if its empty */ + for( i = 0; i < nd->u.scr.shop->items; i++ ) { + if( nd->u.scr.shop->item[i].nameid == id ) + break; + } + } + + if( nd->u.scr.shop->type == NST_MARKET ) { + if( !script_hasdata(st,4) || ( qty = script_getnum(st, 4) ) <= 0 ) { + ShowError("buildin_sellitem: invalid 'qty' for market-type shop!\n"); + return false; + } + } + + if( ( nd->u.scr.shop->type == NST_ZENY || nd->u.scr.shop->type == NST_MARKET ) && value*0.75 < it->value_sell*1.24 ) { + ShowWarning("buildin_sellitem: Item %s [%d] discounted buying price (%d->%d) is less than overcharged selling price (%d->%d) in NPC %s (%s)\n", + it->name, id, value, (int)(value*0.75), it->value_sell, (int)(it->value_sell*1.24), nd->exname, nd->path); + } + + if( i != nd->u.scr.shop->items ) { + nd->u.scr.shop->item[i].value = value; + nd->u.scr.shop->item[i].qty = qty; + if( nd->u.scr.shop->type == NST_MARKET ) /* has been manually updated, make it reflect on sql */ + npc->market_tosql(nd,i); + } else { + for( i = 0; i < nd->u.scr.shop->items; i++ ) { + if( nd->u.scr.shop->item[i].nameid == 0 ) + break; + } + + if( i == nd->u.scr.shop->items ) { + if( nd->u.scr.shop->items == USHRT_MAX ) { + ShowWarning("buildin_sellitem: Can't add %s (%s/%s), shop list is full!\n", it->name, nd->exname, nd->path); + return false; + } + i = nd->u.scr.shop->items; + RECREATE(nd->u.scr.shop->item, struct npc_item_list, ++nd->u.scr.shop->items); + } + + nd->u.scr.shop->item[i].nameid = it->nameid; + nd->u.scr.shop->item[i].value = value; + nd->u.scr.shop->item[i].qty = qty; + } + + return true; +} +/** + * @call stopselling <Item_ID>; + * + * removes <Item_ID> from the current npc shop + * + * @return 1 on success, 0 otherwise + **/ +BUILDIN(stopselling) { + struct npc_data *nd; + int i, id = script_getnum(st,2); + + if( !(nd = map->id2nd(st->oid)) || !nd->u.scr.shop ) { + ShowWarning("buildin_stopselling: trying to run without a proper NPC!\n"); + return false; + } + + for( i = 0; i < nd->u.scr.shop->items; i++ ) { + if( nd->u.scr.shop->item[i].nameid == id ) + break; + } + + if( i != nd->u.scr.shop->items ) { + int cursor; + + if( nd->u.scr.shop->type == NST_MARKET ) + npc->market_delfromsql(nd,i); + + nd->u.scr.shop->item[i].nameid = 0; + nd->u.scr.shop->item[i].value = 0; + nd->u.scr.shop->item[i].qty = 0; + + for( i = 0, cursor = 0; i < nd->u.scr.shop->items; i++ ) { + if( nd->u.scr.shop->item[i].nameid == 0 ) + continue; + + if( cursor != i ) { + nd->u.scr.shop->item[cursor].nameid = nd->u.scr.shop->item[i].nameid; + nd->u.scr.shop->item[cursor].value = nd->u.scr.shop->item[i].value; + nd->u.scr.shop->item[cursor].qty = nd->u.scr.shop->item[i].qty; + } + + cursor++; + } + + script_pushint(st, 1); + } else + script_pushint(st, 0); + + return true; +} +/** + * @call setcurrency <Val1>{,<Val2>}; + * + * updates currently-attached player shop currency + **/ +/* setcurrency(<Val1>,{<Val2>}) */ +BUILDIN(setcurrency) { + int val1 = script_getnum(st,2), + val2 = script_hasdata(st, 3) ? script_getnum(st,3) : 0; + struct npc_data *nd; + + if( !(nd = map->id2nd(st->oid)) ) { + ShowWarning("buildin_setcurrency: trying to run without a proper NPC!\n"); + return false; + } + + npc->trader_funds[0] = val1; + npc->trader_funds[1] = val2; + + return true; +} +/** + * @call tradertype(<type>); + * + * defaults to 0, so no need to call when you're doing zeny + * check enum npc_shop_types for list + * cleans shop list on use + **/ +BUILDIN(tradertype) { + int type = script_getnum(st, 2); + struct npc_data *nd; + + if( !(nd = map->id2nd(st->oid)) ) { + ShowWarning("buildin_tradertype: trying to run without a proper NPC!\n"); + return false; + } else if ( type < 0 || type > NST_MAX ) { + ShowWarning("buildin_tradertype: invalid type param %d!\n",type); + return false; + } + + if( !nd->u.scr.shop ) + npc->trader_update(nd->src_id?nd->src_id:nd->bl.id); + else {/* clear list */ + int i; + for( i = 0; i < nd->u.scr.shop->items; i++ ) { + nd->u.scr.shop->item[i].nameid = 0; + nd->u.scr.shop->item[i].value = 0; + nd->u.scr.shop->item[i].qty = 0; + } + npc->market_delfromsql(nd,USHRT_MAX); + } + +#if PACKETVER < 20131223 + if( type == NST_MARKET ) { + ShowWarning("buildin_tradertype: NST_MARKET is only available with PACKETVER 20131223 or newer!\n"); + script->reportsrc(st); + } +#endif + + nd->u.scr.shop->type = type; + + return true; +} +/** + * @call purchaseok(); + * + * signs the transaction can proceed + **/ +BUILDIN(purchaseok) { + struct npc_data *nd; + + if( !(nd = map->id2nd(st->oid)) || !nd->u.scr.shop ) { + ShowWarning("buildin_purchaseok: trying to run without a proper NPC!\n"); + return false; + } + + npc->trader_ok = true; + + return true; +} +/** + * @call shopcount(<Item_ID>); + * + * @return number of available items in the script's attached shop + **/ +BUILDIN(shopcount) { + struct npc_data *nd; + int id = script_getnum(st, 2); + unsigned short i; + + if( !(nd = map->id2nd(st->oid)) ) { + ShowWarning("buildin_shopcount(%d): trying to run without a proper NPC!\n",id); + return false; + } else if ( !nd->u.scr.shop || !nd->u.scr.shop->items ) { + ShowWarning("buildin_shopcount(%d): trying to use without any items!\n",id); + return false; + } else if ( nd->u.scr.shop->type != NST_MARKET ) { + ShowWarning("buildin_shopcount(%d): trying to use on a non-NST_MARKET shop!\n",id); + return false; + } + + /* lookup */ + for(i = 0; i < nd->u.scr.shop->items; i++) { + if( nd->u.scr.shop->item[i].nameid == id ) { + script_pushint(st, nd->u.scr.shop->item[i].qty); + break; + } + } + + /* didn't find it */ + if( i == nd->u.scr.shop->items ) + script_pushint(st, 0); + return true; } // declarations that were supposed to be exported from npc_chat.c #ifdef PCRE_SUPPORT - BUILDIN(defpattern); - BUILDIN(activatepset); - BUILDIN(deactivatepset); - BUILDIN(deletepset); +BUILDIN(defpattern); +BUILDIN(activatepset); +BUILDIN(deactivatepset); +BUILDIN(deletepset); + +BUILDIN(pcre_match) { + const char *input = script_getstr(st, 2); + const char *regex = script_getstr(st, 3); + + script->op_2str(st, C_RE_EQ, input, regex); + return true; +} #endif -bool script_hp_add(char *name, char *args, bool (*func)(struct script_state *st)) { - int n = add_str(name), i = 0; - - if( script->str_data[n].type == C_FUNC ) { - script->str_data[n].func = func; - i = script->str_data[n].val; - if( args ) { - int slen = strlen(args); - if( script->buildin[i] ) { - aFree(script->buildin[i]); - } - CREATE(script->buildin[i], char, slen + 1); - safestrncpy(script->buildin[i], args, slen + 1); - } else { - if( script->buildin[i] ) - aFree(script->buildin[i]); - script->buildin[i] = NULL; +/** + * Adds a built-in script function. + * + * @param buildin Script function data + * @param force Whether to override an existing function with the same name + * (i.e. a plugin overriding a built-in function) + * @return Whether the function was successfully added. + */ +bool script_add_builtin(const struct script_function *buildin, bool override) { + int n = 0, offset = 0; + size_t slen; + if( !buildin ) { + return false; + } + if( buildin->arg ) { + // arg must follow the pattern: (v|s|i|r|l)*\?*\*? + // 'v' - value (either string or int or reference) + // 's' - string + // 'i' - int + // 'r' - reference (of a variable) + // 'l' - label + // '?' - one optional parameter + // '*' - unknown number of optional parameters + char *p = buildin->arg; + while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p; + while( *p == '?' ) ++p; + if( *p == '*' ) ++p; + if( *p != 0 ) { + ShowWarning("add_builtin: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin->name, buildin->arg); + return false; } + } + if( !buildin->name || *script->skip_word(buildin->name) != 0 ) { + ShowWarning("add_builtin: ignoring function with invalid name \"%s\" (must be a word).\n", buildin->name); + return false; + } + if ( !buildin->func ) { + ShowWarning("add_builtin: ignoring function \"%s\" with invalid source function.\n", buildin->name); + return false; + } + slen = buildin->arg ? strlen(buildin->arg) : 0; + n = script->add_str(buildin->name); + if( !override && script->str_data[n].func && script->str_data[n].func != buildin->func ) { + return false; /* something replaced it, skip. */ + } + + if( override && script->str_data[n].type == C_FUNC ) { + // Overriding + offset = script->str_data[n].val; + if( script->buildin[offset] ) + aFree(script->buildin[offset]); + script->buildin[offset] = NULL; } else { - i = script->buildin_count; + // Adding new function + if( strcmp(buildin->name, "setr") == 0 ) script->buildin_set_ref = n; + else if( strcmp(buildin->name, "callsub") == 0 ) script->buildin_callsub_ref = n; + else if( strcmp(buildin->name, "callfunc") == 0 ) script->buildin_callfunc_ref = n; + else if( strcmp(buildin->name, "getelementofarray") == 0 ) script->buildin_getelementofarray_ref = n; + + offset = script->buildin_count; + script->str_data[n].type = C_FUNC; - script->str_data[n].val = i; - script->str_data[n].func = func; - + script->str_data[n].val = offset; + + // Note: This is a no-op if script->buildin is already large enough + // (it'll only have effect when a plugin adds a new command) RECREATE(script->buildin, char *, ++script->buildin_count); - - /* we only store the arguments, its the only thing used out of this */ - if( args != NULL ) { - int slen = strlen(args); - CREATE(script->buildin[i], char, slen + 1); - safestrncpy(script->buildin[i], args, slen + 1); - } else - script->buildin[i] = NULL; } - + + script->str_data[n].func = buildin->func; + + /* we only store the arguments, its the only thing used out of this */ + if( slen ) { + CREATE(script->buildin[offset], char, slen + 1); + safestrncpy(script->buildin[offset], buildin->arg, slen + 1); + } else { + script->buildin[offset] = NULL; + } + return true; } +bool script_hp_add(char *name, char *args, bool (*func)(struct script_state *st)) { + struct script_function buildin; + buildin.name = name; + buildin.arg = args; + buildin.func = func; + return script->add_builtin(&buildin, true); +} + +#define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args } +#define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args } void script_parse_builtin(void) { struct script_function BUILDIN[] = { // NPC interaction @@ -17344,7 +18801,8 @@ void script_parse_builtin(void) { BUILDIN_DEF(warpguild,"siii"), // [Fredzilla] BUILDIN_DEF(setlook,"ii"), BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it - BUILDIN_DEF(set,"rv"), + BUILDIN_DEF2(setr,"set","rv"), + BUILDIN_DEF(setr,"rv?"), // Not meant to be used directly, required for var++/var-- BUILDIN_DEF(setarray,"rv*"), BUILDIN_DEF(cleararray,"rvi"), BUILDIN_DEF(copyarray,"rri"), @@ -17393,9 +18851,9 @@ void script_parse_builtin(void) { BUILDIN_DEF(getequiprefinerycnt,"i"), BUILDIN_DEF(getequipweaponlv,"i"), BUILDIN_DEF(getequippercentrefinery,"i"), - BUILDIN_DEF(successrefitem,"i"), + BUILDIN_DEF(successrefitem,"i?"), BUILDIN_DEF(failedrefitem,"i"), - BUILDIN_DEF(downrefitem,"i"), + BUILDIN_DEF(downrefitem,"i?"), BUILDIN_DEF(statusup,"i"), BUILDIN_DEF(statusup2,"ii"), BUILDIN_DEF(bonus,"iv"), @@ -17474,7 +18932,7 @@ void script_parse_builtin(void) { BUILDIN_DEF(sc_end,"i?"), BUILDIN_DEF(getstatus, "i?"), BUILDIN_DEF(getscrate,"ii?"), - BUILDIN_DEF(debugmes,"s"), + BUILDIN_DEF(debugmes,"v"), BUILDIN_DEF2(catchpet,"pet","i"), BUILDIN_DEF2(birthpet,"bpet",""), BUILDIN_DEF(resetlvl,"i"), @@ -17488,8 +18946,8 @@ void script_parse_builtin(void) { BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"), BUILDIN_DEF(enablewaitingroomevent,"?"), BUILDIN_DEF(disablewaitingroomevent,"?"), - BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT - BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT + BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT + BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT BUILDIN_DEF(getwaitingroomstate,"i?"), BUILDIN_DEF(warpwaitingpc,"sii?"), BUILDIN_DEF(attachrid,"i"), @@ -17498,17 +18956,17 @@ void script_parse_builtin(void) { BUILDIN_DEF(setmapflagnosave,"ssii"), BUILDIN_DEF(getmapflag,"si"), BUILDIN_DEF(setmapflag,"si?"), - BUILDIN_DEF(removemapflag,"si?"), + BUILDIN_DEF(removemapflag,"si"), BUILDIN_DEF(pvpon,"s"), BUILDIN_DEF(pvpoff,"s"), BUILDIN_DEF(gvgon,"s"), BUILDIN_DEF(gvgoff,"s"), BUILDIN_DEF(emotion,"i??"), BUILDIN_DEF(maprespawnguildid,"sii"), - BUILDIN_DEF(agitstart,""), // <Agit> + BUILDIN_DEF(agitstart,""), // <Agit> BUILDIN_DEF(agitend,""), BUILDIN_DEF(agitcheck,""), // <Agitcheck> - BUILDIN_DEF(flagemblem,"i"), // Flag Emblem + BUILDIN_DEF(flagemblem,"i"), // Flag Emblem BUILDIN_DEF(getcastlename,"s"), BUILDIN_DEF(getcastledata,"si"), BUILDIN_DEF(setcastledata,"sii"), @@ -17534,13 +18992,13 @@ void script_parse_builtin(void) { BUILDIN_DEF(clearitem,""), BUILDIN_DEF(classchange,"ii"), BUILDIN_DEF(misceffect,"i"), - BUILDIN_DEF(playBGM,"s"), - BUILDIN_DEF(playBGMall,"s?????"), + BUILDIN_DEF(playbgm,"s"), + BUILDIN_DEF(playbgmall,"s?????"), BUILDIN_DEF(soundeffect,"si"), - BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster] - BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris] - BUILDIN_DEF(guardian,"siisi??"), // summon guardians - BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris] + BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster] + BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris] + BUILDIN_DEF(guardian,"siisi??"), // summon guardians + BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris] BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris] BUILDIN_DEF(petrecovery,"ii"), // [Valaris] BUILDIN_DEF(petloot,"i"), // [Valaris] @@ -17553,7 +19011,7 @@ void script_parse_builtin(void) { BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris] BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris] BUILDIN_DEF(nude,""), // nude command [Valaris] - BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT + BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT BUILDIN_DEF(atcommand,"s"), // [MouseJstr] BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr] BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr] @@ -17565,7 +19023,7 @@ void script_parse_builtin(void) { BUILDIN_DEF(npcspeed,"i"), // [Valaris] BUILDIN_DEF(npcwalkto,"ii"), // [Valaris] BUILDIN_DEF(npcstop,""), // [Valaris] - BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus] + BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus] BUILDIN_DEF(checkoption1,"i"), BUILDIN_DEF(checkoption2,"i"), BUILDIN_DEF(guildgetexp,"i"), @@ -17585,6 +19043,7 @@ void script_parse_builtin(void) { BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr] BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr] BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr] + BUILDIN_DEF(pcre_match,"ss"), #endif BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus] BUILDIN_DEF(getusersname,""), @@ -17643,11 +19102,14 @@ void script_parse_builtin(void) { BUILDIN_DEF(disguise,"i"), //disguise player. Lupus BUILDIN_DEF(undisguise,""), //undisguise player. Lupus BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus + BUILDIN_DEF(addmonsterdrop,"vii"), + BUILDIN_DEF(delmonsterdrop,"vi"), BUILDIN_DEF(axtoi,"s"), BUILDIN_DEF(query_sql,"s*"), BUILDIN_DEF(query_logsql,"s*"), BUILDIN_DEF(escape_sql,"v"), BUILDIN_DEF(atoi,"s"), + BUILDIN_DEF(strtol,"si"), // [zBuffer] List of player cont commands ---> BUILDIN_DEF(rid2name,"i"), BUILDIN_DEF(pcfollow,"ii"), @@ -17670,12 +19132,13 @@ void script_parse_builtin(void) { BUILDIN_DEF(awake,"s"), BUILDIN_DEF(getvariableofnpc,"rs"), BUILDIN_DEF(warpportal,"iisii"), - BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn] + BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn] BUILDIN_DEF2(homunculus_mutate,"hommutate","?"), - BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus] - BUILDIN_DEF(checkhomcall,""), - BUILDIN_DEF(eaclass,"?"), //[Skotlex] - BUILDIN_DEF(roclass,"i?"), //[Skotlex] + BUILDIN_DEF2(homunculus_morphembryo,"morphembryo",""), + BUILDIN_DEF2(homunculus_checkcall,"checkhomcall",""), + BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus] + BUILDIN_DEF(eaclass,"?"), //[Skotlex] + BUILDIN_DEF(roclass,"i?"), //[Skotlex] BUILDIN_DEF(checkvending,"?"), BUILDIN_DEF(checkchatting,"?"), BUILDIN_DEF(checkidle,"?"), @@ -17718,7 +19181,7 @@ void script_parse_builtin(void) { BUILDIN_DEF(bg_get_data,"ii"), BUILDIN_DEF(bg_getareausers,"isiiii"), BUILDIN_DEF(bg_updatescore,"sii"), - + // Instancing BUILDIN_DEF(instance_create,"si?"), BUILDIN_DEF(instance_destroy,"?"), @@ -17733,6 +19196,10 @@ void script_parse_builtin(void) { BUILDIN_DEF(has_instance,"s?"), BUILDIN_DEF(instance_warpall,"sii?"), BUILDIN_DEF(instance_check_party,"i???"), + BUILDIN_DEF(instance_mapname,"s?"), + BUILDIN_DEF(instance_set_respawn,"sii?"), + BUILDIN_DEF2(has_instance,"has_instance2","s"), + /** * 3rd-related **/ @@ -17748,13 +19215,13 @@ void script_parse_builtin(void) { BUILDIN_DEF(getargcount,""), BUILDIN_DEF(getcharip,"?"), BUILDIN_DEF(is_function,"s"), - BUILDIN_DEF(get_revision,""), BUILDIN_DEF(freeloop,"i"), BUILDIN_DEF(getrandgroupitem,"ii"), BUILDIN_DEF(cleanmap,"s"), BUILDIN_DEF2(cleanmap,"cleanarea","siiii"), BUILDIN_DEF(npcskill,"viii"), BUILDIN_DEF(itemeffect,"v"), + BUILDIN_DEF2(itemeffect,"consumeitem","v"), /* alias of itemeffect */ BUILDIN_DEF(delequip,"i"), /** * @commands (script based) @@ -17762,15 +19229,23 @@ void script_parse_builtin(void) { BUILDIN_DEF(bindatcmd, "ss???"), BUILDIN_DEF(unbindatcmd, "s"), BUILDIN_DEF(useatcmd, "s"), - + + /** + * Item bound [Xantara] [Akinari] [Mhalicot/Hercules] + **/ + BUILDIN_DEF2(getitem,"getitembound","vii?"), + BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"), + BUILDIN_DEF(countbound, "?"), + //Quest Log System [Inkfish] + BUILDIN_DEF(questinfo, "ii??"), BUILDIN_DEF(setquest, "i"), BUILDIN_DEF(erasequest, "i"), BUILDIN_DEF(completequest, "i"), BUILDIN_DEF(checkquest, "i?"), BUILDIN_DEF(changequest, "ii"), - BUILDIN_DEF(showevent, "ii"), - + BUILDIN_DEF(showevent, "i?"), + /** * hQueue [Ind/Hercules] **/ @@ -17784,87 +19259,231 @@ void script_parse_builtin(void) { BUILDIN_DEF(qicheck,"i"), BUILDIN_DEF(qiget,"i"), BUILDIN_DEF(qiclear,"i"), + + BUILDIN_DEF(packageitem,"?"), + + BUILDIN_DEF(sit, "?"), + BUILDIN_DEF(stand, "?"), + BUILDIN_DEF(issit, "?"), + + BUILDIN_DEF(montransform, "vii????"), // Monster Transform [malufett/Hercules] + + /* New BG Commands [Hercules] */ + BUILDIN_DEF(bg_create_team,"sii"), + BUILDIN_DEF(bg_join_team,"i?"), + BUILDIN_DEF(bg_match_over,"s?"), + + /* New Shop Support */ + BUILDIN_DEF(openshop,"?"), + BUILDIN_DEF(sellitem,"i??"), + BUILDIN_DEF(stopselling,"i"), + BUILDIN_DEF(setcurrency,"i?"), + BUILDIN_DEF(tradertype,"i"), + BUILDIN_DEF(purchaseok,""), + BUILDIN_DEF(shopcount, "i"), }; - int i,n, len = ARRAYLENGTH(BUILDIN), start = script->buildin_count; - char* p; - RECREATE(script->buildin, char *, start + len); + int i, len = ARRAYLENGTH(BUILDIN); + RECREATE(script->buildin, char *, script->buildin_count + len); // Pre-alloc to speed up + memset(script->buildin + script->buildin_count, '\0', sizeof(char *) * len); for( i = 0; i < len; i++ ) { - // arg must follow the pattern: (v|s|i|r|l)*\?*\*? - // 'v' - value (either string or int or reference) - // 's' - string - // 'i' - int - // 'r' - reference (of a variable) - // 'l' - label - // '?' - one optional parameter - // '*' - unknown number of optional parameters - p = BUILDIN[i].arg; - while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p; - while( *p == '?' ) ++p; - if( *p == '*' ) ++p; - if( *p != 0 ){ - ShowWarning("script_parse_builtin: ignoring function \"%s\" with invalid arg \"%s\".\n", BUILDIN[i].name, BUILDIN[i].arg); - } else if( *skip_word(BUILDIN[i].name) != 0 ){ - ShowWarning("script_parse_builtin: ignoring function with invalid name \"%s\" (must be a word).\n", BUILDIN[i].name); - } else { - int slen = strlen(BUILDIN[i].arg), offset = start + i; - n = add_str(BUILDIN[i].name); - - if (!strcmp(BUILDIN[i].name, "set")) buildin_set_ref = n; - else if (!strcmp(BUILDIN[i].name, "callsub")) buildin_callsub_ref = n; - else if (!strcmp(BUILDIN[i].name, "callfunc")) buildin_callfunc_ref = n; - else if (!strcmp(BUILDIN[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n; - - if( script->str_data[n].func && script->str_data[n].func != BUILDIN[i].func ) - continue;/* something replaced it, skip. */ - - script->str_data[n].type = C_FUNC; - script->str_data[n].val = offset; - script->str_data[n].func = BUILDIN[i].func; - - /* we only store the arguments, its the only thing used out of this */ - if( slen ) { - CREATE(script->buildin[offset], char, slen + 1); - safestrncpy(script->buildin[offset], BUILDIN[i].arg, slen + 1); - } else - script->buildin[offset] = NULL; - - script->buildin_count++; + script->add_builtin(&BUILDIN[i], false); + } +} +#undef BUILDIN_DEF +#undef BUILDIN_DEF2 - } +void script_label_add(int key, int pos) { + int idx = script->label_count; + + if( script->labels_size == script->label_count ) { + script->labels_size += 1024; + RECREATE(script->labels, struct script_label_entry, script->labels_size); } + + script->labels[idx].key = key; + script->labels[idx].pos = pos; + script->label_count++; } +/** + * Sets source-end constants for scripts to play with + **/ +void script_hardcoded_constants(void) { + + /* server defines */ + script->set_constant("PACKETVER",PACKETVER,false); + script->set_constant("MAX_LEVEL",MAX_LEVEL,false); + script->set_constant("MAX_STORAGE",MAX_STORAGE,false); + script->set_constant("MAX_GUILD_STORAGE",MAX_GUILD_STORAGE,false); + script->set_constant("MAX_CART",MAX_INVENTORY,false); + script->set_constant("MAX_INVENTORY",MAX_INVENTORY,false); + script->set_constant("MAX_ZENY",MAX_ZENY,false); + script->set_constant("MAX_BG_MEMBERS",MAX_BG_MEMBERS,false); + script->set_constant("MAX_CHAT_USERS",MAX_CHAT_USERS,false); + + /* status options */ + script->set_constant("Option_Nothing",OPTION_NOTHING,false); + script->set_constant("Option_Sight",OPTION_SIGHT,false); + script->set_constant("Option_Hide",OPTION_HIDE,false); + script->set_constant("Option_Cloak",OPTION_CLOAK,false); + script->set_constant("Option_Falcon",OPTION_FALCON,false); + script->set_constant("Option_Riding",OPTION_RIDING,false); + script->set_constant("Option_Invisible",OPTION_INVISIBLE,false); + script->set_constant("Option_Orcish",OPTION_ORCISH,false); + script->set_constant("Option_Wedding",OPTION_WEDDING,false); + script->set_constant("Option_Chasewalk",OPTION_CHASEWALK,false); + script->set_constant("Option_Flying",OPTION_FLYING,false); + script->set_constant("Option_Xmas",OPTION_XMAS,false); + script->set_constant("Option_Transform",OPTION_TRANSFORM,false); + script->set_constant("Option_Summer",OPTION_SUMMER,false); + script->set_constant("Option_Dragon1",OPTION_DRAGON1,false); + script->set_constant("Option_Wug",OPTION_WUG,false); + script->set_constant("Option_Wugrider",OPTION_WUGRIDER,false); + script->set_constant("Option_Madogear",OPTION_MADOGEAR,false); + script->set_constant("Option_Dragon2",OPTION_DRAGON2,false); + script->set_constant("Option_Dragon3",OPTION_DRAGON3,false); + script->set_constant("Option_Dragon4",OPTION_DRAGON4,false); + script->set_constant("Option_Dragon5",OPTION_DRAGON5,false); + script->set_constant("Option_Hanbok",OPTION_HANBOK,false); + script->set_constant("Option_Oktoberfest",OPTION_OKTOBERFEST,false); + + /* status option compounds */ + script->set_constant("Option_Dragon",OPTION_DRAGON,false); + script->set_constant("Option_Costume",OPTION_COSTUME,false); + + /* send_target */ + script->set_constant("ALL_CLIENT",ALL_CLIENT,false); + script->set_constant("ALL_SAMEMAP",ALL_SAMEMAP,false); + script->set_constant("AREA",AREA,false); + script->set_constant("AREA_WOS",AREA_WOS,false); + script->set_constant("AREA_WOC",AREA_WOC,false); + script->set_constant("AREA_WOSC",AREA_WOSC,false); + script->set_constant("AREA_CHAT_WOC",AREA_CHAT_WOC,false); + script->set_constant("CHAT",CHAT,false); + script->set_constant("CHAT_WOS",CHAT_WOS,false); + script->set_constant("PARTY",PARTY,false); + script->set_constant("PARTY_WOS",PARTY_WOS,false); + script->set_constant("PARTY_SAMEMAP",PARTY_SAMEMAP,false); + script->set_constant("PARTY_SAMEMAP_WOS",PARTY_SAMEMAP_WOS,false); + script->set_constant("PARTY_AREA",PARTY_AREA,false); + script->set_constant("PARTY_AREA_WOS",PARTY_AREA_WOS,false); + script->set_constant("GUILD",GUILD,false); + script->set_constant("GUILD_WOS",GUILD_WOS,false); + script->set_constant("GUILD_SAMEMAP",GUILD_SAMEMAP,false); + script->set_constant("GUILD_SAMEMAP_WOS",GUILD_SAMEMAP_WOS,false); + script->set_constant("GUILD_AREA",GUILD_AREA,false); + script->set_constant("GUILD_AREA_WOS",GUILD_AREA_WOS,false); + script->set_constant("GUILD_NOBG",GUILD_NOBG,false); + script->set_constant("DUEL",DUEL,false); + script->set_constant("DUEL_WOS",DUEL_WOS,false); + script->set_constant("SELF",SELF,false); + script->set_constant("BG",BG,false); + script->set_constant("BG_WOS",BG_WOS,false); + script->set_constant("BG_SAMEMAP",BG_SAMEMAP,false); + script->set_constant("BG_SAMEMAP_WOS",BG_SAMEMAP_WOS,false); + script->set_constant("BG_AREA",BG_AREA,false); + script->set_constant("BG_AREA_WOS",BG_AREA_WOS,false); + script->set_constant("BG_QUEUE",BG_QUEUE,false); +} + +/** + * a mapindex_name2id wrapper meant to help with invalid name handling + **/ +unsigned short script_mapindexname2id (struct script_state *st, const char* name) { + unsigned short index; + + if( !(index=mapindex->name2id(name)) ) { + script->reportsrc(st); + return 0; + } + return index; +} + + void script_defaults(void) { + // aegis->athena slot position conversion table + unsigned int equip[SCRIPT_EQUIP_TABLE_SIZE] = {EQP_HEAD_TOP,EQP_ARMOR,EQP_HAND_L,EQP_HAND_R,EQP_GARMENT,EQP_SHOES,EQP_ACC_L,EQP_ACC_R,EQP_HEAD_MID,EQP_HEAD_LOW,EQP_COSTUME_HEAD_LOW,EQP_COSTUME_HEAD_MID,EQP_COSTUME_HEAD_TOP,EQP_COSTUME_GARMENT,EQP_SHADOW_ARMOR, EQP_SHADOW_WEAPON, EQP_SHADOW_SHIELD, EQP_SHADOW_SHOES, EQP_SHADOW_ACC_R, EQP_SHADOW_ACC_L}; + script = &script_s; - + script->st_db = NULL; script->active_scripts = 0; script->next_id = 0; script->st_ers = NULL; script->stack_ers = NULL; + script->array_ers = NULL; script->hq = NULL; script->hqi = NULL; script->hqs = script->hqis = 0; - memset(&script->hqe, 0, sizeof(script->hqe)); - - script->buildin_count = 0; + script->buildin = NULL; - + script->buildin_count = 0; + script->str_data = NULL; script->str_data_size = 0; script->str_num = LABEL_START; script->str_buf = NULL; script->str_size = 0; script->str_pos = 0; - + memset(script->str_hash, 0, sizeof(script->str_hash)); + script->word_buf = NULL; script->word_size = 0; - + + script->current_item_id = 0; + + script->labels = NULL; + script->label_count = 0; + script->labels_size = 0; + + script->buf = NULL; + script->pos = 0, script->size = 0; + + script->parse_options = 0; + script->buildin_set_ref = 0; + script->buildin_callsub_ref = 0; + script->buildin_callfunc_ref = 0; + script->buildin_getelementofarray_ref = 0; + + memset(script->error_jump,0,sizeof(script->error_jump)); + script->error_msg = NULL; + script->error_pos = NULL; + script->error_report = 0; + script->parser_current_src = NULL; + script->parser_current_file = NULL; + script->parser_current_line = 0; + + memset(&script->syntax,0,sizeof(script->syntax)); + + script->parse_syntax_for_flag = 0; + + memcpy(script->equip, &equip, sizeof(script->equip)); + + memset(&script->config, 0, sizeof(script->config)); + + script->autobonus_db = NULL; + script->userfunc_db = NULL; + + script->potion_flag = script->potion_hp = script->potion_per_hp = + script->potion_sp = script->potion_per_sp = script->potion_target = 0; + + script->generic_ui_array = NULL; + script->generic_ui_array_size = 0; + /* */ script->init = do_init_script; script->final = do_final_script; - + script->reload = script_reload; + + /* parse */ + script->parse = parse_script; + script->add_builtin = script_add_builtin; script->parse_builtin = script_parse_builtin; + script->skip_space = script_skip_space; + script->error = script_error; + script->warning = script_warning; + script->parse_subexpr = script_parse_subexpr; + script->addScript = script_hp_add; script->conv_num = conv_num; script->conv_str = conv_str; @@ -17876,9 +19495,165 @@ void script_defaults(void) { script->push_str = push_str; script->push_copy = push_copy; script->pop_stack = pop_stack; - + script->set_constant = script_set_constant; + script->set_constant2 = script_set_constant2; + script->get_constant = script_get_constant; + script->label_add = script_label_add; + script->run = run_script; + script->run_main = run_script_main; + script->run_timer = run_script_timer; + script->set_var = set_var; + script->stop_instances = script_stop_instances; + script->free_code = script_free_code; + script->free_vars = script_free_vars; + script->alloc_state = script_alloc_state; + script->free_state = script_free_state; + script->add_pending_ref = script_add_pending_ref; + script->run_autobonus = script_run_autobonus; + script->cleararray_pc = script_cleararray_pc; + script->setarray_pc = script_setarray_pc; + script->config_read = script_config_read; + script->add_str = script_add_str; + script->get_str = script_get_str; + script->search_str = script_search_str; + script->setd_sub = setd_sub; + script->attach_state = script_attach_state; + script->queue = script_hqueue_get; script->queue_add = script_hqueue_add; script->queue_del = script_hqueue_del; script->queue_remove = script_hqueue_remove; + script->queue_create = script_hqueue_create; + script->queue_clear = script_hqueue_clear; + + script->parse_curly_close = parse_curly_close; + script->parse_syntax_close = parse_syntax_close; + script->parse_syntax_close_sub = parse_syntax_close_sub; + script->parse_syntax = parse_syntax; + script->get_com = get_com; + script->get_num = get_num; + script->op2name = script_op2name; + script->reportsrc = script_reportsrc; + script->reportdata = script_reportdata; + script->reportfunc = script_reportfunc; + script->disp_warning_message = disp_warning_message; + script->check_event = check_event; + script->calc_hash = calc_hash; + script->addb = add_scriptb; + script->addc = add_scriptc; + script->addi = add_scripti; + script->addl = add_scriptl; + script->set_label = set_label; + script->skip_word = skip_word; + script->add_word = add_word; + script->parse_callfunc = parse_callfunc; + script->parse_nextline = parse_nextline; + script->parse_variable = parse_variable; + script->parse_simpleexpr = parse_simpleexpr; + script->parse_expr = parse_expr; + script->parse_line = parse_line; + script->read_constdb = read_constdb; + script->print_line = script_print_line; + script->errorwarning_sub = script_errorwarning_sub; + script->set_reg = set_reg; + script->stack_expand = stack_expand; + script->push_retinfo = push_retinfo; + script->op_3 = op_3; + script->op_2str = op_2str; + script->op_2num = op_2num; + script->op_2 = op_2; + script->op_1 = op_1; + script->check_buildin_argtype = script_check_buildin_argtype; + script->detach_state = script_detach_state; + script->db_free_code_sub = db_script_free_code_sub; + script->add_autobonus = script_add_autobonus; + script->menu_countoptions = menu_countoptions; + script->buildin_areawarp_sub = buildin_areawarp_sub; + script->buildin_areapercentheal_sub = buildin_areapercentheal_sub; + script->buildin_delitem_delete = buildin_delitem_delete; + script->buildin_delitem_search = buildin_delitem_search; + script->buildin_killmonster_sub_strip = buildin_killmonster_sub_strip; + script->buildin_killmonster_sub = buildin_killmonster_sub; + script->buildin_killmonsterall_sub_strip = buildin_killmonsterall_sub_strip; + script->buildin_killmonsterall_sub = buildin_killmonsterall_sub; + script->buildin_announce_sub = buildin_announce_sub; + script->buildin_getareausers_sub = buildin_getareausers_sub; + script->buildin_getareadropitem_sub = buildin_getareadropitem_sub; + script->mapflag_pvp_sub = script_mapflag_pvp_sub; + script->buildin_pvpoff_sub = buildin_pvpoff_sub; + script->buildin_maprespawnguildid_sub_pc = buildin_maprespawnguildid_sub_pc; + script->buildin_maprespawnguildid_sub_mob = buildin_maprespawnguildid_sub_mob; + script->buildin_mobcount_sub = buildin_mobcount_sub; + script->playbgm_sub = playbgm_sub; + script->playbgm_foreachpc_sub = playbgm_foreachpc_sub; + script->soundeffect_sub = soundeffect_sub; + script->buildin_query_sql_sub = buildin_query_sql_sub; + script->buildin_instance_warpall_sub = buildin_instance_warpall_sub; + script->buildin_mobuseskill_sub = buildin_mobuseskill_sub; + script->cleanfloor_sub = script_cleanfloor_sub; + script->run_func = run_func; + script->getfuncname = script_getfuncname; + + /* script_config base */ + script->config.warn_func_mismatch_argtypes = 1; + script->config.warn_func_mismatch_paramnum = 1; + script->config.check_cmdcount = 65535; + script->config.check_gotocount = 2048; + script->config.input_min_value = 0; + script->config.input_max_value = INT_MAX; + script->config.die_event_name = "OnPCDieEvent"; + script->config.kill_pc_event_name = "OnPCKillEvent"; + script->config.kill_mob_event_name = "OnNPCKillEvent"; + script->config.login_event_name = "OnPCLoginEvent"; + script->config.logout_event_name = "OnPCLogoutEvent"; + script->config.loadmap_event_name = "OnPCLoadMapEvent"; + script->config.baselvup_event_name = "OnPCBaseLvUpEvent"; + script->config.joblvup_event_name = "OnPCJobLvUpEvent"; + script->config.ontouch_name = "OnTouch_";//ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves) + script->config.ontouch2_name = "OnTouch";//ontouch2_name (run whenever a char walks into the OnTouch area) + + // for ENABLE_CASE_CHECK + script->calc_hash_ci = calc_hash_ci; + script->local_casecheck.add_str = script_local_casecheck_add_str; + script->local_casecheck.clear = script_local_casecheck_clear; + script->local_casecheck.str_data = NULL; + script->local_casecheck.str_data_size = 0; + script->local_casecheck.str_num = 1; + script->local_casecheck.str_buf = NULL; + script->local_casecheck.str_size = 0; + script->local_casecheck.str_pos = 0; + memset(script->local_casecheck.str_hash, 0, sizeof(script->local_casecheck.str_hash)); + script->global_casecheck.add_str = script_global_casecheck_add_str; + script->global_casecheck.clear = script_global_casecheck_clear; + script->global_casecheck.str_data = NULL; + script->global_casecheck.str_data_size = 0; + script->global_casecheck.str_num = 1; + script->global_casecheck.str_buf = NULL; + script->global_casecheck.str_size = 0; + script->global_casecheck.str_pos = 0; + memset(script->global_casecheck.str_hash, 0, sizeof(script->global_casecheck.str_hash)); + // end ENABLE_CASE_CHECK + + /** + * Array Handling + **/ + script->array_src = script_array_src; + script->array_update = script_array_update; + script->array_add_member = script_array_add_member; + script->array_remove_member = script_array_remove_member; + script->array_delete = script_array_delete; + script->array_size = script_array_size; + script->array_free_db = script_free_array_db; + script->array_highest_key = script_array_highest_key; + script->array_ensure_zero = script_array_ensure_zero; + /* */ + script->reg_destroy_single = script_reg_destroy_single; + script->reg_destroy = script_reg_destroy; + /* */ + script->generic_ui_array_expand = script_generic_ui_array_expand; + script->array_cpy_list = script_array_cpy_list; + /* */ + script->hardcoded_constants = script_hardcoded_constants; + script->mapindexname2id = script_mapindexname2id; + } |