File: item.cpp

package info (click to toggle)
powder 117-2
  • links: PTS
  • area: non-free
  • in suites: stretch
  • size: 10,576 kB
  • ctags: 3,545
  • sloc: cpp: 55,002; makefile: 541; sh: 258; objc: 245; ansic: 107; csh: 54
file content (4802 lines) | stat: -rw-r--r-- 105,924 bytes parent folder | download | duplicates (6)
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
/*
 * PROPRIETARY INFORMATION.  This software is proprietary to POWDER
 * Development, and is not to be reproduced, transmitted, or disclosed
 * in any way without written permission.
 *
 * Produced by:	Jeff Lait
 *
 *      	POWDER Development
 *
 * NAME:        item.cpp ( POWDER Library, C++ )
 *
 * COMMENTS:
 *	Implementation of the item manipulation routines
 */

#include <mygba.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "assert.h"
#include "artifact.h"
#include "item.h"
#include "itemstack.h"
#include "creature.h"
#include "gfxengine.h"
#include "msg.h"
#include "map.h"
#include "sramstream.h"
#include "mobref.h"
#include "victory.h"
#include "piety.h"
#include "encyc_support.h"

//
// ITEM defintions
//

// Global data:

// This is a list of what true magic item each mundane item will correspond
// to.  What type it refers to varies depending on the item's type, ie:
// potions will refer to POTION_.
u8		glb_magicitem[NUM_ITEMS];
// This tracks if each given item type has been ided.  This is different from
// a specic item has been ided.  A generically ided item will properly
// index its glb_magicitem class to give its type (acid potion rather than
// purple potion), a specific id will do this and its own state (bless vs curse)
bool		glb_itemid[NUM_ITEMS];

// This is the overloaded name someone has given this dude.
// It has been strduped so should be freed.
char		*glb_itemnames[NUM_ITEMS];

int 		 glbItemCount = 0;

// This is for the callback to track who did the zapping.
MOBREF		 ITEM::ourZapper;

void
item_init()
{
    memset(glb_itemnames, 0, sizeof(char *) * NUM_ITEMS);
}

ITEM::ITEM()
{
    myNext = 0;
    myName.setName(0);
    glbItemCount++;
}

ITEM::~ITEM()
{
    MOB		*mob;

    mob = myCorpseMob.getMob();

    if (mob)
    {
	// This MUST be safe, as we own it!
	// We want to punish the owner if we are a familiar.
	// This will catch the case of corpses decaying...
	// (Or being eaten!)
	// One problem is I think double jeapordy for smashing a statue
	// of your familiar.  But such evil scum that do that deserve
	// to be punished.
	if (mob->hasIntrinsic(INTRINSIC_FAMILIAR))
	{
	    MOB		*master;
	    master = mob->getMaster();
	    if (master)
	    {
		master->systemshock();

		master->formatAndReport("%U <feel> a great loss.");
	    }
	}

	delete mob;
    }

    glbItemCount--;
}

void
item_randomizetype(MAGICTYPE_NAMES magictype, int num)
{
    int			 i, j;
    u8			*lut;
    
    lut = new u8[num];
    for (i = 0; i < num; i++)
    {
	lut[i] = i;
    }
    // Now, mix randomly...
    rand_shuffle(lut, num);

    // Now, run through our items and assign the relevant type for each
    // potion, asserting we have enough!
    j = 0;
    for (i = 0; i < NUM_ITEMS; i++)
    {
	if (glb_itemdefs[i].magictype == magictype)
	{
	    glb_magicitem[i] = lut[j++];
	    UT_ASSERT(j <= num);
	}
    }
    delete [] lut;

    // Verify we gave away each item exactly once.
    UT_ASSERT(j == num);
}

void
ITEM::init()
{
    int		 i;

    // Clear the item names.
    for (i = 0; i < NUM_ITEMS; i++)
    {
	if (glb_itemnames[i])
	    free(glb_itemnames[i]);
	glb_itemnames[i] = 0;
    }
    
    // Build all the magic lookup tables...
    memset(glb_magicitem, 0, NUM_ITEMS);

    // Clear out the id flags.
    memset(glb_itemid, 0, NUM_ITEMS);

    // Now, for each of the special types, enumerate them...
    item_randomizetype(MAGICTYPE_POTION, NUM_POTIONS);
    item_randomizetype(MAGICTYPE_SCROLL, NUM_SCROLLS);
    item_randomizetype(MAGICTYPE_RING, NUM_RINGS);
    item_randomizetype(MAGICTYPE_HELM, NUM_HELMS);
    item_randomizetype(MAGICTYPE_BOOTS, NUM_BOOTSS);
    item_randomizetype(MAGICTYPE_AMULET, NUM_AMULETS);
    item_randomizetype(MAGICTYPE_WAND, NUM_WANDS);
    item_randomizetype(MAGICTYPE_SPELLBOOK, NUM_SPELLBOOKS);
    item_randomizetype(MAGICTYPE_STAFF, NUM_STAFFS);
}

ITEM *
ITEM::createClone() const
{
    ITEM	*clone;

    clone = ITEM::create((ITEM_NAMES) myDefinition);

    clone->myStackCount = myStackCount;
    clone->myX = myX;
    clone->myY = myY;
    clone->myName = myName;
    clone->myEnchantment = myEnchantment;
    clone->myCharges = myCharges;
    clone->myPoison = myPoison;
    clone->myPoisonCharges = myPoisonCharges;
    clone->myFlag1 = myFlag1;
    clone->myCorpseMob.setMob(0);

    return clone;
}

bool
ITEM::petrify(MAP *map, MOB *petrifier)
{
    // Mounds of flesh turn into boulders.
    // Things made of flesh become stones.

    if (myDefinition == ITEM_MOUNDFLESH)
    {
	myDefinition = ITEM_BOULDER;
	myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 | ITEMFLAG1_ISBLOCKING);
	formatAndReport("The mound of flesh hardens into a boulder.");
	return true;
    }

    if (getMaterial() == MATERIAL_FLESH)
    {

	if (defn().isquest)
	{
	    // No petrification of quest items!
	    formatAndReport("%U <resist> the petrification!");
	    return false;
	}

	// This is made of flesh.  Should turn into a rock.
	formatAndReport("%U <harden> into a rock.");

	myDefinition = ITEM_ROCK;
	return true;
    }

    return false;
}

bool
ITEM::unpetrify(MAP *map, MOB *unpetrifier)
{
    if (myDefinition == ITEM_STATUE)
    {
	// This is at least possible
	MOB		*mob;

	mob = myCorpseMob.getMob();

	if (!mob)
	{
	    // Mobless statue.
	    formatAndReport("%U, being but a work of art, <collapse> into a meatball.");
	    myDefinition = ITEM_MEATBALL;
	    return true;
	}

	// Bring back to life
	mob->clearIntrinsic(INTRINSIC_DEAD);
	// Register on the map.
	mob->move(getX(), getY(), true);
	map->registerMob(mob);

	// We may have somehow been looted in the meantime.
	mob->rebuildAppearance();
	mob->rebuildWornIntrinsic();

	mob->formatAndReport("The statue of %U comes to life!");

	// Important so we don't delete ourselves :>
	myCorpseMob.setMob(0);
	map->dropItem(this);
	delete this;

	return true;
    }

    if (myDefinition == ITEM_BOULDER)
    {
	// Becomes a mound of flesh.
	formatAndReport("%U <collapse> into a mound of flesh.");
	myDefinition = ITEM_MOUNDFLESH;
	myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 & ~ITEMFLAG1_ISBLOCKING);

	return true;
    }

    if (getMaterial() == MATERIAL_STONE)
    {
	if (defn().isquest)
	{
	    // No petrification of quest items!
	    formatAndReport("%U <remain> stony!");
	    return false;
	}

	// Transforms into a meatball.
	formatAndReport("%U <collapse>...");
	myDefinition = ITEM_MEATBALL;
	formatAndReport("into %U.");
	return true;
    }
    
    return false;
}

ITEM *
ITEM::polymorph(MOB *polyer)
{
    // Flavour text...
    formatAndReport(polyer, "A shimmering cloud engulfs %U.");

    // Check for poly-immune items.
    if (defn().isquest || hasIntrinsic(INTRINSIC_UNCHANGING) || isArtifact())
    {
	formatAndReport("%U <shudder>.");
	return 0;
    }

    // Create a new item of the same type.
    ITEMTYPE_NAMES		itemtype;

    itemtype = getItemType();

    // Note artifacts can never arise from polying.
    ITEM	*newitem;

    newitem = ITEM::createRandomType(itemtype, false);

    newitem->formatAndReport(polyer, "The cloud dissipates revealing %U.");

    return newitem;
}

bool
ITEM::resurrect(MAP *map, MOB *resser, int x, int y)
{
    if (myDefinition == ITEM_BONES || myDefinition == ITEM_CORPSE)
    {
	// This is at least possible
	MOB		*mob;

	mob = myCorpseMob.getMob();

	if (!mob)
	{
	    // This likely should be an assert: Why does a corpse/bones
	    // lack a mob?
	    resser->reportMessage("The spirit has travelled too far.");
	    
	    return false;
	}

	// See if we can find room in which to resurrect the mob.
	// The closest valid square is used.
	// We or in STD_SWIM here as we want creatures brought back
	// inside water to stay in the water.
	if (!map->findCloseTile(x, y, (MOVE_NAMES) (mob->getMoveType() | MOVE_STD_SWIM)))
	{
	    formatAndReport("%U <twitch> feebily.");
	    return false;
	}
	
	// Bring back to the verge of death.
	mob->setMinimalHP();
	if (mob->canResurrectFromCorpse())
	{
	    // Trolls get full hit points.
	    mob->setMaximalHP();
	}
	mob->clearIntrinsic(INTRINSIC_DEAD);

	// Register on the map.
	mob->move(x, y, true);
	map->registerMob(mob);
	
	// The mob may have been looted on death.  Thus, we rebuild
	// it's worn intrinisics.
	mob->rebuildWornIntrinsic();
	mob->rebuildAppearance();

	mob->formatAndReport("%U <return> from the dead!");

	// After being resurrected, one is, of course, thankful.
	mob->makeSlaveOf(resser);

	// Important so we don't delete ourselves :>
	myCorpseMob.setMob(0);

	// Drop back into whatever liquid we were in.  This is a bit
	// mean as pets that die in pits will insta die on resurrecting
	// as they will fall back into the pit.  However, you can easily
	// fix this by carrying the corpse out.
	map->dropMobs(mob->getX(), mob->getY(), true, resser);

	return true;
    }
    return false;
}

bool
ITEM::raiseZombie(MAP *map, MOB *raiser)
{
    if (myDefinition == ITEM_CORPSE)
    {
	// This is at least possible
	MOB		*mob, *zombie;

	// It doesn't matter if the mob is there, it just allows
	// us to get extra hits.
	mob = myCorpseMob.getMob();

	// Note we don't clear out myCorpseMob which means that
	// if the mob is a familiar we will be punished by zombifying
	// it.
	// On reflection, I think this is appropriate.

	zombie = MOB::create(MOB_ZOMBIE);
	zombie->destroyInventory();
	if (mob)
	{
	    zombie->forceHP(mob->getMaxHP());
	    zombie->setOrigDefinition(mob->getDefinition());
	}

	// Register on the map.
	zombie->move(getX(), getY(), true);
	map->registerMob(zombie);

	zombie->formatAndReport("%U <rise> from the dead!");

	// The zombie is marked as tame...
	zombie->makeSlaveOf(raiser);

	return true;
    }
    return false;
}

bool
ITEM::raiseSkeleton(MAP *map, MOB *raiser)
{
    if (myDefinition == ITEM_BONES)
    {
	// This is at least possible
	MOB		*mob, *skeleton;

	// It doesn't matter if the mob is there, it just allows
	// us to get extra hits.
	mob = myCorpseMob.getMob();

	skeleton = MOB::create(MOB_SKELETON);
	skeleton->destroyInventory();
	if (mob)
	{
	    skeleton->forceHP(mob->getMaxHP());
	    skeleton->setOrigDefinition(mob->getDefinition());
	}

	// Register on the map.
	skeleton->move(getX(), getY(), true);
	map->registerMob(skeleton);

	skeleton->formatAndReport("%U <rise> from the dead!");

	// The skeleton is marked as tame...
	skeleton->makeSlaveOf(raiser);

	return true;
    }
    return false;
}

bool
ITEM::doBreak(int dc, MOB *breaker, MOB *owner, MAP *map, int x, int y)
{
    bool		shouldbreak = false;
    bool		deleteself = false;
    
    // Determine if this DC is enough to break the item...
    
    // For now, potions break...
    if (getItemType() == ITEMTYPE_POTION)
    {
	shouldbreak = true;
    }

    if (getDefinition() == ITEM_BOTTLE)
	shouldbreak = true;

    if (getDefinition() == ITEM_MINDACIDHAND)
    {
	deleteself = true;
	shouldbreak = true;
    }

    // TODO: Build break chance into item properties,
    // can have Glass Swords then.
    if (getDefinition() == ITEM_ARROW || getDefinition() == ITEM_FIREARROW)
    {
	// Blessed arrows much less likely to break than cursed.
	int		chance;

	chance = 10;
	if (isCursed())
	    chance = 50;
	if (isBlessed())
	    chance = 1;

	// A bit more likely to break...
	if (getDefinition() == ITEM_FIREARROW)
	    chance *= 2;

	// If the breaker is a ranger, we don't break!
	if (breaker && breaker->hasIntrinsic(INTRINSIC_DRESSED_RANGER))
	    chance = 0;
	
	if (rand_chance(chance))
	{
	    deleteself = true;
	    shouldbreak = true;
	}
    }

    // Exit early if there is no breakage.
    if (!shouldbreak)
	return false;

    // Stash this before piety as piety may kill us.
    ourZapper.setMob(breaker);

    // Provide the breaker with piety...
    if (breaker)
	breaker->pietyBreak(this);

    // Switch on type & hence effect.
    switch (getItemType())
    {
	case ITEMTYPE_MISC:
	{
	    if (getDefinition() == ITEM_BOTTLE)
	    {
		// Empty bottles grenade...
		// FALL THROUGH
	    }
	    else
		break;
	}

	case ITEMTYPE_POTION:
	{
	    // Potions are always fully destroyed
	    deleteself = true;

	    // Give the appropriate dialog...
	    formatAndReport("%U <shatter>.");

	    if (map)
	    {
		map->fireBall(x, y, 1 + isBlessed() - isCursed(), true,
				grenadeCallbackStatic,
				this);
	    }

	    break;
	}

	case ITEMTYPE_AMULET:
	case ITEMTYPE_WEAPON:
	case ITEMTYPE_ARMOUR:
	case ITEMTYPE_RING:
	case ITEMTYPE_SCROLL:
	case ITEMTYPE_SPELLBOOK:
	case ITEMTYPE_WAND:
	case ITEMTYPE_STAFF:
	case NUM_ITEMTYPES:
	case ITEMTYPE_NONE:
	case ITEMTYPE_ANY:
	case ITEMTYPE_ARTIFACT:
	    break;
    }


    // Destroy our self if required.
    if (deleteself)
    {
	// If we are stacked, we can escape actually breaking...
	if (getStackCount() > 1)
	{
	    ITEM		*top;

	    // This is very round about.  The theory goes we may
	    // do something zany in splitStack or delete...
	    top = splitStack();
	    delete top;

	    // Well, it broke, but the ITEM * is still valid...
	    return false;
	}
	
	if (owner)
	{
	    owner->dropItem(getX(), getY());
	    owner->rebuildAppearance();
	    owner->rebuildWornIntrinsic();
	}
	delete this;
    }

    // Breakage occurred!
    return true;
}

