-
Notifications
You must be signed in to change notification settings - Fork 757
/
npc.c
6201 lines (5351 loc) · 195 KB
/
npc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* This file is part of Hercules.
* http://herc.ws - http://github.com/HerculesWS/Hercules
*
* Copyright (C) 2012-2022 Hercules Dev Team
* Copyright (C) Athena Dev Teams
*
* Hercules is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#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 "map/battle.h"
#include "map/chat.h"
#include "map/clan.h"
#include "map/clif.h"
#include "map/guild.h"
#include "map/instance.h"
#include "map/intif.h"
#include "map/itemdb.h"
#include "map/log.h"
#include "map/map.h"
#include "map/mob.h"
#include "map/pc.h"
#include "map/pet.h"
#include "map/quest.h"
#include "map/script.h"
#include "map/skill.h"
#include "map/status.h"
#include "map/unit.h"
#include "map/achievement.h"
#include "common/HPM.h"
#include "common/cbasetypes.h"
#include "common/db.h"
#include "common/ers.h"
#include "common/memmgr.h"
#include "common/nullpo.h"
#include "common/showmsg.h"
#include "common/socket.h"
#include "common/sql.h"
#include "common/strlib.h"
#include "common/timer.h"
#include "common/utils.h"
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
static struct npc_interface npc_s;
struct npc_interface *npc;
//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 */
static 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];
const char *event_name[UCHAR_MAX];
uint8 event_count;
} script_event[NPCE_MAX];
/**
* Returns the viewdata for normal npc classes.
* @param class_ The NPC class ID.
* @return The viewdata, or NULL if the ID is invalid.
*/
static struct view_data *npc_get_viewdata(int class_)
{
if (class_ == INVISIBLE_CLASS)
return &npc_viewdb[0];
if (npc->db_checkid(class_)) {
if (class_ < MAX_NPC_CLASS) {
return &npc_viewdb[class_];
} else if (class_ >= MAX_NPC_CLASS2_START && class_ < MAX_NPC_CLASS2_END) {
return &npc_viewdb2[class_-MAX_NPC_CLASS2_START];
}
}
return NULL;
}
/**
* Checks if a given id is a valid npc id.
*
* Since new npcs are added all the time, the max valid value is the one before the first mob (Scorpion = 1001)
*
* @param id The NPC ID to validate.
* @return Whether the value is a valid ID.
*/
static bool npc_db_checkid(int id)
{
if (id >= WARP_CLASS && id <= 125) // First subrange
return true;
if (id == HIDDEN_WARP_CLASS || id == INVISIBLE_CLASS) // Special IDs not included in the valid ranges
return true;
if (id > 400 && id < MAX_NPC_CLASS) // Second subrange
return true;
if (id >= MAX_NPC_CLASS2_START && id < MAX_NPC_CLASS2_END) // Second range
return true;
if (pc->db_checkid(id))
return true;
// Anything else is invalid
return false;
}
/// Returns a new npc id that isn't being used in id_db.
/// Fatal error if nothing is available.
static int npc_get_new_npc_id(void)
{
if (npc->npc_id >= START_NPC_NUM && !map->blid_exists(npc->npc_id))
return npc->npc_id++;// available
else {// find next id
int base_id = npc->npc_id;
while (base_id != ++npc->npc_id) {
if (npc->npc_id < START_NPC_NUM)
npc->npc_id = START_NPC_NUM;
if (!map->blid_exists(npc->npc_id))
return npc->npc_id++;// available
}
// full loop, nothing available
ShowFatalError("npc_get_new_npc_id: All ids are taken. Exiting...");
exit(1);
}
}
static int npc_isnear_sub(struct block_list *bl, va_list args)
{
const struct npc_data *nd = NULL;
nullpo_ret(bl);
Assert_ret(bl->type == BL_NPC);
nd = BL_UCCAST(BL_NPC, bl);
if( nd->option & (OPTION_HIDE|OPTION_INVISIBLE) )
return 0;
if( battle_config.vendchat_near_hiddennpc && ( nd->class_ == FAKE_NPC || nd->class_ == HIDDEN_WARP_CLASS ) )
return 0;
if (nd->dyn.isdynamic)
return 0;
return 1;
}
static bool npc_isnear(struct block_list *bl)
{
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;
}
static int npc_ontouch_event(struct map_session_data *sd, struct npc_data *nd)
{
char name[EVENT_NAME_LENGTH];
nullpo_retr(1, nd);
if( nd->touching_id )
return 0; // Attached a player already. Can't trigger on anyone else.
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);
}
static int npc_ontouch2_event(struct map_session_data *sd, struct npc_data *nd)
{
char name[EVENT_NAME_LENGTH];
nullpo_retr(1, sd);
nullpo_retr(1, 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);
}
static int npc_onuntouch_event(struct map_session_data *sd, struct npc_data *nd)
{
char name[EVENT_NAME_LENGTH];
nullpo_ret(sd);
nullpo_ret(nd);
if (sd->areanpc_id != nd->bl.id)
return 0;
snprintf(name, ARRAYLENGTH(name), "%s::%s", nd->exname, script->config.onuntouch_name);
return npc->event(sd, name, 2);
}
/*==========================================
* Sub-function of npc_enable, runs OnTouch event when enabled
*------------------------------------------*/
static int npc_enable_sub(struct block_list *bl, va_list ap)
{
struct npc_data *nd;
nullpo_ret(bl);
nullpo_ret(nd=va_arg(ap,struct npc_data *));
if (bl->type == BL_PC) {
struct map_session_data *sd = BL_UCAST(BL_PC, bl);
if (nd->option&OPTION_INVISIBLE)
return 1;
if (nd->dyn.isdynamic && nd->dyn.owner_id != sd->status.char_id)
return 1;
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, STOPWALKING_FLAG_FIXPOS);
npc->click(sd,nd);
}
}
return 0;
}
/*==========================================
* Disable / Enable NPC
*------------------------------------------*/
static int npc_enable(const char *name, int flag)
{
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);
return 0;
}
if (flag&1) {
nd->option&=~OPTION_INVISIBLE;
clif->spawn(&nd->bl);
} else if (flag&2)
nd->option&=~OPTION_HIDE;
else if (flag&4)
nd->option|= OPTION_HIDE;
else { //Can't change the view_data to invisible class because the view_data for all npcs is shared! [Skotlex]
nd->option|= OPTION_INVISIBLE;
clif->clearunit_area(&nd->bl,CLR_OUTSIGHT); // Hack to trick maya purple card [Xazax]
}
if (nd->class_ == WARP_CLASS || nd->class_ == FLAG_CLASS) { //Client won't display option changes for these classes [Toms]
if (nd->option&(OPTION_HIDE|OPTION_INVISIBLE))
clif->clearunit_area(&nd->bl, CLR_OUTSIGHT);
else
clif->spawn(&nd->bl);
} 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
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;
}
/*==========================================
* NPC lookup (get npc_data through npcname)
*------------------------------------------*/
static struct npc_data *npc_name2id(const char *name)
{
return strdb_get(npc->name_db, name);
}
/**
* For the Secure NPC Timeout option (check config/Secure.h) [RR]
**/
/**
* Timer to check for idle time and timeout the dialog if necessary
**/
static 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 = 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
}
switch( sd->npc_idle_type ) {
case NPCT_INPUT:
timeout = NPC_SECURE_TIMEOUT_INPUT;
break;
case NPCT_MENU:
timeout = NPC_SECURE_TIMEOUT_MENU;
break;
//case NPCT_WAIT: var starts with this value
}
if( DIFF_TICK(tick,sd->npc_idle_tick) > (timeout*1000) ) {
/**
* If we still have the NPC script attached, tell it to stop.
**/
if( sd->st )
sd->st->state = END;
sd->state.menu_or_input = 0;
sd->npc_menu = 0;
/**
* This guy's been idle for longer than allowed, close him.
**/
clif->scriptclose(sd,sd->npc_id);
sd->npc_idle_timer = INVALID_TIMER;
/**
* We will end the script ourselves, client will request to end it again if it have dialog,
* however it will be ignored, workaround for client stuck if NPC have no dialog. [hemagx]
**/
sd->state.dialog = 0;
npc->scriptcont(sd, sd->npc_id, true);
} else //Create a new instance of ourselves to continue
sd->npc_idle_timer = timer->add(timer->gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc->secure_timeout_timer,sd->bl.id,0);
#endif
return 0;
}
/*==========================================
* Dequeue event and add timer for execution (100ms)
*------------------------------------------*/
static int npc_event_dequeue(struct map_session_data *sd)
{
nullpo_ret(sd);
if(sd->npc_id) { //Current script is aborted.
if(sd->state.using_fake_npc){
clif->clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
sd->state.using_fake_npc = 0;
}
if (sd->st) {
script->free_state(sd->st);
sd->st = NULL;
}
sd->npc_id = 0;
}
if (!sd->eventqueue[0][0])
return 0; //Nothing to dequeue
if (!pc->addeventtimer(sd,100,sd->eventqueue[0])) { //Failed to dequeue, couldn't set a timer.
ShowWarning("npc_event_dequeue: event timer is full !\n");
return 0;
}
//Event dequeued successfully, shift other elements.
memmove(sd->eventqueue[0], sd->eventqueue[1], (MAX_EVENTQUEUE-1)*sizeof(sd->eventqueue[0]));
sd->eventqueue[MAX_EVENTQUEUE-1][0]=0;
return 1;
}
/**
* @see DBCreateData
*/
static struct DBData npc_event_export_create(union 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)
{
char* lname;
int pos;
nullpo_ret(nd);
Assert_ret(i >= 0 && i < nd->u.scr.label_list_num);
lname = nd->u.scr.label_list[i].name;
pos = nd->u.scr.label_list[i].pos;
if ((nd->u.scr.label_list[i].flags & LABEL_IS_EXTERN) != 0
&& ((nd->u.scr.label_list[i].flags & LABEL_IS_USERFUNC) == 0
|| script->config.functions_as_events)) {
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;
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;
}
static int npc_event_sub(struct map_session_data *sd, struct event_data *ev, const char *eventname); //[Lance]
/**
* Exec name (NPC events) on player or global
* Do on all NPC when called with foreach
*/
static void npc_event_doall_sub(void *key, void *data, va_list ap)
{
struct event_data* ev = data;
int* c;
const char* name;
int rid;
nullpo_retv(c = va_arg(ap, int*));
nullpo_retv(name = va_arg(ap, const char*));
rid = va_arg(ap, int);
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
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_npc(ev->nd->u.scr.script, ev->pos, rid, ev->nd->bl.id);
}
(*c)++;
}
}
// runs the specified event (supports both single-npc and global events)
static int npc_event_do(const char *name)
{
nullpo_ret(name);
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_npc(ev->nd->u.scr.script, ev->pos, 0, ev->nd->bl.id);
return 1;
}
}
return 0;
}
// runs the specified event, with a RID attached (global only)
static 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 (label_linkdb == NULL)
return 0;
linkdb_foreach(label_linkdb, npc->event_doall_sub, &c, name, rid);
return c;
}
// runs the specified event (global only)
static int npc_event_doall(const char *name)
{
return npc->event_doall_id(name, 0);
}
/*==========================================
* Clock event execution
* OnMinute/OnClock/OnHour/OnDay/OnDDHHMM
*------------------------------------------*/
static 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 clock;
struct tm* t;
char buf[64];
int c = 0;
clock = time(NULL);
t = localtime(&clock);
if (t->tm_min != ev_tm_b.tm_min ) {
char* day;
switch (t->tm_wday) {
case 0: day = "Sun"; break;
case 1: day = "Mon"; break;
case 2: day = "Tue"; break;
case 3: day = "Wed"; break;
case 4: day = "Thu"; break;
case 5: day = "Fri"; break;
case 6: day = "Sat"; break;
default:day = ""; break;
}
sprintf(buf,"OnMinute%02d",t->tm_min);
c += npc->event_doall(buf);
sprintf(buf,"OnClock%02d%02d",t->tm_hour,t->tm_min);
c += npc->event_doall(buf);
sprintf(buf,"On%s%02d%02d",day,t->tm_hour,t->tm_min);
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);
}
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);
}
memcpy(&ev_tm_b,t,sizeof(ev_tm_b));
return c;
}
/**
* OnInit event execution (the start of the event and watch)
* @param reload Is the server reloading?
**/
static 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"));
// This interval has already been added on startup
if( !reload )
timer->add_interval(timer->gettick()+100,npc->event_do_clock,0,0,1000);
}
/*==========================================
* Incorporation of the label for the timer event
* called from npc_parse_script
*------------------------------------------*/
static int npc_timerevent_export(struct npc_data *nd, int i)
{
int t = 0, len = 0;
char *lname;
int pos;
nullpo_ret(nd);
lname = nd->u.scr.label_list[i].name;
pos = nd->u.scr.label_list[i].pos;
if (sscanf(lname, "OnTimer%d%n", &t, &len) == 1 && len < NAME_LENGTH && lname[len] == '\0') {
// Timer event
struct npc_timerevent_list *te = nd->u.scr.timer_event;
int j, k = nd->u.scr.timeramount;
if (te == NULL)
te = (struct npc_timerevent_list *)aMalloc(sizeof(struct npc_timerevent_list));
else
te = (struct npc_timerevent_list *)aRealloc( te, sizeof(struct npc_timerevent_list) * (k+1) );
for (j = 0; j < k; j++) {
if (te[j].timer > t) {
memmove(te+j+1, te+j, sizeof(struct npc_timerevent_list)*(k-j));
break;
}
}
te[j].timer = t;
te[j].pos = pos;
nd->u.scr.timer_event = te;
nd->u.scr.timeramount++;
}
return 0;
}
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 from when timer was started to when last time the event triggered.
};
/*==========================================
* triger 'OnTimerXXXX' events
*------------------------------------------*/
static int npc_timerevent(int tid, int64 tick, int id, intptr_t data)
{
int old_rid, old_timer;
int64 old_tick;
struct npc_data *nd = map->id2nd(id);
struct npc_timerevent_list *te;
struct timer_event_data *ted = (struct timer_event_data*)data;
struct map_session_data *sd=NULL;
nullpo_ret(ted);
if( nd == NULL ) {
ShowError("npc_timerevent: NPC not found??\n");
return 0;
}
if (ted->rid && (sd = map->id2sd(ted->rid)) == NULL) {
ShowError("npc_timerevent: Attached player not found.\n");
ers_free(npc->timer_event_ers, ted);
return 0;
}
// These stuffs might need to be restored.
old_rid = nd->u.scr.rid;
old_tick = nd->u.scr.timertick;
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
// Locate the event
te = nd->u.scr.timer_event + ted->next;
// Arrange for the next event
ted->next++;
if( nd->u.scr.timeramount > ted->next )
{
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 = timer->add(tick+next,npc->timerevent,id,(intptr_t)ted);
else
nd->u.scr.timerid = timer->add(tick+next,npc->timerevent,id,(intptr_t)ted);
}
else
{
if( sd )
sd->npc_timer_id = INVALID_TIMER;
else
nd->u.scr.timerid = INVALID_TIMER;
ers_free(npc->timer_event_ers, ted);
}
// Run the script
script->run_npc(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 )
{ // Restore previous data, only if this timer is a player-attached one.
nd->u.scr.timer = old_timer;
nd->u.scr.timertick = old_tick;
}
return 0;
}
/*==========================================
* Start/Resume NPC timer
*------------------------------------------*/
static int npc_timerevent_start(struct npc_data *nd, int rid)
{
int j;
int64 tick = timer->gettick();
struct map_session_data *sd = NULL; //Player to whom script is attached.
nullpo_ret(nd);
// 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 = map->id2sd(nd->u.scr.rid)) == NULL) {
// 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->npc_timer_id != INVALID_TIMER )
return 0;
} else if( nd->u.scr.timerid != INVALID_TIMER || nd->u.scr.timertick )
return 0;
if (j < nd->u.scr.timeramount) {
int next;
struct timer_event_data *ted;
// Arrange for the next event
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 = 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 = timer->add(tick+next,npc->timerevent,nd->bl.id,(intptr_t)ted);
}
} else if (!sd) {
nd->u.scr.timertick = tick;
}
return 0;
}
/*==========================================
* Stop NPC timer
*------------------------------------------*/
static int npc_timerevent_stop(struct npc_data *nd)
{
struct map_session_data *sd = NULL;
int *tid;
nullpo_ret(nd);
if (nd->u.scr.rid && (sd = map->id2sd(nd->u.scr.rid)) == NULL) {
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;
// Delete timer
if (*tid != INVALID_TIMER) {
const struct TimerData *td = timer->get(*tid);
if (td && td->data)
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_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.
}
return 0;
}
/*==========================================
* Aborts a running NPC timer that is attached to a player.
*------------------------------------------*/
static void npc_timerevent_quit(struct map_session_data *sd)
{
const struct TimerData *td;
struct npc_data* nd;
struct timer_event_data *ted;
nullpo_retv(sd);
// Check timer existence
if( sd->npc_timer_id == INVALID_TIMER )
return;
if( !(td = timer->get(sd->npc_timer_id)) )
{
sd->npc_timer_id = INVALID_TIMER;
return;
}
// Delete timer
nd = map->id2nd(td->id);
ted = (struct timer_event_data*)td->data;
timer->delete(sd->npc_timer_id, npc->timerevent);
sd->npc_timer_id = INVALID_TIMER;
// Execute OnTimerQuit
if (nd != NULL) {
char buf[EVENT_NAME_LENGTH];
struct event_data *ev;
snprintf(buf, ARRAYLENGTH(buf), "%s::OnTimerQuit", nd->exname);
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);
ev = NULL;
}
if( ev )
{
int old_rid,old_timer;
int64 old_tick;
nullpo_retv(ted);
//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.
old_tick = nd->u.scr.timertick;
old_timer = nd->u.scr.timer;
nd->u.scr.rid = sd->bl.id;
nd->u.scr.timertick = timer->gettick();
nd->u.scr.timer = ted->time;
//Execute label
script->run_npc(nd->u.scr.script,ev->pos,sd->bl.id,nd->bl.id);
//Restore previous data.
nd->u.scr.rid = old_rid;
nd->u.scr.timer = old_timer;
nd->u.scr.timertick = old_tick;
}
}
ers_free(npc->timer_event_ers, ted);
}
/*==========================================
* Get the tick value of an NPC timer
* If it's stopped, return stopped time
*------------------------------------------*/
static 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(timer->gettick(), nd->u.scr.timertick);
return tick;
}
/*==========================================
* Set tick for running and stopped timer
*------------------------------------------*/
static int npc_settimerevent_tick(struct npc_data *nd, int newtimer)
{
bool flag;
int old_rid;
//struct map_session_data *sd = NULL;
nullpo_ret(nd);
// TODO: Set player attached timer's tick.
old_rid = nd->u.scr.rid;
nd->u.scr.rid = 0;
// Check if timer is started
flag = (nd->u.scr.timerid != INVALID_TIMER || nd->u.scr.timertick);
if( flag ) npc->timerevent_stop(nd);
nd->u.scr.timer = newtimer;
if( flag ) npc->timerevent_start(nd, -1);
nd->u.scr.rid = old_rid;
return 0;
}
static int npc_event_sub(struct map_session_data *sd, struct event_data *ev, const char *eventname)
{
nullpo_retr(2, sd);
nullpo_retr(2, eventname);
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,EVENT_NAME_LENGTH); //Event enqueued.
return 0;
}
ShowWarning("npc_event: player's event queue is full, can't add event '%s' !\n", eventname);
return 1;
}
if( ev->nd->option&OPTION_INVISIBLE )
{
//Disabled npc, shouldn't trigger event.
npc->event_dequeue(sd);
return 2;
}
script->run_npc(ev->nd->u.scr.script,ev->pos,sd->bl.id,ev->nd->bl.id);
return 0;
}
/*==========================================
* NPC processing event type
*------------------------------------------*/
static int npc_event(struct map_session_data *sd, const char *eventname, int ontouch)
{
struct event_data* ev = (struct event_data*)strdb_get(npc->ev_db, eventname);
struct npc_data *nd;
nullpo_ret(sd);
if( ev == NULL || (nd = ev->nd) == NULL ) {
if( !ontouch )
ShowError("npc_event: event not found [%s]\n", eventname);
return ontouch;
}
switch(ontouch) {
case 1:
nd->touching_id = sd->bl.id;
sd->touching_id = nd->bl.id;
break;
case 2:
sd->areanpc_id = nd->bl.id;
break;
}
return npc->event_sub(sd,ev,eventname);
}
/**
* Executes OnTouch, OnUnTouch events when necessary. Also unsets `touching_id` of @p sd if not touching anymore.
* @remark @p sd and @p bl is expected to be not `NULL`
* @param sd map_session_data of BL_PC to check on
* @param x int of x-coordinate to check
* @param y int of y-coordinate to check
* @param check_if_warped set to true if you want to check if BL_PC has been warped by script
* @retval 0 Success
* @retval 1 only if check check_if_warped == true and OnTouch might have warped @p bl
* @retval -1 @p sd or @p bl is `NULL`
*/
static int npc_handle_touch_events(struct map_session_data *sd, int x, int y, bool check_if_warped)
{
nullpo_retr(-1, sd);
struct block_list *bl = &sd->bl;
nullpo_retr(-1, bl);
if (sd->touching_id != 0)
npc->touchnext_areanpc(sd, false);
if (map->getcell(bl->m, bl, x, y, CELL_CHKNPC) != 0) {
npc->touch_areanpc(sd, bl->m, x, y);
if (check_if_warped && bl->prev == NULL) // Script could have warped char
return 1;
} else {
npc->untouch_areanpc(sd, bl->m, x, y);
}
return 0;
}
/*==========================================
* Sub chk then execute area event type
*------------------------------------------*/
static 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 = map->id2sd(bl->id)));
pc_id = va_arg(ap,int);
name = va_arg(ap,char*);
if( sd->state.warping )
return 0;
if( pc_ishiding(sd) )
return 0;
if( pc_id == sd->bl.id )
return 0;
npc->event(sd,name,1);
return 1;
}
/*==========================================
* Chk if sd is still touching his assigned npc.
* If not, it unsets it and searches for another player in range.
*------------------------------------------*/
static int npc_touchnext_areanpc(struct map_session_data *sd, bool leavemap)
{
struct npc_data *nd;
short xs, ys;
nullpo_retr(1, sd);
nd = map->id2nd(sd->touching_id);
if( !nd || nd->touching_id != sd->bl.id )
return 1;
xs = nd->u.scr.xs;
ys = nd->u.scr.ys;
if( sd->bl.m != nd->bl.m ||
sd->bl.x < nd->bl.x - xs || sd->bl.x > nd->bl.x + xs ||
sd->bl.y < nd->bl.y - ys || sd->bl.y > nd->bl.y + ys ||
pc_ishiding(sd) || 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);
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);