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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
|
/**
* This file is part of Hercules.
* http://herc.ws - http://github.com/HerculesWS/Hercules
*
* Copyright (C) 2012-2020 Hercules Dev Team
* Copyright (C) Athena Dev Teams
*
* Hercules is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define HERCULES_CORE
#include "config/core.h" // RENEWAL_CAST
#include "unit.h"
#include "map/battle.h"
#include "map/battleground.h"
#include "map/chat.h"
#include "map/chrif.h"
#include "map/clan.h"
#include "map/clif.h"
#include "map/duel.h"
#include "map/elemental.h"
#include "map/guild.h"
#include "map/homunculus.h"
#include "map/instance.h"
#include "map/intif.h"
#include "map/map.h"
#include "map/mercenary.h"
#include "map/mob.h"
#include "map/npc.h"
#include "map/party.h"
#include "map/path.h"
#include "map/pc.h"
#include "map/pet.h"
#include "map/script.h"
#include "map/skill.h"
#include "map/status.h"
#include "map/storage.h"
#include "map/trade.h"
#include "map/vending.h"
#include "common/HPM.h"
#include "common/db.h"
#include "common/memmgr.h"
#include "common/nullpo.h"
#include "common/random.h"
#include "common/showmsg.h"
#include "common/socket.h"
#include "common/timer.h"
#include "common/utils.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 };
static struct unit_interface unit_s;
struct unit_interface *unit;
/**
* 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.
*
* __Warning:__ if bl->type is not known or NULL,
* an assertion will be triggered and NULL returned.
* @param bl block_list to process, it is expected to be not NULL.
* @return a pointer to the given object's unit_data
**/
static struct unit_data *unit_bl2ud(struct block_list *bl)
{
Assert_retr(NULL, bl != NULL);
switch (bl->type) {
case BL_PC:
return &BL_UCAST(BL_PC, bl)->ud;
case BL_MOB:
return &BL_UCAST(BL_MOB, bl)->ud;
case BL_PET:
return &BL_UCAST(BL_PET, bl)->ud;
case BL_NPC:
return BL_UCAST(BL_NPC, bl)->ud;
case BL_HOM:
return &BL_UCAST(BL_HOM, bl)->ud;
case BL_MER:
return &BL_UCAST(BL_MER, bl)->ud;
case BL_ELEM:
return &BL_UCAST(BL_ELEM, bl)->ud;
case BL_SKILL: // No assertion to not spam the server console when attacking a skill type unit such as Ice Wall.
return NULL;
default:
Assert_retr(NULL, false);
}
}
/**
* Returns the const unit_data for the given const block_list. If the object is using
* shared unit_data (i.e. in case of BL_NPC), it returns the shared data.
*
* __Warning:__ if bl->type is not known or NULL,
* an assertion will be triggered and NULL returned.
* @param bl block_list to process, it is expected to be not NULL.
* @return a pointer to the given object's unit_data
**/
static const struct unit_data *unit_cbl2ud(const struct block_list *bl)
{
Assert_retr(NULL, bl != NULL);
switch (bl->type) {
case BL_PC:
return &BL_UCCAST(BL_PC, bl)->ud;
case BL_MOB:
return &BL_UCCAST(BL_MOB, bl)->ud;
case BL_PET:
return &BL_UCCAST(BL_PET, bl)->ud;
case BL_NPC:
return BL_UCCAST(BL_NPC, bl)->ud;
case BL_HOM:
return &BL_UCCAST(BL_HOM, bl)->ud;
case BL_MER:
return &BL_UCCAST(BL_MER, bl)->ud;
case BL_ELEM:
return &BL_UCCAST(BL_ELEM, bl)->ud;
case BL_SKILL: // No assertion to not spam the server console when attacking a skill type unit such as Ice Wall.
return NULL;
default:
Assert_retr(NULL, false);
}
}
/**
* 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
*/
static struct unit_data *unit_bl2ud2(struct block_list *bl)
{
struct npc_data *nd = BL_CAST(BL_NPC, bl);
if (nd != NULL && nd->ud == &npc->base_ud) {
nd->ud = NULL;
CREATE(nd->ud, struct unit_data, 1);
unit->dataset(&nd->bl);
}
return unit->bl2ud(bl);
}
/**
* TODO: understand purpose of this function
* @param bl block_list to process
* @return 0: success, 1: fail, 2: nullpointer
*/
static int unit_walk_toxy_sub(struct block_list *bl)
{
nullpo_retr(2, bl);
struct unit_data *ud = unit->bl2ud(bl);
if (ud == NULL)
return 2;
struct walkpath_data wpd = {0};
if (!path->search(&wpd, bl, bl->m, bl->x, bl->y, ud->to_x, ud->to_y, ud->state.walk_easy, CELL_CHKNOPASS))
return 1;
#ifdef OFFICIAL_WALKPATH
if (bl->type != BL_NPC // If type is an NPC, disregard.
&& !path->search_long(NULL, bl, bl->m, bl->x, bl->y, ud->to_x, ud->to_y, CELL_CHKNOPASS) // Check if there is an obstacle between
&& wpd.path_len > 14) { // Official number of walkable cells is 14 if and only if there is an obstacle between. [malufett]
return 1;
}
#endif
ud->walkpath = wpd;
if (ud->target_to != 0 && 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.
// Trim the last part of the path to account for range,
// but always move at least one cell when requested to move.
for (int i = ud->chaserange * 10 - 10; i > 0 && ud->walkpath.path_len > 1;) {
enum unit_dir dir;
ud->walkpath.path_len--;
dir = ud->walkpath.path[ud->walkpath.path_len];
Assert_retr(1, dir >= UNIT_DIR_FIRST && dir < UNIT_DIR_MAX);
if (unit_is_diagonal_dir(dir))
i -= MOVE_COST * 20; // When chasing, units will target a diamond-shaped area in range [Playtester]
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) {
struct map_session_data *sd = BL_UCAST(BL_PC, bl);
sd->head_dir = 0;
clif->walkok(sd);
}
clif->move(ud);
int timer_delay;
if (ud->walkpath.path_pos >= ud->walkpath.path_len)
timer_delay = -1;
else if ((ud->walkpath.path[ud->walkpath.path_pos] & 1) != 0)
timer_delay = status->get_speed(bl) * MOVE_DIAGONAL_COST / MOVE_COST;
else
timer_delay = status->get_speed(bl);
if (timer_delay > 0)
ud->walktimer = timer->add(timer->gettick() + timer_delay, unit->walk_toxy_timer, bl->id, 0); //TODO: check if unit->walk_toxy_timer uses any intptr data
return 0;
}
/**
* Triggered on full step if stepaction is true and executes remembered action.
* @param tid: Timer ID
* @param tick: Unused
* @param id: ID of bl to do the action
* @param data: Unused
* @return 0: success, 1: fail, 2: nullpointer
*/
static int unit_steptimer(int tid, int64 tick, int id, intptr_t data)
{
struct block_list *bl = map->id2bl(id);
if (bl == NULL || bl->prev == NULL)
return 2;
struct unit_data *ud = unit->bl2ud(bl);
if (ud == NULL)
return 2;
if (ud->steptimer != tid) {
ShowError("unit_steptimer mismatch %d != %d\n", ud->steptimer, tid);
return 1;
}
ud->steptimer = INVALID_TIMER;
if (!ud->stepaction)
return 1;
// Set to false here because if an error occurs, it should not be executed again
ud->stepaction = false;
if (ud->target_to == 0)
return 1;
// Flush target_to as it might contain map coordinates which should not be used by other functions
int target_id = ud->target_to;
ud->target_to = 0;
// If stepaction is set then we remembered a client request that should be executed on the next step
// Execute request now if target is in attack range
if (ud->stepskill_id != 0 && (skill->get_inf(ud->stepskill_id) & INF_GROUND_SKILL) != 0) {
// Execute ground skill
struct map_data *md = &map->list[bl->m];
unit->skilluse_pos(bl, target_id % md->xs, target_id / md->xs, ud->stepskill_id, ud->stepskill_lv);
} else {
// If a player has target_id set and target is in range, attempt attack
struct block_list *tbl = map->id2bl(target_id);
nullpo_retr(2, tbl);
if (status->check_visibility(bl, tbl) == 0) // Target not visible
return 1;
if (ud->stepskill_id == 0)
unit->attack(bl, tbl->id, ud->state.attack_continue + 2); // Execute normal attack
else
unit->skilluse_id(bl, tbl->id, ud->stepskill_id, ud->stepskill_lv); // Execute non-ground skill
}
return 0;
}
/**
* Warps homunculus or mercenary towards his master in case he's too far away for 3 seconds.
* @param master_bl: block_list of master
* @param slave_bl: block_list of homunculus/mercenary master owns
* @return 0: success, 1: fail
*/
static int unit_warpto_master(struct block_list *master_bl, struct block_list *slave_bl)
{
nullpo_retr(1, master_bl);
nullpo_retr(1, slave_bl);
int64 *masterteleport_timer;
struct homun_data *hd = BL_CAST(BL_HOM, slave_bl);
struct mercenary_data *md = BL_CAST(BL_MER, slave_bl);
bool check = true;
if (hd != NULL) {
masterteleport_timer = &hd->masterteleport_timer;
check = homun_alive(hd);
} else if (md != NULL) {
masterteleport_timer = &md->masterteleport_timer;
} else {
return 1;
}
if (check && !check_distance_bl(master_bl, slave_bl, MAX_MER_DISTANCE)) {
if (*masterteleport_timer == 0) {
*masterteleport_timer = timer->gettick();
return 0;
} else if (DIFF_TICK(timer->gettick(), *masterteleport_timer) > 3000) {
unit->warp(slave_bl, master_bl->m, master_bl->x, master_bl->y, CLR_TELEPORT);
}
}
*masterteleport_timer = 0; // resets tick in case he isn't far anymore.
return 0;
}
/**
* Timer for walking to target coordinates or object.
* @param tid: timer id
* @param tick: tick
* @param id: id of bl to do the action
* @param data: unused
* @return 0: success, 1: fail
*/
static int unit_walk_toxy_timer(int tid, int64 tick, int id, intptr_t data)
{
struct block_list *bl = map->id2bl(id);
if (bl == NULL || bl->prev == NULL) // Stop moved because it is missing from the block_list
return 1;
struct unit_data *ud = unit->bl2ud(bl);
if (ud == NULL)
return 1;
if (ud->walktimer != tid) {
ShowError("unit_walk_timer mismatch %d != %d\n",ud->walktimer,tid);
return 1;
}
ud->walktimer = INVALID_TIMER;
if (ud->walkpath.path_pos >= ud->walkpath.path_len)
return 1;
enum unit_dir dir = ud->walkpath.path[ud->walkpath.path_pos];
Assert_retr(1, dir >= UNIT_DIR_FIRST && dir < UNIT_DIR_MAX);
int x = bl->x;
int y = bl->y;
ud->dir = dir;
int dx = dirx[dir];
int dy = diry[dir];
// Get icewall walk block depending on boss mode (players can't be trapped)
unsigned char icewall_walk_block = 0;
struct mob_data *md = BL_CAST(BL_MOB, bl);
if (md != NULL) {
if ((md->status.mode & MD_BOSS) != 0)
icewall_walk_block = battle_config.boss_icewall_walk_block;
else
icewall_walk_block = battle_config.mob_icewall_walk_block;
}
// Monsters will walk into an icewall from the west and south if they already started walking
if (map->getcell(bl->m, bl, x + dx, y + dy, CELL_CHKNOPASS)
&& (icewall_walk_block == 0 || !map->getcell(bl->m, bl, x + dx, y + dy, CELL_CHKICEWALL) || dx < 0 || dy < 0))
return unit->walk_toxy_sub(bl);
// Monsters can only leave icewalls to the west and south
// But if movement fails more than icewall_walk_block times, they can ignore this rule
if (md != NULL && md->walktoxy_fail_count < icewall_walk_block && map->getcell(bl->m, bl, x, y, CELL_CHKICEWALL) != 0 && (dx > 0 || dy > 0)) {
// Needs to be done here so that rudeattack skills are invoked
md->walktoxy_fail_count++;
clif->fixpos(bl);
// Monsters in this situation first use a chase skill, then unlock target and then use an idle skill
if ((++ud->walk_count % WALK_SKILL_INTERVAL) == 0)
mob->skill_use(md, tick, -1);
mob->unlocktarget(md, tick);
if ((++ud->walk_count % WALK_SKILL_INTERVAL) != 0)
mob->skill_use(md, tick, -1);
return 1;
}
struct map_session_data *sd = BL_CAST(BL_PC, bl);
//Refresh view for all those we lose sight
map->foreachinmovearea(clif->outsight, bl, AREA_SIZE, dx, dy, (sd != NULL ? 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 1; // 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 != NULL ? BL_ALL : BL_PC), bl);
ud->walktimer = INVALID_TIMER;
struct mercenary_data *mrd = BL_CAST(BL_MER, bl);
if (sd != NULL) {
if (sd->touching_id != 0)
npc->touchnext_areanpc(sd, false);
if (map->getcell(bl->m, bl, 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 {
npc->untouch_areanpc(sd, bl->m, x, y);
}
if (sd->md != NULL) // mercenary should be warped after being 3 seconds too far from the master [greenbox]
unit->warpto_master(bl, &sd->md->bl);
if (sd->hd != NULL)
unit->warpto_master(bl, &sd->hd->bl);
} else if (md) {
// Movement was successful, reset walktoxy_fail_count
md->walktoxy_fail_count = 0;
if (map->getcell(bl->m, bl, x, y, CELL_CHKNPC) != 0 && 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) == 0
&& map->list[bl->m].users > 0 && mob->skill_use(md, tick, -1) == 1) {
// Walk skills are supposed to be used while walking
if (!(ud->skill_id == NPC_SELFDESTRUCTION && ud->skilltimer != INVALID_TIMER)
&& md->state.skillstate != MSS_WALK) {
// Skill used, abort walking
clif->fixpos(bl); // Fix position as walk has been canceled.
return 1;
}
// Resend walk packet for proper Self Destruction display.
clif->move(ud);
}
} else if (mrd != NULL && mrd->master != NULL) {
unit->warpto_master(&mrd->master->bl, bl);
}
if(tid == INVALID_TIMER) // A directly invoked timer is from battle_stop_walking, therefore the rest is irrelevant.
return 0;
// If stepaction is set then we remembered a client request that should be executed on the next step
if (ud->stepaction && ud->target_to != 0) {
// Delete old stepaction even if not executed yet, the latest command is what counts
if (ud->steptimer != INVALID_TIMER) {
timer->delete(ud->steptimer, unit->steptimer);
ud->steptimer = INVALID_TIMER;
}
// Delay stepactions by half a step (so they are executed at full step)
int timer_delay;
if ((ud->walkpath.path[ud->walkpath.path_pos] & 1) != 0)
timer_delay = status->get_speed(bl) * 14 / 20;
else
timer_delay = status->get_speed(bl) / 2;
ud->steptimer = timer->add(tick + timer_delay, unit->steptimer, bl->id, 0);
}
if (ud->state.change_walk_target) {
if (unit->walk_toxy_sub(bl) == 0)
return 0;
clif->fixpos(bl);
return 1;
}
int timer_delay;
ud->walkpath.path_pos++;
if(ud->walkpath.path_pos>=ud->walkpath.path_len)
timer_delay = -1;
else if ((ud->walkpath.path[ud->walkpath.path_pos] & 1) != 0)
timer_delay = status->get_speed(bl) * 14 / 10;
else
timer_delay = status->get_speed(bl);
if (timer_delay > 0) {
ud->walktimer = timer->add(tick + timer_delay, unit->walk_toxy_timer, id, 0);
if (md != NULL && DIFF_TICK(tick, md->dmgtick) < 3000) // not required not damaged recently
clif->move(ud);
} else if (ud->state.running != 0) {
// Keep trying to run.
if (!(unit->run(bl, NULL, SC_RUN) || unit->run(bl, sd, SC_WUGDASH)))
ud->state.running = 0;
} else if (!ud->stepaction && ud->target_to != 0) {
// Update target trajectory.
struct block_list *tbl = map->id2bl(ud->target_to);
if (tbl == NULL || status->check_visibility(bl, tbl) == 0) { // not visible
// Cancel chase.
ud->to_x = bl->x;
ud->to_y = bl->y;
if (tbl != NULL && bl->type == BL_MOB && mob->warpchase(BL_UCAST(BL_MOB, bl), tbl) != 0)
return 0;
ud->target_to = 0;
return 1;
}
if (tbl->m == bl->m && check_distance_bl(bl, tbl, ud->chaserange)) {
//Reached destination.
ud->target_to = 0;
if (ud->state.attack_continue) {
//Aegis uses one before every attack, we should
//only need this one for syncing purposes. [Skotlex]
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);
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;
if (battle_config.official_cell_stack_limit != 0 && map->count_oncell(bl->m, x, y, BL_CHAR | BL_NPC, 0x1 | 0x2) > battle_config.official_cell_stack_limit) {
// Walked on occupied cell, call unit->walk_toxy again
if (ud->steptimer != INVALID_TIMER) {
// Execute step timer on next step instead
timer->delete(ud->steptimer, unit->steptimer);
ud->steptimer = INVALID_TIMER;
}
return unit->walk_toxy(bl, x, y, 8);
}
}
return 0;
}
/**
* Timer for delayed execution of unit->walk_toxy once triggered
* @param tid: Timer ID, unused
* @param tick: Tick, unused
* @param id: ID of block_list to execute the action
* @param data: uint32 data cast to intptr_t with x-coord in lowest 16 bits and y-coord in highest 16 bits
* @return 0: success, 1: failure
*/
static int unit_delay_walk_toxy_timer(int tid, int64 tick, int id, intptr_t data)
{
struct block_list *bl = map->id2bl(id);
if (bl == NULL || bl->prev == NULL)
return 1;
short x = (short)GetWord((uint32)data, 0);
short y = (short)GetWord((uint32)data, 1);
unit->walk_toxy(bl, x, y, 0);
return 0;
}
/**
* Makes a unit walk to (x, y) coordinates
* @param bl: block_list of unit to move
* @param x: x-coordinate
* @param y: y-coordinate
* @param flag: flag paramater with following options:
* - `& 1` -> 1/0 = easy / hard
* - `& 2` -> Force walking
* - `& 4` -> Delay walking, if the reason you can't walk is the `canwalk delay`
* - `& 8` -> Search for an unoccupied cell and cancel if none available
* .
* @return 0: success, 1: failure
*/
static int unit_walk_toxy(struct block_list *bl, short x, short y, int flag)
{
// TODO: change flag to enum? [skyleo]
struct unit_data* ud = NULL;
struct status_change* sc = NULL;
struct walkpath_data wpd;
nullpo_retr(1, bl);
ud = unit->bl2ud(bl);
if (ud == NULL)
return 1;
if ((flag & 8) != 0 && battle_config.check_occupied_cells != 0) {
if (!map->closest_freecell(bl->m, bl, &x, &y, BL_CHAR | BL_NPC, 1)) // This might change x and y
return 1;
}
if (!path->search(&wpd, bl, bl->m, bl->x, bl->y, x, y, flag & 1, CELL_CHKNOPASS)) // Count walk path cells
return 1;
if (bl->type != BL_NPC) {
#ifdef OFFICIAL_WALKPATH
// Check if there is an obstacle between
// Official number of walkable cells is 14 if and only if there is an obstacle between. [malufett]
if (!path->search_long(NULL, bl, bl->m, bl->x, bl->y, x, y, CELL_CHKNOPASS)
&& (wpd.path_len > (battle_config.max_walk_path / 17) * 14))
return 1;
#endif
if (wpd.path_len > battle_config.max_walk_path)
return 1;
}
if ((flag & 4) != 0 && 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_walk_toxy_timer, bl->id,
(intptr_t)MakeDWord((uint16)x, (uint16)y));
return 0;
}
if ((flag & 2) == 0 && ((status_get_mode(bl) & MD_CANMOVE) == 0 || unit->can_move(bl) == 0))
return 1;
ud->state.walk_easy = flag & 1;
ud->to_x = x;
ud->to_y = y;
unit->stop_attack(bl); //Sets target to 0
if ((flag & 8) == 0) // Stepaction might be delayed due to occupied cell
unit->stop_stepaction(bl); // unit->walktoxy removes any remembered stepaction and resets ud->target_to
sc = status->get_sc(bl);
if (sc != NULL) {
if (sc->data[SC_CONFUSION] != NULL || sc->data[SC__CHAOS] != NULL) // Randomize the target position
map->random_dir(bl, &ud->to_x, &ud->to_y);
if (sc->data[SC_COMBOATTACK] != NULL)
status_change_end(bl, SC_COMBOATTACK, INVALID_TIMER);
}
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->walk_toxy_sub
ud->state.change_walk_target = 1;
return 0;
}
return unit->walk_toxy_sub(bl);
}
/**
* Sets CHASE / FOLLOW states, in case bl is a mob.
* WARNING: This shouldn't be done if there's no path to reach
* @param bl: block_list of mob
*/
static inline void set_mobstate(struct block_list *bl)
{
struct mob_data* md = BL_CAST(BL_MOB, bl);
if (md != NULL) {
if (md->state.aggressive != 0)
md->state.skillstate = MSS_FOLLOW;
else
md->state.skillstate = MSS_RUSH;
}
}
/**
* Timer used for when a unit can't walk towards its target yet due to it's canmove_tick,
* keeps retrying until it works or target changes.
* @param tid: Timer ID, unused
* @param tick: Tick, unused
* @param id: ID of block_list to execute the action
* @param data: ID of block_list to walk towards
* @return 0: success, 1: failure
*/
static int unit_walktobl_timer(int tid, int64 tick, int id, intptr_t data)
{
struct block_list *bl = map->id2bl(id);
if (bl == NULL)
return 1;
struct unit_data *ud = unit->bl2ud(bl);
if (ud == NULL)
return 1;
if (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_timer, id, data);
else if (unit->can_move(bl) != 0 && unit->walk_toxy_sub(bl) == 0 && ud->state.attack_continue != 0)
set_mobstate(bl);
}
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.
static 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;
} else if (range == 0) {
//Should walk on the same cell as target (for looters)
ud->to_x = tbl->x;
ud->to_y = tbl->y;
}
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->stop_attack(bl); //Sets target to 0
sc = status->get_sc(bl);
if (sc && (sc->data[SC_CONFUSION] || sc->data[SC__CHAOS])) //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;
if ((flag & 2) != 0)
set_mobstate(bl);
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_timer, bl->id, ud->target);
return 1;
}
if(!unit->can_move(bl))
return 0;
if (unit->walk_toxy_sub(bl) == 0) {
if ((flag & 2) != 0)
set_mobstate(bl);
return 1;
}
return 0;
}
/**
* Called by unit_run when an object was hit
* @param sd Required only when using SC_WUGDASH
**/
static void unit_run_hit(struct block_list *bl, struct status_change *sc, struct map_session_data *sd, enum sc_type type)
{
int lv;
struct unit_data *ud;
Assert_retv(type >= 0 && type < SC_MAX);
lv = sc->data[type]->val1;
//If you can't run forward, you must be next to a wall, so bounce back. [Skotlex]
if( type == SC_RUN )
clif->sc_load(bl, bl->id, AREA, status->get_sc_icon(SC_TING), 0, 0, 0);
ud = unit->bl2ud(bl);
nullpo_retv(ud);
//Set running to 0 beforehand so status_change_end knows not to enable spurt [Kevin]
ud->state.running = 0;
status_change_end(bl, type, INVALID_TIMER);
if (type == SC_RUN) {
if (lv > 0)
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, status->get_sc_icon(SC_TING));
} else if (sd) {
clif->fixpos(bl);
skill->castend_damage_id(bl, &sd->bl, RA_WUGDASH, lv, timer->gettick(), SD_LEVEL);
}
return;
}
/**
* Makes character run, used for SC_RUN and SC_WUGDASH
* @param sd Required only when using SC_WUGDASH
* @retval true Finished running
* @retval false Hit an object/Couldn't run
**/
static bool unit_run(struct block_list *bl, struct map_session_data *sd, enum sc_type type)
{
struct status_change *sc;
short to_x,to_y,dir_x,dir_y;
int i;
nullpo_retr(false, bl);
sc = status->get_sc(bl);
if( !(sc && sc->data[type]) )
return false;
if( !unit->can_move(bl) ) {
status_change_end(bl, type, INVALID_TIMER);
return false;
}
dir_x = dirx[sc->data[type]->val2];
dir_y = diry[sc->data[type]->val2];
// determine destination cell
to_x = bl->x;
to_y = bl->y;
// Search for available path
for(i = 0; i < AREA_SIZE; i++) {
if (!map->getcell(bl->m, bl, 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 ( map->count_oncell(bl->m, to_x + dir_x, to_y + dir_y, BL_PC | BL_MOB | BL_NPC, 0x2) )
break;
to_x += dir_x;
to_y += dir_y;
}
// Can't run forward
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))) {
unit->run_hit(bl, sc, sd, type);
return false;
}
if (unit->walk_toxy(bl, to_x, to_y, 1) == 0)
return true;
//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->walk_toxy(bl, to_x, to_y, 1) != 0);
if ( i == 0 ) {
unit->run_hit(bl, sc, sd, type);
return false;
}
return 1;
}
//Makes bl attempt to run dist cells away from target. Uses hard-paths.
static int unit_escape(struct block_list *bl, struct block_list *target, short dist)
{
nullpo_ret(bl);
enum unit_dir dir = map->calc_dir(target, bl->x, bl->y);
Assert_retr(1, dir >= UNIT_DIR_FIRST && dir < UNIT_DIR_MAX);
while (dist > 0 && map->getcell(bl->m, bl, bl->x + dist * dirx[dir], bl->y + dist * diry[dir], CELL_CHKNOREACH))
dist--;
if (dist > 0 && unit->walk_toxy(bl, bl->x + dist * dirx[dir], bl->y + dist * diry[dir], 0) == 0)
return 1;
else
return 0;
}
//Instant warp function.
static int unit_movepos(struct block_list *bl, short dst_x, short dst_y, int easy, bool checkpath)
{
short dx,dy;
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, STOPWALKING_FLAG_FIXPOS);
unit->stop_attack(bl);
if (checkpath && (map->getcell(bl->m, bl, dst_x, dst_y, CELL_CHKNOPASS) || !path->search(NULL, bl, 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;
enum unit_dir 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, 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
npc->untouch_areanpc(sd, bl->m, bl->x, bl->y);
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,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;
}
/**
* Sets the facing direction of a unit
* @param bl: unit to modify
* @param dir: the facing direction @see enum unit_dir
* @return 0: success, 1: failure
*/
static int unit_set_dir(struct block_list *bl, enum unit_dir dir)
{
nullpo_retr(1, bl);
struct unit_data *ud = unit->bl2ud(bl);
if (ud == NULL)
return 1;
ud->dir = dir;
if (bl->type == BL_PC)
BL_UCAST(BL_PC, bl)->head_dir = 0;
clif->changed_dir(bl, AREA);
return 0;
}
/**
* Get the facing direction of a unit
* @param bl: unit to request data from
* @return the facing direction @see enum unit_dir
*/
static enum unit_dir unit_getdir(const struct block_list *bl)
{
nullpo_retr(UNIT_DIR_NORTH, bl);
if (bl->type == BL_NPC)
return BL_UCCAST(BL_NPC, bl)->dir;
const struct unit_data *ud = unit->cbl2ud(bl);
if (ud == NULL)
return UNIT_DIR_NORTH;
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.
static 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, bl->m, bl->x, bl->y, dx, dy, count);
nx = result>>16;
ny = result&0xffff;
if(!su) {
unit->stop_walking(bl, STOPWALKING_FLAG_NONE);
}
if( sd ) {
unit->stop_stepaction(bl); //Stop stepaction when knocked back
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, bl->x, bl->y, CELL_CHKNPC)) {
npc->touch_areanpc(sd, bl->m, bl->x, bl->y);
} else {
npc->untouch_areanpc(sd, bl->m, bl->x, bl->y);;
}
}
}
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.
static int unit_warp(struct block_list *bl, short m, short x, short y, enum 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:
{
const struct mob_data *md = BL_UCCAST(BL_MOB, bl);
if (map->list[bl->m].flag.monster_noteleport && md->master_id == 0)
return 1;
if (m != bl->m && map->list[m].flag.nobranch && battle_config.mob_warp&4 && md->master_id == 0)
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:%u, target position map %d (%s) at [%d,%d]\n", bl->id, bl->type, m, map->list[m].name, x, y);
return 2;
}
} else if (bl->type != BL_NPC && map->getcell(m, bl, 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:%u, 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(BL_UCAST(BL_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: @see unit_stopwalking_flag.
* Upper bytes may be used for other purposes depending on the unit type.
*------------------------------------------*/
static int unit_stop_walking(struct block_list *bl, int flag)
{
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
//behavior changes in the future, this code could break!
td = timer->get(ud->walktimer);
timer->delete(ud->walktimer, unit->walk_toxy_timer);
ud->walktimer = INVALID_TIMER;
ud->state.change_walk_target = 0;
tick = timer->gettick();
if( (flag&STOPWALKING_FLAG_ONESTEP && !ud->walkpath.path_pos) //Force moving at least one cell.
|| (flag&STOPWALKING_FLAG_NEXTCELL && 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->walk_toxy_timer(INVALID_TIMER, tick, bl->id, ud->walkpath.path_pos);
}
if(flag&STOPWALKING_FLAG_FIXPOS)
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 && flag&~STOPWALKING_FLAG_MASK)
ud->canmove_tick = timer->gettick() + (flag>>8);
//Read, 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;
}
static int unit_skilluse_id(struct block_list *src, int target_id, uint16 skill_id, uint16 skill_lv)
{
int casttime = skill->cast_fix(src, skill_id, skill_lv);
int castcancel = skill->get_castcancel(skill_id);
int ret = unit->skilluse_id2(src, target_id, skill_id, skill_lv, casttime, castcancel);
struct map_session_data *sd = BL_CAST(BL_PC, src);
if (sd != NULL && (ret == 0 || !skill->is_item_skill(sd, skill_id, skill_lv)))
pc->itemskill_clear(sd);
return ret;
}
static 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]
*------------------------------------------*/
static 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) {
// Prevent moving while casting
if (sd == NULL)
return 0; // Only players are affected by SA_FREECAST and similar
if ((skill->get_inf2(ud->skill_id) & (INF2_FREE_CAST_REDUCED | INF2_FREE_CAST_NORMAL)) == 0) {
// Skills with an explicit free cast setting always allow walking regardless of SA_FREECAST
if ((skill->get_inf2(ud->skill_id) & INF2_GUILD_SKILL) != 0)
return 0; // SA_FREECAST doesn't affect guild skills
if (pc->checkskill(sd, SA_FREECAST) == 0)
return 0; // SA_FREECAST not available
}
}
if (DIFF_TICK(ud->canmove_tick, timer->gettick()) > 0)
return 0;
if (sd && (
pc_issit(sd) ||
sd->state.vending ||
sd->state.prevend ||
sd->state.buyingstore ||
sd->block_action.move
))
return 0; //Can't move
// Status changes that block movement
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_FALLENEMPIRE]
|| 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_HOVERING] )
|| 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_DEEP_SLEEP]
|| (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_NEEDLE_OF_PARALYZE]
|| sc->data[SC_VACUUM_EXTREME]
|| (sc->data[SC_FEAR] && sc->data[SC_FEAR]->val2 > 0)
|| sc->data[SC_NETHERWORLD]
|| sc->data[SC_SUHIDE]
|| (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;
}
// Icewall walk block special trapped monster mode
if(bl->type == BL_MOB) {
struct mob_data *md = BL_CAST(BL_MOB, bl);
if (md && ((md->status.mode&MD_BOSS && battle_config.boss_icewall_walk_block == 1 && map->getcell(bl->m, bl, bl->x, bl->y, CELL_CHKICEWALL))
|| (!(md->status.mode&MD_BOSS) && battle_config.mob_icewall_walk_block == 1 && map->getcell(bl->m, bl, bl->x, bl->y, CELL_CHKICEWALL)))) {
md->walktoxy_fail_count = 1; //Make sure rudeattacked skills are invoked
return 0;
}
}
return 1;
}
/*==========================================
* Resume running after a walk delay
*------------------------------------------*/
static int unit_resume_running(int tid, int64 tick, int id, intptr_t data)
{
struct unit_data *ud = (struct unit_data *)data;
struct map_session_data *sd = map->id2sd(id);
nullpo_ret(ud);
if(sd && pc_isridingwug(sd))
clif->skill_nodamage(ud->bl,ud->bl,RA_WUGDASH,ud->skill_lv,
sc_start4(ud->bl,ud->bl,status->skill2sc(RA_WUGDASH),100,ud->skill_lv,unit->getdir(ud->bl),0,0,1));
else
clif->skill_nodamage(ud->bl,ud->bl,TK_RUN,ud->skill_lv,
sc_start4(ud->bl,ud->bl,status->skill2sc(TK_RUN),100,ud->skill_lv,unit->getdir(ud->bl),0,0,0));
if (sd) clif->walkok(sd);
return 0;
}
/*==========================================
* Applies walk delay to character, considering that
* if type is 0, this is a damage induced delay: if previous delay is active, do not change it.
* if type is 1, this is a skill induced delay: walk-delay may only be increased, not decreased.
*------------------------------------------*/
static int unit_set_walkdelay(struct block_list *bl, int64 tick, int delay, int type)
{
struct unit_data *ud = unit->bl2ud(bl);
if (delay <= 0 || !ud) return 0;
nullpo_ret(bl);
if (type) {
//Bosses can ignore skill induced walkdelay (but not damage induced)
if (bl->type == BL_MOB && (BL_UCCAST(BL_MOB, bl)->status.mode&MD_BOSS))
return 0;
//Make sure walk delay is not decreased
if (DIFF_TICK(ud->canmove_tick, tick+delay) > 0)
return 0;
} else {
//Don't set walk delays when already trapped.
if (!unit->can_move(bl))
return 0;
//Immune to being stopped for double the flinch time
if (DIFF_TICK(ud->canmove_tick, tick-delay) > 0)
return 0;
}
ud->canmove_tick = tick + delay;
if (ud->walktimer != INVALID_TIMER) {
//Stop walking, if chasing, readjust timers.
if (delay == 1) {
//Minimal delay (walk-delay) disabled. Just stop walking.
unit->stop_walking(bl, STOPWALKING_FLAG_NEXTCELL);
} else {
//Resume running after can move again [Kevin]
if (ud->state.running) {
timer->add(ud->canmove_tick, unit->resume_running, bl->id, (intptr_t)ud);
} else {
unit->stop_walking(bl, STOPWALKING_FLAG_NEXTCELL);
if (ud->target)
timer->add(ud->canmove_tick + 1, unit->walktobl_timer, bl->id, ud->target);
}
}
}
return 1;
}
//-------------- stop here
static int unit_skilluse_id2(struct block_list *src, int target_id, uint16 skill_id, uint16 skill_lv, int casttime, int castcancel)
{
struct unit_data *ud;
struct status_data *tstatus;
struct status_change *sc;
struct map_session_data *sd = NULL;
struct block_list * target = NULL;
int64 tick = timer->gettick();
int temp = 0, range;
nullpo_ret(src);
if(status->isdead(src))
return 0; //Do not continue source is dead
sd = BL_CAST(BL_PC, src);
ud = unit->bl2ud(src);
if(ud == NULL) return 0;
sc = status->get_sc(src);
if (sc && !sc->count)
sc = NULL; //Unneeded
//temp: used to signal combo-skills right now.
if (sc && sc->data[SC_COMBOATTACK]
&& skill->is_combo(skill_id)
&& (sc->data[SC_COMBOATTACK]->val1 == skill_id
|| ( sd?skill->check_condition_castbegin(sd,skill_id,skill_lv):0 )
)
) {
if (sc->data[SC_COMBOATTACK]->val2)
target_id = sc->data[SC_COMBOATTACK]->val2;
else if( skill->get_inf(skill_id) != 1 ) // Only non-targetable skills should use auto target
target_id = ud->target;
if( skill->get_inf(skill_id)&INF_SELF_SKILL && skill->get_nk(skill_id)&NK_NO_DAMAGE )// exploit fix
target_id = src->id;
temp = 1;
} else if ( target_id == src->id &&
skill->get_inf(skill_id)&INF_SELF_SKILL &&
skill->get_inf2(skill_id)&INF2_NO_TARGET_SELF )
{
target_id = ud->target; //Auto-select target. [Skotlex]
temp = 1;
}
if (sd) {
//Target_id checking.
if(skill->not_ok(skill_id, sd)) // [MouseJstr]
return 0;
switch (skill_id) {
//Check for skills that auto-select target
case MO_CHAINCOMBO:
if (sc && sc->data[SC_BLADESTOP]) {
if ((target=map->id2bl(sc->data[SC_BLADESTOP]->val4)) == NULL)
return 0;
}
break;
case WE_MALE:
case WE_FEMALE:
{
struct map_session_data *p_sd = NULL;
if (!sd->status.partner_id)
return 0;
p_sd = map->charid2sd(sd->status.partner_id);
if (p_sd == NULL) {
clif->skill_fail(sd, skill_id, USESKILL_FAIL_LEVEL, 0, 0);
return 0;
}
target = &p_sd->bl;
}
break;
case GC_WEAPONCRUSH:
if( sc && sc->data[SC_COMBOATTACK] && sc->data[SC_COMBOATTACK]->val1 == GC_WEAPONBLOCKING ) {
if( (target=map->id2bl(sc->data[SC_COMBOATTACK]->val2)) == NULL ) {
clif->skill_fail(sd, skill_id, USESKILL_FAIL_GC_WEAPONBLOCKING, 0, 0);
return 0;
}
} else {
clif->skill_fail(sd, skill_id, USESKILL_FAIL_GC_WEAPONBLOCKING, 0, 0);
return 0;
}
break;
}
if (target)
target_id = target->id;
}
if (src->type==BL_HOM)
switch(skill_id) { //Homun-auto-target skills.
case HVAN_CHAOTIC:
target_id = ud->target; // Choose attack target for now
target = map->id2bl(target_id);
if (target != NULL)
break;
FALLTHROUGH // Attacking nothing, choose master as default target instead
case HLIF_HEAL:
case HLIF_AVOID:
case HAMI_DEFENCE:
case HAMI_CASTLE:
target = battle->get_master(src);
if (!target) return 0;
target_id = target->id;
}
if( !target ) // choose default target
target = map->id2bl(target_id);
if( !target || src->m != target->m || !src->prev || !target->prev )
return 0;
if( battle_config.ksprotection && sd && mob->ksprotected(src, target) )
return 0;
//Normally not needed because clif.c checks for it, but the at/char/script commands don't! [Skotlex]
if(ud->skilltimer != INVALID_TIMER && skill_id != SA_CASTCANCEL && skill_id != SO_SPELLFIST)
return 0;
if(skill->get_inf2(skill_id)&INF2_NO_TARGET_SELF && src->id == target_id)
return 0;
if(!status->check_skilluse(src, target, skill_id, 0))
return 0;
if( src != target && status->isdead(target) ) {
/**
* Skills that may be cast on dead targets
**/
switch( skill_id ) {
case NPC_WIDESOULDRAIN:
case PR_REDEMPTIO:
case ALL_RESURRECTION:
case WM_DEADHILLHERE:
break;
default:
return 1;
}
}
tstatus = status->get_status_data(target);
// Record the status of the previous skill)
if (sd) {
if ((skill->get_inf2(skill_id)&INF2_ENSEMBLE_SKILL) && skill->check_pc_partner(sd, skill_id, &skill_lv, 1, 0) < 1) {
clif->skill_fail(sd, skill_id, USESKILL_FAIL_LEVEL, 0, 0);
return 0;
}
switch (skill_id){
case SA_CASTCANCEL:
if (ud->skill_id != skill_id) {
sd->skill_id_old = ud->skill_id;
sd->skill_lv_old = ud->skill_lv;
}
break;
case BD_ENCORE:
//Prevent using the dance skill if you no longer have the skill in your tree.
if (!sd->skill_id_dance || pc->checkskill(sd, sd->skill_id_dance) <= 0){
clif->skill_fail(sd, skill_id, USESKILL_FAIL_LEVEL, 0, 0);
return 0;
}
sd->skill_id_old = skill_id;
break;
case WL_WHITEIMPRISON:
if (battle->check_target(src, target, BCT_SELF | BCT_ENEMY) < 0) {
clif->skill_fail(sd, skill_id, USESKILL_FAIL_TOTARGET, 0, 0);
return 0;
}
break;
case MG_FIREBOLT:
case MG_LIGHTNINGBOLT:
case MG_COLDBOLT:
sd->skill_id_old = skill_id;
sd->skill_lv_old = skill_lv;
break;
}
}
if (sd != NULL && skill->check_condition_castbegin(sd, skill_id, skill_lv) == 0)
return 0;
if (src->type == BL_MOB) {
const struct mob_data *src_md = BL_UCCAST(BL_MOB, src);
switch (skill_id) {
case NPC_SUMMONSLAVE:
case NPC_SUMMONMONSTER:
case AL_TELEPORT:
if (src_md->master_id != 0 && src_md->special_state.ai != AI_NONE)
return 0;
}
}
if (src->type == BL_NPC) // NPC-objects can override cast distance
range = AREA_SIZE; // Maximum visible distance before NPC goes out of sight
else
range = skill->get_range2(src, skill_id, skill_lv); // Skill cast distance from database
// New action request received, delete previous action request if not executed yet
if(ud->stepaction || ud->steptimer != INVALID_TIMER)
unit->stop_stepaction(src);
// Remember the skill request from the client while walking to the next cell
if(src->type == BL_PC && ud->walktimer != INVALID_TIMER && !battle->check_range(src, target, range-1)) {
ud->stepaction = true;
ud->target_to = target_id;
ud->stepskill_id = skill_id;
ud->stepskill_lv = skill_lv;
return 0; // Attacking will be handled by unit_walk_toxy_timer in this case
}
//Check range when not using skill on yourself or is a combo-skill during attack
//(these are supposed to always have the same range as your attack)
if( src->id != target_id && (!temp || ud->attacktimer == INVALID_TIMER) ) {
if( skill->get_state(ud->skill_id) == ST_MOVE_ENABLE ) {
if( !unit->can_reach_bl(src, target, range + 1, 1, NULL, NULL) )
return 0; // Walk-path check failed.
} else if( src->type == BL_MER && skill_id == MA_REMOVETRAP ) {
if( !battle->check_range(battle->get_master(src), target, range + 1) )
return 0; // Aegis calc remove trap based on Master position, ignoring mercenary O.O
} else if (!battle->check_range(src, target, range)) {
return 0; // Arrow-path check failed.
}
}
if (!temp) //Stop attack on non-combo skills [Skotlex]
unit->stop_attack(src);
else if(ud->attacktimer != INVALID_TIMER) //Else-wise, delay current attack sequence
ud->attackabletime = tick + status_get_adelay(src);
ud->state.skillcastcancel = castcancel;
//temp: Used to signal force cast now.
temp = 0;
switch(skill_id){
case ALL_RESURRECTION:
if(battle->check_undead(tstatus->race,tstatus->def_ele)) {
temp = 1;
} else if (!status->isdead(target))
return 0; //Can't cast on non-dead characters.
break;
case MO_FINGEROFFENSIVE:
if(sd)
casttime += casttime * min(skill_lv, sd->spiritball);
break;
case MO_EXTREMITYFIST:
if (sc && sc->data[SC_COMBOATTACK] &&
(sc->data[SC_COMBOATTACK]->val1 == MO_COMBOFINISH ||
sc->data[SC_COMBOATTACK]->val1 == CH_TIGERFIST ||
sc->data[SC_COMBOATTACK]->val1 == CH_CHAINCRUSH))
casttime = -1;
temp = 1;
break;
case CR_DEVOTION:
if (sd) {
int i = 0, count = min(skill_lv, 5);
ARR_FIND(0, count, i, sd->devotion[i] == target_id);
if (i == count) {
ARR_FIND(0, count, i, sd->devotion[i] == 0);
if(i == count) {
clif->skill_fail(sd, skill_id, USESKILL_FAIL_LEVEL, 0, 0);
return 0; // Can't cast on other characters when limit is reached
}
}
}
break;
case AB_CLEARANCE:
if (target->type != BL_MOB && battle->check_target(src, target, BCT_PARTY) <= 0 && sd) {
clif->skill_fail(sd, skill_id, USESKILL_FAIL_TOTARGET, 0, 0);
return 0;
}
break;
case SR_GATEOFHELL:
case SR_TIGERCANNON:
if (sc && sc->data[SC_COMBOATTACK] &&
sc->data[SC_COMBOATTACK]->val1 == SR_FALLENEMPIRE)
casttime = -1;
temp = 1;
break;
case SA_SPELLBREAKER:
temp = 1;
break;
case ST_CHASEWALK:
if (sc && sc->data[SC_CHASEWALK])
casttime = -1;
break;
case TK_RUN:
if (sc && sc->data[SC_RUN])
casttime = -1;
break;
case HP_BASILICA:
if( sc && sc->data[SC_BASILICA] )
casttime = -1; // No Casting time on basilica cancel
break;
case KN_CHARGEATK:
{
unsigned int k = (distance_bl(src,target)-1)/3; //+100% every 3 cells of distance
if( k > 2 ) k = 2; // ...but hard-limited to 300%.
casttime += casttime * k;
}
break;
case GD_EMERGENCYCALL: //Emergency Call double cast when the user has learned Leap [Daegaladh]
if (sd && (pc->checkskill(sd,TK_HIGHJUMP) || pc->checkskill(sd,SU_LOPE) >= 3))
casttime *= 2;
break;
case RA_WUGDASH:
if (sc && sc->data[SC_WUGDASH])
casttime = -1;
break;
case EL_WIND_SLASH:
case EL_HURRICANE:
case EL_TYPOON_MIS:
case EL_STONE_HAMMER:
case EL_ROCK_CRUSHER:
case EL_STONE_RAIN:
case EL_ICE_NEEDLE:
case EL_WATER_SCREW:
case EL_TIDAL_WEAPON:
if( src->type == BL_ELEM ){
sd = BL_CAST(BL_PC, battle->get_master(src));
if( sd && sd->skill_id_old == SO_EL_ACTION ){
casttime = -1;
sd->skill_id_old = 0;
}
}
break;
case NC_DISJOINT:
if (target->type == BL_PC) {
struct mob_data *md;
if( (md = map->id2md(target->id)) && md->master_id != src->id )
casttime <<= 1;
}
break;
}
// moved here to prevent Suffragium from ending if skill fails
#ifndef RENEWAL_CAST
if (!(skill->get_castnodex(skill_id, skill_lv)&2))
casttime = skill->cast_fix_sc(src, casttime);
#else
casttime = skill->vf_cast_fix(src, casttime, skill_id, skill_lv);
#endif
if (src->type == BL_NPC) { // NPC-objects do not have cast time
casttime = 0;
}
if( sc ) {
/**
* why the if else chain: these 3 status do not stack, so its efficient that way.
**/
if( sc->data[SC_CLOAKING] && !(sc->data[SC_CLOAKING]->val4&4) && skill_id != AS_CLOAKING ) {
status_change_end(src, SC_CLOAKING, INVALID_TIMER);
if (!src->prev) return 0; //Warped away!
} else if( sc->data[SC_CLOAKINGEXCEED] && !(sc->data[SC_CLOAKINGEXCEED]->val4&4) && skill_id != GC_CLOAKINGEXCEED ) {
status_change_end(src,SC_CLOAKINGEXCEED, INVALID_TIMER);
if (!src->prev) return 0;
}
}
if (!ud->state.running) //need TK_RUN or WUGDASH handler to be done before that, see bugreport:6026
unit->stop_walking(src, STOPWALKING_FLAG_FIXPOS);// even though this is not how official works but this will do the trick. bugreport:6829
if (sd != NULL && sd->state.itemskill_no_casttime == 1 && skill->is_item_skill(sd, skill_id, skill_lv))
casttime = 0;
// in official this is triggered even if no cast time.
clif->useskill(src, src->id, target_id, 0,0, skill_id, skill_lv, casttime);
if( casttime > 0 || temp )
{
if (sd != NULL && target->type == BL_MOB) {
struct mob_data *md = BL_UCAST(BL_MOB, target);
mob->skill_event(md, src, tick, -1); //Cast targeted skill event.
if (tstatus->mode&(MD_CASTSENSOR_IDLE|MD_CASTSENSOR_CHASE) &&
battle->check_target(target, src, BCT_ENEMY) > 0)
{
switch (md->state.skillstate) {
case MSS_RUSH:
case MSS_FOLLOW:
if (!(tstatus->mode&MD_CASTSENSOR_CHASE))
break;
md->target_id = src->id;
md->state.aggressive = (tstatus->mode&MD_ANGRY)?1:0;
md->min_chase = md->db->range3;
break;
case MSS_IDLE:
case MSS_WALK:
if (!(tstatus->mode&MD_CASTSENSOR_IDLE))
break;
md->target_id = src->id;
md->state.aggressive = (tstatus->mode&MD_ANGRY)?1:0;
md->min_chase = md->db->range3;
break;
}
}
}
}
if( casttime <= 0 )
ud->state.skillcastcancel = 0;
if( !sd || sd->skillitem != skill_id || skill->get_cast(skill_id,skill_lv) )
ud->canact_tick = tick + casttime + 100;
if( sd )
{
switch( skill_id )
{
case CG_ARROWVULCAN:
sd->canequip_tick = tick + casttime;
break;
}
}
ud->skilltarget = target_id;
ud->skillx = 0;
ud->skilly = 0;
ud->skill_id = skill_id;
ud->skill_lv = skill_lv;
if( casttime > 0 ) {
if (src->id != target->id) // self-targeted skills shouldn't show different direction
unit->set_dir(src, map->calc_dir(src, target->x, target->y));
ud->skilltimer = timer->add( tick+casttime, skill->castend_id, src->id, 0 );
if (sd && (pc->checkskill(sd, SA_FREECAST) > 0 || skill_id == LG_EXEEDBREAK || (skill->get_inf2(ud->skill_id) & INF2_FREE_CAST_REDUCED) != 0))
status_calc_bl(&sd->bl, SCB_SPEED|SCB_ASPD);
} else
skill->castend_id(ud->skilltimer,tick,src->id,0);
if (sd != NULL && battle_config.prevent_logout_trigger & PLT_SKILL)
sd->canlog_tick = timer->gettick();
return 1;
}
static int unit_skilluse_pos(struct block_list *src, short skill_x, short skill_y, uint16 skill_id, uint16 skill_lv)
{
int casttime = skill->cast_fix(src, skill_id, skill_lv);
int castcancel = skill->get_castcancel(skill_id);
int ret = unit->skilluse_pos2(src, skill_x, skill_y, skill_id, skill_lv, casttime, castcancel);
struct map_session_data *sd = BL_CAST(BL_PC, src);
if (sd != NULL && (ret == 0 || !skill->is_item_skill(sd, skill_id, skill_lv)))
pc->itemskill_clear(sd);
return ret;
}
static int unit_skilluse_pos2(struct block_list *src, short skill_x, short skill_y, uint16 skill_id, uint16 skill_lv, int casttime, int castcancel)
{
struct map_session_data *sd = NULL;
struct unit_data *ud = NULL;
struct status_change *sc;
struct block_list bl;
int64 tick = timer->gettick();
int range;
nullpo_ret(src);
if (!src->prev) return 0; // not on the map
if(status->isdead(src)) return 0;
sd = BL_CAST(BL_PC, src);
ud = unit->bl2ud(src);
if(ud == NULL) return 0;
if(ud->skilltimer != INVALID_TIMER) //Normally not needed since clif.c checks for it, but at/char/script commands don't! [Skotlex]
return 0;
sc = status->get_sc(src);
if (sc && !sc->count)
sc = NULL;
if( sd )
{
if( skill->not_ok(skill_id, sd) || !skill->check_condition_castbegin(sd, skill_id, skill_lv) )
return 0;
/**
* "WHY IS IT HEREE": ice wall cannot be canceled past this point, the client displays the animation even,
* if we cancel it from castend_pos, so it has to be here for it to not display the animation.
**/
if (skill_id == WZ_ICEWALL && map->getcell(src->m, src, skill_x, skill_y, CELL_CHKNOICEWALL))
return 0;
}
if (!status->check_skilluse(src, NULL, skill_id, 0))
return 0;
if (map->getcell(src->m, src, skill_x, skill_y, CELL_CHKWALL)) {
// can't cast ground targeted spells on wall cells
if (sd) clif->skill_fail(sd, skill_id, USESKILL_FAIL_LEVEL, 0, 0);
return 0;
}
/* Check range and obstacle */
bl.type = BL_NUL;
bl.m = src->m;
bl.x = skill_x;
bl.y = skill_y;
if (src->type == BL_NPC) // NPC-objects can override cast distance
range = AREA_SIZE; // Maximum visible distance before NPC goes out of sight
else
range = skill->get_range2(src, skill_id, skill_lv); // Skill cast distance from database
// New action request received, delete previous action request if not executed yet
if(ud->stepaction || ud->steptimer != INVALID_TIMER)
unit->stop_stepaction(src);
// Remember the skill request from the client while walking to the next cell
if(src->type == BL_PC && ud->walktimer != INVALID_TIMER && !battle->check_range(src, &bl, range-1)) {
struct map_data *md = &map->list[src->m];
// Convert coordinates to target_to so we can use it as target later
ud->stepaction = true;
ud->target_to = (skill_x + skill_y*md->xs);
ud->stepskill_id = skill_id;
ud->stepskill_lv = skill_lv;
return 0; // Attacking will be handled by unit_walk_toxy_timer in this case
}
if( skill->get_state(ud->skill_id) == ST_MOVE_ENABLE ) {
if( !unit->can_reach_bl(src, &bl, range + 1, 1, NULL, NULL) )
return 0; //Walk-path check failed.
} else if( !battle->check_range(src, &bl, range) )
return 0; //Arrow-path check failed.
unit->stop_attack(src);
// moved here to prevent Suffragium from ending if skill fails
#ifndef RENEWAL_CAST
if (!(skill->get_castnodex(skill_id, skill_lv)&2))
casttime = skill->cast_fix_sc(src, casttime);
#else
casttime = skill->vf_cast_fix(src, casttime, skill_id, skill_lv );
#endif
if (src->type == BL_NPC) { // NPC-objects do not have cast time
casttime = 0;
}
ud->state.skillcastcancel = castcancel&&casttime>0?1:0;
if( !sd || sd->skillitem != skill_id || skill->get_cast(skill_id,skill_lv) )
ud->canact_tick = tick + casttime + 100;
#if 0
if (sd) {
switch (skill_id) {
case ????:
sd->canequip_tick = tick + casttime;
}
}
#endif // 0
ud->skill_id = skill_id;
ud->skill_lv = skill_lv;
ud->skillx = skill_x;
ud->skilly = skill_y;
ud->skilltarget = 0;
if( sc ) {
/**
* why the if else chain: these 3 status do not stack, so its efficient that way.
**/
if (sc->data[SC_CLOAKING] && !(sc->data[SC_CLOAKING]->val4&4)) {
status_change_end(src, SC_CLOAKING, INVALID_TIMER);
if (!src->prev) return 0; //Warped away!
} else if (sc->data[SC_CLOAKINGEXCEED] && !(sc->data[SC_CLOAKINGEXCEED]->val4&4)) {
status_change_end(src, SC_CLOAKINGEXCEED, INVALID_TIMER);
if (!src->prev) return 0;
}
}
unit->stop_walking(src, STOPWALKING_FLAG_FIXPOS);
if (sd != NULL && sd->state.itemskill_no_casttime == 1 && skill->is_item_skill(sd, skill_id, skill_lv))
casttime = 0;
// in official this is triggered even if no cast time.
clif->useskill(src, src->id, 0, skill_x, skill_y, skill_id, skill_lv, casttime);
if( casttime > 0 ) {
unit->set_dir(src, map->calc_dir(src, skill_x, skill_y));
ud->skilltimer = timer->add( tick+casttime, skill->castend_pos, src->id, 0 );
if ((sd && pc->checkskill(sd, SA_FREECAST) > 0) || skill_id == LG_EXEEDBREAK || (skill->get_inf2(ud->skill_id) & INF2_FREE_CAST_REDUCED) != 0) {
status_calc_bl(&sd->bl, SCB_SPEED|SCB_ASPD);
}
} else {
ud->skilltimer = INVALID_TIMER;
skill->castend_pos(ud->skilltimer,tick,src->id,0);
}
if (sd != NULL && battle_config.prevent_logout_trigger & PLT_SKILL)
sd->canlog_tick = timer->gettick();
return 1;
}
/*========================================
* update a block's attack target
*----------------------------------------*/
static int unit_set_target(struct unit_data *ud, int target_id)
{
nullpo_ret(ud);
if (ud->target != target_id) {
struct unit_data * ux;
struct block_list* target;
if (ud->target && (target = map->id2bl(ud->target)) != NULL && (ux = unit->bl2ud(target)) != NULL && ux->target_count > 0)
--ux->target_count;
if (target_id && (target = map->id2bl(target_id)) != NULL && (ux = unit->bl2ud(target)) != NULL && ux->target_count < UCHAR_MAX)
++ux->target_count;
}
ud->target = target_id;
return 0;
}
/**
* Stop a unit's attacks
* @param bl: Object to stop
*/
static void unit_stop_attack(struct block_list *bl)
{
struct unit_data *ud;
nullpo_retv(bl);
ud = unit->bl2ud(bl);
nullpo_retv(ud);
//Clear target
unit->set_target(ud, 0);
if(ud->attacktimer == INVALID_TIMER)
return;
//Clear timer
timer->delete(ud->attacktimer, unit->attack_timer);
ud->attacktimer = INVALID_TIMER;
}
/**
* Stop a unit's step action
* @param bl: Object to stop
*/
static void unit_stop_stepaction(struct block_list *bl)
{
struct unit_data *ud;
nullpo_retv(bl);
ud = unit->bl2ud(bl);
nullpo_retv(ud);
//Clear remembered step action
ud->stepaction = false;
ud->target_to = 0;
ud->stepskill_id = 0;
ud->stepskill_lv = 0;
if(ud->steptimer == INVALID_TIMER)
return;
//Clear timer
timer->delete(ud->steptimer, unit->steptimer);
ud->steptimer = INVALID_TIMER;
}
//Means current target is unattackable. For now only unlocks mobs.
static int unit_unattackable(struct block_list *bl)
{
struct unit_data *ud = unit->bl2ud(bl);
nullpo_ret(bl);
if (ud) {
ud->state.attack_continue = 0;
ud->state.step_attack = 0;
unit->set_target(ud, 0);
}
if (bl->type == BL_MOB)
mob->unlocktarget(BL_UCAST(BL_MOB, bl), timer->gettick());
else if (bl->type == BL_PET)
pet->unlocktarget(BL_UCAST(BL_PET, bl));
return 0;
}
/*==========================================
* Attack request
* If type is an ongoing attack
*------------------------------------------*/
static int unit_attack(struct block_list *src, int target_id, int continuous)
{
struct block_list *target;
struct unit_data *ud;
int range;
nullpo_ret(src);
nullpo_ret(ud = unit->bl2ud(src));
target = map->id2bl(target_id);
if( target==NULL || status->isdead(target) ) {
unit->unattackable(src);
return 1;
}
if (src->type == BL_PC) {
struct map_session_data *sd = BL_UCAST(BL_PC, src);
if (target->type == BL_NPC) { // monster npcs [Valaris]
if (sd->block_action.npc == 0) { // *pcblock script command
npc->click(sd, BL_UCAST(BL_NPC, target)); // submitted by leinsirk10 [Celest]
}
return 0;
}
if( pc_is90overweight(sd) || pc_isridingwug(sd) ) { // overweight or mounted on warg - stop attacking
unit->stop_attack(src);
return 0;
}
if( !pc->can_attack(sd, target_id) ) {
unit->stop_attack(src);
return 0;
}
}
if( battle->check_target(src,target,BCT_ENEMY) <= 0 || !status->check_skilluse(src, target, 0, 0) ) {
unit->unattackable(src);
return 1;
}
ud->state.attack_continue = (continuous&1)?1:0;
ud->state.step_attack = (continuous&2)?1:0;
unit->set_target(ud, target_id);
range = status_get_range(src);
if (continuous) //If you're to attack continuously, set to auto-case character
ud->chaserange = range;
//Just change target/type. [Skotlex]
if(ud->attacktimer != INVALID_TIMER)
return 0;
// New action request received, delete previous action request if not executed yet
if(ud->stepaction || ud->steptimer != INVALID_TIMER)
unit->stop_stepaction(src);
// Remember the attack request from the client while walking to the next cell
if(src->type == BL_PC && ud->walktimer != INVALID_TIMER && !battle->check_range(src, target, range-1)) {
ud->stepaction = true;
ud->target_to = ud->target;
ud->stepskill_id = 0;
ud->stepskill_lv = 0;
return 0; // Attacking will be handled by unit_walk_toxy_timer in this case
}
if(DIFF_TICK(ud->attackabletime, timer->gettick()) > 0)
//Do attack next time it is possible. [Skotlex]
ud->attacktimer=timer->add(ud->attackabletime,unit->attack_timer,src->id,0);
else //Attack NOW.
unit->attack_timer(INVALID_TIMER, timer->gettick(), src->id, 0);
return 0;
}
//Cancels an ongoing combo, resets attackable time and restarts the
//attack timer to resume attacking after amotion time. [Skotlex]
static int unit_cancel_combo(struct block_list *bl)
{
struct unit_data *ud;
nullpo_ret(bl);
if (!status_change_end(bl, SC_COMBOATTACK, INVALID_TIMER))
return 0; //Combo wasn't active.
ud = unit->bl2ud(bl);
nullpo_ret(ud);
ud->attackabletime = timer->gettick() + status_get_amotion(bl);
if (ud->attacktimer == INVALID_TIMER)
return 1; //Nothing more to do.
timer->delete(ud->attacktimer, unit->attack_timer);
ud->attacktimer=timer->add(ud->attackabletime,unit->attack_timer,bl->id,0);
return 1;
}
/*==========================================
*
*------------------------------------------*/
static bool unit_can_reach_pos(struct block_list *bl, int x, int y, int easy)
{
nullpo_retr(false, bl);
if (bl->x == x && bl->y == y) //Same place
return true;
return path->search(NULL,bl,bl->m,bl->x,bl->y,x,y,easy,CELL_CHKNOREACH);
}
/*==========================================
*
*------------------------------------------*/
static bool unit_can_reach_bl(struct block_list *bl, struct block_list *tbl, int range, int easy, short *x, short *y)
{
short dx,dy;
struct walkpath_data wpd;
nullpo_retr(false, bl);
nullpo_retr(false, tbl);
if( bl->m != tbl->m)
return false;
if( bl->x==tbl->x && bl->y==tbl->y )
return true;
if(range>0 && !check_distance_bl(bl, tbl, range))
return false;
// It judges whether it can adjoin or not.
dx=tbl->x - bl->x;
dy=tbl->y - bl->y;
dx=(dx>0)?1:((dx<0)?-1:0);
dy=(dy>0)?1:((dy<0)?-1:0);
if (map->getcell(tbl->m, bl, tbl->x - dx, tbl->y - dy, CELL_CHKNOPASS)) {
int i;
//Look for a suitable cell to place in.
for (i=0;i<8 && map->getcell(tbl->m, bl, tbl->x - dirx[i], tbl->y - diry[i], CELL_CHKNOPASS); i++);
if (i==8) return false; //No valid cells.
dx = dirx[i];
dy = diry[i];
}
if (x) *x = tbl->x-dx;
if (y) *y = tbl->y-dy;
if (!path->search(&wpd,bl,bl->m,bl->x,bl->y,tbl->x-dx,tbl->y-dy,easy,CELL_CHKNOREACH))
return false;
#ifdef OFFICIAL_WALKPATH
if( !path->search_long(NULL, bl, bl->m, bl->x, bl->y, tbl->x-dx, tbl->y-dy, CELL_CHKNOPASS) // Check if there is an obstacle between
&& wpd.path_len > 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 false;
#endif
return true;
}
/*==========================================
* Calculates position of Pet/Mercenary/Homunculus/Elemental
*------------------------------------------*/
static int unit_calc_pos(struct block_list *bl, int tx, int ty, enum unit_dir dir)
{
int dx, dy, x, y;
struct unit_data *ud = unit->bl2ud(bl);
nullpo_ret(ud);
Assert_retr(1, dir >= UNIT_DIR_FIRST && dir < UNIT_DIR_MAX);
ud->to_x = tx;
ud->to_y = ty;
// 2 cells from Master Position
dx = -dirx[dir] * 2;
dy = -diry[dir] * 2;
x = tx + dx;
y = ty + dy;
if (!unit->can_reach_pos(bl, x, y, 0)) {
if( dx > 0 ) x--; else if( dx < 0 ) x++;
if( dy > 0 ) y--; else if( dy < 0 ) y++;
if (!unit->can_reach_pos(bl, x, y, 0)) {
int i;
for (i = 0; i < 12; i++) {
enum unit_dir k = rnd() % UNIT_DIR_MAX; // Pick a Random Dir
dx = -dirx[k] * 2;
dy = -diry[k] * 2;
x = tx + dx;
y = ty + dy;
if (unit->can_reach_pos(bl, x, y, 0)) {
break;
} else {
if( dx > 0 ) x--; else if( dx < 0 ) x++;
if( dy > 0 ) y--; else if( dy < 0 ) y++;
if( unit->can_reach_pos(bl, x, y, 0) )
break;
}
}
if (i == 12) {
x = tx; y = tx; // Exactly Master Position
if (!unit->can_reach_pos(bl, x, y, 0))
return 1;
}
}
}
ud->to_x = x;
ud->to_y = y;
return 0;
}
/*==========================================
* Continuous Attack (function timer)
*------------------------------------------*/
static int unit_attack_timer_sub(struct block_list *src, int tid, int64 tick)
{
struct block_list *target;
struct unit_data *ud;
struct status_data *sstatus;
struct map_session_data *sd = NULL;
struct mob_data *md = NULL;
int range;
if( (ud=unit->bl2ud(src))==NULL )
return 0;
if( ud->attacktimer != tid )
{
ShowError("unit_attack_timer %d != %d\n",ud->attacktimer,tid);
return 0;
}
sd = BL_CAST(BL_PC, src);
md = BL_CAST(BL_MOB, src);
ud->attacktimer = INVALID_TIMER;
target=map->id2bl(ud->target);
if( src == NULL || src->prev == NULL || target==NULL || target->prev == NULL )
return 0;
if( status->isdead(src) || status->isdead(target)
|| battle->check_target(src,target,BCT_ENEMY) <= 0 || !status->check_skilluse(src, target, 0, 0)
#ifdef OFFICIAL_WALKPATH
|| !path->search_long(NULL, src, src->m, src->x, src->y, target->x, target->y, CELL_CHKWALL)
#endif
|| (sd && !pc->can_attack(sd, ud->target) )
)
return 0; // can't attack under these conditions
if (src->m != target->m) {
if (src->type == BL_MOB && mob->warpchase(BL_UCAST(BL_MOB, src), target))
return 1; // Follow up.
return 0;
}
if (ud->skilltimer != INVALID_TIMER && !(sd && (pc->checkskill(sd, SA_FREECAST) > 0 || (skill->get_inf2(ud->skill_id) & (INF2_FREE_CAST_REDUCED | INF2_FREE_CAST_NORMAL)) != 0)))
return 0; // can't attack while casting
if (!battle_config.sdelay_attack_enable && DIFF_TICK(ud->canact_tick, tick) > 0 && !(sd && (pc->checkskill(sd, SA_FREECAST) > 0 || (skill->get_inf2(ud->skill_id) & (INF2_FREE_CAST_REDUCED | INF2_FREE_CAST_NORMAL)) != 0)))
{ // attacking when under cast delay has restrictions:
if( tid == INVALID_TIMER ) { //requested attack.
if(sd) clif->skill_fail(sd, 1, USESKILL_FAIL_SKILLINTERVAL, 0, 0);
return 0;
}
//Otherwise, we are in a combo-attack, delay this until your canact time is over. [Skotlex]
if( ud->state.attack_continue ) {
if( DIFF_TICK(ud->canact_tick, ud->attackabletime) > 0 )
ud->attackabletime = ud->canact_tick;
ud->attacktimer=timer->add(ud->attackabletime,unit->attack_timer,src->id,0);
}
return 1;
}
sstatus = status->get_status_data(src);
range = sstatus->rhw.range;
if( (unit->is_walking(target) || ud->state.step_attack)
&& (target->type == BL_PC || !map->getcell(target->m, src, target->x, target->y, CELL_CHKICEWALL)))
range++; // Extra range when chasing (does not apply to mobs locked in an icewall)
if(sd && !check_distance_client_bl(src,target,range)) {
// Player tries to attack but target is too far, notify client
clif->movetoattack(sd,target);
return 1;
} else if(md && !check_distance_bl(src,target,range)) {
// Monster: Chase if required
unit->walktobl(src,target,ud->chaserange,ud->state.walk_easy|2);
return 1;
}
if( !battle->check_range(src,target,range) ) {
//Within range, but no direct line of attack
if( ud->state.attack_continue ) {
if(ud->chaserange > 2) ud->chaserange-=2;
unit->walktobl(src,target,ud->chaserange,ud->state.walk_easy|2);
}
return 1;
}
//Sync packet only for players.
//Non-players use the sync packet on the walk timer. [Skotlex]
if (tid == INVALID_TIMER && sd) clif->fixpos(src);
map->freeblock_lock();
if( DIFF_TICK(ud->attackabletime,tick) <= 0 ) {
if (battle_config.attack_direction_change && (src->type&battle_config.attack_direction_change)) {
ud->dir = map->calc_dir(src, target->x,target->y );
}
if(ud->walktimer != INVALID_TIMER)
unit->stop_walking(src, STOPWALKING_FLAG_FIXPOS);
if(md) {
//First attack is always a normal attack
if(md->state.skillstate == MSS_ANGRY || md->state.skillstate == MSS_BERSERK) {
if (mob->skill_use(md,tick,-1)) {
map->freeblock_unlock();
return 1;
}
} else {
// Set mob's ANGRY/BERSERK states.
md->state.skillstate = md->state.aggressive?MSS_ANGRY:MSS_BERSERK;
}
if (sstatus->mode&MD_ASSIST && DIFF_TICK(md->last_linktime, tick) < MIN_MOBLINKTIME) {
// Link monsters nearby [Skotlex]
md->last_linktime = tick;
map->foreachinrange(mob->linksearch, src, md->db->range2, BL_MOB, md->class_, target, tick);
}
}
if (src->type == BL_PET && pet->attackskill(BL_UCAST(BL_PET, src), target->id)) {
map->freeblock_unlock();
return 1;
}
ud->attacktarget_lv = battle->weapon_attack(src,target,tick,0);
if(sd && sd->status.pet_id > 0 && sd->pd && battle_config.pet_attack_support)
pet->target_check(sd,target,0);
/**
* Applied when you're unable to attack (e.g. out of ammo)
* We should stop here otherwise timer keeps on and this happens endlessly
**/
if (ud->attacktarget_lv == ATK_NONE) {
map->freeblock_unlock();
return 1;
}
ud->attackabletime = tick + sstatus->adelay;
// You can't move if you can't attack neither.
if (src->type&battle_config.attack_walk_delay)
unit->set_walkdelay(src, tick, sstatus->amotion, 1);
}
if(ud->state.attack_continue) {
unit->set_dir(src, map->calc_dir(src, target->x, target->y));
if( src->type == BL_PC )
pc->update_idle_time(sd, BCIDLE_ATTACK);
ud->attacktimer = timer->add(ud->attackabletime,unit->attack_timer,src->id,0);
}
map->freeblock_unlock();
if (sd != NULL && battle_config.prevent_logout_trigger & PLT_ATTACK)
sd->canlog_tick = timer->gettick();
return 1;
}
static int unit_attack_timer(int tid, int64 tick, int id, intptr_t data)
{
struct block_list *bl;
bl = map->id2bl(id);
if(bl && unit->attack_timer_sub(bl, tid, tick) == 0)
unit->unattackable(bl);
return 0;
}
/*==========================================
* Cancels an ongoing skill cast.
* flag&1: Cast-Cancel invoked.
* flag&2: Cancel only if skill is can be cancel.
*------------------------------------------*/
static int unit_skillcastcancel(struct block_list *bl, int type)
{
struct map_session_data *sd = NULL;
struct unit_data *ud = unit->bl2ud( bl);
int64 tick = timer->gettick();
int ret=0, skill_id;
nullpo_ret(bl);
if (!ud || ud->skilltimer == INVALID_TIMER)
return 0; //Nothing to cancel.
sd = BL_CAST(BL_PC, bl);
if (type&2) {
//See if it can be canceled.
if (!ud->state.skillcastcancel)
return 0;
if (sd && (sd->special_state.no_castcancel2
|| (sd->special_state.no_castcancel && !map_flag_gvg(bl->m) && !map->list[bl->m].flag.battleground))) //fixed flags being read the wrong way around [blackhole89]
return 0;
}
ud->canact_tick = tick;
if(type&1 && sd)
skill_id = sd->skill_id_old;
else
skill_id = ud->skill_id;
if (skill->get_inf(skill_id) & INF_GROUND_SKILL)
ret = timer->delete( ud->skilltimer, skill->castend_pos );
else
ret = timer->delete( ud->skilltimer, skill->castend_id );
if( ret < 0 )
ShowError("delete timer error %d : skill %d (%s)\n",ret,skill_id,skill->get_name(skill_id));
ud->skilltimer = INVALID_TIMER;
if (sd && (pc->checkskill(sd, SA_FREECAST) > 0 || (skill->get_inf2(ud->skill_id) & INF2_FREE_CAST_REDUCED) != 0))
status_calc_bl(&sd->bl, SCB_SPEED|SCB_ASPD);
if( sd ) {
switch( skill_id ) {
case CG_ARROWVULCAN:
sd->canequip_tick = tick;
break;
}
}
if (bl->type == BL_MOB)
BL_UCAST(BL_MOB, bl)->skill_idx = -1;
clif->skillcastcancel(bl);
return 1;
}
// unit_data initialization process
static void unit_dataset(struct block_list *bl)
{
struct unit_data *ud = unit->bl2ud(bl);
nullpo_retv(ud);
unit->init_ud(ud);
ud->bl = bl;
}
static void unit_init_ud(struct unit_data *ud)
{
nullpo_retv(ud);
memset (ud, 0, sizeof(struct unit_data));
ud->walktimer = INVALID_TIMER;
ud->skilltimer = INVALID_TIMER;
ud->attacktimer = INVALID_TIMER;
ud->steptimer = INVALID_TIMER;
ud->attackabletime =
ud->canact_tick =
ud->canmove_tick = timer->gettick();
}
/*==========================================
* Counts the number of units attacking 'bl'
*------------------------------------------*/
static int unit_counttargeted(struct block_list *bl)
{
struct unit_data* ud;
if (bl && (ud = unit->bl2ud(bl)) != NULL)
return ud->target_count;
return 0;
}
/*==========================================
*
*------------------------------------------*/
static int unit_fixdamage(struct block_list *src, struct block_list *target, int sdelay, int ddelay, int64 damage, short div, unsigned char type, int64 damage2)
{
nullpo_ret(target);
if(damage+damage2 <= 0)
return 0;
return status_fix_damage(src,target,damage+damage2,clif->damage(target,target,sdelay,ddelay,damage,div,type,damage2));
}
/*==========================================
* To change the size of the char (player or mob only)
*------------------------------------------*/
static int unit_changeviewsize(struct block_list *bl, short size)
{
nullpo_ret(bl);
size=(size<0)?-1:(size>0)?1:0;
if(bl->type == BL_PC) {
BL_UCAST(BL_PC, bl)->state.size = size;
} else if(bl->type == BL_MOB) {
BL_UCAST(BL_MOB, bl)->special_state.size = size;
} else
return 0;
if(size!=0)
clif->specialeffect(bl,421+size, AREA);
return 0;
}
/*==========================================
* Removes a bl/ud from the map.
* Returns 1 on success. 0 if it couldn't be removed or the bl was free'd
* if clrtype is 1 (death), appropriate cleanup is performed.
* Otherwise it is assumed bl is being warped.
* On-Kill specific stuff is not performed here, look at status->damage for that.
*------------------------------------------*/
static int unit_remove_map(struct block_list *bl, enum clr_type clrtype, const char *file, int line, const char *func)
{
struct unit_data *ud = unit->bl2ud(bl);
struct status_change *sc = status->get_sc(bl);
nullpo_ret(bl);
nullpo_ret(ud);
if(bl->prev == NULL)
return 0; //Already removed?
map->freeblock_lock();
if (ud->walktimer != INVALID_TIMER)
unit->stop_walking(bl, STOPWALKING_FLAG_NONE);
if (ud->skilltimer != INVALID_TIMER)
unit->skillcastcancel(bl,0);
//Clear target even if there is no timer
if (ud->target || ud->attacktimer != INVALID_TIMER)
unit->stop_attack(bl);
//Clear stepaction even if there is no timer
if (ud->stepaction || ud->steptimer != INVALID_TIMER)
unit->stop_stepaction(bl);
// Do not reset can-act delay. [Skotlex]
ud->attackabletime = ud->canmove_tick /*= ud->canact_tick*/ = timer->gettick();
if(sc && sc->count ) { //map-change/warp dispells.
status_change_end(bl, SC_BLADESTOP, INVALID_TIMER);
status_change_end(bl, SC_BASILICA, INVALID_TIMER);
status_change_end(bl, SC_ANKLESNARE, INVALID_TIMER);
status_change_end(bl, SC_TRICKDEAD, INVALID_TIMER);
status_change_end(bl, SC_BLADESTOP_WAIT, INVALID_TIMER);
status_change_end(bl, SC_RUN, INVALID_TIMER);
status_change_end(bl, SC_DANCING, INVALID_TIMER);
status_change_end(bl, SC_WARM, INVALID_TIMER);
status_change_end(bl, SC_DEVOTION, INVALID_TIMER);
status_change_end(bl, SC_MARIONETTE_MASTER, INVALID_TIMER);
status_change_end(bl, SC_MARIONETTE, INVALID_TIMER);
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_HIDING, INVALID_TIMER);
// Ensure the bl is a PC; if so, we'll handle the removal of cloaking and cloaking exceed later
if ( bl->type != BL_PC ) {
status_change_end(bl, SC_CLOAKING, INVALID_TIMER);
status_change_end(bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
}
status_change_end(bl, SC_CHASEWALK, INVALID_TIMER);
if (sc->data[SC_GOSPEL] && sc->data[SC_GOSPEL]->val4 == BCT_SELF)
status_change_end(bl, SC_GOSPEL, INVALID_TIMER);
status_change_end(bl, SC_HLIF_CHANGE, INVALID_TIMER);
status_change_end(bl, SC_STOP, INVALID_TIMER);
status_change_end(bl, SC_WUGDASH, INVALID_TIMER);
status_change_end(bl, SC_CAMOUFLAGE, INVALID_TIMER);
status_change_end(bl, SC_MAGNETICFIELD, INVALID_TIMER);
status_change_end(bl, SC_NEUTRALBARRIER_MASTER, INVALID_TIMER);
status_change_end(bl, SC_NEUTRALBARRIER, INVALID_TIMER);
status_change_end(bl, SC_STEALTHFIELD_MASTER, INVALID_TIMER);
status_change_end(bl, SC_STEALTHFIELD, INVALID_TIMER);
status_change_end(bl, SC__SHADOWFORM, INVALID_TIMER);
status_change_end(bl, SC__MANHOLE, INVALID_TIMER);
status_change_end(bl, SC_VACUUM_EXTREME, INVALID_TIMER);
status_change_end(bl, SC_CURSEDCIRCLE_ATKER, INVALID_TIMER); //callme before warp
status_change_end(bl, SC_NETHERWORLD, INVALID_TIMER);
status_change_end(bl, SC_SUHIDE, INVALID_TIMER);
status_change_end(bl, SC_SV_ROOTTWIST, INVALID_TIMER);
}
if (bl->type&(BL_CHAR|BL_PET)) {
skill->unit_move(bl,timer->gettick(),4);
skill->cleartimerskill(bl);
}
switch( bl->type ) {
case BL_PC:
{
struct map_session_data *sd = BL_UCAST(BL_PC, bl);
if(sd->shadowform_id) {
struct block_list *d_bl = map->id2bl(sd->shadowform_id);
if( d_bl )
status_change_end(d_bl,SC__SHADOWFORM,INVALID_TIMER);
}
//Leave/reject all invitations.
if (sd->chat_id != 0)
chat->leave(sd, false);
if(sd->trade_partner)
trade->cancel(sd);
buyingstore->close(sd);
searchstore->close(sd);
if( sd->menuskill_id != AL_TELEPORT ) { // issue: 8027
if(sd->state.storage_flag == STORAGE_FLAG_NORMAL)
storage->pc_quit(sd,0);
else if (sd->state.storage_flag == STORAGE_FLAG_GUILD)
gstorage->pc_quit(sd,0);
sd->state.storage_flag = STORAGE_FLAG_CLOSED; //Force close it when being warped.
}
if(sd->party_invite>0)
party->reply_invite(sd,sd->party_invite,0);
if(sd->guild_invite>0)
guild->reply_invite(sd,sd->guild_invite,0);
if(sd->guild_alliance>0)
guild->reply_reqalliance(sd,sd->guild_alliance_account,0);
if(sd->menuskill_id)
sd->menuskill_id = sd->menuskill_val = 0;
if( sd->touching_id )
npc->touchnext_areanpc(sd,true);
// Check if warping and not changing the map.
if ( sd->state.warping && !sd->state.changemap ) {
status_change_end(bl, SC_CLOAKING, INVALID_TIMER);
status_change_end(bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
}
sd->npc_shopid = 0;
sd->adopt_invite = 0;
if(sd->pvp_timer != INVALID_TIMER) {
timer->delete(sd->pvp_timer,pc->calc_pvprank_timer);
sd->pvp_timer = INVALID_TIMER;
sd->pvp_rank = 0;
}
if(sd->duel_group > 0)
duel->leave(sd->duel_group, sd);
if(pc_issit(sd)) {
pc->setstand(sd);
skill->sit(sd,0);
}
party->send_dot_remove(sd);//minimap dot fix [Kevin]
guild->send_dot_remove(sd);
bg->send_dot_remove(sd);
if( map->list[bl->m].users <= 0 || sd->state.debug_remove_map ) {
// this is only place where map users is decreased, if the mobs were removed too soon then this function was executed too many times [FlavioJS]
if( sd->debug_file == NULL || !(sd->state.debug_remove_map) ) {
sd->debug_file = "";
sd->debug_line = 0;
sd->debug_func = "";
}
ShowDebug("unit_remove_map: unexpected state when removing player AID/CID:%d/%d"
" (active=%d connect_new=%d rewarp=%d changemap=%d debug_remove_map=%d)"
" from map=%s (users=%d)."
" Previous call from %s:%d(%s), current call from %s:%d(%s)."
" Please report this!!!\n",
sd->status.account_id, sd->status.char_id,
sd->state.active, sd->state.connect_new, sd->state.rewarp, sd->state.changemap, sd->state.debug_remove_map,
map->list[bl->m].name, map->list[bl->m].users,
sd->debug_file, sd->debug_line, sd->debug_func, file, line, func);
} else if (--map->list[bl->m].users == 0 && battle_config.dynamic_mobs) //[Skotlex]
map->removemobs(bl->m);
if (!(pc_isinvisible(sd))) {
// decrement the number of active pvp players on the map
--map->list[bl->m].users_pvp;
}
if( map->list[bl->m].instance_id >= 0 ) {
instance->list[map->list[bl->m].instance_id].users--;
instance->check_idle(map->list[bl->m].instance_id);
}
if( sd->state.hpmeter_visible ) {
map->list[bl->m].hpmeter_visible--;
sd->state.hpmeter_visible = 0;
}
sd->state.debug_remove_map = 1; // temporary state to track double remove_map's [FlavioJS]
sd->debug_file = file;
sd->debug_line = line;
sd->debug_func = func;
break;
}
case BL_MOB:
{
struct mob_data *md = BL_UCAST(BL_MOB, bl);
// Drop previous target mob_slave_keep_target: no.
if (!battle_config.mob_slave_keep_target)
md->target_id=0;
md->attacked_id=0;
md->state.skillstate= MSS_IDLE;
break;
}
case BL_PET:
{
struct pet_data *pd = BL_UCAST(BL_PET, bl);
if( pd->pet.intimate <= 0 && !(pd->msd && !pd->msd->state.active) ) {
//If logging out, this is deleted on unit->free
clif->clearunit_area(bl,clrtype);
map->delblock(bl);
unit->free(bl,CLR_OUTSIGHT);
map->freeblock_unlock();
return 0;
}
break;
}
case BL_HOM: {
struct homun_data *hd = BL_UCAST(BL_HOM, bl);
if( !hd->homunculus.intimacy && !(hd->master && !hd->master->state.active) ) {
//If logging out, this is deleted on unit->free
clif->emotion(bl, E_SOB);
clif->clearunit_area(bl,clrtype);
map->delblock(bl);
unit->free(bl,CLR_OUTSIGHT);
map->freeblock_unlock();
return 0;
}
break;
}
case BL_MER: {
struct mercenary_data *md = BL_UCAST(BL_MER, bl);
ud->canact_tick = ud->canmove_tick;
if( mercenary->get_lifetime(md) <= 0 && !(md->master && !md->master->state.active) ) {
clif->clearunit_area(bl,clrtype);
map->delblock(bl);
unit->free(bl,CLR_OUTSIGHT);
map->freeblock_unlock();
return 0;
}
break;
}
case BL_ELEM: {
struct elemental_data *ed = BL_UCAST(BL_ELEM, bl);
ud->canact_tick = ud->canmove_tick;
if( elemental->get_lifetime(ed) <= 0 && !(ed->master && !ed->master->state.active) ) {
clif->clearunit_area(bl,clrtype);
map->delblock(bl);
unit->free(bl,0);
map->freeblock_unlock();
return 0;
}
break;
}
default: break;// do nothing
}
/**
* BL_MOB is handled by mob_dead unless the monster is not dead.
**/
if( bl->type != BL_MOB || !status->isdead(bl) )
clif->clearunit_area(bl,clrtype);
map->delblock(bl);
map->freeblock_unlock();
return 1;
}
static void unit_remove_map_pc(struct map_session_data *sd, enum clr_type clrtype)
{
nullpo_retv(sd);
unit->remove_map(&sd->bl,clrtype,ALC_MARK);
//CLR_RESPAWN is the warp from logging out, CLR_TELEPORT is the warp from teleporting, but pets/homunc need to just 'vanish' instead of showing the warping animation.
if (clrtype == CLR_RESPAWN || clrtype == CLR_TELEPORT) clrtype = CLR_OUTSIGHT;
if(sd->pd)
unit->remove_map(&sd->pd->bl, clrtype, ALC_MARK);
if(homun_alive(sd->hd))
unit->remove_map(&sd->hd->bl, clrtype, ALC_MARK);
if(sd->md)
unit->remove_map(&sd->md->bl, clrtype, ALC_MARK);
if(sd->ed)
unit->remove_map(&sd->ed->bl, clrtype, ALC_MARK);
}
static void unit_free_pc(struct map_session_data *sd)
{
nullpo_retv(sd);
if (sd->pd) unit->free(&sd->pd->bl,CLR_OUTSIGHT);
if (sd->hd) unit->free(&sd->hd->bl,CLR_OUTSIGHT);
if (sd->md) unit->free(&sd->md->bl,CLR_OUTSIGHT);
if (sd->ed) unit->free(&sd->ed->bl,CLR_OUTSIGHT);
unit->free(&sd->bl,CLR_TELEPORT);
}
/*==========================================
* Function to free all related resources to the bl
* if unit is on map, it is removed using the clrtype specified
*------------------------------------------*/
static int unit_free(struct block_list *bl, enum clr_type clrtype)
{
struct unit_data *ud = unit->bl2ud( bl );
nullpo_ret(bl);
nullpo_ret(ud);
map->freeblock_lock();
if( bl->prev ) //Players are supposed to logout with a "warp" effect.
unit->remove_map(bl, clrtype, ALC_MARK);
switch( bl->type ) {
case BL_PC:
{
struct map_session_data *sd = BL_UCAST(BL_PC, bl);
sd->state.loggingout = 1;
if( status->isdead(bl) )
pc->setrestartvalue(sd,2);
pc->delinvincibletimer(sd);
pc->delautobonus(sd,sd->autobonus,ARRAYLENGTH(sd->autobonus),false);
pc->delautobonus(sd,sd->autobonus2,ARRAYLENGTH(sd->autobonus2),false);
pc->delautobonus(sd,sd->autobonus3,ARRAYLENGTH(sd->autobonus3),false);
if( sd->followtimer != INVALID_TIMER )
pc->stop_following(sd);
if( sd->duel_invite > 0 )
duel->reject(sd->duel_invite, sd);
// Notify friends that this char logged out. [Skotlex]
map->foreachpc(clif->friendslist_toggle_sub, sd->status.account_id, sd->status.char_id, 0);
party->send_logout(sd);
guild->send_memberinfoshort(sd,0);
clan->member_offline(sd);
pc->cleareventtimer(sd);
pc->inventory_rental_clear(sd);
pc->delspiritball(sd,sd->spiritball,1);
pc->del_charm(sd, sd->charm_count, sd->charm_type);
if( sd->st && sd->st->state != RUN ) {// free attached scripts that are waiting
script->free_state(sd->st);
sd->st = NULL;
sd->npc_id = 0;
}
if( sd->combos ) {
aFree(sd->combos);
sd->combos = NULL;
}
sd->combo_count = 0;
/* [Ind/Hercules] */
if( sd->sc_display_count ) {
int i;
for(i = 0; i < sd->sc_display_count; i++) {
ers_free(pc->sc_display_ers, sd->sc_display[i]);
}
sd->sc_display_count = 0;
}
if( sd->sc_display != NULL ) {
aFree(sd->sc_display);
sd->sc_display = NULL;
}
if( sd->instance != NULL ) {
aFree(sd->instance);
sd->instance = NULL;
}
VECTOR_CLEAR(sd->channels);
VECTOR_CLEAR(sd->script_queues);
VECTOR_CLEAR(sd->achievement); // Achievement [Smokexyz/Hercules]
VECTOR_CLEAR(sd->storage.item);
VECTOR_CLEAR(sd->hatEffectId);
VECTOR_CLEAR(sd->title_ids); // Title [Dastgir/Hercules]
sd->storage.received = false;
if( sd->quest_log != NULL ) {
aFree(sd->quest_log);
sd->quest_log = NULL;
sd->num_quests = sd->avail_quests = 0;
}
HPM->data_store_destroy(&sd->hdata);
break;
}
case BL_PET:
{
struct pet_data *pd = BL_UCAST(BL_PET, bl);
struct map_session_data *sd = pd->msd;
pet->hungry_timer_delete(pd);
if( pd->a_skill )
{
aFree(pd->a_skill);
pd->a_skill = NULL;
}
if( pd->s_skill )
{
if (pd->s_skill->timer != INVALID_TIMER) {
timer->delete(pd->s_skill->timer, pet->skill_support_timer);
}
aFree(pd->s_skill);
pd->s_skill = NULL;
}
if( pd->recovery )
{
if(pd->recovery->timer != INVALID_TIMER)
timer->delete(pd->recovery->timer, pet->recovery_timer);
aFree(pd->recovery);
pd->recovery = NULL;
}
if( pd->bonus )
{
if (pd->bonus->timer != INVALID_TIMER)
timer->delete(pd->bonus->timer, pet->skill_bonus_timer);
aFree(pd->bonus);
pd->bonus = NULL;
}
if( pd->loot )
{
pet->lootitem_drop(pd,sd);
if (pd->loot->item)
aFree(pd->loot->item);
aFree (pd->loot);
pd->loot = NULL;
}
if (pd->pet.intimate > 0) {
intif->save_petdata(pd->pet.account_id,&pd->pet);
} else {
//Remove pet.
intif->delete_petdata(pd->pet.pet_id);
if (sd) sd->status.pet_id = 0;
}
if( sd )
sd->pd = NULL;
break;
}
case BL_MOB:
{
struct mob_data *md = BL_UCAST(BL_MOB, bl);
if( md->spawn_timer != INVALID_TIMER )
{
timer->delete(md->spawn_timer,mob->delayspawn);
md->spawn_timer = INVALID_TIMER;
}
if( md->deletetimer != INVALID_TIMER )
{
timer->delete(md->deletetimer,mob->timer_delete);
md->deletetimer = INVALID_TIMER;
}
if( md->lootitem )
{
aFree(md->lootitem);
md->lootitem=NULL;
}
if( md->guardian_data )
{
struct guild_castle* gc = md->guardian_data->castle;
if( md->guardian_data->number >= 0 && md->guardian_data->number < MAX_GUARDIANS )
{
gc->guardian[md->guardian_data->number].id = 0;
}
else
{
int i;
ARR_FIND(0, gc->temp_guardians_max, i, gc->temp_guardians[i] == md->bl.id);
if( i < gc->temp_guardians_max )
gc->temp_guardians[i] = 0;
}
aFree(md->guardian_data);
md->guardian_data = NULL;
}
if( md->spawn )
{
md->spawn->active--;
if( !md->spawn->state.dynamic )
{// permanently remove the mob
if( --md->spawn->num == 0 )
{// Last freed mob is responsible for deallocating the group's spawn data.
aFree(md->spawn);
md->spawn = NULL;
}
}
}
if( md->base_status)
{
aFree(md->base_status);
md->base_status = NULL;
}
if( mob->is_clone(md->class_) )
mob->clone_delete(md);
if( md->tomb_nid )
mob->mvptomb_destroy(md);
HPM->data_store_destroy(&md->hdata);
break;
}
case BL_HOM:
{
struct homun_data *hd = BL_UCAST(BL_HOM, bl);
struct map_session_data *sd = hd->master;
homun->hunger_timer_delete(hd);
if( hd->homunculus.intimacy > 0 )
homun->save(hd);
else {
intif->homunculus_requestdelete(hd->homunculus.hom_id);
if( sd )
sd->status.hom_id = 0;
}
if( sd )
sd->hd = NULL;
break;
}
case BL_MER:
{
struct mercenary_data *md = BL_UCAST(BL_MER, bl);
struct map_session_data *sd = md->master;
if( mercenary->get_lifetime(md) > 0 )
mercenary->save(md);
else
{
intif->mercenary_delete(md->mercenary.mercenary_id);
if( sd )
sd->status.mer_id = 0;
}
if( sd )
sd->md = NULL;
mercenary->contract_stop(md);
break;
}
case BL_ELEM: {
struct elemental_data *ed = BL_UCAST(BL_ELEM, bl);
struct map_session_data *sd = ed->master;
if( elemental->get_lifetime(ed) > 0 )
elemental->save(ed);
else {
intif->elemental_delete(ed->elemental.elemental_id);
if( sd )
sd->status.ele_id = 0;
}
if( sd )
sd->ed = NULL;
elemental->summon_stop(ed);
break;
}
}
skill->clear_unitgroup(bl);
status->change_clear(bl,1);
map->deliddb(bl);
if( bl->type != BL_PC ) //Players are handled by map_quit
map->freeblock(bl);
map->freeblock_unlock();
return 0;
}
static int do_init_unit(bool minimal)
{
if (minimal)
return 0;
timer->add_func_list(unit->attack_timer, "unit_attack_timer");
timer->add_func_list(unit->walk_toxy_timer, "unit_walk_toxy_timer");
timer->add_func_list(unit->walktobl_timer, "unit_walktobl_timer");
timer->add_func_list(unit->delay_walk_toxy_timer, "unit_delay_walk_toxy_timer");
timer->add_func_list(unit->steptimer, "unit_steptimer");
return 0;
}
static int do_final_unit(void)
{
// nothing to do
return 0;
}
void unit_defaults(void)
{
unit = &unit_s;
unit->init = do_init_unit;
unit->final = do_final_unit;
/* */
unit->bl2ud = unit_bl2ud;
unit->cbl2ud = unit_cbl2ud;
unit->bl2ud2 = unit_bl2ud2;
unit->init_ud = unit_init_ud;
unit->attack_timer = unit_attack_timer;
unit->walk_toxy_timer = unit_walk_toxy_timer;
unit->walk_toxy_sub = unit_walk_toxy_sub;
unit->delay_walk_toxy_timer = unit_delay_walk_toxy_timer;
unit->walk_toxy = unit_walk_toxy;
unit->walktobl_timer = unit_walktobl_timer;
unit->walktobl = unit_walktobl;
unit->run = unit_run;
unit->run_hit = unit_run_hit;
unit->escape = unit_escape;
unit->movepos = unit_movepos;
unit->set_dir = unit_set_dir;
unit->getdir = unit_getdir;
unit->blown = unit_blown;
unit->warp = unit_warp;
unit->warpto_master = unit_warpto_master;
unit->stop_walking = unit_stop_walking;
unit->skilluse_id = unit_skilluse_id;
unit->steptimer = unit_steptimer;
unit->stop_stepaction = unit_stop_stepaction;
unit->is_walking = unit_is_walking;
unit->can_move = unit_can_move;
unit->resume_running = unit_resume_running;
unit->set_walkdelay = unit_set_walkdelay;
unit->skilluse_id2 = unit_skilluse_id2;
unit->skilluse_pos = unit_skilluse_pos;
unit->skilluse_pos2 = unit_skilluse_pos2;
unit->set_target = unit_set_target;
unit->stop_attack = unit_stop_attack;
unit->unattackable = unit_unattackable;
unit->attack = unit_attack;
unit->cancel_combo = unit_cancel_combo;
unit->can_reach_pos = unit_can_reach_pos;
unit->can_reach_bl = unit_can_reach_bl;
unit->calc_pos = unit_calc_pos;
unit->attack_timer_sub = unit_attack_timer_sub;
unit->skillcastcancel = unit_skillcastcancel;
unit->dataset = unit_dataset;
unit->counttargeted = unit_counttargeted;
unit->fixdamage = unit_fixdamage;
unit->changeviewsize = unit_changeviewsize;
unit->remove_map = unit_remove_map;
unit->remove_map_pc = unit_remove_map_pc;
unit->free_pc = unit_free_pc;
unit->free = unit_free;
}
|