/
mob.c
5254 lines (4599 loc) · 159 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) Athena Dev Teams - Licensed under GNU GPL
// For more information, see LICENCE in the main folder
#include "../common/cbasetypes.h"
#include "../common/timer.h"
#include "../common/db.h"
#include "../common/nullpo.h"
#include "../common/malloc.h"
#include "../common/showmsg.h"
#include "../common/ers.h"
#include "../common/random.h"
#include "../common/strlib.h"
#include "../common/utils.h"
#include "../common/socket.h"
#include "map.h"
#include "path.h"
#include "channel.h"
#include "clif.h"
#include "intif.h"
#include "pc.h"
#include "pet.h"
#include "status.h"
#include "mob.h"
#include "homunculus.h"
#include "mercenary.h"
#include "elemental.h"
#include "guild.h"
#include "itemdb.h"
#include "skill.h"
#include "battle.h"
#include "party.h"
#include "npc.h"
#include "log.h"
#include "script.h"
#include "atcommand.h"
#include "date.h"
#include "quest.h"
#include "achievement.h"
#include "region.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#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)
#define MOB_LAZYSKILLPERC 0 // Probability for mobs far from players from doing their IDLE skill. (rate of 1000 minute)
// 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?
#define MAX_MOB_CHAT 50 //Max Skill's messages
//Dynamic mob database, allows saving of memory when there's big gaps in the mob_db [Skotlex]
struct mob_db *mob_db_data[MAX_MOB_DB+1];
struct mob_db *mob_dummy = NULL; //Dummy mob to be returned when a non-existant one is requested.
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]; }
//Dynamic mob chat database
struct mob_chat *mob_chat_db[MAX_MOB_CHAT+1];
struct mob_chat *mob_chat(short id) { if(id<=0 || id>MAX_MOB_CHAT || mob_chat_db[id]==NULL) return (struct mob_chat*)NULL; return mob_chat_db[id]; }
//Dynamic item drop ratio database for per-item drop ratio modifiers overriding global drop ratios.
#define MAX_ITEMRATIO_MOBS 10
struct s_mob_item_drop_ratio {
unsigned short nameid;
int drop_ratio;
unsigned short mob_id[MAX_ITEMRATIO_MOBS];
};
static DBMap *mob_item_drop_ratio;
static struct eri *item_drop_ers; //For loot drops delay structures.
static struct eri *item_drop_list_ers;
static struct {
int qty;
int mob_id[350];
} summon[MAX_RANDOMMONSTER];
//Defines the Manuk/Splendide mob groups for the status reductions [Epoque]
const int mob_manuk[8] = { MOBID_TATACHO, MOBID_CENTIPEDE, MOBID_NEPENTHES, MOBID_HILLSRION, MOBID_HARDROCK_MOMMOTH, MOBID_G_TATACHO, MOBID_G_HILLSRION, MOBID_CENTIPEDE_LARVA };
const int mob_splendide[5] = { MOBID_TENDRILRION, MOBID_CORNUS, MOBID_NAGA, MOBID_LUCIOLA_VESPA, MOBID_PINGUICULA };
/*==========================================
* Local prototype declaration (only required thing)
*------------------------------------------*/
static int mob_makedummymobdb(int);
static int mob_spawn_guardian_sub(int tid, unsigned int tick, int id, intptr_t data);
int mobskill_use(struct mob_data *md,unsigned int tick,int event);
int mob_skill_id2skill_idx(int mob_id,uint16 skill_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 *mob = mob_db(i);
if(mob == mob_dummy) //Skip dummy mobs.
continue;
if(strcmpi(mob->name,str)==0 || strcmpi(mob->jname,str)==0 || strcmpi(mob->sprite,str)==0)
return i;
}
return 0;
}
static int mobdb_searchname_array_sub(struct mob_db* mob, const char *str)
{
if (mob == mob_dummy)
return 1;
if(!mob->base_exp && !mob->job_exp && mob->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(stristr(mob->jname,str))
return 0;
if(stristr(mob->name,str))
return 0;
return strcmpi(mob->jname,str);
}
/**
* Create and display a tombstone on the map
* @author [GreenBox]
* @param md : the mob to create a tombstone for
* @param killer : name of who has killed the mob
* @param time : time at wich the killed happen
*/
void mvptomb_create(struct mob_data *md, char *killer, time_t time)
{
struct npc_data *nd;
if ( md->tomb_nid )
mvptomb_destroy(md);
CREATE(nd, struct npc_data, 1);
nd->bl.id = md->tomb_nid = npc_get_new_npc_id();
nd->ud.dir = md->ud.dir;
nd->bl.m = md->bl.m;
nd->bl.x = md->bl.x;
nd->bl.y = md->bl.y;
nd->bl.type = BL_NPC;
safestrncpy(nd->name, msg_txt(NULL,656), sizeof(nd->name));
nd->class_ = 565;
nd->speed = 200;
nd->subtype = NPCTYPE_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);
if(map_addblock(&nd->bl))
return;
status_set_viewdata(&nd->bl, nd->class_);
status_change_init(&nd->bl);
unit_dataset(&nd->bl);
clif_spawn(&nd->bl);
}
/** Destroys MVP Tomb
* @param md
*/
void mvptomb_destroy(struct mob_data *md) {
struct npc_data *nd;
if ( (nd = map_id2nd(md->tomb_nid)) ) {
int16 m = nd->bl.m, i;
clif_clearunit_area(&nd->bl,CLR_OUTSIGHT);
map_delblock(&nd->bl);
ARR_FIND( 0, map[m].npc_num, i, map[m].npc[i] == nd );
if( !(i == map[m].npc_num) ) {
map[m].npc_num--;
map[m].npc[i] = map[m].npc[map[m].npc_num];
map[m].npc[map[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 count = 0, i;
struct mob_db* mob;
for(i=MIN_MOB_DB;i<=MAX_MOB_DB;i++){
mob = mob_db(i);
if (mob == mob_dummy || mob_is_clone(i) ) //keep clones out (or you leak player stats)
continue;
if (!mobdb_searchname_array_sub(mob, str)) {
if (count < size)
data[count] = mob;
count++;
}
}
return count;
}
/*==========================================
* Mob Search by Level. Returns number of matches
*------------------------------------------*/
int mobdb_searchlevel_array(struct mob_db **data, int size, int level)
{
int count = 0, i;
struct mob_db *mob;
for( i = MIN_MOB_DB; i <= MAX_MOB_DB; i++ )
{
mob = mob_db(i);
if( mob == mob_dummy || mob_is_clone(i) || mob->lv != level || !(mob->base_exp || mob->job_exp) || mob->spawn[0].qty < 1 )
continue;
if( count < size )
data[count] = mob;
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 mob_id)
{
if (mob_db(mob_id) == mob_dummy)
return 0;
return &mob_db(mob_id)->vd;
}
/*==========================================
* Cleans up mob-spawn data to make it "valid"
*------------------------------------------*/
int mob_parse_dataset(struct spawn_data *data)
{
size_t len;
if ((!mobdb_checkid(data->id) && !mob_is_clone(data->id)) || !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->id)->name, sizeof(data->name));
else if(strcmp(data->name,"--ja--")==0)
safestrncpy(data->name, mob_db(data->id)->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->mob_id = data->id;
md->state.boss = data->state.boss;
md->db = mob_db(md->mob_id);
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->mob_id);
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 *mob;
int i=0, mob_id;
if(type < 0 || type >= MAX_RANDOMMONSTER) {
ShowError("mob_get_random_id: Invalid type (%d) of random monster.\n", type);
return 0;
}
do {
if (type)
mob_id = summon[type].mob_id[rnd()%summon[type].qty];
else //Dead branch
mob_id = rnd() % MAX_MOB_DB;
mob = mob_db(mob_id);
} while ((mob == mob_dummy ||
mob_is_clone(mob_id) ||
(flag&1 && mob->summonper[type] <= rnd() % 1000000) ||
(flag&2 && lv < mob->lv) ||
(flag&4 && mob->status.mode&MD_BOSS) ||
(flag&8 && mob->spawn[0].qty < 1)
) && (i++) < MAX_MOB_DB);
if(i >= MAX_MOB_DB) // no suitable monster found, use fallback for given list
mob_id = mob_db_data[0]->summonper[type];
return mob_id;
}
int mob_get_random_id_lv(int lv, int range)
{
int i, count = 0;
struct mob_db* mob;
int lv_min = lv - range;
int lv_max = lv + range;
int Mob_id[MOB_CLONE_START];
// Count Mobs
for( i = MIN_MOB_DB; i < MOB_CLONE_START; i++ )
{
mob = mob_db(i);
if( mob == mob_dummy || !mob->hunting || mob->lv < lv_min || mob->lv > lv_max )
continue;
Mob_id[count] = i; // Store Valid ID
count++;
}
if( count == 0 )
return 0; // Nothing to do
return Mob_id[rand()%count];
}
/*==========================================
* 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
*t_sd; // Mob Target
struct mob_data *md;
unsigned int tick = gettick();
if( !battle_config.ksprotection )
return false; // KS Protection Disabled
if( !(md = BL_CAST(BL_MOB,target)) )
return false; // Tarjet 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;
struct map_session_data *pl_sd; // Owner
char output[128];
if( map[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 || md->option.is_event || md->db->ffa )
return false; // MVP, Slaves and Event 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 == 2 && sd->status.party_id && sd->status.party_id == sce->val3) || // Party KS allowed
(sce->val2 == 3 && sd->status.guild_id && sd->status.guild_id == sce->val4) ) // Guild KS allowed
break;
if( t_sd && (
(sce->val2 == 1 && sce->val1 != t_sd->bl.id) ||
(sce->val2 == 2 && sce->val3 && sce->val3 != t_sd->status.party_id) ||
(sce->val2 == 3 && 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(src, target, SC_KSPROTECTED, 10000, sd->bl.id, sd->state.noks, sd->status.party_id, sd->status.guild_id, battle_config.ksprotection, SCSTART_NOAVOID);
return false;
}
/*==========================================
* Script de Evento - Crea un Area de Explosiones que puede dañar segun el tipo:
* 0 = Jugadores y Mob
* 1 = Solamente Mob
*------------------------------------------*/
int mob_demolition (int map, short x, short y, short ratio, int type, int amount, unsigned int power)
{
struct mob_data *md = NULL;
short x1 = x - (ratio / 2), y1 = y - (ratio / 2);
int count;
if (ratio <= 0)
ratio = 1;
for (count = 0; count < amount; count++) {
md = mob_once_spawn_sub(NULL, map, rand()%(ratio)+x1, rand()%(ratio)+y1, "--ja--", 1142, "", SZ_SMALL, AI_NONE);
if (!md)
continue;
md->option.is_event = md->option.no_expdrop = true;
md->option.ai_type = (type < 1)?0:1;
if( md->option.ai_type == 1 ) md->special_state.ai = 1;
md->option.max_hp = power;
mob_spawn (md); // Crea el MOB
clif_misceffect(&md->bl, 92); // Meteor Storm
mobskill_use(md, gettick(), (rand()%100<50)?MSC_AFTERSKILL:MSC_SKILLUSED);
}
return 0;
}
// The MOB appearance for one time (for scripts) [TAB]
// Creacion de MOB especial sin Drop ni EXP para los GM's menores a 40
int mob_once_spawn_especial(struct map_session_data *sd, const char *mapname, short x, short y, const char *mobname, int class_, int amount, const char *event, int hp_mod, short size, short ai_type, bool no_slaves, short allow_warp, short hp_show, bool announce_hprate, bool announce_killer, bool no_expdrop, int TeamID, short item_drop, short item_amount, bool is_war, short exp_boost, bool drop_boost)
{
struct mob_data *md = NULL;
struct region_data* rd;
int m, count, lv = 255;
if(sd) lv = sd->status.base_level;
if( sd && strcmp(mapname,"this") == 0 )
m = sd->bl.m;
else
m = map_mapname2mapid(mapname);
if( m < 0 || amount <= 0 )
return 0;
size = cap_value(size, 0, 2);
for( count = 0; count < amount; count++ )
{
if( (md = mob_once_spawn_sub(sd ? &sd->bl : NULL, m, x, y, mobname, (class_ < 0 ? mob_get_random_id(-class_-1, battle_config.random_monster_checklv ? 3 : 1, lv) : class_) + (MAX_MOB_DB * size), item_drop == -1 ? "DoTreasure::OnTreasureOpen" : event, SZ_SMALL, AI_NONE)) == NULL )
continue;
// Mob Configuration:
md->option.is_event = true;
md->option.no_expdrop = no_expdrop;
md->option.ai_type = ai_type;
if( ai_type == 1 ) md->special_state.ai = 1;
md->option.no_slaves = no_slaves;
md->option.allow_warp = allow_warp;
md->option.hp_show = hp_show;
md->option.announce_hprate = announce_hprate;
md->option.announce_killer = announce_killer;
if( map[m].flag.gvg ) md->option.guild_id = TeamID;
else if( map[m].flag.pvp ) md->option.party_id = TeamID;
else if( map[m].flag.fvf ) md->option.faction_id = TeamID;
md->option.exp_boost = exp_boost;
md->option.drop_boost = drop_boost;
if( item_drop > 0 )
{
md->option.item_drop = item_drop;
md->option.item_amount = cap_value(item_amount,1,200);
}
if( is_war && (rd = region_search(map[m].region_id)) != NULL && !map_flag_vs(m) )
{
struct guild *g = guild_search(rd->guild_id);
md->option.is_war = is_war;
md->option.guild_id = rd->guild_id;
md->option.guild_emblem_id = (g == NULL) ? 0 : g->emblem_id;
}
if( hp_mod > 0 )
md->option.max_hp = hp_mod;
mob_spawn (md);
if( class_ < 0 && battle_config.dead_branch_active )
sc_start4(NULL, &md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE|MD_CANATTACK|MD_CANMOVE, 0, 60000);
}
return (md)?md->bl.id : 0;
}
struct mob_data *mob_once_spawn_sub(struct block_list *bl, int16 m, int16 x, int16 y, const char *mobname, int mob_id, 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.id = mob_id;
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[m].xs || y <= 0 || y >= map[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 mob_id, int amount, const char* event, unsigned int size, unsigned int ai)
{
struct mob_data* md = NULL;
int count, lv;
if (m < 0 || amount <= 0)
return 0; // invalid input
lv = (sd) ? sd->status.base_level : 255;
for (count = 0; count < amount; count++)
{
int c = (mob_id >= 0) ? mob_id : mob_get_random_id(-mob_id - 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 (mob_id == MOBID_EMPERIUM)
{
struct guild_castle* gc = guild_mapindex2gc(map[m].index);
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;
md->guardian_data->guild_id = gc->guild_id;
if (g)
{
md->guardian_data->emblem_id = g->emblem_id;
memcpy(md->guardian_data->guild_name, g->name, NAME_LENGTH);
}
else if (gc->guild_id) //Guild not yet available, retry in 5.
add_timer(gettick()+5000,mob_spawn_guardian_sub,md->bl.id,md->guardian_data->guild_id);
}
} // end addition [Valaris]
memset(&md->option, 0, sizeof(md->option)); // MobSettings
mob_spawn(md);
if (mob_id < 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 mob_id, 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,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, mob_id, 1, event, size, ai);
}
return id; // id of last spawned mob
}
/*==========================================
* Set a Guardian's guild data [Skotlex]
*------------------------------------------*/
static int mob_spawn_guardian_sub(int tid, unsigned int 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;
int guardup_lv;
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);
if (md->mob_id == MOBID_EMPERIUM)
{ //Not sure this is the best way, but otherwise we'd be invoking this for ALL guardians spawned later on.
md->guardian_data->guild_id = 0;
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->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;
}
guardup_lv = guild_checkskill(g,GD_GUARDUP);
md->guardian_data->emblem_id = g->emblem_id;
memcpy(md->guardian_data->guild_name, g->name, NAME_LENGTH);
md->guardian_data->guardup_lv = guardup_lv;
if( guardup_lv )
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 mob_id, 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(mob_id<=0) {
mob_id = mob_get_random_id(-mob_id-1, 1, 99);
if (!mob_id) return 0;
}
data.id = mob_id;
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, mob_id, map[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",mob_id, guardian, map[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[m].name);
if (gc == NULL)
{
ShowError("mob_spawn_guardian: No castle set at map %s\n", map[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", mob_id, map[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[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->guild_id = gc->guild_id;
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->emblem_id = g->emblem_id;
memcpy (md->guardian_data->guild_name, g->name, NAME_LENGTH);
md->guardian_data->guardup_lv = guild_checkskill(g,GD_GUARDUP);
} else if (md->guardian_data->guild_id)
add_timer(gettick()+5000,mob_spawn_guardian_sub,md->bl.id,md->guardian_data->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 mob_id, const char* event, 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( mob_id <= 0 )
{
mob_id = mob_get_random_id(-mob_id-1,1,99);
if( !mob_id ) return 0;
}
data.id = mob_id;
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",mob_id, bg_id, map[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 mob_id;
struct block_list *target;
unsigned int tick;
nullpo_ret(bl);
md=(struct mob_data *)bl;
mob_id = va_arg(ap, int);
target = va_arg(ap, struct block_list *);
tick=va_arg(ap, unsigned int);
if (md->mob_id == mob_id && 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, unsigned int 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, struct map_session_data* sd)
{
unsigned int spawntime, mode;