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
|
// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
// For more information, see LICENCE in the main folder
#include "../common/cbasetypes.h"
#include "../common/timer.h"
#include "../common/db.h"
#include "../common/nullpo.h"
#include "../common/malloc.h"
#include "../common/showmsg.h"
#include "../common/ers.h"
#include "../common/strlib.h"
#include "../common/utils.h"
#include "../common/socket.h"
#include "map.h"
#include "path.h"
#include "clif.h"
#include "intif.h"
#include "pc.h"
#include "pet.h"
#include "status.h"
#include "mob.h"
#include "mercenary.h" //[orn]
#include "guild.h"
#include "itemdb.h"
#include "skill.h"
#include "battle.h"
#include "party.h"
#include "npc.h"
#include "log.h"
#include "script.h"
#include "atcommand.h"
#include "date.h"
#include "irc.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#define ACTIVE_AI_RANGE 2 //Distance added on top of 'AREA_SIZE' at which mobs enter active AI mode.
#define IDLE_SKILL_INTERVAL 10 //Active idle skills should be triggered every 1 second (1000/MIN_MOBTHINKTIME)
#define MOB_LAZYSKILLPERC 0 // Probability for mobs far from players from doing their IDLE skill. (rate of 1000 minute)
// Move probability for mobs away from players (rate of 1000 minute)
// in Aegis, this is 100% for mobs that have been activated by players and none otherwise.
#define MOB_LAZYMOVEPERC(md) (md->state.spotted?1000:0)
#define MOB_LAZYWARPPERC 20 // Warp probability in the negligent mode MOB (rate of 1000 minute)
#define MAX_MINCHASE 30 //Max minimum chase value to use for mobs.
#define RUDE_ATTACKED_COUNT 2 //After how many rude-attacks should the skill be used?
//Used to determine default enemy type of mobs (for use in eachinrange calls)
#define DEFAULT_ENEMY_TYPE(md) (md->special_state.ai?BL_CHAR:BL_PC|BL_HOM)
//Dynamic mob database, allows saving of memory when there's big gaps in the mob_db [Skotlex]
struct mob_db *mob_db_data[MAX_MOB_DB+1];
struct mob_db *mob_dummy = NULL; //Dummy mob to be returned when a non-existant one is requested.
struct mob_db *mob_db(int index) { if (index < 0 || index > MAX_MOB_DB || mob_db_data[index] == NULL) return mob_dummy; return mob_db_data[index]; }
static struct eri *item_drop_ers; //For loot drops delay structures.
static struct eri *item_drop_list_ers;
static struct {
int qty;
int class_[350];
} summon[MAX_RANDOMMONSTER];
static DBMap* barricade_db;
#define CLASSCHANGE_BOSS_NUM 21
/*==========================================
* Local prototype declaration (only required thing)
*------------------------------------------*/
static int mob_makedummymobdb(int);
static int mob_spawn_guardian_sub(int,unsigned int,int,int);
int mobskill_use(struct mob_data *md,unsigned int tick,int event);
int mob_skillid2skillidx(int class_,int skillid);
/*==========================================
* Mob is searched with a name.
*------------------------------------------*/
int mobdb_searchname(const char *str)
{
int i;
struct mob_db* mob;
for(i=0;i<=MAX_MOB_DB;i++){
mob = mob_db(i);
if(mob == mob_dummy) //Skip dummy mobs.
continue;
if(strcmpi(mob->name,str)==0 || strcmpi(mob->jname,str)==0 || strcmpi(mob->sprite,str)==0)
return i;
}
return 0;
}
static int mobdb_searchname_array_sub(struct mob_db* mob, const char *str)
{
if (mob == mob_dummy)
return 1; //Invalid mob.
if(!mob->base_exp && !mob->job_exp)
return 1; //Discount slave-mobs (no exp) as requested by Playtester. [Skotlex]
if(stristr(mob->jname,str))
return 0;
if(stristr(mob->name,str))
return 0;
return strcmpi(mob->jname,str);
}
/*==========================================
* Founds up to N matches. Returns number of matches [Skotlex]
*------------------------------------------*/
int mobdb_searchname_array(struct mob_db** data, int size, const char *str)
{
int count = 0, i;
struct mob_db* mob;
for(i=0;i<=MAX_MOB_DB;i++){
mob = mob_db(i);
if (mob == mob_dummy)
continue;
if (!mobdb_searchname_array_sub(mob, str)) {
if (count < size)
data[count] = mob;
count++;
}
}
return count;
}
/*==========================================
* Id Mob is checked.
*------------------------------------------*/
int mobdb_checkid(const int id)
{
if (mob_db(id) == mob_dummy)
return 0;
if (mob_is_clone(id)) //checkid is used mostly for random ID based code, therefore clone mobs are out of the question.
return 0;
return id;
}
/*==========================================
* Returns the view data associated to this mob class.
*------------------------------------------*/
struct view_data * mob_get_viewdata(int class_)
{
if (mob_db(class_) == mob_dummy)
return 0;
return &mob_db(class_)->vd;
}
/*==========================================
* Cleans up mob-spawn data to make it "valid"
*------------------------------------------*/
int mob_parse_dataset(struct spawn_data *data)
{
int i;
//FIXME: This implementation is not stable, npc scripts will stop working once MAX_MOB_DB changes value! [Skotlex]
if(data->class_ > 2*MAX_MOB_DB){ // large/tiny mobs [Valaris]
data->state.size=2;
data->class_ -= 2*MAX_MOB_DB;
} else if (data->class_ > MAX_MOB_DB) {
data->state.size=1;
data->class_ -= MAX_MOB_DB;
}
if ((!mobdb_checkid(data->class_) && !mob_is_clone(data->class_)) || !data->num)
return 0;
//better safe than sorry, current md->npc_event has a size of 50
if ((i=strlen(data->eventname)) >= 50)
return 0;
if (data->eventname[0])
{
if(i <= 2)
{ //Portable monster big/small implementation. [Skotlex]
i = atoi(data->eventname);
if (i) {
if (i&2)
data->state.size=1;
else if (i&4)
data->state.size=2;
if (i&8)
data->state.ai=1;
data->eventname[0] = '\0'; //Clear event as it is not used.
}
} else {
if (data->eventname[i-1] == '"')
data->eventname[i-1] = '\0'; //Remove trailing quote.
if (data->eventname[0] == '"') //Strip leading quotes
memmove(data->eventname, data->eventname+1, i-1);
}
}
if (!data->level)
data->level = mob_db(data->class_)->lv;
if(strcmp(data->name,"--en--")==0)
strncpy(data->name,mob_db(data->class_)->name,NAME_LENGTH-1);
else if(strcmp(data->name,"--ja--")==0)
strncpy(data->name,mob_db(data->class_)->jname,NAME_LENGTH-1);
return 1;
}
/*==========================================
* Generates the basic mob data using the spawn_data provided.
*------------------------------------------*/
struct mob_data* mob_spawn_dataset(struct spawn_data *data)
{
struct mob_data *md = (struct mob_data*)aCalloc(1, sizeof(struct mob_data));
md->bl.id= npc_get_new_npc_id();
md->bl.type = BL_MOB;
md->bl.m = data->m;
md->bl.x = data->x;
md->bl.y = data->y;
md->class_ = data->class_;
md->db = mob_db(md->class_);
memcpy(md->name, data->name, NAME_LENGTH);
if (data->state.ai)
md->special_state.ai = data->state.ai;
if (data->state.size)
md->special_state.size = data->state.size;
if (data->eventname[0] && strlen(data->eventname) >= 4)
memcpy(md->npc_event, data->eventname, 50);
md->level = data->level;
if(md->db->status.mode&MD_LOOTER)
md->lootitem = (struct item *)aCalloc(LOOTITEM_SIZE,sizeof(struct item));
md->deletetimer = -1;
md->skillidx = -1;
status_set_viewdata(&md->bl, md->class_);
status_change_init(&md->bl);
unit_dataset(&md->bl);
map_addiddb(&md->bl);
return md;
}
/*==========================================
* Fetches a random mob_id [Skotlex]
* type: Where to fetch from:
* 0: dead branch list
* 1: poring list
* 2: bloody branch list
* flag:
* &1: Apply the summon success chance found in the list (otherwise get any monster from the db)
* &2: Apply a monster check level.
* &4: Selected monster should not be a boss type
* &8: Selected monster must have normal spawn.
* lv: Mob level to check against
*------------------------------------------*/
int mob_get_random_id(int type, int flag, int lv)
{
struct mob_db *mob;
int i=0, class_;
if(type < 0 || type >= MAX_RANDOMMONSTER) {
ShowError("mob_get_random_id: Invalid type (%d) of random monster.\n", type);
return 0;
}
do {
if (type)
class_ = summon[type].class_[rand()%summon[type].qty];
else //Dead branch
class_ = rand() % MAX_MOB_DB;
mob = mob_db(class_);
} while ((mob == mob_dummy ||
mob_is_clone(class_) ||
(flag&1 && mob->summonper[type] <= rand() % 1000000) ||
(flag&2 && lv < mob->lv) ||
(flag&4 && mob->status.mode&MD_BOSS) ||
(flag&8 && mob->spawn[0].qty < 1)
) && (i++) < MAX_MOB_DB);
if(i >= MAX_MOB_DB)
class_ = mob_db_data[0]->summonper[type];
return class_;
}
/*==========================================
* Kill Steal Protection [Zephyrus]
*------------------------------------------*/
bool mob_ksprotected (struct block_list *src, struct block_list *target)
{
struct block_list *s_bl;
struct map_session_data *sd, *pl_sd;
struct status_change_entry *sce;
struct mob_data *md;
unsigned int tick = gettick();
char output[128];
if( !battle_config.ksprotection )
return false; // KS Protection Disabled
if( !(md = BL_CAST(BL_MOB,target)) )
return false; // Tarjet is not MOB
if( (s_bl = battle_get_master(src)) == NULL )
s_bl = src;
if( !(sd = BL_CAST(BL_PC,s_bl)) )
return false; // Master is not PC
do {
if( map[md->bl.m].flag.allowks || map[md->bl.m].flag.gvg || map[md->bl.m].flag.pvp )
return false; // Ignores GVG, PVP and AllowKS map flags
if( md->db->mexp || md->master_id )
return false; // MVP, Slaves mobs ignores KS
if( (sce = md->sc.data[SC_KSPROTECTED]) == NULL )
break; // No KS Protected
if( sd->bl.id == sce->val1 )
break; // Same Player
if( !(pl_sd = map_id2sd(sce->val1)) )
break; // Owner offline
if( pl_sd->bl.m != md->bl.m )
break; // Protection expires on different map
if( !pl_sd->state.noks )
return false; // No KS Protected, but normal players should be protected too
if( pl_sd->state.noks == 2 && pl_sd->status.party_id && pl_sd->status.party_id == sd->status.party_id )
break; // Party KS allowed
if( pl_sd->state.noks == 3 && pl_sd->status.guild_id && pl_sd->status.guild_id == sd->status.guild_id )
break; // Guild KS allowed
// Message to KS
if( DIFF_TICK(sd->ks_floodprotect_tick, tick) <= 0 )
{
sprintf(output, "[KS Warning!! - Owner : %s]", pl_sd->status.name);
clif_disp_onlyself(sd, output, strlen(output));
sd->ks_floodprotect_tick = tick + 2000;
}
// Message to Owner
if( DIFF_TICK(pl_sd->ks_floodprotect_tick, tick) <= 0 )
{
sprintf(output, "[Warning!! - %s is KS you]", sd->status.name);
clif_disp_onlyself(pl_sd, output, strlen(output));
pl_sd->ks_floodprotect_tick = tick + 2000;
}
return true;
} while(0);
status_change_start(target, SC_KSPROTECTED, 10000, sd->bl.id, 0, 0, 0, battle_config.ksprotection, 0);
return false;
}
struct mob_data *mob_once_spawn_sub(struct block_list *bl, int m, short x, short y, const char *mobname, int class_, const char *event)
{
struct spawn_data data;
memset(&data, 0, sizeof(struct spawn_data));
data.m = m;
data.num = 1;
data.class_ = class_;
if (mobname)
safestrncpy(data.name, mobname, sizeof(data.name));
else
if(battle_config.override_mob_names==1)
strcpy(data.name,"--en--");
else
strcpy(data.name,"--ja--");
if (event)
safestrncpy(data.eventname, event, sizeof(data.eventname));
// Locate spot next to player.
if (bl && (x < 0 || y < 0))
map_search_freecell(bl, m, &x, &y, 1, 1, 0);
// if none found, pick random position on map
if (x <= 0 || y <= 0 || map_getcell(m,x,y,CELL_CHKNOREACH))
map_search_freecell(NULL, m, &x, &y, -1, -1, 1);
data.x = x;
data.y = y;
if (!mob_parse_dataset(&data))
return NULL;
return mob_spawn_dataset(&data);
}
/*==========================================
* Spawn a single mob on the specified coordinates.
*------------------------------------------*/
int mob_once_spawn(struct map_session_data* sd, int m, short x, short y, const char* mobname, int class_, int amount, const char* event)
{
struct mob_data* md = NULL;
int count, lv;
if (m < 0 || amount <= 0)
return 0; // invalid input
if(sd)
lv = sd->status.base_level;
else
lv = 255;
for (count = 0; count < amount; count++)
{
int c = ( class_ >= 0 ) ? class_ : mob_get_random_id(-class_-1, battle_config.random_monster_checklv?3:1, lv);
md = mob_once_spawn_sub(sd?&sd->bl:NULL, m, x, y, mobname, c, event);
if (!md) continue;
if(class_ == MOBID_EMPERIUM) {
struct guild_castle* gc = guild_mapindex2gc(map[m].index);
struct guild* g = gc?guild_search(gc->guild_id):NULL;
if(gc) {
md->guardian_data = (struct guardian_data*)aCalloc(1, sizeof(struct guardian_data));
md->guardian_data->castle = gc;
md->guardian_data->number = MAX_GUARDIANS;
md->guardian_data->guild_id = gc->guild_id;
if (g)
{
md->guardian_data->emblem_id = g->emblem_id;
memcpy(md->guardian_data->guild_name, g->name, NAME_LENGTH);
}
else if (gc->guild_id) //Guild not yet available, retry in 5.
add_timer(gettick()+5000,mob_spawn_guardian_sub,md->bl.id,md->guardian_data->guild_id);
}
} // end addition [Valaris]
mob_spawn(md);
if (class_ < 0 && battle_config.dead_branch_active)
//Behold Aegis's masterful decisions yet again...
//"I understand the "Aggressive" part, but the "Can Move" and "Can Attack" is just stupid" - Poki#3
sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE|MD_CANATTACK|MD_CANMOVE, 0, 60000);
}
return (md)?md->bl.id : 0; // id of last spawned mob
}
/*==========================================
* Spawn mobs in the specified area.
*------------------------------------------*/
int mob_once_spawn_area(struct map_session_data* sd,int m,int x0,int y0,int x1,int y1,const char* mobname,int class_,int amount,const char* event)
{
int i,max,id=0;
int lx=-1,ly=-1;
if (m < 0 || amount <= 0)
return 0; // invalid input
// normalize x/y coordinates
if( x0 > x1 ) swap(x0,x1);
if( y0 > y1 ) swap(y0,y1);
// choose a suitable max. number of attempts
max = (y1-y0+1)*(x1-x0+1)*3;
if( max > 1000 )
max = 1000;
// spawn mobs, one by one
for( i = 0; i < amount; i++)
{
int x,y;
int j = 0;
// find a suitable map cell
do {
x = rand()%(x1-x0+1)+x0;
y = rand()%(y1-y0+1)+y0;
j++;
} while( map_getcell(m,x,y,CELL_CHKNOPASS) && j < max );
if( j == max )
{// attempt to find an available cell failed
if( lx == -1 && ly == -1 )
return 0; // total failure
// fallback to last good x/y pair
x = lx;
y = ly;
}
// record last successful coordinates
lx = x;
ly = y;
id = mob_once_spawn(sd,m,x,y,mobname,class_,1,event);
}
return id; // id of last spawned mob
}
/*==========================================
* Barricades [Zephyrus]
*------------------------------------------*/
void mob_barricade_nextxy(short x, short y, short dir, int pos, short *x1, short *y1)
{ // Calculates Next X-Y Position
if( dir == 0 || dir == 4 )
*x1 = x; // Keep X
else if( dir > 0 && dir < 4 )
*x1 = x - pos; // Going left
else
*x1 = x + pos; // Going right
if( dir == 2 || dir == 6 )
*y1 = y;
else if( dir > 2 && dir < 6 )
*y1 = y - pos;
else
*y1 = y + pos;
}
short mob_barricade_build(short m, short x, short y, short count, short dir, bool killable, const char* event)
{
int i, j;
short x1;
short y1;
struct mob_data *md;
struct barricade_data *barricade;
if( count <= 0 )
return 1;
if( !event )
return 2;
if( (barricade = (struct barricade_data *)strdb_get(barricade_db,event)) != NULL )
return 3; // Already a barricade with event name
if( map_getcell(m, x, y, CELL_CHKNOREACH) )
return 4; // Starting cell problem
CREATE(barricade, struct barricade_data, 1);
barricade->dir = dir;
barricade->x = x;
barricade->y = y;
barricade->m = m;
safestrncpy(barricade->npc_event, event, sizeof(barricade->npc_event));
barricade->amount = 0;
barricade->killable = killable;
ShowInfo("New Barricade: %s.\n", barricade->npc_event);
for( i = 0; i < count; i++ )
{
mob_barricade_nextxy(x, y, dir, i, &x1, &y1);
if( map_getcell(m, x1, y1, CELL_CHKNOREACH) )
break; // Collision
if( i % 2 == 0 )
{
barricade->amount++;
j = mob_once_spawn(NULL, m, x1, y1, "--ja--", killable ? MOBID_BARRICADEB : MOBID_BARRICADEA, 1, "");
md = (struct mob_data *)map_id2bl(j);
md->barricade = barricade;
}
map_setgatcell(m, x1, y1, (killable ? 5 : 1));
clif_changemapcell(0, m, x1, y1, (killable ? 5 : 1), ALL_SAMEMAP);
}
barricade->count = i;
strdb_put(barricade_db, barricade->npc_event, barricade);
map[m].barricade_num++;
return 0;
}
void mob_barricade_get(struct map_session_data *sd)
{ // Update Barricade cell in client - Required only on changemap
struct barricade_data *barricade;
DBIterator* iter;
DBKey key;
short x1, y1;
int i;
if( map[sd->bl.m].barricade_num <= 0 )
return;
iter = barricade_db->iterator(barricade_db);
for( barricade = (struct barricade_data *)iter->first(iter,&key); iter->exists(iter); barricade = (struct barricade_data *)iter->next(iter,&key) )
{
if( sd->bl.m != barricade->m )
continue;
for( i = 0; i < barricade->count; i++ )
{
mob_barricade_nextxy(barricade->x, barricade->y, barricade->dir, i, &x1, &y1);
clif_changemapcell(sd->fd, barricade->m, x1, y1, (barricade->killable ? 5 : 1), SELF);
}
}
iter->destroy(iter);
}
static void mob_barricade_break(struct barricade_data *barricade)
{
int i;
short x1, y1;
if( barricade == NULL )
return;
if( --barricade->amount > 0 )
return; // There are still barricades
for( i = 0; i < barricade->count; i++ )
{
mob_barricade_nextxy(barricade->x, barricade->y, barricade->dir, i, &x1, &y1);
map_setgatcell(barricade->m, x1, y1, 0);
clif_changemapcell(0, barricade->m, x1, y1, 0, ALL_SAMEMAP);
}
npc_event_do(barricade->npc_event);
map[barricade->m].barricade_num--;
strdb_remove(barricade_db, barricade->npc_event);
}
static int mob_barricade_destroy_sub(struct block_list *bl, va_list ap)
{
TBL_MOB* md = (TBL_MOB*)bl;
char *event = va_arg(ap,char *);
if( md->barricade == NULL )
return 0;
if( strcmp(event, md->barricade->npc_event) == 0 )
status_kill(bl);
return 0;
}
void mob_barricade_destroy(short m, const char *event)
{
if( !event )
return;
map_foreachinmap(mob_barricade_destroy_sub, m, BL_MOB, event, 0);
}
void mod_barricade_clearall(void)
{
struct barricade_data *barricade;
DBIterator* iter;
DBKey key;
short x1, y1;
int i;
iter = barricade_db->iterator(barricade_db);
for( barricade = (struct barricade_data *)iter->first(iter,&key); iter->exists(iter); barricade = (struct barricade_data *)iter->next(iter,&key) )
{
for( i = 0; i < barricade->count; i++ )
{
mob_barricade_nextxy(barricade->x, barricade->y, barricade->dir, i, &x1, &y1);
map_setgatcell(barricade->m, x1, y1, 0);
clif_changemapcell(0, barricade->m, x1, y1, 0, ALL_SAMEMAP);
}
}
iter->destroy(iter);
barricade_db->clear(barricade_db, NULL);
}
/*==========================================
* Set a Guardian's guild data [Skotlex]
*------------------------------------------*/
static int mob_spawn_guardian_sub(int tid,unsigned int tick,int id,int data)
{ //Needed because the guild_data may not be available at guardian spawn time.
struct block_list* bl = map_id2bl(id);
struct mob_data* md;
struct guild* g;
int guardup_lv;
if (bl == NULL) //It is possible mob was already removed from map when the castle has no owner. [Skotlex]
return 0;
if (bl->type != BL_MOB)
{
ShowError("mob_spawn_guardian_sub: Block error!\n");
return 0;
}
md = (struct mob_data*)bl;
nullpo_retr(0, md->guardian_data);
g = guild_search(data);
if (g == NULL)
{ //Liberate castle, if the guild is not found this is an error! [Skotlex]
ShowError("mob_spawn_guardian_sub: Couldn't load guild %d!\n",data);
if (md->class_ == MOBID_EMPERIUM)
{ //Not sure this is the best way, but otherwise we'd be invoking this for ALL guardians spawned later on.
md->guardian_data->guild_id = 0;
if (md->guardian_data->castle->guild_id) //Free castle up.
{
ShowNotice("Clearing ownership of castle %d (%s)\n", md->guardian_data->castle->castle_id, md->guardian_data->castle->castle_name);
guild_castledatasave(md->guardian_data->castle->castle_id, 1, 0);
}
} else {
if( md->guardian_data->number >= 0 && md->guardian_data->number < MAX_GUARDIANS && md->guardian_data->castle->guardian[md->guardian_data->number].visible )
{ //Safe removal of guardian.
md->guardian_data->castle->guardian[md->guardian_data->number].visible = 0;
guild_castledatasave(md->guardian_data->castle->castle_id, 10+md->guardian_data->number,0);
}
unit_free(&md->bl,0); //Remove guardian.
}
return 0;
}
guardup_lv = guild_checkskill(g,GD_GUARDUP);
md->guardian_data->emblem_id = g->emblem_id;
memcpy(md->guardian_data->guild_name, g->name, NAME_LENGTH);
md->guardian_data->guardup_lv = guardup_lv;
if( guardup_lv )
status_calc_mob(md, 0); //Give bonuses.
return 0;
}
/*==========================================
* Summoning Guardians [Valaris]
*------------------------------------------*/
int mob_spawn_guardian(const char* mapname, short x, short y, const char* mobname, int class_, const char* event, int guardian, bool has_index)
{
struct mob_data *md=NULL;
struct spawn_data data;
struct guild *g=NULL;
struct guild_castle *gc;
int m;
memset(&data, 0, sizeof(struct spawn_data));
data.num = 1;
m=map_mapname2mapid(mapname);
if(m<0)
{
ShowWarning("mob_spawn_guardian: Map [%s] not found.\n", mapname);
return 0;
}
data.m = m;
data.num = 1;
if(class_<=0) {
class_ = mob_get_random_id(-class_-1, 1, 99);
if (!class_) return 0;
}
data.class_ = class_;
if( !has_index )
{
guardian = -1;
}
else if( guardian < 0 || guardian >= MAX_GUARDIANS )
{
ShowError("mob_spawn_guardian: Invalid guardian index %d for guardian %d (castle map %s)\n", guardian, class_, map[m].name);
return 0;
}
if((x<=0 || y<=0) && !map_search_freecell(NULL, m, &x, &y, -1,-1, 0))
{
ShowWarning("mob_spawn_guardian: Couldn't locate a spawn cell for guardian class %d (index %d) at castle map %s\n",class_, guardian, map[m].name);
return 0;
}
data.x = x;
data.y = y;
safestrncpy(data.name, mobname, sizeof(data.name));
safestrncpy(data.eventname, event, sizeof(data.eventname));
if (!mob_parse_dataset(&data))
return 0;
gc=guild_mapname2gc(map[m].name);
if (gc == NULL)
{
ShowError("mob_spawn_guardian: No castle set at map %s\n", map[m].name);
return 0;
}
if (!gc->guild_id)
ShowWarning("mob_spawn_guardian: Spawning guardian %d on a castle with no guild (castle map %s)\n", class_, map[m].name);
else
g = guild_search(gc->guild_id);
if( has_index && gc->guardian[guardian].id )
{ //Check if guardian already exists, refuse to spawn if so.
struct mob_data *md2 = (TBL_MOB*)map_id2bl(gc->guardian[guardian].id);
if (md2 && md2->bl.type == BL_MOB &&
md2->guardian_data && md2->guardian_data->number == guardian)
{
ShowError("mob_spawn_guardian: Attempted to spawn guardian in position %d which already has a guardian (castle map %s)\n", guardian, map[m].name);
return 0;
}
}
md = mob_spawn_dataset(&data);
md->guardian_data = (struct guardian_data*)aCalloc(1, sizeof(struct guardian_data));
md->guardian_data->number = guardian;
md->guardian_data->guild_id = gc->guild_id;
md->guardian_data->castle = gc;
if( has_index )
{// permanent guardian
gc->guardian[guardian].id = md->bl.id;
}
else
{// temporary guardian
int i;
ARR_FIND(0, gc->temp_guardians_max, i, gc->temp_guardians[i] == 0);
if( i == gc->temp_guardians_max )
{
++(gc->temp_guardians_max);
RECREATE(gc->temp_guardians, int, gc->temp_guardians_max);
}
gc->temp_guardians[i] = md->bl.id;
}
if (g)
{
md->guardian_data->emblem_id = g->emblem_id;
memcpy (md->guardian_data->guild_name, g->name, NAME_LENGTH);
md->guardian_data->guardup_lv = guild_checkskill(g,GD_GUARDUP);
} else if (md->guardian_data->guild_id)
add_timer(gettick()+5000,mob_spawn_guardian_sub,md->bl.id,md->guardian_data->guild_id);
mob_spawn(md);
return md->bl.id;
}
/*==========================================
* Reachability to a Specification ID existence place
* state indicates type of 'seek' mob should do:
* - MSS_LOOT: Looking for item, path must be easy.
* - MSS_RUSH: Chasing attacking player, path is complex
* - MSS_FOLLOW: Initiative/support seek, path is complex
*------------------------------------------*/
int mob_can_reach(struct mob_data *md,struct block_list *bl,int range, int state)
{
int easy = 0;
nullpo_retr(0, md);
nullpo_retr(0, bl);
switch (state) {
case MSS_RUSH:
case MSS_FOLLOW:
easy = 0; //(battle_config.mob_ai&0x1?0:1);
break;
case MSS_LOOT:
default:
easy = 1;
break;
}
return unit_can_reach_bl(&md->bl, bl, range, easy, NULL, NULL);
}
/*==========================================
* Links nearby mobs (supportive mobs)
*------------------------------------------*/
int mob_linksearch(struct block_list *bl,va_list ap)
{
struct mob_data *md;
int class_;
struct block_list *target;
unsigned int tick;
nullpo_retr(0, bl);
nullpo_retr(0, ap);
md=(struct mob_data *)bl;
class_ = va_arg(ap, int);
target = va_arg(ap, struct block_list *);
tick=va_arg(ap, unsigned int);
if (md->class_ == class_ && DIFF_TICK(md->last_linktime, tick) < MIN_MOBLINKTIME
&& !md->target_id)
{
md->last_linktime = tick;
if( mob_can_reach(md,target,md->db->range2, MSS_FOLLOW) ){ // Reachability judging
md->target_id = target->id;
md->min_chase=md->db->range3;
return 1;
}
}
return 0;
}
/*==========================================
* mob spawn with delay (timer function)
*------------------------------------------*/
static int mob_delayspawn(int tid, unsigned int tick, int m, int n)
{
struct block_list *bl = map_id2bl(m);
if (bl && bl->type == BL_MOB)
mob_spawn((TBL_MOB*)bl);
return 0;
}
/*==========================================
* spawn timing calculation
*------------------------------------------*/
int mob_setdelayspawn(struct mob_data *md)
{
unsigned int spawntime;
if (!md->spawn) //Doesn't has respawn data!
return unit_free(&md->bl,1);
spawntime = md->spawn->delay1; //Base respawn time
if (md->spawn->delay2) //random variance
spawntime+= rand()%md->spawn->delay2;
if (spawntime < 5000) //Min respawn time (is it needed?)
spawntime = 5000;
add_timer(gettick()+spawntime, mob_delayspawn, md->bl.id, 0);
return 0;
}
static int mob_count_sub(struct block_list *bl,va_list ap)
{
return 1;
}
/*==========================================
* Mob spawning. Initialization is also variously here.
*------------------------------------------*/
int mob_spawn (struct mob_data *md)
{
int i=0;
unsigned int c =0, tick = gettick();
md->last_thinktime = tick;
if (md->bl.prev != NULL)
unit_remove_map(&md->bl,2);
else
if (md->spawn && md->class_ != md->spawn->class_)
{
md->class_ = md->spawn->class_;
status_set_viewdata(&md->bl, md->class_);
md->db = mob_db(md->class_);
memcpy(md->name,md->spawn->name,NAME_LENGTH);
}
if (md->spawn) { //Respawn data
md->bl.m = md->spawn->m;
md->bl.x = md->spawn->x;
md->bl.y = md->spawn->y;
if ((md->bl.x == 0 && md->bl.y == 0) || md->spawn->xs || md->spawn->ys)
{ //Monster can be spawned on an area.
if (!map_search_freecell(&md->bl, -1,
&md->bl.x, &md->bl.y, md->spawn->xs, md->spawn->ys,
battle_config.no_spawn_on_player?4:0)) {
// retry again later
add_timer(tick+5000,mob_delayspawn,md->bl.id,0);
return 1;
}
} else if (battle_config.no_spawn_on_player>99 &&
map_foreachinrange(mob_count_sub, &md->bl, AREA_SIZE, BL_PC))
{ //retry again later (players on sight)
add_timer(tick+5000,mob_delayspawn,md->bl.id,0);
return 1;
}
}
memset(&md->state, 0, sizeof(md->state));
status_calc_mob(md, 1);
md->attacked_id = 0;
md->target_id = 0;
md->move_fail_count = 0;
// md->master_id = 0;
md->master_dist = 0;
md->state.aggressive = md->status.mode&MD_ANGRY?1:0;
md->state.skillstate = MSS_IDLE;
md->next_walktime = tick+rand()%5000+1000;
md->last_linktime = tick;
md->last_pcneartime = 0;
for (i = 0, c = tick-1000*3600*10; i < MAX_MOBSKILL; i++)
md->skilldelay[i] = c;
memset(md->dmglog, 0, sizeof(md->dmglog));
md->tdmg = 0;
if (md->lootitem)
memset(md->lootitem, 0, sizeof(md->lootitem));
md->lootitem_count = 0;
if(md->db->option)
// Added for carts, falcons and pecos for cloned monsters. [Valaris]
md->sc.option = md->db->option;
map_addblock(&md->bl);
clif_spawn(&md->bl);
skill_unit_move(&md->bl,tick,1);
mobskill_use(md, tick, MSC_SPAWN);
return 0;
}
/*==========================================
* Determines if the mob can change target. [Skotlex]
*------------------------------------------*/
static int mob_can_changetarget(struct mob_data* md, struct block_list* target, int mode)
{
// if the monster was provoked ignore the above rule [celest]
if(md->state.provoke_flag)
{
if (md->state.provoke_flag == target->id)
return 1;
else if (!(battle_config.mob_ai&0x4))
return 0;
}
switch (md->state.skillstate) {
case MSS_BERSERK:
if (!(mode&MD_CHANGETARGET_MELEE))
return 0;
return (battle_config.mob_ai&0x4 || check_distance_bl(&md->bl, target, 3));
case MSS_RUSH:
return (mode&MD_CHANGETARGET_CHASE);
case MSS_FOLLOW:
case MSS_ANGRY:
case MSS_IDLE:
case MSS_WALK:
case MSS_LOOT:
return 1;
default:
return 0;
}
}
/*==========================================
* Determination for an attack of a monster
*------------------------------------------*/
int mob_target(struct mob_data *md,struct block_list *bl,int dist)
{
nullpo_retr(0, md);
nullpo_retr(0, bl);
// Nothing will be carried out if there is no mind of changing TAGE by TAGE ending.
if(md->target_id && !mob_can_changetarget(md, bl, status_get_mode(&md->bl)))
return 0;
if(!status_check_skilluse(&md->bl, bl, 0, 0))
return 0;
md->target_id = bl->id; // Since there was no disturbance, it locks on to target.
if (md->state.provoke_flag && bl->id != md->state.provoke_flag)
md->state.provoke_flag = 0;
md->min_chase=dist+md->db->range3;
if(md->min_chase>MAX_MINCHASE)
md->min_chase=MAX_MINCHASE;
return 0;
}
/*==========================================
* The ?? routine of an active monster
*------------------------------------------*/
static int mob_ai_sub_hard_activesearch(struct block_list *bl,va_list ap)
{
struct mob_data *md;
struct block_list **target;
int dist;
nullpo_retr(0, bl);
nullpo_retr(0, ap);
md=va_arg(ap,struct mob_data *);
target= va_arg(ap,struct block_list**);
//If can't seek yet, not an enemy, or you can't attack it, skip.
if ((*target) == bl || !status_check_skilluse(&md->bl, bl, 0, 0))
return 0;
if(battle_check_target(&md->bl,bl,BCT_ENEMY)<=0)
return 0;
if(md->nd && mob_script_callback(md, bl, CALLBACK_DETECT))
return 1; // We have script handling the work.
switch (bl->type)
{
case BL_PC:
if (((TBL_PC*)bl)->state.gangsterparadise &&
!(status_get_mode(&md->bl)&MD_BOSS))
return 0; //Gangster paradise protection.
default:
if (battle_config.hom_setting&0x4 &&
(*target) && (*target)->type == BL_HOM && bl->type != BL_HOM)
return 0; //For some reason Homun targets are never overriden.
dist = distance_bl(&md->bl, bl);
if(
((*target) == NULL || !check_distance_bl(&md->bl, *target, dist)) &&
battle_check_range(&md->bl,bl,md->db->range2)
) { //Pick closest target?
(*target) = bl;
md->target_id=bl->id;
md->min_chase= dist + md->db->range3;
if(md->min_chase>MAX_MINCHASE)
md->min_chase=MAX_MINCHASE;
return 1;
}
break;
}
return 0;
}
/*==========================================
* chase target-change routine.
*------------------------------------------*/
static int mob_ai_sub_hard_changechase(struct block_list *bl,va_list ap)
{
struct mob_data *md;
struct block_list **target;
nullpo_retr(0, bl);
nullpo_retr(0, ap);
md=va_arg(ap,struct mob_data *);
target= va_arg(ap,struct block_list**);
//If can't seek yet, not an enemy, or you can't attack it, skip.
if ((*target) == bl ||
battle_check_target(&md->bl,bl,BCT_ENEMY)<=0 ||
!status_check_skilluse(&md->bl, bl, 0, 0))
return 0;
if(battle_check_range (&md->bl, bl, md->status.rhw.range))
{
(*target) = bl;
md->target_id=bl->id;
md->min_chase= md->db->range3;
}
return 1;
}
/*==========================================
* loot monster item search
*------------------------------------------*/
static int mob_ai_sub_hard_lootsearch(struct block_list *bl,va_list ap)
{
struct mob_data* md;
struct block_list **target;
int dist;
md=va_arg(ap,struct mob_data *);
target= va_arg(ap,struct block_list**);
dist=distance_bl(&md->bl, bl);
if(mob_can_reach(md,bl,dist+1, MSS_LOOT) &&
((*target) == NULL || !check_distance_bl(&md->bl, *target, dist)) //New target closer than previous one.
) {
(*target) = bl;
md->target_id=bl->id;
md->min_chase=md->db->range3;
}
return 0;
}
static int mob_warpchase_sub(struct block_list *bl,va_list ap)
{
struct mob_data* md;
struct block_list *target;
struct npc_data **target_nd;
struct npc_data *nd;
int *min_distance;
int cur_distance;
md=va_arg(ap,struct mob_data *);
target= va_arg(ap, struct block_list*);
target_nd= va_arg(ap, struct npc_data**);
min_distance= va_arg(ap, int*);
nd = (TBL_NPC*) bl;
if(nd->subtype != WARP)
return 0; //Not a warp
if(nd->u.warp.mapindex != map[target->m].index)
return 0; //Does not lead to the same map.
cur_distance = distance_blxy(target, nd->u.warp.x, nd->u.warp.y);
if (cur_distance < *min_distance)
{ //Pick warp that leads closest to target.
*target_nd = nd;
*min_distance = cur_distance;
return 1;
}
return 0;
}
/*==========================================
* Processing of slave monsters
*------------------------------------------*/
static int mob_ai_sub_hard_slavemob(struct mob_data *md,unsigned int tick)
{
struct block_list *bl;
int old_dist;
bl=map_id2bl(md->master_id);
if (!bl || status_isdead(bl)) {
status_kill(&md->bl);
return 1;
}
if (bl->prev == NULL)
return 0; //Master not on a map? Could be warping, do not process.
if(status_get_mode(&md->bl)&MD_CANMOVE)
{ //If the mob can move, follow around. [Check by Skotlex]
// Distance with between slave and master is measured.
old_dist=md->master_dist;
md->master_dist=distance_bl(&md->bl, bl);
// Since the master was in near immediately before, teleport is carried out and it pursues.
if(bl->m != md->bl.m ||
(old_dist<10 && md->master_dist>18) ||
md->master_dist > MAX_MINCHASE
){
md->master_dist = 0;
unit_warp(&md->bl,bl->m,bl->x,bl->y,3);
return 1;
}
if(md->target_id) //Slave is busy with a target.
return 0;
// Approach master if within view range, chase back to Master's area also if standing on top of the master.
if((md->master_dist>MOB_SLAVEDISTANCE || md->master_dist == 0) &&
unit_can_move(&md->bl))
{
short x = bl->x, y = bl->y;
mob_stop_attack(md);
if(map_search_freecell(&md->bl, bl->m, &x, &y, MOB_SLAVEDISTANCE, MOB_SLAVEDISTANCE, 1)
&& unit_walktoxy(&md->bl, x, y, 0))
return 1;
}
} else if (bl->m != md->bl.m && map_flag_gvg(md->bl.m)) {
//Delete the summoned mob if it's in a gvg ground and the master is elsewhere. [Skotlex]
status_kill(&md->bl);
return 1;
}
//Avoid attempting to lock the master's target too often to avoid unnecessary overload. [Skotlex]
if (DIFF_TICK(md->last_linktime, tick) < MIN_MOBLINKTIME && !md->target_id)
{
struct unit_data *ud = unit_bl2ud(bl);
md->last_linktime = tick;
if (ud) {
struct block_list *tbl=NULL;
if (ud->target && ud->state.attack_continue)
tbl=map_id2bl(ud->target);
else if (ud->skilltarget) {
tbl = map_id2bl(ud->skilltarget);
//Required check as skilltarget is not always an enemy. [Skotlex]
if (tbl && battle_check_target(&md->bl, tbl, BCT_ENEMY) <= 0)
tbl = NULL;
}
if (tbl && status_check_skilluse(&md->bl, tbl, 0, 0)) {
if(md->nd)
mob_script_callback(md, bl, CALLBACK_ASSIST);
md->target_id=tbl->id;
md->min_chase=md->db->range3+distance_bl(&md->bl, tbl);
if(md->min_chase>MAX_MINCHASE)
md->min_chase=MAX_MINCHASE;
return 1;
}
}
}
return 0;
}
/*==========================================
* A lock of target is stopped and mob moves to a standby state.
* This also triggers idle skill/movement since the AI can get stuck
* when trying to pick new targets when the current chosen target is
* unreachable.
*------------------------------------------*/
int mob_unlocktarget(struct mob_data *md, unsigned int tick)
{
nullpo_retr(0, md);
if(md->nd)
mob_script_callback(md, map_id2bl(md->target_id), CALLBACK_UNLOCK);
switch (md->state.skillstate) {
case MSS_WALK:
if (md->ud.walktimer != -1)
break;
//Because it is not unset when the mob finishes walking.
md->state.skillstate = MSS_IDLE;
case MSS_IDLE:
// Idle skill.
if ((md->target_id || !(++md->ud.walk_count%IDLE_SKILL_INTERVAL)) &&
mobskill_use(md, tick, -1))
break;
//Random walk.
if (!md->master_id &&
DIFF_TICK(md->next_walktime, tick) <= 0 &&
!mob_randomwalk(md,tick))
//Delay next random walk when this one failed.
md->next_walktime=tick+rand()%3000;
break;
default:
mob_stop_attack(md);
if (battle_config.mob_ai&0x8)
mob_stop_walking(md,1); //Immediately stop chasing.
md->state.skillstate = MSS_IDLE;
md->next_walktime=tick+rand()%3000+3000;
break;
}
if (md->target_id) {
md->target_id=0;
md->ud.target = 0;
}
return 0;
}
/*==========================================
* Random walk
*------------------------------------------*/
int mob_randomwalk(struct mob_data *md,unsigned int tick)
{
const int retrycount=20;
int i,x,y,c,d;
int speed;
nullpo_retr(0, md);
if(DIFF_TICK(md->next_walktime,tick)>0 ||
md->state.no_random_walk ||
!unit_can_move(&md->bl) ||
!(status_get_mode(&md->bl)&MD_CANMOVE))
return 0;
d =12-md->move_fail_count;
if(d<5) d=5;
for(i=0;i<retrycount;i++){ // Search of a movable place
int r=rand();
x=r%(d*2+1)-d;
y=r/(d*2+1)%(d*2+1)-d;
x+=md->bl.x;
y+=md->bl.y;
if((map_getcell(md->bl.m,x,y,CELL_CHKPASS)) && unit_walktoxy(&md->bl,x,y,1)){
break;
}
}
if(i==retrycount){
md->move_fail_count++;
if(md->move_fail_count>1000){
ShowWarning("MOB can't move. random spawn %d, class = %d, at %s (%d,%d)\n",md->bl.id,md->class_,map[md->bl.m].name, md->bl.x, md->bl.y);
md->move_fail_count=0;
mob_spawn(md);
}
return 0;
}
speed=status_get_speed(&md->bl);
for(i=c=0;i<md->ud.walkpath.path_len;i++){ // The next walk start time is calculated.
if(md->ud.walkpath.path[i]&1)
c+=speed*14/10;
else
c+=speed;
}
md->state.skillstate=MSS_WALK;
md->move_fail_count=0;
md->next_walktime = tick+rand()%3000+3000+c;
return 1;
}
int mob_warpchase(struct mob_data *md, struct block_list *target)
{
struct npc_data *warp = NULL;
int distance = AREA_SIZE;
if (!(target && battle_config.mob_ai&0x40 && battle_config.mob_warp&1))
return 0; //Can't warp chase.
if (target->m == md->bl.m && check_distance_bl(&md->bl, target, AREA_SIZE))
return 0; //No need to do a warp chase.
if (md->ud.walktimer != -1 &&
map_getcell(md->bl.m,md->ud.to_x,md->ud.to_y,CELL_CHKNPC))
return 1; //Already walking to a warp.
//Search for warps within mob's viewing range.
map_foreachinrange (mob_warpchase_sub, &md->bl,
md->db->range2, BL_NPC, md, target, &warp, &distance);
if (warp && unit_walktobl(&md->bl, &warp->bl, 0, 1))
return 1;
return 0;
}
/*==========================================
* AI of MOB whose is near a Player
*------------------------------------------*/
static bool mob_ai_sub_hard(struct mob_data *md, unsigned int tick)
{
struct block_list *tbl = NULL, *abl = NULL;
int dist;
int mode;
int search_size;
int view_range, can_move;
if(md->bl.prev == NULL || md->status.hp <= 0)
return false;
if (DIFF_TICK(tick, md->last_thinktime) < MIN_MOBTHINKTIME)
return false;
md->last_thinktime = tick;
if (md->ud.skilltimer != -1)
return false;
if(md->ud.walktimer != -1 && md->ud.walkpath.path_pos <= 3)
return false;
// Abnormalities
if((md->sc.opt1 > 0 && md->sc.opt1 != OPT1_STONEWAIT) || md->sc.data[SC_BLADESTOP])
{ //Should reset targets.
md->target_id = md->attacked_id = 0;
return false;
}
if (md->sc.count && md->sc.data[SC_BLIND])
view_range = 3;
else
view_range = md->db->range2;
mode = status_get_mode(&md->bl);
can_move = (mode&MD_CANMOVE)&&unit_can_move(&md->bl);
if (md->target_id)
{ //Check validity of current target. [Skotlex]
tbl = map_id2bl(md->target_id);
if (!tbl || tbl->m != md->bl.m ||
(md->ud.attacktimer == -1 && !status_check_skilluse(&md->bl, tbl, 0, 0)) ||
(md->ud.walktimer != -1 && !(battle_config.mob_ai&0x1) && !check_distance_bl(&md->bl, tbl, md->min_chase)) ||
(
tbl->type == BL_PC &&
((((TBL_PC*)tbl)->state.gangsterparadise && !(mode&MD_BOSS)) ||
((TBL_PC*)tbl)->invincible_timer != INVALID_TIMER)
)) { //Unlock current target.
if (mob_warpchase(md, tbl))
return true; //Chasing this target.
mob_unlocktarget(md, tick-(battle_config.mob_ai&0x8?3000:0)); //Imediately do random walk.
tbl = NULL;
}
}
// Check for target change.
if (md->attacked_id && mode&MD_CANATTACK)
{
if (md->attacked_id == md->target_id)
{ //Rude attacked check.
if (!battle_check_range(&md->bl, tbl, md->status.rhw.range) &&
( //Can't attack back and can't reach back.
(!can_move && DIFF_TICK(tick, md->ud.canmove_tick) > 0 &&
(battle_config.mob_ai&0x2 || md->sc.data[SC_SPIDERWEB])) ||
(!mob_can_reach(md, tbl, md->min_chase, MSS_RUSH))
) &&
md->state.attacked_count++ >= RUDE_ATTACKED_COUNT &&
!mobskill_use(md, tick, MSC_RUDEATTACKED) && //If can't rude Attack
can_move && unit_escape(&md->bl, tbl, rand()%10 +1)) //Attempt escape
{ //Escaped
md->attacked_id = 0;
return true;
}
} else
if ((abl= map_id2bl(md->attacked_id)) && (!tbl || mob_can_changetarget(md, abl, mode))) {
if (md->bl.m != abl->m || abl->prev == NULL ||
(dist = distance_bl(&md->bl, abl)) >= MAX_MINCHASE ||
battle_check_target(&md->bl, abl, BCT_ENEMY) <= 0 ||
(battle_config.mob_ai&0x2 && !status_check_skilluse(&md->bl, abl, 0, 0)) || //Retaliate check
(!battle_check_range(&md->bl, abl, md->status.rhw.range) &&
( //Reach check
(!can_move && DIFF_TICK(tick, md->ud.canmove_tick) > 0 &&
(battle_config.mob_ai&0x2 || md->sc.data[SC_SPIDERWEB])) ||
!mob_can_reach(md, abl, dist+md->db->range3, MSS_RUSH)
)
)
) { //Rude attacked
if (md->state.attacked_count++ >= RUDE_ATTACKED_COUNT &&
!mobskill_use(md, tick, MSC_RUDEATTACKED) && can_move &&
!tbl && unit_escape(&md->bl, abl, rand()%10 +1))
{ //Escaped.
//TODO: Maybe it shouldn't attempt to run if it has another, valid target?
md->attacked_id = 0;
return true;
}
} else if (!(battle_config.mob_ai&0x2) && !status_check_skilluse(&md->bl, abl, 0, 0)) {
//Can't attack back, but didn't invoke a rude attacked skill...
} else { //Attackable
if (!tbl || dist < md->status.rhw.range || !check_distance_bl(&md->bl, tbl, dist)
|| battle_gettarget(tbl) != md->bl.id)
{ //Change if the new target is closer than the actual one
//or if the previous target is not attacking the mob. [Skotlex]
md->target_id = md->attacked_id; // set target
if (md->state.attacked_count)
md->state.attacked_count--; //Should we reset rude attack count?
md->min_chase = dist+md->db->range3;
if(md->min_chase>MAX_MINCHASE)
md->min_chase=MAX_MINCHASE;
tbl = abl; //Set the new target
}
}
}
//Clear it since it's been checked for already.
md->attacked_id = 0;
}
// Processing of slave monster
if (md->master_id > 0 && mob_ai_sub_hard_slavemob(md, tick))
return true;
// Scan area for targets
if (!tbl && mode&MD_LOOTER && md->lootitem && DIFF_TICK(tick, md->ud.canact_tick) > 0 &&
(md->lootitem_count < LOOTITEM_SIZE || battle_config.monster_loot_type != 1))
{ // Scan area for items to loot, avoid trying to loot of the mob is full and can't consume the items.
map_foreachinrange (mob_ai_sub_hard_lootsearch, &md->bl,
view_range, BL_ITEM, md, &tbl);
}
if ((!tbl && mode&MD_AGGRESSIVE) || md->state.skillstate == MSS_FOLLOW)
{
map_foreachinrange (mob_ai_sub_hard_activesearch, &md->bl,
view_range, DEFAULT_ENEMY_TYPE(md), md, &tbl);
} else
if (mode&MD_CHANGECHASE && (md->state.skillstate == MSS_RUSH || md->state.skillstate == MSS_FOLLOW))
{
search_size = view_range<md->status.rhw.range ? view_range:md->status.rhw.range;
map_foreachinrange (mob_ai_sub_hard_changechase, &md->bl,
search_size, DEFAULT_ENEMY_TYPE(md), md, &tbl);
}
if (!tbl) { //No targets available.
if (mode&MD_ANGRY && !md->state.aggressive)
md->state.aggressive = 1; //Restore angry state when no targets are available.
//This handles triggering idle walk/skill.
mob_unlocktarget(md, tick);
return true;
}
//Target exists, attack or loot as applicable.
if (tbl->type == BL_ITEM)
{ //Loot time.
struct flooritem_data *fitem;
if (md->ud.target == tbl->id && md->ud.walktimer != -1)
return true; //Already locked.
if (md->lootitem == NULL)
{ //Can't loot...
mob_unlocktarget (md, tick);
return true;
}
if (!check_distance_bl(&md->bl, tbl, 1))
{ //Still not within loot range.
if (!(mode&MD_CANMOVE))
{ //A looter that can't move? Real smart.
mob_unlocktarget(md,tick);
return true;
}
if (!can_move) //Stuck. Wait before walking.
return true;
md->state.skillstate = MSS_LOOT;
if (!unit_walktobl(&md->bl, tbl, 0, 1))
mob_unlocktarget(md, tick); //Can't loot...
return true;
}
//Within looting range.
if (md->ud.attacktimer != -1)
return true; //Busy attacking?
fitem = (struct flooritem_data *)tbl;
if(log_config.enable_logs&0x10) //Logs items, taken by (L)ooter Mobs [Lupus]
log_pick_mob(md, "L", fitem->item_data.nameid, fitem->item_data.amount, &fitem->item_data);
if (md->lootitem_count < LOOTITEM_SIZE) {
memcpy (&md->lootitem[md->lootitem_count++], &fitem->item_data, sizeof(md->lootitem[0]));
} else { //Destroy first looted item...
if (md->lootitem[0].card[0] == CARD0_PET)
intif_delete_petdata( MakeDWord(md->lootitem[0].card[1],md->lootitem[0].card[2]) );
memmove(&md->lootitem[0], &md->lootitem[1], (LOOTITEM_SIZE-1)*sizeof(md->lootitem[0]));
memcpy (&md->lootitem[LOOTITEM_SIZE-1], &fitem->item_data, sizeof(md->lootitem[0]));
}
if (pcdb_checkid(md->vd->class_))
{ //Give them walk act/delay to properly mimic players. [Skotlex]
clif_takeitem(&md->bl,tbl);
md->ud.canact_tick = tick + md->status.amotion;
unit_set_walkdelay(&md->bl, tick, md->status.amotion, 1);
}
//Clear item.
map_clearflooritem (tbl->id);
mob_unlocktarget (md,tick);
return true;
}
//Attempt to attack.
//At this point we know the target is attackable, we just gotta check if the range matches.
if (md->ud.target == tbl->id && md->ud.attacktimer != -1) //Already locked.
return true;
if (battle_check_range (&md->bl, tbl, md->status.rhw.range))
{ //Target within range, engage
unit_attack(&md->bl,tbl->id,1);
return true;
}
//Out of range...
if (!(mode&MD_CANMOVE))
{ //Can't chase. Attempt an idle skill before unlocking.
md->state.skillstate = MSS_IDLE;
if (!mobskill_use(md, tick, -1))
mob_unlocktarget(md,tick);
return true;
}
if (!can_move)
{ //Stuck. Attempt an idle skill
md->state.skillstate = MSS_IDLE;
if (!(++md->ud.walk_count%IDLE_SKILL_INTERVAL))
mobskill_use(md, tick, -1);
return true;
}
if (md->ud.walktimer != -1 && md->ud.target == tbl->id &&
(
!(battle_config.mob_ai&0x1) ||
check_distance_blxy(tbl, md->ud.to_x, md->ud.to_y, md->status.rhw.range)
)) //Current target tile is still within attack range.
return true;
//Follow up if possible.
if(!mob_can_reach(md, tbl, md->min_chase, MSS_RUSH) ||
!unit_walktobl(&md->bl, tbl, md->status.rhw.range, 2))
mob_unlocktarget(md,tick);
return true;
}
static int mob_ai_sub_hard_timer(struct block_list *bl,va_list ap)
{
struct mob_data *md = (struct mob_data*)bl;
unsigned int tick = va_arg(ap, unsigned int);
if (mob_ai_sub_hard(md, tick))
{ //Hard AI triggered.
if(!md->state.spotted)
md->state.spotted = 1;
md->last_pcneartime = tick;
}
return 0;
}
/*==========================================
* Serious processing for mob in PC field of view (foreachclient)
*------------------------------------------*/
static int mob_ai_sub_foreachclient(struct map_session_data *sd,va_list ap)
{
unsigned int tick;
tick=va_arg(ap,unsigned int);
map_foreachinrange(mob_ai_sub_hard_timer,&sd->bl, AREA_SIZE+ACTIVE_AI_RANGE, BL_MOB,tick);
return 0;
}
/*==========================================
* Negligent mode MOB AI (PC is not in near)
*------------------------------------------*/
static int mob_ai_sub_lazy(DBKey key,void * data,va_list ap)
{
struct mob_data *md = (struct mob_data *)data;
unsigned int tick;
int mode;
nullpo_retr(0, md);
if(md->bl.prev == NULL)
return 0;
tick=va_arg(ap,unsigned int);
if (md->nd || (battle_config.mob_ai&0x20 && map[md->bl.m].users>0))
return (int)mob_ai_sub_hard(md, tick);
if (md->bl.prev==NULL || md->status.hp == 0)
return 1;
if(battle_config.mob_active_time &&
md->last_pcneartime &&
!(md->status.mode&MD_BOSS) &&
DIFF_TICK(tick,md->last_thinktime) > MIN_MOBTHINKTIME)
{
if (DIFF_TICK(tick,md->last_pcneartime) < battle_config.mob_active_time)
return (int)mob_ai_sub_hard(md, tick);
md->last_pcneartime = 0;
}
if(battle_config.boss_active_time &&
md->last_pcneartime &&
(md->status.mode&MD_BOSS) &&
DIFF_TICK(tick,md->last_thinktime) > MIN_MOBTHINKTIME)
{
if (DIFF_TICK(tick,md->last_pcneartime) < battle_config.boss_active_time)
return (int)mob_ai_sub_hard(md, tick);
md->last_pcneartime = 0;
}
if(DIFF_TICK(tick,md->last_thinktime)< 10*MIN_MOBTHINKTIME)
return 0;
md->last_thinktime=tick;
if (md->master_id) {
mob_ai_sub_hard_slavemob (md,tick);
return 0;
}
mode = status_get_mode(&md->bl);
if(DIFF_TICK(md->next_walktime,tick)<0 &&
(mode&MD_CANMOVE) && unit_can_move(&md->bl) ){
if( map[md->bl.m].users>0 ){
// Since PC is in the same map, somewhat better negligent processing is carried out.
// It sometimes moves.
if(rand()%1000<MOB_LAZYMOVEPERC(md))
mob_randomwalk(md,tick);
else if(rand()%1000<MOB_LAZYSKILLPERC) //Chance to do a mob's idle skill.
mobskill_use(md, tick, -1);
// MOB which is not not the summons MOB but BOSS, either sometimes reboils.
// People don't want this, it seems custom, noone can prove it....
// else if( rand()%1000<MOB_LAZYWARPPERC
// && (md->spawn && !md->spawn->x && !md->spawn->y)
// && !md->target_id && !(mode&MD_BOSS))
// unit_warp(&md->bl,-1,-1,-1,0);
}else{
// Since PC is not even in the same map, suitable processing is carried out even if it takes.
// MOB which is not BOSS which is not Summons MOB, either -- a case -- sometimes -- leaping
if( rand()%1000<MOB_LAZYWARPPERC
&& (md->spawn && !md->spawn->x && !md->spawn->y)
&& !(mode&MD_BOSS))
unit_warp(&md->bl,-1,-1,-1,0);
}
md->next_walktime = tick+rand()%10000+5000;
}
return 0;
}
/*==========================================
* Negligent processing for mob outside PC field of view (interval timer function)
*------------------------------------------*/
static int mob_ai_lazy(int tid,unsigned int tick,int id,int data)
{
map_foreachmob(mob_ai_sub_lazy,tick);
return 0;
}
/*==========================================
* Serious processing for mob in PC field of view (interval timer function)
*------------------------------------------*/
static int mob_ai_hard(int tid,unsigned int tick,int id,int data)
{
if (battle_config.mob_ai&0x20)
map_foreachmob(mob_ai_sub_lazy,tick);
else
clif_foreachclient(mob_ai_sub_foreachclient,tick);
return 0;
}
/*==========================================
* Initializes the delay drop structure for mob-dropped items.
*------------------------------------------*/
static struct item_drop* mob_setdropitem(int nameid, int qty)
{
struct item_drop *drop = ers_alloc(item_drop_ers, struct item_drop);
memset(&drop->item_data, 0, sizeof(struct item));
drop->item_data.nameid = nameid;
drop->item_data.amount = qty;
drop->item_data.identify = itemdb_isidentified(nameid);
drop->next = NULL;
return drop;
};
/*==========================================
* Initializes the delay drop structure for mob-looted items.
*------------------------------------------*/
static struct item_drop* mob_setlootitem(struct item* item)
{
struct item_drop *drop = ers_alloc(item_drop_ers, struct item_drop);
memcpy(&drop->item_data, item, sizeof(struct item));
drop->next = NULL;
return drop;
};
/*==========================================
* item drop with delay (timer function)
*------------------------------------------*/
static int mob_delay_item_drop(int tid, unsigned int tick, int id, int data)
{
struct item_drop_list *list;
struct item_drop *ditem, *ditem_prev;
list=(struct item_drop_list *)id;
ditem = list->item;
while (ditem) {
map_addflooritem(&ditem->item_data,ditem->item_data.amount,
list->m,list->x,list->y,
list->first_charid,list->second_charid,list->third_charid,0);
ditem_prev = ditem;
ditem = ditem->next;
ers_free(item_drop_ers, ditem_prev);
}
ers_free(item_drop_list_ers, list);
return 0;
}
/*==========================================
* Sets the item_drop into the item_drop_list.
* Also performs logging and autoloot if enabled.
* rate is the drop-rate of the item, required for autoloot.
* flag : Killed only by homunculus?
*------------------------------------------*/
static void mob_item_drop(struct mob_data *md, struct item_drop_list *dlist, struct item_drop *ditem, int loot, int drop_rate, unsigned short flag)
{
TBL_PC* sd;
if(log_config.enable_logs&0x10)
{ //Logs items, dropped by mobs [Lupus]
if (loot)
log_pick_mob(md, "L", ditem->item_data.nameid, -ditem->item_data.amount, &ditem->item_data);
else
log_pick_mob(md, "M", ditem->item_data.nameid, -ditem->item_data.amount, NULL);
}
sd = map_charid2sd(dlist->first_charid);
if( sd == NULL ) sd = map_charid2sd(dlist->second_charid);
if( sd == NULL ) sd = map_charid2sd(dlist->third_charid);
if( sd
&& (drop_rate <= sd->state.autoloot || ditem->item_data.nameid == sd->state.autolootid)
&& (battle_config.idle_no_autoloot == 0 || DIFF_TICK(last_tick, sd->idletime) < battle_config.idle_no_autoloot)
&& (battle_config.homunculus_autoloot?1:!flag)
#ifdef AUTOLOOT_DISTANCE
&& check_distance_blxy(&sd->bl, dlist->x, dlist->y, AUTOLOOT_DISTANCE)
#endif
) { //Autoloot.
if (party_share_loot(party_search(sd->status.party_id),
sd, &ditem->item_data, sd->status.char_id) == 0
) {
ers_free(item_drop_ers, ditem);
return;
}
}
ditem->next = dlist->item;
dlist->item = ditem;
}
int mob_timer_delete(int tid, unsigned int tick, int id, int data)
{
struct block_list *bl=map_id2bl(id);
nullpo_retr(0, bl);
if (bl->type != BL_MOB)
return 0; //??
//for Alchemist CANNIBALIZE [Lupus]
((TBL_MOB*)bl)->deletetimer = -1;
unit_free(bl,3);
return 0;
}
int mob_convertslave_sub(struct block_list *bl,va_list ap)
{
struct mob_data *md, *md2 = NULL;
nullpo_retr(0, bl);
nullpo_retr(0, ap);
nullpo_retr(0, md = (struct mob_data *)bl);
md2=va_arg(ap,TBL_MOB *);
if(md->master_id > 0 && md->master_id == md2->bl.id){
md->state.killer = md2->state.killer;
md->special_state.ai = md2->special_state.ai;
md->nd = md2->nd;
md->callback_flag = md2->callback_flag;
}
return 0;
}
int mob_convertslave(struct mob_data *md)
{
nullpo_retr(0, md);
map_foreachinmap(mob_convertslave_sub, md->bl.m, BL_MOB, md);
return 0;
}
/*==========================================
*
*------------------------------------------*/
int mob_deleteslave_sub(struct block_list *bl,va_list ap)
{
struct mob_data *md;
int id;
nullpo_retr(0, bl);
nullpo_retr(0, ap);
nullpo_retr(0, md = (struct mob_data *)bl);
id=va_arg(ap,int);
if(md->master_id > 0 && md->master_id == id )
status_kill(bl);
return 0;
}
/*==========================================
*
*------------------------------------------*/
int mob_deleteslave(struct mob_data *md)
{
nullpo_retr(0, md);
map_foreachinmap(mob_deleteslave_sub, md->bl.m, BL_MOB,md->bl.id);
return 0;
}
// Mob respawning through KAIZEL or NPC_REBIRTH [Skotlex]
int mob_respawn(int tid, unsigned int tick, int id,int data )
{
struct block_list *bl = map_id2bl(id);
if(!bl) return 0;
status_revive(bl, data, 0);
return 1;
}
void mob_log_damage(struct mob_data *md, struct block_list *src, int damage)
{
int char_id = 0, flag = 0;
if(damage < 0) return; //Do nothing for absorbed damage.
if(!damage && !(src->type&DEFAULT_ENEMY_TYPE(md)))
return; //Do not log non-damaging effects from non-enemies.
switch (src->type) {
case BL_PC:
{
struct map_session_data *sd = (TBL_PC*)src;
char_id = sd->status.char_id;
if (damage)
md->attacked_id = src->id;
break;
}
case BL_HOM: //[orn]
{
struct homun_data *hd = (TBL_HOM*)src;
flag = 1;
if (hd->master)
char_id = hd->master->status.char_id;
if (damage)
md->attacked_id = src->id;
break;
}
case BL_PET:
{
struct pet_data *pd = (TBL_PET*)src;
if (battle_config.pet_attack_exp_to_master && pd->msd) {
char_id = pd->msd->status.char_id;
damage=(damage*battle_config.pet_attack_exp_rate)/100; //Modify logged damage accordingly.
}
//Let mobs retaliate against the pet's master [Skotlex]
if(pd->msd && damage)
md->attacked_id = pd->msd->bl.id;
break;
}
case BL_MOB:
{
struct mob_data* md2 = (TBL_MOB*)src;
if(md2->special_state.ai && md2->master_id) {
struct map_session_data* msd = map_id2sd(md2->master_id);
if (msd) char_id = msd->status.char_id;
}
if (!damage)
break;
//Let players decide whether to retaliate versus the master or the mob. [Skotlex]
if (md2->master_id && battle_config.retaliate_to_master)
md->attacked_id = md2->master_id;
else
md->attacked_id = src->id;
break;
}
default: //For all unhandled types.
md->attacked_id = src->id;
}
//Log damage...
if(char_id) {
int i,minpos;
unsigned int mindmg;
for(i=0,minpos=DAMAGELOG_SIZE-1,mindmg=UINT_MAX;i<DAMAGELOG_SIZE;i++){
if(md->dmglog[i].id==char_id &&
md->dmglog[i].flag==flag)
break;
if(md->dmglog[i].id==0) { //Store data in first empty slot.
md->dmglog[i].id = char_id;
md->dmglog[i].flag= flag;
break;
}
if(md->dmglog[i].dmg<mindmg && i)
{ //Never overwrite first hit slot (he gets double exp bonus)
minpos=i;
mindmg=md->dmglog[i].dmg;
}
}
if(i<DAMAGELOG_SIZE)
md->dmglog[i].dmg+=damage;
else {
md->dmglog[minpos].id = char_id;
md->dmglog[minpos].flag= flag;
md->dmglog[minpos].dmg = damage;
}
}
return;
}
//Call when a mob has received damage.
void mob_damage(struct mob_data *md, struct block_list *src, int damage)
{
if (damage > 0)
{ //Store total damage...
if (UINT_MAX - (unsigned int)damage > md->tdmg)
md->tdmg+=damage;
else if (md->tdmg == UINT_MAX)
damage = 0; //Stop recording damage once the cap has been reached.
else { //Cap damage log...
damage = (int)(UINT_MAX - md->tdmg);
md->tdmg = UINT_MAX;
}
if (md->state.aggressive)
{ //No longer aggressive, change to retaliate AI.
md->state.aggressive = 0;
if(md->state.skillstate== MSS_ANGRY)
md->state.skillstate = MSS_BERSERK;
if(md->state.skillstate== MSS_FOLLOW)
md->state.skillstate = MSS_RUSH;
}
//Log damage
if (src) mob_log_damage(md, src, damage);
}
if (battle_config.show_mob_info&3)
clif_charnameack (0, &md->bl);
if (!src)
return;
if(md->nd)
mob_script_callback(md, src, CALLBACK_ATTACK);
if(md->special_state.ai==2/* && md->master_id == src->id*/)
{ //LOne WOlf explained that ANYONE can trigger the marine countdown skill. [Skotlex]
md->state.alchemist = 1;
mobskill_use(md, gettick(), MSC_ALCHEMIST);
}
}
/*==========================================
* Signals death of mob.
* type&1 -> no drops, type&2 -> no exp
*------------------------------------------*/
int mob_dead(struct mob_data *md, struct block_list *src, int type)
{
struct status_data *status;
struct map_session_data *sd = NULL, *tmpsd[DAMAGELOG_SIZE];
struct map_session_data *mvp_sd = NULL, *second_sd = NULL, *third_sd = NULL;
struct {
struct party_data *p;
int id,zeny;
unsigned int base_exp,job_exp;
} pt[DAMAGELOG_SIZE];
int i,temp,count,pnum=0,m=md->bl.m;
unsigned int mvp_damage, tick = gettick();
unsigned short flaghom = 1; // [Zephyrus] Does the mob only received damage from homunculus?
if(src && src->type == BL_PC) {
sd = (struct map_session_data *)src;
mvp_sd = sd;
}
status = &md->status;
if( md->guardian_data && md->guardian_data->number >= 0 && md->guardian_data->number < MAX_GUARDIANS )
guild_castledatasave(md->guardian_data->castle->castle_id, 10+md->guardian_data->number,0);
md->state.skillstate = MSS_DEAD;
mobskill_use(md,tick,-1); //On Dead skill.
if (md->sc.data[SC_KAIZEL])
{ //Revive in a bit.
add_timer(gettick()+3000, mob_respawn, md->bl.id, md->sc.data[SC_KAIZEL]->val2); //% of life to rebirth with
map_delblock(&md->bl);
return 1; //Return 1 to only clear the object.
}
map_freeblock_lock();
memset(pt,0,sizeof(pt));
if(src && src->type == BL_MOB)
mob_unlocktarget((struct mob_data *)src,tick);
if(sd) {
int sp = 0, hp = 0;
sp += sd->sp_gain_value;
sp += sd->sp_gain_race[status->race];
sp += sd->sp_gain_race[status->mode&MD_BOSS?RC_BOSS:RC_NONBOSS];
hp += sd->hp_gain_value;
if (hp||sp)
status_heal(src, hp, sp, battle_config.show_hp_sp_gain?2:0);
if (sd->mission_mobid == md->class_) { //TK_MISSION [Skotlex]
if (++sd->mission_count >= 100 && (temp = mob_get_random_id(0, 0xE, sd->status.base_level)))
{
pc_addfame(sd, 1);
sd->mission_mobid = temp;
pc_setglobalreg(sd,"TK_MISSION_ID", temp);
sd->mission_count = 0;
clif_mission_info(sd, temp, 0);
}
pc_setglobalreg(sd,"TK_MISSION_COUNT", sd->mission_count);
}
}
// filter out entries not eligible for exp distribution
memset(tmpsd,0,sizeof(tmpsd));
for(i = 0, count = 0, mvp_damage = 0; i < DAMAGELOG_SIZE && md->dmglog[i].id; i++)
{
struct map_session_data* tsd = map_charid2sd(md->dmglog[i].id);
if(tsd == NULL)
continue; // skip empty entries
if(tsd->bl.m != m)
continue; // skip players not on this map
count++; //Only logged into same map chars are counted for the total.
if (pc_isdead(tsd))
continue; // skip dead players
if(md->dmglog[i].flag && !merc_is_hom_active(tsd->hd))
continue; // skip homunc's share if inactive
if(md->dmglog[i].dmg > mvp_damage)
{
third_sd = second_sd;
second_sd = mvp_sd;
mvp_sd = tsd;
mvp_damage = md->dmglog[i].dmg;
}
tmpsd[i] = tsd; // record as valid damage-log entry
if(!md->dmglog[i].flag && flaghom)
flaghom = 0; // Damage received from other Types != Homunculus
}
if(!battle_config.exp_calc_type && count > 1)
{ //Apply first-attacker 200% exp share bonus
//TODO: Determine if this should go before calculating the MVP player instead of after.
if (UINT_MAX - md->dmglog[0].dmg > md->tdmg) {
md->tdmg += md->dmglog[0].dmg;
md->dmglog[0].dmg<<=1;
} else {
md->dmglog[0].dmg+= UINT_MAX - md->tdmg;
md->tdmg = UINT_MAX;
}
}
if(!(type&2) && //No exp
(!map[m].flag.pvp || battle_config.pvp_exp) && //Pvp no exp rule [MouseJstr]
(!md->master_id || !md->special_state.ai) && //Only player-summoned mobs do not give exp. [Skotlex]
(!map[m].flag.nobaseexp || !map[m].flag.nojobexp) //Gives Exp
) { //Experience calculation.
int bonus = 100; //Bonus on top of your share (common to all attackers).
if (md->sc.data[SC_RICHMANKIM])
bonus += md->sc.data[SC_RICHMANKIM]->val2;
if(sd) {
temp = status_get_class(&md->bl);
if(sd->sc.data[SC_MIRACLE]) i = 2; //All mobs are Star Targets
else
ARR_FIND(0, 3, i, temp == sd->hate_mob[i] &&
(battle_config.allow_skill_without_day || sg_info[i].day_func()));
if(i<3 && (temp=pc_checkskill(sd,sg_info[i].bless_id)))
bonus += (i==2?20:10)*temp;
}
if(battle_config.mobs_level_up && md->level > md->db->lv) // [Valaris]
bonus += (md->level-md->db->lv)*battle_config.mobs_level_up_exp_rate;
for(i = 0; i < DAMAGELOG_SIZE && md->dmglog[i].id; i++)
{
int flag=1,zeny=0;
unsigned int base_exp, job_exp;
double per; //Your share of the mob's exp
if (!tmpsd[i]) continue;
if (!battle_config.exp_calc_type && md->tdmg)
//jAthena's exp formula based on total damage.
per = (double)md->dmglog[i].dmg/(double)md->tdmg;
else {
//eAthena's exp formula based on max hp.
per = (double)md->dmglog[i].dmg/(double)status->max_hp;
if (per > 2) per = 2; // prevents unlimited exp gain
}
if (count>1 && battle_config.exp_bonus_attacker) {
//Exp bonus per additional attacker.
if (count > battle_config.exp_bonus_max_attacker)
count = battle_config.exp_bonus_max_attacker;
per += per*((count-1)*battle_config.exp_bonus_attacker)/100.;
}
// change experience for different sized monsters [Valaris]
if(md->special_state.size==1)
per /=2.;
else if(md->special_state.size==2)
per *=2.;
if(battle_config.zeny_from_mobs && md->level) {
// zeny calculation moblv + random moblv [Valaris]
zeny=(int) ((md->level+rand()%md->level)*per*bonus/100.);
if(md->db->mexp > 0)
zeny*=rand()%250;
}
if (map[m].flag.nobaseexp || !md->db->base_exp)
base_exp = 0;
else
base_exp = (unsigned int)cap_value(md->db->base_exp * per * bonus/100. * map[m].bexp/100., 1, UINT_MAX);
if (map[m].flag.nojobexp || !md->db->job_exp || md->dmglog[i].flag) //Homun earned job-exp is always lost.
job_exp = 0;
else
job_exp = (unsigned int)cap_value(md->db->job_exp * per * bonus/100. * map[m].jexp/100., 1, UINT_MAX);
if((temp = tmpsd[i]->status.party_id )>0 && !md->dmglog[i].flag) //Homun-done damage (flag 1) is not given to party
{
int j;
for(j=0;j<pnum && pt[j].id!=temp;j++); //Locate party.
if(j==pnum){ //Possibly add party.
pt[pnum].p = party_search(temp);
if(pt[pnum].p && pt[pnum].p->party.exp)
{
pt[pnum].id=temp;
pt[pnum].base_exp=base_exp;
pt[pnum].job_exp=job_exp;
pt[pnum].zeny=zeny; // zeny share [Valaris]
pnum++;
flag=0;
}
}else{ //Add to total
if (pt[j].base_exp > UINT_MAX - base_exp)
pt[j].base_exp=UINT_MAX;
else
pt[j].base_exp+=base_exp;
if (pt[j].job_exp > UINT_MAX - job_exp)
pt[j].job_exp=UINT_MAX;
else
pt[j].job_exp+=job_exp;
pt[j].zeny+=zeny; // zeny share [Valaris]
flag=0;
}
}
if(flag) {
if(base_exp && md->dmglog[i].flag) //tmpsd[i] is null if it has no homunc.
merc_hom_gainexp(tmpsd[i]->hd, base_exp);
if(base_exp || job_exp)
pc_gainexp(tmpsd[i], &md->bl, base_exp, job_exp);
if(zeny) // zeny from mobs [Valaris]
pc_getzeny(tmpsd[i], zeny);
}
}
for(i=0;i<pnum;i++) //Party share.
party_exp_share(pt[i].p, &md->bl, pt[i].base_exp,pt[i].job_exp,pt[i].zeny);
} //End EXP giving.
if (!(type&1) &&
!map[m].flag.nomobloot &&
(
!md->special_state.ai || //Non special mob
battle_config.alchemist_summon_reward == 2 || //All summoned give drops
(md->special_state.ai==2 && battle_config.alchemist_summon_reward == 1) //Marine Sphere Drops items.
)
) { //item drop
struct item_drop_list *dlist = ers_alloc(item_drop_list_ers, struct item_drop_list);
struct item_drop *ditem;
int drop_rate;
dlist->m = md->bl.m;
dlist->x = md->bl.x;
dlist->y = md->bl.y;
dlist->first_charid = (mvp_sd ? mvp_sd->status.char_id : 0);
dlist->second_charid = (second_sd ? second_sd->status.char_id : 0);
dlist->third_charid = (third_sd ? third_sd->status.char_id : 0);
dlist->item = NULL;
for (i = 0; i < MAX_MOB_DROP; i++)
{
if (md->db->dropitem[i].nameid <= 0)
continue;
drop_rate = md->db->dropitem[i].p;
if (drop_rate <= 0) {
if (battle_config.drop_rate0item)
continue;
drop_rate = 1;
}
// change drops depending on monsters size [Valaris]
if(md->special_state.size==1 && drop_rate >= 2)
drop_rate/=2;
else if(md->special_state.size==2)
drop_rate*=2;
if (src) {
//Drops affected by luk as a fixed increase [Valaris]
if (battle_config.drops_by_luk)
drop_rate += status_get_luk(src)*battle_config.drops_by_luk/100;
//Drops affected by luk as a % increase [Skotlex]
if (battle_config.drops_by_luk2)
drop_rate += (int)(0.5+drop_rate*status_get_luk(src)*battle_config.drops_by_luk2/10000.);
}
if (sd && battle_config.pk_mode &&
(int)(md->level - sd->status.base_level) >= 20)
drop_rate = (int)(drop_rate*1.25); // pk_mode increase drops if 20 level difference [Valaris]
// attempt to drop the item
if (rand() % 10000 >= drop_rate)
{ // Double try by Bubble Gum
if (!(sd && sd->sc.data[SC_ITEMBOOST] && rand() % 10000 < drop_rate))
continue;
}
ditem = mob_setdropitem(md->db->dropitem[i].nameid, 1);
//A Rare Drop Global Announce by Lupus
if(drop_rate<=battle_config.rare_drop_announce) {
struct item_data *i_data;
char message[128];
i_data = itemdb_search(ditem->item_data.nameid);
sprintf (message, msg_txt(541), (mvp_sd?mvp_sd->status.name:"???"), md->name, i_data->jname, (float)drop_rate/100);
//MSG: "'%s' won %s's %s (chance: %0.02f%%)"
intif_GMmessage(message,strlen(message)+1,0);
}
// Announce first, or else ditem will be freed. [Lance]
// By popular demand, use base drop rate for autoloot code. [Skotlex]
mob_item_drop(md, dlist, ditem, 0, md->db->dropitem[i].p, flaghom);
}
// Ore Discovery [Celest]
if (sd == mvp_sd && pc_checkskill(sd,BS_FINDINGORE)>0 && battle_config.finding_ore_rate/10 >= rand()%10000) {
ditem = mob_setdropitem(itemdb_searchrandomid(IG_FINDINGORE), 1);
mob_item_drop(md, dlist, ditem, 0, battle_config.finding_ore_rate/10, 0);
}
if(sd) {
// process script-granted extra drop bonuses
int itemid = 0;
for (i = 0; i < ARRAYLENGTH(sd->add_drop) && (sd->add_drop[i].id || sd->add_drop[i].group); i++)
{
if (sd->add_drop[i].race & (1<<status->race) ||
sd->add_drop[i].race & 1<<(status->mode&MD_BOSS?RC_BOSS:RC_NONBOSS))
{
//check if the bonus item drop rate should be multiplied with mob level/10 [Lupus]
if(sd->add_drop[i].rate < 0) {
//it's negative, then it should be multiplied. e.g. for Mimic,Myst Case Cards, etc
// rate = base_rate * (mob_level/10) + 1
drop_rate = -sd->add_drop[i].rate*(md->level/10)+1;
drop_rate = cap_value(drop_rate, battle_config.item_drop_adddrop_min, battle_config.item_drop_adddrop_max);
if (drop_rate > 10000) drop_rate = 10000;
}
else
//it's positive, then it goes as it is
drop_rate = sd->add_drop[i].rate;
if (rand()%10000 >= drop_rate)
continue;
itemid = (sd->add_drop[i].id > 0) ? sd->add_drop[i].id : itemdb_searchrandomid(sd->add_drop[i].group);
mob_item_drop(md, dlist, mob_setdropitem(itemid,1), 0, drop_rate, 0);
}
}
// process script-granted zeny bonus (get_zeny_num) [Skotlex]
if(sd->get_zeny_num && rand()%100 < sd->get_zeny_rate)
{
i = sd->get_zeny_num > 0?sd->get_zeny_num:-md->level*sd->get_zeny_num;
if (!i) i = 1;
pc_getzeny(sd, 1+rand()%i);
}
}
// process items looted by the mob
if(md->lootitem) {
for(i = 0; i < md->lootitem_count; i++)
mob_item_drop(md, dlist, mob_setlootitem(&md->lootitem[i]), 1, 10000, 0);
}
if (dlist->item) //There are drop items.
add_timer(tick + (!battle_config.delay_battle_damage?500:0), mob_delay_item_drop, (int)dlist, 0);
else //No drops
ers_free(item_drop_list_ers, dlist);
} else if (md->lootitem && md->lootitem_count) { //Loot MUST drop!
struct item_drop_list *dlist = ers_alloc(item_drop_list_ers, struct item_drop_list);
dlist->m = md->bl.m;
dlist->x = md->bl.x;
dlist->y = md->bl.y;
dlist->first_charid = (mvp_sd ? mvp_sd->status.char_id : 0);
dlist->second_charid = (second_sd ? second_sd->status.char_id : 0);
dlist->third_charid = (third_sd ? third_sd->status.char_id : 0);
dlist->item = NULL;
for(i = 0; i < md->lootitem_count; i++)
mob_item_drop(md, dlist, mob_setlootitem(&md->lootitem[i]), 1, 10000, 0);
add_timer(tick + (!battle_config.delay_battle_damage?500:0), mob_delay_item_drop, (int)dlist, 0);
}
if(mvp_sd && md->db->mexp > 0 && !md->special_state.ai)
{
int log_mvp[2] = {0};
int j;
unsigned int mexp;
struct item item;
double exp;
//mapflag: noexp check [Lorky]
if (map[m].flag.nobaseexp || type&2)
exp =1;
else {
exp = md->db->mexp;
if (count > 1)
exp += exp*(battle_config.exp_bonus_attacker*(count-1))/100.; //[Gengar]
}
mexp = (unsigned int)cap_value(exp, 1, UINT_MAX);
if(use_irc && irc_announce_mvp_flag)
irc_announce_mvp(mvp_sd,md);
clif_mvp_effect(mvp_sd);
clif_mvp_exp(mvp_sd,mexp);
pc_gainexp(mvp_sd, &md->bl, mexp,0);
log_mvp[1] = mexp;
if(map[m].flag.nomvploot || type&1)
; //No drops.
else
for(j=0;j<3;j++)
{
i = rand() % 3;
if(md->db->mvpitem[i].nameid <= 0)
continue;
temp = md->db->mvpitem[i].p;
if(temp <= 0 && !battle_config.drop_rate0item)
temp = 1;
if(temp <= rand()%10000+1) //if ==0, then it doesn't drop
continue;
memset(&item,0,sizeof(item));
item.nameid=md->db->mvpitem[i].nameid;
item.identify= itemdb_isidentified(item.nameid);
clif_mvp_item(mvp_sd,item.nameid);
log_mvp[0] = item.nameid;
//A Rare MVP Drop Global Announce by Lupus
if(temp<=battle_config.rare_drop_announce) {
struct item_data *i_data;
char message[128];
i_data = itemdb_exists(item.nameid);
sprintf (message, msg_txt(541), mvp_sd->status.name, md->name, i_data->jname, temp/100.);
//MSG: "'%s' won %s's %s (chance: %0.02f%%)"
intif_GMmessage(message,strlen(message)+1,0);
}
if((temp = pc_additem(mvp_sd,&item,1)) != 0) {
clif_additem(mvp_sd,0,0,temp);
map_addflooritem(&item,1,mvp_sd->bl.m,mvp_sd->bl.x,mvp_sd->bl.y,mvp_sd->status.char_id,(second_sd?second_sd->status.char_id:0),(third_sd?third_sd->status.char_id:0),1);
}
if(log_config.enable_logs&0x200) {//Logs items, MVP prizes [Lupus]
log_pick_mob(md, "M", item.nameid, -1, NULL);
if (!temp)
log_pick_pc(mvp_sd, "P", item.nameid, 1, NULL);
}
break;
}
if(log_config.mvpdrop > 0)
log_mvpdrop(mvp_sd, md->class_, log_mvp);
}
if (type&2 && !sd && md->class_ == MOBID_EMPERIUM)
//Emperium destroyed by script. Discard mvp character. [Skotlex]
mvp_sd = NULL;
if(src && src->type == BL_MOB){
struct mob_data *smd = (struct mob_data *)src;
if(smd->nd)
mob_script_callback(smd, &md->bl, CALLBACK_KILL);
}
if(md->nd)
mob_script_callback(md, src, CALLBACK_DEAD);
else
if(md->npc_event[0])
{
md->status.hp = 0; //So that npc_event invoked functions KNOW that I am dead.
if(src)
switch (src->type) {
case BL_PET:
sd = ((TBL_PET*)src)->msd;
break;
case BL_HOM:
sd = ((TBL_HOM*)src)->master;
break;
}
if(sd && battle_config.mob_npc_event_type) {
pc_setglobalreg(sd,"killerrid",sd->bl.id);
npc_event(sd,md->npc_event,0);
} else if(mvp_sd) {
pc_setglobalreg(mvp_sd,"killerrid",sd?sd->bl.id:0);
npc_event(mvp_sd,md->npc_event,0);
}
md->status.hp = 1;
} else if (mvp_sd) { //lordalfa
pc_setglobalreg(mvp_sd,"killedrid",md->class_);
npc_script_event(mvp_sd, NPCE_KILLNPC); // PCKillNPC [Lance]
}
if( md->barricade != NULL )
mob_barricade_break(md->barricade);
if(md->deletetimer!=-1) {
delete_timer(md->deletetimer,mob_timer_delete);
md->deletetimer=-1;
}
mob_deleteslave(md);
map_freeblock_unlock();
if(pcdb_checkid(md->vd->class_))
{ //Player mobs are not removed automatically by the client.
if(md->nd){
md->vd->dead_sit = 1;
return 1; // Let the dead body stay there.. we have something to do with it :D
} else
clif_clearunit_delayed(&md->bl, tick+3000);
}
if(!md->spawn) //Tell status_damage to remove it from memory.
return 5; // Note: Actually, it's 4. Oh well...
mob_setdelayspawn(md); //Set respawning.
return 3; //Remove from map.
}
void mob_revive(struct mob_data *md, unsigned int hp)
{
unsigned int tick = gettick();
md->state.skillstate = MSS_IDLE;
md->last_thinktime = tick;
md->next_walktime = tick+rand()%50+5000;
md->last_linktime = tick;
md->last_pcneartime = 0;
if (!md->bl.prev)
map_addblock(&md->bl);
if(pcdb_checkid(md->vd->class_) && md->nd)
md->vd->dead_sit = 0;
else
clif_spawn(&md->bl);
skill_unit_move(&md->bl,tick,1);
mobskill_use(md, tick, MSC_SPAWN);
if (battle_config.show_mob_info&3)
clif_charnameack (0, &md->bl);
}
int mob_guardian_guildchange(struct block_list *bl,va_list ap)
{
struct mob_data *md;
struct guild* g;
nullpo_retr(0, bl);
nullpo_retr(0, md = (struct mob_data *)bl);
if (!md->guardian_data)
return 0;
if (md->guardian_data->castle->guild_id == 0)
{ //Castle with no owner? Delete the guardians.
if (md->class_ == MOBID_EMPERIUM)
{ //But don't delete the emperium, just clear it's guild-data
md->guardian_data->guild_id = 0;
md->guardian_data->emblem_id = 0;
md->guardian_data->guild_name[0] = '\0';
} else {
if( md->guardian_data->number >= 0 && md->guardian_data->number < MAX_GUARDIANS && md->guardian_data->castle->guardian[md->guardian_data->number].visible )
{ //Safe removal of guardian.
md->guardian_data->castle->guardian[md->guardian_data->number].visible = 0;
guild_castledatasave(md->guardian_data->castle->castle_id, 10+md->guardian_data->number,0);
}
unit_free(&md->bl,0); //Remove guardian.
}
return 0;
}
g = guild_search(md->guardian_data->castle->guild_id);
if (g == NULL)
{ //Properly remove guardian info from Castle data.
ShowError("mob_guardian_guildchange: New Guild (id %d) does not exists!\n", md->guardian_data->guild_id);
if( md->guardian_data->number >= 0 && md->guardian_data->number < MAX_GUARDIANS )
{
md->guardian_data->castle->guardian[md->guardian_data->number].visible = 0;
guild_castledatasave(md->guardian_data->castle->castle_id, 10+md->guardian_data->number,0);
}
unit_free(&md->bl,0);
return 0;
}
md->guardian_data->guild_id = g->guild_id;
md->guardian_data->emblem_id = g->emblem_id;
md->guardian_data->guardup_lv = guild_checkskill(g,GD_GUARDUP);
memcpy(md->guardian_data->guild_name, g->name, NAME_LENGTH);
return 1;
}
/*==========================================
* Pick a random class for the mob
*------------------------------------------*/
int mob_random_class (int *value, size_t count)
{
nullpo_retr(0, value);
// no count specified, look into the array manually, but take only max 5 elements
if (count < 1) {
count = 0;
while(count < 5 && mobdb_checkid(value[count])) count++;
if(count < 1) // nothing found
return 0;
} else {
// check if at least the first value is valid
if(mobdb_checkid(value[0]) == 0)
return 0;
}
//Pick a random value, hoping it exists. [Skotlex]
return mobdb_checkid(value[rand()%count]);
}
/*==========================================
* Change mob base class
*------------------------------------------*/
int mob_class_change (struct mob_data *md, int class_)
{
unsigned int tick = gettick();
int i, c, hp_rate;
nullpo_retr(0, md);
if (md->bl.prev == NULL)
return 0;
//Disable class changing for some targets...
if (md->guardian_data)
return 0; //Guardians/Emperium
if (md->class_ >= 1324 && md->class_ <= 1363)
return 0; //Treasure Boxes
if (md->special_state.ai > 1)
return 0; //Marine Spheres and Floras.
if (mob_is_clone(md->class_))
return 0; //Clones
if (md->class_ == class_)
return 0; //Nothing to change.
hp_rate = status_calc_life(md->status.hp, md->status.max_hp);
md->class_ = class_;
md->db = mob_db(class_);
if (battle_config.override_mob_names==1)
memcpy(md->name,md->db->name,NAME_LENGTH);
else
memcpy(md->name,md->db->jname,NAME_LENGTH);
mob_stop_attack(md);
mob_stop_walking(md, 0);
unit_skillcastcancel(&md->bl, 0);
status_set_viewdata(&md->bl, class_);
clif_mob_class_change(md,class_);
status_calc_mob(md, 3);
md->ud.state.speed_changed = 1; //Speed change update.
if (battle_config.monster_class_change_recover) {
memset(md->dmglog, 0, sizeof(md->dmglog));
md->tdmg = 0;
} else {
md->status.hp = md->status.max_hp*hp_rate/100;
if(md->status.hp < 1) md->status.hp = 1;
}
for(i=0,c=tick-1000*3600*10;i<MAX_MOBSKILL;i++)
md->skilldelay[i] = c;
if(md->lootitem == NULL && md->db->status.mode&MD_LOOTER)
md->lootitem=(struct item *)aCalloc(LOOTITEM_SIZE,sizeof(struct item));
//Targets should be cleared no morph
md->target_id = md->attacked_id = 0;
//Need to update name display.
clif_charnameack(0, &md->bl);
return 0;
}
/*==========================================
* mob��
*------------------------------------------*/
void mob_heal(struct mob_data *md,unsigned int heal)
{
if (battle_config.show_mob_info&3)
clif_charnameack (0, &md->bl);
}
/*==========================================
* Added by RoVeRT
*------------------------------------------*/
int mob_warpslave_sub(struct block_list *bl,va_list ap)
{
struct mob_data *md=(struct mob_data *)bl;
struct block_list *master;
short x,y,range=0;
master = va_arg(ap, struct block_list*);
range = va_arg(ap, int);
if(md->master_id!=master->id)
return 0;
map_search_freecell(master, 0, &x, &y, range, range, 0);
unit_warp(&md->bl, master->m, x, y,2);
return 1;
}
/*==========================================
* Added by RoVeRT
* Warps slaves. Range is the area around the master that they can
* appear in randomly.
*------------------------------------------*/
int mob_warpslave(struct block_list *bl, int range)
{
if (range < 1)
range = 1; //Min range needed to avoid crashes and stuff. [Skotlex]
return map_foreachinmap(mob_warpslave_sub, bl->m, BL_MOB, bl, range);
}
/*==========================================
* ��ʓ��̎�芪���̐��v�Z�p(foreachinarea)
*------------------------------------------*/
int mob_countslave_sub(struct block_list *bl,va_list ap)
{
int id;
struct mob_data *md;
id=va_arg(ap,int);
md = (struct mob_data *)bl;
if( md->master_id==id )
return 1;
return 0;
}
/*==========================================
* ��ʓ��̎�芪���̐��v�Z
*------------------------------------------*/
int mob_countslave(struct block_list *bl)
{
return map_foreachinmap(mob_countslave_sub, bl->m, BL_MOB,bl->id);
}
/*==========================================
* Summons amount slaves contained in the value[5] array using round-robin. [adapted by Skotlex]
*------------------------------------------*/
int mob_summonslave(struct mob_data *md2,int *value,int amount,int skill_id)
{
struct mob_data *md;
struct spawn_data data;
int count = 0,k=0,hp_rate=0;
nullpo_retr(0, md2);
nullpo_retr(0, value);
memset(&data, 0, sizeof(struct spawn_data));
data.m = md2->bl.m;
data.x = md2->bl.x;
data.y = md2->bl.y;
data.num = 1;
data.state.size = md2->special_state.size;
data.state.ai = md2->special_state.ai;
if(mobdb_checkid(value[0]) == 0)
return 0;
while(count < 5 && mobdb_checkid(value[count])) count++;
if(count < 1) return 0;
if (amount > 0 && amount < count) { //Do not start on 0, pick some random sub subset [Skotlex]
k = rand()%count;
amount+=k; //Increase final value by same amount to preserve total number to summon.
}
if (!battle_config.monster_class_change_recover &&
(skill_id == NPC_TRANSFORMATION || skill_id == NPC_METAMORPHOSIS))
hp_rate = status_calc_life(md2->status.hp, md2->status.max_hp);
for(;k<amount;k++) {
short x,y;
data.class_ = value[k%count]; //Summon slaves in round-robin fashion. [Skotlex]
if (mobdb_checkid(data.class_) == 0)
continue;
if (map_search_freecell(&md2->bl, 0, &x, &y, MOB_SLAVEDISTANCE, MOB_SLAVEDISTANCE, 0)) {
data.x = x;
data.y = y;
} else {
data.x = md2->bl.x;
data.y = md2->bl.y;
}
//These two need to be loaded from the db for each slave.
if(battle_config.override_mob_names==1)
strcpy(data.name,"--en--");
else
strcpy(data.name,"--ja--");
data.level = 0;
if (!mob_parse_dataset(&data))
continue;
md= mob_spawn_dataset(&data);
if(skill_id == NPC_SUMMONSLAVE){
md->master_id=md2->bl.id;
md->state.killer = md2->state.killer;
md->special_state.ai = md2->special_state.ai;
md->nd = md2->nd;
md->callback_flag = md2->callback_flag;
}
mob_spawn(md);
if (hp_rate) //Scale HP
md->status.hp = md->status.max_hp*hp_rate/100;
//Inherit the aggressive mode of the master.
if (battle_config.slaves_inherit_mode && md->master_id)
{
switch (battle_config.slaves_inherit_mode) {
case 1: //Always aggressive
if (!(md->status.mode&MD_AGGRESSIVE))
sc_start4(&md->bl, SC_MODECHANGE, 100,1,0, MD_AGGRESSIVE, 0, 0);
break;
case 2: //Always passive
if (md->status.mode&MD_AGGRESSIVE)
sc_start4(&md->bl, SC_MODECHANGE, 100,1,0, 0, MD_AGGRESSIVE, 0);
break;
default: //Copy master.
if (md2->status.mode&MD_AGGRESSIVE)
sc_start4(&md->bl, SC_MODECHANGE, 100,1,0, MD_AGGRESSIVE, 0, 0);
else
sc_start4(&md->bl, SC_MODECHANGE, 100,1,0, 0, MD_AGGRESSIVE, 0);
break;
}
}
clif_skill_nodamage(&md->bl,&md->bl,skill_id,amount,1);
}
return 0;
}
/*==========================================
*MOBskill����Y��skillid��skillidx��Ԃ�
*------------------------------------------*/
int mob_skillid2skillidx(int class_,int skillid)
{
int i, max = mob_db(class_)->maxskill;
struct mob_skill *ms=mob_db(class_)->skill;
if(ms==NULL)
return -1;
ARR_FIND( 0, max, i, ms[i].skill_id == skillid );
return ( i < max ) ? i : -1;
}
/*==========================================
* Friendly Mob whose HP is decreasing by a nearby MOB is looked for.
*------------------------------------------*/
int mob_getfriendhprate_sub(struct block_list *bl,va_list ap)
{
int min_rate, max_rate,rate;
struct block_list **fr;
struct mob_data *md;
md = va_arg(ap,struct mob_data *);
min_rate=va_arg(ap,int);
max_rate=va_arg(ap,int);
fr=va_arg(ap,struct block_list **);
if( md->bl.id == bl->id && !(battle_config.mob_ai&0x10))
return 0;
if ((*fr) != NULL) //A friend was already found.
return 0;
if (battle_check_target(&md->bl,bl,BCT_ENEMY)>0)
return 0;
rate = status_calc_life(status_get_hp(bl), status_get_max_hp(bl));
if (rate >= min_rate && rate <= max_rate)
(*fr) = bl;
return 1;
}
static struct block_list *mob_getfriendhprate(struct mob_data *md,int min_rate,int max_rate)
{
struct block_list *fr=NULL;
int type = BL_MOB;
nullpo_retr(NULL, md);
if (md->special_state.ai) //Summoned creatures. [Skotlex]
type = BL_PC;
map_foreachinrange(mob_getfriendhprate_sub, &md->bl, 8, type,md,min_rate,max_rate,&fr);
return fr;
}
/*==========================================
* Check hp rate of its master
*------------------------------------------*/
struct block_list *mob_getmasterhpltmaxrate(struct mob_data *md,int rate)
{
if( md && md->master_id > 0 )
{
struct block_list *bl = map_id2bl(md->master_id);
if( bl && status_calc_life(status_get_hp(bl), status_get_max_hp(bl)) < rate )
return bl;
}
return NULL;
}
/*==========================================
* What a status state suits by nearby MOB is looked for.
*------------------------------------------*/
int mob_getfriendstatus_sub(struct block_list *bl,va_list ap)
{
int cond1,cond2;
struct mob_data **fr, *md, *mmd;
int flag=0;
nullpo_retr(0, bl);
nullpo_retr(0, ap);
nullpo_retr(0, md=(struct mob_data *)bl);
nullpo_retr(0, mmd=va_arg(ap,struct mob_data *));
if( mmd->bl.id == bl->id && !(battle_config.mob_ai&0x10) )
return 0;
if (battle_check_target(&mmd->bl,bl,BCT_ENEMY)>0)
return 0;
cond1=va_arg(ap,int);
cond2=va_arg(ap,int);
fr=va_arg(ap,struct mob_data **);
if( cond2==-1 ){
int j;
for(j=SC_COMMON_MIN;j<=SC_COMMON_MAX && !flag;j++){
if ((flag=(md->sc.data[j] != NULL))) //Once an effect was found, break out. [Skotlex]
break;
}
}else
flag=( md->sc.data[cond2] != NULL );
if( flag^( cond1==MSC_FRIENDSTATUSOFF ) )
(*fr)=md;
return 0;
}
struct mob_data *mob_getfriendstatus(struct mob_data *md,int cond1,int cond2)
{
struct mob_data* fr = NULL;
nullpo_retr(0, md);
map_foreachinrange(mob_getfriendstatus_sub, &md->bl, 8,BL_MOB, md,cond1,cond2,&fr);
return fr;
}
/*==========================================
* Skill use judging
*------------------------------------------*/
int mobskill_use(struct mob_data *md, unsigned int tick, int event)
{
struct mob_skill *ms;
struct block_list *fbl = NULL; //Friend bl, which can either be a BL_PC or BL_MOB depending on the situation. [Skotlex]
struct block_list *bl;
struct mob_data *fmd = NULL;
int i,j,n;
nullpo_retr (0, md);
nullpo_retr (0, ms = md->db->skill);
if (!battle_config.mob_skill_rate || md->ud.skilltimer != -1 || !md->db->maxskill)
return 0;
if (event == -1 && DIFF_TICK(md->ud.canact_tick, tick) > 0)
return 0; //Skill act delay only affects non-event skills.
//Pick a starting position and loop from that.
i = battle_config.mob_ai&0x100?rand()%md->db->maxskill:0;
for (n = 0; n < md->db->maxskill; i++, n++) {
int c2, flag = 0;
if (i == md->db->maxskill)
i = 0;
if (DIFF_TICK(tick, md->skilldelay[i]) < ms[i].delay)
continue;
c2 = ms[i].cond2;
if (ms[i].state != md->state.skillstate) {
if (md->state.skillstate != MSS_DEAD && (ms[i].state == MSS_ANY ||
(ms[i].state == MSS_ANYTARGET && md->target_id && md->state.skillstate != MSS_LOOT)
)) //ANYTARGET works with any state as long as there's a target. [Skotlex]
;
else
continue;
}
if (rand() % 10000 > ms[i].permillage) //Lupus (max value = 10000)
continue;
if (ms[i].cond1 == event)
flag = 1; //Trigger skill.
else if (ms[i].cond1 == MSC_SKILLUSED)
flag = ((event & 0xffff) == MSC_SKILLUSED && ((event >> 16) == c2 || c2 == 0));
else if(event == -1){
//Avoid entering on defined events to avoid "hyper-active skill use" due to the overflow of calls to this function in battle.
switch (ms[i].cond1)
{
case MSC_ALWAYS:
flag = 1; break;
case MSC_MYHPLTMAXRATE: // HP< maxhp%
flag = status_calc_life(md->status.hp, md->status.max_hp);
flag = (flag <= c2);
break;
case MSC_MYHPINRATE:
flag = status_calc_life(md->status.hp, md->status.max_hp);
flag = (flag >= c2 && flag <= ms[i].val[0]);
break;
case MSC_MYSTATUSON: // status[num] on
case MSC_MYSTATUSOFF: // status[num] off
if (!md->sc.count) {
flag = 0;
} else if (ms[i].cond2 == -1) {
for (j = SC_COMMON_MIN; j <= SC_COMMON_MAX; j++)
if ((flag = (md->sc.data[j]!=NULL)) != 0)
break;
} else {
flag = (md->sc.data[ms[i].cond2]!=NULL);
}
flag ^= (ms[i].cond1 == MSC_MYSTATUSOFF); break;
case MSC_FRIENDHPLTMAXRATE: // friend HP < maxhp%
flag = ((fbl = mob_getfriendhprate(md, 0, ms[i].cond2)) != NULL); break;
case MSC_FRIENDHPINRATE :
flag = ((fbl = mob_getfriendhprate(md, ms[i].cond2, ms[i].val[0])) != NULL); break;
case MSC_FRIENDSTATUSON: // friend status[num] on
case MSC_FRIENDSTATUSOFF: // friend status[num] off
flag = ((fmd = mob_getfriendstatus(md, ms[i].cond1, ms[i].cond2)) != NULL); break;
case MSC_SLAVELT: // slave < num
flag = (mob_countslave(&md->bl) < c2 ); break;
case MSC_ATTACKPCGT: // attack pc > num
flag = (unit_counttargeted(&md->bl, 0) > c2); break;
case MSC_SLAVELE: // slave <= num
flag = (mob_countslave(&md->bl) <= c2 ); break;
case MSC_ATTACKPCGE: // attack pc >= num
flag = (unit_counttargeted(&md->bl, 0) >= c2); break;
case MSC_AFTERSKILL:
flag = (md->ud.skillid == c2); break;
case MSC_RUDEATTACKED:
flag = (md->state.attacked_count >= RUDE_ATTACKED_COUNT);
if (flag) md->state.attacked_count = 0; //Rude attacked count should be reset after the skill condition is met. Thanks to Komurka [Skotlex]
break;
case MSC_MASTERHPLTMAXRATE:
flag = ((fbl = mob_getmasterhpltmaxrate(md, ms[i].cond2)) != NULL); break;
case MSC_MASTERATTACKED:
flag = (md->master_id > 0 && (fbl=map_id2bl(md->master_id)) && unit_counttargeted(fbl, 0) > 0); break;
case MSC_ALCHEMIST:
flag = (md->state.alchemist);
break;
}
}
if (!flag)
continue; //Skill requisite failed to be fulfilled.
//Execute skill
if (skill_get_casttype(ms[i].skill_id) == CAST_GROUND)
{ //Ground skill.
short x, y;
switch (ms[i].target) {
case MST_RANDOM: //Pick a random enemy within skill range.
bl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),
skill_get_range2(&md->bl, ms[i].skill_id, ms[i].skill_lv));
break;
case MST_TARGET:
case MST_AROUND5:
case MST_AROUND6:
case MST_AROUND7:
case MST_AROUND8:
bl = map_id2bl(md->target_id);
break;
case MST_MASTER:
bl = &md->bl;
if (md->master_id)
bl = map_id2bl(md->master_id);
if (bl) //Otherwise, fall through.
break;
case MST_FRIEND:
bl = fbl?fbl:(fmd?&fmd->bl:&md->bl);
break;
default:
bl = &md->bl;
break;
}
if (!bl) continue;
x = bl->x;
y = bl->y;
// Look for an area to cast the spell around...
if (ms[i].target >= MST_AROUND1 || ms[i].target >= MST_AROUND5) {
j = ms[i].target >= MST_AROUND1?
(ms[i].target-MST_AROUND1) +1:
(ms[i].target-MST_AROUND5) +1;
map_search_freecell(&md->bl, md->bl.m, &x, &y, j, j, 3);
}
md->skillidx = i;
if (!unit_skilluse_pos2(&md->bl, x, y,
ms[i].skill_id, ms[i].skill_lv,
ms[i].casttime, ms[i].cancel))
continue;
} else {
//Targetted skill
switch (ms[i].target) {
case MST_RANDOM: //Pick a random enemy within skill range.
bl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),
skill_get_range2(&md->bl, ms[i].skill_id, ms[i].skill_lv));
break;
case MST_TARGET:
bl = map_id2bl(md->target_id);
break;
case MST_MASTER:
bl = &md->bl;
if (md->master_id)
bl = map_id2bl(md->master_id);
if (bl) //Otherwise, fall through.
break;
case MST_FRIEND:
if (fbl) {
bl = fbl;
break;
} else if (fmd) {
bl = &fmd->bl;
break;
} // else fall through
default:
bl = &md->bl;
break;
}
if (!bl) continue;
md->skillidx = i;
if (!unit_skilluse_id2(&md->bl, bl->id,
ms[i].skill_id, ms[i].skill_lv,
ms[i].casttime, ms[i].cancel))
continue;
}
//Skill used. Post-setups...
if(battle_config.mob_ai&0x200)
{ //pass on delay to same skill.
for (j = 0; j < md->db->maxskill; j++)
if (md->db->skill[j].skill_id == ms[i].skill_id)
md->skilldelay[j]=tick;
} else
md->skilldelay[i]=tick;
return 1;
}
//No skill was used.
md->skillidx = -1;
return 0;
}
/*==========================================
* Skill use event processing
*------------------------------------------*/
int mobskill_event(struct mob_data *md, struct block_list *src, unsigned int tick, int flag)
{
int target_id, res = 0;
target_id = md->target_id;
if (!target_id || battle_config.mob_changetarget_byskill)
md->target_id = src->id;
if (flag == -1)
res = mobskill_use(md, tick, MSC_CASTTARGETED);
else if ((flag&0xffff) == MSC_SKILLUSED)
res = mobskill_use(md, tick, flag);
else if (flag&BF_SHORT)
res = mobskill_use(md, tick, MSC_CLOSEDATTACKED);
else if (flag&BF_LONG && !(flag&BF_MAGIC)) //Long-attacked should not include magic.
res = mobskill_use(md, tick, MSC_LONGRANGEATTACKED);
if (!res)
//Restore previous target only if skill condition failed to trigger. [Skotlex]
md->target_id = target_id;
//Otherwise check if the target is an enemy, and unlock if needed.
else if (battle_check_target(&md->bl, src, BCT_ENEMY) <= 0)
md->target_id = target_id;
return res;
}
// Player cloned mobs. [Valaris]
int mob_is_clone(int class_)
{
if(class_ < MOB_CLONE_START || class_ > MOB_CLONE_END)
return 0;
if (mob_db(class_) == mob_dummy)
return 0;
return class_;
}
//Flag values:
//&1: Set special ai (fight mobs, not players)
//If mode is not passed, a default aggressive mode is used.
//If master_id is passed, clone is attached to him.
//Returns: ID of newly crafted copy.
int mob_clone_spawn(struct map_session_data *sd, int m, int x, int y, const char *event, int master_id, int mode, int flag, unsigned int duration)
{
int class_;
int i,j,inf,skill_id;
struct mob_data *md;
struct mob_skill *ms;
struct mob_db* db;
struct status_data *status;
nullpo_retr(0, sd);
ARR_FIND( MOB_CLONE_START, MOB_CLONE_END, class_, mob_db_data[class_] == NULL );
if(class_ >= MOB_CLONE_END)
return 0;
db = mob_db_data[class_]=(struct mob_db*)aCalloc(1, sizeof(struct mob_db));
status = &db->status;
sprintf(db->sprite,sd->status.name);
sprintf(db->name,sd->status.name);
sprintf(db->jname,sd->status.name);
db->lv=status_get_lv(&sd->bl);
memcpy(status, &sd->base_status, sizeof(struct status_data));
status->rhw.atk2= status->dex + status->rhw.atk + status->rhw.atk2; //Max ATK
status->rhw.atk = status->dex; //Min ATK
if (status->lhw.atk) {
status->lhw.atk2= status->dex + status->lhw.atk + status->lhw.atk2; //Max ATK
status->lhw.atk = status->dex; //Min ATK
}
if (mode) //User provided mode.
status->mode = mode;
else if (flag&1) //Friendly Character, remove looting.
status->mode &= ~MD_LOOTER;
status->hp = status->max_hp;
status->sp = status->max_sp;
memcpy(&db->vd, &sd->vd, sizeof(struct view_data));
db->base_exp=1;
db->job_exp=1;
db->range2=AREA_SIZE; //Let them have the same view-range as players.
db->range3=AREA_SIZE; //Min chase of a screen.
db->option=sd->sc.option;
//Skill copy [Skotlex]
ms = &db->skill[0];
//Go Backwards to give better priority to advanced skills.
for (i=0,j = MAX_SKILL_TREE-1;j>=0 && i< MAX_MOBSKILL ;j--) {
skill_id = skill_tree[pc_class2idx(sd->status.class_)][j].id;
if (!skill_id || sd->status.skill[skill_id].lv < 1 ||
(skill_get_inf2(skill_id)&(INF2_WEDDING_SKILL|INF2_GUILD_SKILL)) ||
skill_get_nocast(skill_id)&16
)
continue;
//Normal aggressive mob, disable skills that cannot help them fight
//against players (those with flags UF_NOMOB and UF_NOPC are specific
//to always aid players!) [Skotlex]
if (!(flag&1) &&
skill_get_unit_id(skill_id, 0) &&
skill_get_unit_flag(skill_id)&(UF_NOMOB|UF_NOPC))
continue;
memset (&ms[i], 0, sizeof(struct mob_skill));
ms[i].skill_id = skill_id;
ms[i].skill_lv = sd->status.skill[skill_id].lv;
ms[i].state = MSS_ANY;
ms[i].permillage = 500*battle_config.mob_skill_rate/100; //Default chance of all skills: 5%
ms[i].emotion = -1;
ms[i].cancel = 0;
ms[i].casttime = skill_castfix(&sd->bl,skill_id, ms[i].skill_lv);
ms[i].delay = 5000+skill_delayfix(&sd->bl,skill_id, ms[i].skill_lv);
inf = skill_get_inf(skill_id);
if (inf&INF_ATTACK_SKILL) {
ms[i].target = MST_TARGET;
ms[i].cond1 = MSC_ALWAYS;
if (skill_get_range(skill_id, ms[i].skill_lv) > 3)
ms[i].state = MSS_ANYTARGET;
else
ms[i].state = MSS_BERSERK;
} else if(inf&INF_GROUND_SKILL) {
if (skill_get_inf2(skill_id)&INF2_TRAP) { //Traps!
ms[i].state = MSS_IDLE;
ms[i].target = MST_AROUND2;
ms[i].delay = 60000;
} else if (skill_get_unit_target(skill_id) == BCT_ENEMY) { //Target Enemy
ms[i].state = MSS_ANYTARGET;
ms[i].target = MST_TARGET;
ms[i].cond1 = MSC_ALWAYS;
} else { //Target allies
ms[i].target = MST_FRIEND;
ms[i].cond1 = MSC_FRIENDHPLTMAXRATE;
ms[i].cond2 = 95;
}
} else if (inf&INF_SELF_SKILL) {
if (skill_get_inf2(skill_id)&INF2_NO_TARGET_SELF) { //auto-select target skill.
ms[i].target = MST_TARGET;
ms[i].cond1 = MSC_ALWAYS;
if (skill_get_range(skill_id, ms[i].skill_lv) > 3) {
ms[i].state = MSS_ANYTARGET;
} else {
ms[i].state = MSS_BERSERK;
}
} else { //Self skill
ms[i].target = MST_SELF;
ms[i].cond1 = MSC_MYHPLTMAXRATE;
ms[i].cond2 = 90;
ms[i].permillage = 2000;
//Delay: Remove the stock 5 secs and add half of the support time.
ms[i].delay += -5000 +(skill_get_time(skill_id, ms[i].skill_lv) + skill_get_time2(skill_id, ms[i].skill_lv))/2;
if (ms[i].delay < 5000)
ms[i].delay = 5000; //With a minimum of 5 secs.
}
} else if (inf&INF_SUPPORT_SKILL) {
ms[i].target = MST_FRIEND;
ms[i].cond1 = MSC_FRIENDHPLTMAXRATE;
ms[i].cond2 = 90;
if (skill_id == AL_HEAL)
ms[i].permillage = 5000; //Higher skill rate usage for heal.
else if (skill_id == ALL_RESURRECTION)
ms[i].cond2 = 1;
//Delay: Remove the stock 5 secs and add half of the support time.
ms[i].delay += -5000 +(skill_get_time(skill_id, ms[i].skill_lv) + skill_get_time2(skill_id, ms[i].skill_lv))/2;
if (ms[i].delay < 2000)
ms[i].delay = 2000; //With a minimum of 2 secs.
if (i+1 < MAX_MOBSKILL) { //duplicate this so it also triggers on self.
memcpy(&ms[i+1], &ms[i], sizeof(struct mob_skill));
db->maxskill = ++i;
ms[i].target = MST_SELF;
ms[i].cond1 = MSC_MYHPLTMAXRATE;
}
} else {
switch (skill_id) { //Certain Special skills that are passive, and thus, never triggered.
case MO_TRIPLEATTACK:
case TF_DOUBLE:
case GS_CHAINACTION:
ms[i].state = MSS_BERSERK;
ms[i].target = MST_TARGET;
ms[i].cond1 = MSC_ALWAYS;
ms[i].permillage = skill_id==MO_TRIPLEATTACK?(3000-ms[i].skill_lv*100):(ms[i].skill_lv*500);
ms[i].delay -= 5000; //Remove the added delay as these could trigger on "all hits".
break;
default: //Untreated Skill
continue;
}
}
if (battle_config.mob_skill_rate!= 100)
ms[i].permillage = ms[i].permillage*battle_config.mob_skill_rate/100;
if (battle_config.mob_skill_delay != 100)
ms[i].delay = ms[i].delay*battle_config.mob_skill_delay/100;
db->maxskill = ++i;
}
//Finally, spawn it.
md = mob_once_spawn_sub(&sd->bl, m, x, y, "--en--",class_,event);
if (!md) return 0; //Failed?
if (master_id || flag || duration) { //Further manipulate crafted char.
if (flag&1) //Friendly Character
md->special_state.ai = 1;
if (master_id) //Attach to Master
md->master_id = master_id;
if (duration) //Auto Delete after a while.
md->deletetimer = add_timer (gettick() + duration, mob_timer_delete, md->bl.id, 0);
}
#if 0
//I am playing with this for packet-research purposes, enable it if you want, but don't remove it :X [Skotlex]
//Guardian data
if (sd->status.guild_id) {
struct guild* g = guild_search(sd->status.guild_id);
md->guardian_data = aCalloc(1, sizeof(struct guardian_data));
md->guardian_data->castle = NULL;
md->guardian_data->number = MAX_GUARDIANS;
md->guardian_data->guild_id = sd->status.guild_id;
if (g)
{
md->guardian_data->emblem_id = g->emblem_id;
memcpy(md->guardian_data->guild_name, g->name, NAME_LENGTH);
}
}
#endif
mob_spawn(md);
return md->bl.id;
}
int mob_clone_delete(int class_)
{
if (class_ >= MOB_CLONE_START && class_ < MOB_CLONE_END
&& mob_db_data[class_]!=NULL) {
aFree(mob_db_data[class_]);
mob_db_data[class_]=NULL;
return 1;
}
return 0;
}
int mob_script_callback(struct mob_data *md, struct block_list *target, short action_type)
{
// DEBUG: Uncomment these if errors occur. ---
// nullpo_retr(md, 0);
// nullpo_retr(md->nd, 0);
// -------------------------------------------
if(md->callback_flag&action_type){
int regkey = add_str(".ai_action");
linkdb_replace(&md->nd->u.scr.script->script_vars,(void *)regkey, (void *)(int)action_type);
if(target){
linkdb_replace(&md->nd->u.scr.script->script_vars,(void *)(regkey+(1<<24)), (void *)(int)target->type);
linkdb_replace(&md->nd->u.scr.script->script_vars,(void *)(regkey+(2<<24)), (void *)target->id);
}
linkdb_replace(&md->nd->u.scr.script->script_vars,(void *)(regkey+(3<<24)), (void *)md->bl.id);
run_script(md->nd->u.scr.script, 0, 0, md->nd->bl.id);
return 1;
}
return 0;
}
//
// ������
//
/*==========================================
* Since un-setting [ mob ] up was used, it is an initial provisional value setup.
*------------------------------------------*/
static int mob_makedummymobdb(int class_)
{
if (mob_dummy != NULL)
{
if (mob_db(class_) == mob_dummy)
return 1; //Using the mob_dummy data already. [Skotlex]
if (class_ > 0 && class_ <= MAX_MOB_DB)
{ //Remove the mob data so that it uses the dummy data instead.
aFree(mob_db_data[class_]);
mob_db_data[class_] = NULL;
}
return 0;
}
//Initialize dummy data.
mob_dummy = (struct mob_db*)aCalloc(1, sizeof(struct mob_db)); //Initializing the dummy mob.
sprintf(mob_dummy->sprite,"DUMMY");
sprintf(mob_dummy->name,"Dummy");
sprintf(mob_dummy->jname,"Dummy");
mob_dummy->lv=1;
mob_dummy->status.max_hp=1000;
mob_dummy->status.max_sp=1;
mob_dummy->status.rhw.range=1;
mob_dummy->status.rhw.atk=7;
mob_dummy->status.rhw.atk2=10;
mob_dummy->status.str=1;
mob_dummy->status.agi=1;
mob_dummy->status.vit=1;
mob_dummy->status.int_=1;
mob_dummy->status.dex=6;
mob_dummy->status.luk=2;
mob_dummy->status.speed=300;
mob_dummy->status.adelay=1000;
mob_dummy->status.amotion=500;
mob_dummy->status.dmotion=500;
mob_dummy->base_exp=2;
mob_dummy->job_exp=1;
mob_dummy->range2=10;
mob_dummy->range3=10;
return 0;
}
//Adjusts the drop rate of item according to the criteria given. [Skotlex]
static unsigned int mob_drop_adjust(int baserate, int rate_adjust, unsigned short rate_min, unsigned short rate_max)
{
double rate = baserate;
if (battle_config.logarithmic_drops && rate_adjust > 0 && baserate > 0) //Logarithmic drops equation by Ishizu-Chan
//Equation: Droprate(x,y) = x * (5 - log(x)) ^ (ln(y) / ln(5))
//x is the normal Droprate, y is the Modificator.
rate = rate * pow((5.0 - log10(rate)), (log(rate_adjust/100.) / log(5.0))) + 0.5;
else
//Classical linear rate adjustment.
rate = rate * rate_adjust/100;
return (unsigned int)cap_value(rate,rate_min,rate_max);
}
/*==========================================
* processes one mobdb entry
*------------------------------------------*/
static bool mob_parse_dbrow(char** str)
{
struct mob_db *db;
struct status_data *status;
int class_, i, k;
double exp, maxhp;
struct mob_data data;
class_ = str[0] ? atoi(str[0]) : 0;
if (class_ == 0)
return false; //Leave blank lines alone... [Skotlex]
if (class_ <= 1000 || class_ > MAX_MOB_DB) {
ShowWarning("Mob with ID: %d not loaded. ID must be in range [%d-%d]\n", class_, 1000, MAX_MOB_DB);
return false;
}
if (pcdb_checkid(class_)) {
ShowWarning("Mob with ID: %d not loaded. That ID is reserved for player classes.\n");
return false;
}
if (class_ >= MOB_CLONE_START && class_ < MOB_CLONE_END) {
ShowWarning("Mob with ID: %d not loaded. Range %d-%d is reserved for player clones. Please increase MAX_MOB_DB (%d)\n", class_, MOB_CLONE_START, MOB_CLONE_END-1, MAX_MOB_DB);
return false;
}
if (mob_db_data[class_] == NULL)
mob_db_data[class_] = (struct mob_db*)aCalloc(1, sizeof (struct mob_db));
db = mob_db_data[class_];
status = &db->status;
db->vd.class_ = class_;
strncpy(db->sprite, str[1], NAME_LENGTH);
strncpy(db->jname, str[2], NAME_LENGTH);
strncpy(db->name, str[3], NAME_LENGTH);
db->lv = atoi(str[4]);
db->lv = cap_value(db->lv, 1, USHRT_MAX);
status->max_hp = atoi(str[5]);
status->max_sp = atoi(str[6]);
exp = (double)atoi(str[7]) * (double)battle_config.base_exp_rate / 100.;
db->base_exp = (unsigned int)cap_value(exp, 0, UINT_MAX);
exp = (double)atoi(str[8]) * (double)battle_config.job_exp_rate / 100.;
db->job_exp = (unsigned int)cap_value(exp, 0, UINT_MAX);
status->rhw.range = atoi(str[9]);
status->rhw.atk = atoi(str[10]);
status->rhw.atk2 = atoi(str[11]);
status->def = atoi(str[12]);
status->mdef = atoi(str[13]);
status->str = atoi(str[14]);
status->agi = atoi(str[15]);
status->vit = atoi(str[16]);
status->int_ = atoi(str[17]);
status->dex = atoi(str[18]);
status->luk = atoi(str[19]);
//All status should be min 1 to prevent divisions by zero from some skills. [Skotlex]
if (status->str < 1) status->str = 1;
if (status->agi < 1) status->agi = 1;
if (status->vit < 1) status->vit = 1;
if (status->int_< 1) status->int_= 1;
if (status->dex < 1) status->dex = 1;
if (status->luk < 1) status->luk = 1;
db->range2 = atoi(str[20]);
db->range3 = atoi(str[21]);
if (battle_config.view_range_rate != 100) {
db->range2 = db->range2 * battle_config.view_range_rate / 100;
if (db->range2 < 1)
db->range2 = 1;
}
if (battle_config.chase_range_rate != 100) {
db->range3 = db->range3 * battle_config.chase_range_rate / 100;
if (db->range3 < db->range2)
db->range3 = db->range2;
}
status->size = atoi(str[22]);
status->race = atoi(str[23]);
i = atoi(str[24]); //Element
status->def_ele = i%10;
status->ele_lv = i/20;
if (status->def_ele >= ELE_MAX) {
ShowWarning("Mob with ID: %d has invalid element type %d (max element is %d)\n", class_, status->def_ele, ELE_MAX-1);
status->def_ele = ELE_NEUTRAL;
}
if (status->ele_lv < 1 || status->ele_lv > 4) {
ShowWarning("Mob with ID: %d has invalid element level %d (max is 4)\n", class_, status->ele_lv);
status->ele_lv = 1;
}
status->mode = (int)strtol(str[25], NULL, 0);
if (!battle_config.monster_active_enable)
status->mode &= ~MD_AGGRESSIVE;
status->speed = atoi(str[26]);
status->aspd_rate = 1000;
status->adelay = atoi(str[27]);
status->amotion = atoi(str[28]);
//If the attack animation is longer than the delay, the client crops the attack animation!
//On aegis there is no real visible effect of having a recharge-time less than amotion anyway.
if (status->adelay < status->amotion)
status->adelay = status->amotion;
status->dmotion = atoi(str[29]);
if(battle_config.monster_damage_delay_rate != 100)
status->dmotion = status->dmotion * battle_config.monster_damage_delay_rate / 100;
data.bl.type = BL_MOB;
data.level = db->lv;
memcpy(&data.status, status, sizeof(struct status_data));
status_calc_misc(&data.bl, status, db->lv);
// MVP EXP Bonus, Chance: MEXP,ExpPer
// Some new MVP's MEXP multipled by high exp-rate cause overflow. [LuzZza]
exp = (double)atoi(str[30]) * (double)battle_config.mvp_exp_rate / 100.;
db->mexp = (unsigned int)cap_value(exp, 0, UINT_MAX);
db->mexpper = atoi(str[31]);
//Now that we know if it is an mvp or not, apply battle_config modifiers [Skotlex]
maxhp = (double)status->max_hp;
if (db->mexp > 0) { //Mvp
if (battle_config.mvp_hp_rate != 100)
maxhp = maxhp * (double)battle_config.mvp_hp_rate / 100.;
} else //Normal mob
if (battle_config.monster_hp_rate != 100)
maxhp = maxhp * (double)battle_config.monster_hp_rate / 100.;
status->max_hp = (unsigned int)cap_value(maxhp, 1, UINT_MAX);
if(status->max_sp < 1) status->max_sp = 1;
//Since mobs always respawn with full life...
status->hp = status->max_hp;
status->sp = status->max_sp;
// MVP Drops: MVP1id,MVP1per,MVP2id,MVP2per,MVP3id,MVP3per
for(i = 0; i < 3; i++) {
struct item_data *id;
db->mvpitem[i].nameid = atoi(str[32+i*2]);
if (!db->mvpitem[i].nameid) {
db->mvpitem[i].p = 0; //No item....
continue;
}
db->mvpitem[i].p = mob_drop_adjust(atoi(str[33+i*2]), battle_config.item_rate_mvp, battle_config.item_drop_mvp_min, battle_config.item_drop_mvp_max);
//calculate and store Max available drop chance of the MVP item
if (db->mvpitem[i].p) {
id = itemdb_search(db->mvpitem[i].nameid);
if (id->maxchance == 10000 || (id->maxchance < db->mvpitem[i].p/10 + 1) ) {
//item has bigger drop chance or sold in shops
id->maxchance = db->mvpitem[i].p/10 + 1; //reduce MVP drop info to not spoil common drop rate
}
}
}
for(i = 0; i < MAX_MOB_DROP; i++) {
int rate = 0, rate_adjust, type;
unsigned short ratemin, ratemax;
struct item_data *id;
k = 38+i*2;
db->dropitem[i].nameid = atoi(str[k]);
if (!db->dropitem[i].nameid) {
db->dropitem[i].p = 0; //No drop.
continue;
}
type = itemdb_type(db->dropitem[i].nameid);
rate = atoi(str[k+1]);
if (class_ >= 1324 && class_ <= 1363)
{ //Treasure box drop rates [Skotlex]
rate_adjust = battle_config.item_rate_treasure;
ratemin = battle_config.item_drop_treasure_min;
ratemax = battle_config.item_drop_treasure_max;
}
else switch (type)
{ // Added suport to restrict normal drops of MVP's [Reddozen]
case IT_HEALING:
rate_adjust = (status->mode&MD_BOSS) ? battle_config.item_rate_heal_boss : battle_config.item_rate_heal;
ratemin = battle_config.item_drop_heal_min;
ratemax = battle_config.item_drop_heal_max;
break;
case IT_USABLE:
rate_adjust = (status->mode&MD_BOSS) ? battle_config.item_rate_use_boss : battle_config.item_rate_use;
ratemin = battle_config.item_drop_use_min;
ratemax = battle_config.item_drop_use_max;
break;
case IT_WEAPON:
case IT_ARMOR:
case IT_PETARMOR:
rate_adjust = (status->mode&MD_BOSS) ? battle_config.item_rate_equip_boss : battle_config.item_rate_equip;
ratemin = battle_config.item_drop_equip_min;
ratemax = battle_config.item_drop_equip_max;
break;
case IT_CARD:
rate_adjust = (status->mode&MD_BOSS) ? battle_config.item_rate_card_boss : battle_config.item_rate_card;
ratemin = battle_config.item_drop_card_min;
ratemax = battle_config.item_drop_card_max;
break;
default:
rate_adjust = (status->mode&MD_BOSS) ? battle_config.item_rate_common_boss : battle_config.item_rate_common;
ratemin = battle_config.item_drop_common_min;
ratemax = battle_config.item_drop_common_max;
break;
}
db->dropitem[i].p = mob_drop_adjust(rate, rate_adjust, ratemin, ratemax);
//calculate and store Max available drop chance of the item
if (db->dropitem[i].p && (class_ < 1324 || class_ > 1363)) { //Skip treasure chests.
id = itemdb_search(db->dropitem[i].nameid);
if (id->maxchance == 10000 || (id->maxchance < db->dropitem[i].p) ) {
id->maxchance = db->dropitem[i].p; //item has bigger drop chance or sold in shops
}
for (k = 0; k< MAX_SEARCH; k++) {
if (id->mob[k].chance <= db->dropitem[i].p)
break;
}
if (k == MAX_SEARCH)
continue;
if (id->mob[k].id != class_)
memmove(&id->mob[k+1], &id->mob[k], (MAX_SEARCH-k-1)*sizeof(id->mob[0]));
id->mob[k].chance = db->dropitem[i].p;
id->mob[k].id = class_;
}
}
return true;
}
/*==========================================
* mob_db.txt reading
*------------------------------------------*/
static int mob_readdb(void)
{
const char* filename[] = { "mob_db.txt", "mob_db2.txt" };
int fi;
for( fi = 0; fi < ARRAYLENGTH(filename); ++fi )
{
uint32 lines = 0, count = 0;
char line[1024];
char path[256];
FILE* fp;
sprintf(path, "%s/%s", db_path, filename[fi]);
fp = fopen(path, "r");
if(fp == NULL) {
if(fi > 0)
continue;
return -1;
}
// process rows one by one
while(fgets(line, sizeof(line), fp))
{
char *str[38+2*MAX_MOB_DROP], *p, *np;
int i;
lines++;
if(line[0] == '/' && line[1] == '/')
continue;
for(i = 0, p = line; i < 38 + 2*MAX_MOB_DROP; i++)
{
str[i] = p;
if((np = strchr(p, ',')) != NULL) {
*np = '\0'; p = np + 1;
}
}
if(i < 38 + 2*MAX_MOB_DROP) {
ShowWarning("mob_readdb: Insufficient columns for mob with id: %d, skipping.\n", atoi(str[0]));
continue;
}
if (!mob_parse_dbrow(str))
continue;
count++;
}
fclose(fp);
ShowStatus("Done reading '"CL_WHITE"%lu"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", count, filename[fi]);
}
return 0;
}
#ifndef TXT_ONLY
/*==========================================
* mob_db table reading
*------------------------------------------*/
static int mob_read_sqldb(void)
{
const char* mob_db_name[] = { mob_db_db, mob_db2_db };
int fi;
for( fi = 0; fi < ARRAYLENGTH(mob_db_name); ++fi )
{
uint32 lines = 0, count = 0;
// retrieve all rows from the mob database
if( SQL_ERROR == Sql_Query(mmysql_handle, "SELECT * FROM `%s`", mob_db_name[fi]) )
{
Sql_ShowDebug(mmysql_handle);
continue;
}
// process rows one by one
while( SQL_SUCCESS == Sql_NextRow(mmysql_handle) )
{
// wrap the result into a TXT-compatible format
char line[1024];
char* str[38+2*MAX_MOB_DROP];
char* p;
int i;
lines++;
for(i = 0, p = line; i < 38 + 2*MAX_MOB_DROP; i++)
{
char* data;
size_t len;
Sql_GetData(mmysql_handle, i, &data, &len);
strcpy(p, data);
str[i] = p;
p+= len + 1;
}
if (!mob_parse_dbrow(str))
continue;
count++;
}
// free the query result
Sql_FreeResult(mmysql_handle);
ShowStatus("Done reading '"CL_WHITE"%lu"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", count, mob_db_name[fi]);
count = 0;
}
return 0;
}
#endif /* not TXT_ONLY */
/*==========================================
* MOB display graphic change data reading
*------------------------------------------*/
static int mob_readdb_mobavail(void)
{
FILE *fp;
char line[1024];
int ln=0;
int class_,j,k;
char *str[20],*p,*np;
sprintf(line, "%s/mob_avail.txt", db_path);
if( (fp=fopen(line,"r"))==NULL ){
ShowError("can't read %s\n", line);
return -1;
}
while(fgets(line, sizeof(line), fp))
{
if(line[0]=='/' && line[1]=='/')
continue;
memset(str,0,sizeof(str));
for(j=0,p=line;j<12;j++){
if((np=strchr(p,','))!=NULL){
str[j]=p;
*np=0;
p=np+1;
} else
str[j]=p;
}
if(str[0]==NULL)
continue;
class_=atoi(str[0]);
if (class_ == 0)
continue; //Leave blank lines alone... [Skotlex]
if(mob_db(class_) == mob_dummy) // �l���ُ�Ȃ珈�����Ȃ��B
continue;
k=atoi(str[1]);
if(k < 0)
continue;
memset(&mob_db_data[class_]->vd, 0, sizeof(struct view_data));
mob_db_data[class_]->vd.class_=k;
//Player sprites
if(pcdb_checkid(k) && j>=12) {
mob_db_data[class_]->vd.sex=atoi(str[2]);
mob_db_data[class_]->vd.hair_style=atoi(str[3]);
mob_db_data[class_]->vd.hair_color=atoi(str[4]);
mob_db_data[class_]->vd.weapon=atoi(str[5]);
mob_db_data[class_]->vd.shield=atoi(str[6]);
mob_db_data[class_]->vd.head_top=atoi(str[7]);
mob_db_data[class_]->vd.head_mid=atoi(str[8]);
mob_db_data[class_]->vd.head_bottom=atoi(str[9]);
mob_db_data[class_]->option=atoi(str[10])&~(OPTION_HIDE|OPTION_CLOAK|OPTION_INVISIBLE);
mob_db_data[class_]->vd.cloth_color=atoi(str[11]); // Monster player dye option - Valaris
}
else if(str[2] && atoi(str[2]) > 0)
mob_db_data[class_]->vd.head_bottom=atoi(str[2]); // mob equipment [Valaris]
ln++;
}
fclose(fp);
ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n",ln,"mob_avail.txt");
return 0;
}
/*==========================================
* Reading of random monster data
*------------------------------------------*/
static int mob_read_randommonster(void)
{
FILE *fp;
char line[1024];
char *str[10],*p;
int i,j;
const char* mobfile[] = {
"mob_branch.txt",
"mob_poring.txt",
"mob_boss.txt",
"mob_pouch.txt"};
memset(&summon, 0, sizeof(summon));
for( i = 0; i < ARRAYLENGTH(mobfile) && i < MAX_RANDOMMONSTER; i++ )
{
mob_db_data[0]->summonper[i] = 1002; // �ݒ肵�Y�ꂽ�ꍇ�̓|�������o��悤�ɂ��Ă���
sprintf(line, "%s/%s", db_path, mobfile[i]);
fp=fopen(line,"r");
if(fp==NULL){
ShowError("can't read %s\n",line);
return -1;
}
while(fgets(line, sizeof(line), fp))
{
int class_;
if(line[0] == '/' && line[1] == '/')
continue;
memset(str,0,sizeof(str));
for(j=0,p=line;j<3 && p;j++){
str[j]=p;
p=strchr(p,',');
if(p) *p++=0;
}
if(str[0]==NULL || str[2]==NULL)
continue;
class_ = atoi(str[0]);
if(mob_db(class_) == mob_dummy)
continue;
mob_db_data[class_]->summonper[i]=atoi(str[2]);
if (i) {
if( summon[i].qty < ARRAYLENGTH(summon[i].class_) ) //MvPs
summon[i].class_[summon[i].qty++] = class_;
else {
ShowDebug("Can't store more random mobs from %s, increase size of mob.c:summon variable!\n", mobfile[i]);
break;
}
}
}
if (i && !summon[i].qty)
{ //At least have the default here.
summon[i].class_[0] = mob_db_data[0]->summonper[i];
summon[i].qty = 1;
}
fclose(fp);
ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n",mobfile[i]);
}
return 0;
}
/*==========================================
* mob_skill_db.txt reading
*------------------------------------------*/
static int mob_readskilldb(void)
{
FILE *fp;
char line[1024];
int i,tmp, count;
const struct {
char str[32];
int id;
} cond1[] = {
{ "always", MSC_ALWAYS },
{ "myhpltmaxrate", MSC_MYHPLTMAXRATE },
{ "myhpinrate", MSC_MYHPINRATE },
{ "friendhpltmaxrate",MSC_FRIENDHPLTMAXRATE },
{ "friendhpinrate", MSC_FRIENDHPINRATE },
{ "mystatuson", MSC_MYSTATUSON },
{ "mystatusoff", MSC_MYSTATUSOFF },
{ "friendstatuson", MSC_FRIENDSTATUSON },
{ "friendstatusoff", MSC_FRIENDSTATUSOFF },
{ "attackpcgt", MSC_ATTACKPCGT },
{ "attackpcge", MSC_ATTACKPCGE },
{ "slavelt", MSC_SLAVELT },
{ "slavele", MSC_SLAVELE },
{ "closedattacked", MSC_CLOSEDATTACKED },
{ "longrangeattacked",MSC_LONGRANGEATTACKED },
{ "skillused", MSC_SKILLUSED },
{ "afterskill", MSC_AFTERSKILL },
{ "casttargeted", MSC_CASTTARGETED },
{ "rudeattacked", MSC_RUDEATTACKED },
{ "masterhpltmaxrate",MSC_MASTERHPLTMAXRATE },
{ "masterattacked", MSC_MASTERATTACKED },
{ "alchemist", MSC_ALCHEMIST },
{ "onspawn", MSC_SPAWN},
}, cond2[] ={
{ "anybad", -1 },
{ "stone", SC_STONE },
{ "freeze", SC_FREEZE },
{ "stun", SC_STUN },
{ "sleep", SC_SLEEP },
{ "poison", SC_POISON },
{ "curse", SC_CURSE },
{ "silence", SC_SILENCE },
{ "confusion", SC_CONFUSION },
{ "blind", SC_BLIND },
{ "hiding", SC_HIDING },
{ "sight", SC_SIGHT },
}, state[] = {
{ "any", MSS_ANY }, //All states except Dead
{ "idle", MSS_IDLE },
{ "walk", MSS_WALK },
{ "loot", MSS_LOOT },
{ "dead", MSS_DEAD },
{ "attack", MSS_BERSERK }, //Retaliating attack
{ "angry", MSS_ANGRY }, //Preemptive attack (aggressive mobs)
{ "chase", MSS_RUSH }, //Chase escaping target
{ "follow", MSS_FOLLOW }, //Preemptive chase (aggressive mobs)
{ "anytarget",MSS_ANYTARGET }, //Berserk+Angry+Rush+Follow
}, target[] = {
{ "target", MST_TARGET },
{ "randomtarget", MST_RANDOM },
{ "self", MST_SELF },
{ "friend", MST_FRIEND },
{ "master", MST_MASTER },
{ "around5", MST_AROUND5 },
{ "around6", MST_AROUND6 },
{ "around7", MST_AROUND7 },
{ "around8", MST_AROUND8 },
{ "around1", MST_AROUND1 },
{ "around2", MST_AROUND2 },
{ "around3", MST_AROUND3 },
{ "around4", MST_AROUND4 },
{ "around", MST_AROUND },
};
int x;
const char* filename[] = { "mob_skill_db.txt","mob_skill_db2.txt" };
if( battle_config.mob_skill_rate == 0 ) {
ShowStatus("Mob skill use disabled. Not reading mob skills.\n");
return 0;
}
for( x = 0; x < ARRAYLENGTH(filename); ++x )
{
int last_mob_id = 0;
count = 0;
sprintf(line, "%s/%s", db_path, filename[x]);
fp=fopen(line,"r");
if(fp==NULL){
if(x==0)
ShowError("can't read %s\n",line);
continue;
}
while(fgets(line, sizeof(line), fp))
{
char *sp[20],*p;
int mob_id;
struct mob_skill *ms, gms;
int j=0;
count++;
if(line[0] == '/' && line[1] == '/')
continue;
memset(sp,0,sizeof(sp));
for(i=0,p=line;i<18 && p;i++){
sp[i]=p;
if((p=strchr(p,','))!=NULL)
*p++=0;
}
if(i == 0 || (mob_id=atoi(sp[0]))== 0)
continue;
if(i < 18) {
ShowError("mob_skill: Insufficient number of fields for skill at %s, line %d\n", filename[x], count);
continue;
}
if (mob_id > 0 && mob_db(mob_id) == mob_dummy)
{
if (mob_id != last_mob_id) {
ShowError("mob_skill: Non existant Mob id %d at %s, line %d\n", mob_id, filename[x], count);
last_mob_id = mob_id;
}
continue;
}
if( strcmp(sp[1],"clear")==0 ){
if (mob_id < 0)
continue;
memset(mob_db_data[mob_id]->skill,0,sizeof(struct mob_skill));
mob_db_data[mob_id]->maxskill=0;
continue;
}
if (mob_id < 0)
{ //Prepare global skill. [Skotlex]
memset(&gms, 0, sizeof (struct mob_skill));
ms = &gms;
} else {
ARR_FIND( 0, MAX_MOBSKILL, i, (ms = &mob_db_data[mob_id]->skill[i])->skill_id == 0 );
if( i == MAX_MOBSKILL ) {
if (mob_id != last_mob_id) {
ShowError("mob_skill: readdb: too many skills! Line %d in %d[%s]\n", count,mob_id,mob_db_data[mob_id]->sprite);
last_mob_id = mob_id;
}
continue;
}
}
//State
ARR_FIND( 0, ARRAYLENGTH(state), j, strcmp(sp[2],state[j].str) == 0 );
if( j < ARRAYLENGTH(state) )
ms->state = state[j].id;
else {
ShowWarning("mob_skill: Unrecognized state %s at %s, line %d\n", sp[2], filename[x], count);
ms->state = MSS_ANY;
}
//Skill ID
j=atoi(sp[3]);
if (j<=0 || j>MAX_SKILL_DB) //fixed Lupus
{
if (mob_id < 0)
ShowError("Invalid Skill ID (%d) for all mobs\n", j);
else
ShowError("Invalid Skill ID (%d) for mob %d (%s)\n", j, mob_id, mob_db_data[mob_id]->sprite);
continue;
}
ms->skill_id=j;
//Skill lvl
j= atoi(sp[4])<=0 ? 1 : atoi(sp[4]);
ms->skill_lv= j>battle_config.mob_max_skilllvl ? battle_config.mob_max_skilllvl : j; //we strip max skill level
//Apply battle_config modifiers to rate (permillage) and delay [Skotlex]
tmp = atoi(sp[5]);
if (battle_config.mob_skill_rate != 100)
tmp = tmp*battle_config.mob_skill_rate/100;
if (tmp > 10000)
ms->permillage= 10000;
else
ms->permillage= tmp;
ms->casttime=atoi(sp[6]);
ms->delay=atoi(sp[7]);
if (battle_config.mob_skill_delay != 100)
ms->delay = ms->delay*battle_config.mob_skill_delay/100;
if (ms->delay < 0) //time overflow?
ms->delay = INT_MAX;
ms->cancel=atoi(sp[8]);
if( strcmp(sp[8],"yes")==0 ) ms->cancel=1;
//Target
ARR_FIND( 0, ARRAYLENGTH(target), j, strcmp(sp[9],target[j].str) == 0 );
if( j < ARRAYLENGTH(target) )
ms->target = target[j].id;
else {
ShowWarning("mob_skill: Unrecognized target %s at %s, line %d\n", sp[9], filename[x], count);
ms->target = MST_TARGET;
}
//Check that the target condition is right for the skill type. [Skotlex]
if (skill_get_casttype(ms->skill_id) == CAST_GROUND)
{ //Ground skill.
if (ms->target > MST_AROUND)
{
ShowWarning("Wrong mob skill target for ground skill %d (%s) for %s.\n",
ms->skill_id, skill_get_name(ms->skill_id),
mob_id < 0?"all mobs":mob_db_data[mob_id]->sprite);
ms->target = MST_TARGET;
}
} else if (ms->target > MST_MASTER) {
ShowWarning("Wrong mob skill target 'around' for non-ground skill %d (%s) for %s\n.",
ms->skill_id, skill_get_name(ms->skill_id),
mob_id < 0?"all mobs":mob_db_data[mob_id]->sprite);
ms->target = MST_TARGET;
}
//Cond1
ARR_FIND( 0, ARRAYLENGTH(cond1), j, strcmp(sp[10],cond1[j].str) == 0 );
if( j < ARRAYLENGTH(cond1) )
ms->cond1 = cond1[j].id;
else {
ShowWarning("mob_skill: Unrecognized condition 1 %s at %s, line %d\n", sp[10], filename[x], count);
ms->cond1 = -1;
}
//Cond2
// numeric value
ms->cond2 = atoi(sp[11]);
// or special constant
ARR_FIND( 0, ARRAYLENGTH(cond2), j, strcmp(sp[11],cond2[j].str) == 0 );
if( j < ARRAYLENGTH(cond2) )
ms->cond2 = cond2[j].id;
ms->val[0]=(int)strtol(sp[12],NULL,0);
ms->val[1]=(int)strtol(sp[13],NULL,0);
ms->val[2]=(int)strtol(sp[14],NULL,0);
ms->val[3]=(int)strtol(sp[15],NULL,0);
ms->val[4]=(int)strtol(sp[16],NULL,0);
if(ms->skill_id == NPC_EMOTION && mob_id>0 &&
ms->val[1] == mob_db(mob_id)->status.mode)
{
ms->val[1] = 0;
ms->val[4] = 1; //request to return mode to normal.
}
if(ms->skill_id == NPC_EMOTION_ON && mob_id>0 && ms->val[1])
{ //Adds a mode to the mob.
//Remove aggressive mode when the new mob type is passive.
if (!(ms->val[1]&MD_AGGRESSIVE))
ms->val[3]|=MD_AGGRESSIVE;
ms->val[2]|= ms->val[1]; //Add the new mode.
ms->val[1] = 0; //Do not "set" it.
}
if(sp[17] != NULL && strlen(sp[17])>2)
ms->emotion=atoi(sp[17]);
else
ms->emotion=-1;
if (mob_id < 0)
{ //Set this skill to ALL mobs. [Skotlex]
mob_id *= -1;
for (i = 1; i < MAX_MOB_DB; i++)
{
if (mob_db_data[i] == NULL)
continue;
if (mob_db_data[i]->status.mode&MD_BOSS)
{
if (!(mob_id&2)) //Skill not for bosses
continue;
} else
if (!(mob_id&1)) //Skill not for normal enemies.
continue;
for(j=0;j<MAX_MOBSKILL;j++)
if( mob_db_data[i]->skill[j].skill_id == 0)
break;
if(j==MAX_MOBSKILL)
continue;
memcpy (&mob_db_data[i]->skill[j], ms, sizeof(struct mob_skill));
mob_db_data[i]->maxskill=j+1;
}
} else //Skill set on a single mob.
mob_db_data[mob_id]->maxskill=i+1;
}
fclose(fp);
ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n",filename[x]);
}
return 0;
}
/*==========================================
* mob_race_db.txt reading
*------------------------------------------*/
static int mob_readdb_race(void)
{
FILE *fp;
char line[1024];
int race,j,k;
char *str[20],*p,*np;
sprintf(line, "%s/mob_race2_db.txt", db_path);
if( (fp=fopen(line,"r"))==NULL ){
ShowError("can't read %s\n", line);
return -1;
}
while(fgets(line, sizeof(line), fp))
{
if(line[0]=='/' && line[1]=='/')
continue;
memset(str,0,sizeof(str));
for(j=0,p=line;j<12;j++){
if((np=strchr(p,','))!=NULL){
str[j]=p;
*np=0;
p=np+1;
} else
str[j]=p;
}
if(str[0]==NULL)
continue;
race=atoi(str[0]);
if (race < 0 || race >= MAX_MOB_RACE_DB)
continue;
for (j=1; j<20; j++) {
if (!str[j])
break;
k=atoi(str[j]);
if (mob_db(k) == mob_dummy)
continue;
mob_db_data[k]->race2 = race;
}
}
fclose(fp);
ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n","mob_race2_db.txt");
return 0;
}
void mob_reload(void)
{
int i;
#ifndef TXT_ONLY
if(db_use_sqldbs)
mob_read_sqldb();
else
#endif /* TXT_ONLY */
mob_readdb();
mob_readdb_mobavail();
mob_read_randommonster();
//Mob skills need to be cleared before re-reading them. [Skotlex]
for (i = 0; i < MAX_MOB_DB; i++)
if (mob_db_data[i])
{
memset(&mob_db_data[i]->skill,0,sizeof(mob_db_data[i]->skill));
mob_db_data[i]->maxskill=0;
}
mob_readskilldb();
mob_readdb_race();
}
void mob_clear_spawninfo()
{ //Clears spawn related information for a script reload.
int i;
for (i = 0; i < MAX_MOB_DB; i++)
if (mob_db_data[i])
memset(&mob_db_data[i]->spawn,0,sizeof(mob_db_data[i]->spawn));
}
/*==========================================
* Circumference initialization of mob
*------------------------------------------*/
int do_init_mob(void)
{ //Initialize the mob database
memset(mob_db_data,0,sizeof(mob_db_data)); //Clear the array
mob_db_data[0] = (struct mob_db*)aCalloc(1, sizeof (struct mob_db)); //This mob is used for random spawns
mob_makedummymobdb(0); //The first time this is invoked, it creates the dummy mob
item_drop_ers = ers_new(sizeof(struct item_drop));
item_drop_list_ers = ers_new(sizeof(struct item_drop_list));
barricade_db = strdb_alloc(DB_OPT_RELEASE_DATA,2*NAME_LENGTH+2+1);
#ifndef TXT_ONLY
if(db_use_sqldbs)
mob_read_sqldb();
else
#endif /* TXT_ONLY */
mob_readdb();
mob_readdb_mobavail();
mob_read_randommonster();
mob_readskilldb();
mob_readdb_race();
add_timer_func_list(mob_delayspawn,"mob_delayspawn");
add_timer_func_list(mob_delay_item_drop,"mob_delay_item_drop");
add_timer_func_list(mob_ai_hard,"mob_ai_hard");
add_timer_func_list(mob_ai_lazy,"mob_ai_lazy");
add_timer_func_list(mob_timer_delete,"mob_timer_delete");
add_timer_func_list(mob_spawn_guardian_sub,"mob_spawn_guardian_sub");
add_timer_func_list(mob_respawn,"mob_respawn");
add_timer_interval(gettick()+MIN_MOBTHINKTIME,mob_ai_hard,0,0,MIN_MOBTHINKTIME);
add_timer_interval(gettick()+MIN_MOBTHINKTIME*10,mob_ai_lazy,0,0,MIN_MOBTHINKTIME*10);
return 0;
}
/*==========================================
* Clean memory usage.
*------------------------------------------*/
int do_final_mob(void)
{
int i;
if (mob_dummy)
{
aFree(mob_dummy);
mob_dummy = NULL;
}
for (i = 0; i <= MAX_MOB_DB; i++)
{
if (mob_db_data[i] != NULL)
{
aFree(mob_db_data[i]);
mob_db_data[i] = NULL;
}
}
ers_destroy(item_drop_ers);
ers_destroy(item_drop_list_ers);
barricade_db->destroy(barricade_db,NULL);
return 0;
}
|