forked from HerculesWS/Hercules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmob.c
6341 lines (5451 loc) · 198 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
/**
* This file is part of Hercules.
* http://herc.ws - http://github.com/HerculesWS/Hercules
*
* Copyright (C) 2012-2024 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" // 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 "map/achievement.h"
#include "common/HPM.h"
#include "common/cbasetypes.h"
#include "common/conf.h"
#include "common/db.h"
#include "common/ers.h"
#include "common/memmgr.h"
#include "common/msgtable.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>
static 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_CASTTIME (10 * 60 * 1000) // Maximum cast time for monster skills. (10 minutes)
#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 DBMap *item_drop_ratio_other_db = NULL;
static struct eri *item_drop_ers; //For loot drops delay structures.
static struct eri *item_drop_list_ers;
static 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];
}
static 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.
*------------------------------------------*/
static int mobdb_searchname(const char *str)
{
int i;
nullpo_ret(str);
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;
}
static int mobdb_searchname_array_sub(struct mob_db *monster, const char *str, int flag)
{
nullpo_ret(monster);
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
nullpo_ret(str);
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]
*------------------------------------------*/
/// Creates a timer to spawn a tomb
/// @param nd : The tomb
static void mvptomb_spawn_delayed(struct npc_data *nd)
{
nullpo_retv(nd);
if (nd->u.tomb.spawn_timer != INVALID_TIMER)
timer->delete(nd->u.tomb.spawn_timer, mob->mvptomb_delayspawn);
nd->u.tomb.spawn_timer = timer->add(timer->gettick() + battle_config.mvp_tomb_spawn_delay, mob->mvptomb_delayspawn, nd->bl.id, 0);
}
/// Spawns a tomb after the delay has ended
/// @param tid : Timer id
/// @param tick : current tick
/// @param id : NPC Id
/// @param data : 0
static int mvptomb_delayspawn(int tid, int64 tick, int id, intptr_t data)
{
struct npc_data *nd = map->id2nd(id);
if (nd == NULL)
return 0;
if (nd->u.tomb.spawn_timer != tid) {
ShowError("mvptomb_delay_spawn: Timer mismatch: %d != %d\n", tid, nd->u.tomb.spawn_timer);
return 0;
}
nd->u.tomb.spawn_timer = INVALID_TIMER;
// Sets view data to make the tomb visible and notifies client
status->set_viewdata(&nd->bl, nd->class_);
clif->spawn(&(nd->bl));
return 0;
}
static void mvptomb_create(struct mob_data *md, char *killer, time_t time)
{
struct npc_data *nd;
nullpo_retv(md);
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(MSGTBL_TOMB), sizeof(nd->name)); // "Tomb"
nd->u.tomb.md = md;
nd->u.tomb.kill_time = time;
nd->u.tomb.spawn_timer = INVALID_TIMER;
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);
// Tomb npc is created but not yet visible, we set view data and spawn it after some time
mob->mvptomb_spawn_delayed(nd);
}
static void mvptomb_destroy(struct mob_data *md)
{
struct npc_data *nd;
nullpo_retv(md);
if ( (nd = map->id2nd(md->tomb_nid)) ) {
int16 m = nd->bl.m;
struct s_mapiterator *iter = mapit_geteachpc();
for (struct map_session_data *sd = BL_UCAST(BL_PC, mapit->first(iter)); mapit->exists(iter); sd = BL_UCAST(BL_PC, mapit->next(iter))) {
if (sd->npc_id == nd->bl.id) {
sd->state.npc_unloaded = 1;
}
}
mapit->free(iter);
clif->clearunit_area(&nd->bl,CLR_OUTSIGHT);
map->delblock(&nd->bl);
int i = 0;
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;
}
if (nd->u.tomb.spawn_timer != INVALID_TIMER)
timer->delete(nd->u.tomb.spawn_timer, mob->mvptomb_delayspawn);
map->deliddb(&nd->bl);
aFree(nd);
}
md->tomb_nid = 0;
}
/*==========================================
* Founds up to N matches. Returns number of matches [Skotlex]
*------------------------------------------*/
static int mobdb_searchname_array(struct mob_db **data, int size, const char *str, int flag)
{
int count = 0, i;
struct mob_db* monster;
nullpo_ret(data);
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.
*------------------------------------------*/
static 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.
*------------------------------------------*/
static struct view_data *mob_get_viewdata(int class_)
{
if (mob->db(class_) == mob->dummy)
return 0;
return &mob->db(class_)->vd;
}
/**
* Create unique view data associated to a spawned monster.
* @param md: Mob to adjust
*/
static void mob_set_dynamic_viewdata(struct mob_data *md)
{
nullpo_retv(md);
// If it is a valid monster and it has not already been created
if (md->vd_changed)
return;
// Allocate a dynamic entry
struct view_data *vd = (struct view_data *)aMalloc(sizeof(struct view_data));
// Copy the current values
memcpy(vd, md->vd, sizeof(struct view_data));
// Update the pointer to the new entry
md->vd = vd;
// Flag it as changed so it is freed later on
md->vd_changed = true;
}
/**
* Free any view data associated to a spawned monster.
* @param md: Mob to free
*/
static void mob_free_dynamic_viewdata(struct mob_data *md)
{
nullpo_retv(md);
// If it is a valid monster and it has already been allocated
if (!md->vd_changed)
return;
// Free it
aFree(md->vd);
// Remove the reference
md->vd = NULL;
// Unflag it as changed
md->vd_changed = false;
}
/*==========================================
* Cleans up mob-spawn data to make it "valid"
*------------------------------------------*/
static int mob_parse_dataset(struct spawn_data *data)
{
size_t len;
nullpo_ret(data);
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, DEFAULT_MOB_NAME) == 0)
safestrncpy(data->name, mob->db(data->class_)->name, sizeof(data->name));
else if (strcmp(data->name, DEFAULT_MOB_JNAME) == 0)
safestrncpy(data->name, mob->db(data->class_)->jname, sizeof(data->name));
return 1;
}
/**
* Generates basic mob data by using the passed spawn data.
*
* @param data The mobs spawn data.
* @param npc_id If spawned by NPC script, this holds the ID of the invoking NPC.
* @return The generated mob data, later used to spawn the mob.
*
**/
static struct mob_data *mob_spawn_dataset(struct spawn_data *data, int npc_id)
{
nullpo_retr(NULL, data);
struct mob_data *md = (struct mob_data *)aCalloc(1, sizeof(struct mob_data));
memcpy(md->name, data->name, NAME_LENGTH);
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_);
md->npc_id = npc_id;
md->spawn_timer = INVALID_TIMER;
md->deletetimer = INVALID_TIMER;
md->skill_idx = -1;
if (data->level > 0 && data->level <= MAX_LEVEL)
md->level = data->level;
if (data->state.ai > 0)
md->special_state.ai = data->state.ai;
if (data->state.size > 0)
md->special_state.size = data->state.size;
if (data->eventname[0] != '\0' && strlen(data->eventname) >= 4)
memcpy(md->npc_event, data->eventname, 50);
if ((md->db->status.mode & MD_LOOTER) == MD_LOOTER)
md->lootitem = (struct item *)aCalloc(LOOTITEM_SIZE, sizeof(struct item));
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
*------------------------------------------*/
static int mob_get_random_id(enum mob_groups type, int flag, int lv)
{
Assert_ret(type >= 0 && type < MOBG_MAX_GROUP);
struct mob_db *monster;
int i = 0;
int class_ = 0;
do {
if (type) {
const int idx = rnd() % VECTOR_LENGTH(mob->mob_groups[type]);
class_ = VECTOR_INDEX(mob->mob_groups[type], idx);
} else {
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
class_ = MOBID_PORING;
return class_;
}
/*==========================================
* Kill Steal Protection [Zephyrus]
*------------------------------------------*/
static 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 (status->isdead(target) != 0)
return false; // Target is dead.
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, msg_sd(sd, MSGTBL_KS_WARNING_OWNER), pl_sd->status.name); // [KS Warning!! - Owner : %s]
clif_disp_onlyself(sd, output);
sd->ks_floodprotect_tick = tick + 2000;
}
// Message to Owner
if( DIFF_TICK(pl_sd->ks_floodprotect_tick, tick) <= 0 )
{
sprintf(output, msg_sd(pl_sd, MSGTBL_KS_WARNING_PLAYER), sd->status.name); // [Watch out! %s is trying to KS you!]
clif_disp_onlyself(pl_sd, 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, 0);
return false;
}
/**
* Prepares a mob's spawn data.
*
* @param bl The invoking character's block list.
* @param m The ID of the map where the mob should be spawned.
* @param x The x coordinate where the mob should be spawned.
* @param y The y coordinate where the mob should be spawned.
* @param mobname The mob's display name.
* @param class_ The mob's ID in database.
* @param event The name of the event which should be executed when the mob is killed.
* @param size The mob's size.
* @param ai The mob's AI.
* @param npc_id If spawned by NPC script, this holds the ID of the invoking NPC.
* @return The mob data generated by mob->spawn_dataset().
*
**/
static 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, int npc_id)
{
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 != NULL)
safestrncpy(data.name, mobname, sizeof(data.name));
else if (battle_config.override_mob_names == 1)
strcpy(data.name, DEFAULT_MOB_NAME);
else
strcpy(data.name, DEFAULT_MOB_JNAME);
if (event != NULL)
safestrncpy(data.eventname, event, sizeof(data.eventname));
/** Locate spot next to player. **/
if (bl != NULL && (x < 0 || y < 0))
map->search_free_cell(bl, m, &x, &y, 1, 1, SFC_DEFAULT);
/** 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_free_cell(NULL, m, &x, &y, -1, -1, SFC_XY_CENTER);
data.x = x;
data.y = y;
if (mob->parse_dataset(&data) == 0)
return NULL;
return mob->spawn_dataset(&data, npc_id);
}
/**
* Spawns a given amount of mobs.
*
* @param sd The invoking character.
* @param m The ID of the map where the mob should be spawned.
* @param x The x coordinate where the mob should be spawned.
* @param y The y coordinate where the mob should be spawned.
* @param mobname The mob's display name.
* @param class_ The mob's ID in database.
* @param amount The amount of mobs to spawn.
* @param event The name of the event which should be executed when the mob is killed.
* @param size The mob's size.
* @param ai The mob's AI.
* @return The last spawned mob's GID, or 0 if spawning failed.
*
**/
static 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)
{
bool no_guardian_data = false;
if (ai > 0 && (ai & 0x200) == 0x200) {
no_guardian_data = true;
ai &= ~0x200;
}
if (m < 0 || amount <= 0)
return 0;
struct mob_data *md = NULL;
for (int i = 0; i < amount; i++) {
int mob_id = class_;
if (mob_id < 0) {
mob_id = mob->get_random_id(-class_ - 1, (battle_config.random_monster_checklv == 1) ? 3 : 1,
(sd != NULL) ? sd->status.base_level : 255);
}
md = mob->once_spawn_sub((sd != NULL) ? &sd->bl : NULL, m, x, y, mobname, mob_id, event, size, ai,
(sd != NULL) ? sd->npc_id : 0);
if (md == NULL)
continue;
if (class_ == MOBID_EMPELIUM && !no_guardian_data) {
struct guild_castle *gc = guild->mapindex2gc(map_id2index(m));
if (gc != NULL) {
struct guild *g = guild->search(gc->guild_id);
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 != NULL)
md->guardian_data->g = g;
else if (gc->guild_id > 0) /// Guild not yet available, retry in 5s.
timer->add(timer->gettick() + 5000, mob->spawn_guardian_sub, md->bl.id,
gc->guild_id);
}
}
mob->spawn(md);
if (class_ < 0 && battle_config.dead_branch_active == 1) {
/// Behold Aegis' masterful decisions yet again...
/// "I understand the "Aggressive" part, but the "Can Move" and "Can Attack" is just stupid" [Poki]
sc_start4(NULL, &md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE|MD_CANATTACK|MD_CANMOVE|MD_ANGRY,
0, 60000, 0);
}
}
return (md != NULL) ? md->bl.id : 0; /// ID of last spawned mob.
}
/*==========================================
* Spawn mobs in the specified area.
*------------------------------------------*/
static 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.
* Required because the guild data may not be available at guardian spawn time.
*
* @param tid Required parameter for timer functions. Unused inside the function.
* @param tick Required parameter for timer functions. Unused inside the function.
* @param id The guardian mob's GID.
* @param data The guild ID.
* @return 1 on success, 0 on failure.
*
* @author Skotlex
*
**/
static int mob_spawn_guardian_sub(int tid, int64 tick, int id, intptr_t data)
{
struct block_list *bl = map->id2bl(id);
if (bl == NULL || bl->type != BL_MOB) /// It is possible mob was already removed from map when the castle has no owner. [Skotlex]
return 0;
struct mob_data *md = BL_UCAST(BL_MOB, bl);
if (md->guardian_data == NULL)
return 0;
struct guild *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_EMPELIUM) {
md->guardian_data->g = NULL;
if (md->guardian_data->castle->guild_id > 0) { /// Free castle up.
ShowNotice("mob_spawn_guardian_sub: 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->number >= 0 && md->guardian_data->number < MAX_GUARDIANS &&
md->guardian_data->castle->guardian[md->guardian_data->number].visible == 1)
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) > 0)
status_calc_mob(md, SCO_NONE); /// Give bonuses.
return 1;
}
/**
* Summons a castle guardian mob.
*
* @param mapname The name of the map where the guardian should be spawned.
* @param x The x coordinate where the guardian should be spawned.
* @param y The y coordinate where the guardian should be spawned.
* @param mobname The guardian's display name.
* @param class_ The guardian's mob ID in database.
* @param event The name of the event which should be executed when the guardian is killed.
* @param guardian The guardian's index.
* @param has_index If false, the guardian will be temporarily.
* @param npc_id If spawned by NPC script, this holds the ID of the invoking NPC.
* @return The spawned guardian's GID, or 0 if spawning failed.
*
* @author Valaris
*
**/
static int mob_spawn_guardian(const char *mapname, short x, short y, const char *mobname, int class_, const char *event,
int guardian, bool has_index, int npc_id)
{
nullpo_ret(mapname);
nullpo_ret(mobname);
nullpo_ret(event);
const int map_id = map->mapname2mapid(mapname);
if (map_id == INDEX_NOT_FOUND) {
ShowWarning("mob_spawn_guardian: Map [%s] not found.\n", mapname);
return 0;
}
if ((x <= 0 || y <= 0) && map->search_free_cell(NULL, map_id, &x, &y, -1, -1, SFC_XY_CENTER) != 0) {
ShowWarning("mob_spawn_guardian: Couldn't locate a spawn cell for guardian class %d (index %d) on castle map %s.\n",
class_, guardian, mapname);
return 0;
}
if (class_ <= 0 && (class_ = mob->get_random_id(-class_ - 1, 1, 99)) == 0)
return 0;
if (!has_index) {
guardian = -1;
} else if (guardian < 0 || guardian >= MAX_GUARDIANS) {
ShowError("mob_spawn_guardian: Invalid guardian index %d for guardian %d on castle map %s.\n",
guardian, class_, mapname);
return 0;
}
struct spawn_data data;
memset(&data, 0, sizeof(struct spawn_data));
data.num = 1;
data.m = map_id;
data.class_ = class_;
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) == 0)
return 0;
struct guild_castle *gc = guild->mapname2gc(mapname);
if (gc == NULL) {
ShowError("mob_spawn_guardian: No castle set on map %s.\n", mapname);
return 0;
}
struct guild *g = NULL;
if (gc->guild_id == 0)
ShowWarning("mob_spawn_guardian: Spawning guardian %d on a castle map %s with no guild.\n",
class_, mapname);
else
g = guild->search(gc->guild_id);
if (has_index && gc->guardian[guardian].id != 0) { /// Check if guardian already exists, refuse to spawn if so.
struct mob_data *md = map->id2md(gc->guardian[guardian].id);
if (md != NULL && md->guardian_data != NULL && md->guardian_data->number == guardian) {
ShowError("mob_spawn_guardian: Attempted to spawn guardian in position %d which already has a guardian on castle map %s.\n",
guardian, mapname);
return 0;
}
}
struct mob_data *md = mob->spawn_dataset(&data, npc_id);
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 != NULL)
md->guardian_data->g = g;
else if (gc->guild_id > 0)
timer->add(timer->gettick() + 5000, mob->spawn_guardian_sub, md->bl.id, gc->guild_id);
mob->spawn(md);
return md->bl.id;
}
/**
* Spawn a mob with allegiance to the given battle group.
*
* @param mapname The name of the map where the mob should be spawned.
* @param x The x coordinate where the mob should be spawned.
* @param y The y coordinate where the mob should be spawned.
* @param mobname The mob's display name.
* @param class_ The mob's mob ID in database.
* @param event The name of the event which should be executed when the mob is killed.
* @param bg_id The battle group ID.
* @param npc_id If spawned by NPC script, this holds the ID of the invoking NPC.
* @return The spawned mob's GID, or 0 if spawning failed.
*
* @author Zephyrus
*
**/
static int mob_spawn_bg(const char *mapname, short x, short y, const char *mobname, int class_, const char *event,
unsigned int bg_id, int npc_id)
{
nullpo_ret(mapname);
nullpo_ret(mobname);
nullpo_ret(event);
const int map_id = map->mapname2mapid(mapname);
if (map_id == INDEX_NOT_FOUND) {
ShowWarning("mob_spawn_bg: Map [%s] not found.\n", mapname);
return 0;
}
if ((x <= 0 || y <= 0) && map->search_free_cell(NULL, map_id, &x, &y, -1, -1, SFC_XY_CENTER) != 0) {
ShowWarning("mob_spawn_bg: Couldn't locate a spawn cell for guardian class %d (bg_id %u) on map %s.\n", class_, bg_id, mapname);
return 0;
}
if (class_ <= 0 && (class_ = mob->get_random_id(-class_ - 1, 1, 99)) == 0)
return 0;
struct spawn_data data;
memset(&data, 0, sizeof(struct spawn_data));
data.num = 1;
data.m = map_id;
data.class_ = class_;
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) == 0)
return 0;
struct mob_data *md = mob->spawn_dataset(&data, npc_id);
mob->spawn(md);
md->bg_id = bg_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
*------------------------------------------*/
static 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: