From 408f4d9391dc280c1ac1b0fdfaa0be33f6d27d25 Mon Sep 17 00:00:00 2001
From: shennetsind <shennetsind@54d463be-8e91-2dee-dedb-b68131a5f0ec>
Date: Sat, 19 May 2012 23:15:28 +0000
Subject: NPC Loaded by @loadnpc will now trigger their OnInit labels as
 they're loaded.

git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@16130 54d463be-8e91-2dee-dedb-b68131a5f0ec
---
 src/map/atcommand.c |  2 +-
 src/map/npc.c       | 28 ++++++++++++++++++++--------
 src/map/npc.h       |  2 +-
 3 files changed, 22 insertions(+), 10 deletions(-)

(limited to 'src')

diff --git a/src/map/atcommand.c b/src/map/atcommand.c
index 0524ce321..e171cf239 100644
--- a/src/map/atcommand.c
+++ b/src/map/atcommand.c
@@ -4526,7 +4526,7 @@ ACMD_FUNC(loadnpc)
 
 	// add to list of script sources and run it
 	npc_addsrcfile(message);
-	npc_parsesrcfile(message);
+	npc_parsesrcfile(message,true);
 	npc_read_event_script();
 
 	clif_displaymessage(fd, msg_txt(262));
diff --git a/src/map/npc.c b/src/map/npc.c
index 31bad9c6b..5976412db 100644
--- a/src/map/npc.c
+++ b/src/map/npc.c
@@ -2272,8 +2272,7 @@ 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)
-{
+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
 	char mapname[32];
 	struct script_code *script;
@@ -2388,6 +2387,20 @@ static const char* npc_parse_script(char* w1, char* w2, char* w3, char* w4, cons
 
 	nd->u.scr.timerid = INVALID_TIMER;
 
+	if( runOnInit ) {
+		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) ) ) {
+			
+			//Execute OnInit
+			run_script(nd->u.scr.script,ev->pos,0,nd->bl.id);
+			
+		}
+	}
+	
 	return end;
 }
 
@@ -3199,7 +3212,7 @@ static const char* npc_parse_mapflag(char* w1, char* w2, char* w3, char* w4, con
 	return strchr(start,'\n');// continue
 }
 
-void npc_parsesrcfile(const char* filepath)
+void npc_parsesrcfile(const char* filepath, bool runOnInit)
 {
 	int m, lines = 0;
 	FILE* fp;
@@ -3325,7 +3338,7 @@ void npc_parsesrcfile(const char* filepath)
 			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);
+				p = npc_parse_script(w1,w2,w3,w4, p, buffer, filepath,runOnInit);
 		}
 		else if( (i=0, sscanf(w2,"duplicate%n",&i), (i > 0 && w2[i] == '(')) && count > 3 )
 		{
@@ -3479,7 +3492,7 @@ int npc_reload(void) {
 	// 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);
+		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"
@@ -3577,10 +3590,9 @@ int do_init_npc(void)
 	timer_event_ers = ers_new(sizeof(struct timer_event_data));
 	// process all npc files
 	ShowStatus("Loading NPCs...\r");
-	for( file = npc_src_files; file != NULL; file = file->next )
-	{
+	for( file = npc_src_files; file != NULL; file = file->next ) {
 		ShowStatus("Loading NPC file: %s"CL_CLL"\r", file->name);
-		npc_parsesrcfile(file->name);
+		npc_parsesrcfile(file->name,false);
 	}
 	ShowInfo ("Done loading '"CL_WHITE"%d"CL_RESET"' NPCs:"CL_CLL"\n"
 		"\t-'"CL_WHITE"%d"CL_RESET"' Warps\n"
diff --git a/src/map/npc.h b/src/map/npc.h
index 5e180220c..6f8737c65 100644
--- a/src/map/npc.h
+++ b/src/map/npc.h
@@ -134,7 +134,7 @@ int npc_get_new_npc_id(void);
 
 void npc_addsrcfile(const char* name);
 void npc_delsrcfile(const char* name);
-void npc_parsesrcfile(const char* filepath);
+void npc_parsesrcfile(const char* filepath, bool runOnInit);
 void do_clear_npc(void);
 int do_final_npc(void);
 int do_init_npc(void);
-- 
cgit v1.2.3-70-g09d2