void
ITEM::renameType(const char *newname)
{
    if (glb_itemnames[myDefinition])
	free(glb_itemnames[myDefinition]);
    if (newname && newname[0])
    {
	glb_itemnames[myDefinition] = strdup(newname);
    }
    else
    {
	glb_itemnames[myDefinition] = 0;
    }
}

void
ITEM::rename(const char *newname)
{
    myName.setName(newname);
}

void
ITEM::rename(BUF buf)
{
    rename(buf.buffer());
}

const char *
ITEM::getPersonalName() const
{
    return myName.getName();
}

ITEM *
ITEM::create(ITEM_NAMES definition, bool allowartifact, bool nocurse)
{
    ITEM		*item;

    item = new ITEM();

    item->myDefinition = definition;
    item->myStackCount = rand_dice(glb_itemdefs[item->myDefinition].stacksize);
    item->myX = item->myY = 0;
    item->myFlag1 = (ITEMFLAG1_NAMES)glb_itemdefs[item->myDefinition].flag1;
    item->myNext = 0;
    item->myEnchantment = 0;
    item->myCharges = 0;

    item->myPoison = POISON_NONE;
    item->myPoisonCharges = 0;

    // Determine cursed state...
    CURSECHANCE_NAMES	cursechance;

    cursechance = (CURSECHANCE_NAMES) 
		    glb_itemdefs[item->myDefinition].cursechance;
    // Do indirection on special magic types...
    switch ((MAGICTYPE_NAMES) glb_itemdefs[item->myDefinition].magictype)
    {
	case MAGICTYPE_POTION:
	    cursechance = (CURSECHANCE_NAMES) glb_potiondefs[glb_magicitem[item->myDefinition]].cursechance;
	    break;
	case MAGICTYPE_SCROLL:
	    cursechance = (CURSECHANCE_NAMES) glb_scrolldefs[glb_magicitem[item->myDefinition]].cursechance;
	    break;
	case MAGICTYPE_RING:
	    cursechance = (CURSECHANCE_NAMES) glb_ringdefs[glb_magicitem[item->myDefinition]].cursechance;
	    break;
	case MAGICTYPE_HELM:
	    cursechance = (CURSECHANCE_NAMES) glb_helmdefs[glb_magicitem[item->myDefinition]].cursechance;
	    break;
	case MAGICTYPE_BOOTS:
	    cursechance = (CURSECHANCE_NAMES) glb_bootsdefs[glb_magicitem[item->myDefinition]].cursechance;
	    break;
	case MAGICTYPE_AMULET:
	    cursechance = (CURSECHANCE_NAMES) glb_amuletdefs[glb_magicitem[item->myDefinition]].cursechance;
	    break;
	case MAGICTYPE_WAND:
	    cursechance = (CURSECHANCE_NAMES) glb_wanddefs[glb_magicitem[item->myDefinition]].cursechance;
	    break;
	case MAGICTYPE_SPELLBOOK:
	    cursechance = (CURSECHANCE_NAMES) glb_spellbookdefs[glb_magicitem[item->myDefinition]].cursechance;
	    break;
	case MAGICTYPE_STAFF:
	    cursechance = (CURSECHANCE_NAMES) glb_staffdefs[glb_magicitem[item->myDefinition]].cursechance;
	    break;
	case MAGICTYPE_NONE:
	    break;

	case NUM_MAGICTYPES:
	    UT_ASSERT(!"Unknown magic type!");
	    break;
    }

    // Probabilities of each.
    int		curse, normal, bless;

    curse = glb_cursechancedefs[cursechance].curse;
    normal = glb_cursechancedefs[cursechance].normal;
    bless = glb_cursechancedefs[cursechance].bless;

    UT_ASSERT(curse + normal + bless == 100);
    int		percent;
    percent = rand_choice(100);
    if (!nocurse)
    {
	if (percent < curse)
	    item->myFlag1 = (ITEMFLAG1_NAMES) (item->myFlag1 | ITEMFLAG1_ISCURSED);
	else if (percent < curse + bless)
	    item->myFlag1 = (ITEMFLAG1_NAMES) (item->myFlag1 | ITEMFLAG1_ISBLESSED);
    }

    // Determine enchantment.  Only certain types of items will
    // have a default enchantment.
    // You can enchant anything, however.
    if (item->isBoots() || item->isHelmet() || item->isShield() || 
	item->isJacket() || item->isWeapon())
    {
	// Armour enchant or weapon
	// We have a 10% chance of +/-, 50-50 for each.
	// This gives 1% for +2 or -2, 18% chance for +1 and 18% chance for -1.
	// Bah, not very good.  This makes 40% of items enchanted...
	// Thus, we hard code:
	// 1% +2
	// 9% +1
	// 9% -1
	// 1% -2
	// This is also silly, as we get limitted to +/-2.
	// Instead, just use a power distribution and have 10% less chance
	// for every bonus.
	// The major flaw with this is that we go from a 20% enchanted
	// to a 10% enchanted.  Thus, we encompsass the first test
	// with a 20% check.
	
	// Randomly determine if we will increase or decrease the enchantment.
	int			enchantdir;
	if (rand_choice(2))
	    enchantdir = 1;
	else
	    enchantdir = -1;

	item->myEnchantment = 0;

	if (rand_chance(20))
	{
	    item->enchant(enchantdir);

	    // Each additional level of enchantment has 10% chance of going
	    // through.
	    // This loop will terminate.  I hope.
	    // Call me paranoid...
	    int		maxenchant = 50;
	    while (rand_chance(10))
	    {
		item->enchant(enchantdir);
		maxenchant--;
		if (!maxenchant)
		{
		    UT_ASSERT(!"Excessive enchantment!");
		    break;
		}
	    }
	}

	// If the enchantment was negative, we get an extra 30% chance
	// of it being (more) cursed.
	if (item->myEnchantment < 0 && rand_chance(30))
	    item->makeCursed();
	// Otherwise, if it is positive, there is a chance for blessed.
	if (item->myEnchantment > 0 && rand_chance(30))
	    item->makeBlessed();
    }

    // Add in charges...
    if (item->getMagicType() == MAGICTYPE_WAND)
    {
	item->myCharges = rand_dice(glb_wanddefs[item->getMagicClass()].charges);
    }

    if (item->getMagicType() == MAGICTYPE_SPELLBOOK)
    {
	item->myCharges = rand_dice(glb_spellbookdefs[item->getMagicClass()].charges);
    }

    // One in a hundred mundanes is a real artifact!  Woot!
    if (allowartifact && rand_chance(1))
	item->makeArtifact();
	
    return item;
}

ITEM_NAMES
ITEM::lookupMagicItem(MAGICTYPE_NAMES magictype, int magicclass)
{
    int		itemtype;
    
    for (itemtype = 0; itemtype < NUM_ITEMS; itemtype++)
    {
	if (glb_itemdefs[itemtype].magictype == magictype)
	{
	    if (glb_magicitem[itemtype] == magicclass)
	    {
		return (ITEM_NAMES) itemtype;
	    }
	}
    }

    return ITEM_NONE;
}

ITEM *
ITEM::createMagicItem(MAGICTYPE_NAMES magictype, int magicclass,
		      bool allowartifact,
		      bool nocurse)
{
    ITEM_NAMES	item;

    item = lookupMagicItem(magictype, magicclass);
    if (item != ITEM_NONE)
        return ITEM::create(item, allowartifact, nocurse);

    return 0;
}

ITEM *
ITEM::createRandom(bool allowartifact)
{
    int		table[NUM_ITEMS];
    int		i, max;
    int		prob;

    max = 0;
    for (i = 0; i < NUM_ITEMS; i++)
    {
	if (glb_itemdefs[i].magictype == MAGICTYPE_SCROLL)
	    prob = glb_scrolldefs[glb_magicitem[i]].rarity;
	else
	    prob = glb_itemdefs[i].rarity;

	prob *= ((int)glb_itemtypedefs[glb_itemdefs[i].itemtype].rarity);
	table[i] = prob;
	max += prob;
    }
    
    return createRandomFromTable(table, max, allowartifact);
}

ITEM *
ITEM::createRandomType(ITEMTYPE_NAMES type, bool allowartifact)
{
    int		table[NUM_ITEMS];
    int		i, max;
    int		prob;

    // Special case item types.
    if (type == ITEMTYPE_ANY)
	return ITEM::createRandom(allowartifact);

    if (type == ITEMTYPE_ARTIFACT)
    {
	ITEM		*item;

	item = ITEM::createRandom(false);
	item->makeArtifact();
	return item;
    }

    max = 0;
    for (i = 0; i < NUM_ITEMS; i++)
    {
	if (glb_itemdefs[i].itemtype == type)
	{
	    if (glb_itemdefs[i].magictype == MAGICTYPE_SCROLL)
		prob = glb_scrolldefs[glb_magicitem[i]].rarity;
	    else
		prob = glb_itemdefs[i].rarity;
	}
	else
	    prob = 0;

	table[i] = prob;
	max += prob;
    }
    
    return createRandomFromTable(table, max, allowartifact);
}

ITEM *
ITEM::createRandomFromTable(int *table, int max_prob, bool allowartifact)
{
    int		item, i;

    if (!max_prob)
    {
	UT_ASSERT(!"No such items.");
	return 0;
    }

    item = rand_choice(max_prob);
    for (i = 0; i < NUM_ITEMS; i++)
    {
	item -= table[i];
	if (item < 0)
	    return create((ITEM_NAMES) i, allowartifact);
    }

    UT_ASSERT(!"Could not find item!");
    return 0;
}

ITEM *
ITEM::createCorpse(MOB *mob)
{
    ITEM		*item;

    item = ITEM::create(ITEM_CORPSE, false);
    item->myCorpseMob.setMob(mob);

    item->myCharges = 20;

    // Boneless critters get extra time as corpses as they have
    // no skeleton state.
    if (mob->isBoneless())
	item->myCharges += 20;

    return item;
}

ITEM *
ITEM::createStatue(MOB *mob)
{
    ITEM		*item;

    item = ITEM::create(ITEM_STATUE, false);
    item->myCorpseMob.setMob(mob);

    return item;
}

void
ITEM::setPos(int x, int y)
{
    myX = x;
    myY = y;
}

void
ITEM::enchant(int delta)
{
    int		newval;

    newval = myEnchantment + delta;

    if (newval < -99)
	newval = -99;
    if (newval > 99)
	newval = 99;

    myEnchantment = newval;
}

void
ITEM::makeArtifact(const char *name)
{
    myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 | ITEMFLAG1_ISARTIFACT);

    if (!name)
	rename(artifact_buildname(getDefinition()));
    else
	rename(name);
}

void
ITEM::makeVanilla()
{
    // Ensure it isn't artifact.
    myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 & ~ITEMFLAG1_ISARTIFACT);
    rename(0);
    makePoisoned(POISON_NONE, 0);
    myEnchantment = 0;
    myCharges = 0;

    // Ensure neither bless nor cursed.
    myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 & ~ITEMFLAG1_ISBLESSED);
    myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 & ~ITEMFLAG1_ISCURSED);
}

bool
ITEM::doHeartbeat(MAP *map, MOB *owner)
{
    // Determine if we are in a proper phase.
    if (!speed_isheartbeatphase())
	return true;

    // Run mind acid decay...
    if (myDefinition == ITEM_MINDACIDHAND)
    {
	if (myCharges)
	    myCharges--;
	else
	{
	    // Has fully decayed...
	    // The owner is confused.
	    if (owner)
	    {
		owner->formatAndReport("Mind-acid soaks into %R skin.");

		owner->setTimedIntrinsic(owner, INTRINSIC_CONFUSED,
					rand_dice(3, 5, 10));
	    }

	    // Request deletion.
	    return false;
	}
    }

    // Run decay...
    if (myDefinition == ITEM_CORPSE ||
	myDefinition == ITEM_BONES)
    {
	// Check for the "preserved" bit...
	if (myCharges == 255)
	    return true;
	    
	if (myCharges)
	    myCharges--;
	else
	{
	    // Has fully decayed...
	    if (myDefinition == ITEM_CORPSE)
	    {
		// We may want to resurrect ourselves.
		MOB		*mob;

		mob = myCorpseMob.getMob();
		if (mob)
		{
		    if (mob->canResurrectFromCorpse())
		    {
			// Trigger resurrection on self.
			if (owner)
			{
			    if (resurrect(glbCurLevel, mob, 
					    owner->getX(), owner->getY()))
			    {
				return false;
			    }
			}
			else
			{
			    if (resurrect(glbCurLevel, mob, 
					    getX(), getY()))
			    {
				return false;
			    }
			}
		    }
		    
		    if (mob->isBoneless())
		    {
			// Don't create bones...
			return false;
		    }
		}
		
		myDefinition = ITEM_BONES;
		myCharges = 20;
	    }
	    else
	    {
		// Erase.
		return false;
	    }
	}
    }

    return true;
}

MATERIAL_NAMES
ITEM::getMaterial() const
{
    // If we are a corpse, inherit from our base type.
    MOB		*corpse;

    corpse = getCorpseMob();
    if (corpse)
    {
	return corpse->getMaterial();
    }
    return (MATERIAL_NAMES) glb_itemdefs[myDefinition].material;
}

ATTACK_NAMES
ITEM::getAttackName() const
{
    return (ATTACK_NAMES) glb_itemdefs[myDefinition].attack;
}

ATTACK_NAMES
ITEM::getThrownAttackName() const
{
    return (ATTACK_NAMES) glb_itemdefs[myDefinition].thrownattack;
}

SKILL_NAMES
ITEM::getAttackSkill() const
{
    return (SKILL_NAMES) glb_itemdefs[myDefinition].attackskill;
}

SKILL_NAMES
ITEM::getSpecialSkill() const
{
    return (SKILL_NAMES) glb_itemdefs[myDefinition].specialskill;
}

const ATTACK_DEF *
ITEM::getAttack() const
{
    const ARTIFACT	*art;

    art = getArtifact();

    if (art && art->hasattack)
    {
	return &art->attack;
    }
    
    return &glb_attackdefs[glb_itemdefs[myDefinition].attack];
}

const ATTACK_DEF *
ITEM::getThrownAttack() const
{
    const ARTIFACT	*art;

    art = getArtifact();

    if (art && art->hasthrownattack)
    {
	return &art->thrownattack;
    }
    
    return &glb_attackdefs[glb_itemdefs[myDefinition].thrownattack];
}

bool
ITEM::requiresLauncher() const
{
    if (glb_itemdefs[myDefinition].launcher == ITEM_NONE)
	return false;

    return true;
}

bool
ITEM::canLaunchThis(ITEM *launcher) const
{
    if (!launcher)
	return false;

    if (launcher->getDefinition() == glb_itemdefs[myDefinition].launcher)
	return true;

    return false;
}

