summaryrefslogtreecommitdiff
path: root/src/map/npc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/map/npc.c')
-rw-r--r--src/map/npc.c3302
1 files changed, 1993 insertions, 1309 deletions
diff --git a/src/map/npc.c b/src/map/npc.c
index 46e6d0fd2..f1c6f4fbd 100644
--- a/src/map/npc.c
+++ b/src/map/npc.c
@@ -2,49 +2,46 @@
// See the LICENSE file
// Portions Copyright (c) Athena Dev Teams
-#include "../common/cbasetypes.h"
-#include "../common/timer.h"
-#include "../common/nullpo.h"
-#include "../common/malloc.h"
-#include "../common/showmsg.h"
-#include "../common/strlib.h"
-#include "../common/utils.h"
-#include "../common/ers.h"
-#include "../common/db.h"
-#include "../common/socket.h"
-#include "map.h"
-#include "log.h"
-#include "clif.h"
-#include "intif.h"
-#include "pc.h"
-#include "status.h"
-#include "itemdb.h"
-#include "script.h"
-#include "mob.h"
-#include "pet.h"
-#include "instance.h"
-#include "battle.h"
-#include "skill.h"
-#include "unit.h"
+#define HERCULES_CORE
+
+#include "../config/core.h" // NPC_SECURE_TIMEOUT_INPUT, NPC_SECURE_TIMEOUT_MENU, NPC_SECURE_TIMEOUT_NEXT, SECURE_NPCTIMEOUT, SECURE_NPCTIMEOUT_INTERVAL
#include "npc.h"
-#include "chat.h"
+#include <errno.h>
+#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <math.h>
#include <time.h>
-#include <errno.h>
-
-struct npc_data* fake_nd;
+#include "battle.h"
+#include "chat.h"
+#include "clif.h"
+#include "instance.h"
+#include "intif.h"
+#include "itemdb.h"
+#include "log.h"
+#include "map.h"
+#include "mob.h"
+#include "pc.h"
+#include "pet.h"
+#include "script.h"
+#include "skill.h"
+#include "status.h"
+#include "unit.h"
+#include "../common/HPM.h"
+#include "../common/cbasetypes.h"
+#include "../common/db.h"
+#include "../common/ers.h"
+#include "../common/malloc.h"
+#include "../common/nullpo.h"
+#include "../common/showmsg.h"
+#include "../common/socket.h"
+#include "../common/strlib.h"
+#include "../common/timer.h"
+#include "../common/utils.h"
-// linked list of npc source files
-struct npc_src_list {
- struct npc_src_list* next;
- char name[4]; // dynamic array, the structure is allocated with extra bytes (string length)
-};
-static struct npc_src_list* npc_src_files = NULL;
+struct npc_interface npc_s;
static int npc_id=START_NPC_NUM;
static int npc_warp=0;
@@ -54,50 +51,17 @@ static int npc_mob=0;
static int npc_delay_mob=0;
static int npc_cache_mob=0;
-/// Returns a new npc id that isn't being used in id_db.
-/// Fatal error if nothing is available.
-int npc_get_new_npc_id(void) {
- if( npc_id >= START_NPC_NUM && !iMap->blid_exists(npc_id) )
- return npc_id++;// available
- else {// find next id
- int base_id = npc_id;
- while( base_id != ++npc_id ) {
- if( npc_id < START_NPC_NUM )
- npc_id = START_NPC_NUM;
- if( !iMap->blid_exists(npc_id) )
- return npc_id++;// available
- }
- // full loop, nothing available
- ShowFatalError("npc_get_new_npc_id: All ids are taken. Exiting...");
- exit(1);
- }
-}
-
-static DBMap* ev_db; // const char* event_name -> struct event_data*
-static DBMap* npcname_db; // const char* npc_name -> struct npc_data*
-
-struct event_data {
- struct npc_data *nd;
- int pos;
-};
-
-static struct eri *timer_event_ers; //For the npc timer data. [Skotlex]
-
-/* hello */
static char *npc_last_path;
static char *npc_last_ref;
-
-struct npc_path_data {
- char* path;
- unsigned short references;
-};
struct npc_path_data *npc_last_npd;
-static DBMap *npc_path_db;
//For holding the view data of npc classes. [Skotlex]
static struct view_data npc_viewdb[MAX_NPC_CLASS];
static struct view_data npc_viewdb2[MAX_NPC_CLASS2_END-MAX_NPC_CLASS2_START];
+/* for speedup */
+unsigned int npc_market_qty[MAX_INVENTORY];
+
static struct script_event_s
{ //Holds pointers to the commonly executed scripts for speedup. [Skotlex]
struct event_data *event[UCHAR_MAX];
@@ -119,7 +83,26 @@ struct view_data* npc_get_viewdata(int class_)
return NULL;
}
-static int npc_isnear_sub(struct block_list* bl, va_list args) {
+/// Returns a new npc id that isn't being used in id_db.
+/// Fatal error if nothing is available.
+int npc_get_new_npc_id(void) {
+ if( npc_id >= START_NPC_NUM && !map->blid_exists(npc_id) )
+ return npc_id++;// available
+ else {// find next id
+ int base_id = npc_id;
+ while( base_id != ++npc_id ) {
+ if( npc_id < START_NPC_NUM )
+ npc_id = START_NPC_NUM;
+ if( !map->blid_exists(npc_id) )
+ return npc_id++;// available
+ }
+ // full loop, nothing available
+ ShowFatalError("npc_get_new_npc_id: All ids are taken. Exiting...");
+ exit(1);
+ }
+}
+
+int npc_isnear_sub(struct block_list* bl, va_list args) {
struct npc_data *nd = (struct npc_data*)bl;
if( nd->option & (OPTION_HIDE|OPTION_INVISIBLE) )
@@ -129,12 +112,11 @@ static int npc_isnear_sub(struct block_list* bl, va_list args) {
}
bool npc_isnear(struct block_list * bl) {
-
- if( battle_config.min_npc_vendchat_distance > 0 &&
- iMap->foreachinrange(npc_isnear_sub,bl, battle_config.min_npc_vendchat_distance, BL_NPC) )
- return true;
-
- return false;
+ if( battle_config.min_npc_vendchat_distance > 0
+ && map->foreachinrange(npc->isnear_sub,bl, battle_config.min_npc_vendchat_distance, BL_NPC) )
+ return true;
+
+ return false;
}
int npc_ontouch_event(struct map_session_data *sd, struct npc_data *nd)
@@ -147,8 +129,8 @@ int npc_ontouch_event(struct map_session_data *sd, struct npc_data *nd)
if( pc_ishiding(sd) )
return 1; // Can't trigger 'OnTouch_'. try 'OnTouch' later.
- snprintf(name, ARRAYLENGTH(name), "%s::%s", nd->exname, script_config.ontouch_name);
- return npc_event(sd,name,1);
+ snprintf(name, ARRAYLENGTH(name), "%s::%s", nd->exname, script->config.ontouch_name);
+ return npc->event(sd,name,1);
}
int npc_ontouch2_event(struct map_session_data *sd, struct npc_data *nd)
@@ -158,8 +140,8 @@ int npc_ontouch2_event(struct map_session_data *sd, struct npc_data *nd)
if( sd->areanpc_id == nd->bl.id )
return 0;
- snprintf(name, ARRAYLENGTH(name), "%s::%s", nd->exname, script_config.ontouch2_name);
- return npc_event(sd,name,2);
+ snprintf(name, ARRAYLENGTH(name), "%s::%s", nd->exname, script->config.ontouch2_name);
+ return npc->event(sd,name,2);
}
/*==========================================
@@ -178,13 +160,13 @@ int npc_enable_sub(struct block_list *bl, va_list ap)
if (nd->option&OPTION_INVISIBLE)
return 1;
- if( npc_ontouch_event(sd,nd) > 0 && npc_ontouch2_event(sd,nd) > 0 )
+ if( npc->ontouch_event(sd,nd) > 0 && npc->ontouch2_event(sd,nd) > 0 )
{ // failed to run OnTouch event, so just click the npc
if (sd->npc_id != 0)
return 0;
pc_stop_walking(sd,1);
- npc_click(sd,nd);
+ npc->click(sd,nd);
}
}
return 0;
@@ -195,7 +177,7 @@ int npc_enable_sub(struct block_list *bl, va_list ap)
*------------------------------------------*/
int npc_enable(const char* name, int flag)
{
- struct npc_data* nd = npc_name2id(name);
+ struct npc_data* nd = npc->name2id(name);
if ( nd == NULL ) {
ShowError("npc_enable: Attempted to %s a non-existing NPC '%s' (flag=%d).\n", (flag&3) ? "show" : "hide", name, flag);
@@ -222,8 +204,8 @@ int npc_enable(const char* name, int flag)
} else
clif->changeoption(&nd->bl);
- if( flag&3 && (nd->u.scr.xs >= 0 || nd->u.scr.ys >= 0) ) //check if player standing on a OnTouchArea
- iMap->foreachinarea( npc_enable_sub, nd->bl.m, nd->bl.x-nd->u.scr.xs, nd->bl.y-nd->u.scr.ys, nd->bl.x+nd->u.scr.xs, nd->bl.y+nd->u.scr.ys, BL_PC, nd );
+ if( flag&3 && (nd->u.scr.xs >= 0 || nd->u.scr.ys >= 0) ) //check if player standing on a OnTouchArea
+ map->foreachinarea( npc->enable_sub, nd->bl.m, nd->bl.x-nd->u.scr.xs, nd->bl.y-nd->u.scr.ys, nd->bl.x+nd->u.scr.xs, nd->bl.y+nd->u.scr.ys, BL_PC, nd );
return 0;
}
@@ -233,19 +215,19 @@ int npc_enable(const char* name, int flag)
*------------------------------------------*/
struct npc_data* npc_name2id(const char* name)
{
- return (struct npc_data *) strdb_get(npcname_db, name);
+ return (struct npc_data *) strdb_get(npc->name_db, name);
}
/**
* For the Secure NPC Timeout option (check config/Secure.h) [RR]
**/
-#ifdef SECURE_NPCTIMEOUT
/**
* Timer to check for idle time and timeout the dialog if necessary
**/
-int npc_rr_secure_timeout_timer(int tid, unsigned int tick, int id, intptr_t data) {
+int npc_rr_secure_timeout_timer(int tid, int64 tick, int id, intptr_t data) {
+#ifdef SECURE_NPCTIMEOUT
struct map_session_data* sd = NULL;
unsigned int timeout = NPC_SECURE_TIMEOUT_NEXT;
- if( (sd = iMap->id2sd(id)) == NULL || !sd->npc_id ) {
+ if( (sd = map->id2sd(id)) == NULL || !sd->npc_id ) {
if( sd ) sd->npc_idle_timer = INVALID_TIMER;
return 0;//Not logged in anymore OR no longer attached to a npc
}
@@ -276,10 +258,10 @@ int npc_rr_secure_timeout_timer(int tid, unsigned int tick, int id, intptr_t dat
clif->scriptclear(sd,sd->npc_id);
sd->npc_idle_timer = INVALID_TIMER;
} else //Create a new instance of ourselves to continue
- 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_timer = timer->add(timer->gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc->secure_timeout_timer,sd->bl.id,0);
+#endif
return 0;
}
-#endif
/*==========================================
* Dequeue event and add timer for execution (100ms)
@@ -294,7 +276,7 @@ int npc_event_dequeue(struct map_session_data* sd)
sd->state.using_fake_npc = 0;
}
if (sd->st) {
- script_free_state(sd->st);
+ script->free_state(sd->st);
sd->st = NULL;
}
sd->npc_id = 0;
@@ -313,24 +295,39 @@ int npc_event_dequeue(struct map_session_data* sd)
return 1;
}
+/**
+ * @see DBCreateData
+ */
+DBData npc_event_export_create(DBKey key, va_list args)
+{
+ struct linkdb_node** head_ptr;
+ CREATE(head_ptr, struct linkdb_node*, 1);
+ *head_ptr = NULL;
+ return DB->ptr2data(head_ptr);
+}
+
/*==========================================
* exports a npc event label
* called from npc_parse_script
*------------------------------------------*/
-static int npc_event_export(struct npc_data *nd, int i)
+int npc_event_export(struct npc_data *nd, int i)
{
char* lname = nd->u.scr.label_list[i].name;
int pos = nd->u.scr.label_list[i].pos;
if ((lname[0] == 'O' || lname[0] == 'o') && (lname[1] == 'N' || lname[1] == 'n')) {
struct event_data *ev;
+ struct linkdb_node **label_linkdb = NULL;
char buf[EVENT_NAME_LENGTH];
snprintf(buf, ARRAYLENGTH(buf), "%s::%s", nd->exname, lname);
+ if (strdb_exists(npc->ev_db, buf)) // There was already another event of the same name?
+ return 1;
// generate the data and insert it
CREATE(ev, struct event_data, 1);
ev->nd = nd;
ev->pos = pos;
- if (strdb_put(ev_db, buf, ev)) // There was already another event of the same name?
- return 1;
+ strdb_put(npc->ev_db, buf, ev);
+ label_linkdb = strdb_ensure(npc->ev_label_db, lname, npc->event_export_create);
+ linkdb_insert(label_linkdb, nd, ev);
}
return 0;
}
@@ -340,99 +337,80 @@ int npc_event_sub(struct map_session_data* sd, struct event_data* ev, const char
/**
* Exec name (NPC events) on player or global
* Do on all NPC when called with foreach
- * @see DBApply
*/
-int npc_event_doall_sub(DBKey key, DBData *data, va_list ap)
+void npc_event_doall_sub(void *key, void *data, va_list ap)
{
- const char* p = key.str;
- struct event_data* ev;
+ struct event_data* ev = data;
int* c;
const char* name;
int rid;
- nullpo_ret(ev = DB->data2ptr(data));
- nullpo_ret(c = va_arg(ap, int *));
- nullpo_ret(name = va_arg(ap, const char *));
+ nullpo_retv(c = va_arg(ap, int*));
+ nullpo_retv(name = va_arg(ap, const char*));
rid = va_arg(ap, int);
- p = strchr(p, ':'); // match only the event name
- if( p && strcmpi(name, p) == 0 /* && !ev->nd->src_id */ ) // Do not run on duplicates. [Paradox924X]
+ if (ev /* && !ev->nd->src_id */) // Do not run on duplicates. [Paradox924X]
{
- if(rid) // a player may only have 1 script running at the same time
- npc_event_sub(iMap->id2sd(rid),ev,key.str);
- else
- run_script(ev->nd->u.scr.script,ev->pos,rid,ev->nd->bl.id);
+ if(rid) { // a player may only have 1 script running at the same time
+ char buf[EVENT_NAME_LENGTH];
+ snprintf(buf, ARRAYLENGTH(buf), "%s::%s", ev->nd->exname, name);
+ npc->event_sub(map->id2sd(rid), ev, buf);
+ }
+ else {
+ script->run(ev->nd->u.scr.script, ev->pos, rid, ev->nd->bl.id);
+ }
(*c)++;
}
-
- return 0;
}
-/**
- * @see DBApply
- */
-static int npc_event_do_sub(DBKey key, DBData *data, va_list ap)
+// runs the specified event (supports both single-npc and global events)
+int npc_event_do(const char* name)
{
- const char* p = key.str;
- struct event_data* ev;
- int* c;
- const char* name;
-
- nullpo_ret(ev = DB->data2ptr(data));
- nullpo_ret(c = va_arg(ap, int *));
- nullpo_ret(name = va_arg(ap, const char *));
-
- if( p && strcmpi(name, p) == 0 ) {
- run_script(ev->nd->u.scr.script,ev->pos,0,ev->nd->bl.id);
- (*c)++;
+ if( name[0] == ':' && name[1] == ':' ) {
+ return npc->event_doall(name+2); // skip leading "::"
+ }
+ else {
+ struct event_data *ev = strdb_get(npc->ev_db, name);
+ if (ev) {
+ script->run(ev->nd->u.scr.script, ev->pos, 0, ev->nd->bl.id);
+ return 1;
+ }
}
-
return 0;
}
-// runs the specified event (supports both single-npc and global events)
-int npc_event_do(const char* name)
+// runs the specified event, with a RID attached (global only)
+int npc_event_doall_id(const char* name, int rid)
{
int c = 0;
+ struct linkdb_node **label_linkdb = strdb_get(npc->ev_label_db, name);
- if( name[0] == ':' && name[1] == ':' )
- ev_db->foreach(ev_db,npc_event_doall_sub,&c,name,0);
- else
- ev_db->foreach(ev_db,npc_event_do_sub,&c,name);
+ if (label_linkdb == NULL)
+ return 0;
+ linkdb_foreach(label_linkdb, npc->event_doall_sub, &c, name, rid);
return c;
}
// runs the specified event (global only)
int npc_event_doall(const char* name)
{
- return npc_event_doall_id(name, 0);
-}
-
-// runs the specified event, with a RID attached (global only)
-int npc_event_doall_id(const char* name, int rid)
-{
- int c = 0;
- char buf[64];
- safesnprintf(buf, sizeof(buf), "::%s", name);
- ev_db->foreach(ev_db,npc_event_doall_sub,&c,buf,rid);
- return c;
+ return npc->event_doall_id(name, 0);
}
/*==========================================
* Clock event execution
* OnMinute/OnClock/OnHour/OnDay/OnDDHHMM
*------------------------------------------*/
-int npc_event_do_clock(int tid, unsigned int tick, int id, intptr_t data)
-{
+int npc_event_do_clock(int tid, int64 tick, int id, intptr_t data) {
static struct tm ev_tm_b; // tracks previous execution time
- time_t timer;
+ time_t clock;
struct tm* t;
char buf[64];
int c = 0;
- timer = time(NULL);
- t = localtime(&timer);
+ clock = time(NULL);
+ t = localtime(&clock);
if (t->tm_min != ev_tm_b.tm_min ) {
char* day;
@@ -449,37 +427,40 @@ int npc_event_do_clock(int tid, unsigned int tick, int id, intptr_t data)
}
sprintf(buf,"OnMinute%02d",t->tm_min);
- c += npc_event_doall(buf);
+ c += npc->event_doall(buf);
sprintf(buf,"OnClock%02d%02d",t->tm_hour,t->tm_min);
- c += npc_event_doall(buf);
+ c += npc->event_doall(buf);
sprintf(buf,"On%s%02d%02d",day,t->tm_hour,t->tm_min);
- c += npc_event_doall(buf);
+ c += npc->event_doall(buf);
}
if (t->tm_hour != ev_tm_b.tm_hour) {
sprintf(buf,"OnHour%02d",t->tm_hour);
- c += npc_event_doall(buf);
+ c += npc->event_doall(buf);
}
if (t->tm_mday != ev_tm_b.tm_mday) {
sprintf(buf,"OnDay%02d%02d",t->tm_mon+1,t->tm_mday);
- c += npc_event_doall(buf);
+ c += npc->event_doall(buf);
}
memcpy(&ev_tm_b,t,sizeof(ev_tm_b));
return c;
}
-/*==========================================
- * OnInit Event execution (the start of the event and watch)
- *------------------------------------------*/
-void npc_event_do_oninit(void)
+/**
+ * OnInit event execution (the start of the event and watch)
+ * @param reload Is the server reloading?
+ **/
+void npc_event_do_oninit( bool reload )
{
- ShowStatus("Event '"CL_WHITE"OnInit"CL_RESET"' executed with '"CL_WHITE"%d"CL_RESET"' NPCs."CL_CLL"\n", npc_event_doall("OnInit"));
+ ShowStatus("Event '"CL_WHITE"OnInit"CL_RESET"' executed with '"CL_WHITE"%d"CL_RESET"' NPCs."CL_CLL"\n", npc->event_doall("OnInit"));
- iTimer->add_timer_interval(iTimer->gettick()+100,npc_event_do_clock,0,0,1000);
+ // This interval has already been added on startup
+ if( !reload )
+ timer->add_interval(timer->gettick()+100,npc->event_do_clock,0,0,1000);
}
/*==========================================
@@ -488,10 +469,10 @@ void npc_event_do_oninit(void)
*------------------------------------------*/
int npc_timerevent_export(struct npc_data *nd, int i)
{
- int t = 0, k = 0;
+ int t = 0, len = 0;
char *lname = nd->u.scr.label_list[i].name;
int pos = nd->u.scr.label_list[i].pos;
- if (sscanf(lname, "OnTimer%d%n", &t, &k) == 1 && lname[k] == '\0') {
+ if (sscanf(lname, "OnTimer%d%n", &t, &len) == 1 && lname[len] == '\0') {
// Timer event
struct npc_timerevent_list *te = nd->u.scr.timer_event;
int j, k = nd->u.scr.timeramount;
@@ -522,25 +503,22 @@ struct timer_event_data {
/*==========================================
* triger 'OnTimerXXXX' events
*------------------------------------------*/
-int npc_timerevent(int tid, unsigned int tick, int id, intptr_t data)
-{
+int npc_timerevent(int tid, int64 tick, int id, intptr_t data) {
int old_rid, old_timer;
- unsigned int old_tick;
- struct npc_data* nd=(struct npc_data *)iMap->id2bl(id);
+ int64 old_tick;
+ struct npc_data* nd=(struct npc_data *)map->id2bl(id);
struct npc_timerevent_list *te;
struct timer_event_data *ted = (struct timer_event_data*)data;
struct map_session_data *sd=NULL;
- if( nd == NULL )
- {
+ if( nd == NULL ) {
ShowError("npc_timerevent: NPC not found??\n");
return 0;
}
- if( ted->rid && !(sd = iMap->id2sd(ted->rid)) )
- {
+ if( ted->rid && !(sd = map->id2sd(ted->rid)) ) {
ShowError("npc_timerevent: Attached player not found.\n");
- ers_free(timer_event_ers, ted);
+ ers_free(npc->timer_event_ers, ted);
return 0;
}
@@ -550,9 +528,9 @@ int npc_timerevent(int tid, unsigned int tick, int id, intptr_t data)
old_timer = nd->u.scr.timer;
// Set the values of the timer
- nd->u.scr.rid = sd?sd->bl.id:0; //attached rid
- nd->u.scr.timertick = tick; //current time tick
- nd->u.scr.timer = ted->time; //total time from beginning to now
+ nd->u.scr.rid = sd?sd->bl.id:0; //attached rid
+ nd->u.scr.timertick = tick; //current time tick
+ nd->u.scr.timer = ted->time; //total time from beginning to now
// Locate the event
te = nd->u.scr.timer_event + ted->next;
@@ -561,13 +539,12 @@ int npc_timerevent(int tid, unsigned int tick, int id, intptr_t data)
ted->next++;
if( nd->u.scr.timeramount > ted->next )
{
- int next;
- next = nd->u.scr.timer_event[ ted->next ].timer - nd->u.scr.timer_event[ ted->next - 1 ].timer;
+ int next = nd->u.scr.timer_event[ ted->next ].timer - nd->u.scr.timer_event[ ted->next - 1 ].timer;
ted->time += next;
if( sd )
- sd->npc_timer_id = iTimer->add_timer(tick+next,npc_timerevent,id,(intptr_t)ted);
+ sd->npc_timer_id = timer->add(tick+next,npc->timerevent,id,(intptr_t)ted);
else
- nd->u.scr.timerid = iTimer->add_timer(tick+next,npc_timerevent,id,(intptr_t)ted);
+ nd->u.scr.timerid = timer->add(tick+next,npc->timerevent,id,(intptr_t)ted);
}
else
{
@@ -576,11 +553,11 @@ int npc_timerevent(int tid, unsigned int tick, int id, intptr_t data)
else
nd->u.scr.timerid = INVALID_TIMER;
- ers_free(timer_event_ers, ted);
+ ers_free(npc->timer_event_ers, ted);
}
// Run the script
- run_script(nd->u.scr.script,te->pos,nd->u.scr.rid,nd->bl.id);
+ script->run(nd->u.scr.script,te->pos,nd->u.scr.rid,nd->bl.id);
nd->u.scr.rid = old_rid; // Attached-rid should be restored anyway.
if( sd )
@@ -594,10 +571,9 @@ int npc_timerevent(int tid, unsigned int tick, int id, intptr_t data)
/*==========================================
* Start/Resume NPC timer
*------------------------------------------*/
-int npc_timerevent_start(struct npc_data* nd, int rid)
-{
+int npc_timerevent_start(struct npc_data* nd, int rid) {
int j;
- unsigned int tick = iTimer->gettick();
+ int64 tick = timer->gettick();
struct map_session_data *sd = NULL; //Player to whom script is attached.
nullpo_ret(nd);
@@ -605,45 +581,41 @@ int npc_timerevent_start(struct npc_data* nd, int rid)
// Check if there is an OnTimer Event
ARR_FIND( 0, nd->u.scr.timeramount, j, nd->u.scr.timer_event[j].timer > nd->u.scr.timer );
- if( nd->u.scr.rid > 0 && !(sd = iMap->id2sd(nd->u.scr.rid)) )
- { // Failed to attach timer to this player.
+ if( nd->u.scr.rid > 0 && !(sd = map->id2sd(nd->u.scr.rid)) ) {
+ // Failed to attach timer to this player.
ShowError("npc_timerevent_start: Attached player not found!\n");
return 1;
}
-
// Check if timer is already started.
- if( sd )
- {
+ if( sd ) {
if( sd->npc_timer_id != INVALID_TIMER )
return 0;
- }
- else if( nd->u.scr.timerid != INVALID_TIMER || nd->u.scr.timertick )
+ } else if( nd->u.scr.timerid != INVALID_TIMER || nd->u.scr.timertick )
return 0;
- if (j < nd->u.scr.timeramount)
- {
+ if (j < nd->u.scr.timeramount) {
int next;
struct timer_event_data *ted;
// Arrange for the next event
- ted = ers_alloc(timer_event_ers, struct timer_event_data);
+ ted = ers_alloc(npc->timer_event_ers, struct timer_event_data);
ted->next = j; // Set event index
ted->time = nd->u.scr.timer_event[j].timer;
next = nd->u.scr.timer_event[j].timer - nd->u.scr.timer;
if( sd )
{
ted->rid = sd->bl.id; // Attach only the player if attachplayerrid was used.
- sd->npc_timer_id = iTimer->add_timer(tick+next,npc_timerevent,nd->bl.id,(intptr_t)ted);
+ sd->npc_timer_id = timer->add(tick+next,npc->timerevent,nd->bl.id,(intptr_t)ted);
}
else
{
ted->rid = 0;
nd->u.scr.timertick = tick; // Set when timer is started
- nd->u.scr.timerid = iTimer->add_timer(tick+next,npc_timerevent,nd->bl.id,(intptr_t)ted);
+ nd->u.scr.timerid = timer->add(tick+next,npc->timerevent,nd->bl.id,(intptr_t)ted);
}
- }
- else if (!sd)
- {
+
+ } else if (!sd) {
nd->u.scr.timertick = tick;
+
}
return 0;
@@ -659,12 +631,10 @@ int npc_timerevent_stop(struct npc_data* nd)
nullpo_ret(nd);
- if( nd->u.scr.rid && !(sd = iMap->id2sd(nd->u.scr.rid)) )
- {
+ if( nd->u.scr.rid && !(sd = map->id2sd(nd->u.scr.rid)) ) {
ShowError("npc_timerevent_stop: Attached player not found!\n");
return 1;
}
-
tid = sd?&sd->npc_timer_id:&nd->u.scr.timerid;
if( *tid == INVALID_TIMER && (sd || !nd->u.scr.timertick) ) // Nothing to stop
return 0;
@@ -672,16 +642,16 @@ int npc_timerevent_stop(struct npc_data* nd)
// Delete timer
if ( *tid != INVALID_TIMER )
{
- td = iTimer->get_timer(*tid);
+ td = timer->get(*tid);
if( td && td->data )
- ers_free(timer_event_ers, (void*)td->data);
- iTimer->delete_timer(*tid,npc_timerevent);
+ ers_free(npc->timer_event_ers, (void*)td->data);
+ timer->delete(*tid,npc->timerevent);
*tid = INVALID_TIMER;
}
if( !sd && nd->u.scr.timertick )
{
- nd->u.scr.timer += DIFF_TICK(iTimer->gettick(),nd->u.scr.timertick); // Set 'timer' to the time that has passed since the beginning of the timers
+ nd->u.scr.timer += DIFF_TICK32(timer->gettick(),nd->u.scr.timertick); // Set 'timer' to the time that has passed since the beginning of the timers
nd->u.scr.timertick = 0; // Set 'tick' to zero so that we know it's off.
}
@@ -696,19 +666,19 @@ void npc_timerevent_quit(struct map_session_data* sd)
struct npc_data* nd;
struct timer_event_data *ted;
- // Check timer existance
+ // Check timer existence
if( sd->npc_timer_id == INVALID_TIMER )
return;
- if( !(td = iTimer->get_timer(sd->npc_timer_id)) )
+ if( !(td = timer->get(sd->npc_timer_id)) )
{
sd->npc_timer_id = INVALID_TIMER;
return;
}
// Delete timer
- nd = (struct npc_data *)iMap->id2bl(td->id);
+ nd = (struct npc_data *)map->id2bl(td->id);
ted = (struct timer_event_data*)td->data;
- iTimer->delete_timer(sd->npc_timer_id, npc_timerevent);
+ timer->delete(sd->npc_timer_id, npc->timerevent);
sd->npc_timer_id = INVALID_TIMER;
// Execute OnTimerQuit
@@ -718,7 +688,7 @@ void npc_timerevent_quit(struct map_session_data* sd)
struct event_data *ev;
snprintf(buf, ARRAYLENGTH(buf), "%s::OnTimerQuit", nd->exname);
- ev = (struct event_data*)strdb_get(ev_db, buf);
+ ev = (struct event_data*)strdb_get(npc->ev_db, buf);
if( ev && ev->nd != nd )
{
ShowWarning("npc_timerevent_quit: Unable to execute \"OnTimerQuit\", two NPCs have the same event name [%s]!\n",buf);
@@ -727,7 +697,7 @@ void npc_timerevent_quit(struct map_session_data* sd)
if( ev )
{
int old_rid,old_timer;
- unsigned int old_tick;
+ int64 old_tick;
//Set timer related info.
old_rid = (nd->u.scr.rid == sd->bl.id ? 0 : nd->u.scr.rid); // Detach rid if the last attached player logged off.
@@ -735,11 +705,11 @@ void npc_timerevent_quit(struct map_session_data* sd)
old_timer = nd->u.scr.timer;
nd->u.scr.rid = sd->bl.id;
- nd->u.scr.timertick = iTimer->gettick();
+ nd->u.scr.timertick = timer->gettick();
nd->u.scr.timer = ted->time;
//Execute label
- run_script(nd->u.scr.script,ev->pos,sd->bl.id,nd->bl.id);
+ script->run(nd->u.scr.script,ev->pos,sd->bl.id,nd->bl.id);
//Restore previous data.
nd->u.scr.rid = old_rid;
@@ -747,23 +717,22 @@ void npc_timerevent_quit(struct map_session_data* sd)
nd->u.scr.timertick = old_tick;
}
}
- ers_free(timer_event_ers, ted);
+ ers_free(npc->timer_event_ers, ted);
}
/*==========================================
* Get the tick value of an NPC timer
* If it's stopped, return stopped time
*------------------------------------------*/
-int npc_gettimerevent_tick(struct npc_data* nd)
-{
- int tick;
+int64 npc_gettimerevent_tick(struct npc_data* nd) {
+ int64 tick;
nullpo_ret(nd);
// TODO: Get player attached timer's tick. Now we can just get it by using 'getnpctimer' inside OnTimer event.
tick = nd->u.scr.timer; // The last time it's active(start, stop or event trigger)
if( nd->u.scr.timertick ) // It's a running timer
- tick += DIFF_TICK(iTimer->gettick(), nd->u.scr.timertick);
+ tick += DIFF_TICK(timer->gettick(), nd->u.scr.timertick);
return tick;
}
@@ -785,11 +754,11 @@ int npc_settimerevent_tick(struct npc_data* nd, int newtimer)
nd->u.scr.rid = 0;
// Check if timer is started
- flag = (nd->u.scr.timerid != INVALID_TIMER);
+ flag = (nd->u.scr.timerid != INVALID_TIMER || nd->u.scr.timertick);
- if( flag ) npc_timerevent_stop(nd);
+ if( flag ) npc->timerevent_stop(nd);
nd->u.scr.timer = newtimer;
- if( flag ) npc_timerevent_start(nd, -1);
+ if( flag ) npc->timerevent_start(nd, -1);
nd->u.scr.rid = old_rid;
return 0;
@@ -804,7 +773,7 @@ int npc_event_sub(struct map_session_data* sd, struct event_data* ev, const char
ARR_FIND( 0, MAX_EVENTQUEUE, i, sd->eventqueue[i][0] == '\0' );
if( i < MAX_EVENTQUEUE )
{
- safestrncpy(sd->eventqueue[i],eventname,50); //Event enqueued.
+ safestrncpy(sd->eventqueue[i],eventname,EVENT_NAME_LENGTH); //Event enqueued.
return 0;
}
@@ -814,10 +783,10 @@ int npc_event_sub(struct map_session_data* sd, struct event_data* ev, const char
if( ev->nd->option&OPTION_INVISIBLE )
{
//Disabled npc, shouldn't trigger event.
- npc_event_dequeue(sd);
+ npc->event_dequeue(sd);
return 2;
}
- run_script(ev->nd->u.scr.script,ev->pos,sd->bl.id,ev->nd->bl.id);
+ script->run(ev->nd->u.scr.script,ev->pos,sd->bl.id,ev->nd->bl.id);
return 0;
}
@@ -826,7 +795,7 @@ int npc_event_sub(struct map_session_data* sd, struct event_data* ev, const char
*------------------------------------------*/
int npc_event(struct map_session_data* sd, const char* eventname, int ontouch)
{
- struct event_data* ev = (struct event_data*)strdb_get(ev_db, eventname);
+ struct event_data* ev = (struct event_data*)strdb_get(npc->ev_db, eventname);
struct npc_data *nd;
nullpo_ret(sd);
@@ -847,20 +816,19 @@ int npc_event(struct map_session_data* sd, const char* eventname, int ontouch)
break;
}
- return npc_event_sub(sd,ev,eventname);
+ return npc->event_sub(sd,ev,eventname);
}
/*==========================================
* Sub chk then execute area event type
*------------------------------------------*/
-int npc_touch_areanpc_sub(struct block_list *bl, va_list ap)
-{
+int npc_touch_areanpc_sub(struct block_list *bl, va_list ap) {
struct map_session_data *sd;
int pc_id;
char *name;
nullpo_ret(bl);
- nullpo_ret((sd = iMap->id2sd(bl->id)));
+ nullpo_ret((sd = map->id2sd(bl->id)));
pc_id = va_arg(ap,int);
name = va_arg(ap,char*);
@@ -872,7 +840,7 @@ int npc_touch_areanpc_sub(struct block_list *bl, va_list ap)
if( pc_id == sd->bl.id )
return 0;
- npc_event(sd,name,1);
+ npc->event(sd,name,1);
return 1;
}
@@ -881,9 +849,8 @@ int npc_touch_areanpc_sub(struct block_list *bl, va_list ap)
* Chk if sd is still touching his assigned npc.
* If not, it unsets it and searches for another player in range.
*------------------------------------------*/
-int npc_touchnext_areanpc(struct map_session_data* sd, bool leavemap)
-{
- struct npc_data *nd = iMap->id2nd(sd->touching_id);
+int npc_touchnext_areanpc(struct map_session_data* sd, bool leavemap) {
+ struct npc_data *nd = map->id2nd(sd->touching_id);
short xs, ys;
if( !nd || nd->touching_id != sd->bl.id )
@@ -900,8 +867,8 @@ int npc_touchnext_areanpc(struct map_session_data* sd, bool leavemap)
char name[EVENT_NAME_LENGTH];
nd->touching_id = sd->touching_id = 0;
- snprintf(name, ARRAYLENGTH(name), "%s::%s", nd->exname, script_config.ontouch_name);
- iMap->forcountinarea(npc_touch_areanpc_sub,nd->bl.m,nd->bl.x - xs,nd->bl.y - ys,nd->bl.x + xs,nd->bl.y + ys,1,BL_PC,sd->bl.id,name);
+ snprintf(name, ARRAYLENGTH(name), "%s::%s", nd->exname, script->config.ontouch_name);
+ map->forcountinarea(npc->touch_areanpc_sub,nd->bl.m,nd->bl.x - xs,nd->bl.y - ys,nd->bl.x + xs,nd->bl.y + ys,1,BL_PC,sd->bl.id,name);
}
return 0;
}
@@ -922,51 +889,53 @@ int npc_touch_areanpc(struct map_session_data* sd, int16 m, int16 x, int16 y)
//if(sd->npc_id)
// return 1;
- for(i=0;i<map[m].npc_num;i++) {
- if (map[m].npc[i]->option&OPTION_INVISIBLE) {
+ for(i=0;i<map->list[m].npc_num;i++) {
+ if (map->list[m].npc[i]->option&OPTION_INVISIBLE) {
f=0; // a npc was found, but it is disabled; don't print warning
continue;
}
- switch(map[m].npc[i]->subtype) {
+ switch(map->list[m].npc[i]->subtype) {
case WARP:
- xs=map[m].npc[i]->u.warp.xs;
- ys=map[m].npc[i]->u.warp.ys;
+ xs=map->list[m].npc[i]->u.warp.xs;
+ ys=map->list[m].npc[i]->u.warp.ys;
break;
case SCRIPT:
- xs=map[m].npc[i]->u.scr.xs;
- ys=map[m].npc[i]->u.scr.ys;
+ xs=map->list[m].npc[i]->u.scr.xs;
+ ys=map->list[m].npc[i]->u.scr.ys;
break;
default:
continue;
}
- if( x >= map[m].npc[i]->bl.x-xs && x <= map[m].npc[i]->bl.x+xs
- && y >= map[m].npc[i]->bl.y-ys && y <= map[m].npc[i]->bl.y+ys )
+ if( x >= map->list[m].npc[i]->bl.x-xs && x <= map->list[m].npc[i]->bl.x+xs
+ && y >= map->list[m].npc[i]->bl.y-ys && y <= map->list[m].npc[i]->bl.y+ys )
break;
}
- if( i == map[m].npc_num )
- {
+ if( i == map->list[m].npc_num ) {
if( f == 1 ) // no npc found
- ShowError("npc_touch_areanpc : stray NPC cell/NPC not found in the block on coordinates '%s',%d,%d\n", map[m].name, x, y);
+ ShowError("npc_touch_areanpc : stray NPC cell/NPC not found in the block on coordinates '%s',%d,%d\n", map->list[m].name, x, y);
return 1;
}
- switch(map[m].npc[i]->subtype) {
+ switch(map->list[m].npc[i]->subtype) {
case WARP:
if( pc_ishiding(sd) || (sd->sc.count && sd->sc.data[SC_CAMOUFLAGE]) )
break; // hidden chars cannot use warps
- pc->setpos(sd,map[m].npc[i]->u.warp.mapindex,map[m].npc[i]->u.warp.x,map[m].npc[i]->u.warp.y,CLR_OUTSIGHT);
+ pc->setpos(sd,map->list[m].npc[i]->u.warp.mapindex,map->list[m].npc[i]->u.warp.x,map->list[m].npc[i]->u.warp.y,CLR_OUTSIGHT);
break;
case SCRIPT:
- for (j = i; j < map[m].npc_num; j++) {
- if (map[m].npc[j]->subtype != WARP) {
+ for (j = i; j < map->list[m].npc_num; j++) {
+ if (map->list[m].npc[j]->subtype != WARP) {
continue;
}
- if ((sd->bl.x >= (map[m].npc[j]->bl.x - map[m].npc[j]->u.warp.xs) && sd->bl.x <= (map[m].npc[j]->bl.x + map[m].npc[j]->u.warp.xs)) &&
- (sd->bl.y >= (map[m].npc[j]->bl.y - map[m].npc[j]->u.warp.ys) && sd->bl.y <= (map[m].npc[j]->bl.y + map[m].npc[j]->u.warp.ys))) {
+ if ((sd->bl.x >= (map->list[m].npc[j]->bl.x - map->list[m].npc[j]->u.warp.xs)
+ && sd->bl.x <= (map->list[m].npc[j]->bl.x + map->list[m].npc[j]->u.warp.xs))
+ && (sd->bl.y >= (map->list[m].npc[j]->bl.y - map->list[m].npc[j]->u.warp.ys)
+ && sd->bl.y <= (map->list[m].npc[j]->bl.y + map->list[m].npc[j]->u.warp.ys))
+ ) {
if( pc_ishiding(sd) || (sd->sc.count && sd->sc.data[SC_CAMOUFLAGE]) )
break; // hidden chars cannot use warps
- pc->setpos(sd,map[m].npc[j]->u.warp.mapindex,map[m].npc[j]->u.warp.x,map[m].npc[j]->u.warp.y,CLR_OUTSIGHT);
+ pc->setpos(sd,map->list[m].npc[j]->u.warp.mapindex,map->list[m].npc[j]->u.warp.x,map->list[m].npc[j]->u.warp.y,CLR_OUTSIGHT);
found_warp = 1;
break;
}
@@ -976,16 +945,16 @@ int npc_touch_areanpc(struct map_session_data* sd, int16 m, int16 x, int16 y)
break;
}
- if( npc_ontouch_event(sd,map[m].npc[i]) > 0 && npc_ontouch2_event(sd,map[m].npc[i]) > 0 )
+ if( npc->ontouch_event(sd,map->list[m].npc[i]) > 0 && npc->ontouch2_event(sd,map->list[m].npc[i]) > 0 )
{ // failed to run OnTouch event, so just click the npc
- struct unit_data *ud = unit_bl2ud(&sd->bl);
+ struct unit_data *ud = unit->bl2ud(&sd->bl);
if( ud && ud->walkpath.path_pos < ud->walkpath.path_len )
{ // Since walktimer always == INVALID_TIMER at this time, we stop walking manually. [Inkfish]
clif->fixpos(&sd->bl);
ud->walkpath.path_pos = ud->walkpath.path_len;
}
- sd->areanpc_id = map[m].npc[i]->bl.id;
- npc_click(sd,map[m].npc[i]);
+ sd->areanpc_id = map->list[m].npc[i]->bl.id;
+ npc->click(sd,map->list[m].npc[i]);
}
break;
}
@@ -1001,46 +970,45 @@ int npc_touch_areanpc2(struct mob_data *md)
struct event_data* ev;
int xs, ys;
- for( i = 0; i < map[m].npc_num; i++ ) {
- if( map[m].npc[i]->option&OPTION_INVISIBLE )
+ for( i = 0; i < map->list[m].npc_num; i++ ) {
+ if( map->list[m].npc[i]->option&OPTION_INVISIBLE )
continue;
- switch( map[m].npc[i]->subtype ) {
+ switch( map->list[m].npc[i]->subtype ) {
case WARP:
if( !( battle_config.mob_warp&1 ) )
continue;
- xs = map[m].npc[i]->u.warp.xs;
- ys = map[m].npc[i]->u.warp.ys;
+ xs = map->list[m].npc[i]->u.warp.xs;
+ ys = map->list[m].npc[i]->u.warp.ys;
break;
case SCRIPT:
- xs = map[m].npc[i]->u.scr.xs;
- ys = map[m].npc[i]->u.scr.ys;
+ xs = map->list[m].npc[i]->u.scr.xs;
+ ys = map->list[m].npc[i]->u.scr.ys;
break;
default:
continue; // Keep Searching
}
- if( x >= map[m].npc[i]->bl.x-xs && x <= map[m].npc[i]->bl.x+xs && y >= map[m].npc[i]->bl.y-ys && y <= map[m].npc[i]->bl.y+ys )
- { // In the npc touch area
- switch( map[m].npc[i]->subtype )
- {
+ if( x >= map->list[m].npc[i]->bl.x-xs && x <= map->list[m].npc[i]->bl.x+xs && y >= map->list[m].npc[i]->bl.y-ys && y <= map->list[m].npc[i]->bl.y+ys ) {
+ // In the npc touch area
+ switch( map->list[m].npc[i]->subtype ) {
case WARP:
- xs = iMap->mapindex2mapid(map[m].npc[i]->u.warp.mapindex);
+ xs = map->mapindex2mapid(map->list[m].npc[i]->u.warp.mapindex);
if( m < 0 )
break; // Cannot Warp between map servers
- if( unit_warp(&md->bl, xs, map[m].npc[i]->u.warp.x, map[m].npc[i]->u.warp.y, CLR_OUTSIGHT) == 0 )
+ if( unit->warp(&md->bl, xs, map->list[m].npc[i]->u.warp.x, map->list[m].npc[i]->u.warp.y, CLR_OUTSIGHT) == 0 )
return 1; // Warped
break;
case SCRIPT:
- if( map[m].npc[i]->bl.id == md->areanpc_id )
+ if( map->list[m].npc[i]->bl.id == md->areanpc_id )
break; // Already touch this NPC
- snprintf(eventname, ARRAYLENGTH(eventname), "%s::OnTouchNPC", map[m].npc[i]->exname);
- if( (ev = (struct event_data*)strdb_get(ev_db, eventname)) == NULL || ev->nd == NULL )
+ snprintf(eventname, ARRAYLENGTH(eventname), "%s::OnTouchNPC", map->list[m].npc[i]->exname);
+ if( (ev = (struct event_data*)strdb_get(npc->ev_db, eventname)) == NULL || ev->nd == NULL )
break; // No OnTouchNPC Event
- md->areanpc_id = map[m].npc[i]->bl.id;
+ md->areanpc_id = map->list[m].npc[i]->bl.id;
id = md->bl.id; // Stores Unique ID
- run_script(ev->nd->u.scr.script, ev->pos, md->bl.id, ev->nd->bl.id);
- if( iMap->id2md(id) == NULL ) return 1; // Not Warped, but killed
+ script->run(ev->nd->u.scr.script, ev->pos, md->bl.id, ev->nd->bl.id);
+ if( map->id2md(id) == NULL ) return 1; // Not Warped, but killed
break;
}
@@ -1055,8 +1023,7 @@ int npc_touch_areanpc2(struct mob_data *md)
//Flag determines the type of object to check for:
//&1: NPC Warps
//&2: NPCs with on-touch events.
-int npc_check_areanpc(int flag, int16 m, int16 x, int16 y, int16 range)
-{
+int npc_check_areanpc(int flag, int16 m, int16 x, int16 y, int16 range) {
int i;
int x0,y0,x1,y1;
int xs,ys;
@@ -1064,49 +1031,49 @@ int npc_check_areanpc(int flag, int16 m, int16 x, int16 y, int16 range)
if (range < 0) return 0;
x0 = max(x-range, 0);
y0 = max(y-range, 0);
- x1 = min(x+range, map[m].xs-1);
- y1 = min(y+range, map[m].ys-1);
+ x1 = min(x+range, map->list[m].xs-1);
+ y1 = min(y+range, map->list[m].ys-1);
//First check for npc_cells on the range given
i = 0;
for (ys = y0; ys <= y1 && !i; ys++) {
- for(xs = x0; xs <= x1 && !i; xs++){
- if (iMap->getcell(m,xs,ys,CELL_CHKNPC))
+ for(xs = x0; xs <= x1 && !i; xs++) {
+ if (map->getcell(m,xs,ys,CELL_CHKNPC))
i = 1;
}
}
if (!i) return 0; //No NPC_CELLs.
//Now check for the actual NPC on said range.
- for(i=0;i<map[m].npc_num;i++) {
- if (map[m].npc[i]->option&OPTION_INVISIBLE)
+ for(i=0;i<map->list[m].npc_num;i++) {
+ if (map->list[m].npc[i]->option&OPTION_INVISIBLE)
continue;
- switch(map[m].npc[i]->subtype) {
+ switch(map->list[m].npc[i]->subtype) {
case WARP:
if (!(flag&1))
continue;
- xs=map[m].npc[i]->u.warp.xs;
- ys=map[m].npc[i]->u.warp.ys;
+ xs=map->list[m].npc[i]->u.warp.xs;
+ ys=map->list[m].npc[i]->u.warp.ys;
break;
case SCRIPT:
if (!(flag&2))
continue;
- xs=map[m].npc[i]->u.scr.xs;
- ys=map[m].npc[i]->u.scr.ys;
+ xs=map->list[m].npc[i]->u.scr.xs;
+ ys=map->list[m].npc[i]->u.scr.ys;
break;
default:
continue;
}
- if( x1 >= map[m].npc[i]->bl.x-xs && x0 <= map[m].npc[i]->bl.x+xs
- && y1 >= map[m].npc[i]->bl.y-ys && y0 <= map[m].npc[i]->bl.y+ys )
+ if( x1 >= map->list[m].npc[i]->bl.x-xs && x0 <= map->list[m].npc[i]->bl.x+xs
+ && y1 >= map->list[m].npc[i]->bl.y-ys && y0 <= map->list[m].npc[i]->bl.y+ys )
break; // found a npc
}
- if (i==map[m].npc_num)
+ if (i==map->list[m].npc_num)
return 0;
- return (map[m].npc[i]->bl.id);
+ return (map->list[m].npc[i]->bl.id);
}
/*==========================================
@@ -1141,7 +1108,7 @@ struct npc_data* npc_checknear(struct map_session_data* sd, struct block_list* b
*------------------------------------------*/
int npc_globalmessage(const char* name, const char* mes)
{
- struct npc_data* nd = npc_name2id(name);
+ struct npc_data* nd = npc->name2id(name);
char temp[100];
if (!nd)
@@ -1154,28 +1121,27 @@ int npc_globalmessage(const char* name, const char* mes)
}
// MvP tomb [GreenBox]
-void run_tomb(struct map_session_data* sd, struct npc_data* nd)
-{
+void run_tomb(struct map_session_data* sd, struct npc_data* nd) {
char buffer[200];
- char time[10];
+ char time[10];
- strftime(time, sizeof(time), "%H:%M", localtime(&nd->u.tomb.kill_time));
+ strftime(time, sizeof(time), "%H:%M", localtime(&nd->u.tomb.kill_time));
// TODO: Find exact color?
- snprintf(buffer, sizeof(buffer), msg_txt(657), nd->u.tomb.md->db->name);
- clif->scriptmes(sd, nd->bl.id, buffer);
+ snprintf(buffer, sizeof(buffer), msg_txt(857), nd->u.tomb.md->db->name); // "[ ^EE0000%s^000000 ]"
+ clif->scriptmes(sd, nd->bl.id, buffer);
- clif->scriptmes(sd, nd->bl.id, msg_txt(658));
+ clif->scriptmes(sd, nd->bl.id, msg_txt(858)); // "Has met its demise"
- snprintf(buffer, sizeof(buffer), msg_txt(659), time);
- clif->scriptmes(sd, nd->bl.id, buffer);
+ snprintf(buffer, sizeof(buffer), msg_txt(859), time); // "Time of death : ^EE0000%s^000000"
+ clif->scriptmes(sd, nd->bl.id, buffer);
- clif->scriptmes(sd, nd->bl.id, msg_txt(660));
+ clif->scriptmes(sd, nd->bl.id, msg_txt(860)); // "Defeated by"
- snprintf(buffer, sizeof(buffer), msg_txt(661), nd->u.tomb.killer_name[0] ? nd->u.tomb.killer_name : "Unknown");
- clif->scriptmes(sd, nd->bl.id, buffer);
+ snprintf(buffer, sizeof(buffer), msg_txt(861), nd->u.tomb.killer_name[0] ? nd->u.tomb.killer_name : msg_txt(15)); // "[^EE0000%s^000000]" / "Unknown"
+ clif->scriptmes(sd, nd->bl.id, buffer);
- clif->scriptclose(sd, nd->bl.id);
+ clif->scriptclose(sd, nd->bl.id);
}
/*==========================================
@@ -1186,14 +1152,27 @@ int npc_click(struct map_session_data* sd, struct npc_data* nd)
{
nullpo_retr(1, sd);
+ // This usually happens when the player clicked on a NPC that has the view id
+ // of a mob, to activate this kind of npc it's needed to be in a 2,2 range
+ // from it. If the OnTouch area of a npc, coincides with the 2,2 range of
+ // another it's expected that the OnTouch event be put first in stack, because
+ // unit_walktoxy_timer is executed before any other function in this case.
+ // So it's best practice to put an 'end;' before OnTouch events in npcs that
+ // have view ids of mobs to avoid this "issue" [Panikon]
if (sd->npc_id != 0) {
- ShowError("npc_click: npc_id != 0\n");
+ // The player clicked a npc after entering an OnTouch area
+ if( sd->areanpc_id != sd->npc_id )
+ ShowError("npc_click: npc_id != 0\n");
+
return 1;
}
- if(!nd) return 1;
- if ((nd = npc_checknear(sd,&nd->bl)) == NULL)
+ if( !nd )
return 1;
+
+ if ((nd = npc->checknear(sd,&nd->bl)) == NULL)
+ return 1;
+
//Hidden/Disabled npc.
if (nd->class_ < 0 || nd->option&(OPTION_INVISIBLE|OPTION_HIDE))
return 1;
@@ -1206,10 +1185,14 @@ int npc_click(struct map_session_data* sd, struct npc_data* nd)
clif->cashshop_show(sd,nd);
break;
case SCRIPT:
- run_script(nd->u.scr.script,0,sd->bl.id,nd->bl.id);
+ if( nd->u.scr.shop && nd->u.scr.shop->items && nd->u.scr.trader ) {
+ if( !npc->trader_open(sd,nd) )
+ return 1;
+ } else
+ script->run(nd->u.scr.script,0,sd->bl.id,nd->bl.id);
break;
case TOMB:
- run_tomb(sd,nd);
+ npc->run_tomb(sd,nd);
break;
}
@@ -1219,22 +1202,22 @@ int npc_click(struct map_session_data* sd, struct npc_data* nd)
/*==========================================
*
*------------------------------------------*/
-int npc_scriptcont(struct map_session_data* sd, int id, bool closing)
-{
+int npc_scriptcont(struct map_session_data* sd, int id, bool closing) {
+ struct block_list *target = map->id2bl(id);
nullpo_retr(1, sd);
if( id != sd->npc_id ){
- TBL_NPC* nd_sd=(TBL_NPC*)iMap->id2bl(sd->npc_id);
- TBL_NPC* nd=(TBL_NPC*)iMap->id2bl(id);
+ TBL_NPC* nd_sd=(TBL_NPC*)map->id2bl(sd->npc_id);
+ TBL_NPC* nd = BL_CAST(BL_NPC, target);
ShowDebug("npc_scriptcont: %s (sd->npc_id=%d) is not %s (id=%d).\n",
nd_sd?(char*)nd_sd->name:"'Unknown NPC'", (int)sd->npc_id,
nd?(char*)nd->name:"'Unknown NPC'", (int)id);
return 1;
}
-
- if(id != fake_nd->bl.id) { // Not item script
- if ((npc_checknear(sd,iMap->id2bl(id))) == NULL){
- ShowWarning("npc_scriptcont: failed npc_checknear test.\n");
+
+ if(id != npc->fake_nd->bl.id) { // Not item script
+ if ((npc->checknear(sd,target)) == NULL){
+ ShowWarning("npc_scriptcont: failed npc->checknear test.\n");
return 1;
}
}
@@ -1245,19 +1228,22 @@ int npc_scriptcont(struct map_session_data* sd, int id, bool closing)
/**
* Update the last NPC iteration
**/
- sd->npc_idle_tick = iTimer->gettick();
+ sd->npc_idle_tick = timer->gettick();
#endif
/**
* WPE can get to this point with a progressbar; we deny it.
**/
- if( sd->progressbar.npc_id && DIFF_TICK(sd->progressbar.timeout,iTimer->gettick()) > 0 )
+ if( sd->progressbar.npc_id && DIFF_TICK(sd->progressbar.timeout,timer->gettick()) > 0 )
return 1;
-
+
+ if( !sd->st )
+ return 1;
+
if( closing && sd->st->state == CLOSE )
sd->st->state = END;
- run_script_main(sd->st);
+ script->run_main(sd->st);
return 0;
}
@@ -1265,25 +1251,30 @@ int npc_scriptcont(struct map_session_data* sd, int id, bool closing)
/*==========================================
* Chk if valid call then open buy or selling list
*------------------------------------------*/
-int npc_buysellsel(struct map_session_data* sd, int id, int type)
-{
+int npc_buysellsel(struct map_session_data* sd, int id, int type) {
struct npc_data *nd;
nullpo_retr(1, sd);
- if ((nd = npc_checknear(sd,iMap->id2bl(id))) == NULL)
+ if ((nd = npc->checknear(sd,map->id2bl(id))) == NULL)
return 1;
- if (nd->subtype!=SHOP) {
- ShowError("no such shop npc : %d\n",id);
+ if ( nd->subtype != SHOP && !(nd->subtype == SCRIPT && nd->u.scr.shop && nd->u.scr.shop->items) ) {
+
+ if( nd->subtype == SCRIPT )
+ ShowError("npc_buysellsel: trader '%s' has no shop list!\n",nd->exname);
+ else
+ ShowError("npc_buysellsel: no such shop npc %d (%s)\n",id,nd->exname);
+
if (sd->npc_id == id)
- sd->npc_id=0;
+ sd->npc_id = 0;
return 1;
}
- if (nd->option & OPTION_INVISIBLE) // can't buy if npc is not visible (hack?)
+
+ if (nd->option & OPTION_INVISIBLE) // can't buy if npc is not visible (hack?)
return 1;
- if( nd->class_ < 0 && !sd->state.callshop )
- {// not called through a script and is not a visible NPC so an invalid call
+
+ if( nd->class_ < 0 && !sd->state.callshop ) {// not called through a script and is not a visible NPC so an invalid call
return 1;
}
@@ -1296,92 +1287,114 @@ int npc_buysellsel(struct map_session_data* sd, int id, int type)
} else {
clif->selllist(sd);
}
+
return 0;
}
+
/*==========================================
* Cash Shop Buy List
*------------------------------------------*/
-int npc_cashshop_buylist(struct map_session_data *sd, int points, int count, unsigned short* item_list)
-{
- int i, j, nameid, amount, new_, w, vt;
- struct npc_data *nd = (struct npc_data *)iMap->id2bl(sd->npc_shopid);
-
- if( !nd || nd->subtype != CASHSHOP )
- return 1;
-
- if( sd->state.trading )
- return 4;
-
- new_ = 0;
- w = 0;
- vt = 0; // Global Value
-
- // Validating Process ----------------------------------------------------
- for( i = 0; i < count; i++ )
- {
- nameid = item_list[i*2+1];
- amount = item_list[i*2+0];
-
- if( !itemdb_exists(nameid) || amount <= 0 )
- return 5;
-
- ARR_FIND(0,nd->u.shop.count,j,nd->u.shop.shop_item[j].nameid == nameid);
- if( j == nd->u.shop.count || nd->u.shop.shop_item[j].value <= 0 )
- return 5;
-
- if( !itemdb_isstackable(nameid) && amount > 1 )
- {
- ShowWarning("Player %s (%d:%d) sent a hexed packet trying to buy %d of nonstackable item %d!\n", sd->status.name, sd->status.account_id, sd->status.char_id, amount, nameid);
- amount = item_list[i*2+0] = 1;
- }
-
- switch( pc->checkadditem(sd,nameid,amount) )
- {
- case ADDITEM_NEW:
- new_++;
- break;
- case ADDITEM_OVERAMOUNT:
- return 3;
- }
-
- vt += nd->u.shop.shop_item[j].value * amount;
- w += itemdb_weight(nameid) * amount;
- }
-
- if( w + sd->weight > sd->max_weight )
- return 3;
- if( pc->inventoryblank(sd) < new_ )
- return 3;
- if( points > vt ) points = vt;
-
- // Payment Process ----------------------------------------------------
- if( sd->kafraPoints < points || sd->cashPoints < (vt - points) )
- return 6;
- pc->paycash(sd,vt,points);
-
- // Delivery Process ----------------------------------------------------
- for( i = 0; i < count; i++ )
- {
- struct item item_tmp;
-
- nameid = item_list[i*2+1];
- amount = item_list[i*2+0];
-
- memset(&item_tmp,0,sizeof(item_tmp));
-
- if( !pet_create_egg(sd,nameid) )
- {
- item_tmp.nameid = nameid;
- item_tmp.identify = 1;
- pc->additem(sd,&item_tmp,amount,LOG_TYPE_NPC);
- }
- }
-
- return 0;
+int npc_cashshop_buylist(struct map_session_data *sd, int points, int count, unsigned short* item_list) {
+ int i, j, nameid, amount, new_, w, vt;
+ struct npc_data *nd = NULL;
+ struct npc_item_list *shop = NULL;
+ unsigned short shop_size = 0;
+
+ if( sd->state.trading )
+ return ERROR_TYPE_EXCHANGE;
+
+ if( count <= 0 )
+ return ERROR_TYPE_ITEM_ID;
+
+ if( points < 0 )
+ return ERROR_TYPE_MONEY;
+
+ if( !(nd = (struct npc_data *)map->id2bl(sd->npc_shopid)) )
+ return ERROR_TYPE_NPC;
+
+ if( nd->subtype != CASHSHOP ) {
+ if( nd->subtype == SCRIPT && nd->u.scr.shop && nd->u.scr.shop->type != NST_ZENY && nd->u.scr.shop->type != NST_MARKET ) {
+ shop = nd->u.scr.shop->item;
+ shop_size = nd->u.scr.shop->items;
+ } else
+ return ERROR_TYPE_NPC;
+ } else {
+ shop = nd->u.shop.shop_item;
+ shop_size = nd->u.shop.count;
+ }
+
+ new_ = 0;
+ w = 0;
+ vt = 0; // Global Value
+
+ // Validating Process ----------------------------------------------------
+ for( i = 0; i < count; i++ ) {
+ nameid = item_list[i*2+1];
+ amount = item_list[i*2+0];
+
+ if( !itemdb->exists(nameid) || amount <= 0 )
+ return ERROR_TYPE_ITEM_ID;
+
+ ARR_FIND(0,shop_size,j,shop[j].nameid == nameid);
+ if( j == shop_size || shop[j].value <= 0 )
+ return ERROR_TYPE_ITEM_ID;
+
+ if( !itemdb->isstackable(nameid) && amount > 1 ) {
+ ShowWarning("Player %s (%d:%d) sent a hexed packet trying to buy %d of non-stackable item %d!\n",
+ sd->status.name, sd->status.account_id, sd->status.char_id, amount, nameid);
+ amount = item_list[i*2+0] = 1;
+ }
+
+ switch( pc->checkadditem(sd,nameid,amount) ) {
+ case ADDITEM_NEW:
+ new_++;
+ break;
+ case ADDITEM_OVERAMOUNT:
+ return ERROR_TYPE_INVENTORY_WEIGHT;
+ }
+
+ vt += shop[j].value * amount;
+ w += itemdb_weight(nameid) * amount;
+ }
+
+ if( w + sd->weight > sd->max_weight )
+ return ERROR_TYPE_INVENTORY_WEIGHT;
+
+ if( pc->inventoryblank(sd) < new_ )
+ return ERROR_TYPE_INVENTORY_WEIGHT;
+
+ if( points > vt ) points = vt;
+
+ // Payment Process ----------------------------------------------------
+ if( nd->subtype == SCRIPT && nd->u.scr.shop->type == NST_CUSTOM ) {
+ if( !npc->trader_pay(nd,sd,vt,points) )
+ return ERROR_TYPE_MONEY;
+ } else {
+ if( sd->kafraPoints < points || sd->cashPoints < (vt - points) )
+ return ERROR_TYPE_MONEY;
+ pc->paycash(sd,vt,points);
+ }
+ // Delivery Process ----------------------------------------------------
+ for( i = 0; i < count; i++ ) {
+ struct item item_tmp;
+
+ nameid = item_list[i*2+1];
+ amount = item_list[i*2+0];
+
+ memset(&item_tmp,0,sizeof(item_tmp));
+
+ if( !pet->create_egg(sd,nameid) ) {
+ item_tmp.nameid = nameid;
+ item_tmp.identify = 1;
+ pc->additem(sd,&item_tmp,amount,LOG_TYPE_NPC);
+ }
+ }
+
+ return ERROR_TYPE_NONE;
}
//npc_buylist for script-controlled shops.
-static int npc_buylist_sub(struct map_session_data* sd, int n, unsigned short* item_list, struct npc_data* nd)
+int npc_buylist_sub(struct map_session_data* sd, int n, unsigned short* item_list, struct npc_data* nd)
{
char npc_ev[EVENT_NAME_LENGTH];
int i;
@@ -1389,93 +1402,314 @@ static int npc_buylist_sub(struct map_session_data* sd, int n, unsigned short* i
int key_amount = 0;
// discard old contents
- script_cleararray_pc(sd, "@bought_nameid", (void*)0);
- script_cleararray_pc(sd, "@bought_quantity", (void*)0);
+ script->cleararray_pc(sd, "@bought_nameid", (void*)0);
+ script->cleararray_pc(sd, "@bought_quantity", (void*)0);
// save list of bought items
- for( i = 0; i < n; i++ )
- {
- script_setarray_pc(sd, "@bought_nameid", i, (void*)(intptr_t)item_list[i*2+1], &key_nameid);
- script_setarray_pc(sd, "@bought_quantity", i, (void*)(intptr_t)item_list[i*2], &key_amount);
+ for( i = 0; i < n; i++ ) {
+ script->setarray_pc(sd, "@bought_nameid", i, (void*)(intptr_t)item_list[i*2+1], &key_nameid);
+ script->setarray_pc(sd, "@bought_quantity", i, (void*)(intptr_t)item_list[i*2], &key_amount);
}
// invoke event
snprintf(npc_ev, ARRAYLENGTH(npc_ev), "%s::OnBuyItem", nd->exname);
- npc_event(sd, npc_ev, 0);
+ npc->event(sd, npc_ev, 0);
return 0;
}
+/**
+ * Loads persistent NPC Market Data from SQL
+ **/
+void npc_market_fromsql(void) {
+ SqlStmt* stmt = SQL->StmtMalloc(map->mysql_handle);
+ char name[NAME_LENGTH+1];
+ int itemid;
+ int amount;
+
+ if ( SQL_ERROR == SQL->StmtPrepare(stmt, "SELECT `name`, `itemid`, `amount` FROM `%s`", map->npc_market_data_db)
+ || SQL_ERROR == SQL->StmtExecute(stmt)
+ ) {
+ SqlStmt_ShowDebug(stmt);
+ SQL->StmtFree(stmt);
+ return;
+ }
+
+ SQL->StmtBindColumn(stmt, 0, SQLDT_STRING, &name[0], sizeof(name), NULL, NULL);
+ SQL->StmtBindColumn(stmt, 1, SQLDT_INT, &itemid, 0, NULL, NULL);
+ SQL->StmtBindColumn(stmt, 2, SQLDT_INT, &amount, 0, NULL, NULL);
+
+ while ( SQL_SUCCESS == SQL->StmtNextRow(stmt) ) {
+ struct npc_data *nd = NULL;
+ unsigned short i;
+
+ if( !(nd = npc->name2id(name)) ) {
+ ShowError("npc_market_fromsql: NPC '%s' not found! skipping...\n",name);
+ npc->market_delfromsql_sub(name, USHRT_MAX);
+ continue;
+ } else if ( nd->subtype != SCRIPT || !nd->u.scr.shop || !nd->u.scr.shop->items || nd->u.scr.shop->type != NST_MARKET ) {
+ ShowError("npc_market_fromsql: NPC '%s' is not proper for market, skipping...\n",name);
+ npc->market_delfromsql_sub(name, USHRT_MAX);
+ continue;
+ }
+
+ for(i = 0; i < nd->u.scr.shop->items; i++) {
+ if( nd->u.scr.shop->item[i].nameid == itemid ) {
+ nd->u.scr.shop->item[i].qty = amount;
+ break;
+ }
+ }
+
+ if( i == nd->u.scr.shop->items ) {
+ ShowError("npc_market_fromsql: NPC '%s' does not sell item %d (qty %d), deleting...\n",name,itemid,amount);
+ npc->market_delfromsql_sub(name, itemid);
+ continue;
+ }
+
+ }
+
+ SQL->StmtFree(stmt);
+}
+/**
+ * Saves persistent NPC Market Data into SQL
+ **/
+void npc_market_tosql(struct npc_data *nd, unsigned short index) {
+ if( SQL_ERROR == SQL->Query(map->mysql_handle, "REPLACE INTO `%s` VALUES ('%s','%d','%d')",
+ map->npc_market_data_db, nd->exname, nd->u.scr.shop->item[index].nameid, nd->u.scr.shop->item[index].qty) )
+ Sql_ShowDebug(map->mysql_handle);
+}
+/**
+ * Removes persistent NPC Market Data from SQL
+ */
+void npc_market_delfromsql_sub(const char *npcname, unsigned short index) {
+ if( index == USHRT_MAX ) {
+ if( SQL_ERROR == SQL->Query(map->mysql_handle, "DELETE FROM `%s` WHERE `name`='%s'", map->npc_market_data_db, npcname) )
+ Sql_ShowDebug(map->mysql_handle);
+ } else {
+ if( SQL_ERROR == SQL->Query(map->mysql_handle, "DELETE FROM `%s` WHERE `name`='%s' AND `itemid`='%d' LIMIT 1",
+ map->npc_market_data_db, npcname, index) )
+ Sql_ShowDebug(map->mysql_handle);
+ }
+}
+/**
+ * Removes persistent NPC Market Data from SQL
+ **/
+void npc_market_delfromsql(struct npc_data *nd, unsigned short index) {
+ npc->market_delfromsql_sub(nd->exname, index == USHRT_MAX ? index : nd->u.scr.shop->item[index].nameid);
+}
+/**
+ * Judges whether to allow and spawn a trader's window.
+ **/
+bool npc_trader_open(struct map_session_data *sd, struct npc_data *nd) {
+
+ if( !nd->u.scr.shop || !nd->u.scr.shop->items )
+ return false;
+
+ switch( nd->u.scr.shop->type ) {
+ case NST_ZENY:
+ sd->state.callshop = 1;
+ clif->npcbuysell(sd,nd->bl.id);
+ return true;/* we skip sd->npc_shopid, npc->buysell will set it then when the player selects */
+ case NST_MARKET: {
+ unsigned short i;
+
+ for(i = 0; i < nd->u.scr.shop->items; i++) {
+ if( nd->u.scr.shop->item[i].qty )
+ break;
+ }
+
+ /* nothing to display, no items available */
+ if( i == nd->u.scr.shop->items ) {
+ clif->colormes(sd->fd,COLOR_RED, msg_txt(881));
+ return false;
+ }
+ clif->npc_market_open(sd,nd);
+ }
+ break;
+ default:
+ clif->cashshop_show(sd,nd);
+ break;
+ }
+
+ sd->npc_shopid = nd->bl.id;
+
+ return true;
+}
+/**
+ * Creates (npc_data)->u.scr.shop and updates all duplicates across the server to match the created pointer
+ *
+ * @param master id of the original npc
+ **/
+void npc_trader_update(int master) {
+ DBIterator* iter;
+ struct block_list* bl;
+ struct npc_data *master_nd = map->id2nd(master);
+
+ CREATE(master_nd->u.scr.shop,struct npc_shop_data,1);
+
+ iter = db_iterator(map->id_db);
+
+ for( bl = (struct block_list*)dbi_first(iter); dbi_exists(iter); bl = (struct block_list*)dbi_next(iter) ) {
+ if( bl->type == BL_NPC ) {
+ struct npc_data* nd = (struct npc_data*)bl;
+
+ if( nd->src_id == master ) {
+ nd->u.scr.shop = master_nd->u.scr.shop;
+ }
+ }
+ }
+
+ dbi_destroy(iter);
+}
+/**
+ * Tries to issue a CountFunds event to the shop.
+ *
+ * @param nd shop
+ * @param sd player
+ **/
+void npc_trader_count_funds(struct npc_data *nd, struct map_session_data *sd) {
+ char evname[EVENT_NAME_LENGTH];
+ struct event_data *ev = NULL;
+
+ npc->trader_funds[0] = npc->trader_funds[1] = 0;/* clear */
+
+ switch( nd->u.scr.shop->type ) {
+ case NST_CASH:
+ npc->trader_funds[0] = sd->cashPoints;
+ npc->trader_funds[1] = sd->kafraPoints;
+ return;
+ case NST_CUSTOM:
+ break;
+ default:
+ ShowError("npc_trader_count_funds: unsupported shop type %d\n",nd->u.scr.shop->type);
+ return;
+ }
+
+ snprintf(evname, EVENT_NAME_LENGTH, "%s::OnCountFunds",nd->exname);
+
+ if ( (ev = strdb_get(npc->ev_db, evname)) )
+ script->run(ev->nd->u.scr.script, ev->pos, sd->bl.id, ev->nd->bl.id);
+ else
+ ShowError("npc_trader_count_funds: '%s' event '%s' not found, operation failed\n",nd->exname,evname);
+
+ /* the callee will rely on npc->trader_funds, upon success script->run updates them */
+}
+/**
+ * Tries to issue a payment to the NPC Event capable of handling it
+ *
+ * @param nd shop
+ * @param sd player
+ * @param price total cost
+ * @param points the amount input in the shop by the user to use from the secondary currency (if any is being employed)
+ *
+ * @return bool whether it was successful (if the script does not respond it will fail)
+ **/
+bool npc_trader_pay(struct npc_data *nd, struct map_session_data *sd, int price, int points) {
+ char evname[EVENT_NAME_LENGTH];
+ struct event_data *ev = NULL;
+
+ npc->trader_ok = false;/* clear */
+
+ snprintf(evname, EVENT_NAME_LENGTH, "%s::OnPayFunds",nd->exname);
+
+ if ( (ev = strdb_get(npc->ev_db, evname)) ) {
+ pc->setreg(sd,script->add_str("@price"),price);
+ pc->setreg(sd,script->add_str("@points"),points);
+
+ script->run(ev->nd->u.scr.script, ev->pos, sd->bl.id, ev->nd->bl.id);
+ } else
+ ShowError("npc_trader_pay: '%s' event '%s' not found, operation failed\n",nd->exname,evname);
+
+ return npc->trader_ok;/* run script will deal with it */
+}
/*==========================================
* Cash Shop Buy
*------------------------------------------*/
-int npc_cashshop_buy(struct map_session_data *sd, int nameid, int amount, int points)
-{
- struct npc_data *nd = (struct npc_data *)iMap->id2bl(sd->npc_shopid);
+int npc_cashshop_buy(struct map_session_data *sd, int nameid, int amount, int points) {
+ struct npc_data *nd = NULL;
struct item_data *item;
+ struct npc_item_list *shop = NULL;
int i, price, w;
-
+ unsigned short shop_size = 0;
+
if( amount <= 0 )
- return 5;
+ return ERROR_TYPE_ITEM_ID;
if( points < 0 )
- return 6;
-
- if( !nd || nd->subtype != CASHSHOP )
- return 1;
+ return ERROR_TYPE_MONEY;
if( sd->state.trading )
- return 4;
+ return ERROR_TYPE_EXCHANGE;
+
+ if( !(nd = (struct npc_data *)map->id2bl(sd->npc_shopid)) )
+ return ERROR_TYPE_NPC;
- if( (item = itemdb_exists(nameid)) == NULL )
- return 5; // Invalid Item
+ if( (item = itemdb->exists(nameid)) == NULL )
+ return ERROR_TYPE_ITEM_ID; // Invalid Item
- ARR_FIND(0, nd->u.shop.count, i, nd->u.shop.shop_item[i].nameid == nameid);
- if( i == nd->u.shop.count )
- return 5;
- if( nd->u.shop.shop_item[i].value <= 0 )
- return 5;
+ if( nd->subtype != CASHSHOP ) {
+ if( nd->subtype == SCRIPT && nd->u.scr.shop && nd->u.scr.shop->type != NST_ZENY && nd->u.scr.shop->type != NST_MARKET ) {
+ shop = nd->u.scr.shop->item;
+ shop_size = nd->u.scr.shop->items;
+ } else
+ return ERROR_TYPE_NPC;
+ } else {
+ shop = nd->u.shop.shop_item;
+ shop_size = nd->u.shop.count;
+ }
+
+ ARR_FIND(0, shop_size, i, shop[i].nameid == nameid);
+
+ if( i == shop_size )
+ return ERROR_TYPE_ITEM_ID;
+
+ if( shop[i].value <= 0 )
+ return ERROR_TYPE_ITEM_ID;
- if(!itemdb_isstackable(nameid) && amount > 1)
- {
- ShowWarning("Player %s (%d:%d) sent a hexed packet trying to buy %d of nonstackable item %d!\n",
+ if(!itemdb->isstackable(nameid) && amount > 1) {
+ ShowWarning("Player %s (%d:%d) sent a hexed packet trying to buy %d of non-stackable item %d!\n",
sd->status.name, sd->status.account_id, sd->status.char_id, amount, nameid);
amount = 1;
}
- switch( pc->checkadditem(sd, nameid, amount) )
- {
+ switch( pc->checkadditem(sd, nameid, amount) ) {
case ADDITEM_NEW:
if( pc->inventoryblank(sd) == 0 )
- return 3;
+ return ERROR_TYPE_INVENTORY_WEIGHT;
break;
case ADDITEM_OVERAMOUNT:
- return 3;
+ return ERROR_TYPE_INVENTORY_WEIGHT;
}
w = item->weight * amount;
if( w + sd->weight > sd->max_weight )
- return 3;
+ return ERROR_TYPE_INVENTORY_WEIGHT;
- if( (double)nd->u.shop.shop_item[i].value * amount > INT_MAX )
- {
+ if( (double)shop[i].value * amount > INT_MAX ) {
ShowWarning("npc_cashshop_buy: Item '%s' (%d) price overflow attempt!\n", item->name, nameid);
ShowDebug("(NPC:'%s' (%s,%d,%d), player:'%s' (%d/%d), value:%d, amount:%d)\n",
- nd->exname, map[nd->bl.m].name, nd->bl.x, nd->bl.y, sd->status.name, sd->status.account_id, sd->status.char_id, nd->u.shop.shop_item[i].value, amount);
- return 5;
+ nd->exname, map->list[nd->bl.m].name, nd->bl.x, nd->bl.y,
+ sd->status.name, sd->status.account_id, sd->status.char_id,
+ shop[i].value, amount);
+ return ERROR_TYPE_ITEM_ID;
}
- price = nd->u.shop.shop_item[i].value * amount;
+ price = shop[i].value * amount;
+
if( points > price )
points = price;
- if( (sd->kafraPoints < points) || (sd->cashPoints < price - points) )
- return 6;
-
- pc->paycash(sd, price, points);
+ if( nd->subtype == SCRIPT && nd->u.scr.shop->type == NST_CUSTOM ) {
+ if( !npc->trader_pay(nd,sd,price,points) )
+ return ERROR_TYPE_MONEY;
+ } else {
+ if( (sd->kafraPoints < points) || (sd->cashPoints < price - points) )
+ return ERROR_TYPE_MONEY;
+
+ pc->paycash(sd, price, points);
+ }
- if( !pet_create_egg(sd, nameid) )
- {
+ if( !pet->create_egg(sd, nameid) ) {
struct item item_tmp;
memset(&item_tmp, 0, sizeof(struct item));
item_tmp.nameid = nameid;
@@ -1484,114 +1718,124 @@ int npc_cashshop_buy(struct map_session_data *sd, int nameid, int amount, int po
pc->additem(sd,&item_tmp, amount, LOG_TYPE_NPC);
}
- return 0;
+ return ERROR_TYPE_NONE;
}
/// Player item purchase from npc shop.
///
/// @param item_list 'n' pairs <amount,itemid>
/// @return result code for clif->parse_NpcBuyListSend
-int npc_buylist(struct map_session_data* sd, int n, unsigned short* item_list)
-{
+int npc_buylist(struct map_session_data* sd, int n, unsigned short* item_list) {
struct npc_data* nd;
+ struct npc_item_list *shop = NULL;
double z;
int i,j,w,skill_t,new_, idx = skill->get_index(MC_DISCOUNT);
-
+ unsigned short shop_size = 0;
+
nullpo_retr(3, sd);
nullpo_retr(3, item_list);
-
- nd = npc_checknear(sd,iMap->id2bl(sd->npc_shopid));
+
+ nd = npc->checknear(sd,map->id2bl(sd->npc_shopid));
if( nd == NULL )
return 3;
- if( nd->subtype != SHOP )
- return 3;
-
+
+ if( nd->subtype != SHOP ) {
+ if( nd->subtype == SCRIPT && nd->u.scr.shop && nd->u.scr.shop->type == NST_ZENY ) {
+ shop = nd->u.scr.shop->item;
+ shop_size = nd->u.scr.shop->items;
+ } else
+ return 3;
+ } else {
+ shop = nd->u.shop.shop_item;
+ shop_size = nd->u.shop.count;
+ }
+
z = 0;
w = 0;
new_ = 0;
// process entries in buy list, one by one
for( i = 0; i < n; ++i ) {
int nameid, amount, value;
-
+
// find this entry in the shop's sell list
- ARR_FIND( 0, nd->u.shop.count, j,
- item_list[i*2+1] == nd->u.shop.shop_item[j].nameid || //Normal items
- item_list[i*2+1] == itemdb_viewid(nd->u.shop.shop_item[j].nameid) //item_avail replacement
- );
-
- if( j == nd->u.shop.count )
+ ARR_FIND( 0, shop_size, j,
+ item_list[i*2+1] == shop[j].nameid || //Normal items
+ item_list[i*2+1] == itemdb_viewid(shop[j].nameid) //item_avail replacement
+ );
+
+ if( j == shop_size )
return 3; // no such item in shop
-
+
amount = item_list[i*2+0];
- nameid = item_list[i*2+1] = nd->u.shop.shop_item[j].nameid; //item_avail replacement
- value = nd->u.shop.shop_item[j].value;
-
- if( !itemdb_exists(nameid) )
+ nameid = item_list[i*2+1] = shop[j].nameid; //item_avail replacement
+ value = shop[j].value;
+
+ if( !itemdb->exists(nameid) )
return 3; // item no longer in itemdb
-
- if( !itemdb_isstackable(nameid) && amount > 1 ) {
+
+ if( !itemdb->isstackable(nameid) && amount > 1 ) {
//Exploit? You can't buy more than 1 of equipment types o.O
- ShowWarning("Player %s (%d:%d) sent a hexed packet trying to buy %d of nonstackable item %d!\n",
- sd->status.name, sd->status.account_id, sd->status.char_id, amount, nameid);
+ ShowWarning("Player %s (%d:%d) sent a hexed packet trying to buy %d of non-stackable item %d!\n",
+ sd->status.name, sd->status.account_id, sd->status.char_id, amount, nameid);
amount = item_list[i*2+0] = 1;
}
-
+
if( nd->master_nd ) {
// Script-controlled shops decide by themselves, what can be bought and for what price.
continue;
}
-
+
switch( pc->checkadditem(sd,nameid,amount) ) {
case ADDITEM_EXIST:
break;
-
+
case ADDITEM_NEW:
new_++;
break;
-
+
case ADDITEM_OVERAMOUNT:
return 2;
}
-
+
value = pc->modifybuyvalue(sd,value);
-
+
z += (double)value * amount;
w += itemdb_weight(nameid) * amount;
}
-
+
if( nd->master_nd != NULL ) //Script-based shops.
- return npc_buylist_sub(sd,n,item_list,nd->master_nd);
-
+ return npc->buylist_sub(sd,n,item_list,nd->master_nd);
+
if( z > (double)sd->status.zeny )
return 1; // Not enough Zeny
if( w + sd->weight > sd->max_weight )
return 2; // Too heavy
if( pc->inventoryblank(sd) < new_ )
return 3; // Not enough space to store items
-
+
pc->payzeny(sd,(int)z,LOG_TYPE_NPC, NULL);
-
+
for( i = 0; i < n; ++i ) {
int nameid = item_list[i*2+1];
int amount = item_list[i*2+0];
struct item item_tmp;
-
+
if (itemdb_type(nameid) == IT_PETEGG)
- pet_create_egg(sd, nameid);
+ pet->create_egg(sd, nameid);
else {
memset(&item_tmp,0,sizeof(item_tmp));
item_tmp.nameid = nameid;
item_tmp.identify = 1;
-
+
pc->additem(sd,&item_tmp,amount,LOG_TYPE_NPC);
}
}
-
+
// custom merchant shop exp bonus
if( battle_config.shop_exp > 0 && z > 0 && (skill_t = pc->checkskill2(sd,idx)) > 0 ) {
if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
skill_t = sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
-
+
if( skill_t > 0 ) {
z = z * (double)skill_t * (double)battle_config.shop_exp/10000.;
if( z < 1 )
@@ -1599,13 +1843,123 @@ int npc_buylist(struct map_session_data* sd, int n, unsigned short* item_list)
pc->gainexp(sd,NULL,0,(int)z, false);
}
}
-
+
return 0;
}
+/**
+ * parses incoming npc market purchase list
+ **/
+int npc_market_buylist(struct map_session_data* sd, unsigned short list_size, struct packet_npc_market_purchase *p) {
+ struct npc_data* nd;
+ struct npc_item_list *shop = NULL;
+ double z;
+ int i,j,w,new_;
+ unsigned short shop_size = 0;
+
+ nullpo_retr(1, sd);
+ nullpo_retr(1, p);
+
+ nd = npc->checknear(sd,map->id2bl(sd->npc_shopid));
+
+ if( nd == NULL || nd->subtype != SCRIPT || !list_size || !nd->u.scr.shop || nd->u.scr.shop->type != NST_MARKET )
+ return 1;
+
+ shop = nd->u.scr.shop->item;
+ shop_size = nd->u.scr.shop->items;
+
+ z = 0;
+ w = 0;
+ new_ = 0;
+
+ // process entries in buy list, one by one
+ for( i = 0; i < list_size; ++i ) {
+ int nameid, amount, value;
+
+ // find this entry in the shop's sell list
+ ARR_FIND( 0, shop_size, j,
+ p->list[i].ITID == shop[j].nameid || //Normal items
+ p->list[i].ITID == itemdb_viewid(shop[j].nameid) //item_avail replacement
+ );
+
+ if( j == shop_size ) /* TODO find official response for this */
+ return 1; // no such item in shop
+
+ if( p->list[i].qty > shop[j].qty )
+ return 1;
+
+ amount = p->list[i].qty;
+ nameid = p->list[i].ITID = shop[j].nameid; //item_avail replacement
+ value = shop[j].value;
+ npc_market_qty[i] = j;
+
+ if( !itemdb->exists(nameid) ) /* TODO find official response for this */
+ return 1; // item no longer in itemdb
+
+ if( !itemdb->isstackable(nameid) && amount > 1 ) {
+ //Exploit? You can't buy more than 1 of equipment types o.O
+ ShowWarning("Player %s (%d:%d) sent a hexed packet trying to buy %d of non-stackable item %d!\n",
+ sd->status.name, sd->status.account_id, sd->status.char_id, amount, nameid);
+ amount = p->list[i].qty = 1;
+ }
+
+ switch( pc->checkadditem(sd,nameid,amount) ) {
+ case ADDITEM_EXIST:
+ break;
+
+ case ADDITEM_NEW:
+ new_++;
+ break;
+
+ case ADDITEM_OVERAMOUNT: /* TODO find official response for this */
+ return 1;
+ }
+
+ z += (double)value * amount;
+ w += itemdb_weight(nameid) * amount;
+ }
+
+ if( z > (double)sd->status.zeny ) /* TODO find official response for this */
+ return 1; // Not enough Zeny
+
+ if( w + sd->weight > sd->max_weight ) /* TODO find official response for this */
+ return 1; // Too heavy
+
+ if( pc->inventoryblank(sd) < new_ ) /* TODO find official response for this */
+ return 1; // Not enough space to store items
+
+ pc->payzeny(sd,(int)z,LOG_TYPE_NPC, NULL);
+
+ for( i = 0; i < list_size; ++i ) {
+ int nameid = p->list[i].ITID;
+ int amount = p->list[i].qty;
+ struct item item_tmp;
+
+ j = npc_market_qty[i];
+
+ if( p->list[i].qty > shop[j].qty ) /* wohoo someone tampered with the packet. */
+ return 1;
+
+ shop[j].qty -= amount;
+
+ npc->market_tosql(nd,j);
+
+ if (itemdb_type(nameid) == IT_PETEGG)
+ pet->create_egg(sd, nameid);
+ else {
+ memset(&item_tmp,0,sizeof(item_tmp));
+ item_tmp.nameid = nameid;
+ item_tmp.identify = 1;
+
+ pc->additem(sd,&item_tmp,amount,LOG_TYPE_NPC);
+ }
+ }
+
+ return 0;
+}
/// npc_selllist for script-controlled shops
-static int npc_selllist_sub(struct map_session_data* sd, int n, unsigned short* item_list, struct npc_data* nd)
+int npc_selllist_sub(struct map_session_data* sd, int n, unsigned short* item_list, struct npc_data* nd)
{
char npc_ev[EVENT_NAME_LENGTH];
char card_slot[NAME_LENGTH];
@@ -1618,17 +1972,17 @@ static int npc_selllist_sub(struct map_session_data* sd, int n, unsigned short*
int key_card[MAX_SLOTS];
// discard old contents
- script_cleararray_pc(sd, "@sold_nameid", (void*)0);
- script_cleararray_pc(sd, "@sold_quantity", (void*)0);
- script_cleararray_pc(sd, "@sold_refine", (void*)0);
- script_cleararray_pc(sd, "@sold_attribute", (void*)0);
- script_cleararray_pc(sd, "@sold_identify", (void*)0);
+ script->cleararray_pc(sd, "@sold_nameid", (void*)0);
+ script->cleararray_pc(sd, "@sold_quantity", (void*)0);
+ script->cleararray_pc(sd, "@sold_refine", (void*)0);
+ script->cleararray_pc(sd, "@sold_attribute", (void*)0);
+ script->cleararray_pc(sd, "@sold_identify", (void*)0);
for( j = 0; j < MAX_SLOTS; j++ )
{// clear each of the card slot entries
key_card[j] = 0;
snprintf(card_slot, sizeof(card_slot), "@sold_card%d", j + 1);
- script_cleararray_pc(sd, card_slot, (void*)0);
+ script->cleararray_pc(sd, card_slot, (void*)0);
}
// save list of to be sold items
@@ -1636,26 +1990,26 @@ static int npc_selllist_sub(struct map_session_data* sd, int n, unsigned short*
{
idx = item_list[i*2]-2;
- script_setarray_pc(sd, "@sold_nameid", i, (void*)(intptr_t)sd->status.inventory[idx].nameid, &key_nameid);
- script_setarray_pc(sd, "@sold_quantity", i, (void*)(intptr_t)item_list[i*2+1], &key_amount);
+ script->setarray_pc(sd, "@sold_nameid", i, (void*)(intptr_t)sd->status.inventory[idx].nameid, &key_nameid);
+ script->setarray_pc(sd, "@sold_quantity", i, (void*)(intptr_t)item_list[i*2+1], &key_amount);
- if( itemdb_isequip(sd->status.inventory[idx].nameid) )
+ if( itemdb->isequip(sd->status.inventory[idx].nameid) )
{// process equipment based information into the arrays
- script_setarray_pc(sd, "@sold_refine", i, (void*)(intptr_t)sd->status.inventory[idx].refine, &key_refine);
- script_setarray_pc(sd, "@sold_attribute", i, (void*)(intptr_t)sd->status.inventory[idx].attribute, &key_attribute);
- script_setarray_pc(sd, "@sold_identify", i, (void*)(intptr_t)sd->status.inventory[idx].identify, &key_identify);
+ script->setarray_pc(sd, "@sold_refine", i, (void*)(intptr_t)sd->status.inventory[idx].refine, &key_refine);
+ script->setarray_pc(sd, "@sold_attribute", i, (void*)(intptr_t)sd->status.inventory[idx].attribute, &key_attribute);
+ script->setarray_pc(sd, "@sold_identify", i, (void*)(intptr_t)sd->status.inventory[idx].identify, &key_identify);
for( j = 0; j < MAX_SLOTS; j++ )
{// store each of the cards from the equipment in the array
snprintf(card_slot, sizeof(card_slot), "@sold_card%d", j + 1);
- script_setarray_pc(sd, card_slot, i, (void*)(intptr_t)sd->status.inventory[idx].card[j], &key_card[j]);
+ script->setarray_pc(sd, card_slot, i, (void*)(intptr_t)sd->status.inventory[idx].card[j], &key_card[j]);
}
}
}
// invoke event
snprintf(npc_ev, ARRAYLENGTH(npc_ev), "%s::OnSellItem", nd->exname);
- npc_event(sd, npc_ev, 0);
+ npc->event(sd, npc_ev, 0);
return 0;
}
@@ -1664,19 +2018,24 @@ static int npc_selllist_sub(struct map_session_data* sd, int n, unsigned short*
///
/// @param item_list 'n' pairs <index,amount>
/// @return result code for clif->parse_NpcSellListSend
-int npc_selllist(struct map_session_data* sd, int n, unsigned short* item_list)
-{
+int npc_selllist(struct map_session_data* sd, int n, unsigned short* item_list) {
double z;
- int i,skill_t, idx = skill->get_index(MC_OVERCHARGE);
+ int i,skill_t, skill_idx = skill->get_index(MC_OVERCHARGE);
struct npc_data *nd;
nullpo_retr(1, sd);
nullpo_retr(1, item_list);
- if( ( nd = npc_checknear(sd, iMap->id2bl(sd->npc_shopid)) ) == NULL || nd->subtype != SHOP ) {
+ if( ( nd = npc->checknear(sd, map->id2bl(sd->npc_shopid)) ) == NULL ) {
return 1;
}
+ if( nd->subtype != SHOP ) {
+ if( !(nd->subtype == SCRIPT && nd->u.scr.shop && nd->u.scr.shop->type == NST_ZENY) )
+ return 1;
+ }
+
+
z = 0;
// verify the sell list
@@ -1706,7 +2065,7 @@ int npc_selllist(struct map_session_data* sd, int n, unsigned short* item_list)
}
if( nd->master_nd ) { // Script-controlled shops
- return npc_selllist_sub(sd, n, item_list, nd->master_nd);
+ return npc->selllist_sub(sd, n, item_list, nd->master_nd);
}
// delete items
@@ -1717,8 +2076,8 @@ int npc_selllist(struct map_session_data* sd, int n, unsigned short* item_list)
amount = item_list[i*2+1];
if( sd->inventory_data[idx]->type == IT_PETEGG && sd->status.inventory[idx].card[0] == CARD0_PET ) {
- if( search_petDB_index(sd->status.inventory[idx].nameid, PET_EGG) >= 0 ) {
- intif_delete_petdata(MakeDWord(sd->status.inventory[idx].card[1], sd->status.inventory[idx].card[2]));
+ if( pet->search_petDB_index(sd->status.inventory[idx].nameid, PET_EGG) >= 0 ) {
+ intif->delete_petdata(MakeDWord(sd->status.inventory[idx].card[1], sd->status.inventory[idx].card[2]));
}
}
@@ -1731,9 +2090,9 @@ int npc_selllist(struct map_session_data* sd, int n, unsigned short* item_list)
pc->getzeny(sd, (int)z, LOG_TYPE_NPC, NULL);
// custom merchant shop exp bonus
- if( battle_config.shop_exp > 0 && z > 0 && ( skill_t = pc->checkskill2(sd,idx) ) > 0) {
- if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
- skill_t = sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
+ if( battle_config.shop_exp > 0 && z > 0 && ( skill_t = pc->checkskill2(sd,skill_idx) ) > 0) {
+ if( sd->status.skill[skill_idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
+ skill_t = sd->status.skill[skill_idx].flag - SKILL_FLAG_REPLACED_LV_0;
if( skill_t > 0 ) {
z = z * (double)skill_t * (double)battle_config.shop_exp/10000.;
@@ -1754,103 +2113,122 @@ int npc_remove_map(struct npc_data* nd) {
if(nd->bl.prev == NULL || nd->bl.m < 0)
return 1; //Not assigned to a map.
- m = nd->bl.m;
+ m = nd->bl.m;
clif->clearunit_area(&nd->bl,CLR_RESPAWN);
- npc_unsetcells(nd);
- iMap->delblock(&nd->bl);
- //Remove npc from map[].npc list. [Skotlex]
- ARR_FIND( 0, map[m].npc_num, i, map[m].npc[i] == nd );
- if( i == map[m].npc_num ) return 2; //failed to find it?
-
- map[m].npc_num--;
- map[m].npc[i] = map[m].npc[map[m].npc_num];
- map[m].npc[map[m].npc_num] = NULL;
+ npc->unsetcells(nd);
+ map->delblock(&nd->bl);
+ //Remove npc from map->list[].npc list. [Skotlex]
+ ARR_FIND( 0, map->list[m].npc_num, i, map->list[m].npc[i] == nd );
+ if( i == map->list[m].npc_num ) return 2; //failed to find it?
+
+ map->list[m].npc_num--;
+ map->list[m].npc[i] = map->list[m].npc[map->list[m].npc_num];
+ map->list[m].npc[map->list[m].npc_num] = NULL;
return 0;
}
/**
* @see DBApply
*/
-static int npc_unload_ev(DBKey key, DBData *data, va_list ap)
+int npc_unload_ev(DBKey key, DBData *data, va_list ap)
{
struct event_data* ev = DB->data2ptr(data);
char* npcname = va_arg(ap, char *);
if(strcmp(ev->nd->exname,npcname)==0){
- db_remove(ev_db, key);
+ db_remove(npc->ev_db, key);
return 1;
}
return 0;
}
+/**
+ * @see DBApply
+ */
+int npc_unload_ev_label(DBKey key, DBData *data, va_list ap)
+{
+ struct linkdb_node **label_linkdb = DB->data2ptr(data);
+ struct npc_data* nd = va_arg(ap, struct npc_data *);
+
+ linkdb_erase(label_linkdb, nd);
+
+ return 0;
+}
+
//Chk if npc matches src_id, then unload.
//Sub-function used to find duplicates.
-static int npc_unload_dup_sub(struct npc_data* nd, va_list args)
+int npc_unload_dup_sub(struct npc_data* nd, va_list args)
{
int src_id;
src_id = va_arg(args, int);
if (nd->src_id == src_id)
- npc_unload(nd, true);
+ npc->unload(nd, true);
return 0;
}
//Removes all npcs that are duplicates of the passed one. [Skotlex]
-void npc_unload_duplicates(struct npc_data* nd)
-{
- iMap->map_foreachnpc(npc_unload_dup_sub,nd->bl.id);
+void npc_unload_duplicates(struct npc_data* nd) {
+ map->foreachnpc(npc->unload_dup_sub,nd->bl.id);
}
//Removes an npc from map and db.
//Single is to free name (for duplicates).
int npc_unload(struct npc_data* nd, bool single) {
+ unsigned int i;
+
nullpo_ret(nd);
- npc_remove_map(nd);
- iMap->deliddb(&nd->bl);
+ npc->remove_map(nd);
+ map->deliddb(&nd->bl);
if( single )
- strdb_remove(npcname_db, nd->exname);
+ strdb_remove(npc->name_db, nd->exname);
if (nd->chat_id) // remove npc chatroom object and kick users
- chat_deletenpcchat(nd);
+ chat->delete_npc_chat(nd);
#ifdef PCRE_SUPPORT
- npc_chat_finalize(nd); // deallocate npc PCRE data structures
+ npc_chat->finalize(nd); // deallocate npc PCRE data structures
#endif
if( single && nd->path ) {
struct npc_path_data* npd = NULL;
if( nd->path && nd->path != npc_last_ref ) {
- npd = strdb_get(npc_path_db, nd->path);
+ npd = strdb_get(npc->path_db, nd->path);
}
if( npd && --npd->references == 0 ) {
- strdb_remove(npc_path_db, nd->path);/* remove from db */
+ strdb_remove(npc->path_db, nd->path);/* remove from db */
aFree(nd->path);/* remove now that no other instances exist */
}
}
+
+ if( single && nd->bl.m != -1 )
+ map->remove_questinfo(nd->bl.m,nd);
- if( (nd->subtype == SHOP || nd->subtype == CASHSHOP) && nd->src_id == 0) //src check for duplicate shops [Orcao]
+ if( nd->src_id == 0 && ( nd->subtype == SHOP || nd->subtype == CASHSHOP ) ) //src check for duplicate shops [Orcao]
aFree(nd->u.shop.shop_item);
else if( nd->subtype == SCRIPT ) {
struct s_mapiterator* iter;
struct block_list* bl;
- if( single )
- ev_db->foreach(ev_db,npc_unload_ev,nd->exname); //Clean up all events related
+ if( single ) {
+ npc->ev_db->foreach(npc->ev_db,npc->unload_ev,nd->exname); //Clean up all events related
+ npc->ev_label_db->foreach(npc->ev_label_db,npc->unload_ev_label,nd);
+ }
iter = mapit_geteachpc();
for( bl = (struct block_list*)mapit->first(iter); mapit->exists(iter); bl = (struct block_list*)mapit->next(iter) ) {
struct map_session_data *sd = ((TBL_PC*)bl);
if( sd && sd->npc_timer_id != INVALID_TIMER ) {
- const struct TimerData *td = iTimer->get_timer(sd->npc_timer_id);
+ const struct TimerData *td = timer->get(sd->npc_timer_id);
if( td && td->id != nd->bl.id )
continue;
if( td && td->data )
- ers_free(timer_event_ers, (void*)td->data);
- iTimer->delete_timer(sd->npc_timer_id, npc_timerevent);
+ ers_free(npc->timer_event_ers, (void*)td->data);
+ timer->delete(sd->npc_timer_id, npc->timerevent);
sd->npc_timer_id = INVALID_TIMER;
}
}
@@ -1858,17 +2236,16 @@ int npc_unload(struct npc_data* nd, bool single) {
if (nd->u.scr.timerid != INVALID_TIMER) {
const struct TimerData *td;
- td = iTimer->get_timer(nd->u.scr.timerid);
+ td = timer->get(nd->u.scr.timerid);
if (td && td->data)
- ers_free(timer_event_ers, (void*)td->data);
- iTimer->delete_timer(nd->u.scr.timerid, npc_timerevent);
+ ers_free(npc->timer_event_ers, (void*)td->data);
+ timer->delete(nd->u.scr.timerid, npc->timerevent);
}
if (nd->u.scr.timer_event)
aFree(nd->u.scr.timer_event);
if (nd->src_id == 0) {
if(nd->u.scr.script) {
- script_stop_instances(nd->bl.id);
- script_free_code(nd->u.scr.script);
+ script->free_code(nd->u.scr.script);
nd->u.scr.script = NULL;
}
if (nd->u.scr.label_list) {
@@ -1876,16 +2253,30 @@ int npc_unload(struct npc_data* nd, bool single) {
nd->u.scr.label_list = NULL;
nd->u.scr.label_list_num = 0;
}
+ if(nd->u.scr.shop) {
+ if(nd->u.scr.shop->item)
+ aFree(nd->u.scr.shop->item);
+ aFree(nd->u.scr.shop);
+ }
}
if( nd->u.scr.guild_id )
guild->flag_remove(nd);
}
- if( nd->ud != &npc_base_ud ) {
+ if( nd->ud != &npc->base_ud ) {
aFree(nd->ud);
nd->ud = NULL;
}
+ for( i = 0; i < nd->hdatac; i++ ) {
+ if( nd->hdata[i]->flag.free ) {
+ aFree(nd->hdata[i]->data);
+ }
+ aFree(nd->hdata[i]);
+ }
+ if( nd->hdata )
+ aFree(nd->hdata);
+
aFree(nd);
return 0;
@@ -1896,9 +2287,9 @@ int npc_unload(struct npc_data* nd, bool single) {
//
/// Clears the npc source file list
-static void npc_clearsrcfile(void)
+void npc_clearsrcfile(void)
{
- struct npc_src_list* file = npc_src_files;
+ struct npc_src_list* file = npc->src_files;
struct npc_src_list* file_tofree;
while( file != NULL )
@@ -1907,7 +2298,7 @@ static void npc_clearsrcfile(void)
file = file->next;
aFree(file_tofree);
}
- npc_src_files = NULL;
+ npc->src_files = NULL;
}
/// Adds a npc source file (or removes all)
@@ -1918,12 +2309,12 @@ void npc_addsrcfile(const char* name)
if( strcmpi(name, "clear") == 0 )
{
- npc_clearsrcfile();
+ npc->clearsrcfile();
return;
}
// prevent multiple insert of source files
- file = npc_src_files;
+ file = npc->src_files;
while( file != NULL )
{
if( strcmp(name, file->name) == 0 )
@@ -1936,7 +2327,7 @@ void npc_addsrcfile(const char* name)
file->next = NULL;
safestrncpy(file->name, name, strlen(name) + 1);
if( file_prev == NULL )
- npc_src_files = file;
+ npc->src_files = file;
else
file_prev->next = file;
}
@@ -1944,12 +2335,12 @@ void npc_addsrcfile(const char* name)
/// Removes a npc source file (or all)
void npc_delsrcfile(const char* name)
{
- struct npc_src_list* file = npc_src_files;
+ struct npc_src_list* file = npc->src_files;
struct npc_src_list* file_prev = NULL;
if( strcmpi(name, "all") == 0 )
{
- npc_clearsrcfile();
+ npc->clearsrcfile();
return;
}
@@ -1957,8 +2348,8 @@ void npc_delsrcfile(const char* name)
{
if( strcmp(file->name, name) == 0 )
{
- if( npc_src_files == file )
- npc_src_files = file->next;
+ if( npc->src_files == file )
+ npc->src_files = file->next;
else
file_prev->next = file->next;
aFree(file);
@@ -1971,8 +2362,7 @@ void npc_delsrcfile(const char* name)
/// Parses and sets the name and exname of a npc.
/// Assumes that m, x and y are already set in nd.
-static void npc_parsename(struct npc_data* nd, const char* name, const char* start, const char* buffer, const char* filepath)
-{
+void npc_parsename(struct npc_data* nd, const char* name, const char* start, const char* buffer, const char* filepath) {
const char* p;
struct npc_data* dnd;// duplicate npc
char newname[NAME_LENGTH];
@@ -1982,7 +2372,7 @@ static void npc_parsename(struct npc_data* nd, const char* name, const char* sta
if( p ) { // <Display name>::<Unique name>
size_t len = p-name;
if( len > NAME_LENGTH ) {
- ShowWarning("npc_parsename: Display name of '%s' is too long (len=%u) in file '%s', line'%d'. Truncating to %u characters.\n", name, (unsigned int)len, filepath, strline(buffer,start-buffer), NAME_LENGTH);
+ ShowWarning("npc_parsename: Display name of '%s' is too long (len=%u) in file '%s', line '%d'. Truncating to %u characters.\n", name, (unsigned int)len, filepath, strline(buffer,start-buffer), NAME_LENGTH);
safestrncpy(nd->name, name, sizeof(nd->name));
} else {
memcpy(nd->name, name, len);
@@ -1990,23 +2380,23 @@ static void npc_parsename(struct npc_data* nd, const char* name, const char* sta
}
len = strlen(p+2);
if( len > NAME_LENGTH )
- ShowWarning("npc_parsename: Unique name of '%s' is too long (len=%u) in file '%s', line'%d'. Truncating to %u characters.\n", name, (unsigned int)len, filepath, strline(buffer,start-buffer), NAME_LENGTH);
+ ShowWarning("npc_parsename: Unique name of '%s' is too long (len=%u) in file '%s', line '%d'. Truncating to %u characters.\n", name, (unsigned int)len, filepath, strline(buffer,start-buffer), NAME_LENGTH);
safestrncpy(nd->exname, p+2, sizeof(nd->exname));
} else {// <Display name>
size_t len = strlen(name);
if( len > NAME_LENGTH )
- ShowWarning("npc_parsename: Name '%s' is too long (len=%u) in file '%s', line'%d'. Truncating to %u characters.\n", name, (unsigned int)len, filepath, strline(buffer,start-buffer), NAME_LENGTH);
+ ShowWarning("npc_parsename: Name '%s' is too long (len=%u) in file '%s', line '%d'. Truncating to %u characters.\n", name, (unsigned int)len, filepath, strline(buffer,start-buffer), NAME_LENGTH);
safestrncpy(nd->name, name, sizeof(nd->name));
safestrncpy(nd->exname, name, sizeof(nd->exname));
}
if( *nd->exname == '\0' || strstr(nd->exname,"::") != NULL ) {// invalid
snprintf(newname, ARRAYLENGTH(newname), "0_%d_%d_%d", nd->bl.m, nd->bl.x, nd->bl.y);
- ShowWarning("npc_parsename: Invalid unique name in file '%s', line'%d'. Renaming '%s' to '%s'.\n", filepath, strline(buffer,start-buffer), nd->exname, newname);
+ ShowWarning("npc_parsename: Invalid unique name in file '%s', line '%d'. Renaming '%s' to '%s'.\n", filepath, strline(buffer,start-buffer), nd->exname, newname);
safestrncpy(nd->exname, newname, sizeof(nd->exname));
}
- if( (dnd=npc_name2id(nd->exname)) != NULL ) {// duplicate unique name, generate new one
+ if( (dnd=npc->name2id(nd->exname)) != NULL ) {// duplicate unique name, generate new one
char this_mapname[32];
char other_mapname[32];
int i = 0;
@@ -2014,12 +2404,12 @@ static void npc_parsename(struct npc_data* nd, const char* name, const char* sta
do {
++i;
snprintf(newname, ARRAYLENGTH(newname), "%d_%d_%d_%d", i, nd->bl.m, nd->bl.x, nd->bl.y);
- } while( npc_name2id(newname) != NULL );
+ } while( npc->name2id(newname) != NULL );
- strcpy(this_mapname, (nd->bl.m==-1?"(not on a map)":mapindex_id2name(map[nd->bl.m].index)));
- strcpy(other_mapname, (dnd->bl.m==-1?"(not on a map)":mapindex_id2name(map[dnd->bl.m].index)));
+ strcpy(this_mapname, (nd->bl.m == -1 ? "(not on a map)" : mapindex_id2name(map_id2index(nd->bl.m))));
+ strcpy(other_mapname, (dnd->bl.m == -1 ? "(not on a map)" : mapindex_id2name(map_id2index(dnd->bl.m))));
- ShowWarning("npc_parsename: Duplicate unique name in file '%s', line'%d'. Renaming '%s' to '%s'.\n", filepath, strline(buffer,start-buffer), nd->exname, newname);
+ ShowWarning("npc_parsename: Duplicate unique name in file '%s', line '%d'. Renaming '%s' to '%s'.\n", filepath, strline(buffer,start-buffer), nd->exname, newname);
ShowDebug("this npc:\n display name '%s'\n unique name '%s'\n map=%s, x=%d, y=%d\n", nd->name, nd->exname, this_mapname, nd->bl.x, nd->bl.y);
ShowDebug("other npc in '%s' :\n display name '%s'\n unique name '%s'\n map=%s, x=%d, y=%d\n",dnd->path, dnd->name, dnd->exname, other_mapname, dnd->bl.x, dnd->bl.y);
safestrncpy(nd->exname, newname, sizeof(nd->exname));
@@ -2028,9 +2418,9 @@ static void npc_parsename(struct npc_data* nd, const char* name, const char* sta
if( npc_last_path != filepath ) {
struct npc_path_data * npd = NULL;
- if( !(npd = strdb_get(npc_path_db,filepath) ) ) {
+ if( !(npd = strdb_get(npc->path_db,filepath) ) ) {
CREATE(npd, struct npc_path_data, 1);
- strdb_put(npc_path_db, filepath, npd);
+ strdb_put(npc->path_db, filepath, npd);
CREATE(npd->path, char, strlen(filepath)+1);
safestrncpy(npd->path, filepath, strlen(filepath)+1);
@@ -2051,27 +2441,74 @@ static void npc_parsename(struct npc_data* nd, const char* name, const char* sta
}
}
+// Parse View
+// Support for using Constants in place of NPC View IDs.
+int npc_parseview(const char* w4, const char* start, const char* buffer, const char* filepath) {
+ int val = -1, i = 0;
+ char viewid[1024]; // Max size of name from const.txt, see script->read_constdb.
+
+ // Extract view ID / constant
+ while (w4[i] != '\0') {
+ if (ISSPACE(w4[i]) || w4[i] == '/' || w4[i] == ',')
+ break;
+
+ i++;
+ }
+
+ safestrncpy(viewid, w4, i+=1);
+
+ // Check if view id is not an ID (only numbers).
+ if(!npc->viewisid(viewid))
+ {
+ // Check if constant exists and get its value.
+ if(!script->get_constant(viewid, &val)) {
+ ShowWarning("npc_parseview: Invalid NPC constant '%s' specified in file '%s', line'%d'. Defaulting to INVISIBLE_CLASS. \n", viewid, filepath, strline(buffer,start-buffer));
+ val = INVISIBLE_CLASS;
+ }
+ } else {
+ // NPC has an ID specified for view id.
+ val = atoi(w4);
+ }
+
+ return val;
+}
+
+// View is ID
+// Checks if given view is an ID or constant.
+bool npc_viewisid(const char * viewid)
+{
+ if(atoi(viewid) != -1)
+ {
+ // Loop through view, looking for non-numeric character.
+ while (*viewid) {
+ if (ISDIGIT(*viewid++) == 0) return false;
+ }
+ }
+
+ return true;
+}
+
//Add then display an npc warp on map
struct npc_data* npc_add_warp(char* name, short from_mapid, short from_x, short from_y, short xs, short ys, unsigned short to_mapindex, short to_x, short to_y) {
int i, flag = 0;
struct npc_data *nd;
CREATE(nd, struct npc_data, 1);
- nd->bl.id = npc_get_new_npc_id();
- iMap->addnpc(from_mapid, nd);
+ nd->bl.id = npc->get_new_npc_id();
+ map->addnpc(from_mapid, nd);
nd->bl.prev = nd->bl.next = NULL;
nd->bl.m = from_mapid;
nd->bl.x = from_x;
nd->bl.y = from_y;
safestrncpy(nd->exname, name, ARRAYLENGTH(nd->exname));
- if (npc_name2id(nd->exname) != NULL)
+ if (npc->name2id(nd->exname) != NULL)
flag = 1;
if (flag == 1)
snprintf(nd->exname, ARRAYLENGTH(nd->exname), "warp_%d_%d_%d", from_mapid, from_x, from_y);
- for( i = 0; npc_name2id(nd->exname) != NULL; ++i )
+ for( i = 0; npc->name2id(nd->exname) != NULL; ++i )
snprintf(nd->exname, ARRAYLENGTH(nd->exname), "warp%d_%d_%d_%d", i, from_mapid, from_x, from_y);
safestrncpy(nd->name, nd->exname, ARRAYLENGTH(nd->name));
@@ -2088,20 +2525,19 @@ struct npc_data* npc_add_warp(char* name, short from_mapid, short from_x, short
nd->u.warp.ys = xs;
nd->bl.type = BL_NPC;
nd->subtype = WARP;
- npc_setcells(nd);
- iMap->addblock(&nd->bl);
- status_set_viewdata(&nd->bl, nd->class_);
- nd->ud = &npc_base_ud;
- if( map[nd->bl.m].users )
+ npc->setcells(nd);
+ map->addblock(&nd->bl);
+ status->set_viewdata(&nd->bl, nd->class_);
+ nd->ud = &npc->base_ud;
+ if( map->list[nd->bl.m].users )
clif->spawn(&nd->bl);
- strdb_put(npcname_db, nd->exname, nd);
+ strdb_put(npc->name_db, nd->exname, nd);
return nd;
}
/// Parses a warp npc.
-static const char* npc_parse_warp(char* w1, char* w2, char* w3, char* w4, const char* start, const char* buffer, const char* filepath)
-{
+const char* npc_parse_warp(char* w1, char* w2, char* w3, char* w4, const char* start, const char* buffer, const char* filepath, int *retval) {
int x, y, xs, ys, to_x, to_y, m;
unsigned short i;
char mapname[32], to_mapname[32];
@@ -2110,34 +2546,36 @@ static const char* npc_parse_warp(char* w1, char* w2, char* w3, char* w4, const
// w1=<from map name>,<fromX>,<fromY>,<facing>
// w4=<spanx>,<spany>,<to map name>,<toX>,<toY>
if( sscanf(w1, "%31[^,],%d,%d", mapname, &x, &y) != 3
- || sscanf(w4, "%d,%d,%31[^,],%d,%d", &xs, &ys, to_mapname, &to_x, &to_y) != 5 )
- {
+ || sscanf(w4, "%d,%d,%31[^,],%d,%d", &xs, &ys, to_mapname, &to_x, &to_y) != 5
+ ) {
ShowError("npc_parse_warp: Invalid warp definition in file '%s', line '%d'.\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n", filepath, strline(buffer,start-buffer), w1, w2, w3, w4);
+ if (retval) *retval = EXIT_FAILURE;
return strchr(start,'\n');// skip and continue
}
- m = iMap->mapname2mapid(mapname);
- i = mapindex_name2id(to_mapname);
- if( i == 0 )
- {
+ m = map->mapname2mapid(mapname);
+ i = mapindex->name2id(to_mapname);
+ if( i == 0 ) {
ShowError("npc_parse_warp: Unknown destination map in file '%s', line '%d' : %s\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n", filepath, strline(buffer,start-buffer), to_mapname, w1, w2, w3, w4);
+ if (retval) *retval = EXIT_FAILURE;
return strchr(start,'\n');// skip and continue
}
- if( m != -1 && ( x < 0 || x >= map[m].xs || y < 0 || y >= map[m].ys ) ) {
- ShowError("npc_parse_warp: out-of-bounds coordinates (\"%s\",%d,%d), map is %dx%d, in file '%s', line '%d'\n", map[m].name, x, y, map[m].xs, map[m].ys,filepath,strline(buffer,start-buffer));
+ if( m != -1 && ( x < 0 || x >= map->list[m].xs || y < 0 || y >= map->list[m].ys ) ) {
+ ShowError("npc_parse_warp: out-of-bounds coordinates (\"%s\",%d,%d), map is %dx%d, in file '%s', line '%d'\n", map->list[m].name, x, y, map->list[m].xs, map->list[m].ys,filepath,strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
return strchr(start,'\n');;//try next
}
CREATE(nd, struct npc_data, 1);
- nd->bl.id = npc_get_new_npc_id();
- iMap->addnpc(m, nd);
+ nd->bl.id = npc->get_new_npc_id();
+ map->addnpc(m, nd);
nd->bl.prev = nd->bl.next = NULL;
nd->bl.m = m;
nd->bl.x = x;
nd->bl.y = y;
- npc_parsename(nd, w3, start, buffer, filepath);
+ npc->parsename(nd, w3, start, buffer, filepath);
if (!battle_config.warp_point_debug)
nd->class_ = WARP_CLASS;
@@ -2153,86 +2591,108 @@ static const char* npc_parse_warp(char* w1, char* w2, char* w3, char* w4, const
npc_warp++;
nd->bl.type = BL_NPC;
nd->subtype = WARP;
- npc_setcells(nd);
- iMap->addblock(&nd->bl);
- status_set_viewdata(&nd->bl, nd->class_);
- nd->ud = &npc_base_ud;
- if( map[nd->bl.m].users )
+ npc->setcells(nd);
+ map->addblock(&nd->bl);
+ status->set_viewdata(&nd->bl, nd->class_);
+ nd->ud = &npc->base_ud;
+ if( map->list[nd->bl.m].users )
clif->spawn(&nd->bl);
- strdb_put(npcname_db, nd->exname, nd);
+ strdb_put(npc->name_db, nd->exname, nd);
return strchr(start,'\n');// continue
}
-/// Parses a shop/cashshop npc.
-static const char* npc_parse_shop(char* w1, char* w2, char* w3, char* w4, const char* start, const char* buffer, const char* filepath)
-{
+/**
+ * Parses a SHOP/CASHSHOP npc
+ * @param retval Pointer to the status, used to know whether there was an error or not, if so it will be EXIT_FAILURE
+ * @retval Parsing position (currently only '\n')
+ **/
+const char* npc_parse_shop(char* w1, char* w2, char* w3, char* w4, const char* start, const char* buffer, const char* filepath, int *retval) {
//TODO: could be rewritten to NOT need this temp array [ultramage]
- #define MAX_SHOPITEM 100
- struct npc_item_list items[MAX_SHOPITEM];
+ // We could use nd->u.shop.shop_item to store directly the items, but this could lead
+ // to unecessary memory usage by the server, using a temp dynamic array is the
+ // best way to do this without having to do multiple reallocs [Panikon]
+ struct npc_item_list *items = NULL;
+ size_t items_count = 40; // Starting items size
+
char *p;
int x, y, dir, m, i;
struct npc_data *nd;
enum npc_subtype type;
- if( strcmp(w1,"-") == 0 ) {// 'floating' shop?
+ if( strcmp(w1,"-") == 0 ) {
+ // 'floating' shop
x = y = dir = 0;
m = -1;
} else {// w1=<map name>,<x>,<y>,<facing>
char mapname[32];
if( sscanf(w1, "%31[^,],%d,%d,%d", mapname, &x, &y, &dir) != 4
- || strchr(w4, ',') == NULL )
- {
+ || strchr(w4, ',') == NULL
+ ) {
ShowError("npc_parse_shop: Invalid shop definition in file '%s', line '%d'.\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n", filepath, strline(buffer,start-buffer), w1, w2, w3, w4);
+ if (retval) *retval = EXIT_FAILURE;
return strchr(start,'\n');// skip and continue
}
- m = iMap->mapname2mapid(mapname);
+ m = map->mapname2mapid(mapname);
}
- if( m != -1 && ( x < 0 || x >= map[m].xs || y < 0 || y >= map[m].ys ) ) {
- ShowError("npc_parse_shop: out-of-bounds coordinates (\"%s\",%d,%d), map is %dx%d, in file '%s', line '%d'\n", map[m].name, x, y, map[m].xs, map[m].ys,filepath,strline(buffer,start-buffer));
- return strchr(start,'\n');;//try next
+ if( m != -1 && ( x < 0 || x >= map->list[m].xs || y < 0 || y >= map->list[m].ys ) ) {
+ ShowError("npc_parse_shop: out-of-bounds coordinates (\"%s\",%d,%d), map is %dx%d, in file '%s', line '%d'\n", map->list[m].name, x, y, map->list[m].xs, map->list[m].ys,filepath,strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
+ return strchr(start,'\n');//try next
}
- if( !strcasecmp(w2,"cashshop") )
+ if( strcmp(w2,"cashshop") == 0 )
type = CASHSHOP;
else
type = SHOP;
+ items = aMalloc(sizeof(items[0])*items_count);
+
p = strchr(w4,',');
- for( i = 0; i < ARRAYLENGTH(items) && p; ++i )
- {
+ for( i = 0; p; ++i ) {
int nameid, value;
struct item_data* id;
- if( sscanf(p, ",%d:%d", &nameid, &value) != 2 )
- {
+
+ if( i == items_count-1 ) { // Grow array
+ items_count *= 2;
+ items = aRealloc(items, sizeof(items[0])*items_count);
+ }
+
+ if( sscanf(p, ",%d:%d", &nameid, &value) != 2 ) {
ShowError("npc_parse_shop: Invalid item definition in file '%s', line '%d'. Ignoring the rest of the line...\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n", filepath, strline(buffer,start-buffer), w1, w2, w3, w4);
+ if (retval) *retval = EXIT_FAILURE;
break;
}
- if( (id = itemdb_exists(nameid)) == NULL )
- {
+ if( (id = itemdb->exists(nameid)) == NULL ) {
ShowWarning("npc_parse_shop: Invalid sell item in file '%s', line '%d' (id '%d').\n", filepath, strline(buffer,start-buffer), nameid);
p = strchr(p+1,',');
+ if (retval) *retval = EXIT_FAILURE;
continue;
}
- if( value < 0 )
- {
+ if( value < 0 ) {
+ if( value != -1 )
+ ShowWarning("npc_parse_shop: Item %s [%d] with invalid selling value '%d' in file '%s', line '%d', defaulting to buy price...\n",
+ id->name, nameid, value, filepath, strline(buffer,start-buffer));
+
if( type == SHOP ) value = id->value_buy;
else value = 0; // Cashshop doesn't have a "buy price" in the item_db
}
- if( type == SHOP && value == 0 )
- { // NPC selling items for free!
+ if( type == SHOP && value == 0 ) {
+ // NPC selling items for free!
ShowWarning("npc_parse_shop: Item %s [%d] is being sold for FREE in file '%s', line '%d'.\n",
id->name, nameid, filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
}
- if( type == SHOP && value*0.75 < id->value_sell*1.24 )
- {// Exploit possible: you can buy and sell back with profit
- ShowWarning("npc_parse_shop: Item %s [%d] discounted buying price (%d->%d) is less than overcharged selling price (%d->%d) at file '%s', line '%d'.\n",
+ if( type == SHOP && value*0.75 < id->value_sell*1.24 ) {
+ // Exploit possible: you can buy and sell back with profit
+ ShowWarning("npc_parse_shop: Item %s [%d] discounted buying price (%d->%d) is less than overcharged selling price (%d->%d) in file '%s', line '%d'.\n",
id->name, nameid, value, (int)(value*0.75), id->value_sell, (int)(id->value_sell*1.24), filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
}
//for logs filters, atcommands and iteminfo script command
if( id->maxchance == 0 )
@@ -2242,95 +2702,79 @@ static const char* npc_parse_shop(char* w1, char* w2, char* w3, char* w4, const
items[i].value = value;
p = strchr(p+1,',');
}
- if( i == 0 )
- {
+ if( i == 0 ) {
ShowWarning("npc_parse_shop: Ignoring empty shop in file '%s', line '%d'.\n", filepath, strline(buffer,start-buffer));
+ aFree(items);
+ if (retval) *retval = EXIT_FAILURE;
return strchr(start,'\n');// continue
}
CREATE(nd, struct npc_data, 1);
CREATE(nd->u.shop.shop_item, struct npc_item_list, i);
- memcpy(nd->u.shop.shop_item, items, sizeof(struct npc_item_list)*i);
+ memcpy(nd->u.shop.shop_item, items, sizeof(items[0])*i);
+ aFree(items);
+
nd->u.shop.count = i;
nd->bl.prev = nd->bl.next = NULL;
nd->bl.m = m;
nd->bl.x = x;
nd->bl.y = y;
- nd->bl.id = npc_get_new_npc_id();
- npc_parsename(nd, w3, start, buffer, filepath);
- nd->class_ = m==-1?-1:atoi(w4);
+ nd->bl.id = npc->get_new_npc_id();
+ npc->parsename(nd, w3, start, buffer, filepath);
+ nd->class_ = m == -1 ? -1 : npc->parseview(w4, start, buffer, filepath);
nd->speed = 200;
++npc_shop;
nd->bl.type = BL_NPC;
nd->subtype = type;
if( m >= 0 ) {// normal shop npc
- iMap->addnpc(m,nd);
- iMap->addblock(&nd->bl);
- status_set_viewdata(&nd->bl, nd->class_);
- nd->ud = &npc_base_ud;
+ map->addnpc(m,nd);
+ map->addblock(&nd->bl);
+ status->set_viewdata(&nd->bl, nd->class_);
+ nd->ud = &npc->base_ud;
nd->dir = dir;
- if( map[nd->bl.m].users )
+ if( map->list[nd->bl.m].users )
clif->spawn(&nd->bl);
- } else {// 'floating' shop?
- iMap->addiddb(&nd->bl);
+ } else {// 'floating' shop
+ map->addiddb(&nd->bl);
}
- strdb_put(npcname_db, nd->exname, nd);
+ strdb_put(npc->name_db, nd->exname, nd);
return strchr(start,'\n');// continue
}
-/**
- * NPC other label
- * Not sure, seem to add label in a chainlink
- * @see DBApply
- */
-int npc_convertlabel_db(DBKey key, DBData *data, va_list ap)
-{
- const char* lname = (const char*)key.str;
- int lpos = DB->data2i(data);
- struct npc_label_list** label_list;
- int* label_list_num;
- const char* filepath;
- struct npc_label_list* label;
- const char *p;
- int len;
-
- nullpo_ret(label_list = va_arg(ap,struct npc_label_list**));
- nullpo_ret(label_list_num = va_arg(ap,int*));
- nullpo_ret(filepath = va_arg(ap,const char*));
-
- // In case of labels not terminated with ':', for user defined function support
- p = lname;
- while( ISALNUM(*p) || *p == '_' )
- ++p;
- len = p-lname;
-
- // here we check if the label fit into the buffer
- if( len > 23 )
- {
- ShowError("npc_parse_script: label name longer than 23 chars! '%s'\n (%s)", lname, filepath);
- return 0;
- }
-
- if( *label_list == NULL )
- {
- *label_list = (struct npc_label_list *) aCalloc (1, sizeof(struct npc_label_list));
- *label_list_num = 0;
- } else
- *label_list = (struct npc_label_list *) aRealloc (*label_list, sizeof(struct npc_label_list)*(*label_list_num+1));
- label = *label_list+*label_list_num;
+void npc_convertlabel_db(struct npc_label_list* label_list, const char *filepath) {
+ int i;
+
+ for( i = 0; i < script->label_count; i++ ) {
+ const char* lname = script->get_str(script->labels[i].key);
+ int lpos = script->labels[i].pos;
+ struct npc_label_list* label;
+ const char *p;
+ size_t len;
+
+ // In case of labels not terminated with ':', for user defined function support
+ p = lname;
- safestrncpy(label->name, lname, sizeof(label->name));
- label->pos = lpos;
- ++(*label_list_num);
+ while( ISALNUM(*p) || *p == '_' )
+ ++p;
+ len = p-lname;
- return 0;
+ // here we check if the label fit into the buffer
+ if( len > 23 ) {
+ ShowError("npc_parse_script: label name longer than 23 chars! (%s) in file '%s'.\n", lname, filepath);
+ return;
+ }
+
+ label = &label_list[i];
+
+ safestrncpy(label->name, lname, sizeof(label->name));
+ label->pos = lpos;
+ }
}
// Skip the contents of a script.
-static const char* npc_skip_script(const char* start, const char* buffer, const char* filepath)
-{
+const char* npc_skip_script(const char* start, const char* buffer, const char* filepath, int *retval) {
const char* p;
int curly_count;
@@ -2339,16 +2783,16 @@ static const char* npc_skip_script(const char* start, const char* buffer, const
// initial bracket (assumes the previous part is ok)
p = strchr(start,'{');
- if( p == NULL )
- {
- ShowError("npc_skip_script: Missing left curly in file '%s', line'%d'.", filepath, strline(buffer,start-buffer));
+ if( p == NULL ) {
+ ShowError("npc_skip_script: Missing left curly in file '%s', line '%d'.\n", filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
return NULL;// can't continue
}
// skip everything
for( curly_count = 1; curly_count > 0 ; )
{
- p = skip_space(p+1) ;
+ p = script->skip_space(p+1) ;
if( *p == '}' )
{// right curly
--curly_count;
@@ -2361,23 +2805,23 @@ static const char* npc_skip_script(const char* start, const char* buffer, const
{// string
for( ++p; *p != '"' ; ++p )
{
- if( *p == '\\' && (unsigned char)p[-1] <= 0x7e )
+ if( *p == '\\' && (unsigned char)p[-1] <= 0x7e ) {
++p;// escape sequence (not part of a multibyte character)
- else if( *p == '\0' )
- {
- script_error(buffer, filepath, 0, "Unexpected end of string.", p);
+ } else if( *p == '\0' ) {
+ script->error(buffer, filepath, 0, "Unexpected end of string.", p);
+ if (retval) *retval = EXIT_FAILURE;
return NULL;// can't continue
- }
- else if( *p == '\n' )
- {
- script_error(buffer, filepath, 0, "Unexpected newline at string.", p);
+ } else if( *p == '\n' ) {
+ script->error(buffer, filepath, 0, "Unexpected newline at string.", p);
+ if (retval) *retval = EXIT_FAILURE;
return NULL;// can't continue
}
}
}
else if( *p == '\0' )
{// end of buffer
- ShowError("Missing %d right curlys at file '%s', line '%d'.\n", curly_count, filepath, strline(buffer,p-buffer));
+ ShowError("Missing %d right curlys in file '%s', line '%d'.\n", curly_count, filepath, strline(buffer,p-buffer));
+ if (retval) *retval = EXIT_FAILURE;
return NULL;// can't continue
}
}
@@ -2390,10 +2834,10 @@ static const char* npc_skip_script(const char* start, const char* buffer, const
/// -%TAB%script%TAB%<NPC Name>%TAB%-1,{<code>}
/// <map name>,<x>,<y>,<facing>%TAB%script%TAB%<NPC Name>%TAB%<sprite id>,{<code>}
/// <map name>,<x>,<y>,<facing>%TAB%script%TAB%<NPC Name>%TAB%<sprite id>,<triggerX>,<triggerY>,{<code>}
-static const char* npc_parse_script(char* w1, char* w2, char* w3, char* w4, const char* start, const char* buffer, const char* filepath, bool runOnInit) {
- int x, y, dir = 0, m, xs = 0, ys = 0, class_ = 0; // [Valaris] thanks to fov
+const char* npc_parse_script(char* w1, char* w2, char* w3, char* w4, const char* start, const char* buffer, const char* filepath, int options, int *retval) {
+ int x, y, dir = 0, m, xs = 0, ys = 0; // [Valaris] thanks to fov
char mapname[32];
- struct script_code *script;
+ struct script_code *scriptroot;
int i;
const char* end;
const char* script_start;
@@ -2407,15 +2851,13 @@ static const char* npc_parse_script(char* w1, char* w2, char* w3, char* w4, cons
x = 0;
y = 0;
m = -1;
- }
- else
- {// npc in a map
- if( sscanf(w1, "%31[^,],%d,%d,%d", mapname, &x, &y, &dir) != 4 )
- {
+ } else {// npc in a map
+ if( sscanf(w1, "%31[^,],%d,%d,%d", mapname, &x, &y, &dir) != 4 ) {
ShowError("npc_parse_script: Invalid placement format for a script in file '%s', line '%d'. Skipping the rest of file...\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n", filepath, strline(buffer,start-buffer), w1, w2, w3, w4);
+ if (retval) *retval = EXIT_FAILURE;
return NULL;// unknown format, don't continue
}
- m = iMap->mapname2mapid(mapname);
+ m = map->mapname2mapid(mapname);
}
script_start = strstr(start,",{");
@@ -2423,34 +2865,33 @@ static const char* npc_parse_script(char* w1, char* w2, char* w3, char* w4, cons
if( strstr(w4,",{") == NULL || script_start == NULL || (end != NULL && script_start > end) )
{
ShowError("npc_parse_script: Missing left curly ',{' in file '%s', line '%d'. Skipping the rest of the file.\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n", filepath, strline(buffer,start-buffer), w1, w2, w3, w4);
+ if (retval) *retval = EXIT_FAILURE;
return NULL;// can't continue
}
++script_start;
- end = npc_skip_script(script_start, buffer, filepath);
+ end = npc->skip_script(script_start, buffer, filepath, retval);
if( end == NULL )
return NULL;// (simple) parse error, don't continue
- script = parse_script(script_start, filepath, strline(buffer,script_start-buffer), SCRIPT_USE_LABEL_DB);
+ scriptroot = script->parse(script_start, filepath, strline(buffer,script_start-buffer), SCRIPT_USE_LABEL_DB, retval);
label_list = NULL;
label_list_num = 0;
- if( script )
- {
- DBMap* label_db = script_get_label_db();
- label_db->foreach(label_db, npc_convertlabel_db, &label_list, &label_list_num, filepath);
- db_clear(label_db); // not needed anymore, so clear the db
+ if( script->label_count ) {
+ CREATE(label_list,struct npc_label_list,script->label_count);
+ label_list_num = script->label_count;
+ npc->convertlabel_db(label_list,filepath);
}
CREATE(nd, struct npc_data, 1);
- if( sscanf(w4, "%d,%d,%d", &class_, &xs, &ys) == 3 )
+ if( sscanf(w4, "%*[^,],%d,%d", &xs, &ys) == 2 )
{// OnTouch area defined
nd->u.scr.xs = xs;
nd->u.scr.ys = ys;
}
else
{// no OnTouch area
- class_ = atoi(w4);
nd->u.scr.xs = -1;
nd->u.scr.ys = -1;
}
@@ -2459,57 +2900,61 @@ static const char* npc_parse_script(char* w1, char* w2, char* w3, char* w4, cons
nd->bl.m = m;
nd->bl.x = x;
nd->bl.y = y;
- npc_parsename(nd, w3, start, buffer, filepath);
- nd->bl.id = npc_get_new_npc_id();
- nd->class_ = class_;
+ npc->parsename(nd, w3, start, buffer, filepath);
+ nd->bl.id = npc->get_new_npc_id();
+ nd->class_ = m == -1 ? -1 : npc->parseview(w4, start, buffer, filepath);
nd->speed = 200;
- nd->u.scr.script = script;
+ nd->u.scr.script = scriptroot;
nd->u.scr.label_list = label_list;
nd->u.scr.label_list_num = label_list_num;
-
+ if( options&NPO_TRADER )
+ nd->u.scr.trader = true;
+ nd->u.scr.shop = NULL;
+
++npc_script;
nd->bl.type = BL_NPC;
nd->subtype = SCRIPT;
if( m >= 0 ) {
- iMap->addnpc(m, nd);
- nd->ud = &npc_base_ud;
+ map->addnpc(m, nd);
+ nd->ud = &npc->base_ud;
nd->dir = dir;
- npc_setcells(nd);
- iMap->addblock(&nd->bl);
- if( class_ >= 0 ) {
- status_set_viewdata(&nd->bl, nd->class_);
- if( map[nd->bl.m].users )
+ npc->setcells(nd);
+ map->addblock(&nd->bl);
+ if( nd->class_ >= 0 ) {
+ status->set_viewdata(&nd->bl, nd->class_);
+ if( map->list[nd->bl.m].users )
clif->spawn(&nd->bl);
}
} else {
- // we skip iMap->addnpc, but still add it to the list of ID's
- iMap->addiddb(&nd->bl);
+ // we skip map->addnpc, but still add it to the list of ID's
+ map->addiddb(&nd->bl);
}
- strdb_put(npcname_db, nd->exname, nd);
+ strdb_put(npc->name_db, nd->exname, nd);
//-----------------------------------------
// Loop through labels to export them as necessary
for (i = 0; i < nd->u.scr.label_list_num; i++) {
- if (npc_event_export(nd, i)) {
- ShowWarning("npc_parse_script : duplicate event %s::%s (%s)\n",
+ if (npc->event_export(nd, i)) {
+ ShowWarning("npc_parse_script: duplicate event %s::%s in file '%s'.\n",
nd->exname, nd->u.scr.label_list[i].name, filepath);
+ if (retval) *retval = EXIT_FAILURE;
}
- npc_timerevent_export(nd, i);
+ npc->timerevent_export(nd, i);
}
nd->u.scr.timerid = INVALID_TIMER;
- if( runOnInit ) {
+ if( options&NPO_ONINIT ) {
char evname[EVENT_NAME_LENGTH];
struct event_data *ev;
snprintf(evname, ARRAYLENGTH(evname), "%s::OnInit", nd->exname);
- if( ( ev = (struct event_data*)strdb_get(ev_db, evname) ) ) {
+ if( ( ev = (struct event_data*)strdb_get(npc->ev_db, evname) ) ) {
//Execute OnInit
- run_script(nd->u.scr.script,ev->pos,0,nd->bl.id);
+ script->run(nd->u.scr.script,ev->pos,0,nd->bl.id);
}
}
@@ -2523,9 +2968,9 @@ static const char* npc_parse_script(char* w1, char* w2, char* w3, char* w4, cons
/// shop/cashshop/npc: <map name>,<x>,<y>,<facing>%TAB%duplicate(<name of target>)%TAB%<NPC Name>%TAB%<sprite id>
/// npc: -%TAB%duplicate(<name of target>)%TAB%<NPC Name>%TAB%<sprite id>,<triggerX>,<triggerY>
/// npc: <map name>,<x>,<y>,<facing>%TAB%duplicate(<name of target>)%TAB%<NPC Name>%TAB%<sprite id>,<triggerX>,<triggerY>
-const char* npc_parse_duplicate(char* w1, char* w2, char* w3, char* w4, const char* start, const char* buffer, const char* filepath)
-{
- int x, y, dir, m, xs = -1, ys = -1, class_ = 0;
+/// !!Only NPO_ONINIT is available trough options!!
+const char* npc_parse_duplicate(char* w1, char* w2, char* w3, char* w4, const char* start, const char* buffer, const char* filepath, int options, int *retval) {
+ int x, y, dir, m, xs = -1, ys = -1;
char mapname[32];
char srcname[128];
int i;
@@ -2543,14 +2988,16 @@ const char* npc_parse_duplicate(char* w1, char* w2, char* w3, char* w4, const ch
// get the npc being duplicated
if( w2[length-1] != ')' || length <= 11 || length-11 >= sizeof(srcname) )
{// does not match 'duplicate(%127s)', name is empty or too long
- ShowError("npc_parse_script: bad duplicate name in file '%s', line '%d' : %s\n", filepath, strline(buffer,start-buffer), w2);
+ ShowError("npc_parse_script: bad duplicate name in file '%s', line '%d': %s\n", filepath, strline(buffer,start-buffer), w2);
+ if (retval) *retval = EXIT_FAILURE;
return end;// next line, try to continue
}
safestrncpy(srcname, w2+10, length-10);
- dnd = npc_name2id(srcname);
+ dnd = npc->name2id(srcname);
if( dnd == NULL) {
- ShowError("npc_parse_script: original npc not found for duplicate in file '%s', line '%d' : %s\n", filepath, strline(buffer,start-buffer), srcname);
+ ShowError("npc_parse_script: original npc not found for duplicate in file '%s', line '%d': %s\n", filepath, strline(buffer,start-buffer), srcname);
+ if (retval) *retval = EXIT_FAILURE;
return end;// next line, try to continue
}
src_id = dnd->bl.id;
@@ -2561,24 +3008,28 @@ const char* npc_parse_duplicate(char* w1, char* w2, char* w3, char* w4, const ch
x = y = dir = 0;
m = -1;
} else {
- if( sscanf(w1, "%31[^,],%d,%d,%d", mapname, &x, &y, &dir) != 4 )// <map name>,<x>,<y>,<facing>
- {
+ int fields = sscanf(w1, "%31[^,],%d,%d,%d", mapname, &x, &y, &dir);
+ if( type == WARP && fields == 3 ) { // <map name>,<x>,<y>
+ dir = 0;
+ } else if( fields != 4 ) {// <map name>,<x>,<y>,<facing>
ShowError("npc_parse_duplicate: Invalid placement format for duplicate in file '%s', line '%d'. Skipping line...\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n", filepath, strline(buffer,start-buffer), w1, w2, w3, w4);
+ if (retval) *retval = EXIT_FAILURE;
return end;// next line, try to continue
}
- m = iMap->mapname2mapid(mapname);
+ m = map->mapname2mapid(mapname);
}
- if( m != -1 && ( x < 0 || x >= map[m].xs || y < 0 || y >= map[m].ys ) ) {
- ShowError("npc_parse_duplicate: out-of-bounds coordinates (\"%s\",%d,%d), map is %dx%d, in file '%s', line '%d'\n", map[m].name, x, y, map[m].xs, map[m].ys,filepath,strline(buffer,start-buffer));
+ if( m != -1 && ( x < 0 || x >= map->list[m].xs || y < 0 || y >= map->list[m].ys ) ) {
+ ShowError("npc_parse_duplicate: out-of-bounds coordinates (\"%s\",%d,%d), map is %dx%d, in file '%s', line '%d'\n", map->list[m].name, x, y, map->list[m].xs, map->list[m].ys,filepath,strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
return end;//try next
}
if( type == WARP && sscanf(w4, "%d,%d", &xs, &ys) == 2 );// <spanx>,<spany>
- else if( type == SCRIPT && sscanf(w4, "%d,%d,%d", &class_, &xs, &ys) == 3);// <sprite id>,<triggerX>,<triggerY>
- else if( type != WARP ) class_ = atoi(w4);// <sprite id>
- else {
+ else if( type == SCRIPT && sscanf(w4, "%*[^,],%d,%d", &xs, &ys) == 2);// <sprite id>,<triggerX>,<triggerY>
+ else if( type == WARP ) {
ShowError("npc_parse_duplicate: Invalid span format for duplicate warp in file '%s', line '%d'. Skipping line...\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n", filepath, strline(buffer,start-buffer), w1, w2, w3, w4);
+ if (retval) *retval = EXIT_FAILURE;
return end;// next line, try to continue
}
@@ -2588,9 +3039,9 @@ const char* npc_parse_duplicate(char* w1, char* w2, char* w3, char* w4, const ch
nd->bl.m = m;
nd->bl.x = x;
nd->bl.y = y;
- npc_parsename(nd, w3, start, buffer, filepath);
- nd->bl.id = npc_get_new_npc_id();
- nd->class_ = class_;
+ npc->parsename(nd, w3, start, buffer, filepath);
+ nd->bl.id = npc->get_new_npc_id();
+ nd->class_ = m == -1 ? -1 : npc->parseview(w4, start, buffer, filepath);
nd->speed = 200;
nd->src_id = src_id;
nd->bl.type = BL_NPC;
@@ -2603,6 +3054,8 @@ const char* npc_parse_duplicate(char* w1, char* w2, char* w3, char* w4, const ch
nd->u.scr.script = dnd->u.scr.script;
nd->u.scr.label_list = dnd->u.scr.label_list;
nd->u.scr.label_list_num = dnd->u.scr.label_list_num;
+ nd->u.scr.shop = dnd->u.scr.shop;
+ nd->u.scr.trader = dnd->u.scr.trader;
break;
case SHOP:
@@ -2628,21 +3081,21 @@ const char* npc_parse_duplicate(char* w1, char* w2, char* w3, char* w4, const ch
//Add the npc to its location
if( m >= 0 ) {
- iMap->addnpc(m, nd);
- nd->ud = &npc_base_ud;
+ map->addnpc(m, nd);
+ nd->ud = &npc->base_ud;
nd->dir = dir;
- npc_setcells(nd);
- iMap->addblock(&nd->bl);
- if( class_ >= 0 ) {
- status_set_viewdata(&nd->bl, nd->class_);
- if( map[nd->bl.m].users )
+ npc->setcells(nd);
+ map->addblock(&nd->bl);
+ if( nd->class_ >= 0 ) {
+ status->set_viewdata(&nd->bl, nd->class_);
+ if( map->list[nd->bl.m].users )
clif->spawn(&nd->bl);
}
} else {
- // we skip iMap->addnpc, but still add it to the list of ID's
- iMap->addiddb(&nd->bl);
+ // we skip map->addnpc, but still add it to the list of ID's
+ map->addiddb(&nd->bl);
}
- strdb_put(npcname_db, nd->exname, nd);
+ strdb_put(npc->name_db, nd->exname, nd);
if( type != SCRIPT )
return end;
@@ -2650,43 +3103,58 @@ const char* npc_parse_duplicate(char* w1, char* w2, char* w3, char* w4, const ch
//-----------------------------------------
// Loop through labels to export them as necessary
for (i = 0; i < nd->u.scr.label_list_num; i++) {
- if (npc_event_export(nd, i)) {
- ShowWarning("npc_parse_duplicate : duplicate event %s::%s (%s)\n",
+ if (npc->event_export(nd, i)) {
+ ShowWarning("npc_parse_duplicate: duplicate event %s::%s in file '%s'.\n",
nd->exname, nd->u.scr.label_list[i].name, filepath);
+ if (retval) *retval = EXIT_FAILURE;
}
- npc_timerevent_export(nd, i);
+ npc->timerevent_export(nd, i);
}
nd->u.scr.timerid = INVALID_TIMER;
+ if( type == SCRIPT && options&NPO_ONINIT ) {
+ // From npc_parse_script
+ char evname[EVENT_NAME_LENGTH];
+ struct event_data *ev;
+
+ snprintf(evname, ARRAYLENGTH(evname), "%s::OnInit", nd->exname);
+
+ if( ( ev = (struct event_data*)strdb_get(npc->ev_db, evname) ) ) {
+
+ //Execute OnInit
+ script->run(nd->u.scr.script,ev->pos,0,nd->bl.id);
+
+ }
+ }
return end;
}
int npc_duplicate4instance(struct npc_data *snd, int16 m) {
char newname[NAME_LENGTH];
- if( map[m].instance_id == -1 )
+ if( m == -1 || map->list[m].instance_id == -1 )
return 1;
- snprintf(newname, ARRAYLENGTH(newname), "dup_%d_%d", map[m].instance_id, snd->bl.id);
- if( npc_name2id(newname) != NULL ) { // Name already in use
- ShowError("npc_duplicate4instance: the npcname (%s) is already in use while trying to duplicate npc %s in instance %d.\n", newname, snd->exname, map[m].instance_id);
+ snprintf(newname, ARRAYLENGTH(newname), "dup_%d_%d", map->list[m].instance_id, snd->bl.id);
+ if( npc->name2id(newname) != NULL ) { // Name already in use
+ ShowError("npc_duplicate4instance: the npcname (%s) is already in use while trying to duplicate npc %s in instance %d.\n", newname, snd->exname, map->list[m].instance_id);
return 1;
}
if( snd->subtype == WARP ) { // Adjust destination, if instanced
struct npc_data *wnd = NULL; // New NPC
- int dm = iMap->mapindex2mapid(snd->u.warp.mapindex), im;
+ int dm = map->mapindex2mapid(snd->u.warp.mapindex), im;
if( dm < 0 ) return 1;
- if( ( im = instance->mapid2imapid(dm, map[m].instance_id) ) == -1 ) {
- ShowError("npc_duplicate4instance: warp (%s) leading to instanced map (%s), but instance map is not attached to current instance.\n", map[dm].name, snd->exname);
+ if( ( im = instance->mapid2imapid(dm, map->list[m].instance_id) ) == -1 ) {
+ ShowError("npc_duplicate4instance: warp (%s) leading to instanced map (%s), but instance map is not attached to current instance.\n", map->list[dm].name, snd->exname);
return 1;
}
CREATE(wnd, struct npc_data, 1);
- wnd->bl.id = npc_get_new_npc_id();
- iMap->addnpc(m, wnd);
+ wnd->bl.id = npc->get_new_npc_id();
+ map->addnpc(m, wnd);
wnd->bl.prev = wnd->bl.next = NULL;
wnd->bl.m = m;
wnd->bl.x = snd->bl.x;
@@ -2702,18 +3170,18 @@ int npc_duplicate4instance(struct npc_data *snd, int16 m) {
wnd->u.warp.ys = snd->u.warp.ys;
wnd->bl.type = BL_NPC;
wnd->subtype = WARP;
- npc_setcells(wnd);
- iMap->addblock(&wnd->bl);
- status_set_viewdata(&wnd->bl, wnd->class_);
- wnd->ud = &npc_base_ud;
- if( map[wnd->bl.m].users )
+ npc->setcells(wnd);
+ map->addblock(&wnd->bl);
+ status->set_viewdata(&wnd->bl, wnd->class_);
+ wnd->ud = &npc->base_ud;
+ if( map->list[wnd->bl.m].users )
clif->spawn(&wnd->bl);
- strdb_put(npcname_db, wnd->exname, wnd);
+ strdb_put(npc->name_db, wnd->exname, wnd);
} else {
static char w1[50], w2[50], w3[50], w4[50];
const char* stat_buf = "- call from instancing subsystem -\n";
- snprintf(w1, sizeof(w1), "%s,%d,%d,%d", map[m].name, snd->bl.x, snd->bl.y, snd->dir);
+ snprintf(w1, sizeof(w1), "%s,%d,%d,%d", map->list[m].name, snd->bl.x, snd->bl.y, snd->dir);
snprintf(w2, sizeof(w2), "duplicate(%s)", snd->exname);
snprintf(w3, sizeof(w3), "%s::%s", snd->name, newname);
@@ -2722,7 +3190,7 @@ int npc_duplicate4instance(struct npc_data *snd, int16 m) {
else
snprintf(w4, sizeof(w4), "%d", snd->class_);
- npc_parse_duplicate(w1, w2, w3, w4, stat_buf, stat_buf, "INSTANCING");
+ npc->parse_duplicate(w1, w2, w3, w4, stat_buf, stat_buf, "INSTANCING", NPO_NONE, NULL);
}
return 0;
@@ -2746,14 +3214,14 @@ void npc_setcells(struct npc_data* nd) {
return; // Other types doesn't have touch area
}
- if (m < 0 || xs < 0 || ys < 0 || map[m].cell == (struct mapcell *)0xdeadbeaf) //invalid range or map
+ if (m < 0 || xs < 0 || ys < 0 || map->list[m].cell == (struct mapcell *)0xdeadbeaf) //invalid range or map
return;
for (i = y-ys; i <= y+ys; i++) {
for (j = x-xs; j <= x+xs; j++) {
- if (iMap->getcell(m, j, i, CELL_CHKNOPASS))
+ if (map->getcell(m, j, i, CELL_CHKNOPASS))
continue;
- map[m].setcell(m, j, i, CELL_NPC, true);
+ map->list[m].setcell(m, j, i, CELL_NPC, true);
}
}
}
@@ -2762,7 +3230,7 @@ int npc_unsetcells_sub(struct block_list* bl, va_list ap) {
struct npc_data *nd = (struct npc_data*)bl;
int id = va_arg(ap,int);
if (nd->bl.id == id) return 0;
- npc_setcells(nd);
+ npc->setcells(nd);
return 1;
}
@@ -2770,44 +3238,49 @@ void npc_unsetcells(struct npc_data* nd) {
int16 m = nd->bl.m, x = nd->bl.x, y = nd->bl.y, xs, ys;
int i,j, x0, x1, y0, y1;
- if (nd->subtype == WARP) {
- xs = nd->u.warp.xs;
- ys = nd->u.warp.ys;
- } else {
- xs = nd->u.scr.xs;
- ys = nd->u.scr.ys;
+ switch(nd->subtype) {
+ case WARP:
+ xs = nd->u.warp.xs;
+ ys = nd->u.warp.ys;
+ break;
+ case SCRIPT:
+ xs = nd->u.scr.xs;
+ ys = nd->u.scr.ys;
+ break;
+ default:
+ return; // Other types doesn't have touch area
}
- if (m < 0 || xs < 0 || ys < 0 || map[m].cell == (struct mapcell *)0xdeadbeaf)
+ if (m < 0 || xs < 0 || ys < 0 || map->list[m].cell == (struct mapcell *)0xdeadbeaf)
return;
//Locate max range on which we can locate npc cells
//FIXME: does this really do what it's supposed to do? [ultramage]
- for(x0 = x-xs; x0 > 0 && iMap->getcell(m, x0, y, CELL_CHKNPC); x0--);
- for(x1 = x+xs; x1 < map[m].xs-1 && iMap->getcell(m, x1, y, CELL_CHKNPC); x1++);
- for(y0 = y-ys; y0 > 0 && iMap->getcell(m, x, y0, CELL_CHKNPC); y0--);
- for(y1 = y+ys; y1 < map[m].ys-1 && iMap->getcell(m, x, y1, CELL_CHKNPC); y1++);
+ for(x0 = x-xs; x0 > 0 && map->getcell(m, x0, y, CELL_CHKNPC); x0--);
+ for(x1 = x+xs; x1 < map->list[m].xs-1 && map->getcell(m, x1, y, CELL_CHKNPC); x1++);
+ for(y0 = y-ys; y0 > 0 && map->getcell(m, x, y0, CELL_CHKNPC); y0--);
+ for(y1 = y+ys; y1 < map->list[m].ys-1 && map->getcell(m, x, y1, CELL_CHKNPC); y1++);
//Erase this npc's cells
for (i = y-ys; i <= y+ys; i++)
for (j = x-xs; j <= x+xs; j++)
- map[m].setcell(m, j, i, CELL_NPC, false);
+ map->list[m].setcell(m, j, i, CELL_NPC, false);
//Re-deploy NPC cells for other nearby npcs.
- iMap->foreachinarea( npc_unsetcells_sub, m, x0, y0, x1, y1, BL_NPC, nd->bl.id );
+ map->foreachinarea( npc->unsetcells_sub, m, x0, y0, x1, y1, BL_NPC, nd->bl.id );
}
void npc_movenpc(struct npc_data* nd, int16 x, int16 y)
{
const int16 m = nd->bl.m;
- if (m < 0 || nd->bl.prev == NULL) return; //Not on a map.
+ if (m < 0 || nd->bl.prev == NULL) return; //Not on a map.
- x = cap_value(x, 0, map[m].xs-1);
- y = cap_value(y, 0, map[m].ys-1);
+ x = cap_value(x, 0, map->list[m].xs-1);
+ y = cap_value(y, 0, map->list[m].ys-1);
- iMap->foreachinrange(clif->outsight, &nd->bl, AREA_SIZE, BL_PC, &nd->bl);
- iMap->moveblock(&nd->bl, x, y, iTimer->gettick());
- iMap->foreachinrange(clif->insight, &nd->bl, AREA_SIZE, BL_PC, &nd->bl);
+ map->foreachinrange(clif->outsight, &nd->bl, AREA_SIZE, BL_PC, &nd->bl);
+ map->moveblock(&nd->bl, x, y, timer->gettick());
+ map->foreachinrange(clif->insight, &nd->bl, AREA_SIZE, BL_PC, &nd->bl);
}
/// Changes the display name of the npc.
@@ -2819,7 +3292,7 @@ void npc_setdisplayname(struct npc_data* nd, const char* newname)
nullpo_retv(nd);
safestrncpy(nd->name, newname, sizeof(nd->name));
- if( map[nd->bl.m].users )
+ if( map->list[nd->bl.m].users )
clif->charnameack(0, &nd->bl);
}
@@ -2827,25 +3300,24 @@ void npc_setdisplayname(struct npc_data* nd, const char* newname)
///
/// @param nd Target npc
/// @param class_ New display class
-void npc_setclass(struct npc_data* nd, short class_)
-{
+void npc_setclass(struct npc_data* nd, short class_) {
nullpo_retv(nd);
if( nd->class_ == class_ )
return;
- if( map[nd->bl.m].users )
+ if( map->list[nd->bl.m].users )
clif->clearunit_area(&nd->bl, CLR_OUTSIGHT);// fade out
nd->class_ = class_;
- status_set_viewdata(&nd->bl, class_);
- if( map[nd->bl.m].users )
+ status->set_viewdata(&nd->bl, class_);
+ if( map->list[nd->bl.m].users )
clif->spawn(&nd->bl);// fade in
}
// @commands (script based)
int npc_do_atcmd_event(struct map_session_data* sd, const char* command, const char* message, const char* eventname)
{
- struct event_data* ev = (struct event_data*)strdb_get(ev_db, eventname);
+ struct event_data* ev = (struct event_data*)strdb_get(npc->ev_db, eventname);
struct npc_data *nd;
struct script_state *st;
int i = 0, j = 0, k = 0;
@@ -2859,7 +3331,6 @@ int npc_do_atcmd_event(struct map_session_data* sd, const char* command, const c
}
if( sd->npc_id != 0 ) { // Enqueue the event trigger.
- int i;
ARR_FIND( 0, MAX_EVENTQUEUE, i, sd->eventqueue[i][0] == '\0' );
if( i < MAX_EVENTQUEUE ) {
safestrncpy(sd->eventqueue[i],eventname,50); //Event enqueued.
@@ -2871,12 +3342,12 @@ int npc_do_atcmd_event(struct map_session_data* sd, const char* command, const c
}
if( ev->nd->option&OPTION_INVISIBLE ) { // Disabled npc, shouldn't trigger event.
- npc_event_dequeue(sd);
+ npc->event_dequeue(sd);
return 2;
}
- st = script_alloc_state(ev->nd->u.scr.script, ev->pos, sd->bl.id, ev->nd->bl.id);
- setd_sub(st, NULL, ".@atcmd_command$", 0, (void *)command, NULL);
+ st = script->alloc_state(ev->nd->u.scr.script, ev->pos, sd->bl.id, ev->nd->bl.id);
+ script->setd_sub(st, NULL, ".@atcmd_command$", 0, (void *)command, NULL);
// split atcmd parameters based on spaces
temp = (char*)aMalloc(strlen(message) + 1);
@@ -2889,7 +3360,7 @@ int npc_do_atcmd_event(struct map_session_data* sd, const char* command, const c
temp[k] = '\0';
k = 0;
if( temp[0] != '\0' ) {
- setd_sub( st, NULL, ".@atcmd_parameters$", j++, (void *)temp, NULL );
+ script->setd_sub( st, NULL, ".@atcmd_parameters$", j++, (void *)temp, NULL );
}
} else {
temp[k] = message[i];
@@ -2897,46 +3368,44 @@ int npc_do_atcmd_event(struct map_session_data* sd, const char* command, const c
}
}
- setd_sub(st, NULL, ".@atcmd_numparameters", 0, (void *)__64BPTRSIZE(j), NULL);
+ script->setd_sub(st, NULL, ".@atcmd_numparameters", 0, (void *)h64BPTRSIZE(j), NULL);
aFree(temp);
- run_script_main(st);
+ script->run_main(st);
return 0;
}
/// Parses a function.
/// function%TAB%script%TAB%<function name>%TAB%{<code>}
-static const char* npc_parse_function(char* w1, char* w2, char* w3, char* w4, const char* start, const char* buffer, const char* filepath)
-{
+const char* npc_parse_function(char* w1, char* w2, char* w3, char* w4, const char* start, const char* buffer, const char* filepath, int *retval) {
DBMap* func_db;
DBData old_data;
- struct script_code *script;
+ struct script_code *scriptroot;
const char* end;
const char* script_start;
script_start = strstr(start,"\t{");
end = strchr(start,'\n');
- if( *w4 != '{' || script_start == NULL || (end != NULL && script_start > end) )
- {
+ if( *w4 != '{' || script_start == NULL || (end != NULL && script_start > end) ) {
ShowError("npc_parse_function: Missing left curly '%%TAB%%{' in file '%s', line '%d'. Skipping the rest of the file.\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n", filepath, strline(buffer,start-buffer), w1, w2, w3, w4);
+ if (retval) *retval = EXIT_FAILURE;
return NULL;// can't continue
}
++script_start;
- end = npc_skip_script(script_start,buffer,filepath);
+ end = npc->skip_script(script_start,buffer,filepath, retval);
if( end == NULL )
return NULL;// (simple) parse error, don't continue
- script = parse_script(script_start, filepath, strline(buffer,start-buffer), SCRIPT_RETURN_EMPTY_SCRIPT);
- if( script == NULL )// parse error, continue
+ scriptroot = script->parse(script_start, filepath, strline(buffer,start-buffer), SCRIPT_RETURN_EMPTY_SCRIPT, retval);
+ if( scriptroot == NULL )// parse error, continue
return end;
- func_db = script_get_userfunc_db();
- if (func_db->put(func_db, DB->str2key(w3), DB->ptr2data(script), &old_data))
- {
+ func_db = script->userfunc_db;
+ if (func_db->put(func_db, DB->str2key(w3), DB->ptr2data(scriptroot), &old_data)) {
struct script_code *oldscript = (struct script_code*)DB->data2ptr(&old_data);
- ShowInfo("npc_parse_function: Overwriting user function [%s] (%s:%d)\n", w3, filepath, strline(buffer,start-buffer));
- script_free_vars(oldscript->script_vars);
+ ShowWarning("npc_parse_function: Overwriting user function [%s] in file '%s', line '%d'.\n", w3, filepath, strline(buffer,start-buffer));
+ script->free_vars(oldscript->local.vars);
aFree(oldscript->script_buf);
aFree(oldscript);
}
@@ -2950,143 +3419,142 @@ static const char* npc_parse_function(char* w1, char* w2, char* w3, char* w4, co
* Parse Mob 2 - Actually Spawns Mob
* [Wizputer]
*------------------------------------------*/
-void npc_parse_mob2(struct spawn_data* mob)
+void npc_parse_mob2(struct spawn_data* mobspawn)
{
int i;
- for( i = mob->active; i < mob->num; ++i ) {
- struct mob_data* md = mob_spawn_dataset(mob);
- md->spawn = mob;
+ for( i = mobspawn->active; i < mobspawn->num; ++i ) {
+ struct mob_data* md = mob->spawn_dataset(mobspawn);
+ md->spawn = mobspawn;
md->spawn->active++;
- mob_spawn(md);
+ mob->spawn(md);
}
}
-static const char* npc_parse_mob(char* w1, char* w2, char* w3, char* w4, const char* start, const char* buffer, const char* filepath)
-{
+const char* npc_parse_mob(char* w1, char* w2, char* w3, char* w4, const char* start, const char* buffer, const char* filepath, int *retval) {
int num, class_, m,x,y,xs,ys, i,j;
int mob_lv = -1, ai = -1, size = -1;
char mapname[32], mobname[NAME_LENGTH];
- struct spawn_data mob, *data;
+ struct spawn_data mobspawn, *data;
struct mob_db* db;
- memset(&mob, 0, sizeof(struct spawn_data));
+ memset(&mobspawn, 0, sizeof(struct spawn_data));
- mob.state.boss = !strcmpi(w2,"boss_monster");
+ mobspawn.state.boss = (strcmp(w2,"boss_monster") == 0 ? 1 : 0);
// w1=<map name>,<x>,<y>,<xs>,<ys>
// w3=<mob name>{,<mob level>}
// w4=<mob id>,<amount>,<delay1>,<delay2>,<event>{,<mob size>,<mob ai>}
if( sscanf(w1, "%31[^,],%d,%d,%d,%d", mapname, &x, &y, &xs, &ys) < 3
- || sscanf(w3, "%23[^,],%d", mobname, &mob_lv) < 1
- || sscanf(w4, "%d,%d,%u,%u,%127[^,],%d,%d[^\t\r\n]", &class_, &num, &mob.delay1, &mob.delay2, mob.eventname, &size, &ai) < 2 )
- {
+ || sscanf(w3, "%23[^,],%d", mobname, &mob_lv) < 1
+ || sscanf(w4, "%d,%d,%u,%u,%127[^,],%d,%d[^\t\r\n]", &class_, &num, &mobspawn.delay1, &mobspawn.delay2, mobspawn.eventname, &size, &ai) < 2
+ ) {
ShowError("npc_parse_mob: Invalid mob definition in file '%s', line '%d'.\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n", filepath, strline(buffer,start-buffer), w1, w2, w3, w4);
+ if (retval) *retval = EXIT_FAILURE;
return strchr(start,'\n');// skip and continue
}
- if( mapindex_name2id(mapname) == 0 )
- {
+ if( mapindex->name2id(mapname) == 0 ) {
ShowError("npc_parse_mob: Unknown map '%s' in file '%s', line '%d'.\n", mapname, filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
return strchr(start,'\n');// skip and continue
}
- m = iMap->mapname2mapid(mapname);
+ m = map->mapname2mapid(mapname);
if( m < 0 )//Not loaded on this map-server instance.
return strchr(start,'\n');// skip and continue
- mob.m = (unsigned short)m;
+ mobspawn.m = (unsigned short)m;
- if( x < 0 || x >= map[mob.m].xs || y < 0 || y >= map[mob.m].ys )
- {
- ShowError("npc_parse_mob: Spawn coordinates out of range: %s (%d,%d), map size is (%d,%d) - %s %s (file '%s', line '%d').\n", map[mob.m].name, x, y, (map[mob.m].xs-1), (map[mob.m].ys-1), w1, w3, filepath, strline(buffer,start-buffer));
+ if( x < 0 || x >= map->list[mobspawn.m].xs || y < 0 || y >= map->list[mobspawn.m].ys ) {
+ ShowError("npc_parse_mob: Spawn coordinates out of range: %s (%d,%d), map size is (%d,%d) - %s %s in file '%s', line '%d'.\n", map->list[mobspawn.m].name, x, y, (map->list[mobspawn.m].xs-1), (map->list[mobspawn.m].ys-1), w1, w3, filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
return strchr(start,'\n');// skip and continue
}
// check monster ID if exists!
- if( mobdb_checkid(class_) == 0 )
- {
- ShowError("npc_parse_mob: Unknown mob ID %d (file '%s', line '%d').\n", class_, filepath, strline(buffer,start-buffer));
+ if( mob->db_checkid(class_) == 0 ) {
+ ShowError("npc_parse_mob: Unknown mob ID %d in file '%s', line '%d'.\n", class_, filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
return strchr(start,'\n');// skip and continue
}
- if( num < 1 || num > 1000 )
- {
- ShowError("npc_parse_mob: Invalid number of monsters %d, must be inside the range [1,1000] (file '%s', line '%d').\n", num, filepath, strline(buffer,start-buffer));
+ if( num < 1 || num > 1000 ) {
+ ShowError("npc_parse_mob: Invalid number of monsters %d, must be inside the range [1,1000] in file '%s', line '%d'.\n", num, filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
return strchr(start,'\n');// skip and continue
}
- if( (mob.state.size < 0 || mob.state.size > 2) && size != -1 )
- {
- ShowError("npc_parse_mob: Invalid size number %d for mob ID %d (file '%s', line '%d').\n", mob.state.size, class_, filepath, strline(buffer, start - buffer));
+ if( (mobspawn.state.size < 0 || mobspawn.state.size > 2) && size != -1 ) {
+ ShowError("npc_parse_mob: Invalid size number %d for mob ID %d in file '%s', line '%d'.\n", mobspawn.state.size, class_, filepath, strline(buffer, start - buffer));
+ if (retval) *retval = EXIT_FAILURE;
return strchr(start, '\n');
}
- if( (mob.state.ai < 0 || mob.state.ai > 4) && ai != -1 )
- {
- ShowError("npc_parse_mob: Invalid ai %d for mob ID %d (file '%s', line '%d').\n", mob.state.ai, class_, filepath, strline(buffer, start - buffer));
+ if( (mobspawn.state.ai < 0 || mobspawn.state.ai > 4) && ai != -1 ) {
+ ShowError("npc_parse_mob: Invalid ai %d for mob ID %d in file '%s', line '%d'.\n", mobspawn.state.ai, class_, filepath, strline(buffer, start - buffer));
+ if (retval) *retval = EXIT_FAILURE;
return strchr(start, '\n');
}
- if( (mob_lv == 0 || mob_lv > MAX_LEVEL) && mob_lv != -1 )
- {
- ShowError("npc_parse_mob: Invalid level %d for mob ID %d (file '%s', line '%d').\n", mob_lv, class_, filepath, strline(buffer, start - buffer));
+ if( (mob_lv == 0 || mob_lv > MAX_LEVEL) && mob_lv != -1 ) {
+ ShowError("npc_parse_mob: Invalid level %d for mob ID %d in file '%s', line '%d'.\n", mob_lv, class_, filepath, strline(buffer, start - buffer));
+ if (retval) *retval = EXIT_FAILURE;
return strchr(start, '\n');
}
- mob.num = (unsigned short)num;
- mob.active = 0;
- mob.class_ = (short) class_;
- mob.x = (unsigned short)x;
- mob.y = (unsigned short)y;
- mob.xs = (signed short)xs;
- mob.ys = (signed short)ys;
+ mobspawn.num = (unsigned short)num;
+ mobspawn.active = 0;
+ mobspawn.class_ = (short) class_;
+ mobspawn.x = (unsigned short)x;
+ mobspawn.y = (unsigned short)y;
+ mobspawn.xs = (signed short)xs;
+ mobspawn.ys = (signed short)ys;
if (mob_lv > 0 && mob_lv <= MAX_LEVEL)
- mob.level = mob_lv;
+ mobspawn.level = mob_lv;
if (size > 0 && size <= 2)
- mob.state.size = size;
+ mobspawn.state.size = size;
if (ai > 0 && ai <= 4)
- mob.state.ai = ai;
+ mobspawn.state.ai = ai;
- if (mob.num > 1 && battle_config.mob_count_rate != 100) {
- if ((mob.num = mob.num * battle_config.mob_count_rate / 100) < 1)
- mob.num = 1;
+ if (mobspawn.num > 1 && battle_config.mob_count_rate != 100) {
+ if ((mobspawn.num = mobspawn.num * battle_config.mob_count_rate / 100) < 1)
+ mobspawn.num = 1;
}
- if (battle_config.force_random_spawn || (mob.x == 0 && mob.y == 0))
- { //Force a random spawn anywhere on the map.
- mob.x = mob.y = 0;
- mob.xs = mob.ys = -1;
+ if (battle_config.force_random_spawn || (mobspawn.x == 0 && mobspawn.y == 0)) {
+ //Force a random spawn anywhere on the map.
+ mobspawn.x = mobspawn.y = 0;
+ mobspawn.xs = mobspawn.ys = -1;
}
- if(mob.delay1>0xfffffff || mob.delay2>0xfffffff) {
- ShowError("npc_parse_mob: Invalid spawn delays %u %u (file '%s', line '%d').\n", mob.delay1, mob.delay2, filepath, strline(buffer,start-buffer));
+ if(mobspawn.delay1>0xfffffff || mobspawn.delay2>0xfffffff) {
+ ShowError("npc_parse_mob: Invalid spawn delays %u %u in file '%s', line '%d'.\n", mobspawn.delay1, mobspawn.delay2, filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
return strchr(start,'\n');// skip and continue
}
//Use db names instead of the spawn file ones.
if(battle_config.override_mob_names==1)
- strcpy(mob.name,"--en--");
+ strcpy(mobspawn.name,"--en--");
else if (battle_config.override_mob_names==2)
- strcpy(mob.name,"--ja--");
+ strcpy(mobspawn.name,"--ja--");
else
- safestrncpy(mob.name, mobname, sizeof(mob.name));
+ safestrncpy(mobspawn.name, mobname, sizeof(mobspawn.name));
//Verify dataset.
- if( !mob_parse_dataset(&mob) )
- {
- ShowError("npc_parse_mob: Invalid dataset for monster ID %d (file '%s', line '%d').\n", class_, filepath, strline(buffer,start-buffer));
+ if( !mob->parse_dataset(&mobspawn) ) {
+ ShowError("npc_parse_mob: Invalid dataset for monster ID %d in file '%s', line '%d'.\n", class_, filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
return strchr(start,'\n');// skip and continue
}
//Update mob spawn lookup database
- db = mob_db(class_);
- for( i = 0; i < ARRAYLENGTH(db->spawn); ++i )
- {
- if (map[mob.m].index == db->spawn[i].mapindex)
- { //Update total
- db->spawn[i].qty += mob.num;
+ db = mob->db(class_);
+ for( i = 0; i < ARRAYLENGTH(db->spawn); ++i ) {
+ if (map_id2index(mobspawn.m) == db->spawn[i].mapindex) {
+ //Update total
+ db->spawn[i].qty += mobspawn.num;
//Re-sort list
for( j = i; j > 0 && db->spawn[j-1].qty < db->spawn[i].qty; --j );
- if( j != i )
- {
+ if( j != i ) {
xs = db->spawn[i].mapindex;
ys = db->spawn[i].qty;
memmove(&db->spawn[j+1], &db->spawn[j], (i-j)*sizeof(db->spawn[0]));
@@ -3095,33 +3563,33 @@ static const char* npc_parse_mob(char* w1, char* w2, char* w3, char* w4, const c
}
break;
}
- if (mob.num > db->spawn[i].qty)
- { //Insert into list
+ if (mobspawn.num > db->spawn[i].qty) {
+ //Insert into list
memmove(&db->spawn[i+1], &db->spawn[i], sizeof(db->spawn) -(i+1)*sizeof(db->spawn[0]));
- db->spawn[i].mapindex = map[mob.m].index;
- db->spawn[i].qty = mob.num;
+ db->spawn[i].mapindex = map_id2index(mobspawn.m);
+ db->spawn[i].qty = mobspawn.num;
break;
}
}
//Now that all has been validated. We allocate the actual memory that the re-spawn data will use.
data = (struct spawn_data*)aMalloc(sizeof(struct spawn_data));
- memcpy(data, &mob, sizeof(struct spawn_data));
+ memcpy(data, &mobspawn, sizeof(struct spawn_data));
// spawn / cache the new mobs
- if( battle_config.dynamic_mobs && iMap->addmobtolist(data->m, data) >= 0 ) {
+ if( battle_config.dynamic_mobs && map->addmobtolist(data->m, data) >= 0 ) {
data->state.dynamic = true;
npc_cache_mob += data->num;
// check if target map has players
// (usually shouldn't occur when map server is just starting,
// but not the case when we do @reloadscript
- if( map[data->m].users > 0 ) {
- npc_parse_mob2(data);
+ if( map->list[data->m].users > 0 ) {
+ npc->parse_mob2(data);
}
} else {
data->state.dynamic = false;
- npc_parse_mob2(data);
+ npc->parse_mob2(data);
npc_delay_mob += data->num;
}
@@ -3134,7 +3602,7 @@ static const char* npc_parse_mob(char* w1, char* w2, char* w3, char* w4, const c
* eg : bat_c01 mapflag battleground 2
* also chking if mapflag conflict with another
*------------------------------------------*/
-const char* npc_parse_mapflag(char* w1, char* w2, char* w3, char* w4, const char* start, const char* buffer, const char* filepath) {
+const char* npc_parse_mapflag(char* w1, char* w2, char* w3, char* w4, const char* start, const char* buffer, const char* filepath, int *retval) {
int16 m;
char mapname[32];
int state = 1;
@@ -3143,12 +3611,14 @@ const char* npc_parse_mapflag(char* w1, char* w2, char* w3, char* w4, const char
if( sscanf(w1, "%31[^,]", mapname) != 1 )
{
ShowError("npc_parse_mapflag: Invalid mapflag definition in file '%s', line '%d'.\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n", filepath, strline(buffer,start-buffer), w1, w2, w3, w4);
+ if (retval) *retval = EXIT_FAILURE;
return strchr(start,'\n');// skip and continue
}
- m = iMap->mapname2mapid(mapname);
- if( m < 0 )
- {
- ShowWarning("npc_parse_mapflag: Unknown map in file '%s', line '%d' : %s\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n", mapname, filepath, strline(buffer,start-buffer), w1, w2, w3, w4);
+ m = map->mapname2mapid(mapname);
+ if (m < 0) {
+ ShowWarning("npc_parse_mapflag: Unknown map in file '%s', line '%d': %s\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n",
+ filepath, strline(buffer,start-buffer), mapname, w1, w2, w3, w4);
+ if (retval) *retval = EXIT_FAILURE;
return strchr(start,'\n');// skip and continue
}
@@ -3160,77 +3630,80 @@ const char* npc_parse_mapflag(char* w1, char* w2, char* w3, char* w4, const char
int savex, savey;
if (state == 0)
; //Map flag disabled.
- else if (!strcmpi(w4, "SavePoint")) {
- map[m].save.map = 0;
- map[m].save.x = -1;
- map[m].save.y = -1;
+ else if (w4 && !strcmpi(w4, "SavePoint")) {
+ map->list[m].save.map = 0;
+ map->list[m].save.x = -1;
+ map->list[m].save.y = -1;
} else if (sscanf(w4, "%31[^,],%d,%d", savemap, &savex, &savey) == 3) {
- map[m].save.map = mapindex_name2id(savemap);
- map[m].save.x = savex;
- map[m].save.y = savey;
- if (!map[m].save.map) {
- ShowWarning("npc_parse_mapflag: Specified save point map '%s' for mapflag 'nosave' not found (file '%s', line '%d'), using 'SavePoint'.\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n", savemap, filepath, strline(buffer,start-buffer), w1, w2, w3, w4);
- map[m].save.x = -1;
- map[m].save.y = -1;
+ map->list[m].save.map = mapindex->name2id(savemap);
+ map->list[m].save.x = savex;
+ map->list[m].save.y = savey;
+ if (!map->list[m].save.map) {
+ ShowWarning("npc_parse_mapflag: Specified save point map '%s' for mapflag 'nosave' not found in file '%s', line '%d', using 'SavePoint'.\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n", savemap, filepath, strline(buffer,start-buffer), w1, w2, w3, w4);
+ if (retval) *retval = EXIT_FAILURE;
+ map->list[m].save.x = -1;
+ map->list[m].save.y = -1;
}
}
- map[m].flag.nosave = state;
+ map->list[m].flag.nosave = state;
}
else if (!strcmpi(w3,"autotrade"))
- map[m].flag.autotrade=state;
+ map->list[m].flag.autotrade=state;
else if (!strcmpi(w3,"allowks"))
- map[m].flag.allowks=state; // [Kill Steal Protection]
+ map->list[m].flag.allowks=state; // [Kill Steal Protection]
else if (!strcmpi(w3,"town"))
- map[m].flag.town=state;
+ map->list[m].flag.town=state;
else if (!strcmpi(w3,"nomemo"))
- map[m].flag.nomemo=state;
+ map->list[m].flag.nomemo=state;
else if (!strcmpi(w3,"noteleport"))
- map[m].flag.noteleport=state;
+ map->list[m].flag.noteleport=state;
else if (!strcmpi(w3,"nowarp"))
- map[m].flag.nowarp=state;
+ map->list[m].flag.nowarp=state;
else if (!strcmpi(w3,"nowarpto"))
- map[m].flag.nowarpto=state;
+ map->list[m].flag.nowarpto=state;
else if (!strcmpi(w3,"noreturn"))
- map[m].flag.noreturn=state;
+ map->list[m].flag.noreturn=state;
else if (!strcmpi(w3,"monster_noteleport"))
- map[m].flag.monster_noteleport=state;
+ map->list[m].flag.monster_noteleport=state;
else if (!strcmpi(w3,"nobranch"))
- map[m].flag.nobranch=state;
+ map->list[m].flag.nobranch=state;
else if (!strcmpi(w3,"nopenalty")) {
- map[m].flag.noexppenalty=state;
- map[m].flag.nozenypenalty=state;
+ map->list[m].flag.noexppenalty=state;
+ map->list[m].flag.nozenypenalty=state;
}
else if (!strcmpi(w3,"pvp")) {
struct map_zone_data *zone;
- map[m].flag.pvp = state;
- if( state && (map[m].flag.gvg || map[m].flag.gvg_dungeon || map[m].flag.gvg_castle) ) {
- map[m].flag.gvg = 0;
- map[m].flag.gvg_dungeon = 0;
- map[m].flag.gvg_castle = 0;
- ShowWarning("npc_parse_mapflag: You can't set PvP and GvG flags for the same map! Removing GvG flags from %s (file '%s', line '%d').\n", map[m].name, filepath, strline(buffer,start-buffer));
- }
- if( state && map[m].flag.battleground ) {
- map[m].flag.battleground = 0;
- ShowWarning("npc_parse_mapflag: You can't set PvP and BattleGround flags for the same map! Removing BattleGround flag from %s (file '%s', line '%d').\n", map[m].name, filepath, strline(buffer,start-buffer));
- }
- if( state && (zone = strdb_get(zone_db, MAP_ZONE_PVP_NAME)) && map[m].zone != zone ) {
- iMap->zone_change(m,zone,start,buffer,filepath);
+ map->list[m].flag.pvp = state;
+ if( state && (map->list[m].flag.gvg || map->list[m].flag.gvg_dungeon || map->list[m].flag.gvg_castle) ) {
+ map->list[m].flag.gvg = 0;
+ map->list[m].flag.gvg_dungeon = 0;
+ map->list[m].flag.gvg_castle = 0;
+ ShowWarning("npc_parse_mapflag: You can't set PvP and GvG flags for the same map! Removing GvG flags from %s in file '%s', line '%d'.\n", map->list[m].name, filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
+ }
+ if( state && map->list[m].flag.battleground ) {
+ map->list[m].flag.battleground = 0;
+ ShowWarning("npc_parse_mapflag: You can't set PvP and BattleGround flags for the same map! Removing BattleGround flag from %s in file '%s', line '%d'.\n", map->list[m].name, filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
+ }
+ if( state && (zone = strdb_get(map->zone_db, MAP_ZONE_PVP_NAME)) && map->list[m].zone != zone ) {
+ map->zone_change(m,zone,start,buffer,filepath);
} else if ( !state ) {
- map[m].zone = &map_zone_pk;
+ map->list[m].zone = &map->zone_pk;
}
}
else if (!strcmpi(w3,"pvp_noparty"))
- map[m].flag.pvp_noparty=state;
+ map->list[m].flag.pvp_noparty=state;
else if (!strcmpi(w3,"pvp_noguild"))
- map[m].flag.pvp_noguild=state;
+ map->list[m].flag.pvp_noguild=state;
else if (!strcmpi(w3, "pvp_nightmaredrop")) {
char drop_arg1[16], drop_arg2[16];
int drop_per = 0;
- if (sscanf(w4, "%[^,],%[^,],%d", drop_arg1, drop_arg2, &drop_per) == 3) {
+ if (sscanf(w4, "%15[^,],%15[^,],%d", drop_arg1, drop_arg2, &drop_per) == 3) {
int drop_id = 0, drop_type = 0;
if (!strcmpi(drop_arg1, "random"))
drop_id = -1;
- else if (itemdb_exists((drop_id = atoi(drop_arg1))) == NULL)
+ else if (itemdb->exists((drop_id = atoi(drop_arg1))) == NULL)
drop_id = 0;
if (!strcmpi(drop_arg2, "inventory"))
drop_type = 1;
@@ -3240,150 +3713,157 @@ const char* npc_parse_mapflag(char* w1, char* w2, char* w3, char* w4, const char
drop_type = 3;
if (drop_id != 0) {
- RECREATE(map[m].drop_list, struct map_drop_list, ++map[m].drop_list_count);
- map[m].drop_list[map[m].drop_list_count-1].drop_id = drop_id;
- map[m].drop_list[map[m].drop_list_count-1].drop_type = drop_type;
- map[m].drop_list[map[m].drop_list_count-1].drop_per = drop_per;
- map[m].flag.pvp_nightmaredrop = 1;
+ RECREATE(map->list[m].drop_list, struct map_drop_list, ++map->list[m].drop_list_count);
+ map->list[m].drop_list[map->list[m].drop_list_count-1].drop_id = drop_id;
+ map->list[m].drop_list[map->list[m].drop_list_count-1].drop_type = drop_type;
+ map->list[m].drop_list[map->list[m].drop_list_count-1].drop_per = drop_per;
+ map->list[m].flag.pvp_nightmaredrop = 1;
}
} else if (!state) //Disable
- map[m].flag.pvp_nightmaredrop = 0;
+ map->list[m].flag.pvp_nightmaredrop = 0;
}
else if (!strcmpi(w3,"pvp_nocalcrank"))
- map[m].flag.pvp_nocalcrank=state;
+ map->list[m].flag.pvp_nocalcrank=state;
else if (!strcmpi(w3,"gvg")) {
struct map_zone_data *zone;
- map[m].flag.gvg = state;
- if( state && map[m].flag.pvp ) {
- map[m].flag.pvp = 0;
- ShowWarning("npc_parse_mapflag: You can't set PvP and GvG flags for the same map! Removing PvP flag from %s (file '%s', line '%d').\n", map[m].name, filepath, strline(buffer,start-buffer));
+ map->list[m].flag.gvg = state;
+ if( state && map->list[m].flag.pvp ) {
+ map->list[m].flag.pvp = 0;
+ ShowWarning("npc_parse_mapflag: You can't set PvP and GvG flags for the same map! Removing PvP flag from %s in file '%s', line '%d'.\n", map->list[m].name, filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
}
- if( state && map[m].flag.battleground ) {
- map[m].flag.battleground = 0;
- ShowWarning("npc_parse_mapflag: You can't set GvG and BattleGround flags for the same map! Removing BattleGround flag from %s (file '%s', line '%d').\n", map[m].name, filepath, strline(buffer,start-buffer));
+ if( state && map->list[m].flag.battleground ) {
+ map->list[m].flag.battleground = 0;
+ ShowWarning("npc_parse_mapflag: You can't set GvG and BattleGround flags for the same map! Removing BattleGround flag from %s in file '%s', line '%d'.\n", map->list[m].name, filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
}
- if( state && (zone = strdb_get(zone_db, MAP_ZONE_GVG_NAME)) && map[m].zone != zone ) {
- iMap->zone_change(m,zone,start,buffer,filepath);
+ if( state && (zone = strdb_get(map->zone_db, MAP_ZONE_GVG_NAME)) && map->list[m].zone != zone ) {
+ map->zone_change(m,zone,start,buffer,filepath);
}
}
else if (!strcmpi(w3,"gvg_noparty"))
- map[m].flag.gvg_noparty=state;
+ map->list[m].flag.gvg_noparty=state;
else if (!strcmpi(w3,"gvg_dungeon")) {
- map[m].flag.gvg_dungeon=state;
- if (state) map[m].flag.pvp=0;
+ map->list[m].flag.gvg_dungeon=state;
+ if (state) map->list[m].flag.pvp=0;
}
else if (!strcmpi(w3,"gvg_castle")) {
- map[m].flag.gvg_castle=state;
- if (state) map[m].flag.pvp=0;
+ map->list[m].flag.gvg_castle=state;
+ if (state) map->list[m].flag.pvp=0;
}
else if (!strcmpi(w3,"battleground")) {
struct map_zone_data *zone;
if( state ) {
if( sscanf(w4, "%d", &state) == 1 )
- map[m].flag.battleground = state;
+ map->list[m].flag.battleground = state;
else
- map[m].flag.battleground = 1; // Default value
+ map->list[m].flag.battleground = 1; // Default value
} else
- map[m].flag.battleground = 0;
+ map->list[m].flag.battleground = 0;
- if( map[m].flag.battleground && map[m].flag.pvp ) {
- map[m].flag.pvp = 0;
- ShowWarning("npc_parse_mapflag: You can't set PvP and BattleGround flags for the same map! Removing PvP flag from %s (file '%s', line '%d').\n", map[m].name, filepath, strline(buffer,start-buffer));
+ if( map->list[m].flag.battleground && map->list[m].flag.pvp ) {
+ map->list[m].flag.pvp = 0;
+ ShowWarning("npc_parse_mapflag: You can't set PvP and BattleGround flags for the same map! Removing PvP flag from %s in file '%s', line '%d'.\n", map->list[m].name, filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
}
- if( map[m].flag.battleground && (map[m].flag.gvg || map[m].flag.gvg_dungeon || map[m].flag.gvg_castle) ) {
- map[m].flag.gvg = 0;
- map[m].flag.gvg_dungeon = 0;
- map[m].flag.gvg_castle = 0;
- ShowWarning("npc_parse_mapflag: You can't set GvG and BattleGround flags for the same map! Removing GvG flag from %s (file '%s', line '%d').\n", map[m].name, filepath, strline(buffer,start-buffer));
+ if( map->list[m].flag.battleground && (map->list[m].flag.gvg || map->list[m].flag.gvg_dungeon || map->list[m].flag.gvg_castle) ) {
+ map->list[m].flag.gvg = 0;
+ map->list[m].flag.gvg_dungeon = 0;
+ map->list[m].flag.gvg_castle = 0;
+ ShowWarning("npc_parse_mapflag: You can't set GvG and BattleGround flags for the same map! Removing GvG flag from %s in file '%s', line '%d'.\n", map->list[m].name, filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
}
- if( state && (zone = strdb_get(zone_db, MAP_ZONE_BG_NAME)) && map[m].zone != zone ) {
- iMap->zone_change(m,zone,start,buffer,filepath);
+ if( state && (zone = strdb_get(map->zone_db, MAP_ZONE_BG_NAME)) && map->list[m].zone != zone ) {
+ map->zone_change(m,zone,start,buffer,filepath);
}
}
else if (!strcmpi(w3,"noexppenalty"))
- map[m].flag.noexppenalty=state;
+ map->list[m].flag.noexppenalty=state;
else if (!strcmpi(w3,"nozenypenalty"))
- map[m].flag.nozenypenalty=state;
+ map->list[m].flag.nozenypenalty=state;
else if (!strcmpi(w3,"notrade"))
- map[m].flag.notrade=state;
+ map->list[m].flag.notrade=state;
else if (!strcmpi(w3,"novending"))
- map[m].flag.novending=state;
+ map->list[m].flag.novending=state;
else if (!strcmpi(w3,"nodrop"))
- map[m].flag.nodrop=state;
+ map->list[m].flag.nodrop=state;
else if (!strcmpi(w3,"noskill"))
- map[m].flag.noskill=state;
+ map->list[m].flag.noskill=state;
else if (!strcmpi(w3,"noicewall"))
- map[m].flag.noicewall=state;
+ map->list[m].flag.noicewall=state;
else if (!strcmpi(w3,"snow"))
- map[m].flag.snow=state;
+ map->list[m].flag.snow=state;
else if (!strcmpi(w3,"clouds"))
- map[m].flag.clouds=state;
+ map->list[m].flag.clouds=state;
else if (!strcmpi(w3,"clouds2"))
- map[m].flag.clouds2=state;
+ map->list[m].flag.clouds2=state;
else if (!strcmpi(w3,"fog"))
- map[m].flag.fog=state;
+ map->list[m].flag.fog=state;
else if (!strcmpi(w3,"fireworks"))
- map[m].flag.fireworks=state;
+ map->list[m].flag.fireworks=state;
else if (!strcmpi(w3,"sakura"))
- map[m].flag.sakura=state;
+ map->list[m].flag.sakura=state;
else if (!strcmpi(w3,"leaves"))
- map[m].flag.leaves=state;
+ map->list[m].flag.leaves=state;
else if (!strcmpi(w3,"nightenabled"))
- map[m].flag.nightenabled=state;
+ map->list[m].flag.nightenabled=state;
else if (!strcmpi(w3,"noexp")) {
- map[m].flag.nobaseexp=state;
- map[m].flag.nojobexp=state;
+ map->list[m].flag.nobaseexp=state;
+ map->list[m].flag.nojobexp=state;
}
else if (!strcmpi(w3,"nobaseexp"))
- map[m].flag.nobaseexp=state;
+ map->list[m].flag.nobaseexp=state;
else if (!strcmpi(w3,"nojobexp"))
- map[m].flag.nojobexp=state;
+ map->list[m].flag.nojobexp=state;
else if (!strcmpi(w3,"noloot")) {
- map[m].flag.nomobloot=state;
- map[m].flag.nomvploot=state;
+ map->list[m].flag.nomobloot=state;
+ map->list[m].flag.nomvploot=state;
}
else if (!strcmpi(w3,"nomobloot"))
- map[m].flag.nomobloot=state;
+ map->list[m].flag.nomobloot=state;
else if (!strcmpi(w3,"nomvploot"))
- map[m].flag.nomvploot=state;
+ map->list[m].flag.nomvploot=state;
else if (!strcmpi(w3,"nocommand")) {
if (state) {
if (sscanf(w4, "%d", &state) == 1)
- map[m].nocommand =state;
+ map->list[m].nocommand =state;
else //No level specified, block everyone.
- map[m].nocommand =100;
+ map->list[m].nocommand =100;
} else
- map[m].nocommand=0;
+ map->list[m].nocommand=0;
}
else if (!strcmpi(w3,"jexp")) {
- map[m].jexp = (state) ? atoi(w4) : 100;
- if( map[m].jexp < 0 ) map[m].jexp = 100;
- map[m].flag.nojobexp = (map[m].jexp==0)?1:0;
+ map->list[m].jexp = (state) ? atoi(w4) : 100;
+ if( map->list[m].jexp < 0 ) map->list[m].jexp = 100;
+ map->list[m].flag.nojobexp = (map->list[m].jexp==0)?1:0;
}
else if (!strcmpi(w3,"bexp")) {
- map[m].bexp = (state) ? atoi(w4) : 100;
- if( map[m].bexp < 0 ) map[m].bexp = 100;
- map[m].flag.nobaseexp = (map[m].bexp==0)?1:0;
+ map->list[m].bexp = (state) ? atoi(w4) : 100;
+ if( map->list[m].bexp < 0 ) map->list[m].bexp = 100;
+ map->list[m].flag.nobaseexp = (map->list[m].bexp==0)?1:0;
}
else if (!strcmpi(w3,"loadevent"))
- map[m].flag.loadevent=state;
+ map->list[m].flag.loadevent=state;
else if (!strcmpi(w3,"nochat"))
- map[m].flag.nochat=state;
+ map->list[m].flag.nochat=state;
else if (!strcmpi(w3,"partylock"))
- map[m].flag.partylock=state;
+ map->list[m].flag.partylock=state;
else if (!strcmpi(w3,"guildlock"))
- map[m].flag.guildlock=state;
+ map->list[m].flag.guildlock=state;
else if (!strcmpi(w3,"reset"))
- map[m].flag.reset=state;
+ map->list[m].flag.reset=state;
+ else if (!strcmpi(w3,"notomb"))
+ map->list[m].flag.notomb=state;
else if (!strcmpi(w3,"adjust_unit_duration")) {
int skill_id, k;
char skill_name[MAP_ZONE_MAPFLAG_LENGTH], modifier[MAP_ZONE_MAPFLAG_LENGTH];
- int len = strlen(w4);
+ size_t len = w4 ? strlen(w4) : 0;
modifier[0] = '\0';
- memcpy(skill_name, w4, MAP_ZONE_MAPFLAG_LENGTH);
+ if( w4 )
+ memcpy(skill_name, w4, MAP_ZONE_MAPFLAG_LENGTH);
for(k = 0; k < len; k++) {
if( skill_name[k] == '\t' ) {
@@ -3394,50 +3874,55 @@ const char* npc_parse_mapflag(char* w1, char* w2, char* w3, char* w4, const char
}
if( modifier[0] == '\0' ) {
- ShowWarning("npc_parse_mapflag: Missing 5th param for 'adjust_unit_duration' flag! removing flag from %s (file '%s', line '%d').\n", map[m].name, filepath, strline(buffer,start-buffer));
+ ShowWarning("npc_parse_mapflag: Missing 5th param for 'adjust_unit_duration' flag! removing flag from %s in file '%s', line '%d'.\n", map->list[m].name, filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
} else if( !( skill_id = skill->name2id(skill_name) ) || !skill->get_unit_id( skill->name2id(skill_name), 0) ) {
- ShowWarning("npc_parse_mapflag: Unknown skill (%s) for 'adjust_unit_duration' flag! removing flag from %s (file '%s', line '%d').\n",skill_name, map[m].name, filepath, strline(buffer,start-buffer));
+ ShowWarning("npc_parse_mapflag: Unknown skill (%s) for 'adjust_unit_duration' flag! removing flag from %s in file '%s', line '%d'.\n",skill_name, map->list[m].name, filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
} else if ( atoi(modifier) < 1 || atoi(modifier) > USHRT_MAX ) {
- ShowWarning("npc_parse_mapflag: Invalid modifier '%d' for skill '%s' for 'adjust_unit_duration' flag! removing flag from %s (file '%s', line '%d').\n", atoi(modifier), skill_name, map[m].name, filepath, strline(buffer,start-buffer));
+ ShowWarning("npc_parse_mapflag: Invalid modifier '%d' for skill '%s' for 'adjust_unit_duration' flag! removing flag from %s in file '%s', line '%d'.\n", atoi(modifier), skill_name, map->list[m].name, filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
} else {
- int idx = map[m].unit_count;
+ int idx = map->list[m].unit_count;
- ARR_FIND(0, idx, k, map[m].units[k]->skill_id == skill_id);
+ ARR_FIND(0, idx, k, map->list[m].units[k]->skill_id == skill_id);
if( k < idx ) {
if( atoi(modifier) != 100 )
- map[m].units[k]->modifier = (unsigned short)atoi(modifier);
+ map->list[m].units[k]->modifier = (unsigned short)atoi(modifier);
else { /* remove */
int cursor = 0;
- aFree(map[m].units[k]);
- map[m].units[k] = NULL;
+ aFree(map->list[m].units[k]);
+ map->list[m].units[k] = NULL;
for( k = 0; k < idx; k++ ) {
- if( map[m].units[k] == NULL )
+ if( map->list[m].units[k] == NULL )
continue;
- memmove(&map[m].units[cursor], &map[m].units[k], sizeof(struct mapflag_skill_adjust));
-
+ map->list[m].units[cursor] = map->list[m].units[k];
+
cursor++;
}
- if( !( map[m].unit_count = cursor ) ) {
- aFree(map[m].units);
- map[m].units = NULL;
+ if( !( map->list[m].unit_count = cursor ) ) {
+ aFree(map->list[m].units);
+ map->list[m].units = NULL;
}
}
} else if( atoi(modifier) != 100 ) {
- RECREATE(map[m].units, struct mapflag_skill_adjust*, ++map[m].unit_count);
- CREATE(map[m].units[idx],struct mapflag_skill_adjust,1);
- map[m].units[idx]->skill_id = (unsigned short)skill_id;
- map[m].units[idx]->modifier = (unsigned short)atoi(modifier);
+ RECREATE(map->list[m].units, struct mapflag_skill_adjust*, ++map->list[m].unit_count);
+ CREATE(map->list[m].units[idx],struct mapflag_skill_adjust,1);
+ map->list[m].units[idx]->skill_id = (unsigned short)skill_id;
+ map->list[m].units[idx]->modifier = (unsigned short)atoi(modifier);
}
}
} else if (!strcmpi(w3,"adjust_skill_damage")) {
int skill_id, k;
char skill_name[MAP_ZONE_MAPFLAG_LENGTH], modifier[MAP_ZONE_MAPFLAG_LENGTH];
- int len = strlen(w4);
+ size_t len = w4 ? strlen(w4) : 0;
modifier[0] = '\0';
- memcpy(skill_name, w4, MAP_ZONE_MAPFLAG_LENGTH);
+
+ if( w4 )
+ memcpy(skill_name, w4, MAP_ZONE_MAPFLAG_LENGTH);
for(k = 0; k < len; k++) {
if( skill_name[k] == '\t' ) {
@@ -3448,78 +3933,95 @@ const char* npc_parse_mapflag(char* w1, char* w2, char* w3, char* w4, const char
}
if( modifier[0] == '\0' ) {
- ShowWarning("npc_parse_mapflag: Missing 5th param for 'adjust_skill_damage' flag! removing flag from %s (file '%s', line '%d').\n", map[m].name, filepath, strline(buffer,start-buffer));
+ ShowWarning("npc_parse_mapflag: Missing 5th param for 'adjust_skill_damage' flag! removing flag from %s in file '%s', line '%d'.\n", map->list[m].name, filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
} else if( !( skill_id = skill->name2id(skill_name) ) ) {
- ShowWarning("npc_parse_mapflag: Unknown skill (%s) for 'adjust_skill_damage' flag! removing flag from %s (file '%s', line '%d').\n", skill_name, map[m].name, filepath, strline(buffer,start-buffer));
+ ShowWarning("npc_parse_mapflag: Unknown skill (%s) for 'adjust_skill_damage' flag! removing flag from %s in file '%s', line '%d'.\n", skill_name, map->list[m].name, filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
} else if ( atoi(modifier) < 1 || atoi(modifier) > USHRT_MAX ) {
- ShowWarning("npc_parse_mapflag: Invalid modifier '%d' for skill '%s' for 'adjust_skill_damage' flag! removing flag from %s (file '%s', line '%d').\n", atoi(modifier), skill_name, map[m].name, filepath, strline(buffer,start-buffer));
+ ShowWarning("npc_parse_mapflag: Invalid modifier '%d' for skill '%s' for 'adjust_skill_damage' flag! removing flag from %s in file '%s', line '%d'.\n", atoi(modifier), skill_name, map->list[m].name, filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
} else {
- int idx = map[m].skill_count;
-
- ARR_FIND(0, idx, k, map[m].skills[k]->skill_id == skill_id);
+ int idx = map->list[m].skill_count;
+
+ ARR_FIND(0, idx, k, map->list[m].skills[k]->skill_id == skill_id);
if( k < idx ) {
if( atoi(modifier) != 100 )
- map[m].skills[k]->modifier = (unsigned short)atoi(modifier);
+ map->list[m].skills[k]->modifier = (unsigned short)atoi(modifier);
else { /* remove */
int cursor = 0;
- aFree(map[m].skills[k]);
- map[m].skills[k] = NULL;
+ aFree(map->list[m].skills[k]);
+ map->list[m].skills[k] = NULL;
for( k = 0; k < idx; k++ ) {
- if( map[m].skills[k] == NULL )
+ if( map->list[m].skills[k] == NULL )
continue;
- memmove(&map[m].skills[cursor], &map[m].skills[k], sizeof(struct mapflag_skill_adjust));
-
+ map->list[m].skills[cursor] = map->list[m].skills[k];
+
cursor++;
}
- if( !( map[m].skill_count = cursor ) ) {
- aFree(map[m].skills);
- map[m].skills = NULL;
+ if( !( map->list[m].skill_count = cursor ) ) {
+ aFree(map->list[m].skills);
+ map->list[m].skills = NULL;
}
}
} else if( atoi(modifier) != 100 ) {
- RECREATE(map[m].skills, struct mapflag_skill_adjust*, ++map[m].skill_count);
- CREATE(map[m].skills[idx],struct mapflag_skill_adjust,1);
- map[m].skills[idx]->skill_id = (unsigned short)skill_id;
- map[m].skills[idx]->modifier = (unsigned short)atoi(modifier);
+ RECREATE(map->list[m].skills, struct mapflag_skill_adjust*, ++map->list[m].skill_count);
+ CREATE(map->list[m].skills[idx],struct mapflag_skill_adjust,1);
+ map->list[m].skills[idx]->skill_id = (unsigned short)skill_id;
+ map->list[m].skills[idx]->modifier = (unsigned short)atoi(modifier);
}
}
} else if (!strcmpi(w3,"zone")) {
struct map_zone_data *zone;
- if( !(zone = strdb_get(zone_db, w4)) ) {
- ShowWarning("npc_parse_mapflag: Invalid zone '%s'! removing flag from %s (file '%s', line '%d').\n", w4, map[m].name, filepath, strline(buffer,start-buffer));
- } else if( map[m].zone != zone ) {
- iMap->zone_change(m,zone,start,buffer,filepath);
+ if( !(zone = strdb_get(map->zone_db, w4)) ) {
+ ShowWarning("npc_parse_mapflag: Invalid zone '%s'! removing flag from %s in file '%s', line '%d'.\n", w4, map->list[m].name, filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
+ } else if( map->list[m].zone != zone ) {
+ map->zone_change(m,zone,start,buffer,filepath);
}
} else if ( !strcmpi(w3,"nomapchannelautojoin") ) {
- map[m].flag.chsysnolocalaj = state;
+ map->list[m].flag.chsysnolocalaj = state;
} else if ( !strcmpi(w3,"invincible_time_inc") ) {
- map[m].invincible_time_inc = (state) ? atoi(w4) : 0;
+ map->list[m].invincible_time_inc = (state) ? atoi(w4) : 0;
} else if ( !strcmpi(w3,"noknockback") ) {
- map[m].flag.noknockback = state;
+ map->list[m].flag.noknockback = state;
} else if ( !strcmpi(w3,"weapon_damage_rate") ) {
- map[m].weapon_damage_rate = (state) ? atoi(w4) : 100;
+ map->list[m].weapon_damage_rate = (state) ? atoi(w4) : 100;
} else if ( !strcmpi(w3,"magic_damage_rate") ) {
- map[m].magic_damage_rate = (state) ? atoi(w4) : 100;
+ map->list[m].magic_damage_rate = (state) ? atoi(w4) : 100;
} else if ( !strcmpi(w3,"misc_damage_rate") ) {
- map[m].misc_damage_rate = (state) ? atoi(w4) : 100;
+ map->list[m].misc_damage_rate = (state) ? atoi(w4) : 100;
} else if ( !strcmpi(w3,"short_damage_rate") ) {
- map[m].short_damage_rate = (state) ? atoi(w4) : 100;
+ map->list[m].short_damage_rate = (state) ? atoi(w4) : 100;
} else if ( !strcmpi(w3,"long_damage_rate") ) {
- map[m].long_damage_rate = (state) ? atoi(w4) : 100;
- } else
- ShowError("npc_parse_mapflag: unrecognized mapflag '%s' (file '%s', line '%d').\n", w3, filepath, strline(buffer,start-buffer));
+ map->list[m].long_damage_rate = (state) ? atoi(w4) : 100;
+ } else if ( !strcmpi(w3,"src4instance") ) {
+ map->list[m].flag.src4instance = (state) ? 1 : 0;
+ } else if ( !strcmpi(w3,"nocashshop") ) {
+ map->list[m].flag.nocashshop = (state) ? 1 : 0;
+ } else {
+ ShowError("npc_parse_mapflag: unrecognized mapflag '%s' in file '%s', line '%d'.\n", w3, filepath, strline(buffer,start-buffer));
+ if (retval) *retval = EXIT_FAILURE;
+ }
return strchr(start,'\n');// continue
}
-//Read file and create npc/func/mapflag/monster... accordingly.
-//@runOnInit should we exec OnInit when it's done ?
-void npc_parsesrcfile(const char* filepath, bool runOnInit)
-{
+/**
+ * Parses a script file and creates NPCs/functions/mapflags/monsters/etc
+ * accordingly.
+ *
+ * @param filepath File name and path.
+ * @param runOnInit Whether the OnInit label should be called.
+ * @retval EXIT_SUCCESS if filepath was loaded correctly.
+ * @retval EXIT_FAILURE if there were errors/warnings when loading filepath.
+ */
+int npc_parsesrcfile(const char* filepath, bool runOnInit) {
+ int success = EXIT_SUCCESS;
int16 m, x, y;
int lines = 0;
FILE* fp;
@@ -3529,10 +4031,9 @@ void npc_parsesrcfile(const char* filepath, bool runOnInit)
// read whole file to buffer
fp = fopen(filepath, "rb");
- if( fp == NULL )
- {
+ if( fp == NULL ) {
ShowError("npc_parsesrcfile: File not found '%s'.\n", filepath);
- return;
+ return EXIT_FAILURE;
}
fseek(fp, 0, SEEK_END);
len = ftell(fp);
@@ -3540,103 +4041,118 @@ void npc_parsesrcfile(const char* filepath, bool runOnInit)
fseek(fp, 0, SEEK_SET);
len = fread(buffer, sizeof(char), len, fp);
buffer[len] = '\0';
- if( ferror(fp) )
- {
+ if( ferror(fp) ) {
ShowError("npc_parsesrcfile: Failed to read file '%s' - %s\n", filepath, strerror(errno));
aFree(buffer);
fclose(fp);
- return;
+ return EXIT_FAILURE;
}
fclose(fp);
+ if ((unsigned char)buffer[0] == 0xEF && (unsigned char)buffer[1] == 0xBB && (unsigned char)buffer[2] == 0xBF) {
+ // UTF-8 BOM. This is most likely an error on the user's part, because:
+ // - BOM is discouraged in UTF-8, and the only place where you see it is Notepad and such.
+ // - It's unlikely that the user wants to use UTF-8 data here, since we don't really support it, nor does the client by default.
+ // - If the user really wants to use UTF-8 (instead of latin1, EUC-KR, SJIS, etc), then they can still do it <without BOM>.
+ // More info at http://unicode.org/faq/utf_bom.html#bom5 and http://en.wikipedia.org/wiki/Byte_order_mark#UTF-8
+ ShowError("npc_parsesrcfile: Detected unsupported UTF-8 BOM in file '%s'. Stopping (please consider using another character set.)\n", filepath);
+ aFree(buffer);
+ fclose(fp);
+ return EXIT_FAILURE;
+ }
+
// parse buffer
- for( p = skip_space(buffer); p && *p ; p = skip_space(p) )
- {
+ for( p = script->skip_space(buffer); p && *p ; p = script->skip_space(p) ) {
int pos[9];
char w1[2048], w2[2048], w3[2048], w4[2048];
int i, count;
lines++;
// w1<TAB>w2<TAB>w3<TAB>w4
- count = sv->parse(p, len+buffer-p, 0, '\t', pos, ARRAYLENGTH(pos), (e_svopt)(SV_TERMINATE_LF|SV_TERMINATE_CRLF));
+ count = sv->parse(p, (int)(len+buffer-p), 0, '\t', pos, ARRAYLENGTH(pos), (e_svopt)(SV_TERMINATE_LF|SV_TERMINATE_CRLF));
if( count < 0 )
{
ShowError("npc_parsesrcfile: Parse error in file '%s', line '%d'. Stopping...\n", filepath, strline(buffer,p-buffer));
+ success = EXIT_FAILURE;
break;
}
// fill w1
- if( pos[3]-pos[2] > ARRAYLENGTH(w1)-1 )
+ if( pos[3]-pos[2] > ARRAYLENGTH(w1)-1 ) {
ShowWarning("npc_parsesrcfile: w1 truncated, too much data (%d) in file '%s', line '%d'.\n", pos[3]-pos[2], filepath, strline(buffer,p-buffer));
+ success = EXIT_FAILURE;
+ }
i = min(pos[3]-pos[2], ARRAYLENGTH(w1)-1);
memcpy(w1, p+pos[2], i*sizeof(char));
w1[i] = '\0';
// fill w2
- if( pos[5]-pos[4] > ARRAYLENGTH(w2)-1 )
+ if( pos[5]-pos[4] > ARRAYLENGTH(w2)-1 ) {
ShowWarning("npc_parsesrcfile: w2 truncated, too much data (%d) in file '%s', line '%d'.\n", pos[5]-pos[4], filepath, strline(buffer,p-buffer));
+ success = EXIT_FAILURE;
+ }
i = min(pos[5]-pos[4], ARRAYLENGTH(w2)-1);
memcpy(w2, p+pos[4], i*sizeof(char));
w2[i] = '\0';
// fill w3
- if( pos[7]-pos[6] > ARRAYLENGTH(w3)-1 )
+ if( pos[7]-pos[6] > ARRAYLENGTH(w3)-1 ) {
ShowWarning("npc_parsesrcfile: w3 truncated, too much data (%d) in file '%s', line '%d'.\n", pos[7]-pos[6], filepath, strline(buffer,p-buffer));
+ success = EXIT_FAILURE;
+ }
i = min(pos[7]-pos[6], ARRAYLENGTH(w3)-1);
memcpy(w3, p+pos[6], i*sizeof(char));
w3[i] = '\0';
// fill w4 (to end of line)
- if( pos[1]-pos[8] > ARRAYLENGTH(w4)-1 )
+ if( pos[1]-pos[8] > ARRAYLENGTH(w4)-1 ) {
ShowWarning("npc_parsesrcfile: w4 truncated, too much data (%d) in file '%s', line '%d'.\n", pos[1]-pos[8], filepath, strline(buffer,p-buffer));
- if( pos[8] != -1 )
- {
+ success = EXIT_FAILURE;
+ }
+ if( pos[8] != -1 ) {
i = min(pos[1]-pos[8], ARRAYLENGTH(w4)-1);
memcpy(w4, p+pos[8], i*sizeof(char));
w4[i] = '\0';
- }
- else
+ } else {
w4[0] = '\0';
+ }
- if( count < 3 )
- {// Unknown syntax
+ if( count < 3 ) {
+ // Unknown syntax
ShowError("npc_parsesrcfile: Unknown syntax in file '%s', line '%d'. Stopping...\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n", filepath, strline(buffer,p-buffer), w1, w2, w3, w4);
+ success = EXIT_FAILURE;
break;
}
- if( strcmp(w1,"-") !=0 && strcasecmp(w1,"function") != 0 )
+ if( strcmp(w1,"-") != 0 && strcmp(w1,"function") != 0 )
{// w1 = <map name>,<x>,<y>,<facing>
char mapname[MAP_NAME_LENGTH*2];
x = y = 0;
sscanf(w1,"%23[^,],%hd,%hd[^,]",mapname,&x,&y);
- if( !mapindex_name2id(mapname) )
- {// Incorrect map, we must skip the script info...
+ if( !mapindex->name2id(mapname) ) {
+ // Incorrect map, we must skip the script info...
ShowError("npc_parsesrcfile: Unknown map '%s' in file '%s', line '%d'. Skipping line...\n", mapname, filepath, strline(buffer,p-buffer));
- if( strcasecmp(w2,"script") == 0 && count > 3 )
- {
- if((p = npc_skip_script(p,buffer,filepath)) == NULL)
- {
+ success = EXIT_FAILURE;
+ if( strcmp(w2,"script") == 0 && count > 3 ) {
+ if((p = npc->skip_script(p,buffer,filepath, &success)) == NULL) {
break;
}
}
p = strchr(p,'\n');// next line
continue;
}
- m = iMap->mapname2mapid(mapname);
- if( m < 0 )
- {// "mapname" is not assigned to this server, we must skip the script info...
- if( strcasecmp(w2,"script") == 0 && count > 3 )
- {
- if((p = npc_skip_script(p,buffer,filepath)) == NULL)
- {
+ m = map->mapname2mapid(mapname);
+ if( m < 0 ) {
+ // "mapname" is not assigned to this server, we must skip the script info...
+ if( strcmp(w2,"script") == 0 && count > 3 ) {
+ if((p = npc->skip_script(p,buffer,filepath, &success)) == NULL) {
break;
}
}
p = strchr(p,'\n');// next line
continue;
}
- if (x < 0 || x >= map[m].xs || y < 0 || y >= map[m].ys) {
+ if (x < 0 || x >= map->list[m].xs || y < 0 || y >= map->list[m].ys) {
ShowError("npc_parsesrcfile: Unknown coordinates ('%d', '%d') for map '%s' in file '%s', line '%d'. Skipping line...\n", x, y, mapname, filepath, strline(buffer,p-buffer));
- if( strcasecmp(w2,"script") == 0 && count > 3 )
- {
- if((p = npc_skip_script(p,buffer,filepath)) == NULL)
- {
+ success = EXIT_FAILURE;
+ if( strcmp(w2,"script") == 0 && count > 3 ) {
+ if((p = npc->skip_script(p,buffer,filepath, &success)) == NULL) {
break;
}
}
@@ -3645,42 +4161,71 @@ void npc_parsesrcfile(const char* filepath, bool runOnInit)
}
}
- if( strcasecmp(w2,"warp") == 0 && count > 3 )
+ if( strcmp(w2,"mapflag") == 0 && count >= 3 )
{
- p = npc_parse_warp(w1,w2,w3,w4, p, buffer, filepath);
+ p = npc->parse_mapflag(w1, w2, trim(w3), trim(w4), p, buffer, filepath, &success);
}
- else if( (!strcasecmp(w2,"shop") || !strcasecmp(w2,"cashshop")) && count > 3 )
+ else if( count == 3 ) {
+ ShowError("npc_parsesrcfile: Unable to parse, probably a missing TAB in file '%s', line '%d'. Skipping line...\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n", filepath, strline(buffer,p-buffer), w1, w2, w3, w4);
+ p = strchr(p,'\n');// skip and continue
+ success = EXIT_FAILURE;
+ }
+ else if( strcmp(w2,"script") == 0 )
{
- p = npc_parse_shop(w1,w2,w3,w4, p, buffer, filepath);
+ if( strcmp(w1,"function") == 0 ) {
+ p = npc->parse_function(w1, w2, w3, w4, p, buffer, filepath, &success);
+ } else {
+#ifdef ENABLE_CASE_CHECK
+ if( strcasecmp(w1, "function") == 0 ) DeprecationWarning("npc_parsesrcfile", w1, "function", filepath, strline(buffer, p-buffer)); // TODO
+#endif // ENABLE_CASE_CHECK
+ p = npc->parse_script(w1,w2,w3,w4, p, buffer, filepath,runOnInit?NPO_ONINIT:NPO_NONE, &success);
+ }
}
- else if( strcasecmp(w2,"script") == 0 && count > 3 )
+ else if( strcmp(w2,"trader") == 0 ) {
+ p = npc->parse_script(w1,w2,w3,w4, p, buffer, filepath,(runOnInit?NPO_ONINIT:NPO_NONE)|NPO_TRADER, &success);
+ }
+ else if( strcmp(w2,"warp") == 0 )
{
- if( strcasecmp(w1,"function") == 0 )
- p = npc_parse_function(w1, w2, w3, w4, p, buffer, filepath);
- else
- p = npc_parse_script(w1,w2,w3,w4, p, buffer, filepath,runOnInit);
+ p = npc->parse_warp(w1,w2,w3,w4, p, buffer, filepath, &success);
}
- else if( (i=0, sscanf(w2,"duplicate%n",&i), (i > 0 && w2[i] == '(')) && count > 3 )
+ else if( (i=0, sscanf(w2,"duplicate%n",&i), (i > 0 && w2[i] == '(')) )
{
- p = npc_parse_duplicate(w1,w2,w3,w4, p, buffer, filepath);
+ p = npc->parse_duplicate(w1,w2,w3,w4, p, buffer, filepath, (runOnInit?NPO_ONINIT:NPO_NONE), &success);
}
- else if( (strcmpi(w2,"monster") == 0 || strcmpi(w2,"boss_monster") == 0) && count > 3 )
+ else if( (strcmp(w2,"monster") == 0 || strcmp(w2,"boss_monster") == 0) )
{
- p = npc_parse_mob(w1, w2, w3, w4, p, buffer, filepath);
+ p = npc->parse_mob(w1, w2, w3, w4, p, buffer, filepath, &success);
}
- else if( strcmpi(w2,"mapflag") == 0 && count >= 3 )
+ else if( (strcmp(w2,"shop") == 0 || strcmp(w2,"cashshop") == 0) )
{
- p = npc_parse_mapflag(w1, w2, trim(w3), trim(w4), p, buffer, filepath);
+ p = npc->parse_shop(w1,w2,w3,w4, p, buffer, filepath, &success);
}
else
{
+#ifdef ENABLE_CASE_CHECK
+ if( strcasecmp(w2, "warp") == 0 ) { DeprecationWarning("npc_parsesrcfile", w2, "warp", filepath, strline(buffer, p-buffer)); } // TODO
+ else if( strcasecmp(w2,"shop") == 0 ) { DeprecationWarning("npc_parsesrcfile", w2, "shop", filepath, strline(buffer, p-buffer)); } // TODO
+ else if( strcasecmp(w2,"cashshop") == 0 ) { DeprecationWarning("npc_parsesrcfile", w2, "cashshop", filepath, strline(buffer, p-buffer)); } // TODO
+ else if( strcasecmp(w2, "script") == 0 ) { DeprecationWarning("npc_parsesrcfile", w2, "script", filepath, strline(buffer, p-buffer)); } // TODO
+ else if( strcasecmp(w2,"trader") == 0 ) DeprecationWarning("npc_parsesrcfile", w2, "trader", filepath, strline(buffer, p-buffer)) // TODO
+ else if( strncasecmp(w2, "duplicate", 9) == 0 ) {
+ char temp[10];
+ safestrncpy(temp, w2, 10);
+ DeprecationWarning("npc_parsesrcfile", temp, "duplicate", filepath, strline(buffer, p-buffer)); // TODO
+ }
+ else if( strcasecmp(w2,"monster") == 0 ) { DeprecationWarning("npc_parsesrcfile", w2, "monster", filepath, strline(buffer, p-buffer)); } // TODO:
+ else if( strcasecmp(w2,"boss_monster") == 0 ) { DeprecationWarning("npc_parsesrcfile", w2, "boss_monster", filepath, strline(buffer, p-buffer)); } // TODO
+ else if( strcasecmp(w2, "mapflag") == 0 ) { DeprecationWarning("npc_parsesrcfile", w2, "mapflag", filepath, strline(buffer, p-buffer)); } // TODO
+ else
+#endif // ENABLE_CASE_CHECK
ShowError("npc_parsesrcfile: Unable to parse, probably a missing or extra TAB in file '%s', line '%d'. Skipping line...\n * w1=%s\n * w2=%s\n * w3=%s\n * w4=%s\n", filepath, strline(buffer,p-buffer), w1, w2, w3, w4);
p = strchr(p,'\n');// skip and continue
+ success = EXIT_FAILURE;
}
}
aFree(buffer);
- return;
+ return success;
}
int npc_script_event(struct map_session_data* sd, enum npce_event type)
@@ -3693,7 +4238,7 @@ int npc_script_event(struct map_session_data* sd, enum npce_event type)
return 0;
}
for (i = 0; i<script_event[type].event_count; i++)
- npc_event_sub(sd,script_event[type].event[i],script_event[type].event_name[i]);
+ npc->event_sub(sd,script_event[type].event[i],script_event[type].event_name[i]);
return i;
}
@@ -3704,14 +4249,14 @@ void npc_read_event_script(void)
char *name;
const char *event_name;
} config[] = {
- {"Login Event",script_config.login_event_name},
- {"Logout Event",script_config.logout_event_name},
- {"Load Map Event",script_config.loadmap_event_name},
- {"Base LV Up Event",script_config.baselvup_event_name},
- {"Job LV Up Event",script_config.joblvup_event_name},
- {"Die Event",script_config.die_event_name},
- {"Kill PC Event",script_config.kill_pc_event_name},
- {"Kill NPC Event",script_config.kill_mob_event_name},
+ {"Login Event",script->config.login_event_name},
+ {"Logout Event",script->config.logout_event_name},
+ {"Load Map Event",script->config.loadmap_event_name},
+ {"Base LV Up Event",script->config.baselvup_event_name},
+ {"Job LV Up Event",script->config.joblvup_event_name},
+ {"Die Event",script->config.die_event_name},
+ {"Kill PC Event",script->config.kill_pc_event_name},
+ {"Kill NPC Event",script->config.kill_mob_event_name},
};
for (i = 0; i < NPCE_MAX; i++)
@@ -3724,7 +4269,7 @@ void npc_read_event_script(void)
safestrncpy(name+2,config[i].event_name,62);
script_event[i].event_count = 0;
- iter = db_iterator(ev_db);
+ iter = db_iterator(npc->ev_db);
for( data = iter->first(iter,&key); iter->exists(iter); data = iter->next(iter,&key) )
{
const char* p = key.str;
@@ -3737,11 +4282,15 @@ void npc_read_event_script(void)
break;
}
- if( (p=strchr(p,':')) && p && strcmpi(name,p)==0 )
+ if( (p=strchr(p,':')) && strcmp(name,p) == 0 )
{
script_event[i].event[count] = ed;
script_event[i].event_name[count] = key.str;
script_event[i].event_count++;
+#ifdef ENABLE_CASE_CHECK
+ } else if( p && strcasecmp(name, p) == 0 ) {
+ DeprecationWarning2("npc_read_event_script", p, name, config[i].event_name); // TODO
+#endif // ENABLE_CASE_CHECK
}
}
dbi_destroy(iter);
@@ -3754,211 +4303,236 @@ void npc_read_event_script(void)
}
}
-void npc_clear_pathlist(void) {
- struct npc_path_data *npd = NULL;
- DBIterator *path_list = db_iterator(npc_path_db);
+/**
+ * @see DBApply
+ */
+int npc_path_db_clear_sub(DBKey key, DBData *data, va_list args)
+{
+ struct npc_path_data *npd = DB->data2ptr(data);
+ if (npd->path)
+ aFree(npd->path);
+ return 0;
+}
- /* free all npc_path_data filepaths */
- for( npd = dbi_first(path_list); dbi_exists(path_list); npd = dbi_next(path_list) ) {
- if( npd->path )
- aFree(npd->path);
- }
+/**
+ * @see DBApply
+ */
+int npc_ev_label_db_clear_sub(DBKey key, DBData *data, va_list args)
+{
+ struct linkdb_node **label_linkdb = DB->data2ptr(data);
+ linkdb_final(label_linkdb); // linked data (struct event_data*) is freed when clearing ev_db
+ return 0;
+}
- dbi_destroy(path_list);
+/**
+ * Main npc file processing
+ * @param npc_min Minimum npc id - used to know how many NPCs were loaded
+ **/
+void npc_process_files( int npc_min ) {
+ struct npc_src_list *file; // Current file
+
+ ShowStatus("Loading NPCs...\r");
+ for( file = npc->src_files; file != NULL; file = file->next ) {
+ ShowStatus("Loading NPC file: %s"CL_CLL"\r", file->name);
+ if (npc->parsesrcfile(file->name, false) != EXIT_SUCCESS)
+ map->retval = EXIT_FAILURE;
+ }
+ ShowInfo ("Done loading '"CL_WHITE"%d"CL_RESET"' NPCs:"CL_CLL"\n"
+ "\t-'"CL_WHITE"%d"CL_RESET"' Warps\n"
+ "\t-'"CL_WHITE"%d"CL_RESET"' Shops\n"
+ "\t-'"CL_WHITE"%d"CL_RESET"' Scripts\n"
+ "\t-'"CL_WHITE"%d"CL_RESET"' Spawn sets\n"
+ "\t-'"CL_WHITE"%d"CL_RESET"' Mobs Cached\n"
+ "\t-'"CL_WHITE"%d"CL_RESET"' Mobs Not Cached\n",
+ npc_id - npc_min, npc_warp, npc_shop, npc_script, npc_mob, npc_cache_mob, npc_delay_mob);
}
//Clear then reload npcs files
int npc_reload(void) {
- struct npc_src_list *nsl;
int16 m, i;
int npc_new_min = npc_id;
struct s_mapiterator* iter;
struct block_list* bl;
+ if (map->retval == EXIT_FAILURE)
+ map->retval = EXIT_SUCCESS; // Clear return status in case something failed before.
+
/* clear guild flag cache */
guild->flags_clear();
- npc_clear_pathlist();
+ npc->path_db->clear(npc->path_db, npc->path_db_clear_sub);
- db_clear(npc_path_db);
-
- db_clear(npcname_db);
- db_clear(ev_db);
+ db_clear(npc->name_db);
+ db_clear(npc->ev_db);
+ npc->ev_label_db->clear(npc->ev_label_db, npc->ev_label_db_clear_sub);
+ npc_last_npd = NULL;
+ npc_last_path = NULL;
+ npc_last_ref = NULL;
+
//Remove all npcs/mobs. [Skotlex]
iter = mapit_geteachiddb();
for( bl = (struct block_list*)mapit->first(iter); mapit->exists(iter); bl = (struct block_list*)mapit->next(iter) ) {
switch(bl->type) {
- case BL_NPC:
- if( bl->id != fake_nd->bl.id )// don't remove fake_nd
- npc_unload((struct npc_data *)bl, false);
- break;
- case BL_MOB:
- unit_free(bl,CLR_OUTSIGHT);
- break;
+ case BL_NPC:
+ if( bl->id != npc->fake_nd->bl.id )// don't remove fake_nd
+ npc->unload((struct npc_data *)bl, false);
+ break;
+ case BL_MOB:
+ unit->free(bl,CLR_OUTSIGHT);
+ break;
}
}
mapit->free(iter);
- if(battle_config.dynamic_mobs)
- {// dynamic check by [random]
- for (m = 0; m < iMap->map_num; m++) {
+ if(battle_config.dynamic_mobs) {// dynamic check by [random]
+ for (m = 0; m < map->count; m++) {
for (i = 0; i < MAX_MOB_LIST_PER_MAP; i++) {
- if (map[m].moblist[i] != NULL) {
- aFree(map[m].moblist[i]);
- map[m].moblist[i] = NULL;
+ if (map->list[m].moblist[i] != NULL) {
+ aFree(map->list[m].moblist[i]);
+ map->list[m].moblist[i] = NULL;
}
- if( map[m].mob_delete_timer != INVALID_TIMER )
+ if( map->list[m].mob_delete_timer != INVALID_TIMER )
{ // Mobs were removed anyway,so delete the timer [Inkfish]
- iTimer->delete_timer(map[m].mob_delete_timer, iMap->removemobs_timer);
- map[m].mob_delete_timer = INVALID_TIMER;
+ timer->delete(map->list[m].mob_delete_timer, map->removemobs_timer);
+ map->list[m].mob_delete_timer = INVALID_TIMER;
}
}
+ if (map->list[m].npc_num > 0)
+ ShowWarning("npc_reload: %d npcs weren't removed at map %s!\n", map->list[m].npc_num, map->list[m].name);
}
- if (map[m].npc_num > 0)
- ShowWarning("npc_reload: %d npcs weren't removed at map %s!\n", map[m].npc_num, map[m].name);
}
// clear mob spawn lookup index
- mob_clear_spawninfo();
+ mob->clear_spawninfo();
npc_warp = npc_shop = npc_script = 0;
npc_mob = npc_cache_mob = npc_delay_mob = 0;
// reset mapflags
- iMap->flags_init();
+ map->flags_init();
- //TODO: the following code is copy-pasted from do_init_npc(); clean it up
- // Reloading npcs now
- for (nsl = npc_src_files; nsl; nsl = nsl->next) {
- ShowStatus("Loading NPC file: %s"CL_CLL"\r", nsl->name);
- npc_parsesrcfile(nsl->name,false);
- }
- ShowInfo ("Done loading '"CL_WHITE"%d"CL_RESET"' NPCs:"CL_CLL"\n"
- "\t-'"CL_WHITE"%d"CL_RESET"' Warps\n"
- "\t-'"CL_WHITE"%d"CL_RESET"' Shops\n"
- "\t-'"CL_WHITE"%d"CL_RESET"' Scripts\n"
- "\t-'"CL_WHITE"%d"CL_RESET"' Spawn sets\n"
- "\t-'"CL_WHITE"%d"CL_RESET"' Mobs Cached\n"
- "\t-'"CL_WHITE"%d"CL_RESET"' Mobs Not Cached\n",
- npc_id - npc_new_min, npc_warp, npc_shop, npc_script, npc_mob, npc_cache_mob, npc_delay_mob);
-
- instance->final();
+ // Reprocess npc files and reload constants
+ itemdb->name_constants();
+ npc_process_files( npc_new_min );
- iMap->zone_init();
-
- npc->motd = npc_name2id("HerculesMOTD"); /* [Ind/Hercules] */
-
- //Re-read the NPC Script Events cache.
- npc_read_event_script();
+ instance->reload();
+
+ map->zone_init();
- /* refresh guild castle flags on both woe setups */
- npc_event_doall("OnAgitInit");
- npc_event_doall("OnAgitInit2");
+ npc->motd = npc->name2id("HerculesMOTD"); /* [Ind/Hercules] */
- //Execute the OnInit event for freshly loaded npcs. [Skotlex]
- ShowStatus("Event '"CL_WHITE"OnInit"CL_RESET"' executed with '"CL_WHITE"%d"CL_RESET"' NPCs.\n",npc_event_doall("OnInit"));
+ //Re-read the NPC Script Events cache.
+ npc->read_event_script();
+ // Execute main initialisation events
+ // The correct initialisation order is:
+ // OnInit -> OnInterIfInit -> OnInterIfInitOnce -> OnAgitInit -> OnAgitInit2
+ npc->event_do_oninit( true );
+ npc->market_fromsql();
// Execute rest of the startup events if connected to char-server. [Lance]
- if(!CheckForCharServer()){
- ShowStatus("Event '"CL_WHITE"OnInterIfInit"CL_RESET"' executed with '"CL_WHITE"%d"CL_RESET"' NPCs.\n", npc_event_doall("OnInterIfInit"));
- ShowStatus("Event '"CL_WHITE"OnInterIfInitOnce"CL_RESET"' executed with '"CL_WHITE"%d"CL_RESET"' NPCs.\n", npc_event_doall("OnInterIfInitOnce"));
+ // Executed when connection is established with char-server in chrif_connectack
+ if( !intif->CheckForCharServer() ) {
+ ShowStatus("Event '"CL_WHITE"OnInterIfInit"CL_RESET"' executed with '"CL_WHITE"%d"CL_RESET"' NPCs.\n", npc->event_doall("OnInterIfInit"));
+ ShowStatus("Event '"CL_WHITE"OnInterIfInitOnce"CL_RESET"' executed with '"CL_WHITE"%d"CL_RESET"' NPCs.\n", npc->event_doall("OnInterIfInitOnce"));
}
+ // Refresh guild castle flags on both woe setups
+ // These events are only executed after receiving castle information from char-server
+ npc->event_doall("OnAgitInit");
+ npc->event_doall("OnAgitInit2");
+
return 0;
}
//Unload all npc in the given file
-bool npc_unloadfile( const char* path ) {
- DBIterator * iter = db_iterator(npcname_db);
+bool npc_unloadfile( const char* filepath ) {
+ DBIterator * iter = db_iterator(npc->name_db);
struct npc_data* nd = NULL;
bool found = false;
for( nd = dbi_first(iter); dbi_exists(iter); nd = dbi_next(iter) ) {
- if( nd->path && strcasecmp(nd->path,path) == 0 ) {
+ if( nd->path && strcasecmp(nd->path,filepath) == 0 ) { // FIXME: This can break in case-sensitive file systems
found = true;
- npc_unload_duplicates(nd);/* unload any npcs which could duplicate this but be in a different file */
- npc_unload(nd, true);
+ npc->unload_duplicates(nd);/* unload any npcs which could duplicate this but be in a different file */
+ npc->unload(nd, true);
}
}
dbi_destroy(iter);
if( found ) /* refresh event cache */
- npc_read_event_script();
+ npc->read_event_script();
return found;
}
void do_clear_npc(void) {
- db_clear(npcname_db);
- db_clear(ev_db);
+ db_clear(npc->name_db);
+ db_clear(npc->ev_db);
+ npc->ev_label_db->clear(npc->ev_label_db, npc->ev_label_db_clear_sub);
}
/*==========================================
* Destructor
*------------------------------------------*/
int do_final_npc(void) {
- npc_clear_pathlist();
- ev_db->destroy(ev_db, NULL);
- npcname_db->destroy(npcname_db, NULL);
- npc_path_db->destroy(npc_path_db, NULL);
- ers_destroy(timer_event_ers);
- npc_clearsrcfile();
+ db_destroy(npc->ev_db);
+ npc->ev_label_db->destroy(npc->ev_label_db, npc->ev_label_db_clear_sub);
+ db_destroy(npc->name_db);
+ npc->path_db->destroy(npc->path_db, npc->path_db_clear_sub);
+ ers_destroy(npc->timer_event_ers);
+ npc->clearsrcfile();
return 0;
}
-static void npc_debug_warps_sub(struct npc_data* nd)
-{
+void npc_debug_warps_sub(struct npc_data* nd) {
int16 m;
if (nd->bl.type != BL_NPC || nd->subtype != WARP || nd->bl.m < 0)
return;
- m = iMap->mapindex2mapid(nd->u.warp.mapindex);
+ m = map->mapindex2mapid(nd->u.warp.mapindex);
if (m < 0) return; //Warps to another map, nothing to do about it.
if (nd->u.warp.x == 0 && nd->u.warp.y == 0) return; // random warp
- if (iMap->getcell(m, nd->u.warp.x, nd->u.warp.y, CELL_CHKNPC)) {
+ if (map->getcell(m, nd->u.warp.x, nd->u.warp.y, CELL_CHKNPC)) {
ShowWarning("Warp %s at %s(%d,%d) warps directly on top of an area npc at %s(%d,%d)\n",
nd->name,
- map[nd->bl.m].name, nd->bl.x, nd->bl.y,
- map[m].name, nd->u.warp.x, nd->u.warp.y
+ map->list[nd->bl.m].name, nd->bl.x, nd->bl.y,
+ map->list[m].name, nd->u.warp.x, nd->u.warp.y
);
}
- if (iMap->getcell(m, nd->u.warp.x, nd->u.warp.y, CELL_CHKNOPASS)) {
+ if (map->getcell(m, nd->u.warp.x, nd->u.warp.y, CELL_CHKNOPASS)) {
ShowWarning("Warp %s at %s(%d,%d) warps to a non-walkable tile at %s(%d,%d)\n",
nd->name,
- map[nd->bl.m].name, nd->bl.x, nd->bl.y,
- map[m].name, nd->u.warp.x, nd->u.warp.y
+ map->list[nd->bl.m].name, nd->bl.x, nd->bl.y,
+ map->list[m].name, nd->u.warp.x, nd->u.warp.y
);
}
}
-static void npc_debug_warps(void)
-{
+static void npc_debug_warps(void) {
int16 m, i;
- for (m = 0; m < iMap->map_num; m++)
- for (i = 0; i < map[m].npc_num; i++)
- npc_debug_warps_sub(map[m].npc[i]);
+ for (m = 0; m < map->count; m++)
+ for (i = 0; i < map->list[m].npc_num; i++)
+ npc->debug_warps_sub(map->list[m].npc[i]);
}
/*==========================================
* npc initialization
*------------------------------------------*/
-int do_init_npc(void)
-{
- struct npc_src_list *file;
+int do_init_npc(bool minimal) {
int i;
- memset(&npc_base_ud, 0, sizeof( struct unit_data) );
- npc_base_ud.bl = NULL;
- npc_base_ud.walktimer = INVALID_TIMER;
- npc_base_ud.skilltimer = INVALID_TIMER;
- npc_base_ud.attacktimer = INVALID_TIMER;
- npc_base_ud.attackabletime =
- npc_base_ud.canact_tick =
- npc_base_ud.canmove_tick = iTimer->gettick();
+ memset(&npc->base_ud, 0, sizeof( struct unit_data) );
+ npc->base_ud.bl = NULL;
+ npc->base_ud.walktimer = INVALID_TIMER;
+ npc->base_ud.skilltimer = INVALID_TIMER;
+ npc->base_ud.attacktimer = INVALID_TIMER;
+ npc->base_ud.attackabletime =
+ npc->base_ud.canact_tick =
+ npc->base_ud.canmove_tick = timer->gettick();
//Stock view data for normal npcs.
memset(&npc_viewdb, 0, sizeof(npc_viewdb));
@@ -3969,58 +4543,58 @@ int do_init_npc(void)
for( i = MAX_NPC_CLASS2_START; i < MAX_NPC_CLASS2_END; i++ )
npc_viewdb2[i - MAX_NPC_CLASS2_START].class_ = i;
- ev_db = strdb_alloc((DBOptions)(DB_OPT_DUP_KEY|DB_OPT_RELEASE_DATA),2*NAME_LENGTH+2+1);
- npcname_db = strdb_alloc(DB_OPT_BASE,NAME_LENGTH);
- npc_path_db = strdb_alloc(DB_OPT_BASE|DB_OPT_DUP_KEY|DB_OPT_RELEASE_DATA,80);
+ npc->ev_db = strdb_alloc(DB_OPT_DUP_KEY|DB_OPT_RELEASE_DATA, EVENT_NAME_LENGTH);
+ npc->ev_label_db = strdb_alloc(DB_OPT_DUP_KEY|DB_OPT_RELEASE_DATA, NAME_LENGTH);
+ npc->name_db = strdb_alloc(DB_OPT_BASE, NAME_LENGTH);
+ npc->path_db = strdb_alloc(DB_OPT_DUP_KEY|DB_OPT_RELEASE_DATA, 0);
- timer_event_ers = ers_new(sizeof(struct timer_event_data),"clif.c::timer_event_ers",ERS_OPT_NONE);
+ npc_last_npd = NULL;
+ npc_last_path = NULL;
+ npc_last_ref = NULL;
- // process all npc files
- ShowStatus("Loading NPCs...\r");
- for( file = npc_src_files; file != NULL; file = file->next ) {
- ShowStatus("Loading NPC file: %s"CL_CLL"\r", file->name);
- npc_parsesrcfile(file->name,false);
+ // Should be loaded before npc processing, otherwise labels could overwrite constant values
+ // and lead to undefined behavior [Panikon]
+ itemdb->name_constants();
+
+ if (!minimal) {
+ npc->timer_event_ers = ers_new(sizeof(struct timer_event_data),"clif.c::timer_event_ers",ERS_OPT_NONE);
+
+ npc_process_files(START_NPC_NUM);
}
- ShowInfo ("Done loading '"CL_WHITE"%d"CL_RESET"' NPCs:"CL_CLL"\n"
- "\t-'"CL_WHITE"%d"CL_RESET"' Warps\n"
- "\t-'"CL_WHITE"%d"CL_RESET"' Shops\n"
- "\t-'"CL_WHITE"%d"CL_RESET"' Scripts\n"
- "\t-'"CL_WHITE"%d"CL_RESET"' Spawn sets\n"
- "\t-'"CL_WHITE"%d"CL_RESET"' Mobs Cached\n"
- "\t-'"CL_WHITE"%d"CL_RESET"' Mobs Not Cached\n",
- npc_id - START_NPC_NUM, npc_warp, npc_shop, npc_script, npc_mob, npc_cache_mob, npc_delay_mob);
- iMap->zone_init();
+ if (!minimal) {
+ map->zone_init();
- npc->motd = npc_name2id("HerculesMOTD"); /* [Ind/Hercules] */
+ npc->motd = npc->name2id("HerculesMOTD"); /* [Ind/Hercules] */
- // set up the events cache
- memset(script_event, 0, sizeof(script_event));
- npc_read_event_script();
+ // set up the events cache
+ memset(script_event, 0, sizeof(script_event));
+ npc->read_event_script();
- //Debug function to locate all endless loop warps.
- if (battle_config.warp_point_debug)
- npc_debug_warps();
+ //Debug function to locate all endless loop warps.
+ if (battle_config.warp_point_debug)
+ npc->debug_warps();
- iTimer->add_timer_func_list(npc_event_do_clock,"npc_event_do_clock");
- iTimer->add_timer_func_list(npc_timerevent,"npc_timerevent");
+ timer->add_func_list(npc->event_do_clock,"npc_event_do_clock");
+ timer->add_func_list(npc->timerevent,"npc_timerevent");
+ }
// Init dummy NPC
- fake_nd = (struct npc_data *)aCalloc(1,sizeof(struct npc_data));
- fake_nd->bl.m = -1;
- fake_nd->bl.id = npc_get_new_npc_id();
- fake_nd->class_ = -1;
- fake_nd->speed = 200;
- strcpy(fake_nd->name,"FAKE_NPC");
- memcpy(fake_nd->exname, fake_nd->name, 9);
+ npc->fake_nd = (struct npc_data *)aCalloc(1,sizeof(struct npc_data));
+ npc->fake_nd->bl.m = -1;
+ npc->fake_nd->bl.id = npc->get_new_npc_id();
+ npc->fake_nd->class_ = -1;
+ npc->fake_nd->speed = 200;
+ strcpy(npc->fake_nd->name,"FAKE_NPC");
+ memcpy(npc->fake_nd->exname, npc->fake_nd->name, 9);
npc_script++;
- fake_nd->bl.type = BL_NPC;
- fake_nd->subtype = SCRIPT;
+ npc->fake_nd->bl.type = BL_NPC;
+ npc->fake_nd->subtype = SCRIPT;
- strdb_put(npcname_db, fake_nd->exname, fake_nd);
- fake_nd->u.scr.timerid = INVALID_TIMER;
- iMap->addiddb(&fake_nd->bl);
+ strdb_put(npc->name_db, npc->fake_nd->exname, npc->fake_nd);
+ npc->fake_nd->u.scr.timerid = INVALID_TIMER;
+ map->addiddb(&npc->fake_nd->bl);
// End of initialization
return 0;
@@ -4029,4 +4603,114 @@ void npc_defaults(void) {
npc = &npc_s;
npc->motd = NULL;
+ npc->ev_db = NULL;
+ npc->ev_label_db = NULL;
+ npc->name_db = NULL;
+ npc->path_db = NULL;
+ npc->timer_event_ers = NULL;
+ npc->fake_nd = NULL;
+ npc->src_files = NULL;
+ /* */
+ npc->trader_ok = false;
+ npc->trader_funds[0] = npc->trader_funds[1] = 0;
+ /* */
+ npc->init = do_init_npc;
+ npc->final = do_final_npc;
+ /* */
+ npc->get_new_npc_id = npc_get_new_npc_id;
+ npc->get_viewdata = npc_get_viewdata;
+ npc->isnear_sub = npc_isnear_sub;
+ npc->isnear = npc_isnear;
+ npc->ontouch_event = npc_ontouch_event;
+ npc->ontouch2_event = npc_ontouch2_event;
+ npc->enable_sub = npc_enable_sub;
+ npc->enable = npc_enable;
+ npc->name2id = npc_name2id;
+ npc->event_dequeue = npc_event_dequeue;
+ npc->event_export_create = npc_event_export_create;
+ npc->event_export = npc_event_export;
+ npc->event_sub = npc_event_sub;
+ npc->event_doall_sub = npc_event_doall_sub;
+ npc->event_do = npc_event_do;
+ npc->event_doall_id = npc_event_doall_id;
+ npc->event_doall = npc_event_doall;
+ npc->event_do_clock = npc_event_do_clock;
+ npc->event_do_oninit = npc_event_do_oninit;
+ npc->timerevent_export = npc_timerevent_export;
+ npc->timerevent = npc_timerevent;
+ npc->timerevent_start = npc_timerevent_start;
+ npc->timerevent_stop = npc_timerevent_stop;
+ npc->timerevent_quit = npc_timerevent_quit;
+ npc->gettimerevent_tick = npc_gettimerevent_tick;
+ npc->settimerevent_tick = npc_settimerevent_tick;
+ npc->event = npc_event;
+ npc->touch_areanpc_sub = npc_touch_areanpc_sub;
+ npc->touchnext_areanpc = npc_touchnext_areanpc;
+ npc->touch_areanpc = npc_touch_areanpc;
+ npc->touch_areanpc2 = npc_touch_areanpc2;
+ npc->check_areanpc = npc_check_areanpc;
+ npc->checknear = npc_checknear;
+ npc->globalmessage = npc_globalmessage;
+ npc->run_tomb = run_tomb;
+ npc->click = npc_click;
+ npc->scriptcont = npc_scriptcont;
+ npc->buysellsel = npc_buysellsel;
+ npc->cashshop_buylist = npc_cashshop_buylist;
+ npc->buylist_sub = npc_buylist_sub;
+ npc->cashshop_buy = npc_cashshop_buy;
+ npc->buylist = npc_buylist;
+ npc->selllist_sub = npc_selllist_sub;
+ npc->selllist = npc_selllist;
+ npc->remove_map = npc_remove_map;
+ npc->unload_ev = npc_unload_ev;
+ npc->unload_ev_label = npc_unload_ev_label;
+ npc->unload_dup_sub = npc_unload_dup_sub;
+ npc->unload_duplicates = npc_unload_duplicates;
+ npc->unload = npc_unload;
+ npc->clearsrcfile = npc_clearsrcfile;
+ npc->addsrcfile = npc_addsrcfile;
+ npc->delsrcfile = npc_delsrcfile;
+ npc->parsename = npc_parsename;
+ npc->parseview = npc_parseview;
+ npc->viewisid = npc_viewisid;
+ npc->add_warp = npc_add_warp;
+ npc->parse_warp = npc_parse_warp;
+ npc->parse_shop = npc_parse_shop;
+ npc->convertlabel_db = npc_convertlabel_db;
+ npc->skip_script = npc_skip_script;
+ npc->parse_script = npc_parse_script;
+ npc->parse_duplicate = npc_parse_duplicate;
+ npc->duplicate4instance = npc_duplicate4instance;
+ npc->setcells = npc_setcells;
+ npc->unsetcells_sub = npc_unsetcells_sub;
+ npc->unsetcells = npc_unsetcells;
+ npc->movenpc = npc_movenpc;
+ npc->setdisplayname = npc_setdisplayname;
+ npc->setclass = npc_setclass;
+ npc->do_atcmd_event = npc_do_atcmd_event;
+ npc->parse_function = npc_parse_function;
+ npc->parse_mob2 = npc_parse_mob2;
+ npc->parse_mob = npc_parse_mob;
+ npc->parse_mapflag = npc_parse_mapflag;
+ npc->parsesrcfile = npc_parsesrcfile;
+ npc->script_event = npc_script_event;
+ npc->read_event_script = npc_read_event_script;
+ npc->path_db_clear_sub = npc_path_db_clear_sub;
+ npc->ev_label_db_clear_sub = npc_ev_label_db_clear_sub;
+ npc->reload = npc_reload;
+ npc->unloadfile = npc_unloadfile;
+ npc->do_clear_npc = do_clear_npc;
+ npc->debug_warps_sub = npc_debug_warps_sub;
+ npc->debug_warps = npc_debug_warps;
+ npc->secure_timeout_timer = npc_rr_secure_timeout_timer;
+ /* */
+ npc->trader_count_funds = npc_trader_count_funds;
+ npc->trader_pay = npc_trader_pay;
+ npc->trader_update = npc_trader_update;
+ npc->market_buylist = npc_market_buylist;
+ npc->trader_open = npc_trader_open;
+ npc->market_fromsql = npc_market_fromsql;
+ npc->market_tosql = npc_market_tosql;
+ npc->market_delfromsql = npc_market_delfromsql;
+ npc->market_delfromsql_sub = npc_market_delfromsql_sub;
}