forked from HerculesWS/Hercules
-
Notifications
You must be signed in to change notification settings - Fork 1
/
unit.c
2641 lines (2302 loc) · 76.2 KB
/
unit.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/showmsg.h"
#include "../common/timer.h"
#include "../common/nullpo.h"
#include "../common/db.h"
#include "../common/malloc.h"
#include "../common/random.h"
#include "../common/HPM.h"
#include "map.h"
#include "path.h"
#include "pc.h"
#include "mob.h"
#include "pet.h"
#include "homunculus.h"
#include "instance.h"
#include "mercenary.h"
#include "elemental.h"
#include "skill.h"
#include "clif.h"
#include "duel.h"
#include "npc.h"
#include "guild.h"
#include "status.h"
#include "unit.h"
#include "battle.h"
#include "battleground.h"
#include "chat.h"
#include "trade.h"
#include "vending.h"
#include "party.h"
#include "intif.h"
#include "chrif.h"
#include "script.h"
#include "storage.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const short dirx[8]={0,-1,-1,-1,0,1,1,1};
const short diry[8]={1,1,0,-1,-1,-1,0,1};
struct unit_interface unit_s;
/**
* Returns the unit_data for the given block_list. If the object is using
* shared unit_data (i.e. in case of BL_NPC), it returns the shared data.
* @param bl block_list to process
* @return a pointer to the given object's unit_data
**/
struct unit_data* unit_bl2ud(struct block_list *bl) {
if( bl == NULL) return NULL;
if( bl->type == BL_PC) return &((struct map_session_data*)bl)->ud;
if( bl->type == BL_MOB) return &((struct mob_data*)bl)->ud;
if( bl->type == BL_PET) return &((struct pet_data*)bl)->ud;
if( bl->type == BL_NPC) return ((struct npc_data*)bl)->ud;
if( bl->type == BL_HOM) return &((struct homun_data*)bl)->ud;
if( bl->type == BL_MER) return &((struct mercenary_data*)bl)->ud;
if( bl->type == BL_ELEM) return &((struct elemental_data*)bl)->ud;
return NULL;
}
/**
* Returns the unit_data for the given block_list. If the object is using
* shared unit_data (i.e. in case of BL_NPC), it recreates a copy of the
* data so that it's safe to modify.
* @param bl block_list to process
* @return a pointer to the given object's unit_data
*/
struct unit_data* unit_bl2ud2(struct block_list *bl) {
if( bl && bl->type == BL_NPC && ((struct npc_data*)bl)->ud == &npc->base_ud ) {
struct npc_data *nd = (struct npc_data *)bl;
nd->ud = NULL;
CREATE(nd->ud, struct unit_data, 1);
unit->dataset(&nd->bl);
}
return unit->bl2ud(bl);
}
int unit_walktoxy_sub(struct block_list *bl)
{
int i;
struct walkpath_data wpd;
struct unit_data *ud = NULL;
nullpo_retr(1, bl);
ud = unit->bl2ud(bl);
if(ud == NULL) return 0;
if( !path->search(&wpd,bl->m,bl->x,bl->y,ud->to_x,ud->to_y,ud->state.walk_easy,CELL_CHKNOPASS) )
return 0;
memcpy(&ud->walkpath,&wpd,sizeof(wpd));
if (ud->target_to && ud->chaserange>1) {
//Generally speaking, the walk path is already to an adjacent tile
//so we only need to shorten the path if the range is greater than 1.
uint8 dir;
//Trim the last part of the path to account for range,
//but always move at least one cell when requested to move.
for (i = ud->chaserange*10; i > 0 && ud->walkpath.path_len>1;) {
ud->walkpath.path_len--;
dir = ud->walkpath.path[ud->walkpath.path_len];
if(dir&1)
i -= MOVE_DIAGONAL_COST;
else
i -= MOVE_COST;
ud->to_x -= dirx[dir];
ud->to_y -= diry[dir];
}
}
ud->state.change_walk_target=0;
if (bl->type == BL_PC) {
((TBL_PC *)bl)->head_dir = 0;
clif->walkok((TBL_PC*)bl);
}
clif->move(ud);
if(ud->walkpath.path_pos>=ud->walkpath.path_len)
i = -1;
else if(ud->walkpath.path[ud->walkpath.path_pos]&1)
i = status->get_speed(bl)*MOVE_DIAGONAL_COST/MOVE_COST;
else
i = status->get_speed(bl);
if( i > 0)
ud->walktimer = timer->add(timer->gettick()+i,unit->walktoxy_timer,bl->id,i);
return 1;
}
int unit_walktoxy_timer(int tid, int64 tick, int id, intptr_t data) {
int i;
int x,y,dx,dy;
uint8 dir;
struct block_list *bl;
struct map_session_data *sd;
struct mob_data *md;
struct unit_data *ud;
struct mercenary_data *mrd;
bl = map->id2bl(id);
if(bl == NULL)
return 0;
sd = BL_CAST(BL_PC, bl);
md = BL_CAST(BL_MOB, bl);
mrd = BL_CAST(BL_MER, bl);
ud = unit->bl2ud(bl);
if(ud == NULL) return 0;
if(ud->walktimer != tid){
ShowError("unit_walk_timer mismatch %d != %d\n",ud->walktimer,tid);
return 0;
}
ud->walktimer = INVALID_TIMER;
if (bl->prev == NULL) return 0; // Stop moved because it is missing from the block_list
if(ud->walkpath.path_pos>=ud->walkpath.path_len)
return 0;
if(ud->walkpath.path[ud->walkpath.path_pos]>=8)
return 1;
x = bl->x;
y = bl->y;
dir = ud->walkpath.path[ud->walkpath.path_pos];
ud->dir = dir;
dx = dirx[(int)dir];
dy = diry[(int)dir];
if(map->getcell(bl->m,x+dx,y+dy,CELL_CHKNOPASS))
return unit->walktoxy_sub(bl);
//Refresh view for all those we lose sight
map->foreachinmovearea(clif->outsight, bl, AREA_SIZE, dx, dy, sd?BL_ALL:BL_PC, bl);
x += dx;
y += dy;
map->moveblock(bl, x, y, tick);
ud->walk_count++; //walked cell counter, to be used for walk-triggered skills. [Skotlex]
status_change_end(bl, SC_ROLLINGCUTTER, INVALID_TIMER); //If you move, you lose your counters. [malufett]
if (bl->x != x || bl->y != y || ud->walktimer != INVALID_TIMER)
return 0; //map->moveblock has altered the object beyond what we expected (moved/warped it)
ud->walktimer = -2; // arbitrary non-INVALID_TIMER value to make the clif code send walking packets
map->foreachinmovearea(clif->insight, bl, AREA_SIZE, -dx, -dy, sd?BL_ALL:BL_PC, bl);
ud->walktimer = INVALID_TIMER;
if(sd) {
if( sd->touching_id )
npc->touchnext_areanpc(sd,false);
if(map->getcell(bl->m,x,y,CELL_CHKNPC)) {
npc->touch_areanpc(sd,bl->m,x,y);
if (bl->prev == NULL) //Script could have warped char, abort remaining of the function.
return 0;
} else
sd->areanpc_id=0;
if( sd->md ) { // mercenary should be warped after being 3 seconds too far from the master [greenbox]
if( !check_distance_bl(&sd->bl, &sd->md->bl, MAX_MER_DISTANCE) ) {
if (sd->md->masterteleport_timer == 0)
sd->md->masterteleport_timer = timer->gettick();
else if (DIFF_TICK(timer->gettick(), sd->md->masterteleport_timer) > 3000) {
sd->md->masterteleport_timer = 0;
unit->warp( &sd->md->bl, sd->bl.m, sd->bl.x, sd->bl.y, CLR_TELEPORT );
}
} else // reset the tick, he is not far anymore
sd->md->masterteleport_timer = 0;
}
if( sd->hd ) {
if( homun_alive(sd->hd) && !check_distance_bl(&sd->bl, &sd->hd->bl, MAX_MER_DISTANCE) ) {
if (sd->hd->masterteleport_timer == 0)
sd->hd->masterteleport_timer = timer->gettick();
else if (DIFF_TICK(timer->gettick(), sd->hd->masterteleport_timer) > 3000) {
sd->hd->masterteleport_timer = 0;
unit->warp( &sd->hd->bl, sd->bl.m, sd->bl.x, sd->bl.y, CLR_TELEPORT );
}
} else
sd->hd->masterteleport_timer = 0;
}
} else if (md) {
if( map->getcell(bl->m,x,y,CELL_CHKNPC) ) {
if( npc->touch_areanpc2(md) ) return 0; // Warped
} else
md->areanpc_id = 0;
if (md->min_chase > md->db->range3) md->min_chase--;
//Walk skills are triggered regardless of target due to the idle-walk mob state.
//But avoid triggering on stop-walk calls.
if(tid != INVALID_TIMER &&
!(ud->walk_count%WALK_SKILL_INTERVAL) &&
mob->skill_use(md, tick, -1))
{
if (!(ud->skill_id == NPC_SELFDESTRUCTION && ud->skilltimer != INVALID_TIMER))
{ //Skill used, abort walking
clif->fixpos(bl); //Fix position as walk has been cancelled.
return 0;
}
//Resend walk packet for proper Self Destruction display.
clif->move(ud);
}
}
else if( mrd && mrd->master )
{
if (!check_distance_bl(&mrd->master->bl, bl, MAX_MER_DISTANCE))
{
// mercenary should be warped after being 3 seconds too far from the master [greenbox]
if (mrd->masterteleport_timer == 0)
{
mrd->masterteleport_timer = timer->gettick();
}
else if (DIFF_TICK(timer->gettick(), mrd->masterteleport_timer) > 3000)
{
mrd->masterteleport_timer = 0;
unit->warp( bl, mrd->master->bl.id, mrd->master->bl.x, mrd->master->bl.y, CLR_TELEPORT );
}
}
else
{
mrd->masterteleport_timer = 0;
}
}
if(tid == INVALID_TIMER) //A directly invoked timer is from battle_stop_walking, therefore the rest is irrelevant.
return 0;
if(ud->state.change_walk_target)
return unit->walktoxy_sub(bl);
ud->walkpath.path_pos++;
if(ud->walkpath.path_pos>=ud->walkpath.path_len)
i = -1;
else if(ud->walkpath.path[ud->walkpath.path_pos]&1)
i = status->get_speed(bl)*14/10;
else
i = status->get_speed(bl);
if(i > 0) {
ud->walktimer = timer->add(tick+i,unit->walktoxy_timer,id,i);
if( md && DIFF_TICK(tick,md->dmgtick) < 3000 )//not required not damaged recently
clif->move(ud);
} else if(ud->state.running) {
//Keep trying to run.
if ( !(unit->run(bl) || unit->wugdash(bl,sd)) )
ud->state.running = 0;
} else if (ud->target_to) {
//Update target trajectory.
struct block_list *tbl = map->id2bl(ud->target_to);
if (!tbl || !status->check_visibility(bl, tbl)) {
//Cancel chase.
ud->to_x = bl->x;
ud->to_y = bl->y;
if (tbl && bl->type == BL_MOB && mob->warpchase((TBL_MOB*)bl, tbl) )
return 0;
ud->target_to = 0;
return 0;
}
if (tbl->m == bl->m && check_distance_bl(bl, tbl, ud->chaserange)) {
//Reached destination.
if (ud->state.attack_continue)
{ //Aegis uses one before every attack, we should
//only need this one for syncing purposes. [Skotlex]
ud->target_to = 0;
clif->fixpos(bl);
unit->attack(bl, tbl->id, ud->state.attack_continue);
}
} else { //Update chase-path
unit->walktobl(bl, tbl, ud->chaserange, ud->state.walk_easy|(ud->state.attack_continue?2:0));
return 0;
}
} else {
//Stopped walking. Update to_x and to_y to current location [Skotlex]
ud->to_x = bl->x;
ud->to_y = bl->y;
}
return 0;
}
int unit_delay_walktoxy_timer(int tid, int64 tick, int id, intptr_t data) {
struct block_list *bl = map->id2bl(id);
if (!bl || bl->prev == NULL)
return 0;
unit->walktoxy(bl, (short)((data>>16)&0xffff), (short)(data&0xffff), 0);
return 1;
}
//flag parameter:
//&1 -> 1/0 = easy/hard
//&2 -> force walking
//&4 -> Delay walking if the reason you can't walk is the canwalk delay
int unit_walktoxy( struct block_list *bl, short x, short y, int flag)
{
struct unit_data* ud = NULL;
struct status_change* sc = NULL;
struct walkpath_data wpd;
nullpo_ret(bl);
ud = unit->bl2ud(bl);
if( ud == NULL) return 0;
if (!path->search(&wpd, bl->m, bl->x, bl->y, x, y, flag&1, CELL_CHKNOPASS)) // Count walk path cells
return 0;
#ifdef OFFICIAL_WALKPATH
if( !path->search_long(NULL, bl->m, bl->x, bl->y, x, y, CELL_CHKNOPASS) // Check if there is an obstacle between
&& (wpd.path_len > (battle_config.max_walk_path/17)*14) // Official number of walkable cells is 14 if and only if there is an obstacle between. [malufett]
&& (bl->type != BL_NPC) ) // If type is a NPC, please disregard.
return 0;
#endif
if ((wpd.path_len > battle_config.max_walk_path) && (bl->type != BL_NPC))
return 0;
if (flag&4 && DIFF_TICK(ud->canmove_tick, timer->gettick()) > 0 &&
DIFF_TICK(ud->canmove_tick, timer->gettick()) < 2000) {
// Delay walking command. [Skotlex]
timer->add(ud->canmove_tick+1, unit->delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF));
return 1;
}
if(!(flag&2) && (!(status_get_mode(bl)&MD_CANMOVE) || !unit->can_move(bl)))
return 0;
ud->state.walk_easy = flag&1;
ud->to_x = x;
ud->to_y = y;
unit->set_target(ud, 0);
sc = status->get_sc(bl);
if (sc && sc->data[SC_CONFUSION]) //Randomize the target position
map->random_dir(bl, &ud->to_x, &ud->to_y);
if(ud->walktimer != INVALID_TIMER) {
// When you come to the center of the grid because the change of destination while you're walking right now
// Call a function from a timer unit->walktoxy_sub
ud->state.change_walk_target = 1;
return 1;
}
if(ud->attacktimer != INVALID_TIMER) {
timer->delete( ud->attacktimer, unit->attack_timer );
ud->attacktimer = INVALID_TIMER;
}
return unit->walktoxy_sub(bl);
}
//To set Mob's CHASE/FOLLOW states (shouldn't be done if there's no path to reach)
static inline void set_mobstate(struct block_list* bl, int flag)
{
struct mob_data* md = BL_CAST(BL_MOB,bl);
if( md && flag )
md->state.skillstate = md->state.aggressive ? MSS_FOLLOW : MSS_RUSH;
}
int unit_walktobl_sub(int tid, int64 tick, int id, intptr_t data) {
struct block_list *bl = map->id2bl(id);
struct unit_data *ud = bl?unit->bl2ud(bl):NULL;
if (ud && ud->walktimer == INVALID_TIMER && ud->target == data) {
if (DIFF_TICK(ud->canmove_tick, tick) > 0) //Keep waiting?
timer->add(ud->canmove_tick+1, unit->walktobl_sub, id, data);
else if (unit->can_move(bl)) {
if (unit->walktoxy_sub(bl))
set_mobstate(bl, ud->state.attack_continue);
}
}
return 0;
}
// Chases a tbl. If the flag&1, use hard-path seek,
// if flag&2, start attacking upon arrival within range, otherwise just walk to that character.
int unit_walktobl(struct block_list *bl, struct block_list *tbl, int range, int flag)
{
struct unit_data *ud = NULL;
struct status_change *sc = NULL;
nullpo_ret(bl);
nullpo_ret(tbl);
ud = unit->bl2ud(bl);
if( ud == NULL) return 0;
if (!(status_get_mode(bl)&MD_CANMOVE))
return 0;
if (!unit->can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, flag&1, &ud->to_x, &ud->to_y)) {
ud->to_x = bl->x;
ud->to_y = bl->y;
ud->target_to = 0;
return 0;
}
ud->state.walk_easy = flag&1;
ud->target_to = tbl->id;
ud->chaserange = range; //Note that if flag&2, this SHOULD be attack-range
ud->state.attack_continue = flag&2?1:0; //Chase to attack.
unit->set_target(ud, 0);
sc = status->get_sc(bl);
if (sc && sc->data[SC_CONFUSION]) //Randomize the target position
map->random_dir(bl, &ud->to_x, &ud->to_y);
if(ud->walktimer != INVALID_TIMER) {
ud->state.change_walk_target = 1;
set_mobstate(bl, flag&2);
return 1;
}
if(DIFF_TICK(ud->canmove_tick, timer->gettick()) > 0)
{ //Can't move, wait a bit before invoking the movement.
timer->add(ud->canmove_tick+1, unit->walktobl_sub, bl->id, ud->target);
return 1;
}
if(!unit->can_move(bl))
return 0;
if(ud->attacktimer != INVALID_TIMER) {
timer->delete( ud->attacktimer, unit->attack_timer );
ud->attacktimer = INVALID_TIMER;
}
if (unit->walktoxy_sub(bl)) {
set_mobstate(bl, flag&2);
return 1;
}
return 0;
}
int unit_run(struct block_list *bl) {
struct status_change *sc = status->get_sc(bl);
short to_x,to_y,dir_x,dir_y;
int lv;
int i;
if (!(sc && sc->data[SC_RUN]))
return 0;
if (!unit->can_move(bl)) {
status_change_end(bl, SC_RUN, INVALID_TIMER);
return 0;
}
lv = sc->data[SC_RUN]->val1;
dir_x = dirx[sc->data[SC_RUN]->val2];
dir_y = diry[sc->data[SC_RUN]->val2];
// determine destination cell
to_x = bl->x;
to_y = bl->y;
for(i=0;i<AREA_SIZE;i++) {
if(!map->getcell(bl->m,to_x+dir_x,to_y+dir_y,CELL_CHKPASS))
break;
//if sprinting and there's a PC/Mob/NPC, block the path [Kevin]
if(sc->data[SC_RUN] && map->count_oncell(bl->m, to_x+dir_x, to_y+dir_y, BL_PC|BL_MOB|BL_NPC))
break;
to_x += dir_x;
to_y += dir_y;
}
if( (to_x == bl->x && to_y == bl->y ) || (to_x == (bl->x+1) || to_y == (bl->y+1)) || (to_x == (bl->x-1) || to_y == (bl->y-1))) {
//If you can't run forward, you must be next to a wall, so bounce back. [Skotlex]
clif->sc_load(bl,bl->id,AREA,SI_TING,0,0,0);
//Set running to 0 beforehand so status_change_end knows not to enable spurt [Kevin]
unit->bl2ud(bl)->state.running = 0;
status_change_end(bl, SC_RUN, INVALID_TIMER);
skill->blown(bl,bl,skill->get_blewcount(TK_RUN,lv),unit->getdir(bl),0);
clif->fixpos(bl); //Why is a clif->slide (skill->blown) AND a fixpos needed? Ask Aegis.
clif->sc_end(bl,bl->id,AREA,SI_TING);
return 0;
}
if (unit->walktoxy(bl, to_x, to_y, 1))
return 1;
//There must be an obstacle nearby. Attempt walking one cell at a time.
do {
to_x -= dir_x;
to_y -= dir_y;
} while (--i > 0 && !unit->walktoxy(bl, to_x, to_y, 1));
if ( i == 0 ) {
// copy-paste from above
clif->sc_load(bl,bl->id,AREA,SI_TING,0,0,0);
//Set running to 0 beforehand so status_change_end knows not to enable spurt [Kevin]
unit->bl2ud(bl)->state.running = 0;
status_change_end(bl, SC_RUN, INVALID_TIMER);
skill->blown(bl,bl,skill->get_blewcount(TK_RUN,lv),unit->getdir(bl),0);
clif->fixpos(bl);
clif->sc_end(bl,bl->id,AREA,SI_TING);
return 0;
}
return 1;
}
//Exclusive function to Wug Dash state. [Jobbie/3CeAM]
int unit_wugdash(struct block_list *bl, struct map_session_data *sd) {
struct status_change *sc = status->get_sc(bl);
short to_x,to_y,dir_x,dir_y;
int lv;
int i;
if (!(sc && sc->data[SC_WUGDASH]))
return 0;
nullpo_ret(sd);
nullpo_ret(bl);
if (!unit->can_move(bl)) {
status_change_end(bl,SC_WUGDASH,INVALID_TIMER);
return 0;
}
lv = sc->data[SC_WUGDASH]->val1;
dir_x = dirx[sc->data[SC_WUGDASH]->val2];
dir_y = diry[sc->data[SC_WUGDASH]->val2];
to_x = bl->x;
to_y = bl->y;
for(i=0;i<AREA_SIZE;i++) {
if(!map->getcell(bl->m,to_x+dir_x,to_y+dir_y,CELL_CHKPASS))
break;
if(sc->data[SC_WUGDASH] && map->count_oncell(bl->m, to_x+dir_x, to_y+dir_y, BL_PC|BL_MOB|BL_NPC))
break;
to_x += dir_x;
to_y += dir_y;
}
if(to_x == bl->x && to_y == bl->y) {
unit->bl2ud(bl)->state.running = 0;
status_change_end(bl,SC_WUGDASH,INVALID_TIMER);
if( sd ){
clif->fixpos(bl);
skill->castend_damage_id(bl, &sd->bl, RA_WUGDASH, lv, timer->gettick(), SD_LEVEL);
}
return 0;
}
if (unit->walktoxy(bl, to_x, to_y, 1))
return 1;
do {
to_x -= dir_x;
to_y -= dir_y;
} while (--i > 0 && !unit->walktoxy(bl, to_x, to_y, 1));
if (i==0) {
unit->bl2ud(bl)->state.running = 0;
status_change_end(bl,SC_WUGDASH,INVALID_TIMER);
if( sd ){
clif->fixpos(bl);
skill->castend_damage_id(bl, &sd->bl, RA_WUGDASH, lv, timer->gettick(), SD_LEVEL);
}
return 0;
}
return 1;
}
//Makes bl attempt to run dist cells away from target. Uses hard-paths.
int unit_escape(struct block_list *bl, struct block_list *target, short dist) {
uint8 dir = map->calc_dir(target, bl->x, bl->y);
while( dist > 0 && map->getcell(bl->m, bl->x + dist*dirx[dir], bl->y + dist*diry[dir], CELL_CHKNOREACH) )
dist--;
return ( dist > 0 && unit->walktoxy(bl, bl->x + dist*dirx[dir], bl->y + dist*diry[dir], 0) );
}
//Instant warp function.
int unit_movepos(struct block_list *bl, short dst_x, short dst_y, int easy, bool checkpath) {
short dx,dy;
uint8 dir;
struct unit_data *ud = NULL;
struct map_session_data *sd = NULL;
nullpo_ret(bl);
sd = BL_CAST(BL_PC, bl);
ud = unit->bl2ud(bl);
if( ud == NULL) return 0;
unit->stop_walking(bl,1);
unit->stop_attack(bl);
if( checkpath && (map->getcell(bl->m,dst_x,dst_y,CELL_CHKNOPASS) || !path->search(NULL,bl->m,bl->x,bl->y,dst_x,dst_y,easy,CELL_CHKNOREACH)) )
return 0; // unreachable
ud->to_x = dst_x;
ud->to_y = dst_y;
dir = map->calc_dir(bl, dst_x, dst_y);
ud->dir = dir;
dx = dst_x - bl->x;
dy = dst_y - bl->y;
map->foreachinmovearea(clif->outsight, bl, AREA_SIZE, dx, dy, sd?BL_ALL:BL_PC, bl);
map->moveblock(bl, dst_x, dst_y, timer->gettick());
ud->walktimer = -2; // arbitrary non-INVALID_TIMER value to make the clif code send walking packets
map->foreachinmovearea(clif->insight, bl, AREA_SIZE, -dx, -dy, sd?BL_ALL:BL_PC, bl);
ud->walktimer = INVALID_TIMER;
if(sd) {
if( sd->touching_id )
npc->touchnext_areanpc(sd,false);
if(map->getcell(bl->m,bl->x,bl->y,CELL_CHKNPC)) {
npc->touch_areanpc(sd,bl->m,bl->x,bl->y);
if (bl->prev == NULL) //Script could have warped char, abort remaining of the function.
return 0;
} else
sd->areanpc_id=0;
if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
{ // Check if pet needs to be teleported. [Skotlex]
int flag = 0;
struct block_list* pbl = &sd->pd->bl;
if( !checkpath && !path->search(NULL,pbl->m,pbl->x,pbl->y,dst_x,dst_y,0,CELL_CHKNOPASS) )
flag = 1;
else if (!check_distance_bl(&sd->bl, pbl, AREA_SIZE)) //Too far, teleport.
flag = 2;
if( flag )
{
unit->movepos(pbl,sd->bl.x,sd->bl.y, 0, 0);
clif->slide(pbl,pbl->x,pbl->y);
}
}
}
return 1;
}
int unit_setdir(struct block_list *bl,unsigned char dir)
{
struct unit_data *ud;
nullpo_ret(bl );
ud = unit->bl2ud(bl);
if (!ud) return 0;
ud->dir = dir;
if (bl->type == BL_PC)
((TBL_PC *)bl)->head_dir = 0;
clif->changed_dir(bl, AREA);
return 0;
}
uint8 unit_getdir(struct block_list *bl) {
struct unit_data *ud;
nullpo_ret(bl);
if( bl->type == BL_NPC )
return ((TBL_NPC*)bl)->dir;
ud = unit->bl2ud(bl);
if (!ud) return 0;
return ud->dir;
}
// Pushes a unit by given amount of cells into given direction. Only
// map cell restrictions are respected.
// flag:
// &1 Do not send position update packets.
int unit_blown(struct block_list* bl, int dx, int dy, int count, int flag)
{
if(count) {
struct map_session_data* sd;
struct skill_unit* su = NULL;
int nx, ny, result;
nullpo_ret(bl);
sd = BL_CAST(BL_PC, bl);
su = BL_CAST(BL_SKILL, bl);
result = path->blownpos(bl->m, bl->x, bl->y, dx, dy, count);
nx = result>>16;
ny = result&0xffff;
if(!su) {
unit->stop_walking(bl, 0);
}
if( sd ) {
sd->ud.to_x = nx;
sd->ud.to_y = ny;
}
dx = nx-bl->x;
dy = ny-bl->y;
if(dx || dy) {
map->foreachinmovearea(clif->outsight, bl, AREA_SIZE, dx, dy, bl->type == BL_PC ? BL_ALL : BL_PC, bl);
if(su) {
skill->unit_move_unit_group(su->group, bl->m, dx, dy);
} else {
map->moveblock(bl, nx, ny, timer->gettick());
}
map->foreachinmovearea(clif->insight, bl, AREA_SIZE, -dx, -dy, bl->type == BL_PC ? BL_ALL : BL_PC, bl);
if(!(flag&1)) {
clif->blown(bl);
}
if(sd) {
if(sd->touching_id) {
npc->touchnext_areanpc(sd, false);
}
if(map->getcell(bl->m, bl->x, bl->y, CELL_CHKNPC)) {
npc->touch_areanpc(sd, bl->m, bl->x, bl->y);
} else {
sd->areanpc_id = 0;
}
}
}
count = path->distance(dx, dy);
}
return count; // return amount of knocked back cells
}
//Warps a unit/ud to a given map/position.
//In the case of players, pc->setpos is used.
//it respects the no warp flags, so it is safe to call this without doing nowarpto/nowarp checks.
int unit_warp(struct block_list *bl,short m,short x,short y,clr_type type)
{
struct unit_data *ud;
nullpo_ret(bl);
ud = unit->bl2ud(bl);
if(bl->prev==NULL || !ud)
return 1;
if (type == CLR_DEAD)
//Type 1 is invalid, since you shouldn't warp a bl with the "death"
//animation, it messes up with unit_remove_map! [Skotlex]
return 1;
if( m<0 ) m=bl->m;
switch (bl->type) {
case BL_MOB:
if (map->list[bl->m].flag.monster_noteleport && ((TBL_MOB*)bl)->master_id == 0)
return 1;
if (m != bl->m && map->list[m].flag.nobranch && battle_config.mob_warp&4 && !(((TBL_MOB *)bl)->master_id))
return 1;
break;
case BL_PC:
if (map->list[bl->m].flag.noteleport)
return 1;
break;
}
if (x<0 || y<0) {
//Random map position.
if (!map->search_freecell(NULL, m, &x, &y, -1, -1, 1)) {
ShowWarning("unit_warp failed. Unit Id:%d/Type:%d, target position map %d (%s) at [%d,%d]\n", bl->id, bl->type, m, map->list[m].name, x, y);
return 2;
}
} else if (map->getcell(m,x,y,CELL_CHKNOREACH)) {
//Invalid target cell
ShowWarning("unit_warp: Specified non-walkable target cell: %d (%s) at [%d,%d]\n", m, map->list[m].name, x,y);
if (!map->search_freecell(NULL, m, &x, &y, 4, 4, 1)) {
//Can't find a nearby cell
ShowWarning("unit_warp failed. Unit Id:%d/Type:%d, target position map %d (%s) at [%d,%d]\n", bl->id, bl->type, m, map->list[m].name, x, y);
return 2;
}
}
if (bl->type == BL_PC) //Use pc_setpos
return pc->setpos((TBL_PC*)bl, map_id2index(m), x, y, type);
if (!unit->remove_map(bl, type, ALC_MARK))
return 3;
if (bl->m != m && battle_config.clear_unit_onwarp &&
battle_config.clear_unit_onwarp&bl->type)
skill->clear_unitgroup(bl);
bl->x=ud->to_x=x;
bl->y=ud->to_y=y;
bl->m=m;
map->addblock(bl);
clif->spawn(bl);
skill->unit_move(bl,timer->gettick(),1);
return 0;
}
/*==========================================
* Caused the target object to stop moving.
* Flag values:
* &0x1: Issue a fixpos packet afterwards
* &0x2: Force the unit to move one cell if it hasn't yet
* &0x4: Enable moving to the next cell when unit was already half-way there
* (may cause on-touch/place side-effects, such as a scripted map change)
*------------------------------------------*/
int unit_stop_walking(struct block_list *bl,int type)
{
struct unit_data *ud;
const struct TimerData* td;
int64 tick;
nullpo_ret(bl);
ud = unit->bl2ud(bl);
if(!ud || ud->walktimer == INVALID_TIMER)
return 0;
//NOTE: We are using timer data after deleting it because we know the
//timer->delete function does not messes with it. If the function's
//behaviour changes in the future, this code could break!
td = timer->get(ud->walktimer);
timer->delete(ud->walktimer, unit->walktoxy_timer);
ud->walktimer = INVALID_TIMER;
ud->state.change_walk_target = 0;
tick = timer->gettick();
if( (type&0x02 && !ud->walkpath.path_pos) //Force moving at least one cell.
|| (type&0x04 && td && DIFF_TICK(td->tick, tick) <= td->data/2) //Enough time has passed to cover half-cell
) {
ud->walkpath.path_len = ud->walkpath.path_pos+1;
unit->walktoxy_timer(INVALID_TIMER, tick, bl->id, ud->walkpath.path_pos);
}
if(type&0x01)
clif->fixpos(bl);
ud->walkpath.path_len = 0;
ud->walkpath.path_pos = 0;
ud->to_x = bl->x;
ud->to_y = bl->y;
if(bl->type == BL_PET && type&~0xff)
ud->canmove_tick = timer->gettick() + (type>>8);
//Readded, the check in unit_set_walkdelay means dmg during running won't fall through to this place in code [Kevin]
if (ud->state.running) {
status_change_end(bl, SC_RUN, INVALID_TIMER);
status_change_end(bl, SC_WUGDASH, INVALID_TIMER);
}
return 1;
}
int unit_skilluse_id(struct block_list *src, int target_id, uint16 skill_id, uint16 skill_lv)
{
return unit->skilluse_id2(
src, target_id, skill_id, skill_lv,
skill->cast_fix(src, skill_id, skill_lv),
skill->get_castcancel(skill_id)
);
}
int unit_is_walking(struct block_list *bl)
{
struct unit_data *ud = unit->bl2ud(bl);
nullpo_ret(bl);
if(!ud) return 0;
return (ud->walktimer != INVALID_TIMER);
}
/*==========================================
* Determines if the bl can move based on status changes. [Skotlex]
*------------------------------------------*/
int unit_can_move(struct block_list *bl) {
struct map_session_data *sd;
struct unit_data *ud;
struct status_change *sc;
nullpo_ret(bl);
ud = unit->bl2ud(bl);
sc = status->get_sc(bl);
sd = BL_CAST(BL_PC, bl);
if (!ud)
return 0;
if (ud->skilltimer != INVALID_TIMER && ud->skill_id != LG_EXEEDBREAK && (!sd || !pc->checkskill(sd, SA_FREECAST) || skill->get_inf2(ud->skill_id)&INF2_GUILD_SKILL))
return 0; // prevent moving while casting
if (DIFF_TICK(ud->canmove_tick, timer->gettick()) > 0)
return 0;
if (sd && (
pc_issit(sd) ||
sd->state.vending ||
sd->state.buyingstore ||
sd->state.blockedmove
))
return 0; //Can't move
if (sc) {
if( sc->count
&& (
sc->data[SC_ANKLESNARE]
|| sc->data[SC_AUTOCOUNTER]
|| sc->data[SC_TRICKDEAD]
|| sc->data[SC_BLADESTOP]
|| sc->data[SC_BLADESTOP_WAIT]
|| (sc->data[SC_GOSPEL] && sc->data[SC_GOSPEL]->val4 == BCT_SELF) // cannot move while gospel is in effect
|| (sc->data[SC_BASILICA] && sc->data[SC_BASILICA]->val4 == bl->id) // Basilica caster cannot move
|| sc->data[SC_STOP]
|| sc->data[SC_RG_CCONFINE_M]
|| sc->data[SC_RG_CCONFINE_S]
|| sc->data[SC_GS_MADNESSCANCEL]
|| (sc->data[SC_GRAVITATION] && sc->data[SC_GRAVITATION]->val3 == BCT_SELF)
|| sc->data[SC_WHITEIMPRISON]
|| sc->data[SC_ELECTRICSHOCKER]
|| sc->data[SC_WUGBITE]
|| sc->data[SC_THORNS_TRAP]
|| sc->data[SC_MAGNETICFIELD]
|| sc->data[SC__MANHOLE]
|| sc->data[SC_CURSEDCIRCLE_ATKER]
|| sc->data[SC_CURSEDCIRCLE_TARGET]
|| (sc->data[SC_COLD] && bl->type != BL_MOB)
|| sc->data[SC_NETHERWORLD]
|| (sc->data[SC_CAMOUFLAGE] && sc->data[SC_CAMOUFLAGE]->val1 < 3 && !(sc->data[SC_CAMOUFLAGE]->val3&1))
|| sc->data[SC_MEIKYOUSISUI]
|| sc->data[SC_KG_KAGEHUMI]
|| sc->data[SC_KYOUGAKU]
|| sc->data[SC_NEEDLE_OF_PARALYZE]
|| sc->data[SC_VACUUM_EXTREME]
|| (sc->data[SC_FEAR] && sc->data[SC_FEAR]->val2 > 0)
|| (sc->data[SC_SPIDERWEB] && sc->data[SC_SPIDERWEB]->val1)
|| (sc->data[SC_CLOAKING] && sc->data[SC_CLOAKING]->val1 < 3 && !(sc->data[SC_CLOAKING]->val4&1)) //Need wall at level 1-2
|| (
sc->data[SC_DANCING] && sc->data[SC_DANCING]->val4
&& (
!sc->data[SC_LONGING]
|| (sc->data[SC_DANCING]->val1&0xFFFF) == CG_MOONLIT
|| (sc->data[SC_DANCING]->val1&0xFFFF) == CG_HERMODE
)
)
)
)
return 0;
if (sc->opt1 > 0 && sc->opt1 != OPT1_STONEWAIT && sc->opt1 != OPT1_BURNING && !(sc->opt1 == OPT1_CRYSTALIZE && bl->type == BL_MOB))
return 0;
if ((sc->option & OPTION_HIDE) && (!sd || pc->checkskill(sd, RG_TUNNELDRIVE) <= 0))
return 0;
}