SIZE_NAMES
ITEM::getSize() const
{
    return (SIZE_NAMES) glb_itemdefs[myDefinition].size;
}

int
ITEM::getNoiseLevel() const
{
    return glb_itemdefs[myDefinition].noise;
}

MOB *
ITEM::getCorpseMob() const
{
    if (myDefinition != ITEM_CORPSE &&
	myDefinition != ITEM_BONES)
	return 0;
    return myCorpseMob.getMob();
}

MOB *
ITEM::getStatueMob() const
{
    if (myDefinition != ITEM_STATUE)
	return 0;

    return myCorpseMob.getMob();
}

int
ITEM::calcAverageDamage() const
{
    ATTACK_NAMES	 same;
    int			 dam = 0;
    const ATTACK_DEF	*attack, *sameattack;
    
    attack = getAttack();
    while (attack)
    {
	sameattack = attack;

	while (sameattack)
	{
	    dam += (sameattack->damage.myNumdie *
			(1 + sameattack->damage.mySides)) / 2;
	    dam += sameattack->damage.myBonus;
	    dam += getEnchantment();

	    // Non physical attacks are always cooler.
	    if (sameattack->element != ELEMENT_PHYSICAL)
		dam += 2;
	    
	    same = (ATTACK_NAMES)sameattack->sameattack;
	    if (same == ATTACK_NONE)
		sameattack = 0;
	    else
		sameattack = &glb_attackdefs[same];
	}
	if (attack->nextattack == ATTACK_NONE)
	    attack = 0;
	else
	    attack = &glb_attackdefs[attack->nextattack];
    }

    // Extra stuff: bonus for poisoned.
    if (isPoisoned())
    {
	dam += 4;
    }

    // Being made of silver is just cool.
    if (getMaterial() == MATERIAL_SILVER)
    {
	dam += 4;
    }
    return dam;
}

ITEMTYPE_NAMES
ITEM::getItemType() const
{
    return (ITEMTYPE_NAMES) glb_itemdefs[getDefinition()].itemtype;
}

bool
ITEM::isCarryIntrinsic() const
{
    const ARTIFACT		*art;

    // Artifacts may have carry intrinsics...
    art = getArtifact();
    if (art && art->iscarryintrinsic)
	return true;
    
    return glb_itemdefs[myDefinition].iscarryintrinsic;
}

bool
ITEM::isFullyIdentified() const
{
    return isIdentified() && isKnownCursed() &&
	   isKnownEnchant() && isKnownCharges() &&
	   isKnownPoison();
}

bool
ITEM::isIdentified() const
{
    return glb_itemid[myDefinition];
}

bool
ITEM::isKnownCursed() const
{
    return (myFlag1 & ITEMFLAG1_ISKNOWCURSE) ? true : false;
}

bool
ITEM::isKnownNotCursed() const
{
    return (myFlag1 & ITEMFLAG1_ISKNOWNOTCURSE) ? true : false;
}

bool
ITEM::isKnownEnchant() const
{
    return (myFlag1 & ITEMFLAG1_ISKNOWENCHANT) ? true : false;
}

bool
ITEM::isKnownCharges() const
{
    return (myFlag1 & ITEMFLAG1_ISKNOWCHARGES) ? true : false;
}

bool
ITEM::isKnownPoison() const
{
    return (myFlag1 & ITEMFLAG1_ISKNOWPOISON) ? true : false;
}

bool
ITEM::isKnownClass() const
{
    if (glb_itemid[myDefinition])
	return true;
    return false;
}

bool
ITEM::isInventory() const
{
    return (myFlag1 & ITEMFLAG1_ISINVENTORY) ? true : false;
}

bool
ITEM::isArtifact() const
{
    return (myFlag1 & ITEMFLAG1_ISARTIFACT) ? true : false;
}

const ARTIFACT *
ITEM::getArtifact() const
{
    if (!isArtifact())
	return 0;

    return artifact_buildartifact(myName.getName(), getDefinition());
}

void
ITEM::setInventory(bool isinventory)
{
    if (isinventory)
	myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 | ITEMFLAG1_ISINVENTORY);
    else
	myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 & ~ITEMFLAG1_ISINVENTORY);
}

void
ITEM::markIdentified(bool silent) 
{
    markEnchantKnown();
    markCursedKnown();
    markChargesKnown();
    markPoisonKnown();

    markClassKnown(silent);
}

void
ITEM::markClassKnown(bool silent)
{
    // Avatar gets piety for this.
    if (!glb_itemid[myDefinition])
    {
	MOB		*avatar;

	avatar = MOB::getAvatar();
	if (avatar)
	{
	    // Only spam for interesting class changes.
	    if (!silent && getMagicType() != MAGICTYPE_NONE)
		avatar->formatAndReport("%U <learn> that %IU <I:be> %Ii.", this);
	    avatar->pietyIdentify((ITEM_NAMES) myDefinition);
	}
    }

    glb_itemid[myDefinition] = 1;
}

void
ITEM::markEnchantKnown()
{
    myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 | ITEMFLAG1_ISKNOWENCHANT);
}

void
ITEM::markCursedKnown()
{
    myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 | ITEMFLAG1_ISKNOWCURSE);
}

void
ITEM::markNotCursedKnown(bool newknown)
{
    if (newknown)
	myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 | ITEMFLAG1_ISKNOWNOTCURSE);
    else
	myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 & ~ITEMFLAG1_ISKNOWNOTCURSE);
}

void
ITEM::markChargesKnown()
{
    myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 | ITEMFLAG1_ISKNOWCHARGES);
}

void
ITEM::clearChargesKnown()
{
    myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 & (~ITEMFLAG1_ISKNOWCHARGES));
}

void
ITEM::markPoisonKnown()
{
    myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 | ITEMFLAG1_ISKNOWPOISON);
}

void
ITEM::makeCursed()
{
    if (myFlag1 & ITEMFLAG1_ISBLESSED)
	myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 & ~ITEMFLAG1_ISBLESSED);
    else
	myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 | ITEMFLAG1_ISCURSED);

    // Regardless, anyone's theories about this are now wrong.
    // One could argue we should not clear this flag as if they
    // cursing was obvious we usually do a markCursedKnown, so if
    // that is not done it is truly the avatar that doesn't know
    // the change, and hence the player shouldn't as well.  
    // For now, I consider that logic too mean.
    markNotCursedKnown(false);
}

void
ITEM::makeBlessed()
{
    if (myFlag1 & ITEMFLAG1_ISCURSED)
	myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 & ~ITEMFLAG1_ISCURSED);
    else
	myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 | ITEMFLAG1_ISBLESSED);
}

void
ITEM::makePoisoned(POISON_NAMES poison, int charges)
{
    myPoison = poison;
    myPoisonCharges = charges;
}

bool
ITEM::isPoisoned() const
{
    if (myPoison != POISON_NONE && myPoisonCharges)
	return true;
    return false;
}

void
ITEM::applyPoison(MOB *src, MOB *target, bool force)
{
    if (!force && rand_choice(glb_poisondefs[myPoison].modulus))
	return;

    if (!isPoisoned())
	return;

    // Is this a sensible time?
    target->setTimedIntrinsic( src, (INTRINSIC_NAMES) 
				glb_poisondefs[myPoison].intrinsic,
				rand_dice(3, 3, 0) );

    myPoisonCharges--;
}

bool
ITEM::isCursed() const
{
    return (myFlag1 & ITEMFLAG1_ISCURSED) ? true : false;
}

bool
ITEM::isBlessed() const
{
    return (myFlag1 & ITEMFLAG1_ISBLESSED) ? true : false;
}

bool
ITEM::isQuivered() const
{
    return (myFlag1 & ITEMFLAG1_ISQUIVERED) ? true : false;
}

void
ITEM::markQuivered(bool newquiver)
{
    if (newquiver)
	myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 | ITEMFLAG1_ISQUIVERED);
    else
	myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 & ~ITEMFLAG1_ISQUIVERED);
    
}

bool
ITEM::isFavorite() const
{
    return (myFlag1 & ITEMFLAG1_ISFAVORITE) ? true : false;
}

void
ITEM::markFavorite(bool newfavorite)
{
    if (newfavorite)
	myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 | ITEMFLAG1_ISFAVORITE);
    else
	myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 & ~ITEMFLAG1_ISFAVORITE);
    
}

bool
ITEM::isMapped() const
{
    return (myFlag1 & ITEMFLAG1_ISMAPPED) ? true : false;
}

void
ITEM::markMapped(bool val)
{
    if (val)
	myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 | ITEMFLAG1_ISMAPPED);
    else
	myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 & ~ITEMFLAG1_ISMAPPED);
}

bool
ITEM::isBelowGrade() const
{
    return (myFlag1 & ITEMFLAG1_ISBELOWGRADE) ? true : false;
}

void
ITEM::markBelowGrade(bool val)
{
    if (val)
	myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 | ITEMFLAG1_ISBELOWGRADE);
    else
	myFlag1 = (ITEMFLAG1_NAMES) (myFlag1 & ~ITEMFLAG1_ISBELOWGRADE);
}

bool
ITEM::isWeapon() const
{
    // Not the simplest of computation as we can equip anything...
    if (getAttackName() != ATTACK_MISUSED)
	return true;
    return false;
}

bool
ITEM::isArmor() const
{
    return (myFlag1 & ITEMFLAG1_ARMORSLOTMASK) ? true : false;
}

bool
ITEM::isBoots() const
{
    return (myFlag1 & ITEMFLAG1_ARMORSLOTMASK) == ITEMFLAG1_ISBOOTS;
}

bool
ITEM::isHelmet() const
{
    return (myFlag1 & ITEMFLAG1_ARMORSLOTMASK) == ITEMFLAG1_ISHELMET;
}

bool
ITEM::isJacket() const
{
    return (myFlag1 & ITEMFLAG1_ARMORSLOTMASK) == ITEMFLAG1_ISJACKET;
}

bool
ITEM::isShield() const
{
    return (myFlag1 & ITEMFLAG1_ARMORSLOTMASK) == ITEMFLAG1_ISSHIELD;
}

bool
ITEM::isRing() const
{
    return (myFlag1 & ITEMFLAG1_ARMORSLOTMASK) == ITEMFLAG1_ISRING;
}

bool
ITEM::isAmulet() const
{
    return (myFlag1 & ITEMFLAG1_ARMORSLOTMASK) == ITEMFLAG1_ISAMULET;
}

bool
ITEM::isPassable() const
{
    return !(myFlag1 & ITEMFLAG1_ISBLOCKING);
}

int
ITEM::getStackOrder() const
{
    // This is used to force the sorting of items.  We want users
    // to quickly see if there are interesting drops so put corpses
    // on the bottom.  Likewise, impassable objects need to be known
    // or people won't know why they can't move there - this currently
    // only means boulders.
    // Finally, artifacts are put on the top to draw attention to them
    // as they are cool.
    if (myDefinition == ITEM_CORPSE ||
	myDefinition == ITEM_BONES)
    {
	return 0;
    }

    if (!isPassable())
	return 3;

    if (isArtifact())
	return 2;

    // Default level.
    return 1;
}

const char *
ITEM::getItemName(ITEM_NAMES item, bool forceid)
{
    const char		*rawname = 0;

    if (forceid || glb_itemid[item])
    {
	switch ((MAGICTYPE_NAMES) glb_itemdefs[item].magictype)
	{
	    case NUM_MAGICTYPES:
	    case MAGICTYPE_NONE:
		rawname = glb_itemdefs[item].name;
		break;
		
	    case MAGICTYPE_POTION:
		rawname = glb_potiondefs[glb_magicitem[item]].name;
		break;

	    case MAGICTYPE_SCROLL:
		rawname = glb_scrolldefs[glb_magicitem[item]].name;
		break;

	    case MAGICTYPE_RING:
		rawname = glb_ringdefs[glb_magicitem[item]].name;
		break;

	    case MAGICTYPE_HELM:
		rawname = glb_helmdefs[glb_magicitem[item]].name;
		break;

	    case MAGICTYPE_BOOTS:
		rawname = glb_bootsdefs[glb_magicitem[item]].name;
		break;

	    case MAGICTYPE_AMULET:
		rawname = glb_amuletdefs[glb_magicitem[item]].name;
		break;

	    case MAGICTYPE_WAND:
		rawname = glb_wanddefs[glb_magicitem[item]].name;
		break;

	    case MAGICTYPE_SPELLBOOK:
		rawname = glb_spellbookdefs[glb_magicitem[item]].name;
		break;

	    case MAGICTYPE_STAFF:
		rawname = glb_staffdefs[glb_magicitem[item]].name;
		break;
	}
    }
    else
    {
	rawname = glb_itemdefs[item].name;
    }

    return rawname;
}

const char *
ITEM::getRawName(bool idclass) const
{
    const char		*rawname = 0;

    rawname = ITEM::getItemName(getDefinition(), idclass);

    return rawname;
}

BUF
ITEM::getName(bool article, bool shortname, bool neverpronoun, bool idclass, bool forcesingle) const
{
    BUF			 rawname;
    bool		 ignorecurse = false;
    BUF			 result;

    rawname.reference(getRawName(idclass));

    // In some cases, the actual name comes from what mob it actually is.
    if (myDefinition == ITEM_BONES || myDefinition == ITEM_CORPSE ||
	myDefinition == ITEM_STATUE)    
    {
	MOB		*mob;

	mob = myCorpseMob.getMob();
	if (mob)
	{
	    BUF		buf, mobname;

	    mobname = mob->getName(false, false, true);

	    buf.sprintf("%s%s %s",
			(myCharges == 255 ? "preserved " : ""),
			mobname.buffer(), rawname.buffer());
	    rawname = buf;
	}
    }

    // We have a special case off the front here.  We don't want blessed
    // bottles of water to be "a holy bottle of water", but rather be
    // "a bottle of holy water".  This is because it is the water that
    // is affected, not the bottle.
    if (myDefinition == ITEM_WATER)
    {
	if (isKnownCursed())
	{
	    if (isCursed())
		rawname.reference("bottle of unholy water");
	    if (isBlessed())
		rawname.reference("bottle of holy water");

	    ignorecurse = true;
	}
    }
    
    // NB: This must handle every case which causes further tmp buffers
    // to be needed.
    if (!article && 
	(forcesingle || (getStackCount() == 1)) &&    
	(shortname || 
		((!isKnownCursed() || ignorecurse) && 
		 !isKnownEnchant() && !isKnownCharges() && 
		 !isKnownPoison() && !glb_itemnames[myDefinition] &&
		 !myName.getName())))
    {
	return rawname;
    }

    BUF			buf;

    if (shortname || 
	    ((!isKnownCursed() || ignorecurse) && !isKnownEnchant() && !isKnownCharges() && !isKnownPoison() && !glb_itemnames[myDefinition] && !myName.getName()))
    {
	// We require an article, but that is it.
	return gram_createcount(rawname, forcesingle ? 1 : getStackCount(), article);
    }
    else
    {
	// We first build the name without the article, as the prefix
	// may alter what the proper article is.  Eg: an evil sword.
	// The problem with this are cases where there should
	// be no article - in these cases, the presence of the prefix
	// may trick us into adding an unnecessary article, Eg:
	// an evil Baezl'bub's black heart.
	BUF		nonarticle;

	nonarticle.clear();
	
	if (isKnownCursed() && !ignorecurse)
	{
	    if (isCursed())
		nonarticle.strcat("evil ");
	    if (isBlessed())
		nonarticle.strcat("holy ");
	}

	if (isKnownPoison() && isPoisoned())
	{
	    nonarticle.strcat("poisoned ");
	}
	
	if (isKnownEnchant())
	{
	    // Don't waste space on +0 unless something where that
	    // counts.
	    if (getEnchantment() != 0 ||
		isBoots() || isHelmet() || isShield() || 
		isJacket() || isWeapon())
	    {
		BUF		tmp;

		tmp.sprintf("%+d ", getEnchantment());
		nonarticle.strcat(tmp);
	    }
	}

	nonarticle.strcat(rawname);
	
	if (article && isArtifact() && (forcesingle || (getStackCount() == 1)))
	{
	    buf.strcpy("the ");
	    buf.strcat(nonarticle);
	}
	else
	{
	    buf = gram_createcount(nonarticle, forcesingle ? 1 : getStackCount(), article);
	}

	// Now, we append the charge count if we know it, and the magictype
	// is something with charges.  Currently, that means wands or 
	// spellbooks.
	if (isKnownCharges() && 
	    (getMagicType() == MAGICTYPE_WAND ||
	     getMagicType() == MAGICTYPE_SPELLBOOK ||
	     getDefinition() == ITEM_LIGHTNINGRAPIER))
	{
	    BUF			tmp;

	    tmp.sprintf(" (%d)", myCharges);
	    buf.strcat(tmp);
	}

	if (myName.getName())
	{
	    // User named items are called.  Artifact items
	    // just *are* that name.  Ie: the long sword foobar.
	    if (isArtifact())
		buf.strcat(" ");
	    else
		buf.strcat(" called ");
	    buf.strcat(myName.getName());
	}
	else if (glb_itemnames[myDefinition])
	{
	    buf.strcat(" named ");
	    buf.strcat(glb_itemnames[myDefinition]);
	}
    }
    
    return buf;
}

