-
Notifications
You must be signed in to change notification settings - Fork 756
/
mob.c
5103 lines (4497 loc) · 158 KB
/
mob.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
// Copyright (c) Hercules Dev Team, licensed under GNU GPL.
// See the LICENSE file
// Portions Copyright (c) Athena Dev Teams
#define HERCULES_CORE
#include "config/core.h" // AUTOLOOT_DISTANCE, DBPATH, DEFTYPE_MAX, DEFTYPE_MIN, RENEWAL_DROP, RENEWAL_EXP
#include "mob.h"
#include "map/atcommand.h"
#include "map/battle.h"
#include "map/clif.h"
#include "map/date.h"
#include "map/elemental.h"
#include "map/guild.h"
#include "map/homunculus.h"
#include "map/intif.h"
#include "map/itemdb.h"
#include "map/log.h"
#include "map/map.h"
#include "map/mercenary.h"
#include "map/npc.h"
#include "map/party.h"
#include "map/path.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 "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/random.h"
#include "common/showmsg.h"
#include "common/socket.h"
#include "common/strlib.h"
#include "common/timer.h"
#include "common/utils.h"
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct mob_interface mob_s;
struct mob_interface *mob;
#define ACTIVE_AI_RANGE 2 //Distance added on top of 'AREA_SIZE' at which mobs enter active AI mode.
#define IDLE_SKILL_INTERVAL 10 //Active idle skills should be triggered every 1 second (1000/MIN_MOBTHINKTIME)
// Probability for mobs far from players from doing their IDLE skill. (rate of 1000 minute)
// in Aegis, this is 100% for mobs that have been activated by players and none otherwise.
#define MOB_LAZYSKILLPERC(md) (md->state.spotted?1000:0)
// Move probability for mobs away from players (rate of 1000 minute)
// in Aegis, this is 100% for mobs that have been activated by players and none otherwise.
#define MOB_LAZYMOVEPERC(md) ((md)->state.spotted?1000:0)
#define MOB_MAX_DELAY (24*3600*1000)
#define MAX_MINCHASE 30 //Max minimum chase value to use for mobs.
#define RUDE_ATTACKED_COUNT 2 //After how many rude-attacks should the skill be used?
//Dynamic item drop ratio database for per-item drop ratio modifiers overriding global drop ratios.
#define MAX_ITEMRATIO_MOBS 10
struct item_drop_ratio {
int drop_ratio;
int mob_id[MAX_ITEMRATIO_MOBS];
};
static struct item_drop_ratio *item_drop_ratio_db[MAX_ITEMDB];
static struct eri *item_drop_ers; //For loot drops delay structures.
static struct eri *item_drop_list_ers;
static struct {
int qty;
int class_[350];
} summon[MAX_RANDOMMONSTER];
struct mob_db *mob_db(int index) {
if (index < 0 || index > MAX_MOB_DB || mob->db_data[index] == NULL)
return mob->dummy;
return mob->db_data[index];
}
struct mob_chat *mob_chat(short id) {
if(id <= 0 || id > MAX_MOB_CHAT || mob->chat_db[id] == NULL)
return NULL;
return mob->chat_db[id];
}
/*==========================================
* Mob is searched with a name.
*------------------------------------------*/
int mobdb_searchname(const char *str)
{
int i;
for(i=0;i<=MAX_MOB_DB;i++){
struct mob_db *monster = mob->db(i);
if(monster == mob->dummy) //Skip dummy mobs.
continue;
if(strcmpi(monster->name,str)==0 || strcmpi(monster->jname,str)==0)
return i;
if(battle_config.case_sensitive_aegisnames && strcmp(monster->sprite,str)==0)
return i;
if(!battle_config.case_sensitive_aegisnames && strcasecmp(monster->sprite,str)==0)
return i;
}
return 0;
}
int mobdb_searchname_array_sub(struct mob_db* monster, const char *str, int flag) {
if (monster == mob->dummy)
return 1;
if(!monster->base_exp && !monster->job_exp && monster->spawn[0].qty < 1)
return 1; // Monsters with no base/job exp and no spawn point are, by this criteria, considered "slave mobs" and excluded from search results
if( !flag ) {
if(stristr(monster->jname,str))
return 0;
if(stristr(monster->name,str))
return 0;
} else {
if(strcmpi(monster->jname,str) == 0)
return 0;
if(strcmpi(monster->name,str) == 0)
return 0;
}
if (battle_config.case_sensitive_aegisnames)
return strcmp(monster->sprite,str);
return strcasecmp(monster->sprite,str);
}
/*==========================================
* MvP Tomb [GreenBox]
*------------------------------------------*/
void mvptomb_create(struct mob_data *md, char *killer, time_t time)
{
struct npc_data *nd;
if ( md->tomb_nid )
mob->mvptomb_destroy(md);
nd = npc->create_npc(TOMB, md->bl.m, md->bl.x, md->bl.y, md->ud.dir, MOB_TOMB);
md->tomb_nid = nd->bl.id;
safestrncpy(nd->name, msg_txt(856), sizeof(nd->name)); // "Tomb"
nd->u.tomb.md = md;
nd->u.tomb.kill_time = time;
if (killer)
safestrncpy(nd->u.tomb.killer_name, killer, NAME_LENGTH);
else
nd->u.tomb.killer_name[0] = '\0';
map->addnpc(nd->bl.m, nd);
map->addblock(&nd->bl);
status->set_viewdata(&nd->bl, nd->class_);
clif->spawn(&nd->bl);
}
void mvptomb_destroy(struct mob_data *md) {
struct npc_data *nd;
if ( (nd = map->id2nd(md->tomb_nid)) ) {
int16 m, i;
m = nd->bl.m;
clif->clearunit_area(&nd->bl,CLR_OUTSIGHT);
map->delblock(&nd->bl);
ARR_FIND( 0, map->list[m].npc_num, i, map->list[m].npc[i] == nd );
if( !(i == map->list[m].npc_num) ) {
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;
}
map->deliddb(&nd->bl);
aFree(nd);
}
md->tomb_nid = 0;
}
/*==========================================
* Founds up to N matches. Returns number of matches [Skotlex]
*------------------------------------------*/
int mobdb_searchname_array(struct mob_db** data, int size, const char *str, int flag)
{
int count = 0, i;
struct mob_db* monster;
for(i=0;i<=MAX_MOB_DB;i++){
monster = mob->db(i);
if (monster == mob->dummy || mob->is_clone(i) ) //keep clones out (or you leak player stats)
continue;
if (!mob->db_searchname_array_sub(monster, str, flag)) {
if (count < size)
data[count] = monster;
count++;
}
}
return count;
}
/*==========================================
* Id Mob is checked.
*------------------------------------------*/
int mobdb_checkid(const int id)
{
if (mob->db(id) == mob->dummy)
return 0;
if (mob->is_clone(id)) //checkid is used mostly for random ID based code, therefore clone mobs are out of the question.
return 0;
return id;
}
/*==========================================
* Returns the view data associated to this mob class.
*------------------------------------------*/
struct view_data * mob_get_viewdata(int class_)
{
if (mob->db(class_) == mob->dummy)
return 0;
return &mob->db(class_)->vd;
}
/*==========================================
* Cleans up mob-spawn data to make it "valid"
*------------------------------------------*/
int mob_parse_dataset(struct spawn_data *data)
{
size_t len;
if ((!mob->db_checkid(data->class_) && !mob->is_clone(data->class_)) || !data->num)
return 0;
if( ( len = strlen(data->eventname) ) > 0 )
{
if( data->eventname[len-1] == '"' )
data->eventname[len-1] = '\0'; //Remove trailing quote.
if( data->eventname[0] == '"' ) //Strip leading quotes
memmove(data->eventname, data->eventname+1, len-1);
}
if(strcmp(data->name,"--en--")==0)
safestrncpy(data->name, mob->db(data->class_)->name, sizeof(data->name));
else if(strcmp(data->name,"--ja--")==0)
safestrncpy(data->name, mob->db(data->class_)->jname, sizeof(data->name));
return 1;
}
/*==========================================
* Generates the basic mob data using the spawn_data provided.
*------------------------------------------*/
struct mob_data* mob_spawn_dataset(struct spawn_data *data) {
struct mob_data *md = (struct mob_data*)aCalloc(1, sizeof(struct mob_data));
md->bl.id= npc->get_new_npc_id();
md->bl.type = BL_MOB;
md->bl.m = data->m;
md->bl.x = data->x;
md->bl.y = data->y;
md->class_ = data->class_;
md->state.boss = data->state.boss;
md->db = mob->db(md->class_);
if (data->level > 0 && data->level <= MAX_LEVEL)
md->level = data->level;
memcpy(md->name, data->name, NAME_LENGTH);
if (data->state.ai)
md->special_state.ai = data->state.ai;
if (data->state.size)
md->special_state.size = data->state.size;
if (data->eventname[0] && strlen(data->eventname) >= 4)
memcpy(md->npc_event, data->eventname, 50);
if(md->db->status.mode&MD_LOOTER)
md->lootitem = (struct item *)aCalloc(LOOTITEM_SIZE,sizeof(struct item));
md->spawn_timer = INVALID_TIMER;
md->deletetimer = INVALID_TIMER;
md->skill_idx = -1;
status->set_viewdata(&md->bl, md->class_);
status->change_init(&md->bl);
unit->dataset(&md->bl);
map->addiddb(&md->bl);
return md;
}
/*==========================================
* Fetches a random mob_id [Skotlex]
* type: Where to fetch from:
* 0: dead branch list
* 1: poring list
* 2: bloody branch list
* flag:
* &1: Apply the summon success chance found in the list (otherwise get any monster from the db)
* &2: Apply a monster check level.
* &4: Selected monster should not be a boss type
* &8: Selected monster must have normal spawn.
* lv: Mob level to check against
*------------------------------------------*/
int mob_get_random_id(int type, int flag, int lv)
{
struct mob_db *monster;
int i=0, class_;
if(type < 0 || type >= MAX_RANDOMMONSTER) {
ShowError("mob_get_random_id: Invalid type (%d) of random monster.\n", type);
return 0;
}
do {
if (type)
class_ = summon[type].class_[rnd()%summon[type].qty];
else //Dead branch
class_ = rnd() % MAX_MOB_DB;
monster = mob->db(class_);
} while ((monster == mob->dummy ||
mob->is_clone(class_) ||
(flag&1 && monster->summonper[type] <= rnd() % 1000000) ||
(flag&2 && lv < monster->lv) ||
(flag&4 && monster->status.mode&MD_BOSS) ||
(flag&8 && monster->spawn[0].qty < 1)
) && (i++) < MAX_MOB_DB);
if(i >= MAX_MOB_DB) // no suitable monster found, use fallback for given list
class_ = mob->db_data[0]->summonper[type];
return class_;
}
/*==========================================
* Kill Steal Protection [Zephyrus]
*------------------------------------------*/
bool mob_ksprotected(struct block_list *src, struct block_list *target) {
struct block_list *s_bl, *t_bl;
struct map_session_data
*sd, // Source
*pl_sd, // Owner
*t_sd; // Mob Target
struct mob_data *md;
int64 tick = timer->gettick();
char output[128];
if( !battle_config.ksprotection )
return false; // KS Protection Disabled
if( !(md = BL_CAST(BL_MOB,target)) )
return false; // Target is not MOB
if( (s_bl = battle->get_master(src)) == NULL )
s_bl = src;
if( !(sd = BL_CAST(BL_PC,s_bl)) )
return false; // Master is not PC
t_bl = map->id2bl(md->target_id);
if( !t_bl || (s_bl = battle->get_master(t_bl)) == NULL )
s_bl = t_bl;
t_sd = BL_CAST(BL_PC,s_bl);
do {
struct status_change_entry *sce;
if( map->list[md->bl.m].flag.allowks || map_flag_ks(md->bl.m) )
return false; // Ignores GVG, PVP and AllowKS map flags
if( md->db->mexp || md->master_id )
return false; // MVP, Slaves mobs ignores KS
if( (sce = md->sc.data[SC_KSPROTECTED]) == NULL )
break; // No KS Protected
if( sd->bl.id == sce->val1 || // Same Owner
(sce->val2 == KSPROTECT_PARTY && sd->status.party_id && sd->status.party_id == sce->val3) || // Party KS allowed
(sce->val2 == KSPROTECT_GUILD && sd->status.guild_id && sd->status.guild_id == sce->val4) ) // Guild KS allowed
break;
if( t_sd && (
(sce->val2 == KSPROTECT_SELF && sce->val1 != t_sd->bl.id) ||
(sce->val2 == KSPROTECT_PARTY && sce->val3 && sce->val3 != t_sd->status.party_id) ||
(sce->val2 == KSPROTECT_GUILD && sce->val4 && sce->val4 != t_sd->status.guild_id)) )
break;
if( (pl_sd = map->id2sd(sce->val1)) == NULL || pl_sd->bl.m != md->bl.m )
break;
if( !pl_sd->state.noks )
return false; // No KS Protected, but normal players should be protected too
// Message to KS
if( DIFF_TICK(sd->ks_floodprotect_tick, tick) <= 0 )
{
sprintf(output, "[KS Warning!! - Owner : %s]", pl_sd->status.name);
clif_disp_onlyself(sd, output, strlen(output));
sd->ks_floodprotect_tick = tick + 2000;
}
// Message to Owner
if( DIFF_TICK(pl_sd->ks_floodprotect_tick, tick) <= 0 )
{
sprintf(output, "[Watch out! %s is trying to KS you!]", sd->status.name);
clif_disp_onlyself(pl_sd, output, strlen(output));
pl_sd->ks_floodprotect_tick = tick + 2000;
}
return true;
} while(0);
status->change_start(NULL, target, SC_KSPROTECTED, 10000, sd->bl.id, sd->state.noks,
sd->status.party_id, sd->status.guild_id, battle_config.ksprotection, SCFLAG_NONE);
return false;
}
struct mob_data *mob_once_spawn_sub(struct block_list *bl, int16 m, int16 x, int16 y, const char *mobname, int class_, const char *event, unsigned int size, unsigned int ai)
{
struct spawn_data data;
memset(&data, 0, sizeof(struct spawn_data));
data.m = m;
data.num = 1;
data.class_ = class_;
data.state.size = size;
data.state.ai = ai;
if (mobname)
safestrncpy(data.name, mobname, sizeof(data.name));
else
if (battle_config.override_mob_names == 1)
strcpy(data.name, "--en--");
else
strcpy(data.name, "--ja--");
if (event)
safestrncpy(data.eventname, event, sizeof(data.eventname));
// Locate spot next to player.
if (bl && (x < 0 || y < 0))
map->search_freecell(bl, m, &x, &y, 1, 1, 0);
// if none found, pick random position on map
if (x <= 0 || x >= map->list[m].xs || y <= 0 || y >= map->list[m].ys)
map->search_freecell(NULL, m, &x, &y, -1, -1, 1);
data.x = x;
data.y = y;
if (!mob->parse_dataset(&data))
return NULL;
return mob->spawn_dataset(&data);
}
/*==========================================
* Spawn a single mob on the specified coordinates.
*------------------------------------------*/
int mob_once_spawn(struct map_session_data* sd, int16 m, int16 x, int16 y, const char* mobname, int class_, int amount, const char* event, unsigned int size, unsigned int ai) {
struct mob_data* md = NULL;
int count, lv;
bool no_guardian_data = false;
if( ai && ai&0x200 ) {
no_guardian_data = true;
ai &=~ 0x200;
}
if (m < 0 || amount <= 0)
return 0; // invalid input
lv = (sd) ? sd->status.base_level : 255;
for (count = 0; count < amount; count++) {
int c = (class_ >= 0) ? class_ : mob->get_random_id(-class_ - 1, (battle_config.random_monster_checklv) ? 3 : 1, lv);
md = mob->once_spawn_sub((sd) ? &sd->bl : NULL, m, x, y, mobname, c, event, size, ai);
if (!md)
continue;
if ( class_ == MOBID_EMPERIUM && !no_guardian_data ) {
struct guild_castle* gc = guild->mapindex2gc(map_id2index(m));
struct guild* g = (gc) ? guild->search(gc->guild_id) : NULL;
if( gc ) {
md->guardian_data = (struct guardian_data*)aCalloc(1, sizeof(struct guardian_data));
md->guardian_data->castle = gc;
md->guardian_data->number = MAX_GUARDIANS;
if( g )
md->guardian_data->g = g;
else if( gc->guild_id ) //Guild not yet available, retry in 5.
timer->add(timer->gettick()+5000,mob->spawn_guardian_sub,md->bl.id,gc->guild_id);
}
} // end addition [Valaris]
mob->spawn(md);
if (class_ < 0 && battle_config.dead_branch_active) {
//Behold Aegis's masterful decisions yet again...
//"I understand the "Aggressive" part, but the "Can Move" and "Can Attack" is just stupid" - Poki#3
sc_start4(NULL, &md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE|MD_CANATTACK|MD_CANMOVE|MD_ANGRY, 0, 60000);
}
}
return (md) ? md->bl.id : 0; // id of last spawned mob
}
/*==========================================
* Spawn mobs in the specified area.
*------------------------------------------*/
int mob_once_spawn_area(struct map_session_data* sd, int16 m, int16 x0, int16 y0, int16 x1, int16 y1, const char* mobname, int class_, int amount, const char* event, unsigned int size, unsigned int ai)
{
int i, max, id = 0;
int lx = -1, ly = -1;
if (m < 0 || amount <= 0)
return 0; // invalid input
// normalize x/y coordinates
if (x0 > x1)
swap(x0, x1);
if (y0 > y1)
swap(y0, y1);
// choose a suitable max. number of attempts
max = (y1 - y0 + 1)*(x1 - x0 + 1)*3;
if (max > 1000)
max = 1000;
// spawn mobs, one by one
for (i = 0; i < amount; i++)
{
int x, y;
int j = 0;
// find a suitable map cell
do {
x = rnd()%(x1-x0+1)+x0;
y = rnd()%(y1-y0+1)+y0;
j++;
} while (map->getcell(m, NULL, x, y, CELL_CHKNOPASS) && j < max);
if (j == max)
{// attempt to find an available cell failed
if (lx == -1 && ly == -1)
return 0; // total failure
// fallback to last good x/y pair
x = lx;
y = ly;
}
// record last successful coordinates
lx = x;
ly = y;
id = mob->once_spawn(sd, m, x, y, mobname, class_, 1, event, size, ai);
}
return id; // id of last spawned mob
}
/**
* Sets a guardian's guild data and liberates castle if couldn't retrieve guild data
* @param data (int)guild_id
* @retval Always 0
* @author Skotlex
**/
int mob_spawn_guardian_sub(int tid, int64 tick, int id, intptr_t data) {
//Needed because the guild data may not be available at guardian spawn time.
struct block_list* bl = map->id2bl(id);
struct mob_data* md;
struct guild* g;
if( bl == NULL ) //It is possible mob was already removed from map when the castle has no owner. [Skotlex]
return 0;
if( bl->type != BL_MOB ) {
ShowError("mob_spawn_guardian_sub: Block error!\n");
return 0;
}
md = (struct mob_data*)bl;
nullpo_ret(md->guardian_data);
g = guild->search((int)data);
if( g == NULL ) { //Liberate castle, if the guild is not found this is an error! [Skotlex]
ShowError("mob_spawn_guardian_sub: Couldn't load guild %d!\n", (int)data);
//Not sure this is the best way, but otherwise we'd be invoking this for ALL guardians spawned later on.
if( md->class_ == MOBID_EMPERIUM && md->guardian_data ) {
md->guardian_data->g = NULL;
if( md->guardian_data->castle->guild_id ) {//Free castle up.
ShowNotice("Clearing ownership of castle %d (%s)\n", md->guardian_data->castle->castle_id, md->guardian_data->castle->castle_name);
guild->castledatasave(md->guardian_data->castle->castle_id, 1, 0);
}
} else {
if( md->guardian_data && md->guardian_data->number >= 0 && md->guardian_data->number < MAX_GUARDIANS
&& md->guardian_data->castle->guardian[md->guardian_data->number].visible )
guild->castledatasave(md->guardian_data->castle->castle_id, 10+md->guardian_data->number,0);
unit->free(&md->bl,CLR_OUTSIGHT); // Remove guardian.
}
return 0;
}
if( guild->checkskill(g,GD_GUARDUP) )
status_calc_mob(md, SCO_NONE); // Give bonuses.
return 0;
}
/*==========================================
* Summoning Guardians [Valaris]
*------------------------------------------*/
int mob_spawn_guardian(const char* mapname, short x, short y, const char* mobname, int class_, const char* event, int guardian, bool has_index)
{
struct mob_data *md=NULL;
struct spawn_data data;
struct guild *g=NULL;
struct guild_castle *gc;
int16 m;
memset(&data, 0, sizeof(struct spawn_data));
data.num = 1;
m=map->mapname2mapid(mapname);
if(m<0)
{
ShowWarning("mob_spawn_guardian: Map [%s] not found.\n", mapname);
return 0;
}
data.m = m;
data.num = 1;
if(class_<=0) {
class_ = mob->get_random_id(-class_-1, 1, 99);
if (!class_) return 0;
}
data.class_ = class_;
if( !has_index ) {
guardian = -1;
} else if( guardian < 0 || guardian >= MAX_GUARDIANS ) {
ShowError("mob_spawn_guardian: Invalid guardian index %d for guardian %d (castle map %s)\n", guardian, class_, map->list[m].name);
return 0;
}
if((x<=0 || y<=0) && !map->search_freecell(NULL, m, &x, &y, -1,-1, 1)) {
ShowWarning("mob_spawn_guardian: Couldn't locate a spawn cell for guardian class %d (index %d) at castle map %s\n",class_, guardian, map->list[m].name);
return 0;
}
data.x = x;
data.y = y;
safestrncpy(data.name, mobname, sizeof(data.name));
safestrncpy(data.eventname, event, sizeof(data.eventname));
if (!mob->parse_dataset(&data))
return 0;
gc=guild->mapname2gc(map->list[m].name);
if (gc == NULL) {
ShowError("mob_spawn_guardian: No castle set at map %s\n", map->list[m].name);
return 0;
}
if (!gc->guild_id)
ShowWarning("mob_spawn_guardian: Spawning guardian %d on a castle with no guild (castle map %s)\n", class_, map->list[m].name);
else
g = guild->search(gc->guild_id);
if( has_index && gc->guardian[guardian].id ) {
//Check if guardian already exists, refuse to spawn if so.
struct mob_data *md2 = (TBL_MOB*)map->id2bl(gc->guardian[guardian].id);
if (md2 && md2->bl.type == BL_MOB
&& md2->guardian_data
&& md2->guardian_data->number == guardian
) {
ShowError("mob_spawn_guardian: Attempted to spawn guardian in position %d which already has a guardian (castle map %s)\n", guardian, map->list[m].name);
return 0;
}
}
md = mob->spawn_dataset(&data);
md->guardian_data = (struct guardian_data*)aCalloc(1, sizeof(struct guardian_data));
md->guardian_data->number = guardian;
md->guardian_data->castle = gc;
if( has_index )
{// permanent guardian
gc->guardian[guardian].id = md->bl.id;
}
else
{// temporary guardian
int i;
ARR_FIND(0, gc->temp_guardians_max, i, gc->temp_guardians[i] == 0);
if( i == gc->temp_guardians_max )
{
++(gc->temp_guardians_max);
RECREATE(gc->temp_guardians, int, gc->temp_guardians_max);
}
gc->temp_guardians[i] = md->bl.id;
}
if( g )
md->guardian_data->g = g;
else if( gc->guild_id )
timer->add(timer->gettick()+5000,mob->spawn_guardian_sub,md->bl.id,gc->guild_id);
mob->spawn(md);
return md->bl.id;
}
/*==========================================
* Summoning BattleGround [Zephyrus]
*------------------------------------------*/
int mob_spawn_bg(const char* mapname, short x, short y, const char* mobname, int class_, const char* event, unsigned int bg_id)
{
struct mob_data *md = NULL;
struct spawn_data data;
int16 m;
if( (m = map->mapname2mapid(mapname)) < 0 ) {
ShowWarning("mob_spawn_bg: Map [%s] not found.\n", mapname);
return 0;
}
memset(&data, 0, sizeof(struct spawn_data));
data.m = m;
data.num = 1;
if( class_ <= 0 )
{
class_ = mob->get_random_id(-class_-1,1,99);
if( !class_ ) return 0;
}
data.class_ = class_;
if( (x <= 0 || y <= 0) && !map->search_freecell(NULL, m, &x, &y, -1,-1, 1) ) {
ShowWarning("mob_spawn_bg: Couldn't locate a spawn cell for guardian class %d (bg_id %d) at map %s\n",class_, bg_id, map->list[m].name);
return 0;
}
data.x = x;
data.y = y;
safestrncpy(data.name, mobname, sizeof(data.name));
safestrncpy(data.eventname, event, sizeof(data.eventname));
if( !mob->parse_dataset(&data) )
return 0;
md = mob->spawn_dataset(&data);
mob->spawn(md);
md->bg_id = bg_id; // BG Team ID
return md->bl.id;
}
/*==========================================
* Reachability to a Specification ID existence place
* state indicates type of 'seek' mob should do:
* - MSS_LOOT: Looking for item, path must be easy.
* - MSS_RUSH: Chasing attacking player, path is complex
* - MSS_FOLLOW: Initiative/support seek, path is complex
*------------------------------------------*/
int mob_can_reach(struct mob_data *md,struct block_list *bl,int range, int state)
{
int easy = 0;
nullpo_ret(md);
nullpo_ret(bl);
switch (state) {
case MSS_RUSH:
case MSS_FOLLOW:
easy = 0; //(battle_config.mob_ai&0x1?0:1);
break;
case MSS_LOOT:
default:
easy = 1;
break;
}
return unit->can_reach_bl(&md->bl, bl, range, easy, NULL, NULL);
}
/*==========================================
* Links nearby mobs (supportive mobs)
*------------------------------------------*/
int mob_linksearch(struct block_list *bl,va_list ap) {
struct mob_data *md;
int class_;
struct block_list *target;
int64 tick;
nullpo_ret(bl);
md=(struct mob_data *)bl;
class_ = va_arg(ap, int);
target = va_arg(ap, struct block_list *);
tick = va_arg(ap, int64);
if (md->class_ == class_ && DIFF_TICK(md->last_linktime, tick) < MIN_MOBLINKTIME
&& !md->target_id)
{
md->last_linktime = tick;
if (mob->can_reach(md,target,md->db->range2, MSS_FOLLOW)) {
// Reachability judging
md->target_id = target->id;
md->min_chase=md->db->range3;
return 1;
}
}
return 0;
}
/*==========================================
* mob spawn with delay (timer function)
*------------------------------------------*/
int mob_delayspawn(int tid, int64 tick, int id, intptr_t data) {
struct block_list* bl = map->id2bl(id);
struct mob_data* md = BL_CAST(BL_MOB, bl);
if( md )
{
if( md->spawn_timer != tid )
{
ShowError("mob_delayspawn: Timer mismatch: %d != %d\n", tid, md->spawn_timer);
return 0;
}
md->spawn_timer = INVALID_TIMER;
mob->spawn(md);
}
return 0;
}
/*==========================================
* spawn timing calculation
*------------------------------------------*/
int mob_setdelayspawn(struct mob_data *md)
{
unsigned int spawntime, mode;
struct mob_db *db;
if (!md->spawn) //Doesn't has respawn data!
return unit->free(&md->bl,CLR_DEAD);
spawntime = md->spawn->delay1; //Base respawn time
if (md->spawn->delay2) //random variance
spawntime+= rnd()%md->spawn->delay2;
//Apply the spawn delay fix [Skotlex]
db = mob->db(md->spawn->class_);
mode = db->status.mode;
if (mode & MD_BOSS) {
//Bosses
if (battle_config.boss_spawn_delay != 100) {
// Divide by 100 first to prevent overflows
//(precision loss is minimal as duration is in ms already)
spawntime = spawntime/100*battle_config.boss_spawn_delay;
}
} else if (mode&MD_PLANT) {
//Plants
if (battle_config.plant_spawn_delay != 100) {
spawntime = spawntime/100*battle_config.plant_spawn_delay;
}
} else if (battle_config.mob_spawn_delay != 100) {
//Normal mobs
spawntime = spawntime/100*battle_config.mob_spawn_delay;
}
if (spawntime < 5000) //Monsters should never respawn faster than within 5 seconds
spawntime = 5000;
if( md->spawn_timer != INVALID_TIMER )
timer->delete(md->spawn_timer, mob->delayspawn);
md->spawn_timer = timer->add(timer->gettick()+spawntime, mob->delayspawn, md->bl.id, 0);
return 0;
}
int mob_count_sub(struct block_list *bl, va_list ap) {
int mobid[10] = { 0 }, i;
ARR_FIND(0, 10, i, (mobid[i] = va_arg(ap, int)) == 0); //fetch till 0
if (mobid[0]) { //if there one let's check it otherwise go backward
TBL_MOB *md = BL_CAST(BL_MOB, bl);
nullpo_ret(md);
ARR_FIND(0, 10, i, md->class_ == mobid[i]);
return (i < 10) ? 1 : 0;
}
return 1; //backward compatibility
}
/*==========================================
* Mob spawning. Initialization is also variously here.
*------------------------------------------*/
int mob_spawn (struct mob_data *md)
{
int i=0;
int64 tick = timer->gettick();
int64 c = 0;
md->last_thinktime = tick;
if (md->bl.prev != NULL)
unit->remove_map(&md->bl,CLR_RESPAWN,ALC_MARK);
else if (md->spawn && md->class_ != md->spawn->class_) {
md->class_ = md->spawn->class_;
status->set_viewdata(&md->bl, md->class_);
md->db = mob->db(md->class_);
memcpy(md->name,md->spawn->name,NAME_LENGTH);
}
if (md->spawn) { //Respawn data
md->bl.m = md->spawn->m;
md->bl.x = md->spawn->x;
md->bl.y = md->spawn->y;
if( (md->bl.x == 0 && md->bl.y == 0) || md->spawn->xs || md->spawn->ys ) {
//Monster can be spawned on an area.
if( !map->search_freecell(&md->bl, -1, &md->bl.x, &md->bl.y, md->spawn->xs, md->spawn->ys, battle_config.no_spawn_on_player?4:0) ) {
// retry again later
if( md->spawn_timer != INVALID_TIMER )
timer->delete(md->spawn_timer, mob->delayspawn);
md->spawn_timer = timer->add(tick+5000,mob->delayspawn,md->bl.id,0);
return 1;
}
} else if( battle_config.no_spawn_on_player > 99 && map->foreachinrange(mob->count_sub, &md->bl, AREA_SIZE, BL_PC) ) {
// retry again later (players on sight)
if( md->spawn_timer != INVALID_TIMER )
timer->delete(md->spawn_timer, mob->delayspawn);
md->spawn_timer = timer->add(tick+5000,mob->delayspawn,md->bl.id,0);
return 1;
}
}
memset(&md->state, 0, sizeof(md->state));
status_calc_mob(md, SCO_FIRST);
md->attacked_id = 0;
md->target_id = 0;
md->move_fail_count = 0;
md->ud.state.attack_continue = 0;
md->ud.target_to = 0;
md->ud.dir = 0;
if( md->spawn_timer != INVALID_TIMER )
{
timer->delete(md->spawn_timer, mob->delayspawn);
md->spawn_timer = INVALID_TIMER;
}
//md->master_id = 0;
md->master_dist = 0;
md->state.aggressive = (md->status.mode&MD_ANGRY) ? 1 : 0;
md->state.skillstate = MSS_IDLE;
md->next_walktime = tick+rnd()%1000+MIN_RANDOMWALKTIME;
md->last_linktime = tick;
md->dmgtick = tick - 5000;
md->last_pcneartime = 0;
for (i = 0, c = tick-MOB_MAX_DELAY; i < MAX_MOBSKILL; i++)
md->skilldelay[i] = c;
memset(md->dmglog, 0, sizeof(md->dmglog));
md->tdmg = 0;
if (md->lootitem)
memset(md->lootitem, 0, sizeof(*md->lootitem));
md->lootitem_count = 0;
if(md->db->option)
// Added for carts, falcons and pecos for cloned monsters. [Valaris]
md->sc.option = md->db->option;
// MvP tomb [GreenBox]
if ( md->tomb_nid )
mob->mvptomb_destroy(md);
map->addblock(&md->bl);
if( map->list[md->bl.m].users )
clif->spawn(&md->bl);
skill->unit_move(&md->bl,tick,1);
mob->skill_use(md, tick, MSC_SPAWN);
return 0;
}
/*==========================================
* Determines if the mob can change target. [Skotlex]
*------------------------------------------*/
int mob_can_changetarget(struct mob_data* md, struct block_list* target, int mode)
{
// if the monster was provoked ignore the above rule [celest]
if(md->state.provoke_flag)
{
if (md->state.provoke_flag == target->id)
return 1;
else if (!(battle_config.mob_ai&0x4))
return 0;
}
switch (md->state.skillstate) {
case MSS_BERSERK:
if (!(mode&MD_CHANGETARGET_MELEE))
return 0;
return (battle_config.mob_ai&0x4 || check_distance_bl(&md->bl, target, 3));
case MSS_RUSH:
return (mode&MD_CHANGETARGET_CHASE);
case MSS_FOLLOW:
case MSS_ANGRY:
case MSS_IDLE: