summaryrefslogtreecommitdiff
path: root/src/map
diff options
context:
space:
mode:
authorInkfish <Inkfish@54d463be-8e91-2dee-dedb-b68131a5f0ec>2009-05-07 14:52:22 +0000
committerInkfish <Inkfish@54d463be-8e91-2dee-dedb-b68131a5f0ec>2009-05-07 14:52:22 +0000
commit8a2839bcc9547e6e1239f87c6901a1fe46dc06a8 (patch)
tree6a75c3bbf2479d299a240deb0f3ca9ab3c1c1728 /src/map
parent7666e881212f15d7d8ed22ef6ee82f98b15c175c (diff)
downloadhercules-8a2839bcc9547e6e1239f87c6901a1fe46dc06a8.tar.gz
hercules-8a2839bcc9547e6e1239f87c6901a1fe46dc06a8.tar.bz2
hercules-8a2839bcc9547e6e1239f87c6901a1fe46dc06a8.tar.xz
hercules-8a2839bcc9547e6e1239f87c6901a1fe46dc06a8.zip
* Clean up for npc timer code
git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@13738 54d463be-8e91-2dee-dedb-b68131a5f0ec
Diffstat (limited to 'src/map')
-rw-r--r--src/map/npc.c74
-rw-r--r--src/map/script.c69
2 files changed, 77 insertions, 66 deletions
diff --git a/src/map/npc.c b/src/map/npc.c
index d8b1436a0..fd1aa93b9 100644
--- a/src/map/npc.c
+++ b/src/map/npc.c
@@ -405,7 +405,7 @@ int npc_timerevent_import(char* lname, void* data, va_list ap)
struct timer_event_data {
int rid; //Attached player for this timer.
int next; //timer index (starts with 0, then goes up to nd->u.scr.timeramount
- int time; //holds total time elapsed for the script since time 0 (whenthe timers started)
+ int time; //holds total time elapsed for the script from when timer started to when last time event triggered.
unsigned int otick; //Holds tick value at which timer sequence was started (that is, it stores the tick value for which T= 0
};
@@ -445,7 +445,7 @@ int npc_timerevent(int tid, unsigned int tick, int id, intptr data)
t = nd->u.scr.timer = ted->time;
ted->next++;
- if( nd->u.scr.timeramount > ted->next)
+ if( nd->u.scr.timeramount > ted->next )
{
next = nd->u.scr.timer_event[ ted->next ].timer - nd->u.scr.timer_event[ ted->next - 1 ].timer;
ted->time += next;
@@ -479,7 +479,7 @@ int npc_timerevent(int tid, unsigned int tick, int id, intptr data)
int npc_timerevent_start(struct npc_data* nd, int rid)
{
int j, next;
- struct map_session_data *sd=NULL; //Player to whom script is attached.
+ struct map_session_data *sd = NULL; //Player to whom script is attached.
struct timer_event_data *ted;
nullpo_retr(0, nd);
@@ -487,12 +487,13 @@ int npc_timerevent_start(struct npc_data* nd, int rid)
if( nd->u.scr.timeramount == 0 )
return 0;
+ // 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( j >= nd->u.scr.timeramount ) // check if there is a timer to use !!BEFORE!! you write stuff to the structures [Shinomori]
+ if( j >= nd->u.scr.timeramount ) // Check if there is an OnTimer Event
return 0;
if( nd->u.scr.rid > 0 && !(sd = map_id2sd(nd->u.scr.rid)) )
- { //Try to attach timer to this player.
+ { //Failed to attach timer to this player.
ShowError("npc_timerevent_start: Attached player not found!\n");
return 1;
}
@@ -507,17 +508,12 @@ int npc_timerevent_start(struct npc_data* nd, int rid)
return 0;
ted = ers_alloc(timer_event_ers, struct timer_event_data);
- ted->next = j;
- nd->u.scr.timertick = ted->otick = gettick();
+ ted->next = j; // Set event index
+ nd->u.scr.timertick = ted->otick = gettick(); // Set when timer is started
//Attach only the player if attachplayerrid was used.
ted->rid = sd?sd->bl.id:0;
- // Do not store it to make way to two types of timers: globals and personals.
-// if( rid >= 0 ) // changed to: attaching to given rid by default [Shinomori]
-// nd->u.scr.rid=rid;
- // if rid is less than 0 leave it unchanged [celest]
-
next = nd->u.scr.timer_event[j].timer - nd->u.scr.timer;
ted->time = nd->u.scr.timer_event[j].timer;
if( sd )
@@ -531,11 +527,12 @@ int npc_timerevent_start(struct npc_data* nd, int rid)
*------------------------------------------*/
int npc_timerevent_stop(struct npc_data* nd)
{
- struct map_session_data *sd =NULL;
+ struct map_session_data *sd = NULL;
const struct TimerData *td = NULL;
int *tid;
nullpo_retr(0, nd);
+
if( nd->u.scr.rid && !(sd = map_id2sd(nd->u.scr.rid)) )
{
ShowError("npc_timerevent_stop: Attached player not found!\n");
@@ -543,7 +540,7 @@ int npc_timerevent_stop(struct npc_data* nd)
}
tid = sd?&sd->npc_timer_id:&nd->u.scr.timerid;
- if (*tid == -1) //Nothing to stop
+ if( *tid == -1 ) //Nothing to stop
return 0;
td = get_timer(*tid);
@@ -552,10 +549,10 @@ int npc_timerevent_stop(struct npc_data* nd)
delete_timer(*tid,npc_timerevent);
*tid = -1;
- //Set the timer tick to the time that has passed since the beginning of the timers and now.
+ //Set 'timer' to the time that has passed since the beginning of the timers and now.
nd->u.scr.timer += DIFF_TICK(gettick(),nd->u.scr.timertick);
+ //Set 'tick' to zero so that we know it's off.
nd->u.scr.timertick = 0;
-// nd->u.scr.rid = 0; //Eh? why detach?
return 0;
}
@@ -570,17 +567,17 @@ void npc_timerevent_quit(struct map_session_data* sd)
if( sd->npc_timer_id == -1 )
return;
-
if( !(td = get_timer(sd->npc_timer_id)) )
{
sd->npc_timer_id = -1;
- return; //??
+ return;
}
nd = (struct npc_data *)map_id2bl(td->id);
ted = (struct timer_event_data*)td->data;
delete_timer(sd->npc_timer_id, npc_timerevent);
sd->npc_timer_id = -1;
+
if( nd && nd->bl.type == BL_NPC )
{ //Execute OnTimerQuit
char buf[NAME_LENGTH*2+3];
@@ -622,43 +619,48 @@ void npc_timerevent_quit(struct map_session_data* sd)
}
/*==========================================
- * タイマー値の所得
+ * Get the tick value of a npc timer
+ * If it's stopped, return stopped time
*------------------------------------------*/
int npc_gettimerevent_tick(struct npc_data* nd)
{
int tick;
nullpo_retr(0, nd);
- tick=nd->u.scr.timer;
- if (nd->u.scr.timertick)
- tick+=DIFF_TICK(gettick(), nd->u.scr.timertick);
+ tick = nd->u.scr.timer; // The last time it's active(stop or event trigger)
+ if( nd->u.scr.timertick ) // It's a running timer
+ tick += DIFF_TICK(gettick(), nd->u.scr.timertick);
+
return tick;
}
+
/*==========================================
- * タイマー値の設定
+ * Set tick for running and stopped timer
*------------------------------------------*/
int npc_settimerevent_tick(struct npc_data* nd, int newtimer)
{
- int flag;
- struct map_session_data *sd=NULL;
+ bool flag;
+ struct map_session_data *sd = NULL;
nullpo_retr(0, nd);
- if (nd->u.scr.rid) {
- sd = map_id2sd(nd->u.scr.rid);
- if (!sd) {
+ //Check if timer is started
+ if( nd->u.scr.rid )
+ {
+ if( !(sd = map_id2sd(nd->u.scr.rid)) )
+ {
ShowError("npc_settimerevent_tick: Attached player not found!\n");
return 1;
}
- flag= sd->npc_timer_id != -1 ;
- } else
- flag= nd->u.scr.timerid != -1 ;
+ flag = (sd->npc_timer_id != -1);
+ }
+ else
+ flag = (nd->u.scr.timerid != -1);
+
+ if( flag ) npc_timerevent_stop(nd);
+ nd->u.scr.timer = newtimer;
+ if( flag ) npc_timerevent_start(nd, -1);
- if(flag)
- npc_timerevent_stop(nd);
- nd->u.scr.timer=newtimer;
- if(flag)
- npc_timerevent_start(nd, -1);
return 0;
}
diff --git a/src/map/script.c b/src/map/script.c
index 6fb3d662c..85cd4d095 100644
--- a/src/map/script.c
+++ b/src/map/script.c
@@ -7712,6 +7712,7 @@ BUILDIN_FUNC(initnpctimer)
{
struct npc_data *nd;
int flag = 0;
+
if( script_hasdata(st,3) )
{ //Two arguments: NPC name and attach flag.
nd = npc_name2id(script_getstr(st, 2));
@@ -7736,11 +7737,11 @@ BUILDIN_FUNC(initnpctimer)
}
}
else
- nd=(struct npc_data *)map_id2bl(st->oid);
+ nd = (struct npc_data *)map_id2bl(st->oid);
- if (!nd)
+ if( !nd )
return 0;
- if (flag) //Attach
+ if( flag ) //Attach
{
TBL_PC* sd = script_rid2sd(st);
if( sd == NULL )
@@ -7759,6 +7760,7 @@ BUILDIN_FUNC(startnpctimer)
{
struct npc_data *nd;
int flag = 0;
+
if( script_hasdata(st,3) )
{ //Two arguments: NPC name and attach flag.
nd = npc_name2id(script_getstr(st, 2));
@@ -7785,9 +7787,9 @@ BUILDIN_FUNC(startnpctimer)
else
nd=(struct npc_data *)map_id2bl(st->oid);
- if (!nd)
+ if( !nd )
return 0;
- if (flag) //Attach
+ if( flag ) //Attach
{
TBL_PC* sd = script_rid2sd(st);
if( sd == NULL )
@@ -7805,6 +7807,7 @@ BUILDIN_FUNC(stopnpctimer)
{
struct npc_data *nd;
int flag = 0;
+
if( script_hasdata(st,3) )
{ //Two arguments: NPC name and attach flag.
nd = npc_name2id(script_getstr(st, 2));
@@ -7831,9 +7834,9 @@ BUILDIN_FUNC(stopnpctimer)
else
nd=(struct npc_data *)map_id2bl(st->oid);
- if (!nd)
+ if( !nd )
return 0;
- if (flag) //Detach
+ if( flag ) //Detach
nd->u.scr.rid = 0;
npc_timerevent_stop(nd);
@@ -7846,35 +7849,41 @@ BUILDIN_FUNC(getnpctimer)
{
struct npc_data *nd;
TBL_PC *sd;
- int type=script_getnum(st,2);
- int val=0;
+ int type = script_getnum(st,2);
+ int val = 0;
+
if( script_hasdata(st,3) )
nd = npc_name2id(script_getstr(st,3));
else
nd = (struct npc_data *)map_id2bl(st->oid);
- if (!nd || nd->bl.type != BL_NPC)
+ if( !nd || nd->bl.type != BL_NPC )
{
script_pushint(st,0);
ShowError("getnpctimer: Invalid NPC.\n");
return 1;
}
- switch(type){
- case 0: val=npc_gettimerevent_tick(nd); break;
+ switch( type )
+ {
+ case 0: val = npc_gettimerevent_tick(nd); break;
case 1:
- if (nd->u.scr.rid) {
+ if( nd->u.scr.rid )
+ {
sd = map_id2sd(nd->u.scr.rid);
- if (!sd) {
+ if( !sd )
+ {
ShowError("buildin_getnpctimer: Attached player not found!\n");
break;
}
val = (sd->npc_timer_id != -1);
- } else
- val= (nd->u.scr.timerid !=-1);
+ }
+ else
+ val = (nd->u.scr.timerid !=-1);
break;
- case 2: val= nd->u.scr.timeramount; break;
+ case 2: val = nd->u.scr.timeramount; break;
}
+
script_pushint(st,val);
return 0;
}
@@ -7885,13 +7894,14 @@ BUILDIN_FUNC(setnpctimer)
{
int tick;
struct npc_data *nd;
- tick=script_getnum(st,2);
+
+ tick = script_getnum(st,2);
if( script_hasdata(st,3) )
- nd=npc_name2id(script_getstr(st,3));
+ nd = npc_name2id(script_getstr(st,3));
else
- nd=(struct npc_data *)map_id2bl(st->oid);
+ nd = (struct npc_data *)map_id2bl(st->oid);
- if (!nd || nd->bl.type != BL_NPC)
+ if( !nd || nd->bl.type != BL_NPC )
{
script_pushint(st,1);
ShowError("setnpctimer: Invalid NPC.\n");
@@ -7909,11 +7919,9 @@ BUILDIN_FUNC(setnpctimer)
BUILDIN_FUNC(attachnpctimer)
{
TBL_PC *sd;
- struct npc_data *nd;
+ struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
- nd=(struct npc_data *)map_id2bl(st->oid);
-
- if (!nd || nd->bl.type != BL_NPC)
+ if( !nd || nd->bl.type != BL_NPC )
{
script_pushint(st,1);
ShowError("setnpctimer: Invalid NPC.\n");
@@ -7921,11 +7929,11 @@ BUILDIN_FUNC(attachnpctimer)
}
if( script_hasdata(st,2) )
- sd=map_nick2sd(script_getstr(st,2));
+ sd = map_nick2sd(script_getstr(st,2));
else
sd = script_rid2sd(st);
- if (!sd)
+ if( !sd )
{
script_pushint(st,1);
ShowWarning("attachnpctimer: Invalid player.\n");
@@ -7943,12 +7951,13 @@ BUILDIN_FUNC(attachnpctimer)
BUILDIN_FUNC(detachnpctimer)
{
struct npc_data *nd;
+
if( script_hasdata(st,2) )
- nd=npc_name2id(script_getstr(st,2));
+ nd = npc_name2id(script_getstr(st,2));
else
- nd=(struct npc_data *)map_id2bl(st->oid);
+ nd = (struct npc_data *)map_id2bl(st->oid);
- if (!nd || nd->bl.type != BL_NPC)
+ if( !nd || nd->bl.type != BL_NPC )
{
script_pushint(st,1);
ShowError("detachnpctimer: Invalid NPC.\n");