void
ITEM::formatAndReport(const char *str)
{
    formatAndReport(0, str);
}
    
void
ITEM::formatAndReport(MOB *owner, const char *str)
{
    BUF		buf;

    if (!str)
	return;

    // Ignore if we are loading so we don't spam about re-wetting
    // sunk treasure.
    if (MAP::isLoading())
	return;

    buf = MOB::formatToString(str, 0, this, 0, 0);

    if (!owner && !isInventory())
    {
	// Spam our report to the map
	glbCurLevel->reportMessage(buf, getX(), getY());
    }
    else
    {
	if (owner)
	    owner->reportMessage(buf);
	else if (MOB::getAvatar())
	    MOB::getAvatar()->reportMessage(buf);
    }
}

void
ITEM::viewDiscoveries()
{
    ITEM_NAMES		item;
    int			num = 0;
    
    // Written while waiting for C967 to show up in the line
    // at the passport office.
    gfx_pager_addtext("Your discoveries:");
    gfx_pager_newline();
    gfx_pager_separator();

    FOREACH_ITEM(item)
    {
	// Check to see if this is a meaningful id
	// Unlike nethack, I don't think iding longswords is interesting.
	const char *basename, *idname;
	
	idname = ITEM::getItemName(item);
	basename = glb_itemdefs[item].name;
	// No need for strcmp here since we are using the 
	// prebuilt tables.
	if (idname != basename)
	{
	    BUF		buf;

	    buf.sprintf("%s:", basename);
	    gfx_pager_addtext(gram_capitalize(buf));
	    gfx_pager_newline();
	    buf.sprintf("  %s", idname);
	    gfx_pager_addtext(gram_capitalize(buf));
	    gfx_pager_newline();
	    num++;
	}
    }

    if (!num)
    {
	gfx_pager_addtext("You have learned nothing.");
	gfx_pager_newline();
    }

    gfx_pager_display();
}

void
ITEM::viewDescription() const
{
    pageDescription(false);

    gfx_pager_display();
}

BUF
ITEM::buildAttackName(const ATTACK_DEF *attack)
{
    BUF buf;

    buf.strcpy("");
    while (attack)
    {
	BUF		section;
	bool		skipbonus = false;

	if (attack->damage.myNumdie)
	{
	    if (attack->damage.mySides < 2)
	    {
		// Constant damage.  Fold in the bonus damage
		// and report just this.
		section.sprintf("%d",
			    attack->damage.myNumdie + attack->damage.myBonus);
		skipbonus = true;
		buf.strcat(section);
	    }
	    else
	    {
		section.sprintf("%d", 
			attack->damage.myNumdie);
		buf.strcat(section);
		section.sprintf("d%d", 
			attack->damage.mySides);
		buf.strcat(section);
	    }
	}
	if (!skipbonus && attack->damage.myBonus)
	{
	    section.sprintf("%+d", 
		attack->damage.myBonus);
	    buf.strcat(section);
	}
	if (attack->bonustohit)
	{
	    section.sprintf("[%+d]", attack->bonustohit);
	    buf.strcat(section);
	}
	section.sprintf(" (%s)", glb_elementdefs[attack->element].name);
	buf.strcat(section);

	if (attack->nextattack != ATTACK_NONE)
	{
	    buf.strcat(", {");

	    section = buildAttackName(&glb_attackdefs[attack->nextattack]);
	    buf.strcat(section);

	    buf.strcat("}");
	}
	
	if (attack->sameattack != ATTACK_NONE)
	{
	    buf.strcat(", ");
	    attack = &glb_attackdefs[attack->sameattack];
	}
	else
	    attack = 0;
    }

    return buf;
}

void
ITEM::pageDescription(bool brief) const
{
    BUF			buf;
    int			skilllevel;
    
    gfx_pager_addtext(gram_capitalize(getName()));
    gfx_pager_newline();

    // Add the identify info.
    const ARTIFACT		*art = getArtifact();
    if ((isIdentified() && !art) ||
	(isFullyIdentified() && art))
    {
	bool		hasany = false;
	INTRINSIC_NAMES	intrinsic;
	const char 	*prequel;

	if (!brief)
	{
	    gfx_pager_separator();
	}

	if (art)
	    prequel = "This artifact grants ";
	else
	    prequel = "This item grants ";

	for (intrinsic = INTRINSIC_NONE; intrinsic < NUM_INTRINSICS;
		intrinsic = (INTRINSIC_NAMES) (intrinsic + 1))
	{
	    if (hasIntrinsic(intrinsic))
	    {
		if (!hasany)
		{
		    gfx_pager_addtext(prequel);
		    hasany = true;
		}
		else
		    gfx_pager_addtext(", ");

		gfx_pager_addtext(glb_intrinsicdefs[intrinsic].name);
	    }
	}

	// Light radius:
	if (isLight())
	{
	    if (!hasany)
	    {
		gfx_pager_addtext(prequel);
		hasany = true;
	    }
	    else
		gfx_pager_addtext(", ");

	    buf.sprintf("light radius %d", getLightRadius());
	    gfx_pager_addtext(buf);
	}

	// Amour class
	if (getAC())
	{
	    if (!hasany)
	    {
		gfx_pager_addtext(prequel);
		hasany = true;
	    }
	    else
		gfx_pager_addtext(", ");

	    buf.sprintf("armour %d", getAC());
	    gfx_pager_addtext(buf);
	}

	// Finish off the special stuff list, and go to mandatory list.
	if (hasany)
	{
	    gfx_pager_addtext(".  ");
	    gfx_pager_newline();
	}

	if (isCarryIntrinsic())
	{
	    gfx_pager_addtext("Intrinsics are granted by carrying this item.");
	    gfx_pager_newline();
	}

	const ATTACK_DEF	*attack;
	
	gfx_pager_addtext("Melee: ");
	buf = buildAttackName(getAttack());
	gfx_pager_addtext(buf);
	gfx_pager_newline();

	gfx_pager_addtext("Thrown ");
	attack = getThrownAttack();

	buf.sprintf("%d: ", attack->range);
	gfx_pager_addtext(buf);
	
	buf = buildAttackName(attack);
	gfx_pager_addtext(buf);

	gfx_pager_newline();
    }

    // Skip out here if brief.
    if (brief)
	return;

    // Add the boring stats.
    gfx_pager_separator();

    buf.strcpy("Size: ");
    buf.strcat(gram_capitalize(glb_sizedefs[getSize()].name));
    gfx_pager_addtext(buf);
    gfx_pager_newline();

    buf.strcpy("Material: ");
    buf.strcat(gram_capitalize(glb_materialdefs[getMaterial()].name));
    gfx_pager_addtext(buf);
    gfx_pager_newline();

    buf.sprintf("Weight: %d",
	    glb_itemdefs[getDefinition()].weight);
    gfx_pager_addtext(buf);
    gfx_pager_newline();

    const char *noisetext;
    switch (getNoiseLevel())
    {
	case 0:
	    noisetext = "Silent";
	    break;
	case 1:
	    noisetext = "Quiet";
	    break;
	case 2:
	    noisetext = "Average";
	    break;
	case 3:
	    noisetext = "Loud";
	    break;
	default:
	case 4:
	    noisetext = "Very Loud";
	    break;
    }

    buf.sprintf("Noise: %s", noisetext);
    gfx_pager_addtext(buf);
    gfx_pager_newline();

    gfx_pager_addtext("Attack Style: ");
    buf.strcpy(glb_skilldefs[getAttackSkill()].name);
    // Remove first space.
    {
	char	*c;
	for (c = buf.evildata(); *c && !isspace(*c); c++);
	*c = '\0';
    }
    gfx_pager_addtext(buf);
    gfx_pager_newline();

    // Special skill, if any.
    if (getSpecialSkill() != SKILL_NONE)
    {
	gfx_pager_addtext("Special Skill: ");
	gfx_pager_addtext(glb_skilldefs[getSpecialSkill()].name);
	gfx_pager_newline();
    }

    // Calculate your attack ability.
    skilllevel = MOB::getAvatar()->getWeaponSkillLevel(this, ATTACKSTYLE_MELEE);
    if (skilllevel)
    {
	gfx_pager_addtext("Melee Skill: ");
	while (skilllevel--)
	{
	    gfx_pager_addtext("*");
	}
	gfx_pager_newline();
    }

    skilllevel = MOB::getAvatar()->getWeaponSkillLevel(this, ATTACKSTYLE_THROWN);
    if (skilllevel)
    {
	gfx_pager_addtext("Thrown Skill: ");
	while (skilllevel--)
	{
	    gfx_pager_addtext("*");
	}
	gfx_pager_newline();
    }

    skilllevel = MOB::getAvatar()->getArmourSkillLevel(this);
    if (skilllevel)
    {
	gfx_pager_addtext("Armour Skill: ");
	while (skilllevel--)
	{
	    gfx_pager_addtext("*");
	}
	gfx_pager_newline();
    }

    // Add encyclopedia description.
    if (encyc_hasentry("ITEM", getDefinition()))
    {
	gfx_pager_separator();
	encyc_pageentry("ITEM", getDefinition());
    }

    // Add the magical encylopedia entry
    if (isIdentified() && getMagicType() != MAGICTYPE_NONE)
    {
	if (encyc_hasentry(glb_magictypedefs[getMagicType()].cycname, getMagicClass()))
	{
	    gfx_pager_separator();
	    encyc_pageentry(glb_magictypedefs[getMagicType()].cycname, getMagicClass());
	}
    }
}

const char *
ITEM::getPronoun() const
{
    return gram_getpronoun(getPerson());
}

const char *
ITEM::getPossessive() const
{
    return gram_getpossessive(getPerson());
}

const char *
ITEM::getReflexive() const
{
    return gram_getreflexive(getPerson());
}

const char *
ITEM::getAccusative() const
{
    return gram_getaccusative(getPerson());
}

BUF
ITEM::conjugate(const char *infinitive, bool past) const
{
    return gram_conjugate(infinitive, getPerson(), past);
}

VERB_PERSON
ITEM::getPerson() const
{
    // Determine our plurality...
    if (myStackCount > 1)
    {
	// We are stacked, we are plural no matter what...
	return VERB_THEY;
    }

    const char		*name;

    // *cries*
    // Guess who called getPronoun inside of getName?
    name = getRawName();

    if (gram_isnameplural(name))
    {
	return VERB_THEY;
    }
    return VERB_IT;
}
    
bool
ITEM::isLight() const
{
    if (glb_itemdefs[myDefinition].lightradius)
	return true;

    // Check for ring of light...
    if (getMagicType() == MAGICTYPE_RING &&
	getMagicClass() == RING_LIGHT)
    {
	return true;
    }

    const ARTIFACT 	*art;
    art = getArtifact();

    if (art && art->lightradius)
	return true;

    return false;
}

int
ITEM::getLightRadius() const
{
    int		radius;

    radius = glb_itemdefs[myDefinition].lightradius;
    
    // Check for ring of light...
    if (getMagicType() == MAGICTYPE_RING &&
	getMagicClass() == RING_LIGHT)
    {
	if (radius < 3)
	    radius = 3;
    }

    const ARTIFACT		*art;

    art = getArtifact();
    if (art)
    {
	if (radius < art->lightradius)
	    radius = art->lightradius;
    }
    
    return radius;
}

TILE_NAMES
ITEM::getTile() const
{
    return (TILE_NAMES) glb_itemdefs[myDefinition].tile;
}

MINI_NAMES
ITEM::getMiniTile() const
{
    return (MINI_NAMES) glb_itemdefs[myDefinition].minitile;
}

//
// Stack Routines
//

bool
ITEM::canMerge(ITEM *item) const
{
    // Verify we match on all particulars.
    if (item->myDefinition != myDefinition)
	return false;

    // If either of our items have a name, we can't merge.
    if (item->myName.getName() || myName.getName())
    {
	// However, if both have a name
	if (item->myName.getName() && myName.getName())
	{
	    // And the names match
	    if (!strcmp(item->myName.getName(), myName.getName()))
	    {
		// We can merge!
	    }
	    else
		return false;
	}
	else
	    return false;
    }
    
    // Prohibit merging of artifacts unless both are artifacts.
    // Otherwise, naming an item the same as an artifact would allow
    // the creation of artifacts.
    if (item->isArtifact() != isArtifact())
	return false;

    // Prohibit merging of corpses!
    // This is required as we keep the mob * underneath them.
    // We also don't want to merge statues for the same reason.
    // Thus, if either item has a non-zero corpsemob, it is illegal.
    if (!item->myCorpseMob.isNull() || !myCorpseMob.isNull())
	return false;

    if (item->myEnchantment != myEnchantment)
	return false;

    if (item->myCharges != myCharges)
	return false;

    if (item->myPoison != myPoison)
	return false;

    if (item->myPoisonCharges != myPoisonCharges)
	return false;

    // Flags are a bit trickier, as we only care about some of them.

    if (item->isBlessed() != isBlessed())
	return false;
    if (item->isCursed() != isCursed())
	return false;

    // We don't want to reveal curse states by merging, so known items
    // won't merge with unknown.
    if (item->isKnownCursed() != isKnownCursed())
	return false;
    // We only care about enchantment amounts where it is armour or
    // weapon.
    if (getItemType() == ITEMTYPE_ARMOUR ||
	getItemType() == ITEMTYPE_WEAPON)
    {
	if (item->isKnownEnchant() != isKnownEnchant())
	    return false;
    }
    // We only care about charges with books and wands...
    if (getItemType() == ITEMTYPE_SPELLBOOK ||
	getItemType() == ITEMTYPE_WAND)
    {
	if (item->isKnownCharges() != isKnownCharges())
	    return false;
    }
    // We only care about poison if there is poison.  This does
    // let people id non-poisoned things but this is less of an
    // issue than the pain of detect curse not letting stuff stack.
    if (myPoisonCharges)
	if (item->isKnownPoison() != isKnownPoison())
	    return false;

    // Make sure a merge would not overflow our stack count.
    if (myStackCount + item->myStackCount > 99)
	return false;

    // These items are indistinguishable, merge them!
    return true;
}

