summaryrefslogtreecommitdiff
path: root/src/map/pc.c
blob: c0f4d47ef71561f16eb1c022d9c3c620c02142c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
/**
 * This file is part of Hercules.
 * http://herc.ws - http://github.com/HerculesWS/Hercules
 *
 * Copyright (C) 2012-2016  Hercules Dev Team
 * Copyright (C)  Athena Dev Teams
 *
 * Hercules is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#define HERCULES_CORE

#include "config/core.h" // DBPATH, GP_BOUND_ITEMS, MAX_SPIRITBALL, RENEWAL, RENEWAL_ASPD, RENEWAL_CAST, RENEWAL_DROP, RENEWAL_EXP, SECURE_NPCTIMEOUT
#include "pc.h"

#include "map/atcommand.h" // get_atcommand_level()
#include "map/battle.h" // battle_config
#include "map/battleground.h"
#include "map/channel.h"
#include "map/chat.h"
#include "map/chrif.h"
#include "map/clif.h"
#include "map/date.h" // is_day_of_*()
#include "map/duel.h"
#include "map/elemental.h"
#include "map/guild.h" // guild-"search(), guild_request_info()
#include "map/homunculus.h"
#include "map/instance.h"
#include "map/intif.h"
#include "map/itemdb.h"
#include "map/log.h"
#include "map/mail.h"
#include "map/map.h"
#include "map/mercenary.h"
#include "map/mob.h" // struct mob_data
#include "map/npc.h" // fake_nd
#include "map/party.h" // party-"search()
#include "map/path.h"
#include "map/pc_groups.h"
#include "map/pet.h" // pet_unlocktarget()
#include "map/quest.h"
#include "map/script.h" // script_config
#include "map/skill.h"
#include "map/status.h" // struct status_data
#include "map/storage.h"
#include "common/cbasetypes.h"
#include "common/conf.h"
#include "common/core.h" // get_svn_revision()
#include "common/HPM.h"
#include "common/memmgr.h"
#include "common/mmo.h" // NAME_LENGTH, MAX_CARTS, NEW_CARTS
#include "common/nullpo.h"
#include "common/random.h"
#include "common/showmsg.h"
#include "common/socket.h"
#include "common/sql.h"
#include "common/strlib.h" // safestrncpy()
#include "common/sysinfo.h"
#include "common/timer.h"
#include "common/utils.h"

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

struct pc_interface pc_s;
struct pc_interface *pc;

//Converts a class to its array index for CLASS_COUNT defined arrays.
//Note that it does not do a validity check for speed purposes, where parsing
//player input make sure to use a pc->db_checkid first!
int pc_class2idx(int class_) {
	if (class_ >= JOB_NOVICE_HIGH) {
		class_ += - JOB_NOVICE_HIGH + JOB_MAX_BASIC;
	}
	Assert_ret(class_ >= 0 && class_ < CLASS_COUNT);
	return class_;
}

/**
 * Creates a new dummy map session data.
 * Used when there is no real player attached, but it is
 * required to provide a session.
 * Caller must release dummy on its own when it's no longer needed.
 */
struct map_session_data* pc_get_dummy_sd(void)
{
	struct map_session_data *dummy_sd;
	CREATE(dummy_sd, struct map_session_data, 1);
	dummy_sd->group = pcg->get_dummy_group(); // map_session_data.group is expected to be non-NULL at all times
	return dummy_sd;
}

/**
 * Sets player's group.
 * Caller should handle error (preferably display message and disconnect).
 * @param group_id Group ID
 * @return 1 on error, 0 on success
 */
int pc_set_group(struct map_session_data *sd, int group_id)
{
	GroupSettings *group = pcg->id2group(group_id);
	nullpo_retr(1, sd);
	if (group == NULL)
		return 1;
	sd->group_id = group_id;
	sd->group = group;
	return 0;
}

/**
 * Checks if commands used by player should be logged.
 */
bool pc_should_log_commands(struct map_session_data *sd)
{
	nullpo_retr(true, sd);
	return pcg->should_log_commands(sd->group);
}

int pc_invincible_timer(int tid, int64 tick, int id, intptr_t data)
{
	struct map_session_data *sd = map->id2sd(id);

	if (sd == NULL)
		return 1;

	if(sd->invincible_timer != tid){
		ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
		return 0;
	}
	sd->invincible_timer = INVALID_TIMER;
	skill->unit_move(&sd->bl,tick,1);

	return 0;
}

void pc_setinvincibletimer(struct map_session_data* sd, int val)
{
	nullpo_retv(sd);

	val += map->list[sd->bl.m].invincible_time_inc;

	if( sd->invincible_timer != INVALID_TIMER )
		timer->delete(sd->invincible_timer,pc->invincible_timer);
	sd->invincible_timer = timer->add(timer->gettick()+val,pc->invincible_timer,sd->bl.id,0);
}

void pc_delinvincibletimer(struct map_session_data* sd)
{
	nullpo_retv(sd);

	if( sd->invincible_timer != INVALID_TIMER )
	{
		timer->delete(sd->invincible_timer,pc->invincible_timer);
		sd->invincible_timer = INVALID_TIMER;
		skill->unit_move(&sd->bl,timer->gettick(),1);
	}
}

int pc_spiritball_timer(int tid, int64 tick, int id, intptr_t data) {
	struct map_session_data *sd = map->id2sd(id);
	int i;

	if (sd == NULL)
		return 1;

	if( sd->spiritball <= 0 )
	{
		ShowError("pc_spiritball_timer: %d spiritball's available. (aid=%d cid=%d tid=%d)\n", sd->spiritball, sd->status.account_id, sd->status.char_id, tid);
		sd->spiritball = 0;
		return 0;
	}

	ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
	if( i == sd->spiritball )
	{
		ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
		return 0;
	}

	sd->spiritball--;
	if( i != sd->spiritball )
		memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int));
	sd->spirit_timer[sd->spiritball] = INVALID_TIMER;

	clif->spiritball(&sd->bl);

	return 0;
}

/**
* Get the possible number of spiritball that a player can call.
* @param sd the affected player structure
* @param min the minimum number of spiritball regardless the level of MO_CALLSPIRITS
* @retval total number of spiritball
**/
int pc_getmaxspiritball(struct map_session_data *sd, int min) {
	int result;

	nullpo_ret(sd);

	result = pc->checkskill(sd, MO_CALLSPIRITS);

	if ( min && result < min )
		result = min;
	else if ( sd->sc.data[SC_RAISINGDRAGON] )
		result += sd->sc.data[SC_RAISINGDRAGON]->val1;
	if ( result > MAX_SPIRITBALL )
		result = MAX_SPIRITBALL;
	return result;
}

int pc_addspiritball(struct map_session_data *sd,int interval,int max)
{
	int tid, i;

	nullpo_ret(sd);

	if(max > MAX_SPIRITBALL)
		max = MAX_SPIRITBALL;
	if(sd->spiritball < 0)
		sd->spiritball = 0;

	if( sd->spiritball && sd->spiritball >= max ) {
		if(sd->spirit_timer[0] != INVALID_TIMER)
			timer->delete(sd->spirit_timer[0],pc->spiritball_timer);
		sd->spiritball--;
		if( sd->spiritball != 0 )
			memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int));
		sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
	}

	tid = timer->add(timer->gettick()+interval, pc->spiritball_timer, sd->bl.id, 0);
	ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == INVALID_TIMER || DIFF_TICK(timer->get(tid)->tick, timer->get(sd->spirit_timer[i])->tick) < 0);
	if( i != sd->spiritball )
		memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int));
	sd->spirit_timer[i] = tid;
	sd->spiritball++;
	if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
		clif->millenniumshield(&sd->bl,sd->spiritball);
	else
		clif->spiritball(&sd->bl);

	return 0;
}

int pc_delspiritball(struct map_session_data *sd,int count,int type)
{
	int i;

	nullpo_ret(sd);

	if(sd->spiritball <= 0) {
		sd->spiritball = 0;
		return 0;
	}

	if(count <= 0)
		return 0;
	if(count > sd->spiritball)
		count = sd->spiritball;
	sd->spiritball -= count;
	if(count > MAX_SPIRITBALL)
		count = MAX_SPIRITBALL;

	for(i=0;i<count;i++) {
		if(sd->spirit_timer[i] != INVALID_TIMER) {
			timer->delete(sd->spirit_timer[i],pc->spiritball_timer);
			sd->spirit_timer[i] = INVALID_TIMER;
		}
	}
	for(i=count;i<MAX_SPIRITBALL;i++) {
		sd->spirit_timer[i-count] = sd->spirit_timer[i];
		sd->spirit_timer[i] = INVALID_TIMER;
	}

	if(!type) {
		if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
			clif->millenniumshield(&sd->bl,sd->spiritball);
		else
			clif->spiritball(&sd->bl);
	}
	return 0;
}
int pc_check_banding(struct block_list *bl, va_list ap)
{
	int *c, *b_sd;
	struct block_list *src;
	const struct map_session_data *tsd;
	struct status_change *sc;

	nullpo_ret(bl);
	Assert_ret(bl->type == BL_PC);
	tsd = BL_UCCAST(BL_PC, bl);

	nullpo_ret(src = va_arg(ap,struct block_list *));
	c = va_arg(ap,int *);
	b_sd = va_arg(ap, int *);

	if(pc_isdead(tsd))
		return 0;

	sc = status->get_sc(bl);

	if( sc && sc->data[SC_BANDING] )
	{
		b_sd[(*c)++] = tsd->bl.id;
		return 1;
	}

	return 0;
}
int pc_banding(struct map_session_data *sd, uint16 skill_lv) {
	int c;
	int b_sd[MAX_PARTY]; // In case of a full Royal Guard party.
	int i, j, hp, extra_hp = 0, tmp_qty = 0;
	struct map_session_data *bsd;
	struct status_change *sc;
	int range = skill->get_splash(LG_BANDING,skill_lv);

	nullpo_ret(sd);

	c = 0;
	memset(b_sd, 0, sizeof(b_sd));
	i = party->foreachsamemap(pc->check_banding,sd,range,&sd->bl,&c,&b_sd);

	if( c < 1 ) {
		//just recalc status no need to recalc hp
		if( (sc = status->get_sc(&sd->bl)) != NULL  && sc->data[SC_BANDING] ) {
			// No more Royal Guards in Banding found.
			sc->data[SC_BANDING]->val2 = 0; // Reset the counter
			status_calc_bl(&sd->bl, status->sc2scb_flag(SC_BANDING));
		}
		return 0;
	}

	//Add yourself
	hp = status_get_hp(&sd->bl);
	i++;

	// Get total HP of all Royal Guards in party.
	for( j = 0; j < i; j++ ) {
		bsd = map->id2sd(b_sd[j]);
		if( bsd != NULL )
			hp += status_get_hp(&bsd->bl);
	}

	// Set average HP.
	hp = hp / i;

	// If a Royal Guard have full HP, give more HP to others that haven't full HP.
	for (j = 0; j < i; j++) {
		int tmp_hp;
		bsd = map->id2sd(b_sd[j]);
		if (bsd != NULL && (tmp_hp = hp - status_get_max_hp(&bsd->bl)) > 0) {
			extra_hp += tmp_hp;
			tmp_qty++;
		}
	}

	if( extra_hp > 0 && tmp_qty > 0 )
		hp += extra_hp / tmp_qty;

	for( j = 0; j < i; j++ ) {
		bsd = map->id2sd(b_sd[j]);
		if( bsd != NULL ) {
			status->set_hp(&bsd->bl,hp,0); // Set hp
			if( (sc = status->get_sc(&bsd->bl)) != NULL  && sc->data[SC_BANDING] ) {
				sc->data[SC_BANDING]->val2 = c; // Set the counter. It doesn't count your self.
				status_calc_bl(&bsd->bl, status->sc2scb_flag(SC_BANDING)); // Set atk and def.
			}
		}
	}

	return c;
}

// Increases a player's fame points and displays a notice to him
void pc_addfame(struct map_session_data *sd,int count)
{
	int ranktype = -1;
	nullpo_retv(sd);
	sd->status.fame += count;
	if(sd->status.fame > MAX_FAME)
		sd->status.fame = MAX_FAME;
	switch(sd->class_&MAPID_UPPERMASK){
		case MAPID_BLACKSMITH: ranktype = RANKTYPE_BLACKSMITH; break;
		case MAPID_ALCHEMIST:  ranktype = RANKTYPE_ALCHEMIST; break;
		case MAPID_TAEKWON: ranktype = RANKTYPE_TAEKWON; break;
	}
	clif->update_rankingpoint(sd, ranktype, count);
	chrif->updatefamelist(sd);
}

// Check whether a player ID is in the fame rankers' list of its job, returns his/her position if so, 0 else
unsigned char pc_famerank(int char_id, int job)
{
	int i;

	switch(job){
		case MAPID_BLACKSMITH: // Blacksmith
		    for(i = 0; i < MAX_FAME_LIST; i++){
				if(pc->smith_fame_list[i].id == char_id)
				    return i + 1;
			}
			break;
		case MAPID_ALCHEMIST: // Alchemist
			for(i = 0; i < MAX_FAME_LIST; i++){
				if(pc->chemist_fame_list[i].id == char_id)
					return i + 1;
			}
			break;
		case MAPID_TAEKWON: // Taekwon
			for(i = 0; i < MAX_FAME_LIST; i++){
				if(pc->taekwon_fame_list[i].id == char_id)
					return i + 1;
			}
			break;
	}

	return 0;
}

int pc_setrestartvalue(struct map_session_data *sd,int type) {
	struct status_data *st, *bst;
	nullpo_ret(sd);

	bst = &sd->base_status;
	st = &sd->battle_status;

	if (type&1) {
		//Normal resurrection
		st->hp = 1; //Otherwise status->heal may fail if dead.
		status->heal(&sd->bl, bst->hp, 0, 1);
		if( st->sp < bst->sp )
			status->set_sp(&sd->bl, bst->sp, 1);
	} else { //Just for saving on the char-server (with values as if respawned)
		sd->status.hp = bst->hp;
		sd->status.sp = (st->sp < bst->sp) ? bst->sp : st->sp;
	}
	return 0;
}

/*==========================================
	Rental System
 *------------------------------------------*/
int pc_inventory_rental_end(int tid, int64 tick, int id, intptr_t data) {
	struct map_session_data *sd = map->id2sd(id);
	if( sd == NULL )
		return 0;
	if( tid != sd->rental_timer )
	{
		ShowError("pc_inventory_rental_end: invalid timer id.\n");
		return 0;
	}

	pc->inventory_rentals(sd);
	return 1;
}

int pc_inventory_rental_clear(struct map_session_data *sd)
{
	nullpo_ret(sd);
	if( sd->rental_timer != INVALID_TIMER )
	{
		timer->delete(sd->rental_timer, pc->inventory_rental_end);
		sd->rental_timer = INVALID_TIMER;
	}

	return 1;
}
/* assumes i is valid (from default areas where it is called, it is) */
void pc_rental_expire(struct map_session_data *sd, int i) {
	short nameid;

	nullpo_retv(sd);
	Assert_retv(i >= 0 && i < MAX_INVENTORY);
	nameid = sd->status.inventory[i].nameid;

	/* Soon to be dropped, we got plans to integrate it with item db */
	switch( nameid ) {
		case ITEMID_REINS_OF_MOUNT:
			status_change_end(&sd->bl,SC_ALL_RIDING,INVALID_TIMER);
			break;
		case ITEMID_LOVE_ANGEL:
			if( sd->status.font == 1 ) {
				sd->status.font = 0;
				clif->font(sd);
			}
			break;
		case ITEMID_SQUIRREL:
			if( sd->status.font == 2 ) {
				sd->status.font = 0;
				clif->font(sd);
			}
			break;
		case ITEMID_GOGO:
			if( sd->status.font == 3 ) {
				sd->status.font = 0;
				clif->font(sd);
			}
			break;
		case ITEMID_PICTURE_DIARY:
			if( sd->status.font == 4 ) {
				sd->status.font = 0;
				clif->font(sd);
			}
			break;
		case ITEMID_MINI_HEART:
			if( sd->status.font == 5 ) {
				sd->status.font = 0;
				clif->font(sd);
			}
			break;
		case ITEMID_NEWCOMER:
			if( sd->status.font == 6 ) {
				sd->status.font = 0;
				clif->font(sd);
			}
			break;
		case ITEMID_KID:
			if( sd->status.font == 7 ) {
				sd->status.font = 0;
				clif->font(sd);
			}
			break;
		case ITEMID_MAGIC_CASTLE:
			if( sd->status.font == 8 ) {
				sd->status.font = 0;
				clif->font(sd);
			}
			break;
		case ITEMID_BULGING_HEAD:
			if( sd->status.font == 9 ) {
				sd->status.font = 0;
				clif->font(sd);
			}
			break;
	}

	clif->rental_expired(sd->fd, i, sd->status.inventory[i].nameid);
	pc->delitem(sd, i, sd->status.inventory[i].amount, 0, DELITEM_NORMAL, LOG_TYPE_RENTAL);
}
void pc_inventory_rentals(struct map_session_data *sd)
{
	int i, c = 0;
	int64 expire_tick, next_tick = INT64_MAX;

	nullpo_retv(sd);
	for( i = 0; i < MAX_INVENTORY; i++ )
	{ // Check for Rentals on Inventory
		if( sd->status.inventory[i].nameid == 0 )
			continue; // Nothing here
		if( sd->status.inventory[i].expire_time == 0 )
			continue;

		if( sd->status.inventory[i].expire_time <= time(NULL) ) {
			pc->rental_expire(sd,i);
		} else {
			expire_tick = (int64)(sd->status.inventory[i].expire_time - time(NULL)) * 1000;
			clif->rental_time(sd->fd, sd->status.inventory[i].nameid, (int)(expire_tick / 1000));
			next_tick = min(expire_tick, next_tick);
			c++;
		}
	}

	if( c > 0 ) // min(next_tick,3600000) 1 hour each timer to keep announcing to the owner, and to avoid a but with rental time > 15 days
		sd->rental_timer = timer->add(timer->gettick() + min(next_tick,3600000), pc->inventory_rental_end, sd->bl.id, 0);
	else
		sd->rental_timer = INVALID_TIMER;
}

void pc_inventory_rental_add(struct map_session_data *sd, int seconds)
{
	int tick = seconds * 1000;

	if( sd == NULL )
		return;

	if( sd->rental_timer != INVALID_TIMER )
	{
		const struct TimerData * td;
		td = timer->get(sd->rental_timer);
		if( DIFF_TICK(td->tick, timer->gettick()) > tick )
		{ // Update Timer as this one ends first than the current one
			pc->inventory_rental_clear(sd);
			sd->rental_timer = timer->add(timer->gettick() + tick, pc->inventory_rental_end, sd->bl.id, 0);
		}
	}
	else
		sd->rental_timer = timer->add(timer->gettick() + min(tick,3600000), pc->inventory_rental_end, sd->bl.id, 0);
}

/*==========================================
 * prepares character for saving.
 *------------------------------------------*/
int pc_makesavestatus(struct map_session_data *sd)
{
	nullpo_ret(sd);

	if(!battle_config.save_clothcolor)
		sd->status.clothes_color=0;

	if (!battle_config.save_body_style)
		sd->status.body = 0;


	//Only copy the Cart/Peco/Falcon options, the rest are handled via
	//status change load/saving. [Skotlex]
#ifdef NEW_CARTS
	sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
#else
	sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
#endif
	if (sd->sc.data[SC_JAILED]) { //When Jailed, do not move last point.
		if(pc_isdead(sd)){
			pc->setrestartvalue(sd,0);
		} else {
			sd->status.hp = sd->battle_status.hp;
			sd->status.sp = sd->battle_status.sp;
		}
		sd->status.last_point.map = sd->mapindex;
		sd->status.last_point.x = sd->bl.x;
		sd->status.last_point.y = sd->bl.y;
		return 0;
	}

	if(pc_isdead(sd)){
		pc->setrestartvalue(sd,0);
		memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
	} else {
		sd->status.hp = sd->battle_status.hp;
		sd->status.sp = sd->battle_status.sp;
		sd->status.last_point.map = sd->mapindex;
		sd->status.last_point.x = sd->bl.x;
		sd->status.last_point.y = sd->bl.y;
	}

	if( ( map->list[sd->bl.m].flag.nosave && sd->state.autotrade != 2 ) || map->list[sd->bl.m].instance_id >= 0) {
		struct map_data *m=&map->list[sd->bl.m];
		if(m->save.map)
			memcpy(&sd->status.last_point,&m->save,sizeof(sd->status.last_point));
		else
			memcpy(&sd->status.last_point,&sd->status.save_point,sizeof(sd->status.last_point));
	}
	if( sd->status.last_point.map == 0 ) {
		sd->status.last_point.map = 1;
		sd->status.last_point.x = 0;
		sd->status.last_point.y = 0;
	}

	if( sd->status.save_point.map == 0 ) {
		sd->status.save_point.map = 1;
		sd->status.save_point.x = 0;
		sd->status.save_point.y = 0;
	}
	return 0;
}

/*==========================================
 * Off init ? Connection?
 *------------------------------------------*/
int pc_setnewpc(struct map_session_data *sd, int account_id, int char_id, int login_id1, unsigned int client_tick, int sex, int fd)
{
	nullpo_ret(sd);

	sd->bl.id        = account_id;
	sd->status.account_id   = account_id;
	sd->status.char_id      = char_id;
	sd->status.sex   = sex;
	sd->login_id1    = login_id1;
	sd->login_id2    = 0; // at this point, we can not know the value :(
	sd->client_tick  = client_tick;
	sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
	sd->bl.type      = BL_PC;
	sd->canlog_tick  = timer->gettick();
	//Required to prevent homunculus copuing a base speed of 0.
	sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
	sd->state.warp_clean = 1;
	sd->catch_target_class = -1;
	return 0;
}

int pc_equippoint(struct map_session_data *sd,int n)
{
	int ep = 0;

	nullpo_ret(sd);
	Assert_ret(n >= 0 && n < MAX_INVENTORY);

	if(!sd->inventory_data[n])
		return 0;

	if (!itemdb->isequip2(sd->inventory_data[n]))
		return 0; //Not equippable by players.

	ep = sd->inventory_data[n]->equip;
	if (sd->inventory_data[n]->look == W_DAGGER
	 || sd->inventory_data[n]->look == W_1HSWORD
	 || sd->inventory_data[n]->look == W_1HAXE
	) {
		if (pc->checkskill(sd,AS_LEFT) > 0
		 || (sd->class_&MAPID_UPPERMASK) == MAPID_ASSASSIN
		 || (sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO
		) {
			//Kagerou and Oboro can dual wield daggers. [Rytech]
			if( ep == EQP_HAND_R )
				return EQP_ARMS;
			if( ep == EQP_SHADOW_WEAPON )
				return EQP_SHADOW_ARMS;
		}
	}
	return ep;
}

int pc_setinventorydata(struct map_session_data *sd)
{
	int i;

	nullpo_ret(sd);

	for (i = 0; i < MAX_INVENTORY; i++) {
		int id = sd->status.inventory[i].nameid;
		sd->inventory_data[i] = id?itemdb->search(id):NULL;
	}
	return 0;
}

int pc_calcweapontype(struct map_session_data *sd)
{
	nullpo_ret(sd);

	// single-hand
	if(sd->weapontype2 == W_FIST) {
		sd->status.weapon = sd->weapontype1;
		return 1;
	}
	if(sd->weapontype1 == W_FIST) {
		sd->status.weapon = sd->weapontype2;
		return 1;
	}
	// dual-wield
	sd->status.weapon = 0;
	switch (sd->weapontype1){
		case W_DAGGER:
			switch (sd->weapontype2) {
				case W_DAGGER:  sd->status.weapon = W_DOUBLE_DD; break;
				case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
				case W_1HAXE:   sd->status.weapon = W_DOUBLE_DA; break;
			}
			break;
		case W_1HSWORD:
			switch (sd->weapontype2) {
				case W_DAGGER:  sd->status.weapon = W_DOUBLE_DS; break;
				case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
				case W_1HAXE:   sd->status.weapon = W_DOUBLE_SA; break;
			}
			break;
		case W_1HAXE:
			switch (sd->weapontype2) {
				case W_DAGGER:  sd->status.weapon = W_DOUBLE_DA; break;
				case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
				case W_1HAXE:   sd->status.weapon = W_DOUBLE_AA; break;
			}
	}
	// unknown, default to right hand type
	if (!sd->status.weapon)
		sd->status.weapon = sd->weapontype1;

	return 2;
}

int pc_setequipindex(struct map_session_data *sd)
{
	int i,j;

	nullpo_ret(sd);

	for(i=0;i<EQI_MAX;i++)
		sd->equip_index[i] = -1;

	for(i=0;i<MAX_INVENTORY;i++) {
		if(sd->status.inventory[i].nameid <= 0)
			continue;
		if(sd->status.inventory[i].equip) {
			for(j=0;j<EQI_MAX;j++)
				if(sd->status.inventory[i].equip & pc->equip_pos[j])
					sd->equip_index[j] = i;

			if(sd->status.inventory[i].equip & EQP_HAND_R)
			{
				if(sd->inventory_data[i])
					sd->weapontype1 = sd->inventory_data[i]->look;
				else
					sd->weapontype1 = 0;
			}

			if( sd->status.inventory[i].equip & EQP_HAND_L )
			{
				if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
					sd->weapontype2 = sd->inventory_data[i]->look;
				else
					sd->weapontype2 = 0;
			}
		}
	}
	pc->calcweapontype(sd);

	return 0;
}

bool pc_isequipped(struct map_session_data *sd, int nameid)
{
	int i, j;

	nullpo_retr(false, sd);
	for (i = 0; i < EQI_MAX; i++) {
		int index = sd->equip_index[i];
		if( index < 0 ) continue;

		if( i == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index ) continue;
		if( i == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index ) continue;
		if( i == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index) ) continue;

		if( !sd->inventory_data[index] ) continue;

		if( sd->inventory_data[index]->nameid == nameid )
			return true;

		for( j = 0; j < sd->inventory_data[index]->slot; j++ )
			if( sd->status.inventory[index].card[j] == nameid )
				return true;
	}

	return false;
}

bool pc_can_Adopt(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd )
{
	if( !p1_sd || !p2_sd || !b_sd )
		return false;

	if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
		return false; // already adopted baby / in adopt request

	if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
		return false; // You need to be married and in party with baby to adopt

	if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
		return false; // Not married, wrong married

	if( p2_sd->status.party_id != p1_sd->status.party_id )
		return false; // Both parents need to be in the same party

	// Parents need to have their ring equipped
	if( !pc->isequipped(p1_sd, WEDDING_RING_M) && !pc->isequipped(p1_sd, WEDDING_RING_F) )
		return false;

	if( !pc->isequipped(p2_sd, WEDDING_RING_M) && !pc->isequipped(p2_sd, WEDDING_RING_F) )
		return false;

	// Already adopted a baby
	if( p1_sd->status.child || p2_sd->status.child ) {
		clif->adopt_reply(p1_sd, 0);
		return false;
	}

	// Parents need at least lvl 70 to adopt
	if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
		clif->adopt_reply(p1_sd, 1);
		return false;
	}

	if( b_sd->status.partner_id ) {
		clif->adopt_reply(p1_sd, 2);
		return false;
	}

	if( !( ( b_sd->status.class_ >= JOB_NOVICE && b_sd->status.class_ <= JOB_THIEF ) || b_sd->status.class_ == JOB_SUPER_NOVICE ) )
		return false;

	return true;
}

/*==========================================
 * Adoption Process
 *------------------------------------------*/
bool pc_adoption(struct map_session_data *p1_sd, struct map_session_data *p2_sd, struct map_session_data *b_sd)
{
	int job, joblevel;
	unsigned int jobexp;

	if( !pc->can_Adopt(p1_sd, p2_sd, b_sd) )
		return false;

	nullpo_retr(false, b_sd);
	// Preserve current job levels and progress
	joblevel = b_sd->status.job_level;
	jobexp = b_sd->status.job_exp;

	job = pc->mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
	if( job != -1 && !pc->jobchange(b_sd, job, 0) )
	{ // Success, proceed to configure parents and baby skills
		p1_sd->status.child = b_sd->status.char_id;
		p2_sd->status.child = b_sd->status.char_id;
		b_sd->status.father = p1_sd->status.char_id;
		b_sd->status.mother = p2_sd->status.char_id;

		// Restore progress
		b_sd->status.job_level = joblevel;
		clif->updatestatus(b_sd, SP_JOBLEVEL);
		b_sd->status.job_exp = jobexp;
		clif->updatestatus(b_sd, SP_JOBEXP);

		// Baby Skills
		pc->skill(b_sd, WE_BABY, 1, SKILL_GRANT_PERMANENT);
		pc->skill(b_sd, WE_CALLPARENT, 1, SKILL_GRANT_PERMANENT);

		// Parents Skills
		pc->skill(p1_sd, WE_CALLBABY, 1, SKILL_GRANT_PERMANENT);
		pc->skill(p2_sd, WE_CALLBABY, 1, SKILL_GRANT_PERMANENT);

		return true;
	}

	return false; // Job Change Fail
}

/*=================================================
 * Checks if the player can equip the item at index n in inventory.
 * Returns 0 (no) or 1 (yes).
 *------------------------------------------------*/
int pc_isequip(struct map_session_data *sd,int n)
{
	struct item_data *item;

	nullpo_ret(sd);

	item = sd->inventory_data[n];

	if(item == NULL)
		return 0;

#if PACKETVER <= 20100707
	if (itemdb_is_shadowequip(item->equip) || itemdb_is_costumeequip(item->equip))
		return 0;
#endif

	if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
		return 1;

	if (item->elv && sd->status.base_level < (unsigned int)item->elv) {
		clif->msgtable(sd, MSG_ITEM_CANT_EQUIP_LVL);
		return 0;
	}
	if (item->elvmax && sd->status.base_level > (unsigned int)item->elvmax) {
		clif->msgtable(sd, MSG_ITEM_CANT_EQUIP_LVL);
		return 0;
	}
	if(item->sex != 2 && sd->status.sex != item->sex)
		return 0;

	if ( item->equip & EQP_AMMO ) {
		if ( (sd->state.active && !pc_iscarton(sd)) // check if sc data is already loaded.
			&& (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC) ) {
			clif->msgtable(sd, MSG_ITEM_NEED_CART);
			return 0;
		}
		if ( !pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC) ) {
			clif->msgtable(sd, MSG_ITEM_NEED_MADO);
			return 0;
		}
	}
	if (sd->sc.count) {

		if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.data[SC_NOEQUIPWEAPON]) // Also works with left-hand weapons [DracoRPG]
			return 0;
		if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.data[SC_NOEQUIPSHIELD])
			return 0;
		if(item->equip & EQP_ARMOR && sd->sc.data[SC_NOEQUIPARMOR])
			return 0;
		if(item->equip & EQP_HEAD_TOP && sd->sc.data[SC_NOEQUIPHELM])
			return 0;
		if(item->equip & EQP_ACC && sd->sc.data[SC__STRIPACCESSARY])
			return 0;
		if(item->equip && sd->sc.data[SC_KYOUGAKU])
			return 0;

		if (sd->sc.data[SC_SOULLINK] && sd->sc.data[SC_SOULLINK]->val2 == SL_SUPERNOVICE) {
			//Spirit of Super Novice equip bonuses. [Skotlex]
			if (sd->status.base_level > 90 && item->equip & EQP_HELM)
				return 1; //Can equip all helms

			if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON)
				switch(item->look) { //In weapons, the look determines type of weapon.
					case W_DAGGER: //Level 4 Knives are equippable.. this means all knives, I'd guess?
					case W_1HSWORD: //All 1H swords
					case W_1HAXE: //All 1H Axes
					case W_MACE: //All 1H Maces
					case W_STAFF: //All 1H Staves
						return 1;
				}
		}
	}
	//Not equipable by class. [Skotlex]
	if (!(1ULL<<(sd->class_&MAPID_BASEMASK)&item->class_base[(sd->class_&JOBL_2_1)?1:((sd->class_&JOBL_2_2)?2:0)]))
		return 0;
	//Not usable by upper class. [Inkfish]
	while( 1 ) {
		if( item->class_upper&ITEMUPPER_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY)) ) break;
		if( item->class_upper&ITEMUPPER_UPPER  &&   sd->class_&(JOBL_UPPER|JOBL_THIRD)            ) break;
		if( item->class_upper&ITEMUPPER_BABY   &&   sd->class_&JOBL_BABY                          ) break;
		if( item->class_upper&ITEMUPPER_THIRD  &&   sd->class_&JOBL_THIRD                         ) break;
		return 0;
	}

	if ( battle_config.unequip_restricted_equipment & 1 ) {
		int i;
		for ( i = 0; i < map->list[sd->bl.m].zone->disabled_items_count; i++ )
			if ( map->list[sd->bl.m].zone->disabled_items[i] == sd->status.inventory[n].nameid )
				return 0;
	}

	if ( battle_config.unequip_restricted_equipment & 2 ) {
		if ( !itemdb_isspecial( sd->status.inventory[n].card[0] ) ) {
			int i, slot;
			for ( slot = 0; slot < MAX_SLOTS; slot++ )
				for ( i = 0; i < map->list[sd->bl.m].zone->disabled_items_count; i++ )
					if ( map->list[sd->bl.m].zone->disabled_items[i] == sd->status.inventory[n].card[slot] )
						return 0;
		}
	}

	return 1;
}

/*==========================================
 * No problem with the session id
 * set the status that has been sent from char server
 *------------------------------------------*/
bool pc_authok(struct map_session_data *sd, int login_id2, time_t expiration_time, int group_id, const struct mmo_charstatus *st, bool changing_mapservers)
{
	int i;
	int64 tick = timer->gettick();
	uint32 ip;

	nullpo_retr(false, sd);
	ip = sockt->session[sd->fd]->client_addr;

	sd->login_id2 = login_id2;

	if (pc->set_group(sd, group_id) != 0) {
		ShowWarning("pc_authok: %s (AID:%d) logged in with unknown group id (%d)! kicking...\n",
			st->name, sd->status.account_id, group_id);
		clif->authfail_fd(sd->fd, 0);
		return false;
	}

	memcpy(&sd->status, st, sizeof(*st));

	if (st->sex != sd->status.sex) {
		clif->authfail_fd(sd->fd, 0);
		return false;
	}

	//Set the map-server used job id. [Skotlex]
	i = pc->jobid2mapid(sd->status.class_);
	if (i == -1) { //Invalid class?
		ShowError("pc_authok: Invalid class %d for player %s (%d:%d). Class was changed to novice.\n", sd->status.class_, sd->status.name, sd->status.account_id, sd->status.char_id);
		sd->status.class_ = JOB_NOVICE;
		sd->class_ = MAPID_NOVICE;
	} else
		sd->class_ = i;

	// Checks and fixes to character status data, that are required
	// in case of configuration change or stuff, which cannot be
	// checked on char-server.
	if( sd->status.hair < MIN_HAIR_STYLE || sd->status.hair > MAX_HAIR_STYLE ) {
		sd->status.hair = MIN_HAIR_STYLE;
	}
	if( sd->status.hair_color < MIN_HAIR_COLOR || sd->status.hair_color > MAX_HAIR_COLOR ) {
		sd->status.hair_color = MIN_HAIR_COLOR;
	}
	if( sd->status.clothes_color < MIN_CLOTH_COLOR || sd->status.clothes_color > MAX_CLOTH_COLOR ) {
		sd->status.clothes_color = MIN_CLOTH_COLOR;
	}
	if (sd->status.body < MIN_BODY_STYLE || sd->status.body > MAX_BODY_STYLE) {
		sd->status.body = MIN_BODY_STYLE;
	}

	//Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
	if(!sd->status.hp) pc_setdead(sd);
	sd->state.connect_new = 1;

	sd->followtimer = INVALID_TIMER; // [MouseJstr]
	sd->invincible_timer = INVALID_TIMER;
	sd->npc_timer_id = INVALID_TIMER;
	sd->pvp_timer = INVALID_TIMER;
	sd->fontcolor_tid = INVALID_TIMER;
	sd->expiration_tid = INVALID_TIMER;
	/**
	 * For the Secure NPC Timeout option (check config/Secure.h) [RR]
	 **/
#ifdef SECURE_NPCTIMEOUT
	/**
	 * Initialize to defaults/expected
	 **/
	sd->npc_idle_timer = INVALID_TIMER;
	sd->npc_idle_tick = tick;
	sd->npc_idle_type = NPCT_INPUT;
#endif

	sd->canuseitem_tick = tick;
	sd->canusecashfood_tick = tick;
	sd->canequip_tick = tick;
	sd->cantalk_tick = tick;
	sd->canskill_tick = tick;
	sd->cansendmail_tick = tick;
	sd->hchsysch_tick = tick;

	sd->idletime = sockt->last_tick;

	for(i = 0; i < MAX_SPIRITBALL; i++)
		sd->spirit_timer[i] = INVALID_TIMER;
	for(i = 0; i < ARRAYLENGTH(sd->autobonus); i++)
		sd->autobonus[i].active = INVALID_TIMER;
	for(i = 0; i < ARRAYLENGTH(sd->autobonus2); i++)
		sd->autobonus2[i].active = INVALID_TIMER;
	for(i = 0; i < ARRAYLENGTH(sd->autobonus3); i++)
		sd->autobonus3[i].active = INVALID_TIMER;

	if (battle_config.item_auto_get)
		sd->state.autoloot = 10000;

	if (battle_config.disp_experience)
		sd->state.showexp = 1;
	if (battle_config.disp_zeny)
		sd->state.showzeny = 1;

	if (!(battle_config.display_skill_fail&2))
		sd->state.showdelay = 1;

	pc->setinventorydata(sd);
	pc_setequipindex(sd);

	if( sd->status.option & OPTION_INVISIBLE && !pc->can_use_command(sd, "@hide") )
		sd->status.option &=~ OPTION_INVISIBLE;

	status->change_init(&sd->bl);

	sd->sc.option = sd->status.option; //This is the actual option used in battle.

	//Set here because we need the inventory data for weapon sprite parsing.
	status->set_viewdata(&sd->bl, sd->status.class_);
	unit->dataset(&sd->bl);

	sd->guild_x = -1;
	sd->guild_y = -1;

	sd->disguise = -1;

	sd->instance = NULL;
	sd->instances = 0;

	sd->bg_queue.arena = NULL;
	sd->bg_queue.ready = 0;
	sd->bg_queue.client_has_bg_data = 0;
	sd->bg_queue.type = 0;

	VECTOR_INIT(sd->script_queues);

	sd->state.dialog = 0;

	sd->delayed_damage = 0;

	if( battle_config.item_check )
		sd->state.itemcheck = 1;

	// Event Timers
	for( i = 0; i < MAX_EVENTTIMER; i++ )
		sd->eventtimer[i] = INVALID_TIMER;
	// Rental Timer
	sd->rental_timer = INVALID_TIMER;

	for( i = 0; i < MAX_PC_FEELHATE; i++ )
		sd->hate_mob[i] = -1;

	sd->quest_log = NULL;
	sd->num_quests = 0;
	sd->avail_quests = 0;
	sd->save_quest = false;

	sd->regs.vars = i64db_alloc(DB_OPT_BASE);
	sd->regs.arrays = NULL;
	sd->vars_dirty = false;
	sd->vars_ok = false;
	sd->vars_received = 0x0;

	sd->lang_id = map->default_lang_id;

	//warp player
	if ((i=pc->setpos(sd,sd->status.last_point.map, sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT)) != 0) {
		ShowError ("Last_point_map %s - id %d not found (error code %d)\n", mapindex_id2name(sd->status.last_point.map), sd->status.last_point.map, i);

		// try warping to a default map instead (church graveyard)
		if (pc->setpos(sd, mapindex->name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != 0) {
			// if we fail again
			clif->authfail_fd(sd->fd, 0);
			return false;
		}
	} else if (map->getcell(map->mapindex2mapid(sd->status.last_point.map), &sd->bl, sd->status.last_point.x, sd->status.last_point.y, CELL_CHKNOPASS)) {
		//warp player stuck in invaild cell
		pc->setpos(sd,sd->status.last_point.map,0,0,CLR_OUTSIGHT);
	}

	clif->authok(sd);

	//Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
	sd->die_counter=-1;

	//display login notice
	ShowInfo("'"CL_WHITE"%s"CL_RESET"' logged in."
	         " (AID/CID: '"CL_WHITE"%d/%d"CL_RESET"',"
	         " IP: '"CL_WHITE"%u.%u.%u.%u"CL_RESET"',"
	         " Group '"CL_WHITE"%d"CL_RESET"').\n",
	         sd->status.name, sd->status.account_id, sd->status.char_id,
	         CONVIP(ip), sd->group_id);
	// Send friends list
	clif->friendslist_send(sd);

	if( !changing_mapservers ) {

		if (battle_config.display_version == 1) {
			char buf[256];
			sprintf(buf, msg_sd(sd,1295), sysinfo->vcstype(), sysinfo->vcsrevision_src(), sysinfo->vcsrevision_scripts()); // %s revision '%s' (src) / '%s' (scripts)
			clif->message(sd->fd, buf);
		}

		if (expiration_time != 0) {
			sd->expiration_time = expiration_time;
		}

		/**
		 * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
		 **/
		clif->changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
	}

	/**
	 * Check if player have any cool downs on
	 **/
	skill->cooldown_load(sd);

	/**
	 * Check if player have any item cooldowns on
	 **/
	pc->itemcd_do(sd,true);

#ifdef GP_BOUND_ITEMS
	if( sd->status.party_id == 0 )
		pc->bound_clear(sd,IBT_PARTY);
#endif

	/* [Ind/Hercules] */
	sd->sc_display = NULL;
	sd->sc_display_count = 0;

	// Request all registries (auth is considered completed whence they arrive)
	intif->request_registry(sd,7);
	return true;
}

/*==========================================
 * Closes a connection because it failed to be authenticated from the char server.
 *------------------------------------------*/
void pc_authfail(struct map_session_data *sd)
{
	nullpo_retv(sd);
	clif->authfail_fd(sd->fd, 0);
	return;
}

//Attempts to set a mob.
int pc_set_hate_mob(struct map_session_data *sd, int pos, struct block_list *bl)
{
	int class_;
	if (!sd || !bl || pos < 0 || pos >= MAX_PC_FEELHATE)
		return 0;
	if (sd->hate_mob[pos] != -1) {
		//Can't change hate targets.
		clif->hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
		return 0;
	}

	class_ = status->get_class(bl);
	if (!pc->db_checkid(class_)) {
		unsigned int max_hp = status_get_max_hp(bl);
		if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
			return 0;
		if (pos != status_get_size(bl))
			return 0; //Wrong size
	}
	sd->hate_mob[pos] = class_;
	pc_setglobalreg(sd,script->add_str(pc->sg_info[pos].hate_var),class_+1);
	clif->hate_info(sd, pos, class_, 1);
	return 1;
}

/*==========================================
 * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
 *------------------------------------------*/
int pc_reg_received(struct map_session_data *sd)
{
	int i, idx = 0;

	nullpo_ret(sd);
	sd->vars_ok = true;

	sd->change_level_2nd = pc_readglobalreg(sd,script->add_str("jobchange_level"));
	sd->change_level_3rd = pc_readglobalreg(sd,script->add_str("jobchange_level_3rd"));
	sd->die_counter = pc_readglobalreg(sd,script->add_str("PC_DIE_COUNTER"));

	// Cash shop
	sd->cashPoints = pc_readaccountreg(sd,script->add_str("#CASHPOINTS"));
	sd->kafraPoints = pc_readaccountreg(sd,script->add_str("#KAFRAPOINTS"));

	// Cooking Exp
	sd->cook_mastery = pc_readglobalreg(sd,script->add_str("COOK_MASTERY"));

	if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
		// Better check for class rather than skill to prevent "skill resets" from unsetting this
		sd->mission_mobid = pc_readglobalreg(sd,script->add_str("TK_MISSION_ID"));
		sd->mission_count = pc_readglobalreg(sd,script->add_str("TK_MISSION_COUNT"));
	}

	//SG map and mob read [Komurka]
	for (i = 0; i < MAX_PC_FEELHATE; i++) {
		//for now - someone need to make reading from txt/sql
		int j = pc_readglobalreg(sd,script->add_str(pc->sg_info[i].feel_var));
		if (j != 0) {
			sd->feel_map[i].index = j;
			sd->feel_map[i].m = map->mapindex2mapid(j);
		} else {
			sd->feel_map[i].index = 0;
			sd->feel_map[i].m = -1;
		}
		sd->hate_mob[i] = pc_readglobalreg(sd,script->add_str(pc->sg_info[i].hate_var))-1;
	}

	if ((i = pc->checkskill(sd,RG_PLAGIARISM)) > 0) {
		sd->cloneskill_id = pc_readglobalreg(sd,script->add_str("CLONE_SKILL"));
		if (sd->cloneskill_id > 0 && (idx = skill->get_index(sd->cloneskill_id)) > 0) {
			sd->status.skill[idx].id = sd->cloneskill_id;
			sd->status.skill[idx].lv = pc_readglobalreg(sd,script->add_str("CLONE_SKILL_LV"));
			if (sd->status.skill[idx].lv > i)
				sd->status.skill[idx].lv = i;
			sd->status.skill[idx].flag = SKILL_FLAG_PLAGIARIZED;
		}
	}
	if ((i = pc->checkskill(sd,SC_REPRODUCE)) > 0) {
		sd->reproduceskill_id = pc_readglobalreg(sd,script->add_str("REPRODUCE_SKILL"));
		if( sd->reproduceskill_id > 0 && (idx = skill->get_index(sd->reproduceskill_id)) > 0) {
			sd->status.skill[idx].id = sd->reproduceskill_id;
			sd->status.skill[idx].lv = pc_readglobalreg(sd,script->add_str("REPRODUCE_SKILL_LV"));
			if( i < sd->status.skill[idx].lv)
				sd->status.skill[idx].lv = i;
			sd->status.skill[idx].flag = SKILL_FLAG_PLAGIARIZED;
		}
	}

	//Weird... maybe registries were reloaded?
	if (sd->state.active)
		return 0;
	sd->state.active = 1;

	if (sd->status.party_id)
		party->member_joined(sd);
	if (sd->status.guild_id)
		guild->member_joined(sd);

	// pet
	if (sd->status.pet_id > 0)
		intif->request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);

	// Homunculus [albator]
	if( sd->status.hom_id > 0 )
		intif->homunculus_requestload(sd->status.account_id, sd->status.hom_id);
	if( sd->status.mer_id > 0 )
		intif->mercenary_request(sd->status.mer_id, sd->status.char_id);
	if( sd->status.ele_id > 0 )
		intif->elemental_request(sd->status.ele_id, sd->status.char_id);

	map->addiddb(&sd->bl);
	map->delnickdb(sd->status.char_id, sd->status.name);
	if (!chrif->auth_finished(sd))
		ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);

	pc->load_combo(sd);

	status_calc_pc(sd,SCO_FIRST|SCO_FORCE);
	chrif->scdata_request(sd->status.account_id, sd->status.char_id);

	intif->Mail_requestinbox(sd->status.char_id, 0); // MAIL SYSTEM - Request Mail Inbox
	intif->request_questlog(sd);

	if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
		sd->state.connect_new = 1;
		clif->pLoadEndAck(sd->fd, sd);
	}

	if (pc_isinvisible(sd)) {
		sd->vd.class_ = INVISIBLE_CLASS;
		clif->message(sd->fd, msg_sd(sd,11)); // Invisible: On
		// decrement the number of pvp players on the map
		map->list[sd->bl.m].users_pvp--;

		if( map->list[sd->bl.m].flag.pvp && !map->list[sd->bl.m].flag.pvp_nocalcrank && sd->pvp_timer != INVALID_TIMER ) {// unregister the player for ranking
			timer->delete( sd->pvp_timer, pc->calc_pvprank_timer );
			sd->pvp_timer = INVALID_TIMER;
		}
		clif->changeoption(&sd->bl);
	}

	if( npc->motd ) /* [Ind/Hercules] */
		script->run(npc->motd->u.scr.script, 0, sd->bl.id, npc->fake_nd->bl.id);

	return 1;
}

int pc_calc_skillpoint(struct map_session_data* sd) {
	int  i,inf2,skill_point=0;

	nullpo_ret(sd);

	for (i = 1; i < MAX_SKILL; i++) {
		int skill_lv = pc->checkskill2(sd,i);
		if (skill_lv > 0) {
			inf2 = skill->dbs->db[i].inf2;
			if((!(inf2&INF2_QUEST_SKILL) || battle_config.quest_skill_learn) &&
				!(inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL|INF2_GUILD_SKILL)) //Do not count wedding/link skills. [Skotlex]
				) {
				if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
					skill_point += skill_lv;
				else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
					skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
			}
		}
	}

	return skill_point;
}

/*==========================================
 * Calculation of skill level.
 *------------------------------------------*/
int pc_calc_skilltree(struct map_session_data *sd)
{
	int i,id=0,flag;
	int c=0;

	nullpo_ret(sd);
	i = pc->calc_skilltree_normalize_job(sd);
	c = pc->mapid2jobid(i, sd->status.sex);
	if( c == -1 )
	{ //Unable to normalize job??
		ShowError("pc_calc_skilltree: Unable to normalize job %d for character %s (%d:%d)\n", i, sd->status.name, sd->status.account_id, sd->status.char_id);
		return 1;
	}
	c = pc->class2idx(c);

	for( i = 0; i < MAX_SKILL; i++ ) {
		if( sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
			sd->status.skill[i].id = 0; //First clear skills.
		/* permanent skills that must be re-checked */
		if( sd->status.skill[i].flag == SKILL_FLAG_PERMANENT ) {
			switch( skill->dbs->db[i].nameid ) {
				case NV_TRICKDEAD:
					if( (sd->class_&(MAPID_BASEMASK|JOBL_2)) != MAPID_NOVICE ) {
							sd->status.skill[i].id = 0;
							sd->status.skill[i].lv = 0;
							sd->status.skill[i].flag = 0;
					}
					break;
			}
		}
	}

	for( i = 0; i < MAX_SKILL; i++ ) {
		if( sd->status.skill[i].flag != SKILL_FLAG_PERMANENT && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED && sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED )
		{ // Restore original level of skills after deleting earned skills.
			sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
			sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
		}

		if( sd->sc.count && sd->sc.data[SC_SOULLINK] && sd->sc.data[SC_SOULLINK]->val2 == SL_BARDDANCER && skill->dbs->db[i].nameid >= DC_HUMMING && skill->dbs->db[i].nameid <= DC_SERVICEFORYOU )
		{ //Enable Bard/Dancer spirit linked skills.
			if( sd->status.sex )
			{ //Link dancer skills to bard.
				// i can be < 8?
				if( sd->status.skill[i-8].lv < 10 )
					continue;
				sd->status.skill[i].id = skill->dbs->db[i].nameid;
				sd->status.skill[i].lv = sd->status.skill[i-8].lv; // Set the level to the same as the linking skill
				sd->status.skill[i].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
			} else { //Link bard skills to dancer.
				if( sd->status.skill[i].lv < 10 )
					continue;
				// i can be < 8?
				sd->status.skill[i-8].id = skill->dbs->db[i-8].nameid;
				sd->status.skill[i-8].lv = sd->status.skill[i].lv; // Set the level to the same as the linking skill
				sd->status.skill[i-8].flag = SKILL_FLAG_TEMPORARY; // Tag it as a non-savable, non-uppable, bonus skill
			}
		}
	}

	if( pc_has_permission(sd, PC_PERM_ALL_SKILL) ) {
		for( i = 0; i < MAX_SKILL; i++ ) {
			switch(skill->dbs->db[i].nameid) {
				/**
				 * Dummy skills must be added here otherwise they'll be displayed in the,
				 * skill tree and since they have no icons they'll give resource errors
				 **/
				case SM_SELFPROVOKE:
				case AB_DUPLELIGHT_MELEE:
				case AB_DUPLELIGHT_MAGIC:
				case WL_CHAINLIGHTNING_ATK:
				case WL_TETRAVORTEX_FIRE:
				case WL_TETRAVORTEX_WATER:
				case WL_TETRAVORTEX_WIND:
				case WL_TETRAVORTEX_GROUND:
				case WL_SUMMON_ATK_FIRE:
				case WL_SUMMON_ATK_WIND:
				case WL_SUMMON_ATK_WATER:
				case WL_SUMMON_ATK_GROUND:
				case LG_OVERBRAND_BRANDISH:
				case LG_OVERBRAND_PLUSATK:
					continue;
				default:
					break;
			}
			if( skill->dbs->db[i].inf2&(INF2_NPC_SKILL|INF2_GUILD_SKILL) )
				continue; //Only skills you can't have are npc/guild ones
			if( skill->dbs->db[i].max > 0 )
				sd->status.skill[i].id = skill->dbs->db[i].nameid;
		}
		return 0;
	}

	do {
		flag = 0;
		for (i = 0; i < MAX_SKILL_TREE && (id = pc->skill_tree[c][i].id) > 0; i++) {
			int idx = pc->skill_tree[c][i].idx;
			bool satisfied = true;
			if (sd->status.skill[idx].id > 0)
				continue; //Skill already known.

			if (!battle_config.skillfree) {
				int j;
				for (j = 0; j < VECTOR_LENGTH(pc->skill_tree[c][i].need); j++) {
					struct skill_tree_requirement *req = &VECTOR_INDEX(pc->skill_tree[c][i].need, j);
					int level;
					if (sd->status.skill[req->idx].id == 0
					 || sd->status.skill[req->idx].flag == SKILL_FLAG_TEMPORARY
					 || sd->status.skill[req->idx].flag == SKILL_FLAG_PLAGIARIZED)
						level = 0; //Not learned.
					else if (sd->status.skill[req->idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
						level = sd->status.skill[req->idx].flag - SKILL_FLAG_REPLACED_LV_0;
					else
						level = pc->checkskill2(sd, req->idx);
					if (level < req->lv) {
						satisfied = false;
						break;
					}
				}
				if (sd->status.job_level < pc->skill_tree[c][i].joblv) {
					int jobid = pc->mapid2jobid(sd->class_, sd->status.sex); // need to get its own skilltree
					if (jobid > -1) {
						if (!pc->skill_tree[pc->class2idx(jobid)][i].inherited)
							satisfied = false; // job level requirement wasn't satisfied
					} else {
						satisfied = false;
					}
				}
			}
			if (satisfied) {
				int inf2 = skill->dbs->db[idx].inf2;

				if(!sd->status.skill[idx].lv && (
					(inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
					inf2&INF2_WEDDING_SKILL ||
					(inf2&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SOULLINK])
				))
					continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.

				sd->status.skill[idx].id = id;

				if(inf2&INF2_SPIRIT_SKILL) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
					sd->status.skill[idx].lv = 1; // need to manually specify a skill level
					sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
				}
				flag = 1; // skill list has changed, perform another pass
			}
		}
	} while(flag);

	//
	if( c > 0 && (sd->class_&MAPID_UPPERMASK) == MAPID_TAEKWON && sd->status.base_level >= 90 && sd->status.skill_point == 0 && pc->famerank(sd->status.char_id, MAPID_TAEKWON) )
	{
		/* Taekwon Ranger Bonus Skill Tree
		============================================
		- Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
		- (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
		- (sd->status.skill_point == 0) to wait until all skill points are asigned to avoid problems with Job Change quest. */

		for( i = 0; i < MAX_SKILL_TREE && (id = pc->skill_tree[c][i].id) > 0; i++ ) {
			int idx = pc->skill_tree[c][i].idx;
			if( (skill->dbs->db[idx].inf2&(INF2_QUEST_SKILL|INF2_WEDDING_SKILL)) )
				continue; //Do not include Quest/Wedding skills.

			if( sd->status.skill[idx].id == 0 ) {
				sd->status.skill[idx].id = id;
				sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
			} else if( id != NV_BASIC ) {
				sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Remember original level
			}

			sd->status.skill[idx].lv = skill->tree_get_max(id, sd->status.class_);
		}
	}

	return 0;
}

//Checks if you can learn a new skill after having leveled up a skill.
void pc_check_skilltree(struct map_session_data *sd, int skill_id)
{
	int i,id=0,flag;
	int c=0;

	if(battle_config.skillfree)
		return; //Function serves no purpose if this is set

	nullpo_retv(sd);
	i = pc->calc_skilltree_normalize_job(sd);
	c = pc->mapid2jobid(i, sd->status.sex);
	if (c == -1) { //Unable to normalize job??
		ShowError("pc_check_skilltree: Unable to normalize job %d for character %s (%d:%d)\n", i, sd->status.name, sd->status.account_id, sd->status.char_id);
		return;
	}
	c = pc->class2idx(c);
	do {
		flag = 0;
		for (i = 0; i < MAX_SKILL_TREE && (id = pc->skill_tree[c][i].id) > 0; i++) {
			int j, idx = pc->skill_tree[c][i].idx;
			bool satisfied = true;

			if (sd->status.skill[idx].id) //Already learned
				continue;

			for (j = 0; j < VECTOR_LENGTH(pc->skill_tree[c][i].need); j++) {
				struct skill_tree_requirement *req = &VECTOR_INDEX(pc->skill_tree[c][i].need, j);
				int level;
				if (sd->status.skill[req->idx].id == 0
				 || sd->status.skill[req->idx].flag == SKILL_FLAG_TEMPORARY
				 || sd->status.skill[req->idx].flag == SKILL_FLAG_PLAGIARIZED)
					level = 0; //Not learned.
				else if (sd->status.skill[req->idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
					level = sd->status.skill[req->idx].flag - SKILL_FLAG_REPLACED_LV_0;
				else
					level = pc->checkskill2(sd,req->idx);
				if (level < req->lv) {
					satisfied = false;
					break;
				}
			}
			if (!satisfied)
				continue;

			if (sd->status.job_level < pc->skill_tree[c][i].joblv) {
				int jobid = pc->mapid2jobid(sd->class_, sd->status.sex); // need to get its own skilltree
				if (jobid > -1) {
					if (!pc->skill_tree[pc->class2idx(jobid)][i].inherited)
						continue;
				} else {
					continue;
				}
			}

			j = skill->dbs->db[idx].inf2;
			if( !sd->status.skill[idx].lv && (
				(j&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
				j&INF2_WEDDING_SKILL ||
				(j&INF2_SPIRIT_SKILL && !sd->sc.data[SC_SOULLINK])
			) )
				continue; //Cannot be learned via normal means.

			sd->status.skill[idx].id = id;

			flag = 1;
		}
	} while(flag);
}

// Make sure all the skills are in the correct condition
// before persisting to the backend.. [MouseJstr]
int pc_clean_skilltree(struct map_session_data *sd)
{
	int i;
	nullpo_ret(sd);
	for (i = 0; i < MAX_SKILL; i++){
		if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
			sd->status.skill[i].id = 0;
			sd->status.skill[i].lv = 0;
			sd->status.skill[i].flag = 0;
		} else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0) {
			sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
			sd->status.skill[i].flag = 0;
		}
	}

	return 0;
}

int pc_calc_skilltree_normalize_job(struct map_session_data *sd)
{
	int skill_point, novice_skills;
	int c;

	nullpo_ret(sd);
	c = sd->class_;
	if (!battle_config.skillup_limit || pc_has_permission(sd, PC_PERM_ALL_SKILL))
		return c;

	skill_point = pc->calc_skillpoint(sd);

	novice_skills = pc->max_level[pc->class2idx(JOB_NOVICE)][1] - 1;

	sd->sktree.second = sd->sktree.third = 0;

	// limit 1st class and above to novice job levels
	if(skill_point < novice_skills) {
		c = MAPID_NOVICE;
	}
	// limit 2nd class and above to first class job levels (super novices are exempt)
	else if ((sd->class_&JOBL_2) && (sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
	{
		// regenerate change_level_2nd
		if (!sd->change_level_2nd)
		{
			if (sd->class_&JOBL_THIRD)
			{
				// if neither 2nd nor 3rd jobchange levels are known, we have to assume a default for 2nd
				if (!sd->change_level_3rd)
					sd->change_level_2nd = pc->max_level[pc->class2idx(pc->mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex))][1]; // FIXME
				else
					sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
						- (sd->status.job_level - 1)
						- (sd->change_level_3rd - 1)
						- novice_skills;
			}
			else
			{
				sd->change_level_2nd = 1 + skill_point + sd->status.skill_point
						- (sd->status.job_level - 1)
						- novice_skills;

			}

			pc_setglobalreg (sd, script->add_str("jobchange_level"), sd->change_level_2nd);
		}

		if (skill_point < novice_skills + (sd->change_level_2nd - 1)) {
			c &= MAPID_BASEMASK;
			sd->sktree.second = ( novice_skills + (sd->change_level_2nd - 1) ) - skill_point;
		} else if(sd->class_&JOBL_THIRD) { // limit 3rd class to 2nd class/trans job levels
			// regenerate change_level_3rd
			if (!sd->change_level_3rd) {
					sd->change_level_3rd = 1 + skill_point + sd->status.skill_point
						- (sd->status.job_level - 1)
						- (sd->change_level_2nd - 1)
						- novice_skills;
					pc_setglobalreg (sd, script->add_str("jobchange_level_3rd"), sd->change_level_3rd);
			}

			if (skill_point < novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1)) {
				c &= MAPID_UPPERMASK;
				sd->sktree.third = (novice_skills + (sd->change_level_2nd - 1) + (sd->change_level_3rd - 1)) - skill_point;
			}
		}
	}

	// restore non-limiting flags
	c |= sd->class_&(JOBL_UPPER|JOBL_BABY);

	return c;
}

/*==========================================
 * Updates the weight status
 *------------------------------------------
 * 1: overweight 50%
 * 2: overweight 90%
 * It's assumed that SC_WEIGHTOVER50 and SC_WEIGHTOVER90 are only started/stopped here.
 */
int pc_updateweightstatus(struct map_session_data *sd)
{
	int old_overweight;
	int new_overweight;

	nullpo_retr(1, sd);

	old_overweight = (sd->sc.data[SC_WEIGHTOVER90]) ? 2 : (sd->sc.data[SC_WEIGHTOVER50]) ? 1 : 0;
	new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;

	if( old_overweight == new_overweight )
		return 0; // no change

	// stop old status change
	if( old_overweight == 1 )
		status_change_end(&sd->bl, SC_WEIGHTOVER50, INVALID_TIMER);
	else if( old_overweight == 2 )
		status_change_end(&sd->bl, SC_WEIGHTOVER90, INVALID_TIMER);

	// start new status change
	if( new_overweight == 1 )
		sc_start(NULL,&sd->bl, SC_WEIGHTOVER50, 100, 0, 0);
	else if( new_overweight == 2 )
		sc_start(NULL,&sd->bl, SC_WEIGHTOVER90, 100, 0, 0);

	// update overweight status
	sd->regen.state.overweight = new_overweight;

	return 0;
}

int pc_disguise(struct map_session_data *sd, int class_) {
	nullpo_ret(sd);
	if (class_ == -1 && sd->disguise == -1)
		return 0;
	if (class_ >= 0 && sd->disguise == class_)
		return 0;

	if (pc_isinvisible(sd)) { //Character is invisible. Stealth class-change. [Skotlex]
		sd->disguise = class_; //viewdata is set on uncloaking.
		return 2;
	}

	if (sd->bl.prev != NULL) {
		if( class_ == -1 && sd->disguise == sd->status.class_ ) {
			clif->clearunit_single(-sd->bl.id,CLR_OUTSIGHT,sd->fd);
		} else if ( class_ != sd->status.class_ ) {
			pc_stop_walking(sd, STOPWALKING_FLAG_NONE);
			clif->clearunit_area(&sd->bl, CLR_OUTSIGHT);
		}
	}

	if (class_ == -1) {
		sd->disguise = -1;
		class_ = sd->status.class_;
	} else
		sd->disguise = class_;

	status->set_viewdata(&sd->bl, class_);
	clif->changeoption(&sd->bl);
	// We need to update the client so it knows that a costume is being used
	if( sd->sc.option&OPTION_COSTUME ) {
		clif->changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
		clif->changelook(&sd->bl,LOOK_WEAPON,0);
		clif->changelook(&sd->bl,LOOK_SHIELD,0);
		clif->changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
	}

	if (sd->bl.prev != NULL) {
		clif->spawn(&sd->bl);
		if (class_ == sd->status.class_ && pc_iscarton(sd)) {
			//It seems the cart info is lost on undisguise.
			clif->cartlist(sd);
			clif->updatestatus(sd,SP_CARTINFO);
		}
		if (sd->chat_id != 0) {
			struct chat_data *cd = map->id2cd(sd->chat_id);

			if (cd != NULL)
				clif->dispchat(cd,0);
		}
	}
	return 1;
}

int pc_bonus_autospell(struct s_autospell *spell, int max, short id, short lv, short rate, short flag, short card_id)
{
	int i;

	if( !rate )
		return 0;

	nullpo_ret(spell);
	Assert_ret(max <= 15);  // autospell array size
	for( i = 0; i < max && spell[i].id; i++ )
	{
		if( (spell[i].card_id == card_id || spell[i].rate < 0 || rate < 0) && spell[i].id == id && spell[i].lv == lv )
		{
			if( !battle_config.autospell_stacking && spell[i].rate > 0 && rate > 0 )
				return 0;
			rate += spell[i].rate;
			break;
		}
	}
	if (i == max) {
		ShowWarning("pc_bonus: Reached max (%d) number of autospells per character!\n", max);
		return 0;
	}
	spell[i].id = id;
	spell[i].lv = lv;
	spell[i].rate = rate;
	//Auto-update flag value.
	if (!(flag&BF_RANGEMASK)) flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
	if (!(flag&BF_WEAPONMASK)) flag|=BF_WEAPON; //No attack type defined? Use weapon.
	if (!(flag&BF_SKILLMASK)) {
		if (flag&(BF_MAGIC|BF_MISC)) flag|=BF_SKILL; //These two would never trigger without BF_SKILL
		if (flag&BF_WEAPON) flag|=BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
	}
	spell[i].flag|= flag;
	spell[i].card_id = card_id;
	return 1;
}

int pc_bonus_autospell_onskill(struct s_autospell *spell, int max, short src_skill, short id, short lv, short rate, short card_id)
{
	int i;

	if( !rate )
		return 0;

	nullpo_ret(spell);
	Assert_ret(max <= 15);  // autospell array size
	for( i = 0; i < max && spell[i].id; i++ )
	{
		;  // each autospell works independently
	}

	if( i == max )
	{
		ShowWarning("pc_bonus: Reached max (%d) number of autospells per character!\n", max);
		return 0;
	}

	spell[i].flag    = src_skill;
	spell[i].id      = id;
	spell[i].lv      = lv;
	spell[i].rate    = rate;
	spell[i].card_id = card_id;
	return 1;
}

/**
 * Adds an AddEff/AddEff2/AddEffWhenHit bonus to a character.
 *
 * @param effect     Effects array to append to.
 * @param max        Size of the effect array.
 * @param id         Effect ID (@see enum sc_type).
 * @param rate       Trigger rate.
 * @param arrow_rate Trigger rate modifier for ranged attacks (adds to the base rate).
 * @param flag       Trigger flags (@see enum auto_trigger_flag).
 * @param duration   Fixed (non-reducible) duration in ms. If 0, uses the default (reducible) duration of the given effect.
 * @retval 1 on success.
 * @retval 0 on failure.
 */
int pc_bonus_addeff(struct s_addeffect* effect, int max, enum sc_type id, int16 rate, int16 arrow_rate, uint8 flag, uint16 duration)
{
	int i;

	nullpo_ret(effect);
	if (!(flag&(ATF_SHORT|ATF_LONG)))
		flag|=ATF_SHORT|ATF_LONG; //Default range: both
	if (!(flag&(ATF_TARGET|ATF_SELF)))
		flag|=ATF_TARGET; //Default target: enemy.
	if (!(flag&(ATF_WEAPON|ATF_MAGIC|ATF_MISC)))
		flag|=ATF_WEAPON; //Default type: weapon.

	for (i = 0; i < max && effect[i].flag; i++) {
		// Update existing effect if any.
		if (effect[i].id == id && effect[i].flag == flag && effect[i].duration == duration) {
			effect[i].rate += rate;
			effect[i].arrow_rate += arrow_rate;
			return 1;
		}
	}
	if (i == max) {
		ShowWarning("pc_bonus: Reached max (%d) number of add effects per character!\n", max);
		return 0;
	}
	effect[i].id = id;
	effect[i].rate = rate;
	effect[i].arrow_rate = arrow_rate;
	effect[i].flag = flag;
	effect[i].duration = duration;
	return 1;
}

int pc_bonus_addeff_onskill(struct s_addeffectonskill* effect, int max, enum sc_type id, short rate, short skill_id, unsigned char target) {
	int i;

	nullpo_ret(effect);
	for( i = 0; i < max && effect[i].skill; i++ ) {
		if( effect[i].id == id && effect[i].skill == skill_id && effect[i].target == target ) {
			effect[i].rate += rate;
			return 1;
		}
	}
	if( i == max ) {
		ShowWarning("pc_bonus: Reached max (%d) number of add effects on skill per character!\n", max);
		return 0;
	}
	effect[i].id = id;
	effect[i].rate = rate;
	effect[i].skill = skill_id;
	effect[i].target = target;
	return 1;
}

int pc_bonus_item_drop(struct s_add_drop *drop, const short max, short id, short group, int race_mask, int rate) {
	int i;

	nullpo_ret(drop);
	//Apply config rate adjustment settings.
	if (rate >= 0) { //Absolute drop.
		if (battle_config.item_rate_adddrop != 100)
			rate = rate*battle_config.item_rate_adddrop/100;
		if (rate < battle_config.item_drop_adddrop_min)
			rate = battle_config.item_drop_adddrop_min;
		else if (rate > battle_config.item_drop_adddrop_max)
			rate = battle_config.item_drop_adddrop_max;
	} else { //Relative drop, max/min limits are applied at drop time.
		if (battle_config.item_rate_adddrop != 100)
			rate = rate*battle_config.item_rate_adddrop/100;
		if (rate > -1)
			rate = -1;
	}
	for(i = 0; i < max && (drop[i].id || drop[i].group); i++) {
		if (((id && drop[i].id == id) || (group && drop[i].group == group))
		 && race_mask != RCMASK_NONE
		) {
			drop[i].race |= race_mask;
			if (drop[i].rate > 0 && rate > 0) {
				//Both are absolute rates.
				if (drop[i].rate < rate)
					drop[i].rate = rate;
			} else
			if(drop[i].rate < 0 && rate < 0) {
				//Both are relative rates.
				if (drop[i].rate > rate)
					drop[i].rate = rate;
			} else if (rate < 0) //Give preference to relative rate.
					drop[i].rate = rate;
			return 1;
		}
	}
	if(i == max) {
		ShowWarning("pc_bonus: Reached max (%d) number of added drops per character!\n", max);
		return 0;
	}
	drop[i].id = id;
	drop[i].group = group;
	drop[i].race |= race_mask;
	drop[i].rate = rate;
	return 1;
}

int pc_addautobonus(struct s_autobonus *bonus,char max,const char *bonus_script,short rate,unsigned int dur,short flag,const char *other_script,unsigned short pos,bool onskill) {
	int i;

	nullpo_ret(bonus);
	nullpo_ret(bonus_script);
	ARR_FIND(0, max, i, bonus[i].rate == 0);
	if( i == max )
	{
		ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", max);
		return 0;
	}

	if( !onskill )
	{
		if( !(flag&BF_RANGEMASK) )
			flag|=BF_SHORT|BF_LONG; //No range defined? Use both.
		if( !(flag&BF_WEAPONMASK) )
			flag|=BF_WEAPON; //No attack type defined? Use weapon.
		if( !(flag&BF_SKILLMASK) )
		{
			if( flag&(BF_MAGIC|BF_MISC) )
				flag|=BF_SKILL; //These two would never trigger without BF_SKILL
			if( flag&BF_WEAPON )
				flag|=BF_NORMAL|BF_SKILL;
		}
	}

	bonus[i].rate = rate;
	bonus[i].duration = dur;
	bonus[i].active = INVALID_TIMER;
	bonus[i].atk_type = flag;
	bonus[i].pos = pos;
	bonus[i].bonus_script = aStrdup(bonus_script);
	bonus[i].other_script = other_script?aStrdup(other_script):NULL;
	return 1;
}

int pc_delautobonus(struct map_session_data* sd, struct s_autobonus *autobonus,char max,bool restore)
{
	int i;
	nullpo_ret(sd);
	nullpo_ret(autobonus);

	for( i = 0; i < max; i++ )
	{
		if( autobonus[i].active != INVALID_TIMER )
		{
			if( restore && sd->state.autobonus&autobonus[i].pos )
			{
				if( autobonus[i].bonus_script )
				{
					int j;
					ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->status.inventory[sd->equip_index[j]].equip == autobonus[i].pos );
					if( j < EQI_MAX )
						script->run_autobonus(autobonus[i].bonus_script,sd->bl.id,sd->equip_index[j]);
				}
				continue;
			}
			else
			{ // Logout / Unequipped an item with an activated bonus
				timer->delete(autobonus[i].active,pc->endautobonus);
				autobonus[i].active = INVALID_TIMER;
			}
		}

		if( autobonus[i].bonus_script ) aFree(autobonus[i].bonus_script);
		if( autobonus[i].other_script ) aFree(autobonus[i].other_script);
		autobonus[i].bonus_script = autobonus[i].other_script = NULL;
		autobonus[i].rate = autobonus[i].atk_type = autobonus[i].duration = autobonus[i].pos = 0;
		autobonus[i].active = INVALID_TIMER;
	}

	return 0;
}

int pc_exeautobonus(struct map_session_data *sd,struct s_autobonus *autobonus)
{
	nullpo_ret(sd);
	nullpo_ret(autobonus);

	if( autobonus->other_script )
	{
		int j;
		ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->status.inventory[sd->equip_index[j]].equip == autobonus->pos );
		if( j < EQI_MAX )
			script->run_autobonus(autobonus->other_script,sd->bl.id,sd->equip_index[j]);
	}

	autobonus->active = timer->add(timer->gettick()+autobonus->duration, pc->endautobonus, sd->bl.id, (intptr_t)autobonus);
	sd->state.autobonus |= autobonus->pos;
	status_calc_pc(sd,SCO_NONE);

	return 0;
}

int pc_endautobonus(int tid, int64 tick, int id, intptr_t data) {
	struct map_session_data *sd = map->id2sd(id);
	struct s_autobonus *autobonus = (struct s_autobonus *)data;

	nullpo_ret(sd);
	nullpo_ret(autobonus);

	autobonus->active = INVALID_TIMER;
	sd->state.autobonus &= ~autobonus->pos;
	status_calc_pc(sd,SCO_NONE);
	return 0;
}

int pc_bonus_addele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
{
	int i;
	struct weapon_data* wd;

	nullpo_ret(sd);
	wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);

	ARR_FIND(0, MAX_PC_BONUS, i, wd->addele2[i].rate == 0);

	if (i == MAX_PC_BONUS)
	{
		ShowWarning("pc_addele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
		return 0;
	}

	if (!(flag&BF_RANGEMASK))
		flag |= BF_SHORT|BF_LONG;
	if (!(flag&BF_WEAPONMASK))
		flag |= BF_WEAPON;
	if (!(flag&BF_SKILLMASK))
	{
		if (flag&(BF_MAGIC|BF_MISC))
			flag |= BF_SKILL;
		if (flag&BF_WEAPON)
			flag |= BF_NORMAL|BF_SKILL;
	}

	wd->addele2[i].ele = ele;
	wd->addele2[i].rate = rate;
	wd->addele2[i].flag = flag;

	return 0;
}

int pc_bonus_subele(struct map_session_data* sd, unsigned char ele, short rate, short flag)
{
	int i;

	nullpo_ret(sd);
	ARR_FIND(0, MAX_PC_BONUS, i, sd->subele2[i].rate == 0);

	if (i == MAX_PC_BONUS)
	{
		ShowWarning("pc_subele: Reached max (%d) possible bonuses for this player.\n", MAX_PC_BONUS);
		return 0;
	}

	if (!(flag&BF_RANGEMASK))
		flag |= BF_SHORT|BF_LONG;
	if (!(flag&BF_WEAPONMASK))
		flag |= BF_WEAPON;
	if (!(flag&BF_SKILLMASK))
	{
		if (flag&(BF_MAGIC|BF_MISC))
			flag |= BF_SKILL;
		if (flag&BF_WEAPON)
			flag |= BF_NORMAL|BF_SKILL;
	}

	sd->subele2[i].ele = ele;
	sd->subele2[i].rate = rate;
	sd->subele2[i].flag = flag;

	return 0;
}

/**
 * Loops through the fields in a race bitmask (enum RaceMask => enum Race)
 *
 * To be used in pc_bonus functions with races represented in array form.
 */
#define BONUS_FOREACH_RCARRAY_FROMMASK(loop_counter, mask) \
	for ((loop_counter) = RC_FORMLESS; (loop_counter) < RC_MAX; ++(loop_counter)) \
		if (((mask) & 1<<(loop_counter)) == RCMASK_NONE) { \
			continue; \
		} else

/*==========================================
 * Add a bonus(type) to player sd
 *------------------------------------------*/
int pc_bonus(struct map_session_data *sd,int type,int val) {
	struct status_data *bst;
	int bonus;
	int i;
	nullpo_ret(sd);

	bst = &sd->base_status;

	switch(type){
		case SP_STR:
		case SP_AGI:
		case SP_VIT:
		case SP_INT:
		case SP_DEX:
		case SP_LUK:
			if(sd->state.lr_flag != 2)
				sd->param_bonus[type-SP_STR]+=val;
			break;
		case SP_ATK1:
			if(!sd->state.lr_flag) {
				bonus = bst->rhw.atk + val;
				bst->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
			}
			else if(sd->state.lr_flag == 1) {
				bonus = bst->lhw.atk + val;
				bst->lhw.atk =  cap_value(bonus, 0, USHRT_MAX);
			}
			break;
		case SP_ATK2:
			if(!sd->state.lr_flag) {
				bonus = bst->rhw.atk2 + val;
				bst->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
			}
			else if(sd->state.lr_flag == 1) {
				bonus = bst->lhw.atk2 + val;
				bst->lhw.atk2 =  cap_value(bonus, 0, USHRT_MAX);
			}
			break;
		case SP_BASE_ATK:
			if(sd->state.lr_flag != 2) {
#ifdef RENEWAL
				bst->equip_atk += val;
#else
				bonus = bst->batk + val;
				bst->batk = cap_value(bonus, 0, USHRT_MAX);
#endif
			}
			break;
		case SP_DEF1:
			if(sd->state.lr_flag != 2) {
				bonus = bst->def + val;
	#ifdef RENEWAL
				bst->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
	#else
				bst->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
	#endif
			}
			break;
		case SP_DEF2:
			if(sd->state.lr_flag != 2) {
				bonus = bst->def2 + val;
				bst->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
			}
			break;
		case SP_MDEF1:
			if(sd->state.lr_flag != 2) {
				bonus = bst->mdef + val;
	#ifdef RENEWAL
				bst->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
	#else
				bst->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
	#endif
				if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
					sd->bonus.shieldmdef += bonus;
				}
			}
			break;
		case SP_MDEF2:
			if(sd->state.lr_flag != 2) {
				bonus = bst->mdef2 + val;
				bst->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
			}
			break;
		case SP_HIT:
			if(sd->state.lr_flag != 2) {
				bonus = bst->hit + val;
				bst->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
			} else
				sd->bonus.arrow_hit+=val;
			break;
		case SP_FLEE1:
			if(sd->state.lr_flag != 2) {
				bonus = bst->flee + val;
				bst->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
			}
			break;
		case SP_FLEE2:
			if(sd->state.lr_flag != 2) {
				bonus = bst->flee2 + val*10;
				bst->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
			}
			break;
		case SP_CRITICAL:
			if(sd->state.lr_flag != 2) {
				bonus = bst->cri + val*10;
				bst->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
			} else
				sd->bonus.arrow_cri += val*10;
			break;
		case SP_ATKELE:
			if(val >= ELE_MAX) {
				ShowError("pc_bonus: SP_ATKELE: Invalid element %d\n", val);
				break;
			}
			switch (sd->state.lr_flag) {
			case 2:
				switch (sd->status.weapon) {
					case W_BOW:
					case W_REVOLVER:
					case W_RIFLE:
					case W_GATLING:
					case W_SHOTGUN:
					case W_GRENADE:
						//Become weapon element.
						bst->rhw.ele=val;
						break;
					default: //Become arrow element.
						sd->bonus.arrow_ele=val;
						break;
				}
				break;
			case 1:
				bst->lhw.ele=val;
				break;
			default:
				bst->rhw.ele=val;
				break;
			}
			break;
		case SP_DEFELE:
			if(val >= ELE_MAX) {
				ShowError("pc_bonus: SP_DEFELE: Invalid element %d\n", val);
				break;
			}
			if(sd->state.lr_flag != 2)
				bst->def_ele=val;
			break;
		case SP_MAXHP:
			if(sd->state.lr_flag == 2)
				break;
			val += (int)bst->max_hp;
			//Negative bonuses will underflow, this will be handled in status_calc_pc through casting
			//If this is called outside of status_calc_pc, you'd better pray they do not underflow and end with UINT_MAX max_hp.
			bst->max_hp = (unsigned int)val;
			break;
		case SP_MAXSP:
			if(sd->state.lr_flag == 2)
				break;
			val += (int)bst->max_sp;
			bst->max_sp = (unsigned int)val;
			break;
	#ifndef RENEWAL_CAST
		case SP_VARCASTRATE:
	#endif
		case SP_CASTRATE:
			if(sd->state.lr_flag != 2)
				sd->castrate+=val;
			break;
		case SP_MAXHPRATE:
			if(sd->state.lr_flag != 2)
				sd->hprate+=val;
			break;
		case SP_MAXSPRATE:
			if(sd->state.lr_flag != 2)
				sd->sprate+=val;
			break;
		case SP_SPRATE:
			if(sd->state.lr_flag != 2)
				sd->dsprate+=val;
			break;
		case SP_ATTACKRANGE:
			switch (sd->state.lr_flag) {
			case 2:
				switch (sd->status.weapon) {
					case W_BOW:
					case W_REVOLVER:
					case W_RIFLE:
					case W_GATLING:
					case W_SHOTGUN:
					case W_GRENADE:
						bst->rhw.range += val;
				}
				break;
			case 1:
				bst->lhw.range += val;
				break;
			default:
				bst->rhw.range += val;
				break;
			}
			break;
		case SP_SPEED_RATE: //Non stackable increase
			if(sd->state.lr_flag != 2)
				sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
			break;
		case SP_SPEED_ADDRATE: //Stackable increase
			if(sd->state.lr_flag != 2)
				sd->bonus.speed_add_rate -= val;
			break;
		case SP_ASPD: //Raw increase
			if(sd->state.lr_flag != 2)
				sd->bonus.aspd_add -= 10*val;
			break;
		case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
			if(sd->state.lr_flag != 2)
	#ifndef RENEWAL_ASPD
				bst->aspd_rate -= 10*val;
	#else
				bst->aspd_rate2 += val;
	#endif
			break;
		case SP_HP_RECOV_RATE:
			if(sd->state.lr_flag != 2)
				sd->hprecov_rate += val;
			break;
		case SP_SP_RECOV_RATE:
			if(sd->state.lr_flag != 2)
				sd->sprecov_rate += val;
			break;
		case SP_CRITICAL_DEF:
			if(sd->state.lr_flag != 2)
				sd->bonus.critical_def += val;
			break;
		case SP_NEAR_ATK_DEF:
			if(sd->state.lr_flag != 2)
				sd->bonus.near_attack_def_rate += val;
			break;
		case SP_LONG_ATK_DEF:
			if(sd->state.lr_flag != 2)
				sd->bonus.long_attack_def_rate += val;
			break;
		case SP_DOUBLE_RATE:
			if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
				sd->bonus.double_rate = val;
			break;
		case SP_DOUBLE_ADD_RATE:
			if(sd->state.lr_flag == 0)
				sd->bonus.double_add_rate += val;
			break;
		case SP_MATK_RATE:
			if(sd->state.lr_flag != 2)
				sd->matk_rate += val;
			break;
		case SP_IGNORE_DEF_ELE:
			if( (val >= ELE_MAX && val != ELE_ALL) || (val < ELE_NEUTRAL) ) {
				ShowError("pc_bonus: SP_IGNORE_DEF_ELE: Invalid element %d\n", val);
				break;
			}
			if ( val == ELE_ALL ) {
				for ( i = ELE_NEUTRAL; i < ELE_MAX; i++ ) {
					if(!sd->state.lr_flag)
						sd->right_weapon.ignore_def_ele |= 1<<i;
					else if(sd->state.lr_flag == 1)
						sd->left_weapon.ignore_def_ele |= 1<<i;
				}
			} else {
				if(!sd->state.lr_flag)
					sd->right_weapon.ignore_def_ele |= 1<<val;
				else if(sd->state.lr_flag == 1)
					sd->left_weapon.ignore_def_ele |= 1<<val;
			}
			break;
		case SP_IGNORE_DEF_RACE:
		{
			uint32 race_mask = map->race_id2mask(val);
			if (race_mask == RCMASK_NONE) {
				ShowWarning("pc_bonus: SP_IGNORE_DEF_RACE: Invalid Race (%d)\n", val);
				break;
			}
			if (!sd->state.lr_flag)
				sd->right_weapon.ignore_def_race |= race_mask;
			else if (sd->state.lr_flag == 1)
				sd->left_weapon.ignore_def_race |= race_mask;
		}
			break;
		case SP_ATK_RATE:
			if(sd->state.lr_flag != 2)
				sd->bonus.atk_rate += val;
			break;
		case SP_MAGIC_ATK_DEF:
			if(sd->state.lr_flag != 2)
				sd->bonus.magic_def_rate += val;
			break;
		case SP_MISC_ATK_DEF:
			if(sd->state.lr_flag != 2)
				sd->bonus.misc_def_rate += val;
			break;
		case SP_IGNORE_MDEF_RATE:
			if (sd->state.lr_flag != 2) {
				// Decomposed RC_ALL:
				sd->ignore_mdef[RC_NONBOSS] += val;
				sd->ignore_mdef[RC_BOSS] += val;
			}
			break;
		case SP_IGNORE_MDEF_ELE:
			if( (val >= ELE_MAX && val != ELE_ALL) || (val < ELE_NEUTRAL) ) {
				ShowError("pc_bonus: SP_IGNORE_MDEF_ELE: Invalid element %d\n", val);
				break;
			}
			if (sd->state.lr_flag != 2) {
				if ( val == ELE_ALL ) {
					for ( i = ELE_NEUTRAL; i < ELE_MAX; i++ ) {
						sd->bonus.ignore_mdef_ele |= 1<<i;
					}
				} else {
					sd->bonus.ignore_mdef_ele |= 1<<val;
				}
			}
			break;
		case SP_IGNORE_MDEF_RACE:
		{
			uint32 race_mask = map->race_id2mask(val);
			if (race_mask == RCMASK_NONE) {
				ShowWarning("pc_bonus: SP_IGNORE_MDEF_RACE: Invalid Race (%d)\n", val);
				break;
			}
			if (sd->state.lr_flag != 2) {
				sd->bonus.ignore_mdef_race |= race_mask;
			}
		}
			break;
		case SP_PERFECT_HIT_RATE:
			if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
				sd->bonus.perfect_hit = val;
			break;
		case SP_PERFECT_HIT_ADD_RATE:
			if(sd->state.lr_flag != 2)
				sd->bonus.perfect_hit_add += val;
			break;
		case SP_CRITICAL_RATE:
			if(sd->state.lr_flag != 2)
				sd->critical_rate+=val;
			break;
		case SP_DEF_RATIO_ATK_ELE:
			if( (val >= ELE_MAX && val != ELE_ALL) || (val < ELE_NEUTRAL) ) {
				ShowError("pc_bonus: SP_DEF_RATIO_ATK_ELE: Invalid element %d\n", val);
				break;
			}
			if ( val == ELE_ALL ) {
				for ( i = ELE_NEUTRAL; i < ELE_MAX; i++ ) {
					if(!sd->state.lr_flag)
						sd->right_weapon.def_ratio_atk_ele |= 1<<i;
					else if(sd->state.lr_flag == 1)
						sd->left_weapon.def_ratio_atk_ele |= 1<<i;
				}
			} else {
				if(!sd->state.lr_flag)
					sd->right_weapon.def_ratio_atk_ele |= 1<<val;
				else if(sd->state.lr_flag == 1)
					sd->left_weapon.def_ratio_atk_ele |= 1<<val;
			}
			break;
		case SP_DEF_RATIO_ATK_RACE:
		{
			uint32 race_mask = map->race_id2mask(val);
			if (race_mask == RCMASK_NONE) {
				ShowWarning("pc_bonus: SP_DEF_RATIO_ATK_RACE: Invalid Race (%d)\n", val);
				break;
			}
			if (!sd->state.lr_flag)
				sd->right_weapon.def_ratio_atk_race |= race_mask;
			else if (sd->state.lr_flag == 1)
				sd->left_weapon.def_ratio_atk_race |= race_mask;
		}
			break;
		case SP_HIT_RATE:
			if(sd->state.lr_flag != 2)
				sd->hit_rate += val;
			break;
		case SP_FLEE_RATE:
			if(sd->state.lr_flag != 2)
				sd->flee_rate += val;
			break;
		case SP_FLEE2_RATE:
			if(sd->state.lr_flag != 2)
				sd->flee2_rate += val;
			break;
		case SP_DEF_RATE:
			if(sd->state.lr_flag != 2)
				sd->def_rate += val;
			break;
		case SP_DEF2_RATE:
			if(sd->state.lr_flag != 2)
				sd->def2_rate += val;
			break;
		case SP_MDEF_RATE:
			if(sd->state.lr_flag != 2)
				sd->mdef_rate += val;
			break;
		case SP_MDEF2_RATE:
			if(sd->state.lr_flag != 2)
				sd->mdef2_rate += val;
			break;
		case SP_RESTART_FULL_RECOVER:
			if(sd->state.lr_flag != 2)
				sd->special_state.restart_full_recover = 1;
			break;
		case SP_NO_CASTCANCEL:
			if(sd->state.lr_flag != 2)
				sd->special_state.no_castcancel = 1;
			break;
		case SP_NO_CASTCANCEL2:
			if(sd->state.lr_flag != 2)
				sd->special_state.no_castcancel2 = 1;
			break;
		case SP_NO_SIZEFIX:
			if(sd->state.lr_flag != 2)
				sd->special_state.no_sizefix = 1;
			break;
		case SP_NO_MAGIC_DAMAGE:
			if(sd->state.lr_flag == 2)
				break;
			val+= sd->special_state.no_magic_damage;
			sd->special_state.no_magic_damage = cap_value(val,0,100);
			break;
		case SP_NO_WEAPON_DAMAGE:
			if(sd->state.lr_flag == 2)
				break;
			val+= sd->special_state.no_weapon_damage;
			sd->special_state.no_weapon_damage = cap_value(val,0,100);
			break;
		case SP_NO_MISC_DAMAGE:
			if(sd->state.lr_flag == 2)
				break;
			val+= sd->special_state.no_misc_damage;
			sd->special_state.no_misc_damage = cap_value(val,0,100);
			break;
		case SP_NO_GEMSTONE:
			if(sd->state.lr_flag != 2)
				sd->special_state.no_gemstone = 1;
			break;
		case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
			if(sd->state.lr_flag != 2) {
				sd->special_state.intravision = 1;
				clif->status_change(&sd->bl, SI_CLAIRVOYANCE, 1, 0, 0, 0, 0);
			}
			break;
		case SP_NO_KNOCKBACK:
			if(sd->state.lr_flag != 2)
				sd->special_state.no_knockback = 1;
			break;
		case SP_SPLASH_RANGE:
			if(sd->bonus.splash_range < val)
				sd->bonus.splash_range = val;
			break;
		case SP_SPLASH_ADD_RANGE:
			sd->bonus.splash_add_range += val;
			break;
		case SP_SHORT_WEAPON_DAMAGE_RETURN:
			if(sd->state.lr_flag != 2)
				sd->bonus.short_weapon_damage_return += val;
			break;
		case SP_LONG_WEAPON_DAMAGE_RETURN:
			if(sd->state.lr_flag != 2)
				sd->bonus.long_weapon_damage_return += val;
			break;
		case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
			if(sd->state.lr_flag != 2)
				sd->bonus.magic_damage_return += val;
			break;
		case SP_ALL_STATS: // [Valaris]
			if(sd->state.lr_flag!=2) {
				sd->param_bonus[SP_STR-SP_STR]+=val;
				sd->param_bonus[SP_AGI-SP_STR]+=val;
				sd->param_bonus[SP_VIT-SP_STR]+=val;
				sd->param_bonus[SP_INT-SP_STR]+=val;
				sd->param_bonus[SP_DEX-SP_STR]+=val;
				sd->param_bonus[SP_LUK-SP_STR]+=val;
			}
			break;
		case SP_AGI_VIT: // [Valaris]
			if(sd->state.lr_flag!=2) {
				sd->param_bonus[SP_AGI-SP_STR]+=val;
				sd->param_bonus[SP_VIT-SP_STR]+=val;
			}
			break;
		case SP_AGI_DEX_STR: // [Valaris]
			if(sd->state.lr_flag!=2) {
				sd->param_bonus[SP_AGI-SP_STR]+=val;
				sd->param_bonus[SP_DEX-SP_STR]+=val;
				sd->param_bonus[SP_STR-SP_STR]+=val;
			}
			break;
		case SP_PERFECT_HIDE: // [Valaris]
			if(sd->state.lr_flag!=2)
				sd->special_state.perfect_hiding=1;
			break;
		case SP_UNBREAKABLE:
			if(sd->state.lr_flag!=2)
				sd->bonus.unbreakable += val;
			break;
		case SP_UNBREAKABLE_WEAPON:
			if(sd->state.lr_flag != 2)
				sd->bonus.unbreakable_equip |= EQP_WEAPON;
			break;
		case SP_UNBREAKABLE_ARMOR:
			if(sd->state.lr_flag != 2)
				sd->bonus.unbreakable_equip |= EQP_ARMOR;
			break;
		case SP_UNBREAKABLE_HELM:
			if(sd->state.lr_flag != 2)
				sd->bonus.unbreakable_equip |= EQP_HELM;
			break;
		case SP_UNBREAKABLE_SHIELD:
			if(sd->state.lr_flag != 2)
				sd->bonus.unbreakable_equip |= EQP_SHIELD;
			break;
		case SP_UNBREAKABLE_GARMENT:
			if(sd->state.lr_flag != 2)
				sd->bonus.unbreakable_equip |= EQP_GARMENT;
			break;
		case SP_UNBREAKABLE_SHOES:
			if(sd->state.lr_flag != 2)
				sd->bonus.unbreakable_equip |= EQP_SHOES;
			break;
		case SP_CLASSCHANGE: // [Valaris]
			if(sd->state.lr_flag !=2)
				sd->bonus.classchange=val;
			break;
		case SP_LONG_ATK_RATE:
			if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
				sd->bonus.long_attack_atk_rate+=val;
			break;
		case SP_BREAK_WEAPON_RATE:
			if(sd->state.lr_flag != 2)
				sd->bonus.break_weapon_rate+=val;
			break;
		case SP_BREAK_ARMOR_RATE:
			if(sd->state.lr_flag != 2)
				sd->bonus.break_armor_rate+=val;
			break;
		case SP_ADD_STEAL_RATE:
			if(sd->state.lr_flag != 2)
				sd->bonus.add_steal_rate+=val;
			break;
		case SP_DELAYRATE:
			if(sd->state.lr_flag != 2)
				sd->delayrate+=val;
			break;
		case SP_CRIT_ATK_RATE:
			if(sd->state.lr_flag != 2)
				sd->bonus.crit_atk_rate += val;
			break;
		case SP_NO_REGEN:
			if(sd->state.lr_flag != 2)
				sd->regen.state.block|=val;
			break;
		case SP_UNSTRIPABLE_WEAPON:
			if(sd->state.lr_flag != 2)
				sd->bonus.unstripable_equip |= EQP_WEAPON;
			break;
		case SP_UNSTRIPABLE:
		case SP_UNSTRIPABLE_ARMOR:
			if(sd->state.lr_flag != 2)
				sd->bonus.unstripable_equip |= EQP_ARMOR;
			break;
		case SP_UNSTRIPABLE_HELM:
			if(sd->state.lr_flag != 2)
				sd->bonus.unstripable_equip |= EQP_HELM;
			break;
		case SP_UNSTRIPABLE_SHIELD:
			if(sd->state.lr_flag != 2)
				sd->bonus.unstripable_equip |= EQP_SHIELD;
			break;
		case SP_HP_DRAIN_VALUE:
			if (sd->state.lr_flag == 0) {
				// Decomposed RC_ALL:
				sd->right_weapon.hp_drain[RC_NONBOSS].value += val;
				sd->right_weapon.hp_drain[RC_BOSS].value += val;
			} else if (sd->state.lr_flag == 1) {
				// Decomposed RC_ALL:
				sd->left_weapon.hp_drain[RC_NONBOSS].value += val;
				sd->left_weapon.hp_drain[RC_BOSS].value += val;
			}
			break;
		case SP_SP_DRAIN_VALUE:
			if (sd->state.lr_flag == 0) {
				// Decomposed RC_ALL:
				sd->right_weapon.sp_drain[RC_NONBOSS].value += val;
				sd->right_weapon.sp_drain[RC_BOSS].value += val;
			} else if (sd->state.lr_flag == 1) {
				// Decomposed RC_ALL:
				sd->left_weapon.sp_drain[RC_NONBOSS].value += val;
				sd->left_weapon.sp_drain[RC_BOSS].value += val;
			}
			break;
		case SP_SP_GAIN_VALUE:
			if(!sd->state.lr_flag)
				sd->bonus.sp_gain_value += val;
			break;
		case SP_HP_GAIN_VALUE:
			if(!sd->state.lr_flag)
				sd->bonus.hp_gain_value += val;
			break;
		case SP_MAGIC_SP_GAIN_VALUE:
			if(!sd->state.lr_flag)
				sd->bonus.magic_sp_gain_value += val;
			break;
		case SP_MAGIC_HP_GAIN_VALUE:
			if(!sd->state.lr_flag)
				sd->bonus.magic_hp_gain_value += val;
			break;
		case SP_ADD_HEAL_RATE:
			if(sd->state.lr_flag != 2)
				sd->bonus.add_heal_rate += val;
			break;
		case SP_ADD_HEAL2_RATE:
			if(sd->state.lr_flag != 2)
				sd->bonus.add_heal2_rate += val;
			break;
		case SP_ADD_ITEM_HEAL_RATE:
			if(sd->state.lr_flag != 2)
				sd->bonus.itemhealrate2 += val;
			break;
		case SP_EMATK:
			   if(sd->state.lr_flag != 2)
				   sd->bonus.ematk += val;
			   break;
		case SP_FIXCASTRATE:
			if(sd->state.lr_flag != 2)
				sd->bonus.fixcastrate -= val;
			break;
		case SP_ADD_FIXEDCAST:
			if(sd->state.lr_flag != 2)
				sd->bonus.add_fixcast += val;

			break;
	#ifdef RENEWAL_CAST
		case SP_VARCASTRATE:
			if(sd->state.lr_flag != 2)
				sd->bonus.varcastrate -= val;
			break;
		case SP_ADD_VARIABLECAST:
			if(sd->state.lr_flag != 2)
				sd->bonus.add_varcast += val;
			break;
	#endif
		case SP_ADD_MONSTER_DROP_CHAINITEM:
			if (sd->state.lr_flag != 2)
				pc->bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, val, map->race_id2mask(RC_ALL), 10000);
		break;
		case SP_ADDMAXWEIGHT:
			if (sd->state.lr_flag != 2)
				sd->max_weight += val;
			break;
		default:
			ShowWarning("pc_bonus: unknown type %d %d !\n",type,val);
			Assert_report(0);
			break;
	}
	return 0;
}

/*==========================================
 * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
 *------------------------------------------*/
int pc_bonus2(struct map_session_data *sd,int type,int type2,int val)
{
	int i;

	nullpo_ret(sd);

	switch(type){
		case SP_ADDELE:
			if( (type2 >= ELE_MAX && type2 != ELE_ALL) || (type2 < ELE_NEUTRAL) ) {
				ShowError("pc_bonus2: SP_ADDELE: Invalid element %d\n", type2);
				break;
			}
			if ( type2 == ELE_ALL ) {
				for ( i = ELE_NEUTRAL; i < ELE_MAX; i++ ) {
					if ( !sd->state.lr_flag )
						sd->right_weapon.addele[i] += val;
					else if ( sd->state.lr_flag == 1 )
						sd->left_weapon.addele[i] += val;
					else if ( sd->state.lr_flag == 2 )
						sd->arrow_addele[i] += val;
				}
			} else {
				if(!sd->state.lr_flag)
					sd->right_weapon.addele[type2] += val;
				else if(sd->state.lr_flag == 1)
					sd->left_weapon.addele[type2] += val;
				else if(sd->state.lr_flag == 2)
					sd->arrow_addele[type2] += val;
			}
			break;
		case SP_ADDRACE:
		{
			uint32 race_mask = map->race_id2mask(type2);
			if (race_mask == RCMASK_NONE) {
				ShowWarning("pc_bonus2: SP_ADDRACE: Invalid Race (%d)\n", type2);
				break;
			}
			BONUS_FOREACH_RCARRAY_FROMMASK(i, race_mask) {
				if (sd->state.lr_flag == 0) {
					sd->right_weapon.addrace[i] += val;
				} else if (sd->state.lr_flag == 1) {
					sd->left_weapon.addrace[i] += val;
				} else if (sd->state.lr_flag == 2) {
					sd->arrow_addrace[i] += val;
				}
			}
		}
			break;
		case SP_ADDSIZE:
			if(!sd->state.lr_flag)
				sd->right_weapon.addsize[type2]+=val;
			else if(sd->state.lr_flag == 1)
				sd->left_weapon.addsize[type2]+=val;
			else if(sd->state.lr_flag == 2)
				sd->arrow_addsize[type2]+=val;
			break;
		case SP_SUBELE:
			if( (type2 >= ELE_MAX && type2 != ELE_ALL) || (type2 < ELE_NEUTRAL) ) {
				ShowError("pc_bonus2: SP_SUBELE: Invalid element %d\n", type2);
				break;
			}
			if(sd->state.lr_flag != 2) {
				if ( type2 == ELE_ALL ) {
					for ( i = ELE_NEUTRAL; i < ELE_MAX; i++ ){
						sd->subele[i] += val;
					}
				} else {
					sd->subele[type2] += val;
				}
			}
			break;
		case SP_SUBRACE:
		{
			uint32 race_mask = map->race_id2mask(type2);
			if (race_mask == RCMASK_NONE) {
				ShowWarning("pc_bonus2: SP_SUBRACE: Invalid Race (%d)\n", type2);
				break;
			}
			if (sd->state.lr_flag == 2)
				break;
			BONUS_FOREACH_RCARRAY_FROMMASK(i, race_mask) {
				sd->subrace[i] += val;
			}
		}
			break;
		case SP_ADDEFF:
			if (type2 > SC_MAX) {
				ShowWarning("pc_bonus2 (Add Effect): %d is not supported.\n", type2);
				break;
			}
			pc->bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
			                 sd->state.lr_flag!=2?val:0, sd->state.lr_flag==2?val:0, 0, 0);
			break;
		case SP_ADDEFF2:
			if (type2 > SC_MAX) {
				ShowWarning("pc_bonus2 (Add Effect2): %d is not supported.\n", type2);
				break;
			}
			pc->bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
			                 sd->state.lr_flag!=2?val:0, sd->state.lr_flag==2?val:0, ATF_SELF, 0);
			break;
		case SP_RESEFF:
			if (type2 < SC_COMMON_MIN || type2 > SC_COMMON_MAX) {
				ShowWarning("pc_bonus2 (Resist Effect): %d is not supported.\n", type2);
				break;
			}
			if(sd->state.lr_flag == 2)
				break;
			i = sd->reseff[type2-SC_COMMON_MIN]+val;
			sd->reseff[type2-SC_COMMON_MIN]= cap_value(i, 0, 10000);
			break;
		case SP_MAGIC_ADDELE:
			if( (type2 >= ELE_MAX && type2 != ELE_ALL) || (type2 < ELE_NEUTRAL) ) {
				ShowError("pc_bonus2: SP_MAGIC_ADDELE: Invalid element %d\n", type2);
				break;
			}
			if ( sd->state.lr_flag != 2 ) {
				if ( type2 == ELE_ALL ) {
					for ( i = ELE_NEUTRAL; i < ELE_MAX; i++ )
						sd->magic_addele[i] += val;
				} else {
					sd->magic_addele[type2] += val;
				}
			}
			break;
		case SP_MAGIC_ADDRACE:
		{
			uint32 race_mask = map->race_id2mask(type2);
			if (race_mask == RCMASK_NONE) {
				ShowWarning("pc_bonus2: SP_MAGIC_ADDRACE: Invalid Race (%d)\n", type2);
				break;
			}
			if (sd->state.lr_flag == 2)
				break;
			BONUS_FOREACH_RCARRAY_FROMMASK(i, race_mask) {
				sd->magic_addrace[i] += val;
			}
		}
			break;
		case SP_MAGIC_ADDSIZE:
			if(sd->state.lr_flag != 2)
				sd->magic_addsize[type2]+=val;
			break;
		case SP_MAGIC_ATK_ELE:
			if( (type2 >= ELE_MAX && type2 != ELE_ALL) || (type2 < ELE_NEUTRAL) ) {
				ShowError("pc_bonus2: SP_MAGIC_ATK_ELE: Invalid element %d\n", type2);
				break;
			}
			if ( sd->state.lr_flag != 2 ) {
				if ( type2 == ELE_ALL ) {
					for ( i = ELE_NEUTRAL; i < ELE_MAX; i++ )
						sd->magic_atk_ele[i] += val;
				} else {
					sd->magic_atk_ele[type2] += val;
				}
			}
			break;
		case SP_ADD_DAMAGE_CLASS:
			switch (sd->state.lr_flag) {
				case 0: //Right hand
					ARR_FIND(0, ARRAYLENGTH(sd->right_weapon.add_dmg), i, sd->right_weapon.add_dmg[i].rate == 0 || sd->right_weapon.add_dmg[i].class_ == type2);
					if (i == ARRAYLENGTH(sd->right_weapon.add_dmg)) {
						ShowWarning("pc_bonus2: Reached max (%d) number of add Class dmg bonuses per character!\n",
									ARRAYLENGTH(sd->right_weapon.add_dmg));
						break;
					}
					sd->right_weapon.add_dmg[i].class_ = type2;
					sd->right_weapon.add_dmg[i].rate += val;
					if (!sd->right_weapon.add_dmg[i].rate) { //Shift the rest of elements up.
						if( i != ARRAYLENGTH(sd->right_weapon.add_dmg) - 1 )
							memmove(&sd->right_weapon.add_dmg[i], &sd->right_weapon.add_dmg[i+1], sizeof(sd->right_weapon.add_dmg) - (i+1)*sizeof(sd->right_weapon.add_dmg[0]));
					}
					break;
				case 1: //Left hand
					ARR_FIND(0, ARRAYLENGTH(sd->left_weapon.add_dmg), i, sd->left_weapon.add_dmg[i].rate == 0 || sd->left_weapon.add_dmg[i].class_ == type2);
					if (i == ARRAYLENGTH(sd->left_weapon.add_dmg)) {
						ShowWarning("pc_bonus2: Reached max (%d) number of add Class dmg bonuses per character!\n",
									ARRAYLENGTH(sd->left_weapon.add_dmg));
						break;
					}
					sd->left_weapon.add_dmg[i].class_ = type2;
					sd->left_weapon.add_dmg[i].rate += val;
					if (!sd->left_weapon.add_dmg[i].rate) { //Shift the rest of elements up.
						if( i != ARRAYLENGTH(sd->left_weapon.add_dmg) - 1 )
							memmove(&sd->left_weapon.add_dmg[i], &sd->left_weapon.add_dmg[i+1], sizeof(sd->left_weapon.add_dmg) - (i+1)*sizeof(sd->left_weapon.add_dmg[0]));
					}
					break;
			}
			break;
		case SP_ADD_MAGIC_DAMAGE_CLASS:
			if(sd->state.lr_flag == 2)
				break;
			ARR_FIND(0, ARRAYLENGTH(sd->add_mdmg), i, sd->add_mdmg[i].rate == 0 || sd->add_mdmg[i].class_ == type2);
			if (i == ARRAYLENGTH(sd->add_mdmg)) {
				ShowWarning("pc_bonus2: Reached max (%d) number of add Class magic dmg bonuses per character!\n", ARRAYLENGTH(sd->add_mdmg));
				break;
			}
			sd->add_mdmg[i].class_ = type2;
			sd->add_mdmg[i].rate += val;
			if (!sd->add_mdmg[i].rate && i != ARRAYLENGTH(sd->add_mdmg) - 1) //Shift the rest of elements up.
				memmove(&sd->add_mdmg[i], &sd->add_mdmg[i+1], sizeof(sd->add_mdmg) - (i+1)*sizeof(sd->add_mdmg[0]));
			break;
		case SP_ADD_DEF_CLASS:
			if(sd->state.lr_flag == 2)
				break;
			ARR_FIND(0, ARRAYLENGTH(sd->add_def), i, sd->add_def[i].rate == 0 || sd->add_def[i].class_ == type2);
			if (i == ARRAYLENGTH(sd->add_def)) {
				ShowWarning("pc_bonus2: Reached max (%d) number of add Class def bonuses per character!\n", ARRAYLENGTH(sd->add_def));
				break;
			}
			sd->add_def[i].class_ = type2;
			sd->add_def[i].rate += val;
			if ( !sd->add_def[i].rate && i != ARRAYLENGTH(sd->add_def) - 1) //Shift the rest of elements up.
				memmove(&sd->add_def[i], &sd->add_def[i+1], sizeof(sd->add_def) - (i+1)*sizeof(sd->add_def[0]));
			break;
		case SP_ADD_MDEF_CLASS:
			if(sd->state.lr_flag == 2)
				break;
			ARR_FIND(0, ARRAYLENGTH(sd->add_mdef), i, sd->add_mdef[i].rate == 0 || sd->add_mdef[i].class_ == type2);
			if (i == ARRAYLENGTH(sd->add_mdef)) {
				ShowWarning("pc_bonus2: Reached max (%d) number of add Class mdef bonuses per character!\n", ARRAYLENGTH(sd->add_mdef));
				break;
			}
			sd->add_mdef[i].class_ = type2;
			sd->add_mdef[i].rate += val;
			if (!sd->add_mdef[i].rate && i != ARRAYLENGTH(sd->add_mdef) - 1) //Shift the rest of elements up.
				memmove(&sd->add_mdef[i], &sd->add_mdef[i+1], sizeof(sd->add_mdef) - (i+1)*sizeof(sd->add_mdef[0]));
			break;
		case SP_HP_DRAIN_RATE:
			if (sd->state.lr_flag == 0) {
				// Decomposed RC_ALL:
				sd->right_weapon.hp_drain[RC_NONBOSS].rate += type2;
				sd->right_weapon.hp_drain[RC_NONBOSS].per += val;
				sd->right_weapon.hp_drain[RC_BOSS].rate += type2;
				sd->right_weapon.hp_drain[RC_BOSS].per += val;
			} else if (sd->state.lr_flag == 1) {
				// Decomposed RC_ALL:
				sd->left_weapon.hp_drain[RC_NONBOSS].rate += type2;
				sd->left_weapon.hp_drain[RC_NONBOSS].per += val;
				sd->left_weapon.hp_drain[RC_BOSS].rate += type2;
				sd->left_weapon.hp_drain[RC_BOSS].per += val;
			}
			break;
		case SP_HP_DRAIN_VALUE:
			if (sd->state.lr_flag == 0) {
				// Decomposed RC_ALL:
				sd->right_weapon.hp_drain[RC_NONBOSS].value += type2;
				sd->right_weapon.hp_drain[RC_NONBOSS].type = val;
				sd->right_weapon.hp_drain[RC_BOSS].value += type2;
				sd->right_weapon.hp_drain[RC_BOSS].type = val;
			} else if (sd->state.lr_flag == 1) {
				// Decomposed RC_ALL:
				sd->left_weapon.hp_drain[RC_NONBOSS].value += type2;
				sd->left_weapon.hp_drain[RC_NONBOSS].type = val;
				sd->left_weapon.hp_drain[RC_BOSS].value += type2;
				sd->left_weapon.hp_drain[RC_BOSS].type = val;
			}
			break;
		case SP_SP_DRAIN_RATE:
			if (sd->state.lr_flag == 0) {
				// Decomposed RC_ALL:
				sd->right_weapon.sp_drain[RC_NONBOSS].rate += type2;
				sd->right_weapon.sp_drain[RC_NONBOSS].per += val;
				sd->right_weapon.sp_drain[RC_BOSS].rate += type2;
				sd->right_weapon.sp_drain[RC_BOSS].per += val;
			} else if (sd->state.lr_flag == 1) {
				// Decomposed RC_ALL:
				sd->left_weapon.sp_drain[RC_NONBOSS].rate += type2;
				sd->left_weapon.sp_drain[RC_NONBOSS].per += val;
				sd->left_weapon.sp_drain[RC_BOSS].rate += type2;
				sd->left_weapon.sp_drain[RC_BOSS].per += val;
			}
			break;
		case SP_SP_DRAIN_VALUE:
			if (sd->state.lr_flag == 0) {
				// Decomposed RC_ALL:
				sd->right_weapon.sp_drain[RC_NONBOSS].value += type2;
				sd->right_weapon.sp_drain[RC_NONBOSS].type = val;
				sd->right_weapon.sp_drain[RC_BOSS].value += type2;
				sd->right_weapon.sp_drain[RC_BOSS].type = val;
			} else if (sd->state.lr_flag == 1) {
				// Decomposed RC_ALL:
				sd->left_weapon.sp_drain[RC_NONBOSS].value += type2;
				sd->left_weapon.sp_drain[RC_NONBOSS].type = val;
				sd->left_weapon.sp_drain[RC_BOSS].value += type2;
				sd->left_weapon.sp_drain[RC_BOSS].type = val;
			}
			break;
		case SP_HP_VANISH_RATE:
			if (sd->state.lr_flag != 2) {
				sd->bonus.hp_vanish_rate += type2;
				sd->bonus.hp_vanish_per = max(sd->bonus.hp_vanish_per, val);
				sd->bonus.hp_vanish_trigger = 0;
			}
			break;
		case SP_SP_VANISH_RATE:
			if (sd->state.lr_flag != 2) {
				sd->bonus.sp_vanish_rate += type2;
				sd->bonus.sp_vanish_per = max(sd->bonus.sp_vanish_per, val);
				sd->bonus.sp_vanish_trigger = 0;
			}
			break;
		case SP_GET_ZENY_NUM:
			if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
				sd->bonus.get_zeny_rate = val;
				sd->bonus.get_zeny_num = type2;
			}
			break;
		case SP_ADD_GET_ZENY_NUM:
			if(sd->state.lr_flag != 2) {
				sd->bonus.get_zeny_rate += val;
				sd->bonus.get_zeny_num += type2;
			}
			break;
		case SP_WEAPON_COMA_ELE:
			if( (type2 >= ELE_MAX && type2 != ELE_ALL) || (type2 < ELE_NEUTRAL) ) {
				ShowError("pc_bonus2: SP_WEAPON_COMA_ELE: Invalid element %d\n", type2);
				break;
			}
			if(sd->state.lr_flag == 2)
				break;
			if ( type2 == ELE_ALL ) {
				for ( i = ELE_NEUTRAL; i < ELE_MAX; i++ )
					sd->weapon_coma_ele[i] += val;
			} else {
				sd->weapon_coma_ele[type2] += val;
			}
			sd->special_state.bonus_coma = 1;
			break;
		case SP_WEAPON_COMA_RACE:
		{
			uint32 race_mask = map->race_id2mask(type2);
			if (race_mask == RCMASK_NONE) {
				ShowWarning("pc_bonus2: SP_WEAPON_COMA_RACE: Invalid Race (%d)\n", type2);
				break;
			}
			if(sd->state.lr_flag == 2)
				break;
			BONUS_FOREACH_RCARRAY_FROMMASK(i, race_mask) {
				sd->weapon_coma_race[i] += val;
			}
			sd->special_state.bonus_coma = 1;
		}
			break;
		case SP_WEAPON_ATK:
			if(sd->state.lr_flag != 2)
				sd->weapon_atk[type2]+=val;
			break;
		case SP_WEAPON_ATK_RATE:
			if(sd->state.lr_flag != 2)
				sd->weapon_atk_rate[type2]+=val;
			break;
		case SP_CRITICAL_ADDRACE:
		{
			uint32 race_mask = map->race_id2mask(type2);
			if (race_mask == RCMASK_NONE) {
				ShowWarning("pc_bonus2: SP_CRITICAL_ADDRACE: Invalid Race (%d)\n", type2);
				break;
			}
			if (sd->state.lr_flag == 2)
				break;
			BONUS_FOREACH_RCARRAY_FROMMASK(i, race_mask) {
				sd->critaddrace[i] += val*10;
			}
		}
			break;
		case SP_ADDEFF_WHENHIT:
			if (type2 > SC_MAX) {
				ShowWarning("pc_bonus2 (Add Effect when hit): %d is not supported.\n", type2);
				break;
			}
			if(sd->state.lr_flag != 2)
				pc->bonus_addeff(sd->addeff2, ARRAYLENGTH(sd->addeff2), (sc_type)type2, val, 0, 0, 0);
			break;
		case SP_SKILL_ATK:
			if(sd->state.lr_flag == 2)
				break;
			ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == 0 || sd->skillatk[i].id == type2);
			if (i == ARRAYLENGTH(sd->skillatk)) {
				//Better mention this so the array length can be updated. [Skotlex]
				ShowDebug("script->run: bonus2 bSkillAtk reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n",
				          ARRAYLENGTH(sd->skillatk), type2, val);
				break;
			}
			if (sd->skillatk[i].id == type2)
				sd->skillatk[i].val += val;
			else {
				sd->skillatk[i].id = type2;
				sd->skillatk[i].val = val;
			}
			break;
		case SP_SKILL_HEAL:
			if(sd->state.lr_flag == 2)
				break;
			ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == 0 || sd->skillheal[i].id == type2);
			if (i == ARRAYLENGTH(sd->skillheal)) {
				// Better mention this so the array length can be updated. [Skotlex]
				ShowDebug("script->run: bonus2 bSkillHeal reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n",
				          ARRAYLENGTH(sd->skillheal), type2, val);
				break;
			}
			if (sd->skillheal[i].id == type2)
				sd->skillheal[i].val += val;
			else {
				sd->skillheal[i].id = type2;
				sd->skillheal[i].val = val;
			}
			break;
		case SP_SKILL_HEAL2:
			if(sd->state.lr_flag == 2)
				break;
			ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == 0 || sd->skillheal2[i].id == type2);
			if (i == ARRAYLENGTH(sd->skillheal2)) {
				// Better mention this so the array length can be updated. [Skotlex]
				ShowDebug("script->run: bonus2 bSkillHeal2 reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n",
				          ARRAYLENGTH(sd->skillheal2), type2, val);
				break;
			}
			if (sd->skillheal2[i].id == type2)
				sd->skillheal2[i].val += val;
			else {
				sd->skillheal2[i].id = type2;
				sd->skillheal2[i].val = val;
			}
			break;
		case SP_ADD_SKILL_BLOW:
			if(sd->state.lr_flag == 2)
				break;
			ARR_FIND(0, ARRAYLENGTH(sd->skillblown), i, sd->skillblown[i].id == 0 || sd->skillblown[i].id == type2);
			if (i == ARRAYLENGTH(sd->skillblown)) {
				//Better mention this so the array length can be updated. [Skotlex]
				ShowDebug("script->run: bonus2 bSkillBlown reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n",
				          ARRAYLENGTH(sd->skillblown), type2, val);
				break;
			}
			if(sd->skillblown[i].id == type2)
				sd->skillblown[i].val += val;
			else {
				sd->skillblown[i].id = type2;
				sd->skillblown[i].val = val;
			}
			break;
#ifndef RENEWAL_CAST
		case SP_VARCASTRATE:
#endif
		case SP_CASTRATE:
			if(sd->state.lr_flag == 2)
				break;
			ARR_FIND(0, ARRAYLENGTH(sd->skillcast), i, sd->skillcast[i].id == 0 || sd->skillcast[i].id == type2);
			if (i == ARRAYLENGTH(sd->skillcast)) {
				//Better mention this so the array length can be updated. [Skotlex]
				ShowDebug("script->run: bonus2 %s reached its limit (%d skills per character), bonus skill %d (+%d%%) lost.\n",
					type == SP_CASTRATE ? "bCastRate" : "bVariableCastrate",
					ARRAYLENGTH(sd->skillcast), type2, val);
				break;
			}
			if(sd->skillcast[i].id == type2)
				sd->skillcast[i].val += val;
			else {
				sd->skillcast[i].id = type2;
				sd->skillcast[i].val = val;
			}
			break;

		case SP_FIXCASTRATE:
			if(sd->state.lr_flag == 2)
				break;

			ARR_FIND(0, ARRAYLENGTH(sd->skillfixcastrate), i, sd->skillfixcastrate[i].id == 0 || sd->skillfixcastrate[i].id == type2);

			if (i == ARRAYLENGTH(sd->skillfixcastrate)) {
				ShowDebug("script->run: bonus2 bFixedCastrate reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n",
				          ARRAYLENGTH(sd->skillfixcastrate), type2, val);
				break;
			}

			if(sd->skillfixcastrate[i].id == type2)
				sd->skillfixcastrate[i].val -= val;

			else {
				sd->skillfixcastrate[i].id = type2;
				sd->skillfixcastrate[i].val -= val;
			}

			break;

		case SP_HP_LOSS_RATE:
			if(sd->state.lr_flag != 2) {
				sd->hp_loss.value = type2;
				sd->hp_loss.rate = val;
			}
			break;
		case SP_HP_REGEN_RATE:
			if(sd->state.lr_flag != 2) {
				sd->hp_regen.value = type2;
				sd->hp_regen.rate = val;
			}
			break;
		case SP_ADDRACE2:
			if (!(type2 > RC2_NONE && type2 < RC2_MAX))
				break;
			if(sd->state.lr_flag != 2)
				sd->right_weapon.addrace2[type2] += val;
			else
				sd->left_weapon.addrace2[type2] += val;
			break;
		case SP_SUBSIZE:
			if(sd->state.lr_flag != 2)
				sd->subsize[type2]+=val;
			break;
		case SP_SUBRACE2:
			if (!(type2 > RC2_NONE && type2 < RC2_MAX))
				break;
			if(sd->state.lr_flag != 2)
				sd->subrace2[type2]+=val;
			break;
		case SP_ADD_ITEM_HEAL_RATE:
			if(sd->state.lr_flag == 2)
				break;
			//Standard item bonus.
			for(i=0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid && sd->itemhealrate[i].nameid != type2; i++);
			if (i == ARRAYLENGTH(sd->itemhealrate)) {
				ShowWarning("pc_bonus2: Reached max (%d) number of item heal bonuses per character!\n", ARRAYLENGTH(sd->itemhealrate));
				break;
			}
			sd->itemhealrate[i].nameid = type2;
			sd->itemhealrate[i].rate += val;
			break;
		case SP_EXP_ADDRACE:
		{
			uint32 race_mask = map->race_id2mask(type2);
			if (race_mask == RCMASK_NONE) {
				ShowWarning("pc_bonus2: SP_EXP_ADDRACE: Invalid Race (%d)\n", type2);
				break;
			}
			if (sd->state.lr_flag == 2)
				break;
			BONUS_FOREACH_RCARRAY_FROMMASK(i, race_mask) {
				sd->expaddrace[i] += val;
			}
		}
			break;
		case SP_SP_GAIN_RACE:
		{
			uint32 race_mask = map->race_id2mask(type2);
			if (race_mask == RCMASK_NONE) {
				ShowWarning("pc_bonus2: SP_SP_GAIN_RACE: Invalid Race (%d)\n", type2);
				break;
			}
			if (sd->state.lr_flag == 2)
				break;
			BONUS_FOREACH_RCARRAY_FROMMASK(i, race_mask) {
				sd->sp_gain_race[i] += val;
			}
		}
			break;
		case SP_ADD_MONSTER_DROP_ITEM:
			if (sd->state.lr_flag != 2)
				pc->bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, map->race_id2mask(RC_ALL), val);
			break;
		case SP_SP_LOSS_RATE:
			if(sd->state.lr_flag != 2) {
				sd->sp_loss.value = type2;
				sd->sp_loss.rate = val;
			}
			break;
		case SP_SP_REGEN_RATE:
			if(sd->state.lr_flag != 2) {
				sd->sp_regen.value = type2;
				sd->sp_regen.rate = val;
			}
			break;
		case SP_HP_DRAIN_VALUE_RACE:
		{
			uint32 race_mask = map->race_id2mask(type2);
			if (race_mask == RCMASK_NONE) {
				ShowWarning("pc_bonus2: SP_HP_DRAIN_VALUE_RACE: Invalid Race (%d)\n", type2);
				break;
			}
			BONUS_FOREACH_RCARRAY_FROMMASK(i, race_mask) {
				if (sd->state.lr_flag == 0)
					sd->right_weapon.hp_drain[i].value += val;
				else if(sd->state.lr_flag == 1)
					sd->left_weapon.hp_drain[i].value += val;
			}
		}
			break;
		case SP_SP_DRAIN_VALUE_RACE:
		{
			uint32 race_mask = map->race_id2mask(type2);
			if (race_mask == RCMASK_NONE) {
				ShowWarning("pc_bonus2: SP_SP_DRAIN_VALUE_RACE: Invalid Race (%d)\n", type2);
				break;
			}
			BONUS_FOREACH_RCARRAY_FROMMASK(i, race_mask) {
				if (sd->state.lr_flag == 0)
					sd->right_weapon.sp_drain[i].value += val;
				else if (sd->state.lr_flag == 1)
					sd->left_weapon.sp_drain[i].value += val;
			}
		}
			break;
		case SP_IGNORE_MDEF_RATE:
		{
			uint32 race_mask = map->race_id2mask(type2);
			if (race_mask == RCMASK_NONE) {
				ShowWarning("pc_bonus2: SP_IGNORE_MDEF_RATE: Invalid Race (%d)\n", type2);
				break;
			}
			if (sd->state.lr_flag == 2)
				break;
			BONUS_FOREACH_RCARRAY_FROMMASK(i, race_mask) {
				sd->ignore_mdef[i] += val;
			}
		}
			break;
		case SP_IGNORE_DEF_RATE:
		{
			uint32 race_mask = map->race_id2mask(type2);
			if (race_mask == RCMASK_NONE) {
				ShowWarning("pc_bonus2: SP_IGNORE_DEF_RATE: Invalid Race (%d)\n", type2);
				break;
			}
			if (sd->state.lr_flag == 2)
				break;
			BONUS_FOREACH_RCARRAY_FROMMASK(i, race_mask) {
				sd->ignore_def[i] += val;
			}
		}
			break;
		case SP_SP_GAIN_RACE_ATTACK:
		{
			uint32 race_mask = map->race_id2mask(type2);
			if (race_mask == RCMASK_NONE) {
				ShowWarning("pc_bonus2: SP_SP_GAIN_RACE_ATTACK: Invalid Race (%d)\n", type2);
				break;
			}
			if (sd->state.lr_flag == 2)
				break;
			BONUS_FOREACH_RCARRAY_FROMMASK(i, race_mask) {
				sd->sp_gain_race_attack[i] = cap_value(sd->sp_gain_race_attack[i] + val, 0, INT16_MAX);
			}
		}
			break;
		case SP_HP_GAIN_RACE_ATTACK:
		{
			uint32 race_mask = map->race_id2mask(type2);
			if (race_mask == RCMASK_NONE) {
				ShowWarning("pc_bonus2: SP_HP_GAIN_RACE_ATTACK: Invalid Race (%d)\n", type2);
				break;
			}
			if (sd->state.lr_flag == 2)
				break;
			BONUS_FOREACH_RCARRAY_FROMMASK(i, race_mask) {
				sd->hp_gain_race_attack[i] = cap_value(sd->hp_gain_race_attack[i] + val, 0, INT16_MAX);
			}
		}
			break;
		case SP_SKILL_USE_SP_RATE: //bonus2 bSkillUseSPrate,n,x;
			if(sd->state.lr_flag == 2)
				break;
			ARR_FIND(0, ARRAYLENGTH(sd->skillusesprate), i, sd->skillusesprate[i].id == 0 || sd->skillusesprate[i].id == type2);
			if (i == ARRAYLENGTH(sd->skillusesprate)) {
				ShowDebug("script->run: bonus2 bSkillUseSPrate reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n",
				          ARRAYLENGTH(sd->skillusesprate), type2, val);
				break;
			}
			if (sd->skillusesprate[i].id == type2)
				sd->skillusesprate[i].val += val;
			else {
				sd->skillusesprate[i].id = type2;
				sd->skillusesprate[i].val = val;
			}
			break;
		case SP_SKILL_COOLDOWN:
			if(sd->state.lr_flag == 2)
				break;
			ARR_FIND(0, ARRAYLENGTH(sd->skillcooldown), i, sd->skillcooldown[i].id == 0 || sd->skillcooldown[i].id == type2);
			if (i == ARRAYLENGTH(sd->skillcooldown)) {
				ShowDebug("script->run: bonus2 bSkillCoolDown reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n",
				          ARRAYLENGTH(sd->skillcooldown), type2, val);
				break;
			}
			if (sd->skillcooldown[i].id == type2)
				sd->skillcooldown[i].val += val;
			else {
				sd->skillcooldown[i].id = type2;
				sd->skillcooldown[i].val = val;
			}
			break;
		case SP_SKILL_FIXEDCAST:
			if(sd->state.lr_flag == 2)
				break;
			ARR_FIND(0, ARRAYLENGTH(sd->skillfixcast), i, sd->skillfixcast[i].id == 0 || sd->skillfixcast[i].id == type2);
			if (i == ARRAYLENGTH(sd->skillfixcast)) {
				ShowDebug("script->run: bonus2 bSkillFixedCast reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n",
				          ARRAYLENGTH(sd->skillfixcast), type2, val);
				break;
			}
			if (sd->skillfixcast[i].id == type2)
				sd->skillfixcast[i].val += val;
			else {
				sd->skillfixcast[i].id = type2;
				sd->skillfixcast[i].val = val;
			}
			break;
		case SP_SKILL_VARIABLECAST:
			if(sd->state.lr_flag == 2)
				break;
			ARR_FIND(0, ARRAYLENGTH(sd->skillvarcast), i, sd->skillvarcast[i].id == 0 || sd->skillvarcast[i].id == type2);
			if (i == ARRAYLENGTH(sd->skillvarcast)) {
				ShowDebug("script->run: bonus2 bSkillVariableCast reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n",
				          ARRAYLENGTH(sd->skillvarcast), type2, val);
				break;
			}
			if (sd->skillvarcast[i].id == type2)
				sd->skillvarcast[i].val += val;
			else {
				sd->skillvarcast[i].id = type2;
				sd->skillvarcast[i].val = val;
			}
			break;
	#ifdef RENEWAL_CAST
		case SP_VARCASTRATE:
			if(sd->state.lr_flag == 2)
				break;
			ARR_FIND(0, ARRAYLENGTH(sd->skillcast), i, sd->skillcast[i].id == 0 || sd->skillcast[i].id == type2);
			if (i == ARRAYLENGTH(sd->skillcast)) {
				ShowDebug("script->run: bonus2 bVariableCastrate reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n",
				          ARRAYLENGTH(sd->skillcast), type2, val);
				break;
			}
			if(sd->skillcast[i].id == type2)
				sd->skillcast[i].val -= val;
			else {
				sd->skillcast[i].id = type2;
				sd->skillcast[i].val -= val;
			}
			break;
	#endif
		case SP_SKILL_USE_SP: //bonus2 bSkillUseSP,n,x;
			if(sd->state.lr_flag == 2)
				break;
			ARR_FIND(0, ARRAYLENGTH(sd->skillusesp), i, sd->skillusesp[i].id == 0 || sd->skillusesp[i].id == type2);
			if (i == ARRAYLENGTH(sd->skillusesp)) {
				ShowDebug("script->run: bonus2 bSkillUseSP reached it's limit (%d skills per character), bonus skill %d (+%d%%) lost.\n",
				          ARRAYLENGTH(sd->skillusesp), type2, val);
				break;
			}
			if (sd->skillusesp[i].id == type2)
				sd->skillusesp[i].val += val;
			else {
				sd->skillusesp[i].id = type2;
				sd->skillusesp[i].val = val;
			}
			break;
		case SP_ADD_MONSTER_DROP_CHAINITEM:
		{
			uint32 race_mask = map->race_id2mask(val);
			if (race_mask == RCMASK_NONE) {
				ShowWarning("pc_bonus2: SP_ADD_MONSTER_DROP_CHAINITEM: Invalid Race (%d)\n", val);
				break;
			}
			if (sd->state.lr_flag != 2)
				pc->bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), 0, type2, race_mask, 10000);
		}
			break;
#ifdef RENEWAL
		case SP_RACE_TOLERANCE:
		{
			uint32 race_mask = map->race_id2mask(type2);
			if (race_mask == RCMASK_NONE) {
				ShowWarning("pc_bonus2: SP_RACE_TOLERANCE: Invalid Race (%d)\n", type2);
				break;
			}
			if (sd->state.lr_flag == 2)
				break;
			BONUS_FOREACH_RCARRAY_FROMMASK(i, race_mask)
				sd->race_tolerance[i] += val;
			}
			break;
#endif
		default:
			ShowWarning("pc_bonus2: unknown type %d %d %d!\n",type,type2,val);
			Assert_report(0);
			break;
	}
	return 0;
}

int pc_bonus3(struct map_session_data *sd,int type,int type2,int type3,int val)
{
	int i;
	nullpo_ret(sd);

	switch(type){
		case SP_ADD_MONSTER_DROP_ITEM:
		{
			uint32 race_mask = map->race_id2mask(type3);
			if (race_mask == RCMASK_NONE) {
				ShowWarning("pc_bonus2: SP_ADD_MONSTER_DROP_ITEM: Invalid Race (%d)\n", type3);
				break;
			}
			if (sd->state.lr_flag != 2)
				pc->bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, race_mask, val);
		}
			break;
		case SP_ADD_CLASS_DROP_ITEM:
			if(sd->state.lr_flag != 2)
				pc->bonus_item_drop(sd->add_drop, ARRAYLENGTH(sd->add_drop), type2, 0, -type3, val);
			break;
		case SP_AUTOSPELL:
			if(sd->state.lr_flag != 2)
			{
				int target = skill->get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
				target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill->get_inf2(type2)&INF2_NO_TARGET_SELF));
				pc->bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell),
					target?-type2:type2, type3, val, 0, status->current_equip_card_id);
			}
			break;
		case SP_AUTOSPELL_WHENHIT:
			if(sd->state.lr_flag != 2)
			{
				int target = skill->get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
				target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill->get_inf2(type2)&INF2_NO_TARGET_SELF));
				pc->bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2),
					target?-type2:type2, type3, val, BF_NORMAL|BF_SKILL, status->current_equip_card_id);
			}
			break;
		case SP_SP_DRAIN_RATE:
			if (sd->state.lr_flag == 0) {
				// Decomposed RC_ALL:
				sd->right_weapon.sp_drain[RC_NONBOSS].rate += type2;
				sd->right_weapon.sp_drain[RC_NONBOSS].per += type3;
				sd->right_weapon.sp_drain[RC_NONBOSS].type = val;
				sd->right_weapon.sp_drain[RC_BOSS].rate += type2;
				sd->right_weapon.sp_drain[RC_BOSS].per += type3;
				sd->right_weapon.sp_drain[RC_BOSS].type = val;
			} else if (sd->state.lr_flag == 1) {
				// Decomposed RC_ALL:
				sd->left_weapon.sp_drain[RC_NONBOSS].rate += type2;
				sd->left_weapon.sp_drain[RC_NONBOSS].per += type3;
				sd->left_weapon.sp_drain[RC_NONBOSS].type = val;
				sd->left_weapon.sp_drain[RC_BOSS].rate += type2;
				sd->left_weapon.sp_drain[RC_BOSS].per += type3;
				sd->left_weapon.sp_drain[RC_BOSS].type = val;
			}
			break;
		case SP_HP_DRAIN_RATE_RACE:
		{
			uint32 race_mask = map->race_id2mask(type2);
			if (race_mask == RCMASK_NONE) {
				ShowWarning("pc_bonus3: SP_HP_DRAIN_RATE_RACE: Invalid Race (%d)\n", type2);
				break;
			}
			BONUS_FOREACH_RCARRAY_FROMMASK(i, race_mask) {
				if (sd->state.lr_flag == 0) {
					sd->right_weapon.hp_drain[i].rate += type3;
					sd->right_weapon.hp_drain[i].per += val;
				} else if(sd->state.lr_flag == 1) {
					sd->left_weapon.hp_drain[i].rate += type3;
					sd->left_weapon.hp_drain[i].per += val;
				}
			}
		}
			break;
		case SP_SP_DRAIN_RATE_RACE:
		{
			uint32 race_mask = map->race_id2mask(type2);
			if (race_mask == RCMASK_NONE) {
				ShowWarning("pc_bonus3: SP_SP_DRAIN_RATE_RACE: Invalid Race (%d)\n", type2);
				break;
			}
			BONUS_FOREACH_RCARRAY_FROMMASK(i, race_mask) {
				if (sd->state.lr_flag == 0) {
					sd->right_weapon.sp_drain[i].rate += type3;
					sd->right_weapon.sp_drain[i].per += val;
				} else if(sd->state.lr_flag == 1) {
					sd->left_weapon.sp_drain[i].rate += type3;
					sd->left_weapon.sp_drain[i].per += val;
				}
			}
		}
			break;
		case SP_ADDEFF:
			if (type2 > SC_MAX) {
				ShowWarning("pc_bonus3 (Add Effect): %d is not supported.\n", type2);
				break;
			}
			pc->bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
			                 sd->state.lr_flag!=2?type3:0, sd->state.lr_flag==2?type3:0, val, 0);
			break;

		case SP_ADDEFF_WHENHIT:
			if (type2 > SC_MAX) {
				ShowWarning("pc_bonus3 (Add Effect when hit): %d is not supported.\n", type2);
				break;
			}
			if(sd->state.lr_flag != 2)
				pc->bonus_addeff(sd->addeff2, ARRAYLENGTH(sd->addeff2), (sc_type)type2, type3, 0, val, 0);
			break;

		case SP_ADDEFF_ONSKILL:
			if( type3 > SC_MAX ) {
				ShowWarning("pc_bonus3 (Add Effect on skill): %d is not supported.\n", type3);
				break;
			}
			if( sd->state.lr_flag != 2 )
				pc->bonus_addeff_onskill(sd->addeff3, ARRAYLENGTH(sd->addeff3), (sc_type)type3, val, type2, ATF_TARGET);
			break;

		case SP_ADDELE:
			if( (type2 >= ELE_MAX && type2 != ELE_ALL) || (type2 < ELE_NEUTRAL) ) {
				ShowError("pc_bonus3: SP_ADDELE: Invalid element %d\n", type2);
				break;
			}
			if ( sd->state.lr_flag != 2 ) {
				if ( type2 == ELE_ALL ) {
					for ( i = ELE_NEUTRAL; i < ELE_MAX; i++ )
						pc_bonus_addele(sd, (unsigned char)i, type3, val);
				} else {
					pc_bonus_addele(sd, (unsigned char)type2, type3, val);
				}
			}
			break;

		case SP_SUBELE:
			if( (type2 >= ELE_MAX && type2 != ELE_ALL) || (type2 < ELE_NEUTRAL) ) {
				ShowError("pc_bonus3: SP_SUBELE: Invalid element %d\n", type2);
				break;
			}
			if ( sd->state.lr_flag != 2 ) {
				if ( type2 == ELE_ALL ) {
					for ( i = ELE_NEUTRAL; i < ELE_MAX; i++ )
						pc_bonus_subele(sd, (unsigned char)i, type3, val);
				} else {
					pc_bonus_subele(sd, (unsigned char)type2, type3, val);
				}
			}
			break;
		case SP_HP_VANISH_RATE:
			if (sd->state.lr_flag != 2) {
				sd->bonus.hp_vanish_rate += type2;
				sd->bonus.hp_vanish_per = max(sd->bonus.hp_vanish_per, type3);
				sd->bonus.hp_vanish_trigger = val;
			}
			break;
		case SP_SP_VANISH_RATE:
			if (sd->state.lr_flag != 2) {
				sd->bonus.sp_vanish_rate += type2;
				sd->bonus.sp_vanish_per = max(sd->bonus.sp_vanish_per, type3);
				sd->bonus.sp_vanish_trigger = val;
			}
			break;

		default:
			ShowWarning("pc_bonus3: unknown type %d %d %d %d!\n",type,type2,type3,val);
			Assert_report(0);
			break;
	}

	return 0;
}

int pc_bonus4(struct map_session_data *sd,int type,int type2,int type3,int type4,int val) {
	int i;
	nullpo_ret(sd);

	switch(type) {
	case SP_AUTOSPELL:
		if(sd->state.lr_flag != 2)
			pc->bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1) ? type2 : -type2, (val&2) ? -type3 : type3, type4, 0, status->current_equip_card_id);
		break;

	case SP_AUTOSPELL_WHENHIT:
		if(sd->state.lr_flag != 2)
			pc->bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2), (val&1) ? type2 : -type2, (val&2) ? -type3 : type3, type4, BF_NORMAL|BF_SKILL, status->current_equip_card_id);
		break;

	case SP_AUTOSPELL_ONSKILL:
		if(sd->state.lr_flag != 2) {
			int target = skill->get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
			target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !(skill->get_inf2(type2)&INF2_NO_TARGET_SELF));

			pc->bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, target?-type3:type3, type4, val, status->current_equip_card_id);
		}
		break;

	case SP_ADDEFF_ONSKILL:
		if( type2 > SC_MAX ) {
			ShowWarning("pc_bonus4 (Add Effect on skill): %d is not supported.\n", type2);
			break;
		}
		if( sd->state.lr_flag != 2 )
			pc->bonus_addeff_onskill(sd->addeff3, ARRAYLENGTH(sd->addeff3), (sc_type)type3, type4, type2, val);
		break;

	case SP_SET_DEF_RACE: //bonus4 bSetDefRace,n,x,r,y;
	{
		uint32 race_mask = map->race_id2mask(type2);
		if (race_mask == RCMASK_NONE) {
			ShowWarning("pc_bonus4: SP_SET_DEF_RACE: Invalid Race (%d)\n", type2);
			break;
		}
		if (sd->state.lr_flag == 2)
			break;
		BONUS_FOREACH_RCARRAY_FROMMASK(i, race_mask) {
			sd->def_set_race[i].rate = type3;
			sd->def_set_race[i].tick = type4;
			sd->def_set_race[i].value = val;
		}
	}
		break;

	case SP_SET_MDEF_RACE: //bonus4 bSetMDefRace,n,x,r,y;
	{
		uint32 race_mask = map->race_id2mask(type2);
		if (race_mask == RCMASK_NONE) {
			ShowWarning("pc_bonus4: SP_SET_MDEF_RACE: Invalid Race (%d)\n", type2);
			break;
		}
		if (sd->state.lr_flag == 2)
			break;
		BONUS_FOREACH_RCARRAY_FROMMASK(i, race_mask) {
			sd->mdef_set_race[i].rate = type3;
			sd->mdef_set_race[i].tick = type4;
			sd->mdef_set_race[i].value = val;
		}
	}
		break;

	case SP_ADDEFF:
	{
		uint16 duration;
		if (type2 > SC_MAX) {
			ShowWarning("pc_bonus4 (Add Effect): %d is not supported.\n", type2);
			break;
		}
		if (val < 0 || val > UINT16_MAX) {
			ShowWarning("pc_bonus4 (Add Effect): invalid duration %d. Valid range: [0:%d].\n", val, UINT16_MAX);
			duration = (val < 0 ? 0 : UINT16_MAX);
		} else {
			duration = (uint16)val;
		}

		pc->bonus_addeff(sd->addeff, ARRAYLENGTH(sd->addeff), (sc_type)type2,
		                 sd->state.lr_flag!=2?type3:0, sd->state.lr_flag==2?type3:0, type4, duration);
	}
		break;

	default:
		ShowWarning("pc_bonus4: unknown type %d %d %d %d %d!\n",type,type2,type3,type4,val);
		Assert_report(0);
		break;
	}

	return 0;
}

int pc_bonus5(struct map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val) {
	nullpo_ret(sd);

	switch(type){
		case SP_AUTOSPELL:
			if(sd->state.lr_flag != 2)
				pc->bonus_autospell(sd->autospell, ARRAYLENGTH(sd->autospell), (val&1) ? type2 : -type2, (val&2) ? -type3 : type3, type4, type5, status->current_equip_card_id);
			break;

		case SP_AUTOSPELL_WHENHIT:
			if(sd->state.lr_flag != 2)
				pc->bonus_autospell(sd->autospell2, ARRAYLENGTH(sd->autospell2), (val&1) ? type2 : -type2, (val&2) ? -type3 : type3, type4, type5, status->current_equip_card_id);
			break;

		case SP_AUTOSPELL_ONSKILL:
			if(sd->state.lr_flag != 2)
				pc->bonus_autospell_onskill(sd->autospell3, ARRAYLENGTH(sd->autospell3), type2, (val&1) ? -type3 : type3, (val&2) ? -type4 : type4, type5, status->current_equip_card_id);
			break;

		default:
			ShowWarning("pc_bonus5: unknown type %d %d %d %d %d %d!\n",type,type2,type3,type4,type5,val);
			Assert_report(0);
			break;
	}

	return 0;
}

#undef BONUS_FOREACH_RCARRAY_FROMMASK

/*==========================================
 * Grants a player a given skill.
 * Flag values: @see enum pc_skill_flag
 *------------------------------------------*/
int pc_skill(struct map_session_data *sd, int id, int level, int flag)
{
	uint16 index = 0;
	nullpo_ret(sd);

	if (!(index = skill->get_index(id))) {
		ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", id);
		return 0;
	}
	if( level > MAX_SKILL_LEVEL ) {
		ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
		return 0;
	}
	if( flag == SKILL_GRANT_TEMPSTACK && sd->status.skill[index].lv + level > MAX_SKILL_LEVEL ) {
		ShowError("pc_skill: Skill level bonus %d too high. Max lv supported is %d. Curr lv is %d\n", level, MAX_SKILL_LEVEL, sd->status.skill[index].lv);
		return 0;
	}

	switch( flag ){
		case SKILL_GRANT_PERMANENT: //Set skill data overwriting whatever was there before.
			sd->status.skill[index].id   = id;
			sd->status.skill[index].lv   = level;
			sd->status.skill[index].flag = SKILL_FLAG_PERMANENT;
			if( level == 0 ) { //Remove skill.
				sd->status.skill[index].id = 0;
				clif->deleteskill(sd,id);
			} else
				clif->addskill(sd,id);
			if( !skill->dbs->db[index].inf ) //Only recalculate for passive skills.
				status_calc_pc(sd, SCO_NONE);
		break;
		case SKILL_GRANT_TEMPORARY: //Item bonus skill.
			if( sd->status.skill[index].id == id ) {
				if( sd->status.skill[index].lv >= level )
					return 0;
				if( sd->status.skill[index].flag == SKILL_FLAG_PERMANENT ) //Non-granted skill, store it's level.
					sd->status.skill[index].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[index].lv;
			} else {
				sd->status.skill[index].id   = id;
				sd->status.skill[index].flag = SKILL_FLAG_TEMPORARY;
			}
			sd->status.skill[index].lv = level;
		break;
		case SKILL_GRANT_TEMPSTACK: //Add skill bonus on top of what you had.
			if( sd->status.skill[index].id == id ) {
				if( sd->status.skill[index].flag == SKILL_FLAG_PERMANENT )
					sd->status.skill[index].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[index].lv; // Store previous level.
			} else {
				sd->status.skill[index].id   = id;
				sd->status.skill[index].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
			}
			sd->status.skill[index].lv += level;
		break;
		case SKILL_GRANT_UNCONDITIONAL:
			sd->status.skill[index].id   = id;
			sd->status.skill[index].lv   = level;
			sd->status.skill[index].flag = SKILL_FLAG_PERM_GRANTED;
			if( level == 0 ) { //Remove skill.
				sd->status.skill[index].id = 0;
				clif->deleteskill(sd,id);
			} else
				clif->addskill(sd,id);
			if( !skill->dbs->db[index].inf ) //Only recalculate for passive skills.
				status_calc_pc(sd, SCO_NONE);
			break;
		default: //Unknown flag?
			return 0;
	}
	return 1;
}

/**
 * Checks if the given card can be inserted into the given equipment piece.
 *
 * @param sd        The current character.
 * @param idx_card  The card's inventory index (note: it must be a valid index and can be checked by pc_can_insert_card)
 * @param idx_equip The target equipment's inventory index.
 * @retval true if the card can be inserted.
 */
bool pc_can_insert_card_into(struct map_session_data* sd, int idx_card, int idx_equip)
{
	int i;

	nullpo_ret(sd);

	if (idx_equip < 0 || idx_equip >= MAX_INVENTORY || sd->inventory_data[idx_equip] == NULL)
		return false; //Invalid item index.
	if (sd->status.inventory[idx_equip].nameid <= 0 || sd->status.inventory[idx_equip].amount < 1)
		return false; // target item missing
	if (sd->inventory_data[idx_equip]->type != IT_WEAPON && sd->inventory_data[idx_equip]->type != IT_ARMOR)
		return false; // only weapons and armor are allowed
	if (sd->status.inventory[idx_equip].identify == 0)
		return false; // target must be identified
	if (itemdb_isspecial(sd->status.inventory[idx_equip].card[0]))
		return false; // card slots reserved for other purposes
	if (sd->status.inventory[idx_equip].equip != 0)
		return false; // item must be unequipped
	if ((sd->inventory_data[idx_equip]->equip & sd->inventory_data[idx_card]->equip) == 0)
		return false; // card cannot be compounded on this item type
	if (sd->inventory_data[idx_equip]->type == IT_WEAPON && sd->inventory_data[idx_card]->equip == EQP_SHIELD)
		return false; // attempted to place shield card on left-hand weapon.

	ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->status.inventory[idx_equip].card[i] == 0);
	if (i == sd->inventory_data[idx_equip]->slot)
		return false; // no free slots
	return true;
}

/**
 * Checks if the given item is card and it can be inserted into some equipment.
 *
 * @param sd        The current character.
 * @param idx_card  The card's inventory index.
 * @retval true if the card can be inserted.
 */
bool pc_can_insert_card(struct map_session_data* sd, int idx_card)
{
	nullpo_ret(sd);

	if (idx_card < 0 || idx_card >= MAX_INVENTORY || sd->inventory_data[idx_card] == NULL)
		return false; //Invalid card index.
	if (sd->status.inventory[idx_card].nameid <= 0 || sd->status.inventory[idx_card].amount < 1)
		return false; // target card missing
	if (sd->inventory_data[idx_card]->type != IT_CARD)
		return false; // must be a card
	return true;
}

/*==========================================
 * Attempt to insert card into item.
 * Return:
 *   0 = fail
 *   1 = success
 *------------------------------------------*/
int pc_insert_card(struct map_session_data* sd, int idx_card, int idx_equip)
{
	int nameid;

	nullpo_ret(sd);

	if (sd->state.trading != 0)
		return 0;

	if (!pc->can_insert_card(sd, idx_card) || !pc->can_insert_card_into(sd, idx_card, idx_equip))
		return 0;

	// remember the card id to insert
	nameid = sd->status.inventory[idx_card].nameid;

	if( pc->delitem(sd, idx_card, 1, 1, DELITEM_NORMAL, LOG_TYPE_CARD) == 1 )
	{// failed
		clif->insert_card(sd,idx_equip,idx_card,1);
	}
	else
	{// success
		int i;
		ARR_FIND( 0, sd->inventory_data[idx_equip]->slot, i, sd->status.inventory[idx_equip].card[i] == 0);
		if (i == sd->inventory_data[idx_equip]->slot)
			return 0; // no free slots
		logs->pick_pc(sd, LOG_TYPE_CARD, -1, &sd->status.inventory[idx_equip],sd->inventory_data[idx_equip]);
		sd->status.inventory[idx_equip].card[i] = nameid;
		logs->pick_pc(sd, LOG_TYPE_CARD,  1, &sd->status.inventory[idx_equip],sd->inventory_data[idx_equip]);
		clif->insert_card(sd,idx_equip,idx_card,0);
		return 1;
	}

	return 0;
}

//
// Items
//

/*==========================================
 * Update buying value by skills
 *------------------------------------------*/
int pc_modifybuyvalue(struct map_session_data *sd, int orig_value)
{
	int skill_lv, rate1 = 0, rate2 = 0;
	if (orig_value <= 0)
		return 0;
	if ((skill_lv=pc->checkskill(sd,MC_DISCOUNT)) > 0)   // merchant discount
		rate1 = 5+skill_lv*2-((skill_lv==10)? 1:0);
	if ((skill_lv=pc->checkskill(sd,RG_COMPULSION)) > 0) // rogue discount
		rate2 = 5+skill_lv*4;
	if (rate1 < rate2)
		rate1 = rate2;
	if (rate1 != 0)
		orig_value = apply_percentrate(orig_value, 100-rate1, 100);
	if (orig_value < 1)
		orig_value = 1;
	return orig_value;
}

/*==========================================
 * Update selling value by skills
 *------------------------------------------*/
int pc_modifysellvalue(struct map_session_data *sd, int orig_value)
{
	int skill_lv, rate = 0;
	if (orig_value <= 0)
		return 0;
	if ((skill_lv=pc->checkskill(sd,MC_OVERCHARGE)) > 0) //OverCharge
		rate = 5+skill_lv*2-((skill_lv==10)? 1:0);
	if (rate != 0)
		orig_value = apply_percentrate(orig_value, 100+rate, 100);
	if (orig_value < 1)
		orig_value = 1;
	return orig_value;
}

/*==========================================
 * Checking if we have enough place on inventory for new item
 * Make sure to take 30k as limit (for client I guess)
 *------------------------------------------*/
int pc_checkadditem(struct map_session_data *sd,int nameid,int amount)
{
	int i;
	struct item_data* data;

	nullpo_ret(sd);

	if(amount > MAX_AMOUNT)
		return ADDITEM_OVERAMOUNT;

	data = itemdb->search(nameid);

	if(!itemdb->isstackable2(data))
		return ADDITEM_NEW;

	if( data->stack.inventory && amount > data->stack.amount )
		return ADDITEM_OVERAMOUNT;

	for(i=0;i<MAX_INVENTORY;i++){
		// FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
		if(sd->status.inventory[i].nameid==nameid){
			if( amount > MAX_AMOUNT - sd->status.inventory[i].amount || ( data->stack.inventory && amount > data->stack.amount - sd->status.inventory[i].amount ) )
				return ADDITEM_OVERAMOUNT;
			return ADDITEM_EXIST;
		}
	}

	return ADDITEM_NEW;
}

/*==========================================
 * Return number of available place in inventory
 * Each non stackable item will reduce place by 1
 *------------------------------------------*/
int pc_inventoryblank(struct map_session_data *sd)
{
	int i,b;

	nullpo_ret(sd);

	for(i=0,b=0;i<MAX_INVENTORY;i++){
		if(sd->status.inventory[i].nameid==0)
			b++;
	}

	return b;
}

/*==========================================
 * attempts to remove zeny from player (sd)
 *------------------------------------------*/
int pc_payzeny(struct map_session_data *sd,int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
{
	nullpo_retr(-1,sd);

	zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
	if( zeny < 0 )
	{
		ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
		return 1;
	}

	if( sd->status.zeny < zeny )
		return 1; //Not enough.

	sd->status.zeny -= zeny;
	clif->updatestatus(sd,SP_ZENY);

	if(!tsd) tsd = sd;
	logs->zeny(sd, type, tsd, -zeny);
	if( zeny > 0 && sd->state.showzeny ) {
		char output[255];
		sprintf(output, "Removed %dz.", zeny);
		clif_disp_onlyself(sd, output);
	}

	return 0;
}
/*==========================================
 * Cash Shop
 *------------------------------------------*/

int pc_paycash(struct map_session_data *sd, int price, int points)
{
	int cash;
	nullpo_retr(-1,sd);

	points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
	if( price < 0 || points < 0 )
	{
		ShowError("pc_paycash: Paying negative points (price=%d, points=%d, account_id=%d, char_id=%d).\n", price, points, sd->status.account_id, sd->status.char_id);
		return -2;
	}

	if( points > price )
	{
		ShowWarning("pc_paycash: More kafra points provided than needed (price=%d, points=%d, account_id=%d, char_id=%d).\n", price, points, sd->status.account_id, sd->status.char_id);
		points = price;
	}

	cash = price-points;

	if( sd->cashPoints < cash || sd->kafraPoints < points )
	{
		ShowError("pc_paycash: Not enough points (cash=%d, kafra=%d) to cover the price (cash=%d, kafra=%d) (account_id=%d, char_id=%d).\n", sd->cashPoints, sd->kafraPoints, cash, points, sd->status.account_id, sd->status.char_id);
		return -1;
	}

	pc_setaccountreg(sd, script->add_str("#CASHPOINTS"), sd->cashPoints-cash);
	pc_setaccountreg(sd, script->add_str("#KAFRAPOINTS"), sd->kafraPoints-points);

	if( battle_config.cashshop_show_points )
	{
		char output[128];
		sprintf(output, msg_sd(sd,504), points, cash, sd->kafraPoints, sd->cashPoints);
		clif_disp_onlyself(sd, output);
	}
	return cash+points;
}

int pc_getcash(struct map_session_data *sd, int cash, int points)
{
	char output[128];
	nullpo_retr(-1,sd);

	cash = cap_value(cash,-MAX_ZENY,MAX_ZENY); //prevent command UB
	points = cap_value(points,-MAX_ZENY,MAX_ZENY); //prevent command UB
	if( cash > 0 )
	{
		if( cash > MAX_ZENY-sd->cashPoints )
		{
			ShowWarning("pc_getcash: Cash point overflow (cash=%d, have cash=%d, account_id=%d, char_id=%d).\n", cash, sd->cashPoints, sd->status.account_id, sd->status.char_id);
			cash = MAX_ZENY-sd->cashPoints;
		}

		pc_setaccountreg(sd, script->add_str("#CASHPOINTS"), sd->cashPoints+cash);

		if( battle_config.cashshop_show_points )
		{
			sprintf(output, msg_sd(sd,505), cash, sd->cashPoints);
			clif_disp_onlyself(sd, output);
		}
		return cash;
	}
	else if( cash < 0 )
	{
		ShowError("pc_getcash: Obtaining negative cash points (cash=%d, account_id=%d, char_id=%d).\n", cash, sd->status.account_id, sd->status.char_id);
		return -1;
	}

	if( points > 0 )
	{
		if( points > MAX_ZENY-sd->kafraPoints )
		{
			ShowWarning("pc_getcash: Kafra point overflow (points=%d, have points=%d, account_id=%d, char_id=%d).\n", points, sd->kafraPoints, sd->status.account_id, sd->status.char_id);
			points = MAX_ZENY-sd->kafraPoints;
		}

		pc_setaccountreg(sd, script->add_str("#KAFRAPOINTS"), sd->kafraPoints+points);

		if( battle_config.cashshop_show_points )
		{
			sprintf(output, msg_sd(sd,506), points, sd->kafraPoints);
			clif_disp_onlyself(sd, output);
		}
		return points;
	}
	else if( points < 0 )
	{
		ShowError("pc_getcash: Obtaining negative kafra points (points=%d, account_id=%d, char_id=%d).\n", points, sd->status.account_id, sd->status.char_id);
		return -1;
	}
	return -2; //shouldn't happen but jsut in case
}

/*==========================================
 * Attempts to give zeny to player (sd)
 * tsd (optional) from who for log (if null take sd)
 *------------------------------------------*/
int pc_getzeny(struct map_session_data *sd,int zeny, enum e_log_pick_type type, struct map_session_data *tsd)
{
	nullpo_retr(-1,sd);

	zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
	if( zeny < 0 )
	{
		ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
		return 1;
	}

	if( zeny > MAX_ZENY - sd->status.zeny )
		zeny = MAX_ZENY - sd->status.zeny;

	sd->status.zeny += zeny;
	clif->updatestatus(sd,SP_ZENY);

	if(!tsd) tsd = sd;
	logs->zeny(sd, type, tsd, zeny);
	if( zeny > 0 && sd->state.showzeny ) {
		char output[255];
		sprintf(output, "Gained %dz.", zeny);
		clif_disp_onlyself(sd, output);
	}

	return 0;
}

/**
 * Searches for the specified item ID in inventory and return its inventory index.
 *
 * If the item is found, the returned value is guaranteed to be a valid index
 * (non-negative, smaller than MAX_INVENTORY).
 *
 * @param sd      Character to search on.
 * @param item_id The item ID to search.
 * @return the inventory index of the first instance of the requested item.
 * @retval INDEX_NOT_FOUND if the item wasn't found.
 */
int pc_search_inventory(struct map_session_data *sd, int item_id) {
	int i;
	nullpo_retr(INDEX_NOT_FOUND, sd);

	ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == item_id && (sd->status.inventory[i].amount > 0 || item_id == 0) );
	return ( i < MAX_INVENTORY ) ? i : INDEX_NOT_FOUND;
}

/*==========================================
 * Attempt to add a new item to inventory.
 * Return:
 * 0 = success
 * 1 = invalid itemid not found or negative amount
 * 2 = overweight
 * 3 = ?
 * 4 = no free place found
 * 5 = max amount reached
 * 6 = ?
 * 7 = stack limitation
 *------------------------------------------*/
int pc_additem(struct map_session_data *sd,struct item *item_data,int amount,e_log_pick_type log_type)
{
	struct item_data *data;
	int i;
	unsigned int w;

	nullpo_retr(1, sd);
	nullpo_retr(1, item_data);

	if( item_data->nameid <= 0 || amount <= 0 )
		return 1;
	if( amount > MAX_AMOUNT )
		return 5;

	data = itemdb->search(item_data->nameid);

	if( data->stack.inventory && amount > data->stack.amount )
	{// item stack limitation
		return 7;
	}

	w = data->weight*amount;
	if(sd->weight + w > sd->max_weight)
		return 2;

	if( item_data->bound ) {
		switch( (enum e_item_bound_type)item_data->bound ) {
			case IBT_CHARACTER:
			case IBT_ACCOUNT:
				break; /* no restrictions */
			case IBT_PARTY:
				if( !sd->status.party_id ) {
					ShowError("pc_additem: can't add party_bound item to character without party!\n");
					ShowError("pc_additem: %s - x%d %s (%d)\n",sd->status.name,amount,data->jname,data->nameid);
					return 7;/* need proper code? */
				}
				break;
			case IBT_GUILD:
				if( !sd->status.guild_id ) {
					ShowError("pc_additem: can't add guild_bound item to character without guild!\n");
					ShowError("pc_additem: %s - x%d %s (%d)\n",sd->status.name,amount,data->jname,data->nameid);
					return 7;/* need proper code? */
				}
				break;
		}
	}

	i = MAX_INVENTORY;

	// Stackable | Non Rental
	if( itemdb->isstackable2(data) && item_data->expire_time == 0 ) {
		for( i = 0; i < MAX_INVENTORY; i++ ) {
			if( sd->status.inventory[i].nameid == item_data->nameid &&
			    sd->status.inventory[i].bound == item_data->bound &&
			    sd->status.inventory[i].expire_time == 0 &&
				sd->status.inventory[i].unique_id == item_data->unique_id &&
			    memcmp(&sd->status.inventory[i].card, &item_data->card, sizeof(item_data->card)) == 0 ) {
				if( amount > MAX_AMOUNT - sd->status.inventory[i].amount || ( data->stack.inventory && amount > data->stack.amount - sd->status.inventory[i].amount ) )
					return 5;
				sd->status.inventory[i].amount += amount;
				clif->additem(sd,i,amount,0);
				break;
			}
		}
	}

	if ( i >= MAX_INVENTORY ) {
		i = pc->search_inventory(sd,0);
		if (i == INDEX_NOT_FOUND)
			return 4;

		memcpy(&sd->status.inventory[i], item_data, sizeof(sd->status.inventory[0]));
		// clear equip and favorite fields first, just in case
		if( item_data->equip )
			sd->status.inventory[i].equip = 0;
		if( item_data->favorite )
			sd->status.inventory[i].favorite = 0;

		sd->status.inventory[i].amount = amount;
		sd->inventory_data[i] = data;
		clif->additem(sd,i,amount,0);
	}

	if( ( !itemdb->isstackable2(data) || data->flag.force_serial || data->type == IT_CASH) && !item_data->unique_id )
			sd->status.inventory[i].unique_id = itemdb->unique_id(sd);

	logs->pick_pc(sd, log_type, amount, &sd->status.inventory[i],sd->inventory_data[i]);

	sd->weight += w;
	clif->updatestatus(sd,SP_WEIGHT);
	//Auto-equip
	if(data->flag.autoequip)
		pc->equipitem(sd, i, data->equip);

	/* rental item check */
	if( item_data->expire_time ) {
		if( time(NULL) > item_data->expire_time ) {
			pc->rental_expire(sd,i);
		} else {
			int seconds = (int)( item_data->expire_time - time(NULL) );
			clif->rental_time(sd->fd, sd->status.inventory[i].nameid, seconds);
			pc->inventory_rental_add(sd, seconds);
		}
	}

	return 0;
}

/*==========================================
 * Remove an item at index n from inventory by amount.
 * Parameters :
 * @type
 *   1 : don't notify deletion
 *   2 : don't notify weight change
 * reason: @see enum delitem_reason
 * Return:
 *   0 = success
 *   1 = invalid itemid or negative amount
 *------------------------------------------*/
int pc_delitem(struct map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
{
	nullpo_retr(1, sd);
	Assert_retr(1, n >= 0 && n < MAX_INVENTORY);

	if(sd->status.inventory[n].nameid==0 || amount <= 0 || sd->status.inventory[n].amount<amount || sd->inventory_data[n] == NULL)
		return 1;

	logs->pick_pc(sd, log_type, -amount, &sd->status.inventory[n],sd->inventory_data[n]);

	sd->status.inventory[n].amount -= amount;
	sd->weight -= sd->inventory_data[n]->weight*amount ;
	if( sd->status.inventory[n].amount <= 0 ){
		if(sd->status.inventory[n].equip)
			pc->unequipitem(sd, n, PCUNEQUIPITEM_RECALC|PCUNEQUIPITEM_FORCE);
		memset(&sd->status.inventory[n],0,sizeof(sd->status.inventory[0]));
		sd->inventory_data[n] = NULL;
	}
	if(!(type&1))
		clif->delitem(sd,n,amount,reason);
	if(!(type&2))
		clif->updatestatus(sd,SP_WEIGHT);

	return 0;
}

/*==========================================
 * Attempt to drop an item.
 * Return:
 *   0 = fail
 *   1 = success
 *------------------------------------------*/
int pc_dropitem(struct map_session_data *sd,int n,int amount)
{
	nullpo_retr(1, sd);

	if(n < 0 || n >= MAX_INVENTORY)
		return 0;

	if(amount <= 0)
		return 0;

	if(sd->status.inventory[n].nameid <= 0 ||
		sd->status.inventory[n].amount <= 0 ||
		sd->status.inventory[n].amount < amount ||
		sd->state.trading || sd->state.vending ||
		!sd->inventory_data[n] //pc->delitem would fail on this case.
		)
		return 0;

	if( map->list[sd->bl.m].flag.nodrop ) {
		clif->message (sd->fd, msg_sd(sd,271));
		return 0; //Can't drop items in nodrop mapflag maps.
	}

	if( !pc->candrop(sd,&sd->status.inventory[n]) )
	{
		clif->message (sd->fd, msg_sd(sd,263));
		return 0;
	}

	if (!map->addflooritem(&sd->bl, &sd->status.inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2))
		return 0;

	pc->delitem(sd, n, amount, 1, DELITEM_NORMAL, LOG_TYPE_PICKDROP_PLAYER);
	clif->dropitem(sd, n, amount);
	return 1;
}

/*==========================================
 * Attempt to pick up an item.
 * Return:
 *   0 = fail
 *   1 = success
 *------------------------------------------*/
int pc_takeitem(struct map_session_data *sd,struct flooritem_data *fitem)
{
	int flag=0;
	int64 tick = timer->gettick();
	struct party_data *p=NULL;

	nullpo_ret(sd);
	nullpo_ret(fitem);

	if(!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
		return 0; // Distance is too far

	if( pc_has_permission(sd,PC_PERM_DISABLE_PICK_UP) )
		return 0;

	if (sd->status.party_id)
		p = party->search(sd->status.party_id);

	if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
		struct map_session_data *first_sd = map->charid2sd(fitem->first_get_charid);
		if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
			if (!(p && p->party.item&1 &&
				first_sd && first_sd->status.party_id == sd->status.party_id
			))
				return 0;
		} else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
			struct map_session_data *second_sd = map->charid2sd(fitem->second_get_charid);
			if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
				if (!(p && p->party.item&1 &&
					((first_sd && first_sd->status.party_id == sd->status.party_id) ||
					(second_sd && second_sd->status.party_id == sd->status.party_id))
				))
					return 0;
			} else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id) {
				struct map_session_data *third_sd = map->charid2sd(fitem->third_get_charid);
				if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
					if (!(p && p->party.item&1 &&
						((first_sd && first_sd->status.party_id == sd->status.party_id) ||
						(second_sd && second_sd->status.party_id == sd->status.party_id) ||
						(third_sd && third_sd->status.party_id == sd->status.party_id))
					))
						return 0;
				}
			}
		}
	}

	//This function takes care of giving the item to whoever should have it, considering party-share options.
	if ((flag = party->share_loot(p,sd,&fitem->item_data, fitem->first_get_charid))) {
		clif->additem(sd,0,0,flag);
		return 1;
	}

	//Display pickup animation.
	pc_stop_attack(sd);
	clif->takeitem(&sd->bl,&fitem->bl);
	map->clearflooritem(&fitem->bl);
	return 1;
}

/*==========================================
 * Check if item is usable.
 * Return:
 *   0 = no
 *   1 = yes
 *------------------------------------------*/
int pc_isUseitem(struct map_session_data *sd,int n)
{
	struct item_data *item;
	int nameid;

	nullpo_ret(sd);
	Assert_ret(n >= 0 && n < MAX_INVENTORY);

	item = sd->inventory_data[n];
	nameid = sd->status.inventory[n].nameid;

	if( item == NULL )
		return 0;
	//Not consumable item
	if (!itemdb->is_item_usable(item))
		return 0;
	if( !item->script ) //if it has no script, you can't really consume it!
		return 0;

	if ((item->item_usage.flag&INR_SITTING) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override)) {
		clif->msgtable(sd, MSG_ITEM_NEED_STANDING);
		//clif->messagecolor_self(sd->fd, COLOR_WHITE, msg_txt(1474));
		return 0; // You cannot use this item while sitting.
	}

	if (sd->state.storage_flag != STORAGE_FLAG_CLOSED && item->type != IT_CASH) {
		clif->messagecolor_self(sd->fd, COLOR_RED, msg_sd(sd,1475));
		return 0; // You cannot use this item while storage is open.
	}

	switch( nameid ) { // TODO: Is there no better way to handle this, other than hardcoding item IDs?
		case ITEMID_ANODYNE:
			if( map_flag_gvg2(sd->bl.m) )
				return 0;
			/* Fall through */
		case ITEMID_ALOEBERA:
			if( pc_issit(sd) )
				return 0;
			break;
		case ITEMID_WING_OF_FLY:
		case ITEMID_GIANT_FLY_WING:
			if( map->list[sd->bl.m].flag.noteleport || map_flag_gvg2(sd->bl.m) ) {
				clif->skill_mapinfomessage(sd,0);
				return 0;
			}
			/* Fall through */
		case ITEMID_WING_OF_BUTTERFLY:
		case ITEMID_DUN_TELE_SCROLL1:
		case ITEMID_DUN_TELE_SCROLL2:
		case ITEMID_WOB_RUNE:     // Yellow Butterfly Wing
		case ITEMID_WOB_SCHWALTZ: // Green Butterfly Wing
		case ITEMID_WOB_RACHEL:   // Red Butterfly Wing
		case ITEMID_WOB_LOCAL:    // Blue Butterfly Wing
		case ITEMID_SIEGE_TELEPORT_SCROLL:
			if( sd->duel_group && !battle_config.duel_allow_teleport ) {
				clif->message(sd->fd, msg_sd(sd,863)); // "Duel: Can't use this item in duel."
				return 0;
			}
			if( nameid != ITEMID_WING_OF_FLY && nameid != ITEMID_GIANT_FLY_WING && map->list[sd->bl.m].flag.noreturn )
				return 0;
			break;
		case ITEMID_BRANCH_OF_DEAD_TREE:
		case ITEMID_RED_POUCH_OF_SURPRISE:
		case ITEMID_BLOODY_DEAD_BRANCH:
		case ITEMID_PORING_BOX:
			if( map->list[sd->bl.m].flag.nobranch || map_flag_gvg2(sd->bl.m) )
				return 0;
			break;
		case ITEMID_BUBBLE_GUM:
		case ITEMID_COMP_BUBBLE_GUM:
			if( sd->sc.data[SC_CASH_RECEIVEITEM] )
				return 0;
			break;
		case ITEMID_BATTLE_MANUAL:
		case ITEMID_COMP_BATTLE_MANUAL:
		case ITEMID_THICK_MANUAL50:
		case ITEMID_NOBLE_NAMEPLATE:
		case ITEMID_BATTLE_MANUAL25:
		case ITEMID_BATTLE_MANUAL100:
		case ITEMID_BATTLE_MANUAL_X3:
			if( sd->sc.data[SC_CASH_PLUSEXP] )
				return 0;
			break;
		case ITEMID_JOB_MANUAL50:
			if( sd->sc.data[SC_CASH_PLUSONLYJOBEXP] )
				return 0;
			break;

		// Mercenary Items
		case ITEMID_MERCENARY_RED_POTION:
		case ITEMID_MERCENARY_BLUE_POTION:
		case ITEMID_M_CENTER_POTION:
		case ITEMID_M_AWAKENING_POTION:
		case ITEMID_M_BERSERK_POTION:
			if( sd->md == NULL || sd->md->db == NULL )
				return 0;
			if (sd->md->sc.data[SC_BERSERK])
				return 0;
			if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
				return 0;
			if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
				return 0;
			break;

		case ITEMID_NEURALIZER:
			if( !map->list[sd->bl.m].flag.reset )
				return 0;
			break;
	}

	if( nameid >= ITEMID_BOW_MERCENARY_SCROLL1 && nameid <= ITEMID_SPEARMERCENARY_SCROLL10 && sd->md != NULL ) // Mercenary Scrolls
		return 0;

	/**
	 * Only Rune Knights may use runes
	 **/
	if( itemdb_is_rune(nameid) && (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
		return 0;
	/**
	 * Only GCross may use poisons
	 **/
	else if( itemdb_is_poison(nameid) && (sd->class_&MAPID_THIRDMASK) != MAPID_GUILLOTINE_CROSS )
		return 0;

	if( item->package || item->group ) {
		if (pc_is90overweight(sd)) {
			clif->msgtable(sd, MSG_ITEM_CANT_OBTAIN_WEIGHT);
			return 0;
		}
		if (!pc->inventoryblank(sd)) {
			clif->messagecolor_self(sd->fd, COLOR_RED, msg_sd(sd,1477));
			return 0;
		}
	}

	//Gender check
	if(item->sex != 2 && sd->status.sex != item->sex)
		return 0;
	//Required level check
	if (item->elv && sd->status.base_level < (unsigned int)item->elv) {
		clif->msgtable(sd, MSG_ITEM_CANT_USE_LVL);
		return 0;
	}

	if (item->elvmax && sd->status.base_level > (unsigned int)item->elvmax) {
		clif->msgtable(sd, MSG_ITEM_CANT_USE_LVL);
		return 0;
	}

	//Not equipable by class. [Skotlex]
	if (!(
		(1ULL<<(sd->class_&MAPID_BASEMASK)) &
		(item->class_base[(sd->class_&JOBL_2_1) ? 1 : ((sd->class_&JOBL_2_2) ? 2 : 0)])
	))
		return 0;

	//Not usable by upper class. [Haru]
	while( 1 ) {
		// Normal classes (no upper, no baby, no third classes)
		if( item->class_upper&ITEMUPPER_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_THIRD|JOBL_BABY)) ) break;
#ifdef RENEWAL
		// Upper classes (no third classes)
		if( item->class_upper&ITEMUPPER_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD) ) break;
#else
		//pre-re has no use for the extra, so we maintain the previous for backwards compatibility
		if( item->class_upper&ITEMUPPER_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD) ) break;
#endif
		// Baby classes (no third classes)
		if( item->class_upper&ITEMUPPER_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD) ) break;
		// Third classes (no upper, no baby classes)
		if( item->class_upper&ITEMUPPER_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)) ) break;
		// Upper third classes
		if( item->class_upper&ITEMUPPER_THURDUPPER && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER ) break;
		// Baby third classes
		if( item->class_upper&ITEMUPPER_THIRDBABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY ) break;
		return 0;
	}

	return 1;
}

/*==========================================
 * Last checks to use an item.
 * Return:
 *   0 = fail
 *   1 = success
 *------------------------------------------*/
int pc_useitem(struct map_session_data *sd,int n) {
	int64 tick = timer->gettick();
	int amount, nameid, i;
	bool removeItem = false;

	nullpo_ret(sd);
	Assert_ret(n >= 0 && n < MAX_INVENTORY);

	if( sd->npc_id || sd->state.workinprogress&1 ){
		/* TODO: add to clif->messages enum */
#ifdef RENEWAL
		clif->msgtable(sd, MSG_NPC_WORK_IN_PROGRESS); // TODO look for the client date that has this message.
#endif
		return 0;
	}

	if( sd->status.inventory[n].nameid <= 0 || sd->status.inventory[n].amount <= 0 )
		return 0;

	if( !pc->isUseitem(sd,n) )
		return 0;

	// Store information for later use before it is lost (via pc->delitem) [Paradox924X]
	nameid = sd->inventory_data[n]->nameid;

	if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
		return 0;

	// Statuses that don't let the player use items
	if (sd->sc.count && (
		sd->sc.data[SC_BERSERK] ||
		(sd->sc.data[SC_GRAVITATION] && sd->sc.data[SC_GRAVITATION]->val3 == BCT_SELF) ||
		sd->sc.data[SC_TRICKDEAD] ||
		sd->sc.data[SC_HIDING] ||
		sd->sc.data[SC__SHADOWFORM] ||
		sd->sc.data[SC__INVISIBILITY] ||
		sd->sc.data[SC__MANHOLE] ||
		sd->sc.data[SC_KG_KAGEHUMI] ||
		sd->sc.data[SC_WHITEIMPRISON] ||
		sd->sc.data[SC_DEEP_SLEEP] ||
		sd->sc.data[SC_SATURDAY_NIGHT_FEVER] ||
		sd->sc.data[SC_COLD] ||
		pc_ismuted(&sd->sc, MANNER_NOITEM)
	    ))
		return 0;

	//Prevent mass item usage. [Skotlex]
	if( DIFF_TICK(sd->canuseitem_tick, tick) > 0 ||
		(itemdb_iscashfood(nameid) && DIFF_TICK(sd->canusecashfood_tick, tick) > 0)
	)
		return 0;

	/* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
	if( sd->inventory_data[n]->flag.delay_consume && nameid != ITEMID_REINS_OF_MOUNT ) {
		if( sd->sc.data[SC_ALL_RIDING] )
			return 0;
		else if( pc_issit(sd) )
			return 0;
	}
	//Since most delay-consume items involve using a "skill-type" target cursor,
	//perform a skill-use check before going through. [Skotlex]
	//resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
	//FIXME: Is this really needed here? It'll be checked in unit.c after all and this prevents skill items using when silenced [Inkfish]
	if( sd->inventory_data[n]->flag.delay_consume && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status->check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
		return 0;

	if( sd->inventory_data[n]->delay > 0 ) {
		ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == nameid );
			if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
				ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
		if( i < MAX_ITEMDELAYS ) {
			if( sd->item_delay[i].nameid ) {// found
				if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
					int e_tick = (int)(DIFF_TICK(sd->item_delay[i].tick, tick)/1000);
					clif->msgtable_num(sd, MSG_SECONDS_UNTIL_USE, e_tick + 1); // [%d] seconds left until you can use
					return 0; // Delay has not expired yet
				}
			} else {// not yet used item (all slots are initially empty)
				sd->item_delay[i].nameid = nameid;
			}
			if (!(nameid == ITEMID_REINS_OF_MOUNT && pc_hasmount(sd)))
				sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay;
		} else {// should not happen
			ShowError("pc_useitem: Exceeded item delay array capacity! (nameid=%d, char_id=%d)\n", nameid, sd->status.char_id);
		}
		//clean up used delays so we can give room for more
		for(i = 0; i < MAX_ITEMDELAYS; i++) {
			if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
				sd->item_delay[i].tick = 0;
				sd->item_delay[i].nameid = 0;
			}
		}
	}

	/* on restricted maps the item is consumed but the effect is not used */
	for(i = 0; i < map->list[sd->bl.m].zone->disabled_items_count; i++) {
		if( map->list[sd->bl.m].zone->disabled_items[i] == nameid ) {
			clif->msgtable(sd, MSG_ITEM_CANT_USE_AREA); // This item cannot be used within this area
			if( battle_config.item_restricted_consumption_type && sd->status.inventory[n].expire_time == 0 ) {
				clif->useitemack(sd,n,sd->status.inventory[n].amount-1,true);
				pc->delitem(sd, n, 1, 1, DELITEM_NORMAL, LOG_TYPE_CONSUME);
			}
			return 0;
		}
	}

	//Dead Branch & Bloody Branch & Porings Box
	if( nameid == ITEMID_BRANCH_OF_DEAD_TREE || nameid == ITEMID_BLOODY_DEAD_BRANCH || nameid == ITEMID_PORING_BOX )
		logs->branch(sd);

	sd->itemid = sd->status.inventory[n].nameid;
	sd->itemindex = n;
	if(sd->catch_target_class != -1) //Abort pet catching.
		sd->catch_target_class = -1;

	amount = sd->status.inventory[n].amount;
	//Check if the item is to be consumed immediately [Skotlex]
	if (sd->inventory_data[n]->flag.delay_consume || sd->inventory_data[n]->flag.keepafteruse)
		clif->useitemack(sd,n,amount,true);
	else {
		if (sd->status.inventory[n].expire_time == 0) {
			clif->useitemack(sd, n, amount - 1, true);
			removeItem = true;
		} else {
			clif->useitemack(sd, n, 0, false);
		}
	}

	if(sd->status.inventory[n].card[0]==CARD0_CREATE &&
		pc->famerank(MakeDWord(sd->status.inventory[n].card[2],sd->status.inventory[n].card[3]), MAPID_ALCHEMIST))
	{
		script->potion_flag = 2; // Famous player's potions have 50% more efficiency
		if (sd->sc.data[SC_SOULLINK] && sd->sc.data[SC_SOULLINK]->val2 == SL_ROGUE)
			script->potion_flag = 3; //Even more effective potions.
	}

	//Update item use time.
	sd->canuseitem_tick = tick + battle_config.item_use_interval;
	if( itemdb_iscashfood(nameid) )
		sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;

	script->run_use_script(sd, sd->inventory_data[n], npc->fake_nd->bl.id);
	script->potion_flag = 0;

	if (removeItem)
		pc->delitem(sd, n, 1, 1, DELITEM_NORMAL, LOG_TYPE_CONSUME);
	return 1;
}

/*==========================================
 * Add item on cart for given index.
 * Return:
 *   0 = success
 *   1 = fail
 *------------------------------------------*/
int pc_cart_additem(struct map_session_data *sd,struct item *item_data,int amount,e_log_pick_type log_type)
{
	struct item_data *data;
	int i,w;

	nullpo_retr(1, sd);
	nullpo_retr(1, item_data);

	if(item_data->nameid <= 0 || amount <= 0)
		return 1;
	data = itemdb->search(item_data->nameid);

	if( data->stack.cart && amount > data->stack.amount )
	{// item stack limitation
		return 1;
	}

	if (!itemdb_cancartstore(item_data, pc_get_group_level(sd)) || (item_data->bound > IBT_ACCOUNT && !pc_can_give_bound_items(sd))) {
		// Check item trade restrictions
		clif->message (sd->fd, msg_sd(sd,264));
		return 1;/* TODO: there is no official response to this? */
	}

	if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
		return 1;

	i = MAX_CART;
	if( itemdb->isstackable2(data) && !item_data->expire_time )
	{
		ARR_FIND( 0, MAX_CART, i,
			sd->status.cart[i].nameid == item_data->nameid && sd->status.cart[i].bound == item_data->bound &&
			sd->status.cart[i].card[0] == item_data->card[0] && sd->status.cart[i].card[1] == item_data->card[1] &&
			sd->status.cart[i].card[2] == item_data->card[2] && sd->status.cart[i].card[3] == item_data->card[3] );
	};

	if( i < MAX_CART && item_data->unique_id == sd->status.cart[i].unique_id)
	{// item already in cart, stack it
		if( amount > MAX_AMOUNT - sd->status.cart[i].amount || ( data->stack.cart && amount > data->stack.amount - sd->status.cart[i].amount ) )
			return 2; // no room

		sd->status.cart[i].amount+=amount;
		clif->cart_additem(sd,i,amount,0);
	}
	else
	{// item not stackable or not present, add it
		ARR_FIND( 0, MAX_CART, i, sd->status.cart[i].nameid == 0 );
		if( i == MAX_CART )
			return 2; // no room

		memcpy(&sd->status.cart[i],item_data,sizeof(sd->status.cart[0]));
		sd->status.cart[i].amount=amount;
		sd->cart_num++;
		clif->cart_additem(sd,i,amount,0);
	}
	sd->status.cart[i].favorite = 0;/* clear */
	logs->pick_pc(sd, log_type, amount, &sd->status.cart[i],data);

	sd->cart_weight += w;
	clif->updatestatus(sd,SP_CARTINFO);

	return 0;
}

/*==========================================
 * Delete item on cart for given index.
 * Return:
 *   0 = success
 *   1 = fail
 *------------------------------------------*/
int pc_cart_delitem(struct map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type) {
	struct item_data * data;
	nullpo_retr(1, sd);
	Assert_retr(1, n >= 0 && n < MAX_INVENTORY);

	if( sd->status.cart[n].nameid == 0 || sd->status.cart[n].amount < amount || !(data = itemdb->exists(sd->status.cart[n].nameid)) )
		return 1;

	logs->pick_pc(sd, log_type, -amount, &sd->status.cart[n],data);

	sd->status.cart[n].amount -= amount;
	sd->cart_weight -= data->weight*amount ;
	if(sd->status.cart[n].amount <= 0){
		memset(&sd->status.cart[n],0,sizeof(sd->status.cart[0]));
		sd->cart_num--;
	}
	if(!type) {
		clif->cart_delitem(sd,n,amount);
		clif->updatestatus(sd,SP_CARTINFO);
	}

	return 0;
}

/*==========================================
 * Transfer item from inventory to cart.
 * Return:
 *   0 = fail
 *   1 = succes
 *------------------------------------------*/
int pc_putitemtocart(struct map_session_data *sd,int idx,int amount)
{
	struct item *item_data;
	int flag;

	nullpo_ret(sd);

	if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
		return 1;

	item_data = &sd->status.inventory[idx];

	if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending )
		return 1;

	if( (flag = pc->cart_additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0 )
		return pc->delitem(sd, idx, amount, 0, DELITEM_TOCART, LOG_TYPE_NONE);

	return flag;
}

/*==========================================
 * Get number of item in cart.
 * Return:
 * -1 = itemid not found or no amount found
 * x  = remaining itemid on cart after get
 *------------------------------------------*/
int pc_cartitem_amount(struct map_session_data* sd, int idx, int amount)
{
	struct item* item_data;

	nullpo_retr(-1, sd);
	Assert_retr(-1, idx >= 0 && idx < MAX_CART);

	item_data = &sd->status.cart[idx];
	if( item_data->nameid == 0 || item_data->amount == 0 )
		return -1;

	return item_data->amount - amount;
}

/*==========================================
 * Retrieve an item at index idx from cart.
 * Return:
 *   0 = player not found or (FIXME) succes (from pc->cart_delitem)
 *   1 = failure
 *------------------------------------------*/
int pc_getitemfromcart(struct map_session_data *sd,int idx,int amount)
{
	struct item *item_data;
	int flag;

	nullpo_ret(sd);

	if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
		return 1;

	item_data=&sd->status.cart[idx];

	if(item_data->nameid==0 || amount < 1 || item_data->amount<amount || sd->state.vending )
		return 1;

	if((flag = pc->additem(sd,item_data,amount,LOG_TYPE_NONE)) == 0)
		return pc->cart_delitem(sd,idx,amount,0,LOG_TYPE_NONE);

	return flag;
}

void pc_bound_clear(struct map_session_data *sd, enum e_item_bound_type type)
{
	int i;

	nullpo_retv(sd);
	switch( type ) {
		/* both restricted to inventory */
		case IBT_PARTY:
		case IBT_CHARACTER:
			for( i = 0; i < MAX_INVENTORY; i++ ){
				if( sd->status.inventory[i].bound == type ) {
					pc->delitem(sd, i, sd->status.inventory[i].amount, 0, DELITEM_SKILLUSE, LOG_TYPE_OTHER); // FIXME: is this the correct reason flag?
				}
			}
			break;
		case IBT_ACCOUNT:
			ShowError("Helllo! You reached pc_bound_clear for IBT_ACCOUNT, unfortunately no scenario was expected for this!\n");
			break;
		case IBT_GUILD: {
				struct guild_storage *gstor = idb_get(gstorage->db,sd->status.guild_id);

				for( i = 0; i < MAX_INVENTORY; i++ ){
					if(sd->status.inventory[i].bound == type) {
						if( gstor )
							gstorage->additem(sd,gstor,&sd->status.inventory[i],sd->status.inventory[i].amount);
						pc->delitem(sd, i, sd->status.inventory[i].amount, 0, DELITEM_SKILLUSE, gstor ? LOG_TYPE_GSTORAGE : LOG_TYPE_OTHER); // FIXME: is this the correct reason flag?
					}
				}
				if( gstor )
					gstorage->close(sd);
			}
			break;
	}
}
/*==========================================
 *  Display item stolen msg to player sd
 *------------------------------------------*/
int pc_show_steal(struct block_list *bl,va_list ap)
{
	struct map_session_data *sd = NULL, *tsd = NULL;
	int itemid;

	struct item_data *item=NULL;
	char output[100];

	sd=va_arg(ap,struct map_session_data *);
	itemid=va_arg(ap,int);

	nullpo_ret(bl);
	Assert_ret(bl->type == BL_PC);
	tsd = BL_UCAST(BL_PC, bl);
	nullpo_ret(sd);

	if((item=itemdb->exists(itemid))==NULL)
		sprintf(output,"%s stole an Unknown Item (id: %i).",sd->status.name, itemid);
	else
		sprintf(output,"%s stole %s.",sd->status.name,item->jname);
	clif->message(tsd->fd, output);

	return 0;
}
/*==========================================
 * Steal an item from bl (mob).
 * Return:
 *   0 = fail
 *   1 = succes
 *------------------------------------------*/
int pc_steal_item(struct map_session_data *sd,struct block_list *bl, uint16 skill_lv)
{
	int i,itemid,flag;
	int rate;
	struct status_data *sd_status, *md_status;
	struct mob_data *md = BL_CAST(BL_MOB, bl);
	struct item tmp_item;
	struct item_data *data = NULL;

	if (sd == NULL || md == NULL)
		return 0;

	if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING && md->sc.opt1 != OPT1_CRYSTALIZE ) ) //already stolen from / status change check
		return 0;

	sd_status= status->get_status_data(&sd->bl);
	md_status= status->get_status_data(bl);

	if (md->master_id || md_status->mode&MD_BOSS || mob_is_treasure(md) ||
		map->list[bl->m].flag.nomobloot || // check noloot map flag [Lorky]
		(battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
			md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
	) { //Can't steal from
		md->state.steal_flag = UCHAR_MAX;
		return 0;
	}

	// base skill success chance (percentual)
	rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4 + sd->bonus.add_steal_rate;

	if( rate < 1 )
		return 0;

	// Try dropping one item, in the order from first to last possible slot.
	// Droprate is affected by the skill success rate.
	for (i = 0; i < MAX_STEAL_DROP; i++) {
		if (md->db->dropitem[i].nameid == 0)
			continue;
		if ((data = itemdb->exists(md->db->dropitem[i].nameid)) == NULL)
			continue;
		if (rnd() % 10000 < apply_percentrate(md->db->dropitem[i].p, rate, 100))
			break;
	}
	if (i == MAX_STEAL_DROP)
		return 0;

	itemid = md->db->dropitem[i].nameid;
	memset(&tmp_item,0,sizeof(tmp_item));
	tmp_item.nameid = itemid;
	tmp_item.amount = 1;
	tmp_item.identify = itemdb->isidentified2(data);
	flag = pc->additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);

	//TODO: Should we disable stealing when the item you stole couldn't be added to your inventory? Perhaps players will figure out a way to exploit this behaviour otherwise?
	md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more

	if(flag) { //Failed to steal due to overweight
		clif->additem(sd,0,0,flag);
		return 0;
	}

	if(battle_config.show_steal_in_same_party)
		party->foreachsamemap(pc->show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);

	//Logs items, Stolen from mobs [Lupus]
	logs->pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item, data);

	//A Rare Steal Global Announce by Lupus
	if(md->db->dropitem[i].p<=battle_config.rare_drop_announce) {
		char message[128];
		sprintf (message, msg_txt(542), sd->status.name, md->db->jname, data->jname, (float)md->db->dropitem[i].p / 100);
		//MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
		intif->broadcast(message, (int)strlen(message)+1, BC_DEFAULT);
	}
	return 1;
}

/**
 * Steals zeny from a monster through the RG_STEALCOIN skill.
 *
 * @param sd     Source character
 * @param target Target monster
 *
 * @return Amount of stolen zeny (0 in case of failure)
 **/
int pc_steal_coin(struct map_session_data *sd, struct block_list *target) {
	int rate, skill_lv;
	struct mob_data *md = BL_CAST(BL_MOB, target);

	if (sd == NULL || md == NULL)
		return 0;

	if (md->state.steal_coin_flag || md->sc.data[SC_STONE] || md->sc.data[SC_FREEZE] || md->status.mode&MD_BOSS)
		return 0;

	if (mob_is_treasure(md))
		return 0;

	skill_lv = pc->checkskill(sd, RG_STEALCOIN);
	rate = skill_lv * 10 + (sd->status.base_level - md->level) * 2 + sd->battle_status.dex / 2 + sd->battle_status.luk / 2;
	if(rnd()%1000 < rate) {
		int amount = md->level * skill_lv / 10 + md->level * 8 + rnd()%(md->level * 2 + 1); // mob_lv * skill_lv / 10 + random [mob_lv*8; mob_lv*10]

		pc->getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
		md->state.steal_coin_flag = 1;
		return amount;
	}
	return 0;
}

/*==========================================
 * Set's a player position.
 * Return values:
 * 0 - Success.
 * 1 - Invalid map index.
 * 2 - Map not in this map-server, and failed to locate alternate map-server.
 *------------------------------------------*/
int pc_setpos(struct map_session_data* sd, unsigned short map_index, int x, int y, clr_type clrtype) {
	int16 m;

	nullpo_ret(sd);

	if( !map_index || !mapindex_id2name(map_index) || ( m = map->mapindex2mapid(map_index) ) == -1 ) {
		ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", map_index);
		return 1;
	}

	if( pc_isdead(sd) ) { //Revive dead people before warping them
		pc->setstand(sd);
		pc->setrestartvalue(sd,1);
	}

	if( map->list[m].flag.src4instance ) {
		struct party_data *p;
		bool stop = false;
		int i = 0, j = 0;

		if( sd->instances ) {
			for( i = 0; i < sd->instances; i++ ) {
				if( sd->instance[i] >= 0 ) {
					ARR_FIND(0, instance->list[sd->instance[i]].num_map, j, map->list[instance->list[sd->instance[i]].map[j]].instance_src_map == m && !map->list[instance->list[sd->instance[i]].map[j]].custom_name);
					if( j != instance->list[sd->instance[i]].num_map )
						break;
				}
			}
			if( i != sd->instances ) {
				m = instance->list[sd->instance[i]].map[j];
				map_index = map_id2index(m);
				stop = true;
			}
		}
		if ( !stop && sd->status.party_id && (p = party->search(sd->status.party_id)) != NULL && p->instances ) {
			for( i = 0; i < p->instances; i++ ) {
				if( p->instance[i] >= 0 ) {
					ARR_FIND(0, instance->list[p->instance[i]].num_map, j, map->list[instance->list[p->instance[i]].map[j]].instance_src_map == m && !map->list[instance->list[p->instance[i]].map[j]].custom_name);
					if( j != instance->list[p->instance[i]].num_map )
						break;
				}
			}
			if( i != p->instances ) {
				m = instance->list[p->instance[i]].map[j];
				map_index = map_id2index(m);
				stop = true;
			}
		}
		if ( !stop && sd->status.guild_id && sd->guild && sd->guild->instances ) {
			for( i = 0; i < sd->guild->instances; i++ ) {
				if( sd->guild->instance[i] >= 0 ) {
					ARR_FIND(0, instance->list[sd->guild->instance[i]].num_map, j, map->list[instance->list[sd->guild->instance[i]].map[j]].instance_src_map == m && !map->list[instance->list[sd->guild->instance[i]].map[j]].custom_name);
					if( j != instance->list[sd->guild->instance[i]].num_map )
						break;
				}
			}
			if( i != sd->guild->instances ) {
				m = instance->list[sd->guild->instance[i]].map[j];
				map_index = map_id2index(m);
				//stop = true; Uncomment if adding new checks
			}
		}

		/* we hit a instance, if empty we populate the spawn data */
		if( map->list[m].instance_id >= 0 && instance->list[map->list[m].instance_id].respawn.map == 0 &&
		    instance->list[map->list[m].instance_id].respawn.x == 0 &&
		    instance->list[map->list[m].instance_id].respawn.y == 0) {
			instance->list[map->list[m].instance_id].respawn.map = map_index;
			instance->list[map->list[m].instance_id].respawn.x = x;
			instance->list[map->list[m].instance_id].respawn.y = y;
		}
	}

	sd->state.changemap = (sd->mapindex != map_index);
	sd->state.warping = 1;
	sd->state.workinprogress = 0;
	if( sd->state.changemap ) { // Misc map-changing settings
		int i;
		sd->state.pmap = sd->bl.m;

		for (i = 0; i < VECTOR_LENGTH(sd->script_queues); i++) {
			struct script_queue *queue = script->queue(VECTOR_INDEX(sd->script_queues, i));
			if (queue && queue->event_mapchange[0] != '\0') {
				pc->setregstr(sd, script->add_str("@Queue_Destination_Map$"), map->list[m].name);
				npc->event(sd, queue->event_mapchange, 0);
			}
		}

		if( map->list[m].cell == (struct mapcell *)0xdeadbeaf )
			map->cellfromcache(&map->list[m]);
		if (sd->sc.count) { // Cancel some map related stuff.
			if (sd->sc.data[SC_JAILED])
				return 1; //You may not get out!
			status_change_end(&sd->bl, SC_CASH_BOSS_ALARM, INVALID_TIMER);
			status_change_end(&sd->bl, SC_WARM, INVALID_TIMER);
			status_change_end(&sd->bl, SC_SUN_COMFORT, INVALID_TIMER);
			status_change_end(&sd->bl, SC_MOON_COMFORT, INVALID_TIMER);
			status_change_end(&sd->bl, SC_STAR_COMFORT, INVALID_TIMER);
			status_change_end(&sd->bl, SC_MIRACLE, INVALID_TIMER);
			status_change_end(&sd->bl, SC_NEUTRALBARRIER_MASTER, INVALID_TIMER);//Will later check if this is needed. [Rytech]
			status_change_end(&sd->bl, SC_NEUTRALBARRIER, INVALID_TIMER);
			status_change_end(&sd->bl, SC_STEALTHFIELD_MASTER, INVALID_TIMER);
			status_change_end(&sd->bl, SC_STEALTHFIELD, INVALID_TIMER);
			if (sd->sc.data[SC_KNOWLEDGE]) {
				struct status_change_entry *sce = sd->sc.data[SC_KNOWLEDGE];
				if (sce->timer != INVALID_TIMER)
					timer->delete(sce->timer, status->change_timer);
				sce->timer = timer->add(timer->gettick() + skill->get_time(SG_KNOWLEDGE, sce->val1), status->change_timer, sd->bl.id, SC_KNOWLEDGE);
			}
			status_change_end(&sd->bl, SC_PROPERTYWALK, INVALID_TIMER);
			status_change_end(&sd->bl, SC_CLOAKING, INVALID_TIMER);
			status_change_end(&sd->bl, SC_CLOAKINGEXCEED, INVALID_TIMER);
		}
		for( i = 0; i < EQI_MAX; i++ ) {
			if( sd->equip_index[ i ] >= 0 )
				if( !pc->isequip( sd , sd->equip_index[ i ] ) )
					pc->unequipitem(sd, sd->equip_index[i], PCUNEQUIPITEM_FORCE);
		}
		if (battle_config.clear_unit_onwarp&BL_PC)
			skill->clear_unitgroup(&sd->bl);
		party->send_dot_remove(sd); //minimap dot fix [Kevin]
		guild->send_dot_remove(sd);
		bg->send_dot_remove(sd);
		if (sd->regen.state.gc)
			sd->regen.state.gc = 0;
		// make sure vending is allowed here
		if (sd->state.vending && map->list[m].flag.novending) {
			clif->message (sd->fd, msg_sd(sd,276)); // "You can't open a shop on this map"
			vending->close(sd);
		}

		if (map->list[sd->bl.m].channel) {
			channel->leave(map->list[sd->bl.m].channel,sd);
		}
	}

	if( m < 0 ) {
		uint32 ip;
		uint16 port;
		//if can't find any map-servers, just abort setting position.
		if(!sd->mapindex || map->mapname2ipport(map_index,&ip,&port))
			return 2;

		if (sd->npc_id)
			npc->event_dequeue(sd);
		npc->script_event(sd, NPCE_LOGOUT);
		//remove from map, THEN change x/y coordinates
		unit->remove_map_pc(sd,clrtype);
		sd->mapindex = map_index;
		sd->bl.x=x;
		sd->bl.y=y;
		pc->clean_skilltree(sd);
		chrif->save(sd,2);
		chrif->changemapserver(sd, ip, (short)port);

		//Free session data from this map server [Kevin]
		unit->free_pc(sd);

		return 0;
	}

	if( x < 0 || x >= map->list[m].xs || y < 0 || y >= map->list[m].ys ) {
		ShowError("pc_setpos: attempt to place player %s (%d:%d) on invalid coordinates (%s-%d,%d)\n", sd->status.name, sd->status.account_id, sd->status.char_id, mapindex_id2name(map_index),x,y);
		x = y = 0; // make it random
	}

	if( x == 0 && y == 0 ) {// pick a random walkable cell
		do {
			x=rnd()%(map->list[m].xs-2)+1;
			y=rnd()%(map->list[m].ys-2)+1;
		} while(map->getcell(m, &sd->bl, x, y, CELL_CHKNOPASS));
	}

	if (sd->state.vending && map->getcell(m, &sd->bl, x, y, CELL_CHKNOVENDING)) {
		clif->message (sd->fd, msg_sd(sd,204)); // "You can't open a shop on this cell."
		vending->close(sd);
	}

	if(sd->bl.prev != NULL){
		unit->remove_map_pc(sd,clrtype);
		clif->changemap(sd,m,x,y); // [MouseJstr]
	} else if(sd->state.active)
		//Tag player for rewarping after map-loading is done. [Skotlex]
		sd->state.rewarp = 1;

	sd->mapindex = map_index;
	sd->bl.m = m;
	sd->bl.x = sd->ud.to_x = x;
	sd->bl.y = sd->ud.to_y = y;

	if( sd->status.guild_id > 0 && map->list[m].flag.gvg_castle ) { // Increased guild castle regen [Valaris]
		struct guild_castle *gc = guild->mapindex2gc(sd->mapindex);
		if(gc && gc->guild_id == sd->status.guild_id)
			sd->regen.state.gc = 1;
	}

	if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 ) {
		sd->pd->bl.m = m;
		sd->pd->bl.x = sd->pd->ud.to_x = x;
		sd->pd->bl.y = sd->pd->ud.to_y = y;
		sd->pd->ud.dir = sd->ud.dir;
	}

	if( homun_alive(sd->hd) ) {
		sd->hd->bl.m = m;
		sd->hd->bl.x = sd->hd->ud.to_x = x;
		sd->hd->bl.y = sd->hd->ud.to_y = y;
		sd->hd->ud.dir = sd->ud.dir;
	}

	if( sd->md ) {
		sd->md->bl.m = m;
		sd->md->bl.x = sd->md->ud.to_x = x;
		sd->md->bl.y = sd->md->ud.to_y = y;
		sd->md->ud.dir = sd->ud.dir;
	}

	/* given autotrades have no clients you have to trigger this manually otherwise they get stuck in memory limbo bugreport:7495 */
	if( sd->state.autotrade )
		clif->pLoadEndAck(0,sd);

	return 0;
}

/*==========================================
 * Warp player sd to random location on current map.
 * May fail if no walkable cell found (1000 attempts).
 * Return:
 *   0 = fail or FIXME success (from pc->setpos)
 *   x(1|2) = fail
 *------------------------------------------*/
int pc_randomwarp(struct map_session_data *sd, clr_type type) {
	int x,y,i=0;
	int16 m;

	nullpo_ret(sd);

	m=sd->bl.m;

	if (map->list[sd->bl.m].flag.noteleport) //Teleport forbidden
		return 0;

	do {
		x=rnd()%(map->list[m].xs-2)+1;
		y=rnd()%(map->list[m].ys-2)+1;
	} while (map->getcell(m, &sd->bl, x, y, CELL_CHKNOPASS) && (i++) < 1000 );

	if (i < 1000)
		return pc->setpos(sd,map_id2index(sd->bl.m),x,y,type);

	return 0;
}

/*==========================================
 * Records a memo point at sd's current position
 * pos - entry to replace, (-1: shift oldest entry out)
 *------------------------------------------*/
int pc_memo(struct map_session_data* sd, int pos) {
	int skill_lv;

	nullpo_ret(sd);

	// check mapflags
	if( sd->bl.m >= 0 && (map->list[sd->bl.m].flag.nomemo || map->list[sd->bl.m].flag.nowarpto) && !pc_has_permission(sd, PC_PERM_WARP_ANYWHERE) ) {
		clif->skill_mapinfomessage(sd, 1); // "Saved point cannot be memorized."
		return 0;
	}

	// check inputs
	if( pos < -1 || pos >= MAX_MEMOPOINTS )
		return 0; // invalid input

	// check required skill level
	skill_lv = pc->checkskill(sd, AL_WARP);
	if( skill_lv < 1 ) {
		clif->skill_memomessage(sd,2); // "You haven't learned Warp."
		return 0;
	}
	if( skill_lv < 2 || skill_lv - 2 < pos ) {
		clif->skill_memomessage(sd,1); // "Skill Level is not high enough."
		return 0;
	}

	if( pos == -1 )
	{
		int i;
		// prevent memo-ing the same map multiple times
		ARR_FIND( 0, MAX_MEMOPOINTS, i, sd->status.memo_point[i].map == map_id2index(sd->bl.m) );
		memmove(&sd->status.memo_point[1], &sd->status.memo_point[0], (min(i,MAX_MEMOPOINTS-1))*sizeof(struct point));
		pos = 0;
	}

	sd->status.memo_point[pos].map = map_id2index(sd->bl.m);
	sd->status.memo_point[pos].x = sd->bl.x;
	sd->status.memo_point[pos].y = sd->bl.y;

	clif->skill_memomessage(sd, 0);

	return 1;
}

//
// Skills
//
/*==========================================
 * Return player sd skill_lv learned for given skill
 *------------------------------------------*/
int pc_checkskill(struct map_session_data *sd,uint16 skill_id) {
	uint16 index = 0;
	if(sd == NULL) return 0;
	if( skill_id >= GD_SKILLBASE && skill_id < GD_MAX ) {
		struct guild *g;

		if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
			return guild->checkskill(g,skill_id);
		return 0;
	} else if(!(index = skill->get_index(skill_id)) || index >= ARRAYLENGTH(sd->status.skill) ) {
		ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
		return 0;
	}

	if(sd->status.skill[index].id == skill_id)
		return (sd->status.skill[index].lv);

	return 0;
}
int pc_checkskill2(struct map_session_data *sd,uint16 index) {
	if(sd == NULL) return 0;
	if(index >= ARRAYLENGTH(sd->status.skill) ) {
		ShowError("pc_checkskill: Invalid skill index %d (char_id=%d).\n", index, sd->status.char_id);
		return 0;
	}
	if( skill->dbs->db[index].nameid >= GD_SKILLBASE && skill->dbs->db[index].nameid < GD_MAX ) {
		struct guild *g;

		if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
			return guild->checkskill(g,skill->dbs->db[index].nameid);
		return 0;
	}
	if(sd->status.skill[index].id == skill->dbs->db[index].nameid)
		return (sd->status.skill[index].lv);

	return 0;
}

/*==========================================
 * Chk if we still have the correct weapon to continue the skill (actually status)
 * If not ending it
 * Return
 *   0 - No status found or all done
 *------------------------------------------*/
int pc_checkallowskill(struct map_session_data *sd)
{
	const enum sc_type scw_list[] = {
		SC_TWOHANDQUICKEN,
		SC_ONEHANDQUICKEN,
		SC_AURABLADE,
		SC_PARRYING,
		SC_SPEARQUICKEN,
		SC_ADRENALINE,
		SC_ADRENALINE2,
		SC_DANCING,
		SC_GS_GATLINGFEVER,
#ifdef RENEWAL
		SC_LKCONCENTRATION,
		SC_EDP,
#endif
		SC_FEARBREEZE,
		SC_EXEEDBREAK,
	};
	const enum sc_type scs_list[] = {
		SC_AUTOGUARD,
		SC_DEFENDER,
		SC_REFLECTSHIELD,
		SC_LG_REFLECTDAMAGE
	};
	int i;
	nullpo_ret(sd);

	if(!sd->sc.count)
		return 0;

	for (i = 0; i < ARRAYLENGTH(scw_list); i++) {
		// Skills requiring specific weapon types
		if( scw_list[i] == SC_DANCING && !battle_config.dancing_weaponswitch_fix )
			continue;
		if( sd->sc.data[scw_list[i]]
		 && !pc_check_weapontype(sd,skill->get_weapontype(status->sc2skill(scw_list[i]))))
			status_change_end(&sd->bl, scw_list[i], INVALID_TIMER);
	}

	if(sd->sc.data[SC_STRUP] && sd->status.weapon)
		// Spurt requires bare hands (feet, in fact xD)
		status_change_end(&sd->bl, SC_STRUP, INVALID_TIMER);

	if(sd->status.shield <= 0) { // Skills requiring a shield
		for (i = 0; i < ARRAYLENGTH(scs_list); i++)
			if(sd->sc.data[scs_list[i]])
				status_change_end(&sd->bl, scs_list[i], INVALID_TIMER);
	}
	return 0;
}

/*==========================================
 * Return equiped itemid? on player sd at pos
 * Return
 * -1 : mean nothing equiped
 * idx : (this index could be used in inventory to found item_data)
 *------------------------------------------*/
int pc_checkequip(struct map_session_data *sd,int pos)
{
	int i;

	nullpo_retr(-1, sd);

	for(i=0;i<EQI_MAX;i++){
		if(pos & pc->equip_pos[i])
			return sd->equip_index[i];
	}

	return -1;
}

/*==========================================
 * Convert's from the client's lame Job ID system
 * to the map server's 'makes sense' system. [Skotlex]
 *------------------------------------------*/
int pc_jobid2mapid(unsigned short b_class)
{
	switch(b_class)
	{
	//Novice And 1-1 Jobs
		case JOB_NOVICE:                return MAPID_NOVICE;
		case JOB_SWORDMAN:              return MAPID_SWORDMAN;
		case JOB_MAGE:                  return MAPID_MAGE;
		case JOB_ARCHER:                return MAPID_ARCHER;
		case JOB_ACOLYTE:               return MAPID_ACOLYTE;
		case JOB_MERCHANT:              return MAPID_MERCHANT;
		case JOB_THIEF:                 return MAPID_THIEF;
		case JOB_TAEKWON:               return MAPID_TAEKWON;
		case JOB_WEDDING:               return MAPID_WEDDING;
		case JOB_GUNSLINGER:            return MAPID_GUNSLINGER;
		case JOB_NINJA:                 return MAPID_NINJA;
		case JOB_XMAS:                  return MAPID_XMAS;
		case JOB_SUMMER:                return MAPID_SUMMER;
		case JOB_GANGSI:                return MAPID_GANGSI;
	//2-1 Jobs
		case JOB_SUPER_NOVICE:          return MAPID_SUPER_NOVICE;
		case JOB_KNIGHT:                return MAPID_KNIGHT;
		case JOB_WIZARD:                return MAPID_WIZARD;
		case JOB_HUNTER:                return MAPID_HUNTER;
		case JOB_PRIEST:                return MAPID_PRIEST;
		case JOB_BLACKSMITH:            return MAPID_BLACKSMITH;
		case JOB_ASSASSIN:              return MAPID_ASSASSIN;
		case JOB_STAR_GLADIATOR:        return MAPID_STAR_GLADIATOR;
		case JOB_KAGEROU:
		case JOB_OBORO:                 return MAPID_KAGEROUOBORO;
		case JOB_REBELLION:             return MAPID_REBELLION;
		case JOB_DEATH_KNIGHT:          return MAPID_DEATH_KNIGHT;
	//2-2 Jobs
		case JOB_CRUSADER:              return MAPID_CRUSADER;
		case JOB_SAGE:                  return MAPID_SAGE;
		case JOB_BARD:
		case JOB_DANCER:                return MAPID_BARDDANCER;
		case JOB_MONK:                  return MAPID_MONK;
		case JOB_ALCHEMIST:             return MAPID_ALCHEMIST;
		case JOB_ROGUE:                 return MAPID_ROGUE;
		case JOB_SOUL_LINKER:           return MAPID_SOUL_LINKER;
		case JOB_DARK_COLLECTOR:        return MAPID_DARK_COLLECTOR;
	//Trans Novice And Trans 1-1 Jobs
		case JOB_NOVICE_HIGH:           return MAPID_NOVICE_HIGH;
		case JOB_SWORDMAN_HIGH:         return MAPID_SWORDMAN_HIGH;
		case JOB_MAGE_HIGH:             return MAPID_MAGE_HIGH;
		case JOB_ARCHER_HIGH:           return MAPID_ARCHER_HIGH;
		case JOB_ACOLYTE_HIGH:          return MAPID_ACOLYTE_HIGH;
		case JOB_MERCHANT_HIGH:         return MAPID_MERCHANT_HIGH;
		case JOB_THIEF_HIGH:            return MAPID_THIEF_HIGH;
	//Trans 2-1 Jobs
		case JOB_LORD_KNIGHT:           return MAPID_LORD_KNIGHT;
		case JOB_HIGH_WIZARD:           return MAPID_HIGH_WIZARD;
		case JOB_SNIPER:                return MAPID_SNIPER;
		case JOB_HIGH_PRIEST:           return MAPID_HIGH_PRIEST;
		case JOB_WHITESMITH:            return MAPID_WHITESMITH;
		case JOB_ASSASSIN_CROSS:        return MAPID_ASSASSIN_CROSS;
	//Trans 2-2 Jobs
		case JOB_PALADIN:               return MAPID_PALADIN;
		case JOB_PROFESSOR:             return MAPID_PROFESSOR;
		case JOB_CLOWN:
		case JOB_GYPSY:                 return MAPID_CLOWNGYPSY;
		case JOB_CHAMPION:              return MAPID_CHAMPION;
		case JOB_CREATOR:               return MAPID_CREATOR;
		case JOB_STALKER:               return MAPID_STALKER;
	//Baby Novice And Baby 1-1 Jobs
		case JOB_BABY:                  return MAPID_BABY;
		case JOB_BABY_SWORDMAN:         return MAPID_BABY_SWORDMAN;
		case JOB_BABY_MAGE:             return MAPID_BABY_MAGE;
		case JOB_BABY_ARCHER:           return MAPID_BABY_ARCHER;
		case JOB_BABY_ACOLYTE:          return MAPID_BABY_ACOLYTE;
		case JOB_BABY_MERCHANT:         return MAPID_BABY_MERCHANT;
		case JOB_BABY_THIEF:            return MAPID_BABY_THIEF;
	//Baby 2-1 Jobs
		case JOB_SUPER_BABY:            return MAPID_SUPER_BABY;
		case JOB_BABY_KNIGHT:           return MAPID_BABY_KNIGHT;
		case JOB_BABY_WIZARD:           return MAPID_BABY_WIZARD;
		case JOB_BABY_HUNTER:           return MAPID_BABY_HUNTER;
		case JOB_BABY_PRIEST:           return MAPID_BABY_PRIEST;
		case JOB_BABY_BLACKSMITH:       return MAPID_BABY_BLACKSMITH;
		case JOB_BABY_ASSASSIN:         return MAPID_BABY_ASSASSIN;
	//Baby 2-2 Jobs
		case JOB_BABY_CRUSADER:         return MAPID_BABY_CRUSADER;
		case JOB_BABY_SAGE:             return MAPID_BABY_SAGE;
		case JOB_BABY_BARD:
		case JOB_BABY_DANCER:           return MAPID_BABY_BARDDANCER;
		case JOB_BABY_MONK:             return MAPID_BABY_MONK;
		case JOB_BABY_ALCHEMIST:        return MAPID_BABY_ALCHEMIST;
		case JOB_BABY_ROGUE:            return MAPID_BABY_ROGUE;
	//3-1 Jobs
		case JOB_SUPER_NOVICE_E:        return MAPID_SUPER_NOVICE_E;
		case JOB_RUNE_KNIGHT:           return MAPID_RUNE_KNIGHT;
		case JOB_WARLOCK:               return MAPID_WARLOCK;
		case JOB_RANGER:                return MAPID_RANGER;
		case JOB_ARCH_BISHOP:           return MAPID_ARCH_BISHOP;
		case JOB_MECHANIC:              return MAPID_MECHANIC;
		case JOB_GUILLOTINE_CROSS:      return MAPID_GUILLOTINE_CROSS;
	//3-2 Jobs
		case JOB_ROYAL_GUARD:           return MAPID_ROYAL_GUARD;
		case JOB_SORCERER:              return MAPID_SORCERER;
		case JOB_MINSTREL:
		case JOB_WANDERER:              return MAPID_MINSTRELWANDERER;
		case JOB_SURA:                  return MAPID_SURA;
		case JOB_GENETIC:               return MAPID_GENETIC;
		case JOB_SHADOW_CHASER:         return MAPID_SHADOW_CHASER;
	//Trans 3-1 Jobs
		case JOB_RUNE_KNIGHT_T:         return MAPID_RUNE_KNIGHT_T;
		case JOB_WARLOCK_T:             return MAPID_WARLOCK_T;
		case JOB_RANGER_T:              return MAPID_RANGER_T;
		case JOB_ARCH_BISHOP_T:         return MAPID_ARCH_BISHOP_T;
		case JOB_MECHANIC_T:            return MAPID_MECHANIC_T;
		case JOB_GUILLOTINE_CROSS_T:    return MAPID_GUILLOTINE_CROSS_T;
	//Trans 3-2 Jobs
		case JOB_ROYAL_GUARD_T:         return MAPID_ROYAL_GUARD_T;
		case JOB_SORCERER_T:            return MAPID_SORCERER_T;
		case JOB_MINSTREL_T:
		case JOB_WANDERER_T:            return MAPID_MINSTRELWANDERER_T;
		case JOB_SURA_T:                return MAPID_SURA_T;
		case JOB_GENETIC_T:             return MAPID_GENETIC_T;
		case JOB_SHADOW_CHASER_T:       return MAPID_SHADOW_CHASER_T;
	//Baby 3-1 Jobs
		case JOB_SUPER_BABY_E:          return MAPID_SUPER_BABY_E;
		case JOB_BABY_RUNE:             return MAPID_BABY_RUNE;
		case JOB_BABY_WARLOCK:          return MAPID_BABY_WARLOCK;
		case JOB_BABY_RANGER:           return MAPID_BABY_RANGER;
		case JOB_BABY_BISHOP:           return MAPID_BABY_BISHOP;
		case JOB_BABY_MECHANIC:         return MAPID_BABY_MECHANIC;
		case JOB_BABY_CROSS:            return MAPID_BABY_CROSS;
	//Baby 3-2 Jobs
		case JOB_BABY_GUARD:            return MAPID_BABY_GUARD;
		case JOB_BABY_SORCERER:         return MAPID_BABY_SORCERER;
		case JOB_BABY_MINSTREL:
		case JOB_BABY_WANDERER:         return MAPID_BABY_MINSTRELWANDERER;
		case JOB_BABY_SURA:             return MAPID_BABY_SURA;
		case JOB_BABY_GENETIC:          return MAPID_BABY_GENETIC;
		case JOB_BABY_CHASER:           return MAPID_BABY_CHASER;
		default:
			return -1;
	}
}

//Reverts the map-style class id to the client-style one.
int pc_mapid2jobid(unsigned short class_, int sex)
{
	switch(class_)
	{
	//Novice And 1-1 Jobs
		case MAPID_NOVICE:                return JOB_NOVICE;
		case MAPID_SWORDMAN:              return JOB_SWORDMAN;
		case MAPID_MAGE:                  return JOB_MAGE;
		case MAPID_ARCHER:                return JOB_ARCHER;
		case MAPID_ACOLYTE:               return JOB_ACOLYTE;
		case MAPID_MERCHANT:              return JOB_MERCHANT;
		case MAPID_THIEF:                 return JOB_THIEF;
		case MAPID_TAEKWON:               return JOB_TAEKWON;
		case MAPID_WEDDING:               return JOB_WEDDING;
		case MAPID_GUNSLINGER:            return JOB_GUNSLINGER;
		case MAPID_NINJA:                 return JOB_NINJA;
		case MAPID_XMAS:                  return JOB_XMAS;
		case MAPID_SUMMER:                return JOB_SUMMER;
		case MAPID_GANGSI:                return JOB_GANGSI;
	//2-1 Jobs
		case MAPID_SUPER_NOVICE:          return JOB_SUPER_NOVICE;
		case MAPID_KNIGHT:                return JOB_KNIGHT;
		case MAPID_WIZARD:                return JOB_WIZARD;
		case MAPID_HUNTER:                return JOB_HUNTER;
		case MAPID_PRIEST:                return JOB_PRIEST;
		case MAPID_BLACKSMITH:            return JOB_BLACKSMITH;
		case MAPID_ASSASSIN:              return JOB_ASSASSIN;
		case MAPID_STAR_GLADIATOR:        return JOB_STAR_GLADIATOR;
		case MAPID_KAGEROUOBORO:          return sex?JOB_KAGEROU:JOB_OBORO;
		case MAPID_REBELLION:             return JOB_REBELLION;
		case MAPID_DEATH_KNIGHT:          return JOB_DEATH_KNIGHT;
	//2-2 Jobs
		case MAPID_CRUSADER:              return JOB_CRUSADER;
		case MAPID_SAGE:                  return JOB_SAGE;
		case MAPID_BARDDANCER:            return sex?JOB_BARD:JOB_DANCER;
		case MAPID_MONK:                  return JOB_MONK;
		case MAPID_ALCHEMIST:             return JOB_ALCHEMIST;
		case MAPID_ROGUE:                 return JOB_ROGUE;
		case MAPID_SOUL_LINKER:           return JOB_SOUL_LINKER;
		case MAPID_DARK_COLLECTOR:        return JOB_DARK_COLLECTOR;
	//Trans Novice And Trans 2-1 Jobs
		case MAPID_NOVICE_HIGH:           return JOB_NOVICE_HIGH;
		case MAPID_SWORDMAN_HIGH:         return JOB_SWORDMAN_HIGH;
		case MAPID_MAGE_HIGH:             return JOB_MAGE_HIGH;
		case MAPID_ARCHER_HIGH:           return JOB_ARCHER_HIGH;
		case MAPID_ACOLYTE_HIGH:          return JOB_ACOLYTE_HIGH;
		case MAPID_MERCHANT_HIGH:         return JOB_MERCHANT_HIGH;
		case MAPID_THIEF_HIGH:            return JOB_THIEF_HIGH;
	//Trans 2-1 Jobs
		case MAPID_LORD_KNIGHT:           return JOB_LORD_KNIGHT;
		case MAPID_HIGH_WIZARD:           return JOB_HIGH_WIZARD;
		case MAPID_SNIPER:                return JOB_SNIPER;
		case MAPID_HIGH_PRIEST:           return JOB_HIGH_PRIEST;
		case MAPID_WHITESMITH:            return JOB_WHITESMITH;
		case MAPID_ASSASSIN_CROSS:        return JOB_ASSASSIN_CROSS;
	//Trans 2-2 Jobs
		case MAPID_PALADIN:               return JOB_PALADIN;
		case MAPID_PROFESSOR:             return JOB_PROFESSOR;
		case MAPID_CLOWNGYPSY:            return sex?JOB_CLOWN:JOB_GYPSY;
		case MAPID_CHAMPION:              return JOB_CHAMPION;
		case MAPID_CREATOR:               return JOB_CREATOR;
		case MAPID_STALKER:               return JOB_STALKER;
	//Baby Novice And Baby 1-1 Jobs
		case MAPID_BABY:                  return JOB_BABY;
		case MAPID_BABY_SWORDMAN:         return JOB_BABY_SWORDMAN;
		case MAPID_BABY_MAGE:             return JOB_BABY_MAGE;
		case MAPID_BABY_ARCHER:           return JOB_BABY_ARCHER;
		case MAPID_BABY_ACOLYTE:          return JOB_BABY_ACOLYTE;
		case MAPID_BABY_MERCHANT:         return JOB_BABY_MERCHANT;
		case MAPID_BABY_THIEF:            return JOB_BABY_THIEF;
	//Baby 2-1 Jobs
		case MAPID_SUPER_BABY:            return JOB_SUPER_BABY;
		case MAPID_BABY_KNIGHT:           return JOB_BABY_KNIGHT;
		case MAPID_BABY_WIZARD:           return JOB_BABY_WIZARD;
		case MAPID_BABY_HUNTER:           return JOB_BABY_HUNTER;
		case MAPID_BABY_PRIEST:           return JOB_BABY_PRIEST;
		case MAPID_BABY_BLACKSMITH:       return JOB_BABY_BLACKSMITH;
		case MAPID_BABY_ASSASSIN:         return JOB_BABY_ASSASSIN;
	//Baby 2-2 Jobs
		case MAPID_BABY_CRUSADER:         return JOB_BABY_CRUSADER;
		case MAPID_BABY_SAGE:             return JOB_BABY_SAGE;
		case MAPID_BABY_BARDDANCER:       return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
		case MAPID_BABY_MONK:             return JOB_BABY_MONK;
		case MAPID_BABY_ALCHEMIST:        return JOB_BABY_ALCHEMIST;
		case MAPID_BABY_ROGUE:            return JOB_BABY_ROGUE;
	//3-1 Jobs
		case MAPID_SUPER_NOVICE_E:        return JOB_SUPER_NOVICE_E;
		case MAPID_RUNE_KNIGHT:           return JOB_RUNE_KNIGHT;
		case MAPID_WARLOCK:               return JOB_WARLOCK;
		case MAPID_RANGER:                return JOB_RANGER;
		case MAPID_ARCH_BISHOP:           return JOB_ARCH_BISHOP;
		case MAPID_MECHANIC:              return JOB_MECHANIC;
		case MAPID_GUILLOTINE_CROSS:      return JOB_GUILLOTINE_CROSS;
	//3-2 Jobs
		case MAPID_ROYAL_GUARD:           return JOB_ROYAL_GUARD;
		case MAPID_SORCERER:              return JOB_SORCERER;
		case MAPID_MINSTRELWANDERER:      return sex?JOB_MINSTREL:JOB_WANDERER;
		case MAPID_SURA:                  return JOB_SURA;
		case MAPID_GENETIC:               return JOB_GENETIC;
		case MAPID_SHADOW_CHASER:         return JOB_SHADOW_CHASER;
	//Trans 3-1 Jobs
		case MAPID_RUNE_KNIGHT_T:         return JOB_RUNE_KNIGHT_T;
		case MAPID_WARLOCK_T:             return JOB_WARLOCK_T;
		case MAPID_RANGER_T:              return JOB_RANGER_T;
		case MAPID_ARCH_BISHOP_T:         return JOB_ARCH_BISHOP_T;
		case MAPID_MECHANIC_T:            return JOB_MECHANIC_T;
		case MAPID_GUILLOTINE_CROSS_T:    return JOB_GUILLOTINE_CROSS_T;
	//Trans 3-2 Jobs
		case MAPID_ROYAL_GUARD_T:         return JOB_ROYAL_GUARD_T;
		case MAPID_SORCERER_T:            return JOB_SORCERER_T;
		case MAPID_MINSTRELWANDERER_T:    return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
		case MAPID_SURA_T:                return JOB_SURA_T;
		case MAPID_GENETIC_T:             return JOB_GENETIC_T;
		case MAPID_SHADOW_CHASER_T:       return JOB_SHADOW_CHASER_T;
	//Baby 3-1 Jobs
		case MAPID_SUPER_BABY_E:          return JOB_SUPER_BABY_E;
		case MAPID_BABY_RUNE:             return JOB_BABY_RUNE;
		case MAPID_BABY_WARLOCK:          return JOB_BABY_WARLOCK;
		case MAPID_BABY_RANGER:           return JOB_BABY_RANGER;
		case MAPID_BABY_BISHOP:           return JOB_BABY_BISHOP;
		case MAPID_BABY_MECHANIC:         return JOB_BABY_MECHANIC;
		case MAPID_BABY_CROSS:            return JOB_BABY_CROSS;
	//Baby 3-2 Jobs
		case MAPID_BABY_GUARD:            return JOB_BABY_GUARD;
		case MAPID_BABY_SORCERER:         return JOB_BABY_SORCERER;
		case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
		case MAPID_BABY_SURA:             return JOB_BABY_SURA;
		case MAPID_BABY_GENETIC:          return JOB_BABY_GENETIC;
		case MAPID_BABY_CHASER:           return JOB_BABY_CHASER;
		default:
			return -1;
	}
}

/*====================================================
 * This function return the name of the job (by [Yor])
 *----------------------------------------------------*/
const char* job_name(int class_)
{
	switch (class_) {
	case JOB_NOVICE:   // 550
	case JOB_SWORDMAN: // 551
	case JOB_MAGE:     // 552
	case JOB_ARCHER:   // 553
	case JOB_ACOLYTE:  // 554
	case JOB_MERCHANT: // 555
	case JOB_THIEF:    // 556
		return msg_txt(550 - JOB_NOVICE+class_);

	case JOB_KNIGHT:     // 557
	case JOB_PRIEST:     // 558
	case JOB_WIZARD:     // 559
	case JOB_BLACKSMITH: // 560
	case JOB_HUNTER:     // 561
	case JOB_ASSASSIN:   // 562
		return msg_txt(557 - JOB_KNIGHT+class_);

	case JOB_KNIGHT2:
		return msg_txt(557);

	case JOB_CRUSADER:  // 563
	case JOB_MONK:      // 564
	case JOB_SAGE:      // 565
	case JOB_ROGUE:     // 566
	case JOB_ALCHEMIST: // 567
	case JOB_BARD:      // 568
	case JOB_DANCER:    // 569
		return msg_txt(563 - JOB_CRUSADER+class_);

	case JOB_CRUSADER2:
		return msg_txt(563);

	case JOB_WEDDING:      // 570
	case JOB_SUPER_NOVICE: // 571
	case JOB_GUNSLINGER:   // 572
	case JOB_NINJA:        // 573
	case JOB_XMAS:         // 574
		return msg_txt(570 - JOB_WEDDING+class_);

	case JOB_SUMMER:
		return msg_txt(621);

	case JOB_NOVICE_HIGH:   // 575
	case JOB_SWORDMAN_HIGH: // 576
	case JOB_MAGE_HIGH:     // 577
	case JOB_ARCHER_HIGH:   // 578
	case JOB_ACOLYTE_HIGH:  // 579
	case JOB_MERCHANT_HIGH: // 580
	case JOB_THIEF_HIGH:    // 581
		return msg_txt(575 - JOB_NOVICE_HIGH+class_);

	case JOB_LORD_KNIGHT:    // 582
	case JOB_HIGH_PRIEST:    // 583
	case JOB_HIGH_WIZARD:    // 584
	case JOB_WHITESMITH:     // 585
	case JOB_SNIPER:         // 586
	case JOB_ASSASSIN_CROSS: // 587
		return msg_txt(582 - JOB_LORD_KNIGHT+class_);

	case JOB_LORD_KNIGHT2:
		return msg_txt(582);

	case JOB_PALADIN:   // 588
	case JOB_CHAMPION:  // 589
	case JOB_PROFESSOR: // 590
	case JOB_STALKER:   // 591
	case JOB_CREATOR:   // 592
	case JOB_CLOWN:     // 593
	case JOB_GYPSY:     // 594
		return msg_txt(588 - JOB_PALADIN + class_);

	case JOB_PALADIN2:
		return msg_txt(588);

	case JOB_BABY:          // 595
	case JOB_BABY_SWORDMAN: // 596
	case JOB_BABY_MAGE:     // 597
	case JOB_BABY_ARCHER:   // 598
	case JOB_BABY_ACOLYTE:  // 599
	case JOB_BABY_MERCHANT: // 600
	case JOB_BABY_THIEF:    // 601
		return msg_txt(595 - JOB_BABY + class_);

	case JOB_BABY_KNIGHT:     // 602
	case JOB_BABY_PRIEST:     // 603
	case JOB_BABY_WIZARD:     // 604
	case JOB_BABY_BLACKSMITH: // 605
	case JOB_BABY_HUNTER:     // 606
	case JOB_BABY_ASSASSIN:   // 607
		return msg_txt(602 - JOB_BABY_KNIGHT + class_);

	case JOB_BABY_KNIGHT2:
		return msg_txt(602);

	case JOB_BABY_CRUSADER:  // 608
	case JOB_BABY_MONK:      // 609
	case JOB_BABY_SAGE:      // 610
	case JOB_BABY_ROGUE:     // 611
	case JOB_BABY_ALCHEMIST: // 612
	case JOB_BABY_BARD:      // 613
	case JOB_BABY_DANCER:    // 614
		return msg_txt(608 - JOB_BABY_CRUSADER + class_);

	case JOB_BABY_CRUSADER2:
		return msg_txt(608);

	case JOB_SUPER_BABY:
		return msg_txt(615);

	case JOB_TAEKWON:
		return msg_txt(616);
	case JOB_STAR_GLADIATOR:
	case JOB_STAR_GLADIATOR2:
		return msg_txt(617);
	case JOB_SOUL_LINKER:
		return msg_txt(618);

	case JOB_GANGSI:         // 622
	case JOB_DEATH_KNIGHT:   // 623
	case JOB_DARK_COLLECTOR: // 624
		return msg_txt(622 - JOB_GANGSI+class_);

	case JOB_RUNE_KNIGHT:      // 625
	case JOB_WARLOCK:          // 626
	case JOB_RANGER:           // 627
	case JOB_ARCH_BISHOP:      // 628
	case JOB_MECHANIC:         // 629
	case JOB_GUILLOTINE_CROSS: // 630
		return msg_txt(625 - JOB_RUNE_KNIGHT+class_);

	case JOB_RUNE_KNIGHT_T:      // 656
	case JOB_WARLOCK_T:          // 657
	case JOB_RANGER_T:           // 658
	case JOB_ARCH_BISHOP_T:      // 659
	case JOB_MECHANIC_T:         // 660
	case JOB_GUILLOTINE_CROSS_T: // 661
		return msg_txt(656 - JOB_RUNE_KNIGHT_T+class_);

	case JOB_ROYAL_GUARD:   // 631
	case JOB_SORCERER:      // 632
	case JOB_MINSTREL:      // 633
	case JOB_WANDERER:      // 634
	case JOB_SURA:          // 635
	case JOB_GENETIC:       // 636
	case JOB_SHADOW_CHASER: // 637
		return msg_txt(631 - JOB_ROYAL_GUARD+class_);

	case JOB_ROYAL_GUARD_T:   // 662
	case JOB_SORCERER_T:      // 663
	case JOB_MINSTREL_T:      // 664
	case JOB_WANDERER_T:      // 665
	case JOB_SURA_T:          // 666
	case JOB_GENETIC_T:       // 667
	case JOB_SHADOW_CHASER_T: // 668
		return msg_txt(662 - JOB_ROYAL_GUARD_T+class_);

	case JOB_RUNE_KNIGHT2:
		return msg_txt(625);

	case JOB_RUNE_KNIGHT_T2:
		return msg_txt(656);

	case JOB_ROYAL_GUARD2:
		return msg_txt(631);

	case JOB_ROYAL_GUARD_T2:
		return msg_txt(662);

	case JOB_RANGER2:
		return msg_txt(627);

	case JOB_RANGER_T2:
		return msg_txt(658);

	case JOB_MECHANIC2:
		return msg_txt(629);

	case JOB_MECHANIC_T2:
		return msg_txt(660);

	case JOB_BABY_RUNE:     // 638
	case JOB_BABY_WARLOCK:  // 639
	case JOB_BABY_RANGER:   // 640
	case JOB_BABY_BISHOP:   // 641
	case JOB_BABY_MECHANIC: // 642
	case JOB_BABY_CROSS:    // 643
	case JOB_BABY_GUARD:    // 644
	case JOB_BABY_SORCERER: // 645
	case JOB_BABY_MINSTREL: // 646
	case JOB_BABY_WANDERER: // 647
	case JOB_BABY_SURA:     // 648
	case JOB_BABY_GENETIC:  // 649
	case JOB_BABY_CHASER:   // 650
		return msg_txt(638 - JOB_BABY_RUNE+class_);

	case JOB_BABY_RUNE2:
		return msg_txt(638);

	case JOB_BABY_GUARD2:
		return msg_txt(644);

	case JOB_BABY_RANGER2:
		return msg_txt(640);

	case JOB_BABY_MECHANIC2:
		return msg_txt(642);

	case JOB_SUPER_NOVICE_E: // 651
	case JOB_SUPER_BABY_E:   // 652
		return msg_txt(651 - JOB_SUPER_NOVICE_E+class_);

	case JOB_KAGEROU: // 653
	case JOB_OBORO:   // 654
		return msg_txt(653 - JOB_KAGEROU+class_);

	case JOB_REBELLION:
		return msg_txt(655);

	default:
		return msg_txt(620); // "Unknown Job"
	}
}

int pc_check_job_name(const char *name) {
	int i, len;
	struct {
		const char *name;
		int id;
	} names[] = {
		{ "Novice", JOB_NOVICE },
		{ "Swordsman", JOB_SWORDMAN },
		{ "Magician", JOB_MAGE },
		{ "Archer", JOB_ARCHER },
		{ "Acolyte", JOB_ACOLYTE },
		{ "Merchant", JOB_MERCHANT },
		{ "Thief", JOB_THIEF },
		{ "Knight", JOB_KNIGHT },
		{ "Priest", JOB_PRIEST },
		{ "Wizard", JOB_WIZARD },
		{ "Blacksmith", JOB_BLACKSMITH },
		{ "Hunter", JOB_HUNTER },
		{ "Assassin", JOB_ASSASSIN },
		{ "Crusader", JOB_CRUSADER },
		{ "Monk", JOB_MONK },
		{ "Sage", JOB_SAGE },
		{ "Rogue", JOB_ROGUE },
		{ "Alchemist", JOB_ALCHEMIST },
		{ "Bard", JOB_BARD },
		{ "Dancer", JOB_DANCER },
		{ "Super_Novice", JOB_SUPER_NOVICE },
		{ "Gunslinger", JOB_GUNSLINGER },
		{ "Ninja", JOB_NINJA },
		{ "Novice_High", JOB_NOVICE_HIGH },
		{ "Swordsman_High", JOB_SWORDMAN_HIGH },
		{ "Magician_High", JOB_MAGE_HIGH },
		{ "Archer_High", JOB_ARCHER_HIGH },
		{ "Acolyte_High", JOB_ACOLYTE_HIGH },
		{ "Merchant_High", JOB_MERCHANT_HIGH },
		{ "Thief_High", JOB_THIEF_HIGH },
		{ "Lord_Knight", JOB_LORD_KNIGHT },
		{ "High_Priest", JOB_HIGH_PRIEST },
		{ "High_Wizard", JOB_HIGH_WIZARD },
		{ "Whitesmith", JOB_WHITESMITH },
		{ "Sniper", JOB_SNIPER },
		{ "Assassin_Cross", JOB_ASSASSIN_CROSS },
		{ "Paladin", JOB_PALADIN },
		{ "Champion", JOB_CHAMPION },
		{ "Professor", JOB_PROFESSOR },
		{ "Stalker", JOB_STALKER },
		{ "Creator", JOB_CREATOR },
		{ "Clown", JOB_CLOWN },
		{ "Gypsy", JOB_GYPSY },
		{ "Baby_Novice", JOB_BABY },
		{ "Baby_Swordsman", JOB_BABY_SWORDMAN },
		{ "Baby_Magician", JOB_BABY_MAGE },
		{ "Baby_Archer", JOB_BABY_ARCHER },
		{ "Baby_Acolyte", JOB_BABY_ACOLYTE },
		{ "Baby_Merchant", JOB_BABY_MERCHANT },
		{ "Baby_Thief", JOB_BABY_THIEF },
		{ "Baby_Knight", JOB_BABY_KNIGHT },
		{ "Baby_Priest", JOB_BABY_PRIEST },
		{ "Baby_Wizard", JOB_BABY_WIZARD },
		{ "Baby_Blacksmith", JOB_BABY_BLACKSMITH },
		{ "Baby_Hunter", JOB_BABY_HUNTER },
		{ "Baby_Assassin", JOB_BABY_ASSASSIN },
		{ "Baby_Crusader", JOB_BABY_CRUSADER },
		{ "Baby_Monk", JOB_BABY_MONK },
		{ "Baby_Sage", JOB_BABY_SAGE },
		{ "Baby_Rogue", JOB_BABY_ROGUE },
		{ "Baby_Alchemist", JOB_BABY_ALCHEMIST },
		{ "Baby_Bard", JOB_BABY_BARD },
		{ "Baby_Dancer", JOB_BABY_DANCER },
		{ "Super_Baby", JOB_SUPER_BABY },
		{ "Taekwon", JOB_TAEKWON },
		{ "Star_Gladiator", JOB_STAR_GLADIATOR },
		{ "Soul_Linker", JOB_SOUL_LINKER },
		{ "Gangsi", JOB_GANGSI },
		{ "Death_Knight", JOB_DEATH_KNIGHT },
		{ "Dark_Collector", JOB_DARK_COLLECTOR },
		{ "Rune_Knight", JOB_RUNE_KNIGHT },
		{ "Warlock", JOB_WARLOCK },
		{ "Ranger", JOB_RANGER },
		{ "Arch_Bishop", JOB_ARCH_BISHOP },
		{ "Mechanic", JOB_MECHANIC },
		{ "Guillotine_Cross", JOB_GUILLOTINE_CROSS },
		{ "Rune_Knight_Trans", JOB_RUNE_KNIGHT_T },
		{ "Warlock_Trans", JOB_WARLOCK_T },
		{ "Ranger_Trans", JOB_RANGER_T },
		{ "Arch_Bishop_Trans", JOB_ARCH_BISHOP_T },
		{ "Mechanic_Trans", JOB_MECHANIC_T },
		{ "Guillotine_Cross_Trans", JOB_GUILLOTINE_CROSS_T },
		{ "Royal_Guard", JOB_ROYAL_GUARD },
		{ "Sorcerer", JOB_SORCERER },
		{ "Minstrel", JOB_MINSTREL },
		{ "Wanderer", JOB_WANDERER },
		{ "Sura", JOB_SURA },
		{ "Genetic", JOB_GENETIC },
		{ "Shadow_Chaser", JOB_SHADOW_CHASER },
		{ "Royal_Guard_Trans", JOB_ROYAL_GUARD_T },
		{ "Sorcerer_Trans", JOB_SORCERER_T },
		{ "Minstrel_Trans", JOB_MINSTREL_T },
		{ "Wanderer_Trans", JOB_WANDERER_T },
		{ "Sura_Trans", JOB_SURA_T },
		{ "Genetic_Trans", JOB_GENETIC_T },
		{ "Shadow_Chaser_Trans", JOB_SHADOW_CHASER_T },
		{ "Baby_Rune_Knight", JOB_BABY_RUNE },
		{ "Baby_Warlock", JOB_BABY_WARLOCK },
		{ "Baby_Ranger", JOB_BABY_RANGER },
		{ "Baby_Arch_Bishop", JOB_BABY_BISHOP },
		{ "Baby_Mechanic", JOB_BABY_MECHANIC },
		{ "Baby_Guillotine_Cross", JOB_BABY_CROSS },
		{ "Baby_Royal_Guard", JOB_BABY_GUARD },
		{ "Baby_Sorcerer", JOB_BABY_SORCERER },
		{ "Baby_Minstrel", JOB_BABY_MINSTREL },
		{ "Baby_Wanderer", JOB_BABY_WANDERER },
		{ "Baby_Sura", JOB_BABY_SURA },
		{ "Baby_Genetic", JOB_BABY_GENETIC },
		{ "Baby_Shadow_Chaser", JOB_BABY_CHASER },
		{ "Expanded_Super_Novice", JOB_SUPER_NOVICE_E },
		{ "Expanded_Super_Baby", JOB_SUPER_BABY_E },
		{ "Kagerou", JOB_KAGEROU },
		{ "Oboro", JOB_OBORO },
		{ "Rebellion", JOB_REBELLION },
	};

	nullpo_retr(-1, name);
	len = ARRAYLENGTH(names);

	ARR_FIND(0, len, i, strcmpi(names[i].name, name) == 0);

	if ( i == len )
		return -1;

	return names[i].id;
}

int pc_follow_timer(int tid, int64 tick, int id, intptr_t data) {
	struct map_session_data *sd;
	struct block_list *tbl;

	sd = map->id2sd(id);
	nullpo_ret(sd);

	if (sd->followtimer != tid) {
		ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
		sd->followtimer = INVALID_TIMER;
		return 0;
	}

	sd->followtimer = INVALID_TIMER;
	tbl = map->id2bl(sd->followtarget);

	if (tbl == NULL || pc_isdead(sd) || status->isdead(tbl)) {
		pc->stop_following(sd);
		return 0;
	}

	// either player or target is currently detached from map blocks (could be teleporting),
	// but still connected to this map, so we'll just increment the timer and check back later
	if (sd->bl.prev != NULL && tbl->prev != NULL
	 && sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER
	) {
		if((sd->bl.m == tbl->m) && unit->can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
			if (!check_distance_bl(&sd->bl, tbl, 5))
				unit->walktobl(&sd->bl, tbl, 5, 0);
		} else
			pc->setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
	}
	sd->followtimer = timer->add(
		tick + 1000, // increase time a bit to loosen up map's load
		pc->follow_timer, sd->bl.id, 0);
	return 0;
}

int pc_stop_following (struct map_session_data *sd)
{
	nullpo_ret(sd);

	if (sd->followtimer != INVALID_TIMER) {
		timer->delete(sd->followtimer,pc->follow_timer);
		sd->followtimer = INVALID_TIMER;
	}
	sd->followtarget = -1;
	sd->ud.target_to = 0;

	unit->stop_walking(&sd->bl, STOPWALKING_FLAG_FIXPOS);

	return 0;
}

int pc_follow(struct map_session_data *sd,int target_id) {
	struct block_list *bl = map->id2bl(target_id);
	nullpo_retr(1, sd);
	if (bl == NULL /*|| bl->type != BL_PC*/)
		return 1;
	if (sd->followtimer != INVALID_TIMER)
		pc->stop_following(sd);

	sd->followtarget = target_id;
	pc->follow_timer(INVALID_TIMER, timer->gettick(), sd->bl.id, 0);

	return 0;
}

int pc_checkbaselevelup(struct map_session_data *sd) {
	unsigned int next = pc->nextbaseexp(sd);

	nullpo_ret(sd);
	if (!next || sd->status.base_exp < next)
		return 0;

	do {
		sd->status.base_exp -= next;
		//Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
		if(!battle_config.multi_level_up && sd->status.base_exp > next-1)
			sd->status.base_exp = next-1;

		next = pc->gets_status_point(sd->status.base_level);
		sd->status.base_level ++;
		sd->status.status_point += next;

	} while ((next=pc->nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);

	if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
		status_calc_pet(sd->pd,SCO_NONE);

	clif->updatestatus(sd,SP_STATUSPOINT);
	clif->updatestatus(sd,SP_BASELEVEL);
	clif->updatestatus(sd,SP_BASEEXP);
	clif->updatestatus(sd,SP_NEXTBASEEXP);
	status_calc_pc(sd,SCO_FORCE);
	status_percent_heal(&sd->bl,100,100);

	if((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
		sc_start(NULL,&sd->bl,status->skill2sc(PR_KYRIE),100,1,skill->get_time(PR_KYRIE,1));
		sc_start(NULL,&sd->bl,status->skill2sc(PR_IMPOSITIO),100,1,skill->get_time(PR_IMPOSITIO,1));
		sc_start(NULL,&sd->bl,status->skill2sc(PR_MAGNIFICAT),100,1,skill->get_time(PR_MAGNIFICAT,1));
		sc_start(NULL,&sd->bl,status->skill2sc(PR_GLORIA),100,1,skill->get_time(PR_GLORIA,1));
		sc_start(NULL,&sd->bl,status->skill2sc(PR_SUFFRAGIUM),100,1,skill->get_time(PR_SUFFRAGIUM,1));
		if (sd->state.snovice_dead_flag)
			sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
	} else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
		sc_start(NULL,&sd->bl,status->skill2sc(AL_INCAGI),100,10,600000);
		sc_start(NULL,&sd->bl,status->skill2sc(AL_BLESSING),100,10,600000);
	}
	clif->misceffect(&sd->bl,0);
	npc->script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT

	if(sd->status.party_id)
		party->send_levelup(sd);

	pc->baselevelchanged(sd);
	return 1;
}

void pc_baselevelchanged(struct map_session_data *sd) {
	int i;
	nullpo_retv(sd);
	for( i = 0; i < EQI_MAX; i++ ) {
		if( sd->equip_index[i] >= 0 ) {
			if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
				pc->unequipitem(sd, sd->equip_index[i], PCUNEQUIPITEM_RECALC|PCUNEQUIPITEM_FORCE);
		}
	}
}

int pc_checkjoblevelup(struct map_session_data *sd)
{
	unsigned int next = pc->nextjobexp(sd);

	nullpo_ret(sd);
	if(!next || sd->status.job_exp < next)
		return 0;

	do {
		sd->status.job_exp -= next;
		//Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
		if(!battle_config.multi_level_up && sd->status.job_exp > next-1)
			sd->status.job_exp = next-1;

		sd->status.job_level ++;
		sd->status.skill_point ++;

	} while ((next=pc->nextjobexp(sd)) > 0 && sd->status.job_exp >= next);

	clif->updatestatus(sd,SP_JOBLEVEL);
	clif->updatestatus(sd,SP_JOBEXP);
	clif->updatestatus(sd,SP_NEXTJOBEXP);
	clif->updatestatus(sd,SP_SKILLPOINT);
	status_calc_pc(sd,SCO_FORCE);
	clif->misceffect(&sd->bl,1);
	if (pc->checkskill(sd, SG_DEVIL) && !pc->nextjobexp(sd))
		clif->status_change(&sd->bl,SI_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.

	npc->script_event(sd, NPCE_JOBLVUP);
	return 1;
}

/**
 * Alters EXP based on self bonuses that do not get shared with the party
 **/
void pc_calcexp(struct map_session_data *sd, unsigned int *base_exp, unsigned int *job_exp, struct block_list *src) {
	int buff_ratio = 0, buff_job_ratio = 0, race_ratio = 0, pk_ratio = 0;
	int64 jexp, bexp;

	nullpo_retv(sd);
	nullpo_retv(base_exp);
	nullpo_retv(job_exp);

	jexp = *job_exp;
	bexp = *base_exp;

	if (src != NULL) {
		const struct status_data *st = status->get_status_data(src);

#ifdef RENEWAL_EXP //should happen first before we caluclate any modifiers
		if (src->type == BL_MOB) {
			const struct mob_data *md = BL_UCAST(BL_MOB, src);
			int re_mod;
			re_mod = pc->level_penalty_mod(md->level - sd->status.base_level, md->status.race, md->status.mode, 1);
			jexp = apply_percentrate64(jexp, re_mod, 100);
			bexp = apply_percentrate64(bexp, re_mod, 100);
		}
#endif

		//Race modifier
		if (sd->expaddrace[st->race])
			race_ratio += sd->expaddrace[st->race];
		race_ratio += sd->expaddrace[(st->mode&MD_BOSS) ? RC_BOSS : RC_NONBOSS];
	}


	//PK modifier
	/* this doesn't exist in Aegis, instead there's a CrazyKiller check which double all EXP from this point */
	if (battle_config.pk_mode && (int)(status->get_lv(src) - sd->status.base_level) >= 20)
		pk_ratio += 15; // pk_mode additional exp if monster >20 levels [Valaris]


	//Buffs modifier
	if (sd->sc.data[SC_CASH_PLUSEXP]) {
		buff_job_ratio += sd->sc.data[SC_CASH_PLUSEXP]->val1;
		buff_ratio += sd->sc.data[SC_CASH_PLUSEXP]->val1;
	}
	if (sd->sc.data[SC_OVERLAPEXPUP]) {
		buff_job_ratio  += sd->sc.data[SC_OVERLAPEXPUP]->val1;
		buff_ratio += sd->sc.data[SC_OVERLAPEXPUP]->val1;
	}
	if (sd->sc.data[SC_CASH_PLUSONLYJOBEXP])
		buff_job_ratio += sd->sc.data[SC_CASH_PLUSONLYJOBEXP]->val1;

	//Applying Race and PK modifier First then Premium (Perment modifier) and finally buff modifier
	jexp += apply_percentrate64(jexp, race_ratio, 100);
	jexp += apply_percentrate64(jexp, pk_ratio, 100);

	bexp += apply_percentrate64(bexp, race_ratio, 100);
	bexp += apply_percentrate64(bexp, pk_ratio, 100);


	if (sd->status.mod_exp != 100) {
		jexp = apply_percentrate64(jexp, sd->status.mod_exp, 100);
		bexp = apply_percentrate64(bexp, sd->status.mod_exp, 100);
	}

	bexp += apply_percentrate64(bexp, buff_ratio, 100);
	jexp += apply_percentrate64(jexp, buff_ratio + buff_job_ratio, 100);

	*job_exp = (unsigned int)cap_value(jexp, 1, UINT_MAX);
	*base_exp = (unsigned int)cap_value(bexp, 1, UINT_MAX);
}

/**
 * Gives a determined EXP amount to sd and calculates remaining EXP for next level
 * @param src if is NULL no bonuses are taken into account
 * @param is_quest Used to let client know that the EXP was from a quest (clif->displayexp) PACKETVER >= 20091027
 * @retval true success
 **/
bool pc_gainexp(struct map_session_data *sd, struct block_list *src, unsigned int base_exp,unsigned int job_exp,bool is_quest) {
	float nextbp=0, nextjp=0;
	unsigned int nextb=0, nextj=0;
	nullpo_ret(sd);

	if (sd->bl.prev == NULL || pc_isdead(sd))
		return false;

	if (!battle_config.pvp_exp && map->list[sd->bl.m].flag.pvp)  // [MouseJstr]
		return false; // no exp on pvp maps

	if (pc_has_permission(sd,PC_PERM_DISABLE_EXP))
		return false;

	if (src)
		pc->calcexp(sd, &base_exp, &job_exp, src);

	if (sd->status.guild_id > 0)
		base_exp -= guild->payexp(sd,base_exp);

	nextb = pc->nextbaseexp(sd);
	nextj = pc->nextjobexp(sd);

	if (sd->state.showexp || battle_config.max_exp_gain_rate) {
		if (nextb > 0)
			nextbp = (float) base_exp / (float) nextb;
		if (nextj > 0)
			nextjp = (float) job_exp / (float) nextj;

		if(battle_config.max_exp_gain_rate) {
			if (nextbp > battle_config.max_exp_gain_rate/1000.) {
				//Note that this value should never be greater than the original
				//base_exp, therefore no overflow checks are needed. [Skotlex]
				base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
				if (sd->state.showexp)
					nextbp = (float) base_exp / (float) nextb;
			}
			if (nextjp > battle_config.max_exp_gain_rate/1000.) {
				job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
				if (sd->state.showexp)
					nextjp = (float) job_exp / (float) nextj;
			}
		}
	}

	// Cap exp to the level up requirement of the previous level when you are at max level,
	// otherwise cap at UINT_MAX (this is required for some S. Novice bonuses). [Skotlex]
	if (base_exp) {
		nextb = nextb?UINT_MAX:pc->thisbaseexp(sd);
		if(sd->status.base_exp > nextb - base_exp)
			sd->status.base_exp = nextb;
		else
			sd->status.base_exp += base_exp;
		pc->checkbaselevelup(sd);
		clif->updatestatus(sd,SP_BASEEXP);
	}

	if (job_exp) {
		nextj = nextj?UINT_MAX:pc->thisjobexp(sd);
		if(sd->status.job_exp > nextj - job_exp)
			sd->status.job_exp = nextj;
		else
			sd->status.job_exp += job_exp;
		pc->checkjoblevelup(sd);
		clif->updatestatus(sd,SP_JOBEXP);
	}

#if PACKETVER >= 20091027
	if(base_exp)
		clif->displayexp(sd, base_exp, SP_BASEEXP, is_quest);
	if(job_exp)
		clif->displayexp(sd, job_exp,  SP_JOBEXP, is_quest);
#endif

	if(sd->state.showexp) {
		char output[256];
		sprintf(output,
			"Experience Gained Base:%u (%.2f%%) Job:%u (%.2f%%)",base_exp,nextbp*(float)100,job_exp,nextjp*(float)100);
		clif_disp_onlyself(sd, output);
	}

	return true;
}

/*==========================================
 * Returns max level for this character.
 *------------------------------------------*/
int pc_maxbaselv(const struct map_session_data *sd)
{
	return pc->max_level[pc->class2idx(sd->status.class_)][0];
}

int pc_maxjoblv(const struct map_session_data *sd)
{
	return pc->max_level[pc->class2idx(sd->status.class_)][1];
}

/*==========================================
 * base level exp lookup.
 *------------------------------------------*/

//Base exp needed for next level.
unsigned int pc_nextbaseexp(struct map_session_data *sd)
{
	nullpo_ret(sd);

	if ((int)sd->status.base_level >= pc->maxbaselv(sd) || sd->status.base_level <= 0) // FIXME
		return 0;

	return pc->exp_table[pc->class2idx(sd->status.class_)][0][sd->status.base_level-1];
}

//Base exp needed for this level.
unsigned int pc_thisbaseexp(struct map_session_data *sd)
{
	if ((int)sd->status.base_level > pc->maxbaselv(sd) || sd->status.base_level <= 1) // FIXME
		return 0;

	return pc->exp_table[pc->class2idx(sd->status.class_)][0][sd->status.base_level-2];
}

/*==========================================
 * job level exp lookup
 * Return:
 *   0 = not found
 *   x = exp for level
 *------------------------------------------*/

//Job exp needed for next level.
unsigned int pc_nextjobexp(struct map_session_data *sd)
{
	nullpo_ret(sd);

	if ((int)sd->status.job_level >= pc->maxjoblv(sd) || sd->status.job_level <= 0) // FIXME
		return 0;
	return pc->exp_table[pc->class2idx(sd->status.class_)][1][sd->status.job_level-1];
}

//Job exp needed for this level.
unsigned int pc_thisjobexp(struct map_session_data *sd)
{
	if ((int)sd->status.job_level > pc->maxjoblv(sd) || sd->status.job_level <= 1) // FIXME
		return 0;
	return pc->exp_table[pc->class2idx(sd->status.class_)][1][sd->status.job_level-2];
}

/// Returns the value of the specified stat.
int pc_getstat(struct map_session_data* sd, int type)
{
	nullpo_retr(-1, sd);

	switch( type ) {
		case SP_STR: return sd->status.str;
		case SP_AGI: return sd->status.agi;
		case SP_VIT: return sd->status.vit;
		case SP_INT: return sd->status.int_;
		case SP_DEX: return sd->status.dex;
		case SP_LUK: return sd->status.luk;
		default:
			return -1;
	}
}

/// Sets the specified stat to the specified value.
/// Returns the new value.
int pc_setstat(struct map_session_data* sd, int type, int val)
{
	nullpo_retr(-1, sd);

	switch( type ) {
		case SP_STR: sd->status.str = val; break;
		case SP_AGI: sd->status.agi = val; break;
		case SP_VIT: sd->status.vit = val; break;
		case SP_INT: sd->status.int_ = val; break;
		case SP_DEX: sd->status.dex = val; break;
		case SP_LUK: sd->status.luk = val; break;
		default:
			return -1;
	}

	return val;
}

// Calculates the number of status points PC gets when leveling up (from level to level+1)
int pc_gets_status_point(int level)
{
	if (battle_config.use_statpoint_table) //Use values from "db/statpoint.txt"
		return (pc->statp[level+1] - pc->statp[level]);
	else //Default increase
		return ((level+15) / 5);
}

/// Returns the number of stat points needed to change the specified stat by val.
/// If val is negative, returns the number of stat points that would be needed to
/// raise the specified stat from (current value - val) to current value.
int pc_need_status_point(struct map_session_data* sd, int type, int val)
{
	int low, high, sp = 0;

	if ( val == 0 )
		return 0;

	low = pc->getstat(sd,type);

	if ( low >= pc_maxparameter(sd) && val > 0 )
		return 0; // Official servers show '0' when max is reached

	high = low + val;

	if ( val < 0 )
		swap(low, high);

	for ( ; low < high; low++ )
#ifdef RENEWAL // renewal status point cost formula
		sp += (low < 100) ? (2 + (low - 1) / 10) : (16 + 4 * ((low - 100) / 5));
#else
		sp += ( 1 + (low + 9) / 10 );
#endif

	return sp;
}

/**
 * Returns the value the specified stat can be increased by with the current
 * amount of available status points for the current character's class.
 *
 * @param sd   The target character.
 * @param type Stat to verify.
 * @return Maximum value the stat could grow by.
 */
int pc_maxparameterincrease(struct map_session_data* sd, int type) {
	int base, final, status_points = sd->status.status_point;

	base = final = pc->getstat(sd, type);

	while (final <= pc_maxparameter(sd) && status_points >= 0) {
#ifdef RENEWAL // renewal status point cost formula
		status_points -= (final < 100) ? (2 + (final - 1) / 10) : (16 + 4 * ((final - 100) / 5));
#else
		status_points -= ( 1 + (final + 9) / 10 );
#endif
		final++;
	}
	final--;

	return final > base ? final-base : 0;
}

/**
 * Raises a stat by the specified amount.
 *
 * Obeys max_parameter limits.
 * Subtracts status points according to the cost of the increased stat points.
 *
 * @param sd       The target character.
 * @param type     The stat to change (see enum status_point_types)
 * @param increase The stat increase (strictly positive) amount.
 * @retval true  if the stat was increased by any amount.
 * @retval false if there were no changes.
 */
bool pc_statusup(struct map_session_data* sd, int type, int increase) {
	int max_increase = 0, current = 0, needed_points = 0, final_value = 0;

	nullpo_ret(sd);

	// check conditions
	if (type < SP_STR || type > SP_LUK || increase <= 0) {
		clif->statusupack(sd, type, 0, 0);
		return false;
	}

	// check limits
	current = pc->getstat(sd, type);
	max_increase = pc->maxparameterincrease(sd, type);
	increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
	if (increase <= 0 || current + increase > pc_maxparameter(sd)) {
		clif->statusupack(sd, type, 0, 0);
		return false;
	}

	// check status points
	needed_points = pc->need_status_point(sd, type, increase);
	if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
		clif->statusupack(sd, type, 0, 0);
		return false;
	}

	// set new values
	final_value = pc->setstat(sd, type, current + increase);
	sd->status.status_point -= needed_points;

	status_calc_pc(sd, SCO_NONE);

	// update increase cost indicator
	clif->updatestatus(sd, SP_USTR + type-SP_STR);

	// update statpoint count
	clif->updatestatus(sd, SP_STATUSPOINT);

	// update stat value
	clif->statusupack(sd, type, 1, final_value); // required
	if (final_value > 255)
		clif->updatestatus(sd, type); // send after the 'ack' to override the truncated value

	return true;
}

/**
 * Raises a stat by the specified amount.
 *
 * Obeys max_parameter limits.
 * Does not subtract status points for the cost of the modified stat points.
 *
 * @param sd   The target character.
 * @param type The stat to change (see enum status_point_types)
 * @param val  The stat increase (or decrease) amount.
 * @return the stat increase amount.
 * @retval 0 if no changes were made.
 */
int pc_statusup2(struct map_session_data* sd, int type, int val)
{
	int max, need;
	nullpo_ret(sd);

	if( type < SP_STR || type > SP_LUK )
	{
		clif->statusupack(sd,type,0,0);
		return 0;
	}

	need = pc->need_status_point(sd,type,1);

	// set new value
	max = pc_maxparameter(sd);
	val = pc->setstat(sd, type, cap_value(pc->getstat(sd,type) + val, 1, max));

	status_calc_pc(sd,SCO_NONE);

	// update increase cost indicator
	if( need != pc->need_status_point(sd,type,1) )
		clif->updatestatus(sd, SP_USTR + type-SP_STR);

	// update stat value
	clif->statusupack(sd,type,1,val); // required
	if( val > 255 )
		clif->updatestatus(sd,type); // send after the 'ack' to override the truncated value

	return val;
}

/*==========================================
 * Update skill_lv for player sd
 * Skill point allocation
 *------------------------------------------*/
int pc_skillup(struct map_session_data *sd,uint16 skill_id) {
	uint16 index = 0;
	nullpo_ret(sd);

	if( skill_id >= GD_SKILLBASE && skill_id < GD_SKILLBASE+MAX_GUILDSKILL ) {
		guild->skillup(sd, skill_id);
		return 0;
	}

	if( skill_id >= HM_SKILLBASE && skill_id < HM_SKILLBASE+MAX_HOMUNSKILL && sd->hd ) {
		homun->skillup(sd->hd, skill_id);
		return 0;
	}

	if( !(index = skill->get_index(skill_id)) )
		return 0;

	if( sd->status.skill_point > 0 &&
		sd->status.skill[index].id &&
		sd->status.skill[index].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
		sd->status.skill[index].lv < skill->tree_get_max(skill_id, sd->status.class_) )
	{
		sd->status.skill[index].lv++;
		sd->status.skill_point--;
		if( !skill->dbs->db[index].inf )
			status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
		else if( sd->status.skill_point == 0 && (sd->class_&MAPID_UPPERMASK) == MAPID_TAEKWON && sd->status.base_level >= 90 && pc->famerank(sd->status.char_id, MAPID_TAEKWON) )
			pc->calc_skilltree(sd); // Required to grant all TK Ranger skills.
		else
			pc->check_skilltree(sd, skill_id); // Check if a new skill can Lvlup

		clif->skillup(sd,skill_id, sd->status.skill[index].lv, 1);
		clif->updatestatus(sd,SP_SKILLPOINT);
		if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
			clif->updatestatus(sd,SP_CARTINFO);
		if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
			clif->skillinfoblock(sd);
	} else if( battle_config.skillup_limit ){
		if (sd->sktree.second)
			clif->msgtable_num(sd, MSG_SKILL_POINTS_LEFT_JOB1, sd->sktree.second);
		else if (sd->sktree.third)
			clif->msgtable_num(sd, MSG_SKILL_POINTS_LEFT_JOB2, sd->sktree.third);
		else if (pc->calc_skillpoint(sd) < 9) /* TODO: official response? */
			clif->messagecolor_self(sd->fd, COLOR_RED, "You need the basic skills");
	}
	return 0;
}

/*==========================================
 * /allskill
 *------------------------------------------*/
int pc_allskillup(struct map_session_data *sd)
{
	int i;

	nullpo_ret(sd);

	for(i=0;i<MAX_SKILL;i++){
		if (sd->status.skill[i].flag != SKILL_FLAG_PERMANENT && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED && sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED) {
			sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
			sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
			if (sd->status.skill[i].lv == 0)
				sd->status.skill[i].id = 0;
		}
	}

	if (pc_has_permission(sd, PC_PERM_ALL_SKILL)) { //Get ALL skills except npc/guild ones. [Skotlex]
		//and except SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
		for(i=0;i<MAX_SKILL;i++){
			switch( skill->dbs->db[i].nameid ) {
				case SG_DEVIL:
				case MO_TRIPLEATTACK:
				case RG_SNATCHER:
					continue;
				default:
					if( !(skill->dbs->db[i].inf2&(INF2_NPC_SKILL|INF2_GUILD_SKILL)) )
						if ( ( sd->status.skill[i].lv = skill->dbs->db[i].max ) )//Nonexistant skills should return a max of 0 anyway.
							sd->status.skill[i].id = skill->dbs->db[i].nameid;
			}
		}
	} else {
		int id;
		for (i = 0; i < MAX_SKILL_TREE && (id=pc->skill_tree[pc->class2idx(sd->status.class_)][i].id) > 0; i++) {
			int idx = pc->skill_tree[pc->class2idx(sd->status.class_)][i].idx;
			int inf2 = skill->dbs->db[idx].inf2;
			if (
				(inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn) ||
				(inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL)) ||
				id==SG_DEVIL
			)
				continue; //Cannot be learned normally.

			sd->status.skill[idx].id = id;
			sd->status.skill[idx].lv = skill->tree_get_max(id, sd->status.class_); // celest
		}
	}
	status_calc_pc(sd,SCO_NONE);
	//Required because if you could level up all skills previously,
	//the update will not be sent as only the lv variable changes.
	clif->skillinfoblock(sd);
	return 0;
}

/*==========================================
 * /resetlvl
 *------------------------------------------*/
int pc_resetlvl(struct map_session_data* sd,int type)
{
	int  i;

	nullpo_ret(sd);

	if (type != 3) //Also reset skills
		pc->resetskill(sd, PCRESETSKILL_NONE);

	if(type == 1) {
		sd->status.skill_point=0;
		sd->status.base_level=1;
		sd->status.job_level=1;
		sd->status.base_exp=0;
		sd->status.job_exp=0;
		if(sd->sc.option !=0)
			sd->sc.option = 0;

		sd->status.str=1;
		sd->status.agi=1;
		sd->status.vit=1;
		sd->status.int_=1;
		sd->status.dex=1;
		sd->status.luk=1;
		if(sd->status.class_ == JOB_NOVICE_HIGH) {
			sd->status.status_point=100; // not 88 [celest]
			// give platinum skills upon changing
			pc->skill(sd, NV_FIRSTAID, 1, SKILL_GRANT_PERMANENT);
			pc->skill(sd, NV_TRICKDEAD, 1, SKILL_GRANT_PERMANENT);
		}
	}

	if(type == 2){
		sd->status.skill_point=0;
		sd->status.base_level=1;
		sd->status.job_level=1;
		sd->status.base_exp=0;
		sd->status.job_exp=0;
	}
	if(type == 3){
		sd->status.base_level=1;
		sd->status.base_exp=0;
	}
	if(type == 4){
		sd->status.job_level=1;
		sd->status.job_exp=0;
	}

	clif->updatestatus(sd,SP_STATUSPOINT);
	clif->updatestatus(sd,SP_STR);
	clif->updatestatus(sd,SP_AGI);
	clif->updatestatus(sd,SP_VIT);
	clif->updatestatus(sd,SP_INT);
	clif->updatestatus(sd,SP_DEX);
	clif->updatestatus(sd,SP_LUK);
	clif->updatestatus(sd,SP_BASELEVEL);
	clif->updatestatus(sd,SP_JOBLEVEL);
	clif->updatestatus(sd,SP_STATUSPOINT);
	clif->updatestatus(sd,SP_BASEEXP);
	clif->updatestatus(sd,SP_JOBEXP);
	clif->updatestatus(sd,SP_NEXTBASEEXP);
	clif->updatestatus(sd,SP_NEXTJOBEXP);
	clif->updatestatus(sd,SP_SKILLPOINT);

	clif->updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
	clif->updatestatus(sd,SP_UAGI);
	clif->updatestatus(sd,SP_UVIT);
	clif->updatestatus(sd,SP_UINT);
	clif->updatestatus(sd,SP_UDEX);
	clif->updatestatus(sd,SP_ULUK); // End Addition

	for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
		if(sd->equip_index[i] >= 0)
			if(!pc->isequip(sd,sd->equip_index[i]))
				pc->unequipitem(sd, sd->equip_index[i], PCUNEQUIPITEM_FORCE);
	}

	if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
		party->send_levelup(sd);

	status_calc_pc(sd,SCO_FORCE);
	clif->skillinfoblock(sd);

	return 0;
}
/*==========================================
 * /resetstate
 *------------------------------------------*/
int pc_resetstate(struct map_session_data* sd)
{
	nullpo_ret(sd);

	if (battle_config.use_statpoint_table) {
		// New statpoint table used here - Dexity
		if (sd->status.base_level > MAX_LEVEL) {
			//pc->statp[] goes out of bounds, can't reset!
			ShowError("pc_resetstate: Can't reset stats of %d:%d, the base level (%u) is greater than the max level supported (%d)\n",
				sd->status.account_id, sd->status.char_id, sd->status.base_level, MAX_LEVEL);
			return 0;
		}

		sd->status.status_point = pc->statp[sd->status.base_level] + ((sd->class_&JOBL_UPPER) ? 52 : 0); // extra 52+48=100 stat points
	}
	else
	{
		int add=0;
		add += pc->need_status_point(sd, SP_STR, 1-pc->getstat(sd, SP_STR));
		add += pc->need_status_point(sd, SP_AGI, 1-pc->getstat(sd, SP_AGI));
		add += pc->need_status_point(sd, SP_VIT, 1-pc->getstat(sd, SP_VIT));
		add += pc->need_status_point(sd, SP_INT, 1-pc->getstat(sd, SP_INT));
		add += pc->need_status_point(sd, SP_DEX, 1-pc->getstat(sd, SP_DEX));
		add += pc->need_status_point(sd, SP_LUK, 1-pc->getstat(sd, SP_LUK));

		sd->status.status_point+=add;
	}

	pc->setstat(sd, SP_STR, 1);
	pc->setstat(sd, SP_AGI, 1);
	pc->setstat(sd, SP_VIT, 1);
	pc->setstat(sd, SP_INT, 1);
	pc->setstat(sd, SP_DEX, 1);
	pc->setstat(sd, SP_LUK, 1);

	clif->updatestatus(sd,SP_STR);
	clif->updatestatus(sd,SP_AGI);
	clif->updatestatus(sd,SP_VIT);
	clif->updatestatus(sd,SP_INT);
	clif->updatestatus(sd,SP_DEX);
	clif->updatestatus(sd,SP_LUK);

	clif->updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
	clif->updatestatus(sd,SP_UAGI);
	clif->updatestatus(sd,SP_UVIT);
	clif->updatestatus(sd,SP_UINT);
	clif->updatestatus(sd,SP_UDEX);
	clif->updatestatus(sd,SP_ULUK); // End Addition

	clif->updatestatus(sd,SP_STATUSPOINT);

	if( sd->mission_mobid ) { //bugreport:2200
		sd->mission_mobid = 0;
		sd->mission_count = 0;
		pc_setglobalreg(sd,script->add_str("TK_MISSION_ID"), 0);
	}

	status_calc_pc(sd,SCO_NONE);

	return 1;
}

/*==========================================
 * /resetskill
 * @param flag: @see enum pc_resetskill_flag
 *------------------------------------------*/
int pc_resetskill(struct map_session_data* sd, int flag)
{
	int i, inf2, skill_point=0;
	nullpo_ret(sd);

	if( flag&PCRESETSKILL_CHSEX && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
		return 0;

	if( !(flag&PCRESETSKILL_RECOUNT) ) { //Remove stuff lost when resetting skills.

		/**
		 * It has been confirmed on official server that when you reset skills with a ranked tweakwon your skills are not reset (because you have all of them anyway)
		 **/
		if( (sd->class_&MAPID_UPPERMASK) == MAPID_TAEKWON && sd->status.base_level >= 90 && pc->famerank(sd->status.char_id, MAPID_TAEKWON) )
			return 0;

		if( pc->checkskill(sd, SG_DEVIL) &&  !pc->nextjobexp(sd) ) //Remove perma blindness due to skill-reset. [Skotlex]
			clif->sc_end(&sd->bl, sd->bl.id, SELF, SI_DEVIL1);
		i = sd->sc.option;
		if( i&OPTION_RIDING && pc->checkskill(sd, KN_RIDING) )
			i &= ~OPTION_RIDING;
		if( i&OPTION_FALCON && pc->checkskill(sd, HT_FALCON) )
			i &= ~OPTION_FALCON;
		if( i&OPTION_DRAGON && pc->checkskill(sd, RK_DRAGONTRAINING) )
			i &= ~OPTION_DRAGON;
		if( i&OPTION_WUG && pc->checkskill(sd, RA_WUGMASTERY) )
			i &= ~OPTION_WUG;
		if( i&OPTION_WUGRIDER && pc->checkskill(sd, RA_WUGRIDER) )
			i &= ~OPTION_WUGRIDER;
		if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
			i &= ~OPTION_MADOGEAR;
#ifndef NEW_CARTS
		if( i&OPTION_CART && pc->checkskill(sd, MC_PUSHCART) )
			i &= ~OPTION_CART;
#else
		if( sd->sc.data[SC_PUSH_CART] )
			pc->setcart(sd, 0);
#endif
		if( i != sd->sc.option )
			pc->setoption(sd, i);

		if( homun_alive(sd->hd) && pc->checkskill(sd, AM_CALLHOMUN) )
			homun->vaporize(sd, HOM_ST_REST);
	}

	for( i = 1; i < MAX_SKILL; i++ ) {
		// FIXME: We're looping on i = [1..MAX_SKILL-1] (which makes sense as index for sd->status.skill[]) but then we're using the
		// same i to access skill->dbs->db[], and especially to check skill_ischangesex(). This is wrong.
		uint16 skill_id = 0;
		int lv = sd->status.skill[i].lv;
		if (lv < 1) continue;

		inf2 = skill->dbs->db[i].inf2;

		if( inf2&(INF2_WEDDING_SKILL|INF2_SPIRIT_SKILL) ) //Avoid reseting wedding/linker skills.
			continue;

		skill_id = skill->dbs->db[i].nameid;

		// Don't reset trick dead if not a novice/baby
		if( skill_id == NV_TRICKDEAD && (sd->class_&(MAPID_BASEMASK|JOBL_2)) != MAPID_NOVICE ) {
			sd->status.skill[i].lv = 0;
			sd->status.skill[i].flag = 0;
			continue;
		}

		// do not reset basic skill
		if( skill_id == NV_BASIC && (sd->class_&(MAPID_BASEMASK|JOBL_2)) != MAPID_NOVICE )
			continue;

		if( sd->status.skill[i].flag == SKILL_FLAG_PERM_GRANTED )
			continue;

		if( flag&PCRESETSKILL_CHSEX && !skill_ischangesex(i) )
			continue;

		if( inf2&INF2_QUEST_SKILL && !battle_config.quest_skill_learn ) {
			//Only handle quest skills in a special way when you can't learn them manually
			if( battle_config.quest_skill_reset && !(flag&PCRESETSKILL_RECOUNT) ) { //Wipe them
				sd->status.skill[i].lv = 0;
				sd->status.skill[i].flag = 0;
			}
			continue;
		}
		if( sd->status.skill[i].flag == SKILL_FLAG_PERMANENT )
			skill_point += lv;
		else if( sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0 )
			skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);

		if( !(flag&PCRESETSKILL_RECOUNT) ) {// reset
			sd->status.skill[i].lv = 0;
			sd->status.skill[i].flag = 0;
		}
	}

	if( flag&PCRESETSKILL_RECOUNT || !skill_point ) return skill_point;

	sd->status.skill_point += skill_point;

	if (!(flag&PCRESETSKILL_RECOUNT)) {
		// Remove all SCs that can't be inactivated without a skill
		if( sd->sc.data[SC_STORMKICK_READY] )
			status_change_end(&sd->bl, SC_STORMKICK_READY, INVALID_TIMER);
		if( sd->sc.data[SC_DOWNKICK_READY] )
			status_change_end(&sd->bl, SC_DOWNKICK_READY, INVALID_TIMER);
		if( sd->sc.data[SC_TURNKICK_READY] )
			status_change_end(&sd->bl, SC_TURNKICK_READY, INVALID_TIMER);
		if( sd->sc.data[SC_COUNTERKICK_READY] )
			status_change_end(&sd->bl, SC_COUNTERKICK_READY, INVALID_TIMER);
		if( sd->sc.data[SC_DODGE_READY] )
			status_change_end(&sd->bl, SC_DODGE_READY, INVALID_TIMER);
	}

	if (flag&PCRESETSKILL_RESYNC) {
		clif->updatestatus(sd,SP_SKILLPOINT);
		clif->skillinfoblock(sd);
		status_calc_pc(sd,SCO_FORCE);
	}

	return skill_point;
}

/*==========================================
 * /resetfeel [Komurka]
 *------------------------------------------*/
int pc_resetfeel(struct map_session_data* sd)
{
	int i;
	nullpo_ret(sd);

	for (i=0; i<MAX_PC_FEELHATE; i++)
	{
		sd->feel_map[i].m = -1;
		sd->feel_map[i].index = 0;
		pc_setglobalreg(sd,script->add_str(pc->sg_info[i].feel_var),0);
	}

	return 0;
}

int pc_resethate(struct map_session_data* sd)
{
	int i;
	nullpo_ret(sd);

	for (i = 0; i < MAX_PC_FEELHATE; i++) {
		sd->hate_mob[i] = -1;
		pc_setglobalreg(sd,script->add_str(pc->sg_info[i].hate_var),0);
	}
	return 0;
}

int pc_skillatk_bonus(struct map_session_data *sd, uint16 skill_id)
{
	int i, bonus = 0;
	nullpo_ret(sd);

	ARR_FIND(0, ARRAYLENGTH(sd->skillatk), i, sd->skillatk[i].id == skill_id);
	if( i < ARRAYLENGTH(sd->skillatk) ) bonus = sd->skillatk[i].val;

	if(sd->sc.data[SC_PYROTECHNIC_OPTION] || sd->sc.data[SC_AQUAPLAY_OPTION])
		bonus += 10;

	return bonus;
}

int pc_skillheal_bonus(struct map_session_data *sd, uint16 skill_id) {
	int i, bonus = sd->bonus.add_heal_rate;

	if( bonus ) {
		switch( skill_id ) {
			case AL_HEAL:           if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
			case PR_SANCTUARY:      if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
			case AM_POTIONPITCHER:  if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
			case CR_SLIMPITCHER:    if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
			case BA_APPLEIDUN:      if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
		}
	}

	ARR_FIND(0, ARRAYLENGTH(sd->skillheal), i, sd->skillheal[i].id == skill_id);

	if( i < ARRAYLENGTH(sd->skillheal) )
		bonus += sd->skillheal[i].val;

	return bonus;
}

int pc_skillheal2_bonus(struct map_session_data *sd, uint16 skill_id) {
	int i, bonus = sd->bonus.add_heal2_rate;

	ARR_FIND(0, ARRAYLENGTH(sd->skillheal2), i, sd->skillheal2[i].id == skill_id);

	if( i < ARRAYLENGTH(sd->skillheal2) )
		bonus += sd->skillheal2[i].val;

	return bonus;
}

void pc_respawn(struct map_session_data* sd, clr_type clrtype)
{
	if( !pc_isdead(sd) )
		return; // not applicable
	if( sd->bg_id && bg->member_respawn(sd) )
		return; // member revived by battleground

	pc->setstand(sd);
	pc->setrestartvalue(sd,3);
	if( pc->setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, clrtype) )
		clif->resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
}

int pc_respawn_timer(int tid, int64 tick, int id, intptr_t data) {
	struct map_session_data *sd = map->id2sd(id);
	if( sd != NULL )
	{
		sd->pvp_point=0;
		pc->respawn(sd,CLR_OUTSIGHT);
	}

	return 0;
}

/*==========================================
 * Invoked when a player has received damage
 *------------------------------------------*/
void pc_damage(struct map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp)
{
	if (sp) clif->updatestatus(sd,SP_SP);
	if (hp) clif->updatestatus(sd,SP_HP);
	else return;

	if( !src || src == &sd->bl )
		return;

	if( pc_issit(sd) ) {
		pc->setstand(sd);
		skill->sit(sd,0);
	}

	if( sd->progressbar.npc_id ){
		clif->progressbar_abort(sd);
		sd->state.workinprogress = 0;
	}

	if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
		pet->target_check(sd,src,1);

	if( sd->status.ele_id > 0 )
		elemental->set_target(sd,src);

	sd->canlog_tick = timer->gettick();
}

/*==========================================
 * Invoked when a player has negative current hp
 *------------------------------------------*/
int pc_dead(struct map_session_data *sd,struct block_list *src) {
	int i=0,j=0;
	int64 tick = timer->gettick();

	nullpo_retr(0, sd);

	for (j = 0; j < MAX_PC_DEVOTION; j++) {
		if (sd->devotion[j]) {
			struct map_session_data *devsd = map->id2sd(sd->devotion[j]);
			if (devsd)
				status_change_end(&devsd->bl, SC_DEVOTION, INVALID_TIMER);
			sd->devotion[j] = 0;
		}
	}

	if(sd->status.pet_id > 0 && sd->pd) {
		struct pet_data *pd = sd->pd;
		if( !map->list[sd->bl.m].flag.noexppenalty ) {
			pet->set_intimate(pd, pd->pet.intimate - pd->petDB->die);
			if( pd->pet.intimate < 0 )
				pd->pet.intimate = 0;
			clif->send_petdata(sd,sd->pd,1,pd->pet.intimate);
		}
		if( sd->pd->target_id ) // Unlock all targets...
			pet->unlocktarget(sd->pd);
	}

	if (sd->status.hom_id > 0){
	    if(battle_config.homunculus_auto_vapor && sd->hd)
		    homun->vaporize(sd, HOM_ST_REST);
	}

	if( sd->md )
		mercenary->delete(sd->md, 3); // Your mercenary soldier has ran away.

	if( sd->ed )
		elemental->delete(sd->ed, 0);

	// Leave duel if you die [LuzZza]
	if(battle_config.duel_autoleave_when_die) {
		if(sd->duel_group > 0)
			duel->leave(sd->duel_group, sd);
		if(sd->duel_invite > 0)
			duel->reject(sd->duel_invite, sd);
	}

	if (sd->npc_id && sd->st && sd->st->state != RUN)
		npc->event_dequeue(sd);

	pc_setglobalreg(sd,script->add_str("PC_DIE_COUNTER"),sd->die_counter+1);
	pc->setparam(sd, SP_KILLERRID, src?src->id:0);

	if( sd->bg_id ) {/* TODO: purge when bgqueue is deemed ok */
		struct battleground_data *bgd;
		if( (bgd = bg->team_search(sd->bg_id)) != NULL && bgd->die_event[0] )
			npc->event(sd, bgd->die_event, 0);
	}

	for (i = 0; i < VECTOR_LENGTH(sd->script_queues); i++ ) {
		struct script_queue *queue = script->queue(VECTOR_INDEX(sd->script_queues, i));
		if (queue && queue->event_death[0] != '\0')
			npc->event(sd, queue->event_death, 0);
	}

	npc->script_event(sd,NPCE_DIE);

	// Clear anything NPC-related when you die and was interacting with one.
	if ( (sd->npc_id || sd->npc_shopid) && sd->state.dialog) {
		if (sd->state.using_fake_npc) {
			clif->clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
			sd->state.using_fake_npc = 0;
		}
		if (sd->state.menu_or_input)
			sd->state.menu_or_input = 0;
		if (sd->npc_menu)
			sd->npc_menu = 0;

		sd->npc_id = 0;
		sd->npc_shopid = 0;
		if (sd->st && sd->st->state != END)
			sd->st->state = END;
	}

	/* e.g. not killed through pc->damage */
	if( pc_issit(sd) ) {
		clif->sc_end(&sd->bl,sd->bl.id,SELF,SI_SIT);
	}

	pc_setdead(sd);
	//Reset menu skills/item skills
	if (sd->skillitem)
		sd->skillitem = sd->skillitemlv = 0;
	if (sd->menuskill_id)
		sd->menuskill_id = sd->menuskill_val = 0;
	//Reset ticks.
	sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;

	if ( sd->spiritball )
		pc->delspiritball(sd, sd->spiritball, 0);
	if (sd->charm_type != CHARM_TYPE_NONE && sd->charm_count > 0)
		pc->del_charm(sd, sd->charm_count, sd->charm_type);

	if (src != NULL) {
		switch (src->type) {
			case BL_MOB:
			{
				struct mob_data *md = BL_UCAST(BL_MOB, src);
				if (md->target_id==sd->bl.id)
					mob->unlocktarget(md,tick);
				if (battle_config.mobs_level_up && md->status.hp
				 && md->level < pc->maxbaselv(sd)
				 && !md->guardian_data && md->special_state.ai == AI_NONE// Guardians/summons should not level. [Skotlex]
				) {
					// monster level up [Valaris]
					clif->misceffect(&md->bl,0);
					md->level++;
					status_calc_mob(md, SCO_NONE);
					status_percent_heal(src,10,0);

					if( battle_config.show_mob_info&4 )
					{// update name with new level
						clif->charnameack(0, &md->bl);
					}
				}
				src = battle->get_master(src); // Maybe Player Summon
			}
			break;
			case BL_PET: //Pass on to master...
				src = &BL_UCAST(BL_PET, src)->msd->bl;
			break;
			case BL_HOM:
				src = &BL_UCAST(BL_HOM, src)->master->bl;
			break;
			case BL_MER:
				src = &BL_UCAST(BL_MER, src)->master->bl;
			break;
		}
	}

	if (src != NULL && src->type == BL_PC) {
		struct map_session_data *ssd = BL_UCAST(BL_PC, src);
		pc->setparam(ssd, SP_KILLEDRID, sd->bl.id);
		npc->script_event(ssd, NPCE_KILLPC);

		if (battle_config.pk_mode&2) {
			ssd->status.manner -= 5;
			if(ssd->status.manner < 0)
				sc_start(NULL,src,SC_NOCHAT,100,0,0);
#if 0
			// PK/Karma system code (not enabled yet) [celest]
			// originally from Kade Online, so i don't know if any of these is correct ^^;
			// note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
			// karma going down = more 'good' / more honourable.
			// The Karma System way...

			if (sd->status.karma > ssd->status.karma) {
				// If player killed was more evil
				sd->status.karma--;
				ssd->status.karma--;
			}
			else if (sd->status.karma < ssd->status.karma) // If player killed was more good
				ssd->status.karma++;

			// or the PK System way...

			if (sd->status.karma > 0) // player killed is dishonourable?
				ssd->status.karma--; // honour points earned
			sd->status.karma++; // honour points lost

			// To-do: Receive exp on certain occasions
#endif
		}
	}

	if( battle_config.bone_drop==2
	 || (battle_config.bone_drop==1 && map->list[sd->bl.m].flag.pvp)
	) {
		struct item item_tmp;
		memset(&item_tmp,0,sizeof(item_tmp));
		item_tmp.nameid=ITEMID_SKULL_;
		item_tmp.identify=1;
		item_tmp.card[0]=CARD0_CREATE;
		item_tmp.card[1]=0;
		item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
		item_tmp.card[3]=GetWord(sd->status.char_id,1);
		map->addflooritem(&sd->bl, &item_tmp, 1, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 0);
	}

	// activate Steel body if a super novice dies at 99+% exp [celest]
	if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE && !sd->state.snovice_dead_flag) {
		unsigned int next = pc->nextbaseexp(sd);
		if( next == 0 ) next = pc->thisbaseexp(sd);
		if( get_percentage(sd->status.base_exp,next) >= 99 ) {
			sd->state.snovice_dead_flag = 1;
			pc->setstand(sd);
			status_percent_heal(&sd->bl, 100, 100);
			clif->resurrection(&sd->bl, 1);
			if(battle_config.pc_invincible_time)
				pc->setinvincibletimer(sd, battle_config.pc_invincible_time);
			sc_start(NULL,&sd->bl,status->skill2sc(MO_STEELBODY),100,1,skill->get_time(MO_STEELBODY,1));
			if(map_flag_gvg2(sd->bl.m))
				pc->respawn_timer(INVALID_TIMER, timer->gettick(), sd->bl.id, 0);
			return 0;
		}
	}

	// changed penalty options, added death by player if pk_mode [Valaris]
	if( battle_config.death_penalty_type
	   && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
	   && !map->list[sd->bl.m].flag.noexppenalty && !map_flag_gvg2(sd->bl.m)
	   && !sd->sc.data[SC_BABY] && !sd->sc.data[SC_CASH_DEATHPENALTY]
	   ) {
		if (battle_config.death_penalty_base > 0) {
			unsigned int base_penalty = 0;
			switch (battle_config.death_penalty_type) {
				case 1:
					base_penalty = (unsigned int) apply_percentrate64(pc->nextbaseexp(sd), battle_config.death_penalty_base, 10000);
					break;
				case 2:
					base_penalty = (unsigned int) apply_percentrate64(sd->status.base_exp, battle_config.death_penalty_base, 10000);
					break;
			}

			if (base_penalty != 0) {
				if (battle_config.pk_mode && src && src->type==BL_PC)
					base_penalty*=2;
				if( sd->status.mod_death != 100 )
					base_penalty = base_penalty * sd->status.mod_death / 100;
				sd->status.base_exp -= min(sd->status.base_exp, base_penalty);
				clif->updatestatus(sd,SP_BASEEXP);
			}
		}

		if(battle_config.death_penalty_job > 0) {
			unsigned int job_penalty = 0;

			switch (battle_config.death_penalty_type) {
				case 1:
					job_penalty = (unsigned int) apply_percentrate64(pc->nextjobexp(sd), battle_config.death_penalty_job, 10000);
					break;
				case 2:
					job_penalty = (unsigned int) apply_percentrate64(sd->status.job_exp, battle_config.death_penalty_job, 10000);
					break;
			}

			if (job_penalty != 0) {
				if (battle_config.pk_mode && src && src->type==BL_PC)
					job_penalty*=2;
				if( sd->status.mod_death != 100 )
					job_penalty = job_penalty * sd->status.mod_death / 100;
				sd->status.job_exp -= min(sd->status.job_exp, job_penalty);
				clif->updatestatus(sd,SP_JOBEXP);
			}
		}

		if (battle_config.zeny_penalty > 0 && !map->list[sd->bl.m].flag.nozenypenalty) {
			int zeny_penalty = apply_percentrate(sd->status.zeny, battle_config.zeny_penalty, 10000);
			if (zeny_penalty != 0)
				pc->payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
		}
	}

	if(map->list[sd->bl.m].flag.pvp_nightmaredrop) {
		// Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
		for(j=0;j<map->list[sd->bl.m].drop_list_count;j++){
			int id = map->list[sd->bl.m].drop_list[j].drop_id;
			int type = map->list[sd->bl.m].drop_list[j].drop_type;
			int per = map->list[sd->bl.m].drop_list[j].drop_per;
			if(id == 0)
				continue;
			if(id == -1){
				int eq_num=0,eq_n[MAX_INVENTORY],k;
				memset(eq_n,0,sizeof(eq_n));
				for(i=0;i<MAX_INVENTORY;i++){
					if( (type == 1 && !sd->status.inventory[i].equip)
						|| (type == 2 && sd->status.inventory[i].equip)
						||  type == 3)
					{
						ARR_FIND( 0, MAX_INVENTORY, k, eq_n[k] <= 0 );
						if( k < MAX_INVENTORY )
							eq_n[k] = i;

						eq_num++;
					}
				}
				if(eq_num > 0){
					int n = eq_n[rnd()%eq_num];
					if(rnd()%10000 < per){
						if(sd->status.inventory[n].equip)
							pc->unequipitem(sd, n, PCUNEQUIPITEM_RECALC|PCUNEQUIPITEM_FORCE);
						pc->dropitem(sd,n,1);
					}
				}
			}
			else if(id > 0){
				for(i=0;i<MAX_INVENTORY;i++){
					if(sd->status.inventory[i].nameid == id
						&& rnd()%10000 < per
						&& ((type == 1 && !sd->status.inventory[i].equip)
							|| (type == 2 && sd->status.inventory[i].equip)
							|| type == 3) ){
						if(sd->status.inventory[i].equip)
							pc->unequipitem(sd, i, PCUNEQUIPITEM_RECALC|PCUNEQUIPITEM_FORCE);
						pc->dropitem(sd,i,1);
						break;
					}
				}
			}
		}
	}

	// Remove autotrade to prevent autotrading from save point
	if( (sd->state.standalone || sd->state.autotrade)
	 && (map->list[sd->bl.m].flag.pvp || map->list[sd->bl.m].flag.gvg)
	  ) {
		sd->state.autotrade = 0;
		sd->state.standalone = 0;
		pc->autotrade_update(sd,PAUC_REMOVE);
		map->quit(sd);
	}

	// pvp
	// disable certain pvp functions on pk_mode [Valaris]
	if( map->list[sd->bl.m].flag.pvp && !battle_config.pk_mode && !map->list[sd->bl.m].flag.pvp_nocalcrank ) {
		sd->pvp_point -= 5;
		sd->pvp_lost++;
		if (src != NULL && src->type == BL_PC) {
			struct map_session_data *ssd = BL_UCAST(BL_PC, src);
			ssd->pvp_point++;
			ssd->pvp_won++;
		}
		if( sd->pvp_point < 0 )
		{
			timer->add(tick+1, pc->respawn_timer,sd->bl.id,0);
			return 1|8;
		}
	}
	//GvG
	if( map_flag_gvg2(sd->bl.m) ) {
		timer->add(tick+1, pc->respawn_timer, sd->bl.id, 0);
		return 1|8;
	} else if( sd->bg_id ) {
		struct battleground_data *bgd = bg->team_search(sd->bg_id);
		if( bgd && bgd->mapindex > 0 ) { // Respawn by BG
			timer->add(tick+1000, pc->respawn_timer, sd->bl.id, 0);
			return 1|8;
		}
	}

	//Reset "can log out" tick.
	if( battle_config.prevent_logout )
		sd->canlog_tick = timer->gettick() - battle_config.prevent_logout;

	return 1;
}

void pc_revive(struct map_session_data *sd,unsigned int hp, unsigned int sp) {
	nullpo_retv(sd);
	if(hp) clif->updatestatus(sd,SP_HP);
	if(sp) clif->updatestatus(sd,SP_SP);

	pc->setstand(sd);
	if(battle_config.pc_invincible_time > 0)
		pc->setinvincibletimer(sd, battle_config.pc_invincible_time);

	if( sd->state.gmaster_flag ) {
		guild->aura_refresh(sd,GD_LEADERSHIP,guild->checkskill(sd->guild,GD_LEADERSHIP));
		guild->aura_refresh(sd,GD_GLORYWOUNDS,guild->checkskill(sd->guild,GD_GLORYWOUNDS));
		guild->aura_refresh(sd,GD_SOULCOLD,guild->checkskill(sd->guild,GD_SOULCOLD));
		guild->aura_refresh(sd,GD_HAWKEYES,guild->checkskill(sd->guild,GD_HAWKEYES));
	}
}
// script
//
/*==========================================
 * script reading pc status registry
 *------------------------------------------*/
int pc_readparam(struct map_session_data* sd,int type)
{
	int val = 0;

	nullpo_ret(sd);

	switch(type) {
		case SP_SKILLPOINT:      val = sd->status.skill_point; break;
		case SP_STATUSPOINT:     val = sd->status.status_point; break;
		case SP_ZENY:            val = sd->status.zeny; break;
		case SP_BASELEVEL:       val = sd->status.base_level; break;
		case SP_JOBLEVEL:        val = sd->status.job_level; break;
		case SP_CLASS:           val = sd->status.class_; break;
		case SP_BASEJOB:         val = pc->mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
		case SP_UPPER:           val = (sd->class_&JOBL_UPPER) ? 1 : ((sd->class_&JOBL_BABY) ? 2 : 0); break;
		case SP_BASECLASS:       val = pc->mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
		case SP_SEX:             val = sd->status.sex; break;
		case SP_WEIGHT:          val = sd->weight; break;
		case SP_MAXWEIGHT:       val = sd->max_weight; break;
		case SP_BASEEXP:         val = sd->status.base_exp; break;
		case SP_JOBEXP:          val = sd->status.job_exp; break;
		case SP_NEXTBASEEXP:     val = pc->nextbaseexp(sd); break;
		case SP_NEXTJOBEXP:      val = pc->nextjobexp(sd); break;
		case SP_HP:              val = sd->battle_status.hp; break;
		case SP_MAXHP:           val = sd->battle_status.max_hp; break;
		case SP_SP:              val = sd->battle_status.sp; break;
		case SP_MAXSP:           val = sd->battle_status.max_sp; break;
		case SP_STR:             val = sd->status.str; break;
		case SP_AGI:             val = sd->status.agi; break;
		case SP_VIT:             val = sd->status.vit; break;
		case SP_INT:             val = sd->status.int_; break;
		case SP_DEX:             val = sd->status.dex; break;
		case SP_LUK:             val = sd->status.luk; break;
		case SP_KARMA:           val = sd->status.karma; break;
		case SP_MANNER:          val = sd->status.manner; break;
		case SP_FAME:            val = sd->status.fame; break;
		case SP_KILLERRID:       val = sd->killerrid; break;
		case SP_KILLEDRID:       val = sd->killedrid; break;
		case SP_SLOTCHANGE:      val = sd->status.slotchange; break;
		case SP_CHARRENAME:      val = sd->status.rename; break;
		case SP_MOD_EXP:         val = sd->status.mod_exp; break;
		case SP_MOD_DROP:        val = sd->status.mod_drop; break;
		case SP_MOD_DEATH:       val = sd->status.mod_death; break;
		case SP_CRITICAL:        val = sd->battle_status.cri/10; break;
		case SP_ASPD:            val = (2000-sd->battle_status.amotion)/10; break;
		case SP_BASE_ATK:        val = sd->battle_status.batk; break;
		case SP_DEF1:            val = sd->battle_status.def; break;
		case SP_DEF2:            val = sd->battle_status.def2; break;
		case SP_MDEF1:           val = sd->battle_status.mdef; break;
		case SP_MDEF2:           val = sd->battle_status.mdef2; break;
		case SP_HIT:             val = sd->battle_status.hit; break;
		case SP_FLEE1:           val = sd->battle_status.flee; break;
		case SP_FLEE2:           val = sd->battle_status.flee2; break;
		case SP_DEFELE:          val = sd->battle_status.def_ele; break;
#ifndef RENEWAL_CAST
		case SP_VARCASTRATE:
#endif
		case SP_CASTRATE:
				val = sd->castrate+=val;
			break;
		case SP_MAXHPRATE:       val = sd->hprate; break;
		case SP_MAXSPRATE:       val = sd->sprate; break;
		case SP_SPRATE:          val = sd->dsprate; break;
		case SP_SPEED_RATE:      val = sd->bonus.speed_rate; break;
		case SP_SPEED_ADDRATE:   val = sd->bonus.speed_add_rate; break;
		case SP_ASPD_RATE:
#ifndef RENEWAL_ASPD
			val = sd->battle_status.aspd_rate;
#else
			val = sd->battle_status.aspd_rate2;
#endif
			break;
		case SP_HP_RECOV_RATE:   val = sd->hprecov_rate; break;
		case SP_SP_RECOV_RATE:   val = sd->sprecov_rate; break;
		case SP_CRITICAL_DEF:    val = sd->bonus.critical_def; break;
		case SP_NEAR_ATK_DEF:    val = sd->bonus.near_attack_def_rate; break;
		case SP_LONG_ATK_DEF:    val = sd->bonus.long_attack_def_rate; break;
		case SP_DOUBLE_RATE:     val = sd->bonus.double_rate; break;
		case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
		case SP_MATK_RATE:       val = sd->matk_rate; break;
		case SP_ATK_RATE:        val = sd->bonus.atk_rate; break;
		case SP_MAGIC_ATK_DEF:   val = sd->bonus.magic_def_rate; break;
		case SP_MISC_ATK_DEF:    val = sd->bonus.misc_def_rate; break;
		case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
		case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
		case SP_CRITICAL_RATE:   val = sd->critical_rate; break;
		case SP_HIT_RATE:        val = sd->hit_rate; break;
		case SP_FLEE_RATE:       val = sd->flee_rate; break;
		case SP_FLEE2_RATE:      val = sd->flee2_rate; break;
		case SP_DEF_RATE:        val = sd->def_rate; break;
		case SP_DEF2_RATE:       val = sd->def2_rate; break;
		case SP_MDEF_RATE:       val = sd->mdef_rate; break;
		case SP_MDEF2_RATE:      val = sd->mdef2_rate; break;
		case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
		case SP_NO_CASTCANCEL:   val = sd->special_state.no_castcancel?1:0; break;
		case SP_NO_CASTCANCEL2:  val = sd->special_state.no_castcancel2?1:0; break;
		case SP_NO_SIZEFIX:      val = sd->special_state.no_sizefix?1:0; break;
		case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
		case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
		case SP_NO_MISC_DAMAGE:  val = sd->special_state.no_misc_damage; break;
		case SP_NO_GEMSTONE:     val = sd->special_state.no_gemstone?1:0; break;
		case SP_INTRAVISION:     val = sd->special_state.intravision?1:0; break;
		case SP_NO_KNOCKBACK:    val = sd->special_state.no_knockback?1:0; break;
		case SP_SPLASH_RANGE:    val = sd->bonus.splash_range; break;
		case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
		case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
		case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
		case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
		case SP_PERFECT_HIDE:    val = sd->special_state.perfect_hiding?1:0; break;
		case SP_UNBREAKABLE:     val = sd->bonus.unbreakable; break;
		case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
		case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
		case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
		case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
		case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
		case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
		case SP_CLASSCHANGE:     val = sd->bonus.classchange; break;
		case SP_LONG_ATK_RATE:   val = sd->bonus.long_attack_atk_rate; break;
		case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
		case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
		case SP_ADD_STEAL_RATE:  val = sd->bonus.add_steal_rate; break;
		case SP_DELAYRATE:       val = sd->delayrate; break;
		case SP_CRIT_ATK_RATE:   val = sd->bonus.crit_atk_rate; break;
		case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
		case SP_UNSTRIPABLE:
		case SP_UNSTRIPABLE_ARMOR:
			val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
			break;
		case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
		case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
		case SP_SP_GAIN_VALUE:   val = sd->bonus.sp_gain_value; break;
		case SP_HP_GAIN_VALUE:   val = sd->bonus.hp_gain_value; break;
		case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
		case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
		case SP_ADD_HEAL_RATE:   val = sd->bonus.add_heal_rate; break;
		case SP_ADD_HEAL2_RATE:  val = sd->bonus.add_heal2_rate; break;
		case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
		case SP_EMATK:           val = sd->bonus.ematk; break;
		case SP_FIXCASTRATE:     val = sd->bonus.fixcastrate; break;
		case SP_ADD_FIXEDCAST:   val = sd->bonus.add_fixcast; break;
#ifdef RENEWAL_CAST
		case SP_VARCASTRATE:     val = sd->bonus.varcastrate; break;
		case SP_ADD_VARIABLECAST:val = sd->bonus.add_varcast; break;
#endif
	}

	return val;
}

/*==========================================
 * script set pc status registry
 *------------------------------------------*/
int pc_setparam(struct map_session_data *sd,int type,int val)
{
	nullpo_ret(sd);

	switch(type){
	case SP_BASELEVEL:
		if (val > pc->maxbaselv(sd)) //Capping to max
			val = pc->maxbaselv(sd);
		if ((unsigned int)val > sd->status.base_level) {
			int stat = 0, i;
			for (i = 0; i < (int)((unsigned int)val - sd->status.base_level); i++)
				stat += pc->gets_status_point(sd->status.base_level + i);
			sd->status.status_point += stat;
		}
		sd->status.base_level = (unsigned int)val;
		sd->status.base_exp = 0;
		// clif->updatestatus(sd, SP_BASELEVEL);  // Gets updated at the bottom
		clif->updatestatus(sd, SP_NEXTBASEEXP);
		clif->updatestatus(sd, SP_STATUSPOINT);
		clif->updatestatus(sd, SP_BASEEXP);
		status_calc_pc(sd, SCO_FORCE);
		if(sd->status.party_id)
		{
			party->send_levelup(sd);
		}
		break;
	case SP_JOBLEVEL:
		if ((unsigned int)val >= sd->status.job_level) {
			if (val > pc->maxjoblv(sd))
				val = pc->maxjoblv(sd);
			sd->status.skill_point += val - sd->status.job_level;
			clif->updatestatus(sd, SP_SKILLPOINT);
		}
		sd->status.job_level = (unsigned int)val;
		sd->status.job_exp = 0;
		// clif->updatestatus(sd, SP_JOBLEVEL);  // Gets updated at the bottom
		clif->updatestatus(sd, SP_NEXTJOBEXP);
		clif->updatestatus(sd, SP_JOBEXP);
		status_calc_pc(sd, SCO_FORCE);
		break;
	case SP_SKILLPOINT:
		sd->status.skill_point = val;
		break;
	case SP_STATUSPOINT:
		sd->status.status_point = val;
		break;
	case SP_ZENY:
		if( val < 0 )
			return 0;// can't set negative zeny
		logs->zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
		sd->status.zeny = cap_value(val, 0, MAX_ZENY);
		break;
	case SP_BASEEXP:
		if(pc->nextbaseexp(sd) > 0) {
			sd->status.base_exp = val;
			pc->checkbaselevelup(sd);
		}
		break;
	case SP_JOBEXP:
		if(pc->nextjobexp(sd) > 0) {
			sd->status.job_exp = val;
			pc->checkjoblevelup(sd);
		}
		break;
	case SP_SEX:
		sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
		break;
	case SP_WEIGHT:
		sd->weight = val;
		break;
	case SP_MAXWEIGHT:
		sd->max_weight = val;
		break;
	case SP_HP:
		sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
		break;
	case SP_MAXHP:
		sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);

		if( sd->battle_status.max_hp < sd->battle_status.hp )
		{
			sd->battle_status.hp = sd->battle_status.max_hp;
			clif->updatestatus(sd, SP_HP);
		}
		break;
	case SP_SP:
		sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
		break;
	case SP_MAXSP:
		sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);

		if( sd->battle_status.max_sp < sd->battle_status.sp )
		{
			sd->battle_status.sp = sd->battle_status.max_sp;
			clif->updatestatus(sd, SP_SP);
		}
		break;
	case SP_STR:
		sd->status.str = cap_value(val, 1, pc_maxparameter(sd));
		break;
	case SP_AGI:
		sd->status.agi = cap_value(val, 1, pc_maxparameter(sd));
		break;
	case SP_VIT:
		sd->status.vit = cap_value(val, 1, pc_maxparameter(sd));
		break;
	case SP_INT:
		sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd));
		break;
	case SP_DEX:
		sd->status.dex = cap_value(val, 1, pc_maxparameter(sd));
		break;
	case SP_LUK:
		sd->status.luk = cap_value(val, 1, pc_maxparameter(sd));
		break;
	case SP_KARMA:
		sd->status.karma = val;
		break;
	case SP_MANNER:
		sd->status.manner = val;
		if( val < 0 )
			sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
		else {
			status_change_end(&sd->bl, SC_NOCHAT, INVALID_TIMER);
			clif->manner_message(sd, 5);
		}
		return 1; // status_change_start/status_change_end already sends packets warning the client
	case SP_FAME:
		sd->status.fame = val;
		break;
	case SP_KILLERRID:
		sd->killerrid = val;
		return 1;
	case SP_KILLEDRID:
		sd->killedrid = val;
		return 1;
	case SP_SLOTCHANGE:
		sd->status.slotchange = val;
		return 1;
	case SP_CHARRENAME:
		sd->status.rename = val;
		return 1;
	case SP_MOD_EXP:
		sd->status.mod_exp = val;
		return 1;
	case SP_MOD_DROP:
		sd->status.mod_drop = val;
		return 1;
	case SP_MOD_DEATH:
		sd->status.mod_death = val;
		return 1;
	default:
		ShowError("pc_setparam: Attempted to set unknown parameter '%d'.\n", type);
		return 0;
	}
	clif->updatestatus(sd,type);

	return 1;
}

/*==========================================
 * HP/SP Healing. If flag is passed, the heal type is through clif->heal, otherwise update status.
 *------------------------------------------*/
void pc_heal(struct map_session_data *sd,unsigned int hp,unsigned int sp, int type)
{
	nullpo_retv(sd);
	if (type) {
		if (hp)
			clif->heal(sd->fd,SP_HP,hp);
		if (sp)
			clif->heal(sd->fd,SP_SP,sp);
	} else {
		if(hp)
			clif->updatestatus(sd,SP_HP);
		if(sp)
			clif->updatestatus(sd,SP_SP);
	}
	return;
}

/*==========================================
 * HP/SP Recovery
 * Heal player hp and/or sp linearly.
 * Calculate bonus by status.
 *------------------------------------------*/
int pc_itemheal(struct map_session_data *sd,int itemid, int hp,int sp)
{
	int bonus, tmp;

	nullpo_ret(sd);
	if(hp) {
		int i;
		bonus = 100 + (sd->battle_status.vit<<1)
			+ pc->checkskill(sd,SM_RECOVERY)*10
			+ pc->checkskill(sd,AM_LEARNINGPOTION)*5;
		// A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
		if (script->potion_flag > 1)
			bonus += bonus*(script->potion_flag-1)*50/100;
		//All item bonuses.
		bonus += sd->bonus.itemhealrate2;
		//Individual item bonuses.
		for(i = 0; i < ARRAYLENGTH(sd->itemhealrate) && sd->itemhealrate[i].nameid; i++) {
			struct item_data *it = itemdb->exists(sd->itemhealrate[i].nameid);
			if (sd->itemhealrate[i].nameid == itemid || (it && it->group && itemdb->in_group(it->group,itemid))) {
				bonus += bonus*sd->itemhealrate[i].rate/100;
				break;
			}
		}

		tmp = hp*bonus/100;
		if(bonus != 100 && tmp > hp)
			hp = tmp;

		// Recovery Potion
		if( sd->sc.data[SC_HEALPLUS] )
			hp += (int)(hp * sd->sc.data[SC_HEALPLUS]->val1/100.);

		// 2014 Halloween Event : Pumpkin Bonus
		if ( sd->sc.data[SC_MTF_PUMPKIN] && itemid == ITEMID_PUMPKIN )
			hp += (int)(hp * sd->sc.data[SC_MTF_PUMPKIN]->val1/100);
	}
	if(sp) {
		bonus = 100 + (sd->battle_status.int_<<1)
			+ pc->checkskill(sd,MG_SRECOVERY)*10
			+ pc->checkskill(sd,AM_LEARNINGPOTION)*5;
		if (script->potion_flag > 1)
			bonus += bonus*(script->potion_flag-1)*50/100;

		tmp = sp*bonus/100;
		if(bonus != 100 && tmp > sp)
			sp = tmp;
	}
	if( sd->sc.count ) {
		if ( sd->sc.data[SC_CRITICALWOUND] ) {
			hp -= hp * sd->sc.data[SC_CRITICALWOUND]->val2 / 100;
			sp -= sp * sd->sc.data[SC_CRITICALWOUND]->val2 / 100;
		}

		if( sd->sc.data[SC_VITALITYACTIVATION] ){
			hp += hp / 2; // 1.5 times
			sp -= sp / 2;
		}

		if ( sd->sc.data[SC_DEATHHURT] ) {
			hp -= hp * 20 / 100;
			sp -= sp * 20 / 100;
		}

		if( sd->sc.data[SC_WATER_INSIGNIA] && sd->sc.data[SC_WATER_INSIGNIA]->val1 == 2 ) {
			hp += hp / 10;
			sp += sp / 10;
		}
#ifdef RENEWAL
		if( sd->sc.data[SC_EXTREMITYFIST2] )
			sp = 0;
#endif
	}

	return status->heal(&sd->bl, hp, sp, 1);
}

/*==========================================
 * HP/SP Recovery
 * Heal player hp nad/or sp by rate
 *------------------------------------------*/
int pc_percentheal(struct map_session_data *sd,int hp,int sp)
{
	nullpo_ret(sd);

	if(hp > 100) hp = 100;
	else
	if(hp <-100) hp =-100;

	if(sp > 100) sp = 100;
	else
	if(sp <-100) sp =-100;

	if(hp >= 0 && sp >= 0) //Heal
		return status_percent_heal(&sd->bl, hp, sp);

	if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
		return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);

	//Crossed signs
	if(hp) {
		if(hp > 0)
			status_percent_heal(&sd->bl, hp, 0);
		else
			status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
	}

	if(sp) {
		if(sp > 0)
			status_percent_heal(&sd->bl, 0, sp);
		else
			status_percent_damage(NULL, &sd->bl, 0, sp, false);
	}
	return 0;
}

int jobchange_killclone(struct block_list *bl, va_list ap)
{
	struct mob_data *md = NULL;
	int flag = va_arg(ap, int);

	nullpo_ret(bl);
	Assert_ret(bl->type == BL_MOB);
	md = BL_UCAST(BL_MOB, bl);

	if (md->master_id && md->special_state.clone && md->master_id == flag)
		status_kill(&md->bl);
	return 1;
}

/*==========================================
 * Called when player changes job
 * Rewrote to make it tidider [Celest]
 *------------------------------------------*/
int pc_jobchange(struct map_session_data *sd,int job, int upper)
{
	int i, fame_flag=0;
	int b_class, idx = 0;

	nullpo_ret(sd);

	if (job < 0)
		return 1;

	//Normalize job.
	b_class = pc->jobid2mapid(job);
	if (b_class == -1)
		return 1;
	switch (upper) {
		case 1:
			b_class|= JOBL_UPPER;
			break;
		case 2:
			b_class|= JOBL_BABY;
			break;
	}
	//This will automatically adjust bard/dancer classes to the correct gender
	//That is, if you try to jobchange into dancer, it will turn you to bard.
	job = pc->mapid2jobid(b_class, sd->status.sex);
	if (job == -1)
		return 1;

	if ((unsigned short)b_class == sd->class_)
		return 1; //Nothing to change.

	// changing from 1st to 2nd job
	if ((b_class&JOBL_2) && !(sd->class_&JOBL_2) && (b_class&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE) {
		sd->change_level_2nd = sd->status.job_level;
		pc_setglobalreg (sd, script->add_str("jobchange_level"), sd->change_level_2nd);
	}
	// changing from 2nd to 3rd job
	else if((b_class&JOBL_THIRD) && !(sd->class_&JOBL_THIRD)) {
		sd->change_level_3rd = sd->status.job_level;
		pc_setglobalreg (sd, script->add_str("jobchange_level_3rd"), sd->change_level_3rd);
	}

	if(sd->cloneskill_id) {
		idx = skill->get_index(sd->cloneskill_id);
		if( sd->status.skill[idx].flag == SKILL_FLAG_PLAGIARIZED ) {
			sd->status.skill[idx].id = 0;
			sd->status.skill[idx].lv = 0;
			sd->status.skill[idx].flag = 0;
			clif->deleteskill(sd,sd->cloneskill_id);
		}
		sd->cloneskill_id = 0;
		pc_setglobalreg(sd, script->add_str("CLONE_SKILL"), 0);
		pc_setglobalreg(sd, script->add_str("CLONE_SKILL_LV"), 0);
	}

	if(sd->reproduceskill_id) {
		idx = skill->get_index(sd->reproduceskill_id);
		if( sd->status.skill[idx].flag == SKILL_FLAG_PLAGIARIZED ) {
			sd->status.skill[idx].id = 0;
			sd->status.skill[idx].lv = 0;
			sd->status.skill[idx].flag = 0;
			clif->deleteskill(sd,sd->reproduceskill_id);
		}
		sd->reproduceskill_id = 0;
		pc_setglobalreg(sd, script->add_str("REPRODUCE_SKILL"),0);
		pc_setglobalreg(sd, script->add_str("REPRODUCE_SKILL_LV"),0);
	}

	if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
		const int class_ = pc->class2idx(sd->status.class_);
		short id;
		for(i = 0; i < MAX_SKILL_TREE && (id = pc->skill_tree[class_][i].id) > 0; i++) {
			//Remove status specific to your current tree skills.
			enum sc_type sc = status->skill2sc(id);
			if (sc > SC_COMMON_MAX && sd->sc.data[sc])
				status_change_end(&sd->bl, sc, INVALID_TIMER);
		}
	}

	if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
		/* going off star glad lineage, reset feel to not store no-longer-used vars in the database */
		pc->resetfeel(sd);
	}

	sd->status.class_ = job;
	fame_flag = pc->famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
	sd->class_ = (unsigned short)b_class;
	sd->status.job_level=1;
	sd->status.job_exp=0;

	if ((int)sd->status.base_level > pc->maxbaselv(sd)) { // FIXME
		sd->status.base_level = pc->maxbaselv(sd); // FIXME
		sd->status.base_exp=0;
		pc->resetstate(sd);
		clif->updatestatus(sd,SP_STATUSPOINT);
		clif->updatestatus(sd,SP_BASELEVEL);
		clif->updatestatus(sd,SP_BASEEXP);
		clif->updatestatus(sd,SP_NEXTBASEEXP);
	}

	clif->updatestatus(sd,SP_JOBLEVEL);
	clif->updatestatus(sd,SP_JOBEXP);
	clif->updatestatus(sd,SP_NEXTJOBEXP);

	for(i=0;i<EQI_MAX;i++) {
		if(sd->equip_index[i] >= 0)
			if(!pc->isequip(sd,sd->equip_index[i]))
				pc->unequipitem(sd,sd->equip_index[i], PCUNEQUIPITEM_FORCE); // unequip invalid item for class
	}

	//Change look, if disguised, you need to undisguise
	//to correctly calculate new job sprite without
	if (sd->disguise != -1)
		pc->disguise(sd, -1);

	status->set_viewdata(&sd->bl, job);
	clif->changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
	if(sd->vd.cloth_color)
		clif->changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
	if (sd->vd.body_style)
		clif->changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);

	//Update skill tree.
	pc->calc_skilltree(sd);
	clif->skillinfoblock(sd);

	if (sd->ed)
		elemental->delete(sd->ed, 0);
	if (sd->state.vending)
		vending->close(sd);

	map->foreachinmap(pc->jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);

	//Remove peco/cart/falcon
	i = sd->sc.option;
	if( i&OPTION_RIDING && (!pc->checkskill(sd, KN_RIDING) || (sd->class_&MAPID_THIRDMASK) == MAPID_RUNE_KNIGHT) )
		i&=~OPTION_RIDING;
	if( i&OPTION_FALCON && !pc->checkskill(sd, HT_FALCON) )
		i&=~OPTION_FALCON;
	if( i&OPTION_DRAGON && !pc->checkskill(sd,RK_DRAGONTRAINING) )
		i&=~OPTION_DRAGON;
	if( i&OPTION_WUGRIDER && !pc->checkskill(sd,RA_WUGMASTERY) )
		i&=~OPTION_WUGRIDER;
	if( i&OPTION_WUG && !pc->checkskill(sd,RA_WUGMASTERY) )
		i&=~OPTION_WUG;
	if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
		i&=~OPTION_MADOGEAR;
#ifndef NEW_CARTS
	if( i&OPTION_CART && !pc->checkskill(sd, MC_PUSHCART) )
		i&=~OPTION_CART;
#else
	if( sd->sc.data[SC_PUSH_CART] && !pc->checkskill(sd, MC_PUSHCART) )
		pc->setcart(sd, 0);
#endif
	if(i != sd->sc.option)
		pc->setoption(sd, i);

	if(homun_alive(sd->hd) && !pc->checkskill(sd, AM_CALLHOMUN))
		homun->vaporize(sd, HOM_ST_REST);

	if(sd->status.manner < 0)
		clif->changestatus(sd,SP_MANNER,sd->status.manner);

	status_calc_pc(sd,SCO_FORCE);
	pc->checkallowskill(sd);
	pc->equiplookall(sd);

	//if you were previously famous, not anymore.
	if (fame_flag) {
		chrif->save(sd,0);
		chrif->buildfamelist();
	} else if (sd->status.fame > 0) {
		//It may be that now they are famous?
		switch (sd->class_&MAPID_UPPERMASK) {
			case MAPID_BLACKSMITH:
			case MAPID_ALCHEMIST:
			case MAPID_TAEKWON:
				chrif->save(sd,0);
				chrif->buildfamelist();
			break;
		}
	}

	return 0;
}

/*==========================================
 * Tell client player sd has change equipement
 *------------------------------------------*/
int pc_equiplookall(struct map_session_data *sd)
{
	nullpo_ret(sd);

	clif->changelook(&sd->bl,LOOK_WEAPON,0);
	clif->changelook(&sd->bl,LOOK_SHOES,0);
	clif->changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
	clif->changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
	clif->changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
	clif->changelook(&sd->bl,LOOK_ROBE, sd->status.robe);

	return 0;
}

/*==========================================
 * Tell client player sd has change look (hair,equip...)
 *------------------------------------------*/
int pc_changelook(struct map_session_data *sd,int type,int val)
{
	nullpo_ret(sd);

	switch(type){
		case LOOK_BASE:
			status->set_viewdata(&sd->bl, val);
			clif->changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
			clif->changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
			if (sd->vd.cloth_color)
				clif->changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
			if (sd->vd.body_style)
				clif->changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
			clif->skillinfoblock(sd);
			return 0;
			break;
		case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
			val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);

			if (sd->status.hair != val) {
				sd->status.hair=val;
				if (sd->status.guild_id) //Update Guild Window. [Skotlex]
					intif->guild_change_memberinfo(sd->status.guild_id,sd->status.account_id,sd->status.char_id,
					GMI_HAIR,&sd->status.hair,sizeof(sd->status.hair));
			}
			break;
		case LOOK_WEAPON:
			sd->status.weapon=val;
			break;
		case LOOK_HEAD_BOTTOM:
			sd->status.head_bottom=val;
			break;
		case LOOK_HEAD_TOP:
			sd->status.head_top=val;
			break;
		case LOOK_HEAD_MID:
			sd->status.head_mid=val;
			break;
		case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
			val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);

			if (sd->status.hair_color != val) {
				sd->status.hair_color=val;
				if (sd->status.guild_id) //Update Guild Window. [Skotlex]
					intif->guild_change_memberinfo(sd->status.guild_id,sd->status.account_id,sd->status.char_id,
					GMI_HAIR_COLOR,&sd->status.hair_color,sizeof(sd->status.hair_color));
			}
			break;
		case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
			val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);

			sd->status.clothes_color=val;
			break;
		case LOOK_SHIELD:
			sd->status.shield=val;
			break;
		case LOOK_SHOES:
			break;
		case LOOK_ROBE:
			sd->status.robe = val;
			break;
		case LOOK_BODY2:
			val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
			sd->status.body=val;
			break;
	}
	clif->changelook(&sd->bl,type,val);
	return 0;
}

/*==========================================
 * Give an option (type) to player (sd) and display it to client
 *------------------------------------------*/
int pc_setoption(struct map_session_data *sd,int type)
{
	int p_type, new_look=0;
	nullpo_ret(sd);
	p_type = sd->sc.option;

	//Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
	sd->sc.option=type;
	clif->changeoption(&sd->bl);

	if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc->checkskill(sd,RK_DRAGONTRAINING) > 0) ) {
		// Mounting
		clif->sc_load(&sd->bl,sd->bl.id,AREA,SI_RIDING, 0, 0, 0);
		status_calc_pc(sd,SCO_NONE);
	} else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON) ) {
		// Dismount
		clif->sc_end(&sd->bl,sd->bl.id,AREA,SI_RIDING);
		status_calc_pc(sd,SCO_NONE);
	}

#ifndef NEW_CARTS
	if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
		clif->cartlist(sd);
		clif->updatestatus(sd, SP_CARTINFO);
		if(pc->checkskill(sd, MC_PUSHCART) < 10)
			status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
	} else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
		clif->clearcart(sd->fd);
		if(pc->checkskill(sd, MC_PUSHCART) < 10)
			status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
		if ( sd->equip_index[EQI_AMMO] > 0 )
			pc->unequipitem(sd, sd->equip_index[EQI_AMMO], PCUNEQUIPITEM_FORCE);
	}
#endif

	if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
		clif->sc_load(&sd->bl,sd->bl.id,AREA,SI_FALCON, 0, 0, 0);
	else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
		clif->sc_end(&sd->bl,sd->bl.id,AREA,SI_FALCON);

	if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
		clif->sc_load(&sd->bl,sd->bl.id,AREA,SI_WUGRIDER, 0, 0, 0);
		status_calc_pc(sd,SCO_NONE);
	} else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
		clif->sc_end(&sd->bl,sd->bl.id,AREA,SI_WUGRIDER);
		status_calc_pc(sd,SCO_NONE);
	}

	if( (type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR))
	|| (!(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR) ) {
		int i;
		status_calc_pc(sd, SCO_NONE);

		// End all SCs that can be reset when mado is taken off
		for( i = 0; i < SC_MAX; i++ ) {
			if ( !sd->sc.data[i] || !status->get_sc_type(i) )
				continue;
			if ( status->get_sc_type(i)&SC_MADO_NO_RESET )
				continue;
			switch (i) {
				case SC_BERSERK:
				case SC_SATURDAY_NIGHT_FEVER:
					sd->sc.data[i]->val2 = 0;
					break;
			}
			status_change_end(&sd->bl, (sc_type)i, INVALID_TIMER);
		}
		if ( sd->equip_index[EQI_AMMO] > 0 )
			pc->unequipitem(sd, sd->equip_index[EQI_AMMO], PCUNEQUIPITEM_FORCE);
	}

	if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
		new_look = JOB_STAR_GLADIATOR2;
	else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
		new_look = -1;

	if (sd->disguise != -1 || !new_look)
		return 0; //Disguises break sprite changes

	if (new_look < 0) { //Restore normal look.
		status->set_viewdata(&sd->bl, sd->status.class_);
		new_look = sd->vd.class_;
	}

	pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
	clif->changelook(&sd->bl,LOOK_BASE,new_look);
	if (sd->vd.cloth_color)
		clif->changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
	if( sd->vd.body_style )
		clif->changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
	clif->skillinfoblock(sd); // Skill list needs to be updated after base change.

	return 0;
}

/*==========================================
 * Give player a cart
 *------------------------------------------*/
int pc_setcart(struct map_session_data *sd,int type) {
#ifndef NEW_CARTS
	int cart[6] = {OPTION_NOTHING,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
	int option;
#endif
	nullpo_ret(sd);

	if( type < 0 || type > MAX_CARTS )
		return 1;// Never trust the values sent by the client! [Skotlex]

	if( pc->checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
		return 1;// Push cart is required

	if( type == 0 && pc_iscarton(sd) )
		status_change_end(&sd->bl,SC_GN_CARTBOOST,INVALID_TIMER);

#ifdef NEW_CARTS

	switch( type ) {
		case 0:
			if( !sd->sc.data[SC_PUSH_CART] )
				return 0;
			status_change_end(&sd->bl,SC_PUSH_CART,INVALID_TIMER);
			clif->clearcart(sd->fd);
			clif->updatestatus(sd, SP_CARTINFO);
			if ( sd->equip_index[EQI_AMMO] > 0 )
				pc->unequipitem(sd, sd->equip_index[EQI_AMMO], PCUNEQUIPITEM_FORCE);
			break;
		default:/* everything else is an allowed ID so we can move on */
			if( !sd->sc.data[SC_PUSH_CART] ) /* first time, so fill cart data */
				clif->cartlist(sd);
			clif->updatestatus(sd, SP_CARTINFO);
			sc_start(NULL,&sd->bl, SC_PUSH_CART, 100, type, 0);
			clif->sc_load(&sd->bl, sd->bl.id, AREA, SI_ON_PUSH_CART, type, 0, 0);
			if( sd->sc.data[SC_PUSH_CART] )/* forcefully update */
				sd->sc.data[SC_PUSH_CART]->val1 = type;
			break;
	}

	if(pc->checkskill(sd, MC_PUSHCART) < 10)
		status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
#else
	// Update option
	option = sd->sc.option;
	option &= ~OPTION_CART;// clear cart bits
	option |= cart[type]; // set cart
	pc->setoption(sd, option);
#endif

	return 0;
}

/* FIXME: These setter methods are inconsistent in their class/skill checks.
 *        They should be changed so that they all either do or skip the checks.*/

/**
 * Gives/removes a falcon.
 *
 * The target player needs the required skills in order to obtain a falcon.
 *
 * @param sd Target player.
 * @param flag New state.
 **/
void pc_setfalcon(struct map_session_data *sd, bool flag)
{
	nullpo_retv(sd);
	if (flag) {
		if (pc->checkskill(sd,HT_FALCON) > 0) // add falcon if he have the skill
			pc->setoption(sd,sd->sc.option|OPTION_FALCON);
	} else if (pc_isfalcon(sd)) {
		pc->setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
	}
}

/**
 * Mounts/dismounts a Peco or Gryphon.
 *
 * The target player needs the required skills in order to mount a peco.
 *
 * @param sd Target player.
 * @param flag New state.
 **/
void pc_setridingpeco(struct map_session_data *sd, bool flag)
{
	nullpo_retv(sd);
	if (flag) {
		if (pc->checkskill(sd, KN_RIDING))
			pc->setoption(sd, sd->sc.option|OPTION_RIDING);
	} else if (pc_isridingpeco(sd)) {
		pc->setoption(sd, sd->sc.option&~OPTION_RIDING);
	}
}

/**
 * Gives/removes a Mado Gear.
 *
 * The target player needs to be the correct class in order to obtain a mado gear.
 *
 * @param sd Target player.
 * @param flag New state.
 **/
void pc_setmadogear(struct map_session_data *sd, bool flag)
{
	nullpo_retv(sd);
	if (flag) {
		if ((sd->class_&MAPID_THIRDMASK) == MAPID_MECHANIC)
			pc->setoption(sd, sd->sc.option|OPTION_MADOGEAR);
	} else if (pc_ismadogear(sd)) {
		pc->setoption(sd, sd->sc.option&~OPTION_MADOGEAR);
	}
}

/**
 * Mounts/dismounts a dragon.
 *
 * The target player needs the required skills in order to mount a dragon.
 *
 * @param sd Target player.
 * @param type New state. This must be a valid OPTION_DRAGON* or 0.
 **/
void pc_setridingdragon(struct map_session_data *sd, unsigned int type)
{
	nullpo_retv(sd);
	if (type&OPTION_DRAGON) {
		// Ensure only one dragon is set at a time.
		if (type&OPTION_DRAGON1)
			type = OPTION_DRAGON1;
		else if (type&OPTION_DRAGON2)
			type = OPTION_DRAGON2;
		else if (type&OPTION_DRAGON3)
			type = OPTION_DRAGON3;
		else if (type&OPTION_DRAGON4)
			type = OPTION_DRAGON4;
		else if (type&OPTION_DRAGON5)
			type = OPTION_DRAGON5;
		else
			type = OPTION_DRAGON1;

		if (pc->checkskill(sd, RK_DRAGONTRAINING))
			pc->setoption(sd, (sd->sc.option&~OPTION_DRAGON)|type);
	} else if (pc_isridingdragon(sd)) {
		pc->setoption(sd,sd->sc.option&~OPTION_DRAGON); // remove dragon
	}
}

/**
 * Mounts/dismounts a wug.
 *
 * The target player needs the required skills in order to mount a wug.
 *
 * @param sd Target player.
 * @param flag New state.
 **/
void pc_setridingwug(struct map_session_data *sd, bool flag)
{
	nullpo_retv(sd);
	if (flag) {
		if (pc->checkskill(sd, RA_WUGRIDER) > 0)
			pc->setoption(sd,sd->sc.option|OPTION_WUGRIDER);
	} else if (pc_isridingwug(sd)) {
		pc->setoption(sd,sd->sc.option&~OPTION_WUGRIDER); // remove wug
	}
}

/**
 * Determines whether a player can attack based on status changes
 *  Why not use status_check_skilluse?
 *  "src MAY be null to indicate we shouldn't check it, this is a ground-based skill attack."
 *  Even ground-based attacks should be blocked by these statuses
 * Called from unit_attack and unit_attack_timer_sub
 * @retval true Can attack
 **/
bool pc_can_attack( struct map_session_data *sd, int target_id ) {
	nullpo_retr(false, sd);

	if( sd->sc.data[SC_BASILICA] ||
		sd->sc.data[SC__SHADOWFORM] ||
		sd->sc.data[SC__MANHOLE] ||
		sd->sc.data[SC_CURSEDCIRCLE_ATKER] ||
		sd->sc.data[SC_CURSEDCIRCLE_TARGET] ||
		sd->sc.data[SC_COLD] ||
		sd->sc.data[SC_ALL_RIDING] || // The client doesn't let you, this is to make cheat-safe
		sd->sc.data[SC_TRICKDEAD] ||
		(sd->sc.data[SC_SIREN] && sd->sc.data[SC_SIREN]->val2 == target_id) ||
		sd->sc.data[SC_BLADESTOP] ||
		sd->sc.data[SC_DEEP_SLEEP] ||
		sd->sc.data[SC_FALLENEMPIRE] )
			return false;

	return true;
}

/**
 * Determines whether a player can talk/whisper based on status changes
 * Called from clif_parse_GlobalMessage and clif_parse_WisMessage
 * @retval true Can talk
 **/
bool pc_can_talk( struct map_session_data *sd ) {
	nullpo_retr(false, sd);

	if( sd->sc.data[SC_BERSERK] ||
		(sd->sc.data[SC_DEEP_SLEEP] && sd->sc.data[SC_DEEP_SLEEP]->val2) ||
		pc_ismuted(&sd->sc, MANNER_NOCHAT) )
		return false;

	return true;
}

/*==========================================
 * Check if player can drop an item
 *------------------------------------------*/
int pc_candrop(struct map_session_data *sd, struct item *item)
{
	if( item && (item->expire_time || (item->bound && !pc_can_give_bound_items(sd))) )
		return 0;
	if( !pc_can_give_items(sd) ) //check if this GM level can drop items
		return 0;
	return (itemdb_isdropable(item, pc_get_group_level(sd)));
}
/**
 * For '@type' variables (temporary numeric char reg)
 **/
int pc_readreg(struct map_session_data* sd, int64 reg) {
	nullpo_ret(sd);
	return i64db_iget(sd->regs.vars, reg);
}
/**
 * For '@type' variables (temporary numeric char reg)
 **/
void pc_setreg(struct map_session_data* sd, int64 reg, int val) {
	unsigned int index = script_getvaridx(reg);

	nullpo_retv(sd);
	if( val ) {
		i64db_iput(sd->regs.vars, reg, val);
		if( index )
			script->array_update(&sd->regs, reg, false);
	} else {
		i64db_remove(sd->regs.vars, reg);
		if( index )
			script->array_update(&sd->regs, reg, true);
	}
}

/**
 * For '@type$' variables (temporary string char reg)
 **/
char* pc_readregstr(struct map_session_data* sd, int64 reg) {
	struct script_reg_str *p = NULL;

	nullpo_retr(NULL, sd);
	p = i64db_get(sd->regs.vars, reg);

	return p ? p->value : NULL;
}
/**
 * For '@type$' variables (temporary string char reg)
 **/
void pc_setregstr(struct map_session_data* sd, int64 reg, const char* str) {
	struct script_reg_str *p = NULL;
	unsigned int index = script_getvaridx(reg);
	struct DBData prev;

	nullpo_retv(sd);
	nullpo_retv(str);
	if( str[0] ) {
		p = ers_alloc(pc->str_reg_ers, struct script_reg_str);

		p->value = aStrdup(str);
		p->flag.type = 1;

		if( sd->regs.vars->put(sd->regs.vars, DB->i642key(reg), DB->ptr2data(p), &prev) ) {
			p = DB->data2ptr(&prev);
			if( p->value )
				aFree(p->value);
			ers_free(pc->str_reg_ers, p);
		} else {
			if( index )
				script->array_update(&sd->regs, reg, false);
		}
	} else {
		if( sd->regs.vars->remove(sd->regs.vars, DB->i642key(reg), &prev) ) {
			p = DB->data2ptr(&prev);
			if( p->value )
				aFree(p->value);
			ers_free(pc->str_reg_ers, p);
			if( index )
				script->array_update(&sd->regs, reg, true);
		}
	}
}
/**
 * Serves the following variable types:
 * - 'type' (permanent nuneric char reg)
 * - '#type' (permanent numeric account reg)
 * - '##type' (permanent numeric account reg2)
 **/
int pc_readregistry(struct map_session_data *sd, int64 reg) {
	struct script_reg_num *p = NULL;

	nullpo_ret(sd);
	if (!sd->vars_ok) {
		ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", script->get_str(script_getvarid(reg)));
		//This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
		//intif->request_registry(sd,type==3?4:type);
		sockt->eof(sd->fd);
		return 0;
	}

	p = i64db_get(sd->regs.vars, reg);

	return p ? p->value : 0;
}
/**
 * Serves the following variable types:
 * - 'type$' (permanent str char reg)
 * - '#type$' (permanent str account reg)
 * - '##type$' (permanent str account reg2)
 **/
char* pc_readregistry_str(struct map_session_data *sd, int64 reg) {
	struct script_reg_str *p = NULL;

	nullpo_retr(NULL, sd);
	if (!sd->vars_ok) {
		ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", script->get_str(script_getvarid(reg)));
		//This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
		//intif->request_registry(sd,type==3?4:type);
		sockt->eof(sd->fd);
		return NULL;
	}

	p = i64db_get(sd->regs.vars, reg);

	return p ? p->value : NULL;
}
/**
 * Serves the following variable types:
 * - 'type' (permanent nuneric char reg)
 * - '#type' (permanent numeric account reg)
 * - '##type' (permanent numeric account reg2)
 **/
int pc_setregistry(struct map_session_data *sd, int64 reg, int val) {
	struct script_reg_num *p = NULL;
	const char *regname = script->get_str( script_getvarid(reg) );
	unsigned int index = script_getvaridx(reg);

	nullpo_ret(sd);
	/* SAAD! those things should be stored elsewhere e.g. char ones in char table, the cash ones in account_data table! */
	switch( regname[0] ) {
		default: //Char reg
			if( !strcmp(regname,"PC_DIE_COUNTER") && sd->die_counter != val ) {
				int i = (!sd->die_counter && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE);
				sd->die_counter = val;
				if( i )
					status_calc_pc(sd,SCO_NONE); // Lost the bonus.
			} else if( !strcmp(regname,"COOK_MASTERY") && sd->cook_mastery != val ) {
				val = cap_value(val, 0, 1999);
				sd->cook_mastery = val;
			}
			break;
		case '#':
			if( !strcmp(regname,"#CASHPOINTS") && sd->cashPoints != val ) {
				val = cap_value(val, 0, MAX_ZENY);
				sd->cashPoints = val;
			} else if( !strcmp(regname,"#KAFRAPOINTS") && sd->kafraPoints != val ) {
				val = cap_value(val, 0, MAX_ZENY);
				sd->kafraPoints = val;
			}
			break;
	}

	if ( !pc->reg_load && !sd->vars_ok ) {
		ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
		return 0;
	}

	if( (p = i64db_get(sd->regs.vars, reg) ) ) {
		if( val ) {
			if( !p->value && index ) /* its a entry that was deleted, so we reset array */
				script->array_update(&sd->regs, reg, false);
			p->value = val;
		} else {
			p->value = 0;
			if( index )
				script->array_update(&sd->regs, reg, true);
		}
		if( !pc->reg_load )
			p->flag.update = 1;/* either way, it will require either delete or replace */
	} else if( val ) {
		struct DBData prev;

		if( index )
			script->array_update(&sd->regs, reg, false);

		p = ers_alloc(pc->num_reg_ers, struct script_reg_num);

		p->value = val;
		if( !pc->reg_load )
			p->flag.update = 1;

		if( sd->regs.vars->put(sd->regs.vars, DB->i642key(reg), DB->ptr2data(p), &prev) ) {
			p = DB->data2ptr(&prev);
			ers_free(pc->num_reg_ers, p);
		}
	}

	if( !pc->reg_load && p )
		sd->vars_dirty = true;

	return 1;
}
/**
 * Serves the following variable types:
 * - 'type$' (permanent str char reg)
 * - '#type$' (permanent str account reg)
 * - '##type$' (permanent str account reg2)
 **/
int pc_setregistry_str(struct map_session_data *sd, int64 reg, const char *val) {
	struct script_reg_str *p = NULL;
	const char *regname = script->get_str( script_getvarid(reg) );
	unsigned int index = script_getvaridx(reg);

	nullpo_ret(sd);
	nullpo_ret(val);
	if ( !pc->reg_load && !sd->vars_ok ) {
		ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
		return 0;
	}

	if( (p = i64db_get(sd->regs.vars, reg) ) ) {
		if( val[0] ) {
			if( p->value )
				aFree(p->value);
			else if ( index ) /* a entry that was deleted, so we reset */
				script->array_update(&sd->regs, reg, false);
			p->value = aStrdup(val);
		} else {
			p->value = NULL;
			if( index )
				script->array_update(&sd->regs, reg, true);
		}
		if( !pc->reg_load )
			p->flag.update = 1;/* either way, it will require either delete or replace */
	} else if( val[0] ) {
		struct DBData prev;

		if( index )
			script->array_update(&sd->regs, reg, false);

		p = ers_alloc(pc->str_reg_ers, struct script_reg_str);

		p->value = aStrdup(val);
		if( !pc->reg_load )
			p->flag.update = 1;
		p->flag.type = 1;

		if( sd->regs.vars->put(sd->regs.vars, DB->i642key(reg), DB->ptr2data(p), &prev) ) {
			p = DB->data2ptr(&prev);
			if( p->value )
				aFree(p->value);
			ers_free(pc->str_reg_ers, p);
		}
	}

	if( !pc->reg_load && p )
		sd->vars_dirty = true;

	return 1;
}

/*==========================================
 * Exec eventtimer for player sd (retrieved from map_session (id))
 *------------------------------------------*/
int pc_eventtimer(int tid, int64 tick, int id, intptr_t data) {
	struct map_session_data *sd=map->id2sd(id);
	char *p = (char *)data;
	int i;
	if(sd==NULL)
		return 0;

	ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
	if( i < MAX_EVENTTIMER )
	{
		sd->eventtimer[i] = INVALID_TIMER;
		sd->eventcount--;
		npc->event(sd,p,0);
	}
	else
		ShowError("pc_eventtimer: no such event timer\n");

	if (p) aFree(p);
	return 0;
}

/*==========================================
 * Add eventtimer for player sd ?
 *------------------------------------------*/
int pc_addeventtimer(struct map_session_data *sd,int tick,const char *name)
{
	int i;
	nullpo_ret(sd);
	nullpo_ret(name);

	ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
	if( i == MAX_EVENTTIMER )
		return 0;

	sd->eventtimer[i] = timer->add(timer->gettick()+tick, pc->eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
	sd->eventcount++;

	return 1;
}

/*==========================================
 * Del eventtimer for player sd ?
 *------------------------------------------*/
int pc_deleventtimer(struct map_session_data *sd,const char *name)
{
	char* p = NULL;
	int i;

	nullpo_ret(sd);
	nullpo_ret(name);

	if (sd->eventcount <= 0)
		return 0;

	// find the named event timer
	ARR_FIND( 0, MAX_EVENTTIMER, i,
		sd->eventtimer[i] != INVALID_TIMER &&
		(p = (char *)(timer->get(sd->eventtimer[i])->data)) != NULL &&
		strcmp(p, name) == 0
	);
	if( i == MAX_EVENTTIMER )
		return 0; // not found

	timer->delete(sd->eventtimer[i],pc->eventtimer);
	sd->eventtimer[i] = INVALID_TIMER;
	sd->eventcount--;
	aFree(p);

	return 1;
}

/*==========================================
 * Update eventtimer count for player sd
 *------------------------------------------*/
int pc_addeventtimercount(struct map_session_data *sd,const char *name,int tick)
{
	int i;

	nullpo_ret(sd);

	for(i=0;i<MAX_EVENTTIMER;i++)
		if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
			(char *)(timer->get(sd->eventtimer[i])->data), name)==0 ){
				timer->addtick(sd->eventtimer[i],tick);
				break;
		}

	return 0;
}

/*==========================================
 * Remove all eventtimer for player sd
 *------------------------------------------*/
int pc_cleareventtimer(struct map_session_data *sd)
{
	int i;

	nullpo_ret(sd);

	if (sd->eventcount <= 0)
		return 0;

	for(i=0;i<MAX_EVENTTIMER;i++)
		if( sd->eventtimer[i] != INVALID_TIMER ){
			char *p = (char *)(timer->get(sd->eventtimer[i])->data);
			timer->delete(sd->eventtimer[i],pc->eventtimer);
			sd->eventtimer[i] = INVALID_TIMER;
			sd->eventcount--;
			if (p) aFree(p);
		}
	return 0;
}
/* called when a item with combo is worn */
int pc_checkcombo(struct map_session_data *sd, struct item_data *data ) {
	int i, j, k, z;
	int index, success = 0;
	struct pc_combos *combo;

	nullpo_ret(sd);
	nullpo_ret(data);
	for( i = 0; i < data->combos_count; i++ ) {

		/* ensure this isn't a duplicate combo */
		if( sd->combos != NULL ) {
			int x;

			ARR_FIND( 0, sd->combo_count, x, sd->combos[x].id == data->combos[i]->id );

			/* found a match, skip this combo */
			if( x < sd->combo_count )
				continue;
		}

		for( j = 0; j < data->combos[i]->count; j++ ) {
			int id = data->combos[i]->nameid[j];
			bool found = false;

			for( k = 0; k < EQI_MAX; k++ ) {
				index = sd->equip_index[k];
				if( index < 0 ) continue;
				if( k == EQI_HAND_R   &&  sd->equip_index[EQI_HAND_L] == index ) continue;
				if( k == EQI_HEAD_MID &&  sd->equip_index[EQI_HEAD_LOW] == index ) continue;
				if( k == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index) ) continue;

				if(!sd->inventory_data[index])
					continue;

				if ( itemdb_type(id) != IT_CARD ) {
					if ( sd->inventory_data[index]->nameid != id )
						continue;

					found = true;
					break;
				} else { //Cards
					if ( sd->inventory_data[index]->slot == 0 || itemdb_isspecial(sd->status.inventory[index].card[0]) )
						continue;

					for (z = 0; z < sd->inventory_data[index]->slot; z++) {

						if (sd->status.inventory[index].card[z] != id)
							continue;

						// We have found a match
						found = true;
						break;
					}
				}

			}

			if( !found )
				break;/* we haven't found all the ids for this combo, so we can return */
		}

		/* means we broke out of the count loop w/o finding all ids, we can move to the next combo */
		if( j < data->combos[i]->count )
			continue;

		/* we got here, means all items in the combo are matching */

		RECREATE(sd->combos, struct pc_combos, ++sd->combo_count);
		combo = &sd->combos[sd->combo_count - 1];
		combo->bonus = data->combos[i]->script;
		combo->id = data->combos[i]->id;

		success++;
	}

	return success;
}

/* called when a item with combo is removed */
int pc_removecombo(struct map_session_data *sd, struct item_data *data ) {
	int i, retval = 0;

	nullpo_ret(sd);
	nullpo_ret(data);
	if( !sd->combos )
		return 0;/* nothing to do here, player has no combos */

	for( i = 0; i < data->combos_count; i++ ) {
		/* check if this combo exists in this user */
		int x = 0, cursor = 0, j;

		ARR_FIND( 0, sd->combo_count, x, sd->combos[x].id == data->combos[i]->id );
		/* no match, skip this combo */
		if( x == sd->combo_count )
			continue;

		sd->combos[x].bonus = NULL;
		sd->combos[x].id = 0;

		retval++;

		for( j = 0, cursor = 0; j < sd->combo_count; j++ ) {
			if( sd->combos[j].bonus == NULL )
				continue;

			if( cursor != j ) {
				sd->combos[cursor].bonus = sd->combos[j].bonus;
				sd->combos[cursor].id    = sd->combos[j].id;
			}

			cursor++;
		}

		/* it's empty, we can clear all the memory */
		if( (sd->combo_count = cursor) == 0 ) {
			aFree(sd->combos);
			sd->combos = NULL;
			break;
		}
	}

	/* check if combo requirements still fit -- don't touch retval! */
	pc->checkcombo( sd, data );

	return retval;
}
int pc_load_combo(struct map_session_data *sd) {
	int i, ret = 0;
	nullpo_ret(sd);
	for( i = 0; i < EQI_MAX; i++ ) {
		struct item_data *id = NULL;
		int idx = sd->equip_index[i];
		if( sd->equip_index[i] < 0 || !(id = sd->inventory_data[idx] ) )
			continue;
		if( id->combos_count )
			ret += pc->checkcombo(sd,id);
		if(!itemdb_isspecial(sd->status.inventory[idx].card[0])) {
			struct item_data *data;
			int j;
			for( j = 0; j < id->slot; j++ ) {
				if (!sd->status.inventory[idx].card[j])
					continue;
				if ( ( data = itemdb->exists(sd->status.inventory[idx].card[j]) ) != NULL ) {
					if( data->combos_count )
						ret += pc->checkcombo(sd,data);
				}
			}
		}
	}
	return ret;
}

/**
* Equip item at given position.
* @param sd the affected player structure. Must be checked before.
* @param id item structure for equip. Must be checked before.
* @param n inventory item position. Must be checked before.
* @param pos slot position. Must be checked before.
**/
void pc_equipitem_pos(struct map_session_data *sd, struct item_data *id, int n, int pos)
{
	nullpo_retv(sd);
	if ((!map_no_view(sd->bl.m,EQP_SHADOW_WEAPON) && pos & EQP_SHADOW_WEAPON) ||
		 (pos & EQP_HAND_R)) {
		if(id)
			sd->weapontype1 = id->look;
		else
			sd->weapontype1 = 0;
		pc->calcweapontype(sd);
		clif->changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
	}
	if ((!map_no_view(sd->bl.m,EQP_SHADOW_SHIELD) && pos & EQP_SHADOW_SHIELD) ||
		 (pos & EQP_HAND_L)) {
		if (id) {
			if(id->type == IT_WEAPON) {
				sd->status.shield = 0;
				sd->weapontype2 = id->look;
			} else if(id->type == IT_ARMOR) {
				sd->status.shield = id->look;
				sd->weapontype2 = 0;
			}
		} else
			sd->status.shield = sd->weapontype2 = 0;
		pc->calcweapontype(sd);
		clif->changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
	}
	//Added check to prevent sending the same look on multiple slots ->
	//causes client to redraw item on top of itself. (suggested by Lupus)
	if (!map_no_view(sd->bl.m,EQP_HEAD_LOW) && pos & EQP_HEAD_LOW && pc->checkequip(sd,EQP_COSTUME_HEAD_LOW) == -1) {
		if (id && !(pos&(EQP_HEAD_TOP|EQP_HEAD_MID)))
			sd->status.head_bottom = id->look;
		else
			sd->status.head_bottom = 0;
		clif->changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
	}
	if (!map_no_view(sd->bl.m,EQP_HEAD_TOP) && pos & EQP_HEAD_TOP && pc->checkequip(sd,EQP_COSTUME_HEAD_TOP) == -1) {
		if (id)
			sd->status.head_top = id->look;
		else
			sd->status.head_top = 0;
		clif->changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
	}
	if (!map_no_view(sd->bl.m,EQP_HEAD_MID) && pos & EQP_HEAD_MID && pc->checkequip(sd,EQP_COSTUME_HEAD_MID) == -1) {
		if (id && !(pos&EQP_HEAD_TOP))
			sd->status.head_mid = id->look;
		else
			sd->status.head_mid = 0;
		clif->changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
	}
	if (!map_no_view(sd->bl.m,EQP_COSTUME_HEAD_TOP) && pos & EQP_COSTUME_HEAD_TOP) {
		if (id){
			sd->status.head_top = id->look;
		} else
			sd->status.head_top = 0;
		clif->changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
	}
	if (!map_no_view(sd->bl.m,EQP_COSTUME_HEAD_MID) && pos & EQP_COSTUME_HEAD_MID) {
		if(id && !(pos&EQP_HEAD_TOP)){
			sd->status.head_mid = id->look;
		} else
			sd->status.head_mid = 0;
		clif->changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
	}
	if (!map_no_view(sd->bl.m,EQP_COSTUME_HEAD_LOW) && pos & EQP_COSTUME_HEAD_LOW) {
		if (id && !(pos&(EQP_HEAD_TOP|EQP_HEAD_MID))){
			sd->status.head_bottom = id->look;
		} else
			sd->status.head_bottom = 0;
		clif->changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
	}

	if (!map_no_view(sd->bl.m,EQP_SHOES) && pos & EQP_SHOES)
		clif->changelook(&sd->bl,LOOK_SHOES,0);
	if (!map_no_view(sd->bl.m,EQP_GARMENT) && pos&EQP_GARMENT && pc->checkequip(sd,EQP_COSTUME_GARMENT) == -1) {
		sd->status.robe = id ? id->look : 0;
		clif->changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
	}

	if (!map_no_view(sd->bl.m,EQP_COSTUME_GARMENT) && pos & EQP_COSTUME_GARMENT) {
		sd->status.robe = id ? id->look : 0;
		clif->changelook(&sd->bl,LOOK_ROBE,sd->status.robe);
	}
}

/*==========================================
 * Equip item on player sd at req_pos from inventory index n
 * Return:
 *   0 = fail
 *   1 = success
 *------------------------------------------*/
int pc_equipitem(struct map_session_data *sd,int n,int req_pos)
{
	int i,pos,flag=0,iflag;
	struct item_data *id;

	nullpo_ret(sd);

	if( n < 0 || n >= MAX_INVENTORY ) {
		clif->equipitemack(sd,0,0,EIA_FAIL);
		return 0;
	}

	if( DIFF_TICK(sd->canequip_tick,timer->gettick()) > 0 )
	{
		clif->equipitemack(sd,n,0,EIA_FAIL);
		return 0;
	}

	id = sd->inventory_data[n];
	pos = pc->equippoint(sd,n); //With a few exceptions, item should go in all specified slots.

	if(battle_config.battle_log)
		ShowInfo("equip %d(%d) %x:%x\n", sd->status.inventory[n].nameid, n, (unsigned int)(id ? id->equip : 0), (unsigned int)req_pos);
	if(!pc->isequip(sd,n) || !(pos&req_pos) || sd->status.inventory[n].equip != 0 || (sd->status.inventory[n].attribute & ATTR_BROKEN) != 0 ) { // [Valaris]
		// FIXME: pc->isequip: equip level failure uses 2 instead of 0
		clif->equipitemack(sd,n,0,EIA_FAIL); // fail
		return 0;
	}

	if (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_NO_SWITCH_EQUIP])
	{
		clif->equipitemack(sd,n,0,EIA_FAIL); // fail
		return 0;
	}

	/* won't fail from this point onwards */
	if( id->flag.bindonequip && !sd->status.inventory[n].bound ) {
		sd->status.inventory[n].bound = (unsigned char)IBT_CHARACTER;
		clif->notify_bounditem(sd,n);
	}

	if(pos == EQP_ACC) { //Accesories should only go in one of the two,
		pos = req_pos&EQP_ACC;
		if (pos == EQP_ACC) //User specified both slots..
			pos = sd->equip_index[EQI_ACC_R] >= 0 ? EQP_ACC_L : EQP_ACC_R;
	} else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
		pos = (req_pos&EQP_ARMS);
		if (pos == EQP_ARMS) //User specified both slots, pick one for them.
			pos = sd->equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
	} else if(pos == EQP_SHADOW_ACC) { //Accesories should only go in one of the two,
		pos = req_pos&EQP_SHADOW_ACC;
		if (pos == EQP_SHADOW_ACC) //User specified both slots..
			pos = sd->equip_index[EQI_SHADOW_ACC_R] >= 0 ? EQP_SHADOW_ACC_L : EQP_SHADOW_ACC_R;
	} else if( pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) { //Dual wield capable weapon.
		pos = (req_pos&EQP_SHADOW_ARMS);
		if (pos == EQP_SHADOW_ARMS) //User specified both slots, pick one for them.
			pos = sd->equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON;
	}

	if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
		//Update skill-block range database when weapon range changes. [Skotlex]
		i = sd->equip_index[EQI_HAND_R];
		if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
			flag = 1;
		else
			flag = id->range != sd->inventory_data[i]->range;
	}

	for(i=0;i<EQI_MAX;i++) {
		if(pos & pc->equip_pos[i]) {
			if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
				pc->unequipitem(sd, sd->equip_index[i], PCUNEQUIPITEM_FORCE);

			sd->equip_index[i] = n;
		}
	}

	if(pos==EQP_AMMO){
		clif->arrowequip(sd,n);
		clif->arrow_fail(sd,3);
	}
	else
		clif->equipitemack(sd,n,pos,EIA_SUCCESS);

	sd->status.inventory[n].equip=pos;

	pc->equipitem_pos(sd, id, n, pos);

	pc->checkallowskill(sd); //Check if status changes should be halted.
	iflag = sd->npc_item_flag;

	/* check for combos (MUST be before status_calc_pc) */
	if( id->combos_count )
		pc->checkcombo(sd,id);
	if(itemdb_isspecial(sd->status.inventory[n].card[0]))
		; //No cards
	else {
		for( i = 0; i < id->slot; i++ ) {
			struct item_data *data;
			if (!sd->status.inventory[n].card[i])
				continue;
			if ( ( data = itemdb->exists(sd->status.inventory[n].card[i]) ) != NULL ) {
				if( data->combos_count )
					pc->checkcombo(sd,data);
			}
		}
	}

	status_calc_pc(sd,SCO_NONE);
	if (flag) //Update skill data
		clif->skillinfoblock(sd);

	//OnEquip script [Skotlex]
	if (id->equip_script)
		script->run_item_equip_script(sd, id, npc->fake_nd->bl.id);

	if(itemdb_isspecial(sd->status.inventory[n].card[0]))
		; //No cards
	else {
		for( i = 0; i < id->slot; i++ ) {
			struct item_data *data;
			if (!sd->status.inventory[n].card[i])
				continue;
			if ( ( data = itemdb->exists(sd->status.inventory[n].card[i]) ) != NULL ) {
				if (data->equip_script)
					script->run_item_equip_script(sd, data, npc->fake_nd->bl.id);
			}
		}
	}
	sd->npc_item_flag = iflag;

	return 1;
}

/**
* Unrquip item ad given position.
* @param sd the affected player structure. Must be checked before.
* @param n inventory item position. Must be checked before.
* @param pos slot position. Must be checked before.
**/
void pc_unequipitem_pos(struct map_session_data *sd, int n, int pos)
{
	nullpo_retv(sd);
	if (pos & EQP_HAND_R) {
		sd->weapontype1 = 0;
		sd->status.weapon = sd->weapontype2;
		pc->calcweapontype(sd);
		clif->changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
		if (!battle_config.dancing_weaponswitch_fix)
			status_change_end(&sd->bl, SC_DANCING, INVALID_TIMER); // Unequipping => stop dancing.
	}
	if (pos & EQP_HAND_L) {
		sd->status.shield = sd->weapontype2 = 0;
		pc->calcweapontype(sd);
		clif->changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
	}
	if (pos & EQP_HEAD_LOW && pc->checkequip(sd,EQP_COSTUME_HEAD_LOW) == -1) {
		sd->status.head_bottom = 0;
		clif->changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
	}
	if (pos & EQP_HEAD_TOP && pc->checkequip(sd,EQP_COSTUME_HEAD_TOP) == -1) {
		sd->status.head_top = 0;
		clif->changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
	}
	if (pos & EQP_HEAD_MID && pc->checkequip(sd,EQP_COSTUME_HEAD_MID) == -1) {
		sd->status.head_mid = 0;
		clif->changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
	}

	if (pos & EQP_COSTUME_HEAD_TOP) {
		sd->status.head_top = ( pc->checkequip(sd,EQP_HEAD_TOP) >= 0 ) ? sd->inventory_data[pc->checkequip(sd,EQP_HEAD_TOP)]->look : 0;
		clif->changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
	}

	if (pos & EQP_COSTUME_HEAD_MID) {
		sd->status.head_mid = ( pc->checkequip(sd,EQP_HEAD_MID) >= 0 ) ? sd->inventory_data[pc->checkequip(sd,EQP_HEAD_MID)]->look : 0;
		clif->changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
	}

	if (pos & EQP_COSTUME_HEAD_LOW) {
		sd->status.head_bottom = ( pc->checkequip(sd,EQP_HEAD_LOW) >= 0 ) ? sd->inventory_data[pc->checkequip(sd,EQP_HEAD_LOW)]->look : 0;
		clif->changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
	}

	if (pos & EQP_SHOES)
		clif->changelook(&sd->bl,LOOK_SHOES,0);

	if (pos & EQP_GARMENT && pc->checkequip(sd,EQP_COSTUME_GARMENT) == -1) {
		sd->status.robe = 0;
		clif->changelook(&sd->bl, LOOK_ROBE, 0);
	}

	if (pos & EQP_COSTUME_GARMENT) {
		sd->status.robe = ( pc->checkequip(sd,EQP_GARMENT) >= 0 ) ? sd->inventory_data[pc->checkequip(sd,EQP_GARMENT)]->look : 0;
		clif->changelook(&sd->bl,LOOK_ROBE,sd->status.robe);
	}
}

/*==========================================
 * Called when attemting to unequip an item from player
 * type: @see enum pc_unequipitem_flag
 * Return:
 *   0 = fail
 *   1 = success
 *------------------------------------------*/
int pc_unequipitem(struct map_session_data *sd,int n,int flag)
{
	int i,iflag;
	bool status_cacl = false;
	int pos;
	nullpo_ret(sd);

	if( n < 0 || n >= MAX_INVENTORY ) {
		clif->unequipitemack(sd,0,0,UIA_FAIL);
		return 0;
	}

	// if player is berserk then cannot unequip
	if (!(flag&PCUNEQUIPITEM_FORCE) && sd->sc.count && (sd->sc.data[SC_BERSERK] || sd->sc.data[SC_NO_SWITCH_EQUIP]) )
	{
		clif->unequipitemack(sd,n,0,UIA_FAIL);
		return 0;
	}

	if( !(flag&PCUNEQUIPITEM_FORCE) && sd->sc.count && sd->sc.data[SC_KYOUGAKU] )
	{
		clif->unequipitemack(sd,n,0,UIA_FAIL);
		return 0;
	}

	if(battle_config.battle_log)
		ShowInfo("unequip %d %x:%x\n", n, (unsigned int)(pc->equippoint(sd, n)), sd->status.inventory[n].equip);

	if(!sd->status.inventory[n].equip){ //Nothing to unequip
		clif->unequipitemack(sd,n,0,UIA_FAIL);
		return 0;
	}
	for(i=0;i<EQI_MAX;i++) {
		if(sd->status.inventory[n].equip & pc->equip_pos[i])
			sd->equip_index[i] = -1;
	}

	pos = sd->status.inventory[n].equip;
	pc->unequipitem_pos(sd, n, pos);

	clif->unequipitemack(sd,n,pos,UIA_SUCCESS);

	if((pos & EQP_ARMS) &&
		sd->weapontype1 == 0 && sd->weapontype2 == 0 && (!sd->sc.data[SC_TK_SEVENWIND] || sd->sc.data[SC_ASPERSIO])) //Check for seven wind (but not level seven!)
		skill->enchant_elemental_end(&sd->bl,-1);

	if(pos & EQP_ARMOR) {
		// On Armor Change...
		status_change_end(&sd->bl, SC_BENEDICTIO, INVALID_TIMER);
		status_change_end(&sd->bl, SC_ARMOR_RESIST, INVALID_TIMER);
	}

	if( sd->state.autobonus&pos )
		sd->state.autobonus &= ~sd->status.inventory[n].equip; //Check for activated autobonus [Inkfish]

	sd->status.inventory[n].equip=0;
	iflag = sd->npc_item_flag;

	/* check for combos (MUST be before status_calc_pc) */
	if ( sd->inventory_data[n] ) {
		if( sd->inventory_data[n]->combos_count ) {
			if( pc->removecombo(sd,sd->inventory_data[n]) )
				status_cacl = true;
		} if(itemdb_isspecial(sd->status.inventory[n].card[0]))
			; //No cards
		else {
			for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
				struct item_data *data;
				if (!sd->status.inventory[n].card[i])
					continue;
				if ( ( data = itemdb->exists(sd->status.inventory[n].card[i]) ) != NULL ) {
					if( data->combos_count ) {
						if( pc->removecombo(sd,data) )
							status_cacl = true;
					}
				}
			}
		}
	}

	if(flag&PCUNEQUIPITEM_RECALC || status_cacl) {
		pc->checkallowskill(sd);
		status_calc_pc(sd,SCO_NONE);
	}

	if(sd->sc.data[SC_CRUCIS] && !battle->check_undead(sd->battle_status.race,sd->battle_status.def_ele))
		status_change_end(&sd->bl, SC_CRUCIS, INVALID_TIMER);

	//OnUnEquip script [Skotlex]
	if (sd->inventory_data[n]) {
		if (sd->inventory_data[n]->unequip_script) {
			if ( battle_config.unequip_restricted_equipment & 1 ) {
				ARR_FIND(0, map->list[sd->bl.m].zone->disabled_items_count, i,  map->list[sd->bl.m].zone->disabled_items[i] == sd->status.inventory[n].nameid);
				if ( i == map->list[sd->bl.m].zone->disabled_items_count )
					script->run_item_unequip_script(sd, sd->inventory_data[n], npc->fake_nd->bl.id);
			}
			else
				script->run_item_unequip_script(sd, sd->inventory_data[n], npc->fake_nd->bl.id);
		}
		if(itemdb_isspecial(sd->status.inventory[n].card[0]))
			; //No cards
		else {
			for( i = 0; i < sd->inventory_data[n]->slot; i++ ) {
				struct item_data *data;
				if (!sd->status.inventory[n].card[i])
					continue;

				if ( ( data = itemdb->exists(sd->status.inventory[n].card[i]) ) != NULL ) {
					if ( data->unequip_script ) {
						if ( battle_config.unequip_restricted_equipment & 2 ) {
							int j;
							ARR_FIND(0, map->list[sd->bl.m].zone->disabled_items_count, j,  map->list[sd->bl.m].zone->disabled_items[j] == sd->status.inventory[n].card[i]);
							if ( j == map->list[sd->bl.m].zone->disabled_items_count )
								script->run_item_unequip_script(sd, data, npc->fake_nd->bl.id);
						}
						else
							script->run_item_unequip_script(sd, data, npc->fake_nd->bl.id);
					}
				}

			}
		}
	}
	sd->npc_item_flag = iflag;

	return 1;
}

/*==========================================
 * Checking if player (sd) have unauthorize, invalide item
 * on inventory, cart, equiped for the map (item_noequip)
 *------------------------------------------*/
int pc_checkitem(struct map_session_data *sd)
{
	int i, calc_flag = 0;

	nullpo_ret(sd);

	if (sd->state.vending) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
		return 0;

	if (sd->state.itemcheck) { // check for invalid(ated) items
		int id;
		for (i = 0; i < MAX_INVENTORY; i++) {
			id = sd->status.inventory[i].nameid;

			if (!id)
				continue;

			if (!itemdb_available(id)) {
				ShowWarning("Removed invalid/disabled item id %d from inventory (amount=%d, char_id=%d).\n", id, sd->status.inventory[i].amount, sd->status.char_id);
				pc->delitem(sd, i, sd->status.inventory[i].amount, 0, DELITEM_NORMAL, LOG_TYPE_INV_INVALID);
				continue;
			}

			if (!sd->status.inventory[i].unique_id && !itemdb->isstackable(id))
				sd->status.inventory[i].unique_id = itemdb->unique_id(sd);
		}

		for( i = 0; i < MAX_CART; i++ ) {
			id = sd->status.cart[i].nameid;

			if (!id)
				continue;

			if( !itemdb_available(id) ) {
				ShowWarning("Removed invalid/disabled item id %d from cart (amount=%d, char_id=%d).\n", id, sd->status.cart[i].amount, sd->status.char_id);
				pc->cart_delitem(sd, i, sd->status.cart[i].amount, 0, LOG_TYPE_CART_INVALID);
				continue;
			}

			if ( !sd->status.cart[i].unique_id && !itemdb->isstackable(id) )
				sd->status.cart[i].unique_id = itemdb->unique_id(sd);
		}

		for( i = 0; i < MAX_STORAGE; i++ ) {
			id = sd->status.storage.items[i].nameid;

			if (!id)
				continue;

			if( id && !itemdb_available(id) ) {
				ShowWarning("Removed invalid/disabled item id %d from storage (amount=%d, char_id=%d).\n", id, sd->status.storage.items[i].amount, sd->status.char_id);
				storage->delitem(sd, i, sd->status.storage.items[i].amount);
				storage->close(sd);
				continue;
			}

			if ( !sd->status.storage.items[i].unique_id && !itemdb->isstackable(id) )
				sd->status.storage.items[i].unique_id = itemdb->unique_id(sd);
		}

		if (sd->guild) {
			struct guild_storage *guild_storage = idb_get(gstorage->db,sd->guild->guild_id);
			if (guild_storage) {
				for( i = 0; i < MAX_GUILD_STORAGE; i++ ) {
					id = guild_storage->items[i].nameid;

					if (!id)
						continue;

					if( !itemdb_available(id) ) {
						ShowWarning("Removed invalid/disabled item id %d from guild storage (amount=%d, char_id=%d, guild_id=%d).\n", id, guild_storage->items[i].amount, sd->status.char_id, sd->guild->guild_id);
						gstorage->delitem(sd, guild_storage, i, guild_storage->items[i].amount);
						gstorage->close(sd); // force closing
						continue;
					}

					if (!guild_storage->items[i].unique_id && !itemdb->isstackable(id))
						guild_storage->items[i].unique_id = itemdb->unique_id(sd);
				}
			}
		}
		sd->state.itemcheck = 0;
	}

	for( i = 0; i < MAX_INVENTORY; i++) {

		if( sd->status.inventory[i].nameid == 0 )
			continue;

		if( !sd->status.inventory[i].equip )
			continue;

		if( sd->status.inventory[i].equip&~pc->equippoint(sd,i) ) {
			pc->unequipitem(sd, i, PCUNEQUIPITEM_FORCE);
			calc_flag = 1;
			continue;
		}

		if (battle_config.unequip_restricted_equipment&1) {
			int j;
			for (j = 0; j < map->list[sd->bl.m].zone->disabled_items_count; j++) {
				if (map->list[sd->bl.m].zone->disabled_items[j] == sd->status.inventory[i].nameid) {
					pc->unequipitem(sd, i, PCUNEQUIPITEM_FORCE);
					calc_flag = 1;
				}
			}
		}

		if (battle_config.unequip_restricted_equipment&2) {
			if (!itemdb_isspecial(sd->status.inventory[i].card[0])) {
				int j, slot;
				for (slot = 0; slot < MAX_SLOTS; slot++) {
					for (j = 0; j < map->list[sd->bl.m].zone->disabled_items_count; j++) {
						if (map->list[sd->bl.m].zone->disabled_items[j] == sd->status.inventory[i].card[slot]) {
							pc->unequipitem(sd, i, PCUNEQUIPITEM_FORCE);
							calc_flag = 1;
						}
					}
				}
			}
		}

	}

	if( calc_flag && sd->state.active ) {
		pc->checkallowskill(sd);
		status_calc_pc(sd,SCO_NONE);
	}

	return 0;
}

/*==========================================
 * Update PVP rank for sd1 in cmp to sd2
 *------------------------------------------*/
int pc_calc_pvprank_sub(struct block_list *bl, va_list ap)
{
	struct map_session_data *sd1 = NULL;
	struct map_session_data *sd2 = va_arg(ap,struct map_session_data *);

	nullpo_ret(bl);
	Assert_ret(bl->type == BL_PC);
	sd1 = BL_UCAST(BL_PC, bl);
	nullpo_ret(sd2);

	if (pc_isinvisible(sd1) ||pc_isinvisible(sd2)) {
		// cannot register pvp rank for hidden GMs
		return 0;
	}

	if( sd1->pvp_point > sd2->pvp_point )
		sd2->pvp_rank++;
	return 0;
}
/*==========================================
 * Calculate new rank beetween all present players (map->foreachinarea)
 * and display result
 *------------------------------------------*/
int pc_calc_pvprank(struct map_session_data *sd) {
	int old;
	struct map_data *m;
	nullpo_ret(sd);
	m=&map->list[sd->bl.m];
	old=sd->pvp_rank;
	sd->pvp_rank=1;
	map->foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
	if(old!=sd->pvp_rank || sd->pvp_lastusers!=m->users_pvp)
		clif->pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=m->users_pvp,0);
	return sd->pvp_rank;
}
/*==========================================
 * Calculate next sd ranking calculation from config
 *------------------------------------------*/
int pc_calc_pvprank_timer(int tid, int64 tick, int id, intptr_t data) {
	struct map_session_data *sd;

	sd=map->id2sd(id);
	if(sd==NULL)
		return 0;
	sd->pvp_timer = INVALID_TIMER;

	if (pc_isinvisible(sd)) {
		// do not calculate the pvp rank for a hidden GM
		return 0;
	}

	if( pc->calc_pvprank(sd) > 0 )
		sd->pvp_timer = timer->add(timer->gettick()+PVP_CALCRANK_INTERVAL,pc->calc_pvprank_timer,id,data);
	return 0;
}

/*==========================================
 * Checking if sd is married
 * Return:
 *   partner_id = yes
 *   0 = no
 *------------------------------------------*/
int pc_ismarried(struct map_session_data *sd)
{
	if(sd == NULL)
		return -1;
	if(sd->status.partner_id > 0)
		return sd->status.partner_id;
	else
		return 0;
}
/*==========================================
 * Marry player sd to player dstsd
 * Return:
 *   -1 = fail
 *   0 = success
 *------------------------------------------*/
int pc_marriage(struct map_session_data *sd,struct map_session_data *dstsd)
{
	if(sd == NULL || dstsd == NULL ||
		sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
		(sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
		return -1;
	sd->status.partner_id = dstsd->status.char_id;
	dstsd->status.partner_id = sd->status.char_id;
	return 0;
}

/*==========================================
 * Divorce sd from its partner
 * Return:
 *   -1 = fail
 *   0 = success
 *------------------------------------------*/
int pc_divorce(struct map_session_data *sd)
{
	struct map_session_data *p_sd;
	int i;

	if( sd == NULL || !pc->ismarried(sd) )
		return -1;

	if( !sd->status.partner_id )
		return -1; // Char is not married

	if( (p_sd = map->charid2sd(sd->status.partner_id)) == NULL ) {
		// Lets char server do the divorce
		if( chrif->divorce(sd->status.char_id, sd->status.partner_id) )
			return -1; // No char server connected

		return 0;
	}

	// Both players online, lets do the divorce manually
	sd->status.partner_id = 0;
	p_sd->status.partner_id = 0;
	for( i = 0; i < MAX_INVENTORY; i++ )
	{
		if( sd->status.inventory[i].nameid == WEDDING_RING_M || sd->status.inventory[i].nameid == WEDDING_RING_F )
			pc->delitem(sd, i, 1, 0, DELITEM_NORMAL, LOG_TYPE_DIVORCE);
		if( p_sd->status.inventory[i].nameid == WEDDING_RING_M || p_sd->status.inventory[i].nameid == WEDDING_RING_F )
			pc->delitem(p_sd, i, 1, 0, DELITEM_NORMAL, LOG_TYPE_DIVORCE);
	}

	clif->divorced(sd, p_sd->status.name);
	clif->divorced(p_sd, sd->status.name);

	return 0;
}

/*==========================================
 * Get sd partner charid. (Married partner)
 *------------------------------------------*/
struct map_session_data *pc_get_partner(struct map_session_data *sd) {
	if (sd && pc->ismarried(sd))
		// charid2sd returns NULL if not found
		return map->charid2sd(sd->status.partner_id);

	return NULL;
}

/*==========================================
 * Get sd father charid. (Need to be baby)
 *------------------------------------------*/
struct map_session_data *pc_get_father(struct map_session_data *sd) {
	if (sd && sd->class_&JOBL_BABY && sd->status.father > 0)
		// charid2sd returns NULL if not found
		return map->charid2sd(sd->status.father);

	return NULL;
}

/*==========================================
 * Get sd mother charid. (Need to be baby)
 *------------------------------------------*/
struct map_session_data *pc_get_mother(struct map_session_data *sd) {
	if (sd && sd->class_&JOBL_BABY && sd->status.mother > 0)
		// charid2sd returns NULL if not found
		return map->charid2sd(sd->status.mother);

	return NULL;
}

/*==========================================
 * Get sd children charid. (Need to be married)
 *------------------------------------------*/
struct map_session_data *pc_get_child(struct map_session_data *sd) {
	if (sd && pc->ismarried(sd) && sd->status.child > 0)
		// charid2sd returns NULL if not found
		return map->charid2sd(sd->status.child);

	return NULL;
}

/*==========================================
 * Set player sd to bleed. (losing hp and/or sp each diff_tick)
 *------------------------------------------*/
void pc_bleeding (struct map_session_data *sd, unsigned int diff_tick)
{
	int hp = 0, sp = 0;

	nullpo_retv(sd);
	if( pc_isdead(sd) )
		return;

	if (sd->hp_loss.value) {
		sd->hp_loss.tick += diff_tick;
		while (sd->hp_loss.tick >= sd->hp_loss.rate) {
			hp += sd->hp_loss.value;
			sd->hp_loss.tick -= sd->hp_loss.rate;
		}
		if(hp >= sd->battle_status.hp)
			hp = sd->battle_status.hp-1; //Script drains cannot kill you.
	}

	if (sd->sp_loss.value) {
		sd->sp_loss.tick += diff_tick;
		while (sd->sp_loss.tick >= sd->sp_loss.rate) {
			sp += sd->sp_loss.value;
			sd->sp_loss.tick -= sd->sp_loss.rate;
		}
	}

	if (hp > 0 || sp > 0)
		status_zap(&sd->bl, hp, sp);

	return;
}

//Character regen. Flag is used to know which types of regen can take place.
//&1: HP regen
//&2: SP regen
void pc_regen (struct map_session_data *sd, unsigned int diff_tick) {
	int hp = 0, sp = 0;

	nullpo_retv(sd);
	if (sd->hp_regen.value) {
		sd->hp_regen.tick += diff_tick;
		while (sd->hp_regen.tick >= sd->hp_regen.rate) {
			hp += sd->hp_regen.value;
			sd->hp_regen.tick -= sd->hp_regen.rate;
		}
	}

	if (sd->sp_regen.value) {
		sd->sp_regen.tick += diff_tick;
		while (sd->sp_regen.tick >= sd->sp_regen.rate) {
			sp += sd->sp_regen.value;
			sd->sp_regen.tick -= sd->sp_regen.rate;
		}
	}

	if (hp > 0 || sp > 0)
		status->heal(&sd->bl, hp, sp, 0);

	return;
}

/*==========================================
 * Memo player sd savepoint. (map,x,y)
 *------------------------------------------*/
int pc_setsavepoint(struct map_session_data *sd, short map_index, int x, int y) {
	nullpo_ret(sd);

	sd->status.save_point.map = map_index;
	sd->status.save_point.x = x;
	sd->status.save_point.y = y;

	return 0;
}

/*==========================================
 * Save 1 player data at autosave intervall
 *------------------------------------------*/
int pc_autosave(int tid, int64 tick, int id, intptr_t data) {
	int interval;
	struct s_mapiterator* iter;
	struct map_session_data* sd;
	static int last_save_id = 0, save_flag = 0;

	if(save_flag == 2) //Someone was saved on last call, normal cycle
		save_flag = 0;
	else
		save_flag = 1; //Noone was saved, so save first found char.

	iter = mapit_getallusers();
	for (sd = BL_UCAST(BL_PC, mapit->first(iter)); mapit->exists(iter); sd = BL_UCAST(BL_PC, mapit->next(iter))) {
		if(sd->bl.id == last_save_id && save_flag != 1) {
			save_flag = 1;
			continue;
		}

		if(save_flag != 1) //Not our turn to save yet.
			continue;

		//Save char.
		last_save_id = sd->bl.id;
		save_flag = 2;

		chrif->save(sd,0);
		break;
	}
	mapit->free(iter);

	interval = map->autosave_interval/(map->usercount()+1);
	if(interval < map->minsave_interval)
		interval = map->minsave_interval;
	timer->add(timer->gettick()+interval,pc->autosave,0,0);

	return 0;
}

int pc_daynight_timer_sub(struct map_session_data *sd,va_list ap) {
	nullpo_ret(sd);
	if (sd->state.night != map->night_flag && map->list[sd->bl.m].flag.nightenabled) { //Night/day state does not match.
		clif->status_change(&sd->bl, SI_SKE, map->night_flag, 0, 0, 0, 0); //New night effect by dynamix [Skotlex]
		sd->state.night = map->night_flag;
		return 1;
	}
	return 0;
}
/*================================================
 * timer to do the day [Yor]
 * data: 0 = called by timer, 1 = gmcommand/script
 *------------------------------------------------*/
int map_day_timer(int tid, int64 tick, int id, intptr_t data) {
	char tmp_soutput[1024];

	if (data == 0 && battle_config.day_duration <= 0) // if we want a day
		return 0;

	if (!map->night_flag)
		return 0; //Already day.

	map->night_flag = 0; // 0=day, 1=night [Yor]
	map->foreachpc(pc->daynight_timer_sub);
	safestrncpy(tmp_soutput, (data == 0) ? msg_txt(502) : msg_txt(60), sizeof(tmp_soutput)); // The day has arrived!
	intif->broadcast(tmp_soutput, (int)strlen(tmp_soutput) + 1, BC_DEFAULT);
	return 0;
}

/*================================================
 * timer to do the night [Yor]
 * data: 0 = called by timer, 1 = gmcommand/script
 *------------------------------------------------*/
int map_night_timer(int tid, int64 tick, int id, intptr_t data) {
	char tmp_soutput[1024];

	if (data == 0 && battle_config.night_duration <= 0) // if we want a night
		return 0;

	if (map->night_flag)
		return 0; //Already nigth.

	map->night_flag = 1; // 0=day, 1=night [Yor]
	map->foreachpc(pc->daynight_timer_sub);
	safestrncpy(tmp_soutput, (data == 0) ? msg_txt(503) : msg_txt(59), sizeof(tmp_soutput)); // The night has fallen...
	intif->broadcast(tmp_soutput, (int)strlen(tmp_soutput) + 1, BC_DEFAULT);
	return 0;
}

void pc_setstand(struct map_session_data *sd) {
	nullpo_retv(sd);

	status_change_end(&sd->bl, SC_TENSIONRELAX, INVALID_TIMER);
	clif->sc_end(&sd->bl,sd->bl.id,SELF,SI_SIT);
	//Reset sitting tick.
	sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
	sd->state.dead_sit = sd->vd.dead_sit = 0;
}

/**
 * Mechanic (MADO GEAR)
 **/
void pc_overheat(struct map_session_data *sd, int val) {
	int heat = val, skill_lv,
		limit[] = { 10, 20, 28, 46, 66 };

	nullpo_retv(sd);
	if( !pc_ismadogear(sd) || sd->sc.data[SC_OVERHEAT] )
		return; // already burning

	skill_lv = cap_value(pc->checkskill(sd,NC_MAINFRAME),0,4);
	if( sd->sc.data[SC_OVERHEAT_LIMITPOINT] ) {
		heat += sd->sc.data[SC_OVERHEAT_LIMITPOINT]->val1;
		status_change_end(&sd->bl,SC_OVERHEAT_LIMITPOINT,INVALID_TIMER);
	}

	heat = max(0,heat); // Avoid negative HEAT
	if( heat >= limit[skill_lv] )
		sc_start(NULL,&sd->bl,SC_OVERHEAT,100,0,1000);
	else
		sc_start(NULL,&sd->bl,SC_OVERHEAT_LIMITPOINT,100,heat,30000);

	return;
}

/**
 * Check if player is autolooting given itemID.
 */
bool pc_isautolooting(struct map_session_data *sd, int nameid)
{
	int i = 0;

	nullpo_ret(sd);
	if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
		return true;

	if (!sd->state.autolooting)
		return false;

	ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);

	return (i != AUTOLOOTITEM_SIZE);
}

/**
 * Checks if player can use @/#command
 * @param sd Player map session data
 * @param command Command name with @/# and without params
 */
bool pc_can_use_command(struct map_session_data *sd, const char *command) {
	return atcommand->can_use(sd,command);
}

/**
 * Spirit Charm expiration timer.
 *
 * @see TimerFunc
 */
int pc_charm_timer(int tid, int64 tick, int id, intptr_t data)
{
	struct map_session_data *sd = map->id2sd(id);
	int i;

	if (sd == NULL)
		return 1;

	if (sd->charm_count <= 0) {
		ShowError("pc_charm_timer: %d spiritcharm's available. (aid=%d cid=%d tid=%d)\n", sd->charm_count, sd->status.account_id, sd->status.char_id, tid);
		sd->charm_count = 0;
		sd->charm_type = CHARM_TYPE_NONE;
		return 0;
	}

	ARR_FIND(0, sd->charm_count, i, sd->charm_timer[i] == tid);
	if (i == sd->charm_count) {
		ShowError("pc_charm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
		return 0;
	}

	sd->charm_count--;
	if(i != sd->charm_count)
		memmove(sd->charm_timer+i, sd->charm_timer+i+1, (sd->charm_count-i)*sizeof(int));
	sd->charm_timer[sd->charm_count] = INVALID_TIMER;
	if (sd->charm_count <= 0)
		sd->charm_type = CHARM_TYPE_NONE;

	clif->spiritcharm(sd);

	return 0;
}

/**
 * Adds a spirit charm.
 *
 * @param sd       Target character.
 * @param interval Duration.
 * @param max      Maximum amount of charms to add.
 * @param type     Charm type (@see spirit_charm_types)
 */
void pc_add_charm(struct map_session_data *sd, int interval, int max, int type)
{
	int tid, i;

	nullpo_retv(sd);

	if (sd->charm_type != CHARM_TYPE_NONE && type != sd->charm_type) {
		pc->del_charm(sd, sd->charm_count, sd->charm_type);
	}

	if (max > MAX_SPIRITCHARM)
		max = MAX_SPIRITCHARM;
	if (sd->charm_count < 0)
		sd->charm_count = 0;

	if (sd->charm_count && sd->charm_count >= max) {
		if (sd->charm_timer[0] != INVALID_TIMER)
			timer->delete(sd->charm_timer[0],pc->charm_timer);
		sd->charm_count--;
		if (sd->charm_count != 0)
			memmove(sd->charm_timer+0, sd->charm_timer+1, sd->charm_count*sizeof(int));
		sd->charm_timer[sd->charm_count] = INVALID_TIMER;
	}

	tid = timer->add(timer->gettick()+interval, pc->charm_timer, sd->bl.id, 0);
	ARR_FIND(0, sd->charm_count, i, sd->charm_timer[i] == INVALID_TIMER || DIFF_TICK(timer->get(tid)->tick, timer->get(sd->charm_timer[i])->tick) < 0);
	if (i != sd->charm_count)
		memmove(sd->charm_timer+i+1, sd->charm_timer+i, (sd->charm_count-i)*sizeof(int));
	sd->charm_timer[i] = tid;
	sd->charm_count++;
	sd->charm_type = type;

	clif->spiritcharm(sd);
}

/**
 * Removes one or more spirit charms.
 *
 * @param sd    The target character.
 * @param count Amount of charms to remove.
 * @param type  Type of charm to remove.
 */
void pc_del_charm(struct map_session_data *sd, int count, int type)
{
	int i;

	nullpo_retv(sd);

	if (sd->charm_type != type)
		return;

	if (sd->charm_count <= 0) {
		sd->charm_count = 0;
		return;
	}

	if (count <= 0)
		return;
	if (count > sd->charm_count)
		count = sd->charm_count;
	sd->charm_count -= count;
	if (count > MAX_SPIRITCHARM)
		count = MAX_SPIRITCHARM;

	for (i = 0; i < count; i++) {
		if(sd->charm_timer[i] != INVALID_TIMER) {
			timer->delete(sd->charm_timer[i],pc->charm_timer);
			sd->charm_timer[i] = INVALID_TIMER;
		}
	}
	for (i = count; i < MAX_SPIRITCHARM; i++) {
		sd->charm_timer[i-count] = sd->charm_timer[i];
		sd->charm_timer[i] = INVALID_TIMER;
	}
	if (sd->charm_count <= 0)
		sd->charm_type = CHARM_TYPE_NONE;

	clif->spiritcharm(sd);
}

/**
 * Renewal EXP/Itemdrop rate modifier base on level penalty.
 *
 * @param diff Level difference.
 * @param race Monster race.
 * @param mode Monster mode.
 * @param type Modifier type (1=exp 2=itemdrop)
 * @return The percent rate modifier (100 = 100%)
 */
int pc_level_penalty_mod(int diff, unsigned char race, uint32 mode, int type)
{
#if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
	int rate = 100, i;

	if( diff < 0 )
		diff = MAX_LEVEL + ( ~diff + 1 );

	for (i = RC_FORMLESS; i < RC_MAX; i++) {
		int tmp;

		if (race != i) {
			if (mode&MD_BOSS && i < RC_BOSS)
				i = RC_BOSS;
			else if (i <= RC_BOSS)
				continue;
		}

		if ((tmp=pc->level_penalty[type][i][diff]) > 0) {
			rate = tmp;
			break;
		}
	}

	return rate;
#else
	return 100;
#endif
}
int pc_split_str(char *str,char **val,int num)
{
	int i;

	nullpo_ret(val);
	for (i=0; i<num && str; i++){
		val[i] = str;
		str = strchr(str,',');
		if (str && i<num-1) //Do not remove a trailing comma.
			*str++=0;
	}
	return i;
}

int pc_split_atoi(char* str, int* val, char sep, int max)
{
	int i,j;
	nullpo_ret(val);
	for (i=0; i<max; i++) {
		if (!str) break;
		val[i] = atoi(str);
		str = strchr(str,sep);
		if (str)
			*str++=0;
	}
	//Zero up the remaining.
	for(j=i; j < max; j++)
		val[j] = 0;
	return i;
}

int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
{
	static int warning=0;
	int i,j;
	nullpo_ret(val);
	for (i=0; i<max; i++) {
		double f;
		if (!str) break;
		f = atof(str);
		if (f < 0)
			val[i] = 0;
		else if (f > UINT_MAX) {
			val[i] = UINT_MAX;
			if (!warning) {
				warning = 1;
				ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
			}
		} else
			val[i] = (unsigned int)f;
		str = strchr(str,sep);
		if (str)
			*str++=0;
	}
	//Zero up the remaining.
	for(j=i; j < max; j++)
		val[j] = 0;
	return i;
}

/**
 * Parses the skill tree config file.
 *
 * In order to reclaim the memory allocated by this function
 * `pc->clear_skill_tree()` should be used.
 *
 * @remark
 *   This function assumes that the skill tree is clear and zeroed.
 *   If it has been already loaded (ie reloading), it needs to be cleared
 *   before calling this function again.
 *
 * @author [Ind/Hercules]
 */
void pc_read_skill_tree(void)
{
	struct config_t skill_tree_conf;
	struct config_setting_t *skt = NULL;
	char config_filename[128];
	int i = 0;
	struct s_mapiterator *iter;
	struct map_session_data *sd;
	bool loaded[CLASS_COUNT] = { false };

	safesnprintf(config_filename, sizeof(config_filename), "%s/"DBPATH"skill_tree.conf", map->db_path);
	if (!libconfig->load_file(&skill_tree_conf, config_filename))
		return;

	// Foreach job
	while ((skt = libconfig->setting_get_elem(skill_tree_conf.root, i++))) {
		struct config_setting_t *t = NULL;
		int job_idx;
		const char *job_name = config_setting_name(skt);
		int job_id = pc->check_job_name(job_name);

		if (job_id == -1) {
			ShowWarning("pc_read_skill_tree: '%s' unknown job name!\n", job_name);
			continue;
		}
		job_idx = pc->class2idx(job_id);
		if (loaded[job_idx]) {
			ShowWarning("pc_read_skill_tree: Duplicate entry for job '%s'. Skipping.\n", job_name);
			continue;
		}
		loaded[job_idx] = true;

		if ((t = libconfig->setting_get_member(skt, "inherit")) != NULL) {
			int j = 0;
			const char *ijob_name = NULL;
			// Foreach inherited job
			while ((ijob_name = libconfig->setting_get_string_elem(t, j++)) != NULL) {
				int k, ijob_idx;
				int ijob_id = pc->check_job_name(ijob_name);

				if (ijob_id == -1) {
					ShowWarning("pc_read_skill_tree: '%s' trying to inherit unknown '%s'!\n", job_name, ijob_name);
					continue;
				}
				ijob_idx = pc->class2idx(ijob_id);
				if (ijob_idx == job_idx) {
					ShowWarning("pc_read_skill_tree: '%s' trying to inherit itself. Skipping.\n", job_name);
					continue;
				}
				if (!loaded[ijob_idx]) {
					ShowWarning("pc_read_skill_tree: '%s' trying to inherit not yet loaded '%s' (wrong order in the tree). Skipping.\n", job_name, ijob_name);
					continue;
				}

				for (k = 0; k < MAX_SKILL_TREE; k++) {
					int cur;
					struct skill_tree_entry *dst = NULL;
					const struct skill_tree_entry *src = &pc->skill_tree[ijob_idx][k];

					if (src->id == 0)
						break; // No more skills to copy

					ARR_FIND(0, MAX_SKILL_TREE, cur, pc->skill_tree[job_idx][cur].id == 0 || pc->skill_tree[job_idx][cur].id == src->id);
					if (cur == MAX_SKILL_TREE) {
						ShowWarning("pc_read_skill_tree: '%s' can't inherit '%s', skill tree is full!\n", job_name, ijob_name);
						break;
					}
					if (src->id == NV_TRICKDEAD && ((pc->jobid2mapid(job_id)&(MAPID_BASEMASK | JOBL_2)) != MAPID_NOVICE))
						continue; // skip trickdead for non-novices
					dst = &pc->skill_tree[job_idx][cur];
					dst->inherited = 1;
					if (dst->id == 0) {
						// Not existing yet, copy
						dst->id = src->id;
						dst->idx = src->idx;
						dst->max = src->max;
						dst->joblv = src->joblv;
						VECTOR_INIT(dst->need);
						if (VECTOR_LENGTH(src->need) > 0) {
							VECTOR_ENSURE(dst->need, VECTOR_LENGTH(src->need), 1);
							VECTOR_PUSHARRAY(dst->need, VECTOR_DATA(src->need), VECTOR_LENGTH(src->need));
						}
					} else {
						int l;
						// Already existing, merge
						if (src->max > dst->max)
							dst->max = src->max;
						dst->joblv = src->joblv;
						for (l = 0; l < VECTOR_LENGTH(src->need); l++) {
							int m;
							struct skill_tree_requirement *sreq = &VECTOR_INDEX(src->need, l);
							ARR_FIND(0, VECTOR_LENGTH(dst->need), m, VECTOR_INDEX(dst->need, m).id == sreq->id);
							if (m == VECTOR_LENGTH(dst->need)) {
								VECTOR_ENSURE(dst->need, 1, 1);
								VECTOR_PUSHCOPY(dst->need, sreq);
							} else {
								struct skill_tree_requirement *dreq = &VECTOR_INDEX(dst->need, m);
								dreq->lv = sreq->lv;
							}
						}
					}
				}
			}
		}
		if ((t = libconfig->setting_get_member(skt, "skills")) != NULL) {
			int j = 0;
			struct config_setting_t *sk = NULL;
			// Foreach skill
			while ((sk = libconfig->setting_get_elem(t, j++)) != NULL) {
				int skill_id, sk_idx;
				struct config_setting_t *rsk = NULL;
				const char *sk_name = config_setting_name(sk);
				struct skill_tree_entry *tree_entry = NULL;

				if ((skill_id = skill->name2id(sk_name)) == 0) {
					ShowWarning("pc_read_skill_tree: unknown skill '%s' in '%s'\n", sk_name, job_name);
					continue;
				}

				ARR_FIND(0, MAX_SKILL_TREE, sk_idx, pc->skill_tree[job_idx][sk_idx].id == 0 || pc->skill_tree[job_idx][sk_idx].id == skill_id);
				if (sk_idx == MAX_SKILL_TREE) {
					ShowWarning("pc_read_skill_tree: Unable to load skill %d (%s) into '%s's tree. Maximum number of skills per class has been reached.\n", skill_id, sk_name, job_name);
					continue;
				}
				tree_entry = &pc->skill_tree[job_idx][sk_idx];

				if (tree_entry->id != 0 && !tree_entry->inherited) {
					ShowNotice("pc_read_skill_tree: Duplicate %d for '%s' (%d). Skipping.\n", skill_id, job_name, job_id);
					continue;
				}
				if (config_setting_is_group(sk)) {
					int i32 = 0;
					if (libconfig->setting_lookup_int(sk, "MaxLevel", &i32) && i32 > 0) {
						tree_entry->max = (unsigned char)i32;
					} else {
						ShowWarning("pc_read_skill_tree: missing MaxLevel for skill %d (%s) class '%s'. Skipping.\n", skill_id, sk_name, job_name);
						continue;
					}
					if (libconfig->setting_lookup_int(sk, "MinJobLevel", &i32) && i32 > 0) {
						tree_entry->joblv = (unsigned char)i32;
					} else if (!tree_entry->inherited) {
						tree_entry->joblv = 0;
					}
				} else {
					tree_entry->max = (unsigned char)libconfig->setting_get_int(sk);
					if (!tree_entry->inherited)
						tree_entry->joblv = 0;
				}
				if (!tree_entry->inherited) {
					tree_entry->id  = skill_id;
					tree_entry->idx = skill->get_index(skill_id);
					VECTOR_INIT(tree_entry->need);
				}

				if (config_setting_is_group(sk)) {
					int k = 0;
					// Foreach requirement
					while ((rsk = libconfig->setting_get_elem(sk, k++)) != NULL) {
						const char *rsk_name = config_setting_name(rsk);
						int rsk_id = skill->name2id(rsk_name);
						struct skill_tree_requirement *req = NULL;
						int l;

						if (rsk_id == 0) {
							if (strcmp(rsk_name, "MaxLevel") != 0 && strcmp(rsk_name, "MinJobLevel") != 0)
								ShowWarning("pc_read_skill_tree: unknown requirement '%s' for '%s' in '%s'\n", rsk_name, sk_name, job_name);
							continue;
						}
						ARR_FIND(0, VECTOR_LENGTH(tree_entry->need), l, VECTOR_INDEX(tree_entry->need, l).id == rsk_id);
						if (l == VECTOR_LENGTH(tree_entry->need)) {
							VECTOR_ENSURE(tree_entry->need, 1, 1);
							VECTOR_PUSHZEROED(tree_entry->need);
							req = &VECTOR_LAST(tree_entry->need);
							req->id  = rsk_id;
							req->idx = skill->get_index(rsk_id);
						} else {
							req = &VECTOR_INDEX(tree_entry->need, l);
						}
						req->lv  = (unsigned char)libconfig->setting_get_int(rsk);
					}
				}
			}
		}
	}

	libconfig->destroy(&skill_tree_conf);

	/* lets update all players skill tree */
	iter = mapit_getallusers();
	for (sd = BL_UCAST(BL_PC, mapit->first(iter)); mapit->exists(iter); sd = BL_UCAST(BL_PC, mapit->next(iter)))
		clif->skillinfoblock(sd);
	mapit->free(iter);
}

/**
 * Clears the skill tree and frees any allocated memory.
 */
void pc_clear_skill_tree(void)
{
	int i;
	for (i = 0; i < CLASS_COUNT; i++) {
		int j;
		for (j = 0; j < MAX_SKILL_TREE; j++) {
			if (pc->skill_tree[i][j].id == 0)
				continue;
			VECTOR_CLEAR(pc->skill_tree[i][j].need);
		}
	}
	memset(pc->skill_tree, 0, sizeof(pc->skill_tree));
}

bool pc_readdb_levelpenalty(char* fields[], int columns, int current) {
#if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
	int type, race, diff;

	nullpo_retr(false, fields);
	type = atoi(fields[0]);
	race = atoi(fields[1]);
	diff = atoi(fields[2]);

	if( type != 1 && type != 2 ){
		ShowWarning("pc_readdb_levelpenalty: Invalid type %d specified.\n", type);
		return false;
	}

	if (race < RC_FORMLESS || race > RC_MAX) {
		ShowWarning("pc_readdb_levelpenalty: Invalid race %d specified.\n", race);
		return false;
	}

	diff = min(diff, MAX_LEVEL);

	if( diff < 0 )
		diff = min(MAX_LEVEL + ( ~(diff) + 1 ), MAX_LEVEL*2);

	pc->level_penalty[type][race][diff] = atoi(fields[3]);
#endif
	return true;
}

/*==========================================
 * pc DB reading.
 * exp.txt        - required experience values
 * skill_tree.txt - skill tree for every class
 * attr_fix.txt   - elemental adjustment table
 *------------------------------------------*/
int pc_readdb(void) {
	int i,j,k;
	unsigned int count = 0;
	FILE *fp;
	char line[24000],*p;

	//reset
	memset(pc->exp_table,0,sizeof(pc->exp_table));
	memset(pc->max_level,0,sizeof(pc->max_level));

	sprintf(line, "%s/"DBPATH"exp.txt", map->db_path);

	fp=fopen(line, "r");
	if(fp==NULL){
		ShowError("can't read %s\n", line);
		return 1;
	}
	while(fgets(line, sizeof(line), fp)) {
		int jobs[CLASS_COUNT], job_count, job, job_id;
		int type;
		int maxlv;
		char *split[4];
		if(line[0]=='/' && line[1]=='/')
			continue;
		if (pc_split_str(line,split,4) < 4)
			continue;

		job_count = pc_split_atoi(split[1],jobs,':',CLASS_COUNT);
		if (job_count < 1)
			continue;
		job_id = jobs[0];
		if (!pc->db_checkid(job_id)) {
			ShowError("pc_readdb: Invalid job ID %d.\n", job_id);
			continue;
		}
		type = atoi(split[2]);
		if (type < 0 || type > 1) {
			ShowError("pc_readdb: Invalid type %d (must be 0 for base levels, 1 for job levels).\n", type);
			continue;
		}
		maxlv = atoi(split[0]);
		if (maxlv > MAX_LEVEL) {
			ShowWarning("pc_readdb: Specified max level %d for job %d is beyond server's limit (%d).\n ", maxlv, job_id, MAX_LEVEL);
			maxlv = MAX_LEVEL;
		}
		count++;
		job = jobs[0] = pc->class2idx(job_id);
		//We send one less and then one more because the last entry in the exp array should hold 0.
		pc->max_level[job][type] = pc_split_atoui(split[3], pc->exp_table[job][type],',',maxlv-1)+1;
		//Reverse check in case the array has a bunch of trailing zeros... [Skotlex]
		//The reasoning behind the -2 is this... if the max level is 5, then the array
		//should look like this:
		//0: x, 1: x, 2: x: 3: x 4: 0 <- last valid value is at 3.
		while ((i = pc->max_level[job][type]) >= 2 && pc->exp_table[job][type][i-2] <= 0)
			pc->max_level[job][type]--;
		if (pc->max_level[job][type] < maxlv) {
			ShowWarning("pc_readdb: Specified max %d for job %d, but that job's exp table only goes up to level %d.\n", maxlv, job_id, pc->max_level[job][type]);
			ShowInfo("Filling the missing values with the last exp entry.\n");
			//Fill the requested values with the last entry.
			i = (pc->max_level[job][type] <= 2 ? 0: pc->max_level[job][type]-2);
			for (; i+2 < maxlv; i++)
				pc->exp_table[job][type][i] = pc->exp_table[job][type][i-1];
			pc->max_level[job][type] = maxlv;
		}
		//ShowDebug("%s - Class %d: %d\n", type?"Job":"Base", job_id, pc->max_level[job][type]);
		for (i = 1; i < job_count; i++) {
			job_id = jobs[i];
			if (!pc->db_checkid(job_id)) {
				ShowError("pc_readdb: Invalid job ID %d.\n", job_id);
				continue;
			}
			job = pc->class2idx(job_id);
			memcpy(pc->exp_table[job][type], pc->exp_table[jobs[0]][type], sizeof(pc->exp_table[0][0]));
			pc->max_level[job][type] = maxlv;
			//ShowDebug("%s - Class %d: %d\n", type?"Job":"Base", job_id, pc->max_level[job][type]);
		}
	}
	fclose(fp);
	pc->validate_levels();
	ShowStatus("Done reading '"CL_WHITE"%u"CL_RESET"' entries in '"CL_WHITE"%s/"DBPATH"%s"CL_RESET"'.\n",count,map->db_path,"exp.txt");
	count = 0;
	// Reset and read skilltree
	pc->clear_skill_tree();
	pc->read_skill_tree();
#if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
	sv->readdb(map->db_path, "re/level_penalty.txt", ',', 4, 4, -1, pc->readdb_levelpenalty);
	for( k=1; k < 3; k++ ){ // fill in the blanks
		for (j = RC_FORMLESS; j < RC_MAX; j++) {
			int tmp = 0;
			for( i = 0; i < MAX_LEVEL*2; i++ ){
				if( i == MAX_LEVEL+1 )
					tmp = pc->level_penalty[k][j][0];// reset
				if( pc->level_penalty[k][j][i] > 0 )
					tmp = pc->level_penalty[k][j][i];
				else
					pc->level_penalty[k][j][i] = tmp;
			}
		}
	}
#endif

	// Reset then read attr_fix
	for(i=0;i<4;i++)
		for ( j = ELE_NEUTRAL; j<ELE_MAX; j++ )
			for ( k = ELE_NEUTRAL; k<ELE_MAX; k++ )
				battle->attr_fix_table[i][j][k]=100;

	sprintf(line, "%s/"DBPATH"attr_fix.txt", map->db_path);

	fp=fopen(line,"r");
	if(fp==NULL){
		ShowError("can't read %s\n", line);
		return 1;
	}
	while (fgets(line, sizeof(line), fp)) {
		char *split[10];
		int lv,n;
		if (line[0]=='/' && line[1]=='/')
			continue;
		for (j = 0, p = line; j < 3 && p != NULL; j++) {
			split[j] = p;
			p = strchr(p,',');
			if (p != NULL)
				*p++ = 0;
		}
		if (j < 2)
			continue;

		lv=atoi(split[0]);
		n=atoi(split[1]);
		count++;
		for ( i = ELE_NEUTRAL; i<n && i<ELE_MAX; ) {
			if( !fgets(line, sizeof(line), fp) )
				break;
			if(line[0]=='/' && line[1]=='/')
				continue;

			for (j = ELE_NEUTRAL, p = line; j < n && j < ELE_MAX && p != NULL; j++) {
				while (*p == ' ')
					p++;
				battle->attr_fix_table[lv-1][i][j]=atoi(p);
#ifndef RENEWAL
				if(battle_config.attr_recover == 0 && battle->attr_fix_table[lv-1][i][j] < 0)
					battle->attr_fix_table[lv-1][i][j] = 0;
#endif
				p=strchr(p,',');
				if (p != NULL)
					*p++ = 0;
			}

			i++;
		}
	}
	fclose(fp);
	ShowStatus("Done reading '"CL_WHITE"%u"CL_RESET"' entries in '"CL_WHITE"%s/"DBPATH"%s"CL_RESET"'.\n",count,map->db_path,"attr_fix.txt");
	count = 0;
	// reset then read statspoint
	memset(pc->statp,0,sizeof(pc->statp));
	i=1;

	sprintf(line, "%s/"DBPATH"statpoint.txt", map->db_path);
	fp=fopen(line,"r");
	if(fp == NULL){
		ShowWarning("Can't read '"CL_WHITE"%s"CL_RESET"'... Generating DB.\n",line);
		//return 1;
	} else {
		while(fgets(line, sizeof(line), fp))
		{
			int stat;
			if(line[0]=='/' && line[1]=='/')
				continue;
			if ((stat=(int)strtol(line,NULL,10))<0)
				stat=0;
			if (i > MAX_LEVEL)
				break;
			count++;
			pc->statp[i]=stat;
			i++;
		}
		fclose(fp);

		ShowStatus("Done reading '"CL_WHITE"%u"CL_RESET"' entries in '"CL_WHITE"%s/"DBPATH"%s"CL_RESET"'.\n",count,map->db_path,"statpoint.txt");
	}
	// generate the remaining parts of the db if necessary
	k = battle_config.use_statpoint_table; //save setting
	battle_config.use_statpoint_table = 0; //temporarily disable to force pc->gets_status_point use default values
	pc->statp[0] = 45; // seed value
	for (; i <= MAX_LEVEL; i++)
		pc->statp[i] = pc->statp[i-1] + pc->gets_status_point(i-1);
	battle_config.use_statpoint_table = k; //restore setting

	return 0;
}

void pc_validate_levels(void) {
	int i;
	int j;
	for (i = 0; i < JOB_MAX; i++) {
		if (!pc->db_checkid(i)) continue;
		if (i == JOB_WEDDING || i == JOB_XMAS || i == JOB_SUMMER)
			continue; //Classes that do not need exp tables.
		j = pc->class2idx(i);
		if (pc->max_level[j][0] == 0)
			ShowWarning("Class %s (%d) does not has a base exp table.\n", pc->job_name(i), i);
		if (pc->max_level[j][1] == 0)
			ShowWarning("Class %s (%d) does not has a job exp table.\n", pc->job_name(i), i);
	}
}

void pc_itemcd_do(struct map_session_data *sd, bool load) {
	int i,cursor = 0;
	struct item_cd* cd = NULL;

	nullpo_retv(sd);
	if( load ) {
		if( !(cd = idb_get(pc->itemcd_db, sd->status.char_id)) ) {
			// no skill cooldown is associated with this character
			return;
		}
		for(i = 0; i < MAX_ITEMDELAYS; i++) {
			if( cd->nameid[i] && DIFF_TICK(timer->gettick(),cd->tick[i]) < 0 ) {
				sd->item_delay[cursor].tick = cd->tick[i];
				sd->item_delay[cursor].nameid = cd->nameid[i];
				cursor++;
			}
		}
		idb_remove(pc->itemcd_db,sd->status.char_id);
	} else {
		if( !(cd = idb_get(pc->itemcd_db,sd->status.char_id)) ) {
			// create a new skill cooldown object for map storage
			CREATE( cd, struct item_cd, 1 );
			idb_put( pc->itemcd_db, sd->status.char_id, cd );
		}
		for(i = 0; i < MAX_ITEMDELAYS; i++) {
			if( sd->item_delay[i].nameid && DIFF_TICK(timer->gettick(),sd->item_delay[i].tick) < 0 ) {
				cd->tick[cursor] = sd->item_delay[i].tick;
				cd->nameid[cursor] = sd->item_delay[i].nameid;
				cursor++;
			}
		}
	}
	return;
}

void pc_bank_deposit(struct map_session_data *sd, int money) {
	unsigned int limit_check;

	nullpo_retv(sd);
	limit_check = money + sd->status.bank_vault;

	if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
		clif->bank_deposit(sd,BDA_OVERFLOW);
		return;
	} else if ( money > sd->status.zeny ) {
		clif->bank_deposit(sd,BDA_NO_MONEY);
		return;
	}

	if( pc->payzeny(sd,money, LOG_TYPE_BANK, NULL) )
		clif->bank_deposit(sd,BDA_NO_MONEY);
	else {
		sd->status.bank_vault += money;
		if( map->save_settings&256 )
			chrif->save(sd,0);
		clif->bank_deposit(sd,BDA_SUCCESS);
	}
}
void pc_bank_withdraw(struct map_session_data *sd, int money) {
	unsigned int limit_check;

	nullpo_retv(sd);
	limit_check = money + sd->status.zeny;
	if (money <= 0) {
		clif->bank_withdraw(sd,BWA_UNKNOWN_ERROR);
		return;
	} else if (money > sd->status.bank_vault) {
		clif->bank_withdraw(sd,BWA_NO_MONEY);
		return;
	} else if (limit_check > MAX_ZENY) {
		/* no official response for this scenario exists. */
		clif->messagecolor_self(sd->fd, COLOR_RED, msg_sd(sd,1482));
		return;
	}

	if( pc->getzeny(sd,money, LOG_TYPE_BANK, NULL) )
		clif->bank_withdraw(sd,BWA_NO_MONEY);
	else {
		sd->status.bank_vault -= money;
		if( map->save_settings&256 )
			chrif->save(sd,0);
		clif->bank_withdraw(sd,BWA_SUCCESS);
	}
}
/* status change data arrived from char-server */
void pc_scdata_received(struct map_session_data *sd) {
	nullpo_retv(sd);
	pc->inventory_rentals(sd);
	clif->show_modifiers(sd);

	if (sd->expiration_time != 0) { // don't display if it's unlimited or unknow value
		time_t exp_time = sd->expiration_time;
		char tmpstr[1024];
		strftime(tmpstr, sizeof(tmpstr) - 1, msg_sd(sd,501), localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
		clif->wis_message(sd->fd, map->wisp_server_name, tmpstr, (int)strlen(tmpstr));

		pc->expire_check(sd);
	}

	if( sd->state.standalone ) {
		clif->pLoadEndAck(0,sd);
		pc->autotrade_populate(sd);
		pc->autotrade_start(sd);
	}
}
int pc_expiration_timer(int tid, int64 tick, int id, intptr_t data) {
	struct map_session_data *sd = map->id2sd(id);

	if( !sd ) return 0;

	sd->expiration_tid = INVALID_TIMER;

	if( sd->fd )
		clif->authfail_fd(sd->fd,10);

	map->quit(sd);

	return 0;
}
/* This timer exists only when a character with an expire timer > 24h is online */
/* It loops through online players once an hour to check whether a new < 24h is available */
int pc_global_expiration_timer(int tid, int64 tick, int id, intptr_t data) {
	struct s_mapiterator* iter;
	struct map_session_data* sd;

	iter = mapit_getallusers();
	for (sd = BL_UCAST(BL_PC, mapit->first(iter)); mapit->exists(iter); sd = BL_UCAST(BL_PC, mapit->next(iter))) {
		if( sd->expiration_time )
			pc->expire_check(sd);
	}
	mapit->free(iter);

	return 0;
}
void pc_expire_check(struct map_session_data *sd) {
	nullpo_retv(sd);
	/* ongoing timer */
	if( sd->expiration_tid != INVALID_TIMER )
		return;

	/* not within the next 24h, enable the global check */
	if( sd->expiration_time > ( time(NULL) + ( ( 60 * 60 ) * 24 ) ) ) {
		/* global check not running, enable */
		if( pc->expiration_tid == INVALID_TIMER ) {
			/* starts in 1h, repeats every hour */
			pc->expiration_tid = timer->add_interval(timer->gettick() + ((1000*60)*60), pc->global_expiration_timer, 0, 0, ((1000*60)*60));
		}
		return;
	}

	sd->expiration_tid = timer->add(timer->gettick() + (int64)(sd->expiration_time - time(NULL))*1000, pc->expiration_timer, sd->bl.id, 0);
}
/**
 * Loads autotraders
 ***/
void pc_autotrade_load(void)
{
	char *data;

	if (SQL_ERROR == SQL->Query(map->mysql_handle, "SELECT `account_id`,`char_id`,`sex`,`title` FROM `%s`",map->autotrade_merchants_db))
		Sql_ShowDebug(map->mysql_handle);

	while (SQL_SUCCESS == SQL->NextRow(map->mysql_handle)) {
		struct map_session_data *sd;
		int account_id, char_id;
		char title[MESSAGE_SIZE];
		unsigned char sex;

		SQL->GetData(map->mysql_handle, 0, &data, NULL); account_id = atoi(data);
		SQL->GetData(map->mysql_handle, 1, &data, NULL); char_id = atoi(data);
		SQL->GetData(map->mysql_handle, 2, &data, NULL); sex = atoi(data);
		SQL->GetData(map->mysql_handle, 3, &data, NULL); safestrncpy(title, data, sizeof(title));

		CREATE(sd, struct map_session_data, 1);

		pc->setnewpc(sd, account_id, char_id, 0, 0, sex, 0);

		safestrncpy(sd->message, title, MESSAGE_SIZE);
		sd->state.standalone = 1;
		sd->group = pcg->get_dummy_group();

		chrif->authreq(sd,true);
	}
	SQL->FreeResult(map->mysql_handle);
}
/**
 * Loads vending data and sets it up, is triggered when char server data that pc_autotrade_load requested arrives
 **/
void pc_autotrade_start(struct map_session_data *sd) {
	unsigned int count = 0;
	int i;
	char *data;

	nullpo_retv(sd);
	if (SQL_ERROR == SQL->Query(map->mysql_handle, "SELECT `itemkey`,`amount`,`price` FROM `%s` WHERE `char_id` = '%d'",map->autotrade_data_db,sd->status.char_id))
		Sql_ShowDebug(map->mysql_handle);

	while( SQL_SUCCESS == SQL->NextRow(map->mysql_handle) ) {
		int itemkey, amount, price;

		SQL->GetData(map->mysql_handle, 0, &data, NULL); itemkey = atoi(data);
		SQL->GetData(map->mysql_handle, 1, &data, NULL); amount = atoi(data);
		SQL->GetData(map->mysql_handle, 2, &data, NULL); price = atoi(data);

		ARR_FIND(0, MAX_CART, i, sd->status.cart[i].id == itemkey);
		if( i != MAX_CART && itemdb_cantrade(&sd->status.cart[i], 0, 0) ) {
			if( amount > sd->status.cart[i].amount )
				amount = sd->status.cart[i].amount;

			if( amount ) {
				sd->vending[count].index = i;
				sd->vending[count].amount = amount;
				sd->vending[count].value = cap_value(price, 0, (unsigned int)battle_config.vending_max_value);

				count++;
			}
		}
	}

	if( !count ) {
		pc->autotrade_update(sd,PAUC_REMOVE);
		map->quit(sd);
	} else {
		sd->state.autotrade = 1;
		sd->vender_id = ++vending->next_id;
		sd->vend_num = count;
		sd->state.vending = true;
		idb_put(vending->db, sd->status.char_id, sd);
		if( map->list[sd->bl.m].users )
			clif->showvendingboard(&sd->bl,sd->message,0);
	}
}
/**
 * Perform a autotrade action
 **/
void pc_autotrade_update(struct map_session_data *sd, enum e_pc_autotrade_update_action action) {
	int i;

	nullpo_retv(sd);
	/* either way, this goes down */
	if( action != PAUC_START ) {
		if (SQL_ERROR == SQL->Query(map->mysql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'",map->autotrade_data_db,sd->status.char_id))
			Sql_ShowDebug(map->mysql_handle);
	}

	switch( action ) {
		case PAUC_REMOVE:
			if (SQL_ERROR == SQL->Query(map->mysql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d' LIMIT 1",map->autotrade_merchants_db,sd->status.char_id))
				Sql_ShowDebug(map->mysql_handle);
			break;
		case PAUC_START: {
			char title[MESSAGE_SIZE*2+1];

			SQL->EscapeStringLen(map->mysql_handle, title, sd->message, strnlen(sd->message, MESSAGE_SIZE));

			if (SQL_ERROR == SQL->Query(map->mysql_handle, "INSERT INTO `%s` (`account_id`,`char_id`,`sex`,`title`) VALUES ('%d','%d','%d','%s')",
										map->autotrade_merchants_db,
										sd->status.account_id,
										sd->status.char_id,
										sd->status.sex,
										title
										))
				Sql_ShowDebug(map->mysql_handle);
		}
			/* yes we want it to fall */
		case PAUC_REFRESH:
			for( i = 0; i < sd->vend_num; i++ ) {
				if( sd->vending[i].amount == 0 )
					continue;

				if (SQL_ERROR == SQL->Query(map->mysql_handle, "INSERT INTO `%s` (`char_id`,`itemkey`,`amount`,`price`) VALUES ('%d','%d','%d','%u')",
											map->autotrade_data_db,
											sd->status.char_id,
											sd->status.cart[sd->vending[i].index].id,
											sd->vending[i].amount,
											sd->vending[i].value
											))
					Sql_ShowDebug(map->mysql_handle);
			}
			break;
	}
}
/**
 * Handles characters upon @autotrade usage
 **/
void pc_autotrade_prepare(struct map_session_data *sd) {
	struct autotrade_vending *data;
	int i, cursor = 0;
	int account_id, char_id;
	char title[MESSAGE_SIZE];
	unsigned char sex;

	nullpo_retv(sd);
	CREATE(data, struct autotrade_vending, 1);

	memcpy(data->vending, sd->vending, sizeof(sd->vending));

	for(i = 0; i < sd->vend_num; i++) {
		if( sd->vending[i].amount ) {
			memcpy(&data->list[cursor],&sd->status.cart[sd->vending[i].index],sizeof(struct item));
			cursor++;
		}
	}

	data->vend_num = (unsigned char)cursor;

	idb_put(pc->at_db, sd->status.char_id, data);

	account_id = sd->status.account_id;
	char_id = sd->status.char_id;
	sex = sd->status.sex;
	safestrncpy(title, sd->message, sizeof(title));

	sd->npc_id = 0;
	sd->npc_shopid = 0;
	if (sd->st) {
		sd->st->state = END;
		sd->st = NULL;
	}
	map->quit(sd);
	chrif->auth_delete(account_id, char_id, ST_LOGOUT);

	CREATE(sd, struct map_session_data, 1);

	pc->setnewpc(sd, account_id, char_id, 0, 0, sex, 0);

	safestrncpy(sd->message, title, MESSAGE_SIZE);
	sd->state.standalone = 1;
	sd->group = pcg->get_dummy_group();

	chrif->authreq(sd,true);
}
/**
 * Prepares autotrade data from pc->at_db from a player that has already returned from char server
 **/
void pc_autotrade_populate(struct map_session_data *sd) {
	struct autotrade_vending *data;
	int i, j, k, cursor = 0;

	nullpo_retv(sd);
	if( !(data = idb_get(pc->at_db,sd->status.char_id)) )
		return;

	for(i = 0; i < data->vend_num; i++) {
		if( !data->vending[i].amount )
			continue;

		for(j = 0; j < MAX_CART; j++) {
			if( !memcmp((char*)(&data->list[i]) + sizeof(data->list[0].id), (char*)(&sd->status.cart[j]) + sizeof(data->list[0].id), sizeof(struct item) - sizeof(data->list[0].id)) ) {
				if( cursor ) {
					ARR_FIND(0, cursor, k, sd->vending[k].index == j);
					if( k != cursor )
						continue;
				}
				break;
			}
		}

		if( j != MAX_CART ) {
			sd->vending[cursor].index = j;
			sd->vending[cursor].amount = data->vending[i].amount;
			sd->vending[cursor].value = data->vending[i].value;

			cursor++;
		}
	}

	sd->vend_num = cursor;

	pc->autotrade_update(sd,PAUC_START);

	HPM->data_store_destroy(&data->hdata);

	idb_remove(pc->at_db, sd->status.char_id);
}

/**
 * @see DBApply
 */
int pc_autotrade_final(union DBKey key, struct DBData *data, va_list ap)
{
	struct autotrade_vending* at_v = DB->data2ptr(data);
	nullpo_ret(at_v);
	HPM->data_store_destroy(&at_v->hdata);
	return 0;
}

void pc_update_idle_time(struct map_session_data* sd, enum e_battle_config_idletime type)
{
	nullpo_retv(sd);
	if (battle_config.idletime_criteria&type)
		sd->idletime = sockt->last_tick;
}

//Checks if the given class value corresponds to a player class. [Skotlex]
//JOB_NOVICE isn't checked for class_ is supposed to be unsigned
bool pc_db_checkid(unsigned int class_)
{
	return class_ < JOB_MAX_BASIC
		|| (class_ >= JOB_NOVICE_HIGH    && class_ <= JOB_DARK_COLLECTOR )
		|| (class_ >= JOB_RUNE_KNIGHT    && class_ <= JOB_MECHANIC_T2    )
		|| (class_ >= JOB_BABY_RUNE      && class_ <= JOB_BABY_MECHANIC2 )
		|| (class_ >= JOB_SUPER_NOVICE_E && class_ <= JOB_SUPER_BABY_E   )
		|| (class_ >= JOB_KAGEROU        && class_ <= JOB_OBORO          )
		|| (class_ >= JOB_REBELLION      && class_ <  JOB_MAX            );
}

/**
 * checks if player have any kind of magnifier in inventory
 * @param sd map_session_data of Player
 * @return index of magnifer, INDEX_NOT_FOUND if it is not found
 */
int pc_have_magnifier(struct map_session_data *sd)
{
	int n;
	n = pc->search_inventory(sd, ITEMID_MAGNIFIER);
	if (n == INDEX_NOT_FOUND)
		n = pc->search_inventory(sd, ITEMID_NOVICE_MAGNIFIER);
	return n;
}

/**
 * Verifies a chat message, searching for atcommands, checking if the sender
 * character can chat, and updating the idle timer.
 *
 * @param sd      The sender character.
 * @param message The message text.
 * @return Whether the message is a valid chat message.
 */
bool pc_process_chat_message(struct map_session_data *sd, const char *message)
{
	nullpo_retr(false, sd);
	if (atcommand->exec(sd->fd, sd, message, true)) {
		return false;
	}

	if (!pc->can_talk(sd)) {
		return false;
	}

	if (battle_config.min_chat_delay != 0) {
		if (DIFF_TICK(sd->cantalk_tick, timer->gettick()) > 0) {
			return false;
		}
		sd->cantalk_tick = timer->gettick() + battle_config.min_chat_delay;
	}

	pc->update_idle_time(sd, BCIDLE_CHAT);

	return true;
}

/**
 * Checks a chat message, scanning for the Super Novice prayer sequence.
 *
 * If a match is found, the angel is invoked or the counter is incremented as
 * appropriate.
 *
 * @param sd      The sender character.
 * @param message The message text.
 */
void pc_check_supernovice_call(struct map_session_data *sd, const char *message)
{
	unsigned int next = pc->nextbaseexp(sd);
	int percent = 0;

	nullpo_retv(sd);
	nullpo_retv(message);
	if ((sd->class_&MAPID_UPPERMASK) != MAPID_SUPER_NOVICE)
		return;
	if (next == 0)
		next = pc->thisbaseexp(sd);
	if (next == 0)
		return;

	// 0%, 10%, 20%, ...
	percent = (int)( ( (float)sd->status.base_exp/(float)next )*1000. );
	if ((battle_config.snovice_call_type != 0 || percent != 0) && (percent%100) == 0) {
		// 10.0%, 20.0%, ..., 90.0%
		switch (sd->state.snovice_call_flag) {
		case 0:
			if (strstr(message, msg_txt(1479))) // "Dear angel, can you hear my voice?"
				sd->state.snovice_call_flag = 1;
			break;
		case 1:
		{
			char buf[256];
			snprintf(buf, 256, msg_txt(1480), sd->status.name);
			if (strstr(message, buf)) // "I am %s Super Novice~"
				sd->state.snovice_call_flag = 2;
		}
			break;
		case 2:
			if (strstr(message, msg_txt(1481))) // "Help me out~ Please~ T_T"
				sd->state.snovice_call_flag = 3;
			break;
		case 3:
			sc_start(NULL, &sd->bl, status->skill2sc(MO_EXPLOSIONSPIRITS), 100, 17, skill->get_time(MO_EXPLOSIONSPIRITS, 5)); //Lv17-> +50 critical (noted by Poki) [Skotlex]
			clif->skill_nodamage(&sd->bl, &sd->bl, MO_EXPLOSIONSPIRITS, 5, 1);  // prayer always shows successful Lv5 cast and disregards noskill restrictions
			sd->state.snovice_call_flag = 0;
			break;
		}
	}
}

void do_final_pc(void) {
	db_destroy(pc->itemcd_db);
	pc->at_db->destroy(pc->at_db,pc->autotrade_final);

	pcg->final();

	pc->clear_skill_tree();

	ers_destroy(pc->sc_display_ers);
	ers_destroy(pc->num_reg_ers);
	ers_destroy(pc->str_reg_ers);

	return;
}

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

	pc->itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
	pc->at_db = idb_alloc(DB_OPT_RELEASE_DATA);

	pc->readdb();

	timer->add_func_list(pc->invincible_timer, "pc_invincible_timer");
	timer->add_func_list(pc->eventtimer, "pc_eventtimer");
	timer->add_func_list(pc->inventory_rental_end, "pc_inventory_rental_end");
	timer->add_func_list(pc->calc_pvprank_timer, "pc_calc_pvprank_timer");
	timer->add_func_list(pc->autosave, "pc_autosave");
	timer->add_func_list(pc->spiritball_timer, "pc_spiritball_timer");
	timer->add_func_list(pc->follow_timer, "pc_follow_timer");
	timer->add_func_list(pc->endautobonus, "pc_endautobonus");
	timer->add_func_list(pc->charm_timer, "pc_charm_timer");
	timer->add_func_list(pc->global_expiration_timer,"pc_global_expiration_timer");
	timer->add_func_list(pc->expiration_timer,"pc_expiration_timer");

	timer->add(timer->gettick() + map->autosave_interval, pc->autosave, 0, 0);

	// 0=day, 1=night [Yor]
	map->night_flag = battle_config.night_at_start ? 1 : 0;

	if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
		int day_duration = battle_config.day_duration;
		int night_duration = battle_config.night_duration;
		// add night/day timer [Yor]
		timer->add_func_list(pc->map_day_timer, "pc_map_day_timer");
		timer->add_func_list(pc->map_night_timer, "pc_map_night_timer");

		pc->day_timer_tid   = timer->add_interval(timer->gettick() + (map->night_flag ? 0 : day_duration) + night_duration, pc->map_day_timer,   0, 0, day_duration + night_duration);
		pc->night_timer_tid = timer->add_interval(timer->gettick() + day_duration + (map->night_flag ? night_duration : 0), pc->map_night_timer, 0, 0, day_duration + night_duration);
	}

	pcg->init();

	pc->sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.c:sc_display_ers", ERS_OPT_FLEX_CHUNK);
	pc->num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.c::num_reg_ers", ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK);
	pc->str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.c::str_reg_ers", ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK);

	ers_chunk_size(pc->sc_display_ers, 150);
	ers_chunk_size(pc->num_reg_ers, 300);
	ers_chunk_size(pc->str_reg_ers, 50);
}
/*=====================================
* Default Functions : pc.h
* Generated by HerculesInterfaceMaker
* created by Susu
*-------------------------------------*/
void pc_defaults(void) {
	const struct sg_data sg_info[MAX_PC_FEELHATE] = {
		{ SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
		{ SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
		{ SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
	};
	unsigned int equip_pos[EQI_MAX]={EQP_ACC_L,EQP_ACC_R,EQP_SHOES,EQP_GARMENT,EQP_HEAD_LOW,EQP_HEAD_MID,EQP_HEAD_TOP,EQP_ARMOR,EQP_HAND_L,EQP_HAND_R,EQP_COSTUME_HEAD_TOP,EQP_COSTUME_HEAD_MID,EQP_COSTUME_HEAD_LOW,EQP_COSTUME_GARMENT,EQP_AMMO, EQP_SHADOW_ARMOR, EQP_SHADOW_WEAPON, EQP_SHADOW_SHIELD, EQP_SHADOW_SHOES, EQP_SHADOW_ACC_R, EQP_SHADOW_ACC_L };

	pc = &pc_s;

	/* vars */
	pc->at_db = NULL;
	pc->itemcd_db = NULL;
	/* */
	pc->day_timer_tid = INVALID_TIMER;
	pc->night_timer_tid = INVALID_TIMER;

	// These macros are used instead of a sum of sizeof(), to ensure that padding won't interfere with our size, and code won't rot when adding more fields
	memset(ZEROED_BLOCK_POS(pc), 0, ZEROED_BLOCK_SIZE(pc));

	/* */
	memcpy(pc->equip_pos, &equip_pos, sizeof(pc->equip_pos));
	/* */
	memcpy(pc->sg_info, sg_info, sizeof(pc->sg_info));
	/* */
	pc->sc_display_ers = NULL;
	/* */
	pc->expiration_tid = INVALID_TIMER;
	/* */
	pc->num_reg_ers = NULL;
	pc->str_reg_ers = NULL;
	/* */
	pc->reg_load = false;
	/* funcs */
	pc->init = do_init_pc;
	pc->final = do_final_pc;

	pc->get_dummy_sd = pc_get_dummy_sd;
	pc->class2idx = pc_class2idx;

	pc->can_use_command = pc_can_use_command;
	pc->set_group = pc_set_group;
	pc->should_log_commands = pc_should_log_commands;

	pc->setrestartvalue = pc_setrestartvalue;
	pc->makesavestatus = pc_makesavestatus;
	pc->respawn = pc_respawn;
	pc->setnewpc = pc_setnewpc;
	pc->authok = pc_authok;
	pc->authfail = pc_authfail;
	pc->reg_received = pc_reg_received;

	pc->isequip = pc_isequip;
	pc->equippoint = pc_equippoint;
	pc->setinventorydata = pc_setinventorydata;

	pc->checkskill = pc_checkskill;
	pc->checkskill2 = pc_checkskill2;
	pc->checkallowskill = pc_checkallowskill;
	pc->checkequip = pc_checkequip;

	pc->calc_skilltree = pc_calc_skilltree;
	pc->calc_skilltree_normalize_job = pc_calc_skilltree_normalize_job;
	pc->clean_skilltree = pc_clean_skilltree;

	pc->setpos = pc_setpos;
	pc->setsavepoint = pc_setsavepoint;
	pc->randomwarp = pc_randomwarp;
	pc->memo = pc_memo;

	pc->checkadditem = pc_checkadditem;
	pc->inventoryblank = pc_inventoryblank;
	pc->search_inventory = pc_search_inventory;
	pc->payzeny = pc_payzeny;
	pc->additem = pc_additem;
	pc->getzeny = pc_getzeny;
	pc->delitem = pc_delitem;
	// Special Shop System
	pc->paycash = pc_paycash;
	pc->getcash = pc_getcash;

	pc->cart_additem = pc_cart_additem;
	pc->cart_delitem = pc_cart_delitem;
	pc->putitemtocart = pc_putitemtocart;
	pc->getitemfromcart = pc_getitemfromcart;
	pc->cartitem_amount = pc_cartitem_amount;

	pc->takeitem = pc_takeitem;
	pc->dropitem = pc_dropitem;

	pc->isequipped = pc_isequipped;
	pc->can_Adopt = pc_can_Adopt;
	pc->adoption = pc_adoption;

	pc->updateweightstatus = pc_updateweightstatus;

	pc->addautobonus = pc_addautobonus;
	pc->exeautobonus = pc_exeautobonus;
	pc->endautobonus = pc_endautobonus;
	pc->delautobonus = pc_delautobonus;

	pc->bonus = pc_bonus;
	pc->bonus2 = pc_bonus2;
	pc->bonus3 = pc_bonus3;
	pc->bonus4 = pc_bonus4;
	pc->bonus5 = pc_bonus5;
	pc->skill = pc_skill;

	pc->insert_card = pc_insert_card;
	pc->can_insert_card = pc_can_insert_card;
	pc->can_insert_card_into = pc_can_insert_card_into;

	pc->steal_item = pc_steal_item;
	pc->steal_coin = pc_steal_coin;

	pc->modifybuyvalue = pc_modifybuyvalue;
	pc->modifysellvalue = pc_modifysellvalue;

	pc->follow = pc_follow; // [MouseJstr]
	pc->stop_following = pc_stop_following;

	pc->maxbaselv = pc_maxbaselv;
	pc->maxjoblv = pc_maxjoblv;
	pc->checkbaselevelup = pc_checkbaselevelup;
	pc->checkjoblevelup = pc_checkjoblevelup;
	pc->gainexp = pc_gainexp;
	pc->nextbaseexp = pc_nextbaseexp;
	pc->thisbaseexp = pc_thisbaseexp;
	pc->nextjobexp = pc_nextjobexp;
	pc->thisjobexp = pc_thisjobexp;
	pc->gets_status_point = pc_gets_status_point;
	pc->need_status_point = pc_need_status_point;
	pc->maxparameterincrease = pc_maxparameterincrease;
	pc->statusup = pc_statusup;
	pc->statusup2 = pc_statusup2;
	pc->skillup = pc_skillup;
	pc->allskillup = pc_allskillup;
	pc->resetlvl = pc_resetlvl;
	pc->resetstate = pc_resetstate;
	pc->resetskill = pc_resetskill;
	pc->resetfeel = pc_resetfeel;
	pc->resethate = pc_resethate;
	pc->equipitem = pc_equipitem;
	pc->equipitem_pos = pc_equipitem_pos;
	pc->unequipitem = pc_unequipitem;
	pc->unequipitem_pos = pc_unequipitem_pos;
	pc->checkitem = pc_checkitem;
	pc->useitem = pc_useitem;

	pc->skillatk_bonus = pc_skillatk_bonus;
	pc->skillheal_bonus = pc_skillheal_bonus;
	pc->skillheal2_bonus = pc_skillheal2_bonus;

	pc->damage = pc_damage;
	pc->dead = pc_dead;
	pc->revive = pc_revive;
	pc->heal = pc_heal;
	pc->itemheal = pc_itemheal;
	pc->percentheal = pc_percentheal;
	pc->jobchange = pc_jobchange;
	pc->setoption = pc_setoption;
	pc->setcart = pc_setcart;
	pc->setfalcon = pc_setfalcon;
	pc->setridingpeco = pc_setridingpeco;
	pc->setmadogear = pc_setmadogear;
	pc->setridingdragon = pc_setridingdragon;
	pc->setridingwug = pc_setridingwug;
	pc->changelook = pc_changelook;
	pc->equiplookall = pc_equiplookall;

	pc->readparam = pc_readparam;
	pc->setparam = pc_setparam;
	pc->readreg = pc_readreg;
	pc->setreg = pc_setreg;
	pc->readregstr = pc_readregstr;
	pc->setregstr = pc_setregstr;
	pc->readregistry = pc_readregistry;
	pc->setregistry = pc_setregistry;
	pc->readregistry_str = pc_readregistry_str;
	pc->setregistry_str = pc_setregistry_str;

	pc->addeventtimer = pc_addeventtimer;
	pc->deleventtimer = pc_deleventtimer;
	pc->cleareventtimer = pc_cleareventtimer;
	pc->addeventtimercount = pc_addeventtimercount;

	pc->calc_pvprank = pc_calc_pvprank;
	pc->calc_pvprank_timer = pc_calc_pvprank_timer;

	pc->ismarried = pc_ismarried;
	pc->marriage = pc_marriage;
	pc->divorce = pc_divorce;
	pc->get_partner = pc_get_partner;
	pc->get_father = pc_get_father;
	pc->get_mother = pc_get_mother;
	pc->get_child = pc_get_child;

	pc->bleeding = pc_bleeding;
	pc->regen = pc_regen;

	pc->setstand = pc_setstand;
	pc->candrop = pc_candrop;
	pc->can_talk = pc_can_talk;
	pc->can_attack = pc_can_attack;

	pc->jobid2mapid = pc_jobid2mapid; // Skotlex
	pc->mapid2jobid = pc_mapid2jobid; // Skotlex

	pc->job_name = job_name;

	pc->setinvincibletimer = pc_setinvincibletimer;
	pc->delinvincibletimer = pc_delinvincibletimer;

	pc->addspiritball = pc_addspiritball;
	pc->delspiritball = pc_delspiritball;
	pc->addfame = pc_addfame;
	pc->famerank = pc_famerank;
	pc->set_hate_mob = pc_set_hate_mob;
	pc->getmaxspiritball = pc_getmaxspiritball;

	pc->readdb = pc_readdb;
	pc->map_day_timer = map_day_timer; // by [yor]
	pc->map_night_timer = map_night_timer; // by [yor]
	// Rental System
	pc->inventory_rentals = pc_inventory_rentals;
	pc->inventory_rental_clear = pc_inventory_rental_clear;
	pc->inventory_rental_add = pc_inventory_rental_add;

	pc->disguise = pc_disguise;
	pc->isautolooting = pc_isautolooting;

	pc->overheat = pc_overheat;
	pc->banding = pc_banding;

	pc->itemcd_do = pc_itemcd_do;
	pc->load_combo = pc_load_combo;

	pc->add_charm = pc_add_charm;
	pc->del_charm = pc_del_charm;

	pc->baselevelchanged = pc_baselevelchanged;
	pc->level_penalty_mod = pc_level_penalty_mod;

	pc->calc_skillpoint = pc_calc_skillpoint;

	pc->invincible_timer = pc_invincible_timer;
	pc->spiritball_timer = pc_spiritball_timer;
	pc->check_banding = pc_check_banding;
	pc->inventory_rental_end = pc_inventory_rental_end;
	pc->check_skilltree = pc_check_skilltree;
	pc->bonus_autospell = pc_bonus_autospell;
	pc->bonus_autospell_onskill = pc_bonus_autospell_onskill;
	pc->bonus_addeff = pc_bonus_addeff;
	pc->bonus_addeff_onskill = pc_bonus_addeff_onskill;
	pc->bonus_item_drop = pc_bonus_item_drop;
	pc->calcexp = pc_calcexp;
	pc->respawn_timer = pc_respawn_timer;
	pc->jobchange_killclone = jobchange_killclone;
	pc->getstat = pc_getstat;
	pc->setstat = pc_setstat;
	pc->eventtimer = pc_eventtimer;
	pc->daynight_timer_sub = pc_daynight_timer_sub;
	pc->charm_timer = pc_charm_timer;
	pc->readdb_levelpenalty = pc_readdb_levelpenalty;
	pc->autosave = pc_autosave;
	pc->follow_timer = pc_follow_timer;
	pc->read_skill_tree = pc_read_skill_tree;
	pc->clear_skill_tree = pc_clear_skill_tree;
	pc->isUseitem = pc_isUseitem;
	pc->show_steal = pc_show_steal;
	pc->checkcombo = pc_checkcombo;
	pc->calcweapontype = pc_calcweapontype;
	pc->removecombo = pc_removecombo;

	pc->bank_withdraw = pc_bank_withdraw;
	pc->bank_deposit = pc_bank_deposit;

	pc->rental_expire = pc_rental_expire;
	pc->scdata_received = pc_scdata_received;

	pc->bound_clear = pc_bound_clear;

	pc->expiration_timer = pc_expiration_timer;
	pc->global_expiration_timer = pc_global_expiration_timer;
	pc->expire_check = pc_expire_check;
	pc->db_checkid = pc_db_checkid;
	pc->validate_levels = pc_validate_levels;

	pc->check_supernovice_call = pc_check_supernovice_call;
	pc->process_chat_message = pc_process_chat_message;

	/**
	 * Autotrade persistency [Ind/Hercules <3]
	 **/
	pc->autotrade_load = pc_autotrade_load;
	pc->autotrade_update = pc_autotrade_update;
	pc->autotrade_start = pc_autotrade_start;
	pc->autotrade_prepare = pc_autotrade_prepare;
	pc->autotrade_populate = pc_autotrade_populate;
	pc->autotrade_final = pc_autotrade_final;

	pc->check_job_name = pc_check_job_name;
	pc->update_idle_time = pc_update_idle_time;

	pc->have_magnifier = pc_have_magnifier;
}