1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
|
#include "script-fun.hpp"
// script-fun.cpp - EAthena script frontend, engine, and library.
//
// Copyright © ????-2004 Athena Dev Teams
// Copyright © 2004-2011 The Mana World Development Team
// Copyright © 2011 Chuck Miller
// Copyright © 2011-2014 Ben Longbons <b.r.longbons@gmail.com>
// Copyright © 2013 wushin
//
// This file is part of The Mana World (Athena server)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "../compat/fun.hpp"
#include "../generic/db.hpp"
#include "../generic/dumb_ptr.hpp"
#include "../generic/intern-pool.hpp"
#include "../generic/random.hpp"
#include "../io/cxxstdio.hpp"
#include "../io/extract.hpp"
#include "../net/timer.hpp"
#include "../proto2/net-HumanTimeDiff.hpp"
#include "../high/core.hpp"
#include "../high/extract_mmo.hpp"
#include "atcommand.hpp"
#include "battle.hpp"
#include "battle_conf.hpp"
#include "chrif.hpp"
#include "clif.hpp"
#include "globals.hpp"
#include "intif.hpp"
#include "itemdb.hpp"
#include "map.hpp"
#include "mob.hpp"
#include "npc.hpp"
#include "npc-parse.hpp"
#include "party.hpp"
#include "pc.hpp"
#include "script-call-internal.hpp"
#include "script-parse-internal.hpp"
#include "script-persist.hpp"
#include "skill.hpp"
#include "storage.hpp"
#include "npc-internal.hpp"
#include "path.hpp"
#include "../poison.hpp"
namespace tmwa
{
namespace map
{
#define AARG(n) (st->stack->stack_datav[st->start + 2 + (n)])
#define HARG(n) (st->end > st->start + 2 + (n))
enum class MonsterAttitude
{
HOSTILE = 0,
FRIENDLY = 1,
SERVANT = 2,
FROZEN = 3,
};
//
// 埋め込み関数
//
/*==========================================
*
*------------------------------------------
*/
static
void builtin_mes(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
if (sd == nullptr)
return;
sd->state.npc_dialog_mes = 1;
RString mes = HARG(0) ? conv_str(st, &AARG(0)) : ""_s;
clif_scriptmes(sd, st->oid, mes);
}
static
void builtin_clear(ScriptState *st)
{
clif_npc_action(script_rid2sd(st), st->oid, 9, 0, 0, 0);
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_goto(ScriptState *st)
{
if (!AARG(0).is<ScriptDataPos>())
{
PRINTF("fatal: script: goto: not label !\n"_fmt);
st->state = ScriptEndState::END;
abort();
}
st->scriptp.pos = conv_num(st, &AARG(0));
st->state = ScriptEndState::GOTO;
}
/*==========================================
* ユーザー定義関数の呼び出し
*------------------------------------------
*/
static
void builtin_callfunc(ScriptState *st)
{
RString str = conv_str(st, &AARG(0));
Option<P<const ScriptBuffer>> scr_ = userfunc_db.get(str);
OMATCH_BEGIN (scr_)
{
OMATCH_CASE_SOME (scr)
{
int j = 0;
assert (st->start + 3 == st->end);
#if 0
for (int i = st->start + 3; i < st->end; i++, j++)
push_copy(st->stack, i);
#endif
push_int<ScriptDataInt>(st->stack, j); // 引数の数をプッシュ
push_int<ScriptDataInt>(st->stack, st->defsp); // 現在の基準スタックポインタをプッシュ
push_int<ScriptDataInt>(st->stack, st->scriptp.pos); // 現在のスクリプト位置をプッシュ
push_script<ScriptDataRetInfo>(st->stack, TRY_UNWRAP(st->scriptp.code, abort())); // 現在のスクリプトをプッシュ
st->scriptp = ScriptPointer(scr, 0);
st->defsp = st->start + 4 + j;
st->state = ScriptEndState::GOTO;
}
OMATCH_CASE_NONE ()
{
PRINTF("fatal: script: callfunc: function not found! [%s]\n"_fmt, str);
st->state = ScriptEndState::END;
abort();
}
}
OMATCH_END ();
}
static
void builtin_call(ScriptState *st)
{
struct script_data *sdata = &AARG(0);
get_val(st, sdata);
RString str;
if (sdata->is<ScriptDataStr>())
{
str = conv_str(st, sdata);
Option<P<const ScriptBuffer>> scr_ = userfunc_db.get(str);
OMATCH_BEGIN (scr_)
{
OMATCH_CASE_SOME (scr)
{
int j = 0;
for (int i = st->start + 3; i < st->end; i++, j++)
push_copy(st->stack, i);
push_int<ScriptDataInt>(st->stack, j); // 引数の数をプッシュ
push_int<ScriptDataInt>(st->stack, st->defsp); // 現在の基準スタックポインタをプッシュ
push_int<ScriptDataInt>(st->stack, st->scriptp.pos); // 現在のスクリプト位置をプッシュ
push_script<ScriptDataRetInfo>(st->stack, TRY_UNWRAP(st->scriptp.code, abort())); // 現在のスクリプトをプッシュ
st->scriptp = ScriptPointer(scr, 0);
st->defsp = st->start + 4 + j;
st->state = ScriptEndState::GOTO;
return;
}
OMATCH_CASE_NONE ()
{
PRINTF("fatal: script: callfunc: function not found! [%s]\n"_fmt, str);
st->state = ScriptEndState::END;
abort();
}
}
OMATCH_END ();
}
else
{
int pos_ = conv_num(st, &AARG(0));
int j = 0;
for (int i = st->start + 3; i < st->end; i++, j++)
push_copy(st->stack, i);
push_int<ScriptDataInt>(st->stack, j); // 引数の数をプッシュ
push_int<ScriptDataInt>(st->stack, st->defsp); // 現在の基準スタックポインタをプッシュ
push_int<ScriptDataInt>(st->stack, st->scriptp.pos); // 現在のスクリプト位置をプッシュ
push_script<ScriptDataRetInfo>(st->stack, TRY_UNWRAP(st->scriptp.code, abort())); // 現在のスクリプトをプッシュ
st->scriptp.pos = pos_;
st->defsp = st->start + 4 + j;
st->state = ScriptEndState::GOTO;
}
}
static
void builtin_getarg(ScriptState *st)
{
int arg = conv_num(st, &AARG(0));
if(st->defsp < 1 || !(st->stack->stack_datav[st->defsp - 1].is<ScriptDataRetInfo>()))
{
dumb_ptr<npc_data> nd = map_id_is_npc(st->oid);
if(nd)
PRINTF("builtin_getarg: no callfunc or callsub! @ %s\n"_fmt, nd->name);
else
PRINTF("builtin_getarg: no callfunc or callsub! (no npc)\n"_fmt);
st->state = ScriptEndState::END;
return;
}
int i = conv_num(st, &st->stack->stack_datav[st->defsp - 4]); // Number of arguments.
if (arg > i || arg < 0 || i == 0)
{
const char a = 3; // ETX
if (HARG(1))
push_copy(st->stack, st->start + 3);
else
push_str<ScriptDataStr>(st->stack, VString<1>(a));
return;
}
push_copy(st->stack, (st->defsp - 4 - i) + arg);
}
static
void builtin_void(ScriptState *)
{
return;
}
/*==========================================
* サブルーティンの呼び出し
*------------------------------------------
*/
static
void builtin_callsub(ScriptState *st)
{
int pos_ = conv_num(st, &AARG(0));
int j = 0;
assert (st->start + 3 == st->end);
#if 0
for (int i = st->start + 3; i < st->end; i++, j++)
push_copy(st->stack, i);
#endif
push_int<ScriptDataInt>(st->stack, j); // 引数の数をプッシュ
push_int<ScriptDataInt>(st->stack, st->defsp); // 現在の基準スタックポインタをプッシュ
push_int<ScriptDataInt>(st->stack, st->scriptp.pos); // 現在のスクリプト位置をプッシュ
push_script<ScriptDataRetInfo>(st->stack, TRY_UNWRAP(st->scriptp.code, abort())); // 現在のスクリプトをプッシュ
st->scriptp.pos = pos_;
st->defsp = st->start + 4 + j;
st->state = ScriptEndState::GOTO;
}
/*==========================================
* サブルーチン/ユーザー定義関数の終了
*------------------------------------------
*/
static
void builtin_return(ScriptState *st)
{
if (!(st->stack->stack_datav[st->defsp - 1].is<ScriptDataRetInfo>()))
{
dumb_ptr<npc_data> nd = map_id_is_npc(st->oid);
if(nd)
PRINTF("Deprecated: return outside of callfunc or callsub! @ %s\n"_fmt, nd->name);
else
PRINTF("Deprecated: return outside of callfunc or callsub! (no npc)\n"_fmt);
}
if (HARG(0))
{ // 戻り値有り
push_copy(st->stack, st->start + 2);
}
st->state = ScriptEndState::RETFUNC;
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_next(ScriptState *st)
{
st->state = ScriptEndState::STOP;
clif_scriptnext(script_rid2sd(st), st->oid);
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_close(ScriptState *st)
{
if (st->stack->stack_datav[st->defsp - 1].is<ScriptDataRetInfo>())
{
dumb_ptr<npc_data> nd = map_id_is_npc(st->oid);
if(nd)
PRINTF("Deprecated: close in a callfunc or callsub! @ %s\n"_fmt, nd->name);
else
PRINTF("Deprecated: close in a callfunc or callsub! (no npc)\n"_fmt);
}
st->state = ScriptEndState::END;
dumb_ptr<map_session_data> sd = script_rid2sd(st);
if (sd == nullptr)
return;
if (sd->state.npc_dialog_mes)
clif_scriptclose(sd, st->oid);
else
clif_npc_action(sd, st->oid, 5, 0, 0, 0);
}
static
void builtin_close2(ScriptState *st)
{
st->state = ScriptEndState::STOP;
dumb_ptr<map_session_data> sd = script_rid2sd(st);
if (sd == nullptr)
return;
if (sd->state.npc_dialog_mes)
clif_scriptclose(sd, st->oid);
else
clif_npc_action(sd, st->oid, 5, 0, 0, 0);
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_menu(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
if (sd->state.menu_or_input == 0)
{
// First half: show menu.
st->state = ScriptEndState::RERUNLINE;
sd->state.menu_or_input = 1;
MString buf;
for (int i = 0; i < (st->end - (st->start + 2)) / 2; i++)
{
RString choice_str = conv_str(st, &AARG(i * 2 + 0));
if (!choice_str)
break;
buf += choice_str;
buf += ':';
}
clif_scriptmenu(script_rid2sd(st), st->oid, AString(buf));
}
else
{
// Rerun: item is chosen from menu.
if (sd->npc_menu == 0xff)
{
// cancel
sd->state.menu_or_input = 0;
st->state = ScriptEndState::END;
return;
}
// Actually jump to the label.
// Logic change: menu_choices is the *total* number of labels,
// not just the displayed number that ends with the "".
// (Would it be better to pop the stack before rerunning?)
int menu_choices = (st->end - (st->start + 2)) / 2;
pc_setreg(sd, SIR::from(variable_names.intern("@menu"_s)), sd->npc_menu);
sd->state.menu_or_input = 0;
if (sd->npc_menu > 0 && sd->npc_menu <= menu_choices)
{
int arg_index = (sd->npc_menu - 1) * 2 + 1;
if (!AARG(arg_index).is<ScriptDataPos>())
{
PRINTF("fatal: script:menu: not a label\n"_fmt);
st->state = ScriptEndState::END;
abort();
}
st->scriptp.pos = AARG(arg_index).get_if<ScriptDataPos>()->numi;
st->state = ScriptEndState::GOTO;
}
}
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_rand(ScriptState *st)
{
if (HARG(1))
{
int min = conv_num(st, &AARG(0));
int max = conv_num(st, &AARG(1));
if (min > max)
std::swap(max, min);
push_int<ScriptDataInt>(st->stack, random_::in(min, max));
}
else
{
int range = conv_num(st, &AARG(0));
push_int<ScriptDataInt>(st->stack, range <= 0 ? 0 : random_::to(range));
}
}
static
void builtin_max(ScriptState *st)
{
int max=0, num;
if (HARG(1))
{
max = conv_num(st, &AARG(0));
for (int i = 1; HARG(i); i++)
{
num = conv_num(st, &AARG(i));
if (num > max)
max = num;
}
}
else
{
SIR reg = AARG(0).get_if<ScriptDataVariable>()->reg;
ZString name = variable_names.outtern(reg.base());
char prefix = name.front();
if (prefix != '$' && prefix != '@' && prefix != '.')
{
PRINTF("builtin_max: illegal scope!\n"_fmt);
return;
}
for (int i = reg.index(); i < 256; i++)
{
struct script_data vd = get_val2(st, reg.iplus(i));
MATCH_BEGIN (vd)
{
MATCH_CASE (const ScriptDataInt&, u)
{
if (u.numi > max)
max = u.numi;
continue;
}
}
MATCH_END ();
abort();
}
}
push_int<ScriptDataInt>(st->stack, max);
}
static
void builtin_min(ScriptState *st)
{
int min = 0xFFFFFFF6, num;
if (HARG(1))
{
min = conv_num(st, &AARG(0));
for (int i = 1; HARG(i); i++)
{
num = conv_num(st, &AARG(i));
if (num < min)
min = num;
}
}
else
{
SIR reg = AARG(0).get_if<ScriptDataVariable>()->reg;
ZString name = variable_names.outtern(reg.base());
char prefix = name.front();
if (prefix != '$' && prefix != '@' && prefix != '.')
{
PRINTF("builtin_max: illegal scope!\n"_fmt);
return;
}
for (int i = reg.index(); i < 256; i++)
{
struct script_data vd = get_val2(st, reg.iplus(i));
MATCH_BEGIN (vd)
{
MATCH_CASE (const ScriptDataInt&, u)
{
if (u.numi < min)
min = u.numi;
continue;
}
}
MATCH_END ();
abort();
}
}
push_int<ScriptDataInt>(st->stack, min);
}
static
void builtin_average(ScriptState *st)
{
int total, i;
total = conv_num(st, &AARG(0));
for (i = 1; HARG(i); i++)
total += conv_num(st, &AARG(i));
push_int<ScriptDataInt>(st->stack, (total / i));
}
static
void builtin_sqrt(ScriptState *st)
{
push_int<ScriptDataInt>(st->stack, static_cast<int>(sqrt(conv_num(st, &AARG(0)))));
}
static
void builtin_cbrt(ScriptState *st)
{
push_int<ScriptDataInt>(st->stack, static_cast<int>(cbrt(conv_num(st, &AARG(0)))));
}
static
void builtin_pow(ScriptState *st)
{
push_int<ScriptDataInt>(st->stack, static_cast<int>(pow(conv_num(st, &AARG(0)), conv_num(st, &AARG(1)))));
}
/*==========================================
* Check whether the PC is at the specified location
*------------------------------------------
*/
static
void builtin_isat(ScriptState *st)
{
int x, y;
dumb_ptr<map_session_data> sd = script_rid2sd(st);
MapName str = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
x = conv_num(st, &AARG(1));
y = conv_num(st, &AARG(2));
if (!sd)
return;
push_int<ScriptDataInt>(st->stack,
(x == sd->bl_x) && (y == sd->bl_y)
&& (str == sd->bl_m->name_));
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_warp(ScriptState *st)
{
int x, y;
dumb_ptr<map_session_data> sd = script_rid2sd(st);
MapName str = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
x = conv_num(st, &AARG(1));
y = conv_num(st, &AARG(2));
pc_setpos(sd, str, x, y, BeingRemoveWhy::GONE);
}
/*==========================================
* エリア指定ワープ
*------------------------------------------
*/
static
void builtin_areawarp_sub(dumb_ptr<block_list> bl, MapName mapname, int x, int y)
{
dumb_ptr<map_session_data> sd = bl->is_player();
pc_setpos(sd, mapname, x, y, BeingRemoveWhy::GONE);
}
static
void builtin_areawarp(ScriptState *st)
{
int x, y;
int x0, y0, x1, y1;
MapName mapname = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
x0 = conv_num(st, &AARG(1));
y0 = conv_num(st, &AARG(2));
x1 = conv_num(st, &AARG(3));
y1 = conv_num(st, &AARG(4));
MapName str = stringish<MapName>(ZString(conv_str(st, &AARG(5))));
x = conv_num(st, &AARG(6));
y = conv_num(st, &AARG(7));
P<map_local> m = TRY_UNWRAP(map_mapname2mapid(mapname), return);
map_foreachinarea(std::bind(builtin_areawarp_sub, ph::_1, str, x, y),
m,
x0, y0,
x1, y1,
BL::PC);
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_heal(ScriptState *st)
{
int hp, sp;
dumb_ptr<map_session_data> sd = script_rid2sd(st);
hp = conv_num(st, &AARG(0));
sp = conv_num(st, &AARG(1));
if(sd != nullptr && (sd->status.hp < 1 && hp > 0)){
pc_setstand(sd);
if (battle_config.player_invincible_time > interval_t::zero())
pc_setinvincibletimer(sd, battle_config.player_invincible_time);
clif_resurrection(sd, 1);
}
if(HARG(2) && bool(conv_num(st, &AARG(2))) && hp > 0)
pc_itemheal(sd, hp, sp);
else
pc_heal(sd, hp, sp);
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_distance(ScriptState *st)
{
dumb_ptr<block_list> source = map_id2bl(wrap<BlockId>(conv_num(st, &AARG(0))));
dumb_ptr<block_list> target = map_id2bl(wrap<BlockId>(conv_num(st, &AARG(1))));
int distance = 0;
int mode = HARG(2) ? conv_num(st, &AARG(2)) : 0;
switch (mode)
{
// TODO implement case 1 (walk distance)
case 0:
default:
if (source->bl_m != target->bl_m)
{
// FIXME make it work even if source and target are not in the same map
distance = 0x7fffffff;
break;
}
int dx = abs(source->bl_x - target->bl_x);
int dy = abs(source->bl_y - target->bl_y);
distance = sqrt((dx * dx) + (dy * dy)); // Pythagoras' theorem
}
push_int<ScriptDataInt>(st->stack, distance);
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_target(ScriptState *st)
{
// TODO maybe scrap all this and make it use battle_ functions? (add missing functions to battle)
dumb_ptr<block_list> source = map_id2bl(wrap<BlockId>(conv_num(st, &AARG(0))));
dumb_ptr<block_list> target = map_id2bl(wrap<BlockId>(conv_num(st, &AARG(1))));
int flag = conv_num(st, &AARG(2));
int val = 0;
if (!source || !target)
{
push_int<ScriptDataInt>(st->stack, val);
return;
}
if (flag & 0x01)
{
int x0 = source->bl_x - AREA_SIZE;
int y0 = source->bl_y - AREA_SIZE;
int x1 = source->bl_x + AREA_SIZE;
int y1 = source->bl_y + AREA_SIZE;
if (target->bl_x >= x0 && target->bl_x <= x1 && target->bl_y >= y0 && target->bl_y <= y1)
val |= 0x01; // 0x01 target is in visible range
}
if (flag & 0x02)
{
int range = battle_get_range(source);
int x2 = source->bl_x - range;
int y2 = source->bl_y - range;
int x3 = source->bl_x + range;
int y3 = source->bl_y + range;
if (target->bl_x >= x2 && target->bl_x <= x3 && target->bl_y >= y2 && target->bl_y <= y3)
val |= 0x02; // 0x02 target is in attack range
}
if (flag & 0x04)
{
struct walkpath_data wpd;
if (!path_search(&wpd, source->bl_m, source->bl_x, source->bl_y, target->bl_x, target->bl_y, 0))
val |= 0x04; // 0x04 target is walkable (has clear path to target)
}
// TODO 0x08 target is visible (not behind collision)
if (flag & 0x10)
{
if (target->bl_type != BL::PC || (target->bl_type == BL::PC &&
(target->bl_m->flag.get(MapFlag::PVP) || pc_iskiller(source->is_player(), target->is_player()))))
val |= 0x10; // 0x10 target can be attacked by source (killer, killable and so on)
}
if (flag & 0x20)
{
if (battle_check_range(source, target, 0))
val |= 0x20; // 0x20 target is in line of sight
}
push_int<ScriptDataInt>(st->stack, val);
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_injure(ScriptState *st)
{
dumb_ptr<block_list> source = map_id2bl(wrap<BlockId>(conv_num(st, &AARG(0))));
dumb_ptr<block_list> target = map_id2bl(wrap<BlockId>(conv_num(st, &AARG(1))));
int damage_caused = conv_num(st, &AARG(2));
// display damage first, because dealing damage may deallocate the target.
clif_damage(source, target,
gettick(), interval_t::zero(), interval_t::zero(),
damage_caused, 0, DamageType::NORMAL);
battle_damage(source, target, damage_caused, 0);
return;
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_input(ScriptState *st)
{
dumb_ptr<map_session_data> sd = nullptr;
script_data& scrd = AARG(0);
assert (scrd.is<ScriptDataVariable>());
SIR reg = scrd.get_if<ScriptDataVariable>()->reg;
ZString name = variable_names.outtern(reg.base());
// char prefix = name.front();
char postfix = name.back();
sd = script_rid2sd(st);
if (sd->state.menu_or_input)
{
// Second time (rerun)
sd->state.menu_or_input = 0;
if (postfix == '$')
{
set_reg(sd, VariableCode::VARIABLE, reg, sd->npc_str);
}
else
{
//commented by Lupus (check Value Number Input fix in clif.c)
//** Fix by fritz :X keeps people from abusing old input bugs
// wtf?
if (sd->npc_amount < 0) //** If input amount is less then 0
{
clif_tradecancelled(sd); // added "Deal has been cancelled" message by Valaris
builtin_close(st); //** close
}
set_reg(sd, VariableCode::VARIABLE, reg, sd->npc_amount);
}
}
else
{
// First time - send prompt to client, then wait
st->state = ScriptEndState::RERUNLINE;
if (postfix == '$')
clif_scriptinputstr(sd, st->oid);
else
clif_scriptinput(sd, st->oid);
sd->state.menu_or_input = 1;
}
}
static
void builtin_requestitem(ScriptState *st)
{
dumb_ptr<map_session_data> sd = nullptr;
script_data& scrd = AARG(0);
assert (scrd.is<ScriptDataVariable>());
int amount = HARG(1) ? conv_num(st, &AARG(1)) : 1;
if (amount < 1 || amount > 16)
amount = 1;
SIR reg = scrd.get_if<ScriptDataVariable>()->reg;
ZString name = variable_names.outtern(reg.base());
char prefix = name.front();
char postfix = name.back();
if (prefix != '$' && prefix != '@' && prefix != '.')
{
PRINTF("builtin_requestitem: illegal scope!\n"_fmt);
abort();
}
sd = script_rid2sd(st);
if (sd->state.menu_or_input)
{
// Second time (rerunline)
sd->state.menu_or_input = 0;
RString str = sd->npc_str;
RString val;
const char separator = ';';
for (int j = 0; j < amount; j++)
{
auto find = std::find(str.begin(), str.end(), separator);
if (find == str.end())
val = str.xislice_h(std::find(str.begin(), str.end(), ','));
else
{
val = str.xislice_h(find);
val = val.xislice_h(std::find(val.begin(), val.end(), ','));
str = str.xislice_t(find + 1);
}
// check that the item exists in the inventory
int num = atoi(val.c_str());
if (num < 1)
{
j--;
if (find == str.end())
break;
continue;
}
ItemNameId nameid = wrap<ItemNameId>(num);
for (IOff0 i : IOff0::iter())
if (sd->status.inventory[i].nameid == nameid)
goto pass;
fail:
j--;
if (find == str.end())
break;
continue;
pass:
// push to array
if (postfix == '$')
{
Option<P<struct item_data>> i_data = Some(itemdb_search(nameid));
RString item_name = i_data.pmd_pget(&item_data::name).copy_or(stringish<ItemName>(""_s));
if (item_name == ""_s)
goto fail;
if (name.startswith(".@"_s))
{
struct script_data vd = script_data(ScriptDataStr{item_name});
set_scope_reg(st, reg.iplus(j), &vd);
}
else
set_reg(sd, VariableCode::VARIABLE, reg.iplus(j), item_name);
}
else
{
if (name.startswith(".@"_s))
{
struct script_data vd = script_data(ScriptDataInt{num});
set_scope_reg(st, reg.iplus(j), &vd);
}
else
set_reg(sd, VariableCode::VARIABLE, reg.iplus(j), num);
}
if (find == str.end())
break;
}
}
else
{
// First time - send prompt to client, then wait
st->state = ScriptEndState::RERUNLINE;
clif_scriptinputstr(sd, st->oid); // send string prompt
clif_npc_action(sd, st->oid, 10, amount, 0, 0); // send item request
sd->state.menu_or_input = 1;
}
}
static
void builtin_requestlang(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
script_data& scrd = AARG(0);
assert (scrd.is<ScriptDataVariable>());
SIR reg = scrd.get_if<ScriptDataVariable>()->reg;
ZString name = variable_names.outtern(reg.base());
char postfix = name.back();
if (postfix != '$')
{
PRINTF("builtin_requestlang: illegal type (expects string)!\n"_fmt);
abort();
}
if (sd->state.menu_or_input)
{
// Second time (rerunline)
sd->state.menu_or_input = 0;
if (name.startswith(".@"_s))
{
struct script_data vd = script_data(ScriptDataStr{sd->npc_str});
set_scope_reg(st, reg, &vd);
}
else
set_reg(sd, VariableCode::VARIABLE, reg, sd->npc_str);
}
else
{
// First time - send prompt to client, then wait
st->state = ScriptEndState::RERUNLINE;
clif_npc_action(sd, st->oid, 0, 0, 0, 0); // send lang request
clif_scriptinputstr(sd, st->oid); // send string prompt
sd->state.menu_or_input = 1;
}
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_if (ScriptState *st)
{
int sel, i;
sel = conv_num(st, &AARG(0));
st->is_true = sel ? 2 : 1;
if (!sel)
return;
// 関数名をコピー
push_copy(st->stack, st->start + 3);
// 間に引数マーカを入れて
push_int<ScriptDataArg>(st->stack, 0);
// 残りの引数をコピー
for (i = st->start + 4; i < st->end; i++)
{
push_copy(st->stack, i);
}
run_func(st);
}
static
void builtin_if_then_else (ScriptState *st)
{
int condition = conv_num(st, &AARG(0));
push_copy(st->stack, st->start + (condition ? 3 : 4));
}
static
void builtin_else (ScriptState *st)
{
int i;
if (st->is_true < 1)
{
PRINTF("builtin_else: no if statement!\n"_fmt);
abort();
}
if (st->is_true > 1)
return;
st->is_true = 0;
// 関数名をコピー
push_copy(st->stack, st->start + 2);
// 間に引数マーカを入れて
push_int<ScriptDataArg>(st->stack, 0);
// 残りの引数をコピー
for (i = st->start + 3; i < st->end; i++)
{
push_copy(st->stack, i);
}
run_func(st);
}
static
void builtin_elif (ScriptState *st)
{
int sel, i;
if (st->is_true < 1)
{
PRINTF("builtin_elif: no if statement!\n"_fmt);
abort();
}
if (st->is_true > 1)
return;
sel = conv_num(st, &AARG(0));
st->is_true = sel ? 2 : 1;
if (!sel)
return;
// 関数名をコピー
push_copy(st->stack, st->start + 3);
// 間に引数マーカを入れて
push_int<ScriptDataArg>(st->stack, 0);
// 残りの引数をコピー
for (i = st->start + 4; i < st->end; i++)
{
push_copy(st->stack, i);
}
run_func(st);
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_foreach_sub(dumb_ptr<block_list> bl, NpcEvent event, BlockId caster)
{
// call_spell_event_script
argrec_t arg[1] =
{
{"@target_id"_s, static_cast<int32_t>(unwrap<BlockId>(bl->bl_id))},
};
npc_event_do_l(event, caster, arg);
}
static
void builtin_foreach(ScriptState *st)
{
int x0, y0, x1, y1, bl_num;
dumb_ptr<block_list> sd;
bl_num = conv_num(st, &AARG(0));
MapName mapname = stringish<MapName>(ZString(conv_str(st, &AARG(1))));
x0 = conv_num(st, &AARG(2));
y0 = conv_num(st, &AARG(3));
x1 = conv_num(st, &AARG(4));
y1 = conv_num(st, &AARG(5));
ZString event_ = ZString(conv_str(st, &AARG(6)));
BL block_type;
NpcEvent event;
extract(event_, &event);
P<map_local> m = TRY_UNWRAP(map_mapname2mapid(mapname), return);
switch (bl_num)
{
case 0:
block_type = BL::PC;
break;
case 1:
block_type = BL::NPC;
break;
case 2:
block_type = BL::MOB;
break;
case 3:
block_type = BL::NUL;
break;
default:
return;
}
if (HARG(7))
sd = map_id_is_player(wrap<BlockId>(conv_num(st, &AARG(7))));
else if (st->rid)
sd = script_rid2sd(st);
if (sd == nullptr)
{
PRINTF("builtin_foreach: player not attached.\n"_fmt);
}
map_foreachinarea(std::bind(builtin_foreach_sub, ph::_1, event, sd->bl_id),
m,
x0, y0,
x1, y1,
block_type);
}
/*========================================
* Destructs a temp NPC
*----------------------------------------
*/
static
void builtin_destroy(ScriptState *st)
{
BlockId id;
if (HARG(0))
id = wrap<BlockId>(conv_num(st, &AARG(0)));
else
id = st->oid;
dumb_ptr<npc_data> nd = map_id_is_npc(id);
if(!nd || nd->npc_subtype != NpcSubtype::SCRIPT)
return;
nd = nd->is_script();
//assert(nd->disposable == true); we don't care about it anymore
npc_free(nd);
if (!HARG(0))
st->state = ScriptEndState::END;
}
/*========================================
* Creates a temp NPC
*----------------------------------------
*/
static
void builtin_puppet(ScriptState *st)
{
int x, y;
NpcName npc = stringish<NpcName>(ZString(conv_str(st, &AARG(3))));
if (npc_name2id(npc) != nullptr)
{
push_int<ScriptDataInt>(st->stack, 0);
return;
}
dumb_ptr<block_list> bl = map_id2bl(st->oid);
dumb_ptr<npc_data_script> parent_nd = bl->is_npc()->is_script();
dumb_ptr<npc_data_script> nd;
nd.new_();
MapName mapname = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
x = conv_num(st, &AARG(1));
y = conv_num(st, &AARG(2));
Species sprite = wrap<Species>(static_cast<uint16_t>(conv_num(st, &AARG(4))));
P<map_local> m = TRY_UNWRAP(map_mapname2mapid(mapname), return);
nd->bl_prev = nd->bl_next = nullptr;
nd->scr.event_needs_map = false;
// PlayerName::SpellName
nd->name = npc;
nd->sex = SEX::UNSPECIFIED;
// Dynamically set location
nd->bl_m = m;
nd->bl_x = x;
nd->bl_y = y;
if (HARG(5) && HARG(6))
{
nd->scr.xs = ((conv_num(st, &AARG(5)) * 2) + 1); // do the same equation as in AST
nd->scr.ys = ((conv_num(st, &AARG(6)) * 2) + 1);
}
nd->bl_id = npc_get_new_npc_id();
nd->scr.parent = parent_nd->bl_id;
nd->dir = DIR::S;
nd->flag = 0;
nd->sit = DamageType::STAND;
nd->npc_class = sprite;
nd->speed = 200_ms;
nd->option = Opt0::ZERO;
nd->opt1 = Opt1::ZERO;
nd->opt2 = Opt2::ZERO;
nd->opt3 = Opt3::ZERO;
nd->scr.label_listv = parent_nd->scr.label_listv;
nd->bl_type = BL::NPC;
nd->npc_subtype = NpcSubtype::SCRIPT;
npc_script++;
nd->n = map_addnpc(nd->bl_m, nd);
map_addblock(nd);
clif_spawnnpc(nd);
register_npc_name(nd);
for (npc_label_list& el : parent_nd->scr.label_listv)
{
ScriptLabel lname = el.name;
int pos = el.pos;
if (lname.startswith("On"_s))
{
struct event_data ev {};
ev.nd = nd;
ev.pos = pos;
NpcEvent buf;
buf.npc = nd->name;
buf.label = lname;
ev_db.insert(buf, ev);
}
}
for (npc_label_list& el : parent_nd->scr.label_listv)
{
int t_ = 0;
ScriptLabel lname = el.name;
int pos = el.pos;
if (lname.startswith("OnTimer"_s) && extract(lname.xslice_t(7), &t_) && t_ > 0)
{
interval_t t = static_cast<interval_t>(t_);
npc_timerevent_list tel {};
tel.timer = t;
tel.pos = pos;
auto it = std::lower_bound(nd->scr.timer_eventv.begin(), nd->scr.timer_eventv.end(), tel,
[](const npc_timerevent_list& l, const npc_timerevent_list& r)
{
return l.timer < r.timer;
}
);
assert (it == nd->scr.timer_eventv.end() || it->timer != tel.timer);
nd->scr.timer_eventv.insert(it, std::move(tel));
}
}
nd->scr.timer = interval_t::zero();
nd->scr.next_event = nd->scr.timer_eventv.begin();
push_int<ScriptDataInt>(st->stack, unwrap<BlockId>(nd->bl_id));
}
/*==========================================
* 変数設定
*------------------------------------------
*/
static
void builtin_set(ScriptState *st)
{
BlockId id;
dumb_ptr<block_list> bl = nullptr;
if (auto *u = AARG(0).get_if<ScriptDataParam>())
{
SIR reg = u->reg;
if(HARG(2))
{
struct script_data *sdata = &AARG(2);
get_val(st, sdata);
CharName name;
if (sdata->is<ScriptDataStr>())
{
name = stringish<CharName>(ZString(conv_str(st, sdata)));
if (name.to__actual())
bl = map_nick2sd(name);
}
else
{
int num = conv_num(st, sdata);
if (num >= 2000000)
id = wrap<BlockId>(num);
else if (num >= 150000)
{
dumb_ptr<map_session_data> p_sd = nullptr;
if ((p_sd = map_nick2sd(map_charid2nick(wrap<CharId>(num)))) != nullptr)
id = p_sd->bl_id;
else
return;
}
else
return;
bl = map_id2bl(id);
}
}
else
bl = script_rid2sd(st);
if (bl == nullptr)
return;
int val = conv_num(st, &AARG(1));
set_reg(bl, VariableCode::PARAM, reg, val);
return;
}
SIR reg = AARG(0).get_if<ScriptDataVariable>()->reg;
ZString name = variable_names.outtern(reg.base());
VarName name_ = stringish<VarName>(name);
char prefix = name_.front();
char postfix = name_.back();
if (prefix != '$')
{
if(HARG(2))
{
struct script_data *sdata = &AARG(2);
get_val(st, sdata);
if(prefix == '.')
{
if (name_.startswith(".@"_s))
{
PRINTF("builtin_set: illegal scope!\n"_fmt);
return;
}
NpcName n_name;
if (sdata->is<ScriptDataStr>())
{
n_name = stringish<NpcName>(ZString(conv_str(st, sdata)));
bl = npc_name2id(n_name);
}
else
{
id = wrap<BlockId>(conv_num(st, sdata));
bl = map_id2bl(id);
}
}
else
{
CharName c_name;
if (sdata->is<ScriptDataStr>())
{
c_name = stringish<CharName>(ZString(conv_str(st, sdata)));
if (c_name.to__actual())
bl = map_nick2sd(c_name);
}
else
{
id = wrap<BlockId>(conv_num(st, sdata));
bl = map_id2bl(id);
}
}
}
else
{
if(prefix == '.')
{
if (name_.startswith(".@"_s))
{
set_scope_reg(st, reg, &AARG(1));
return;
}
bl = map_id_is_npc(st->oid);
}
else
bl = map_id_is_player(st->rid);
}
if (bl == nullptr)
return;
}
if (postfix == '$')
{
// 文字列
RString str = conv_str(st, &AARG(1));
set_reg(bl, VariableCode::VARIABLE, reg, str);
}
else
{
// 数値
int val = conv_num(st, &AARG(1));
set_reg(bl, VariableCode::VARIABLE, reg, val);
}
}
// this is a special function that returns array index for a variable stored in another being
static
int getarraysize2(SIR reg, dumb_ptr<block_list> bl)
{
int i = reg.index(), c = i;
bool zero = true; // index zero is empty
for (; i < 256; i++)
{
struct script_data vd = ScriptDataVariable{reg.iplus(i)};
get_val(bl, &vd);
MATCH_BEGIN (vd)
{
MATCH_CASE (const ScriptDataStr&, u)
{
if (u.str[0])
{
if (i == 0)
zero = false; // index zero is not empty
c = i;
}
continue;
}
MATCH_CASE (const ScriptDataInt&, u)
{
if (u.numi)
{
if (i == 0)
zero = false; // index zero is not empty
c = i;
}
continue;
}
}
MATCH_END ();
abort();
}
return (c == 0 && zero) ? c : (c + 1);
}
/*==========================================
* 配列変数設定
*------------------------------------------
*/
static
void builtin_setarray(ScriptState *st)
{
dumb_ptr<block_list> bl = nullptr;
SIR reg = AARG(0).get_if<ScriptDataVariable>()->reg;
ZString name = variable_names.outtern(reg.base());
char prefix = name.front();
char postfix = name.back();
int i = 1, j = 0;
if (prefix != '$' && prefix != '@' && prefix != '.')
{
PRINTF("builtin_setarray: illegal scope!\n"_fmt);
return;
}
if (prefix == '.' && !name.startswith(".@"_s))
{
struct script_data *sdata = &AARG(1);
get_val(st, sdata);
i++; // 2nd argument is npc, not an array element
if (sdata->is<ScriptDataStr>())
{
ZString tn = conv_str(st, sdata);
if (tn == "this"_s || tn == "oid"_s)
bl = map_id_is_npc(st->oid);
else
{
NpcName name_ = stringish<NpcName>(tn);
bl = npc_name2id(name_);
}
}
else
{
int tid = conv_num(st, sdata);
if (tid == 0)
bl = map_id_is_npc(st->oid);
else
bl = map_id_is_npc(wrap<BlockId>(tid));
}
if (!bl)
{
PRINTF("builtin_setarray: npc not found\n"_fmt);
return;
}
if (st->oid && bl->bl_id != st->oid)
j = getarraysize2(reg, bl);
}
else if (prefix != '$' && !name.startswith(".@"_s))
{
bl = map_id_is_player(st->rid);
if (!bl)
{
PRINTF("builtin_setarray: rid not attached\n"_fmt);
return;
}
}
for (; i < st->end - st->start - 2 && j < 256; i++, j++)
{
if (name.startswith(".@"_s))
set_scope_reg(st, reg.iplus(j), &AARG(i));
else if (postfix == '$')
set_reg(bl, VariableCode::VARIABLE, reg.iplus(j), conv_str(st, &AARG(i)));
else
set_reg(bl, VariableCode::VARIABLE, reg.iplus(j), conv_num(st, &AARG(i)));
}
}
/*==========================================
* 配列変数クリア
*------------------------------------------
*/
static
void builtin_cleararray(ScriptState *st)
{
dumb_ptr<block_list> bl = nullptr;
SIR reg = AARG(0).get_if<ScriptDataVariable>()->reg;
ZString name = variable_names.outtern(reg.base());
char prefix = name.front();
char postfix = name.back();
int sz = conv_num(st, &AARG(2));
if (prefix != '$' && prefix != '@' && prefix != '.')
{
PRINTF("builtin_cleararray: illegal scope!\n"_fmt);
return;
}
if (prefix == '.' && !name.startswith(".@"_s))
bl = map_id_is_npc(st->oid);
else if (prefix != '$' && !name.startswith(".@"_s))
{
bl = map_id_is_player(st->rid);
if (!bl)
{
PRINTF("builtin_cleararray: no block list\n"_fmt);
return;
}
}
for (int i = 0; i < sz; i++)
{
if (name.startswith(".@"_s))
set_scope_reg(st, reg.iplus(i), &AARG(i));
else if (postfix == '$')
set_reg(bl, VariableCode::VARIABLE, reg.iplus(i), conv_str(st, &AARG(1)));
else
set_reg(bl, VariableCode::VARIABLE, reg.iplus(i), conv_num(st, &AARG(1)));
}
}
/*==========================================
* 配列変数のサイズ所得
*------------------------------------------
*/
static
int getarraysize(ScriptState *st, SIR reg)
{
int i = reg.index(), c = i;
for (; i < 256; i++)
{
struct script_data vd = get_val2(st, reg.iplus(i));
MATCH_BEGIN (vd)
{
MATCH_CASE (const ScriptDataStr&, u)
{
if (u.str[0])
c = i;
continue;
}
MATCH_CASE (const ScriptDataInt&, u)
{
if (u.numi)
c = i;
continue;
}
}
MATCH_END ();
abort();
}
return c + 1;
}
static
void builtin_getarraysize(ScriptState *st)
{
SIR reg = AARG(0).get_if<ScriptDataVariable>()->reg;
ZString name = variable_names.outtern(reg.base());
char prefix = name.front();
if (prefix != '$' && prefix != '@' && prefix != '.')
{
PRINTF("builtin_copyarray: illegal scope!\n"_fmt);
return;
}
push_int<ScriptDataInt>(st->stack, getarraysize(st, reg));
}
/*==========================================
* 指定要素を表す値(キー)を所得する
*------------------------------------------
*/
static
void builtin_getelementofarray(ScriptState *st)
{
if (auto *u = AARG(0).get_if<ScriptDataVariable>())
{
int i = conv_num(st, &AARG(1));
if (i > 255 || i < 0)
{
PRINTF("script: getelementofarray (operator[]): param2 illegal number: %d\n"_fmt,
i);
push_int<ScriptDataInt>(st->stack, 0);
}
else
{
push_reg<ScriptDataVariable>(st->stack,
u->reg.iplus(i));
}
}
else
{
PRINTF("script: getelementofarray (operator[]): param1 not named!\n"_fmt);
push_int<ScriptDataInt>(st->stack, 0);
}
}
static
void builtin_array_search(ScriptState *st)
{
ZString needle_str = ZString(conv_str(st, &AARG(0)));
int needle_int = conv_num(st, &AARG(0));
SIR reg = AARG(1).get_if<ScriptDataVariable>()->reg; // haystack
ZString name = variable_names.outtern(reg.base());
char prefix = name.front();
int i, c;
if (prefix != '$' && prefix != '@' && prefix != '.')
{
PRINTF("builtin_array_search: illegal scope!\n"_fmt);
return;
}
i = reg.index(); c = -1;
for (; i < 256; i++)
{
struct script_data vd = get_val2(st, reg.iplus(i));
MATCH_BEGIN (vd)
{
MATCH_CASE (const ScriptDataStr&, u)
{
if (u.str == needle_str)
{
c = i;
goto Out;
}
continue;
}
MATCH_CASE (const ScriptDataInt&, u)
{
if (u.numi == needle_int)
{
c = i;
goto Out;
}
continue;
}
}
MATCH_END ();
abort();
}
Out:
push_int<ScriptDataInt>(st->stack, c);
}
static
void builtin_wgm(ScriptState *st)
{
ZString message = ZString(conv_str(st, &AARG(0)));
intif_wis_message_to_gm(WISP_SERVER_NAME,
battle_config.hack_info_GM_level,
STRPRINTF("[GM] %s"_fmt, message));
}
static
void builtin_gmlog(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
ZString message = ZString(conv_str(st, &AARG(0)));
log_atcommand(sd, STRPRINTF("{SCRIPT} %s"_fmt, message));
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_setlook(ScriptState *st)
{
LOOK type = LOOK(conv_num(st, &AARG(0)));
int val = conv_num(st, &AARG(1));
pc_changelook(script_rid2sd(st), type, val);
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_countitem(ScriptState *st)
{
ItemNameId nameid;
int count = 0;
dumb_ptr<map_session_data> sd;
struct script_data *data;
sd = script_rid2sd(st);
data = &AARG(0);
get_val(st, data);
if (data->is<ScriptDataStr>())
{
ZString name = ZString(conv_str(st, data));
Option<P<struct item_data>> item_data_ = itemdb_searchname(name);
OMATCH_BEGIN_SOME (item_data, item_data_)
{
nameid = item_data->nameid;
}
OMATCH_END ();
}
else
nameid = wrap<ItemNameId>(conv_num(st, data));
if (nameid)
{
for (IOff0 i : IOff0::iter())
{
if (sd->status.inventory[i].nameid == nameid)
count += sd->status.inventory[i].amount;
}
}
else
{
if (battle_config.error_log)
PRINTF("wrong item ID: countitem (%i)\n"_fmt, nameid);
}
push_int<ScriptDataInt>(st->stack, count);
}
/*==========================================
* 重量チェック
*------------------------------------------
*/
static
void builtin_checkweight(ScriptState *st)
{
ItemNameId nameid;
int amount;
dumb_ptr<map_session_data> sd;
struct script_data *data;
sd = script_rid2sd(st);
data = &AARG(0);
get_val(st, data);
if (data->is<ScriptDataStr>())
{
ZString name = ZString(conv_str(st, data));
Option<P<struct item_data>> item_data_ = itemdb_searchname(name);
OMATCH_BEGIN_SOME (item_data, item_data_)
{
nameid = item_data->nameid;
}
OMATCH_END ();
}
else
nameid = wrap<ItemNameId>(conv_num(st, data));
amount = conv_num(st, &AARG(1));
if (amount <= 0 || !nameid)
{
//If it gets the wrong item ID or the amount<=0, don't count its weight (assume it's a non-existent item)
push_int<ScriptDataInt>(st->stack, 0);
return;
}
if (itemdb_weight(nameid) * amount + sd->weight > sd->max_weight)
{
push_int<ScriptDataInt>(st->stack, 0);
}
else
{
push_int<ScriptDataInt>(st->stack, 1);
}
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_getitem(ScriptState *st)
{
ItemNameId nameid;
int amount;
dumb_ptr<map_session_data> sd;
struct script_data *data;
sd = script_rid2sd(st);
data = &AARG(0);
get_val(st, data);
if (data->is<ScriptDataStr>())
{
ZString name = ZString(conv_str(st, data));
Option<P<struct item_data>> item_data_ = itemdb_searchname(name);
OMATCH_BEGIN_SOME (item_data, item_data_)
{
nameid = item_data->nameid;
}
OMATCH_END ();
}
else
nameid = wrap<ItemNameId>(conv_num(st, data));
if ((amount =
conv_num(st, &AARG(1))) <= 0)
{
return; //return if amount <=0, skip the useles iteration
}
if (nameid)
{
Item item_tmp {};
item_tmp.nameid = nameid;
if (HARG(3)) //アイテムを指定したIDに渡す
sd = map_id2sd(wrap<BlockId>(conv_num(st, &AARG(3))));
if (sd == nullptr) //アイテムを渡す相手がいなかったらお帰り
return;
PickupFail flag;
if ((flag = pc_additem(sd, &item_tmp, amount)) != PickupFail::OKAY)
{
clif_additem(sd, IOff0::from(0), 0, flag);
map_addflooritem(&item_tmp, amount,
sd->bl_m, sd->bl_x, sd->bl_y,
nullptr, nullptr, nullptr);
}
}
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_makeitem(ScriptState *st)
{
ItemNameId nameid;
int amount;
int x, y;
dumb_ptr<map_session_data> sd;
struct script_data *data;
sd = script_rid2sd(st);
data = &AARG(0);
get_val(st, data);
if (data->is<ScriptDataStr>())
{
ZString name = ZString(conv_str(st, data));
Option<P<struct item_data>> item_data_ = itemdb_searchname(name);
OMATCH_BEGIN_SOME (item_data, item_data_)
{
nameid = item_data->nameid;
}
OMATCH_END ();
}
else
nameid = wrap<ItemNameId>(conv_num(st, data));
amount = conv_num(st, &AARG(1));
MapName mapname = stringish<MapName>(ZString(conv_str(st, &AARG(2))));
x = conv_num(st, &AARG(3));
y = conv_num(st, &AARG(4));
P<map_local> m = ((sd && mapname == MOB_THIS_MAP)
? sd->bl_m
: TRY_UNWRAP(map_mapname2mapid(mapname), return));
if (nameid)
{
Item item_tmp {};
item_tmp.nameid = nameid;
map_addflooritem(&item_tmp, amount, m, x, y, nullptr, nullptr, nullptr);
}
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_delitem(ScriptState *st)
{
ItemNameId nameid;
int amount;
dumb_ptr<map_session_data> sd;
struct script_data *data;
sd = script_rid2sd(st);
data = &AARG(0);
get_val(st, data);
if (data->is<ScriptDataStr>())
{
ZString name = ZString(conv_str(st, data));
Option<P<struct item_data>> item_data_ = itemdb_searchname(name);
OMATCH_BEGIN_SOME (item_data, item_data_)
{
nameid = item_data->nameid;
}
OMATCH_END ();
}
else
nameid = wrap<ItemNameId>(conv_num(st, data));
amount = conv_num(st, &AARG(1));
if (!nameid || amount <= 0)
{
//By Lupus. Don't run FOR if you've got the wrong item ID or amount<=0
return;
}
for (IOff0 i : IOff0::iter())
{
if (sd->status.inventory[i].nameid == nameid)
{
if (sd->status.inventory[i].amount >= amount)
{
pc_delitem(sd, i, amount, 0);
break;
}
else
{
amount -= sd->status.inventory[i].amount;
if (amount == 0)
amount = sd->status.inventory[i].amount;
pc_delitem(sd, i, amount, 0);
break;
}
}
}
}
static
void builtin_getversion(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);;
push_int<ScriptDataInt>(st->stack, unwrap<ClientVersion>(sd->client_version));
}
/*==========================================
*キャラ関係のID取得
*------------------------------------------
*/
static
void builtin_getcharid(ScriptState *st)
{
int num;
dumb_ptr<map_session_data> sd;
num = conv_num(st, &AARG(0));
if (HARG(1))
sd = map_nick2sd(stringish<CharName>(ZString(conv_str(st, &AARG(1)))));
else
sd = script_rid2sd(st);
if (sd == nullptr)
{
push_int<ScriptDataInt>(st->stack, -1);
return;
}
if (num == 0)
push_int<ScriptDataInt>(st->stack, unwrap<CharId>(sd->status_key.char_id));
if (num == 1)
push_int<ScriptDataInt>(st->stack, unwrap<PartyId>(sd->status.party_id));
if (num == 2)
push_int<ScriptDataInt>(st->stack, 0/*guild_id*/);
if (num == 3)
push_int<ScriptDataInt>(st->stack, unwrap<AccountId>(sd->status_key.account_id));
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_getnpcid(ScriptState *st)
{
dumb_ptr<npc_data> nd;
if (HARG(0))
nd = npc_name2id(stringish<NpcName>(ZString(conv_str(st, &AARG(0)))));
else
nd = map_id_is_npc(st->oid);
if (nd == nullptr)
{
push_int<ScriptDataInt>(st->stack, -1);
return;
}
push_int<ScriptDataInt>(st->stack, unwrap<BlockId>(nd->bl_id));
}
/*==========================================
*指定IDのPT名取得
*------------------------------------------
*/
static
RString builtin_getpartyname_sub(PartyId party_id)
{
Option<PartyPair> p = party_search(party_id);
return p.pmd_pget(&PartyMost::name).copy_or(PartyName());
}
/*==========================================
* キャラクタの名前
*------------------------------------------
*/
static
void builtin_strcharinfo(ScriptState *st)
{
dumb_ptr<map_session_data> sd;
int num;
if (HARG(1)) //指定したキャラを状態異常にする
sd = map_id_is_player(wrap<BlockId>(conv_num(st, &AARG(1))));
else
sd = script_rid2sd(st);
if (!sd)
{
PRINTF("builtin_strcharinfo: player not found\n"_fmt);
return;
}
num = conv_num(st, &AARG(0));
if (num == 0)
{
RString buf = sd->status_key.name.to__actual();
push_str<ScriptDataStr>(st->stack, buf);
}
if (num == 1)
{
RString buf = builtin_getpartyname_sub(sd->status.party_id);
if (buf)
push_str<ScriptDataStr>(st->stack, buf);
else
push_str<ScriptDataStr>(st->stack, ""_s);
}
if (num == 2)
{
// was: guild name
push_str<ScriptDataStr>(st->stack, ""_s);
}
}
// indexed by the equip_* in db/const.txt
// TODO change to use EQUIP
static
Array<EPOS, 11> equip //=
{{
EPOS::HAT,
EPOS::MISC1,
EPOS::SHIELD,
EPOS::WEAPON,
EPOS::GLOVES,
EPOS::SHOES,
EPOS::CAPE,
EPOS::MISC2,
EPOS::TORSO,
EPOS::LEGS,
EPOS::ARROW,
}};
/*==========================================
* GetEquipID(Pos); Pos: 1-10
*------------------------------------------
*/
static
void builtin_getequipid(ScriptState *st)
{
int num;
dumb_ptr<map_session_data> sd;
if (HARG(1))
sd = map_nick2sd(stringish<CharName>(ZString(conv_str(st, &AARG(1)))));
else
sd = script_rid2sd(st);
if (sd == nullptr)
{
PRINTF("getequipid: sd == nullptr\n"_fmt);
return;
}
num = conv_num(st, &AARG(0));
IOff0 i = pc_checkequip(sd, equip[num - 1]);
if (i.ok())
{
Option<P<struct item_data>> item_ = sd->inventory_data[i];
OMATCH_BEGIN (item_)
{
OMATCH_CASE_SOME (item)
{
push_int<ScriptDataInt>(st->stack, unwrap<ItemNameId>(item->nameid));
}
OMATCH_CASE_NONE ()
{
push_int<ScriptDataInt>(st->stack, 0);
}
}
OMATCH_END ();
}
else
{
push_int<ScriptDataInt>(st->stack, -1);
}
}
/*==========================================
* freeloop
*------------------------------------------
*/
static
void builtin_freeloop(ScriptState *st)
{
int num;
num = conv_num(st, &AARG(0));
if(num == 1)
{
st->freeloop = 1;
}
else
{
st->freeloop = 0;
}
}
/*==========================================
* 装備品による能力値ボーナス
*------------------------------------------
*/
static
void builtin_bonus(ScriptState *st)
{
SP type;
if (auto *u = AARG(0).get_if<ScriptDataParam>())
type = u->reg.sp();
else
type = SP(conv_num(st, &AARG(0)));
int val = conv_num(st, &AARG(1));
dumb_ptr<map_session_data> sd = script_rid2sd(st);
pc_bonus(sd, type, val);
}
/*==========================================
* 装備品による能力値ボーナス
*------------------------------------------
*/
static
void builtin_bonus2(ScriptState *st)
{
SP type;
if (auto *u = AARG(0).get_if<ScriptDataParam>())
type = u->reg.sp();
else
type = SP(conv_num(st, &AARG(0)));
int type2 = conv_num(st, &AARG(1));
int val = conv_num(st, &AARG(2));
dumb_ptr<map_session_data> sd = script_rid2sd(st);
pc_bonus2(sd, type, type2, val);
}
/*==========================================
* スキル所得
*------------------------------------------
*/
static
void builtin_skill(ScriptState *st)
{
int level, flag = 1;
dumb_ptr<map_session_data> sd;
SkillID id = SkillID(conv_num(st, &AARG(0)));
level = conv_num(st, &AARG(1));
if (HARG(2))
flag = conv_num(st, &AARG(2));
sd = script_rid2sd(st);
pc_skill(sd, id, level, flag);
clif_skillinfoblock(sd);
}
/*==========================================
* [Fate] Sets the skill level permanently
*------------------------------------------
*/
static
void builtin_setskill(ScriptState *st)
{
int level;
dumb_ptr<map_session_data> sd;
SkillID id = static_cast<SkillID>(conv_num(st, &AARG(0)));
level = conv_num(st, &AARG(1));
sd = script_rid2sd(st);
level = std::min(level, MAX_SKILL_LEVEL);
level = std::max(level, 0);
sd->status.skill[id].lv = level;
clif_skillinfoblock(sd);
}
/*==========================================
* スキルレベル所得
*------------------------------------------
*/
static
void builtin_getskilllv(ScriptState *st)
{
SkillID id = SkillID(conv_num(st, &AARG(0)));
push_int<ScriptDataInt>(st->stack, pc_checkskill(script_rid2sd(st), id));
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_overrideattack(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
if (HARG(0))
{
interval_t attack_delay = static_cast<interval_t>(conv_num(st, &AARG(0)));
int attack_range = conv_num(st, &AARG(1));
StatusChange icon = StatusChange(conv_num(st, &AARG(2)));
ItemNameId look = wrap<ItemNameId>(static_cast<uint16_t>(conv_num(st, &AARG(3))));
ZString event_ = ZString(conv_str(st, &AARG(4)));
NpcEvent event;
extract(event_, &event);
sd->attack_spell_override = st->oid;
sd->attack_spell_charges = 1;
sd->magic_attack = event;
pc_set_weapon_icon(sd, 1, icon, look);
pc_set_attack_info(sd, attack_delay, attack_range);
}
else
{
// explicit discharge
sd->attack_spell_override = BlockId();
pc_set_weapon_icon(sd, 0, StatusChange::ZERO, ItemNameId());
pc_set_attack_info(sd, interval_t::zero(), 0);
pc_calcstatus(sd, 0);
}
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_getgmlevel(ScriptState *st)
{
push_int<ScriptDataInt>(st->stack, pc_isGM(script_rid2sd(st)).get_all_bits());
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_end(ScriptState *st)
{
if (st->stack->stack_datav[st->defsp - 1].is<ScriptDataRetInfo>())
{
dumb_ptr<npc_data> nd = map_id_is_npc(st->oid);
if(nd)
PRINTF("Deprecated: close in a callfunc or callsub! @ %s\n"_fmt, nd->name);
else
PRINTF("Deprecated: close in a callfunc or callsub! (no npc)\n"_fmt);
}
st->state = ScriptEndState::END;
}
/*==========================================
* [Freeyorp] Return the current opt2
*------------------------------------------
*/
static
void builtin_getopt2(ScriptState *st)
{
dumb_ptr<map_session_data> sd;
sd = script_rid2sd(st);
push_int<ScriptDataInt>(st->stack, static_cast<uint16_t>(sd->opt2));
}
/*==========================================
* [Freeyorp] Sets opt2
*------------------------------------------
*/
static
void builtin_setopt2(ScriptState *st)
{
dumb_ptr<map_session_data> sd;
Opt2 new_opt2 = Opt2(conv_num(st, &AARG(0)));
sd = script_rid2sd(st);
if (new_opt2 == sd->opt2)
return;
sd->opt2 = new_opt2;
clif_changeoption(sd);
pc_calcstatus(sd, 0);
}
/*==========================================
* セーブポイントの保存
*------------------------------------------
*/
static
void builtin_savepoint(ScriptState *st)
{
int x, y;
MapName str = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
x = conv_num(st, &AARG(1));
y = conv_num(st, &AARG(2));
pc_setsavepoint(script_rid2sd(st), str, x, y);
}
/*==========================================
* gettimetick(type)
*
* type The type of time measurement.
* Specify 0 for the system tick, 1 for
* seconds elapsed today, or 2 for seconds
* since Unix epoch. Defaults to 0 for any
* other value.
*------------------------------------------
*/
static
void builtin_gettimetick(ScriptState *st) /* Asgard Version */
{
int type;
type = conv_num(st, &AARG(0));
switch (type)
{
/* Number of seconds elapsed today(0-86399, 00:00:00-23:59:59). */
case 1:
{
struct tm t = TimeT::now();
push_int<ScriptDataInt>(st->stack,
t.tm_hour * 3600 + t.tm_min * 60 + t.tm_sec);
break;
}
/* Seconds since Unix epoch. */
case 2:
push_int<ScriptDataInt>(st->stack, static_cast<time_t>(TimeT::now()));
break;
/* System tick(unsigned int, and yes, it will wrap). */
case 0:
default:
push_int<ScriptDataInt>(st->stack, gettick().time_since_epoch().count());
break;
}
}
/*==========================================
* GetTime(Type);
* 1: Sec 2: Min 3: Hour
* 4: WeekDay 5: MonthDay 6: Month
* 7: Year
*------------------------------------------
*/
static
void builtin_gettime(ScriptState *st) /* Asgard Version */
{
int type = conv_num(st, &AARG(0));
struct tm t = TimeT::now();
switch (type)
{
case 1: //Sec(0~59)
push_int<ScriptDataInt>(st->stack, t.tm_sec);
break;
case 2: //Min(0~59)
push_int<ScriptDataInt>(st->stack, t.tm_min);
break;
case 3: //Hour(0~23)
push_int<ScriptDataInt>(st->stack, t.tm_hour);
break;
case 4: //WeekDay(0~6)
push_int<ScriptDataInt>(st->stack, t.tm_wday);
break;
case 5: //MonthDay(01~31)
push_int<ScriptDataInt>(st->stack, t.tm_mday);
break;
case 6: //Month(01~12)
push_int<ScriptDataInt>(st->stack, t.tm_mon + 1);
break;
case 7: //Year(20xx)
push_int<ScriptDataInt>(st->stack, t.tm_year + 1900);
break;
default: //(format error)
push_int<ScriptDataInt>(st->stack, -1);
break;
}
}
/*==========================================
* カプラ倉庫を開く
*------------------------------------------
*/
static
void builtin_openstorage(ScriptState *st)
{
// int sync = 0;
// if (st->end >= 3) sync = conv_num(st,& (st->stack->stack_data[st->start+2]));
dumb_ptr<map_session_data> sd = script_rid2sd(st);
// if (sync) {
st->state = ScriptEndState::STOP;
sd->npc_flags.storage = 1;
// } else st->state = ScriptEndState::END;
storage_storageopen(sd);
}
/*==========================================
* NPCで経験値上げる
*------------------------------------------
*/
static
void builtin_getexp(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
int base = 0, job = 0;
base = conv_num(st, &AARG(0));
job = conv_num(st, &AARG(1));
if (base < 0 || job < 0)
return;
if (sd)
pc_gainexp_reason(sd, base, job, PC_GAINEXP_REASON::SCRIPT);
}
static
void builtin_summon(ScriptState *st)
{
NpcEvent event;
MapName map = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
int x = conv_num(st, &AARG(1));
int y = conv_num(st, &AARG(2));
dumb_ptr<block_list> owner_e = map_id2bl(wrap<BlockId>(conv_num(st, &AARG(3))));
dumb_ptr<map_session_data> owner = nullptr;
Species monster_id = wrap<Species>(conv_num(st, &AARG(4)));
MonsterAttitude monster_attitude = static_cast<MonsterAttitude>(conv_num(st, &AARG(5)));
interval_t lifespan = static_cast<interval_t>(conv_num(st, &AARG(6)));
if (HARG(7))
extract(ZString(conv_str(st, &AARG(7))), &event);
if (!owner_e)
{
PRINTF("builtin_summon: bad owner\n"_fmt);
return;
}
if (monster_attitude == MonsterAttitude::SERVANT
&& owner_e->bl_type == BL::PC)
owner = owner_e->is_player(); // XXX in the future this should also work with mobs as owner
BlockId mob_id = mob_once_spawn(owner, map, x, y, MobName(), monster_id, 1, event);
dumb_ptr<mob_data> mob = map_id_is_mob(mob_id);
if (mob)
{
mob->mode = get_mob_db(monster_id).mode;
switch (monster_attitude)
{
case MonsterAttitude::SERVANT:
mob->state.special_mob_ai = 1;
mob->mode |= MobMode::AGGRESSIVE;
break;
case MonsterAttitude::FRIENDLY:
mob->mode = MobMode::CAN_ATTACK | (mob->mode & MobMode::CAN_MOVE);
break;
case MonsterAttitude::HOSTILE:
mob->mode = MobMode::CAN_ATTACK | MobMode::AGGRESSIVE | (mob->mode & MobMode::CAN_MOVE);
if (owner)
{
mob->target_id = owner->bl_id;
mob->attacked_id = owner->bl_id;
}
break;
case MonsterAttitude::FROZEN:
mob->mode = MobMode::ZERO;
break;
}
mob->mode |=
MobMode::SUMMONED | MobMode::TURNS_AGAINST_BAD_MASTER;
mob->deletetimer = Timer(gettick() + lifespan,
std::bind(mob_timer_delete, ph::_1, ph::_2,
mob_id));
if (owner)
{
mob->master_id = owner->bl_id;
mob->master_dist = 6;
}
}
}
/*==========================================
* モンスター発生
*------------------------------------------
*/
static
void builtin_monster(ScriptState *st)
{
Species mob_class;
int amount, x, y;
NpcEvent event;
MapName mapname = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
x = conv_num(st, &AARG(1));
y = conv_num(st, &AARG(2));
MobName str = stringish<MobName>(ZString(conv_str(st, &AARG(3))));
mob_class = wrap<Species>(conv_num(st, &AARG(4)));
amount = conv_num(st, &AARG(5));
if (HARG(6))
extract(ZString(conv_str(st, &AARG(6))), &event);
mob_once_spawn(map_id2sd(st->rid), mapname, x, y, str, mob_class, amount,
event);
}
/*==========================================
* モンスター発生
*------------------------------------------
*/
static
void builtin_areamonster(ScriptState *st)
{
Species mob_class;
int amount, x0, y0, x1, y1;
NpcEvent event;
MapName mapname = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
x0 = conv_num(st, &AARG(1));
y0 = conv_num(st, &AARG(2));
x1 = conv_num(st, &AARG(3));
y1 = conv_num(st, &AARG(4));
MobName str = stringish<MobName>(ZString(conv_str(st, &AARG(5))));
mob_class = wrap<Species>(conv_num(st, &AARG(6)));
amount = conv_num(st, &AARG(7));
if (HARG(8))
extract(ZString(conv_str(st, &AARG(8))), &event);
mob_once_spawn_area(map_id2sd(st->rid), mapname, x0, y0, x1, y1, str, mob_class,
amount, event);
}
/*==========================================
* モンスター削除
*------------------------------------------
*/
static
void builtin_killmonster_sub(dumb_ptr<block_list> bl, NpcEvent event)
{
dumb_ptr<mob_data> md = bl->is_mob();
if (event)
{
if (event == md->npc_event)
mob_delete(md);
return;
}
else if (!event)
{
if (md->spawn.delay1 == static_cast<interval_t>(-1)
&& md->spawn.delay2 == static_cast<interval_t>(-1))
mob_delete(md);
return;
}
}
static
void builtin_killmonster(ScriptState *st)
{
MapName mapname = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
ZString event_ = ZString(conv_str(st, &AARG(1)));
NpcEvent event;
if (event_ != "All"_s)
extract(event_, &event);
P<map_local> m = TRY_UNWRAP(map_mapname2mapid(mapname), return);
map_foreachinarea(std::bind(builtin_killmonster_sub, ph::_1, event),
m,
0, 0,
m->xs, m->ys,
BL::MOB);
}
/*==========================================
* NPC主体イベント実行
*------------------------------------------
*/
static
void builtin_donpcevent(ScriptState *st)
{
ZString event_ = ZString(conv_str(st, &AARG(0)));
NpcEvent event;
extract(event_, &event);
npc_event_do(event);
}
/*==========================================
* イベントタイマー追加
*------------------------------------------
*/
static
void builtin_addtimer(ScriptState *st)
{
dumb_ptr<map_session_data> sd;
interval_t tick = static_cast<interval_t>(conv_num(st, &AARG(0)));
ZString event_ = ZString(conv_str(st, &AARG(1)));
NpcEvent event;
extract(event_, &event);
if (HARG(2))
sd = map_id_is_player(wrap<BlockId>(conv_num(st, &AARG(2))));
else if (st->rid)
sd = script_rid2sd(st);
if (sd == nullptr)
{
PRINTF("builtin_addtimer: player not attached.\n"_fmt);
}
pc_addeventtimer(sd, tick, event);
}
/*==========================================
* NPCイベントタイマー追加
*------------------------------------------
*/
static
void builtin_addnpctimer(ScriptState *st)
{
interval_t tick = static_cast<interval_t>(conv_num(st, &AARG(0)));
ZString event_ = ZString(conv_str(st, &AARG(1)));
NpcEvent event;
extract(event_, &event);
npc_addeventtimer(npc_name2id(event.npc), tick, event);
}
/*==========================================
* NPCタイマー初期化
*------------------------------------------
*/
static
void builtin_initnpctimer(ScriptState *st)
{
dumb_ptr<npc_data> nd_;
if (HARG(0))
nd_ = npc_name2id(stringish<NpcName>(ZString(conv_str(st, &AARG(0)))));
else
nd_ = map_id_is_npc(st->oid);
assert (nd_ && nd_->npc_subtype == NpcSubtype::SCRIPT);
dumb_ptr<npc_data_script> nd = nd_->is_script();
npc_settimerevent_tick(nd, interval_t::zero());
npc_timerevent_start(nd);
}
/*==========================================
* NPCタイマー開始
*------------------------------------------
*/
static
void builtin_startnpctimer(ScriptState *st)
{
dumb_ptr<npc_data> nd_;
if (HARG(0))
nd_ = npc_name2id(stringish<NpcName>(ZString(conv_str(st, &AARG(0)))));
else
nd_ = map_id_is_npc(st->oid);
assert (nd_ && nd_->npc_subtype == NpcSubtype::SCRIPT);
dumb_ptr<npc_data_script> nd = nd_->is_script();
npc_timerevent_start(nd);
}
/*==========================================
* NPCタイマー停止
*------------------------------------------
*/
static
void builtin_stopnpctimer(ScriptState *st)
{
dumb_ptr<npc_data> nd_;
if (HARG(0))
nd_ = npc_name2id(stringish<NpcName>(ZString(conv_str(st, &AARG(0)))));
else
nd_ = map_id_is_npc(st->oid);
assert (nd_ && nd_->npc_subtype == NpcSubtype::SCRIPT);
dumb_ptr<npc_data_script> nd = nd_->is_script();
npc_timerevent_stop(nd);
}
/*==========================================
* NPCタイマー情報所得
*------------------------------------------
*/
static
void builtin_getnpctimer(ScriptState *st)
{
dumb_ptr<npc_data> nd_;
int type = conv_num(st, &AARG(0));
int val = 0;
if (HARG(1))
nd_ = npc_name2id(stringish<NpcName>(ZString(conv_str(st, &AARG(1)))));
else
nd_ = map_id_is_npc(st->oid);
assert (nd_ && nd_->npc_subtype == NpcSubtype::SCRIPT);
dumb_ptr<npc_data_script> nd = nd_->is_script();
switch (type)
{
case 0:
val = npc_gettimerevent_tick(nd).count();
break;
case 1:
val = nd->scr.timer_active;
break;
case 2:
val = nd->scr.timer_eventv.size();
break;
}
push_int<ScriptDataInt>(st->stack, val);
}
/*==========================================
* NPCタイマー値設定
*------------------------------------------
*/
static
void builtin_setnpctimer(ScriptState *st)
{
dumb_ptr<npc_data> nd_;
interval_t tick = static_cast<interval_t>(conv_num(st, &AARG(0)));
if (HARG(1))
nd_ = npc_name2id(stringish<NpcName>(ZString(conv_str(st, &AARG(1)))));
else
nd_ = map_id_is_npc(st->oid);
assert (nd_ && nd_->npc_subtype == NpcSubtype::SCRIPT);
dumb_ptr<npc_data_script> nd = nd_->is_script();
npc_settimerevent_tick(nd, tick);
}
static
void builtin_npcaction(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
short command = conv_num(st, &AARG(0));
int id = 0;
short x = HARG(2) ? conv_num(st, &AARG(2)) : 0;
short y = HARG(3) ? conv_num(st, &AARG(3)) : 0;
if(HARG(1))
{
if(command == 2)
{
dumb_ptr<npc_data> nd_;
nd_ = npc_name2id(stringish<NpcName>(ZString(conv_str(st, &AARG(1)))));
id = unwrap<BlockId>(nd_->bl_id);
}
else
id = conv_num(st, &AARG(1));
}
clif_npc_action(sd, st->oid, command, id, x, y);
}
static
void builtin_camera(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
if (HARG(0))
{
if (HARG(1) && !HARG(2))
clif_npc_action(sd, st->oid, 2, 0, conv_num(st, &AARG(0)), conv_num(st, &AARG(1))); // camera to x, y
else
{
dumb_ptr<block_list> bl;
short x = 0, y = 0;
bool rel = false;
if (auto *u = AARG(0).get_if<ScriptDataInt>())
bl = map_id2bl(wrap<BlockId>(u->numi));
if (auto *g = AARG(0).get_if<ScriptDataStr>())
{
if (g->str == "rid"_s || g->str == "player"_s)
bl = sd;
if (g->str == "relative"_s)
rel = true;
else if (g->str == "oid"_s || g->str == "npc"_s)
bl = map_id2bl(st->oid);
else
bl = npc_name2id(stringish<NpcName>(g->str));
}
if (HARG(1) && HARG(2))
{
x = conv_num(st, &AARG(1));
y = conv_num(st, &AARG(2));
}
if (rel)
clif_npc_action(sd, st->oid, 4, 0, x, y); // camera relative from current camera
else
clif_npc_action(sd, st->oid, 2, unwrap<BlockId>(bl->bl_id), x, y); // camera to actor
}
}
else
clif_npc_action(sd, st->oid, 3, 0, 0, 0); // return camera
}
static
void builtin_setnpcdirection(ScriptState *st)
{
dumb_ptr<npc_data> nd_;
DIR dir = static_cast<DIR>(conv_num(st, &AARG(0)));
bool save = bool(conv_num(st, &AARG(2)));
DamageType action;
if (HARG(3))
nd_ = npc_name2id(stringish<NpcName>(ZString(conv_str(st, &AARG(3)))));
else
nd_ = map_id_is_npc(st->oid);
assert (nd_);
if (bool(conv_num(st, &AARG(1))))
action = DamageType::SIT;
else
action = DamageType::STAND;
if (save)
{
nd_->dir = dir;
nd_->sit = action;
}
if (st->rid)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
clif_sitnpc_towards(sd, nd_, action);
clif_setnpcdirection_towards(sd, nd_, dir);
}
else
{
clif_sitnpc(nd_, action);
clif_setnpcdirection(nd_, dir);
}
}
/*==========================================
* 天の声アナウンス
*------------------------------------------
*/
static
void builtin_announce(ScriptState *st)
{
int flag;
ZString str = ZString(conv_str(st, &AARG(0)));
flag = conv_num(st, &AARG(1));
if (flag & 0x0f)
{
dumb_ptr<block_list> bl;
if (flag & 0x08)
bl = map_id2bl(st->oid);
else
bl = script_rid2sd(st);
clif_GMmessage(bl, str, flag);
}
else
intif_GMmessage(str);
}
/*==========================================
* 天の声アナウンス(特定マップ)
*------------------------------------------
*/
static
void builtin_mapannounce_sub(dumb_ptr<block_list> bl, XString str, int flag)
{
clif_GMmessage(bl, str, flag | 3);
}
static
void builtin_mapannounce(ScriptState *st)
{
int flag;
MapName mapname = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
ZString str = ZString(conv_str(st, &AARG(1)));
flag = conv_num(st, &AARG(2));
P<map_local> m = TRY_UNWRAP(map_mapname2mapid(mapname), return);
map_foreachinarea(std::bind(builtin_mapannounce_sub, ph::_1, str, flag & 0x10),
m,
0, 0,
m->xs, m->ys,
BL::PC);
}
/*==========================================
* ユーザー数所得
*------------------------------------------
*/
static
void builtin_getusers(ScriptState *st)
{
int flag = conv_num(st, &AARG(0));
dumb_ptr<block_list> bl = map_id2bl((flag & 0x08) ? st->oid : st->rid);
int val = 0;
switch (flag & 0x07)
{
case 0:
val = bl->bl_m->users;
break;
case 1:
val = map_getusers();
break;
}
push_int<ScriptDataInt>(st->stack, val);
}
/*==========================================
* マップ指定ユーザー数所得
*------------------------------------------
*/
static
void builtin_getareausers_sub(dumb_ptr<block_list> bl, int *users)
{
if (bool(bl->is_player()->status.option & Opt0::HIDE))
return;
(*users)++;
}
static
void builtin_getmapusers(ScriptState *st)
{
int users = 0;
MapName str = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
P<map_local> m = TRY_UNWRAP(map_mapname2mapid(str),
{
push_int<ScriptDataInt>(st->stack, -1);
return;
});
map_foreachinarea(std::bind(builtin_getareausers_sub, ph::_1, &users),
m,
0, 0,
m->xs, m->ys,
BL::PC);
push_int<ScriptDataInt>(st->stack, users);
}
static
void builtin_aggravate(ScriptState *st)
{
dumb_ptr<mob_data> md = map_id_is_mob(wrap<BlockId>(conv_num(st, &AARG(0))));
if (md)
{
dumb_ptr<block_list> target = script_rid2sd(st);
if (HARG(1))
target = map_id2bl(wrap<BlockId>(conv_num(st, &AARG(1))));
mob_aggravate(md, target);
}
}
/*==========================================
* エリア指定ユーザー数所得
*------------------------------------------
*/
static
void builtin_getareausers_living_sub(dumb_ptr<block_list> bl, int *users)
{
if (bool(bl->is_player()->status.option & Opt0::HIDE))
return;
if (!pc_isdead(bl->is_player()))
(*users)++;
}
static
void builtin_getareausers(ScriptState *st)
{
int x0, y0, x1, y1, users = 0;
MapName str = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
x0 = conv_num(st, &AARG(1));
y0 = conv_num(st, &AARG(2));
x1 = conv_num(st, &AARG(3));
y1 = conv_num(st, &AARG(4));
int living = 0;
if (HARG(5))
{
living = conv_num(st, &AARG(5));
}
P<map_local> m = TRY_UNWRAP(map_mapname2mapid(str),
{
push_int<ScriptDataInt>(st->stack, -1);
return;
});
map_foreachinarea(std::bind(living ? builtin_getareausers_living_sub: builtin_getareausers_sub, ph::_1, &users),
m,
x0, y0,
x1, y1,
BL::PC);
push_int<ScriptDataInt>(st->stack, users);
}
/*==========================================
* エリア指定ドロップアイテム数所得
*------------------------------------------
*/
static
void builtin_getareadropitem_sub(dumb_ptr<block_list> bl, ItemNameId item, int *amount)
{
dumb_ptr<flooritem_data> drop = bl->is_item();
if (drop->item_data.nameid == item)
(*amount) += drop->item_data.amount;
}
static
void builtin_getareadropitem_sub_anddelete(dumb_ptr<block_list> bl, ItemNameId item, int *amount)
{
dumb_ptr<flooritem_data> drop = bl->is_item();
if (drop->item_data.nameid == item)
{
(*amount) += drop->item_data.amount;
clif_clearflooritem(drop, nullptr);
map_delobject(drop->bl_id, drop->bl_type);
}
}
static
void builtin_getareadropitem(ScriptState *st)
{
ItemNameId item;
int x0, y0, x1, y1, amount = 0, delitems = 0;
struct script_data *data;
MapName str = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
x0 = conv_num(st, &AARG(1));
y0 = conv_num(st, &AARG(2));
x1 = conv_num(st, &AARG(3));
y1 = conv_num(st, &AARG(4));
data = &AARG(5);
get_val(st, data);
if (data->is<ScriptDataStr>())
{
ZString name = ZString(conv_str(st, data));
Option<P<struct item_data>> item_data_ = itemdb_searchname(name);
OMATCH_BEGIN_SOME (item_data, item_data_)
{
item = item_data->nameid;
}
OMATCH_END ();
}
else
item = wrap<ItemNameId>(conv_num(st, data));
if (HARG(6))
delitems = conv_num(st, &AARG(6));
P<map_local> m = TRY_UNWRAP(map_mapname2mapid(str),
{
push_int<ScriptDataInt>(st->stack, -1);
return;
});
if (delitems)
map_foreachinarea(std::bind(builtin_getareadropitem_sub_anddelete, ph::_1, item, &amount),
m,
x0, y0,
x1, y1,
BL::ITEM);
else
map_foreachinarea(std::bind(builtin_getareadropitem_sub, ph::_1, item, &amount),
m,
x0, y0,
x1, y1,
BL::ITEM);
push_int<ScriptDataInt>(st->stack, amount);
}
/*==========================================
* NPCの有効化
*------------------------------------------
*/
static
void builtin_enablenpc(ScriptState *st)
{
NpcName str = stringish<NpcName>(ZString(conv_str(st, &AARG(0))));
npc_enable(str, 1);
}
/*==========================================
* NPCの無効化
*------------------------------------------
*/
static
void builtin_disablenpc(ScriptState *st)
{
NpcName str = stringish<NpcName>(ZString(conv_str(st, &AARG(0))));
npc_enable(str, 0);
}
/*==========================================
* 状態異常にかかる
*------------------------------------------
*/
static
void builtin_sc_start(ScriptState *st)
{
dumb_ptr<block_list> bl;
int val1;
StatusChange type = static_cast<StatusChange>(conv_num(st, &AARG(0)));
interval_t tick = static_cast<interval_t>(conv_num(st, &AARG(1)));
if (tick < 1_s)
// work around old behaviour of:
// speed potion
// atk potion
// matk potion
//
// which used to use seconds
// all others used milliseconds
tick *= 1000;
val1 = conv_num(st, &AARG(2));
if (HARG(3)) //指定したキャラを状態異常にする
bl = map_id2bl(wrap<BlockId>(conv_num(st, &AARG(3))));
else
bl = map_id2bl(st->rid);
skill_status_change_start(bl, type, val1, tick);
}
/*==========================================
* 状態異常が直る
*------------------------------------------
*/
static
void builtin_sc_end(ScriptState *st)
{
dumb_ptr<block_list> bl;
StatusChange type = StatusChange(conv_num(st, &AARG(0)));
if (HARG(1)) //指定したキャラを状態異常にする
bl = map_id2bl(wrap<BlockId>(conv_num(st, &AARG(1))));
else
bl = map_id2bl(st->rid);
skill_status_change_end(bl, type, nullptr);
}
static
void builtin_sc_check(ScriptState *st)
{
dumb_ptr<block_list> bl;
StatusChange type = StatusChange(conv_num(st, &AARG(0)));
if (HARG(1)) //指定したキャラを状態異常にする
bl = map_id2bl(wrap<BlockId>(conv_num(st, &AARG(1))));
else
bl = map_id2bl(st->rid);
push_int<ScriptDataInt>(st->stack, skill_status_change_active(bl, type));
}
/*==========================================
*
*------------------------------------------
*/
static
void builtin_debugmes(ScriptState *st)
{
RString mes = conv_str(st, &AARG(0));
PRINTF("script debug: %d %d: '%s'\n"_fmt,
st->rid, st->oid, mes);
}
/*==========================================
* ステータスリセット
*------------------------------------------
*/
static
void builtin_resetstatus(ScriptState *st)
{
dumb_ptr<map_session_data> sd;
sd = script_rid2sd(st);
pc_resetstate(sd);
}
/*==========================================
* RIDのアタッチ
*------------------------------------------
*/
static
void builtin_attachrid(ScriptState *st)
{
st->rid = wrap<BlockId>(conv_num(st, &AARG(0)));
push_int<ScriptDataInt>(st->stack, (map_id2sd(st->rid) != nullptr));
}
/*==========================================
* RIDのデタッチ
*------------------------------------------
*/
static
void builtin_detachrid(ScriptState *st)
{
st->rid = BlockId();
}
/*==========================================
* 存在チェック
*------------------------------------------
*/
static
void builtin_isloggedin(ScriptState *st)
{
push_int<ScriptDataInt>(st->stack,
map_id2sd(wrap<BlockId>(conv_num(st, &AARG(0)))) != nullptr);
}
static
void builtin_setmapflag(ScriptState *st)
{
MapName str = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
int i = conv_num(st, &AARG(1));
MapFlag mf = map_flag_from_int(i);
Option<P<map_local>> m_ = map_mapname2mapid(str);
OMATCH_BEGIN_SOME (m, m_)
{
m->flag.set(mf, 1);
}
OMATCH_END ();
}
static
void builtin_removemapflag(ScriptState *st)
{
MapName str = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
int i = conv_num(st, &AARG(1));
MapFlag mf = map_flag_from_int(i);
Option<P<map_local>> m_ = map_mapname2mapid(str);
OMATCH_BEGIN_SOME (m, m_)
{
m->flag.set(mf, 0);
}
OMATCH_END ();
}
static
void builtin_getmapflag(ScriptState *st)
{
int r = -1;
MapName str = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
int i = conv_num(st, &AARG(1));
MapFlag mf = map_flag_from_int(i);
Option<P<map_local>> m_ = map_mapname2mapid(str);
OMATCH_BEGIN_SOME (m, m_)
{
r = m->flag.get(mf);
}
OMATCH_END ();
push_int<ScriptDataInt>(st->stack, r);
}
static
void builtin_pvpon(ScriptState *st)
{
MapName str = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
P<map_local> m = TRY_UNWRAP(map_mapname2mapid(str), return);
if (!m->flag.get(MapFlag::PVP) && !m->flag.get(MapFlag::NOPVP))
{
m->flag.set(MapFlag::PVP, 1);
if (battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
return;
for (io::FD i : iter_fds())
{
Session *s = get_session(i);
if (!s)
continue;
dumb_ptr<map_session_data> pl_sd = dumb_ptr<map_session_data>(static_cast<map_session_data *>(s->session_data.get()));
if (pl_sd && pl_sd->state.auth)
{
if (m == pl_sd->bl_m && !pl_sd->pvp_timer)
{
pl_sd->pvp_timer = Timer(gettick() + 200_ms,
std::bind(pc_calc_pvprank_timer, ph::_1, ph::_2,
pl_sd->bl_id));
//pl_sd->pvp_rank = 0;
//pl_sd->pvp_point = 5;
clif_map_pvp(pl_sd);
}
}
}
}
}
static
void builtin_pvpoff(ScriptState *st)
{
MapName str = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
P<map_local> m = TRY_UNWRAP(map_mapname2mapid(str), return);
if (m->flag.get(MapFlag::PVP))
{
m->flag.set(MapFlag::PVP, 0);
if (battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
return;
for (io::FD i : iter_fds())
{
Session *s = get_session(i);
if (!s)
continue;
dumb_ptr<map_session_data> pl_sd = dumb_ptr<map_session_data>(static_cast<map_session_data *>(s->session_data.get()));
if (pl_sd && pl_sd->state.auth)
{
if (m == pl_sd->bl_m)
{
pl_sd->pvp_timer.cancel();
clif_map_pvp(pl_sd);
}
}
}
}
}
static
void builtin_setpvpchannel(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
int flag;
flag = conv_num(st, &AARG(0));
if (flag < 1)
flag = 0;
sd->state.pvpchannel = flag;
}
static
void builtin_getpvpflag(ScriptState *st)
{
dumb_ptr<map_session_data> sd;
if (HARG(1)) //指定したキャラを状態異常にする
sd = map_id_is_player(wrap<BlockId>(conv_num(st, &AARG(1))));
else
sd = script_rid2sd(st);
if (!sd)
{
PRINTF("builtin_getpvpflag: player not found\n"_fmt);
return;
}
int num = conv_num(st, &AARG(0));
int flag = 0;
switch (num){
case 0:
flag = sd->state.pvpchannel;
break;
case 1:
flag = bool(sd->status.option & Opt0::HIDE);
break;
}
push_int<ScriptDataInt>(st->stack, flag);
}
/*==========================================
* NPCエモーション
*------------------------------------------
*/
static
void builtin_emotion(ScriptState *st)
{
ZString str;
dumb_ptr<map_session_data> pl_sd = nullptr;
int type = conv_num(st, &AARG(0));
if (HARG(1)) {
str = ZString(conv_str(st, &AARG(1)));
CharName player = stringish<CharName>(str);
pl_sd = map_nick2sd(player);
}
if (type < 0 || type > 200)
return;
if (pl_sd != nullptr)
clif_emotion_towards(map_id2bl(st->oid), pl_sd, type);
else if (st->rid && str == "self"_s)
clif_emotion(map_id2sd(st->rid), type);
else
clif_emotion(map_id2bl(st->oid), type);
}
static
void builtin_mapwarp(ScriptState *st) // Added by RoVeRT
{
int x, y;
int x0, y0, x1, y1;
MapName mapname = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
x0 = 0;
y0 = 0;
P<map_local> m = TRY_UNWRAP(map_mapname2mapid(mapname), return);
x1 = m->xs;
y1 = m->ys;
MapName str = stringish<MapName>(ZString(conv_str(st, &AARG(1))));
x = conv_num(st, &AARG(2));
y = conv_num(st, &AARG(3));
map_foreachinarea(std::bind(builtin_areawarp_sub, ph::_1, str, x, y),
m,
x0, y0,
x1, y1,
BL::PC);
}
static
void builtin_mobcount_sub(dumb_ptr<block_list> bl, NpcEvent event, int *c)
{
if (event == bl->is_mob()->npc_event)
(*c)++;
}
static
void builtin_mobcount(ScriptState *st) // Added by RoVeRT
{
int c = 0;
MapName mapname = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
ZString event_ = ZString(conv_str(st, &AARG(1)));
NpcEvent event;
extract(event_, &event);
P<map_local> m = TRY_UNWRAP(map_mapname2mapid(mapname),
{
push_int<ScriptDataInt>(st->stack, -1);
return;
});
map_foreachinarea(std::bind(builtin_mobcount_sub, ph::_1, event, &c),
m,
0, 0,
m->xs, m->ys,
BL::MOB);
push_int<ScriptDataInt>(st->stack, (c - 1));
}
static
void builtin_marriage(ScriptState *st)
{
CharName partner = stringish<CharName>(ZString(conv_str(st, &AARG(0))));
dumb_ptr<map_session_data> sd = script_rid2sd(st);
dumb_ptr<map_session_data> p_sd = map_nick2sd(partner);
if (sd == nullptr || p_sd == nullptr || pc_marriage(sd, p_sd) < 0)
{
push_int<ScriptDataInt>(st->stack, 0);
return;
}
push_int<ScriptDataInt>(st->stack, 1);
}
static
void builtin_divorce(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
if (sd == nullptr || pc_divorce(sd) < 0)
{
push_int<ScriptDataInt>(st->stack, 0);
return;
}
push_int<ScriptDataInt>(st->stack, 1);
}
static
void builtin_getitemlink(ScriptState *st)
{
struct script_data *data;
AString buf;
data = &AARG(0);
ZString name = conv_str(st, data);
ItemNameId item_id;
Option<P<struct item_data>> item_data_ = itemdb_searchname(name);
OMATCH_BEGIN (item_data_)
{
OMATCH_CASE_SOME (item_data)
{
buf = STRPRINTF("@@%d|@@"_fmt, item_data->nameid);
}
OMATCH_CASE_NONE ()
{
buf = "Unknown Item"_s;
}
}
OMATCH_END ();
push_str<ScriptDataStr>(st->stack, buf);
}
static
void builtin_getpartnerid2(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
push_int<ScriptDataInt>(st->stack, unwrap<CharId>(sd->status.partner_id));
}
static
void builtin_chr(ScriptState *st)
{
const char ascii = conv_num(st, &AARG(0));
push_str<ScriptDataStr>(st->stack, VString<1>(ascii));
}
static
void builtin_ord(ScriptState *st)
{
const char ascii = conv_str(st, &AARG(0)).front();
push_int<ScriptDataInt>(st->stack, static_cast<int>(ascii));
}
static
void builtin_explode(ScriptState *st)
{
dumb_ptr<block_list> bl = nullptr;
SIR reg = AARG(0).get_if<ScriptDataVariable>()->reg;
ZString name = variable_names.outtern(reg.base());
const char separator = conv_str(st, &AARG(2)).front();
RString str = conv_str(st, &AARG(1));
RString val;
char prefix = name.front();
char postfix = name.back();
if (prefix != '$' && prefix != '@' && prefix != '.')
{
PRINTF("builtin_explode: illegal scope!\n"_fmt);
return;
}
if (prefix == '.' && !name.startswith(".@"_s))
bl = map_id2bl(st->oid)->is_npc();
else if (prefix != '$' && prefix != '.')
{
bl = map_id2bl(st->rid)->is_player();
assert (bl);
}
for (int j = 0; j < 256; j++)
{
auto find = std::find(str.begin(), str.end(), separator);
if (find == str.end())
{
if (name.startswith(".@"_s))
{
struct script_data vd = script_data(ScriptDataInt{atoi(str.c_str())});
if (postfix == '$')
vd = script_data(ScriptDataStr{str});
set_scope_reg(st, reg.iplus(j), &vd);
}
else if (postfix == '$')
set_reg(bl, VariableCode::VARIABLE, reg.iplus(j), str);
else
set_reg(bl, VariableCode::VARIABLE, reg.iplus(j), atoi(str.c_str()));
break;
}
{
val = str.xislice_h(find);
str = str.xislice_t(find + 1);
if (name.startswith(".@"_s))
{
struct script_data vd = script_data(ScriptDataInt{atoi(val.c_str())});
if (postfix == '$')
vd = script_data(ScriptDataStr{val});
set_scope_reg(st, reg.iplus(j), &vd);
}
else if (postfix == '$')
set_reg(bl, VariableCode::VARIABLE, reg.iplus(j), val);
else
set_reg(bl, VariableCode::VARIABLE, reg.iplus(j), atoi(val.c_str()));
}
}
}
/*==========================================
* PCの所持品情報読み取り
*------------------------------------------
*/
static
void builtin_getinventorylist(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
int j = 0;
if (!sd)
return;
for (IOff0 i : IOff0::iter())
{
if (sd->status.inventory[i].nameid
&& sd->status.inventory[i].amount > 0)
{
pc_setreg(sd, SIR::from(variable_names.intern("@inventorylist_id"_s), j),
unwrap<ItemNameId>(sd->status.inventory[i].nameid));
pc_setreg(sd, SIR::from(variable_names.intern("@inventorylist_amount"_s), j),
sd->status.inventory[i].amount);
pc_setreg(sd, SIR::from(variable_names.intern("@inventorylist_equip"_s), j),
static_cast<uint16_t>(sd->status.inventory[i].equip));
j++;
}
}
pc_setreg(sd, SIR::from(variable_names.intern("@inventorylist_count"_s)), j);
}
static
void builtin_getactivatedpoolskilllist(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
SkillID pool_skills[MAX_SKILL_POOL];
int skill_pool_size = skill_pool(sd, pool_skills);
int i, count = 0;
if (!sd)
return;
for (i = 0; i < skill_pool_size; i++)
{
SkillID skill_id = pool_skills[i];
if (sd->status.skill[skill_id].lv)
{
pc_setreg(sd, SIR::from(variable_names.intern("@skilllist_id"_s), count),
static_cast<uint16_t>(skill_id));
pc_setreg(sd, SIR::from(variable_names.intern("@skilllist_lv"_s), count),
sd->status.skill[skill_id].lv);
pc_setreg(sd, SIR::from(variable_names.intern("@skilllist_flag"_s), count),
static_cast<uint16_t>(sd->status.skill[skill_id].flags));
pc_setregstr(sd, SIR::from(variable_names.intern("@skilllist_name$"_s), count),
skill_name(skill_id));
++count;
}
}
pc_setreg(sd, SIR::from(variable_names.intern("@skilllist_count"_s)), count);
}
static
void builtin_getunactivatedpoolskilllist(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
int i, count = 0;
if (!sd)
return;
for (i = 0; i < skill_pool_skills.size(); i++)
{
SkillID skill_id = skill_pool_skills[i];
if (sd->status.skill[skill_id].lv
&& !bool(sd->status.skill[skill_id].flags & SkillFlags::POOL_ACTIVATED))
{
pc_setreg(sd, SIR::from(variable_names.intern("@skilllist_id"_s), count),
static_cast<uint16_t>(skill_id));
pc_setreg(sd, SIR::from(variable_names.intern("@skilllist_lv"_s), count),
sd->status.skill[skill_id].lv);
pc_setreg(sd, SIR::from(variable_names.intern("@skilllist_flag"_s), count),
static_cast<uint16_t>(sd->status.skill[skill_id].flags));
pc_setregstr(sd, SIR::from(variable_names.intern("@skilllist_name$"_s), count),
skill_name(skill_id));
++count;
}
}
pc_setreg(sd, SIR::from(variable_names.intern("@skilllist_count"_s)), count);
}
static
void builtin_poolskill(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
SkillID skill_id = SkillID(conv_num(st, &AARG(0)));
skill_pool_activate(sd, skill_id);
clif_skillinfoblock(sd);
}
static
void builtin_unpoolskill(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
SkillID skill_id = SkillID(conv_num(st, &AARG(0)));
skill_pool_deactivate(sd, skill_id);
clif_skillinfoblock(sd);
}
/*==========================================
* NPCから発生するエフェクト
* misceffect(effect, [target])
*
* effect The effect type/ID.
* target The player name or being ID on
* which to display the effect. If not
* specified, it attempts to default to
* the current NPC or invoking PC.
*------------------------------------------
*/
static
void builtin_misceffect(ScriptState *st)
{
int type;
BlockId id;
CharName name;
dumb_ptr<block_list> bl = nullptr;
type = conv_num(st, &AARG(0));
if (HARG(1))
{
struct script_data *sdata = &AARG(1);
get_val(st, sdata);
if (sdata->is<ScriptDataStr>())
name = stringish<CharName>(ZString(conv_str(st, sdata)));
else
id = wrap<BlockId>(conv_num(st, sdata));
}
if (name.to__actual())
{
dumb_ptr<map_session_data> sd = map_nick2sd(name);
if (sd)
bl = sd;
}
else if (id)
bl = map_id2bl(id);
else if (st->oid)
bl = map_id2bl(st->oid);
else
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
if (sd)
bl = sd;
}
if (bl)
clif_misceffect(bl, type);
}
/*==========================================
* Special effects [Valaris]
*------------------------------------------
*/
static
void builtin_specialeffect(ScriptState *st)
{
dumb_ptr<block_list> bl = map_id2bl(st->oid);
if (bl == nullptr)
return;
clif_specialeffect(bl,
conv_num(st,
&AARG(0)),
0);
}
static
void builtin_specialeffect2(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
if (sd == nullptr)
return;
clif_specialeffect(sd,
conv_num(st,
&AARG(0)),
0);
}
static
void builtin_get(ScriptState *st)
{
BlockId id;
dumb_ptr<block_list> bl = nullptr;
if (auto *u = AARG(0).get_if<ScriptDataParam>())
{
SIR reg = u->reg;
struct script_data *sdata = &AARG(1);
get_val(st, sdata);
CharName name;
if (sdata->is<ScriptDataStr>())
{
name = stringish<CharName>(ZString(conv_str(st, sdata)));
if (name.to__actual())
bl = map_nick2sd(name);
}
else
{
int num = conv_num(st, sdata);
if (num >= 2000000)
id = wrap<BlockId>(num);
else if (num >= 150000)
{
dumb_ptr<map_session_data> p_sd = nullptr;
if ((p_sd = map_nick2sd(map_charid2nick(wrap<CharId>(num)))) != nullptr)
id = p_sd->bl_id;
else
{
push_int<ScriptDataInt>(st->stack, -1);
return;
}
}
else
{
push_int<ScriptDataInt>(st->stack, -1);
return;
}
bl = map_id2bl(id);
}
if (bl == nullptr)
{
push_int<ScriptDataInt>(st->stack, -1);
return;
}
int var = pc_readparam(bl, reg.sp());
push_int<ScriptDataInt>(st->stack, var);
return;
}
struct script_data *sdata = &AARG(1);
get_val(st, sdata);
SIR reg = AARG(0).get_if<ScriptDataVariable>()->reg;
ZString name_ = variable_names.outtern(reg.base());
char prefix = name_.front();
char postfix = name_.back();
if(prefix == '.')
{
if (name_.startswith(".@"_s))
{
PRINTF("builtin_get: illegal scope!\n"_fmt);
push_int<ScriptDataInt>(st->stack, 0);
return;
}
NpcName name;
if (sdata->is<ScriptDataStr>())
{
name = stringish<NpcName>(ZString(conv_str(st, sdata)));
bl = npc_name2id(name);
}
else
{
id = wrap<BlockId>(conv_num(st, sdata));
bl = map_id2bl(id);
}
}
else if(prefix != '$')
{
CharName name;
if (sdata->is<ScriptDataStr>())
{
name = stringish<CharName>(ZString(conv_str(st, sdata)));
if (name.to__actual())
bl = map_nick2sd(name);
}
else
{
id = wrap<BlockId>(conv_num(st, sdata));
bl = map_id2bl(id);
}
}
else
{
PRINTF("builtin_get: illegal scope !\n"_fmt);
push_int<ScriptDataInt>(st->stack, 0);
return;
}
if (!bl)
{
if (postfix == '$')
push_str<ScriptDataStr>(st->stack, ""_s);
else
push_int<ScriptDataInt>(st->stack, 0);
return;
}
if (postfix == '$')
{
ZString var = pc_readregstr(bl, reg);
push_str<ScriptDataStr>(st->stack, var);
}
else
{
int var = 0;
if (prefix == '#' && bl)
{
if (name_.startswith("##"_s))
var = pc_readaccountreg2(bl->is_player(), stringish<VarName>(name_));
else
var = pc_readaccountreg(bl->is_player(), stringish<VarName>(name_));
}
else if (bl)
var = pc_readreg(bl, reg);
push_int<ScriptDataInt>(st->stack, var);
}
}
/*==========================================
* Nude [Valaris]
*------------------------------------------
*/
static
void builtin_nude(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
if (sd == nullptr)
return;
for (EQUIP i : EQUIPs)
{
IOff0 idx = sd->equip_index_maybe[i];
if (idx.ok())
pc_unequipitem(sd, idx, CalcStatus::LATER);
}
pc_calcstatus(sd, 0);
}
/*==========================================
* UnequipById [Freeyorp]
*------------------------------------------
*/
static
void builtin_unequipbyid(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
if (sd == nullptr)
return;
EQUIP slot_id = EQUIP(conv_num(st, &AARG(0)));
if (slot_id >= EQUIP() && slot_id < EQUIP::COUNT)
{
IOff0 idx = sd->equip_index_maybe[slot_id];
if (idx.ok())
pc_unequipitem(sd, idx, CalcStatus::LATER);
}
pc_calcstatus(sd, 0);
}
/*==========================================
* npcwarp [remoitnane]
* Move NPC to a new position on the same map.
*------------------------------------------
*/
static
void builtin_npcwarp(ScriptState *st)
{
int x, y;
dumb_ptr<npc_data> nd = nullptr;
x = conv_num(st, &AARG(0));
y = conv_num(st, &AARG(1));
NpcName npc = stringish<NpcName>(ZString(conv_str(st, &AARG(2))));
nd = npc_name2id(npc);
if (!nd)
{
PRINTF("builtin_npcwarp: no such npc: '%s'\n"_fmt, npc);
return;
}
P<map_local> m = nd->bl_m;
/* Crude sanity checks. */
if (!nd->bl_prev
|| x < 0 || x > m->xs -1
|| y < 0 || y > m->ys - 1)
return;
npc_enable(npc, 0);
map_delblock(nd); /* [Freeyorp] */
nd->bl_x = x;
nd->bl_y = y;
map_addblock(nd);
npc_enable(npc, 1);
}
/*==========================================
* npcareawarp [remoitnane] [wushin]
* Move NPC to a new area on the same map.
*------------------------------------------
*/
static
void builtin_npcareawarp(ScriptState *st)
{
int x0, y0, x1, y1, x, y, max, cb, lx = -1, ly = -1, j = 0;
dumb_ptr<npc_data> nd = nullptr;
NpcName npc = stringish<NpcName>(ZString(conv_str(st, &AARG(5))));
nd = npc_name2id(npc);
x0 = conv_num(st, &AARG(0));
y0 = conv_num(st, &AARG(1));
x1 = conv_num(st, &AARG(2));
y1 = conv_num(st, &AARG(3));
cb = conv_num(st, &AARG(4));
if (!nd)
{
PRINTF("builtin_npcareawarp: no such npc: '%s'\n"_fmt, npc);
return;
}
max = (y1 - y0 + 1) * (x1 - x0 + 1) * 3;
if (max > 1000)
max = 1000;
P<map_local> m = nd->bl_m;
if (cb) {
do
{
x = random_::in(x0, x1);
y = random_::in(y0, y1);
}
while (bool(map_getcell(m, x, y) & MapCell::UNWALKABLE)
&& (++j) < max);
if (j >= max)
{
if (lx >= 0)
{ // Since reference went wrong, the place which boiled before is used.
x = lx;
y = ly;
}
else
return; // Since reference of the place which boils first went wrong, it stops.
}
}
else
x = random_::in(x0, x1);
y = random_::in(y0, y1);
npc_enable(npc, 0);
map_delblock(nd); /* [Freeyorp] */
nd->bl_x = x;
nd->bl_y = y;
map_addblock(nd);
npc_enable(npc, 1);
}
/*==========================================
* message [MouseJstr]
*------------------------------------------
*/
static
void builtin_message(ScriptState *st)
{
CharName player = stringish<CharName>(ZString(conv_str(st, &AARG(0))));
ZString msg = ZString(conv_str(st, &AARG(1)));
dumb_ptr<map_session_data> pl_sd = map_nick2sd(player);
if (pl_sd == nullptr)
return;
clif_displaymessage(pl_sd->sess, msg);
}
static
void builtin_title(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
ZString msg = ZString(conv_str(st, &AARG(0)));
if (sd == nullptr)
return;
clif_npc_send_title(sd->sess, st->oid, msg);
}
static
void builtin_smsg(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
if (HARG(2))
{
CharName player = stringish<CharName>(ZString(conv_str(st, &AARG(2))));
sd = map_nick2sd(player);
}
int type = HARG(1) ? conv_num(st, &AARG(0)) : 0;
ZString msg = ZString(conv_str(st, (HARG(1) ? &AARG(1) : &AARG(0))));
if (sd == nullptr)
return;
if (type < 0 || type > 0xFF)
type = 0;
clif_server_message(sd, type, msg);
}
static
void builtin_remotecmd(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
if (HARG(1))
{
CharName player = stringish<CharName>(ZString(conv_str(st, &AARG(1))));
sd = map_nick2sd(player);
}
ZString msg = ZString(conv_str(st, &AARG(0)));
if (sd == nullptr)
return;
clif_remote_command(sd, msg);
}
static
void builtin_music(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
ZString msg = ZString(conv_str(st, &AARG(0)));
if (sd == nullptr)
return;
clif_change_music(sd, msg);
}
static
void builtin_mapmask(ScriptState *st)
{
dumb_ptr<npc_data> nd;
dumb_ptr<map_session_data> sd;
int map_mask = conv_num(st, &AARG(0));
if(st->oid)
nd = map_id_is_npc(st->oid);
if(st->rid)
sd = script_rid2sd(st);
if(HARG(1) && sd != nullptr)
sd->bl_m->mask = map_mask;
else if(HARG(1) && nd)
nd->bl_m->mask = map_mask;
if (sd == nullptr)
return;
clif_send_mask(sd, map_mask);
}
static
void builtin_getmask(ScriptState *st)
{
dumb_ptr<npc_data> nd;
dumb_ptr<map_session_data> sd;
int map_mask;
if(st->oid)
nd = map_id_is_npc(st->oid);
if(st->rid)
sd = script_rid2sd(st);
if(sd != nullptr)
map_mask = sd->bl_m->mask;
else if(nd)
map_mask = nd->bl_m->mask;
else
map_mask = -1;
push_int<ScriptDataInt>(st->stack, map_mask);
}
/*==========================================
* npctalk (sends message to surrounding
* area) [Valaris]
*------------------------------------------
*/
static
void builtin_npctalk(ScriptState *st)
{
dumb_ptr<npc_data> nd;
RString str = conv_str(st, &AARG(1));
dumb_ptr<npc_data> nd_ = npc_name2id(stringish<NpcName>(ZString(conv_str(st, &AARG(0)))));
assert (nd_ && nd_->npc_subtype == NpcSubtype::SCRIPT);
nd = nd_->is_script();
if(HARG(2)){
CharName player = stringish<CharName>(ZString(conv_str(st, &AARG(2))));
dumb_ptr<map_session_data> pl_sd = map_nick2sd(player);
if (pl_sd == nullptr)
return;
clif_message_towards(pl_sd, nd, str);
}
else
clif_message(nd, str);
}
/*==========================================
* register cmd
*------------------------------------------
*/
static
void builtin_registercmd(ScriptState *st)
{
RString evoke = conv_str(st, &AARG(0));
ZString event_ = conv_str(st, &AARG(1));
NpcEvent event;
extract(event_, &event);
spells_by_events.put(evoke, event);
}
/*==========================================
* getlook char info. getlook(arg)
*------------------------------------------
*/
static
void builtin_getlook(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
LOOK type = LOOK(conv_num(st, &AARG(0)));
int val = -1;
switch (type)
{
case LOOK::HAIR: //1
val = sd->status.hair;
break;
case LOOK::WEAPON: //2
val = static_cast<uint16_t>(sd->status.weapon);
break;
case LOOK::HEAD_BOTTOM: //3
val = unwrap<ItemNameId>(sd->status.head_bottom);
break;
case LOOK::HEAD_TOP: //4
val = unwrap<ItemNameId>(sd->status.head_top);
break;
case LOOK::HEAD_MID: //5
val = unwrap<ItemNameId>(sd->status.head_mid);
break;
case LOOK::HAIR_COLOR: //6
val = sd->status.hair_color;
break;
case LOOK::CLOTHES_COLOR: //7
val = sd->status.clothes_color;
break;
case LOOK::SHIELD: //8
val = unwrap<ItemNameId>(sd->status.shield);
break;
case LOOK::SHOES: //9
break;
}
push_int<ScriptDataInt>(st->stack, val);
}
/*==========================================
* get char save point. argument: 0- map name, 1- x, 2- y
*------------------------------------------
*/
static
void builtin_getsavepoint(ScriptState *st)
{
int x, y, type;
dumb_ptr<map_session_data> sd;
sd = script_rid2sd(st);
type = conv_num(st, &AARG(0));
x = sd->status.save_point.x;
y = sd->status.save_point.y;
switch (type)
{
case 0:
{
RString mapname = sd->status.save_point.map_;
push_str<ScriptDataStr>(st->stack, mapname);
}
break;
case 1:
push_int<ScriptDataInt>(st->stack, x);
break;
case 2:
push_int<ScriptDataInt>(st->stack, y);
break;
}
}
/*==========================================
* areatimer
*------------------------------------------
*/
static
void builtin_areatimer_sub(dumb_ptr<block_list> bl, interval_t tick, NpcEvent event)
{
if (!bl)
return;
if (bl->bl_type == BL::PC)
{
dumb_ptr<map_session_data> sd = map_id_is_player(bl->bl_id);
pc_addeventtimer(sd, tick, event);
}
else
{
npc_addeventtimer(bl, tick, event);
}
}
static
void builtin_areatimer(ScriptState *st)
{
int x0, y0, x1, y1, bl_num;
bl_num = conv_num(st, &AARG(0));
MapName mapname = stringish<MapName>(ZString(conv_str(st, &AARG(1))));
x0 = conv_num(st, &AARG(2));
y0 = conv_num(st, &AARG(3));
x1 = conv_num(st, &AARG(4));
y1 = conv_num(st, &AARG(5));
interval_t tick = static_cast<interval_t>(conv_num(st, &AARG(6)));
ZString event_ = conv_str(st, &AARG(7));
BL block_type;
NpcEvent event;
extract(event_, &event);
P<map_local> m = TRY_UNWRAP(map_mapname2mapid(mapname), return);
switch (bl_num)
{
case 0:
block_type = BL::PC;
break;
case 1:
block_type = BL::NPC;
break;
case 2:
block_type = BL::MOB;
break;
default:
return;
}
map_foreachinarea(std::bind(builtin_areatimer_sub, ph::_1, tick, event),
m,
x0, y0,
x1, y1,
block_type);
}
/*==========================================
* Check whether the PC is in the specified rectangle
*------------------------------------------
*/
static
void builtin_isin(ScriptState *st)
{
int x1, y1, x2, y2;
dumb_ptr<map_session_data> sd = script_rid2sd(st);
MapName str = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
x1 = conv_num(st, &AARG(1));
y1 = conv_num(st, &AARG(2));
x2 = conv_num(st, &AARG(3));
y2 = conv_num(st, &AARG(4));
if (!sd)
return;
push_int<ScriptDataInt>(st->stack,
(sd->bl_x >= x1 && sd->bl_x <= x2)
&& (sd->bl_y >= y1 && sd->bl_y <= y2)
&& (str == sd->bl_m->name_));
}
/*==========================================
* Check whether the coords are collision
*------------------------------------------
*/
static
void builtin_iscollision(ScriptState *st)
{
int x, y;
MapName mapname = stringish<MapName>(ZString(conv_str(st, &AARG(0))));
P<map_local> m = TRY_UNWRAP(map_mapname2mapid(mapname), return);
x = conv_num(st, &AARG(1));
y = conv_num(st, &AARG(2));
push_int<ScriptDataInt>(st->stack,
bool(map_getcell(m, x, y) & MapCell::UNWALKABLE));
}
// Trigger the shop on a (hopefully) nearby shop NPC
static
void builtin_shop(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
dumb_ptr<npc_data> nd;
if (!sd)
return;
NpcName name = stringish<NpcName>(ZString(conv_str(st, &AARG(0))));
nd = npc_name2id(name);
if (!nd)
{
PRINTF("builtin_shop: no such npc: '%s'\n"_fmt, name);
return;
}
builtin_close(st);
clif_npcbuysell(sd, nd->bl_id);
}
/*==========================================
* Check whether the PC is dead
*------------------------------------------
*/
static
void builtin_isdead(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
push_int<ScriptDataInt>(st->stack, pc_isdead(sd));
}
/*========================================
* Changes a NPC name, and sprite
*----------------------------------------
*/
static
void builtin_fakenpcname(ScriptState *st)
{
NpcName name = stringish<NpcName>(ZString(conv_str(st, &AARG(0))));
NpcName newname = stringish<NpcName>(ZString(conv_str(st, &AARG(1))));
Species newsprite = wrap<Species>(static_cast<uint16_t>(conv_num(st, &AARG(2))));
dumb_ptr<npc_data> nd = npc_name2id(name);
if (!nd)
{
PRINTF("builtin_fakenpcname: no such npc: '%s'\n"_fmt, name);
return;
}
nd->name = newname;
nd->npc_class = newsprite;
// Refresh this npc
npc_enable(name, 0);
npc_enable(name, 1);
}
/*============================
* Gets the PC's x pos
*----------------------------
*/
static
void builtin_getx(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
push_int<ScriptDataInt>(st->stack, sd->bl_x);
}
/*============================
* Gets the PC's y pos
*----------------------------
*/
static
void builtin_gety(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
push_int<ScriptDataInt>(st->stack, sd->bl_y);
}
/*============================
* Gets the PC's direction
*----------------------------
*/
static
void builtin_getdir(ScriptState *st)
{
dumb_ptr<map_session_data> sd = script_rid2sd(st);
push_int<ScriptDataInt>(st->stack, static_cast<uint8_t>(sd->dir));
}
/*
* Get the PC's current map's name
*/
static
void builtin_getmap(ScriptState *st)
{
dumb_ptr<map_session_data> sd;
if (HARG(0)) //指定したキャラを状態異常にする
sd = map_id_is_player(wrap<BlockId>(conv_num(st, &AARG(0))));
else
sd = script_rid2sd(st);
push_str<ScriptDataStr>(st->stack, sd->bl_m->name_);
}
/*
* Get the NPC's info
*/
static
void builtin_strnpcinfo(ScriptState *st)
{
int num = conv_num(st, &AARG(0));
RString name;
dumb_ptr<npc_data> nd;
if(HARG(1)){
struct script_data *sdata = &AARG(1);
get_val(st, sdata);
if (sdata->is<ScriptDataStr>())
{
NpcName name_ = stringish<NpcName>(ZString(conv_str(st, sdata)));
nd = npc_name2id(name_);
}
else
{
BlockId id = wrap<BlockId>(conv_num(st, sdata));
nd = map_id_is_npc(id);
}
if (!nd)
{
PRINTF("builtin_strnpcinfo: npc not found\n"_fmt);
return;
}
} else {
nd = map_id_is_npc(st->oid);
}
assert (nd);
switch(num)
{
case 0:
name = nd->name;
break;
case 1:
name = nd->name.xislice_h(std::find(nd->name.begin(), nd->name.end(), '#'));
break;
case 2:
name = nd->name.xislice_t(std::find(nd->name.begin(), nd->name.end(), '#'));
break;
case 3:
name = nd->bl_m->name_;
break;
}
push_str<ScriptDataStr>(st->stack, name);
}
/*============================
* Gets the NPC's x pos
*----------------------------
*/
static
void builtin_getnpcx(ScriptState *st)
{
dumb_ptr<npc_data> nd;
if(HARG(0)){
NpcName name = stringish<NpcName>(ZString(conv_str(st, &AARG(0))));
nd = npc_name2id(name);
if (!nd)
{
PRINTF("builtin_getnpcx: no such npc: '%s'\n"_fmt, name);
return;
}
} else {
nd = map_id_is_npc(st->oid);
}
assert (nd);
push_int<ScriptDataInt>(st->stack, nd->bl_x);
}
/*============================
* Gets the NPC's y pos
*----------------------------
*/
static
void builtin_getnpcy(ScriptState *st)
{
dumb_ptr<npc_data> nd;
if(HARG(0)){
NpcName name = stringish<NpcName>(ZString(conv_str(st, &AARG(0))));
nd = npc_name2id(name);
if (!nd)
{
PRINTF("builtin_getnpcy: no such npc: '%s'\n"_fmt, name);
return;
}
} else {
nd = map_id_is_npc(st->oid);
}
assert (nd);
push_int<ScriptDataInt>(st->stack, nd->bl_y);
}
static
void builtin_mapexit(ScriptState *)
{
runflag = 0;
}
#define BUILTIN(func, args, ret) \
{builtin_##func, #func ## _s, args, ret}
BuiltinFunction builtin_functions[] =
{
BUILTIN(mes, "?"_s, '\0'),
BUILTIN(clear, ""_s, '\0'),
BUILTIN(goto, "L"_s, '\0'),
BUILTIN(callfunc, "F"_s, '\0'),
BUILTIN(call, "F?*"_s, '.'),
BUILTIN(callsub, "L"_s, '\0'),
BUILTIN(getarg, "i?"_s, '.'),
BUILTIN(return, "?"_s, '\0'),
BUILTIN(void, "?*"_s, '\0'),
BUILTIN(next, ""_s, '\0'),
BUILTIN(close, ""_s, '\0'),
BUILTIN(close2, ""_s, '\0'),
BUILTIN(menu, "sL**"_s, '\0'),
BUILTIN(rand, "i?"_s, 'i'),
BUILTIN(isat, "Mxy"_s, 'i'),
BUILTIN(warp, "Mxy"_s, '\0'),
BUILTIN(areawarp, "MxyxyMxy"_s, '\0'),
BUILTIN(heal, "ii?"_s, '\0'),
BUILTIN(injure, "iii"_s, '\0'),
BUILTIN(input, "N"_s, '\0'),
BUILTIN(requestitem, "N?"_s, '\0'),
BUILTIN(requestlang, "N"_s, '\0'),
BUILTIN(if, "iF*"_s, '\0'),
BUILTIN(elif, "iF*"_s, '\0'),
BUILTIN(else, "F*"_s, '\0'),
BUILTIN(set, "Ne?"_s, '\0'),
BUILTIN(get, "Ne"_s, '.'),
BUILTIN(setarray, "Ne*"_s, '\0'),
BUILTIN(cleararray, "Nei"_s, '\0'),
BUILTIN(getarraysize, "N"_s, 'i'),
BUILTIN(getelementofarray, "Ni"_s, '.'),
BUILTIN(array_search, "eN"_s, 'i'),
BUILTIN(setlook, "ii"_s, '\0'),
BUILTIN(countitem, "I"_s, 'i'),
BUILTIN(checkweight, "Ii"_s, 'i'),
BUILTIN(getitem, "Ii??"_s, '\0'),
BUILTIN(makeitem, "IiMxy"_s, '\0'),
BUILTIN(delitem, "Ii"_s, '\0'),
BUILTIN(getcharid, "i?"_s, 'i'),
BUILTIN(getnpcid, "?"_s, 'i'),
BUILTIN(getversion, ""_s, 'i'),
BUILTIN(strcharinfo, "i?"_s, 's'),
BUILTIN(getequipid, "i?"_s, 'i'),
BUILTIN(bonus, "ii"_s, '\0'),
BUILTIN(bonus2, "iii"_s, '\0'),
BUILTIN(skill, "ii?"_s, '\0'),
BUILTIN(setskill, "ii"_s, '\0'),
BUILTIN(getskilllv, "i"_s, 'i'),
BUILTIN(overrideattack, "?????"_s, '\0'),
BUILTIN(getgmlevel, ""_s, 'i'),
BUILTIN(end, ""_s, '\0'),
BUILTIN(getopt2, ""_s, 'i'),
BUILTIN(setopt2, "i"_s, '\0'),
BUILTIN(savepoint, "Mxy"_s, '\0'),
BUILTIN(gettimetick, "i"_s, 'i'),
BUILTIN(gettime, "i"_s, 'i'),
BUILTIN(openstorage, ""_s, '\0'),
BUILTIN(getexp, "ii"_s, '\0'),
BUILTIN(summon, "Mxysmii?"_s, '\0'),
BUILTIN(monster, "Mxysmi?"_s, '\0'),
BUILTIN(areamonster, "Mxyxysmi?"_s, '\0'),
BUILTIN(killmonster, "ME"_s, '\0'),
BUILTIN(donpcevent, "E"_s, '\0'),
BUILTIN(addtimer, "tE?"_s, '\0'),
BUILTIN(addnpctimer, "tE"_s, '\0'),
BUILTIN(initnpctimer, "?"_s, '\0'),
BUILTIN(startnpctimer, "?"_s, '\0'),
BUILTIN(stopnpctimer, "?"_s, '\0'),
BUILTIN(getnpctimer, "i?"_s, 'i'),
BUILTIN(setnpctimer, "i?"_s, '\0'),
BUILTIN(setnpcdirection, "iii?"_s, '\0'),
BUILTIN(npcaction, "i???"_s, '\0'),
BUILTIN(camera, "???"_s, '\0'),
BUILTIN(announce, "si"_s, '\0'),
BUILTIN(mapannounce, "Msi"_s, '\0'),
BUILTIN(getusers, "i"_s, 'i'),
BUILTIN(getmapusers, "M"_s, 'i'),
BUILTIN(getareausers, "Mxyxy?"_s, 'i'),
BUILTIN(getareadropitem, "Mxyxyi?"_s, 'i'),
BUILTIN(enablenpc, "s"_s, '\0'),
BUILTIN(disablenpc, "s"_s, '\0'),
BUILTIN(sc_start, "iTi?"_s, '\0'),
BUILTIN(sc_end, "i?"_s, '\0'),
BUILTIN(sc_check, "i?"_s, 'i'),
BUILTIN(debugmes, "s"_s, '\0'),
BUILTIN(wgm, "s"_s, '\0'),
BUILTIN(gmlog, "s"_s, '\0'),
BUILTIN(resetstatus, ""_s, '\0'),
BUILTIN(attachrid, "i"_s, 'i'),
BUILTIN(detachrid, ""_s, '\0'),
BUILTIN(isloggedin, "i"_s, 'i'),
BUILTIN(setmapflag, "Mi"_s, '\0'),
BUILTIN(removemapflag, "Mi"_s, '\0'),
BUILTIN(getmapflag, "Mi"_s, 'i'),
BUILTIN(pvpon, "M"_s, '\0'),
BUILTIN(pvpoff, "M"_s, '\0'),
BUILTIN(setpvpchannel, "i"_s, '\0'),
BUILTIN(getpvpflag, "i?"_s, 'i'),
BUILTIN(emotion, "i?"_s, '\0'),
BUILTIN(mapwarp, "MMxy"_s, '\0'),
BUILTIN(mobcount, "ME"_s, 'i'),
BUILTIN(marriage, "P"_s, 'i'),
BUILTIN(divorce, ""_s, 'i'),
BUILTIN(getitemlink, "I"_s, 's'),
BUILTIN(getpartnerid2, ""_s, 'i'),
BUILTIN(explode, "Nss"_s, '\0'),
BUILTIN(getinventorylist, ""_s, '\0'),
BUILTIN(getactivatedpoolskilllist, ""_s, '\0'),
BUILTIN(getunactivatedpoolskilllist, ""_s, '\0'),
BUILTIN(poolskill, "i"_s, '\0'),
BUILTIN(unpoolskill, "i"_s, '\0'),
BUILTIN(misceffect, "i?"_s, '\0'),
BUILTIN(specialeffect, "i"_s, '\0'),
BUILTIN(specialeffect2, "i"_s, '\0'),
BUILTIN(nude, ""_s, '\0'),
BUILTIN(unequipbyid, "i"_s, '\0'),
BUILTIN(npcwarp, "xys"_s, '\0'),
BUILTIN(npcareawarp, "xyxyis"_s, '\0'),
BUILTIN(message, "Ps"_s, '\0'),
BUILTIN(npctalk, "ss?"_s, '\0'),
BUILTIN(registercmd, "ss"_s, '\0'),
BUILTIN(title, "s"_s, '\0'),
BUILTIN(smsg, "e??"_s, '\0'),
BUILTIN(remotecmd, "s?"_s, '\0'),
BUILTIN(music, "s"_s, '\0'),
BUILTIN(mapmask, "i?"_s, '\0'),
BUILTIN(getmask, ""_s, 'i'),
BUILTIN(getlook, "i"_s, 'i'),
BUILTIN(getsavepoint, "i"_s, '.'),
BUILTIN(areatimer, "MxyxytEi"_s, '\0'),
BUILTIN(foreach, "iMxyxyE?"_s, '\0'),
BUILTIN(isin, "Mxyxy"_s, 'i'),
BUILTIN(iscollision, "Mxy"_s, 'i'),
BUILTIN(shop, "s"_s, '\0'),
BUILTIN(isdead, ""_s, 'i'),
BUILTIN(aggravate, "i?"_s, '\0'),
BUILTIN(fakenpcname, "ssi"_s, '\0'),
BUILTIN(puppet, "mxysi??"_s, 'i'),
BUILTIN(destroy, "?"_s, '\0'),
BUILTIN(getx, ""_s, 'i'),
BUILTIN(gety, ""_s, 'i'),
BUILTIN(getdir, ""_s, 'i'),
BUILTIN(getnpcx, "?"_s, 'i'),
BUILTIN(getnpcy, "?"_s, 'i'),
BUILTIN(strnpcinfo, "i?"_s, 's'),
BUILTIN(getmap, "?"_s, 's'),
BUILTIN(mapexit, ""_s, '\0'),
BUILTIN(freeloop, "i"_s, '\0'),
BUILTIN(if_then_else, "iii"_s, '.'),
BUILTIN(max, "e?*"_s, 'i'),
BUILTIN(min, "e?*"_s, 'i'),
BUILTIN(average, "ii*"_s, 'i'),
BUILTIN(sqrt, "i"_s, 'i'),
BUILTIN(cbrt, "i"_s, 'i'),
BUILTIN(pow, "ii"_s, 'i'),
BUILTIN(target, "iii"_s, 'i'),
BUILTIN(distance, "ii?"_s, 'i'),
BUILTIN(chr, "i"_s, 'i'),
BUILTIN(ord, "s"_s, 'i'),
{nullptr, ""_s, ""_s, '\0'},
};
} // namespace map
} // namespace tmwa
|