summaryrefslogtreecommitdiff
path: root/src/map/battle.c
blob: 1fc7f0bc5f463e308615cf0e9df6634630a0ee9e (plain) (blame)
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
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
// Copyright (c) Hercules Dev Team, licensed under GNU GPL.
// See the LICENSE file
// Portions Copyright (c) Athena Dev Teams

#define HERCULES_CORE

#include "../config/core.h" // CELL_NOSTACK, CIRCULAR_AREA, CONSOLE_INPUT, HMAP_ZONE_DAMAGE_CAP_TYPE, OFFICIAL_WALKPATH, RENEWAL, RENEWAL_ASPD, RENEWAL_CAST, RENEWAL_DROP, RENEWAL_EDP, RENEWAL_EXP, RENEWAL_LVDMG, RE_LVL_DMOD(), RE_LVL_MDMOD(), RE_LVL_TMDMOD(), RE_SKILL_REDUCTION(), SCRIPT_CALLFUNC_CHECK, SECURE_NPCTIMEOUT, STATS_OPT_OUT
#include "battle.h"

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "battleground.h"
#include "chrif.h"
#include "clif.h"
#include "elemental.h"
#include "guild.h"
#include "homunculus.h"
#include "itemdb.h"
#include "map.h"
#include "mercenary.h"
#include "mob.h"
#include "party.h"
#include "path.h"
#include "pc.h"
#include "pet.h"
#include "skill.h"
#include "status.h"
#include "../common/HPM.h"
#include "../common/cbasetypes.h"
#include "../common/ers.h"
#include "../common/malloc.h"
#include "../common/nullpo.h"
#include "../common/random.h"
#include "../common/showmsg.h"
#include "../common/socket.h"
#include "../common/strlib.h"
#include "../common/sysinfo.h"
#include "../common/timer.h"
#include "../common/utils.h"

struct Battle_Config battle_config;
struct battle_interface battle_s;

int battle_getcurrentskill(struct block_list *bl) { //Returns the current/last skill in use by this bl.
	struct unit_data *ud;

	if( bl->type == BL_SKILL ) {
		struct skill_unit * su = (struct skill_unit*)bl;
		return su->group?su->group->skill_id:0;
	}

	ud = unit->bl2ud(bl);

	return ud?ud->skill_id:0;
}

/*==========================================
 * Get random targeting enemy
 *------------------------------------------*/
int battle_gettargeted_sub(struct block_list *bl, va_list ap) {
	struct block_list **bl_list;
	struct unit_data *ud;
	int target_id;
	int *c;

	bl_list = va_arg(ap, struct block_list **);
	c = va_arg(ap, int *);
	target_id = va_arg(ap, int);

	if (bl->id == target_id)
		return 0;

	if (*c >= 24)
		return 0;

	if ( !(ud = unit->bl2ud(bl)) )
		return 0;

	if (ud->target == target_id || ud->skilltarget == target_id) {
		bl_list[(*c)++] = bl;
		return 1;
	}

	return 0;
}

struct block_list* battle_gettargeted(struct block_list *target) {
	struct block_list *bl_list[24];
	int c = 0;
	nullpo_retr(NULL, target);

	memset(bl_list, 0, sizeof(bl_list));
	map->foreachinrange(battle->get_targeted_sub, target, AREA_SIZE, BL_CHAR, bl_list, &c, target->id);
	if ( c == 0 )
		return NULL;
	if( c > 24 )
		c = 24;
	return bl_list[rnd()%c];
}


//Returns the id of the current targeted character of the passed bl. [Skotlex]
int battle_gettarget(struct block_list* bl) {

	switch (bl->type) {
		case BL_PC:  return ((struct map_session_data*)bl)->ud.target;
		case BL_MOB: return ((struct mob_data*)bl)->target_id;
		case BL_PET: return ((struct pet_data*)bl)->target_id;
		case BL_HOM: return ((struct homun_data*)bl)->ud.target;
		case BL_MER: return ((struct mercenary_data*)bl)->ud.target;
		case BL_ELEM: return ((struct elemental_data*)bl)->ud.target;
	}

	return 0;
}

int battle_getenemy_sub(struct block_list *bl, va_list ap) {
	struct block_list **bl_list;
	struct block_list *target;
	int *c;

	bl_list = va_arg(ap, struct block_list **);
	c = va_arg(ap, int *);
	target = va_arg(ap, struct block_list *);

	if (bl->id == target->id)
		return 0;

	if (*c >= 24)
		return 0;

	if (status->isdead(bl))
		return 0;

	if (battle->check_target(target, bl, BCT_ENEMY) > 0) {
		bl_list[(*c)++] = bl;
		return 1;
	}

	return 0;
}

// Picks a random enemy of the given type (BL_PC, BL_CHAR, etc) within the range given. [Skotlex]
struct block_list* battle_getenemy(struct block_list *target, int type, int range) {
	struct block_list *bl_list[24];
	int c = 0;

	memset(bl_list, 0, sizeof(bl_list));
	map->foreachinrange(battle->get_enemy_sub, target, range, type, bl_list, &c, target);

	if ( c == 0 )
		return NULL;

	if( c > 24 )
		c = 24;

	return bl_list[rnd()%c];
}
int battle_getenemyarea_sub(struct block_list *bl, va_list ap) {
	struct block_list **bl_list, *src;
	int *c, ignore_id;

	bl_list = va_arg(ap, struct block_list **);
	c = va_arg(ap, int *);
	src = va_arg(ap, struct block_list *);
	ignore_id = va_arg(ap, int);

	if( bl->id == src->id || bl->id == ignore_id )
		return 0; // Ignores Caster and a possible pre-target

	if( *c >= 23 )
		return 0;

	if( status->isdead(bl) )
		return 0;

	if( battle->check_target(src, bl, BCT_ENEMY) > 0 ) {// Is Enemy!...
		bl_list[(*c)++] = bl;
		return 1;
	}

	return 0;
}

// Pick a random enemy
struct block_list* battle_getenemyarea(struct block_list *src, int x, int y, int range, int type, int ignore_id) {
	struct block_list *bl_list[24];
	int c = 0;

	memset(bl_list, 0, sizeof(bl_list));
	map->foreachinarea(battle->get_enemy_area_sub, src->m, x - range, y - range, x + range, y + range, type, bl_list, &c, src, ignore_id);

	if( c == 0 )
		return NULL;
	if( c >= 24 )
		c = 23;

	return bl_list[rnd()%c];
}

int battle_delay_damage_sub(int tid, int64 tick, int id, intptr_t data) {
	struct delay_damage *dat = (struct delay_damage *)data;

	if ( dat ) {
		struct block_list* src = NULL;
		struct block_list* target = map->id2bl(dat->target_id);

		if( !target || status->isdead(target) ) {/* nothing we can do */
			if( dat->src_type == BL_PC && ( src = map->id2bl(dat->src_id) ) && --((TBL_PC*)src)->delayed_damage == 0 && ((TBL_PC*)src)->state.hold_recalc ) {
				((TBL_PC*)src)->state.hold_recalc = 0;
				status_calc_pc(((TBL_PC*)src),SCO_FORCE);
			}
			ers_free(battle->delay_damage_ers, dat);
			return 0;
		}

		src = map->id2bl(dat->src_id);

		//Check to see if you haven't teleported. [Skotlex]
		if( src
		 && (target->type != BL_PC || ((TBL_PC*)target)->invincible_timer == INVALID_TIMER)
		 && (dat->skill_id == MO_EXTREMITYFIST || (target->m == src->m && check_distance_bl(src, target, dat->distance)) )
		) {
			map->freeblock_lock();
			status_fix_damage(src, target, dat->damage, dat->delay);
			if( dat->attack_type && !status->isdead(target) && dat->additional_effects )
				skill->additional_effect(src,target,dat->skill_id,dat->skill_lv,dat->attack_type,dat->dmg_lv,tick);
			if( dat->dmg_lv > ATK_BLOCK && dat->attack_type )
				skill->counter_additional_effect(src,target,dat->skill_id,dat->skill_lv,dat->attack_type,tick);
			map->freeblock_unlock();
		} else if( !src && dat->skill_id == CR_REFLECTSHIELD ) {
			/**
			 * it was monster reflected damage, and the monster died, we pass the damage to the character as expected
			 **/
			map->freeblock_lock();
			status_fix_damage(target, target, dat->damage, dat->delay);
			map->freeblock_unlock();
		}

		if( src && src->type == BL_PC && --((TBL_PC*)src)->delayed_damage == 0 && ((TBL_PC*)src)->state.hold_recalc ) {
			((TBL_PC*)src)->state.hold_recalc = 0;
			status_calc_pc(((TBL_PC*)src),SCO_FORCE);
		}
	}
	ers_free(battle->delay_damage_ers, dat);
	return 0;
}

int battle_delay_damage(int64 tick, int amotion, struct block_list *src, struct block_list *target, int attack_type, uint16 skill_id, uint16 skill_lv, int64 damage, enum damage_lv dmg_lv, int ddelay, bool additional_effects) {
	struct delay_damage *dat;
	struct status_change *sc;
	struct block_list *d_tbl = NULL;
	nullpo_ret(src);
	nullpo_ret(target);

	sc = status->get_sc(target);

	if (sc && sc->data[SC_DEVOTION] && sc->data[SC_DEVOTION]->val1)
		d_tbl = map->id2bl(sc->data[SC_DEVOTION]->val1);

	if (d_tbl && sc && check_distance_bl(target, d_tbl, sc->data[SC_DEVOTION]->val3) && damage > 0 && skill_id != PA_PRESSURE && skill_id != CR_REFLECTSHIELD)
		damage = 0;

	if ( !battle_config.delay_battle_damage || amotion <= 1 ) {
		map->freeblock_lock();
		status_fix_damage(src, target, damage, ddelay); // We have to separate here between reflect damage and others [icescope]
		if( attack_type && !status->isdead(target) && additional_effects )
			skill->additional_effect(src, target, skill_id, skill_lv, attack_type, dmg_lv, timer->gettick());
		if( dmg_lv > ATK_BLOCK && attack_type )
			skill->counter_additional_effect(src, target, skill_id, skill_lv, attack_type, timer->gettick());
		map->freeblock_unlock();
		return 0;
	}
	dat = ers_alloc(battle->delay_damage_ers, struct delay_damage);
	dat->src_id = src->id;
	dat->target_id = target->id;
	dat->skill_id = skill_id;
	dat->skill_lv = skill_lv;
	dat->attack_type = attack_type;
	dat->damage = damage;
	dat->dmg_lv = dmg_lv;
	dat->delay = ddelay;
	dat->distance = distance_bl(src, target) + (battle_config.snap_dodge ? 10 : battle_config.area_size);
	dat->additional_effects = additional_effects;
	dat->src_type = src->type;
	if (src->type != BL_PC && amotion > 1000)
		amotion = 1000; //Aegis places a damage-delay cap of 1 sec to non player attacks. [Skotlex]

	if( src->type == BL_PC ) {
		((TBL_PC*)src)->delayed_damage++;
	}

	timer->add(tick+amotion, battle->delay_damage_sub, 0, (intptr_t)dat);

	return 0;
}
int battle_attr_ratio(int atk_elem,int def_type, int def_lv)
{

	if (atk_elem < 0 || atk_elem >= ELE_MAX)
		return 100;

	if (def_type < 0 || def_type >= ELE_MAX || def_lv < 1 || def_lv > 4)
		return 100;

	return battle->attr_fix_table[def_lv-1][atk_elem][def_type];
}

/*==========================================
 * Does attribute fix modifiers.
 * Added passing of the chars so that the status changes can affect it. [Skotlex]
 * Note: Passing src/target == NULL is perfectly valid, it skips SC_ checks.
 *------------------------------------------*/
int64 battle_attr_fix(struct block_list *src, struct block_list *target, int64 damage,int atk_elem,int def_type, int def_lv)
{
	struct status_change *sc=NULL, *tsc=NULL;
	int ratio;

	if (src) sc = status->get_sc(src);
	if (target) tsc = status->get_sc(target);

	if (atk_elem < 0 || atk_elem >= ELE_MAX)
		atk_elem = rnd()%ELE_MAX;

	if (def_type < 0 || def_type >= ELE_MAX ||
		def_lv < 1 || def_lv > 4) {
		ShowError("battle_attr_fix: unknown attr type: atk=%d def_type=%d def_lv=%d\n",atk_elem,def_type,def_lv);
		return damage;
	}

	ratio = battle->attr_fix_table[def_lv-1][atk_elem][def_type];
	if (sc && sc->count) {
		if(sc->data[SC_VOLCANO] && atk_elem == ELE_FIRE)
			ratio += skill->enchant_eff[sc->data[SC_VOLCANO]->val1-1];
		if(sc->data[SC_VIOLENTGALE] && atk_elem == ELE_WIND)
			ratio += skill->enchant_eff[sc->data[SC_VIOLENTGALE]->val1-1];
		if(sc->data[SC_DELUGE] && atk_elem == ELE_WATER)
			ratio += skill->enchant_eff[sc->data[SC_DELUGE]->val1-1];
		if(sc->data[SC_FIRE_CLOAK_OPTION] && atk_elem == ELE_FIRE)
			damage += damage * sc->data[SC_FIRE_CLOAK_OPTION]->val2 / 100;
	}
	if( target && target->type == BL_SKILL ) {
		if( atk_elem == ELE_FIRE && battle->get_current_skill(target) == GN_WALLOFTHORN ) {
			struct skill_unit *su = (struct skill_unit*)target;
			struct skill_unit_group *sg;
			struct block_list *sgsrc;

			if(!su->alive
			 || (sg = su->group) == NULL || sg->val3 == -1
			 || (sgsrc = map->id2bl(sg->src_id)) == NULL || status->isdead(sgsrc)
			)
				return 0;

			if( sg->unit_id != UNT_FIREWALL ) {
				int x,y;
				x = sg->val3 >> 16;
				y = sg->val3 & 0xffff;
				skill->unitsetting(sgsrc,su->group->skill_id,su->group->skill_lv,x,y,1);
				sg->val3 = -1;
				sg->limit = DIFF_TICK32(timer->gettick(),sg->tick)+300;
			}
		}
	}
	if( tsc && tsc->count ) { //since an atk can only have one type let's optimize this a bit
		switch(atk_elem){
		case ELE_FIRE:
			if( tsc->data[SC_SPIDERWEB]) {
				tsc->data[SC_SPIDERWEB]->val1 = 0; // free to move now
				if( tsc->data[SC_SPIDERWEB]->val2-- > 0 )
					damage <<= 1; // double damage
				if( tsc->data[SC_SPIDERWEB]->val2 == 0 )
					status_change_end(target, SC_SPIDERWEB, INVALID_TIMER);
			}
			if( tsc->data[SC_THORNS_TRAP])
				status_change_end(target, SC_THORNS_TRAP, INVALID_TIMER);
			if( tsc->data[SC_COLD] && target->type != BL_MOB)
				status_change_end(target, SC_COLD, INVALID_TIMER);
			if( tsc->data[SC_EARTH_INSIGNIA]) damage += damage/2;
			if( tsc->data[SC_FIRE_CLOAK_OPTION])
				damage -= damage * tsc->data[SC_FIRE_CLOAK_OPTION]->val2 / 100;
			if( tsc->data[SC_VOLCANIC_ASH]) damage += damage/2; //150%
			break;
		case ELE_HOLY:
			if( tsc->data[SC_ORATIO]) ratio += tsc->data[SC_ORATIO]->val1 * 2;
			break;
		case ELE_POISON:
			if( tsc->data[SC_VENOMIMPRESS] && atk_elem == ELE_POISON ) ratio += tsc->data[SC_VENOMIMPRESS]->val2;
			break;
		case ELE_WIND:
			if( tsc->data[SC_COLD] && target->type != BL_MOB) damage += damage/2;
			if( tsc->data[SC_WATER_INSIGNIA]) damage += damage/2;
			break;
		case ELE_WATER:
			if( tsc->data[SC_FIRE_INSIGNIA]) damage += damage/2;
			break;
		case ELE_EARTH:
			if( tsc->data[SC_WIND_INSIGNIA]) damage += damage/2;
			break;
		}
	} //end tsc check

	if( ratio < 100 )
		return damage - (damage * (100 - ratio) / 100);
	else
		return damage + (damage * (ratio - 100) / 100);
}
int64 battle_calc_weapon_damage(struct block_list *src, struct block_list *bl, uint16 skill_id, uint16 skill_lv, struct weapon_atk *watk, int nk, bool n_ele, short s_ele, short s_ele_, int size, int type, int flag, int flag2){ // [malufett]
#ifdef RENEWAL
	int64 damage, eatk = 0;
	struct status_change *sc;
	struct map_session_data *sd;

	if( !src || !bl )
		return 0;

	sc = status->get_sc(src);
	sd = BL_CAST(BL_PC, src);

	damage = status->get_weapon_atk(src, watk, flag);

	if( sd ){
		if( type == EQI_HAND_R )
			damage = battle->calc_sizefix(sd, damage, EQI_HAND_R, size, flag&8);
		else
			damage = battle->calc_sizefix(sd, damage, EQI_HAND_L, size, flag&8);

		if( flag&2 && sd->bonus.arrow_atk )
			damage += sd->bonus.arrow_atk;

		if( sd->battle_status.equip_atk != 0 )
			eatk = sd->base_status.equip_atk;
	}

	if ( skill_id == TF_POISON )
		eatk += 15 * skill_lv;

	if( sc && sc->count ){
		if( sc->data[SC_ZENKAI] && watk->ele == sc->data[SC_ZENKAI]->val2 )
			eatk += 200;
#ifdef RENEWAL_EDP
		if( sc->data[SC_EDP] && skill_id != AS_GRIMTOOTH && skill_id != AS_VENOMKNIFE && skill_id != ASC_BREAKER ){
			eatk = eatk * (sc->data[SC_EDP]->val4 / 100 - 1);
			damage = damage * (sc->data[SC_EDP]->val4 / 100);
		}
#endif
	}

	if( skill_id != ASC_METEORASSAULT ){
		if( sc && sc->data[SC_SUB_WEAPONPROPERTY] ) // Temporary. [malufett]
			damage += damage * sc->data[SC_SUB_WEAPONPROPERTY]->val2 / 100;
	}

	// Temporary. [malufett]
	damage = battle->calc_elefix(src, bl, skill_id, skill_lv, damage + eatk, nk, n_ele, s_ele, s_ele_, type == EQI_HAND_L, flag);

	/**
	 * In RE Shield Boomerang takes weapon element only for damage calculation,
	 * - resist calculation is always against neutral
	**/
	if ( skill_id == CR_SHIELDBOOMERANG )
		s_ele = s_ele_ = ELE_NEUTRAL;

	// attacker side
	damage = battle->calc_cardfix(BF_WEAPON, src, bl, nk, s_ele, s_ele_, damage, 2|(type == EQI_HAND_L), flag2);

	// target side
	damage = battle->calc_cardfix(BF_WEAPON, src, bl, nk, s_ele, s_ele_, damage, 0, flag2);

	return damage;
#else
	return 0;
#endif
}
/*==========================================
 * Calculates the standard damage of a normal attack assuming it hits,
 * it calculates nothing extra fancy, is needed for magnum breaks WATK_ELEMENT bonus. [Skotlex]
 *------------------------------------------
 * Pass damage2 as NULL to not calc it.
 * Flag values:
 * &1: Critical hit
 * &2: Arrow attack
 * &4: Skill is Magic Crasher
 * &8: Skip target size adjustment (Extremity Fist?)
 *&16: Arrow attack but BOW, REVOLVER, RIFLE, SHOTGUN, GATLING or GRENADE type weapon not equipped (i.e. shuriken, kunai and venom knives not affected by DEX)
 */
/* 'battle_calc_base_damage' is used on renewal, 'battle_calc_base_damage2' otherwise. */
int64 battle_calc_base_damage(struct block_list *src, struct block_list *bl, uint16 skill_id, uint16 skill_lv, int nk, bool n_ele, short s_ele, short s_ele_, int type, int flag, int flag2) {
	int64 damage;
	struct status_data *st = status->get_status_data(src);
	struct status_change *sc = status->get_sc(src);

	if (src->type == BL_PC) {
		int64 batk;
		// Property from mild wind bypasses it
		if (sc && sc->data[SC_TK_SEVENWIND])
			batk = battle->calc_elefix(src, bl, skill_id, skill_lv, status->calc_batk(bl, sc, st->batk, false), nk, n_ele, s_ele, s_ele_, false, flag);
		else
			batk = battle->calc_elefix(src, bl, skill_id, skill_lv, status->calc_batk(bl, sc, st->batk, false), nk, n_ele, ELE_NEUTRAL, ELE_NEUTRAL, false, flag);
		if (type == EQI_HAND_L)
			damage = batk + 3 * battle->calc_weapon_damage(src, bl, skill_id, skill_lv, &st->lhw, nk, n_ele, s_ele, s_ele_, status_get_size(bl), type, flag, flag2) / 4;
		else
			damage = (batk << 1) + battle->calc_weapon_damage(src, bl, skill_id, skill_lv, &st->rhw, nk, n_ele, s_ele, s_ele_, status_get_size(bl), type, flag, flag2);
	}
	else{
		damage = st->batk + battle->calc_weapon_damage(src, bl, skill_id, skill_lv, &st->rhw, nk, n_ele, s_ele, s_ele_, status_get_size(bl), type, flag, flag2);
	}

	return damage;
}
int64 battle_calc_base_damage2(struct status_data *st, struct weapon_atk *wa, struct status_change *sc, unsigned short t_size, struct map_session_data *sd, int flag) {
	unsigned int atkmin=0, atkmax=0;
	short type = 0;
	int64 damage = 0;

	if (!sd) { //Mobs/Pets
		if(flag&4) {
			atkmin = st->matk_min;
			atkmax = st->matk_max;
		} else {
			atkmin = wa->atk;
			atkmax = wa->atk2;
		}
		if (atkmin > atkmax)
			atkmin = atkmax;
	} else { //PCs
		atkmax = wa->atk;
		type = (wa == &st->lhw)?EQI_HAND_L:EQI_HAND_R;

		if (!(flag&1) || (flag&2)) { //Normal attacks
			atkmin = st->dex;

			if (sd->equip_index[type] >= 0 && sd->inventory_data[sd->equip_index[type]])
				atkmin = atkmin*(80 + sd->inventory_data[sd->equip_index[type]]->wlv*20)/100;

			if (atkmin > atkmax)
				atkmin = atkmax;

			if(flag&2 && !(flag&16)) { //Bows
				atkmin = atkmin*atkmax/100;
				if (atkmin > atkmax)
					atkmax = atkmin;
			}
		}
	}

	if (sc && sc->data[SC_MAXIMIZEPOWER])
		atkmin = atkmax;

	//Weapon Damage calculation
	if (!(flag&1))
		damage = (atkmax>atkmin? rnd()%(atkmax-atkmin):0)+atkmin;
	else
		damage = atkmax;

	if (sd) {
		//rodatazone says the range is 0~arrow_atk-1 for non crit
		if (flag&2 && sd->bonus.arrow_atk)
			damage += ( (flag&1) ? sd->bonus.arrow_atk : rnd()%sd->bonus.arrow_atk );

		//SizeFix only for players
		if (!(sd->special_state.no_sizefix || (flag&8)))
			damage = damage * ( type == EQI_HAND_L ? sd->left_weapon.atkmods[t_size] : sd->right_weapon.atkmods[t_size] ) / 100;
	}

	//Finally, add baseatk
	if(flag&4)
		damage += st->matk_min;
	else
		damage += st->batk;

	//rodatazone says that Overrefined bonuses are part of baseatk
	//Here we also apply the weapon_atk_rate bonus so it is correctly applied on left/right hands.
	if(sd) {
		if (type == EQI_HAND_L) {
			if(sd->left_weapon.overrefine)
				damage += rnd()%sd->left_weapon.overrefine+1;
			if (sd->weapon_atk_rate[sd->weapontype2])
				damage += damage * sd->weapon_atk_rate[sd->weapontype2] / 100;
		} else { //Right hand
			if(sd->right_weapon.overrefine)
				damage += rnd()%sd->right_weapon.overrefine+1;
			if (sd->weapon_atk_rate[sd->weapontype1])
				damage += damage * sd->weapon_atk_rate[sd->weapontype1] / 100;
		}
	}
	return damage;
}

int64 battle_calc_sizefix(struct map_session_data *sd, int64 damage, int type, int size,  bool ignore){
	//SizeFix only for players
	if (!(sd->special_state.no_sizefix || (ignore)))
		damage = damage * ( type == EQI_HAND_L ? sd->left_weapon.atkmods[size] : sd->right_weapon.atkmods[size] ) / 100;
	return damage;
}

/*==========================================
 * Passive skill damages increases
 *------------------------------------------*/
int64 battle_addmastery(struct map_session_data *sd,struct block_list *target,int64 dmg,int type) {
	int64 damage;
	struct status_data *st = status->get_status_data(target);
	int weapon, skill_lv;
	damage = dmg;

	nullpo_ret(sd);

	if((skill_lv = pc->checkskill(sd,AL_DEMONBANE)) > 0 &&
		target->type == BL_MOB && //This bonus doesn't work against players.
		(battle->check_undead(st->race,st->def_ele) || st->race==RC_DEMON) )
		damage += (int)(skill_lv*(3+sd->status.base_level/20.0));
		//damage += (skill_lv * 3);
	if( (skill_lv = pc->checkskill(sd, RA_RANGERMAIN)) > 0 && (st->race == RC_BRUTE || st->race == RC_PLANT || st->race == RC_FISH) )
		damage += (skill_lv * 5);
	if( (skill_lv = pc->checkskill(sd,NC_RESEARCHFE)) > 0 && (st->def_ele == ELE_FIRE || st->def_ele == ELE_EARTH) )
		damage += (skill_lv * 10);
	if( pc_ismadogear(sd) )
		damage += 15 * pc->checkskill(sd, NC_MADOLICENCE);
#ifdef RENEWAL
	if( (skill_lv = pc->checkskill(sd,BS_WEAPONRESEARCH)) > 0 )
		damage += (skill_lv * 2);
#endif
	if((skill_lv = pc->checkskill(sd,HT_BEASTBANE)) > 0 && (st->race==RC_BRUTE || st->race==RC_INSECT) ) {
		damage += (skill_lv * 4);
		if (sd->sc.data[SC_SOULLINK] && sd->sc.data[SC_SOULLINK]->val2 == SL_HUNTER)
			damage += sd->status.str;
	}

	if(type == 0)
		weapon = sd->weapontype1;
	else
		weapon = sd->weapontype2;
	switch(weapon) {
		case W_1HSWORD:
			#ifdef RENEWAL
				if((skill_lv = pc->checkskill(sd,AM_AXEMASTERY)) > 0)
					damage += (skill_lv * 3);
			#endif
		case W_DAGGER:
			if((skill_lv = pc->checkskill(sd,SM_SWORD)) > 0)
				damage += (skill_lv * 4);
			if((skill_lv = pc->checkskill(sd,GN_TRAINING_SWORD)) > 0)
				damage += skill_lv * 10;
			break;
		case W_2HSWORD:
			#ifdef RENEWAL
				if((skill_lv = pc->checkskill(sd,AM_AXEMASTERY)) > 0)
					damage += (skill_lv * 3);
			#endif
			if((skill_lv = pc->checkskill(sd,SM_TWOHAND)) > 0)
				damage += (skill_lv * 4);
			break;
		case W_1HSPEAR:
		case W_2HSPEAR:
			if ((skill_lv = pc->checkskill(sd,KN_SPEARMASTERY)) > 0) {
				if (pc_isridingdragon(sd))
					damage += (skill_lv * 10);
				else if (pc_isridingpeco(sd))
					damage += (skill_lv * 5);
				else
					damage += (skill_lv * 4);
			}
			break;
		case W_1HAXE:
		case W_2HAXE:
			if((skill_lv = pc->checkskill(sd,AM_AXEMASTERY)) > 0)
				damage += (skill_lv * 3);
			if((skill_lv = pc->checkskill(sd,NC_TRAININGAXE)) > 0)
				damage += (skill_lv * 5);
			break;
		case W_MACE:
		case W_2HMACE:
			if((skill_lv = pc->checkskill(sd,PR_MACEMASTERY)) > 0)
				damage += (skill_lv * 3);
			if((skill_lv = pc->checkskill(sd,NC_TRAININGAXE)) > 0)
				damage += (skill_lv * 5);
			break;
		case W_FIST:
			if((skill_lv = pc->checkskill(sd,TK_RUN)) > 0)
				damage += (skill_lv * 10);
			// No break, fall through to Knuckles
		case W_KNUCKLE:
			if((skill_lv = pc->checkskill(sd,MO_IRONHAND)) > 0)
				damage += (skill_lv * 3);
			break;
		case W_MUSICAL:
			if((skill_lv = pc->checkskill(sd,BA_MUSICALLESSON)) > 0)
				damage += (skill_lv * 3);
			break;
		case W_WHIP:
			if((skill_lv = pc->checkskill(sd,DC_DANCINGLESSON)) > 0)
				damage += (skill_lv * 3);
			break;
		case W_BOOK:
			if((skill_lv = pc->checkskill(sd,SA_ADVANCEDBOOK)) > 0)
				damage += (skill_lv * 3);
			break;
		case W_KATAR:
			if((skill_lv = pc->checkskill(sd,AS_KATAR)) > 0)
				damage += (skill_lv * 3);
			break;
	}

	return damage;
}

/*==========================================
 * Calculates ATK masteries.
 *------------------------------------------*/
int64 battle_calc_masteryfix(struct block_list *src, struct block_list *target, uint16 skill_id, uint16 skill_lv, int64 damage, int div, bool left, bool weapon) {
	int skill2_lv, i;
	struct status_change *sc;
	struct map_session_data *sd;
	struct status_data *tstatus;

	nullpo_ret(src);
	nullpo_ret(target);

	sc = status->get_sc(src);
	sd = BL_CAST(BL_PC, src);
	tstatus = status->get_status_data(target);

	if ( !sd )
		return damage;

	damage = battle->add_mastery(sd, target, damage, left);

	switch( skill_id ){ // specific skill masteries
		case MO_INVESTIGATE:
		case MO_EXTREMITYFIST:
		case CR_GRANDCROSS:
		case NJ_ISSEN:
		case CR_ACIDDEMONSTRATION:
			return damage;
		case NJ_SYURIKEN:
			if( (skill2_lv = pc->checkskill(sd,NJ_TOBIDOUGU)) > 0
#ifndef RENEWAL
				&& weapon
#endif
				)
				damage += 3 * skill2_lv;
			break;
#ifndef RENEWAL
		case NJ_KUNAI:
			if( weapon )
				damage += 60;
			break;
#endif
		case RA_WUGDASH://(Caster Current Weight x 10 / 8)
			if( sd->weight )
				damage += sd->weight / 8;
			/* Fall through */
		case RA_WUGSTRIKE:
		case RA_WUGBITE:
			damage += 30*pc->checkskill(sd, RA_TOOTHOFWUG);
			break;
		case HT_FREEZINGTRAP:
			damage += 40 * pc->checkskill(sd, RA_RESEARCHTRAP);
			break;
		default:
			battle->calc_masteryfix_unknown(src, target, &skill_id, &skill_lv, &damage, &div, &left, &weapon);
			break;
	}

	if( sc ){ // sc considered as masteries
		if(sc->data[SC_GN_CARTBOOST])
			damage += 10 * sc->data[SC_GN_CARTBOOST]->val1;
		if(sc->data[SC_CAMOUFLAGE])
			damage += 30 * ( 10 - sc->data[SC_CAMOUFLAGE]->val4 );
#ifdef RENEWAL
		if(sc->data[SC_NIBELUNGEN] && weapon)
			damage += sc->data[SC_NIBELUNGEN]->val2;
		if(sc->data[SC_IMPOSITIO])
			damage += sc->data[SC_IMPOSITIO]->val2;
		if(sc->data[SC_DRUMBATTLE]){
			if(tstatus->size == SZ_SMALL)
				damage += sc->data[SC_DRUMBATTLE]->val2;
			else if(tstatus->size == SZ_MEDIUM)
				damage += 10 * sc->data[SC_DRUMBATTLE]->val1;
			//else no bonus for large target
		}
		if(sc->data[SC_GS_MADNESSCANCEL])
			damage += 100;
		if(sc->data[SC_GS_GATLINGFEVER]){
			if(tstatus->size == SZ_SMALL)
				damage += 10 * sc->data[SC_GS_GATLINGFEVER]->val1;
			else if(tstatus->size == SZ_MEDIUM)
				damage += -5 * sc->data[SC_GS_GATLINGFEVER]->val1;
			else
				damage += sc->data[SC_GS_GATLINGFEVER]->val1;
		}
#if 0
		if(sc->data[SC_SPECIALZONE])
			damage += sc->data[SC_SPECIALZONE]->val2 >> 4;
#endif // 0
#endif // RENEWAL
	}

	// general skill masteries
#ifdef RENEWAL
	if( div < 0 ) // div fix
		div = 1;
	if( skill_id == MO_FINGEROFFENSIVE )//The finger offensive spheres on moment of attack do count. [Skotlex]
		damage += div * sd->spiritball_old * 3;
	else
		damage += div * sd->spiritball * 3;
	if( skill_id != CR_SHIELDBOOMERANG ) // Only Shield boomerang doesn't takes the Star Crumbs bonus.
		damage += div * (left ? sd->left_weapon.star : sd->right_weapon.star);
	if( skill_id != MC_CARTREVOLUTION && (skill2_lv=pc->checkskill(sd,BS_HILTBINDING)) > 0 )
		damage += 4;

	if(sd->status.party_id && (skill2_lv=pc->checkskill(sd,TK_POWER)) > 0) {
		if( (i = party->foreachsamemap(party->sub_count, sd, 0)) > 1 )
			damage += 2 * skill2_lv * i * (damage /*+ unknown value*/)  / 100 /*+ unknown value*/;
	}
#else
	if( skill_id != ASC_BREAKER && weapon ) // Adv Katar Mastery is does not applies to ASC_BREAKER, but other masteries DO apply >_>
		if( sd->status.weapon == W_KATAR && (skill2_lv=pc->checkskill(sd,ASC_KATAR)) > 0 )
			damage += damage * (10 + 2 * skill2_lv) / 100;
#endif

	// percentage factor masteries
	if ( sc && sc->data[SC_MIRACLE] )
		i = 2; //Star anger
	else
		ARR_FIND(0, MAX_PC_FEELHATE, i, status->get_class(target) == sd->hate_mob[i]);
	if ( i < MAX_PC_FEELHATE && (skill2_lv=pc->checkskill(sd,pc->sg_info[i].anger_id)) && weapon ) {
		int ratio = sd->status.base_level + status_get_dex(src) + status_get_luk(src);
		if ( i == 2 ) ratio += status_get_str(src); //Star Anger
		if  (skill2_lv < 4 )
			ratio /= (12 - 3 * skill2_lv);
		damage += damage * ratio / 100;
	}

	if( sd->status.class_ == JOB_ARCH_BISHOP_T || sd->status.class_ == JOB_ARCH_BISHOP ){
		if((skill2_lv = pc->checkskill(sd,AB_EUCHARISTICA)) > 0 &&
			(tstatus->race == RC_DEMON || tstatus->def_ele == ELE_DARK) )
			damage += damage * skill2_lv / 100;
	}

	return damage;
}

void battle_calc_masteryfix_unknown(struct block_list *src, struct block_list *target, uint16 *skill_id, uint16 *skill_lv, int64 *damage, int *div, bool *left, bool *weapon) {
}

/*==========================================
 * Elemental attribute fix.
 *------------------------------------------*/
int64 battle_calc_elefix(struct block_list *src, struct block_list *target, uint16 skill_id, uint16 skill_lv, int64 damage, int nk, int n_ele, int s_ele, int s_ele_, bool left, int flag){
	struct status_data *tstatus;

	nullpo_ret(src);
	nullpo_ret(target);

	tstatus = status->get_status_data(target);

	if( (nk&NK_NO_ELEFIX) || n_ele )
		return damage;

	if( damage > 0 ) {
		if( left )
			damage = battle->attr_fix(src, target, damage, s_ele_, tstatus->def_ele, tstatus->ele_lv);
		else{
			damage=battle->attr_fix(src, target, damage, s_ele, tstatus->def_ele, tstatus->ele_lv);
			if( skill_id == MC_CARTREVOLUTION ) //Cart Revolution applies the element fix once more with neutral element
				damage = battle->attr_fix(src,target,damage,ELE_NEUTRAL,tstatus->def_ele, tstatus->ele_lv);
			if( skill_id == NC_ARMSCANNON )
				damage = battle->attr_fix(src,target,damage,ELE_NEUTRAL,tstatus->def_ele, tstatus->ele_lv);
			if( skill_id == GN_CARTCANNON )
				damage = battle->attr_fix(src,target,damage,ELE_NEUTRAL,tstatus->def_ele, tstatus->ele_lv);
			if( skill_id == GS_GROUNDDRIFT ) //Additional 50*lv Neutral damage.
				damage += battle->attr_fix(src,target,50*skill_lv,ELE_NEUTRAL,tstatus->def_ele, tstatus->ele_lv);
		}
	}

#ifndef RENEWAL
	{
		struct status_data *sstatus;
		struct status_change *sc;

		sstatus = status->get_status_data(src);
		sc = status->get_sc(src);

		if( sc && sc->data[SC_SUB_WEAPONPROPERTY] ) { // Descriptions indicate this means adding a percent of a normal attack in another element. [Skotlex]
			int64 temp = battle->calc_base_damage2(sstatus, &sstatus->rhw, sc, tstatus->size, BL_CAST(BL_PC, src), (flag?2:0)) * sc->data[SC_SUB_WEAPONPROPERTY]->val2 / 100;
			damage += battle->attr_fix(src, target, temp, sc->data[SC_SUB_WEAPONPROPERTY]->val1, tstatus->def_ele, tstatus->ele_lv);
			if( left ) {
				temp = battle->calc_base_damage2(sstatus, &sstatus->lhw, sc, tstatus->size, BL_CAST(BL_PC, src), (flag?2:0)) * sc->data[SC_SUB_WEAPONPROPERTY]->val2 / 100;
				damage += battle->attr_fix(src, target, temp, sc->data[SC_SUB_WEAPONPROPERTY]->val1, tstatus->def_ele, tstatus->ele_lv);
			}
		}
	}
#endif
	return damage;
}
/*==========================================
 * Calculates card bonuses damage adjustments.
 * cflag(cardfix flag):
 * &1 - calc for left hand.
 * &2 - atker side cardfix(BF_WEAPON) otherwise target side(BF_WEAPON).
 *------------------------------------------*/
int64 battle_calc_cardfix(int attack_type, struct block_list *src, struct block_list *target, int nk, int s_ele, int s_ele_, int64 damage, int cflag, int wflag){
	struct map_session_data *sd, *tsd;
	short cardfix = 1000, t_class, s_class, s_race2, t_race2;
	struct status_data *sstatus, *tstatus;
	int i;

	if( !damage )
		return 0;

	nullpo_ret(src);
	nullpo_ret(target);

	sd = BL_CAST(BL_PC, src);
	tsd = BL_CAST(BL_PC, target);
	t_class = status->get_class(target);
	s_class = status->get_class(src);
	sstatus = status->get_status_data(src);
	tstatus = status->get_status_data(target);
	s_race2 = status->get_race2(src);

	switch(attack_type){
		case BF_MAGIC:
			if ( sd && !(nk&NK_NO_CARDFIX_ATK) ) {
				cardfix = cardfix * (100 + sd->magic_addrace[tstatus->race]) / 100;
				if (!(nk&NK_NO_ELEFIX))
					cardfix = cardfix*(100+sd->magic_addele[tstatus->def_ele]) / 100;
				cardfix = cardfix * (100 + sd->magic_addsize[tstatus->size]) / 100;
				cardfix = cardfix * (100 + sd->magic_addrace[is_boss(target)?RC_BOSS:RC_NONBOSS]) / 100;
				cardfix = cardfix * (100 + sd->magic_atk_ele[s_ele])/100;
				for(i=0; i< ARRAYLENGTH(sd->add_mdmg) && sd->add_mdmg[i].rate; i++) {
					if(sd->add_mdmg[i].class_ == t_class) {
						cardfix = cardfix * (100 + sd->add_mdmg[i].rate) / 100;
						break;
					}
				}
				if (cardfix != 1000)
					damage = damage * cardfix / 1000;
			}

			if( tsd && !(nk&NK_NO_CARDFIX_DEF) )
			{ // Target cards.
				if (!(nk&NK_NO_ELEFIX))
				{
					int ele_fix = tsd->subele[s_ele];
					for (i = 0; ARRAYLENGTH(tsd->subele2) > i && tsd->subele2[i].rate != 0; i++)
					{
						if(tsd->subele2[i].ele != s_ele) continue;
						if(!(tsd->subele2[i].flag&wflag&BF_WEAPONMASK &&
							 tsd->subele2[i].flag&wflag&BF_RANGEMASK &&
							 tsd->subele2[i].flag&wflag&BF_SKILLMASK))
							continue;
						ele_fix += tsd->subele2[i].rate;
					}
					cardfix = cardfix * (100 - ele_fix) / 100;
				}
				cardfix = cardfix * (100 - tsd->subsize[sstatus->size]) / 100;
				cardfix = cardfix * (100 - tsd->subrace2[s_race2]) / 100;
				cardfix = cardfix * (100 - tsd->subrace[sstatus->race]) / 100;
				cardfix = cardfix * (100 - tsd->subrace[is_boss(src)?RC_BOSS:RC_NONBOSS]) / 100;
				if( sstatus->race != RC_DEMIHUMAN )
					cardfix = cardfix * (100-tsd->subrace[RC_NONDEMIHUMAN]) / 100;

				for(i=0; i < ARRAYLENGTH(tsd->add_mdef) && tsd->add_mdef[i].rate;i++) {
					if(tsd->add_mdef[i].class_ == s_class) {
						cardfix = cardfix * (100-tsd->add_mdef[i].rate) / 100;
						break;
					}
				}
#ifndef RENEWAL
				//It was discovered that ranged defense also counts vs magic! [Skotlex]
				if ( wflag&BF_SHORT )
					cardfix = cardfix * ( 100 - tsd->bonus.near_attack_def_rate ) / 100;
				else
					cardfix = cardfix * ( 100 - tsd->bonus.long_attack_def_rate ) / 100;
#endif

				cardfix = cardfix * ( 100 - tsd->bonus.magic_def_rate ) / 100;

				if( tsd->sc.data[SC_PROTECT_MDEF] )
					cardfix = cardfix * ( 100 - tsd->sc.data[SC_PROTECT_MDEF]->val1 ) / 100;

				if( cardfix != 1000 )
					damage = damage * cardfix / 1000;
			}
			break;
		case BF_WEAPON:
			t_race2 = status->get_race2(target);
			if( cflag&2 ){
				if( sd && !(nk&NK_NO_CARDFIX_ATK) ){
					short cardfix_ = 1000;
					if( sd->state.arrow_atk ){
						cardfix = cardfix * (100 + sd->right_weapon.addrace[tstatus->race] + sd->arrow_addrace[tstatus->race]) / 100;
						if( !(nk&NK_NO_ELEFIX) ){
							int ele_fix = sd->right_weapon.addele[tstatus->def_ele] + sd->arrow_addele[tstatus->def_ele];
							for(i = 0; ARRAYLENGTH(sd->right_weapon.addele2) > i && sd->right_weapon.addele2[i].rate != 0; i++){
								if (sd->right_weapon.addele2[i].ele != tstatus->def_ele) continue;
								if(!(sd->right_weapon.addele2[i].flag&wflag&BF_WEAPONMASK &&
									 sd->right_weapon.addele2[i].flag&wflag&BF_RANGEMASK &&
									 sd->right_weapon.addele2[i].flag&wflag&BF_SKILLMASK))
										continue;
								ele_fix += sd->right_weapon.addele2[i].rate;
							}
							cardfix = cardfix * (100 + ele_fix) / 100;
						}
						cardfix = cardfix * (100 + sd->right_weapon.addsize[tstatus->size]+sd->arrow_addsize[tstatus->size]) / 100;
						cardfix = cardfix * (100 + sd->right_weapon.addrace2[t_race2]) / 100;
						cardfix = cardfix * (100 + sd->right_weapon.addrace[is_boss(target)?RC_BOSS:RC_NONBOSS] + sd->arrow_addrace[is_boss(target)?RC_BOSS:RC_NONBOSS]) / 100;
						if( tstatus->race != RC_DEMIHUMAN )
							cardfix = cardfix * (100 + sd->right_weapon.addrace[RC_NONDEMIHUMAN]+sd->arrow_addrace[RC_NONDEMIHUMAN]) / 100;
					}else{ // Melee attack
						if( !battle_config.left_cardfix_to_right ){
							cardfix=cardfix*(100+sd->right_weapon.addrace[tstatus->race])/100;
							if( !(nk&NK_NO_ELEFIX) ){
								int ele_fix = sd->right_weapon.addele[tstatus->def_ele];
								for (i = 0; ARRAYLENGTH(sd->right_weapon.addele2) > i && sd->right_weapon.addele2[i].rate != 0; i++) {
									if (sd->right_weapon.addele2[i].ele != tstatus->def_ele) continue;
									if(!(sd->right_weapon.addele2[i].flag&wflag&BF_WEAPONMASK &&
										 sd->right_weapon.addele2[i].flag&wflag&BF_RANGEMASK &&
										 sd->right_weapon.addele2[i].flag&wflag&BF_SKILLMASK))
											continue;
									ele_fix += sd->right_weapon.addele2[i].rate;
								}
								cardfix = cardfix * (100+ele_fix) / 100;
							}
							cardfix = cardfix * (100+sd->right_weapon.addsize[tstatus->size]) / 100;
							cardfix = cardfix * (100+sd->right_weapon.addrace2[t_race2]) / 100;
							cardfix = cardfix * (100+sd->right_weapon.addrace[is_boss(target)?RC_BOSS:RC_NONBOSS]) / 100;
							if( tstatus->race != RC_DEMIHUMAN )
								cardfix = cardfix * (100 + sd->right_weapon.addrace[RC_NONDEMIHUMAN]) / 100;

							if( cflag&1 ){
								cardfix_ = cardfix_*(100+sd->left_weapon.addrace[tstatus->race])/100;
								if (!(nk&NK_NO_ELEFIX)){
									int ele_fix_lh = sd->left_weapon.addele[tstatus->def_ele];
									for (i = 0; ARRAYLENGTH(sd->left_weapon.addele2) > i && sd->left_weapon.addele2[i].rate != 0; i++) {
										if (sd->left_weapon.addele2[i].ele != tstatus->def_ele) continue;
										if(!(sd->left_weapon.addele2[i].flag&wflag&BF_WEAPONMASK &&
											 sd->left_weapon.addele2[i].flag&wflag&BF_RANGEMASK &&
											 sd->left_weapon.addele2[i].flag&wflag&BF_SKILLMASK))
												continue;
										ele_fix_lh += sd->left_weapon.addele2[i].rate;
									}
									cardfix = cardfix * (100+ele_fix_lh) / 100;
								}
								cardfix_ = cardfix_ * (100+sd->left_weapon.addsize[tstatus->size]) / 100;
								cardfix_ = cardfix_ * (100+sd->left_weapon.addrace2[t_race2]) / 100;
								cardfix_ = cardfix_ * (100+sd->left_weapon.addrace[is_boss(target)?RC_BOSS:RC_NONBOSS]) / 100;
								if( tstatus->race != RC_DEMIHUMAN )
									cardfix_ = cardfix_*(100+sd->left_weapon.addrace[RC_NONDEMIHUMAN])/100;
							}
						}else{
							int ele_fix = sd->right_weapon.addele[tstatus->def_ele] + sd->left_weapon.addele[tstatus->def_ele];
							for (i = 0; ARRAYLENGTH(sd->right_weapon.addele2) > i && sd->right_weapon.addele2[i].rate != 0; i++){
								if (sd->right_weapon.addele2[i].ele != tstatus->def_ele) continue;
								if(!(sd->right_weapon.addele2[i].flag&wflag&BF_WEAPONMASK &&
									 sd->right_weapon.addele2[i].flag&wflag&BF_RANGEMASK &&
									 sd->right_weapon.addele2[i].flag&wflag&BF_SKILLMASK))
										continue;
								ele_fix += sd->right_weapon.addele2[i].rate;
							}
							for (i = 0; ARRAYLENGTH(sd->left_weapon.addele2) > i && sd->left_weapon.addele2[i].rate != 0; i++){
								if (sd->left_weapon.addele2[i].ele != tstatus->def_ele) continue;
								if(!(sd->left_weapon.addele2[i].flag&wflag&BF_WEAPONMASK &&
									 sd->left_weapon.addele2[i].flag&wflag&BF_RANGEMASK &&
									 sd->left_weapon.addele2[i].flag&wflag&BF_SKILLMASK))
										continue;
								ele_fix += sd->left_weapon.addele2[i].rate;
							}

							cardfix = cardfix * (100 + sd->right_weapon.addrace[tstatus->race] + sd->left_weapon.addrace[tstatus->race]) / 100;
							cardfix = cardfix * (100 + ele_fix) / 100;
							cardfix = cardfix * (100 + sd->right_weapon.addsize[tstatus->size] + sd->left_weapon.addsize[tstatus->size])/100;
							cardfix = cardfix * (100 + sd->right_weapon.addrace2[t_race2] + sd->left_weapon.addrace2[t_race2])/100;
							cardfix = cardfix * (100 + sd->right_weapon.addrace[is_boss(target)?RC_BOSS:RC_NONBOSS] + sd->left_weapon.addrace[is_boss(target)?RC_BOSS:RC_NONBOSS]) / 100;
							if( tstatus->race != RC_DEMIHUMAN )
								cardfix = cardfix * (100+sd->right_weapon.addrace[RC_NONDEMIHUMAN] + sd->left_weapon.addrace[RC_NONDEMIHUMAN]) / 100;
						}
					}

					for( i = 0; i < ARRAYLENGTH(sd->right_weapon.add_dmg) && sd->right_weapon.add_dmg[i].rate; i++ ){
						if( sd->right_weapon.add_dmg[i].class_ == t_class ){
							cardfix = cardfix * (100 + sd->right_weapon.add_dmg[i].rate) / 100;
							break;
						}
					}

					if( cflag&1 ){
						for( i = 0; i < ARRAYLENGTH(sd->left_weapon.add_dmg) && sd->left_weapon.add_dmg[i].rate; i++ ){
							if( sd->left_weapon.add_dmg[i].class_ == t_class ){
								cardfix_ = cardfix_ * (100 + sd->left_weapon.add_dmg[i].rate) / 100;
								break;
							}
						}
					}
#ifndef RENEWAL
					if( wflag&BF_LONG )
						cardfix = cardfix * (100 + sd->bonus.long_attack_atk_rate) / 100;
#endif
					if( (cflag&1) && cardfix_ != 1000 )
						damage = damage * cardfix_ / 1000;
					else if( cardfix != 1000 )
						damage = damage * cardfix / 1000;
				}
			}else{
				// Target side
				if( tsd && !(nk&NK_NO_CARDFIX_DEF) ){
					if( !(nk&NK_NO_ELEFIX) ){
						int ele_fix = tsd->subele[s_ele];
						for (i = 0; ARRAYLENGTH(tsd->subele2) > i && tsd->subele2[i].rate != 0; i++)
						{
							if(tsd->subele2[i].ele != s_ele) continue;
							if(!(tsd->subele2[i].flag&wflag&BF_WEAPONMASK &&
								 tsd->subele2[i].flag&wflag&BF_RANGEMASK &&
								 tsd->subele2[i].flag&wflag&BF_SKILLMASK))
								continue;
							ele_fix += tsd->subele2[i].rate;
						}
						cardfix = cardfix * (100-ele_fix) / 100;
						if( cflag&1 && s_ele_ != s_ele ){
							int ele_fix_lh = tsd->subele[s_ele_];
							for (i = 0; ARRAYLENGTH(tsd->subele2) > i && tsd->subele2[i].rate != 0; i++){
								if(tsd->subele2[i].ele != s_ele_) continue;
								if(!(tsd->subele2[i].flag&wflag&BF_WEAPONMASK &&
									 tsd->subele2[i].flag&wflag&BF_RANGEMASK &&
									 tsd->subele2[i].flag&wflag&BF_SKILLMASK))
									continue;
								ele_fix_lh += tsd->subele2[i].rate;
							}
							cardfix = cardfix * (100 - ele_fix_lh) / 100;
						}
					}
					cardfix = cardfix * (100-tsd->subsize[sstatus->size]) / 100;
					cardfix = cardfix * (100-tsd->subrace2[s_race2]) / 100;
					cardfix = cardfix * (100-tsd->subrace[sstatus->race]) / 100;
					cardfix = cardfix * (100-tsd->subrace[is_boss(src)?RC_BOSS:RC_NONBOSS]) / 100;
					if( sstatus->race != RC_DEMIHUMAN )
						cardfix = cardfix * (100-tsd->subrace[RC_NONDEMIHUMAN]) / 100;

					for( i = 0; i < ARRAYLENGTH(tsd->add_def) && tsd->add_def[i].rate;i++ ){
						if( tsd->add_def[i].class_ == s_class )
						{
							cardfix = cardfix * (100 - tsd->add_def[i].rate) / 100;
							break;
						}
					}

					if( wflag&BF_SHORT )
						cardfix = cardfix * (100 - tsd->bonus.near_attack_def_rate) / 100;
					else // BF_LONG (there's no other choice)
						cardfix = cardfix * (100 - tsd->bonus.long_attack_def_rate) / 100;

					if( tsd->sc.data[SC_PROTECT_DEF] )
						cardfix = cardfix * (100 - tsd->sc.data[SC_PROTECT_DEF]->val1) / 100;

					if( cardfix != 1000 )
						damage = damage * cardfix / 1000;
				}
			}
			break;
		case BF_MISC:
			if( tsd && !(nk&NK_NO_CARDFIX_DEF) ){
			// misc damage reduction from equipment
				if (!(nk&NK_NO_ELEFIX))
				{
					int ele_fix = tsd->subele[s_ele];
					for (i = 0; ARRAYLENGTH(tsd->subele2) > i && tsd->subele2[i].rate != 0; i++)
					{
						if(tsd->subele2[i].ele != s_ele) continue;
						if(!(tsd->subele2[i].flag&wflag&BF_WEAPONMASK &&
							 tsd->subele2[i].flag&wflag&BF_RANGEMASK &&
							 tsd->subele2[i].flag&wflag&BF_SKILLMASK))
							continue;
						ele_fix += tsd->subele2[i].rate;
					}
					cardfix = cardfix * (100 - ele_fix) / 100;
				}
				cardfix = cardfix*(100-tsd->subsize[sstatus->size]) / 100;
				cardfix = cardfix*(100-tsd->subrace2[s_race2]) / 100;
				cardfix = cardfix*(100-tsd->subrace[sstatus->race]) / 100;
				cardfix = cardfix*(100-tsd->subrace[is_boss(src)?RC_BOSS:RC_NONBOSS]) / 100;
				if( sstatus->race != RC_DEMIHUMAN )
					cardfix = cardfix * (100 - tsd->subrace[RC_NONDEMIHUMAN]) / 100;

				cardfix = cardfix * ( 100 - tsd->bonus.misc_def_rate ) / 100;
				if( wflag&BF_SHORT )
					cardfix = cardfix * ( 100 - tsd->bonus.near_attack_def_rate ) / 100;
				else // BF_LONG (there's no other choice)
					cardfix = cardfix * ( 100 - tsd->bonus.long_attack_def_rate ) / 100;

				if (cardfix != 1000)
					damage = damage * cardfix / 1000;
			}
			break;
	}

	return damage;
}

/*==========================================
 * Calculates defense reduction. [malufett]
 * flag:
 * &1 - idef/imdef(Ignore defense)
 * &2 - pdef(Pierce defense)
 * &4 - tdef(Total defense reduction)
 *------------------------------------------*/
int64 battle_calc_defense(int attack_type, struct block_list *src, struct block_list *target, uint16 skill_id, uint16 skill_lv, int64 damage, int flag, int pdef){
	struct status_data *sstatus, *tstatus;
	struct map_session_data *sd, *tsd;
	struct status_change *sc, *tsc;
	int i;

	if( !damage )
		return 0;

	nullpo_ret(src);
	nullpo_ret(target);

	sd = BL_CAST(BL_PC, src);
	tsd = BL_CAST(BL_PC, target);
	sstatus = status->get_status_data(src);
	tstatus = status->get_status_data(target);
	sc = status->get_sc(src);
	tsc = status->get_sc(target);

	switch(attack_type){
		case BF_WEAPON:
			{
			/* Take note in RE
			 *  def1 = equip def
			 *  def2 = status def
			 */
			defType def1 = status->get_def(target); //Don't use tstatus->def1 due to skill timer reductions.
			short def2 = tstatus->def2, vit_def;
#ifdef RENEWAL
			def1 = status->calc_def2(target, tsc, def1, false); // equip def(RE)
			def2 = status->calc_def(target, tsc, def2, false); // status def(RE)
#else
			def1 = status->calc_def(target, tsc, def1, false); // equip def(RE)
			def2 = status->calc_def2(target, tsc, def2, false); // status def(RE)
#endif

			if ( sd ) {
				i = sd->ignore_def[is_boss(target) ? RC_BOSS : RC_NONBOSS];
				i += sd->ignore_def[tstatus->race];
				if ( i ) {
					if ( i > 100 ) i = 100;
					def1 -= def1 * i / 100;
					def2 -= def2 * i / 100;
				}
				if (sd->charm_type == CHARM_TYPE_LAND && sd->charm_count > 0 ) // hidden from status window
					def1 += 10 * def1 * sd->charm_count / 100;
			}

			if( sc && sc->data[SC_EXPIATIO] ){
				i = 5 * sc->data[SC_EXPIATIO]->val1; // 5% per level
				def1 -= def1 * i / 100;
				def2 -= def2 * i / 100;
			}

			if( battle_config.vit_penalty_type && battle_config.vit_penalty_target&target->type ) {
				unsigned char target_count; //256 max targets should be a sane max
				target_count = unit->counttargeted(target);
				if(target_count >= battle_config.vit_penalty_count) {
					if(battle_config.vit_penalty_type == 1) {
						if( !tsc || !tsc->data[SC_STEELBODY] )
							def1 = (def1 * (100 - (target_count - (battle_config.vit_penalty_count - 1))*battle_config.vit_penalty_num))/100;
						def2 = (def2 * (100 - (target_count - (battle_config.vit_penalty_count - 1))*battle_config.vit_penalty_num))/100;
					} else { //Assume type 2
						if( !tsc || !tsc->data[SC_STEELBODY] )
							def1 -= (target_count - (battle_config.vit_penalty_count - 1))*battle_config.vit_penalty_num;
						def2 -= (target_count - (battle_config.vit_penalty_count - 1))*battle_config.vit_penalty_num;
					}
				}
				if(skill_id == AM_ACIDTERROR) def1 = 0; //Acid Terror ignores only armor defense. [Skotlex]
				if(def2 < 1) def2 = 1;
			}
			//Vitality reduction from rodatazone: http://rodatazone.simgaming.net/mechanics/substats.php#def
			if (tsd) {
				//Sd vit-eq
#ifndef RENEWAL
				//[VIT*0.5] + rnd([VIT*0.3], max([VIT*0.3],[VIT^2/150]-1))
				vit_def = def2*(def2-15)/150;
				vit_def = def2/2 + (vit_def>0?rnd()%vit_def:0);
#else
				vit_def = def2;
#endif
				if((battle->check_undead(sstatus->race,sstatus->def_ele) || sstatus->race==RC_DEMON) && //This bonus already doesn't work vs players
					src->type == BL_MOB && (i=pc->checkskill(tsd,AL_DP)) > 0)
					vit_def += i*(int)(3 +(tsd->status.base_level+1)*0.04);   // [orn]
				if( src->type == BL_MOB && (i=pc->checkskill(tsd,RA_RANGERMAIN))>0 &&
					(sstatus->race == RC_BRUTE || sstatus->race == RC_FISH || sstatus->race == RC_PLANT) )
					vit_def += i*5;
			}
			else { //Mob-Pet vit-eq
#ifndef RENEWAL
				//VIT + rnd(0,[VIT/20]^2-1)
				vit_def = (def2/20)*(def2/20);
				vit_def = def2 + (vit_def>0?rnd()%vit_def:0);
#else
				vit_def = def2;
#endif
			}

			if (battle_config.weapon_defense_type) {
				vit_def += def1*battle_config.weapon_defense_type;
				def1 = 0;
			}
		#ifdef RENEWAL
			/**
			* RE DEF Reduction
			* Pierce defense gains 1 atk per def/2
			**/

			if( def1 < -399 ) // it stops at -399
				def1 = 399; // in aegis it set to 1 but in our case it may lead to exploitation so limit it to 399
				//return 1;

			if( flag&2 )
				damage += def1 >> 1;

			if( !(flag&1) && !(flag&2) ) {
				if( flag&4 )
					damage -= (def1 + vit_def);
				else
					damage = (int)((100.0f - def1 / (def1 + 400.0f) * 90.0f) / 100.0f * damage - vit_def);
			}
		#else
				if( def1 > 100 ) def1 = 100;
				if( !(flag&1) ){
					if( flag&2 )
						damage = damage * pdef * (def1+vit_def) / 100;
					else
						damage = damage * (100-def1) / 100;
				}
				if( !(flag&1 || flag&2) )
					damage -= vit_def;
		#endif
			}
			break;

		case BF_MAGIC:
		{
			defType mdef = tstatus->mdef;
			short mdef2= tstatus->mdef2;
#ifdef RENEWAL
			mdef2 = status->calc_mdef(target, tsc, mdef2, false); // status mdef(RE)
			mdef = status->calc_mdef2(target, tsc, mdef, false); // equip mde(RE)
#else
			mdef2 = status->calc_mdef2(target, tsc, mdef2, false); // status mdef(RE)
			mdef = status->calc_mdef(target, tsc, mdef, false); // equip mde(RE)
#endif
			if( flag&1 )
				mdef = 0;

			if(sd) {
				i = sd->ignore_mdef[is_boss(target)?RC_BOSS:RC_NONBOSS];
				i += sd->ignore_mdef[tstatus->race];
				if (i)
				{
					if (i > 100) i = 100;
					mdef -= mdef * i/100;
					//mdef2-= mdef2* i/100;
				}
			}
		#ifdef RENEWAL
			/**
			 * RE MDEF Reduction
			 **/
			if( mdef < -99 ) // it stops at -99
				mdef = 99; // in aegis it set to 1 but in our case it may lead to exploitation so limit it to 99
				//return 1;

			damage = (int)((100.0f - mdef / (mdef + 100.0f) * 90.0f) / 100.0f * damage - mdef2);
		#else
			if(battle_config.magic_defense_type)
				damage = damage - mdef*battle_config.magic_defense_type - mdef2;
			else
				damage = damage * (100-mdef)/100 - mdef2;
		#endif
		}
			break;
	}
	return damage;
}

// Minstrel/Wanderer number check for chorus skills.
int battle_calc_chorusbonus(struct map_session_data *sd) {
	int members = 0;

	if (!sd || !sd->status.party_id)
		return 0;

	members = party->foreachsamemap(party->sub_count_chorus, sd, 0);

	if (members < 3)
		return 0; // Bonus remains 0 unless 3 or more Minstrel's/Wanderer's are in the party.
	if (members > 7)
		return 5; // Maximum effect possible from 7 or more Minstrel's/Wanderer's
	return members - 2; // Effect bonus from additional Minstrel's/Wanderer's if not above the max possible
}

int battle_calc_skillratio(int attack_type, struct block_list *src, struct block_list *target, uint16 skill_id, uint16 skill_lv, int skillratio, int flag){
	int i;
	struct status_change *sc, *tsc;
	struct map_session_data *sd, *tsd;
	struct status_data *st, *tst, *bst;

	nullpo_ret(src);
	nullpo_ret(target);

	sd = BL_CAST(BL_PC, src);
	tsd = BL_CAST(BL_PC, target);
	sc = status->get_sc(src);
	tsc = status->get_sc(target);
	st = status->get_status_data(src);
	bst = status->get_base_status(src);
	tst = status->get_status_data(target);

	switch(attack_type){
		case BF_MAGIC:
			switch(skill_id){
				case MG_NAPALMBEAT:
					skillratio += skill_lv * 10 - 30;
					break;
				case MG_FIREBALL:
			#ifdef RENEWAL
					skillratio += 20 * skill_lv;
			#else
					skillratio += skill_lv * 10 - 30;
			#endif
					break;
				case MG_SOULSTRIKE:
					if (battle->check_undead(tst->race,tst->def_ele))
						skillratio += 5*skill_lv;
					break;
				case MG_FIREWALL:
					skillratio -= 50;
					break;
				case MG_THUNDERSTORM:
					/**
					 * in Renewal Thunder Storm boost is 100% (in pre-re, 80%)
					 **/
					#ifndef RENEWAL
						skillratio -= 20;
					#endif
					break;
				case MG_FROSTDIVER:
					skillratio += 10 * skill_lv;
					break;
				case AL_HOLYLIGHT:
					skillratio += 25;
					if (sc && sc->data[SC_SOULLINK] && sc->data[SC_SOULLINK]->val2 == SL_PRIEST)
						skillratio *= 5; //Does 5x damage include bonuses from other skills?
					break;
				case AL_RUWACH:
					skillratio += 45;
					break;
				case WZ_FROSTNOVA:
					skillratio += (100+skill_lv*10) * 2 / 3 - 100;
					break;
				case WZ_FIREPILLAR:
					if (skill_lv > 10)
						skillratio += 2300; //200% MATK each hit
					else
						skillratio += -60 + 20*skill_lv; //20% MATK each hit
					break;
				case WZ_SIGHTRASHER:
					skillratio += 20 * skill_lv;
					break;
				case WZ_WATERBALL:
					skillratio += 30 * skill_lv;
					break;
				case WZ_STORMGUST:
					skillratio += 40 * skill_lv;
					break;
				case HW_NAPALMVULCAN:
					skillratio += 10 * skill_lv - 30;
					break;
				case SL_STIN:
					skillratio += (tst->size!=SZ_SMALL?-99:10*skill_lv); //target size must be small (0) for full damage.
					break;
				case SL_STUN:
					skillratio += (tst->size!=SZ_BIG?5*skill_lv:-99); //Full damage is dealt on small/medium targets
					break;
				case SL_SMA:
					skillratio += -60 + status->get_lv(src); //Base damage is 40% + lv%
					break;
				case NJ_KOUENKA:
					skillratio -= 10;
					if (sd && sd->charm_type == CHARM_TYPE_FIRE && sd->charm_count > 0)
						skillratio += 20 * sd->charm_count;
					break;
				case NJ_KAENSIN:
					skillratio -= 50;
					if (sd && sd->charm_type == CHARM_TYPE_FIRE && sd->charm_count > 0)
						skillratio += 10 * sd->charm_count;
					break;
				case NJ_BAKUENRYU:
					skillratio += 50 * (skill_lv - 1);
					if (sd && sd->charm_type == CHARM_TYPE_FIRE && sd->charm_count > 0)
						skillratio += 15 * sd->charm_count;
					break;
#ifdef RENEWAL
				case NJ_HYOUSENSOU:
					skillratio -= 30;
					if (sd && sd->charm_type == CHARM_TYPE_WATER && sd->charm_count > 0)
						skillratio += 5 * sd->charm_count;
					break;
#endif
				case NJ_HYOUSYOURAKU:
					skillratio += 50 * skill_lv;
					if (sd && sd->charm_type == CHARM_TYPE_WATER && sd->charm_count > 0)
						skillratio += 25 * sd->charm_count;
					break;
				case NJ_RAIGEKISAI:
					skillratio += 60 + 40 * skill_lv;
					if (sd && sd->charm_type == CHARM_TYPE_WIND && sd->charm_count > 0)
						skillratio += 15 * sd->charm_count;
					break;
				case NJ_KAMAITACHI:
					if (sd && sd->charm_type == CHARM_TYPE_WIND && sd->charm_count > 0)
						skillratio += 10 * sd->charm_count;
					/* Fall through */
				case NPC_ENERGYDRAIN:
					skillratio += 100 * skill_lv;
					break;
				case NPC_EARTHQUAKE:
					skillratio += 100 + 100 * skill_lv + 100 * (skill_lv/2);
					break;
			#ifdef RENEWAL
				case WZ_HEAVENDRIVE:
				case WZ_METEOR:
					skillratio += 25;
					break;
				case WZ_VERMILION:
				{
					int interval = 0, per = interval, ratio = per;
					while( (per++) < skill_lv ){
						ratio += interval;
						if(per%3==0) interval += 20;
					}
					if( skill_lv > 9 )
						ratio -= 10;
					skillratio += ratio;
				}
					break;
				case NJ_HUUJIN:
					skillratio += 50;
					if (sd && sd->charm_type == CHARM_TYPE_WIND && sd->charm_count > 0)
						skillratio += 20 * sd->charm_count;
					break;
			#else
				case WZ_VERMILION:
					skillratio += 20*skill_lv-20;
					break;
			#endif
				/**
					* Arch Bishop
					**/
				case AB_JUDEX:
					skillratio = 300 + 20 * skill_lv;
					RE_LVL_DMOD(100);
					break;
				case AB_ADORAMUS:
					skillratio = 500 + 100 * skill_lv;
					RE_LVL_DMOD(100);
					break;
				case AB_DUPLELIGHT_MAGIC:
					skillratio = 200 + 20 * skill_lv;
					break;
				/**
					* Warlock
					**/
				case WL_SOULEXPANSION: // MATK [{( Skill Level + 4 ) x 100 ) + ( Caster's INT )} x ( Caster's Base Level / 100 )] %
					skillratio = 100 * (skill_lv + 4) + st->int_;
					RE_LVL_DMOD(100);
					break;
				case WL_FROSTMISTY: // MATK [{( Skill Level x 100 ) + 200 } x ( Caster's Base Level / 100 )] %
					skillratio += 100 + 100 * skill_lv;
					RE_LVL_DMOD(100);
					break;
				case WL_JACKFROST:
					if( tsc && tsc->data[SC_FROSTMISTY] ){
						skillratio += 900 + 300 * skill_lv;
						RE_LVL_DMOD(100);
					}else{
						skillratio += 400 + 100 * skill_lv;
						RE_LVL_DMOD(150);
					}
					break;
				case WL_DRAINLIFE:
					skillratio = 200 * skill_lv + status_get_int(src);
					RE_LVL_DMOD(100);
					break;
				case WL_CRIMSONROCK:
					skillratio = 300 * skill_lv;
					RE_LVL_DMOD(100);
					skillratio += 1300;
					break;
				case WL_HELLINFERNO:
					skillratio = 300 * skill_lv;
					RE_LVL_DMOD(100);
					// Shadow: MATK [{( Skill Level x 300 ) x ( Caster Base Level / 100 ) x 4/5 }] %
					// Fire : MATK [{( Skill Level x 300 ) x ( Caster Base Level / 100 ) /5 }] %
					if( flag&ELE_DARK )
						skillratio *= 4;
					skillratio /= 5;
					break;
				case WL_COMET:
					i = ( sc ? distance_xy(target->x, target->y, sc->comet_x, sc->comet_y) : 8 );
					if( i <= 3 ) skillratio += 2400 + 500 * skill_lv; // 7 x 7 cell
					else
					if( i <= 5 ) skillratio += 1900 + 500 * skill_lv; // 11 x 11 cell
					else
					if( i <= 7 ) skillratio += 1400 + 500 * skill_lv; // 15 x 15 cell
					else
						skillratio += 900 + 500 * skill_lv; // 19 x 19 cell

					if( sd && sd->status.party_id ){
						struct map_session_data* psd;
						int p_sd[5] = {0, 0, 0, 0, 0}, c; // just limit it to 5

						c = 0;
						memset (p_sd, 0, sizeof(p_sd));
						party->foreachsamemap(skill->check_condition_char_sub, sd, 3, &sd->bl, &c, &p_sd, skill_id);
						c = ( c > 1 ? rnd()%c : 0 );

						if( (psd = map->id2sd(p_sd[c])) && pc->checkskill(psd,WL_COMET) > 0 ){
							skillratio = skill_lv * 400; //MATK [{( Skill Level x 400 ) x ( Caster's Base Level / 120 )} + 2500 ] %
							RE_LVL_DMOD(120);
							skillratio += 2500;
							status_zap(&psd->bl, 0, skill->get_sp(skill_id, skill_lv) / 2);
						}
					}
					break;
				case WL_CHAINLIGHTNING_ATK:
					skillratio += 400 + 100 * skill_lv;
					RE_LVL_DMOD(100);
					if(flag > 0)
						skillratio += 100 * flag;
					break;
				case WL_EARTHSTRAIN:
					skillratio = 2000 + 100 * skill_lv;
					RE_LVL_DMOD(100);
					break;
				case WL_TETRAVORTEX_FIRE:
				case WL_TETRAVORTEX_WATER:
				case WL_TETRAVORTEX_WIND:
				case WL_TETRAVORTEX_GROUND:
					skillratio += 400 + 500 * skill_lv;
					break;
				case WL_SUMMON_ATK_FIRE:
				case WL_SUMMON_ATK_WATER:
				case WL_SUMMON_ATK_WIND:
				case WL_SUMMON_ATK_GROUND:
					skillratio = (1 + skill_lv) / 2 *  (status->get_lv(src) + (sd ? sd->status.job_level : 50));
					RE_LVL_DMOD(100);
					break;
				case LG_RAYOFGENESIS:
				{
					uint16 lv = skill_lv;
					int bandingBonus = 0;
					if( sc && sc->data[SC_BANDING] )
						bandingBonus = 200 * (sd ? skill->check_pc_partner(sd,skill_id,&lv,skill->get_splash(skill_id,skill_lv),0) : 1);
					skillratio = ((300 * skill_lv) + bandingBonus) * (sd ? sd->status.job_level : 1) / 25;
				}
					break;
				case LG_SHIELDSPELL:
					if ( sd && skill_lv == 2 ) // [(Casters Base Level x 4) + (Shield MDEF x 100) + (Casters INT x 2)] %
						skillratio = 4 * status->get_lv(src) + 100 * sd->bonus.shieldmdef + 2 * st->int_;
					else
						skillratio = 0;
					break;
				case WM_METALICSOUND:
					skillratio = 120 * skill_lv + 60 * ( sd? pc->checkskill(sd, WM_LESSON) : 10 );
					RE_LVL_DMOD(100);
					break;
				case WM_REVERBERATION_MAGIC:
					skillratio = 100 * skill_lv + 100;
					RE_LVL_DMOD(100);
					break;
				case SO_FIREWALK:
					skillratio = 60 * skill_lv;
					RE_LVL_DMOD(100);
					if( sc && sc->data[SC_HEATER_OPTION] )
						skillratio += sc->data[SC_HEATER_OPTION]->val3 / 2;
					break;
				case SO_ELECTRICWALK:
					skillratio = 60 * skill_lv;
					RE_LVL_DMOD(100);
					if( sc && sc->data[SC_BLAST_OPTION] )
						skillratio += sc->data[SC_BLAST_OPTION]->val2 / 2;
					break;
				case SO_EARTHGRAVE:
					skillratio = st->int_ * skill_lv + 200 * (sd ? pc->checkskill(sd,SA_SEISMICWEAPON) : 1);
					RE_LVL_DMOD(100);
					if( sc && sc->data[SC_CURSED_SOIL_OPTION] )
						skillratio += sc->data[SC_CURSED_SOIL_OPTION]->val3 * 5;
					break;
				case SO_DIAMONDDUST:
					skillratio = (st->int_ * skill_lv + 200 * (sd ? pc->checkskill(sd, SA_FROSTWEAPON) : 1)) * status->get_lv(src) / 100;
					if( sc && sc->data[SC_COOLER_OPTION] )
						skillratio += sc->data[SC_COOLER_OPTION]->val3 * 5;
					break;
				case SO_POISON_BUSTER:
					skillratio += 900 + 300 * skill_lv;
					RE_LVL_DMOD(100);
					if( sc && sc->data[SC_CURSED_SOIL_OPTION] )
						skillratio += sc->data[SC_CURSED_SOIL_OPTION]->val3 * 5;
					break;
				case SO_PSYCHIC_WAVE:
					skillratio = 70 * skill_lv + 3 * st->int_;
					RE_LVL_DMOD(100);
					if( sc && ( sc->data[SC_HEATER_OPTION] || sc->data[SC_COOLER_OPTION]
					         || sc->data[SC_BLAST_OPTION] || sc->data[SC_CURSED_SOIL_OPTION] ) )
						skillratio += skillratio * 20 / 100;
					break;
				case SO_VARETYR_SPEAR:
					skillratio = status_get_int(src) * skill_lv + ( sd ? pc->checkskill(sd, SA_LIGHTNINGLOADER) * 50 : 0 );
					RE_LVL_DMOD(100);
					if( sc && sc->data[SC_BLAST_OPTION] )
						skillratio += sc->data[SC_BLAST_OPTION]->val2 * 5;
					break;
				case SO_CLOUD_KILL:
					skillratio = 40 * skill_lv;
					RE_LVL_DMOD(100);
					if( sc && sc->data[SC_CURSED_SOIL_OPTION] )
						skillratio += sc->data[SC_CURSED_SOIL_OPTION]->val3;
					break;
				case GN_DEMONIC_FIRE: {
						int fire_expansion_lv = skill_lv / 100;
						skill_lv = skill_lv % 100;
						skillratio = 110 + 20 * skill_lv;
						if ( fire_expansion_lv == 1 )
							skillratio += status_get_int(src) + (sd?sd->status.job_level:50);
						else if ( fire_expansion_lv == 2 )
							skillratio += status_get_int(src) * 10;
					}
					break;
				// Magical Elemental Spirits Attack Skills
				case EL_FIRE_MANTLE:
				case EL_WATER_SCREW:
					skillratio += 900;
					break;
				case EL_FIRE_ARROW:
				case EL_ROCK_CRUSHER_ATK:
					skillratio += 200;
					break;
				case EL_FIRE_BOMB:
				case EL_ICE_NEEDLE:
				case EL_HURRICANE_ATK:
					skillratio += 400;
					break;
				case EL_FIRE_WAVE:
				case EL_TYPOON_MIS_ATK:
					skillratio += 1100;
					break;
				case MH_ERASER_CUTTER:
					skillratio += 400 + 100 * skill_lv + (skill_lv%2 > 0 ? 0 : 300);
					break;
				case MH_XENO_SLASHER:
					if(skill_lv%2) skillratio += 350 + 50 * skill_lv; //500:600:700
					else skillratio += 400 + 100 * skill_lv; //700:900
					break;
				case MH_HEILIGE_STANGE:
					skillratio += 400 + 250 * skill_lv;
					break;
				case MH_POISON_MIST:
					skillratio += 100 * skill_lv;
					break;
				case KO_KAIHOU:
					if (sd && sd->charm_type != CHARM_TYPE_NONE && sd->charm_count > 0) {
						skillratio += -100 + 200 * sd->charm_count;
						RE_LVL_DMOD(100);
						pc->del_charm(sd, sd->charm_count, sd->charm_type);
					}
					break;
				default:
					battle->calc_skillratio_magic_unknown(&attack_type, src, target, &skill_id, &skill_lv, &skillratio, &flag);
					break;
			}
			break;
		case BF_WEAPON:
			switch( skill_id )
			{
				case SM_BASH:
				case MS_BASH:
					skillratio += 30 * skill_lv;
					break;
				case SM_MAGNUM:
				case MS_MAGNUM:
					skillratio += 20 * skill_lv;
					break;
				case MC_MAMMONITE:
					skillratio += 50 * skill_lv;
					break;
				case HT_POWER:
					skillratio += -50 + 8 * status_get_str(src);
					break;
				case AC_DOUBLE:
				case MA_DOUBLE:
					skillratio += 10 * (skill_lv-1);
					break;
				case AC_SHOWER:
				case MA_SHOWER:
					#ifdef RENEWAL
						skillratio += 50 + 10 * skill_lv;
					#else
						skillratio += -25 + 5 * skill_lv;
					#endif
					break;
				case AC_CHARGEARROW:
				case MA_CHARGEARROW:
					skillratio += 50;
					break;
	#ifndef RENEWAL
				case HT_FREEZINGTRAP:
				case MA_FREEZINGTRAP:
					skillratio += -50 + 10 * skill_lv;
					break;
	#endif
				case KN_PIERCE:
				case ML_PIERCE:
					skillratio += 10 * skill_lv;
					break;
				case MER_CRASH:
					skillratio += 10 * skill_lv;
					break;
				case KN_SPEARSTAB:
					skillratio += 15 * skill_lv;
					break;
				case KN_SPEARBOOMERANG:
					skillratio += 50*skill_lv;
					break;
				case KN_BRANDISHSPEAR:
				case ML_BRANDISH:
				{
					int ratio = 100 + 20 * skill_lv;
					skillratio += ratio - 100;
					if(skill_lv>3 && flag==1) skillratio += ratio / 2;
					if(skill_lv>6 && flag==1) skillratio += ratio / 4;
					if(skill_lv>9 && flag==1) skillratio += ratio / 8;
					if(skill_lv>6 && flag==2) skillratio += ratio / 2;
					if(skill_lv>9 && flag==2) skillratio += ratio / 4;
					if(skill_lv>9 && flag==3) skillratio += ratio / 2;
					break;
				}
				case KN_BOWLINGBASH:
				case MS_BOWLINGBASH:
					skillratio+= 40 * skill_lv;
					break;
				case AS_GRIMTOOTH:
					skillratio += 20 * skill_lv;
					break;
				case AS_POISONREACT:
					skillratio += 30 * skill_lv;
					break;
				case AS_SONICBLOW:
					skillratio += 300 + 40 * skill_lv;
					break;
				case TF_SPRINKLESAND:
					skillratio += 30;
					break;
				case MC_CARTREVOLUTION:
					skillratio += 50;
					if( sd && sd->cart_weight )
						skillratio += 100 * sd->cart_weight / sd->cart_weight_max; // +1% every 1% weight
					else if (!sd)
						skillratio += 100; //Max damage for non players.
					break;
				case NPC_RANDOMATTACK:
					skillratio += 100 * skill_lv;
					break;
				case NPC_WATERATTACK:
				case NPC_GROUNDATTACK:
				case NPC_FIREATTACK:
				case NPC_WINDATTACK:
				case NPC_POISONATTACK:
				case NPC_HOLYATTACK:
				case NPC_DARKNESSATTACK:
				case NPC_UNDEADATTACK:
				case NPC_TELEKINESISATTACK:
				case NPC_BLOODDRAIN:
				case NPC_ACIDBREATH:
				case NPC_DARKNESSBREATH:
				case NPC_FIREBREATH:
				case NPC_ICEBREATH:
				case NPC_THUNDERBREATH:
				case NPC_HELLJUDGEMENT:
				case NPC_PULSESTRIKE:
					skillratio += 100 * (skill_lv-1);
					break;
				case RG_BACKSTAP:
					if( sd && sd->status.weapon == W_BOW && battle_config.backstab_bow_penalty )
						skillratio += (200 + 40 * skill_lv) / 2;
					else
						skillratio += 200 + 40 * skill_lv;
					break;
				case RG_RAID:
					skillratio += 40 * skill_lv;
					break;
				case RG_INTIMIDATE:
					skillratio += 30 * skill_lv;
					break;
				case CR_SHIELDCHARGE:
					skillratio += 20 * skill_lv;
					break;
				case CR_SHIELDBOOMERANG:
					skillratio += 30 * skill_lv;
					break;
				case NPC_DARKCROSS:
				case CR_HOLYCROSS:
				{
					int ratio = 35 * skill_lv;
					#ifdef RENEWAL
						if(sd && sd->status.weapon == W_2HSPEAR)
							ratio *= 2;
					#endif
					skillratio += ratio;
					break;
				}
				case AM_DEMONSTRATION:
					skillratio += 20 * skill_lv;
					break;
				case AM_ACIDTERROR:
					skillratio += 40 * skill_lv;
					break;
				case MO_FINGEROFFENSIVE:
					skillratio+= 50 * skill_lv;
					break;
				case MO_INVESTIGATE:
					skillratio += 75 * skill_lv;
					break;
	#ifndef RENEWAL
				case MO_EXTREMITYFIST:
					{
						//Overflow check. [Skotlex]
						unsigned int ratio = skillratio + 100*(8 + st->sp/10);
						//You'd need something like 6K SP to reach this max, so should be fine for most purposes.
						if (ratio > 60000) ratio = 60000; //We leave some room here in case skillratio gets further increased.
						skillratio = (unsigned short)ratio;
					}
					break;
	#endif
				case MO_TRIPLEATTACK:
					skillratio += 20 * skill_lv;
					break;
				case MO_CHAINCOMBO:
					skillratio += 50 + 50 * skill_lv;
					break;
				case MO_COMBOFINISH:
					skillratio += 140 + 60 * skill_lv;
					break;
				case BA_MUSICALSTRIKE:
				case DC_THROWARROW:
					skillratio += 25 + 25 * skill_lv;
					break;
				case CH_TIGERFIST:
					skillratio += 100 * skill_lv - 60;
					break;
				case CH_CHAINCRUSH:
					skillratio += 300 + 100 * skill_lv;
					break;
				case CH_PALMSTRIKE:
					skillratio += 100 + 100 * skill_lv;
					break;
				case LK_HEADCRUSH:
					skillratio += 40 * skill_lv;
					break;
				case LK_JOINTBEAT:
					i = 10 * skill_lv - 50;
					// Although not clear, it's being assumed that the 2x damage is only for the break neck ailment.
					if (flag&BREAK_NECK) i*=2;
					skillratio += i;
					break;
				case ASC_METEORASSAULT:
					skillratio += 40 * skill_lv - 60;
					break;
				case SN_SHARPSHOOTING:
				case MA_SHARPSHOOTING:
					skillratio += 100 + 50 * skill_lv;
					break;
				case CG_ARROWVULCAN:
					skillratio += 100 + 100 * skill_lv;
					break;
				case AS_SPLASHER:
					skillratio += 400 + 50 * skill_lv;
					if(sd)
						skillratio += 20 * pc->checkskill(sd,AS_POISONREACT);
					break;
	#ifndef RENEWAL
				case ASC_BREAKER:
					skillratio += 100*skill_lv-100;
	#else
				case LK_SPIRALPIERCE:
				case ML_SPIRALPIERCE:
					skillratio += 50 * skill_lv;
	#endif
					break;
				case PA_SACRIFICE:
					skillratio += 10 * skill_lv - 10;
					break;
				case PA_SHIELDCHAIN:
					skillratio += 30 * skill_lv;
					break;
				case WS_CARTTERMINATION:
					i = 10 * (16 - skill_lv);
					if (i < 1) i = 1;
					//Preserve damage ratio when max cart weight is changed.
					if(sd && sd->cart_weight)
						skillratio += sd->cart_weight/i * 80000/battle_config.max_cart_weight - 100;
					else if (!sd)
						skillratio += 80000 / i - 100;
					break;
				case TK_DOWNKICK:
					skillratio += 60 + 20 * skill_lv;
					break;
				case TK_STORMKICK:
					skillratio += 60 + 20 * skill_lv;
					break;
				case TK_TURNKICK:
					skillratio += 90 + 30 * skill_lv;
					break;
				case TK_COUNTER:
					skillratio += 90 + 30 * skill_lv;
					break;
				case TK_JUMPKICK:
					skillratio += -70 + 10*skill_lv;
					if (sc && sc->data[SC_COMBOATTACK] && sc->data[SC_COMBOATTACK]->val1 == skill_id)
						skillratio += 10 * status->get_lv(src) / 3; //Tumble bonus
					if (flag) {
						skillratio += 10 * status->get_lv(src) / 3; //Running bonus (TODO: What is the real bonus?)
						if( sc && sc->data[SC_STRUP] )  // Spurt bonus
							skillratio *= 2;
					}
					break;
				case GS_TRIPLEACTION:
					skillratio += 50 * skill_lv;
					break;
				case GS_BULLSEYE:
					//Only works well against brute/demi-humans non bosses.
					if((tst->race == RC_BRUTE || tst->race == RC_DEMIHUMAN)
						&& !(tst->mode&MD_BOSS))
						skillratio += 400;
					break;
				case GS_TRACKING:
					skillratio += 100 * (skill_lv+1);
					break;
				case GS_PIERCINGSHOT:
#ifdef RENEWAL
					if( sd && sd->weapontype1 == W_RIFLE )
						skillratio += 50 + 30 * skill_lv;
					else
#endif
						skillratio += 20 * skill_lv;
					break;
				case GS_RAPIDSHOWER:
					skillratio += 10 * skill_lv;
					break;
				case GS_DESPERADO:
					skillratio += 50 * (skill_lv-1);
					break;
				case GS_DUST:
					skillratio += 50 * skill_lv;
					break;
				case GS_FULLBUSTER:
					skillratio += 100 * (skill_lv+2);
					break;
				case GS_SPREADATTACK:
				#ifdef RENEWAL
					skillratio += 20 * (skill_lv);
				#else
					skillratio += 20 * (skill_lv-1);
				#endif
					break;
				case NJ_HUUMA:
					skillratio += 50 + 150 * skill_lv;
					break;
				case NJ_TATAMIGAESHI:
					skillratio += 10 * skill_lv;
					break;
				case NJ_KASUMIKIRI:
					skillratio += 10 * skill_lv;
					break;
				case NJ_KIRIKAGE:
					skillratio += 100 * (skill_lv-1);
					break;
				case KN_CHARGEATK:
					{
						int k = (flag-1)/3; //+100% every 3 cells of distance
						if( k > 2 ) k = 2; // ...but hard-limited to 300%.
						skillratio += 100 * k;
					}
					break;
				case HT_PHANTASMIC:
					skillratio += 50;
					break;
				case MO_BALKYOUNG:
					skillratio += 200;
					break;
				case HFLI_MOON: //[orn]
					skillratio += 10 + 110 * skill_lv;
					break;
				case HFLI_SBR44: //[orn]
					skillratio += 100 * (skill_lv-1);
					break;
				case NPC_VAMPIRE_GIFT:
					skillratio += ((skill_lv-1)%5+1) * 100;
					break;
				case RK_SONICWAVE:
					skillratio = (skill_lv + 5) * 100;
					skillratio = skillratio * (100 + (status->get_lv(src)-100) / 2) / 100;
					break;
				case RK_HUNDREDSPEAR:
						skillratio += 500 + (80 * skill_lv);
						if( sd ){
							short index = sd->equip_index[EQI_HAND_R];
							if( index >= 0 && sd->inventory_data[index]
								&& sd->inventory_data[index]->type == IT_WEAPON )
								skillratio += (10000 - min(10000, sd->inventory_data[index]->weight)) / 10;
							skillratio = skillratio * (100 + (status->get_lv(src)-100) / 2) / 100 + 50 * pc->checkskill(sd,LK_SPIRALPIERCE);
						}
					break;
				case RK_WINDCUTTER:
						skillratio = (skill_lv + 2) * 50;
						RE_LVL_DMOD(100);
					break;
				case RK_IGNITIONBREAK:
					i = distance_bl(src,target);
					if( i < 2 )
						skillratio = 300 * skill_lv;
					else if( i < 4 )
						skillratio = 250 * skill_lv;
					else
						skillratio = 200 * skill_lv;
					skillratio = skillratio * status->get_lv(src) / 100;
					if( st->rhw.ele == ELE_FIRE )
						skillratio += 100 * skill_lv;
					break;
				case RK_STORMBLAST:
					skillratio = ((sd ? pc->checkskill(sd,RK_RUNEMASTERY) : 1) + status_get_int(src) / 8) * 100;
					break;
				case RK_PHANTOMTHRUST:
					skillratio = 50 * skill_lv + 10 * (sd ? pc->checkskill(sd,KN_SPEARMASTERY) : 10);
					RE_LVL_DMOD(150);
					break;
				/**
					* GC Guillotine Cross
					**/
				case GC_CROSSIMPACT:
					skillratio += 900 + 100 * skill_lv;
					RE_LVL_DMOD(120);
					break;
				case GC_PHANTOMMENACE:
					skillratio += 200;
					break;
				case GC_COUNTERSLASH:
					//ATK [{(Skill Level x 100) + 300} x Caster's Base Level / 120]% + ATK [(AGI x 2) + (Caster's Job Level x 4)]%
					skillratio += 200 + (100 * skill_lv);
					RE_LVL_DMOD(120);
					break;
				case GC_ROLLINGCUTTER:
					skillratio = 50 + 50 * skill_lv;
					RE_LVL_DMOD(100);
					break;
				case GC_CROSSRIPPERSLASHER:
					skillratio += 300 + 80 * skill_lv;
					RE_LVL_DMOD(100);
					if( sc && sc->data[SC_ROLLINGCUTTER] )
						skillratio += sc->data[SC_ROLLINGCUTTER]->val1 * status_get_agi(src);
					break;
				case GC_DARKCROW:
					skillratio += 100 * (skill_lv - 1);
					break;
				/**
					* Arch Bishop
					**/
				case AB_DUPLELIGHT_MELEE:
					skillratio += 10 * skill_lv;
					break;
				/**
					* Ranger
					**/
				case RA_ARROWSTORM:
					skillratio += 900 + 80 * skill_lv;
					RE_LVL_DMOD(100);
					break;
				case RA_AIMEDBOLT:
					skillratio += 400 + 50 * skill_lv;
					RE_LVL_DMOD(100);
					break;
				case RA_CLUSTERBOMB:
					skillratio += 100 + 100 * skill_lv;
					break;
				case RA_WUGDASH:// ATK 300%
					skillratio = 300;
					if( sc && sc->data[SC_DANCE_WITH_WUG] )
						skillratio += 10 * sc->data[SC_DANCE_WITH_WUG]->val1 * (2 + battle->calc_chorusbonus(sd));
					break;
				case RA_WUGSTRIKE:
					skillratio = 200 * skill_lv;
					if( sc && sc->data[SC_DANCE_WITH_WUG] )
						skillratio += 10 * sc->data[SC_DANCE_WITH_WUG]->val1 * (2 + battle->calc_chorusbonus(sd));
					break;
				case RA_WUGBITE:
					skillratio += 300 + 200 * skill_lv;
					if ( skill_lv == 5 ) skillratio += 100;
					break;
				case RA_SENSITIVEKEEN:
					skillratio = 150 * skill_lv;
					break;
				/**
					* Mechanic
					**/
				case NC_BOOSTKNUCKLE:
					skillratio = skill_lv * 100 + 200 + st->dex;
					RE_LVL_DMOD(120);
					break;
				case NC_PILEBUNKER:
					skillratio = skill_lv*100 + 300 + status_get_str(src);
					RE_LVL_DMOD(100);
					break;
				case NC_VULCANARM:
					skillratio = 70 * skill_lv + status_get_dex(src);
					RE_LVL_DMOD(120);
					break;
				case NC_FLAMELAUNCHER:
				case NC_COLDSLOWER:
					skillratio += 200 + 300 * skill_lv;
					RE_LVL_DMOD(150);
					break;
				case NC_ARMSCANNON:
					switch( tst->size ) {
						case SZ_SMALL: skillratio = 300 + 350 * skill_lv; break; // Medium
						case SZ_MEDIUM: skillratio = 300 + 400 * skill_lv; break;  // Small
						case SZ_BIG: skillratio = 300 + 300 * skill_lv; break;    // Large
					}
					RE_LVL_DMOD(120);
					break;
				case NC_AXEBOOMERANG:
					skillratio = 250 + 50 * skill_lv;
					if( sd ) {
						short index = sd->equip_index[EQI_HAND_R];
						if( index >= 0 && sd->inventory_data[index] && sd->inventory_data[index]->type == IT_WEAPON )
						skillratio += sd->inventory_data[index]->weight / 10;
					}
					RE_LVL_DMOD(100);
					break;
				case NC_POWERSWING:
					skillratio = 300 + 100*skill_lv + ( status_get_str(src)+status_get_dex(src) ) * status->get_lv(src) / 100;
					break;
				case NC_AXETORNADO:
					skillratio = 200 + 100 * skill_lv + st->vit;
					RE_LVL_DMOD(100);
					if( st->rhw.ele == ELE_WIND )
						skillratio = skillratio * 125 / 100;
					if ( distance_bl(src, target) > 2 ) // Will deal 75% damage outside of 5x5 area.
						skillratio = skillratio * 75 / 100;
					break;
				case SC_FATALMENACE:
					skillratio = 100 * (skill_lv+1);
					RE_LVL_DMOD(100);
					break;
				case SC_TRIANGLESHOT:
					skillratio = ( 300 + (skill_lv-1) * status_get_agi(src)/2 );
					RE_LVL_DMOD(120);
					break;
				case SC_FEINTBOMB:
					skillratio = (skill_lv+1) * (st->dex/2) * (sd?sd->status.job_level:50)/10;
					RE_LVL_DMOD(120);
					break;
				case LG_CANNONSPEAR:
					skillratio = (50 + st->str) * skill_lv;
					RE_LVL_DMOD(100);
					break;
				case LG_BANISHINGPOINT:
					skillratio = 50 * skill_lv + 30 * (sd ? pc->checkskill(sd,SM_BASH) : 10);
					RE_LVL_DMOD(100);
					break;
				case LG_SHIELDPRESS:
					skillratio = 150 * skill_lv + st->str;
					if( sd ) {
						short index = sd->equip_index[EQI_HAND_L];
						if( index >= 0 && sd->inventory_data[index] && sd->inventory_data[index]->type == IT_ARMOR )
						skillratio += sd->inventory_data[index]->weight / 10;
					}
					RE_LVL_DMOD(100);
					break;
				case LG_PINPOINTATTACK:
					skillratio = 100 * skill_lv + 5 * st->agi;
					RE_LVL_DMOD(120);
					break;
				case LG_RAGEBURST:
					if( sc ){
						skillratio += -100 + (status_get_max_hp(src) - status_get_hp(src)) / 100 + sc->fv_counter * 200;
						clif->millenniumshield(src, (sc->fv_counter = 0));
					}
					RE_LVL_DMOD(100);
					break;
				case LG_SHIELDSPELL:
					if ( sd && skill_lv == 1 ) {
						struct item_data *shield_data = sd->inventory_data[sd->equip_index[EQI_HAND_L]];
						if( shield_data )
							skillratio = 4 * status->get_lv(src) + 10 * shield_data->def + 2 * st->vit;
						}
					else
						skillratio = 0; // Prevents ATK damage from being done on LV 2 usage since LV 2 us MATK. [Rytech]
					break;
				case LG_MOONSLASHER:
					skillratio = 120 * skill_lv + 80 * (sd ? pc->checkskill(sd,LG_OVERBRAND) : 5);
					RE_LVL_DMOD(100);
					break;
				case LG_OVERBRAND:
					skillratio += -100 + 400 * skill_lv + 50 * ((sd) ? pc->checkskill(sd,CR_SPEARQUICKEN) : 1);
					RE_LVL_DMOD(100);
					break;
				case LG_OVERBRAND_BRANDISH:
					skillratio += -100 + 300 * skill_lv + status_get_str(src) + status_get_dex(src);
					RE_LVL_DMOD(100);
					break;
				case LG_OVERBRAND_PLUSATK:
					skillratio = 200 * skill_lv + rnd_value( 10, 100);
					RE_LVL_DMOD(100);
					break;
				case LG_RAYOFGENESIS:
					skillratio = 300 + 300 * skill_lv;
					RE_LVL_DMOD(100);
					break;
				case LG_EARTHDRIVE:
					if( sd ) {
						short index = sd->equip_index[EQI_HAND_L];
						if( index >= 0 && sd->inventory_data[index] && sd->inventory_data[index]->type == IT_ARMOR )
						skillratio = (1 + skill_lv) * sd->inventory_data[index]->weight / 10;
					}
					RE_LVL_DMOD(100);
					break;
				case LG_HESPERUSLIT:
					skillratio = 120 * skill_lv;
					if( sc && sc->data[SC_BANDING] )
						skillratio += 200 * sc->data[SC_BANDING]->val2;
					if( sc && sc->data[SC_BANDING] && sc->data[SC_BANDING]->val2 > 5 )
						skillratio = skillratio * 150 / 100;
					if( sc && sc->data[SC_INSPIRATION] )
						skillratio += 600;
					RE_LVL_DMOD(100);
					break;
				case SR_DRAGONCOMBO:
					skillratio += 40 * skill_lv;
					RE_LVL_DMOD(100);
					break;
				case SR_SKYNETBLOW:
					if( sc && sc->data[SC_COMBOATTACK] && sc->data[SC_COMBOATTACK]->val1 == SR_DRAGONCOMBO )//ATK [{(Skill Level x 100) + (Caster AGI) + 150} x Caster Base Level / 100] %
						skillratio += 100 * skill_lv + status_get_agi(src) + 50;
					else //ATK [{(Skill Level x 80) + (Caster AGI)} x Caster Base Level / 100] %
						skillratio += -100 + 80 * skill_lv + status_get_agi(src);
					RE_LVL_DMOD(100);
					break;
				case SR_EARTHSHAKER:
					if( tsc && (tsc->data[SC_HIDING] || tsc->data[SC_CLOAKING] || // [(Skill Level x 150) x (Caster Base Level / 100) + (Caster INT x 3)] %
						tsc->data[SC_CHASEWALK] || tsc->data[SC_CLOAKINGEXCEED] || tsc->data[SC__INVISIBILITY]) ){
						skillratio += -100 + 150 * skill_lv;
						RE_LVL_DMOD(100);
						skillratio += status_get_int(src) * 3;
					}else{ //[(Skill Level x 50) x (Caster Base Level / 100) + (Caster INT x 2)] %
						skillratio += 50 * (skill_lv-2);
						RE_LVL_DMOD(100);
						skillratio += status_get_int(src) * 2;
					}
					break;
				case SR_FALLENEMPIRE:// ATK [(Skill Level x 150 + 100) x Caster Base Level / 150] %
					skillratio += 150 *skill_lv;
					RE_LVL_DMOD(150);
					break;
				case SR_TIGERCANNON:// ATK [((Caster consumed HP + SP) / 4) x Caster Base Level / 100] %
					{
						int hp = status_get_max_hp(src) * (10 + 2 * skill_lv) / 100,
							sp = status_get_max_sp(src) * (6 + skill_lv) / 100;
						if( sc && sc->data[SC_COMBOATTACK] && sc->data[SC_COMBOATTACK]->val1 == SR_FALLENEMPIRE ) // ATK [((Caster consumed HP + SP) / 2) x Caster Base Level / 100] %
							skillratio += -100 + (hp+sp) / 2;
						else
							skillratio += -100 + (hp+sp) / 4;
						RE_LVL_DMOD(100);
					}
						break;
				case SR_RAMPAGEBLASTER:
					skillratio += 20 * skill_lv * (sd?sd->spiritball_old:5) - 100;
					if( sc && sc->data[SC_EXPLOSIONSPIRITS] ) {
						skillratio += sc->data[SC_EXPLOSIONSPIRITS]->val1 * 20;
						RE_LVL_DMOD(120);
					} else {
						RE_LVL_DMOD(150);
					}
					break;
				case SR_KNUCKLEARROW:
					if ( flag&4 || map->list[src->m].flag.gvg_castle || tst->mode&MD_BOSS ) {
						// ATK [(Skill Level x 150) + (1000 x Target current weight / Maximum weight) + (Target Base Level x 5) x (Caster Base Level / 150)] %
						skillratio = 150 * skill_lv + status->get_lv(target) * 5 * (status->get_lv(src) / 100) ;
						if( tsd && tsd->weight )
							skillratio += 100 * (tsd->weight / tsd->max_weight);
					}else // ATK [(Skill Level x 100 + 500) x Caster Base Level / 100] %
						skillratio += 400 + (100 * skill_lv);
					RE_LVL_DMOD(100);
					break;
				case SR_WINDMILL: // ATK [(Caster Base Level + Caster DEX) x Caster Base Level / 100] %
					skillratio = status->get_lv(src) + status_get_dex(src);
					RE_LVL_DMOD(100);
					break;
				case SR_GATEOFHELL:
					if( sc && sc->data[SC_COMBOATTACK]
						&& sc->data[SC_COMBOATTACK]->val1 == SR_FALLENEMPIRE )
						skillratio += 800 * skill_lv -100;
					else
						skillratio += 500 * skill_lv -100;
					RE_LVL_DMOD(100);
					break;
				case SR_GENTLETOUCH_QUIET:
					skillratio += 100 * skill_lv - 100 + status_get_dex(src);
					RE_LVL_DMOD(100);
					break;
				case SR_HOWLINGOFLION:
					skillratio += 300 * skill_lv - 100;
					RE_LVL_DMOD(150);
					break;
				case SR_RIDEINLIGHTNING: // ATK [{(Skill Level x 200) + Additional Damage} x Caster Base Level / 100] %
					if( (st->rhw.ele) == ELE_WIND || (st->lhw.ele) == ELE_WIND )
						skillratio += skill_lv * 50;
					skillratio += -100 + 200 * skill_lv;
					RE_LVL_DMOD(100);
					break;
				case WM_REVERBERATION_MELEE:
					skillratio += 200 + 100 * skill_lv;
					RE_LVL_DMOD(100);
					break;
				case WM_SEVERE_RAINSTORM_MELEE:
					skillratio = (st->agi + st->dex) * skill_lv / 5;
					RE_LVL_DMOD(100);
					break;
				case WM_GREAT_ECHO:
				{
					int chorusbonus = battle->calc_chorusbonus(sd);
					skillratio += 300 + 200 * skill_lv;
					//Chorus bonus don't count the first 2 Minstrel's/Wanderer's and only increases when their's 3 or more. [Rytech]
					if (chorusbonus >= 1 && chorusbonus <= 5)
						skillratio += 100<<(chorusbonus-1); // 1->100; 2->200; 3->400; 4->800; 5->1600
					RE_LVL_DMOD(100);
				}
					break;
				case GN_CART_TORNADO:
				{
					int strbonus = bst->str;
					skillratio = 50 * skill_lv + (sd ? sd->cart_weight : battle_config.max_cart_weight) / 10 / max(150 - strbonus, 1) + 50 * (sd ? pc->checkskill(sd, GN_REMODELING_CART) : 5);
				}
					break;
				case GN_CARTCANNON:
					skillratio = 50 * (sd ? pc->checkskill(sd, GN_REMODELING_CART) : 5) * (st->int_ / 40) + 60 * skill_lv;
					break;
				case GN_SPORE_EXPLOSION:
					skillratio = 100 * skill_lv + (200 + st->int_) * status->get_lv(src) / 100;
					/* Fall through */
				case GN_CRAZYWEED_ATK:
					skillratio += 400 + 100 * skill_lv;
					break;
				case GN_SLINGITEM_RANGEMELEEATK:
					if( sd ) {
						switch( sd->itemid ) {
							case ITEMID_APPLE_BOMB:
								skillratio = st->str + st->dex + 300;
								break;
							case ITEMID_MELON_BOMB:
								skillratio = st->str + st->dex + 500;
								break;
							case ITEMID_COCONUT_BOMB:
							case ITEMID_PINEAPPLE_BOMB:
							case ITEMID_BANANA_BOMB:
								skillratio = st->str + st->dex + 800;
								break;
							case ITEMID_BLACK_LUMP:
								skillratio = (st->str + st->agi + st->dex) / 3; // Black Lump
								break;
							case ITEMID_BLACK_HARD_LUMP:
								skillratio = (st->str + st->agi + st->dex) / 2; // Hard Black Lump
								break;
							case ITEMID_VERY_HARD_LUMP:
								skillratio = st->str + st->agi + st->dex; // Extremely Hard Black Lump
								break;
						}
					}
					break;
				case SO_VARETYR_SPEAR://ATK [{( Striking Level x 50 ) + ( Varetyr Spear Skill Level x 50 )} x Caster Base Level / 100 ] %
					skillratio += -100 + 50 * skill_lv + ( sd ? pc->checkskill(sd, SO_STRIKING) * 50 : 0 );
					if( sc && sc->data[SC_BLAST_OPTION] )
						skillratio += (sd ? sd->status.job_level * 5 : 0);
					break;
					// Physical Elemental Spirits Attack Skills
				case EL_CIRCLE_OF_FIRE:
				case EL_FIRE_BOMB_ATK:
				case EL_STONE_RAIN:
					skillratio += 200;
					break;
				case EL_FIRE_WAVE_ATK:
					skillratio += 500;
					break;
				case EL_TIDAL_WEAPON:
					skillratio += 1400;
					break;
				case EL_WIND_SLASH:
					skillratio += 100;
					break;
				case EL_HURRICANE:
					skillratio += 600;
					break;
				case EL_TYPOON_MIS:
				case EL_WATER_SCREW_ATK:
					skillratio += 900;
					break;
				case EL_STONE_HAMMER:
					skillratio += 400;
					break;
				case EL_ROCK_CRUSHER:
					skillratio += 700;
					break;
				case KO_JYUMONJIKIRI:
					skillratio += -100 + 150 * skill_lv;
					RE_LVL_DMOD(120);
					if( tsc && tsc->data[SC_KO_JYUMONJIKIRI] )
						skillratio += status->get_lv(src) * skill_lv;
					break;
				case KO_HUUMARANKA:
					skillratio += -100 + 150 * skill_lv + status_get_agi(src) + status_get_dex(src) + 100 * (sd ? pc->checkskill(sd, NJ_HUUMA) : 0);
					break;
				case KO_SETSUDAN:
					skillratio += -100 + 100 * skill_lv;
					RE_LVL_DMOD(100);
					break;
				case MH_NEEDLE_OF_PARALYZE:
					skillratio += 600 + 100 * skill_lv;
					break;
				case MH_STAHL_HORN:
					skillratio += 400 + 100 * skill_lv;
					break;
				case MH_LAVA_SLIDE:
					skillratio += -100 + 70 * skill_lv;
					break;
				case MH_TINDER_BREAKER:
				case MH_MAGMA_FLOW:
					skillratio += -100 + 100 * skill_lv;
					break;
				default:
					battle->calc_skillratio_weapon_unknown(&attack_type, src, target, &skill_id, &skill_lv, &skillratio, &flag);
					break;
			}
			//Skill damage modifiers that stack linearly
			if(sc && skill_id != PA_SACRIFICE){
#ifdef RENEWAL_EDP
				if( sc->data[SC_EDP] ){
					if( skill_id == AS_SONICBLOW ||
						skill_id == GC_COUNTERSLASH ||
						skill_id == GC_CROSSIMPACT )
							skillratio >>= 1;
				}
#endif
				if(sc->data[SC_OVERTHRUST])
					skillratio += sc->data[SC_OVERTHRUST]->val3;
				if(sc->data[SC_OVERTHRUSTMAX])
					skillratio += sc->data[SC_OVERTHRUSTMAX]->val2;
				if(sc->data[SC_BERSERK])
#ifndef RENEWAL
					skillratio += 100;
#else
					skillratio += 200;
				if( sc->data[SC_TRUESIGHT] )
					skillratio += 2*sc->data[SC_TRUESIGHT]->val1;
				if( sc->data[SC_LKCONCENTRATION] )
					skillratio += sc->data[SC_LKCONCENTRATION]->val2;
				if( sd && sd->status.weapon == W_KATAR && (i=pc->checkskill(sd,ASC_KATAR)) > 0 )
					skillratio += skillratio * (10 + 2 * i) / 100;
#endif
				if( (!skill_id || skill_id == KN_AUTOCOUNTER) && sc->data[SC_CRUSHSTRIKE] ){
					if( sd )
					{//ATK [{Weapon Level * (Weapon Upgrade Level + 6) * 100} + (Weapon ATK) + (Weapon Weight)]%
						short index = sd->equip_index[EQI_HAND_R];
						if( index >= 0 && sd->inventory_data[index] && sd->inventory_data[index]->type == IT_WEAPON )
							skillratio += -100 + sd->inventory_data[index]->weight/10 + st->rhw.atk +
								100 * sd->inventory_data[index]->wlv * (sd->status.inventory[index].refine + 6);
					}
					status_change_end(src, SC_CRUSHSTRIKE, INVALID_TIMER);
					skill->break_equip(src,EQP_WEAPON,2000,BCT_SELF); // 20% chance to destroy the weapon.
				}
			}
	}
	if( skillratio < 1 )
		return 0;
	return skillratio;
}

void battle_calc_skillratio_magic_unknown(int *attack_type, struct block_list *src, struct block_list *target, uint16 *skill_id, uint16 *skill_lv, int *skillratio, int *flag) {
}

void battle_calc_skillratio_weapon_unknown(int *attack_type, struct block_list *src, struct block_list *target, uint16 *skill_id, uint16 *skill_lv, int *skillratio, int *flag) {
}

/*==========================================
 * Check damage trough status.
 * ATK may be MISS, BLOCKED FAIL, reduce, increase, end status...
 * After this we apply bg/gvg reduction
 *------------------------------------------*/
int64 battle_calc_damage(struct block_list *src,struct block_list *bl,struct Damage *d,int64 damage,uint16 skill_id,uint16 skill_lv) {
	struct map_session_data *sd = NULL;
	struct status_change *sc, *tsc;
	struct status_change_entry *sce;
	int div_ = d->div_, flag = d->flag;

	nullpo_ret(bl);

	// need check src for null pointer?

	if( !damage )
		return 0;
	if( battle_config.ksprotection && mob->ksprotected(src, bl) )
		return 0;
	if (bl->type == BL_PC) {
		sd=(struct map_session_data *)bl;
		//Special no damage states
		if(flag&BF_WEAPON && sd->special_state.no_weapon_damage)
			damage -= damage * sd->special_state.no_weapon_damage / 100;

		if(flag&BF_MAGIC && sd->special_state.no_magic_damage)
			damage -= damage * sd->special_state.no_magic_damage / 100;

		if(flag&BF_MISC && sd->special_state.no_misc_damage)
			damage -= damage * sd->special_state.no_misc_damage / 100;

		if(!damage) return 0;
	}

	sc = status->get_sc(bl);
	tsc = status->get_sc(src);

	if( sc && sc->data[SC_INVINCIBLE] && !sc->data[SC_INVINCIBLEOFF] )
		return 1;

	if (skill_id == PA_PRESSURE)
		return damage; //This skill bypass everything else.

	if( sc && sc->count )
	{
		//First, sc_*'s that reduce damage to 0.
		if( sc->data[SC_BASILICA] && !(status_get_mode(src)&MD_BOSS) )
		{
			d->dmg_lv = ATK_BLOCK;
			return 0;
		}
		if( sc->data[SC_WHITEIMPRISON] && skill_id != HW_GRAVITATION ) { // Gravitation and Pressure do damage without removing the effect
			if( skill_id == MG_NAPALMBEAT ||
				skill_id == MG_SOULSTRIKE ||
				skill_id == WL_SOULEXPANSION ||
				(skill_id && skill->get_ele(skill_id, skill_lv) == ELE_GHOST) ||
				(!skill_id && (status->get_status_data(src))->rhw.ele == ELE_GHOST)
					){
				if( skill_id == WL_SOULEXPANSION )
					damage <<= 1; // If used against a player in White Imprison, the skill deals double damage.
				status_change_end(bl,SC_WHITEIMPRISON,INVALID_TIMER); // Those skills do damage and removes effect
			}else{
				d->dmg_lv = ATK_BLOCK;
				return 0;
			}
		}

		if( sc->data[SC_ZEPHYR] && ((flag&BF_LONG) || rnd()%100 < 10) ) {
				d->dmg_lv = ATK_BLOCK;
				return 0;
		}

		if( sc->data[SC_SAFETYWALL] && (flag&(BF_SHORT|BF_MAGIC))==BF_SHORT )
		{
			struct skill_unit_group* group = skill->id2group(sc->data[SC_SAFETYWALL]->val3);
			uint16 src_skill_id = sc->data[SC_SAFETYWALL]->val2;
			if (group) {
				d->dmg_lv = ATK_BLOCK;
				if(src_skill_id == MH_STEINWAND){
					if (--group->val2<=0)
						skill->del_unitgroup(group,ALC_MARK);
					if( (group->val3 - damage) > 0 )
						group->val3 -= (int)cap_value(damage, INT_MIN, INT_MAX);
					else
						skill->del_unitgroup(group,ALC_MARK);
					return 0;
				}
				if( skill_id == SO_ELEMENTAL_SHIELD ) {
					if ( ( group->val2 - damage) > 0 ) {
						group->val2 -= (int)cap_value(damage,INT_MIN,INT_MAX);
					} else
						skill->del_unitgroup(group,ALC_MARK);
					return 0;
				}
				/**
				 * in RE, SW possesses a lifetime equal to 3 times the caster's health
				 **/
			#ifdef RENEWAL
				if ( ( group->val2 - damage) > 0 ) {
					group->val2 -= (int)cap_value(damage,INT_MIN,INT_MAX);
				} else
					skill->del_unitgroup(group,ALC_MARK);
			#else
				if (--group->val2<=0)
					skill->del_unitgroup(group,ALC_MARK);
			#endif
				return 0;
			}
			status_change_end(bl, SC_SAFETYWALL, INVALID_TIMER);
		}

		if( ( sc->data[SC_PNEUMA] && (flag&(BF_MAGIC|BF_LONG)) == BF_LONG ) || sc->data[SC__MANHOLE] ) {
			d->dmg_lv = ATK_BLOCK;
			return 0;
		}
		if( sc->data[SC_NEUTRALBARRIER] && (flag&(BF_MAGIC|BF_LONG)) == BF_LONG && skill_id != CR_ACIDDEMONSTRATION ) {
			d->dmg_lv = ATK_BLOCK;
			return 0;
		}
		if( sc->data[SC__MAELSTROM] && (flag&BF_MAGIC) && skill_id && (skill->get_inf(skill_id)&INF_GROUND_SKILL) ) {
			// {(Maelstrom Skill LevelxAbsorbed Skill Level)+(Caster's Job/5)}/2
			int sp = (sc->data[SC__MAELSTROM]->val1 * skill_lv + sd->status.job_level / 5) / 2;
			status->heal(bl, 0, sp, 3);
			d->dmg_lv = ATK_BLOCK;
			return 0;
		}
		if( sc->data[SC_WEAPONBLOCKING] && flag&(BF_SHORT|BF_WEAPON) && rnd()%100 < sc->data[SC_WEAPONBLOCKING]->val2 )
		{
			clif->skill_nodamage(bl,src,GC_WEAPONBLOCKING,1,1);
			d->dmg_lv = ATK_BLOCK;
			sc_start2(src,bl,SC_COMBOATTACK,100,GC_WEAPONBLOCKING,src->id,2000);
			return 0;
		}
		if ((sce=sc->data[SC_AUTOGUARD]) && flag&BF_WEAPON && !(skill->get_nk(skill_id)&NK_NO_CARDFIX_ATK) && rnd()%100 < sce->val2) {
			int delay;
			struct status_change_entry *sce_d = sc->data[SC_DEVOTION];

			// different delay depending on skill level [celest]
			if (sce->val1 <= 5)
				delay = 300;
			else if (sce->val1 > 5 && sce->val1 <= 9)
				delay = 200;
			else
				delay = 100;

			if (sce_d) {
				// If the target is too far away from the devotion caster, autoguard has no effect
				// Autoguard will be disabled later on
				struct block_list *d_bl = map->id2bl(sce_d->val1);
				if (d_bl && check_distance_bl(bl, d_bl, sce_d->val3)
				  && ((d_bl->type == BL_MER && ((TBL_MER*)d_bl)->master && ((TBL_MER*)d_bl)->master->bl.id == bl->id)
				    || (d_bl->type == BL_PC && ((TBL_PC*)d_bl)->devotion[sce_d->val2] == bl->id))
				) {
					// if player is target of devotion, show guard effect on the devotion caster rather than the target
					clif->skill_nodamage(d_bl, d_bl, CR_AUTOGUARD, sce->val1, 1);
					unit->set_walkdelay(d_bl, timer->gettick(), delay, 1);

					d->dmg_lv = ATK_MISS;
					return 0;
				}
			} else {
				clif->skill_nodamage(bl, bl, CR_AUTOGUARD, sce->val1, 1);
				unit->set_walkdelay(bl, timer->gettick(), delay, 1);

				if(sc->data[SC_CR_SHRINK] && rnd()%100<5*sce->val1)
					skill->blown(bl,src,skill->get_blewcount(CR_SHRINK,1),-1,0);

				d->dmg_lv = ATK_MISS;
				return 0;
			}
		}

		if( (sce = sc->data[SC_MILLENNIUMSHIELD]) && sce->val2 > 0 && damage > 0 ) {
			clif->skill_nodamage(bl, bl, RK_MILLENNIUMSHIELD, 1, 1);
			sce->val3 -= (int)cap_value(damage,INT_MIN,INT_MAX); // absorb damage
			d->dmg_lv = ATK_BLOCK;
			sc_start(src,bl,SC_STUN,15,0,skill->get_time2(RK_MILLENNIUMSHIELD,sce->val1)); // There is a chance to be stunned when one shield is broken.
			if( sce->val3 <= 0 ) { // Shield Down
				sce->val2--;
				if( sce->val2 > 0 ) {
					clif->millenniumshield(bl,sce->val2);
					sce->val3 = 1000; // Next Shield
				} else
					status_change_end(bl,SC_MILLENNIUMSHIELD,INVALID_TIMER); // All shields down
			}
			return 0;
		}


		if( (sce=sc->data[SC_PARRYING]) && flag&BF_WEAPON && skill_id != WS_CARTTERMINATION && rnd()%100 < sce->val2 )
		{ // attack blocked by Parrying
			clif->skill_nodamage(bl, bl, LK_PARRYING, sce->val1,1);
			return 0;
		}

		if(sc->data[SC_DODGE_READY] && ( !sc->opt1 || sc->opt1 == OPT1_BURNING ) &&
			(flag&BF_LONG || sc->data[SC_STRUP])
			&& rnd()%100 < 20) {
			if (sd && pc_issit(sd)) pc->setstand(sd); //Stand it to dodge.
			clif->skill_nodamage(bl,bl,TK_DODGE,1,1);
			if (!sc->data[SC_COMBOATTACK])
				sc_start4(src, bl, SC_COMBOATTACK, 100, TK_JUMPKICK, src->id, 1, 0, 2000);
			return 0;
		}

		if((sc->data[SC_HERMODE]) && flag&BF_MAGIC)
			return 0;

		if(sc->data[SC_NJ_TATAMIGAESHI] && (flag&(BF_MAGIC|BF_LONG)) == BF_LONG)
			return 0;

		if ((sce=sc->data[SC_KAUPE]) && rnd()%100 < sce->val2) {
			//Kaupe blocks damage (skill or otherwise) from players, mobs, homuns, mercenaries.
			clif->specialeffect(bl, 462, AREA);
			//Shouldn't end until Breaker's non-weapon part connects.
			if (skill_id != ASC_BREAKER || !(flag&BF_WEAPON))
				if (--(sce->val3) <= 0) //We make it work like Safety Wall, even though it only blocks 1 time.
					status_change_end(bl, SC_KAUPE, INVALID_TIMER);
			return 0;
		}

		if( flag&BF_MAGIC && (sce=sc->data[SC_PRESTIGE]) && rnd()%100 < sce->val2) {
			clif->specialeffect(bl, 462, AREA); // Still need confirm it.
			return 0;
		}

		if (((sce=sc->data[SC_NJ_UTSUSEMI]) || sc->data[SC_NJ_BUNSINJYUTSU])
		&& flag&BF_WEAPON && !(skill->get_nk(skill_id)&NK_NO_CARDFIX_ATK)) {

			skill->additional_effect (src, bl, skill_id, skill_lv, flag, ATK_BLOCK, timer->gettick() );
			if( !status->isdead(src) )
				skill->counter_additional_effect( src, bl, skill_id, skill_lv, flag, timer->gettick() );
			if (sce) {
				clif->specialeffect(bl, 462, AREA);
				skill->blown(src,bl,sce->val3,-1,0);
			}
			//Both need to be consumed if they are active.
			if (sce && --(sce->val2) <= 0)
				status_change_end(bl, SC_NJ_UTSUSEMI, INVALID_TIMER);
			if ((sce=sc->data[SC_NJ_BUNSINJYUTSU]) && --(sce->val2) <= 0)
				status_change_end(bl, SC_NJ_BUNSINJYUTSU, INVALID_TIMER);

			return 0;
		}

		//Now damage increasing effects
		if( sc->data[SC_LEXAETERNA] && skill_id != PF_SOULBURN
#ifdef RENEWAL
		&& skill_id != CR_ACIDDEMONSTRATION
#endif
		)
		{
			if( src->type != BL_MER || skill_id == 0 )
				damage <<= 1; // Lex Aeterna only doubles damage of regular attacks from mercenaries

			if( skill_id != ASC_BREAKER || !(flag&BF_WEAPON) )
				status_change_end(bl, SC_LEXAETERNA, INVALID_TIMER); //Shouldn't end until Breaker's non-weapon part connects.
		}

#ifdef RENEWAL
		if( sc->data[SC_RAID] ) {
			damage += damage * 20 / 100;

			if (--sc->data[SC_RAID]->val1 == 0)
				status_change_end(bl, SC_RAID, INVALID_TIMER);
		}
#endif

		if( damage ) {
			struct map_session_data *tsd = BL_CAST(BL_PC, src);
			if( sc->data[SC_DEEP_SLEEP] ) {
				damage += damage / 2; // 1.5 times more damage while in Deep Sleep.
				status_change_end(bl,SC_DEEP_SLEEP,INVALID_TIMER);
			}
			if( tsd && sd && sc->data[SC_COLD] && flag&BF_WEAPON ){
				switch(tsd->status.weapon){
					case W_MACE:
					case W_2HMACE:
					case W_1HAXE:
					case W_2HAXE:
						damage = damage * 150/100;
						break;
					case W_MUSICAL:
					case W_WHIP:
						if(!sd->state.arrow_atk)
							break;
					case W_BOW:
					case W_REVOLVER:
					case W_RIFLE:
					case W_GATLING:
					case W_SHOTGUN:
					case W_GRENADE:
					case W_DAGGER:
					case W_1HSWORD:
					case W_2HSWORD:
						damage = damage * 50/100;
						break;
				}
			}
			if( sc->data[SC_SIREN] )
				status_change_end(bl,SC_SIREN,INVALID_TIMER);
		}

		//Finally damage reductions....
		// Assumptio doubles the def & mdef on RE mode, otherwise gives a reduction on the final damage. [Igniz]
#ifndef RENEWAL
		if( sc->data[SC_ASSUMPTIO] ) {
			if( map_flag_vs(bl->m) )
				damage = damage*2/3; //Receive 66% damage
			else
				damage >>= 1; //Receive 50% damage
		}
#endif

		if(sc->data[SC_DEFENDER] &&
			((flag&(BF_LONG|BF_WEAPON)) == (BF_LONG|BF_WEAPON) || skill_id == CR_ACIDDEMONSTRATION))
			damage = damage * ( 100 - sc->data[SC_DEFENDER]->val2 ) / 100;

		if(sc->data[SC_GS_ADJUSTMENT] &&
			(flag&(BF_LONG|BF_WEAPON)) == (BF_LONG|BF_WEAPON))
			damage -= damage * 20 / 100;

		if(sc->data[SC_FOGWALL]) {
			if(flag&BF_SKILL) { //25% reduction
				if ( !(skill->get_inf(skill_id)&INF_GROUND_SKILL) && !(skill->get_nk(skill_id)&NK_SPLASH) )
					damage -= 25*damage/100;
			} else if ((flag&(BF_LONG|BF_WEAPON)) == (BF_LONG|BF_WEAPON)) {
				damage >>= 2; //75% reduction
			}
		}

		if ( sc->data[SC_WATER_BARRIER] )
			damage = damage * ( 100 - 20 ) / 100;

		if( sc->data[SC_FIRE_EXPANSION_SMOKE_POWDER] ) {
			if( (flag&(BF_SHORT|BF_WEAPON)) == (BF_SHORT|BF_WEAPON) )
				damage -= 15 * damage / 100;//15% reduction to physical melee attacks
			else if( (flag&(BF_LONG|BF_WEAPON)) == (BF_LONG|BF_WEAPON) )
				damage -= 50 * damage / 100;//50% reduction to physical ranged attacks
		}

		// Compressed code, fixed by map.h [Epoque]
		if (src->type == BL_MOB) {
			int i;
			if (sc->data[SC_MANU_DEF])
				for (i=0;ARRAYLENGTH(mob->manuk)>i;i++)
					if (mob->manuk[i]==((TBL_MOB*)src)->class_) {
						damage -= damage * sc->data[SC_MANU_DEF]->val1 / 100;
						break;
					}
			if (sc->data[SC_SPL_DEF])
				for (i=0;ARRAYLENGTH(mob->splendide)>i;i++)
					if (mob->splendide[i]==((TBL_MOB*)src)->class_) {
						damage -= damage * sc->data[SC_SPL_DEF]->val1 / 100;
						break;
					}
		}

		if((sce=sc->data[SC_ARMOR]) && //NPC_DEFENDER
			sce->val3&flag && sce->val4&flag)
			damage -= damage * sc->data[SC_ARMOR]->val2 / 100;

		if( sc->data[SC_ENERGYCOAT] && (skill_id == GN_HELLS_PLANT_ATK ||
#ifdef RENEWAL
			((flag&BF_WEAPON || flag&BF_MAGIC) && skill_id != WS_CARTTERMINATION)
#else
			(flag&BF_WEAPON && skill_id != WS_CARTTERMINATION)
#endif
			) )
		{
			struct status_data *sstatus = status->get_status_data(bl);
			int per = 100*sstatus->sp / sstatus->max_sp -1; //100% should be counted as the 80~99% interval
			per /=20; //Uses 20% SP intervals.
			//SP Cost: 1% + 0.5% per every 20% SP
			if (!status->charge(bl, 0, (10+5*per)*sstatus->max_sp/1000))
				status_change_end(bl, SC_ENERGYCOAT, INVALID_TIMER);
			//Reduction: 6% + 6% every 20%
			damage -= damage * (6 * (1+per)) / 100;
		}
		if(sc->data[SC_GRANITIC_ARMOR]){
			damage -= damage * sc->data[SC_GRANITIC_ARMOR]->val2 / 100;
		}
		if(sc->data[SC_PAIN_KILLER]){
			damage -= damage * sc->data[SC_PAIN_KILLER]->val3 / 100;
		}
		if((sce=sc->data[SC_MAGMA_FLOW]) && (rnd()%100 <= sce->val2) ){
			skill->castend_damage_id(bl,src,MH_MAGMA_FLOW,sce->val1,timer->gettick(),0);
		}

		if( sc->data[SC_DARKCROW] && (flag&(BF_SHORT|BF_MAGIC)) == BF_SHORT )
			damage += damage * sc->data[SC_DARKCROW]->val2 / 100;

		if( (sce = sc->data[SC_STONEHARDSKIN]) && flag&(BF_SHORT|BF_WEAPON) && damage > 0 ) {
			sce->val2 -= (int)cap_value(damage,INT_MIN,INT_MAX);
			if( src->type == BL_PC ) {
				TBL_PC *ssd = BL_CAST(BL_PC, src);
				if (ssd && ssd->status.weapon != W_BOW)
					skill->break_equip(src, EQP_WEAPON, 3000, BCT_SELF);
			} else
				skill->break_equip(src, EQP_WEAPON, 3000, BCT_SELF);
			// 30% chance to reduce monster's ATK by 25% for 10 seconds.
			if( src->type == BL_MOB )
				sc_start(bl,src, SC_NOEQUIPWEAPON, 30, 0, skill->get_time2(RK_STONEHARDSKIN, sce->val1));
			if( sce->val2 <= 0 )
				status_change_end(bl, SC_STONEHARDSKIN, INVALID_TIMER);
		}

/**
 * In renewal steel body reduces all incoming damage by 1/10
 **/
#ifdef RENEWAL
		if( sc->data[SC_STEELBODY] ) {
			damage = damage > 10 ? damage / 10 : 1;
		}
#endif

		//Finally added to remove the status of immobile when aimedbolt is used. [Jobbie]
		if( skill_id == RA_AIMEDBOLT && (sc->data[SC_WUGBITE] || sc->data[SC_ANKLESNARE] || sc->data[SC_ELECTRICSHOCKER]) )
		{
			status_change_end(bl, SC_WUGBITE, INVALID_TIMER);
			status_change_end(bl, SC_ANKLESNARE, INVALID_TIMER);
			status_change_end(bl, SC_ELECTRICSHOCKER, INVALID_TIMER);
		}

		//Finally Kyrie because it may, or not, reduce damage to 0.
		if((sce = sc->data[SC_KYRIE]) && damage > 0){
			sce->val2 -= (int)cap_value(damage,INT_MIN,INT_MAX);
			if(flag&BF_WEAPON || skill_id == TF_THROWSTONE){
				if(sce->val2>=0)
					damage=0;
				else
					damage=-sce->val2;
			}
			if((--sce->val3)<=0 || (sce->val2<=0) || skill_id == AL_HOLYLIGHT)
				status_change_end(bl, SC_KYRIE, INVALID_TIMER);
		}

		if( sc->data[SC_MEIKYOUSISUI] && rnd()%100 < 40 ) // custom value
			damage = 0;


		if (!damage) return 0;

		if( (sce = sc->data[SC_LIGHTNINGWALK]) && flag&BF_LONG && rnd()%100 < sce->val1 ) {
			int dx[8]={0,-1,-1,-1,0,1,1,1};
			int dy[8]={1,1,0,-1,-1,-1,0,1};
			uint8 dir = map->calc_dir(bl, src->x, src->y);
			if( unit->movepos(bl, src->x-dx[dir], src->y-dy[dir], 1, 1) ) {
				clif->slide(bl,src->x-dx[dir],src->y-dy[dir]);
				unit->setdir(bl, dir);
			}
			d->dmg_lv = ATK_DEF;
			status_change_end(bl, SC_LIGHTNINGWALK, INVALID_TIMER);
			return 0;
		}

		//Probably not the most correct place, but it'll do here
		//(since battle_drain is strictly for players currently)
		if ((sce=sc->data[SC_HAMI_BLOODLUST]) && flag&BF_WEAPON && damage > 0 &&
			rnd()%100 < sce->val3)
			status->heal(src, damage*sce->val4/100, 0, 3);

		if( (sce = sc->data[SC_FORCEOFVANGUARD]) && flag&BF_WEAPON
			&& rnd()%100 < sce->val2 && sc->fv_counter <= sce->val3 )
				clif->millenniumshield(bl, sc->fv_counter++);

		if (sc->data[SC_STYLE_CHANGE] && rnd()%2) {
			TBL_HOM *hd = BL_CAST(BL_HOM,bl);
			if (hd) homun->addspiritball(hd, 10); //add a sphere
		}

		if( sc->data[SC__DEADLYINFECT] && flag&BF_SHORT && damage > 0 && rnd()%100 < 30 + 10 * sc->data[SC__DEADLYINFECT]->val1 && !is_boss(src) )
			status->change_spread(bl, src); // Deadly infect attacked side
	}

	//SC effects from caster side.
	sc = status->get_sc(src);

	if (sc && sc->count) {
		if( sc->data[SC_INVINCIBLE] && !sc->data[SC_INVINCIBLEOFF] )
			damage += damage * 75 / 100;
		// [Epoque]
		if (bl->type == BL_MOB) {
			int i;

			if ( ((sce=sc->data[SC_MANU_ATK]) && (flag&BF_WEAPON)) ||
				 ((sce=sc->data[SC_MANU_MATK]) && (flag&BF_MAGIC))
				)
				for (i=0;ARRAYLENGTH(mob->manuk)>i;i++)
					if (((TBL_MOB*)bl)->class_==mob->manuk[i]) {
						damage += damage * sce->val1 / 100;
						break;
					}
			if ( ((sce=sc->data[SC_SPL_ATK]) && (flag&BF_WEAPON)) ||
				 ((sce=sc->data[SC_SPL_MATK]) && (flag&BF_MAGIC))
				)
				for (i=0;ARRAYLENGTH(mob->splendide)>i;i++)
					if (((TBL_MOB*)bl)->class_==mob->splendide[i]) {
						damage += damage * sce->val1 / 100;
						break;
					}
		}
		if( tsc->data[SC_POISONINGWEAPON] ) {
			struct status_data *tstatus = status->get_status_data(bl);
			if ( !(flag&BF_SKILL) && (flag&BF_WEAPON) && damage > 0 && rnd()%100 < tsc->data[SC_POISONINGWEAPON]->val3 ) {
				short rate = 100;
				if ( tsc->data[SC_POISONINGWEAPON]->val1 == 9 ) // Oblivion Curse gives a 2nd success chance after the 1st one passes which is reducible. [Rytech]
					rate = 100 - tstatus->int_ * 4 / 5;
				sc_start(src,bl,tsc->data[SC_POISONINGWEAPON]->val2,rate,tsc->data[SC_POISONINGWEAPON]->val1,skill->get_time2(GC_POISONINGWEAPON,1) - (tstatus->vit + tstatus->luk) / 2 * 1000);
			}
		}
		if( sc->data[SC__DEADLYINFECT] && flag&BF_SHORT && damage > 0 && rnd()%100 < 30 + 10 * sc->data[SC__DEADLYINFECT]->val1 && !is_boss(src) )
			status->change_spread(src, bl);
		if (sc->data[SC_SHIELDSPELL_REF] && sc->data[SC_SHIELDSPELL_REF]->val1 == 1 && damage > 0)
			skill->break_equip(bl,EQP_ARMOR,10000,BCT_ENEMY );
		if (sc->data[SC_STYLE_CHANGE] && rnd()%2) {
			TBL_HOM *hd = BL_CAST(BL_HOM,bl);
			if (hd) homun->addspiritball(hd, 10);
		}
	}
	/* no data claims these settings affect anything other than players */
	if( damage && sd && bl->type == BL_PC ) {
		switch( skill_id ) {
			//case PA_PRESSURE: /* pressure also belongs to this list but it doesn't reach this area -- so don't worry about it */
			case HW_GRAVITATION:
			case NJ_ZENYNAGE:
			case KO_MUCHANAGE:
				break;
			default:
				if (flag & BF_SKILL) { //Skills get a different reduction than non-skills. [Skotlex]
					if (flag&BF_WEAPON)
						damage = damage * map->list[bl->m].weapon_damage_rate / 100;
					if (flag&BF_MAGIC)
						damage = damage * map->list[bl->m].magic_damage_rate / 100;
					if (flag&BF_MISC)
						damage = damage * map->list[bl->m].misc_damage_rate / 100;
				} else { //Normal attacks get reductions based on range.
					if (flag & BF_SHORT)
						damage = damage * map->list[bl->m].short_damage_rate / 100;
					if (flag & BF_LONG)
						damage = damage * map->list[bl->m].long_damage_rate / 100;
				}
				if(!damage) damage  = 1;
				break;
		}
	}

	if(battle_config.skill_min_damage && damage > 0 && damage < div_)
	{
		if ((flag&BF_WEAPON && battle_config.skill_min_damage&1)
			|| (flag&BF_MAGIC && battle_config.skill_min_damage&2)
			|| (flag&BF_MISC && battle_config.skill_min_damage&4)
		)
			damage = div_;
	}

	if( bl->type == BL_MOB && !status->isdead(bl) && src != bl) {
	  if (damage > 0 )
			mob->skill_event((TBL_MOB*)bl,src,timer->gettick(),flag);
	  if (skill_id)
			mob->skill_event((TBL_MOB*)bl,src,timer->gettick(),MSC_SKILLUSED|(skill_id<<16));
	}
	if( sd ) {
		if( pc_ismadogear(sd) && rnd()%100 < 50 ) {
			short element = skill->get_ele(skill_id, skill_lv);
			if( !skill_id || element == -1 ) { //Take weapon's element
				struct status_data *sstatus = NULL;
				if( src->type == BL_PC && ((TBL_PC*)src)->bonus.arrow_ele )
					element = ((TBL_PC*)src)->bonus.arrow_ele;
				else if( (sstatus = status->get_status_data(src)) ) {
					element = sstatus->rhw.ele;
				}
			}
			else if( element == -2 ) //Use enchantment's element
				element = status_get_attack_sc_element(src,status->get_sc(src));
			else if( element == -3 ) //Use random element
				element = rnd()%ELE_MAX;
			if( element == ELE_FIRE || element == ELE_WATER )
				pc->overheat(sd,element == ELE_FIRE ? 1 : -1);
		}
	}

	return damage;
}

/*==========================================
 * Calculates BG related damage adjustments.
 *------------------------------------------*/
int64 battle_calc_bg_damage(struct block_list *src, struct block_list *bl, int64 damage, int div_, uint16 skill_id, uint16 skill_lv, int flag)
{
	if( !damage )
		return 0;

	if( bl->type == BL_MOB ) {
		struct mob_data* md = BL_CAST(BL_MOB, bl);

		if( flag&BF_SKILL && (md->class_ == MOBID_BLUE_CRYST || md->class_ == MOBID_PINK_CRYST) )
			return 0; // Crystal cannot receive skill damage on battlegrounds
	}

	return damage;
}

/*==========================================
 * Calculates GVG related damage adjustments.
 *------------------------------------------*/
int64 battle_calc_gvg_damage(struct block_list *src,struct block_list *bl,int64 damage,int div_,uint16 skill_id,uint16 skill_lv,int flag) {
	struct mob_data* md = BL_CAST(BL_MOB, bl);
	int class_ = status->get_class(bl);

	if (!damage) //No reductions to make.
		return 0;

	if(md && md->guardian_data) {
		if(class_ == MOBID_EMPERIUM && flag&BF_SKILL) {
		//Skill immunity.
			switch (skill_id) {
#ifndef RENEWAL
			case MO_TRIPLEATTACK:
			case HW_GRAVITATION:
#endif
			case TF_DOUBLE:
				break;
			default:
				return 0;
			}
		}
		if(src->type != BL_MOB) {
			struct guild *g = src->type == BL_PC ? ((TBL_PC *)src)->guild : guild->search(status->get_guild_id(src));

			if (class_ == MOBID_EMPERIUM && (!g || guild->checkskill(g,GD_APPROVAL) <= 0 ))
				return 0;

			if (g && battle_config.guild_max_castles && guild->checkcastles(g)>=battle_config.guild_max_castles)
				return 0; // [MouseJstr]
		}
	}

	switch (skill_id) {
		case PA_PRESSURE:
		case HW_GRAVITATION:
		case NJ_ZENYNAGE:
		case KO_MUCHANAGE:
		case NC_SELFDESTRUCTION:
			break;
		default:
			/* Uncomment if you want god-mode Emperiums at 100 defense. [Kisuka]
			if (md && md->guardian_data) {
				damage -= damage * (md->guardian_data->castle->defense/100) * battle_config.castle_defense_rate/100;
			}
			*/
			break;
	}
	return damage;
}

/*==========================================
 * HP/SP drain calculation
 *------------------------------------------*/
int battle_calc_drain(int64 damage, int rate, int per) {
	int64 diff = 0;

	if (per && rnd()%1000 < rate) {
		diff = (damage * per) / 100;
		if (diff == 0) {
			if (per > 0)
				diff = 1;
			else
				diff = -1;
		}
	}
	return (int)cap_value(diff,INT_MIN,INT_MAX);
}

/*==========================================
 * Consumes ammo for the given skill.
 *------------------------------------------*/
void battle_consume_ammo(TBL_PC*sd, int skill_id, int lv) {
	int qty=1;
	if (!battle_config.arrow_decrement)
		return;

	if (skill_id) {
		qty = skill->get_ammo_qty(skill_id, lv);
		if (!qty) qty = 1;
	}

	if(sd->equip_index[EQI_AMMO]>=0) //Qty check should have been done in skill_check_condition
		pc->delitem(sd,sd->equip_index[EQI_AMMO],qty,0,1,LOG_TYPE_CONSUME);

	sd->state.arrow_atk = 0;
}
//Skill Range Criteria
int battle_range_type(struct block_list *src, struct block_list *target, uint16 skill_id, uint16 skill_lv) {
	if (battle_config.skillrange_by_distance &&
		(src->type&battle_config.skillrange_by_distance)
	) { //based on distance between src/target [Skotlex]
		if (check_distance_bl(src, target, 5))
			return BF_SHORT;
		return BF_LONG;
	}

	if (skill_id == SR_GATEOFHELL) {
		if (skill_lv < 5)
			return BF_SHORT;
		else
			return BF_LONG;
	}

	//based on used skill's range
	if (skill->get_range2(src, skill_id, skill_lv) < 5)
		return BF_SHORT;
	return BF_LONG;
}
int battle_adjust_skill_damage(int m, unsigned short skill_id) {
	if( map->list[m].skill_count ) {
		int i;
		ARR_FIND(0, map->list[m].skill_count, i, map->list[m].skills[i]->skill_id == skill_id );

		if( i < map->list[m].skill_count ) {
			return map->list[m].skills[i]->modifier;
		}
	}

	return 0;
}
int battle_blewcount_bonus(struct map_session_data *sd, uint16 skill_id) {
	int i;
	if (!sd->skillblown[0].id)
		return 0;
	//Apply the bonus blow count. [Skotlex]
	for (i = 0; i < ARRAYLENGTH(sd->skillblown) && sd->skillblown[i].id; i++) {
		if (sd->skillblown[i].id == skill_id)
			return sd->skillblown[i].val;
	}
	return 0;
}
//For quick div adjustment.
#define damage_div_fix(dmg, div) do { if ((div) > 1) (dmg)*=(div); else if ((div) < 0) (div)*=-1; } while(0)
/*==========================================
 * battle_calc_magic_attack [DracoRPG]
 *------------------------------------------*/
struct Damage battle_calc_magic_attack(struct block_list *src,struct block_list *target,uint16 skill_id,uint16 skill_lv,int mflag) {
	int nk;
	short s_ele = 0;
	unsigned int skillratio = 100; //Skill dmg modifiers.

	TBL_PC *sd;
	struct status_change *sc;
	struct Damage ad;
	struct status_data *sstatus = status->get_status_data(src);
	struct status_data *tstatus = status->get_status_data(target);
	struct {
		unsigned imdef : 2;
		unsigned infdef : 1;
	} flag;

	memset(&ad,0,sizeof(ad));
	memset(&flag,0,sizeof(flag));

	nullpo_retr(ad, src);
	nullpo_retr(ad, target);

	//Initial Values
	ad.damage = 1;
	ad.div_=skill->get_num(skill_id,skill_lv);
	ad.amotion = (skill->get_inf(skill_id)&INF_GROUND_SKILL) ? 0 : sstatus->amotion; //Amotion should be 0 for ground skills.
	ad.dmotion=tstatus->dmotion;
	ad.blewcount = skill->get_blewcount(skill_id,skill_lv);
	ad.flag=BF_MAGIC|BF_SKILL;
	ad.dmg_lv=ATK_DEF;
	nk = skill->get_nk(skill_id);
	flag.imdef = (nk&NK_IGNORE_DEF)? 1 : 0;

	sd = BL_CAST(BL_PC, src);

	sc = status->get_sc(src);

	//Initialize variables that will be used afterwards
	s_ele = skill->get_ele(skill_id, skill_lv);

	if (s_ele == -1){ // pl=-1 : the skill takes the weapon's element
		s_ele = sstatus->rhw.ele;
		if (sd && sd->charm_type != CHARM_TYPE_NONE && sd->charm_count >= MAX_SPIRITCHARM) {
			//Summoning 10 spiritcharm will endow your weapon
			s_ele = sd->charm_type;
		}
	}else if (s_ele == -2) //Use status element
		s_ele = status_get_attack_sc_element(src,status->get_sc(src));
	else if( s_ele == -3 ) //Use random element
		s_ele = rnd()%ELE_MAX;

	if( skill_id == SO_PSYCHIC_WAVE ) {
		if( sc && sc->count ) {
			if( sc->data[SC_HEATER_OPTION] ) s_ele = sc->data[SC_HEATER_OPTION]->val4;
			else if( sc->data[SC_COOLER_OPTION] ) s_ele = sc->data[SC_COOLER_OPTION]->val4;
			else if( sc->data[SC_BLAST_OPTION] ) s_ele = sc->data[SC_BLAST_OPTION]->val3;
			else if( sc->data[SC_CURSED_SOIL_OPTION] ) s_ele = sc->data[SC_CURSED_SOIL_OPTION]->val4;
		}
	}

	//Set miscellaneous data that needs be filled
	if(sd) {
		sd->state.arrow_atk = 0;
		ad.blewcount += battle->blewcount_bonus(sd, skill_id);
	}

	//Skill Range Criteria
	ad.flag |= battle->range_type(src, target, skill_id, skill_lv);
	flag.infdef = (tstatus->mode&MD_PLANT) ? 1 : 0;
	if( !flag.infdef && target->type == BL_SKILL && ((TBL_SKILL*)target)->group->unit_id == UNT_REVERBERATION )
		flag.infdef = 1; // Reverberation takes 1 damage

	switch(skill_id) {
		case MG_FIREWALL:
			if ( tstatus->def_ele == ELE_FIRE || battle->check_undead(tstatus->race, tstatus->def_ele) )
				ad.blewcount = 0; //No knockback
			break;
		case NJ_KAENSIN:
		case PR_SANCTUARY:
			ad.dmotion = 0; //No flinch animation.
			break;
		case WL_HELLINFERNO:
			if( mflag&ELE_DARK )
				s_ele = ELE_DARK;
			break;
		case KO_KAIHOU:
			if (sd && sd->charm_type != CHARM_TYPE_NONE && sd->charm_count > 0) {
				s_ele = sd->charm_type;
			}
			break;
#ifdef RENEWAL
		case CR_ACIDDEMONSTRATION:
		case ASC_BREAKER:
		case HW_MAGICCRASHER:
			flag.imdef = 2;
			break;
#endif
	}

	if (!flag.infdef) //No need to do the math for plants
	{
		int i;
#ifdef RENEWAL
		ad.damage = 0; //reinitialize..
#endif
//MATK_RATE scales the damage. 100 = no change. 50 is halved, 200 is doubled, etc
#define MATK_RATE( a ) ( ad.damage= ad.damage*(a)/100 )
//Adds dmg%. 100 = +100% (double) damage. 10 = +10% damage
#define MATK_ADDRATE( a ) ( ad.damage+= ad.damage*(a)/100 )
//Adds an absolute value to damage. 100 = +100 damage
#define MATK_ADD( a ) ( ad.damage+= (a) )

		switch (skill_id) {
			//Calc base damage according to skill
			case AL_HEAL:
			case PR_BENEDICTIO:
			case PR_SANCTUARY:
				ad.damage = skill->calc_heal(src, target, skill_id, skill_lv, false);
				break;
			/**
			 * Arch Bishop
			 **/
			case AB_HIGHNESSHEAL:
				ad.damage = skill->calc_heal(src, target, AL_HEAL, 10, false) * ( 17 + 3 * skill_lv ) / 10;
				break;
			case PR_ASPERSIO:
				ad.damage = 40;
				break;
			case ALL_RESURRECTION:
			case PR_TURNUNDEAD:
				//Undead check is on skill_castend_damageid code.
				i = 20*skill_lv + sstatus->luk + sstatus->int_ + status->get_lv(src)
				  + 200 - 200*tstatus->hp/tstatus->max_hp; // there is no changed in success chance in renewal. [malufett]
				if(i > 700) i = 700;
				if(rnd()%1000 < i && !(tstatus->mode&MD_BOSS))
					ad.damage = tstatus->hp;
				else {
				#ifdef RENEWAL
					MATK_ADD(status->get_matk(src, 2));
				#else
					ad.damage = status->get_lv(src) + sstatus->int_ + skill_lv * 10;
				#endif
				}
				break;
			case PF_SOULBURN:
				ad.damage = tstatus->sp * 2;
				break;
			/**
			 * Arch Bishop
			 **/
			case AB_RENOVATIO:
				//Damage calculation from iRO wiki. [Jobbie]
				ad.damage = status->get_lv(src) * 10 + sstatus->int_;
				break;
			default: {
				MATK_ADD( status->get_matk(src, 2) );

				if (nk&NK_SPLASHSPLIT) { // Divide MATK in case of multiple targets skill
					if(mflag>0)
						ad.damage/= mflag;
					else
						ShowError("0 enemies targeted by %d:%s, divide per 0 avoided!\n", skill_id, skill->get_name(skill_id));
				}

				if (sc){
					if( sc->data[SC_TELEKINESIS_INTENSE] && s_ele == ELE_GHOST )
						ad.damage += sc->data[SC_TELEKINESIS_INTENSE]->val3;
				}
				switch(skill_id){
					case MG_FIREBOLT:
					case MG_COLDBOLT:
					case MG_LIGHTNINGBOLT:
						if ( sc && sc->data[SC_SPELLFIST] && mflag&BF_SHORT )  {
							skillratio = sc->data[SC_SPELLFIST]->val2 * 50 + sc->data[SC_SPELLFIST]->val4 * 100;// val4 = used bolt level, val2 = used spellfist level. [Rytech]
							ad.div_ = 1;// ad mods, to make it work similar to regular hits [Xazax]
							ad.flag = BF_WEAPON|BF_SHORT;
							ad.type = 0;
						}
					/* Fall through */
					default:
						MATK_RATE(battle->calc_skillratio(BF_MAGIC, src, target, skill_id, skill_lv, skillratio, mflag));
				}
				//Constant/misc additions from skills
				if (skill_id == WZ_FIREPILLAR)
					MATK_ADD(100+50*skill_lv);
				if( sd && ( sd->status.class_ == JOB_ARCH_BISHOP_T || sd->status.class_ == JOB_ARCH_BISHOP ) &&
					(i=pc->checkskill(sd,AB_EUCHARISTICA)) > 0 &&
					(tstatus->race == RC_DEMON || tstatus->def_ele == ELE_DARK) )
					MATK_ADDRATE(i);
			}
		}
#ifndef HMAP_ZONE_DAMAGE_CAP_TYPE
		if( target && skill_id ) {
			for(i = 0; i < map->list[target->m].zone->capped_skills_count; i++) {
				if( skill_id == map->list[target->m].zone->capped_skills[i]->nameid && (map->list[target->m].zone->capped_skills[i]->type & target->type) ) {
					if( target->type == BL_MOB && map->list[target->m].zone->capped_skills[i]->subtype != MZS_NONE ) {
						if( (((TBL_MOB*)target)->status.mode&MD_BOSS) && !(map->list[target->m].zone->disabled_skills[i]->subtype&MZS_BOSS) )
							continue;
						if( ((TBL_MOB*)target)->special_state.clone && !(map->list[target->m].zone->disabled_skills[i]->subtype&MZS_CLONE) )
							continue;
					}
					if( ad.damage > map->list[target->m].zone->capped_skills[i]->cap )
						ad.damage = map->list[target->m].zone->capped_skills[i]->cap;
					if( ad.damage2 > map->list[target->m].zone->capped_skills[i]->cap )
						ad.damage2 = map->list[target->m].zone->capped_skills[i]->cap;
					break;
				}
			}
		}
#endif
#ifdef RENEWAL
		ad.damage = battle->calc_cardfix(BF_MAGIC, src, target, nk, s_ele, 0, ad.damage, 0, ad.flag);
#endif
		if(sd) {
			uint16 rskill;/* redirect skill */
			//Damage bonuses
			if ((i = pc->skillatk_bonus(sd, skill_id)))
				ad.damage += ad.damage*i/100;
			switch(skill_id){
				case WL_CHAINLIGHTNING_ATK:
					rskill = WL_CHAINLIGHTNING;
					break;
				case AB_DUPLELIGHT_MAGIC:
					rskill = AB_DUPLELIGHT;
					break;
				case WL_TETRAVORTEX_FIRE:
				case WL_TETRAVORTEX_WATER:
				case WL_TETRAVORTEX_WIND:
				case WL_TETRAVORTEX_GROUND:
					rskill = WL_TETRAVORTEX;
					break;
				case WL_SUMMON_ATK_FIRE:
				case WL_SUMMON_ATK_WIND:
				case WL_SUMMON_ATK_WATER:
				case WL_SUMMON_ATK_GROUND:
					rskill = WL_RELEASE;
					break;
				case WM_REVERBERATION_MAGIC:
					rskill = WM_REVERBERATION;
					break;
				default:
					rskill = skill_id;
			}
			if( (i = battle->adjust_skill_damage(src->m,rskill)) )
				MATK_RATE(i);

			//Ignore Defense?
			if (!flag.imdef && (
				sd->bonus.ignore_mdef_ele & ( 1 << tstatus->def_ele ) ||
				sd->bonus.ignore_mdef_race & ( 1 << tstatus->race ) ||
				sd->bonus.ignore_mdef_race & ( is_boss(target) ? 1 << RC_BOSS : 1 << RC_NONBOSS )
			))
				flag.imdef = 1;
		}

		ad.damage = battle->calc_defense(BF_MAGIC, src, target, skill_id, skill_lv, ad.damage, flag.imdef, 0);

		if (skill_id == NPC_EARTHQUAKE) {
			//Adds atk2 to the damage, should be influenced by number of hits and skill-ratio, but not mdef reductions. [Skotlex]
			//Also divide the extra bonuses from atk2 based on the number in range [Kevin]
			if(mflag>0)
				ad.damage+= (sstatus->rhw.atk2*skillratio/100)/mflag;
			else
				ShowError("Zero range by %d:%s, divide per 0 avoided!\n", skill_id, skill->get_name(skill_id));
		}

		if(ad.damage<1)
			ad.damage=1;
		else if(sc){//only applies when hit
			// TODO: there is another factor that contribute with the damage and need to be formulated. [malufett]
			switch(skill_id){
				case MG_LIGHTNINGBOLT:
				case MG_THUNDERSTORM:
				case MG_FIREBOLT:
				case MG_FIREWALL:
				case MG_COLDBOLT:
				case MG_FROSTDIVER:
				case WZ_EARTHSPIKE:
				case WZ_HEAVENDRIVE:
					if(sc->data[SC_GUST_OPTION] || sc->data[SC_PETROLOGY_OPTION]
						|| sc->data[SC_PYROTECHNIC_OPTION] || sc->data[SC_AQUAPLAY_OPTION])
						ad.damage += (6 + sstatus->int_/4) + max(sstatus->dex-10,0)/30;
					break;
			}
		}

		if (!(nk&NK_NO_ELEFIX))
			ad.damage=battle->attr_fix(src, target, ad.damage, s_ele, tstatus->def_ele, tstatus->ele_lv);

		if( skill_id == CR_GRANDCROSS || skill_id == NPC_GRANDDARKNESS )
		{ //Apply the physical part of the skill's damage. [Skotlex]
			struct Damage wd = battle->calc_weapon_attack(src,target,skill_id,skill_lv,mflag);
			ad.damage = battle->attr_fix(src, target, wd.damage + ad.damage, s_ele, tstatus->def_ele, tstatus->ele_lv) * (100 + 40*skill_lv)/100;
			if( src == target )
			{
				if( src->type == BL_PC )
					ad.damage = ad.damage/2;
				else
					ad.damage = 0;
			}
		}
#ifndef RENEWAL
		ad.damage = battle->calc_cardfix(BF_MAGIC, src, target, nk, s_ele, 0, ad.damage, 0, ad.flag);
#endif
	}

	damage_div_fix(ad.damage, ad.div_);

	if (flag.infdef && ad.damage)
		ad.damage = ad.damage>0?1:-1;
	if (skill_id != ASC_BREAKER)
		ad.damage = battle->calc_damage(src, target, &ad, ad.damage, skill_id, skill_lv);
	if( map_flag_gvg2(target->m) )
		ad.damage=battle->calc_gvg_damage(src,target,ad.damage,ad.div_,skill_id,skill_lv,ad.flag);
	else if( map->list[target->m].flag.battleground )
		ad.damage=battle->calc_bg_damage(src,target,ad.damage,ad.div_,skill_id,skill_lv,ad.flag);

	switch( skill_id ) { /* post-calc modifiers */
		case SO_VARETYR_SPEAR: { // Physical damage.
			struct Damage wd = battle->calc_weapon_attack(src,target,skill_id,skill_lv,mflag);
			if(!flag.infdef && ad.damage > 1)
				ad.damage += wd.damage;
			break;
		}
		//case HM_ERASER_CUTTER:
	}

	return ad;
#undef MATK_RATE
#undef MATK_ADDRATE
#undef MATK_ADD
}

/*==========================================
 * Calculate Misc damage for skill_id
 *------------------------------------------*/
struct Damage battle_calc_misc_attack(struct block_list *src,struct block_list *target,uint16 skill_id,uint16 skill_lv,int mflag) {
	int temp;
	short i, nk;
	short s_ele;

	struct map_session_data *sd, *tsd;
	struct Damage md; //DO NOT CONFUSE with md of mob_data!
	struct status_data *sstatus = status->get_status_data(src);
	struct status_data *tstatus = status->get_status_data(target);
	struct status_change *tsc = status->get_sc(target);
#ifdef RENEWAL
	struct status_change *sc = status->get_sc(src);
#endif

	memset(&md,0,sizeof(md));

	nullpo_retr(md, src);
	nullpo_retr(md, target);

	//Some initial values
	md.amotion = (skill->get_inf(skill_id)&INF_GROUND_SKILL) ? 0 : sstatus->amotion;
	md.dmotion=tstatus->dmotion;
	md.div_=skill->get_num( skill_id,skill_lv );
	md.blewcount=skill->get_blewcount(skill_id,skill_lv);
	md.dmg_lv=ATK_DEF;
	md.flag=BF_MISC|BF_SKILL;

	nk = skill->get_nk(skill_id);

	sd = BL_CAST(BL_PC, src);
	tsd = BL_CAST(BL_PC, target);

	if(sd) {
		sd->state.arrow_atk = 0;
		md.blewcount += battle->blewcount_bonus(sd, skill_id);
	}

	s_ele = skill->get_ele(skill_id, skill_lv);
	if (s_ele < 0 && s_ele != -3) //Attack that takes weapon's element for misc attacks? Make it neutral [Skotlex]
		s_ele = ELE_NEUTRAL;
	else if (s_ele == -3) //Use random element
		s_ele = rnd()%ELE_MAX;

	//Skill Range Criteria
	md.flag |= battle->range_type(src, target, skill_id, skill_lv);

	switch( skill_id )
	{
#ifdef RENEWAL
	case HT_LANDMINE:
	case MA_LANDMINE:
	case HT_BLASTMINE:
	case HT_CLAYMORETRAP:
		md.damage = skill_lv * sstatus->dex * (3+status->get_lv(src)/100) * (1+sstatus->int_/35);
		md.damage += md.damage * (rnd()%20-10) / 100;
		md.damage += 40 * (sd?pc->checkskill(sd,RA_RESEARCHTRAP):0);
		break;
#else
	case HT_LANDMINE:
	case MA_LANDMINE:
		md.damage=skill_lv*(sstatus->dex+75)*(100+sstatus->int_)/100;
		break;
	case HT_BLASTMINE:
		md.damage=skill_lv*(sstatus->dex/2+50)*(100+sstatus->int_)/100;
		break;
	case HT_CLAYMORETRAP:
		md.damage=skill_lv*(sstatus->dex/2+75)*(100+sstatus->int_)/100;
		break;
#endif
	case HT_BLITZBEAT:
	case SN_FALCONASSAULT:
		//Blitz-beat Damage.
		if(!sd || (temp = pc->checkskill(sd,HT_STEELCROW)) <= 0)
			temp=0;
		md.damage=(sstatus->dex/10+sstatus->int_/2+temp*3+40)*2;
		if(mflag > 1) //Autocasted Blitz.
			nk|=NK_SPLASHSPLIT;

		if (skill_id == SN_FALCONASSAULT) {
			//Div fix of Blitzbeat
			temp = skill->get_num(HT_BLITZBEAT, 5);
			damage_div_fix(md.damage, temp);

			//Falcon Assault Modifier
			md.damage=md.damage*(150+70*skill_lv)/100;
		}
		break;
	case TF_THROWSTONE:
		md.damage=50;
		break;
	case BA_DISSONANCE:
		md.damage=30+skill_lv*10;
		if (sd)
			md.damage+= 3*pc->checkskill(sd,BA_MUSICALLESSON);
		break;
	case NPC_SELFDESTRUCTION:
		md.damage = sstatus->hp;
		break;
	case NPC_SMOKING:
		md.damage=3;
		break;
	case NPC_DARKBREATH:
		md.damage = 500 + (skill_lv-1)*1000 + rnd()%1000;
		if(md.damage > 9999) md.damage = 9999;
		break;
	case PA_PRESSURE:
		md.damage=500+300*skill_lv;
		break;
	case PA_GOSPEL:
		md.damage = 1+rnd()%9999;
		break;
	case CR_ACIDDEMONSTRATION:
#ifdef RENEWAL
		{// [malufett]
			int64 matk=0, atk;
			short tdef = status->get_total_def(target);
			short tmdef =  status->get_total_mdef(target);
			int targetVit = min(120, status_get_vit(target));
			short totaldef = (tmdef + tdef - ((uint64)(tmdef + tdef) >> 32)) >> 1;

			matk = battle->calc_magic_attack(src, target, skill_id, skill_lv, mflag).damage;
			atk = battle->calc_base_damage(src, target, skill_id, skill_lv, nk, false, s_ele, ELE_NEUTRAL, EQI_HAND_R, (sc && sc->data[SC_MAXIMIZEPOWER]?1:0)|(sc && sc->data[SC_WEAPONPERFECT]?8:0), md.flag);
			md.damage = matk + atk;
			if( src->type == BL_MOB ){
				totaldef = (tdef + tmdef) >> 1;
				md.damage = 7 * targetVit * skill_lv * (atk + matk) / 100;
				/*
				// Pending [malufett]
				if( unknown condition ){
					md.damage = 7 * md.damage % 20;
					md.damage = 7 * md.damage / 20;
				}*/
			}else{
				float vitfactor = 0.0f, ftemp;

				if( (vitfactor=(status_get_vit(target)-120.0f)) > 0)
					vitfactor = (vitfactor * (matk + atk) / 10) / status_get_vit(target);
				ftemp = max(0, vitfactor) + (targetVit * (matk + atk)) / 10;
				md.damage = (int64)(ftemp * 70 * skill_lv / 100);
				if (target->type == BL_PC)
					md.damage >>= 1;
			}
			md.damage -= totaldef;
			if( tsc && tsc->data[SC_LEXAETERNA] ) {
				md.damage <<= 1;
				status_change_end(target, SC_LEXAETERNA, INVALID_TIMER);
			}
		}
#else
		// updated the formula based on a Japanese formula found to be exact [Reddozen]
		if(tstatus->vit+sstatus->int_) //crash fix
			md.damage = (int)(7*tstatus->vit*sstatus->int_*sstatus->int_ / (10*(tstatus->vit+sstatus->int_)));
		else
			md.damage = 0;
		if (tsd) md.damage>>=1;
#endif
		// Some monsters have totaldef higher than md.damage in some cases, leading to md.damage < 0
		if( md.damage < 0 )
			md.damage = 0;
		if( md.damage > INT_MAX>>1 )
			//Overflow prevention, will anyone whine if I cap it to a few billion?
			//Not capped to INT_MAX to give some room for further damage increase.
			md.damage = INT_MAX>>1;
		break;

	case KO_MUCHANAGE:
		md.damage = skill->get_zeny(skill_id ,skill_lv);
		md.damage = md.damage * (50 + rnd()%50) / 100;
		if ( is_boss(target) || (sd && !pc->checkskill(sd,NJ_TOBIDOUGU)) )
			md.damage >>= 1;
		break;
	case NJ_ZENYNAGE:
		md.damage = skill->get_zeny(skill_id ,skill_lv);
		if (!md.damage) md.damage = 2;
		md.damage = rnd()%md.damage + md.damage;
		if (is_boss(target))
			md.damage=md.damage / 3;
		else if (tsd)
			md.damage=md.damage / 2;
		break;
	case GS_FLING:
		md.damage = sd?sd->status.job_level:status->get_lv(src);
		break;
	case HVAN_EXPLOSION: //[orn]
		md.damage = sstatus->max_hp * (50 + 50 * skill_lv) / 100;
		break ;
	case ASC_BREAKER:
		{
#ifndef RENEWAL
		md.damage = 500+rnd()%500 + 5*skill_lv * sstatus->int_;
		nk|=NK_IGNORE_FLEE|NK_NO_ELEFIX; //These two are not properties of the weapon based part.
#else
		int ratio = 300 + 50 * skill_lv;
		int64 matk = battle->calc_magic_attack(src, target, skill_id, skill_lv, mflag).damage;
		short totaldef = status->get_total_def(target) + status->get_total_mdef(target);
		int64 atk = battle->calc_base_damage(src, target, skill_id, skill_lv, nk, false, s_ele, ELE_NEUTRAL, EQI_HAND_R, (sc && sc->data[SC_MAXIMIZEPOWER]?1:0)|(sc && sc->data[SC_WEAPONPERFECT]?8:0), md.flag);

		if( sc && sc->data[SC_EDP] )
			ratio >>= 1;
		md.damage = (matk + atk) * ratio / 100;
		md.damage -= totaldef;
#endif
		}
		break;
	case HW_GRAVITATION:
		md.damage = 200+200*skill_lv;
		md.dmotion = 0; //No flinch animation.
		break;
	case NPC_EVILLAND:
		md.damage = skill->calc_heal(src,target,skill_id,skill_lv,false);
		break;
	case RK_DRAGONBREATH:
	case RK_DRAGONBREATH_WATER:
		md.damage = ((status_get_hp(src) / 50) + (status_get_max_sp(src) / 4)) * skill_lv;
		RE_LVL_MDMOD(150);
		if (sd) md.damage = md.damage * (95 + 5 * pc->checkskill(sd,RK_DRAGONTRAINING)) / 100;
		md.flag |= BF_LONG|BF_WEAPON;
		break;
	/**
	 * Ranger
	 **/
	case RA_CLUSTERBOMB:
	case RA_FIRINGTRAP:
	case RA_ICEBOUNDTRAP:
		md.damage = skill_lv * sstatus->dex + sstatus->int_ * 5 ;
		RE_LVL_TMDMOD();
		if(sd)
		{
			int researchskill_lv = pc->checkskill(sd,RA_RESEARCHTRAP);
			if(researchskill_lv)
				md.damage = md.damage * 20 * researchskill_lv / (skill_id == RA_CLUSTERBOMB?50:100);
			else
				md.damage = 0;
		}else
			md.damage = md.damage * 200 / (skill_id == RA_CLUSTERBOMB?50:100);

		break;
	case WM_SOUND_OF_DESTRUCTION:
		md.damage = 1000 * skill_lv + sstatus->int_ * (sd ? pc->checkskill(sd,WM_LESSON) : 10);
		md.damage += md.damage * 10 * battle->calc_chorusbonus(sd) / 100;
		break;
	/**
	 * Mechanic
	 **/
	case NC_SELFDESTRUCTION:
		{
#ifdef RENEWAL
			short totaldef = status->get_total_def(target);
#else
			short totaldef = tstatus->def2 + (short)status->get_def(target);
#endif
			md.damage = ( (sd?pc->checkskill(sd,NC_MAINFRAME):10) + 8 ) * ( skill_lv + 1 ) * ( status_get_sp(src) + sstatus->vit );
			RE_LVL_MDMOD(100);
			md.damage += status_get_hp(src) - totaldef;
		}
		break;
	case NC_MAGMA_ERUPTION:
		md.damage = 1200 + 400 * skill_lv;
		break;
	case GN_THORNS_TRAP:
		md.damage = 100 + 200 * skill_lv + sstatus->int_;
		break;
	case GN_HELLS_PLANT_ATK:
		md.damage = skill_lv * status->get_lv(target) * 10 + sstatus->int_ * 7 / 2 * (18 + (sd ? sd->status.job_level : 0) / 4) * (5 / (10 - (sd ? pc->checkskill(sd, AM_CANNIBALIZE) : 0)));
		md.damage = md.damage*(1000 + tstatus->mdef) / (1000 + tstatus->mdef * 10) - tstatus->mdef2;
		break;
	case KO_HAPPOKUNAI:
		{
			struct Damage wd = battle->calc_weapon_attack(src, target, 0, 1, mflag);
#ifdef RENEWAL
			short totaldef = status->get_total_def(target);
#else
			short totaldef = tstatus->def2 + (short)status->get_def(target);
#endif
			if ( sd )	wd.damage += sd->bonus.arrow_atk;
			md.damage = (int)(3 * (1 + wd.damage) * (5 + skill_lv) / 5.0f);
			md.damage -= totaldef;

		}
		break;
	default:
		battle->calc_misc_attack_unknown(src, target, &skill_id, &skill_lv, &mflag, &md);
		break;
	}

	if (nk&NK_SPLASHSPLIT){ // Divide ATK among targets
		if(mflag>0)
			md.damage/= mflag;
		else
			ShowError("0 enemies targeted by %d:%s, divide per 0 avoided!\n", skill_id, skill->get_name(skill_id));
	}

	damage_div_fix(md.damage, md.div_);

	if (!(nk&NK_IGNORE_FLEE))
	{
		i = 0; //Temp for "hit or no hit"
		if(tsc && tsc->opt1 && tsc->opt1 != OPT1_STONEWAIT && tsc->opt1 != OPT1_BURNING)
			i = 1;
		else {
			short
				flee = tstatus->flee,
#ifdef RENEWAL
				hitrate = 0; //Default hitrate
#else
				hitrate = 80; //Default hitrate
#endif

			if(battle_config.agi_penalty_type && battle_config.agi_penalty_target&target->type) {
				unsigned char attacker_count; //256 max targets should be a sane max
				attacker_count = unit->counttargeted(target);
				if(attacker_count >= battle_config.agi_penalty_count)
				{
					if (battle_config.agi_penalty_type == 1)
						flee = (flee * (100 - (attacker_count - (battle_config.agi_penalty_count - 1))*battle_config.agi_penalty_num))/100;
					else // assume type 2: absolute reduction
						flee -= (attacker_count - (battle_config.agi_penalty_count - 1))*battle_config.agi_penalty_num;
					if(flee < 1) flee = 1;
				}
			}

			hitrate+= sstatus->hit - flee;
#ifdef RENEWAL
			if( sd ) //in Renewal hit bonus from Vultures Eye is not anymore shown in status window
				hitrate += pc->checkskill(sd,AC_VULTURE);
#endif
			if( skill_id == KO_MUCHANAGE )
				hitrate = (int)((10 - ((float)1 / (status_get_dex(src) + status_get_luk(src))) * 500) * ((float)skill_lv / 2 + 5));

			hitrate = cap_value(hitrate, battle_config.min_hitrate, battle_config.max_hitrate);

			if(rnd()%100 < hitrate)
				i = 1;
		}
		if (!i) {
			md.damage = 0;
			md.dmg_lv=ATK_FLEE;
		}
	}
#ifndef HMAP_ZONE_DAMAGE_CAP_TYPE
	if( target && skill_id ) {
		for(i = 0; i < map->list[target->m].zone->capped_skills_count; i++) {
			if( skill_id == map->list[target->m].zone->capped_skills[i]->nameid && (map->list[target->m].zone->capped_skills[i]->type & target->type) ) {
				if( target->type == BL_MOB && map->list[target->m].zone->capped_skills[i]->subtype != MZS_NONE ) {
					if( (((TBL_MOB*)target)->status.mode&MD_BOSS) && !(map->list[target->m].zone->disabled_skills[i]->subtype&MZS_BOSS) )
						continue;
					if( ((TBL_MOB*)target)->special_state.clone && !(map->list[target->m].zone->disabled_skills[i]->subtype&MZS_CLONE) )
						continue;
				}
				if( md.damage > map->list[target->m].zone->capped_skills[i]->cap )
					md.damage = map->list[target->m].zone->capped_skills[i]->cap;
				if( md.damage2 > map->list[target->m].zone->capped_skills[i]->cap )
					md.damage2 = map->list[target->m].zone->capped_skills[i]->cap;
				break;
			}
		}
	}
#endif
	md.damage = battle->calc_cardfix(BF_MISC, src, target, nk, s_ele, 0, md.damage, 0, md.flag);
	if(skill_id){
		uint16 rskill;/* redirect skill id */
		switch(skill_id){
			case GN_HELLS_PLANT_ATK:
				rskill = GN_HELLS_PLANT;
				break;
			default:
				rskill = skill_id;
		}
		if (sd && (i = pc->skillatk_bonus(sd, rskill)))
			md.damage += md.damage*i/100;
	}
	if( (i = battle->adjust_skill_damage(src->m,skill_id)) )
		md.damage = md.damage * i / 100;

	if(md.damage < 0)
		md.damage = 0;
	else if(md.damage && tstatus->mode&MD_PLANT){
		switch(skill_id){
			case HT_LANDMINE:
			case MA_LANDMINE:
			case HT_BLASTMINE:
			case HT_CLAYMORETRAP:
			case RA_CLUSTERBOMB:
#ifdef RENEWAL
				break;
#endif
			default:
				md.damage = 1;
		}
	}

	if(!(nk&NK_NO_ELEFIX))
		md.damage=battle->attr_fix(src, target, md.damage, s_ele, tstatus->def_ele, tstatus->ele_lv);

	md.damage=battle->calc_damage(src,target,&md,md.damage,skill_id,skill_lv);
	if( map_flag_gvg2(target->m) )
		md.damage=battle->calc_gvg_damage(src,target,md.damage,md.div_,skill_id,skill_lv,md.flag);
	else if( map->list[target->m].flag.battleground )
		md.damage=battle->calc_bg_damage(src,target,md.damage,md.div_,skill_id,skill_lv,md.flag);

	switch( skill_id ) {
		case RA_FIRINGTRAP:
		case RA_ICEBOUNDTRAP:
			if( md.damage == 1 ) break;
		case RA_CLUSTERBOMB:
			{
				struct Damage wd;
				wd = battle->calc_weapon_attack(src,target,skill_id,skill_lv,mflag);
				md.damage += wd.damage;
			}
			break;
		case NJ_ZENYNAGE:
			if( sd ) {
				if ( md.damage > sd->status.zeny )
					md.damage = sd->status.zeny;
				pc->payzeny(sd, (int)cap_value(md.damage,INT_MIN,INT_MAX),LOG_TYPE_STEAL,NULL);
			}
		break;
	}

	return md;
}

void battle_calc_misc_attack_unknown(struct block_list *src, struct block_list *target, uint16 *skill_id, uint16 *skill_lv, int *mflag, struct Damage *md) {
}

/*==========================================
 * battle_calc_weapon_attack (by Skotlex)
 *------------------------------------------*/
struct Damage battle_calc_weapon_attack(struct block_list *src,struct block_list *target,uint16 skill_id,uint16 skill_lv,int wflag)
{
	unsigned int skillratio = 100; //Skill dmg modifiers.
	short temp=0;
	short s_ele, s_ele_;
	int i, nk;
	bool n_ele = false; // non-elemental

	struct map_session_data *sd, *tsd;
	struct Damage wd;
	struct status_change *sc = status->get_sc(src);
	struct status_change *tsc = status->get_sc(target);
	struct status_data *sstatus = status->get_status_data(src);
	struct status_data *tstatus = status->get_status_data(target);
	struct {
		unsigned hit : 1;    ///< the attack Hit? (not a miss)
		unsigned cri : 1;    ///< Critical hit
		unsigned idef : 1;   ///< Ignore defense
		unsigned idef2 : 1;  ///< Ignore defense (left weapon)
		unsigned pdef : 2;   ///< Pierces defense (Investigate/Ice Pick)
		unsigned pdef2 : 2;  ///< 1: Use def+def2/100, 2: Use def+def2/50
		unsigned infdef : 1; ///< Infinite defense (plants)
		unsigned arrow : 1;  ///< Attack is arrow-based
		unsigned rh : 1;     ///< Attack considers right hand (wd.damage)
		unsigned lh : 1;     ///< Attack considers left hand (wd.damage2)
		unsigned weapon : 1; ///< It's a weapon attack (consider VVS, and all that)
#ifdef RENEWAL
		unsigned tdef : 1;   ///< Total defense reduction
#endif
	} flag;

	memset(&wd,0,sizeof(wd));
	memset(&flag,0,sizeof(flag));

	nullpo_retr(wd, src);
	nullpo_retr(wd, target);

	//Initial flag
	flag.rh=1;
	flag.weapon=1;
	flag.infdef=(tstatus->mode&MD_PLANT && skill_id != RA_CLUSTERBOMB
#ifdef RENEWAL
		&& skill_id != HT_FREEZINGTRAP
#endif
		?1:0);
	if( !flag.infdef && target->type == BL_SKILL && ((TBL_SKILL*)target)->group->unit_id == UNT_REVERBERATION )
		flag.infdef = 1; // Reverberation takes 1 damage

	//Initial Values
	wd.type=0; //Normal attack
	wd.div_=skill_id?skill->get_num(skill_id,skill_lv):1;
	wd.amotion=(skill_id && skill->get_inf(skill_id)&INF_GROUND_SKILL)?0:sstatus->amotion; //Amotion should be 0 for ground skills.
	if(skill_id == KN_AUTOCOUNTER)
		wd.amotion >>= 1;
	wd.dmotion=tstatus->dmotion;
	wd.blewcount=skill->get_blewcount(skill_id,skill_lv);
	wd.flag = BF_WEAPON; //Initial Flag
	wd.flag |= (skill_id||wflag)?BF_SKILL:BF_NORMAL; // Baphomet card's splash damage is counted as a skill. [Inkfish]
	wd.dmg_lv=ATK_DEF; //This assumption simplifies the assignation later
	nk = skill->get_nk(skill_id);
	if( !skill_id && wflag ) //If flag, this is splash damage from Baphomet Card and it always hits.
		nk |= NK_NO_CARDFIX_ATK|NK_IGNORE_FLEE;
	flag.hit = (nk&NK_IGNORE_FLEE) ? 1 : 0;
	flag.idef = flag.idef2 = (nk&NK_IGNORE_DEF) ? 1 : 0;
#ifdef RENEWAL
	flag.tdef = 0;
#endif
	if (sc && !sc->count)
		sc = NULL; //Skip checking as there are no status changes active.
	if (tsc && !tsc->count)
		tsc = NULL; //Skip checking as there are no status changes active.

	sd = BL_CAST(BL_PC, src);
	tsd = BL_CAST(BL_PC, target);

	if(sd)
		wd.blewcount += battle->blewcount_bonus(sd, skill_id);

	//Set miscellaneous data that needs be filled regardless of hit/miss
	if(
		(sd && sd->state.arrow_atk) ||
		(!sd && ((skill_id && skill->get_ammotype(skill_id)) || sstatus->rhw.range>3))
	)
		flag.arrow = 1;

	if(skill_id) {
		wd.flag |= battle->range_type(src, target, skill_id, skill_lv);
		switch(skill_id) {
			case MO_FINGEROFFENSIVE:
				if(sd) {
					if (battle_config.finger_offensive_type)
						wd.div_ = 1;
					else
						wd.div_ = sd->spiritball_old;
				}
				break;
			case HT_PHANTASMIC:
				//Since these do not consume ammo, they need to be explicitly set as arrow attacks.
				flag.arrow = 1;
				break;
#ifndef RENEWAL
			case PA_SHIELDCHAIN:
			case CR_SHIELDBOOMERANG:
#endif
			case LG_SHIELDPRESS:
			case LG_EARTHDRIVE:
				flag.weapon = 0;
				break;

			case KN_PIERCE:
			case ML_PIERCE:
				wd.div_= (wd.div_>0?tstatus->size+1:-(tstatus->size+1));
				break;

			case TF_DOUBLE: //For NPC used skill.
			case GS_CHAINACTION:
				wd.type = 0x08;
				break;

			case GS_GROUNDDRIFT:
			case KN_SPEARSTAB:
			case KN_BOWLINGBASH:
			case MS_BOWLINGBASH:
			case MO_BALKYOUNG:
			case TK_TURNKICK:
				wd.blewcount=0;
				break;

			case KN_AUTOCOUNTER:
				wd.flag=(wd.flag&~BF_SKILLMASK)|BF_NORMAL;
				break;

			case NPC_CRITICALSLASH:
			case LG_PINPOINTATTACK:
				flag.cri = 1; //Always critical skill.
				break;

			case LK_SPIRALPIERCE:
				if (!sd) wd.flag=(wd.flag&~(BF_RANGEMASK|BF_WEAPONMASK))|BF_LONG|BF_MISC;
				break;

			//When in banding, the number of hits is equal to the number of Royal Guards in banding.
			case LG_HESPERUSLIT:
				if( sc && sc->data[SC_BANDING] && sc->data[SC_BANDING]->val2 > 3 )
					wd.div_ = sc->data[SC_BANDING]->val2;
				break;

			case MO_INVESTIGATE:
				flag.pdef = flag.pdef2 = 2;
				break;

			case RA_AIMEDBOLT:
				if( tsc && (tsc->data[SC_WUGBITE] || tsc->data[SC_ANKLESNARE] || tsc->data[SC_ELECTRICSHOCKER]) )
					wd.div_ = tstatus->size + 2 + ( (rnd()%100 < 50-tstatus->size*10) ? 1 : 0 );
				break;
#ifdef RENEWAL
			case NJ_KUNAI:
			case HW_MAGICCRASHER:
			case NJ_SYURIKEN:
			case GS_MAGICALBULLET:
			case KO_BAKURETSU:
				flag.tdef = 1;
				break;
#endif
		}
	} else //Range for normal attacks.
		wd.flag |= flag.arrow?BF_LONG:BF_SHORT;
	if ((!skill_id || skill_id == PA_SACRIFICE) && tstatus->flee2 && rnd()%1000 < tstatus->flee2) {
		//Check for Lucky Dodge
		wd.type=0x0b;
		wd.dmg_lv=ATK_LUCKY;
		if (wd.div_ < 0) wd.div_*=-1;
		return wd;
	}

	s_ele = s_ele_ = skill->get_ele(skill_id, skill_lv);
	if( !skill_id || s_ele == -1 )
	{ //Take weapon's element
		s_ele = sstatus->rhw.ele;
		s_ele_ = sstatus->lhw.ele;
		if (sd && sd->charm_type != CHARM_TYPE_NONE && sd->charm_count >= MAX_SPIRITCHARM) {
			//Summoning 10 spiritcharm will endow your weapon.
			s_ele = s_ele_ = sd->charm_type;
		}
		if( flag.arrow && sd && sd->bonus.arrow_ele )
			s_ele = sd->bonus.arrow_ele;
		if( battle_config.attack_attr_none&src->type )
			n_ele = true; //Weapon's element is "not elemental"
	}
	else if( s_ele == -2 ) //Use enchantment's element
		s_ele = s_ele_ = status_get_attack_sc_element(src,sc);
	else if( s_ele == -3 ) //Use random element
		s_ele = s_ele_ = rnd()%ELE_MAX;
	switch( skill_id )
	{
		case GS_GROUNDDRIFT:
			s_ele = s_ele_ = wflag; //element comes in flag.
			break;
		case LK_SPIRALPIERCE:
			if (!sd) n_ele = false; //forced neutral for monsters
			break;
		case LG_HESPERUSLIT:
			if ( sc && sc->data[SC_BANDING] && sc->data[SC_BANDING]->val2 == 5 )
				s_ele = ELE_HOLY; // Banding with 5 RGs: change atk element to Holy.
		break;
	}

	if (!(nk & NK_NO_ELEFIX) && !n_ele)
	    if (src->type == BL_HOM)
		n_ele = true; //skill is "not elemental"
	if (sc && sc->data[SC_GOLDENE_FERSE] && ((!skill_id && (rnd() % 100 < sc->data[SC_GOLDENE_FERSE]->val4)) || skill_id == MH_STAHL_HORN)) {
	    s_ele = s_ele_ = ELE_HOLY;
	    n_ele = false;
	}

	if(!skill_id) {
		//Skills ALWAYS use ONLY your right-hand weapon (tested on Aegis 10.2)
		if (sd && sd->weapontype1 == 0 && sd->weapontype2 > 0)
		{
			flag.rh=0;
			flag.lh=1;
		}
		if (sstatus->lhw.atk)
			flag.lh=1;
	}

	if (sd && !skill_id) {
		//Check for double attack.
		if (( (skill_lv=pc->checkskill(sd,TF_DOUBLE)) > 0 && sd->weapontype1 == W_DAGGER )
		 || ( sd->bonus.double_rate > 0 && sd->weapontype1 != W_FIST ) //Will fail bare-handed
		 || ( sc && sc->data[SC_KAGEMUSYA] && sd->weapontype1 != W_FIST ) // Need confirmation
		) {
			//Success chance is not added, the higher one is used [Skotlex]
			if( rnd()%100 < ( 5*skill_lv > sd->bonus.double_rate ? 5*skill_lv : sc && sc->data[SC_KAGEMUSYA]?sc->data[SC_KAGEMUSYA]->val1*3:sd->bonus.double_rate ) )
			{
				wd.div_ = skill->get_num(TF_DOUBLE,skill_lv?skill_lv:1);
				wd.type = 0x08;
			}
		}
		else if( sd->weapontype1 == W_REVOLVER && (skill_lv = pc->checkskill(sd,GS_CHAINACTION)) > 0 && rnd()%100 < 5*skill_lv )
		{
			wd.div_ = skill->get_num(GS_CHAINACTION,skill_lv);
			wd.type = 0x08;
		}
		else if(sc && sc->data[SC_FEARBREEZE] && sd->weapontype1==W_BOW
			&& (i = sd->equip_index[EQI_AMMO]) >= 0 && sd->inventory_data[i] && sd->status.inventory[i].amount > 1){
				int chance = rnd()%100;
				switch(sc->data[SC_FEARBREEZE]->val1){
					case 5:
						if( chance < 3){// 3 % chance to attack 5 times.
							wd.div_ = 5;
							break;
						}
					case 4:
						if( chance < 7){// 6 % chance to attack 4 times.
							wd.div_ = 4;
							break;
						}
					case 3:
						if( chance < 10){// 9 % chance to attack 3 times.
							wd.div_ = 3;
							break;
						}
					case 2:
					case 1:
						if( chance < 13){// 12 % chance to attack 2 times.
							wd.div_ = 2;
							break;
						}
				}
				if ( wd.div_ > 1 ) {
					wd.div_ = min(wd.div_, sd->status.inventory[i].amount);
					sc->data[SC_FEARBREEZE]->val4 = wd.div_ - 1;
					wd.type = 0x08;
				}
		}
	}

	//Check for critical
	if( !flag.cri && !(wd.type&0x08) && sstatus->cri &&
		(!skill_id ||
		skill_id == KN_AUTOCOUNTER ||
		skill_id == SN_SHARPSHOOTING || skill_id == MA_SHARPSHOOTING ||
		skill_id == NJ_KIRIKAGE))
	{
		short cri = sstatus->cri;
		if (sd)
		{
			cri+= sd->critaddrace[tstatus->race];
			if(flag.arrow)
				cri += sd->bonus.arrow_cri;
		}
		if( sc && sc->data[SC_CAMOUFLAGE] )
			cri += 10 * (10-sc->data[SC_CAMOUFLAGE]->val4);
#ifndef RENEWAL
		//The official equation is *2, but that only applies when sd's do critical.
		//Therefore, we use the old value 3 on cases when an sd gets attacked by a mob
		cri -= tstatus->luk*(!sd&&tsd?3:2);
#else
		cri -= status->get_lv(target) / 15 + 2 * status_get_luk(target);
#endif

		if( tsc && tsc->data[SC_SLEEP] ) {
			cri <<= 1;
		}
		switch (skill_id) {
			case 0:
				if(!(sc && sc->data[SC_AUTOCOUNTER]))
					break;
				status_change_end(src, SC_AUTOCOUNTER, INVALID_TIMER);
			case KN_AUTOCOUNTER:
				if(battle_config.auto_counter_type &&
					(battle_config.auto_counter_type&src->type))
					flag.cri = 1;
				else
					cri <<= 1;
				break;
			case SN_SHARPSHOOTING:
			case MA_SHARPSHOOTING:
				cri += 200;
				break;
			case NJ_KIRIKAGE:
				cri += 250 + 50*skill_lv;
				break;
		}
		if(tsd && tsd->bonus.critical_def)
			cri = cri * ( 100 - tsd->bonus.critical_def ) / 100;
		if (rnd()%1000 < cri)
			flag.cri = 1;
	}
	if (flag.cri) {
		wd.type = 0x0a;
#ifndef RENEWAL
		flag.idef = flag.idef2 =
#endif
		flag.hit = 1;
	} else {
		//Check for Perfect Hit
		if(sd && sd->bonus.perfect_hit > 0 && rnd()%100 < sd->bonus.perfect_hit)
			flag.hit = 1;
		if (sc && sc->data[SC_FUSION]) {
			flag.hit = 1; //SG_FUSION always hit [Komurka]
			flag.idef = flag.idef2 = 1; //def ignore [Komurka]
		}
		if( !flag.hit )
			switch(skill_id)
			{
				case AS_SPLASHER:
					if( !wflag ) // Always hits the one exploding.
						flag.hit = 1;
					break;
				case CR_SHIELDBOOMERANG:
					if( sc && sc->data[SC_SOULLINK] && sc->data[SC_SOULLINK]->val2 == SL_CRUSADER )
						flag.hit = 1;
					break;
			}
		if (tsc && !flag.hit && tsc->opt1 && tsc->opt1 != OPT1_STONEWAIT && tsc->opt1 != OPT1_BURNING)
			flag.hit = 1;
	}

	if (!flag.hit) {
		//Hit/Flee calculation
		short flee = tstatus->flee;
#ifdef RENEWAL
		short hitrate = 0; //Default hitrate
#else
		short hitrate = 80; //Default hitrate
#endif

		if(battle_config.agi_penalty_type && battle_config.agi_penalty_target&target->type) {
			unsigned char attacker_count; //256 max targets should be a sane max
			attacker_count = unit->counttargeted(target);
			if(attacker_count >= battle_config.agi_penalty_count) {
				if (battle_config.agi_penalty_type == 1)
					flee = (flee * (100 - (attacker_count - (battle_config.agi_penalty_count - 1))*battle_config.agi_penalty_num))/100;
				else //asume type 2: absolute reduction
					flee -= (attacker_count - (battle_config.agi_penalty_count - 1))*battle_config.agi_penalty_num;
				if(flee < 1) flee = 1;
			}
		}

		hitrate+= sstatus->hit - flee;

		if(wd.flag&BF_LONG && !skill_id && //Fogwall's hit penalty is only for normal ranged attacks.
			tsc && tsc->data[SC_FOGWALL])
			hitrate -= 50;

		if(sd && flag.arrow)
			hitrate += sd->bonus.arrow_hit;
#ifdef RENEWAL
		if( sd ) //in Renewal hit bonus from Vultures Eye is not anymore shown in status window
			hitrate += pc->checkskill(sd,AC_VULTURE);
#endif
		switch(skill_id) {
			//Hit skill modifiers
			//It is proven that bonus is applied on final hitrate, not hit.
			case SM_BASH:
			case MS_BASH:
				hitrate += hitrate * 5 * skill_lv / 100;
				break;
			case MS_MAGNUM:
			case SM_MAGNUM:
				hitrate += hitrate * 10 * skill_lv / 100;
				break;
			case KN_AUTOCOUNTER:
			case PA_SHIELDCHAIN:
			case NPC_WATERATTACK:
			case NPC_GROUNDATTACK:
			case NPC_FIREATTACK:
			case NPC_WINDATTACK:
			case NPC_POISONATTACK:
			case NPC_HOLYATTACK:
			case NPC_DARKNESSATTACK:
			case NPC_UNDEADATTACK:
			case NPC_TELEKINESISATTACK:
			case NPC_BLEEDING:
				hitrate += hitrate * 20 / 100;
				break;
			case KN_PIERCE:
			case ML_PIERCE:
				hitrate += hitrate * 5 * skill_lv / 100;
				break;
			case AS_SONICBLOW:
				if(sd && pc->checkskill(sd,AS_SONICACCEL)>0)
					hitrate += hitrate * 50 / 100;
				break;
			case MC_CARTREVOLUTION:
			case GN_CART_TORNADO:
			case GN_CARTCANNON:
				if( sd && pc->checkskill(sd, GN_REMODELING_CART) )
					hitrate += pc->checkskill(sd, GN_REMODELING_CART) * 4;
				break;
			case GC_VENOMPRESSURE:
				hitrate += 10 + 4 * skill_lv;
				break;
			case SC_FATALMENACE:
				hitrate -= 35 - 5 * skill_lv;
				break;
			case LG_BANISHINGPOINT:
				hitrate += 3 * skill_lv;
				break;
		}

		if( sd ) {
			// Weaponry Research hidden bonus
			if ((temp = pc->checkskill(sd,BS_WEAPONRESEARCH)) > 0)
				hitrate += hitrate * ( 2 * temp ) / 100;

			if( (sd->status.weapon == W_1HSWORD || sd->status.weapon == W_DAGGER) &&
			   (temp = pc->checkskill(sd, GN_TRAINING_SWORD))>0 )
				hitrate += 3 * temp;
		}

		hitrate = cap_value(hitrate, battle_config.min_hitrate, battle_config.max_hitrate);
#ifdef RENEWAL
		if( !sd )
			hitrate = cap_value(hitrate, 5, 95);
#endif
		if(rnd()%100 >= hitrate){
			wd.dmg_lv = ATK_FLEE;
			if (skill_id == SR_GATEOFHELL)
				flag.hit = 1;/* will hit with the special */
		}
		else
			flag.hit = 1;
	} //End hit/miss calculation

	if (flag.hit && !flag.infdef) { //No need to do the math for plants
		//Hitting attack

//Assuming that 99% of the cases we will not need to check for the flag.rh... we don't.
//ATK_RATE scales the damage. 100 = no change. 50 is halved, 200 is doubled, etc
#define ATK_RATE( a ) do { int64 temp__ = (a); wd.damage= wd.damage*temp__/100 ; if(flag.lh) wd.damage2= wd.damage2*temp__/100; } while(0)
#define ATK_RATE2( a , b ) do { wd.damage= wd.damage*(a)/100 ; if(flag.lh) wd.damage2= wd.damage2*(b)/100; } while(0)
#define ATK_RATER(a) ( wd.damage = wd.damage*(a)/100 )
#define ATK_RATEL(a) ( wd.damage2 = wd.damage2*(a)/100 )
//Adds dmg%. 100 = +100% (double) damage. 10 = +10% damage
#define ATK_ADDRATE( a ) do { int64 temp__ = (a); wd.damage+= wd.damage*temp__/100; if(flag.lh) wd.damage2+= wd.damage2*temp__/100; } while(0)
#define ATK_ADDRATE2( a , b ) do { wd.damage+= wd.damage*(a)/100 ; if(flag.lh) wd.damage2+= wd.damage2*(b)/100; } while(0)
//Adds an absolute value to damage. 100 = +100 damage
#define ATK_ADD( a ) do { int64 temp__ = (a); wd.damage += temp__; if (flag.lh) wd.damage2 += temp__; } while(0)
#define ATK_ADD2( a , b ) do { wd.damage += (a); if (flag.lh) wd.damage2 += (b); } while(0)
#ifdef RENEWAL
#define GET_NORMAL_ATTACK( f ) ( wd.damage = battle->calc_base_damage(src, target, skill_id, skill_lv, nk, n_ele, s_ele, s_ele_, EQI_HAND_R, (f), wd.flag) )
#define GET_NORMAL_ATTACK2( f ) ( wd.damage2 = battle->calc_base_damage(src, target, skill_id, skill_lv, nk, n_ele, s_ele, s_ele_, EQI_HAND_L, (f), wd.flag) )
#endif
		switch (skill_id) {
			//Calc base damage according to skill
			case PA_SACRIFICE:
				wd.damage = sstatus->max_hp* 9/100;
				wd.damage2 = 0;
#ifdef RENEWAL
				wd.damage = battle->calc_elefix(src, target, skill_id, skill_lv, wd.damage, nk, n_ele, s_ele, s_ele_, false, wd.flag); // temporary [malufett]
#endif
				break;
			case NJ_ISSEN: // [malufett]
#ifndef RENEWAL
				wd.damage = 40*sstatus->str +skill_lv*(sstatus->hp/10 + 35);
				wd.damage2 = 0;
#else
				{
					short totaldef = status->get_total_def(target);
					i = 0;
					GET_NORMAL_ATTACK( (sc && sc->data[SC_MAXIMIZEPOWER]?1:0)|(sc && sc->data[SC_WEAPONPERFECT]?8:0) );
					if( sc && sc->data[SC_NJ_BUNSINJYUTSU] && (i=sc->data[SC_NJ_BUNSINJYUTSU]->val2) > 0 )
						wd.div_ = ~( i++ + 2 ) + 1;
					if( wd.damage ){
						wd.damage *= sstatus->hp * skill_lv;
						wd.damage = wd.damage / sstatus->max_hp + sstatus->hp + i * (wd.damage / sstatus->max_hp + sstatus->hp) / 5;
					}
					ATK_ADD(-totaldef);
					if( is_boss(target) )
						ATK_RATE(50);
				}
				break;
			case NJ_SYURIKEN: // [malufett]
				GET_NORMAL_ATTACK( (sc && sc->data[SC_MAXIMIZEPOWER]?1:0)|(sc && sc->data[SC_WEAPONPERFECT]?8:0) );
				wd.damage += battle->calc_masteryfix(src, target, skill_id, skill_lv, 4 * skill_lv + (sd ? sd->bonus.arrow_atk : 0), wd.div_, 0, flag.weapon);
				break;
			case MO_EXTREMITYFIST: // [malufett]
				{
					short totaldef = status->get_total_def(target);
					GET_NORMAL_ATTACK( (sc && sc->data[SC_MAXIMIZEPOWER]?1:0)|8 );
					if( wd.damage ){
						wd.damage = (250 + 150 * skill_lv) + (10 * (status_get_sp(src)+1) * wd.damage / 100) + (8 * wd.damage);
						ATK_ADD(-totaldef);
					}
				}
#endif
				break;
#ifndef RENEWAL
			case LK_SPIRALPIERCE:
			case ML_SPIRALPIERCE:
				if (sd) {
					short index = sd->equip_index[EQI_HAND_R];

					if (index >= 0 &&
						sd->inventory_data[index] &&
						sd->inventory_data[index]->type == IT_WEAPON)
						wd.damage = sd->inventory_data[index]->weight*8/100; //80% of weight
					ATK_ADDRATE(50*skill_lv); //Skill modifier applies to weight only.
				} else {
					wd.damage = battle->calc_base_damage2(sstatus, &sstatus->rhw, sc, tstatus->size, sd, 0); //Monsters have no weight and use ATK instead
				}
				i = sstatus->str/10;
				i*=i;
				ATK_ADD(i); //Add str bonus.
				switch (tstatus->size) { //Size-fix. Is this modified by weapon perfection?
					case SZ_SMALL: //Small: 125%
						ATK_RATE(125);
						break;
					//case SZ_MEDIUM: //Medium: 100%
					case SZ_BIG: //Large: 75%
						ATK_RATE(75);
						break;
				}
				break;
#endif
			case CR_SHIELDBOOMERANG:
			case PA_SHIELDCHAIN:
				wd.damage = sstatus->batk;
				if (sd) {
					int damagevalue = 0;
					short index = sd->equip_index[EQI_HAND_L];

					if( index >= 0 && sd->inventory_data[index] && sd->inventory_data[index]->type == IT_ARMOR )
						damagevalue = sd->inventory_data[index]->weight/10;
					ATK_ADD(damagevalue);
				} else
					ATK_ADD(sstatus->rhw.atk2); //Else use Atk2
				break;
			case HFLI_SBR44: //[orn]
				if(src->type == BL_HOM) {
					wd.damage = ((TBL_HOM*)src)->homunculus.intimacy ;
					break;
				}
			default:
			{
				i = (flag.cri
#ifdef RENEWAL
					|| (sc && sc->data[SC_MAXIMIZEPOWER])
#endif
					?1:0)|
					(flag.arrow?2:0)|
#ifndef RENEWAL
					(skill_id == HW_MAGICCRASHER?4:0)|
					(skill_id == MO_EXTREMITYFIST?8:0)|
#endif
					(!skill_id && sc && sc->data[SC_HLIF_CHANGE]?4:0)|
					(sc && sc->data[SC_WEAPONPERFECT]?8:0);
				if (flag.arrow && sd)
				switch(sd->status.weapon) {
					case W_BOW:
					case W_REVOLVER:
					case W_GATLING:
					case W_SHOTGUN:
					case W_GRENADE:
						break;
					default:
						i |= 16; // for ex. shuriken must not be influenced by DEX
				}
#ifdef RENEWAL
				GET_NORMAL_ATTACK( i );
				wd.damage = battle->calc_masteryfix(src, target, skill_id, skill_lv, wd.damage, wd.div_, 0, flag.weapon);
				if (flag.lh){
					GET_NORMAL_ATTACK2( i );
					wd.damage2 = battle->calc_masteryfix(src, target, skill_id, skill_lv, wd.damage2, wd.div_, 1, flag.weapon);
				}
#else
				wd.damage = battle->calc_base_damage2(sstatus, &sstatus->rhw, sc, tstatus->size, sd, i);
				if (flag.lh)
					wd.damage2 = battle->calc_base_damage2(sstatus, &sstatus->lhw, sc, tstatus->size, sd, i);
#endif
				if (nk&NK_SPLASHSPLIT){ // Divide ATK among targets
					if(wflag>0)
						wd.damage/= wflag;
					else
						ShowError("0 enemies targeted by %d:%s, divide per 0 avoided!\n", skill_id, skill->get_name(skill_id));
				}

				//Add any bonuses that modify the base baseatk+watk (pre-skills)
				if(sd) {
					if (sd->bonus.atk_rate)
						ATK_ADDRATE(sd->bonus.atk_rate);
					if(flag.cri && sd->bonus.crit_atk_rate)
						ATK_ADDRATE(sd->bonus.crit_atk_rate);
					if(flag.cri && sc && sc->data[SC_MTF_CRIDAMAGE])
						ATK_ADDRATE(25);// temporary it should be 'bonus.crit_atk_rate'
#ifndef RENEWAL

					if(sd->status.party_id && (temp=pc->checkskill(sd,TK_POWER)) > 0){
						if( (i = party->foreachsamemap(party->sub_count, sd, 0)) > 1 ) // exclude the player himself [Inkfish]
							ATK_ADDRATE(2*temp*i);
					}
#endif
				}
				break;
			} //End default case
		} //End switch(skill_id)

		if( sc && skill_id != PA_SACRIFICE && sc->data[SC_UNLIMIT] && (wd.flag&(BF_LONG|BF_MAGIC)) == BF_LONG) {
			switch(skill_id) {
				case RA_WUGDASH:
				case RA_WUGSTRIKE:
				case RA_WUGBITE:
					break;
				default:
					ATK_ADDRATE( 50 * sc->data[SC_UNLIMIT]->val1 );
			}
		}

		if ( sc && !skill_id && sc->data[SC_EXEEDBREAK] ) {
			ATK_ADDRATE(sc->data[SC_EXEEDBREAK]->val1);
			status_change_end(src, SC_EXEEDBREAK, INVALID_TIMER);
		}

		switch(skill_id){
			case SR_GATEOFHELL:
				if (wd.dmg_lv != ATK_FLEE)
					ATK_RATE(battle->calc_skillratio(BF_WEAPON, src, target, skill_id, skill_lv, skillratio, wflag));
				else
					wd.dmg_lv = ATK_DEF;
				break;
				
			case KO_BAKURETSU:
			{
#ifdef RENEWAL
				GET_NORMAL_ATTACK((sc && sc->data[SC_MAXIMIZEPOWER] ? 1 : 0) | (sc && sc->data[SC_WEAPONPERFECT] ? 8 : 0));
#endif
				skillratio = skill_lv * (50 + status_get_dex(src) / 4);
				skillratio = (int)(skillratio * (sd ? pc->checkskill(sd, NJ_TOBIDOUGU) : 10) * 40.f / 100.0f * status->get_lv(src) / 120);
				ATK_RATE(skillratio + 10 * (sd ? sd->status.job_level : 0));
			}
				break;

	#ifdef RENEWAL
			case NJ_TATAMIGAESHI:
					ATK_RATE(200);
				/* Fall through */
			case LK_SPIRALPIERCE:
			case ML_SPIRALPIERCE: // [malufett]
				if( skill_id != NJ_TATAMIGAESHI ){
					short index = sd?sd->equip_index[EQI_HAND_R]:0;
					GET_NORMAL_ATTACK( (sc && sc->data[SC_MAXIMIZEPOWER]?1:0)|(sc && sc->data[SC_WEAPONPERFECT]?8:0) );
					wd.damage = wd.damage * 70 / 100;
					//n_ele = true; // FIXME: This is has no effect if it's after GET_NORMAL_ATTACK (was this intended, or was it supposed to be put above?)

					if (sd && index >= 0 &&
						sd->inventory_data[index] &&
						sd->inventory_data[index]->type == IT_WEAPON)
						ATK_ADD(sd->inventory_data[index]->weight * 7 / 100);

					switch (tstatus->size) {
						case SZ_SMALL: //Small: 115%
							ATK_RATE(115);
							break;
						case SZ_BIG: //Large: 85%
							ATK_RATE(85);
					}
					wd.damage = battle->calc_masteryfix(src, target, skill_id, skill_lv, wd.damage, wd.div_, 0, flag.weapon);
				}
				/* Fall through */
	#endif
			default:
				ATK_RATE(battle->calc_skillratio(BF_WEAPON, src, target, skill_id, skill_lv, skillratio, wflag));
		}

			//Constant/misc additions from skills
		switch (skill_id) {
#ifdef RENEWAL
			case HW_MAGICCRASHER:
				ATK_ADD(battle->calc_magic_attack(src, target, skill_id, skill_lv, wflag).damage / 5);
				break;
#else
			case MO_EXTREMITYFIST:
				ATK_ADD(250 + 150*skill_lv);
				break;
#endif
			case TK_DOWNKICK:
			case TK_STORMKICK:
			case TK_TURNKICK:
			case TK_COUNTER:
			case TK_JUMPKICK:
				//TK_RUN kick damage bonus.
				if(sd && sd->weapontype1 == W_FIST && sd->weapontype2 == W_FIST)
					ATK_ADD(10*pc->checkskill(sd, TK_RUN));
				break;
			case GS_MAGICALBULLET:
#ifndef RENEWAL
				ATK_ADD( status->get_matk(src, 2) );
#else
				ATK_ADD( battle->calc_magic_attack(src, target, skill_id, skill_lv, wflag).damage );
#endif
#ifndef RENEWAL
			case NJ_SYURIKEN:
				ATK_ADD(4*skill_lv);
#endif
				break;
			case GC_COUNTERSLASH:
				ATK_ADD( status_get_agi(src) * 2 + (sd?sd->status.job_level:0) * 4 );
				break;
			case RA_WUGDASH:
				if( sc && sc->data[SC_DANCE_WITH_WUG] )
					ATK_ADD(2 * sc->data[SC_DANCE_WITH_WUG]->val1 * (2 + battle->calc_chorusbonus(sd)));
				break;
			case SR_TIGERCANNON:
				ATK_ADD( skill_lv * 240 + status->get_lv(target) * 40 );
				if( sc && sc->data[SC_COMBOATTACK]
					&& sc->data[SC_COMBOATTACK]->val1 == SR_FALLENEMPIRE )
						ATK_ADD( skill_lv * 500 + status->get_lv(target) * 40 );
				break;
			case RA_WUGSTRIKE:
			case RA_WUGBITE:
				if(sd)
					ATK_ADD(30*pc->checkskill(sd, RA_TOOTHOFWUG));
				if( sc && sc->data[SC_DANCE_WITH_WUG] )
					ATK_ADD(2 * sc->data[SC_DANCE_WITH_WUG]->val1 * (2 + battle->calc_chorusbonus(sd)));
				break;
			case LG_SHIELDPRESS:
				if( sd ) {
					int damagevalue = 0;
					short index = sd->equip_index[EQI_HAND_L];
					if( index >= 0 && sd->inventory_data[index] && sd->inventory_data[index]->type == IT_ARMOR )
						damagevalue = sstatus->vit * sd->status.inventory[index].refine;
					ATK_ADD(damagevalue);
				}
				break;
			case SR_GATEOFHELL:
				ATK_ADD (sstatus->max_hp - status_get_hp(src));
				if(sc && sc->data[SC_COMBOATTACK] && sc->data[SC_COMBOATTACK]->val1 == SR_FALLENEMPIRE) {
					ATK_ADD( (sstatus->max_sp * (1 + skill_lv * 2 / 10)) + 40 * status->get_lv(src) );
				} else {
					ATK_ADD( (sstatus->sp * (1 + skill_lv * 2 / 10)) + 10 * status->get_lv(src) );
				}
				break;
			case SR_FALLENEMPIRE:// [(Target Size value + Skill Level - 1) x Caster STR] + [(Target current weight x Caster DEX / 120)]
				ATK_ADD( ((tstatus->size+1)*2 + skill_lv - 1) * sstatus->str);
				if( tsd && tsd->weight ){
					ATK_ADD( (tsd->weight/10) * sstatus->dex / 120 );
				}else{
					ATK_ADD( status->get_lv(target) * 50 ); //mobs
				}
				break;
			case KO_SETSUDAN:
				if( tsc && tsc->data[SC_SOULLINK] ){
					ATK_ADDRATE(200*tsc->data[SC_SOULLINK]->val1);
					status_change_end(target,SC_SOULLINK,INVALID_TIMER);
				}
				break;
			case KO_MAKIBISHI:
				wd.damage = 20 * skill_lv;
				break;
		}
#ifndef RENEWAL
		//Div fix.
		damage_div_fix(wd.damage, wd.div_);
#endif
		//The following are applied on top of current damage and are stackable.
		if ( sc ) {
#ifndef RENEWAL
			if( sc->data[SC_TRUESIGHT] )
				ATK_ADDRATE(2*sc->data[SC_TRUESIGHT]->val1);
#endif
#ifndef RENEWAL_EDP
			if( sc->data[SC_EDP] ){
				switch(skill_id){
					case AS_SPLASHER: // Needs more info
					case ASC_BREAKER:
					case ASC_METEORASSAULT: break;
					default:
						ATK_ADDRATE(sc->data[SC_EDP]->val3);
				}
			}
#endif
			if(sc->data[SC_STYLE_CHANGE]){
				TBL_HOM *hd = BL_CAST(BL_HOM,src);
				if (hd) ATK_ADD(hd->homunculus.spiritball * 3);
			}
		}

		switch (skill_id) {
			case AS_SONICBLOW:
				if (sc && sc->data[SC_SOULLINK] &&
					sc->data[SC_SOULLINK]->val2 == SL_ASSASIN)
					ATK_ADDRATE(map_flag_gvg(src->m)?25:100); //+25% dmg on woe/+100% dmg on nonwoe

				if(sd && pc->checkskill(sd,AS_SONICACCEL)>0)
					ATK_ADDRATE(10);
			break;
			case CR_SHIELDBOOMERANG:
				if(sc && sc->data[SC_SOULLINK] &&
					sc->data[SC_SOULLINK]->val2 == SL_CRUSADER)
					ATK_ADDRATE(100);
				break;
		}
		if( skill_id ){
			uint16 rskill;/* redirect skill id */
			switch(skill_id){
				case AB_DUPLELIGHT_MELEE:
					rskill = AB_DUPLELIGHT;
					break;
				case LG_OVERBRAND_BRANDISH:
				case LG_OVERBRAND_PLUSATK:
					rskill = LG_OVERBRAND;
					break;
				case WM_SEVERE_RAINSTORM_MELEE:
					rskill = WM_SEVERE_RAINSTORM;
					break;
				case WM_REVERBERATION_MELEE:
					rskill = WM_REVERBERATION;
					break;
				case GN_CRAZYWEED_ATK:
					rskill = GN_CRAZYWEED;
					break;
				case GN_SLINGITEM_RANGEMELEEATK:
					rskill = GN_SLINGITEM;
					break;
				case RL_R_TRIP_PLUSATK:
					rskill = RL_R_TRIP;
					break;
				case RL_B_FLICKER_ATK:
					rskill = RL_FLICKER;
					break;
				case RL_GLITTERING_GREED_ATK:
					rskill = RL_GLITTERING_GREED;
					break;
				default:
					rskill = skill_id;
			}
			if( (i = battle->adjust_skill_damage(src->m,rskill)) )
				ATK_RATE(i);
		}

		if( sd ) {
			if (skill_id && (i = pc->skillatk_bonus(sd, skill_id)))
				ATK_ADDRATE(i);
	#ifdef RENEWAL
			if( wd.flag&BF_LONG )
				ATK_ADDRATE(sd->bonus.long_attack_atk_rate);
			if( sc && sc->data[SC_MTF_RANGEATK] )
				ATK_ADDRATE(25);// temporary it should be 'bonus.long_attack_atk_rate'
	#endif
			if( (i=pc->checkskill(sd,AB_EUCHARISTICA)) > 0 &&
				(tstatus->race == RC_DEMON || tstatus->def_ele == ELE_DARK) )
				ATK_ADDRATE(-i);
			if( skill_id != PA_SACRIFICE && skill_id != MO_INVESTIGATE && skill_id != CR_GRANDCROSS && skill_id != NPC_GRANDDARKNESS && skill_id != PA_SHIELDCHAIN && !flag.cri )
			{ //Elemental/Racial adjustments
				if( sd->right_weapon.def_ratio_atk_ele & (1<<tstatus->def_ele) ||
					sd->right_weapon.def_ratio_atk_race & (1<<tstatus->race) ||
					sd->right_weapon.def_ratio_atk_race & (1<<(is_boss(target)?RC_BOSS:RC_NONBOSS))
				)
					flag.pdef = 1;

				if( sd->left_weapon.def_ratio_atk_ele & (1<<tstatus->def_ele) ||
					sd->left_weapon.def_ratio_atk_race & (1<<tstatus->race) ||
					sd->left_weapon.def_ratio_atk_race & (1<<(is_boss(target)?RC_BOSS:RC_NONBOSS))
				)
				{ //Pass effect onto right hand if configured so. [Skotlex]
					if (battle_config.left_cardfix_to_right && flag.rh)
						flag.pdef = 1;
					else
						flag.pdef2 = 1;
				}
			}

			if (skill_id != CR_GRANDCROSS && skill_id != NPC_GRANDDARKNESS) {
				//Ignore Defense?
				if (!flag.idef && (
					sd->right_weapon.ignore_def_ele & (1<<tstatus->def_ele) ||
					sd->right_weapon.ignore_def_race & (1<<tstatus->race) ||
					sd->right_weapon.ignore_def_race & (is_boss(target)?1<<RC_BOSS:1<<RC_NONBOSS)
				))
					flag.idef = 1;

				if (!flag.idef2 && (
					sd->left_weapon.ignore_def_ele & (1<<tstatus->def_ele) ||
					sd->left_weapon.ignore_def_race & (1<<tstatus->race) ||
					sd->left_weapon.ignore_def_race & (is_boss(target)?1<<RC_BOSS:1<<RC_NONBOSS)
				)) {
						if(battle_config.left_cardfix_to_right && flag.rh) //Move effect to right hand. [Skotlex]
							flag.idef = 1;
						else
							flag.idef2 = 1;
				}
			}
		}

		if(!flag.idef || !flag.idef2) { //Defense reduction
			wd.damage = battle->calc_defense(BF_WEAPON, src, target, skill_id, skill_lv, wd.damage,
											 (flag.idef?1:0)|(flag.pdef?2:0)
#ifdef RENEWAL
											 |(flag.tdef?4:0)
#endif
											 , flag.pdef);
			if( wd.damage2 )
				wd.damage2 = battle->calc_defense(BF_WEAPON, src, target, skill_id, skill_lv, wd.damage2,
												  (flag.idef2?1:0)|(flag.pdef2?2:0)
#ifdef RENEWAL
												  |(flag.tdef?4:0)
#endif
												  , flag.pdef2);
		}

#ifdef RENEWAL
		//Div fix.
		damage_div_fix(wd.damage, wd.div_);
#endif
		//Post skill/vit reduction damage increases
		if (sc) {
			//SC skill damages
			if(sc->data[SC_AURABLADE]
#ifndef RENEWAL
					&& skill_id != LK_SPIRALPIERCE && skill_id != ML_SPIRALPIERCE
#endif
			){
				int lv = sc->data[SC_AURABLADE]->val1;
#ifdef RENEWAL
				lv *= ((skill_id == LK_SPIRALPIERCE || skill_id == ML_SPIRALPIERCE)?wd.div_:1); // +100 per hit in lv 5
#endif
				ATK_ADD(20*lv);
			}

			if( !skill_id ) {
				if( sc->data[SC_ENCHANTBLADE] ) {
					//[( ( Skill Lv x 20 ) + 100 ) x ( casterBaseLevel / 150 )] + casterInt
					i = ( sc->data[SC_ENCHANTBLADE]->val1 * 20 + 100 ) * status->get_lv(src) / 150 + status_get_int(src);
					i = i - status->get_total_mdef(target) + status->get_matk(src, 2);
					if( i )
						ATK_ADD(i);
				}
				if( sc->data[SC_GIANTGROWTH] && rnd()%100 < 15 )
					ATK_ADDRATE(200); // Triple Damage
			}
		}
#ifndef RENEWAL
		//Refine bonus
		if( sd && flag.weapon && skill_id != MO_INVESTIGATE && skill_id != MO_EXTREMITYFIST )
		{ // Counts refine bonus multiple times
			if( skill_id == MO_FINGEROFFENSIVE )
			{
				ATK_ADD2(wd.div_*sstatus->rhw.atk2, wd.div_*sstatus->lhw.atk2);
			} else {
				ATK_ADD2(sstatus->rhw.atk2, sstatus->lhw.atk2);
			}
		}
		//Set to min of 1
		if (flag.rh && wd.damage < 1) wd.damage = 1;
		if (flag.lh && wd.damage2 < 1) wd.damage2 = 1;
#else
		if (flag.rh && wd.damage < 1) wd.damage = 0;
		if (flag.lh && wd.damage2 < 1) wd.damage2 = 0;
#endif

#ifndef RENEWAL
		wd.damage = battle->calc_masteryfix(src, target, skill_id, skill_lv, wd.damage, wd.div_, 0, flag.weapon);
		if( flag.lh )
			wd.damage2 = battle->calc_masteryfix(src, target, skill_id, skill_lv, wd.damage2, wd.div_, 1, flag.weapon);
#else
		if( sd && flag.cri )
			ATK_ADDRATE(40);
#endif
	} //Here ends flag.hit section, the rest of the function applies to both hitting and missing attacks
	else if(wd.div_ < 0) //Since the attack missed...
		wd.div_ *= -1;
#ifndef RENEWAL
	if(sd && (temp=pc->checkskill(sd,BS_WEAPONRESEARCH)) > 0)
		ATK_ADD(temp*2);
#endif

#ifndef RENEWAL
	wd.damage = battle->calc_elefix(src, target, skill_id, skill_lv, wd.damage, nk, n_ele, s_ele, s_ele_, false, flag.arrow);
	if( flag.lh )
		wd.damage2 = battle->calc_elefix(src, target, skill_id, skill_lv, wd.damage2, nk, n_ele, s_ele, s_ele_, true, flag.arrow);
#endif

	if(skill_id == CR_GRANDCROSS || skill_id == NPC_GRANDDARKNESS)
		return wd; //Enough, rest is not needed.
#ifndef HMAP_ZONE_DAMAGE_CAP_TYPE
	if( target && skill_id ) {
		for(i = 0; i < map->list[target->m].zone->capped_skills_count; i++) {
			if( skill_id == map->list[target->m].zone->capped_skills[i]->nameid && (map->list[target->m].zone->capped_skills[i]->type & target->type) ) {
				if( target->type == BL_MOB && map->list[target->m].zone->capped_skills[i]->subtype != MZS_NONE ) {
					if( (((TBL_MOB*)target)->status.mode&MD_BOSS) && !(map->list[target->m].zone->disabled_skills[i]->subtype&MZS_BOSS) )
						continue;
					if( ((TBL_MOB*)target)->special_state.clone && !(map->list[target->m].zone->disabled_skills[i]->subtype&MZS_CLONE) )
						continue;
				}
				if( wd.damage > map->list[target->m].zone->capped_skills[i]->cap )
					wd.damage = map->list[target->m].zone->capped_skills[i]->cap;
				if( wd.damage2 > map->list[target->m].zone->capped_skills[i]->cap )
					wd.damage2 = map->list[target->m].zone->capped_skills[i]->cap;
				break;
			}
		}
	}
#endif

#ifndef RENEWAL // Offensive damage increment in renewal is done somewhere else
	if (sd) {
		if (skill_id != CR_SHIELDBOOMERANG) //Only Shield boomerang doesn't takes the Star Crumbs bonus.
			ATK_ADD2(wd.div_*sd->right_weapon.star, wd.div_*sd->left_weapon.star);
		if (skill_id==MO_FINGEROFFENSIVE) { //The finger offensive spheres on moment of attack do count. [Skotlex]
			ATK_ADD(wd.div_*sd->spiritball_old*3);
		} else {
			ATK_ADD(wd.div_*sd->spiritball*3);
		}
		//Card Fix, sd side

		wd.damage = battle->calc_cardfix(BF_WEAPON, src, target, nk, s_ele, s_ele_, wd.damage, 2, wd.flag);
		if( flag.lh )
			wd.damage2 = battle->calc_cardfix(BF_WEAPON, src, target, nk, s_ele, s_ele_, wd.damage2, 3, wd.flag);

		if( skill_id == CR_SHIELDBOOMERANG || skill_id == PA_SHIELDCHAIN )
		{ //Refine bonus applies after cards and elements.
			short index= sd->equip_index[EQI_HAND_L];
			if( index >= 0 && sd->inventory_data[index] && sd->inventory_data[index]->type == IT_ARMOR )
				ATK_ADD(10*sd->status.inventory[index].refine);
		}
	}
#endif
	//Card Fix, tsd side
	if(tsd){ //if player on player then it was already measured above
		wd.damage = battle->calc_cardfix(BF_WEAPON, src, target, nk, s_ele, s_ele_, wd.damage, (flag.lh?1:0), wd.flag);
	}
	if( flag.infdef ) { //Plants receive 1 damage when hit
		short class_ = status->get_class(target);
		if( flag.hit || wd.damage > 0 )
			wd.damage = wd.div_; // In some cases, right hand no need to have a weapon to increase damage
		if( flag.lh && (flag.hit || wd.damage2 > 0) )
			wd.damage2 = wd.div_;
		if( flag.hit && class_ == MOBID_EMPERIUM ) {
			if(wd.damage2 > 0) {
				wd.damage2 = battle->attr_fix(src,target,wd.damage2,s_ele_,tstatus->def_ele, tstatus->ele_lv);
				wd.damage2 = battle->calc_gvg_damage(src,target,wd.damage2,wd.div_,skill_id,skill_lv,wd.flag);
			}
			else if(wd.damage > 0) {
				wd.damage = battle->attr_fix(src,target,wd.damage,s_ele_,tstatus->def_ele, tstatus->ele_lv);
				wd.damage = battle->calc_gvg_damage(src,target,wd.damage,wd.div_,skill_id,skill_lv,wd.flag);
			}
			return wd;
		}
		if( !(battle_config.skill_min_damage&1) )
			//Do not return if you are supposed to deal greater damage to plants than 1. [Skotlex]
			return wd;
	}

	if (sd) {
		if (!flag.rh && flag.lh) {
			//Move lh damage to the rh
			wd.damage = wd.damage2;
			wd.damage2 = 0;
			flag.rh=1;
			flag.lh=0;
		} else if(flag.rh && flag.lh) {
			//Dual-wield
			if (wd.damage) {
				temp = pc->checkskill(sd,AS_RIGHT) * 10;
				if( (sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO  )
					temp = pc->checkskill(sd,KO_RIGHT) * 10 + 20;
				ATK_RATER( 50 + temp );
			}
			if (wd.damage2) {
				temp = pc->checkskill(sd,AS_LEFT) * 10;
				if( (sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO  )
					temp = pc->checkskill(sd,KO_LEFT) * 10 + 20;
				ATK_RATEL( 30 + temp );
			}
#ifdef RENEWAL
			if(wd.damage < 0) wd.damage = 0;
			if(wd.damage2 < 0) wd.damage2 = 0;
#else
			if(wd.damage < 1) wd.damage = 1;
			if(wd.damage2 < 1) wd.damage2 = 1;
#endif
		} else if(sd->status.weapon == W_KATAR && !skill_id) { //Katars (offhand damage only applies to normal attacks, tested on Aegis 10.2)
			temp = pc->checkskill(sd,TF_DOUBLE);
			wd.damage2 = wd.damage * (1 + (temp * 2))/100;

			if(wd.damage && !wd.damage2) {
#ifdef RENEWAL
				wd.damage2 = 0;
#else
				wd.damage2 = 1;
#endif
			}
			flag.lh = 1;
		}
	}

	if(!flag.rh && wd.damage)
		wd.damage=0;

	if(!flag.lh && wd.damage2)
		wd.damage2=0;

	if( sc && sc->data[SC_GLOOMYDAY] ) {
		switch( skill_id ) {
			case KN_BRANDISHSPEAR:
			case LK_SPIRALPIERCE:
			case CR_SHIELDCHARGE:
			case CR_SHIELDBOOMERANG:
			case PA_SHIELDCHAIN:
			case RK_HUNDREDSPEAR:
			case LG_SHIELDPRESS:
				wd.damage += wd.damage * sc->data[SC_GLOOMYDAY]->val2 / 100;
		}
	}

	if( sc ) {
		//SG_FUSION hp penalty [Komurka]
		if (sc->data[SC_FUSION]) {
			int hp= sstatus->max_hp;
			if (sd && tsd) {
				hp = 8*hp/100;
				if ((sstatus->hp * 100) <= (sstatus->max_hp * 20))
					hp = sstatus->hp;
			} else
				hp = 2*hp/100; //2% hp loss per hit
			status_zap(src, hp, 0);
		}
		status_change_end(src,SC_CAMOUFLAGE, INVALID_TIMER);
	}

	switch(skill_id){
		case LG_RAYOFGENESIS:
			{
			struct Damage md = battle->calc_magic_attack(src, target, skill_id, skill_lv, wflag);
			wd.damage += md.damage;
			break;
			}
	}

	if( wd.damage + wd.damage2 ) { //There is a total damage value
		int64 damage = wd.damage + wd.damage2;

		if (!wd.damage2) {
#ifdef RENEWAL
			if (skill_id != ASC_BREAKER)
#endif
				wd.damage = battle->calc_damage(src, target, &wd, wd.damage, skill_id, skill_lv);
			if( map_flag_gvg2(target->m) )
				wd.damage=battle->calc_gvg_damage(src,target,wd.damage,wd.div_,skill_id,skill_lv,wd.flag);
			else if( map->list[target->m].flag.battleground )
				wd.damage=battle->calc_bg_damage(src,target,wd.damage,wd.div_,skill_id,skill_lv,wd.flag);
		}
		else if (!wd.damage) {
#ifdef RENEWAL
			if (skill_id != ASC_BREAKER)
#endif
				wd.damage2 = battle->calc_damage(src, target, &wd, wd.damage2, skill_id, skill_lv);
			if (map_flag_gvg2(target->m))
				wd.damage2 = battle->calc_gvg_damage(src, target, wd.damage2, wd.div_, skill_id, skill_lv, wd.flag);
			else if (map->list[target->m].flag.battleground)
				wd.damage = battle->calc_bg_damage(src, target, wd.damage2, wd.div_, skill_id, skill_lv, wd.flag);
		} else {
#ifdef RENEWAL
			if( skill_id != ASC_BREAKER ){
				wd.damage = battle->calc_damage(src, target, &wd, wd.damage, skill_id, skill_lv);
				wd.damage2 = battle->calc_damage(src, target, &wd, wd.damage2, skill_id, skill_lv);
			}
#else
			int64 d1 = wd.damage + wd.damage2,d2 = wd.damage2;
			wd.damage = battle->calc_damage(src,target,&wd,d1,skill_id,skill_lv);
#endif
			if( map_flag_gvg2(target->m) )
				wd.damage = battle->calc_gvg_damage(src,target,wd.damage,wd.div_,skill_id,skill_lv,wd.flag);
			else if( map->list[target->m].flag.battleground )
				wd.damage = battle->calc_bg_damage(src,target,wd.damage,wd.div_,skill_id,skill_lv,wd.flag);
#ifndef RENEWAL
			wd.damage2 = d2*100/d1 * wd.damage/100;
			if(wd.damage > 1 && wd.damage2 < 1) wd.damage2 = 1;
			wd.damage-=wd.damage2;
#endif
		}


		if( src != target ) { // Don't reflect your own damage (Grand Cross)
			if( wd.dmg_lv == ATK_MISS || wd.dmg_lv == ATK_BLOCK ) {
				int64 prev1 = wd.damage, prev2 = wd.damage2;

				wd.damage = damage;
				wd.damage2 = 0;

				battle->reflect_damage(target, src, &wd, skill_id);

				wd.damage = prev1;
				wd.damage2 = prev2;

			} else
				battle->reflect_damage(target, src, &wd, skill_id);
		}
	}
	//Reject Sword bugreport:4493 by Daegaladh
	if(wd.damage && tsc && tsc->data[SC_SWORDREJECT] &&
		(src->type!=BL_PC || (
			((TBL_PC *)src)->weapontype1 == W_DAGGER ||
			((TBL_PC *)src)->weapontype1 == W_1HSWORD ||
			((TBL_PC *)src)->status.weapon == W_2HSWORD
		)) &&
		rnd()%100 < tsc->data[SC_SWORDREJECT]->val2
		) {
		ATK_RATER(50);
		status_fix_damage(target,src,wd.damage,clif->damage(target,src,0,0,wd.damage,0,0,0));
		clif->skill_nodamage(target,target,ST_REJECTSWORD,tsc->data[SC_SWORDREJECT]->val1,1);
		if( --(tsc->data[SC_SWORDREJECT]->val3) <= 0 )
			status_change_end(target, SC_SWORDREJECT, INVALID_TIMER);
	}
#ifndef RENEWAL
	if(skill_id == ASC_BREAKER) {
		//Breaker's int-based damage (a misc attack?)
		struct Damage md = battle->calc_misc_attack(src, target, skill_id, skill_lv, wflag);
		wd.damage += md.damage;
	}
#endif

	return wd;
}

/*==========================================
 * Battle main entry, from skill->attack
 *------------------------------------------*/
struct Damage battle_calc_attack(int attack_type,struct block_list *bl,struct block_list *target,uint16 skill_id,uint16 skill_lv,int count)
{
	struct Damage d;
	struct map_session_data *sd=BL_CAST(BL_PC,bl);
	switch(attack_type) {
		case BF_WEAPON: d = battle->calc_weapon_attack(bl,target,skill_id,skill_lv,count); break;
		case BF_MAGIC:  d = battle->calc_magic_attack(bl,target,skill_id,skill_lv,count);  break;
		case BF_MISC:   d = battle->calc_misc_attack(bl,target,skill_id,skill_lv,count);   break;
	default:
		ShowError("battle_calc_attack: unknown attack type! %d\n",attack_type);
		memset(&d,0,sizeof(d));
		break;
	}

#ifdef HMAP_ZONE_DAMAGE_CAP_TYPE
	if( target && skill_id ) {
		int i;
		for(i = 0; i < map->list[target->m].zone->capped_skills_count; i++) {
			if( skill_id == map->list[target->m].zone->capped_skills[i]->nameid && (map->list[target->m].zone->capped_skills[i]->type & target->type) ) {
				if( target->type == BL_MOB && map->list[target->m].zone->capped_skills[i]->subtype != MZS_NONE ) {
					if( (((TBL_MOB*)target)->status.mode&MD_BOSS) && !(map->list[target->m].zone->disabled_skills[i]->subtype&MZS_BOSS) )
						continue;
					if( ((TBL_MOB*)target)->special_state.clone && !(map->list[target->m].zone->disabled_skills[i]->subtype&MZS_CLONE) )
						continue;
				}
				if( d.damage > map->list[target->m].zone->capped_skills[i]->cap )
					d.damage = map->list[target->m].zone->capped_skills[i]->cap;
				if( d.damage2 > map->list[target->m].zone->capped_skills[i]->cap )
					d.damage2 = map->list[target->m].zone->capped_skills[i]->cap;
				break;
			}
		}
	}
#endif

	if( d.damage + d.damage2 < 1 ) { //Miss/Absorbed
		//Weapon attacks should go through to cause additional effects.
		if (d.dmg_lv == ATK_DEF /*&& attack_type&(BF_MAGIC|BF_MISC)*/) // Isn't it that additional effects don't apply if miss?
			d.dmg_lv = ATK_MISS;
		d.dmotion = 0;
	} else // Some skills like Weaponry Research will cause damage even if attack is dodged
		d.dmg_lv = ATK_DEF;

	if(sd && d.damage+d.damage2>1) {
		if(sd->bonus.sp_vanish_rate && sd->bonus.sp_vanish_trigger && rnd()%10000<sd->bonus.sp_vanish_rate &&
			( (d.flag&sd->bonus.sp_vanish_trigger&BF_WEAPONMASK) || (d.flag&sd->bonus.sp_vanish_trigger&BF_RANGEMASK)
			|| (d.flag&sd->bonus.sp_vanish_trigger&BF_SKILLMASK) ))
			status_percent_damage(&sd->bl,target,0,-sd->bonus.sp_vanish_per,false);
	}
	return d;
}
//Performs reflect damage (magic (maya) is performed over skill.c).
void battle_reflect_damage(struct block_list *target, struct block_list *src, struct Damage *wd,uint16 skill_id) {
	int64 damage = wd->damage + wd->damage2, rdamage = 0, trdamage = 0;
	struct map_session_data *sd, *tsd;
	struct status_change *sc;
	int64 tick = timer->gettick();
	int delay = 50, rdelay = 0;
#ifdef RENEWAL
	int max_reflect_damage;

	max_reflect_damage = max(status_get_max_hp(target), status_get_max_hp(target) * status->get_lv(target) / 100);
#endif

	sd = BL_CAST(BL_PC, src);

	tsd = BL_CAST(BL_PC, target);
	sc = status->get_sc(target);

#ifdef RENEWAL
#define NORMALIZE_RDAMAGE(d) ( trdamage += rdamage = max(1, min(max_reflect_damage, (d))) )
#else
#define NORMALIZE_RDAMAGE(d) ( trdamage += rdamage = max(1, (d)) )
#endif

	if( sc && !sc->count )
		sc = NULL;

	if( sc ) {
		if (wd->flag & BF_SHORT && !(skill->get_inf(skill_id) & (INF_GROUND_SKILL | INF_SELF_SKILL))) {
			if( sc->data[SC_CRESCENTELBOW] && !is_boss(src) && rnd()%100 < sc->data[SC_CRESCENTELBOW]->val2 ){
				//ATK [{(Target HP / 100) x Skill Level} x Caster Base Level / 125] % + [Received damage x {1 + (Skill Level x 0.2)}]
				int ratio = (status_get_hp(src) / 100) * sc->data[SC_CRESCENTELBOW]->val1 * status->get_lv(target) / 125;
				if (ratio > 5000) ratio = 5000; // Maximum of 5000% ATK
				rdamage = ratio + (damage)* (10 + sc->data[SC_CRESCENTELBOW]->val1 * 20 / 10) / 10;
				skill->blown(target, src, skill->get_blewcount(SR_CRESCENTELBOW_AUTOSPELL, sc->data[SC_CRESCENTELBOW]->val1), unit->getdir(src), 0);
				clif->skill_damage(target, src, tick, status_get_amotion(src), 0, rdamage,
						   1, SR_CRESCENTELBOW_AUTOSPELL, sc->data[SC_CRESCENTELBOW]->val1, 6); // This is how official does
				clif->delay_damage(tick + delay, src, target,status_get_amotion(src)+1000,0, rdamage/10, 1, 0);
				status->damage(src, target, status->damage(target, src, rdamage, 0, 0, 1)/10, 0, 0, 1);
				status_change_end(target, SC_CRESCENTELBOW, INVALID_TIMER);
				/* shouldn't this trigger skill->additional_effect? */
				return; // Just put here to minimize redundancy
			}
		}

		if( wd->flag & BF_SHORT ) {
			if( !is_boss(src) ) {
				if( sc->data[SC_DEATHBOUND] && skill_id != WS_CARTTERMINATION ) {
					uint8 dir = map->calc_dir(target,src->x,src->y),
					t_dir = unit->getdir(target);

					if( !map->check_dir(dir,t_dir) ) {
						int64 rd1 = damage * sc->data[SC_DEATHBOUND]->val2 / 100; // Amplify damage.

						trdamage += rdamage = rd1 - (damage = rd1 * 30 / 100); // not normalized as intended.
						rdelay = clif->skill_damage(src, target, tick, status_get_amotion(src), status_get_dmotion(src), -3000, 1, RK_DEATHBOUND, sc->data[SC_DEATHBOUND]->val1, 6);
						skill->blown(target, src, skill->get_blewcount(RK_DEATHBOUND, sc->data[SC_DEATHBOUND]->val1), unit->getdir(src), 0);

						if( tsd ) /* is this right? rdamage as both left and right? */
							battle->drain(tsd, src, rdamage, rdamage, status_get_race(src), 0);
						battle->delay_damage(tick, wd->amotion,target,src,0,CR_REFLECTSHIELD,0,rdamage,ATK_DEF,rdelay,true);

						delay += 100;/* gradual increase so the numbers don't clip in the client */
					}
					wd->damage = wd->damage + wd->damage2;
					wd->damage2 = 0;
					status_change_end(target,SC_DEATHBOUND,INVALID_TIMER);
				}
			}
		}

		if( sc->data[SC_KYOMU] ){
			// Nullify reflecting ability of the conditions onwards
			return;
		}

	}

	if( wd->flag & BF_SHORT ) {
		if ( tsd && tsd->bonus.short_weapon_damage_return ) {
			NORMALIZE_RDAMAGE(damage * tsd->bonus.short_weapon_damage_return / 100);

			rdelay = clif->delay_damage(tick+delay,src, src, status_get_amotion(src), status_get_dmotion(src), rdamage, 1, 4);

			/* is this right? rdamage as both left and right? */
			battle->drain(tsd, src, rdamage, rdamage, status_get_race(src), 0);
			battle->delay_damage(tick, wd->amotion,target,src,0,CR_REFLECTSHIELD,0,rdamage,ATK_DEF,rdelay,true);

			delay += 100;/* gradual increase so the numbers don't clip in the client */
		}

		if( wd->dmg_lv >= ATK_BLOCK ) {/* yes block still applies, somehow gravity thinks it makes sense. */
			struct status_change *ssc;
			if( sc ) {
				struct status_change_entry *sce_d = sc->data[SC_DEVOTION];
				struct block_list *d_bl = NULL;

				if (sce_d && sce_d->val1)
					d_bl = map->id2bl(sce_d->val1);

				if( sc->data[SC_REFLECTSHIELD] && skill_id != WS_CARTTERMINATION && skill_id != GS_DESPERADO
				  && !(d_bl && !(wd->flag&BF_SKILL)) // It should not be a basic attack if the target is under devotion
				  && !(d_bl && sce_d && !check_distance_bl(target, d_bl, sce_d->val3)) // It should not be out of range if the target is under devotion
				) {

					NORMALIZE_RDAMAGE(damage * sc->data[SC_REFLECTSHIELD]->val2 / 100);
#ifndef RENEWAL
					rdelay = clif->delay_damage(tick+delay,src, src, status_get_amotion(src), status_get_dmotion(src), rdamage, 1, 4);
#else
					rdelay = clif->skill_damage(src, src, tick, delay, status_get_dmotion(src), rdamage, 1, CR_REFLECTSHIELD, 1, 4);
#endif
					/* is this right? rdamage as both left and right? */
					if( tsd )
						battle->drain(tsd, src, rdamage, rdamage, status_get_race(src), 0);
					battle->delay_damage(tick, wd->amotion,target,src,0,CR_REFLECTSHIELD,0,rdamage,ATK_DEF,rdelay,true);

					delay += 100;/* gradual increase so the numbers don't clip in the client */
				}
				if( sc->data[SC_LG_REFLECTDAMAGE] && rnd()%100 < (30 + 10*sc->data[SC_LG_REFLECTDAMAGE]->val1) ) {
					bool change = false;

					NORMALIZE_RDAMAGE(damage * sc->data[SC_LG_REFLECTDAMAGE]->val2 / 100);

					trdamage -= rdamage;/* wont count towards total */

					if( sd && !sd->state.autocast ) {
						change = true;
						sd->state.autocast = 1;
					}

					map->foreachinshootrange(battle->damage_area,target,skill->get_splash(LG_REFLECTDAMAGE,1),BL_CHAR,tick,target,delay,wd->dmotion,rdamage,status_get_race(target));

					if( change )
						sd->state.autocast = 0;

					delay += 150;/* gradual increase so the numbers don't clip in the client */

					if( (--sc->data[SC_LG_REFLECTDAMAGE]->val3) <= 0 )
						status_change_end(target, SC_LG_REFLECTDAMAGE, INVALID_TIMER);
				}
				if( sc->data[SC_SHIELDSPELL_DEF] && sc->data[SC_SHIELDSPELL_DEF]->val1 == 2 ){
					NORMALIZE_RDAMAGE(damage * sc->data[SC_SHIELDSPELL_DEF]->val2 / 100);

					rdelay = clif->delay_damage(tick+delay,src, src, status_get_amotion(src), status_get_dmotion(src), rdamage, 1, 4);

					/* is this right? rdamage as both left and right? */
					if( tsd )
						battle->drain(tsd, src, rdamage, rdamage, status_get_race(src), 0);
					battle->delay_damage(tick, wd->amotion,target,src,0,CR_REFLECTSHIELD,0,rdamage,ATK_DEF,rdelay,true);

					delay += 100;/* gradual increase so the numbers don't clip in the client */
				}
			}
			if( ( ssc = status->get_sc(src) ) ) {
				if( ssc->data[SC_INSPIRATION] ) {
					NORMALIZE_RDAMAGE(damage / 100);

					rdelay = clif->delay_damage(tick+delay,target, target, status_get_amotion(target), status_get_dmotion(target), rdamage, 1, 4);

					/* is this right? rdamage as both left and right? */
					if( sd )
						battle->drain(sd, target, rdamage, rdamage, status_get_race(target), 0);
					battle->delay_damage(tick, wd->amotion,src,target,0,CR_REFLECTSHIELD,0,rdamage,ATK_DEF,rdelay,true);

					delay += 100;/* gradual increase so the numbers don't clip in the client */
				}
			}
		}
	} else {/* long */
		if ( tsd && tsd->bonus.long_weapon_damage_return ) {
			NORMALIZE_RDAMAGE(damage * tsd->bonus.long_weapon_damage_return / 100);

			rdelay = clif->delay_damage(tick+delay,src, src, status_get_amotion(src), status_get_dmotion(src), rdamage, 1, 4);

			/* is this right? rdamage as both left and right? */
			battle->drain(tsd, src, rdamage, rdamage, status_get_race(src), 0);
			battle->delay_damage(tick, wd->amotion,target,src,0,CR_REFLECTSHIELD,0,rdamage,ATK_DEF,rdelay,true);

			delay += 100;/* gradual increase so the numbers don't clip in the client */
		}
	}

	// Tell analyzers/compilers that we want to += it even the value is currently unused (it'd be used if we added new checks)
	(void)delay;

	/* something caused reflect */
	if( trdamage ) {
		skill->additional_effect(target, src, CR_REFLECTSHIELD, 1, BF_WEAPON|BF_SHORT|BF_NORMAL,ATK_DEF,tick);
	}

	return;
#undef NORMALIZE_RDAMAGE
}
void battle_drain(TBL_PC *sd, struct block_list *tbl, int64 rdamage, int64 ldamage, int race, int boss)
{
	struct weapon_data *wd;
	int type, thp = 0, tsp = 0, rhp = 0, rsp = 0, hp, sp, i;
	int64 *damage;
	for (i = 0; i < 4; i++) {
		//First two iterations: Right hand
		if (i < 2) { wd = &sd->right_weapon; damage = &rdamage; }
		else { wd = &sd->left_weapon; damage = &ldamage; }
		if (*damage <= 0) continue;
		//First and Third iterations: race, other two boss/nonboss state
		if (i == 0 || i == 2)
			type = race;
		else
			type = boss?RC_BOSS:RC_NONBOSS;

		hp = wd->hp_drain[type].value;
		if (wd->hp_drain[type].rate)
			hp += battle->calc_drain(*damage, wd->hp_drain[type].rate, wd->hp_drain[type].per);

		sp = wd->sp_drain[type].value;
		if (wd->sp_drain[type].rate)
			sp += battle->calc_drain(*damage, wd->sp_drain[type].rate, wd->sp_drain[type].per);

		if (hp) {
			if (wd->hp_drain[type].type)
				rhp += hp;
			thp += hp;
		}
		if (sp) {
			if (wd->sp_drain[type].type)
				rsp += sp;
			tsp += sp;
		}
	}

	if (sd->bonus.sp_vanish_rate && rnd()%1000 < sd->bonus.sp_vanish_rate && !sd->bonus.sp_vanish_trigger)
		status_percent_damage(&sd->bl, tbl, 0, (unsigned char)sd->bonus.sp_vanish_per, false);

	if( sd->sp_gain_race_attack[race] )
		tsp += sd->sp_gain_race_attack[race];
	if( sd->hp_gain_race_attack[race] )
		thp += sd->hp_gain_race_attack[race];

	if (!thp && !tsp) return;

	status->heal(&sd->bl, thp, tsp, battle_config.show_hp_sp_drain?3:1);

	if (rhp || rsp)
		status_zap(tbl, rhp, rsp);
}
// Deals the same damage to targets in area. [pakpil]
int battle_damage_area(struct block_list *bl, va_list ap) {
	int64 tick;
	int amotion, dmotion, damage;
	struct block_list *src;

	nullpo_ret(bl);

	tick = va_arg(ap, int64);
	src=va_arg(ap,struct block_list *);
	amotion=va_arg(ap,int);
	dmotion=va_arg(ap,int);
	damage=va_arg(ap,int);
	if( bl->type == BL_MOB && ((TBL_MOB*)bl)->class_ == MOBID_EMPERIUM )
		return 0;
	if( bl != src && battle->check_target(src,bl,BCT_ENEMY) > 0 ) {
		map->freeblock_lock();
		if( src->type == BL_PC )
			battle->drain((TBL_PC*)src, bl, damage, damage, status_get_race(bl), is_boss(bl));
		if( amotion )
			battle->delay_damage(tick, amotion,src,bl,0,CR_REFLECTSHIELD,0,damage,ATK_DEF,0,true);
		else
			status_fix_damage(src,bl,damage,0);
		clif->damage(bl,bl,amotion,dmotion,damage,1,ATK_BLOCK,0);
		if( !(src->type == BL_PC && ((TBL_PC*)src)->state.autocast) )
			skill->additional_effect(src, bl, CR_REFLECTSHIELD, 1, BF_WEAPON|BF_SHORT|BF_NORMAL,ATK_DEF,tick);
		map->freeblock_unlock();
	}

	return 0;
}
/*==========================================
 * Do a basic physical attack (call trough unit_attack_timer)
 *------------------------------------------*/
enum damage_lv battle_weapon_attack(struct block_list* src, struct block_list* target, int64 tick, int flag) {
	struct map_session_data *sd = NULL, *tsd = NULL;
	struct status_data *sstatus, *tstatus;
	struct status_change *sc, *tsc;
	int64 damage;
	int skillv;
	struct Damage wd;

	nullpo_retr(ATK_NONE, src);
	nullpo_retr(ATK_NONE, target);

	if (src->prev == NULL || target->prev == NULL)
		return ATK_NONE;

	sd = BL_CAST(BL_PC, src);
	tsd = BL_CAST(BL_PC, target);

	sstatus = status->get_status_data(src);
	tstatus = status->get_status_data(target);

	sc = status->get_sc(src);
	tsc = status->get_sc(target);

	if (sc && !sc->count) //Avoid sc checks when there's none to check for. [Skotlex]
		sc = NULL;
	if (tsc && !tsc->count)
		tsc = NULL;

	if (sd)
	{
		sd->state.arrow_atk = (sd->status.weapon == W_BOW || (sd->status.weapon >= W_REVOLVER && sd->status.weapon <= W_GRENADE));
		if (sd->state.arrow_atk)
		{
			int index = sd->equip_index[EQI_AMMO];
			if (index<0) {
				if (sd->weapontype1 > W_KATAR && sd->weapontype1 < W_HUUMA)
					clif->arrow_fail(sd, 0);
				else
					clif->skill_fail(sd, 0, USESKILL_FAIL_NEED_MORE_BULLET, 0);
				return ATK_NONE;
			}
			//Ammo check by Ishizu-chan
			if (sd->inventory_data[index])
			switch (sd->status.weapon) {
			case W_BOW:
				if (sd->inventory_data[index]->look != A_ARROW) {
					clif->arrow_fail(sd,0);
					return ATK_NONE;
				}
			break;
			case W_REVOLVER:
			case W_RIFLE:
			case W_GATLING:
			case W_SHOTGUN:
				if (sd->inventory_data[index]->look != A_BULLET) {
					clif->skill_fail(sd, 0, USESKILL_FAIL_NEED_MORE_BULLET, 0);
					return ATK_NONE;
				}
			break;
			case W_GRENADE:
				if (sd->inventory_data[index]->look != A_GRENADE) {
					clif->skill_fail(sd, 0, USESKILL_FAIL_NEED_MORE_BULLET, 0);
					return ATK_NONE;
				}
			break;
			}
		}
	}
	if (sc && sc->count) {
		if (sc->data[SC_CLOAKING] && !(sc->data[SC_CLOAKING]->val4 & 2))
			status_change_end(src, SC_CLOAKING, INVALID_TIMER);
		else if (sc->data[SC_CLOAKINGEXCEED] && !(sc->data[SC_CLOAKINGEXCEED]->val4 & 2))
			status_change_end(src, SC_CLOAKINGEXCEED, INVALID_TIMER);
	}
	if( tsc && tsc->data[SC_AUTOCOUNTER] && status->check_skilluse(target, src, KN_AUTOCOUNTER, 1) ) {
		uint8 dir = map->calc_dir(target,src->x,src->y);
		int t_dir = unit->getdir(target);
		int dist = distance_bl(src, target);
		if(dist <= 0 || (!map->check_dir(dir,t_dir) && dist <= tstatus->rhw.range+1)) {
			uint16 skill_lv = tsc->data[SC_AUTOCOUNTER]->val1;
			clif->skillcastcancel(target); //Remove the casting bar. [Skotlex]
			clif->damage(src, target, sstatus->amotion, 1, 0, 1, 0, 0); //Display MISS.
			status_change_end(target, SC_AUTOCOUNTER, INVALID_TIMER);
			skill->attack(BF_WEAPON,target,target,src,KN_AUTOCOUNTER,skill_lv,tick,0);
			return ATK_BLOCK;
		}
	}
	if( tsc && tsc->data[SC_BLADESTOP_WAIT] && !is_boss(src) && (src->type == BL_PC || tsd == NULL || distance_bl(src, target) <= (tsd->status.weapon == W_FIST ? 1 : 2)) )
	{
		uint16 skill_lv = tsc->data[SC_BLADESTOP_WAIT]->val1;
		int duration = skill->get_time2(MO_BLADESTOP,skill_lv);
		status_change_end(target, SC_BLADESTOP_WAIT, INVALID_TIMER);
		if(sc_start4(target, src, SC_BLADESTOP, 100, sd?pc->checkskill(sd, MO_BLADESTOP):5, 0, 0, target->id, duration)) {
			//Target locked.
			clif->damage(src, target, sstatus->amotion, 1, 0, 1, 0, 0); //Display MISS.
			clif->bladestop(target, src->id, 1);
			sc_start4(target, target, SC_BLADESTOP, 100, skill_lv, 0, 0, src->id, duration);
			return ATK_BLOCK;
		}
	}

	if(sd && (skillv = pc->checkskill(sd,MO_TRIPLEATTACK)) > 0) {
		int triple_rate= 30 - skillv; //Base Rate
		if (sc && sc->data[SC_SKILLRATE_UP] && sc->data[SC_SKILLRATE_UP]->val1 == MO_TRIPLEATTACK) {
			triple_rate+= triple_rate*(sc->data[SC_SKILLRATE_UP]->val2)/100;
			status_change_end(src, SC_SKILLRATE_UP, INVALID_TIMER);
		}
		if (rnd()%100 < triple_rate) {
			if( skill->attack(BF_WEAPON,src,src,target,MO_TRIPLEATTACK,skillv,tick,0) )
				return ATK_DEF;
			return ATK_MISS;
		}
	}

	if (sc) {
		if (sc->data[SC_SACRIFICE]) {
			uint16 skill_lv = sc->data[SC_SACRIFICE]->val1;
			damage_lv ret_val;

			if( --sc->data[SC_SACRIFICE]->val2 <= 0 )
				status_change_end(src, SC_SACRIFICE, INVALID_TIMER);

			/**
			 * We need to calculate the DMG before the hp reduction, because it can kill the source.
			 * For further information: bugreport:4950
			 **/
			ret_val = (damage_lv)skill->attack(BF_WEAPON,src,src,target,PA_SACRIFICE,skill_lv,tick,0);

			status_zap(src, sstatus->max_hp*9/100, 0);//Damage to self is always 9%
			if( ret_val == ATK_NONE )
				return ATK_MISS;
			return ret_val;
		}
		if (sc->data[SC_MAGICALATTACK]) {
			if( skill->attack(BF_MAGIC,src,src,target,NPC_MAGICALATTACK,sc->data[SC_MAGICALATTACK]->val1,tick,0) )
				return ATK_DEF;
			return ATK_MISS;
		}
		if( sc->data[SC_GENTLETOUCH_ENERGYGAIN] ) {
			if( sd && rnd()%100 < 10 + 5 * sc->data[SC_GENTLETOUCH_ENERGYGAIN]->val1)
				pc->addspiritball(sd,
								 skill->get_time(MO_CALLSPIRITS, sc->data[SC_GENTLETOUCH_ENERGYGAIN]->val1),
								 sc->data[SC_GENTLETOUCH_ENERGYGAIN]->val1);
		}
		if( tsc && tsc->data[SC_GENTLETOUCH_ENERGYGAIN] ) {
			if( tsd && rnd()%100 < 10 + 5 * tsc->data[SC_GENTLETOUCH_ENERGYGAIN]->val1)
				pc->addspiritball(tsd,
								 skill->get_time(MO_CALLSPIRITS, tsc->data[SC_GENTLETOUCH_ENERGYGAIN]->val1),
								 tsc->data[SC_GENTLETOUCH_ENERGYGAIN]->val1);
		}

		if( tsc && tsc->data[SC_MTF_MLEATKED] && rnd()%100 < 20 )
			clif->skill_nodamage(target, target, SM_ENDURE, 5,
				sc_start(target,target, SC_ENDURE, 100, 5, skill->get_time(SM_ENDURE, 5)));
	}

	if(tsc && tsc->data[SC_KAAHI] && tsc->data[SC_KAAHI]->val4 == INVALID_TIMER && tstatus->hp < tstatus->max_hp)
		tsc->data[SC_KAAHI]->val4 = timer->add(tick + skill->get_time2(SL_KAAHI,tsc->data[SC_KAAHI]->val1), status->kaahi_heal_timer, target->id, SC_KAAHI); //Activate heal.

	wd = battle->calc_attack(BF_WEAPON, src, target, 0, 0, flag);

	if( sc && sc->count ) {
		if( sc->data[SC_SPELLFIST] ) {
			if( --(sc->data[SC_SPELLFIST]->val1) >= 0 ){
				struct Damage ad = battle->calc_attack(BF_MAGIC,src,target,sc->data[SC_SPELLFIST]->val3,sc->data[SC_SPELLFIST]->val4,flag|BF_SHORT);
				wd.damage = ad.damage;
				damage_div_fix(wd.damage, wd.div_);
			}else
				status_change_end(src,SC_SPELLFIST,INVALID_TIMER);
		}

		if( sd && sc->data[SC_FEARBREEZE] && sc->data[SC_FEARBREEZE]->val4 > 0 && sd->status.inventory[sd->equip_index[EQI_AMMO]].amount >= sc->data[SC_FEARBREEZE]->val4 && battle_config.arrow_decrement){
			pc->delitem(sd,sd->equip_index[EQI_AMMO],sc->data[SC_FEARBREEZE]->val4,0,1,LOG_TYPE_CONSUME);
			sc->data[SC_FEARBREEZE]->val4 = 0;
		}
	}
	if (sd && sd->state.arrow_atk) //Consume arrow.
		battle->consume_ammo(sd, 0, 0);

	damage = wd.damage + wd.damage2;
	if( damage > 0 && src != target ) {
		if( sc && sc->data[SC_DUPLELIGHT] && (wd.flag&BF_SHORT) && rnd()%100 <= 10+2*sc->data[SC_DUPLELIGHT]->val1 ){
			// Activates it only from melee damage
			uint16 skill_id;
			if( rnd()%2 == 1 )
				skill_id = AB_DUPLELIGHT_MELEE;
			else
				skill_id = AB_DUPLELIGHT_MAGIC;
			skill->attack(skill->get_type(skill_id), src, src, target, skill_id, sc->data[SC_DUPLELIGHT]->val1, tick, SD_LEVEL);
		}
	}

	wd.dmotion = clif->damage(src, target, wd.amotion, wd.dmotion, wd.damage, wd.div_ , wd.type, wd.damage2);

	if (sd && sd->bonus.splash_range > 0 && damage > 0)
		skill->castend_damage_id(src, target, 0, 1, tick, 0);
	if ( target->type == BL_SKILL && damage > 0 ){
		TBL_SKILL *su = (TBL_SKILL*)target;
		if( su->group && su->group->skill_id == HT_BLASTMINE)
			skill->blown(src, target, 3, -1, 0);
	}
	map->freeblock_lock();

	if( skill->check_shadowform(target, damage, wd.div_) ){
		if( !status->isdead(target) )
			skill->additional_effect(src, target, 0, 0, wd.flag, wd.dmg_lv, tick);
		if( wd.dmg_lv > ATK_BLOCK)
			skill->counter_additional_effect(src, target, 0, 0, wd.flag,tick);
	}else
		battle->delay_damage(tick, wd.amotion, src, target, wd.flag, 0, 0, damage, wd.dmg_lv, wd.dmotion, true);
	if( tsc ) {
		if( tsc->data[SC_DEVOTION] ) {
			struct status_change_entry *sce = tsc->data[SC_DEVOTION];
			struct block_list *d_bl = map->id2bl(sce->val1);

			if( d_bl && (
				(d_bl->type == BL_MER && ((TBL_MER*)d_bl)->master && ((TBL_MER*)d_bl)->master->bl.id == target->id) ||
				(d_bl->type == BL_PC && ((TBL_PC*)d_bl)->devotion[sce->val2] == target->id)
				) && check_distance_bl(target, d_bl, sce->val3) )
			{
				clif->damage(d_bl, d_bl, 0, 0, damage, 0, 0, 0);
				status_fix_damage(NULL, d_bl, damage, 0);
			}
			else
				status_change_end(target, SC_DEVOTION, INVALID_TIMER);
		} else if( tsc->data[SC_CIRCLE_OF_FIRE_OPTION] && (wd.flag&BF_SHORT) && target->type == BL_PC ) {
			struct elemental_data *ed = ((TBL_PC*)target)->ed;
			if( ed ) {
				clif->skill_damage(&ed->bl, target, tick, status_get_amotion(src), 0, -30000, 1, EL_CIRCLE_OF_FIRE, tsc->data[SC_CIRCLE_OF_FIRE_OPTION]->val1, 6);
				skill->attack(BF_MAGIC,&ed->bl,&ed->bl,src,EL_CIRCLE_OF_FIRE,tsc->data[SC_CIRCLE_OF_FIRE_OPTION]->val1,tick,wd.flag);
			}
		} else if( tsc->data[SC_WATER_SCREEN_OPTION] && tsc->data[SC_WATER_SCREEN_OPTION]->val1 ) {
			struct block_list *e_bl = map->id2bl(tsc->data[SC_WATER_SCREEN_OPTION]->val1);
			if( e_bl && !status->isdead(e_bl) ) {
				clif->damage(e_bl,e_bl,wd.amotion,wd.dmotion,damage,wd.div_,wd.type,wd.damage2);
				status->damage(target,e_bl,damage,0,0,0);
				// Just show damage in target.
				clif->damage(src, target, wd.amotion, wd.dmotion, damage, wd.div_, wd.type, wd.damage2 );
				map->freeblock_unlock();
				return ATK_NONE;
			}
		}
	}
	if (sc && sc->data[SC_AUTOSPELL] && rnd()%100 < sc->data[SC_AUTOSPELL]->val4) {
		int sp = 0;
		uint16 skill_id = sc->data[SC_AUTOSPELL]->val2;
		uint16 skill_lv = sc->data[SC_AUTOSPELL]->val3;
		int i = rnd()%100;
		if (sc->data[SC_SOULLINK] && sc->data[SC_SOULLINK]->val2 == SL_SAGE)
			i = 0; //Max chance, no skill_lv reduction. [Skotlex]
		if (i >= 50) skill_lv -= 2;
		else if (i >= 15) skill_lv--;
		if (skill_lv < 1) skill_lv = 1;
		sp = skill->get_sp(skill_id,skill_lv) * 2 / 3;

		if (status->charge(src, 0, sp)) {
			switch (skill->get_casttype(skill_id)) {
				case CAST_GROUND:
					skill->castend_pos2(src, target->x, target->y, skill_id, skill_lv, tick, flag);
					break;
				case CAST_NODAMAGE:
					skill->castend_nodamage_id(src, target, skill_id, skill_lv, tick, flag);
					break;
				case CAST_DAMAGE:
					skill->castend_damage_id(src, target, skill_id, skill_lv, tick, flag);
					break;
			}
		}
	}
	if (sd) {
		if( wd.flag&BF_SHORT && sc
		 && sc->data[SC__AUTOSHADOWSPELL] && rnd()%100 < sc->data[SC__AUTOSHADOWSPELL]->val3
		 && sd->status.skill[skill->get_index(sc->data[SC__AUTOSHADOWSPELL]->val1)].id != 0
		 && sd->status.skill[skill->get_index(sc->data[SC__AUTOSHADOWSPELL]->val1)].flag == SKILL_FLAG_PLAGIARIZED
		) {
			int r_skill = sd->status.skill[skill->get_index(sc->data[SC__AUTOSHADOWSPELL]->val1)].id;
			int r_lv = sc->data[SC__AUTOSHADOWSPELL]->val2;

			if (r_skill != AL_HOLYLIGHT && r_skill != PR_MAGNUS) {
				int type;
				if( (type = skill->get_casttype(r_skill)) == CAST_GROUND ) {
					int maxcount = 0;

					if( !(BL_PC&battle_config.skill_reiteration)
					 && skill->get_unit_flag(r_skill)&UF_NOREITERATION )
						type = -1;

					if( BL_PC&battle_config.skill_nofootset
					 && skill->get_unit_flag(r_skill)&UF_NOFOOTSET )
						type = -1;

					if( BL_PC&battle_config.land_skill_limit
					 && (maxcount = skill->get_maxcount(r_skill, r_lv)) > 0
					) {
						int v;
						for(v=0;v<MAX_SKILLUNITGROUP && sd->ud.skillunit[v] && maxcount;v++) {
							if(sd->ud.skillunit[v]->skill_id == r_skill)
								maxcount--;
						}
						if( maxcount == 0 )
							type = -1;
					}

					if( type != CAST_GROUND ) {
						clif->skill_fail(sd,r_skill,USESKILL_FAIL_LEVEL,0);
						map->freeblock_unlock();
						return wd.dmg_lv;
					}
				}

				sd->state.autocast = 1;
				skill->consume_requirement(sd,r_skill,r_lv,3);
				switch( type ) {
					case CAST_GROUND:
						skill->castend_pos2(src, target->x, target->y, r_skill, r_lv, tick, flag);
						break;
					case CAST_NODAMAGE:
						skill->castend_nodamage_id(src, target, r_skill, r_lv, tick, flag);
						break;
					case CAST_DAMAGE:
						skill->castend_damage_id(src, target, r_skill, r_lv, tick, flag);
						break;
				}
				sd->state.autocast = 0;

				sd->ud.canact_tick = tick + skill->delay_fix(src, r_skill, r_lv);
				clif->status_change(src, SI_POSTDELAY, 1, skill->delay_fix(src, r_skill, r_lv), 0, 0, 1);
			}
		}

		if (wd.flag & BF_WEAPON && src != target && damage > 0) {
			if (battle_config.left_cardfix_to_right)
				battle->drain(sd, target, wd.damage, wd.damage, tstatus->race, is_boss(target));
			else
				battle->drain(sd, target, wd.damage, wd.damage2, tstatus->race, is_boss(target));
		}
	}

	if (tsc) {
		if (tsc->data[SC_POISONREACT]
		 && ( rnd()%100 < tsc->data[SC_POISONREACT]->val3
		    || sstatus->def_ele == ELE_POISON
		    )
		 /* && check_distance_bl(src, target, tstatus->rhw.range+1) Doesn't check range! o.O; */
		 && status->check_skilluse(target, src, TF_POISON, 0)
		) {
			//Poison React
			struct status_change_entry *sce = tsc->data[SC_POISONREACT];
			if (sstatus->def_ele == ELE_POISON) {
				sce->val2 = 0;
				skill->attack(BF_WEAPON,target,target,src,AS_POISONREACT,sce->val1,tick,0);
			} else {
				skill->attack(BF_WEAPON,target,target,src,TF_POISON, 5, tick, 0);
				--sce->val2;
			}
			if (sce->val2 <= 0)
				status_change_end(target, SC_POISONREACT, INVALID_TIMER);
		}
	}
	map->freeblock_unlock();
	return wd.dmg_lv;
}
#undef ATK_RATE
#undef ATK_RATE2
#undef ATK_RATER
#undef ATK_RATEL
#undef ATK_ADDRATE
#undef ATK_ADDRATE2
#undef ATK_ADD
#undef ATK_ADD2
#undef GET_NORMAL_ATTACK
#undef GET_NORMAL_ATTACK2

bool battle_check_undead(int race,int element)
{
	if(battle_config.undead_detect_type == 0) {
		if(element == ELE_UNDEAD)
			return true;
	}
	else if(battle_config.undead_detect_type == 1) {
		if(race == RC_UNDEAD)
			return true;
	}
	else {
		if(element == ELE_UNDEAD || race == RC_UNDEAD)
			return true;
	}
	return false;
}

//Returns the upmost level master starting with the given object
struct block_list* battle_get_master(struct block_list *src) {
	struct block_list *prev; //Used for infinite loop check (master of yourself?)
	do {
		prev = src;
		switch (src->type) {
			case BL_PET:
				if (((TBL_PET*)src)->msd)
					src = (struct block_list*)((TBL_PET*)src)->msd;
				break;
			case BL_MOB:
				if (((TBL_MOB*)src)->master_id)
					src = map->id2bl(((TBL_MOB*)src)->master_id);
				break;
			case BL_HOM:
				if (((TBL_HOM*)src)->master)
					src = (struct block_list*)((TBL_HOM*)src)->master;
				break;
			case BL_MER:
				if (((TBL_MER*)src)->master)
					src = (struct block_list*)((TBL_MER*)src)->master;
				break;
			case BL_ELEM:
				if (((TBL_ELEM*)src)->master)
					src = (struct block_list*)((TBL_ELEM*)src)->master;
				break;
			case BL_SKILL:
				if (((TBL_SKILL*)src)->group && ((TBL_SKILL*)src)->group->src_id)
					src = map->id2bl(((TBL_SKILL*)src)->group->src_id);
				break;
		}
	} while (src && src != prev);
	return prev;
}

/*==========================================
 * Checks the state between two targets (rewritten by Skotlex)
 * (enemy, friend, party, guild, etc)
 * See battle.h for possible values/combinations
 * to be used here (BCT_* constants)
 * Return value is:
 * 1: flag holds true (is enemy, party, etc)
 * -1: flag fails
 * 0: Invalid target (non-targetable ever)
 *------------------------------------------*/
int battle_check_target( struct block_list *src, struct block_list *target,int flag)
{
	int16 m; //map
	int state = 0; //Initial state none
	int strip_enemy = 1; //Flag which marks whether to remove the BCT_ENEMY status if it's also friend/ally.
	struct block_list *s_bl = src, *t_bl = target;

	nullpo_ret(src);
	nullpo_ret(target);

	m = target->m;

	if (flag&BCT_ENEMY && ( map->getcell(m,src->x,src->y,CELL_CHKBASILICA) || map->getcell(m,target->x,target->y,CELL_CHKBASILICA) ) ) {
		return -1;
	}

	//t_bl/s_bl hold the 'master' of the attack, while src/target are the actual
	//objects involved.
	if( (t_bl = battle->get_master(target)) == NULL )
		t_bl = target;

	if( (s_bl = battle->get_master(src)) == NULL )
		s_bl = src;

	if ( s_bl->type == BL_PC ) {
		switch( t_bl->type ) {
			case BL_MOB: // Source => PC, Target => MOB
				if (pc_has_permission((TBL_PC*)s_bl, PC_PERM_DISABLE_PVM) )
					return 0;
				break;
			case BL_PC:
				if (pc_has_permission((TBL_PC*)s_bl, PC_PERM_DISABLE_PVP))
					return 0;
				break;
			default:/* anything else goes */
				break;
		}
	}

	switch( target->type ) { // Checks on actual target
		case BL_PC: {
				struct status_change* sc = status->get_sc(src);
			
				if( ((TBL_PC*)target)->invincible_timer != INVALID_TIMER ) {
					switch( battle->get_current_skill(src) ) {
						/* TODO a proper distinction should be established bugreport:8397 */
						case PR_SANCTUARY:
						case PR_MAGNIFICAT:
							break;
						default:
							return -1;
					}
				}
				if ( pc_isinvisible((TBL_PC*)target) )
					return -1; //Cannot be targeted yet.
				if( sc && sc->count ) {
					if( sc->data[SC_SIREN] && sc->data[SC_SIREN]->val2 == target->id )
						return -1;
				}
			}
			break;
		case BL_MOB:
			if(((((TBL_MOB*)target)->special_state.ai == 2 || //Marine Spheres
				(((TBL_MOB*)target)->special_state.ai == 3 && battle_config.summon_flora&1)) && //Floras
				s_bl->type == BL_PC && src->type != BL_MOB) || (((TBL_MOB*)target)->special_state.ai == 4 && t_bl->id != s_bl->id)) //Zanzoe
			{
				//Targetable by players
				state |= BCT_ENEMY;
				strip_enemy = 0;
			}
			break;
		case BL_SKILL:
		{
			TBL_SKILL *su = (TBL_SKILL*)target;
			if( !su->group )
				return 0;
			if( skill->get_inf2(su->group->skill_id)&INF2_TRAP && su->group->unit_id != UNT_USED_TRAPS) { //Only a few skills can target traps...
				switch( battle->get_current_skill(src) ) {
					case RK_DRAGONBREATH:// it can only hit traps in pvp/gvg maps
					case RK_DRAGONBREATH_WATER:
						if( !map->list[m].flag.pvp && !map->list[m].flag.gvg )
							break;
					case 0://you can hit them without skills
					case MA_REMOVETRAP:
					case HT_REMOVETRAP:
					case AC_SHOWER:
					case MA_SHOWER:
					case WZ_SIGHTRASHER:
					case WZ_SIGHTBLASTER:
					case SM_MAGNUM:
					case MS_MAGNUM:
					case RA_DETONATOR:
					case RA_SENSITIVEKEEN:
					case RK_STORMBLAST:
					case SR_RAMPAGEBLASTER:
					case NC_COLDSLOWER:
#ifdef RENEWAL
					case KN_BOWLINGBASH:
					case KN_SPEARSTAB:
					case LK_SPIRALPIERCE:
					case ML_SPIRALPIERCE:
					case MO_FINGEROFFENSIVE:
					case MO_INVESTIGATE:
					case MO_TRIPLEATTACK:
					case MO_EXTREMITYFIST:
					case CR_HOLYCROSS:
					case ASC_METEORASSAULT:
					case RG_RAID:
					case MC_CARTREVOLUTION:
					case HT_CLAYMORETRAP:
					case RA_ICEBOUNDTRAP:
					case RA_FIRINGTRAP:
#endif
						state |= BCT_ENEMY;
						strip_enemy = 0;
						break;
					default:
						return 0;
				}
			} else if (su->group->skill_id==WZ_ICEWALL ||
					su->group->skill_id == GN_WALLOFTHORN) {
				state |= BCT_ENEMY;
				strip_enemy = 0;
			} else {
				//Excepting traps and icewall, you should not be able to target skills.
				return 0;
			}
		}
			break;
		//Valid targets with no special checks here.
		case BL_MER:
		case BL_HOM:
		case BL_ELEM:
			break;
		//All else not specified is an invalid target.
		default:
			return 0;
	} //end switch actual target

	switch( t_bl->type ) { //Checks on target master
		case BL_PC:
		{
			struct map_session_data *sd;
			if( t_bl == s_bl ) break;
			sd = BL_CAST(BL_PC, t_bl);

			if( sd->state.monster_ignore && flag&BCT_ENEMY )
				return 0; // Global immunity only to Attacks
			if( sd->status.karma && s_bl->type == BL_PC && ((TBL_PC*)s_bl)->status.karma )
				state |= BCT_ENEMY; // Characters with bad karma may fight amongst them
			if( sd->state.killable ) {
				state |= BCT_ENEMY; // Everything can kill it
				strip_enemy = 0;
			}
			break;
		}
		case BL_MOB:
		{
			struct mob_data *md = BL_CAST(BL_MOB, t_bl);

			if( !((map->agit_flag || map->agit2_flag) && map->list[m].flag.gvg_castle)
				&& md->guardian_data && (md->guardian_data->g || md->guardian_data->castle->guild_id) )
				return 0; // Disable guardians/emperiums owned by Guilds on non-woe times.
			break;
		}
		default: break; //other type doesn't have slave yet
	} //end switch master target

	switch( src->type ) { //Checks on actual src type
		case BL_PET:
			if (t_bl->type != BL_MOB && flag&BCT_ENEMY)
				return 0; //Pet may not attack non-mobs.
			if (t_bl->type == BL_MOB && ((TBL_MOB*)t_bl)->guardian_data && flag&BCT_ENEMY)
				return 0; //pet may not attack Guardians/Emperium
			break;
		case BL_SKILL: {
				struct skill_unit *su = (struct skill_unit *)src;
				struct status_change* sc = status->get_sc(target);
				if (!su->group)
					return 0;

				if (su->group->src_id == target->id) {
					int inf2 = skill->get_inf2(su->group->skill_id);
					if (inf2&INF2_NO_TARGET_SELF)
						return -1;
					if (inf2&INF2_TARGET_SELF)
						return 1;
				}
				//Status changes that prevent traps from triggering
				if (sc && sc->count && skill->get_inf2(su->group->skill_id)&INF2_TRAP) {
					if( sc->data[SC_WZ_SIGHTBLASTER] && sc->data[SC_WZ_SIGHTBLASTER]->val2 > 0 && sc->data[SC_WZ_SIGHTBLASTER]->val4%2 == 0)
						return -1;
				}
			}
			break;
		case BL_MER:
			if (t_bl->type == BL_MOB && ((TBL_MOB*)t_bl)->class_ == MOBID_EMPERIUM && flag&BCT_ENEMY)
				return 0; //mercenary may not attack Emperium
			break;
	} //end switch actual src

	switch( s_bl->type ) { //Checks on source master
		case BL_PC:
		{
			struct map_session_data *sd = BL_CAST(BL_PC, s_bl);
			if( s_bl != t_bl ) {
				if( sd->state.killer ) {
					state |= BCT_ENEMY; // Can kill anything
					strip_enemy = 0;
				} else if( sd->duel_group
				        && !((!battle_config.duel_allow_pvp && map->list[m].flag.pvp) || (!battle_config.duel_allow_gvg && map_flag_gvg(m)))
				) {
					if( t_bl->type == BL_PC && (sd->duel_group == ((TBL_PC*)t_bl)->duel_group) )
						return (BCT_ENEMY&flag)?1:-1; // Duel targets can ONLY be your enemy, nothing else.
					else
						return 0; // You can't target anything out of your duel
				}
			}
			if( map_flag_gvg(m) && !sd->status.guild_id && t_bl->type == BL_MOB && ((TBL_MOB*)t_bl)->class_ == MOBID_EMPERIUM )
				return 0; //If you don't belong to a guild, can't target emperium.
			if( t_bl->type != BL_PC )
				state |= BCT_ENEMY; //Natural enemy.
			break;
		}
		case BL_MOB:
		{
			struct mob_data *md = BL_CAST(BL_MOB, s_bl);
			if( !((map->agit_flag || map->agit2_flag) && map->list[m].flag.gvg_castle)
				&& md->guardian_data && (md->guardian_data->g || md->guardian_data->castle->guild_id) )
				return 0; // Disable guardians/emperium owned by Guilds on non-woe times.

			if( !md->special_state.ai )
			{ //Normal mobs
				if(
					( target->type == BL_MOB && t_bl->type == BL_PC && ( ((TBL_MOB*)target)->special_state.ai != 4 && ((TBL_MOB*)target)->special_state.ai != 1 ) ) ||
					( t_bl->type == BL_MOB && !((TBL_MOB*)t_bl)->special_state.ai )
				  )
					state |= BCT_PARTY; //Normal mobs with no ai are friends.
				else
					state |= BCT_ENEMY; //However, all else are enemies.
			}
			else
			{
				if( t_bl->type == BL_MOB && !((TBL_MOB*)t_bl)->special_state.ai )
					state |= BCT_ENEMY; //Natural enemy for AI mobs are normal mobs.
			}
			break;
		}
		default:
		//Need some sort of default behavior for unhandled types.
			if (t_bl->type != s_bl->type)
				state |= BCT_ENEMY;
			break;
	} //end switch on src master

	if( (flag&BCT_ALL) == BCT_ALL )
	{ //All actually stands for all attackable chars
		if( target->type&BL_CHAR )
			return 1;
		else
			return -1;
	}
	if( flag == BCT_NOONE ) //Why would someone use this? no clue.
		return -1;

	if( t_bl == s_bl )
	{ //No need for further testing.
		state |= BCT_SELF|BCT_PARTY|BCT_GUILD;
		if( state&BCT_ENEMY && strip_enemy )
			state&=~BCT_ENEMY;
		return (flag&state)?1:-1;
	}

	if( map_flag_vs(m) ) {
		//Check rivalry settings.
		int sbg_id = 0, tbg_id = 0;
		if( map->list[m].flag.battleground ) {
			sbg_id = bg->team_get_id(s_bl);
			tbg_id = bg->team_get_id(t_bl);
		}
		if( flag&(BCT_PARTY|BCT_ENEMY) ) {
			int s_party = status->get_party_id(s_bl);
			int s_guild = status->get_guild_id(s_bl);

			if( s_party && s_party == status->get_party_id(t_bl)
			 && !(map->list[m].flag.pvp && map->list[m].flag.pvp_noparty)
			 && !(map_flag_gvg(m) && map->list[m].flag.gvg_noparty && !( s_guild && s_guild == status->get_guild_id(t_bl) ))
			 && (!map->list[m].flag.battleground || sbg_id == tbg_id) )
				state |= BCT_PARTY;
			else
				state |= BCT_ENEMY;
		}
		if( flag&(BCT_GUILD|BCT_ENEMY) ) {
			int s_guild = status->get_guild_id(s_bl);
			int t_guild = status->get_guild_id(t_bl);
			if( !(map->list[m].flag.pvp && map->list[m].flag.pvp_noguild)
			 && s_guild && t_guild
			 && (s_guild == t_guild || (!(flag&BCT_SAMEGUILD) && guild->isallied(s_guild, t_guild)))
			 && (!map->list[m].flag.battleground || sbg_id == tbg_id) )
				state |= BCT_GUILD;
			else
				state |= BCT_ENEMY;
		}
		if( state&BCT_ENEMY && map->list[m].flag.battleground && sbg_id && sbg_id == tbg_id )
			state &= ~BCT_ENEMY;

		if( state&BCT_ENEMY && battle_config.pk_mode && !map_flag_gvg(m) && s_bl->type == BL_PC && t_bl->type == BL_PC )
		{ // Prevent novice engagement on pk_mode (feature by Valaris)
			TBL_PC *sd = (TBL_PC*)s_bl, *sd2 = (TBL_PC*)t_bl;
			if (
				(sd->class_&MAPID_UPPERMASK) == MAPID_NOVICE ||
				(sd2->class_&MAPID_UPPERMASK) == MAPID_NOVICE ||
				(int)sd->status.base_level < battle_config.pk_min_level ||
				(int)sd2->status.base_level < battle_config.pk_min_level ||
				(battle_config.pk_level_range && abs((int)sd->status.base_level - (int)sd2->status.base_level) > battle_config.pk_level_range)
			)
				state &= ~BCT_ENEMY;
		}
	}//end map_flag_vs chk rivality
	else
	{ //Non pvp/gvg, check party/guild settings.
		if( flag&BCT_PARTY || state&BCT_ENEMY ) {
			int s_party = status->get_party_id(s_bl);
			if(s_party && s_party == status->get_party_id(t_bl))
				state |= BCT_PARTY;
		}
		if( flag&BCT_GUILD || state&BCT_ENEMY ) {
			int s_guild = status->get_guild_id(s_bl);
			int t_guild = status->get_guild_id(t_bl);
			if(s_guild && t_guild && (s_guild == t_guild || (!(flag&BCT_SAMEGUILD) && guild->isallied(s_guild, t_guild))))
				state |= BCT_GUILD;
		}
	} //end non pvp/gvg chk rivality

	if( !state ) //If not an enemy, nor a guild, nor party, nor yourself, it's neutral.
		state = BCT_NEUTRAL;
	//Alliance state takes precedence over enemy one.
	else if( state&BCT_ENEMY && strip_enemy && state&(BCT_SELF|BCT_PARTY|BCT_GUILD) )
		state&=~BCT_ENEMY;

	return (flag&state)?1:-1;
}
/*==========================================
 * Check if can attack from this range
 * Basic check then calling path->search for obstacle etc..
 *------------------------------------------*/
bool battle_check_range(struct block_list *src, struct block_list *bl, int range)
{
	int d;
	nullpo_retr(false, src);
	nullpo_retr(false, bl);

	if( src->m != bl->m )
		return false;

#ifndef CIRCULAR_AREA
	if( src->type == BL_PC ) { // Range for players' attacks and skills should always have a circular check. [Angezerus]
		if ( !check_distance_client_bl(src, bl, range) )
			return false;
	} else
#endif
	if( !check_distance_bl(src, bl, range) )
		return false;

	if( (d = distance_bl(src, bl)) < 2 )
		return true;  // No need for path checking.

	if( d > AREA_SIZE )
		return false; // Avoid targeting objects beyond your range of sight.

	return path->search_long(NULL,src->m,src->x,src->y,bl->x,bl->y,CELL_CHKWALL);
}

static const struct battle_data {
	const char* str;
	int* val;
	int defval;
	int min;
	int max;
} battle_data[] = {
	{ "warp_point_debug",                   &battle_config.warp_point_debug,                0,      0,      1,              },
	{ "enable_critical",                    &battle_config.enable_critical,                 BL_PC,  BL_NUL, BL_ALL,         },
	{ "mob_critical_rate",                  &battle_config.mob_critical_rate,               100,    0,      INT_MAX,        },
	{ "critical_rate",                      &battle_config.critical_rate,                   100,    0,      INT_MAX,        },
	{ "enable_baseatk",                     &battle_config.enable_baseatk,                  BL_PC|BL_HOM, BL_NUL, BL_ALL,   },
	{ "enable_perfect_flee",                &battle_config.enable_perfect_flee,             BL_PC|BL_PET, BL_NUL, BL_ALL,   },
	{ "casting_rate",                       &battle_config.cast_rate,                       100,    0,      INT_MAX,        },
	{ "delay_rate",                         &battle_config.delay_rate,                      100,    0,      INT_MAX,        },
	{ "delay_dependon_dex",                 &battle_config.delay_dependon_dex,              0,      0,      1,              },
	{ "delay_dependon_agi",                 &battle_config.delay_dependon_agi,              0,      0,      1,              },
	{ "skill_delay_attack_enable",          &battle_config.sdelay_attack_enable,            0,      0,      1,              },
	{ "left_cardfix_to_right",              &battle_config.left_cardfix_to_right,           0,      0,      1,              },
	{ "skill_add_range",                    &battle_config.skill_add_range,                 0,      0,      INT_MAX,        },
	{ "skill_out_range_consume",            &battle_config.skill_out_range_consume,         1,      0,      1,              },
	{ "skillrange_by_distance",             &battle_config.skillrange_by_distance,          ~BL_PC, BL_NUL, BL_ALL,         },
	{ "skillrange_from_weapon",             &battle_config.use_weapon_skill_range,          BL_NUL, BL_NUL, BL_ALL,         },
	{ "player_damage_delay_rate",           &battle_config.pc_damage_delay_rate,            100,    0,      INT_MAX,        },
	{ "defunit_not_enemy",                  &battle_config.defnotenemy,                     0,      0,      1,              },
	{ "gvg_traps_target_all",               &battle_config.vs_traps_bctall,                 BL_PC,  BL_NUL, BL_ALL,         },
	{ "traps_setting",                      &battle_config.traps_setting,                   0,      0,      1,              },
	{ "summon_flora_setting",               &battle_config.summon_flora,                    1|2,    0,      1|2,            },
	{ "clear_skills_on_death",              &battle_config.clear_unit_ondeath,              BL_NUL, BL_NUL, BL_ALL,         },
	{ "clear_skills_on_warp",               &battle_config.clear_unit_onwarp,               BL_ALL, BL_NUL, BL_ALL,         },
	{ "random_monster_checklv",             &battle_config.random_monster_checklv,          0,      0,      1,              },
	{ "attribute_recover",                  &battle_config.attr_recover,                    1,      0,      1,              },
	{ "flooritem_lifetime",                 &battle_config.flooritem_lifetime,              60000,  1000,   INT_MAX,        },
	{ "item_auto_get",                      &battle_config.item_auto_get,                   0,      0,      1,              },
	{ "item_first_get_time",                &battle_config.item_first_get_time,             3000,   0,      INT_MAX,        },
	{ "item_second_get_time",               &battle_config.item_second_get_time,            1000,   0,      INT_MAX,        },
	{ "item_third_get_time",                &battle_config.item_third_get_time,             1000,   0,      INT_MAX,        },
	{ "mvp_item_first_get_time",            &battle_config.mvp_item_first_get_time,         10000,  0,      INT_MAX,        },
	{ "mvp_item_second_get_time",           &battle_config.mvp_item_second_get_time,        10000,  0,      INT_MAX,        },
	{ "mvp_item_third_get_time",            &battle_config.mvp_item_third_get_time,         2000,   0,      INT_MAX,        },
	{ "drop_rate0item",                     &battle_config.drop_rate0item,                  0,      0,      1,              },
	{ "base_exp_rate",                      &battle_config.base_exp_rate,                   100,    0,      INT_MAX,        },
	{ "job_exp_rate",                       &battle_config.job_exp_rate,                    100,    0,      INT_MAX,        },
	{ "pvp_exp",                            &battle_config.pvp_exp,                         1,      0,      1,              },
	{ "death_penalty_type",                 &battle_config.death_penalty_type,              0,      0,      2,              },
	{ "death_penalty_base",                 &battle_config.death_penalty_base,              0,      0,      INT_MAX,        },
	{ "death_penalty_job",                  &battle_config.death_penalty_job,               0,      0,      INT_MAX,        },
	{ "zeny_penalty",                       &battle_config.zeny_penalty,                    0,      0,      INT_MAX,        },
	{ "hp_rate",                            &battle_config.hp_rate,                         100,    1,      INT_MAX,        },
	{ "sp_rate",                            &battle_config.sp_rate,                         100,    1,      INT_MAX,        },
	{ "restart_hp_rate",                    &battle_config.restart_hp_rate,                 0,      0,      100,            },
	{ "restart_sp_rate",                    &battle_config.restart_sp_rate,                 0,      0,      100,            },
	{ "guild_aura",                         &battle_config.guild_aura,                      31,     0,      31,             },
	{ "mvp_hp_rate",                        &battle_config.mvp_hp_rate,                     100,    1,      INT_MAX,        },
	{ "mvp_exp_rate",                       &battle_config.mvp_exp_rate,                    100,    0,      INT_MAX,        },
	{ "monster_hp_rate",                    &battle_config.monster_hp_rate,                 100,    1,      INT_MAX,        },
	{ "monster_max_aspd",                   &battle_config.monster_max_aspd,                199,    100,    199,            },
	{ "view_range_rate",                    &battle_config.view_range_rate,                 100,    0,      INT_MAX,        },
	{ "chase_range_rate",                   &battle_config.chase_range_rate,                100,    0,      INT_MAX,        },
	{ "gtb_sc_immunity",                    &battle_config.gtb_sc_immunity,                 50,     0,      INT_MAX,        },
	{ "guild_max_castles",                  &battle_config.guild_max_castles,               0,      0,      INT_MAX,        },
	{ "guild_skill_relog_delay",            &battle_config.guild_skill_relog_delay,         0,      0,      1,              },
	{ "emergency_call",                     &battle_config.emergency_call,                  11,     0,      31,             },
	{ "atcommand_spawn_quantity_limit",     &battle_config.atc_spawn_quantity_limit,        100,    0,      INT_MAX,        },
	{ "atcommand_slave_clone_limit",        &battle_config.atc_slave_clone_limit,           25,     0,      INT_MAX,        },
	{ "partial_name_scan",                  &battle_config.partial_name_scan,               0,      0,      1,              },
	{ "player_skillfree",                   &battle_config.skillfree,                       0,      0,      1,              },
	{ "player_skillup_limit",               &battle_config.skillup_limit,                   1,      0,      1,              },
	{ "weapon_produce_rate",                &battle_config.wp_rate,                         100,    0,      INT_MAX,        },
	{ "potion_produce_rate",                &battle_config.pp_rate,                         100,    0,      INT_MAX,        },
	{ "monster_active_enable",              &battle_config.monster_active_enable,           1,      0,      1,              },
	{ "monster_damage_delay_rate",          &battle_config.monster_damage_delay_rate,       100,    0,      INT_MAX,        },
	{ "monster_loot_type",                  &battle_config.monster_loot_type,               0,      0,      1,              },
	//{ "mob_skill_use",                      &battle_config.mob_skill_use,                   1,      0,      1,              }, //Deprecated
	{ "mob_skill_rate",                     &battle_config.mob_skill_rate,                  100,    0,      INT_MAX,        },
	{ "mob_skill_delay",                    &battle_config.mob_skill_delay,                 100,    0,      INT_MAX,        },
	{ "mob_count_rate",                     &battle_config.mob_count_rate,                  100,    0,      INT_MAX,        },
	{ "mob_spawn_delay",                    &battle_config.mob_spawn_delay,                 100,    0,      INT_MAX,        },
	{ "plant_spawn_delay",                  &battle_config.plant_spawn_delay,               100,    0,      INT_MAX,        },
	{ "boss_spawn_delay",                   &battle_config.boss_spawn_delay,                100,    0,      INT_MAX,        },
	{ "no_spawn_on_player",                 &battle_config.no_spawn_on_player,              0,      0,      100,            },
	{ "force_random_spawn",                 &battle_config.force_random_spawn,              0,      0,      1,              },
	{ "slaves_inherit_mode",                &battle_config.slaves_inherit_mode,             2,      0,      3,              },
	{ "slaves_inherit_speed",               &battle_config.slaves_inherit_speed,            3,      0,      3,              },
	{ "summons_trigger_autospells",         &battle_config.summons_trigger_autospells,      1,      0,      1,              },
	{ "pc_damage_walk_delay_rate",          &battle_config.pc_walk_delay_rate,              20,     0,      INT_MAX,        },
	{ "damage_walk_delay_rate",             &battle_config.walk_delay_rate,                 100,    0,      INT_MAX,        },
	{ "multihit_delay",                     &battle_config.multihit_delay,                  80,     0,      INT_MAX,        },
	{ "quest_skill_learn",                  &battle_config.quest_skill_learn,               0,      0,      1,              },
	{ "quest_skill_reset",                  &battle_config.quest_skill_reset,               0,      0,      1,              },
	{ "basic_skill_check",                  &battle_config.basic_skill_check,               1,      0,      1,              },
	{ "guild_emperium_check",               &battle_config.guild_emperium_check,            1,      0,      1,              },
	{ "guild_exp_limit",                    &battle_config.guild_exp_limit,                 50,     0,      99,             },
	{ "player_invincible_time",             &battle_config.pc_invincible_time,              5000,   0,      INT_MAX,        },
	{ "pet_catch_rate",                     &battle_config.pet_catch_rate,                  100,    0,      INT_MAX,        },
	{ "pet_rename",                         &battle_config.pet_rename,                      0,      0,      1,              },
	{ "pet_friendly_rate",                  &battle_config.pet_friendly_rate,               100,    0,      INT_MAX,        },
	{ "pet_hungry_delay_rate",              &battle_config.pet_hungry_delay_rate,           100,    10,     INT_MAX,        },
	{ "pet_hungry_friendly_decrease",       &battle_config.pet_hungry_friendly_decrease,    5,      0,      INT_MAX,        },
	{ "pet_status_support",                 &battle_config.pet_status_support,              0,      0,      1,              },
	{ "pet_attack_support",                 &battle_config.pet_attack_support,              0,      0,      1,              },
	{ "pet_damage_support",                 &battle_config.pet_damage_support,              0,      0,      1,              },
	{ "pet_support_min_friendly",           &battle_config.pet_support_min_friendly,        900,    0,      950,            },
	{ "pet_equip_min_friendly",             &battle_config.pet_equip_min_friendly,          900,    0,      950,            },
	{ "pet_support_rate",                   &battle_config.pet_support_rate,                100,    0,      INT_MAX,        },
	{ "pet_attack_exp_to_master",           &battle_config.pet_attack_exp_to_master,        0,      0,      1,              },
	{ "pet_attack_exp_rate",                &battle_config.pet_attack_exp_rate,             100,    0,      INT_MAX,        },
	{ "pet_lv_rate",                        &battle_config.pet_lv_rate,                     0,      0,      INT_MAX,        },
	{ "pet_max_stats",                      &battle_config.pet_max_stats,                   99,     0,      INT_MAX,        },
	{ "pet_max_atk1",                       &battle_config.pet_max_atk1,                    750,    0,      INT_MAX,        },
	{ "pet_max_atk2",                       &battle_config.pet_max_atk2,                    1000,   0,      INT_MAX,        },
	{ "pet_disable_in_gvg",                 &battle_config.pet_no_gvg,                      0,      0,      1,              },
	{ "skill_min_damage",                   &battle_config.skill_min_damage,                2|4,    0,      1|2|4,          },
	{ "finger_offensive_type",              &battle_config.finger_offensive_type,           0,      0,      1,              },
	{ "heal_exp",                           &battle_config.heal_exp,                        0,      0,      INT_MAX,        },
	{ "resurrection_exp",                   &battle_config.resurrection_exp,                0,      0,      INT_MAX,        },
	{ "shop_exp",                           &battle_config.shop_exp,                        0,      0,      INT_MAX,        },
	{ "max_heal_lv",                        &battle_config.max_heal_lv,                     11,     1,      INT_MAX,        },
	{ "max_heal",                           &battle_config.max_heal,                        9999,   0,      INT_MAX,        },
	{ "combo_delay_rate",                   &battle_config.combo_delay_rate,                100,    0,      INT_MAX,        },
	{ "item_check",                         &battle_config.item_check,                      0,      0,      1,              },
	{ "item_use_interval",                  &battle_config.item_use_interval,               100,    0,      INT_MAX,        },
	{ "cashfood_use_interval",              &battle_config.cashfood_use_interval,           60000,  0,      INT_MAX,        },
	{ "wedding_modifydisplay",              &battle_config.wedding_modifydisplay,           0,      0,      1,              },
	{ "wedding_ignorepalette",              &battle_config.wedding_ignorepalette,           0,      0,      1,              },
	{ "xmas_ignorepalette",                 &battle_config.xmas_ignorepalette,              0,      0,      1,              },
	{ "summer_ignorepalette",               &battle_config.summer_ignorepalette,            0,      0,      1,              },
	{ "hanbok_ignorepalette",               &battle_config.hanbok_ignorepalette,            0,      0,      1,              },
	{ "natural_healhp_interval",            &battle_config.natural_healhp_interval,         6000,   NATURAL_HEAL_INTERVAL, INT_MAX, },
	{ "natural_healsp_interval",            &battle_config.natural_healsp_interval,         8000,   NATURAL_HEAL_INTERVAL, INT_MAX, },
	{ "natural_heal_skill_interval",        &battle_config.natural_heal_skill_interval,     10000,  NATURAL_HEAL_INTERVAL, INT_MAX, },
	{ "natural_heal_weight_rate",           &battle_config.natural_heal_weight_rate,        50,     50,     101             },
	{ "arrow_decrement",                    &battle_config.arrow_decrement,                 1,      0,      2,              },
	{ "max_aspd",                           &battle_config.max_aspd,                        190,    100,    199,            },
	{ "max_third_aspd",                     &battle_config.max_third_aspd,                  193,    100,    199,            },
	{ "max_walk_speed",                     &battle_config.max_walk_speed,                  300,    100,    100*DEFAULT_WALK_SPEED, },
	{ "max_lv",                             &battle_config.max_lv,                          99,     0,      MAX_LEVEL,      },
	{ "aura_lv",                            &battle_config.aura_lv,                         99,     0,      INT_MAX,        },
	{ "max_hp",                             &battle_config.max_hp,                          32500,  100,    1000000000,     },
	{ "max_sp",                             &battle_config.max_sp,                          32500,  100,    1000000000,     },
	{ "max_cart_weight",                    &battle_config.max_cart_weight,                 8000,   100,    1000000,        },
	{ "max_parameter",                      &battle_config.max_parameter,                   99,     10,     10000,          },
	{ "max_baby_parameter",                 &battle_config.max_baby_parameter,              80,     10,     10000,          },
	{ "max_def",                            &battle_config.max_def,                         99,     0,      INT_MAX,        },
	{ "over_def_bonus",                     &battle_config.over_def_bonus,                  0,      0,      1000,           },
	{ "skill_log",                          &battle_config.skill_log,                       BL_NUL, BL_NUL, BL_ALL,         },
	{ "battle_log",                         &battle_config.battle_log,                      0,      0,      1,              },
	{ "etc_log",                            &battle_config.etc_log,                         1,      0,      1,              },
	{ "save_clothcolor",                    &battle_config.save_clothcolor,                 1,      0,      1,              },
	{ "undead_detect_type",                 &battle_config.undead_detect_type,              0,      0,      2,              },
	{ "auto_counter_type",                  &battle_config.auto_counter_type,               BL_ALL, BL_NUL, BL_ALL,         },
	{ "min_hitrate",                        &battle_config.min_hitrate,                     5,      0,      100,            },
	{ "max_hitrate",                        &battle_config.max_hitrate,                     100,    0,      100,            },
	{ "agi_penalty_target",                 &battle_config.agi_penalty_target,              BL_PC,  BL_NUL, BL_ALL,         },
	{ "agi_penalty_type",                   &battle_config.agi_penalty_type,                1,      0,      2,              },
	{ "agi_penalty_count",                  &battle_config.agi_penalty_count,               3,      2,      INT_MAX,        },
	{ "agi_penalty_num",                    &battle_config.agi_penalty_num,                 10,     0,      INT_MAX,        },
	{ "vit_penalty_target",                 &battle_config.vit_penalty_target,              BL_PC,  BL_NUL, BL_ALL,         },
	{ "vit_penalty_type",                   &battle_config.vit_penalty_type,                1,      0,      2,              },
	{ "vit_penalty_count",                  &battle_config.vit_penalty_count,               3,      2,      INT_MAX,        },
	{ "vit_penalty_num",                    &battle_config.vit_penalty_num,                 5,      0,      INT_MAX,        },
	{ "weapon_defense_type",                &battle_config.weapon_defense_type,             0,      0,      INT_MAX,        },
	{ "magic_defense_type",                 &battle_config.magic_defense_type,              0,      0,      INT_MAX,        },
	{ "skill_reiteration",                  &battle_config.skill_reiteration,               BL_NUL, BL_NUL, BL_ALL,         },
	{ "skill_nofootset",                    &battle_config.skill_nofootset,                 BL_PC,  BL_NUL, BL_ALL,         },
	{ "player_cloak_check_type",            &battle_config.pc_cloak_check_type,             1,      0,      1|2|4,          },
	{ "monster_cloak_check_type",           &battle_config.monster_cloak_check_type,        4,      0,      1|2|4,          },
	{ "sense_type",                         &battle_config.estimation_type,                 1|2,    0,      1|2,            },
	{ "gvg_flee_penalty",                   &battle_config.gvg_flee_penalty,                20,     0,      INT_MAX,        },
	{ "mob_changetarget_byskill",           &battle_config.mob_changetarget_byskill,        0,      0,      1,              },
	{ "attack_direction_change",            &battle_config.attack_direction_change,         BL_ALL, BL_NUL, BL_ALL,         },
	{ "land_skill_limit",                   &battle_config.land_skill_limit,                BL_ALL, BL_NUL, BL_ALL,         },
	{ "monster_class_change_full_recover",  &battle_config.monster_class_change_recover,    1,      0,      1,              },
	{ "produce_item_name_input",            &battle_config.produce_item_name_input,         0x1|0x2, 0,     0x9F,           },
	{ "display_skill_fail",                 &battle_config.display_skill_fail,              2,      0,      1|2|4|8,        },
	{ "chat_warpportal",                    &battle_config.chat_warpportal,                 0,      0,      1,              },
	{ "mob_warp",                           &battle_config.mob_warp,                        0,      0,      1|2|4,          },
	{ "dead_branch_active",                 &battle_config.dead_branch_active,              1,      0,      1,              },
	{ "vending_max_value",                  &battle_config.vending_max_value,               10000000, 1,    MAX_ZENY,       },
	{ "vending_over_max",                   &battle_config.vending_over_max,                1,      0,      1,              },
	{ "show_steal_in_same_party",           &battle_config.show_steal_in_same_party,        0,      0,      1,              },
	{ "party_hp_mode",                      &battle_config.party_hp_mode,                   0,      0,      1,              },
	{ "show_party_share_picker",            &battle_config.party_show_share_picker,         1,      0,      1,              },
	{ "show_picker.item_type",              &battle_config.show_picker_item_type,           112,    0,      INT_MAX,        },
	{ "party_update_interval",              &battle_config.party_update_interval,           1000,   100,    INT_MAX,        },
	{ "party_item_share_type",              &battle_config.party_share_type,                0,      0,      1|2|3,          },
	{ "attack_attr_none",                   &battle_config.attack_attr_none,                ~BL_PC, BL_NUL, BL_ALL,         },
	{ "gx_allhit",                          &battle_config.gx_allhit,                       0,      0,      1,              },
	{ "gx_disptype",                        &battle_config.gx_disptype,                     1,      0,      1,              },
	{ "devotion_level_difference",          &battle_config.devotion_level_difference,       10,     0,      INT_MAX,        },
	{ "player_skill_partner_check",         &battle_config.player_skill_partner_check,      1,      0,      1,              },
	{ "invite_request_check",               &battle_config.invite_request_check,            1,      0,      1,              },
	{ "skill_removetrap_type",              &battle_config.skill_removetrap_type,           0,      0,      1,              },
	{ "disp_experience",                    &battle_config.disp_experience,                 0,      0,      1,              },
	{ "disp_zeny",                          &battle_config.disp_zeny,                       0,      0,      1,              },
	{ "castle_defense_rate",                &battle_config.castle_defense_rate,             100,    0,      100,            },
	{ "bone_drop",                          &battle_config.bone_drop,                       0,      0,      2,              },
	{ "buyer_name",                         &battle_config.buyer_name,                      1,      0,      1,              },
	{ "skill_wall_check",                   &battle_config.skill_wall_check,                1,      0,      1,              },
	{ "official_cell_stack_limit",          &battle_config.official_cell_stack_limit,       1,      1,      255,            },
	{ "custom_cell_stack_limit",            &battle_config.custom_cell_stack_limit,         1,      1,      255,            },
	{ "dancing_weaponswitch_fix",           &battle_config.dancing_weaponswitch_fix,        1,      0,      1,              },

// eAthena additions
	{ "item_logarithmic_drops",             &battle_config.logarithmic_drops,               0,      0,      1,              },
	{ "item_drop_common_min",               &battle_config.item_drop_common_min,            1,      1,      10000,          },
	{ "item_drop_common_max",               &battle_config.item_drop_common_max,            10000,  1,      10000,          },
	{ "item_drop_equip_min",                &battle_config.item_drop_equip_min,             1,      1,      10000,          },
	{ "item_drop_equip_max",                &battle_config.item_drop_equip_max,             10000,  1,      10000,          },
	{ "item_drop_card_min",                 &battle_config.item_drop_card_min,              1,      1,      10000,          },
	{ "item_drop_card_max",                 &battle_config.item_drop_card_max,              10000,  1,      10000,          },
	{ "item_drop_mvp_min",                  &battle_config.item_drop_mvp_min,               1,      1,      10000,          },
	{ "item_drop_mvp_max",                  &battle_config.item_drop_mvp_max,               10000,  1,      10000,          },
	{ "item_drop_heal_min",                 &battle_config.item_drop_heal_min,              1,      1,      10000,          },
	{ "item_drop_heal_max",                 &battle_config.item_drop_heal_max,              10000,  1,      10000,          },
	{ "item_drop_use_min",                  &battle_config.item_drop_use_min,               1,      1,      10000,          },
	{ "item_drop_use_max",                  &battle_config.item_drop_use_max,               10000,  1,      10000,          },
	{ "item_drop_add_min",                  &battle_config.item_drop_adddrop_min,           1,      1,      10000,          },
	{ "item_drop_add_max",                  &battle_config.item_drop_adddrop_max,           10000,  1,      10000,          },
	{ "item_drop_treasure_min",             &battle_config.item_drop_treasure_min,          1,      1,      10000,          },
	{ "item_drop_treasure_max",             &battle_config.item_drop_treasure_max,          10000,  1,      10000,          },
	{ "item_rate_mvp",                      &battle_config.item_rate_mvp,                   100,    0,      1000000,        },
	{ "item_rate_common",                   &battle_config.item_rate_common,                100,    0,      1000000,        },
	{ "item_rate_common_boss",              &battle_config.item_rate_common_boss,           100,    0,      1000000,        },
	{ "item_rate_equip",                    &battle_config.item_rate_equip,                 100,    0,      1000000,        },
	{ "item_rate_equip_boss",               &battle_config.item_rate_equip_boss,            100,    0,      1000000,        },
	{ "item_rate_card",                     &battle_config.item_rate_card,                  100,    0,      1000000,        },
	{ "item_rate_card_boss",                &battle_config.item_rate_card_boss,             100,    0,      1000000,        },
	{ "item_rate_heal",                     &battle_config.item_rate_heal,                  100,    0,      1000000,        },
	{ "item_rate_heal_boss",                &battle_config.item_rate_heal_boss,             100,    0,      1000000,        },
	{ "item_rate_use",                      &battle_config.item_rate_use,                   100,    0,      1000000,        },
	{ "item_rate_use_boss",                 &battle_config.item_rate_use_boss,              100,    0,      1000000,        },
	{ "item_rate_adddrop",                  &battle_config.item_rate_adddrop,               100,    0,      1000000,        },
	{ "item_rate_treasure",                 &battle_config.item_rate_treasure,              100,    0,      1000000,        },
	{ "prevent_logout",                     &battle_config.prevent_logout,                  10000,  0,      60000,          },
	{ "alchemist_summon_reward",            &battle_config.alchemist_summon_reward,         1,      0,      2,              },
	{ "drops_by_luk",                       &battle_config.drops_by_luk,                    0,      0,      INT_MAX,        },
	{ "drops_by_luk2",                      &battle_config.drops_by_luk2,                   0,      0,      INT_MAX,        },
	{ "equip_natural_break_rate",           &battle_config.equip_natural_break_rate,        0,      0,      INT_MAX,        },
	{ "equip_self_break_rate",              &battle_config.equip_self_break_rate,           100,    0,      INT_MAX,        },
	{ "equip_skill_break_rate",             &battle_config.equip_skill_break_rate,          100,    0,      INT_MAX,        },
	{ "pk_mode",                            &battle_config.pk_mode,                         0,      0,      2,              },
	{ "pk_level_range",                     &battle_config.pk_level_range,                  0,      0,      INT_MAX,        },
	{ "manner_system",                      &battle_config.manner_system,                   0xFFF,  0,      0xFFF,          },
	{ "pet_equip_required",                 &battle_config.pet_equip_required,              0,      0,      1,              },
	{ "multi_level_up",                     &battle_config.multi_level_up,                  0,      0,      1,              },
	{ "max_exp_gain_rate",                  &battle_config.max_exp_gain_rate,               0,      0,      INT_MAX,        },
	{ "backstab_bow_penalty",               &battle_config.backstab_bow_penalty,            0,      0,      1,              },
	{ "night_at_start",                     &battle_config.night_at_start,                  0,      0,      1,              },
	{ "show_mob_info",                      &battle_config.show_mob_info,                   0,      0,      1|2|4,          },
	{ "ban_hack_trade",                     &battle_config.ban_hack_trade,                  0,      0,      INT_MAX,        },
	{ "min_hair_style",                     &battle_config.min_hair_style,                  0,      0,      INT_MAX,        },
	{ "max_hair_style",                     &battle_config.max_hair_style,                  23,     0,      INT_MAX,        },
	{ "min_hair_color",                     &battle_config.min_hair_color,                  0,      0,      INT_MAX,        },
	{ "max_hair_color",                     &battle_config.max_hair_color,                  9,      0,      INT_MAX,        },
	{ "min_cloth_color",                    &battle_config.min_cloth_color,                 0,      0,      INT_MAX,        },
	{ "max_cloth_color",                    &battle_config.max_cloth_color,                 4,      0,      INT_MAX,        },
	{ "pet_hair_style",                     &battle_config.pet_hair_style,                  100,    0,      INT_MAX,        },
	{ "castrate_dex_scale",                 &battle_config.castrate_dex_scale,              150,    1,      INT_MAX,        },
	{ "vcast_stat_scale",                   &battle_config.vcast_stat_scale,                530,    1,      INT_MAX,        },
	{ "area_size",                          &battle_config.area_size,                       14,     0,      INT_MAX,        },
	{ "zeny_from_mobs",                     &battle_config.zeny_from_mobs,                  0,      0,      1,              },
	{ "mobs_level_up",                      &battle_config.mobs_level_up,                   0,      0,      1,              },
	{ "mobs_level_up_exp_rate",             &battle_config.mobs_level_up_exp_rate,          1,      1,      INT_MAX,        },
	{ "pk_min_level",                       &battle_config.pk_min_level,                    55,     1,      INT_MAX,        },
	{ "skill_steal_max_tries",              &battle_config.skill_steal_max_tries,           0,      0,      UCHAR_MAX,      },
	{ "exp_calc_type",                      &battle_config.exp_calc_type,                   0,      0,      1,              },
	{ "exp_bonus_attacker",                 &battle_config.exp_bonus_attacker,              25,     0,      INT_MAX,        },
	{ "exp_bonus_max_attacker",             &battle_config.exp_bonus_max_attacker,          12,     2,      INT_MAX,        },
	{ "min_skill_delay_limit",              &battle_config.min_skill_delay_limit,           100,    10,     INT_MAX,        },
	{ "default_walk_delay",                 &battle_config.default_walk_delay,              300,    0,      INT_MAX,        },
	{ "no_skill_delay",                     &battle_config.no_skill_delay,                  BL_MOB, BL_NUL, BL_ALL,         },
	{ "attack_walk_delay",                  &battle_config.attack_walk_delay,               BL_ALL, BL_NUL, BL_ALL,         },
	{ "require_glory_guild",                &battle_config.require_glory_guild,             0,      0,      1,              },
	{ "idle_no_share",                      &battle_config.idle_no_share,                   0,      0,      INT_MAX,        },
	{ "party_even_share_bonus",             &battle_config.party_even_share_bonus,          0,      0,      INT_MAX,        },
	{ "delay_battle_damage",                &battle_config.delay_battle_damage,             1,      0,      1,              },
	{ "hide_woe_damage",                    &battle_config.hide_woe_damage,                 0,      0,      1,              },
	{ "display_version",                    &battle_config.display_version,                 1,      0,      1,              },
	{ "display_hallucination",              &battle_config.display_hallucination,           1,      0,      1,              },
	{ "use_statpoint_table",                &battle_config.use_statpoint_table,             1,      0,      1,              },
	{ "ignore_items_gender",                &battle_config.ignore_items_gender,             1,      0,      1,              },
	{ "copyskill_restrict",                 &battle_config.copyskill_restrict,              2,      0,      2,              },
	{ "berserk_cancels_buffs",              &battle_config.berserk_cancels_buffs,           0,      0,      1,              },
	{ "monster_ai",                         &battle_config.mob_ai,                          0x000,  0x000,  0x77F,          },
	{ "hom_setting",                        &battle_config.hom_setting,                     0xFFFF, 0x0000, 0xFFFF,         },
	{ "dynamic_mobs",                       &battle_config.dynamic_mobs,                    1,      0,      1,              },
	{ "mob_remove_damaged",                 &battle_config.mob_remove_damaged,              1,      0,      1,              },
	{ "show_hp_sp_drain",                   &battle_config.show_hp_sp_drain,                0,      0,      1,              },
	{ "show_hp_sp_gain",                    &battle_config.show_hp_sp_gain,                 1,      0,      1,              },
	{ "mob_npc_event_type",                 &battle_config.mob_npc_event_type,              1,      0,      1,              },
	{ "character_size",                     &battle_config.character_size,                  1|2,    0,      1|2,            },
	{ "retaliate_to_master",                &battle_config.retaliate_to_master,             1,      0,      1,              },
	{ "rare_drop_announce",                 &battle_config.rare_drop_announce,              0,      0,      10000,          },
	{ "duel_allow_pvp",                     &battle_config.duel_allow_pvp,                  0,      0,      1,              },
	{ "duel_allow_gvg",                     &battle_config.duel_allow_gvg,                  0,      0,      1,              },
	{ "duel_allow_teleport",                &battle_config.duel_allow_teleport,             0,      0,      1,              },
	{ "duel_autoleave_when_die",            &battle_config.duel_autoleave_when_die,         1,      0,      1,              },
	{ "duel_time_interval",                 &battle_config.duel_time_interval,              60,     0,      INT_MAX,        },
	{ "duel_only_on_same_map",              &battle_config.duel_only_on_same_map,           0,      0,      1,              },
	{ "skip_teleport_lv1_menu",             &battle_config.skip_teleport_lv1_menu,          0,      0,      1,              },
	{ "mob_max_skilllvl",                   &battle_config.mob_max_skilllvl,                100,    1,      INT_MAX,        },
	{ "allow_skill_without_day",            &battle_config.allow_skill_without_day,         0,      0,      1,              },
	{ "allow_es_magic_player",              &battle_config.allow_es_magic_pc,               0,      0,      1,              },
	{ "skill_caster_check",                 &battle_config.skill_caster_check,              1,      0,      1,              },
	{ "status_cast_cancel",                 &battle_config.sc_castcancel,                   BL_NUL, BL_NUL, BL_ALL,         },
	{ "pc_status_def_rate",                 &battle_config.pc_sc_def_rate,                  100,    0,      INT_MAX,        },
	{ "mob_status_def_rate",                &battle_config.mob_sc_def_rate,                 100,    0,      INT_MAX,        },
	{ "pc_max_status_def",                  &battle_config.pc_max_sc_def,                   100,    0,      INT_MAX,        },
	{ "mob_max_status_def",                 &battle_config.mob_max_sc_def,                  100,    0,      INT_MAX,        },
	{ "sg_miracle_skill_ratio",             &battle_config.sg_miracle_skill_ratio,          1,      0,      10000,          },
	{ "sg_angel_skill_ratio",               &battle_config.sg_angel_skill_ratio,            10,     0,      10000,          },
	{ "autospell_stacking",                 &battle_config.autospell_stacking,              0,      0,      1,              },
	{ "override_mob_names",                 &battle_config.override_mob_names,              0,      0,      2,              },
	{ "min_chat_delay",                     &battle_config.min_chat_delay,                  0,      0,      INT_MAX,        },
	{ "friend_auto_add",                    &battle_config.friend_auto_add,                 1,      0,      1,              },
	{ "hom_rename",                         &battle_config.hom_rename,                      0,      0,      1,              },
	{ "homunculus_show_growth",             &battle_config.homunculus_show_growth,          0,      0,      1,              },
	{ "homunculus_friendly_rate",           &battle_config.homunculus_friendly_rate,        100,    0,      INT_MAX,        },
	{ "vending_tax",                        &battle_config.vending_tax,                     0,      0,      10000,          },
	{ "day_duration",                       &battle_config.day_duration,                    0,      0,      INT_MAX,        },
	{ "night_duration",                     &battle_config.night_duration,                  0,      0,      INT_MAX,        },
	{ "mob_remove_delay",                   &battle_config.mob_remove_delay,                60000,  1000,   INT_MAX,        },
	{ "mob_active_time",                    &battle_config.mob_active_time,                 0,      0,      INT_MAX,        },
	{ "boss_active_time",                   &battle_config.boss_active_time,                0,      0,      INT_MAX,        },
	{ "sg_miracle_skill_duration",          &battle_config.sg_miracle_skill_duration,       3600000, 0,     INT_MAX,        },
	{ "hvan_explosion_intimate",            &battle_config.hvan_explosion_intimate,         45000,  0,      100000,         },
	{ "quest_exp_rate",                     &battle_config.quest_exp_rate,                  100,    0,      INT_MAX,        },
	{ "at_mapflag",                         &battle_config.autotrade_mapflag,               0,      0,      1,              },
	{ "at_timeout",                         &battle_config.at_timeout,                      0,      0,      INT_MAX,        },
	{ "homunculus_autoloot",                &battle_config.homunculus_autoloot,             0,      0,      1,              },
	{ "idle_no_autoloot",                   &battle_config.idle_no_autoloot,                0,      0,      INT_MAX,        },
	{ "max_guild_alliance",                 &battle_config.max_guild_alliance,              3,      0,      3,              },
	{ "ksprotection",                       &battle_config.ksprotection,                    5000,   0,      INT_MAX,        },
	{ "auction_feeperhour",                 &battle_config.auction_feeperhour,              12000,  0,      INT_MAX,        },
	{ "auction_maximumprice",               &battle_config.auction_maximumprice,            500000000, 0,   MAX_ZENY,       },
	{ "homunculus_auto_vapor",              &battle_config.homunculus_auto_vapor,           1,      0,      1,              },
	{ "display_status_timers",              &battle_config.display_status_timers,           1,      0,      1,              },
	{ "skill_add_heal_rate",                &battle_config.skill_add_heal_rate,             7,      0,      INT_MAX,        },
	{ "eq_single_target_reflectable",       &battle_config.eq_single_target_reflectable,    1,      0,      1,              },
	{ "invincible.nodamage",                &battle_config.invincible_nodamage,             0,      0,      1,              },
	{ "mob_slave_keep_target",              &battle_config.mob_slave_keep_target,           0,      0,      1,              },
	{ "autospell_check_range",              &battle_config.autospell_check_range,           0,      0,      1,              },
	{ "knockback_left",                     &battle_config.knockback_left,                  1,      0,      1,              },
	{ "client_reshuffle_dice",              &battle_config.client_reshuffle_dice,           0,      0,      1,              },
	{ "client_sort_storage",                &battle_config.client_sort_storage,             0,      0,      1,              },
	{ "feature.buying_store",               &battle_config.feature_buying_store,            1,      0,      1,              },
	{ "feature.search_stores",              &battle_config.feature_search_stores,           1,      0,      1,              },
	{ "searchstore_querydelay",             &battle_config.searchstore_querydelay,         10,      0,      INT_MAX,        },
	{ "searchstore_maxresults",             &battle_config.searchstore_maxresults,         30,      1,      INT_MAX,        },
	{ "display_party_name",                 &battle_config.display_party_name,              0,      0,      1,              },
	{ "cashshop_show_points",               &battle_config.cashshop_show_points,            0,      0,      1,              },
	{ "mail_show_status",                   &battle_config.mail_show_status,                0,      0,      2,              },
	{ "client_limit_unit_lv",               &battle_config.client_limit_unit_lv,            0,      0,      BL_ALL,         },
	{ "client_emblem_max_blank_percent",    &battle_config.client_emblem_max_blank_percent, 100,    0,      100,            },

	// BattleGround Settings
	{ "bg_update_interval",                 &battle_config.bg_update_interval,              1000,   100,    INT_MAX,        },
	{ "bg_flee_penalty",                    &battle_config.bg_flee_penalty,                 20,     0,      INT_MAX,        },
	/**
	 * rAthena
	 **/
	{ "max_third_parameter",                &battle_config.max_third_parameter,             130,    10,     10000,          },
	{ "max_baby_third_parameter",           &battle_config.max_baby_third_parameter,        117,    10,     10000,          },
	{ "max_extended_parameter",             &battle_config.max_extended_parameter,          125,    10,     10000,          },
	{ "atcommand_max_stat_bypass",          &battle_config.atcommand_max_stat_bypass,       0,      0,      100,            },
	{ "skill_amotion_leniency",             &battle_config.skill_amotion_leniency,          90,     0,      300             },
	{ "mvp_tomb_enabled",                   &battle_config.mvp_tomb_enabled,                1,      0,      1               },
	{ "feature.atcommand_suggestions",      &battle_config.atcommand_suggestions_enabled,   0,      0,      1               },
	{ "min_npc_vendchat_distance",          &battle_config.min_npc_vendchat_distance,       3,      0,      100             },
	{ "atcommand_mobinfo_type",             &battle_config.atcommand_mobinfo_type,          0,      0,      1               },
	{ "homunculus_max_level",               &battle_config.hom_max_level,                   99,     0,      MAX_LEVEL,      },
	{ "homunculus_S_max_level",             &battle_config.hom_S_max_level,                 150,    0,      MAX_LEVEL,      },
	{ "mob_size_influence",                 &battle_config.mob_size_influence,              0,      0,      1,              },
	{ "bowling_bash_area",                  &battle_config.bowling_bash_area,               0,      0,      20,             },
	/**
	 * Hercules
	 **/
	{ "skill_trap_type",                    &battle_config.skill_trap_type,                 0,      0,      1,              },
	{ "item_restricted_consumption_type",   &battle_config.item_restricted_consumption_type,1,      0,      1,              },
	{ "unequip_restricted_equipment",       &battle_config.unequip_restricted_equipment,    0,      0,      3,              },
	{ "max_walk_path",                      &battle_config.max_walk_path,                   17,     1,      MAX_WALKPATH,   },
	{ "item_enabled_npc",                   &battle_config.item_enabled_npc,                1,      0,      1,              },
	{ "gm_ignore_warpable_area",            &battle_config.gm_ignore_warpable_area,         0,      2,      100,            },
	{ "packet_obfuscation",                 &battle_config.packet_obfuscation,              1,      0,      3,              },
	{ "client_accept_chatdori",             &battle_config.client_accept_chatdori,          0,      0,      INT_MAX,        },
	{ "snovice_call_type",                  &battle_config.snovice_call_type,               0,      0,      1,              },
	{ "guild_notice_changemap",             &battle_config.guild_notice_changemap,          2,      0,      2,              },
	{ "feature.banking",                    &battle_config.feature_banking,                 1,      0,      1,              },
	{ "feature.auction",                    &battle_config.feature_auction,                 0,      0,      2,              },
	{ "idletime_criteria",                  &battle_config.idletime_criteria,            0x25,      1,      INT_MAX,        },

	{ "mon_trans_disable_in_gvg",           &battle_config.mon_trans_disable_in_gvg,        0,      0,      1,              },
	{ "case_sensitive_aegisnames",          &battle_config.case_sensitive_aegisnames,       1,      0,      1,              },
	{ "guild_castle_invite",                &battle_config.guild_castle_invite,             0,      0,      1,              },
	{ "guild_castle_expulsion",             &battle_config.guild_castle_expulsion,          0,      0,      1,              },
	{ "song_timer_reset",                   &battle_config.song_timer_reset,                0,      0,      1,              },
	{ "snap_dodge",                         &battle_config.snap_dodge,                      0,      0,      1,              },
	{ "stormgust_knockback",                &battle_config.stormgust_knockback,             1,      0,      1,              },
	{ "monster_chase_refresh",              &battle_config.mob_chase_refresh,               1,      0,      30,             },
	{ "mob_icewall_walk_block",             &battle_config.mob_icewall_walk_block,          75,     0,      255,            },
	{ "boss_icewall_walk_block",            &battle_config.boss_icewall_walk_block,         0,      0,      255,            },
	{ "feature.roulette",                   &battle_config.feature_roulette,                1,      0,      1,              },
};
#ifndef STATS_OPT_OUT
/**
 * Hercules anonymous statistic usage report -- packet is built here, and sent to char server to report.
 **/
void Hercules_report(char* date, char *time_c) {
	int i, bd_size = ARRAYLENGTH(battle_data);
	unsigned int config = 0;
	char timestring[25];
	time_t curtime;
	char* buf;

	enum config_table {
		C_CIRCULAR_AREA         = 0x0001,
		C_CELLNOSTACK           = 0x0002,
		C_CONSOLE_INPUT         = 0x0004,
		C_SCRIPT_CALLFUNC_CHECK = 0x0008,
		C_OFFICIAL_WALKPATH     = 0x0010,
		C_RENEWAL               = 0x0020,
		C_RENEWAL_CAST          = 0x0040,
		C_RENEWAL_DROP          = 0x0080,
		C_RENEWAL_EXP           = 0x0100,
		C_RENEWAL_LVDMG         = 0x0200,
		C_RENEWAL_EDP           = 0x0400,
		C_RENEWAL_ASPD          = 0x0800,
		C_SECURE_NPCTIMEOUT     = 0x1000,
		C_SQL_DB_ITEM           = 0x2000,
		C_SQL_LOGS              = 0x4000,
		C_MEMWATCH              = 0x8000,
		C_DMALLOC               = 0x10000,
		C_GCOLLECT              = 0x20000,
		C_SEND_SHORTLIST        = 0x40000,
		C_SQL_DB_MOB            = 0x80000,
		C_SQL_DB_MOBSKILL       = 0x100000,
		C_PACKETVER_RE          = 0x200000,
	};

	/* we get the current time */
	time(&curtime);
	strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", localtime(&curtime));

#ifdef CIRCULAR_AREA
	config |= C_CIRCULAR_AREA;
#endif

#ifdef CELL_NOSTACK
	config |= C_CELLNOSTACK;
#endif

#ifdef CONSOLE_INPUT
	config |= C_CONSOLE_INPUT;
#endif

#ifdef SCRIPT_CALLFUNC_CHECK
	config |= C_SCRIPT_CALLFUNC_CHECK;
#endif

#ifdef OFFICIAL_WALKPATH
	config |= C_OFFICIAL_WALKPATH;
#endif

#ifdef RENEWAL
	config |= C_RENEWAL;
#endif

#ifdef RENEWAL_CAST
	config |= C_RENEWAL_CAST;
#endif

#ifdef RENEWAL_DROP
	config |= C_RENEWAL_DROP;
#endif

#ifdef RENEWAL_EXP
	config |= C_RENEWAL_EXP;
#endif

#ifdef RENEWAL_LVDMG
	config |= C_RENEWAL_LVDMG;
#endif

#ifdef RENEWAL_EDP
	config |= C_RENEWAL_EDP;
#endif

#ifdef RENEWAL_ASPD
	config |= C_RENEWAL_ASPD;
#endif

#ifdef SECURE_NPCTIMEOUT
	config |= C_SECURE_NPCTIMEOUT;
#endif

#ifdef PACKETVER_RE
	config |= C_PACKETVER_RE;
#endif

	/* non-define part */
	if( map->db_use_sql_item_db )
		config |= C_SQL_DB_ITEM;
	if( map->db_use_sql_mob_db )
		config |= C_SQL_DB_MOB;
	if( map->db_use_sql_mob_skill_db )
		config |= C_SQL_DB_MOBSKILL;

	if( logs->config.sql_logs )
		config |= C_SQL_LOGS;

#ifdef MEMWATCH
	config |= C_MEMWATCH;
#endif
#ifdef DMALLOC
	config |= C_DMALLOC;
#endif
#ifdef GCOLLECT
	config |= C_GCOLLECT;
#endif

#ifdef SEND_SHORTLIST
	config |= C_SEND_SHORTLIST;
#endif

#define BFLAG_LENGTH 35

	CREATE(buf, char, 262 + ( bd_size * ( BFLAG_LENGTH + 4 ) ) + 1 );

	/* build packet */

	WBUFW(buf,0) = 0x3000;
	WBUFW(buf,2) = 262 + ( bd_size * ( BFLAG_LENGTH + 4 ) );
	WBUFW(buf,4) = 0x9f;

	safestrncpy((char*)WBUFP(buf,6), date, 12);
	safestrncpy((char*)WBUFP(buf,18), time_c, 9);
	safestrncpy((char*)WBUFP(buf,27), timestring, 24);

	safestrncpy((char*)WBUFP(buf,51), sysinfo->platform(), 16);
	safestrncpy((char*)WBUFP(buf,67), sysinfo->osversion(), 50);
	safestrncpy((char*)WBUFP(buf,117), sysinfo->cpu(), 32);
	WBUFL(buf,149) = sysinfo->cpucores();
	safestrncpy((char*)WBUFP(buf,153), sysinfo->arch(), 8);
	WBUFB(buf,161) = sysinfo->vcstypeid();
	WBUFB(buf,162) = sysinfo->is64bit();
	safestrncpy((char*)WBUFP(buf,163), sysinfo->vcsrevision_src(), 41);
	safestrncpy((char*)WBUFP(buf,204), sysinfo->vcsrevision_scripts(), 41);
	WBUFB(buf,245) = (sysinfo->is_superuser()? 1 : 0);
	WBUFL(buf,246) = map->getusers();

	WBUFL(buf,250) = config;
	WBUFL(buf,254) = PACKETVER;

	WBUFL(buf,258) = bd_size;
	for( i = 0; i < bd_size; i++ ) {
		safestrncpy((char*)WBUFP(buf,262 + ( i * ( BFLAG_LENGTH + 4 ) ) ), battle_data[i].str, BFLAG_LENGTH);
		WBUFL(buf,262 + BFLAG_LENGTH + ( i * ( BFLAG_LENGTH + 4 )  )  ) = *battle_data[i].val;
	}

	chrif->send_report(buf, 262 + ( bd_size * ( BFLAG_LENGTH + 4 ) ) );

	aFree(buf);

#undef BFLAG_LENGTH
}
static int Hercules_report_timer(int tid, int64 tick, int id, intptr_t data) {
	if( chrif->isconnected() ) {/* char server relays it, so it must be online. */
		Hercules_report(__DATE__,__TIME__);
	}
	return 0;
}
#endif

int battle_set_value(const char* w1, const char* w2)
{
	int val = config_switch(w2);

	int i;
	ARR_FIND(0, ARRAYLENGTH(battle_data), i, strcmpi(w1, battle_data[i].str) == 0);
	if (i == ARRAYLENGTH(battle_data)) {
		if( HPM->parseConf(w1,w2,HPCT_BATTLE) ) /* if plugin-owned, succeed */
			return 1;
		return 0; // not found
	}

	if (val < battle_data[i].min || val > battle_data[i].max)
	{
		ShowWarning("Value for setting '%s': %s is invalid (min:%i max:%i)! Defaulting to %i...\n", w1, w2, battle_data[i].min, battle_data[i].max, battle_data[i].defval);
		val = battle_data[i].defval;
	}

	*battle_data[i].val = val;
	return 1;
}

int battle_get_value(const char* w1)
{
	int i;
	ARR_FIND(0, ARRAYLENGTH(battle_data), i, strcmpi(w1, battle_data[i].str) == 0);
	if (i == ARRAYLENGTH(battle_data))
		return 0; // not found
	else
		return *battle_data[i].val;
}

void battle_set_defaults(void) {
	int i;
	for (i = 0; i < ARRAYLENGTH(battle_data); i++)
		*battle_data[i].val = battle_data[i].defval;
}

void battle_adjust_conf(void) {
	battle_config.monster_max_aspd = 2000 - battle_config.monster_max_aspd*10;
	battle_config.max_aspd = 2000 - battle_config.max_aspd*10;
	battle_config.max_third_aspd = 2000 - battle_config.max_third_aspd*10;
	battle_config.max_walk_speed = 100*DEFAULT_WALK_SPEED/battle_config.max_walk_speed;
	battle_config.max_cart_weight *= 10;

	if(battle_config.max_def > 100 && !battle_config.weapon_defense_type) // added by [Skotlex]
		battle_config.max_def = 100;

	if(battle_config.min_hitrate > battle_config.max_hitrate)
		battle_config.min_hitrate = battle_config.max_hitrate;

	if(battle_config.pet_max_atk1 > battle_config.pet_max_atk2) //Skotlex
		battle_config.pet_max_atk1 = battle_config.pet_max_atk2;

	if (battle_config.day_duration && battle_config.day_duration < 60000) // added by [Yor]
		battle_config.day_duration = 60000;
	if (battle_config.night_duration && battle_config.night_duration < 60000) // added by [Yor]
		battle_config.night_duration = 60000;

#if PACKETVER < 20100427
	if( battle_config.feature_buying_store ) {
		ShowWarning("conf/battle/feature.conf buying_store is enabled but it requires PACKETVER 2010-04-27 or newer, disabling...\n");
		battle_config.feature_buying_store = 0;
	}
#endif

#if PACKETVER < 20100803
	if( battle_config.feature_search_stores ) {
		ShowWarning("conf/battle/feature.conf search_stores is enabled but it requires PACKETVER 2010-08-03 or newer, disabling...\n");
		battle_config.feature_search_stores = 0;
	}
#endif

#if PACKETVER < 20130724
	if( battle_config.feature_banking ) {
		ShowWarning("conf/battle/feature.conf banking is enabled but it requires PACKETVER 2013-07-24 or newer, disabling...\n");
		battle_config.feature_banking = 0;
	}
#endif

#if PACKETVER < 20141022
	if( battle_config.feature_roulette ) {
		ShowWarning("conf/battle/feature.conf roulette is enabled but it requires PACKETVER 2014-10-22 or newer, disabling...\n");
		battle_config.feature_roulette = 0;
	}
#endif

#if PACKETVER > 20120000 && PACKETVER < 20130515 /* exact date (when it started) not known */
	if( battle_config.feature_auction == 1 ) {
		ShowWarning("conf/battle/feature.conf:feature.auction is enabled but it is not stable on PACKETVER "EXPAND_AND_QUOTE(PACKETVER)", disabling...\n");
		ShowWarning("conf/battle/feature.conf:feature.auction change value to '2' to silence this warning and maintain it enabled\n");
		battle_config.feature_auction = 0;
	}
#endif


#ifndef CELL_NOSTACK
	if (battle_config.custom_cell_stack_limit != 1)
		ShowWarning("Battle setting 'custom_cell_stack_limit' takes no effect as this server was compiled without Cell Stack Limit support.\n");
#endif
}

int battle_config_read(const char* cfgName)
{
	FILE* fp;
	static int count = 0;

	if (count == 0)
		battle->config_set_defaults();

	count++;

	fp = fopen(cfgName,"r");
	if (fp == NULL)
		ShowError("File not found: %s\n", cfgName);
	else
	{
		char line[1024], w1[1024], w2[1024];
		while(fgets(line, sizeof(line), fp))
		{
			if (line[0] == '/' && line[1] == '/')
				continue;
			if (sscanf(line, "%1023[^:]:%1023s", w1, w2) != 2)
				continue;
			if (strcmpi(w1, "import") == 0)
				battle->config_read(w2);
			else
			if (battle->config_set_value(w1, w2) == 0)
				ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
		}

		fclose(fp);
	}

	count--;

	if (count == 0) {
		battle->config_adjust();
		clif->bc_ready();
	}

	return 0;
}

void do_init_battle(bool minimal) {
	if (minimal)
		return;

	battle->delay_damage_ers = ers_new(sizeof(struct delay_damage),"battle.c::delay_damage_ers",ERS_OPT_CLEAR);
	timer->add_func_list(battle->delay_damage_sub, "battle_delay_damage_sub");

#ifndef STATS_OPT_OUT
	timer->add_func_list(Hercules_report_timer, "Hercules_report_timer");
	timer->add_interval(timer->gettick()+30000, Hercules_report_timer, 0, 0, 60000 * 30);
#endif

}

void do_final_battle(void) {
	ers_destroy(battle->delay_damage_ers);
}

/* initialize the interface */
void battle_defaults(void) {
	battle = &battle_s;

	battle->bc = &battle_config;

	memset(battle->attr_fix_table, 0, sizeof(battle->attr_fix_table));
	battle->delay_damage_ers = NULL;

	battle->init = do_init_battle;
	battle->final = do_final_battle;

	battle->calc_attack = battle_calc_attack;
	battle->calc_damage = battle_calc_damage;
	battle->calc_gvg_damage = battle_calc_gvg_damage;
	battle->calc_bg_damage = battle_calc_bg_damage;
	battle->weapon_attack = battle_weapon_attack;
	battle->calc_weapon_attack = battle_calc_weapon_attack;
	battle->delay_damage = battle_delay_damage;
	battle->drain = battle_drain;
	battle->reflect_damage = battle_reflect_damage;
	battle->attr_ratio = battle_attr_ratio;
	battle->attr_fix = battle_attr_fix;
	battle->calc_cardfix = battle_calc_cardfix;
	battle->calc_elefix = battle_calc_elefix;
	battle->calc_masteryfix = battle_calc_masteryfix;
	battle->calc_chorusbonus = battle_calc_chorusbonus;
	battle->calc_skillratio = battle_calc_skillratio;
	battle->calc_sizefix = battle_calc_sizefix;
	battle->calc_weapon_damage = battle_calc_weapon_damage;
	battle->calc_defense = battle_calc_defense;
	battle->get_master = battle_get_master;
	battle->get_targeted = battle_gettargeted;
	battle->get_enemy = battle_getenemy;
	battle->get_target = battle_gettarget;
	battle->get_current_skill = battle_getcurrentskill;
	battle->check_undead = battle_check_undead;
	battle->check_target = battle_check_target;
	battle->check_range = battle_check_range;
	battle->consume_ammo = battle_consume_ammo;
	battle->get_targeted_sub = battle_gettargeted_sub;
	battle->get_enemy_sub = battle_getenemy_sub;
	battle->get_enemy_area_sub = battle_getenemyarea_sub;
	battle->delay_damage_sub = battle_delay_damage_sub;
	battle->blewcount_bonus = battle_blewcount_bonus;
	battle->range_type = battle_range_type;
	battle->calc_base_damage = battle_calc_base_damage;
	battle->calc_base_damage2 = battle_calc_base_damage2;
	battle->calc_misc_attack = battle_calc_misc_attack;
	battle->calc_magic_attack = battle_calc_magic_attack;
	battle->adjust_skill_damage = battle_adjust_skill_damage;
	battle->add_mastery = battle_addmastery;
	battle->calc_drain = battle_calc_drain;
	battle->config_read = battle_config_read;
	battle->config_set_defaults = battle_set_defaults;
	battle->config_set_value = battle_set_value;
	battle->config_get_value = battle_get_value;
	battle->config_adjust = battle_adjust_conf;
	battle->get_enemy_area = battle_getenemyarea;
	battle->damage_area = battle_damage_area;
	battle->calc_masteryfix_unknown = battle_calc_masteryfix_unknown;
	battle->calc_skillratio_magic_unknown = battle_calc_skillratio_magic_unknown;
	battle->calc_skillratio_weapon_unknown = battle_calc_skillratio_weapon_unknown;
	battle->calc_misc_attack_unknown = battle_calc_misc_attack_unknown;
}