void
ITEM::mergeStack(ITEM *item)
{
    UT_ASSERT(canMerge(item));
    UT_ASSERT(myStackCount + item->myStackCount <= 99);

    myStackCount += item->myStackCount;

    // Make sure we have the union of knowledge.
    // We don't want to lose any knowledge as a result of a merge,
    // even if that knowledge is deemed useless.
    if (item->isKnownCharges())
	markChargesKnown();
    if (item->isKnownEnchant())
	markEnchantKnown();
    if (item->isKnownPoison())
	markPoisonKnown();
}

ITEM *
ITEM::splitStack(int count)
{
    UT_ASSERT(count > 0);
    UT_ASSERT(count < myStackCount);

    ITEM		*item;

    item = createClone();
    item->myStackCount = count;
    myStackCount -= count;

    return item;
}

void
ITEM::splitAndDeleteStack(int count)
{
    ITEM		*split;

    split = splitStack(count);
    delete split;
}

int
ITEM::getAC() const
{
    int		ac;

    ac = glb_itemdefs[myDefinition].ac;

    // Enchantments...  Only counts if it is armour
    if (isBoots() || isHelmet() || isJacket() || isShield() || isAmulet())
    {
	ac += getEnchantment();
    }

    // Add in the artifact bonus.
    const ARTIFACT		*art;

    art = getArtifact();
    if (art)
	ac += art->acbonus;
    
    return ac;
}

int
ITEM::getCoolness() const
{
    int		cool = 0;

    // Find coolness from magic type.
    switch (getMagicType())
    {
	case MAGICTYPE_RING:
	    cool += glb_ringdefs[getMagicClass()].coolness;
	    break;
	case MAGICTYPE_AMULET:
	    cool += glb_amuletdefs[getMagicClass()].coolness;
	    break;
	default:
	    break;
    }

    return cool;
}

int
ITEM::getWeight() const
{
    int		weight;

    weight = glb_itemdefs[myDefinition].weight;

    return weight;
}

int
ITEM::getCharges() const
{
    int		charges;

    charges = myCharges;
    return charges;
}

void
ITEM::addCharges(int add)
{
    int		charges;

    // Divide the charges among the number of items.
    add += getStackCount() / 2;
    add /= getStackCount();
    // Because I am nice, always restore one charge
    if (add < 1)
	add = 1;

    charges = myCharges;
    charges += add;

    if (charges < 0)
	charges = 0;
    if (charges > 99)
	charges = 99;

    myCharges = charges;
}

void
ITEM::useCharge()
{
    if (myCharges)
	myCharges--;
}

void
ITEM::setAsPreserved()
{
    myCharges = 255;
}

MAGICTYPE_NAMES
ITEM::getMagicType() const
{
    return (MAGICTYPE_NAMES) glb_itemdefs[myDefinition].magictype;
}

int
ITEM::getMagicClass() const
{
    return glb_magicitem[myDefinition];
}

void
ITEM::buildIntrinsicTable(INTRINSIC *&intrinsic) const
{
    if (glb_itemdefs[myDefinition].intrinsic[0])
    {
	if (!intrinsic)
	    intrinsic = new INTRINSIC;
	intrinsic->mergeFromString(glb_itemdefs[myDefinition].intrinsic);
    }

    // Try the artifact class.
    const ARTIFACT		*art;
    
    art = getArtifact();
    if (art && art->intrinsics)
    {
	if (!intrinsic)
	    intrinsic = new INTRINSIC;
	intrinsic->mergeFromString(art->intrinsics);
    }

    // Try the magic item type...
    switch ((MAGICTYPE_NAMES) getMagicType())
    {
	case MAGICTYPE_RING:
	    if (glb_ringdefs[getMagicClass()].intrinsic[0])
	    {
		if (!intrinsic)
		    intrinsic = new INTRINSIC;
		intrinsic->mergeFromString(
			glb_ringdefs[getMagicClass()].intrinsic);
	    }
	    break;
	case MAGICTYPE_HELM:
	    if (glb_helmdefs[getMagicClass()].intrinsic[0])
	    {
		if (!intrinsic)
		    intrinsic = new INTRINSIC;
		intrinsic->mergeFromString(
			glb_helmdefs[getMagicClass()].intrinsic);
	    }
	    break;
	case MAGICTYPE_BOOTS:
	    if (glb_bootsdefs[getMagicClass()].intrinsic[0])
	    {
		if (!intrinsic)
		    intrinsic = new INTRINSIC;
		intrinsic->mergeFromString(
			glb_bootsdefs[getMagicClass()].intrinsic);
	    }
	    break;
	case MAGICTYPE_AMULET:
	    if (glb_amuletdefs[getMagicClass()].intrinsic[0])
	    {
		if (!intrinsic)
		    intrinsic = new INTRINSIC;
		intrinsic->mergeFromString(
			glb_amuletdefs[getMagicClass()].intrinsic);
	    }
	    break;
	case MAGICTYPE_STAFF:
	    if (glb_staffdefs[getMagicClass()].intrinsic[0])
	    {
		if (!intrinsic)
		    intrinsic = new INTRINSIC;
		intrinsic->mergeFromString(
			glb_staffdefs[getMagicClass()].intrinsic);
	    }
	    break;
	case MAGICTYPE_NONE:
	case MAGICTYPE_POTION:
	case MAGICTYPE_SPELLBOOK:
	case MAGICTYPE_SCROLL:
	case MAGICTYPE_WAND:
	    break;

	case NUM_MAGICTYPES:
	    UT_ASSERT(!"Unhandled MAGICTYPE");
	    break;
    }
}

bool
ITEM::hasIntrinsic(INTRINSIC_NAMES intrinsic) const
{
    // Check base item type...
    if (INTRINSIC::hasIntrinsic(glb_itemdefs[myDefinition].intrinsic,
				intrinsic))
	return true;

    // Try the artifact class.
    const ARTIFACT		*art;
    
    art = getArtifact();
    if (art)
    {
	if (INTRINSIC::hasIntrinsic(art->intrinsics, intrinsic))
	    return true;
    }

    // Try the magic item type...
    switch ((MAGICTYPE_NAMES) getMagicType())
    {
	case MAGICTYPE_RING:
	{
	    if (INTRINSIC::hasIntrinsic(glb_ringdefs[getMagicClass()].intrinsic,
					intrinsic) )
		return true;
	    break;
	}
	case MAGICTYPE_HELM:
	{
	    if (INTRINSIC::hasIntrinsic(glb_helmdefs[getMagicClass()].intrinsic,
					intrinsic) )
		return true;
	    break;
	}
	case MAGICTYPE_BOOTS:
	{
	    if (INTRINSIC::hasIntrinsic(glb_bootsdefs[getMagicClass()].intrinsic,
					intrinsic) )
		return true;
	    break;
	}
	case MAGICTYPE_AMULET:
	{
	    if (INTRINSIC::hasIntrinsic(glb_amuletdefs[getMagicClass()].intrinsic,
					intrinsic) )
		return true;
	    break;
	}
	case MAGICTYPE_STAFF:
	{
	    if (INTRINSIC::hasIntrinsic(glb_staffdefs[getMagicClass()].intrinsic,
					intrinsic) )
		return true;
	    break;
	}
	case MAGICTYPE_NONE:
	case MAGICTYPE_POTION:
	case MAGICTYPE_SPELLBOOK:
	case MAGICTYPE_SCROLL:
	case MAGICTYPE_WAND:
	{
	    // These guys all have no built in intrinsics
	    return false;
	}

	case NUM_MAGICTYPES:
	{
	    UT_ASSERT(!"Unhandled MAGICTYPE");
	    return false;
	}
    }
    return false;
}

bool
ITEM::dissolve(MOB *dissolver, bool *interesting)
{
    if (interesting)
	*interesting = true;
    if (canDissolve())
    {
	formatAndReport(dissolver, "%U <dissolve> in the acid!");
	return true;
    }
    if (interesting)
	*interesting = false;

    return false;
}

bool
ITEM::canDissolve() const
{
    bool		dodissolve = false;
    MATERIAL_NAMES	material;

    material = getMaterial();
    dodissolve = glb_materialdefs[material].soluble;

    // If we have resistance to acid, we never dissolve
    // If we have vulnerability, we always dissolve.
    // If we have both, it is as normal.
    if (hasIntrinsic(INTRINSIC_RESISTACID))
    {
	if (!hasIntrinsic(INTRINSIC_VULNACID))
	{
	    dodissolve = false;
	}
    }
    else if (hasIntrinsic(INTRINSIC_VULNACID))
    {
	dodissolve = true;
    }

    // Check for quest flag.  We don't want to dissolve those!
    if (glb_itemdefs[getDefinition()].isquest)
	dodissolve = false;

    return dodissolve;
}

bool
ITEM::ignite(MOB *igniter, bool *interesting)
{
    if (interesting)
	*interesting = true;

    // If we dip a sword, it becomes a flaming sword.
    if (getDefinition() == ITEM_LONGSWORD)
    {
	formatAndReport(igniter, "%U <catch> on fire!");

	myDefinition = ITEM_FLAMESWORD;
	return false;
    }

    if (getDefinition() == ITEM_ICEMACE)
    {
	formatAndReport(igniter, "%U <return> to room temperature.");
	myDefinition = ITEM_MACE;
	return false;
    }

    if (getDefinition() == ITEM_ARROW)
    {
	formatAndReport(igniter, "%U <catch> on fire!");
	myDefinition = ITEM_FIREARROW;
	return false;
    }

    // If we dip a club, it becomes a torch.
    if (getDefinition() == ITEM_CLUB)
    {
	formatAndReport(igniter, "%U <catch> on fire!");

	myDefinition = ITEM_TORCH;
	// Set our shield flag.
	myFlag1 = (ITEMFLAG1_NAMES) 
	    (myFlag1 | glb_itemdefs[ITEM_TORCH].flag1);
	return false;
    }

    // Check for burning up
    if (canBurn())
    {
	formatAndReport(igniter, "%U <burn> up.");
	return true;
    }

    // Got here, nothing interesting happened.
    if (interesting)
	*interesting = false;
    return false;
}

bool
ITEM::electrify(int points, MOB *shocker, bool *interesting)
{
    if (interesting)
	*interesting = true;

    // Energize rapiers.
    if (getDefinition() == ITEM_RAPIER)
    {
	formatAndReport(shocker, "%U <be> filled with electricity!");

	myDefinition = ITEM_LIGHTNINGRAPIER;
	addCharges(points);
	clearChargesKnown();
	return false;
    }

    if (getDefinition() == ITEM_LIGHTNINGRAPIER)
    {
	formatAndReport(shocker, "%U <drink> deeply from the electric current!");

	addCharges(points*2);
	clearChargesKnown();
	return false;
    }

    // Got here, nothing interesting happened.
    if (interesting)
	*interesting = false;
    return false;
}

bool
ITEM::canBurn() const
{
    bool		doburn = false;
    MATERIAL_NAMES	material;

    material = getMaterial();
    doburn = glb_materialdefs[material].burnable;

    // If we have resistance to fire, we never burn
    // If we have vulnerability, we always burn.
    // If we have both, it is as normal.
    if (hasIntrinsic(INTRINSIC_RESISTFIRE))
    {
	if (!hasIntrinsic(INTRINSIC_VULNFIRE))
	{
	    doburn = false;
	}
    }
    else if (hasIntrinsic(INTRINSIC_VULNFIRE))
    {
	doburn = true;
    }

    // Check for quest flag.  We don't want to burn those!
    if (glb_itemdefs[getDefinition()].isquest)
	doburn = false;

    return doburn;
}

// This was factored out in White Rock BC.
bool
ITEM::douse(MOB *douser, bool *interesting)
{
    if (interesting)
	*interesting = true;

    // If the dipped item is a flaming sword, it goes out.
    if (getDefinition() == ITEM_FLAMESWORD)
    {
	formatAndReport(douser, "%U <be> extinguished.");
	myDefinition = ITEM_LONGSWORD;
    }
    else if (getDefinition() == ITEM_FIREARROW)
    {
	formatAndReport(douser, "%U <be> extinguished.");
	myDefinition = ITEM_ARROW;
    }
    // As do torches.
    else if (getDefinition() == ITEM_TORCH)
    {
	formatAndReport(douser, "%U <be> extinguished.");
	myDefinition = ITEM_CLUB;
	// Clear out the isshield flag.
	myFlag1 = (ITEMFLAG1_NAMES) 
	    (myFlag1 & ~glb_itemdefs[ITEM_TORCH].flag1);
    }
    // Empty bottles fill.  This may seem odd as smashing one bottle may
    // fill ten, but the same happens with dipping.
    else if (getDefinition() == ITEM_BOTTLE)
    {
	formatAndReport(douser, "%U <be> filled.");
	myDefinition = ITEM_WATER;
    }
    else
    {
	formatAndReport(douser, "%U <get> wet.");
	if (interesting)
	    *interesting = false;
    }

    // Water destroys nothing right now.
    return false;
}

