/
map.c
5940 lines (5192 loc) · 173 KB
/
map.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
#include "../common/cbasetypes.h"
#include "../common/core.h"
#include "../common/timer.h"
#include "../common/ers.h"
#include "../common/grfio.h"
#include "../common/malloc.h"
#include "../common/socket.h" // WFIFO*()
#include "../common/showmsg.h"
#include "../common/nullpo.h"
#include "../common/random.h"
#include "../common/strlib.h"
#include "../common/utils.h"
#include "../common/conf.h"
#include "../common/console.h"
#include "../common/HPM.h"
#include "map.h"
#include "path.h"
#include "chrif.h"
#include "clif.h"
#include "duel.h"
#include "intif.h"
#include "npc.h"
#include "pc.h"
#include "status.h"
#include "mob.h"
#include "npc.h" // npc_setcells(), npc_unsetcells()
#include "chat.h"
#include "itemdb.h"
#include "storage.h"
#include "skill.h"
#include "trade.h"
#include "party.h"
#include "unit.h"
#include "battle.h"
#include "battleground.h"
#include "quest.h"
#include "script.h"
#include "mapreg.h"
#include "guild.h"
#include "pet.h"
#include "homunculus.h"
#include "instance.h"
#include "mercenary.h"
#include "elemental.h"
#include "atcommand.h"
#include "log.h"
#include "mail.h"
#include "irc-bot.h"
#include "HPMmap.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <math.h>
#ifndef _WIN32
#include <unistd.h>
#endif
struct map_interface map_s;
struct mapit_interface mapit_s;
/*==========================================
* server player count (of all mapservers)
*------------------------------------------*/
void map_setusers(int users) {
map->users = users;
}
int map_getusers(void) {
return map->users;
}
/*==========================================
* server player count (this mapserver only)
*------------------------------------------*/
int map_usercount(void) {
return db_size(map->pc_db);
}
/*==========================================
* Attempt to free a map blocklist
*------------------------------------------*/
int map_freeblock (struct block_list *bl) {
nullpo_retr(map->block_free_lock, bl);
if (map->block_free_lock == 0 || map->block_free_count >= block_free_max) {
if( bl->type == BL_ITEM )
ers_free(map->flooritem_ers, bl);
else
aFree(bl);
bl = NULL;
if (map->block_free_count >= block_free_max)
ShowWarning("map_freeblock: too many free block! %d %d\n", map->block_free_count, map->block_free_lock);
} else
map->block_free[map->block_free_count++] = bl;
return map->block_free_lock;
}
/*==========================================
* Lock blocklist, (prevent map->freeblock usage)
*------------------------------------------*/
int map_freeblock_lock (void) {
return ++map->block_free_lock;
}
/*==========================================
* Remove the lock on map_bl
*------------------------------------------*/
int map_freeblock_unlock (void) {
if ((--map->block_free_lock) == 0) {
int i;
for (i = 0; i < map->block_free_count; i++) {
if( map->block_free[i]->type == BL_ITEM )
ers_free(map->flooritem_ers, map->block_free[i]);
else
aFree(map->block_free[i]);
map->block_free[i] = NULL;
}
map->block_free_count = 0;
} else if (map->block_free_lock < 0) {
ShowError("map_freeblock_unlock: lock count < 0 !\n");
map->block_free_lock = 0;
}
return map->block_free_lock;
}
// Timer function to check if there some remaining lock and remove them if so.
// Called each 1s
int map_freeblock_timer(int tid, int64 tick, int id, intptr_t data) {
if (map->block_free_lock > 0) {
ShowError("map_freeblock_timer: block_free_lock(%d) is invalid.\n", map->block_free_lock);
map->block_free_lock = 1;
map->freeblock_unlock();
}
return 0;
}
/*==========================================
* These pair of functions update the counter of how many objects
* lie on a tile.
*------------------------------------------*/
void map_addblcell(struct block_list *bl) {
#ifdef CELL_NOSTACK
if( bl->m < 0 || bl->x < 0 || bl->x >= map->list[bl->m].xs
|| bl->y < 0 || bl->y >= map->list[bl->m].ys
|| !(bl->type&BL_CHAR) )
return;
map->list[bl->m].cell[bl->x+bl->y*map->list[bl->m].xs].cell_bl++;
#else
return;
#endif
}
void map_delblcell(struct block_list *bl) {
#ifdef CELL_NOSTACK
if( bl->m < 0 || bl->x < 0 || bl->x >= map->list[bl->m].xs
|| bl->y < 0 || bl->y >= map->list[bl->m].ys
|| !(bl->type&BL_CHAR) )
map->list[bl->m].cell[bl->x+bl->y*map->list[bl->m].xs].cell_bl--;
#else
return;
#endif
}
/*==========================================
* Adds a block to the map.
* Returns 0 on success, 1 on failure (illegal coordinates).
*------------------------------------------*/
int map_addblock(struct block_list* bl)
{
int16 m, x, y;
int pos;
nullpo_ret(bl);
if (bl->prev != NULL) {
ShowError("map_addblock: bl->prev != NULL\n");
return 1;
}
m = bl->m;
x = bl->x;
y = bl->y;
if( m < 0 || m >= map->count ) {
ShowError("map_addblock: invalid map id (%d), only %d are loaded.\n", m, map->count);
return 1;
}
if( x < 0 || x >= map->list[m].xs || y < 0 || y >= map->list[m].ys ) {
ShowError("map_addblock: out-of-bounds coordinates (\"%s\",%d,%d), map is %dx%d\n", map->list[m].name, x, y, map->list[m].xs, map->list[m].ys);
return 1;
}
pos = x/BLOCK_SIZE+(y/BLOCK_SIZE)*map->list[m].bxs;
if (bl->type == BL_MOB) {
bl->next = map->list[m].block_mob[pos];
bl->prev = &map->bl_head;
if (bl->next) bl->next->prev = bl;
map->list[m].block_mob[pos] = bl;
} else {
bl->next = map->list[m].block[pos];
bl->prev = &map->bl_head;
if (bl->next) bl->next->prev = bl;
map->list[m].block[pos] = bl;
}
#ifdef CELL_NOSTACK
map->addblcell(bl);
#endif
return 0;
}
/*==========================================
* Removes a block from the map.
*------------------------------------------*/
int map_delblock(struct block_list* bl)
{
int pos;
nullpo_ret(bl);
// blocklist (2ways chainlist)
if (bl->prev == NULL) {
if (bl->next != NULL) {
// can't delete block (already at the begining of the chain)
ShowError("map_delblock error : bl->next!=NULL\n");
}
return 0;
}
#ifdef CELL_NOSTACK
map->delblcell(bl);
#endif
pos = bl->x/BLOCK_SIZE+(bl->y/BLOCK_SIZE)*map->list[bl->m].bxs;
if (bl->next)
bl->next->prev = bl->prev;
if (bl->prev == &map->bl_head) {
//Since the head of the list, update the block_list map of []
if (bl->type == BL_MOB) {
map->list[bl->m].block_mob[pos] = bl->next;
} else {
map->list[bl->m].block[pos] = bl->next;
}
} else {
bl->prev->next = bl->next;
}
bl->next = NULL;
bl->prev = NULL;
return 0;
}
/*==========================================
* Moves a block a x/y target position. [Skotlex]
* Pass flag as 1 to prevent doing skill->unit_move checks
* (which are executed by default on BL_CHAR types)
*------------------------------------------*/
int map_moveblock(struct block_list *bl, int x1, int y1, int64 tick) {
int x0 = bl->x, y0 = bl->y;
struct status_change *sc = NULL;
int moveblock = ( x0/BLOCK_SIZE != x1/BLOCK_SIZE || y0/BLOCK_SIZE != y1/BLOCK_SIZE);
if (!bl->prev) {
//Block not in map, just update coordinates, but do naught else.
bl->x = x1;
bl->y = y1;
return 0;
}
//TODO: Perhaps some outs of bounds checking should be placed here?
if (bl->type&BL_CHAR) {
sc = status->get_sc(bl);
skill->unit_move(bl,tick,2);
status_change_end(bl, SC_RG_CCONFINE_M, INVALID_TIMER);
status_change_end(bl, SC_RG_CCONFINE_S, INVALID_TIMER);
// status_change_end(bl, SC_BLADESTOP, INVALID_TIMER); //Won't stop when you are knocked away, go figure...
status_change_end(bl, SC_NJ_TATAMIGAESHI, INVALID_TIMER);
status_change_end(bl, SC_MAGICROD, INVALID_TIMER);
if (sc && sc->data[SC_PROPERTYWALK] &&
sc->data[SC_PROPERTYWALK]->val3 >= skill->get_maxcount(sc->data[SC_PROPERTYWALK]->val1,sc->data[SC_PROPERTYWALK]->val2) )
status_change_end(bl,SC_PROPERTYWALK,INVALID_TIMER);
} else if (bl->type == BL_NPC)
npc->unsetcells((TBL_NPC*)bl);
if (moveblock) map->delblock(bl);
#ifdef CELL_NOSTACK
else map->delblcell(bl);
#endif
bl->x = x1;
bl->y = y1;
if (moveblock) map->addblock(bl);
#ifdef CELL_NOSTACK
else map->addblcell(bl);
#endif
if (bl->type&BL_CHAR) {
skill->unit_move(bl,tick,3);
if( bl->type == BL_PC && ((TBL_PC*)bl)->shadowform_id ) {//Shadow Form Target Moving
struct block_list *d_bl;
if( (d_bl = map->id2bl(((TBL_PC*)bl)->shadowform_id)) == NULL || !check_distance_bl(bl,d_bl,10) ) {
if( d_bl )
status_change_end(d_bl,SC__SHADOWFORM,INVALID_TIMER);
((TBL_PC*)bl)->shadowform_id = 0;
}
}
if (sc && sc->count) {
if (sc->data[SC_DANCING])
skill->unit_move_unit_group(skill->id2group(sc->data[SC_DANCING]->val2), bl->m, x1-x0, y1-y0);
else {
if (sc->data[SC_CLOAKING])
skill->check_cloaking(bl, sc->data[SC_CLOAKING]);
if (sc->data[SC_WARM])
skill->unit_move_unit_group(skill->id2group(sc->data[SC_WARM]->val4), bl->m, x1-x0, y1-y0);
if (sc->data[SC_BANDING])
skill->unit_move_unit_group(skill->id2group(sc->data[SC_BANDING]->val4), bl->m, x1-x0, y1-y0);
if (sc->data[SC_NEUTRALBARRIER_MASTER])
skill->unit_move_unit_group(skill->id2group(sc->data[SC_NEUTRALBARRIER_MASTER]->val2), bl->m, x1-x0, y1-y0);
else if (sc->data[SC_STEALTHFIELD_MASTER])
skill->unit_move_unit_group(skill->id2group(sc->data[SC_STEALTHFIELD_MASTER]->val2), bl->m, x1-x0, y1-y0);
if( sc->data[SC__SHADOWFORM] ) {//Shadow Form Caster Moving
struct block_list *d_bl;
if( (d_bl = map->id2bl(sc->data[SC__SHADOWFORM]->val2)) == NULL || !check_distance_bl(bl,d_bl,10) )
status_change_end(bl,SC__SHADOWFORM,INVALID_TIMER);
}
if (sc->data[SC_PROPERTYWALK]
&& sc->data[SC_PROPERTYWALK]->val3 < skill->get_maxcount(sc->data[SC_PROPERTYWALK]->val1,sc->data[SC_PROPERTYWALK]->val2)
&& map->find_skill_unit_oncell(bl,bl->x,bl->y,SO_ELECTRICWALK,NULL,0) == NULL
&& map->find_skill_unit_oncell(bl,bl->x,bl->y,SO_FIREWALK,NULL,0) == NULL
&& skill->unitsetting(bl,sc->data[SC_PROPERTYWALK]->val1,sc->data[SC_PROPERTYWALK]->val2,x0, y0,0)
) {
sc->data[SC_PROPERTYWALK]->val3++;
}
}
/* Guild Aura Moving */
if( bl->type == BL_PC && ((TBL_PC*)bl)->state.gmaster_flag ) {
if (sc->data[SC_LEADERSHIP])
skill->unit_move_unit_group(skill->id2group(sc->data[SC_LEADERSHIP]->val4), bl->m, x1-x0, y1-y0);
if (sc->data[SC_GLORYWOUNDS])
skill->unit_move_unit_group(skill->id2group(sc->data[SC_GLORYWOUNDS]->val4), bl->m, x1-x0, y1-y0);
if (sc->data[SC_SOULCOLD])
skill->unit_move_unit_group(skill->id2group(sc->data[SC_SOULCOLD]->val4), bl->m, x1-x0, y1-y0);
if (sc->data[SC_HAWKEYES])
skill->unit_move_unit_group(skill->id2group(sc->data[SC_HAWKEYES]->val4), bl->m, x1-x0, y1-y0);
}
}
} else if (bl->type == BL_NPC)
npc->setcells((TBL_NPC*)bl);
return 0;
}
/*==========================================
* Counts specified number of objects on given cell.
* TODO: merge with bl_getall_area
*------------------------------------------*/
int map_count_oncell(int16 m, int16 x, int16 y, int type) {
int bx,by;
struct block_list *bl;
int count = 0;
if (x < 0 || y < 0 || (x >= map->list[m].xs) || (y >= map->list[m].ys))
return 0;
bx = x/BLOCK_SIZE;
by = y/BLOCK_SIZE;
if (type&~BL_MOB)
for( bl = map->list[m].block[bx+by*map->list[m].bxs] ; bl != NULL ; bl = bl->next )
if(bl->x == x && bl->y == y && bl->type&type)
count++;
if (type&BL_MOB)
for( bl = map->list[m].block_mob[bx+by*map->list[m].bxs] ; bl != NULL ; bl = bl->next )
if(bl->x == x && bl->y == y)
count++;
return count;
}
/*
* Looks for a skill unit on a given cell
* flag&1: runs battle_check_target check based on unit->group->target_flag
*/
struct skill_unit* map_find_skill_unit_oncell(struct block_list* target,int16 x,int16 y,uint16 skill_id,struct skill_unit* out_unit, int flag) {
int16 m,bx,by;
struct block_list *bl;
struct skill_unit *su;
m = target->m;
if (x < 0 || y < 0 || (x >= map->list[m].xs) || (y >= map->list[m].ys))
return NULL;
bx = x/BLOCK_SIZE;
by = y/BLOCK_SIZE;
for( bl = map->list[m].block[bx+by*map->list[m].bxs] ; bl != NULL ; bl = bl->next ) {
if (bl->x != x || bl->y != y || bl->type != BL_SKILL)
continue;
su = (struct skill_unit *) bl;
if( su == out_unit || !su->alive || !su->group || su->group->skill_id != skill_id )
continue;
if( !(flag&1) || battle->check_target(&su->bl,target,su->group->target_flag) > 0 )
return su;
}
return NULL;
}
/** @name Functions for block_list search and manipulation
*/
/* @{ */
/**
* Applies func to every block_list in bl_list starting with bl_list[blockcount].
* Sets bl_list_count back to blockcount.
* Returns the sum of values returned by func.
* @param func Function to be applied
* @param blockcount Index of first relevant entry in bl_list
* @param max Maximum sum of values returned by func (usually max number of func calls)
* @param args Extra arguments for func
* @return Sum of the values returned by func
*/
static int bl_vforeach(int (*func)(struct block_list*, va_list), int blockcount, int max, va_list args) {
int i;
int returnCount = 0;
map->freeblock_lock();
for (i = blockcount; i < map->bl_list_count && returnCount < max; i++) {
if (map->bl_list[i]->prev) { //func() may delete this bl_list[] slot, checking for prev ensures it wasnt queued for deletion.
va_list argscopy;
va_copy(argscopy, args);
returnCount += func(map->bl_list[i], argscopy);
va_end(argscopy);
}
}
map->freeblock_unlock();
map->bl_list_count = blockcount;
return returnCount;
}
/**
* Applies func to every block_list object of bl_type type on map m.
* Returns the sum of values returned by func.
* @param func Function to be applied
* @param m Map id
* @param type enum bl_type
* @param args Extra arguments for func
* @return Sum of the values returned by func
*/
static int map_vforeachinmap(int (*func)(struct block_list*, va_list), int16 m, int type, va_list args) {
int i;
int returnCount = 0;
int bsize;
va_list argscopy;
struct block_list *bl;
int blockcount = map->bl_list_count;
if (m < 0)
return 0;
bsize = map->list[m].bxs * map->list[m].bys;
for (i = 0; i < bsize; i++) {
if (type&~BL_MOB) {
for (bl = map->list[m].block[i]; bl != NULL; bl = bl->next) {
if (bl->type&type && map->bl_list_count < BL_LIST_MAX) {
map->bl_list[map->bl_list_count++] = bl;
}
}
}
if (type&BL_MOB) {
for (bl = map->list[m].block_mob[i]; bl != NULL; bl = bl->next) {
if (map->bl_list_count < BL_LIST_MAX) {
map->bl_list[map->bl_list_count++] = bl;
}
}
}
}
if (map->bl_list_count >= BL_LIST_MAX)
ShowError("map.c:map_vforeachinmap: bl_list size (%d) exceeded\n", BL_LIST_MAX);
va_copy(argscopy, args);
returnCount = bl_vforeach(func, blockcount, INT_MAX, argscopy);
va_end(argscopy);
return returnCount;
}
/**
* Applies func to every block_list object of bl_type type on map m.
* Returns the sum of values returned by func.
* @see map_vforeachinmap
* @param func Function to be applied
* @param m Map id
* @param type enum bl_type
* @param ... Extra arguments for func
* @return Sum of the values returned by func
*/
int map_foreachinmap(int (*func)(struct block_list*, va_list), int16 m, int type, ...) {
int returnCount;
va_list ap;
va_start(ap, type);
returnCount = map->vforeachinmap(func, m, type, ap);
va_end(ap);
return returnCount;
}
/**
* Applies func to every block_list object of bl_type type on all maps
* of instance instance_id.
* Returns the sum of values returned by func.
* @see map_vforeachinmap.
* @param func Function to be applied
* @param m Map id
* @param type enum bl_type
* @param ap Extra arguments for func
* @return Sum of the values returned by func
*/
int map_vforeachininstance(int (*func)(struct block_list*, va_list), int16 instance_id, int type, va_list ap) {
int i;
int returnCount = 0;
for (i = 0; i < instance->list[instance_id].num_map; i++) {
int m = instance->list[instance_id].map[i];
va_list apcopy;
va_copy(apcopy, ap);
returnCount += map->vforeachinmap(func, m, type, apcopy);
va_end(apcopy);
}
return returnCount;
}
/**
* Applies func to every block_list object of bl_type type on all maps
* of instance instance_id.
* Returns the sum of values returned by func.
* @see map_vforeachininstance.
* @param func Function to be applied
* @param m Map id
* @param type enum bl_type
* @param ... Extra arguments for func
* @return Sum of the values returned by func
*/
int map_foreachininstance(int (*func)(struct block_list*, va_list), int16 instance_id, int type, ...) {
int returnCount;
va_list ap;
va_start(ap, type);
returnCount = map->vforeachininstance(func, instance_id, type, ap);
va_end(ap);
return returnCount;
}
/**
* Retrieves all map objects in area that are matched by the type
* and func. Appends them at the end of global bl_list array.
* @param type Matching enum bl_type
* @param m Map
* @param func Matching function
* @param ... Extra arguments for func
* @return Number of found objects
*/
static int bl_getall_area(int type, int m, int x0, int y0, int x1, int y1, int (*func)(struct block_list*, va_list), ...) {
va_list args;
int bx, by;
struct block_list *bl;
int found = 0;
if (m < 0)
return 0;
if (x1 < x0) swap(x0, x1);
if (y1 < y0) swap(y0, y1);
// Limit search area to map size
x0 = max(x0, 0);
y0 = max(y0, 0);
x1 = min(x1, map->list[m].xs - 1);
y1 = min(y1, map->list[m].ys - 1);
for (by = y0 / BLOCK_SIZE; by <= y1 / BLOCK_SIZE; by++) {
for (bx = x0 / BLOCK_SIZE; bx <= x1 / BLOCK_SIZE; bx++) {
if (type&~BL_MOB) {
for (bl = map->list[m].block[bx + by * map->list[m].bxs]; bl != NULL; bl = bl->next) {
if (map->bl_list_count < BL_LIST_MAX
&& bl->type&type
&& bl->x >= x0 && bl->x <= x1
&& bl->y >= y0 && bl->y <= y1) {
if (func) {
va_start(args, func);
if (func(bl, args)) {
map->bl_list[map->bl_list_count++] = bl;
found++;
}
va_end(args);
}
else {
map->bl_list[map->bl_list_count++] = bl;
found++;
}
}
}
}
if (type&BL_MOB) { // TODO: fix this code duplication
for (bl = map->list[m].block_mob[bx + by * map->list[m].bxs]; bl != NULL; bl = bl->next) {
if (map->bl_list_count < BL_LIST_MAX
//&& bl->type&type // block_mob contains BL_MOBs only
&& bl->x >= x0 && bl->x <= x1
&& bl->y >= y0 && bl->y <= y1) {
if (func) {
va_start(args, func);
if (func(bl, args)) {
map->bl_list[map->bl_list_count++] = bl;
found++;
}
va_end(args);
}
else {
map->bl_list[map->bl_list_count++] = bl;
found++;
}
}
}
}
}
}
if (map->bl_list_count >= BL_LIST_MAX)
ShowError("map.c:bl_getall_area: bl_list size (%d) exceeded\n", BL_LIST_MAX);
return found;
}
/**
* Checks if bl is within range cells from center.
* If CIRCULAR AREA is not used always returns 1, since
* preliminary range selection is already done in bl_getall_area.
* @return 1 if matches, 0 otherwise
*/
static int bl_vgetall_inrange(struct block_list *bl, va_list args)
{
#ifdef CIRCULAR_AREA
struct block_list *center = va_arg(args, struct block_list*);
int range = va_arg(args, int);
if (!check_distance_bl(center, bl, range))
return 0;
#endif
return 1;
}
/**
* Applies func to every block_list object of bl_type type within range cells from center.
* Area is rectangular, unless CIRCULAR_AREA is defined.
* Returns the sum of values returned by func.
* @param func Function to be applied
* @param center Center of the selection area
* @param range Range in cells from center
* @param type enum bl_type
* @param ap Extra arguments for func
* @return Sum of the values returned by func
*/
int map_vforeachinrange(int (*func)(struct block_list*, va_list), struct block_list* center, int16 range, int type, va_list ap) {
int returnCount = 0;
int blockcount = map->bl_list_count;
va_list apcopy;
if (range < 0) range *= -1;
bl_getall_area(type, center->m, center->x - range, center->y - range, center->x + range, center->y + range, bl_vgetall_inrange, center, range);
va_copy(apcopy, ap);
returnCount = bl_vforeach(func, blockcount, INT_MAX, apcopy);
va_end(ap);
return returnCount;
}
/**
* Applies func to every block_list object of bl_type type within range cells from center.
* Area is rectangular, unless CIRCULAR_AREA is defined.
* Returns the sum of values returned by func.
* @see map_vforeachinrange
* @param func Function to be applied
* @param center Center of the selection area
* @param range Range in cells from center
* @param type enum bl_type
* @param ... Extra arguments for func
* @return Sum of the values returned by func
*/
int map_foreachinrange(int (*func)(struct block_list*, va_list), struct block_list* center, int16 range, int type, ...) {
int returnCount;
va_list ap;
va_start(ap, type);
returnCount = map->vforeachinrange(func, center, range, type, ap);
va_end(ap);
return returnCount;
}
/**
* Applies func to some block_list objects of bl_type type within range cells from center.
* Limit is set by count parameter.
* Area is rectangular, unless CIRCULAR_AREA is defined.
* Returns the sum of values returned by func.
* @param func Function to be applied
* @param center Center of the selection area
* @param range Range in cells from center
* @param count Maximum sum of values returned by func (usually max number of func calls)
* @param type enum bl_type
* @param ap Extra arguments for func
* @return Sum of the values returned by func
*/
int map_vforcountinrange(int (*func)(struct block_list*, va_list), struct block_list* center, int16 range, int count, int type, va_list ap) {
int returnCount = 0;
int blockcount = map->bl_list_count;
va_list apcopy;
if (range < 0) range *= -1;
bl_getall_area(type, center->m, center->x - range, center->y - range, center->x + range, center->y + range, bl_vgetall_inrange, center, range);
va_copy(apcopy, ap);
returnCount = bl_vforeach(func, blockcount, count, apcopy);
va_end(apcopy);
return returnCount;
}
/**
* Applies func to some block_list objects of bl_type type within range cells from center.
* Limit is set by count parameter.
* Area is rectangular, unless CIRCULAR_AREA is defined.
* Returns the sum of values returned by func.
* @see map_vforcountinrange
* @param func Function to be applied
* @param center Center of the selection area
* @param range Range in cells from center
* @param count Maximum sum of values returned by func (usually max number of func calls)
* @param type enum bl_type
* @param ... Extra arguments for func
* @return Sum of the values returned by func
*/
int map_forcountinrange(int (*func)(struct block_list*, va_list), struct block_list* center, int16 range, int count, int type, ...) {
int returnCount;
va_list ap;
va_start(ap, type);
returnCount = map->vforcountinrange(func, center, range, count, type, ap);
va_end(ap);
return returnCount;
}
/**
* Checks if bl is within shooting range from center.
* There must be a shootable path between bl and center.
* Does not check for range if CIRCULAR AREA is not defined, since
* preliminary range selection is already done in bl_getall_area.
* @return 1 if matches, 0 otherwise
*/
static int bl_vgetall_inshootrange(struct block_list *bl, va_list args)
{
struct block_list *center = va_arg(args, struct block_list*);
#ifdef CIRCULAR_AREA
int range = va_arg(args, int);
if (!check_distance_bl(center, bl, range))
return 0;
#endif
if (!path->search_long(NULL, center->m, center->x, center->y, bl->x, bl->y, CELL_CHKWALL))
return 0;
return 1;
}
/**
* Applies func to every block_list object of bl_type type within shootable range from center.
* There must be a shootable path between bl and center.
* Area is rectangular, unless CIRCULAR_AREA is defined.
* Returns the sum of values returned by func.
* @param func Function to be applied
* @param center Center of the selection area
* @param range Range in cells from center
* @param type enum bl_type
* @param ap Extra arguments for func
* @return Sum of the values returned by func
*/
int map_vforeachinshootrange(int (*func)(struct block_list*, va_list), struct block_list* center, int16 range, int type, va_list ap) {
int returnCount = 0;
int blockcount = map->bl_list_count;
va_list apcopy;
if (range < 0) range *= -1;
bl_getall_area(type, center->m, center->x - range, center->y - range, center->x + range, center->y + range, bl_vgetall_inshootrange, center, range);
va_copy(apcopy, ap);
returnCount = bl_vforeach(func, blockcount, INT_MAX, ap);
va_end(apcopy);
return returnCount;
}
/**
* Applies func to every block_list object of bl_type type within shootable range from center.
* There must be a shootable path between bl and center.
* Area is rectangular, unless CIRCULAR_AREA is defined.
* Returns the sum of values returned by func.
* @param func Function to be applied
* @param center Center of the selection area
* @param range Range in cells from center
* @param type enum bl_type
* @param ... Extra arguments for func
* @return Sum of the values returned by func
*/
int map_foreachinshootrange(int (*func)(struct block_list*, va_list), struct block_list* center, int16 range, int type, ...) {
int returnCount;
va_list ap;
va_start(ap, type);
returnCount = map->vforeachinshootrange(func, center, range, type, ap);
va_end(ap);
return returnCount;
}
/**
* Applies func to every block_list object of bl_type type in
* rectangular area (x0,y0)~(x1,y1) on map m.
* Returns the sum of values returned by func.
* @param func Function to be applied
* @param m Map id
* @param x0 Starting X-coordinate
* @param y0 Starting Y-coordinate
* @param x1 Ending X-coordinate
* @param y1 Ending Y-coordinate
* @param type enum bl_type
* @param ap Extra arguments for func
* @return Sum of the values returned by func
*/
int map_vforeachinarea(int (*func)(struct block_list*, va_list), int16 m, int16 x0, int16 y0, int16 x1, int16 y1, int type, va_list ap) {
int returnCount = 0;
int blockcount = map->bl_list_count;
va_list apcopy;
bl_getall_area(type, m, x0, y0, x1, y1, NULL);
va_copy(apcopy, ap);
returnCount = bl_vforeach(func, blockcount, INT_MAX, apcopy);
va_end(apcopy);
return returnCount;
}
/**
* Applies func to every block_list object of bl_type type in
* rectangular area (x0,y0)~(x1,y1) on map m.
* Returns the sum of values returned by func.
* @see map_vforeachinarea
* @param func Function to be applied
* @param m Map id
* @param x0 Starting X-coordinate
* @param y0 Starting Y-coordinate
* @param x1 Ending X-coordinate
* @param y1 Ending Y-coordinate
* @param type enum bl_type
* @param ... Extra arguments for func
* @return Sum of the values returned by func
*/
int map_foreachinarea(int (*func)(struct block_list*, va_list), int16 m, int16 x0, int16 y0, int16 x1, int16 y1, int type, ...) {
int returnCount;
va_list ap;
va_start(ap, type);
returnCount = map->vforeachinarea(func, m, x0, y0, x1, y1, type, ap);
va_end(ap);
return returnCount;
}
/**
* Applies func to some block_list objects of bl_type type in
* rectangular area (x0,y0)~(x1,y1) on map m.
* Limit is set by @count parameter.
* Returns the sum of values returned by func.
* @param func Function to be applied
* @param m Map id
* @param x0 Starting X-coordinate
* @param y0 Starting Y-coordinate
* @param x1 Ending X-coordinate
* @param y1 Ending Y-coordinate
* @param count Maximum sum of values returned by func (usually max number of func calls)
* @param type enum bl_type
* @param ap Extra arguments for func
* @return Sum of the values returned by func
*/
int map_vforcountinarea(int (*func)(struct block_list*,va_list), int16 m, int16 x0, int16 y0, int16 x1, int16 y1, int count, int type, va_list ap) {
int returnCount = 0;
int blockcount = map->bl_list_count;
va_list apcopy;
bl_getall_area(type, m, x0, y0, x1, y1, NULL);
va_copy(apcopy, ap);
returnCount = bl_vforeach(func, blockcount, count, apcopy);
va_end(apcopy);
return returnCount;
}
/**
* Applies func to some block_list objects of bl_type type in
* rectangular area (x0,y0)~(x1,y1) on map m.
* Limit is set by @count parameter.
* Returns the sum of values returned by func.
* @see map_vforcountinarea
* @param func Function to be applied
* @param m Map id
* @param x0 Starting X-coordinate
* @param y0 Starting Y-coordinate
* @param x1 Ending X-coordinate
* @param y1 Ending Y-coordinate
* @param count Maximum sum of values returned by func (usually max number of func calls)
* @param type enum bl_type
* @param ... Extra arguments for func
* @return Sum of the values returned by func
*/
int map_forcountinarea(int (*func)(struct block_list*,va_list), int16 m, int16 x0, int16 y0, int16 x1, int16 y1, int count, int type, ...) {
int returnCount;
va_list ap;
va_start(ap, type);
returnCount = map->vforcountinarea(func, m, x0, y0, x1, y1, count, type, ap);
va_end(ap);
return returnCount;
}
/**
* Checks if bl is inside area that was in range cells from the center
* before it was moved by (dx,dy) cells, but it is not in range cells
* from center after movement is completed.
* In other words, checks if bl is inside area that is no longer covered
* by center's range.
* Preliminary range selection is already done in bl_getall_area.
* @return 1 if matches, 0 otherwise
*/
static int bl_vgetall_inmovearea(struct block_list *bl, va_list args)
{
int dx = va_arg(args, int);
int dy = va_arg(args, int);
struct block_list *center = va_arg(args, struct block_list*);
int range = va_arg(args, int);
if ((dx > 0 && bl->x < center->x - range + dx) ||
(dx < 0 && bl->x > center->x + range + dx) ||
(dy > 0 && bl->y < center->y - range + dy) ||
(dy < 0 && bl->y > center->y + range + dy))
return 1;
return 0;
}
/**
* Applies func to every block_list object of bl_type type in
* area that was covered by range cells from center, but is no
* longer after center is moved by (dx,dy) cells (i.e. area that
* center has lost sight of).
* If used after center has reached its destination and with
* opposed movement vector (-dx,-dy), selection corresponds
* to new area in center's view).
* Uses rectangular area.
* Returns the sum of values returned by func.
* @param func Function to be applied
* @param center Center of the selection area
* @param range Range in cells from center
* @param dx Center's movement on X-axis