diff --git a/src/map/script.c b/src/map/script.c index c6653d3df3..d8ccee1dd4 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -58,7 +58,7 @@ -TBL_PC *script_rid2sd(struct script_state *st); +static bool script_rid2sd_( struct script_state *st, struct map_session_data** sd, const char *func ); /** * Get `sd` from a account id in `loc` param instead of attached rid @@ -70,12 +70,15 @@ TBL_PC *script_rid2sd(struct script_state *st); static bool script_accid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) { if (script_hasdata(st, loc)) { int id_ = script_getnum(st, loc); - if (!(*sd = map_id2sd(id_))) + if (!(*sd = map_id2sd(id_))){ ShowError("%s: Player with account id '%d' is not found.\n", func, id_); + return false; + }else{ + return true; + } } else - *sd = script_rid2sd(st); - return (*sd) ? true : false; + return script_rid2sd_(st,sd,func); } /** @@ -88,12 +91,15 @@ static bool script_accid2sd_(struct script_state *st, uint8 loc, struct map_sess static bool script_charid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) { if (script_hasdata(st, loc)) { int id_ = script_getnum(st, loc); - if (!(*sd = map_charid2sd(id_))) + if (!(*sd = map_charid2sd(id_))){ ShowError("%s: Player with char id '%d' is not found.\n", func, id_); + return false; + }else{ + return true; + } } else - *sd = script_rid2sd(st); - return (*sd) ? true : false; + return script_rid2sd_(st,sd,func); } /** @@ -106,12 +112,36 @@ static bool script_charid2sd_(struct script_state *st, uint8 loc, struct map_ses static bool script_nick2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) { if (script_hasdata(st, loc)) { const char *name_ = script_getstr(st, loc); - if (!(*sd = map_nick2sd(name_,false))) + if (!(*sd = map_nick2sd(name_,false))){ ShowError("%s: Player with nick '%s' is not found.\n", func, name_); + return false; + }else{ + return true; + } } else - *sd = script_rid2sd(st); - return (*sd) ? true : false; + return script_rid2sd_(st,sd,func); +} + +/** + * Get `sd` from a mapid in `loc` param instead of attached rid + * @param st Script + * @param loc Location to look mapid in script parameter + * @param sd Variable that will be assigned + * @return True if `sd` is assigned, false otherwise + **/ +static bool script_mapid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) { + if (script_hasdata(st, loc)) { + int id_ = script_getnum(st, loc); + if (!(*sd = map_id2sd(id_))){ + ShowError("%s: Player with map id '%d' is not found.\n", func, id_); + return false; + }else{ + return true; + } + } + else + return script_rid2sd_(st,sd,func); } /** @@ -140,6 +170,8 @@ static bool script_rid2bl_(struct script_state *st, uint8 loc, struct block_list #define script_accid2sd(loc,sd) script_accid2sd_(st,(loc),&(sd),__FUNCTION__) #define script_charid2sd(loc,sd) script_charid2sd_(st,(loc),&(sd),__FUNCTION__) #define script_nick2sd(loc,sd) script_nick2sd_(st,(loc),&(sd),__FUNCTION__) +#define script_mapid2sd(loc,sd) script_mapid2sd_(st,(loc),&(sd),__FUNCTION__) +#define script_rid2sd(sd) script_rid2sd_(st,&(sd),__FUNCTION__) #define script_rid2bl(loc,bl) script_rid2bl_(st,(loc),&(bl),__FUNCTION__) /// temporary buffer for passing around compiled bytecode @@ -2629,16 +2661,18 @@ struct script_code* parse_script(const char *src,const char *file,int line,int o /// Returns the player attached to this script, identified by the rid. /// If there is no player attached, the script is terminated. -TBL_PC *script_rid2sd(struct script_state *st) -{ - TBL_PC *sd=map_id2sd(st->rid); - if(!sd){ - ShowError("script_rid2sd: fatal error ! player not attached!\n"); +static bool script_rid2sd_( struct script_state *st, struct map_session_data** sd, const char *func ){ + *sd = map_id2sd( st->rid ); + + if( *sd ){ + return true; + }else{ + ShowError("%s: fatal error ! player not attached!\n",func); script_reportfunc(st); script_reportsrc(st); st->state = END; + return false; } - return sd; } /** @@ -2662,9 +2696,7 @@ void get_val_(struct script_state* st, struct script_data* data, struct map_sess //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS] if( !reference_toconstant(data) && not_server_variable(prefix) ) { - if (sd == NULL) - sd = script_rid2sd(st); - if( sd == NULL ) {// needs player attached + if( sd == NULL && !script_rid2sd(sd) ) {// needs player attached if( postfix == '$' ) {// string variable ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name); data->type = C_CONSTSTR; @@ -4948,8 +4980,8 @@ void script_reload(void) { /// mes ""; BUILDIN_FUNC(mes) { - TBL_PC* sd = script_rid2sd(st); - if( sd == NULL ) + TBL_PC* sd; + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; if( !script_hasdata(st, 3) ) @@ -4979,8 +5011,7 @@ BUILDIN_FUNC(next) { TBL_PC* sd; - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; #ifdef SECURE_NPCTIMEOUT sd->npc_idle_type = NPCT_WAIT; @@ -4998,8 +5029,7 @@ BUILDIN_FUNC(close) { TBL_PC* sd; - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; if( !st->mes_active ) { @@ -5023,8 +5053,7 @@ BUILDIN_FUNC(close2) { TBL_PC* sd; - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; st->state = STOP; @@ -5094,8 +5123,7 @@ BUILDIN_FUNC(menu) const char* text; TBL_PC* sd; - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; #ifdef SECURE_NPCTIMEOUT @@ -5223,8 +5251,7 @@ BUILDIN_FUNC(select) const char* text; TBL_PC* sd; - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; #ifdef SECURE_NPCTIMEOUT @@ -5302,8 +5329,7 @@ BUILDIN_FUNC(prompt) const char *text; TBL_PC* sd; - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; #ifdef SECURE_NPCTIMEOUT @@ -5806,7 +5832,7 @@ BUILDIN_FUNC(warpparty) break; case 2: //"SavePoint" uses save point of the currently attached player - if (( sd = script_rid2sd(st) ) == NULL ) + if ( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; break; } @@ -5873,7 +5899,7 @@ BUILDIN_FUNC(warpguild) : ( strcmp(str,"SavePoint")==0 ) ? 2 : 3; - if( type == 2 && ( sd = script_rid2sd(st) ) == NULL ) + if( type == 2 && !script_rid2sd(sd) ) {// "SavePoint" uses save point of the currently attached player return SCRIPT_CMD_SUCCESS; } @@ -6046,8 +6072,7 @@ BUILDIN_FUNC(input) int min; int max; - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; data = script_getdata(st,2); @@ -6211,8 +6236,7 @@ BUILDIN_FUNC(setarray) if( not_server_variable(*name) ) { - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS;// no player attached } @@ -6262,8 +6286,7 @@ BUILDIN_FUNC(cleararray) if( not_server_variable(*name) ) { - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS;// no player attached } @@ -6329,8 +6352,7 @@ BUILDIN_FUNC(copyarray) if( not_server_variable(*name1) || not_server_variable(*name2) ) { - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS;// no player attached } @@ -6390,8 +6412,7 @@ BUILDIN_FUNC(getarraysize) name = reference_getname(data); if( not_server_variable(*name) ){ - sd = script_rid2sd(st); - if (sd == NULL) + if (!script_rid2sd(sd)) return SCRIPT_CMD_SUCCESS;// no player attached } @@ -6432,8 +6453,7 @@ BUILDIN_FUNC(deletearray) name = reference_getname(data); if( not_server_variable(*name) ) { - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS;// no player attached } @@ -6597,8 +6617,7 @@ BUILDIN_FUNC(cutin) { TBL_PC* sd; - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; clif_cutin(sd,script_getstr(st,2),script_getnum(st,3)); @@ -6619,8 +6638,7 @@ BUILDIN_FUNC(viewpoint) id=script_getnum(st,5); color=script_getnum(st,6); - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; clif_viewpoint(sd,st->oid,type,x,y,id,color); @@ -6662,7 +6680,7 @@ BUILDIN_FUNC(countitem) } } else { - if( !(sd = script_rid2sd(st)) ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; } @@ -6770,7 +6788,7 @@ BUILDIN_FUNC(checkweight) struct item_data* id = NULL; struct map_session_data* sd; - if( ( sd = script_rid2sd(st) ) == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; nbargs = script_lastdata(st)+1; @@ -6857,9 +6875,10 @@ BUILDIN_FUNC(checkweight2) 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; + + if( !script_rid2sd(sd) ) + return SCRIPT_CMD_SUCCESS; data_it = script_getdata(st, 2); data_nb = script_getdata(st, 3); @@ -7000,15 +7019,11 @@ BUILDIN_FUNC(getitem) ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound); return SCRIPT_CMD_FAILURE; } - if( script_hasdata(st,5) ) - sd = map_id2sd(script_getnum(st,5)); - else - sd = script_rid2sd(st); // Attached player + script_mapid2sd(5,sd); it.bound = bound; - } else if( script_hasdata(st,4) ) - sd = map_id2sd(script_getnum(st,4)); // - else - sd = script_rid2sd(st); // Attached player + } else { + script_mapid2sd(4,sd); + } if( sd == NULL ) // no target return SCRIPT_CMD_SUCCESS; @@ -7066,14 +7081,10 @@ BUILDIN_FUNC(getitem2) ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound); return SCRIPT_CMD_FAILURE; } - if( script_hasdata(st,12) ) - sd = map_id2sd(script_getnum(st,12)); - else - sd = script_rid2sd(st); // Attached player - } else if( script_hasdata(st,11) ) - sd = map_id2sd(script_getnum(st,11)); // - else - sd = script_rid2sd(st); // Attached player + script_mapid2sd(12,sd); + } else { + script_mapid2sd(11,sd); + } if( sd == NULL ) // no target return SCRIPT_CMD_SUCCESS; @@ -7308,8 +7319,7 @@ BUILDIN_FUNC(getnameditem) TBL_PC *sd, *tsd; struct script_data *data; - sd = script_rid2sd(st); - if (sd == NULL) + if (!script_rid2sd(sd)) { //Player not attached! script_pushint(st,0); return SCRIPT_CMD_SUCCESS; @@ -7414,8 +7424,7 @@ BUILDIN_FUNC(makeitem) { if(strcmp(mapname,"this")==0) { TBL_PC *sd; - sd = script_rid2sd(st); - if (!sd) + if (!script_rid2sd(sd)) return SCRIPT_CMD_SUCCESS; //Failed... m = sd->bl.m; } else @@ -7471,8 +7480,7 @@ BUILDIN_FUNC(makeitem2) { if (strcmp(mapname,"this")==0) { TBL_PC *sd; - sd = script_rid2sd(st); - if (!sd) + if (!script_rid2sd(sd)) return SCRIPT_CMD_SUCCESS; //Failed... m = sd->bl.m; } @@ -7743,22 +7751,10 @@ BUILDIN_FUNC(delitem) else if(!strncmp(command, "guildstorage", 12)) loc = TABLE_GUILD_STORAGE; - if( script_hasdata(st,4) ) - { - uint32 account_id = script_getnum(st,4); - sd = map_id2sd(account_id); // - if( sd == NULL ) - { - ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id); - st->state = END; - return SCRIPT_CMD_FAILURE; - } - } - else - { - sd = script_rid2sd(st);// attached player - if( sd == NULL ) - return SCRIPT_CMD_SUCCESS; + if( !script_accid2sd(4,sd) ){ + // In any case cancel script execution + st->state = END; + return SCRIPT_CMD_SUCCESS; } if (loc == TABLE_CART && !pc_iscarton(sd)) { @@ -7842,22 +7838,10 @@ BUILDIN_FUNC(delitem2) else if(!strncmp(command, "guildstorage", 12)) loc = TABLE_GUILD_STORAGE; - if( script_hasdata(st,11) ) - { - uint32 account_id = script_getnum(st,11); - sd = map_id2sd(account_id); // - if( sd == NULL ) - { - ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id); - st->state = END; - return SCRIPT_CMD_FAILURE; - } - } - else - { - sd = script_rid2sd(st);// attached player - if( sd == NULL ) - return SCRIPT_CMD_SUCCESS; + if( !script_accid2sd(11,sd) ){ + // In any case cancel script execution + st->state = END; + return SCRIPT_CMD_SUCCESS; } if (loc == TABLE_CART && !pc_iscarton(sd)) { @@ -7929,8 +7913,7 @@ BUILDIN_FUNC(delitem2) BUILDIN_FUNC(enableitemuse) { TBL_PC *sd; - sd=script_rid2sd(st); - if (sd) + if (script_rid2sd(sd)) st->npc_item_flag = sd->npc_item_flag = 1; return SCRIPT_CMD_SUCCESS; } @@ -7938,8 +7921,7 @@ BUILDIN_FUNC(enableitemuse) BUILDIN_FUNC(disableitemuse) { TBL_PC *sd; - sd=script_rid2sd(st); - if (sd) + if (script_rid2sd(sd)) st->npc_item_flag = sd->npc_item_flag = 0; return SCRIPT_CMD_SUCCESS; } @@ -7963,7 +7945,7 @@ BUILDIN_FUNC(readparam) script_nick2sd(3, sd); } }else{ - sd = script_rid2sd(st); + script_rid2sd(sd); } if( !sd ){ @@ -8957,8 +8939,7 @@ BUILDIN_FUNC(bonus) TBL_PC* sd; struct script_data *data; - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; // no player attached type = script_getnum(st,2); @@ -9044,8 +9025,7 @@ BUILDIN_FUNC(autobonus) TBL_PC* sd; const char *bonus_script, *other_script = NULL; - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; // no player attached if (current_equip_combo_pos) @@ -9086,8 +9066,7 @@ BUILDIN_FUNC(autobonus2) TBL_PC* sd; const char *bonus_script, *other_script = NULL; - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; // no player attached if (current_equip_combo_pos) @@ -9128,8 +9107,7 @@ BUILDIN_FUNC(autobonus3) const char *bonus_script, *other_script = NULL; struct script_data *data; - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; // no player attached if (current_equip_combo_pos) @@ -9182,8 +9160,7 @@ BUILDIN_FUNC(skill) struct script_data *data; const char* command = script_getfuncname(st); - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS;// no player attached, report source if (strcmpi(command, "addtoskill") == 0) @@ -9212,8 +9189,7 @@ BUILDIN_FUNC(guildskill) int i; struct script_data *data; - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS;// no player attached, report source data = script_getdata(st, 2); @@ -9236,8 +9212,7 @@ BUILDIN_FUNC(getskilllv) TBL_PC* sd; struct script_data *data; - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS;// no player attached, report source data = script_getdata(st, 2); @@ -9764,8 +9739,7 @@ BUILDIN_FUNC(openstorage) { TBL_PC* sd; - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; storage_storageopen(sd); @@ -9777,8 +9751,7 @@ BUILDIN_FUNC(guildopenstorage) TBL_PC* sd; int ret; - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; ret = storage_guild_storageopen(sd); @@ -9799,8 +9772,7 @@ BUILDIN_FUNC(itemskill) TBL_PC* sd; struct script_data *data; - sd = script_rid2sd(st); - if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER ) + if( !script_rid2sd(sd) || sd->ud.skilltimer != INVALID_TIMER ) return SCRIPT_CMD_SUCCESS; data = script_getdata(st, 2); @@ -9827,8 +9799,7 @@ BUILDIN_FUNC(produce) int trigger; TBL_PC* sd; - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; trigger=script_getnum(st,2); @@ -9843,8 +9814,7 @@ BUILDIN_FUNC(cooking) int trigger; TBL_PC* sd; - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; trigger=script_getnum(st,2); @@ -9860,11 +9830,11 @@ BUILDIN_FUNC(makepet) TBL_PC* sd; int id,pet_id; - id=script_getnum(st,2); - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; + id=script_getnum(st,2); + pet_id = search_petDB_index(id, PET_CLASS); if (pet_id < 0) @@ -9919,8 +9889,7 @@ BUILDIN_FUNC(guildgetexp) TBL_PC* sd; int exp; - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; exp = script_getnum(st,2); @@ -10272,13 +10241,13 @@ BUILDIN_FUNC(clone) *------------------------------------------*/ BUILDIN_FUNC(doevent) { - const char* event = script_getstr(st,2); + const char* event; struct map_session_data* sd; - if( ( sd = script_rid2sd(st) ) == NULL ) - { + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; - } + + event = script_getstr(st,2); check_event(st, event); npc_event(sd, event, 0); @@ -10326,15 +10295,18 @@ BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT *------------------------------------------*/ BUILDIN_FUNC(addtimer) { - int tick = script_getnum(st,2); - const char* event = script_getstr(st, 3); + int tick; + const char* event; TBL_PC* sd; - check_event(st, event); - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; + tick = script_getnum(st,2); + event = script_getstr(st, 3); + + check_event(st, 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 SCRIPT_CMD_FAILURE; @@ -10349,11 +10321,11 @@ BUILDIN_FUNC(deltimer) const char *event; TBL_PC* sd; - event=script_getstr(st, 2); - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; + event=script_getstr(st, 2); + check_event(st, event); pc_deleventtimer(sd,event); return SCRIPT_CMD_SUCCESS; @@ -10366,11 +10338,11 @@ BUILDIN_FUNC(addtimercount) int tick; TBL_PC* sd; + if( !script_rid2sd(sd) ) + return SCRIPT_CMD_SUCCESS; + tick=script_getnum(st,2); event=script_getstr(st,3); - sd = script_rid2sd(st); - if( sd == NULL ) - return SCRIPT_CMD_SUCCESS; check_event(st, event); pc_addeventtimercount(sd,event,tick); @@ -10414,8 +10386,8 @@ BUILDIN_FUNC(initnpctimer) return SCRIPT_CMD_SUCCESS; if( flag ) //Attach { - TBL_PC* sd = script_rid2sd(st); - if( sd == NULL ) + TBL_PC* sd; + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; nd->u.scr.rid = sd->bl.id; } @@ -10463,8 +10435,8 @@ BUILDIN_FUNC(startnpctimer) return SCRIPT_CMD_SUCCESS; if( flag ) //Attach { - TBL_PC* sd = script_rid2sd(st); - if( sd == NULL ) + TBL_PC* sd; + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; nd->u.scr.rid = sd->bl.id; } @@ -10663,7 +10635,20 @@ BUILDIN_FUNC(announce) if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined { send_target target; - 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 + struct block_list *bl; + + // If bc_npc flag is set, use NPC as broadcast source + if(flag&BC_NPC){ + bl = map_id2bl(st->oid); + }else{ + struct map_session_data* sd; + + if( script_rid2sd(sd) ) + bl = &sd->bl; + else + bl = NULL; + } + if (bl == NULL) return SCRIPT_CMD_SUCCESS; @@ -10771,7 +10756,7 @@ BUILDIN_FUNC(getusers) {// npc bl = map_id2bl(st->oid); } - else if((sd = script_rid2sd(st))!=NULL) + else if(script_rid2sd(sd)) {// pc bl = &sd->bl; } @@ -11184,11 +11169,11 @@ BUILDIN_FUNC(catchpet) int pet_id; TBL_PC *sd; - pet_id= script_getnum(st,2); - sd=script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; + pet_id= script_getnum(st,2); + pet_catch_process1(sd,pet_id); return SCRIPT_CMD_SUCCESS; } @@ -11200,8 +11185,7 @@ BUILDIN_FUNC(homunculus_evolution) { TBL_PC *sd; - sd=script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; if(hom_is_active(sd->hd)) @@ -11223,8 +11207,7 @@ BUILDIN_FUNC(homunculus_mutate) int homun_id; TBL_PC *sd; - sd = script_rid2sd(st); - if( sd == NULL || sd->hd == NULL ) + if( !script_rid2sd(sd) || sd->hd == NULL ) return SCRIPT_CMD_SUCCESS; if(script_hasdata(st,2)) @@ -11263,8 +11246,7 @@ BUILDIN_FUNC(morphembryo) struct item item_tmp; TBL_PC *sd; - sd = script_rid2sd(st); - if( sd == NULL || sd->hd == NULL ) + if( !script_rid2sd(sd) || sd->hd == NULL ) return SCRIPT_CMD_SUCCESS; if( hom_is_active(sd->hd) ) { @@ -11299,8 +11281,7 @@ BUILDIN_FUNC(homunculus_shuffle) { TBL_PC *sd; - sd=script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; if(hom_is_active(sd->hd)) @@ -11318,10 +11299,10 @@ BUILDIN_FUNC(homunculus_shuffle) *------------------------------------------*/ BUILDIN_FUNC(checkhomcall) { - TBL_PC *sd = script_rid2sd(st); + TBL_PC *sd; TBL_HOM *hd; - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; hd = sd->hd; @@ -11361,10 +11342,10 @@ BUILDIN_FUNC(roclass) sex = script_getnum(st,3); else { TBL_PC *sd; - if (st->rid && (sd=script_rid2sd(st))) + if (st->rid && script_rid2sd(sd)) sex = sd->status.sex; else - sex = 1; //Just use male when not found. + sex = SEX_MALE; //Just use male when not found. } script_pushint(st,pc_mapid2jobid(class_, sex)); return SCRIPT_CMD_SUCCESS; @@ -11376,8 +11357,7 @@ BUILDIN_FUNC(roclass) BUILDIN_FUNC(birthpet) { TBL_PC *sd; - sd=script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; if( sd->status.pet_id ) @@ -11458,12 +11438,7 @@ BUILDIN_FUNC(changebase) TBL_PC *sd=NULL; int vclass; - if( script_hasdata(st,3) ) - sd=map_id2sd(script_getnum(st,3)); - else - sd=script_rid2sd(st); - - if(sd == NULL) + if( !script_mapid2sd(3,sd) ) return SCRIPT_CMD_SUCCESS; vclass = script_getnum(st,2); @@ -12767,8 +12742,10 @@ BUILDIN_FUNC(getequipcardcnt) TBL_PC *sd; int count; + if( !script_rid2sd(sd) ) + return SCRIPT_CMD_SUCCESS; + num=script_getnum(st,2); - sd=script_rid2sd(st); if (equip_index_check(num)) i=pc_checkequip(sd,equip_bitmask[num]); @@ -12798,8 +12775,13 @@ BUILDIN_FUNC(getequipcardcnt) BUILDIN_FUNC(successremovecards) { int i=-1,c,cardflag=0; - TBL_PC* sd = script_rid2sd(st); - int num = script_getnum(st,2); + TBL_PC* sd; + int num; + + if( !script_rid2sd(sd) ) + return SCRIPT_CMD_SUCCESS; + + num = script_getnum(st,2); if (equip_index_check(num)) i=pc_checkequip(sd,equip_bitmask[num]); @@ -12868,9 +12850,15 @@ BUILDIN_FUNC(successremovecards) { BUILDIN_FUNC(failedremovecards) { int i=-1,c,cardflag=0; - TBL_PC* sd = script_rid2sd(st); - int num = script_getnum(st,2); - int typefail = script_getnum(st,3); + TBL_PC* sd; + int num; + int typefail; + + if( !script_rid2sd(sd) ) + return SCRIPT_CMD_SUCCESS; + + num = script_getnum(st,2); + typefail = script_getnum(st,3); if (equip_index_check(num)) i=pc_checkequip(sd,equip_bitmask[num]); @@ -13018,8 +13006,8 @@ BUILDIN_FUNC(mobcount) // Added by RoVeRT check_event(st, event); if( strcmp(mapname, "this") == 0 ) { - struct map_session_data *sd = script_rid2sd(st); - if( sd ) + struct map_session_data *sd; + if( script_rid2sd(sd) ) m = sd->bl.m; else { script_pushint(st,-1); @@ -13037,11 +13025,10 @@ BUILDIN_FUNC(mobcount) // Added by RoVeRT BUILDIN_FUNC(marriage) { - const char *partner=script_getstr(st,2); - TBL_PC *sd=script_rid2sd(st); - TBL_PC *p_sd=map_nick2sd(partner,false); + TBL_PC *sd; + TBL_PC *p_sd; - if(!sd || !p_sd || !pc_marriage(sd,p_sd)){ + if(!script_rid2sd(sd) || !(p_sd=map_nick2sd(script_getstr(st,2),false)) || !pc_marriage(sd,p_sd)){ script_pushint(st,0); return SCRIPT_CMD_SUCCESS; } @@ -13051,10 +13038,10 @@ BUILDIN_FUNC(marriage) BUILDIN_FUNC(wedding_effect) { - TBL_PC *sd=script_rid2sd(st); + TBL_PC *sd; struct block_list *bl; - if(sd==NULL) { + if(!script_rid2sd(sd)) { bl=map_id2bl(st->oid); } else bl=&sd->bl; @@ -13164,10 +13151,10 @@ BUILDIN_FUNC(warppartner) int x,y; unsigned short mapindex; const char *str; - TBL_PC *sd=script_rid2sd(st); - TBL_PC *p_sd=NULL; + TBL_PC *sd; + TBL_PC *p_sd; - if(sd==NULL || !pc_ismarried(sd) || + if(!script_rid2sd(sd) || !pc_ismarried(sd) || (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) { script_pushint(st,0); return SCRIPT_CMD_SUCCESS; @@ -13494,9 +13481,12 @@ BUILDIN_FUNC(getequipcardid) int i=-1,num,slot; TBL_PC *sd; + if( !script_rid2sd(sd) ) + return SCRIPT_CMD_SUCCESS; + num=script_getnum(st,2); slot=script_getnum(st,3); - sd=script_rid2sd(st); + if (equip_index_check(num)) i=pc_checkequip(sd,equip_bitmask[num]); if(i >= 0 && slot>=0 && slot<4) @@ -13512,9 +13502,9 @@ BUILDIN_FUNC(getequipcardid) BUILDIN_FUNC(petskillbonus) { struct pet_data *pd; - TBL_PC *sd = script_rid2sd(st); + TBL_PC *sd; - if(sd == NULL || sd->pd == NULL) + if(!script_rid2sd(sd) || sd->pd == NULL) return SCRIPT_CMD_FAILURE; pd = sd->pd; @@ -13548,9 +13538,9 @@ BUILDIN_FUNC(petloot) { int max; struct pet_data *pd; - TBL_PC *sd=script_rid2sd(st); + TBL_PC *sd; - if(sd==NULL || sd->pd==NULL) + if(!script_rid2sd(sd) || sd->pd==NULL) return SCRIPT_CMD_SUCCESS; max=script_getnum(st,2); @@ -13727,8 +13717,8 @@ BUILDIN_FUNC(misceffect) if (bl) clif_specialeffect(bl,type,AREA); } else{ - TBL_PC *sd=script_rid2sd(st); - if(sd) + TBL_PC *sd; + if(script_rid2sd(sd)) clif_specialeffect(&sd->bl,type,AREA); } return SCRIPT_CMD_SUCCESS; @@ -13740,7 +13730,7 @@ BUILDIN_FUNC(playBGM) { struct map_session_data* sd; - if( ( sd = script_rid2sd(st) ) != NULL ) { + if( script_rid2sd(sd) ) { clif_playBGM(sd, script_getstr(st,2)); } return SCRIPT_CMD_SUCCESS; @@ -13793,11 +13783,12 @@ BUILDIN_FUNC(playBGMall) *------------------------------------------*/ BUILDIN_FUNC(soundeffect) { - TBL_PC* sd = script_rid2sd(st); - const char* name = script_getstr(st,2); - int type = script_getnum(st,3); + TBL_PC* sd; + + if(script_rid2sd(sd)){ + const char* name = script_getstr(st,2); + int type = script_getnum(st,3); - if(sd) { clif_soundeffect(sd,&sd->bl,name,type); } return SCRIPT_CMD_SUCCESS; @@ -13820,10 +13811,15 @@ int soundeffect_sub(struct block_list* bl,va_list ap) BUILDIN_FUNC(soundeffectall) { struct block_list* bl; + struct map_session_data* sd; const char* name; int type; - bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid); + if( st->rid && script_rid2sd(sd) ) + bl = &sd->bl; + else + bl = map_id2bl(st->oid); + if (!bl) return SCRIPT_CMD_SUCCESS; @@ -13864,10 +13860,10 @@ BUILDIN_FUNC(soundeffectall) BUILDIN_FUNC(petrecovery) { struct pet_data *pd; - TBL_PC *sd=script_rid2sd(st); + TBL_PC *sd; int sc; - if(sd == NULL || sd->pd == NULL) + if(!script_rid2sd(sd) || sd->pd == NULL) return SCRIPT_CMD_FAILURE; sc = script_getnum(st,2); @@ -13900,10 +13896,10 @@ BUILDIN_FUNC(petskillattack) { struct pet_data *pd; struct script_data *data; - TBL_PC *sd = script_rid2sd(st); + TBL_PC *sd; int id = 0; - if(sd == NULL || sd->pd == NULL) + if(!script_rid2sd(sd) || sd->pd == NULL) return SCRIPT_CMD_FAILURE; data = script_getdata(st, 2); @@ -13936,10 +13932,10 @@ BUILDIN_FUNC(petskillattack2) { struct pet_data *pd; struct script_data *data; - TBL_PC *sd = script_rid2sd(st); + TBL_PC *sd; int id = 0; - if(sd == NULL || sd->pd == NULL) + if(!script_rid2sd(sd) || sd->pd == NULL) return SCRIPT_CMD_FAILURE; data = script_getdata(st, 2); @@ -13972,10 +13968,10 @@ BUILDIN_FUNC(petskillsupport) { struct pet_data *pd; struct script_data *data; - TBL_PC *sd = script_rid2sd(st); + TBL_PC *sd; int id = 0; - if(sd == NULL || sd->pd == NULL) + if(!script_rid2sd(sd) || sd->pd == NULL) return SCRIPT_CMD_FAILURE; data = script_getdata(st, 2); @@ -14022,12 +14018,16 @@ BUILDIN_FUNC(skilleffect) { TBL_PC *sd; uint16 skill_id, skill_lv; - struct script_data *data = script_getdata(st, 2); + struct script_data *data; + + if( !script_rid2sd(sd) ) + return SCRIPT_CMD_SUCCESS; + + data = script_getdata(st, 2); get_val(st, data); // Convert into value in case of a variable skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) ); skill_lv = script_getnum(st,3); - sd = script_rid2sd(st); /* 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. */ @@ -14084,8 +14084,8 @@ BUILDIN_FUNC(specialeffect) else { if (target == SELF) { - TBL_PC *sd=script_rid2sd(st); - if (sd) + TBL_PC *sd; + if (script_rid2sd(sd)) clif_specialeffect_single(bl,type,sd->fd); } else { clif_specialeffect(bl, type, target); @@ -14096,12 +14096,14 @@ BUILDIN_FUNC(specialeffect) BUILDIN_FUNC(specialeffect2) { - TBL_PC *sd=script_rid2sd(st); - int type = script_getnum(st,2); - enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA; + TBL_PC *sd; + + if( script_nick2sd(4,sd) ){ + int type = script_getnum(st,2); + enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA; - if( script_nick2sd(4,sd) ) clif_specialeffect(&sd->bl, type, target); + } return SCRIPT_CMD_SUCCESS; } @@ -14138,7 +14140,9 @@ int atcommand_sub(struct script_state* st,int type) { cmd = script_getstr(st,2); if (st->rid) { - sd = script_rid2sd(st); + if( !script_rid2sd(sd) ) + return SCRIPT_CMD_SUCCESS; + fd = sd->fd; } else { //Use a dummy character. sd = &dummy_sd; @@ -14245,9 +14249,7 @@ BUILDIN_FUNC(recovery) switch(type) { case 0: - if(script_hasdata(st,3)) - sd=map_charid2sd(script_getnum(st,3)); - else if((sd = script_rid2sd(st)) == NULL) + if(!script_charid2sd(3,sd)) return SCRIPT_CMD_SUCCESS; //If we don't have sd by now, bail out recovery_sub(sd, revive); break; @@ -14265,7 +14267,7 @@ BUILDIN_FUNC(recovery) } if(script_hasdata(st,3)) p_id = script_getnum(st,3); - else if((sd = script_rid2sd(st))) + else if(script_rid2sd(sd)) p_id = sd->status.party_id; p = party_search(p_id); if(p == NULL) @@ -14293,7 +14295,7 @@ BUILDIN_FUNC(recovery) } if(script_hasdata(st,3)) g_id = script_getnum(st,3); - else if((sd = script_rid2sd(st))) + else if(script_rid2sd(sd)) g_id = sd->status.guild_id; g = guild_search(g_id); if(g == NULL) @@ -14310,7 +14312,7 @@ BUILDIN_FUNC(recovery) case 3: if(script_hasdata(st,3)) map_idx = map_mapname2mapid(script_getstr(st,3)); - else if((sd = script_rid2sd(st))) + else if(script_rid2sd(sd)) map_idx = sd->bl.m; if(map_idx < 1) return SCRIPT_CMD_FAILURE; //No sd and no map given - return @@ -14348,7 +14350,7 @@ BUILDIN_FUNC(recovery) *------------------------------------------*/ BUILDIN_FUNC(getpetinfo) { - TBL_PC *sd=script_rid2sd(st); + TBL_PC *sd; TBL_PET *pd; int type = script_getnum(st,2); @@ -14384,7 +14386,7 @@ BUILDIN_FUNC(getpetinfo) *------------------------------------------*/ BUILDIN_FUNC(gethominfo) { - TBL_PC *sd=script_rid2sd(st); + TBL_PC *sd; TBL_HOM *hd; int type=script_getnum(st,2); @@ -14420,46 +14422,33 @@ BUILDIN_FUNC(getmercinfo) struct map_session_data* sd; struct mercenary_data* md; + if( !script_charid2sd(3,sd) ){ + script_pushnil(st); + return SCRIPT_CMD_FAILURE; + } + type = script_getnum(st,2); + md = sd->md; - if( script_hasdata(st,3) ) - { - uint32 char_id = script_getnum(st,3); - - if( ( sd = map_charid2sd(char_id) ) == NULL ) - { - ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id); - script_pushnil(st); - return SCRIPT_CMD_FAILURE; - } + if( md == NULL ){ + if( type == 2 ) + script_pushconststr(st,""); + else + script_pushint(st,0); + return SCRIPT_CMD_SUCCESS; } - else - { - if( ( sd = script_rid2sd(st) ) == NULL ) - { - script_pushnil(st); - return SCRIPT_CMD_SUCCESS; - } - } - - md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL; switch( type ) { - case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break; - case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break; - case 2: - if( md ) - script_pushstrcopy(st,md->db->name); - 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 5: script_pushint(st,md ? md->mercenary.kill_count : 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; - case 8: script_pushint(st,md ? md->bl.id : 0); break; + case 0: script_pushint(st,md->mercenary.mercenary_id); break; + case 1: script_pushint(st,md->mercenary.class_); break; + case 2: script_pushstrcopy(st,md->db->name); break; + case 3: script_pushint(st,mercenary_get_faith(md)); break; + case 4: script_pushint(st,mercenary_get_calls(md)); break; + case 5: script_pushint(st,md->mercenary.kill_count); break; + case 6: script_pushint(st,mercenary_get_lifetime(md)); break; + case 7: script_pushint(st,md->db->lv); break; + case 8: script_pushint(st,md->bl.id); break; default: ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id); script_pushnil(st); @@ -14476,9 +14465,9 @@ BUILDIN_FUNC(getmercinfo) *------------------------------------------*/ BUILDIN_FUNC(checkequipedcard) { - TBL_PC *sd=script_rid2sd(st); + TBL_PC *sd; - if(sd){ + if(script_rid2sd(sd)){ int n,i,c=0; c=script_getnum(st,2); @@ -14826,9 +14815,12 @@ BUILDIN_FUNC(getmapxy) name=get_str(script_getvarid(num)); prefix=*name; - if(not_server_variable(prefix)) - sd=script_rid2sd(st); - else + if(not_server_variable(prefix)){ + if( !script_rid2sd(sd) ){ + ShowError( "buildin_getmapxy: variable '%s' for mapname is not a server variable, but no player is attached!", name ); + return SCRIPT_CMD_FAILURE; + } + }else sd=NULL; set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2)); @@ -14837,9 +14829,12 @@ BUILDIN_FUNC(getmapxy) name=get_str(script_getvarid(num)); prefix=*name; - if(not_server_variable(prefix)) - sd=script_rid2sd(st); - else + if(not_server_variable(prefix)){ + if( !script_rid2sd(sd) ){ + ShowError( "buildin_getmapxy: variable '%s' for mapX is not a server variable, but no player is attached!", name ); + return SCRIPT_CMD_FAILURE; + } + }else sd=NULL; set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3)); @@ -14848,9 +14843,12 @@ BUILDIN_FUNC(getmapxy) name=get_str(script_getvarid(num)); prefix=*name; - if(not_server_variable(prefix)) - sd=script_rid2sd(st); - else + if(not_server_variable(prefix)){ + if( !script_rid2sd(sd) ){ + ShowError( "buildin_getmapxy: variable '%s' for mapY is not a server variable, but no player is attached!", name ); + return SCRIPT_CMD_FAILURE; + } + }else sd=NULL; set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4)); @@ -14884,8 +14882,7 @@ BUILDIN_FUNC(logmes) const char *str; TBL_PC* sd; - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_FAILURE; str = script_getstr(st,2); @@ -14901,8 +14898,7 @@ BUILDIN_FUNC(summon) struct mob_data *md; int tick = gettick(); - sd=script_rid2sd(st); - if (!sd) + if (!script_rid2sd(sd)) return SCRIPT_CMD_SUCCESS; str =script_getstr(st,2); @@ -14957,8 +14953,7 @@ BUILDIN_FUNC(isequippedcnt) int i, 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... + if (!script_rid2sd(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 SCRIPT_CMD_SUCCESS; } @@ -15012,9 +15007,7 @@ BUILDIN_FUNC(isequipped) //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... + if (!script_rid2sd(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 SCRIPT_CMD_SUCCESS; } @@ -15096,7 +15089,10 @@ BUILDIN_FUNC(cardscnt) int ret = 0; int index; - sd = script_rid2sd(st); + if( !script_rid2sd(sd) ){ + script_pushint(st,0); + return SCRIPT_CMD_SUCCESS; + } for (i=0; id!=0; i++) { FETCH (i+2, id) else id = 0; @@ -15133,7 +15129,7 @@ BUILDIN_FUNC(cardscnt) BUILDIN_FUNC(getrefine) { TBL_PC *sd; - if ((sd = script_rid2sd(st))!= NULL){ + if (script_rid2sd(sd)){ if( current_equip_item_index == -1 ){ script_pushint(st, 0); return SCRIPT_CMD_FAILURE; @@ -15530,8 +15526,7 @@ BUILDIN_FUNC(explode) } if( not_server_variable(*name) ) { - sd = script_rid2sd(st); - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS;// no player attached } @@ -15583,11 +15578,8 @@ BUILDIN_FUNC(implode) return SCRIPT_CMD_FAILURE;// data type mismatch } - if( not_server_variable(*name) ) { - sd = script_rid2sd(st); - - if( sd == NULL ) - return SCRIPT_CMD_SUCCESS;// no player attached + if( not_server_variable(*name) && !script_rid2sd(sd) ) { + return SCRIPT_CMD_SUCCESS;// no player attached } //count chars @@ -15859,7 +15851,7 @@ BUILDIN_FUNC(sscanf){ return SCRIPT_CMD_FAILURE; } buf_p = reference_getname(data); - if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){ + if(not_server_variable(*buf_p) && !script_rid2sd(sd)){ script_pushint(st, -1); if(buf) aFree(buf); if(ref_str) aFree(ref_str); @@ -16297,8 +16289,7 @@ int buildin_query_sql_sub(struct script_state* st, Sql* handle) 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 + if( !script_rid2sd(sd) ) { // no player attached script_reportdata(data); st->state = END; return SCRIPT_CMD_FAILURE; @@ -16438,8 +16429,7 @@ BUILDIN_FUNC(callshop) struct npc_data *nd; const char *shopname; int flag = 0; - sd = script_rid2sd(st); - if (!sd) { + if (!script_rid2sd(sd)) { script_pushint(st,0); return SCRIPT_CMD_SUCCESS; } @@ -16942,11 +16932,9 @@ BUILDIN_FUNC(searchitem) start = reference_getindex(data); name = reference_getname(data); - if( not_server_variable(*name) ) + if( not_server_variable(*name) && !script_rid2sd(sd) ) { - sd = script_rid2sd(st); - if( sd == NULL ) - return SCRIPT_CMD_SUCCESS;// no player attached + return SCRIPT_CMD_SUCCESS;// no player attached } if( is_string_variable(name) ) @@ -17041,38 +17029,19 @@ BUILDIN_FUNC(pcblockskill) BUILDIN_FUNC(pcfollow) { - int id, targetid; - TBL_PC *sd = NULL; + TBL_PC *sd; - - id = script_getnum(st,2); - targetid = script_getnum(st,3); - - if(id) - sd = map_id2sd(id); - else - sd = script_rid2sd(st); - - if(sd) - pc_follow(sd, targetid); + if( script_mapid2sd(2,sd) ) + pc_follow(sd, script_getnum(st,3)); return SCRIPT_CMD_SUCCESS; } BUILDIN_FUNC(pcstopfollow) { - int id; - TBL_PC *sd = NULL; + TBL_PC *sd; - - id = script_getnum(st,2); - - if(id) - sd = map_id2sd(id); - else - sd = script_rid2sd(st); - - if(sd) + if(script_mapid2sd(2,sd)) pc_stop_following(sd); return SCRIPT_CMD_SUCCESS; @@ -18545,9 +18514,12 @@ BUILDIN_FUNC(getfreecell) name = get_str(num&0x00ffffff); prefix = *name; - if (not_server_variable(prefix)) - sd = script_rid2sd(st); - else + if (not_server_variable(prefix)){ + if( !script_rid2sd(sd) ){ + ShowError( "buildin_getfreecell: variable '%s' for mapX is not a server variable, but no player is attached!", name ); + return SCRIPT_CMD_FAILURE; + } + }else sd = NULL; set_reg(st, sd, num, name, (void*)__64BPRTSIZE((int)x), script_getref(st, 3)); @@ -18557,9 +18529,12 @@ BUILDIN_FUNC(getfreecell) name = get_str(num&0x00ffffff); prefix = *name; - if (not_server_variable(prefix)) - sd = script_rid2sd(st); - else + if (not_server_variable(prefix)){ + if( !script_rid2sd(sd) ){ + ShowError( "buildin_getfreecell: variable '%s' for mapY is not a server variable, but no player is attached!", name ); + return SCRIPT_CMD_FAILURE; + } + }else sd = NULL; set_reg(st, sd, num, name, (void*)__64BPRTSIZE((int)y), script_getref(st, 4)); @@ -18575,7 +18550,7 @@ BUILDIN_FUNC(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( !script_rid2sd(sd) || sd->md || sd->status.mer_id != 0 ) return SCRIPT_CMD_SUCCESS; class_ = script_getnum(st,2); @@ -18591,10 +18566,10 @@ BUILDIN_FUNC(mercenary_create) BUILDIN_FUNC(mercenary_heal) { - struct map_session_data *sd = script_rid2sd(st); + struct map_session_data *sd; int hp, sp; - if( sd == NULL || sd->md == NULL ) + if( !script_rid2sd(sd) || sd->md == NULL ) return SCRIPT_CMD_SUCCESS; hp = script_getnum(st,2); sp = script_getnum(st,3); @@ -18605,11 +18580,11 @@ BUILDIN_FUNC(mercenary_heal) BUILDIN_FUNC(mercenary_sc_start) { - struct map_session_data *sd = script_rid2sd(st); + struct map_session_data *sd; enum sc_type type; int tick, val1; - if( sd == NULL || sd->md == NULL ) + if( !script_rid2sd(sd) || sd->md == NULL ) return SCRIPT_CMD_SUCCESS; type = (sc_type)script_getnum(st,2); @@ -18622,10 +18597,10 @@ BUILDIN_FUNC(mercenary_sc_start) BUILDIN_FUNC(mercenary_get_calls) { - struct map_session_data *sd = script_rid2sd(st); + struct map_session_data *sd; int guild; - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; guild = script_getnum(st,2); @@ -18649,10 +18624,10 @@ BUILDIN_FUNC(mercenary_get_calls) BUILDIN_FUNC(mercenary_set_calls) { - struct map_session_data *sd = script_rid2sd(st); + struct map_session_data *sd; int guild, value, *calls; - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; guild = script_getnum(st,2); @@ -18681,10 +18656,10 @@ BUILDIN_FUNC(mercenary_set_calls) BUILDIN_FUNC(mercenary_get_faith) { - struct map_session_data *sd = script_rid2sd(st); + struct map_session_data *sd; int guild; - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; guild = script_getnum(st,2); @@ -18708,10 +18683,10 @@ BUILDIN_FUNC(mercenary_get_faith) BUILDIN_FUNC(mercenary_set_faith) { - struct map_session_data *sd = script_rid2sd(st); + struct map_session_data *sd; int guild, value, *calls; - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; guild = script_getnum(st,2); @@ -18749,7 +18724,7 @@ BUILDIN_FUNC(readbook) struct map_session_data *sd; int book_id, page; - if( (sd = script_rid2sd(st)) == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; book_id = script_getnum(st,2); @@ -19356,7 +19331,7 @@ unsigned short script_instancegetid(struct script_state* st) struct party_data *p = NULL; struct guild *g = NULL; - if ((sd = script_rid2sd(st)) != NULL) { + if (script_rid2sd(sd)) { if (sd->instance_id) instance_id = sd->instance_id; if (instance_id == 0 && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id) @@ -19389,23 +19364,23 @@ BUILDIN_FUNC(instance_create) if (script_hasdata(st, 4)) owner_id = script_getnum(st, 4); else { + // If sd is NULL, instance_create will return -2. struct map_session_data *sd = NULL; - sd = script_rid2sd(st); // If sd is NULL, instance_create will return -2. switch(mode) { case IM_NONE: owner_id = st->oid; break; case IM_CHAR: - if (sd) + if (script_rid2sd(sd)) owner_id = sd->status.char_id; break; case IM_PARTY: - if (sd) + if (script_rid2sd(sd)) owner_id = sd->status.party_id; break; case IM_GUILD: - if (sd) + if (script_rid2sd(sd)) owner_id = sd->status.guild_id; break; default: @@ -19837,12 +19812,14 @@ BUILDIN_FUNC(instance_info) *------------------------------------------*/ BUILDIN_FUNC(setfont) { - struct map_session_data *sd = script_rid2sd(st); - int font = script_getnum(st,2); + struct map_session_data *sd; + int font; - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; + font = script_getnum(st,2); + if( sd->status.font != font ) sd->status.font = font; else @@ -19929,11 +19906,11 @@ BUILDIN_FUNC(areamobuseskill) BUILDIN_FUNC(progressbar) { - struct map_session_data * sd = script_rid2sd(st); + struct map_session_data * sd; const char * color; unsigned int second; - if( !st || !sd ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_SUCCESS; st->state = STOP; @@ -19955,7 +19932,7 @@ BUILDIN_FUNC(pushpc) int cells, dx, dy; struct map_session_data* sd; - if((sd = script_rid2sd(st))==NULL) + if(!script_rid2sd(sd)) { return SCRIPT_CMD_SUCCESS; } @@ -19995,7 +19972,7 @@ BUILDIN_FUNC(buyingstore) { struct map_session_data* sd; - if( ( sd = script_rid2sd(st) ) == NULL ) + if( !script_rid2sd(sd) ) { return SCRIPT_CMD_SUCCESS; } @@ -20020,7 +19997,7 @@ BUILDIN_FUNC(searchstores) unsigned int uses; struct map_session_data* sd; - if( ( sd = script_rid2sd(st) ) == NULL ) + if( !script_rid2sd(sd) ) { return SCRIPT_CMD_SUCCESS; } @@ -20051,7 +20028,7 @@ BUILDIN_FUNC(showdigit) int value; struct map_session_data* sd; - if( ( sd = script_rid2sd(st) ) == NULL ) + if( !script_rid2sd(sd) ) { return SCRIPT_CMD_SUCCESS; } @@ -20233,7 +20210,7 @@ BUILDIN_FUNC(getcharip) } } else - sd = script_rid2sd(st); + script_rid2sd(sd); /* check for sd and IP */ if (!sd || !session[sd->fd]->client_addr) @@ -20460,14 +20437,16 @@ BUILDIN_FUNC(checkre) BUILDIN_FUNC(getrandgroupitem) { TBL_PC* sd; int i, get_count = 0; - uint16 group = script_getnum(st,2), qty = 0; + uint16 group, qty = 0; uint8 sub_group = 1; struct item item_tmp; struct s_item_group_entry *entry = NULL; - if (!(sd = script_rid2sd(st))) + if (!script_rid2sd(sd)) return SCRIPT_CMD_SUCCESS; + group = script_getnum(st,2); + if (!group) { ShowError("buildin_getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2)); return SCRIPT_CMD_FAILURE; @@ -20579,6 +20558,9 @@ BUILDIN_FUNC(npcskill) struct npc_data *nd; struct map_session_data *sd; struct script_data *data; + + if( !script_rid2sd(sd) ) + return SCRIPT_CMD_SUCCESS; data = script_getdata(st, 2); get_val(st, data); // Convert into value in case of a variable @@ -20586,7 +20568,6 @@ BUILDIN_FUNC(npcskill) 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) { @@ -20597,7 +20578,7 @@ BUILDIN_FUNC(npcskill) ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL); return SCRIPT_CMD_FAILURE; } - if (sd == NULL || nd == NULL) { //ain't possible, but I don't trust people. + if (nd == NULL) { //ain't possible, but I don't trust people. return SCRIPT_CMD_FAILURE; } @@ -20838,11 +20819,11 @@ BUILDIN_FUNC(party_delmember) { TBL_PC *sd = NULL; - if( !script_hasdata(st,2) && !script_hasdata(st,3) && !(sd = script_rid2sd(st)) ) { + if( !script_hasdata(st,2) && !script_hasdata(st,3) && !script_rid2sd(sd) ) { script_pushint(st,-1); return SCRIPT_CMD_FAILURE; } - if( sd || (script_getnum(st,2) && (sd = map_charid2sd(script_getnum(st,2)))) ) + if( sd || script_charid2sd(2,sd) ) script_pushint(st,party_removemember2(sd,0,0)); else script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3))); @@ -20952,19 +20933,18 @@ BUILDIN_FUNC(party_destroy) */ BUILDIN_FUNC(is_clientver) { TBL_PC *sd = NULL; - int type = script_getnum(st,2); - int data = script_getnum(st,3); + int type; + int data; int ret = 0; - if (script_hasdata(st,4)) - sd = map_charid2sd(script_getnum(st,4)); - else - sd = script_rid2sd(st); - if (sd == NULL) { + if ( !script_charid2sd(4,sd) ) { script_pushint(st,0); return SCRIPT_CMD_FAILURE; } + type = script_getnum(st,2); + data = script_getnum(st,3); + switch(type){ case 0: ret = (sd->packet_ver >= data)?1:0; @@ -21062,7 +21042,7 @@ BUILDIN_FUNC(montransform) { struct script_data *data; val1 = val2 = val3 = val4 = 0; - if( (sd = script_rid2sd(st)) == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_FAILURE; data = script_getdata(st, 2); @@ -21153,16 +21133,7 @@ BUILDIN_FUNC(bonus_script) { const char *script_str = NULL; struct s_bonus_script_entry *entry = NULL; - if (script_hasdata(st,7)) { - if (!(sd = map_charid2sd(script_getnum(st,7)))) { - ShowError("buildin_bonus_script: Player CID=%d is not found.\n", script_getnum(st,7)); - return SCRIPT_CMD_FAILURE; - } - } - else - sd = script_rid2sd(st); - - if (sd == NULL) + if ( !script_charid2sd(7,sd) ) return SCRIPT_CMD_FAILURE; script_str = script_getstr(st,2); @@ -21203,17 +21174,12 @@ BUILDIN_FUNC(bonus_script_clear) { TBL_PC* sd; bool flag = false; + if (!script_charid2sd(3,sd)) + return SCRIPT_CMD_FAILURE; + if (script_hasdata(st,2)) flag = script_getnum(st,2); - if (script_hasdata(st,3)) - sd = map_charid2sd(script_getnum(st,3)); - else - sd = script_rid2sd(st); - - if (sd == NULL) - return SCRIPT_CMD_FAILURE; - pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL)); return SCRIPT_CMD_SUCCESS; } @@ -21223,9 +21189,9 @@ BUILDIN_FUNC(bonus_script_clear) { * @author [Cydh], [Kichi] */ BUILDIN_FUNC(enable_command) { - TBL_PC* sd = script_rid2sd(st); + TBL_PC* sd; - if (!sd) + if (!script_rid2sd(sd)) return SCRIPT_CMD_FAILURE; sd->state.disable_atcommand_on_npc = 0; return SCRIPT_CMD_SUCCESS; @@ -21236,9 +21202,9 @@ BUILDIN_FUNC(enable_command) { * @author [Cydh], [Kichi] */ BUILDIN_FUNC(disable_command) { - TBL_PC* sd = script_rid2sd(st); + TBL_PC* sd; - if (!sd) + if (!script_rid2sd(sd)) return SCRIPT_CMD_FAILURE; sd->state.disable_atcommand_on_npc = 1; return SCRIPT_CMD_SUCCESS; @@ -21316,12 +21282,9 @@ BUILDIN_FUNC(getguildmember) * @author [Cydh] */ BUILDIN_FUNC(addspiritball) { - uint8 i, count = script_getnum(st,2); - uint16 duration = script_getnum(st,3); + uint8 i, count; + uint16 duration; struct map_session_data *sd = NULL; - - if (count == 0) - return SCRIPT_CMD_SUCCESS; if (script_hasdata(st,4)) { if (!script_isstring(st,4)) @@ -21330,10 +21293,17 @@ BUILDIN_FUNC(addspiritball) { sd = map_nick2sd(script_getstr(st,4),false); } else - sd = script_rid2sd(st); + script_rid2sd(sd); if (!sd) return SCRIPT_CMD_FAILURE; + count = script_getnum(st,2); + + if (count == 0) + return SCRIPT_CMD_SUCCESS; + + duration = script_getnum(st,3); + for (i = 0; i < count; i++) pc_addspiritball(sd,duration,10); return SCRIPT_CMD_SUCCESS; @@ -21346,12 +21316,9 @@ BUILDIN_FUNC(addspiritball) { * @author [Cydh] */ BUILDIN_FUNC(delspiritball) { - uint8 count = script_getnum(st,2); + uint8 count; struct map_session_data *sd = NULL; - if (count == 0) - count = 1; - if (script_hasdata(st,3)) { if (!script_isstring(st,3)) sd = map_charid2sd(script_getnum(st,3)); @@ -21359,10 +21326,15 @@ BUILDIN_FUNC(delspiritball) { sd = map_nick2sd(script_getstr(st,3),false); } else - sd = script_rid2sd(st); + script_rid2sd(sd); if (!sd) return SCRIPT_CMD_FAILURE; + count = script_getnum(st,2); + + if (count == 0) + count = 1; + pc_delspiritball(sd,count,0); return SCRIPT_CMD_SUCCESS; } @@ -21382,7 +21354,7 @@ BUILDIN_FUNC(countspiritball) { sd = map_nick2sd(script_getstr(st,2),false); } else - sd = script_rid2sd(st); + script_rid2sd(sd); if (!sd) return SCRIPT_CMD_FAILURE; script_pushint(st,sd->spiritball); @@ -21672,7 +21644,7 @@ BUILDIN_FUNC(ignoretimeout) else sd = map_nick2sd(script_getstr(st,3),false); } else - sd = script_rid2sd(st); + script_rid2sd(sd); if (!sd) return SCRIPT_CMD_FAILURE; @@ -22082,9 +22054,9 @@ BUILDIN_FUNC(getexp2) { * @author [secretdataz] **/ BUILDIN_FUNC(recalculatestat) { - TBL_PC* sd = script_rid2sd(st); + TBL_PC* sd; - if (sd == NULL) + if (!script_rid2sd(sd)) return SCRIPT_CMD_FAILURE; status_calc_pc(sd, SCO_FORCE); @@ -22093,11 +22065,11 @@ BUILDIN_FUNC(recalculatestat) { BUILDIN_FUNC(hateffect){ #if PACKETVER >= 20150513 - struct map_session_data* sd = script_rid2sd(st); + struct map_session_data* sd; bool enable; int i, effectID; - if( sd == NULL ) + if( !script_rid2sd(sd) ) return SCRIPT_CMD_FAILURE; effectID = script_getnum(st,2); @@ -22146,8 +22118,9 @@ BUILDIN_FUNC(hateffect){ BUILDIN_FUNC(getrandomoptinfo) { struct map_session_data *sd; int val; - int param = script_getnum(st, 2); - if ((sd = script_rid2sd(st)) != NULL && current_equip_item_index != -1 && current_equip_opt_index != -1 && sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].id) { + if (script_rid2sd(sd) && current_equip_item_index != -1 && current_equip_opt_index != -1 && sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].id) { + int param = script_getnum(st, 2); + switch (param) { case ROA_ID: val = sd->inventory.u.items_inventory[current_equip_item_index].option[current_equip_opt_index].id; @@ -22313,7 +22286,7 @@ BUILDIN_FUNC(jobcanentermap) { if (script_hasdata(st, 3)) { jobid = script_getnum(st, 3); } else { - if (!(sd = script_rid2sd(st))) { + if (!script_rid2sd(sd)) { script_pushint(st, false); return SCRIPT_CMD_FAILURE; }