bool
ITEM::actionDip(MOB *dipper, ITEM *dippee,
		ITEM *&newpotion, ITEM *&newitem)
{
    bool		consumed = false;
    bool		possible = false;
    BUF			buf;

    UT_ASSERT(dippee != this);
    // This should already be unlinked.
    UT_ASSERT(!getNext());
    UT_ASSERT(!dippee->getNext());

    newpotion = this;
    newitem = dippee;

    // Check for magic type potion...
    switch ((MAGICTYPE_NAMES) getMagicType())
    {
	case MAGICTYPE_POTION:
	{
	    switch ((POTION_NAMES) getMagicClass())
	    {
		case POTION_GREEKFIRE:
		{
		    bool interesting = false;

		    possible = true;
		    consumed = true;

		    buf = MOB::formatToString("%MU <M:dip> %IU into %U.",
				0, this, dipper, dippee);
		    dipper->reportMessage(buf);
		    if (dippee->ignite(dipper, &interesting))
		    {
			// Burned up...
			delete dippee;
			newitem = 0;
			newpotion = ITEM::create(ITEM_BOTTLE, false, true);
			// Id it...
			markIdentified();
			// Delete self
			delete this;

			break;
		    }
		    else if (interesting)
		    {
			// Something happened.
			newpotion = ITEM::create(ITEM_BOTTLE, false, true);
			// Id it...
			markIdentified();
			// Delete self
			delete this;

			break;
		    }
		    else
		    {
			dipper->formatAndReport("Nothing happens.");
		    }
		    break;
		}

		case POTION_HEAL:
		case POTION_BLIND:
		case POTION_SMOKE:
		    buf = MOB::formatToString("%MU <M:dip> %IU into %U.  Nothing happens.",
				0, this, dipper, dippee);
		    dipper->reportMessage(buf);
		    possible = true;
		    consumed = true;
		    break;

		case POTION_MANA:
		    buf = MOB::formatToString("%MU <M:dip> %IU into %U.",
				0, this, dipper, dippee);
		    dipper->reportMessage(buf);

		    if (dippee->getMagicType() == MAGICTYPE_WAND)
		    {
			// Dipping a wand into a mana potion recharges it.
			dipper->formatAndReport("%IU hungrily <I:drink> the liquid!", dippee);

			newpotion = ITEM::create(ITEM_BOTTLE, false, true);

			int		netnewcharges;

			netnewcharges = rand_dice(1, 
						  6 - isCursed()*3, 
						  isBlessed()*3);

			// Charge the wand.
			dippee->addCharges(netnewcharges);
			// New number of charges is not known.
			dippee->clearChargesKnown();

			// Id it...
			markIdentified();
			// Delete self
			delete this;

			possible = true;
			consumed = true;
		    }
		    else if (dippee->getMagicType() == MAGICTYPE_SPELLBOOK)
		    {
			// Dipping a spellbook into a mana potion recharges it.
			dipper->formatAndReport("The pages of %IU swiftly absorb the liquid!", dippee);

			newpotion = ITEM::create(ITEM_BOTTLE, false, true);

			int		netnewcharges;

			netnewcharges = rand_dice(1, 
						  3 - isCursed()*2, 
						  isBlessed());

			// Charge the spellbook.
			dippee->addCharges(netnewcharges);
			// New number of charges is not known.
			dippee->clearChargesKnown();

			// Id it...
			markIdentified();
			// Delete self
			delete this;

			possible = true;
			consumed = true;
		    }
		    else
		    {
			// Fail the dip.
			dipper->reportMessage("Nothing happens.");
			possible = true;
			consumed = true;
		    }
		    break;
		    
		case POTION_CURE:
		    if (dippee->isPoisoned())
		    {
			dippee->makePoisoned(POISON_NONE, 0);

			// If the user knew it was poisoned, they get
			// a message.  Otherwise, no visible effect.
			// Well, the potion is removed that is a bit
			// of a give away...
			if (dippee->isKnownPoison())
			{
			    buf = MOB::formatToString("%MU <M:dip> %IU into %U.",
					0, this, dipper, dippee);
			    dipper->reportMessage(buf);

			    buf = MOB::formatToString("The poison on %U is neutralized by %IU.",
				    0, dippee, 0, this);
			    dipper->reportMessage(buf);
			}
			else
			{
			    buf = MOB::formatToString("%MU <M:dip> %IU into %U.  Nothing happens.",
					0, this, dipper, dippee);
			    dipper->reportMessage(buf);
			}
			    
			newpotion = ITEM::create(ITEM_BOTTLE, false, true);

			// Id it...
			markIdentified();
			// Delete self
			delete this;

			possible = true;
			consumed = true;
		    }
		    else
		    {
			// Fail the dip.
			buf = MOB::formatToString("%MU <M:dip> %IU into %U.  Nothing happens.",
				    0, this, dipper, dippee);
			dipper->reportMessage(buf);
			possible = true;
			consumed = true;
		    }
		    break;

		case POTION_ENLIGHTENMENT:
		    if (dippee->isFullyIdentified())
		    {
			// No id, no effect.
			buf = MOB::formatToString("%MU <M:dip> %IU into %U.  Nothing happens.",
				    0, this, dipper, dippee);
			dipper->reportMessage(buf);
			possible = true;
			consumed = true;
		    }
		    else
		    {
			buf = MOB::formatToString("%MU <M:dip> %IU into %U.",
				    0, this, dipper, dippee);
			dipper->reportMessage(buf);

			// Identify the dippee.
			if (dipper->isAvatar())
			    dippee->markIdentified();
			dipper->formatAndReport("%U <gain> insight into the nature of %IU.", dippee);
			
			newpotion = ITEM::create(ITEM_BOTTLE, false, true);

			// Id it...
			markIdentified();
			// Delete self
			delete this;

			possible = true;
			consumed = true;
		    }
		    break;

		case POTION_POISON:
		{
		    POISON_NAMES		poison;
		    
		    buf = MOB::formatToString("%MU <M:dip> %IU into %U.",
				0, this, dipper, dippee);
		    dipper->reportMessage(buf);  

		    
		    if (dippee->hasIntrinsic(INTRINSIC_RESISTPOISON))
		    {
			// Fail the dip.
			dipper->reportMessage("Nothing happens.");
			possible = true;
			consumed = true;
			break;
		    }

		    buf.sprintf("A thick slime coats %s.",
			    dippee->getAccusative());
		    dipper->reportMessage(buf);  

		    if (isCursed())
			poison = POISON_MILD;
		    else if (isBlessed())
			poison = POISON_STRONG;
		    else
			poison = POISON_NORMAL;

		    dippee->makePoisoned(poison, rand_dice(1, 3, 0));
		    if (dipper == MOB::getAvatar())
			dippee->markPoisonKnown();

		    newpotion = ITEM::create(ITEM_BOTTLE, false, true);
		    // Id it...
		    markIdentified();
		    // Delete self
		    delete this;

		    possible = true;
		    consumed = true;
		    break;
		}

		case POTION_ACID:
		{
		    buf = MOB::formatToString("%MU <M:dip> %IU into %U.",
				0, this, dipper, dippee);
		    dipper->reportMessage(buf);
		    
		    if (dippee->dissolve(dipper))
		    {
			newitem = 0;
			newpotion = ITEM::create(ITEM_BOTTLE, false, true);
			// Id it...
			markIdentified();
			delete dippee;
			// Delete self
			delete this;
		    }
		    else
		    {
			dipper->reportMessage("Nothing happens.");
		    }
			    
		    possible = true;
		    consumed = true;
		    break;
		}

		case NUM_POTIONS:
		{
		    UT_ASSERT(0);
		    buf.sprintf("Unknown potion type %d", getMagicClass());
		    dipper->reportMessage(buf);
		    break;
		}
	    }
	    break;
	}

	    // Other magic types do nothing:
	case MAGICTYPE_NONE:
	case MAGICTYPE_HELM:
	case MAGICTYPE_BOOTS:
	case MAGICTYPE_WAND:
	case MAGICTYPE_SCROLL:
	case MAGICTYPE_AMULET:
	case MAGICTYPE_RING:
	case MAGICTYPE_SPELLBOOK:
	case MAGICTYPE_STAFF:
	case NUM_MAGICTYPES:
	    break;
    }

    // Check for specific items...
    // Only do this if previous checks failed.
    if (!consumed)
    {
	switch ((ITEM_NAMES) myDefinition)
	{
	    case ITEM_WATER:
	    {
		// If the potion is holy, make new guy holy.
		// If potion evil, make new guy evil.
		// If potion plain, just wet new guy.
		if (isCursed())
		{
		    dipper->formatAndReport("%IU <I:glow> black.", dippee);
		    dippee->makeCursed();
		    if (dipper->isAvatar())
			dippee->markCursedKnown();
		    possible = true;
		    consumed = true;
		    newpotion = ITEM::create(ITEM_BOTTLE, false, true);
		    delete this;
		}
		else if (isBlessed())
		{
		    dipper->formatAndReport("%IU <I:glow> blue.", dippee);
		    dippee->makeBlessed();
		    if (dipper->isAvatar())
			dippee->markCursedKnown();
		    possible = true;
		    consumed = true;
		    newpotion = ITEM::create(ITEM_BOTTLE, false, true);
		    delete this;
		}
		else
		{
		    bool		interesting;
		    if (dippee->douse(dipper, &interesting))
		    {
			newitem = 0;
			delete dippee;
			newpotion = ITEM::create(ITEM_BOTTLE, false, true);
			delete this;
		    }
		    else if (interesting)
		    {
			newpotion = ITEM::create(ITEM_BOTTLE, false, true);
			delete this;
		    }

		    possible = true;
		    consumed = true;
		}
	    }
	    default:
		break;
	}
    }

    if (!possible)
    {
	// No one managed to do anything...
	buf = MOB::formatToString("%MU cannot figure out how to dip %IU into %U.",
				0, this, dipper, dippee);
	dipper->reportMessage(buf);
    }

    return consumed;
}

bool
ITEM::actionZap(MOB *zapper, int dx, int dy, int dz)
{
    bool		consumed = false;
    bool		possible = false;
    bool		targetself = false;
    const char		*directionflavour = 0;
    bool		destroythis = false;
    bool		doid = false;
    bool		forceid = false;
    BUF			buf;
    // Zapper position.
    int			zx, zy;

    zx = zapper->getX();
    zy = zapper->getY();
    if (!dx && !dy && !dz)
    {
	targetself = true;
	directionflavour = zapper->getReflexive();
    }
    if (dz)
    {
	if (dz < 0)
	    directionflavour = "the floor";
	else
	    directionflavour = "the ceiling";
    }
    
    // Try the magic type.
    switch ((MAGICTYPE_NAMES)getMagicType())
    {
	case MAGICTYPE_WAND:
	{
	    // Print prequel message..
	    zapper->formatAndReport("%U <zap> %IU%B1%B2.",
		    this,
		    directionflavour ? " at " : "",
		    directionflavour ? directionflavour : "");
	    
	    // Determine if we have enough charges...
	    if (myCharges == 0)
	    {
		// Check for wrest-last-charge
		// 1 in 20 to be nice as it is much harder UI wise
		// than in Nethack.
		// I have since reduced this even farther as the goal
		// is to make it not a tactical decision.
		if (!rand_choice(7))
		{
		    zapper->formatAndReport("%U <wrest> one last charge.");
		    destroythis = true;
		}
		else
		{
		    zapper->reportMessage("Nothing happens.");
		    possible = true;
		    consumed = true;
		    // To be nice, we record the wand is out of charges.
		    markChargesKnown();
		    // EARLY EXIT:
		    break;
		}
	    }


	    // Getting here implies we successfully zapped.
	    if (myCharges)
		myCharges--;

	    if (!destroythis && !isKnownCharges())
	    {
		if (zapper->isAvatar())
		{
		    if (!rand_choice(10))
		    {
			markChargesKnown();
			buf = MOB::formatToString("%U <know> %r %Iu better.  ",
				zapper, 0, 0, this);
			// This is important enough to be broadcast.
			msg_announce(gram_capitalize(buf));
		    }
		}
	    }

	    // Check to see if it is cursed and fizzled on the user.
	    if (isCursed() && rand_chance(10))
	    {
		doid = false;
		// This is a pretty simple wand to do...
		zapper->formatAndReport("%IU <I:fizzle>.", this);
		if (zapper->isAvatar())
		{
		    // We now know it is cursed
		    markCursedKnown();
		}
		possible = true;
		consumed = true;
		break;
	    }

	    // Apply any of our artifact based powers.
	    if (isArtifact())
	    {
		const ARTIFACT	*art;
		MOB		*target;

		target = glbCurLevel->getMob(zx + dx,
					     zy + dy);
		if (target)
		{
		    art = getArtifact();

		    const char	*intrinsics;

		    intrinsics = art->intrinsics;
		    while (intrinsics && *intrinsics)
		    {
			target->setTimedIntrinsic(zapper,
				    (INTRINSIC_NAMES) *intrinsics,
				    rand_dice(3+isBlessed()-isCursed(), 50, 100));
			intrinsics++;
		    }
		}
	    }
	    
	    switch ((WAND_NAMES) getMagicClass())
	    {
		case WAND_NOTHING:
		{
		    doid = false;
		    // This is a pretty simple wand to do...
		    zapper->reportMessage("Nothing happens.");
		    possible = true;
		    consumed = true;
		    break;
		}
		case WAND_CREATEMONSTER:
		{
		    doid = false;
		    possible = true;
		    consumed = true;

		    if (glbCurLevel->getMob(zx + dx,
					    zy + dy))
		    {
			zapper->reportMessage("Nothing happens.");
			break;
		    }

		    MOB		*mob;

		    mob = MOB::createNPC(100);

		    if (!mob->canMove(zx + dx,
				      zy + dy,
				      true))
		    {
			zapper->reportMessage("Nothing happens.");
			delete mob;
			break;
		    }

		    mob->move(zx + dx,
			      zy + dy,
			      true);
		    glbCurLevel->registerMob(mob);

		    mob->formatAndReport("%U <appear> out of thin air!");

		    // We have a chance of getting a tamed creature
		    // based on the BUC of the wand.
		    int		tamechance = 5;
		    int		hostilechance = 0;
		    if (isBlessed())
			tamechance += 15;
		    if (isCursed())
		    {
			tamechance = 1;
			hostilechance = 20;
		    }

		    if (rand_chance(tamechance))
		    {
			mob->makeSlaveOf(zapper);
		    }
		    else if (rand_chance(hostilechance))
		    {
			// The creature turns on the zapper!
			mob->setAITarget(zapper);
		    }

		    // Imbue the created creature with our wands
		    // abilities.
		    if (isArtifact())
		    {
			const ARTIFACT	*art;
			art = getArtifact();

			const char	*intrinsics;

			intrinsics = art->intrinsics;
			while (intrinsics && *intrinsics)
			{
			    mob->setTimedIntrinsic(zapper,
					(INTRINSIC_NAMES) *intrinsics,
					rand_dice(3+isBlessed()-isCursed(), 50, 100));
			    intrinsics++;
			}
		    }

		    zapper->pietyZapWand(this);

		    doid = true;
		    break;
		}

		case WAND_CREATETRAP:
		{
		    possible = true;
		    consumed = true;
		    doid = glbCurLevel->createTrap(zx + dx,
						    zy + dy,
						    zapper);
		    if (doid)
		    {
			zapper->formatAndReport("%U <create> a hidden surprise.");
			zapper->pietyZapWand(this);
		    }
		    else
			zapper->reportMessage("Nothing happens.");
		    break;
		}
		case WAND_LIGHT:
		{
		    MOB		*target;
		    doid = true;
		    possible = true;
		    consumed = true;
		    ourZapper.setMob(zapper);
		    zapper->formatAndReport("A lit field surrounds %U.");
		    glbCurLevel->applyFlag(SQUAREFLAG_LIT,
			    zx + dx,
			    zy + dy,
			    5, false, true);
		    zapper->pietyZapWand(this);

		    // Blind the target, if any.
		    if (!dz)
		    {
			zapper = ourZapper.getMob();

			target = glbCurLevel->getMob(zx+dx, zy+dy);
			if (target)
			{
			    // Wands always apply damage...
			    target->receiveDamage(ATTACK_LIGHTWAND, 
				    zapper,
				    this, 0,
				    ATTACKSTYLE_WAND);
			}
		    }
		    
		    zapper = ourZapper.getMob();
		    break;
		}
		
		case WAND_TELEPORT:
		{
		    MOB		*tele = 0;
		    
		    possible = true;
		    consumed = true;

		    // If it went vertical, nothing happens..
		    if (!dz)
		    {
			tele = glbCurLevel->getMob(zx + dx,
					    zy + dy);
		    }
		    if (tele)
		    {
			// Teleport the monster
			ourZapper.setMob(zapper);
			tele->actionTeleport();
			zapper = ourZapper.getMob();
			if (zapper)
			    zapper->pietyZapWand(this);
			doid = true;
		    }
		    // Tracked down a bug here.
		    // Bug was: "TODO: Teleport items"
		    // :>
		    // Still in the airport lounge enjoying free beer.
		    // Fear this code!
		    // No doubt as a result of aforementioned free beer,
		    // I foolishly dereferenced zapper->getX() to get the
		    // tile location.  Zapper might be tele in which case
		    // it might very well be dead.  In any case, this
		    // would end up teleporting items at your destination
		    // square rather than source square!
		    ITEMSTACK		stack;
		    glbCurLevel->getItemStack(stack, zx + dx,
						     zy + dy);
		    if (stack.entries())
		    {
			ITEM			*item;
			
			doid = true;
			// We don't want to double count this zap.
			if (zapper && !tele)
			    zapper->pietyZapWand(this);

			for (int i = 0; i < stack.entries(); i++)
			{
			    item = stack(i);
			    item->randomTeleport(glbCurLevel, true, zapper);
			}
		    }

		    if (!doid && zapper)
		    {
			zapper->reportMessage("Nothing happens.");
		    }
		    break;
		}
		
		case WAND_POLYMORPH:
		{
		    MOB		*poly = 0;
		    ITEM	*item = 0;
		    
		    possible = true;
		    consumed = true;

		    // If it went vertical, nothing happens..
		    if (!dz)
		    {
			poly = glbCurLevel->getMob(zx + dx,
					    zy + dy);
		    }
		    if (poly)
		    {
			MOBREF		zapmobref;

			zapmobref.setMob(zapper);

			// Polymorph the monster
			// If the monster was us, zapper may now point
			// to the base object rather than the top object.
			// Thus we track the ref.
			poly->actionPolymorph();
			zapper = zapmobref.getMob();
			doid = true;
		    }
		    // Zapping the ground means polying on the ground.
		    // (doid here means did something)
		    if (dz <= 0 && !doid)
		    {
			item = glbCurLevel->getItem(zx + dx,
						zy + dy);
		    }
		    if (item)
		    {
			ITEM		*newitem;

			newitem = item->polymorph(zapper);
			
			if (newitem)
			{
			    if (item->getStackCount() > 1)
				item = item->splitStack(1);
			    else
				glbCurLevel->dropItem(item);

			    glbCurLevel->acquireItem(newitem,
						zx + dx,
						zy + dy,
						zapper);
			    delete item;
			}

			doid = true;
		    }
		    if (!doid && zapper)
		    {
			zapper->reportMessage("Nothing happens.");
		    }
		    else if (zapper)
		    {
			zapper->pietyZapWand(this);
		    }
		    break;
		}
		
		case WAND_INVISIBLE:
		{
		    MOB		*invis = 0;
		    bool	 getpiety = false;
		    
		    possible = true;
		    consumed = true;
		    

		    // If it went vertical, nothing happens..
		    if (!dz)
		    {
			invis = glbCurLevel->getMob(zx + dx,
					    zy + dy);
		    }
		    if (invis)
		    {
			// Invisible the monster
			// We notice something if the monster
			// is not already invisible.
			bool quietchange = false;
			if (!invis->hasIntrinsic(INTRINSIC_INVISIBLE))
			{
			    if (invis != MOB::getAvatar())
			    {
				invis->formatAndReport("%U <disappear> from sight.");
			    }
			    doid = true;

			    // We must determine if we can see the monster
			    // *before* we make it invisible.  If we wait
			    // for normal doid check, the monster may
			    // be invisible which prevents us from
			    // iding the wand.
			    if (zapper == MOB::getAvatar() ||
				(MOB::getAvatar() && 
				 MOB::getAvatar()->canSense(zapper)))
			    {
				forceid = true;
			    }
			}
			else
			{
			    quietchange = true;
			}
			invis->setTimedIntrinsic(zapper, 
				INTRINSIC_INVISIBLE,
				rand_dice(3+isBlessed()-isCursed(), 50, 100),
				quietchange);
			getpiety = true;
		    }
		    SQUARE_NAMES		tile;

		    tile = glbCurLevel->getTileSafe(zx+dx, zy+dy);

		    // Check to see if we can make a door invisible
		    switch (tile)
		    {
			// Open doors need to be closed first to avoid
			// this nerfing the knock spell.
			case SQUARE_DOOR:
			case SQUARE_BLOCKEDDOOR:
			    doid = true;
			    if (zapper == MOB::getAvatar() ||
				glbCurLevel->hasFOV(zx+dx, zy+dy))
			    {
				forceid = true;
			    }
			    glbCurLevel->reportMessage("The door fades into the wall.", zx+dx, zy+dy);
			    glbCurLevel->setTile(zx+dx, zy+dy, SQUARE_SECRETDOOR);
			    getpiety = true;
			    break;
			case SQUARE_FLOORSMOKEVENT:
			case SQUARE_PATHSMOKEVENT:
			case SQUARE_FLOORPOISONVENT:
			case SQUARE_PATHPOISONVENT:
			case SQUARE_FLOORTELEPORTER:
			case SQUARE_PATHTELEPORTER:
			case SQUARE_FLOORHOLE:
			case SQUARE_PATHHOLE:
			case SQUARE_FLOORPIT:
			case SQUARE_PATHPIT:
			case SQUARE_FLOORSPIKEDPIT:
			case SQUARE_PATHSPIKEDPIT:
			{
			    BUF		buf;

			    doid = true;
			    if (zapper == MOB::getAvatar() ||
				glbCurLevel->hasFOV(zx+dx, zy+dy))
			    {
				forceid = true;
			    }
			    
			    buf.sprintf("%s fades into the ground.",
					glb_squaredefs[tile].description);
			    glbCurLevel->reportMessage(buf, zx+dx, zy+dy);
			    glbCurLevel->setTile(zx+dx, zy+dy, (SQUARE_NAMES) glb_squaredefs[tile].invissquare);
			    getpiety = true;
			    break;
			}

			default:
			    // Do nothing
			    break;
		    }
		    if (!doid)
		    {
			zapper->reportMessage("Nothing happens.");
		    }
		    if (getpiety)
			zapper->pietyZapWand(this);
		    break;
		}

		case WAND_SPEED:
		{
		    MOB		*speed = 0;
		    
		    possible = true;
		    consumed = true;

		    // If it went vertical, nothing happens..
		    if (!dz)
		    {
			speed = glbCurLevel->getMob(zx + dx,
					    zy + dy);
		    }
		    if (speed)
		    {
			// Quicken the monster
			// We notice something if the monster
			// is not already quick.
			if (!speed->hasIntrinsic(INTRINSIC_QUICK))
			{
			    doid = true;
			}
			speed->setTimedIntrinsic(zapper, 
				INTRINSIC_QUICK,
				rand_dice(3+isBlessed()-isCursed(), 50, 100));
			zapper->pietyZapWand(this);
		    }
		    if (!doid)
		    {
			zapper->reportMessage("Nothing happens.");
		    }
		    break;
		}

		case WAND_SLOW:
		{
		    MOB		*slow = 0;
		    
		    possible = true;
		    consumed = true;

		    // If it went vertical, nothing happens..
		    if (!dz)
		    {
			slow = glbCurLevel->getMob(zx + dx,
					    zy + dy);
		    }
		    if (slow)
		    {
			// Quicken the monster
			// We notice something if the monster
			// is not already quick.
			if (!slow->hasIntrinsic(INTRINSIC_SLOW))
			{
			    doid = true;
			}
			slow->setTimedIntrinsic(zapper, 
				INTRINSIC_SLOW,
				rand_dice(3+isBlessed()-isCursed(), 50, 100));
			zapper->pietyZapWand(this);
		    }
		    if (!doid)
		    {
			zapper->reportMessage("Nothing happens.");
		    }
		    break;
		}

		case WAND_FIRE:
		case WAND_ICE:
		{
		    possible = true;
		    consumed = true;
		    doid = true;		// This is pretty obvious.

		    ourZapper.setMob(zapper);
		    zapper->pietyZapWand(this);
		    if (targetself || dz)
		    {
			bool		dosquareeffect = false;

			// While underwater, always works.
			if (zapper->hasIntrinsic(INTRINSIC_SUBMERGED))
			    dosquareeffect = true;
			
			if (dz && !dosquareeffect)
			{
			    // Shot downwards? affect square
			    if (dz < 0)
				dosquareeffect = true;
			    zapper->reportMessage("The ray bounces.");
			}
			// Affect the square.
			if (dosquareeffect && 
			    (WAND_NAMES) getMagicClass() == WAND_ICE)
			{
			    glbCurLevel->freezeSquare(zx, zy, zapper);
			}
			if (dosquareeffect && 
			    (WAND_NAMES) getMagicClass() == WAND_FIRE)
			{
			    glbCurLevel->burnSquare(zx, zy, zapper);
			}
			// Apply to self..
			zapCallback(zx, zy);
		    }
		    else
		    {
			glbCurLevel->fireRay(zx, zy,
				dx, dy,
				6,
				MOVE_STD_FLY, true,
				zapCallbackStatic,
				this);

		    }
		    zapper = ourZapper.getMob();
		    break;
		}
		
		case WAND_SLEEP:
		{
		    possible = true;
		    consumed = true;
		    doid = true;		// This is pretty obvious.

		    ourZapper.setMob(zapper);
		    zapper->pietyZapWand(this);
		    if (targetself || dz)
		    {
			if (dz)
			{
			    zapper->reportMessage("The ray bounces.");
			}

			// Apply to self..
			zapCallback(zx, zy);
		    }
		    else
		    {
			glbCurLevel->fireRay(zx, zy,
				dx, dy,
				6,
				MOVE_STD_FLY, true,
				zapCallbackStatic,
				this);

		    }
		    zapper = ourZapper.getMob();
		    break;
		}

		case WAND_DIGGING:
		{
		    possible = true;
		    consumed = true;
		    doid = true;		// Wands of digging autoid.

		    ourZapper.setMob(zapper);
		    zapper->pietyZapWand(this);
		    zapper->actionDig(dx, dy, dz, 6, true);
		    zapper = ourZapper.getMob();

		    break;
		}

		case NUM_WANDS:
		{
		    UT_ASSERT(!"Unhandled wand type!");
		    break;
		}
	    }
	}

	// Other MAGICTYPES that, of course, do nothing...
	case MAGICTYPE_NONE:
	case MAGICTYPE_POTION:
	case MAGICTYPE_SPELLBOOK:
	case MAGICTYPE_SCROLL:
	case MAGICTYPE_RING:
	case MAGICTYPE_HELM:
	case MAGICTYPE_BOOTS:
	case MAGICTYPE_AMULET:
	case MAGICTYPE_STAFF:
	case NUM_MAGICTYPES:
	    break;
    }

    // Try the definition.
    if (!possible)
    {
	switch ((ITEM_NAMES) myDefinition)
	{
	    case ITEM_LIGHTNINGRAPIER:
	    {
		possible = true;
		consumed = true;
		doid = false;		// Already ided.

		ourZapper.setMob(zapper);
		// Rapiers still count as wands in terms of piety.
		zapper->pietyZapWand(this);

		zapper = ourZapper.getMob();

		// Electrocution by lighting rapiers
		// only occurs if fired while submerged,
		// not if merely fired at the ground.
		if (zapper && zapper->hasIntrinsic(INTRINSIC_SUBMERGED))
		{
		    // Drain our charges..
		    zapper->formatAndReport("%R %Iu violently <I:discharge>.", this);
		    myCharges = 0;
		    setDefinition(ITEM_RAPIER);
		    zapper->rebuildAppearance();
		    // Affect the square.
		    glbCurLevel->electrocuteSquare(zx, zy, zapper);

		    // All done, your targetting has no effect
		    break;
		}
		zapper = ourZapper.getMob();

		// Getting here implies we successfully zapped.
		if (myCharges > 10)
		    myCharges -= 10;
		else
		    myCharges = 0;
		if (!myCharges)
		{
		    if (zapper)
			zapper->formatAndReport("%R %Iu <I:power> down.", this);
		    setDefinition(ITEM_RAPIER);
		    if (zapper)
			zapper->rebuildAppearance();
		}

		if (myCharges && !isKnownCharges())
		{
		    if (zapper && zapper->isAvatar())
		    {
			if (!rand_choice(10))
			{
			    markChargesKnown();
			    buf = MOB::formatToString("%U <know> %r %Iu better.  ",
				    zapper, 0, 0, this);
			    // This is important enough to be broadcast.
			    msg_announce(gram_capitalize(buf));
			}
		    }
		}
		if (targetself || dz)
		{
		    if (dz && zapper)
		    {
			zapper->reportMessage("The ray bounces.");
		    }
		    // Apply to self..
		    zapCallback(zx, zy);
		}
		else
		{
		    glbCurLevel->fireRay(zx, zy,
			    dx, dy,
			    6,
			    MOVE_STD_FLY, true,
			    zapCallbackStatic,
			    this);

		}
		zapper = ourZapper.getMob();
		break;
	    }
	    default:
		// Nothing...
		break;
	}
    }

    // Check if zapper is dead.
    if (zapper)
    {
	if (zapper->hasIntrinsic(INTRINSIC_DEAD))
	{
	    zapper = 0;
	}
    }

    if (!possible && zapper)
    {
	// Noone figured out how to zap it.
	zapper->formatAndReport("%U cannot figure out how to zap %IU.",
				this);
    }

    // Check if we should id this...
    // Ideally you could id a wand by a creature killing itself with it,
    // but that makes the canSense call rather difficult.
    if (doid && zapper)
    {
	if (forceid ||
	    zapper == MOB::getAvatar() ||
	    (MOB::getAvatar() && MOB::getAvatar()->canSense(zapper)))
	{
	    // We only id type, not the particulars!
	    markClassKnown();
	}
    }

    // If we are to destroy this, do so...
    // Wands that crumble to dust with the same charge that kills their
    // owner get a saving throw.
    // (By saving throw, I mean that they don't get destroyed :>)
    if (destroythis && zapper)
    {
	ITEM		*drop;
	
	zapper->formatAndReport("%IU <I:crumble> to dust.", this);
	
	drop = zapper->dropItem(getX(), getY());
	UT_ASSERT(drop == this);
	delete this;
    }

    return consumed;
}

bool
ITEM::grenadeCallbackStatic(int x, int y, bool final, void *data)
{
    return ((ITEM *)data)->grenadeCallback(x, y);
}

bool
ITEM::grenadeCallback(int x, int y)
{
    MOB		*victim;
    BUF		 buf;
    
    // Get the mob at this location.
    victim = glbCurLevel->getMob(x, y);

    // Magic potions..
    if (getMagicType() == MAGICTYPE_POTION)
    {
	POTION_NAMES		potion;

	potion = (POTION_NAMES) getMagicClass();

	switch (potion)
	{
	    case POTION_HEAL:
	    {
		if (!victim)
		    break;

		int		heal;
		
		if (isBlessed())
		    heal = rand_dice(1, 10, 10);
		else if (isCursed())
		    heal = rand_dice(1, 10, 0);
		else
		    heal = rand_dice(1, 20, 0);

		if (victim->receiveHeal(heal, ourZapper.getMob()))
		{
		    victim->formatAndReport("%U <look> healthier.");

		    if (victim->isAvatar() || 
			(MOB::getAvatar() && 
			 MOB::getAvatar()->canSense(victim)))
			markIdentified();
		}
		break;
	    }

	    case POTION_MANA:
	    {
		if (!victim)
		    break;

		int		magic;
		
		if (isBlessed())
		    magic = rand_dice(1, 20, 20);
		else if (isCursed())
		    magic = rand_dice(1, 20, 0);
		else
		    magic = rand_dice(1, 40, 0);

		if (victim->receiveMana(magic, ourZapper.getMob()))
		{
		    victim->formatAndReport("%U <look> recharged.");

		    if (victim->isAvatar() || 
			(MOB::getAvatar() && 
			 MOB::getAvatar()->canSense(victim)))
			markIdentified();
		}
		break;
	    }

	    case POTION_ACID:
	    {
		// Check to see if we upconvert smoke into acid.
		if (glbCurLevel->getSmoke(x, y) == SMOKE_SMOKE)
		{
		    glbCurLevel->reportMessage("The smoke is acidified.", x, y);
		    if (MOB::getAvatar() && 
			glbCurLevel->hasLOS(MOB::getAvatar()->getX(),
				    MOB::getAvatar()->getY(),
				    x, y))
		    {
			markIdentified();
		    }
		    glbCurLevel->setSmoke(x, y, SMOKE_ACID, ourZapper.getMob());
		}
		if (!victim)
		    break;

		// Do identify if the avatar can sense this square.
		if (glbCurLevel->hasFOV(x, y))
		    markIdentified();

		victim->receiveDamage(ATTACK_ACIDPOTION, ourZapper.getMob(), this, 0,
					ATTACKSTYLE_MISC);
		break;
	    }

	    case POTION_GREEKFIRE:
	    {
		// Check for burning squares.  We identify if the
		// avatar witnesses this.
		if (glbCurLevel->burnSquare(x, y, ourZapper.getMob()))
		{
		    if (glbCurLevel->hasFOV(x, y))
			markIdentified();
		}
		
		// Burning the square may kill our victim,
		// so we refetch.
		victim = glbCurLevel->getMob(x, y);
		if (!victim)
		    break;

		// Do identify if the avatar can sense this square.
		if (glbCurLevel->hasFOV(x, y))
		    markIdentified();

		victim->receiveDamage(ATTACK_GREEKFIREPOTION, 
				      ourZapper.getMob(), this, 0,
				      ATTACKSTYLE_MISC);
		break;
	    }

	    case POTION_SMOKE:
	    {
		// We can only put smoke where things can fly.
		if (!(glb_squaredefs[glbCurLevel->getTile(x, y)].movetype &
		      MOVE_STD_FLY))
		    break;
		
		// Do identify if the avatar can sense this.
		if (glbCurLevel->hasFOV(x, y))
		    markIdentified();

		// Make this square smokey.
		// If the smoke potion is poisoned, act accordinly
		if (isPoisoned())
		    glbCurLevel->setSmoke(x, y, SMOKE_POISON, ourZapper.getMob());
		else
		    glbCurLevel->setSmoke(x, y, SMOKE_SMOKE, ourZapper.getMob());
		break;
	    }

	    case POTION_BLIND:
	    {
		if (!victim)
		    break;

		int		turns;

		// You get 3d20 turns of blindness.
		if (isBlessed())
		    turns = rand_dice(3, 10, 30);
		else if (isCursed())
		    turns = rand_dice(3, 10, 0);
		else
		    turns = rand_dice(3, 20, 0);

		// Determine if we notice anything...
		if (victim->isAvatar() ||
		    (MOB::getAvatar() && 
		     MOB::getAvatar()->canSense(victim)))
		{
		    markIdentified();
		}

		victim->setTimedIntrinsic(ourZapper.getMob(), 
					    INTRINSIC_BLIND, turns);
		break;
	    }

	    case POTION_POISON:
	    {
		// Check to see if we upconvert smoke into poison.
		if (glbCurLevel->getSmoke(x, y) == SMOKE_SMOKE)
		{
		    glbCurLevel->reportMessage("The smoke is poisoned.", x, y);
		    if (MOB::getAvatar() && 
			glbCurLevel->hasLOS(MOB::getAvatar()->getX(),
				    MOB::getAvatar()->getY(),
				    x, y))
		    {
			markIdentified();
		    }
		    glbCurLevel->setSmoke(x, y, SMOKE_POISON, ourZapper.getMob());
		}

		if (!victim)
		    break;

		int			turns;
		POISON_NAMES	poison;

		turns = rand_dice(4, 5, 0);
		if (isBlessed())
		    poison = POISON_STRONG;
		else if (isCursed())
		    poison = POISON_MILD;
		else
		    poison = POISON_NORMAL;

		victim->setTimedIntrinsic(ourZapper.getMob(), (INTRINSIC_NAMES)
				  glb_poisondefs[poison].intrinsic, 
				  turns);
		// The avatar is exempt as the permament intrinsic
		// will duplicate this.
		if (!victim->isAvatar())
		{
		    victim->formatAndReport("%U <be> poisoned.");
		}
		
		// Determine if we notice anything...
		if (victim->isAvatar() ||
		    (MOB::getAvatar() && 
		     MOB::getAvatar()->canSense(victim)))
		{
		    markIdentified();
		}

		break;
	    }

	    case POTION_CURE:
	    {
		// Check to see if we downconvert poison smoke into smoke.
		if (glbCurLevel->getSmoke(x, y) == SMOKE_POISON)
		{
		    glbCurLevel->reportMessage("The poisoned smoke is neutralized.", x, y);
		    if (MOB::getAvatar() && 
			glbCurLevel->hasLOS(MOB::getAvatar()->getX(),
				    MOB::getAvatar()->getY(),
				    x, y))
		    {
			markIdentified();
		    }

		    glbCurLevel->setSmoke(x, y, SMOKE_SMOKE, ourZapper.getMob());
		}

		if (!victim)
		    break;

		if (victim->receiveCure())
		{
		    victim->formatAndReport(
			    "The poison is expunged from %R veins.");
		    
		    if (victim->isAvatar() || 
			(MOB::getAvatar() && 
			 MOB::getAvatar()->canSense(victim)))
			markIdentified();
		}
		break;
	    }

	    case POTION_ENLIGHTENMENT:
	    {
		if (!victim)
		    break;

		glbShowIntrinsic(victim);
		markIdentified();
		break;
	    }

	    case NUM_POTIONS:
		UT_ASSERT(!"Unknown potion class!");
		break;
	}
    }
    else
    {
	// Regular stuff...
	switch (getDefinition())
	{
	    case ITEM_WATER:
	    {
		glbCurLevel->douseSquare(x, y, isBlessed(), ourZapper.getMob());

		break;
	    }
	    case ITEM_BOTTLE:
	    {
		if (!victim)
		    break;

		// Do normal piercing damage
		victim->receiveDamage(ATTACK_GLASSFRAGMENTS, ourZapper.getMob(), this,
					0, ATTACKSTYLE_THROWN);
		break;
	    }

	    default:
		// Do nothing!
		// Not handled currently.
		UT_ASSERT(!"Unknown grenade!");
		break;
	}
    }

    return true;
}

bool
ITEM::zapCallbackStatic(int x, int y, bool final, void *data)
{
    if (final) return false;
    return ((ITEM *)data)->zapCallback(x, y);
}

bool
ITEM::zapCallback(int x, int y)
{
    MOB			*mob;
    ATTACK_NAMES	 attack = ATTACK_NONE;

    mob = glbCurLevel->getMob(x, y);
    if (mob)
    {
	if (getMagicType() == MAGICTYPE_WAND)
	{
	    switch ((WAND_NAMES) getMagicClass())
	    {
		case WAND_FIRE:
		    attack = ATTACK_FIREWAND;
		    break;
		case WAND_ICE:
		    attack = ATTACK_ICEWAND;
		    break;
		case WAND_DIGGING:
		    if (mob->getDefinition() == MOB_EARTHELEMENTAL)
		    {
			attack = ATTACK_DIGEARTHELEMENTAL;
		    }
		    break;
		case WAND_SLEEP:
		    attack = ATTACK_SLEEPWAND;
		    break;
		default:
		    break;
	    }
	}
	switch (getDefinition())
	{
	    case ITEM_LIGHTNINGRAPIER:
		// note it is very important that this can't charge lightning
		// rapiers!
		attack = ATTACK_ZAPLIGHTNINGRAPIER;
		break;
	    default:
		// No, I am not going to list all the items here to take
		// advantage of enumeration warnings.
		break;
	}
	if (attack != ATTACK_NONE)
	{
	    mob->receiveDamage(attack, ourZapper.getMob(), this,
				0, ATTACKSTYLE_WAND);
	}
    }

    // Special square related stuff...
    if (getMagicType() == MAGICTYPE_WAND && getMagicClass() == WAND_DIGGING)
    {
	SQUARE_NAMES		tile;

	tile = (SQUARE_NAMES) glbCurLevel->getTile(x, y);
	switch (tile)
	{
	    case SQUARE_EMPTY:
	    case SQUARE_WALL:
	    {
		glbCurLevel->setTile(x, y, SQUARE_CORRIDOR);
		break;
	    }

	    case SQUARE_DOOR:
	    case SQUARE_BLOCKEDDOOR:
	    case SQUARE_SECRETDOOR:
	    {
		// Can't tunnel through these!
		return false;
	    }

	    default:
		break;
	}
    }
    
    if (getMagicType() == MAGICTYPE_WAND && getMagicClass() == WAND_ICE)
    {
	// On successful freeze, fizzle out wand.
	if (glbCurLevel->freezeSquare(x, y, ourZapper.getMob()))
	    return false;
    }
    
    if (getMagicType() == MAGICTYPE_WAND && getMagicClass() == WAND_FIRE)
    {
	// Fire wands keep burning :>
	glbCurLevel->burnSquare(x, y, ourZapper.getMob());
    }
    
    return true;
}

bool
ITEM::randomTeleport(MAP *map, bool mapownsitem, MOB *teleporter)
{
    // Check for tele fixed items.
    if (hasIntrinsic(INTRINSIC_TELEFIXED))
    {
	formatAndReport("%U <shudder>.");
	return false;
    }
    bool teleported = false;
    int x, y;
    if (map->findRandomLoc(x, y, MOVE_WALK,
				true,
				false, false,
				false, false,
				false))
    {
	// Teleport the item
	formatAndReport("%U <teleport>.");
	// We lose our mapping information.
	markMapped(false);
	if (mapownsitem)
	    map->dropItem(this);
	// This ensures our item maps are updated
	// and it drops in lava, etc.
	map->acquireItem(this, x, y, teleporter);
	teleported = true;
    }
    return teleported;
}

bool
ITEM::fallInHole(MAP *curLevel, bool mapownsitem, MOB *dropper)
{
    MAP		*nextmap;
    int		 x, y;

    nextmap = curLevel->getMapDown();
    if (!nextmap)
    {
	formatAndReport("%R fall <be> stopped by an invisible barrier.");
	return false;
    }

    if (!nextmap->findRandomLoc(x, y, MOVE_WALK,
				true,
				false, false,
				false, false,
				false))
    {
	formatAndReport("%U <land> on the edge of a hole.");
	return false;
    }

    formatAndReport("%U <fall> into a hole.");

    // We no longer know where it is
    markMapped(false);

    if (mapownsitem)
	curLevel->dropItem(this);
    nextmap->acquireItem(this, x, y, dropper);

    return true;
}

ITEM *
ITEM::load(SRAMSTREAM &is)
{
    ITEM	*me;
    int		 val;

    me = new ITEM();
    
    is.uread(val, 8);
    me->myDefinition = val;
    me->myName.load(is);
    is.uread(val, 8);
    val ^= 1;
    me->myStackCount = val;
    is.uread(val, 8);
    me->myX = val;
    is.uread(val, 8);
    me->myY = val;
    is.read(val, 8);
    me->myEnchantment = val;
    is.uread(val, 8);
    me->myCharges = val;
    is.uread(val, 8);
    me->myPoison = val;
    is.uread(val, 8);
    me->myPoisonCharges = val;
    is.uread(val, 32);
    val ^= glb_itemdefs[me->myDefinition].flag1;
    me->myFlag1 = (ITEMFLAG1_NAMES) val;

    // Read in the corpse mob.
    me->myCorpseMob.load(is);
    if (!me->myCorpseMob.isNull())
    {
	MOB		*mob;

	mob = MOB::load(is);
	// myCorpseMob will already point to mob, but might as well
	// assert.
	UT_ASSERT(mob == me->myCorpseMob.getMob());
    }

    return me;
}

void
ITEM::save(SRAMSTREAM &os)
{
    os.write(myDefinition, 8);
    myName.save(os);
    os.write(myStackCount ^ 1, 8);
    os.write(myX, 8);
    os.write(myY, 8);
    os.write(myEnchantment, 8);
    os.write(myCharges, 8);
    os.write(myPoison, 8);
    os.write(myPoisonCharges, 8);

    os.write(myFlag1 ^ glb_itemdefs[myDefinition].flag1, 32);

    // Save our corpse mob.
    // If it is non-zero, we also will save the mob itself.
    MOB		*mob;
    mob = myCorpseMob.getMob();

    myCorpseMob.save(os);
    if (mob)
	mob->save(os);

    // TODO: Save the name???
}

bool
ITEM::verifyMob() const
{
    MOB		*mob;
    mob = myCorpseMob.getMob();
    if (mob)
	return mob->verifyMob();
    return true;
}

bool
ITEM::verifyCounterGone(INTRINSIC_COUNTER *counter) const
{
    MOB		*mob;
    mob = myCorpseMob.getMob();
    if (mob)
	return mob->verifyCounterGone(counter);
    return true;
}

void
ITEM::loadGlobal(SRAMSTREAM &is)
{
    int		i, val;

    for (i = 0; i < NUM_ITEMS; i++)
    {
	is.uread(val, 8);
	glb_magicitem[i] = val;
    }

    for (i = 0; i < NUM_ITEMS; i++)
    {
	is.uread(val, 8);
	glb_itemid[i] = val ? true : false;
    }

    for (i = 0; i < NUM_ITEMS; i++)
    {
	char	buf[100];
	is.readString(buf, 100);
	if (glb_itemnames[i])
	    free(glb_itemnames[i]);
	if (buf[0])
	    glb_itemnames[i] = strdup(buf);
	else
	    glb_itemnames[i] = 0;
    }
}

void
ITEM::saveGlobal(SRAMSTREAM &is)
{
    int		i, val;

    for (i = 0; i < NUM_ITEMS; i++)
    {
	val = glb_magicitem[i];
	is.write(val, 8);
    }

    for (i = 0; i < NUM_ITEMS; i++)
    {
	val = glb_itemid[i];
	is.write(val, 8);
    }
    for (i = 0; i < NUM_ITEMS; i++)
    {
	is.writeString(glb_itemnames[i], 100);
    }
}