summaryrefslogtreecommitdiff
path: root/src/traps.cc
blob: 0b423ec69c3fa35dee58859cf71d45c23e5f0416 (plain)
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
/* the below copyright probably still applies, but it is heavily changed
 * copied, adapted & re-engineered by JK.
 * Copyright (c) 1989 James E. Wilson, Robert A. Koeneke
 *
 * This software may be copied and distributed for educational, research, and
 * not for profit purposes provided that this copyright and statement are
 * included in all such copies.
 */

#include "angband.h"
#include "gods.hpp"
#include "xtra1.hpp"

bool_ do_player_trap_call_out(void)
{
	s16b i, sn, cx, cy;
	s16b h_index = 0;
	s16b h_level = 0;
	monster_type *m_ptr;
	char m_name[80];
	bool_ ident = FALSE;

	for (i = 1; i < m_max; i++)
	{
		m_ptr = &m_list[i];

		/* Paranoia -- Skip dead monsters */
		if (!m_ptr->r_idx) continue;

		if (m_ptr->level >= h_level)
		{
			h_level = m_ptr->level;
			h_index = i;
		}
	}

	/* if the level is empty of monsters, h_index will be 0 */
	if (!h_index) return (FALSE);

	m_ptr = &m_list[h_index];

	sn = 0;
	for (i = 0; i < 8; i++)
	{
		cx = p_ptr->px + ddx[i];
		cy = p_ptr->py + ddy[i];

		/* Skip non-empty grids */
		if (!cave_valid_bold(cy, cx)) continue;
		if (cave[cy][cx].feat == FEAT_GLYPH) continue;
		if ((cx == p_ptr->px) && (cy == p_ptr->py)) continue;
		sn++;

		/* Randomize choice */
		if (rand_int(sn) > 0) continue;
		cave[cy][cx].m_idx = h_index;
		cave[m_ptr->fy][m_ptr->fx].m_idx = 0;
		m_ptr->fx = cx;
		m_ptr->fy = cy;

		/* we do not change the sublevel! */
		ident = TRUE;
		update_mon(h_index, TRUE);
		monster_desc(m_name, m_ptr, 0x08);
		msg_format("You hear a rapid-shifting wail, and %s appears!", m_name);
		break;
	}

	return (ident);
}

static bool_ do_trap_teleport_away(object_type *i_ptr, s16b y, s16b x)
{
	bool_ ident = FALSE;
	char o_name[80];

	s16b o_idx = 0;
	object_type *o_ptr;
	cave_type *c_ptr;

	s16b x1;
	s16b y1;

	if (i_ptr == NULL) return (FALSE);

	if (i_ptr->name1 == ART_POWER) return (FALSE);

	while (o_idx == 0)
	{
		x1 = rand_int(cur_wid);
		y1 = rand_int(cur_hgt);

		/* Obtain grid */
		c_ptr = &cave[y1][x1];

		/* Require floor space (or shallow terrain) -KMW- */
		if (!(f_info[c_ptr->feat].flags1 & FF1_FLOOR)) continue;

		o_idx = drop_near(i_ptr, 0, y1, x1);
	}

	o_ptr = &o_list[o_idx];

	x1 = o_ptr->ix;
	y1 = o_ptr->iy;

	if (!p_ptr->blind)
	{
		note_spot(y, x);
		lite_spot(y, x);
		ident = TRUE;
		object_desc(o_name, i_ptr, FALSE, 0);
		if (player_has_los_bold(y1, x1))
		{
			lite_spot(y1, x1);
			msg_format("The %s suddenly stands elsewhere.", o_name);

		}
		else
		{
			msg_format("You suddenly don't see the %s any more!", o_name);
		}
	}
	else
	{
		msg_print("You hear something move.");
	}
	return (ident);
}

/*
 * this handles a trap that places walls around the player
 */
static bool_ player_handle_trap_of_walls(void)
{
	bool_ ident;

	s16b dx, dy, cx, cy;
	s16b sx = 0, sy = 0, sn, i;
	cave_type *cv_ptr;
	bool_ map[5][5] =
	        {
	                {FALSE, FALSE, FALSE, FALSE, FALSE},
	                {FALSE, FALSE, FALSE, FALSE, FALSE},
	                {FALSE, FALSE, FALSE, FALSE, FALSE},
	                {FALSE, FALSE, FALSE, FALSE, FALSE},
	                {FALSE, FALSE, FALSE, FALSE, FALSE}
	        };

	for (dy = -2; dy <= 2; dy++)
		for (dx = -2; dx <= 2; dx++)
		{
			/* Extract the location */
			cx = p_ptr->px + dx;
			cy = p_ptr->py + dy;

			if (!in_bounds(cy, cx)) continue;

			cv_ptr = &cave[cy][cx];

			if (cv_ptr->m_idx) continue;

			/* Lose room and vault */
			cv_ptr->info &= ~(CAVE_ROOM | CAVE_ICKY);
			/* Lose light and knowledge */
			cv_ptr->info &= ~(CAVE_GLOW | CAVE_MARK);

			/* Skip the center */
			if (!dx && !dy) continue;

			/* test for dungeon level */
			if (randint(100) > 10 + max_dlv[dungeon_type]) continue;

			/* Damage this grid */
			map[2 + dx][2 + dy] = TRUE;
		}

	for (dy = -2; dy <= 2; dy++)
		for (dx = -2; dx <= 2; dx++)
		{
			/* Extract the location */
			cx = p_ptr->px + dx;
			cy = p_ptr->py + dy;

			/* Skip unaffected grids */
			if (!map[2 + dx][2 + dy]) continue;

			cv_ptr = &cave[cy][cx];

			if (cv_ptr->m_idx)
			{
				monster_type *m_ptr = &m_list[cv_ptr->m_idx];
				monster_race *r_ptr = race_inf(m_ptr);

				/* Most monsters cannot co-exist with rock */
				if ((!(r_ptr->flags2 & RF2_KILL_WALL)) &&
				                (!(r_ptr->flags2 & RF2_PASS_WALL)))
				{
					char m_name[80];

					/* Assume not safe */
					sn = 0;

					/* Monster can move to escape the wall */
					if (!(r_ptr->flags1 & RF1_NEVER_MOVE))
					{
						/* Look for safety */
						for (i = 0; i < 8; i++)
						{
							/* Access the grid */
							cy = p_ptr->py + ddy[i];
							cx = p_ptr->px + ddx[i];

							/* Skip non-empty grids */
							if (!cave_clean_bold(cy, cx)) continue;

							/* Hack -- no safety on glyph of warding */
							if (cave[cy][cx].feat == FEAT_GLYPH) continue;

							/* Important -- Skip "quake" grids */
							if (map[2 + (cx - p_ptr->px)][2 + (cy - p_ptr->py)]) continue;

							/* Count "safe" grids */
							sn++;

							/* Randomize choice */
							if (rand_int(sn) > 0) continue;

							/* Save the safe grid */
							sx = cx;
							sy = cy;

							ident = TRUE;

							break;  /* discontinue for loop - safe grid found */
						}
					}

					/* Describe the monster */
					monster_desc(m_name, m_ptr, 0);

					/* Scream in pain */
					msg_format("%^s wails out in pain!", m_name);

					/* Monster is certainly awake */
					m_ptr->csleep = 0;

					/* Apply damage directly */
					m_ptr->hp -= (sn ? damroll(4, 8) : 200);

					/* Delete (not kill) "dead" monsters */
					if (m_ptr->hp < 0)
					{
						/* Message */
						msg_format("%^s is entombed in the rock!", m_name);

						/* Delete the monster */
						delete_monster_idx(cave[cy][cx].m_idx);

						/* No longer safe */
						sn = 0;
					}

					/* Hack -- Escape from the rock */
					if (sn)
					{
						s16b m_idx = cave[cy][cx].m_idx;

						/* Update the new location */
						cave[sy][sx].m_idx = m_idx;

						/* Update the old location */
						cave[cy][cx].m_idx = 0;

						/* Move the monster */
						m_ptr->fy = sy;
						m_ptr->fx = sx;

						/* do not change fz */
						/* don't make rock on that square! */
						if ((sx >= (p_ptr->px - 2)) && (sx <= (p_ptr->px + 2)) &&
						                (sy >= (p_ptr->py - 2)) && (sy <= (p_ptr->py + 2)))
						{
							map[2 + (sx - p_ptr->px)][2 + (sy - p_ptr->py)] = FALSE;
						}

						/* Update the monster (new location) */
						update_mon(m_idx, TRUE);

						/* Redraw the old grid */
						lite_spot(cy, cx);

						/* Redraw the new grid */
						lite_spot(sy, sx);
					} /* if sn */
				} /* if monster can co-exist with rock */
			} /* if monster on square */
		}

	/* Examine the quaked region */
	for (dy = -2; dy <= 2; dy++)
		for (dx = -2; dx <= 2; dx++)
		{
			/* Extract the location */
			cx = p_ptr->px + dx;
			cy = p_ptr->py + dy;

			/* Skip unaffected grids */
			if (!map[2 + dx][2 + dy]) continue;

			/* Access the cave grid */
			cv_ptr = &cave[cy][cx];

			/* Paranoia -- never affect player */
			if (!dy && !dx) continue;

			/* Destroy location (if valid) */
			if ((cx < cur_wid) && (cy < cur_hgt) && cave_valid_bold(cy, cx))
			{
				bool_ floor = (f_info[cave[cy][cx].feat].flags1 & FF1_FLOOR);

				/* Delete any object that is still there */
				delete_object(cy, cx);

				if (floor)
				{
					cave_set_feat(cy, cx, FEAT_WALL_OUTER);
				}
				else
				{
					/* Clear previous contents, add floor */
					cave_set_feat(cy, cx, FEAT_FLOOR);
				}
			}
		}

	/* Mega-Hack -- Forget the view and lite */
	p_ptr->update |= PU_UN_VIEW;

	/* Update stuff */
	p_ptr->update |= (PU_VIEW | PU_FLOW | PU_MON_LITE);

	/* Update the monsters */
	p_ptr->update |= (PU_DISTANCE);

	/* Update the health bar */
	p_ptr->redraw |= (PR_FRAME);

	/* Redraw map */
	p_ptr->redraw |= (PR_MAP);

	/* Window stuff */
	p_ptr->window |= (PW_OVERHEAD);
	handle_stuff();

	msg_print("Suddenly the cave shifts around you. The air is getting stale!");

	ident = TRUE;

	return (ident);
}


/*
 * this function handles arrow & dagger traps, in various types.
 * num = number of missiles
 * tval, sval = kind of missiles
 * dd,ds = damage roll for missiles
 * poison_dam = additional poison damage
 * name = name given if you should die from it...
 *
 * return value = ident (always TRUE)
 */
static bool_ player_handle_missile_trap(s16b num, s16b tval, s16b sval, s16b dd, s16b ds,
                                       s16b pdam, cptr name)
{
	object_type *o_ptr, forge;
	s16b i, k_idx = lookup_kind(tval, sval);
	char i_name[80];

	o_ptr = &forge;
	object_prep(o_ptr, k_idx);
	o_ptr->number = num;
	apply_magic(o_ptr, max_dlv[dungeon_type], FALSE, FALSE, FALSE);
	object_desc(i_name, o_ptr, TRUE, 0);

	msg_format("Suddenly %s hit%s you!", i_name,
	           ((num == 1) ? "" : "s"));

	for (i = 0; i < num; i++)
	{
		take_hit(damroll(dd, ds), name);

		redraw_stuff();

		if (pdam > 0)
		{
			if (!(p_ptr->resist_pois || p_ptr->oppose_pois))
			{
				(void)set_poisoned(p_ptr->poisoned + pdam);
			}
		}
	}

	drop_near(o_ptr, -1, p_ptr->py, p_ptr->px);

	return TRUE;
}

/*
 * this function handles a "breath" type trap - acid bolt, lightning balls etc.
 */
static bool_ player_handle_breath_trap(s16b rad, s16b type, u16b trap)
{
	trap_type *t_ptr = &t_info[trap];
	bool_ ident;
	s16b my_dd, my_ds, dam;

	my_dd = t_ptr->dd;
	my_ds = t_ptr->ds;

	/* these traps gets nastier as levels progress */
	if (max_dlv[dungeon_type] > (2 * t_ptr->minlevel))
	{
		my_dd += (max_dlv[dungeon_type] / 15);
		my_ds += (max_dlv[dungeon_type] / 15);
	}
	dam = damroll(my_dd, my_ds);

	ident = project( -2, rad, p_ptr->py, p_ptr->px, dam, type, PROJECT_KILL | PROJECT_JUMP);

	return (ident);
}

/*
 * This function damages the player by a trap
 */
static void trap_hit(s16b trap)
{
	trap_type *t_ptr = &t_info[trap];
	s16b dam = damroll(t_ptr->dd, t_ptr->ds);
	take_hit(dam, t_ptr->name);
}

/*
 * this function activates one trap type, and returns
 * a bool_ indicating if this trap is now identified
 */
bool_ player_activate_trap_type(s16b y, s16b x, object_type *i_ptr, s16b item)
{
	bool_ ident = FALSE;
	s16b trap;

	s16b k, l;

	trap = cave[y][x].t_idx;

	if (i_ptr != NULL)
	{
		trap = i_ptr->pval;
	}

	if ((i_ptr == NULL) && (cave[y][x].o_idx != 0))
	{
		i_ptr = &o_list[cave[y][x].o_idx];
	}

	switch (trap)
	{
		/* stat traps */
	case TRAP_OF_WEAKNESS_I:
		ident = do_dec_stat(A_STR, STAT_DEC_TEMPORARY);
		break;
	case TRAP_OF_WEAKNESS_II:
		ident = do_dec_stat(A_STR, STAT_DEC_NORMAL);
		break;
	case TRAP_OF_WEAKNESS_III:
		ident = do_dec_stat(A_STR, STAT_DEC_PERMANENT);
		break;
	case TRAP_OF_INTELLIGENCE_I:
		ident = do_dec_stat(A_INT, STAT_DEC_TEMPORARY);
		break;
	case TRAP_OF_INTELLIGENCE_II:
		ident = do_dec_stat(A_INT, STAT_DEC_NORMAL);
		break;
	case TRAP_OF_INTELLIGENCE_III:
		ident = do_dec_stat(A_INT, STAT_DEC_PERMANENT);
		break;
	case TRAP_OF_WISDOM_I:
		ident = do_dec_stat(A_WIS, STAT_DEC_TEMPORARY);
		break;
	case TRAP_OF_WISDOM_II:
		ident = do_dec_stat(A_WIS, STAT_DEC_NORMAL);
		break;
	case TRAP_OF_WISDOM_III:
		ident = do_dec_stat(A_WIS, STAT_DEC_PERMANENT);
		break;
	case TRAP_OF_FUMBLING_I:
		ident = do_dec_stat(A_DEX, STAT_DEC_TEMPORARY);
		break;
	case TRAP_OF_FUMBLING_II:
		ident = do_dec_stat(A_DEX, STAT_DEC_NORMAL);
		break;
	case TRAP_OF_FUMBLING_III:
		ident = do_dec_stat(A_DEX, STAT_DEC_PERMANENT);
		break;
	case TRAP_OF_WASTING_I:
		ident = do_dec_stat(A_CON, STAT_DEC_TEMPORARY);
		break;
	case TRAP_OF_WASTING_II:
		ident = do_dec_stat(A_CON, STAT_DEC_NORMAL);
		break;
	case TRAP_OF_WASTING_III:
		ident = do_dec_stat(A_CON, STAT_DEC_PERMANENT);
		break;
	case TRAP_OF_BEAUTY_I:
		ident = do_dec_stat(A_CHR, STAT_DEC_TEMPORARY);
		break;
	case TRAP_OF_BEAUTY_II:
		ident = do_dec_stat(A_CHR, STAT_DEC_NORMAL);
		break;
	case TRAP_OF_BEAUTY_III:
		ident = do_dec_stat(A_CHR, STAT_DEC_PERMANENT);
		break;

		/* Trap of Curse Weapon */
	case TRAP_OF_CURSE_WEAPON:
		{
			ident = curse_weapon();
			break;
		}

		/* Trap of Curse Armor */
	case TRAP_OF_CURSE_ARMOR:
		{
			ident = curse_armor();
			break;
		}

		/* Earthquake Trap */
	case TRAP_OF_EARTHQUAKE:
		{
			msg_print("As you touch the trap, the ground starts to shake.");
			earthquake(y, x, 10);
			ident = TRUE;
			break;
		}

		/* Poison Needle Trap */
	case TRAP_OF_POISON_NEEDLE:
		{
			if (!(p_ptr->resist_pois || p_ptr->oppose_pois))
			{
				msg_print("You prick yourself on a poisoned needle.");
				(void)set_poisoned(p_ptr->poisoned + rand_int(15) + 10);
				ident = TRUE;
			}
			else
			{
				msg_print("You prick yourself on a needle.");
			}
			break;
		}

		/* Summon Monster Trap */
	case TRAP_OF_SUMMON_MONSTER:
		{
			msg_print("A spell hangs in the air.");
			for (k = 0; k < randint(3); k++)
			{
				ident |= summon_specific(y, x, max_dlv[dungeon_type], 0);
			}
			break;
		}

		/* Summon Undead Trap */
	case TRAP_OF_SUMMON_UNDEAD:
		{
			msg_print("A mighty spell hangs in the air.");
			for (k = 0; k < randint(3); k++)
			{
				ident |= summon_specific(y, x, max_dlv[dungeon_type],
				                         SUMMON_UNDEAD);
			}
			break;
		}

		/* Summon Greater Undead Trap */
	case TRAP_OF_SUMMON_GREATER_UNDEAD:
		{
			msg_print("An old and evil spell hangs in the air.");
			for (k = 0; k < randint(3); k++)
			{
				ident |= summon_specific(y, x, max_dlv[dungeon_type],
				                         SUMMON_HI_UNDEAD);
			}
			break;
		}

		/* Teleport Trap */
	case TRAP_OF_TELEPORT:
		{
			msg_print("The world whirls around you.");
			teleport_player(RATIO * 67);
			ident = TRUE;
			break;
		}

		/* Paralyzing Trap */
	case TRAP_OF_PARALYZING:
		{
			if (!p_ptr->free_act)
			{
				msg_print("You touch a poisoned part and can't move.");
				(void)set_paralyzed(rand_int(10) + 10);
				ident = TRUE;
			}
			else
			{
				msg_print("You prick yourself on a needle.");
			}
			break;
		}

		/* Explosive Device */
	case TRAP_OF_EXPLOSIVE_DEVICE:
		{
			msg_print("A hidden explosive device explodes in your face.");
			take_hit(damroll(5, 8), "an explosion");
			ident = TRUE;
			break;
		}

		/* Teleport Away Trap */
	case TRAP_OF_TELEPORT_AWAY:
		{
			int item, amt;
			object_type *o_ptr;

			/* teleport away all items */
			while (cave[y][x].o_idx != 0)
			{
				item = cave[y][x].o_idx;

				o_ptr = &o_list[item];

				amt = o_ptr->number;

				ident = do_trap_teleport_away(o_ptr, y, x);

				floor_item_increase(item, -amt);
				floor_item_optimize(item);
			}
			break;
		}

		/* Lose Memory Trap */
	case TRAP_OF_LOSE_MEMORY:
		{
			lose_exp(p_ptr->exp / 4);

			ident |= dec_stat(A_WIS, rand_int(20) + 10, STAT_DEC_NORMAL);
			ident |= dec_stat(A_INT, rand_int(20) + 10, STAT_DEC_NORMAL);

			if (!p_ptr->resist_conf)
			{
				ident |= set_confused(p_ptr->confused + rand_int(100) + 50);
			}

			if (ident)
			{
				msg_print("You suddenly don't remember what you were doing.");
			}
			else
			{
				msg_print("You feel an alien force probing your mind.");
			}
			break;
		}
		/* Bitter Regret Trap */
	case TRAP_OF_BITTER_REGRET:
		{
			msg_print("An age-old and hideous-sounding spell reverberates off the walls.");

			ident |= dec_stat(A_DEX, 25, TRUE);
			ident |= dec_stat(A_WIS, 25, TRUE);
			ident |= dec_stat(A_CON, 25, TRUE);
			ident |= dec_stat(A_STR, 25, TRUE);
			ident |= dec_stat(A_CHR, 25, TRUE);
			ident |= dec_stat(A_INT, 25, TRUE);
			break;
		}

		/* Bowel Cramps Trap */
	case TRAP_OF_BOWEL_CRAMPS:
		{
			msg_print("A wretched-smelling gas cloud upsets your stomach.");

			(void)set_food(PY_FOOD_STARVE - 1);
			(void)set_poisoned(0);

			if (!p_ptr->free_act)
			{
				(void)set_paralyzed(rand_int(dun_level) + 6);
			}
			ident = TRUE;
			break;
		}

		/* Blindness/Confusion Trap */
	case TRAP_OF_BLINDNESS_CONFUSION:
		{
			msg_print("A powerful magic protected this.");

			if (!p_ptr->resist_blind)
			{
				ident |= set_blind(p_ptr->blind + rand_int(100) + 100);
			}
			if (!p_ptr->resist_conf)
			{
				ident |= set_confused(p_ptr->confused + rand_int(20) + 15);
			}
			break;
		}

		/* Aggravation Trap */
	case TRAP_OF_AGGRAVATION:
		{
			msg_print("You hear a hollow noise echoing through the dungeons.");
			aggravate_monsters(1);
			break;
		}

		/* Multiplication Trap */
	case TRAP_OF_MULTIPLICATION:
		{
			msg_print("You hear a loud click.");
			for (k = -1; k <= 1; k++)
				for (l = -1; l <= 1; l++)
				{
					if ((in_bounds(p_ptr->py + l, p_ptr->px + k)) &&
					                (!cave[p_ptr->py + l][p_ptr->px + k].t_idx))
					{
						place_trap(p_ptr->py + l, p_ptr->px + k);
					}
				}
			ident = TRUE;
			break;
		}

		/* Steal Item Trap */
	case TRAP_OF_STEAL_ITEM:
		{
			/*
			 * please note that magical stealing is not so
			 * easily circumvented
			 */
			if (!p_ptr->paralyzed &&
			                (rand_int(160) < (adj_dex_safe[p_ptr->stat_ind[A_DEX]] +
			                                  p_ptr->lev)))
			{
				/* Saving throw message */
				msg_print("Your backpack seems to vibrate strangely!");
				break;
			}

			/* Find an item */
			for (k = 0; k < rand_int(10); k++)
			{
				char i_name[80];
				object_type *j_ptr, *q_ptr, forge;

				/* Pick an item */
				s16b i = rand_int(INVEN_PACK);

				/* Obtain the item */
				j_ptr = &p_ptr->inventory[i];

				/* Accept real items */
				if (!j_ptr->k_idx) continue;

				/* Don't steal artifacts  -CFT */
				if (artifact_p(j_ptr)) continue;

				/* Get a description */
				object_desc(i_name, j_ptr, FALSE, 3);

				/* Message */
				msg_format("%sour %s (%c) was stolen!",
				           ((j_ptr->number > 1) ? "One of y" : "Y"),
				           i_name, index_to_label(i));

				/* Create the item */
				q_ptr = &forge;
				object_copy(q_ptr, j_ptr);
				q_ptr->number = 1;

				/* Drop it somewhere */
				do_trap_teleport_away(q_ptr, y, x);

				inc_stack_size_ex(i, -1, OPTIMIZE, NO_DESCRIBE);

				ident = TRUE;
			}
			break;
		}

		/* Summon Fast Quylthulgs Trap */
	case TRAP_OF_SUMMON_FAST_QUYLTHULGS:
		{
			for (k = 0; k < randint(3); k++)
			{
				ident |= summon_specific(y, x, max_dlv[dungeon_type], SUMMON_QUYLTHULG);
			}

			if (ident)
			{
				msg_print("You suddenly have company.");
				(void)set_slow(p_ptr->slow + randint(25) + 15);
			}
			break;
		}

		/* Trap of Sinking */
	case TRAP_OF_SINKING:
		{
			msg_print("You fell through a trap door!");

			if (p_ptr->ffall)
			{
				if (dungeon_flags1 & DF1_TOWER)
				{
					msg_print("You float gently down to the previous level.");
				}
				else
				{
					msg_print("You float gently down to the next level.");
				}
			}
			else
			{
				take_hit(damroll(2, 8), "a trap door");
			}

			/* Still alive and autosave enabled */
			if (p_ptr->chp >= 0)
			{
				autosave_checkpoint();
			}

			if (dungeon_flags1 & DF1_TOWER) dun_level--;
			else dun_level++;

			/* Leaving */
			p_ptr->leaving = TRUE;
			break;
		}

		/* Trap of Mana Drain */
	case TRAP_OF_MANA_DRAIN:
		{
			if (p_ptr->csp > 0)
			{
				p_ptr->csp = 0;
				p_ptr->csp_frac = 0;
				p_ptr->redraw |= (PR_FRAME);
				msg_print("You sense a great loss.");
				ident = TRUE;
			}
			else if (p_ptr->msp == 0)
			{
				/* no sense saying this unless you never have mana */
				msg_format("Suddenly you feel glad you're a mere %s",
					   spp_ptr->title);
			}
			else
			{
				msg_print("Your head feels dizzy for a moment.");
			}
			break;
		}
		/* Trap of Missing Money */
	case TRAP_OF_MISSING_MONEY:
		{
			s32b gold = (p_ptr->au / 10) + randint(25);

			if (gold < 2) gold = 2;
			if (gold > 5000) gold = (p_ptr->au / 20) + randint(3000);
			if (gold > p_ptr->au) gold = p_ptr->au;

			p_ptr->au -= gold;
			if (gold <= 0)
			{
				msg_print("You feel something touching you.");
			}
			else if (p_ptr->au)
			{
				msg_print("Your purse feels lighter.");
				msg_format("%ld coins were stolen!", (long)gold);
				ident = TRUE;
			}
			else
			{
				msg_print("Your purse feels empty.");
				msg_print("All of your coins were stolen!");
				ident = TRUE;
			}
			p_ptr->redraw |= (PR_FRAME);
			break;
		}

		/* Trap of No Return */
	case TRAP_OF_NO_RETURN:
		{
			object_type *j_ptr;
			s16b j;

			for (j = 0; j < INVEN_WIELD; j++)
			{
				if (!p_ptr->inventory[j].k_idx) continue;

				j_ptr = &p_ptr->inventory[j];

				if ((j_ptr->tval == TV_SCROLL) &&
				                (j_ptr->sval == SV_SCROLL_WORD_OF_RECALL))
				{
					inc_stack_size_ex(j, -j_ptr->number, OPTIMIZE, NO_DESCRIBE);

					combine_pack();
					reorder_pack();

					if (!ident)
					{
						msg_print("A small fire works its way through your backpack. "
						          "Some scrolls are burnt.");
					}
					else
					{
						msg_print("The fire hasn't finished.");
					}
					ident = TRUE;
				}
				else if ((j_ptr->tval == TV_ROD_MAIN) &&
				                (j_ptr->pval == SV_ROD_RECALL))
				{
					j_ptr->timeout = 0;  /* a long time */
					if (!ident) msg_print("You feel the air stabilise around you.");
					ident = TRUE;
				}
			}
			if ((!ident) && (p_ptr->word_recall))
			{
				msg_print("You feel like staying around.");
				p_ptr->word_recall = 0;
				ident = TRUE;
			}
			break;
		}

		/* Trap of Silent Switching */
	case TRAP_OF_SILENT_SWITCHING:
		{
			s16b i, j, slot1, slot2;
			object_type *j_ptr, *k_ptr;
			u32b f1, f2, f3, f4, f5, esp;

			for (i = INVEN_WIELD; i < INVEN_TOTAL; i++)
			{
				j_ptr = &p_ptr->inventory[i];

				if (!j_ptr->k_idx) continue;

				/* Do not allow this trap to touch the One Ring */
				object_flags(j_ptr, &f1, &f2, &f3, &f4, &f5, &esp);
				if(f3 & TR3_PERMA_CURSE) continue;

				slot1 = wield_slot(j_ptr);

				for (j = 0; j < INVEN_WIELD; j++)
				{
					k_ptr = &p_ptr->inventory[j];

					if (!k_ptr->k_idx) continue;

					/* Do not allow this trap to touch the One Ring */
					object_flags(k_ptr, &f1, &f2, &f3, &f4, &f5, &esp);
					if(f3 & TR3_PERMA_CURSE) continue;

					/* this is a crude hack, but it prevent wielding 6 torches... */
					if (k_ptr->number > 1) continue;

					slot2 = wield_slot(k_ptr);

					/* a chance of 4 in 5 of switching something, then 2 in 5 to do it again */
					if ((slot1 == slot2) &&
					                (rand_int(100) < (80 - (ident * 40))))
					{
						object_type tmp_obj;

						if (p_ptr->inventory[j].name1)
							wield_set(p_ptr->inventory[j].name1, a_info[p_ptr->inventory[j].name1].set, FALSE);
						if (p_ptr->inventory[i].name1)
							takeoff_set(p_ptr->inventory[i].name1, a_info[p_ptr->inventory[i].name1].set);

						tmp_obj = p_ptr->inventory[j];
						p_ptr->inventory[j] = p_ptr->inventory[i];
						p_ptr->inventory[i] = tmp_obj;
						ident = TRUE;
					}
				}
			}

			if (ident)
			{
				p_ptr->update |= (PU_BONUS);
				p_ptr->update |= (PU_TORCH);
				p_ptr->update |= (PU_MANA);
				msg_print("You somehow feel like another person.");
			}
			else
			{
				msg_print("You feel a lack of useful items.");
			}
			break;
		}

		/* Trap of Walls */
	case TRAP_OF_WALLS:
		{
			ident = player_handle_trap_of_walls();
			break;
		}

		/* Trap of Calling Out */
	case TRAP_OF_CALLING_OUT:
		{
			ident = do_player_trap_call_out();

			if (!ident)
			{
				/* Increase "afraid" */
				if (p_ptr->resist_fear)
				{
					msg_print("You feel as if you had a nightmare!");
				}
				else if (rand_int(100) < p_ptr->skill_sav)
				{
					msg_print("You remember having a nightmare!");
				}
				else
				{
					if (set_afraid(p_ptr->afraid + 3 + randint(40)))
					{
						msg_print("You have a vision of a powerful enemy.");
					}
				}
			}
			break;
		}

		/* Trap of Sliding */
	case TRAP_OF_SLIDING:
		break;

		/* Trap of Charges Drain */
	case TRAP_OF_CHARGES_DRAIN:
		{
			/* Find an item */
			for (k = 0; k < 10; k++)
			{
				s16b i = rand_int(INVEN_PACK);

				object_type *j_ptr = &p_ptr->inventory[i];

				/* Drain charged wands/staffs
				   Hack -- don't let artifacts get drained */
				if (((j_ptr->tval == TV_STAFF) ||
				                (j_ptr->tval == TV_WAND)) &&
				                (j_ptr->pval) &&
			                     !artifact_p(j_ptr))
				{
					ident = TRUE;
					j_ptr->pval = j_ptr->pval / (randint(4) + 1);

					/* 60% chance of only 1 */
					if (randint(10) > 3) break;
				}
			}

			if (ident)
			{
				/* Window stuff */
				p_ptr->window |= PW_INVEN;
				/* Combine / Reorder the pack */
				p_ptr->notice |= (PN_COMBINE | PN_REORDER);

				msg_print("Your backpack seems to be turned upside down.");
			}
			else
			{
				msg_print("You hear a wail of great disappointment.");
			}
			break;
		}

		/* Trap of Stair Movement */
	case TRAP_OF_STAIR_MOVEMENT:
		{
			s16b cx, cy, i, j;
			s16b cnt = 0;
			s16b cnt_seen = 0;
			s16b tmps, tmpx;
			s16b tmpspecial, tmpspecial2;
			u32b tmpf;
			bool_ seen = FALSE;
			s16b index_x[20], index_y[20];  /* 20 stairs per level is enough? */
			cave_type *cv_ptr;

			if (max_dlv[dungeon_type] == 99)
			{
				/* no sense in relocating that stair! */
				msg_print("You have a feeling that this trap could be dangerous.");
				break;
			}

			for (cx = 0; cx < cur_wid; cx++)
				for (cy = 0; cy < cur_hgt; cy++)
				{
					cv_ptr = &cave[cy][cx];

					if ((cv_ptr->feat != FEAT_LESS) &&
					                (cv_ptr->feat != FEAT_MORE) &&
					                (cv_ptr->feat != FEAT_SHAFT_UP) &&
					                (cv_ptr->feat != FEAT_SHAFT_DOWN)) continue;

					index_x[cnt] = cx;
					index_y[cnt] = cy;
					cnt++;
				}

			if (cnt == 0)
			{
				if (wizard) msg_print("Executing moving stairs trap on level with no stairs!");
				break;
			}

			for (i = 0; i < cnt; i++)
			{
				seen = FALSE;

				for (j = 0; j < 10; j++) /* try 10 times to relocate */
				{
					cave_type *cv_ptr = &cave[index_y[i]][index_x[i]];
					cave_type *cv_ptr2;

					cx = rand_int(cur_wid);
					cy = rand_int(cur_hgt);

					if ((cx == index_x[i]) || (cy == index_y[i])) continue;

					cv_ptr2 = &cave[cy][cx];

					if (!cave_valid_bold(cy, cx) || cv_ptr2->o_idx != 0) continue;

					/* don't put anything in vaults */
					if (cv_ptr2->info & CAVE_ICKY) continue;

					tmpx = cv_ptr2->mimic;
					tmps = cv_ptr2->info;
					tmpf = cv_ptr2->feat;
					tmpspecial = cv_ptr2->special;
					tmpspecial2 = cv_ptr2->special2;
					cave[cy][cx].mimic = cv_ptr->mimic;
					cave[cy][cx].info = cv_ptr->info;
					cave[cy][cx].special = cv_ptr->special;
					cave[cy][cx].special2 = cv_ptr->special2;
					cave_set_feat(cy, cx, cv_ptr->feat);
					cv_ptr->mimic = tmpx;
					cv_ptr->info = tmps;
					cv_ptr->special = tmpspecial;
					cv_ptr->special2 = tmpspecial2;
					cave_set_feat(index_y[i], index_x[i], tmpf);

					/* if we are placing walls in rooms, make them rubble instead */
					if ((cv_ptr->info & CAVE_ROOM) &&
					                (cv_ptr->feat >= FEAT_WALL_EXTRA) &&
					                (cv_ptr->feat <= FEAT_PERM_SOLID))
					{
						cave_set_feat(index_y[i], index_x[i], FEAT_RUBBLE);
					}

					if (player_has_los_bold(cy, cx))
					{
						note_spot(cy, cx);
						lite_spot(cy, cx);
						seen = TRUE;
					}
					else
					{
						cv_ptr2->info &= ~CAVE_MARK;
					}

					if (player_has_los_bold(index_y[i], index_x[i]))
					{
						note_spot(index_y[i], index_x[i]);
						lite_spot(index_y[i], index_x[i]);
						seen = TRUE;
					}
					else
					{
						cv_ptr->info &= ~CAVE_MARK;
					}
					break;
				}

				if (seen) cnt_seen++;
			}

			ident = (cnt_seen > 0);

			if ((ident) && (cnt_seen > 1))
			{
				msg_print("You see some stairs move.");
			}
			else if (ident)
			{
				msg_print("You see a stair move.");
			}
			else
			{
				msg_print("You hear distant scraping noises.");
			}
			p_ptr->redraw |= PR_MAP;
			break;
		}

		/* Trap of New Trap */
	case TRAP_OF_NEW:
		{
			/* if we're on a floor or on a door, place a new trap */
			if ((item == -1) || (item == -2))
			{
				place_trap(y, x);
				if (player_has_los_bold(y, x))
				{
					note_spot(y, x);
					lite_spot(y, x);
				}
			}
			else
			{
				/* re-trap the chest */
				place_trap(y, x);
			}
			msg_print("You hear a noise, and then its echo.");
			ident = FALSE;
			break;
		}

		/* Trap of Acquirement */
	case TRAP_OF_ACQUIREMENT:
		{
			/* Get a nice thing */
			msg_print("You notice something falling off the trap.");
			acquirement(y, x, 1, TRUE, FALSE);

			/* If we're on a floor or on a door, place a new trap */
			if ((item == -1) || (item == -2))
			{
				place_trap(y, x);
				if (player_has_los_bold(y, x))
				{
					note_spot(y, x);
					lite_spot(y, x);
				}
			}
			else
			{
				/* Re-trap the chest */
				place_trap(y, x);
			}
			msg_print("You hear a noise, and then its echo.");

			/* Never known */
			ident = FALSE;
		}
		break;

		/* Trap of Scatter Items */
	case TRAP_OF_SCATTER_ITEMS:
		{
			s16b i, j;
			bool_ message = FALSE;

			for (i = 0; i < INVEN_PACK; i++)
			{

				if (!p_ptr->inventory[i].k_idx) continue;

				if (rand_int(10) < 3) continue;

				for (j = 0; j < 10; j++)
				{
					object_type tmp_obj, *j_ptr = &tmp_obj;
					s16b cx = x + 15 - rand_int(30);
					s16b cy = y + 15 - rand_int(30);

					if (!in_bounds(cy, cx)) continue;

					if (!cave_floor_bold(cy, cx)) continue;

					object_copy(j_ptr, &p_ptr->inventory[i]);

					inc_stack_size_ex(i, -999, OPTIMIZE, NO_DESCRIBE);

					p_ptr->notice |= (PN_COMBINE | PN_REORDER);

					(void)floor_carry(cy, cx, j_ptr);

					if (!message)
					{
						msg_print("You feel light-footed.");
						message = TRUE;
					}

					if (player_has_los_bold(cy, cx))
					{
						char i_name[80];

						object_desc(i_name, &tmp_obj, TRUE, 3);
						note_spot(cy, cx);
						lite_spot(cy, cx);
						ident = TRUE;
						msg_format("Suddenly %s appear%s!", i_name,
						           (j_ptr->number > 1) ? "" : "s");
					}
					break;
				}
			}
			ident = message;
			break;
		}

		/* Trap of Decay */
	case TRAP_OF_DECAY:
		break;

		/* Trap of Wasting Wands */
	case TRAP_OF_WASTING_WANDS:
		{
			s16b i;
			object_type *j_ptr;

			for (i = 0; i < INVEN_PACK; i++)
			{
				if (!p_ptr->inventory[i].k_idx) continue;

				j_ptr = &p_ptr->inventory[i];

				if ((j_ptr->tval == TV_WAND) && (rand_int(5) == 1))
				{
					if (object_known_p(j_ptr)) ident = TRUE;

					/* Create a Wand of Nothing */
					object_prep(j_ptr, lookup_kind(TV_WAND, SV_WAND_NOTHING));
					hack_apply_magic_power = -99;
					apply_magic(j_ptr, 0, FALSE, FALSE, FALSE);
					j_ptr->ident &= ~IDENT_KNOWN;
					p_ptr->notice |= (PN_COMBINE | PN_REORDER);
				}
				else if ((j_ptr->tval == TV_STAFF) && (rand_int(5) == 1))
				{
					if (object_known_p(j_ptr)) ident = TRUE;

					/* Create a Staff of Nothing */
					object_prep(j_ptr, lookup_kind(TV_STAFF, SV_STAFF_NOTHING));
					hack_apply_magic_power = -99;
					apply_magic(j_ptr, 0, FALSE, FALSE, FALSE);
					j_ptr->ident &= ~IDENT_KNOWN;
					p_ptr->notice |= (PN_COMBINE | PN_REORDER);
				}
			}
			if (ident)
			{
				msg_print("You have lost trust in your backpack!");
			}
			else
			{
				msg_print("You hear an echoing cry of rage.");
			}
			break;
		}

		/* Trap of Filling */
	case TRAP_OF_FILLING:
		{
			s16b nx, ny;

			for (nx = x - 8; nx <= x + 8; nx++)
				for (ny = y - 8; ny <= y + 8; ny++)
				{
					if (!in_bounds (ny, nx)) continue;

					if (rand_int(distance(ny, nx, y, x)) > 3)
					{
						place_trap(ny, nx);
					}
				}

			msg_print("The floor vibrates in a strange way.");
			ident = FALSE;
			break;
		}

	case TRAP_OF_DRAIN_SPEED:
		{
			object_type *j_ptr;
			s16b j, chance = 75;
			u32b f1, f2, f3, f4, f5, esp;

			for (j = 0; j < INVEN_TOTAL; j++)
			{
				/* don't bother the overflow slot */
				if (j == INVEN_PACK) continue;

				if (!p_ptr->inventory[j].k_idx) continue;

				j_ptr = &p_ptr->inventory[j];
				object_flags(j_ptr, &f1, &f2, &f3, &f4, &f5, &esp);

				/* is it a non-artifact speed item? */
				if ((!j_ptr->name1) && (f1 & TR1_SPEED))
				{
					if (randint(100) < chance)
					{
						j_ptr->pval = j_ptr->pval / 2;
						if (j_ptr->pval == 0)
						{
							j_ptr->pval--;
						}
						chance /= 2;
						ident = TRUE;
					}
					inven_item_optimize(j);
				}
			}
			if (!ident)
			{
				msg_print("You feel some things in your pack vibrating.");
			}
			else
			{
				combine_pack();
				reorder_pack();
				msg_print("You suddenly feel you have time for self-reflection.");

				/* Recalculate bonuses */
				p_ptr->update |= (PU_BONUS);

				/* Recalculate mana */
				p_ptr->update |= (PU_MANA);

				/* Window stuff */
				p_ptr->window |= (PW_INVEN | PW_EQUIP | PW_PLAYER);
			}
			break;
		}

		/*
		 * single missile traps
		 */
	case TRAP_OF_ARROW_I:
		ident = player_handle_missile_trap(1, TV_ARROW, SV_AMMO_NORMAL, 4, 8, 0, "Arrow Trap");
		break;
	case TRAP_OF_ARROW_II:
		ident = player_handle_missile_trap(1, TV_BOLT, SV_AMMO_NORMAL, 5, 8, 0, "Bolt Trap");
		break;
	case TRAP_OF_ARROW_III:
		ident = player_handle_missile_trap(1, TV_ARROW, SV_AMMO_HEAVY, 6, 8, 0, "Seeker Arrow Trap");
		break;
	case TRAP_OF_ARROW_IV:
		ident = player_handle_missile_trap(1, TV_BOLT, SV_AMMO_HEAVY, 8, 10, 0, "Seeker Bolt Trap");
		break;
	case TRAP_OF_POISON_ARROW_I:
		ident = player_handle_missile_trap(1, TV_ARROW, SV_AMMO_NORMAL, 4, 8, 10 + randint(20), "Poison Arrow Trap");
		break;
	case TRAP_OF_POISON_ARROW_II:
		ident = player_handle_missile_trap(1, TV_BOLT, SV_AMMO_NORMAL, 5, 8, 15 + randint(30), "Poison Bolt Trap");
		break;
	case TRAP_OF_POISON_ARROW_III:
		ident = player_handle_missile_trap(1, TV_ARROW, SV_AMMO_HEAVY, 6, 8, 30 + randint(50), "Poison Seeker Arrow Trap");
		break;
	case TRAP_OF_POISON_ARROW_IV:
		ident = player_handle_missile_trap(1, TV_BOLT, SV_AMMO_HEAVY, 8, 10, 40 + randint(70), "Poison Seeker Bolt Trap");
		break;
	case TRAP_OF_DAGGER_I:
		ident = player_handle_missile_trap(1, TV_SWORD, SV_BROKEN_DAGGER, 2, 8, 0, "Dagger Trap");
		break;
	case TRAP_OF_DAGGER_II:
		ident = player_handle_missile_trap(1, TV_SWORD, SV_DAGGER, 3, 8, 0, "Dagger Trap");
		break;
	case TRAP_OF_POISON_DAGGER_I:
		ident = player_handle_missile_trap(1, TV_SWORD, SV_BROKEN_DAGGER, 2, 8, 15 + randint(20), "Poison Dagger Trap");
		break;
	case TRAP_OF_POISON_DAGGER_II:
		ident = player_handle_missile_trap(1, TV_SWORD, SV_DAGGER, 3, 8, 20 + randint(30), "Poison Dagger Trap");
		break;

		/*
		 * multiple missile traps
		 * numbers range from 2 (level 0 to 14) to 10 (level 120 and up)
		 */
	case TRAP_OF_ARROWS_I:
		ident = player_handle_missile_trap(2 + (max_dlv[dungeon_type] / 15), TV_ARROW, SV_AMMO_NORMAL, 4, 8, 0, "Arrow Trap");
		break;
	case TRAP_OF_ARROWS_II:
		ident = player_handle_missile_trap(2 + (max_dlv[dungeon_type] / 15), TV_BOLT, SV_AMMO_NORMAL, 5, 8, 0, "Bolt Trap");
		break;
	case TRAP_OF_ARROWS_III:
		ident = player_handle_missile_trap(2 + (max_dlv[dungeon_type] / 15), TV_ARROW, SV_AMMO_HEAVY, 6, 8, 0, "Seeker Arrow Trap");
		break;
	case TRAP_OF_ARROWS_IV:
		ident = player_handle_missile_trap(2 + (max_dlv[dungeon_type] / 15), TV_BOLT, SV_AMMO_HEAVY, 8, 10, 0, "Seeker Bolt Trap");
		break;
	case TRAP_OF_POISON_ARROWS_I:
		ident = player_handle_missile_trap(2 + (max_dlv[dungeon_type] / 15), TV_ARROW, SV_AMMO_NORMAL, 4, 8, 10 + randint(20), "Poison Arrow Trap");
		break;
	case TRAP_OF_POISON_ARROWS_II:
		ident = player_handle_missile_trap(2 + (max_dlv[dungeon_type] / 15), TV_BOLT, SV_AMMO_NORMAL, 5, 8, 15 + randint(30), "Poison Bolt Trap");
		break;
	case TRAP_OF_POISON_ARROWS_III:
		ident = player_handle_missile_trap(2 + (max_dlv[dungeon_type] / 15), TV_ARROW, SV_AMMO_HEAVY, 6, 8, 30 + randint(50), "Poison Seeker Arrow Trap");
		break;
	case TRAP_OF_POISON_ARROWS_IV:
		ident = player_handle_missile_trap(2 + (max_dlv[dungeon_type] / 15), TV_BOLT, SV_AMMO_HEAVY, 8, 10, 40 + randint(70), "Poison Seeker Bolt Trap");
		break;
	case TRAP_OF_DAGGERS_I:
		ident = player_handle_missile_trap(2 + (max_dlv[dungeon_type] / 15), TV_SWORD, SV_BROKEN_DAGGER, 2, 8, 0, "Dagger Trap");
		break;
	case TRAP_OF_DAGGERS_II:
		ident = player_handle_missile_trap(2 + (max_dlv[dungeon_type] / 15), TV_SWORD, SV_DAGGER, 3, 8, 0, "Dagger Trap");
		break;
	case TRAP_OF_POISON_DAGGERS_I:
		ident = player_handle_missile_trap(2 + (max_dlv[dungeon_type] / 15), TV_SWORD, SV_BROKEN_DAGGER, 2, 8, 15 + randint(20), "Poison Dagger Trap");
		break;
	case TRAP_OF_POISON_DAGGERS_II:
		ident = player_handle_missile_trap(2 + (max_dlv[dungeon_type] / 15), TV_SWORD, SV_DAGGER, 3, 8, 20 + randint(30), "Poison Dagger Trap");
		break;

	case TRAP_OF_DROP_ITEMS:
		{
			s16b i;
			bool_ message = FALSE;

			for (i = 0; i < INVEN_PACK; i++)
			{
				object_type tmp_obj;

				if (!p_ptr->inventory[i].k_idx) continue;
				if (randint(100) < 80) continue;
				if (p_ptr->inventory[i].name1 == ART_POWER) continue;

				tmp_obj = p_ptr->inventory[i];

				/* drop carefully */
				drop_near(&tmp_obj, 0, y, x);

				inc_stack_size_ex(i, -999, OPTIMIZE, NO_DESCRIBE);

				p_ptr->notice |= (PN_COMBINE | PN_REORDER);

				if (!message)
				{
					msg_print("You are startled by a sudden sound.");
					message = TRUE;
				}
				ident = TRUE;
			}
			if (!ident)
			{
				msg_print("You hear a sudden, strange sound.");
			}
			break;
		}

	case TRAP_OF_DROP_ALL_ITEMS:
		{
			s16b i;
			bool_ message = FALSE;

			for (i = 0; i < INVEN_PACK; i++)
			{
				object_type tmp_obj;

				if (!p_ptr->inventory[i].k_idx) continue;
				if (randint(100) < 10) continue;
				if (p_ptr->inventory[i].name1 == ART_POWER) continue;

				tmp_obj = p_ptr->inventory[i];

				/* drop carefully */
				drop_near(&tmp_obj, 0, y, x);

				inc_stack_size_ex(i, -999, OPTIMIZE, NO_DESCRIBE);

				p_ptr->notice |= (PN_COMBINE | PN_REORDER);

				if (!message)
				{
					msg_print("You are greatly startled by a sudden sound.");
					message = TRUE;
				}
				ident = TRUE;
			}
			if (!ident)
			{
				msg_print("You hear a sudden, strange sound.");
			}
			break;
		}

	case TRAP_OF_DROP_EVERYTHING:
		{
			s16b i;
			bool_ message = FALSE;

			for (i = 0; i < INVEN_TOTAL; i++)
			{
				object_type tmp_obj;
				if (!p_ptr->inventory[i].k_idx) continue;
				if (randint(100) < 30) continue;
				if (p_ptr->inventory[i].name1 == ART_POWER) continue;

				tmp_obj = p_ptr->inventory[i];
				/* drop carefully */

				drop_near(&tmp_obj, 0, y, x);

				inc_stack_size_ex(i, -999, OPTIMIZE, NO_DESCRIBE);

				p_ptr->notice |= (PN_COMBINE | PN_REORDER);

				if (!message)
				{
					msg_print("You are completely startled by a sudden sound.");
					message = TRUE;
				}
				ident = TRUE;
			}
			if (!ident)
			{
				msg_print("You hear a sudden, strange sound.");
			}
			break;
		}

		/* Bolt Trap */
	case TRAP_G_ELEC_BOLT:
		ident = player_handle_breath_trap(1, GF_ELEC, TRAP_G_ELEC_BOLT);
		break;
	case TRAP_G_POIS_BOLT:
		ident = player_handle_breath_trap(1, GF_POIS, TRAP_G_POIS_BOLT);
		break;
	case TRAP_G_ACID_BOLT:
		ident = player_handle_breath_trap(1, GF_ACID, TRAP_G_ACID_BOLT);
		break;
	case TRAP_G_COLD_BOLT:
		ident = player_handle_breath_trap(1, GF_COLD, TRAP_G_COLD_BOLT);
		break;
	case TRAP_G_FIRE_BOLT:
		ident = player_handle_breath_trap(1, GF_FIRE, TRAP_G_FIRE_BOLT);
		break;
	case TRAP_OF_ELEC_BOLT:
		ident = player_handle_breath_trap(1, GF_ELEC, TRAP_OF_ELEC_BOLT);
		break;
	case TRAP_OF_POIS_BOLT:
		ident = player_handle_breath_trap(1, GF_POIS, TRAP_OF_POIS_BOLT);
		break;
	case TRAP_OF_ACID_BOLT:
		ident = player_handle_breath_trap(1, GF_ACID, TRAP_OF_ACID_BOLT);
		break;
	case TRAP_OF_COLD_BOLT:
		ident = player_handle_breath_trap(1, GF_COLD, TRAP_OF_COLD_BOLT);
		break;
	case TRAP_OF_FIRE_BOLT:
		ident = player_handle_breath_trap(1, GF_FIRE, TRAP_OF_FIRE_BOLT);
		break;
	case TRAP_OF_PLASMA_BOLT:
		ident = player_handle_breath_trap(1, GF_PLASMA, TRAP_OF_PLASMA_BOLT);
		break;
	case TRAP_OF_WATER_BOLT:
		ident = player_handle_breath_trap(1, GF_WATER, TRAP_OF_WATER_BOLT);
		break;
	case TRAP_OF_LITE_BOLT:
		ident = player_handle_breath_trap(1, GF_LITE, TRAP_OF_LITE_BOLT);
		break;
	case TRAP_OF_DARK_BOLT:
		ident = player_handle_breath_trap(1, GF_DARK, TRAP_OF_DARK_BOLT);
		break;
	case TRAP_OF_SHARDS_BOLT:
		ident = player_handle_breath_trap(1, GF_SHARDS, TRAP_OF_SHARDS_BOLT);
		break;
	case TRAP_OF_SOUND_BOLT:
		ident = player_handle_breath_trap(1, GF_SOUND, TRAP_OF_SOUND_BOLT);
		break;
	case TRAP_OF_CONFUSION_BOLT:
		ident = player_handle_breath_trap(1, GF_CONFUSION, TRAP_OF_CONFUSION_BOLT);
		break;
	case TRAP_OF_FORCE_BOLT:
		ident = player_handle_breath_trap(1, GF_FORCE, TRAP_OF_FORCE_BOLT);
		break;
	case TRAP_OF_INERTIA_BOLT:
		ident = player_handle_breath_trap(1, GF_INERTIA, TRAP_OF_INERTIA_BOLT);
		break;
	case TRAP_OF_MANA_BOLT:
		ident = player_handle_breath_trap(1, GF_MANA, TRAP_OF_MANA_BOLT);
		break;
	case TRAP_OF_ICE_BOLT:
		ident = player_handle_breath_trap(1, GF_ICE, TRAP_OF_ICE_BOLT);
		break;
	case TRAP_OF_CHAOS_BOLT:
		ident = player_handle_breath_trap(1, GF_CHAOS, TRAP_OF_CHAOS_BOLT);
		break;
	case TRAP_OF_NETHER_BOLT:
		ident = player_handle_breath_trap(1, GF_NETHER, TRAP_OF_NETHER_BOLT);
		break;
	case TRAP_OF_DISENCHANT_BOLT:
		ident = player_handle_breath_trap(1, GF_DISENCHANT, TRAP_OF_DISENCHANT_BOLT);
		break;
	case TRAP_OF_NEXUS_BOLT:
		ident = player_handle_breath_trap(1, GF_NEXUS, TRAP_OF_NEXUS_BOLT);
		break;
	case TRAP_OF_TIME_BOLT:
		ident = player_handle_breath_trap(1, GF_TIME, TRAP_OF_TIME_BOLT);
		break;
	case TRAP_OF_GRAVITY_BOLT:
		ident = player_handle_breath_trap(1, GF_GRAVITY, TRAP_OF_GRAVITY_BOLT);
		break;

		/* Ball Trap */
	case TRAP_OF_ELEC_BALL:
		ident = player_handle_breath_trap(3, GF_ELEC, TRAP_OF_ELEC_BALL);
		break;
	case TRAP_OF_POIS_BALL:
		ident = player_handle_breath_trap(3, GF_POIS, TRAP_OF_POIS_BALL);
		break;
	case TRAP_OF_ACID_BALL:
		ident = player_handle_breath_trap(3, GF_ACID, TRAP_OF_ACID_BALL);
		break;
	case TRAP_OF_COLD_BALL:
		ident = player_handle_breath_trap(3, GF_COLD, TRAP_OF_COLD_BALL);
		break;
	case TRAP_OF_FIRE_BALL:
		ident = player_handle_breath_trap(3, GF_FIRE, TRAP_OF_FIRE_BALL);
		break;
	case TRAP_OF_PLASMA_BALL:
		ident = player_handle_breath_trap(3, GF_PLASMA, TRAP_OF_PLASMA_BALL);
		break;
	case TRAP_OF_WATER_BALL:
		ident = player_handle_breath_trap(3, GF_WATER, TRAP_OF_WATER_BALL);
		break;
	case TRAP_OF_LITE_BALL:
		ident = player_handle_breath_trap(3, GF_LITE, TRAP_OF_LITE_BALL);
		break;
	case TRAP_OF_DARK_BALL:
		ident = player_handle_breath_trap(3, GF_DARK, TRAP_OF_DARK_BALL);
		break;
	case TRAP_OF_SHARDS_BALL:
		ident = player_handle_breath_trap(3, GF_SHARDS, TRAP_OF_SHARDS_BALL);
		break;
	case TRAP_OF_SOUND_BALL:
		ident = player_handle_breath_trap(3, GF_SOUND, TRAP_OF_SOUND_BALL);
		break;
	case TRAP_OF_CONFUSION_BALL:
		ident = player_handle_breath_trap(3, GF_CONFUSION, TRAP_OF_CONFUSION_BALL);
		break;
	case TRAP_OF_FORCE_BALL:
		ident = player_handle_breath_trap(3, GF_FORCE, TRAP_OF_FORCE_BALL);
		break;
	case TRAP_OF_INERTIA_BALL:
		ident = player_handle_breath_trap(3, GF_INERTIA, TRAP_OF_INERTIA_BALL);
		break;
	case TRAP_OF_MANA_BALL:
		ident = player_handle_breath_trap(3, GF_MANA, TRAP_OF_MANA_BALL);
		break;
	case TRAP_OF_ICE_BALL:
		ident = player_handle_breath_trap(3, GF_ICE, TRAP_OF_ICE_BALL);
		break;
	case TRAP_OF_CHAOS_BALL:
		ident = player_handle_breath_trap(3, GF_CHAOS, TRAP_OF_CHAOS_BALL);
		break;
	case TRAP_OF_NETHER_BALL:
		ident = player_handle_breath_trap(3, GF_NETHER, TRAP_OF_NETHER_BALL);
		break;
	case TRAP_OF_DISENCHANT_BALL:
		ident = player_handle_breath_trap(3, GF_DISENCHANT, TRAP_OF_DISENCHANT_BALL);
		break;
	case TRAP_OF_NEXUS_BALL:
		ident = player_handle_breath_trap(3, GF_NEXUS, TRAP_OF_NEXUS_BALL);
		break;
	case TRAP_OF_TIME_BALL:
		ident = player_handle_breath_trap(3, GF_TIME, TRAP_OF_TIME_BALL);
		break;
	case TRAP_OF_GRAVITY_BALL:
		ident = player_handle_breath_trap(3, GF_GRAVITY, TRAP_OF_GRAVITY_BALL);
		break;

		/* -SC- */
	case TRAP_OF_FEMINITY:
		{
			msg_print("Gas sprouts out... you feel yourself transmute.");
			p_ptr->psex = SEX_FEMALE;
			sp_ptr = &sex_info[p_ptr->psex];
			ident = TRUE;
			trap_hit(trap);
			break;
		}

	case TRAP_OF_MASCULINITY:
		{
			msg_print("Gas sprouts out... you feel yourself transmute.");
			p_ptr->psex = SEX_MALE;
			sp_ptr = &sex_info[p_ptr->psex];
			ident = TRUE;
			trap_hit(trap);
			break;
		}

	case TRAP_OF_NEUTRALITY:
		{
			msg_print("Gas sprouts out... you feel yourself transmute.");
			p_ptr->psex = SEX_NEUTER;
			sp_ptr = &sex_info[p_ptr->psex];
			ident = TRUE;
			trap_hit(trap);
			break;
		}

	case TRAP_OF_AGING:
		{
			msg_print("Colors are scintillating around you. "
			          "You see your past running before your eyes.");
			p_ptr->age += randint((rp_ptr->b_age + rmp_ptr->b_age) / 2);
			ident = TRUE;
			trap_hit(trap);
			break;
		}

	case TRAP_OF_GROWING:
		{
			s16b tmp;

			msg_print("Heavy fumes sprout out... you feel yourself transmute.");
			if (p_ptr->psex == SEX_FEMALE) tmp = rp_ptr->f_b_ht + rmp_ptr->f_b_ht;
			else tmp = rp_ptr->m_b_ht + rmp_ptr->m_b_ht;

			p_ptr->ht += randint(tmp / 4);
			ident = TRUE;
			trap_hit(trap);
			break;
		}

	case TRAP_OF_SHRINKING:
		{
			s16b tmp;

			msg_print("Heavy fumes sprout out... you feel yourself transmute.");
			if (p_ptr->psex == SEX_FEMALE) tmp = rp_ptr->f_b_ht + rmp_ptr->f_b_ht;
			else tmp = rp_ptr->m_b_ht + rmp_ptr->m_b_ht;

			p_ptr->ht -= randint(tmp / 4);
			if (p_ptr->ht <= tmp / 4) p_ptr->ht = tmp / 4;
			ident = TRUE;
			trap_hit(trap);
			break;
		}

		/* Trap of Divine Anger */
	case TRAP_OF_DIVINE_ANGER:
		{
			if (p_ptr->pgod == 0)
			{
				msg_format("Suddenly you feel glad you're a mere %s", spp_ptr->title);
			}
			else
			{
				cptr name;

				name = deity_info[p_ptr->pgod].name;
				msg_format("You feel you have angered %s.", name);
				inc_piety(p_ptr->pgod, -3000);
			}
			break;
		}

		/* Trap of Divine Wrath */
	case TRAP_OF_DIVINE_WRATH:
		{
			if (p_ptr->pgod == 0)
			{
				msg_format("Suddenly you feel glad you're a mere %s", spp_ptr->title);
			}
			else
			{
				cptr name;

				name = deity_info[p_ptr->pgod].name;

				msg_format("%s quakes in rage: ``Thou art supremely insolent, mortal!!''", name);
				inc_piety(p_ptr->pgod, -500 * p_ptr->lev);
			}
			break;
		}

		/* Trap of hallucination */
	case TRAP_OF_HALLUCINATION:
		{
			msg_print("Scintillating colors hypnotise you for a moment.");

			set_image(80);
		}
		break;

		/* Bolt Trap */
	case TRAP_OF_ROCKET:
		ident = player_handle_breath_trap(1, GF_ROCKET, trap);
		break;
	case TRAP_OF_NUKE_BOLT:
		ident = player_handle_breath_trap(1, GF_NUKE, trap);
		break;
	case TRAP_OF_HOLY_FIRE:
		ident = player_handle_breath_trap(1, GF_HOLY_FIRE, trap);
		break;
	case TRAP_OF_HELL_FIRE:
		ident = player_handle_breath_trap(1, GF_HELL_FIRE, trap);
		break;
	case TRAP_OF_PSI_BOLT:
		ident = player_handle_breath_trap(1, GF_PSI, trap);
		break;
	case TRAP_OF_PSI_DRAIN:
		ident = player_handle_breath_trap(1, GF_PSI_DRAIN, trap);
		break;

		/* Ball Trap */
	case TRAP_OF_NUKE_BALL:
		ident = player_handle_breath_trap(3, GF_NUKE, TRAP_OF_NUKE_BALL);
		break;
	case TRAP_OF_PSI_BALL:
		ident = player_handle_breath_trap(3, GF_PSI, TRAP_OF_NUKE_BALL);
		break;

	default:
		{
			msg_print(format("Executing unknown trap %d", trap));
		}
	}
	return ident;
}

void player_activate_door_trap(s16b y, s16b x)
{
	cave_type *c_ptr;
	bool_ ident = FALSE;

	c_ptr = &cave[y][x];

	/* Return if trap or door not found */
	if ((c_ptr->t_idx == 0) ||
	                !(f_info[c_ptr->feat].flags1 & FF1_DOOR)) return;

	/* Disturb */
	disturb(0);

	/* Message */
	msg_print("You found a trap!");

	/* Pick a trap */
	pick_trap(y, x);

	/* Hit the trap */
	ident = player_activate_trap_type(y, x, NULL, -1);
	if (ident)
	{
		t_info[c_ptr->t_idx].ident = TRUE;
		msg_format("You identified that trap as %s.",
			   t_info[c_ptr->t_idx].name);
	}
}


/*
 * Places a random trap at the given location.
 *
 * The location must be a valid, empty, clean, floor grid.
 */
void place_trap(int y, int x)
{
	s16b trap;
	trap_type *t_ptr;
	int cnt;
	u32b flags;
	cave_type *c_ptr = &cave[y][x];
	dungeon_info_type *d_ptr = &d_info[dungeon_type];

	/* No traps in town or on first level */
	if (dun_level <= 1) return;

	/*
	 * Avoid open doors -- because DOOR flag is added to make much more
	 * important processing faster
	 */
	if (c_ptr->feat == FEAT_OPEN) return;
	if (c_ptr->feat == FEAT_BROKEN) return;

	/* Traps only appears on empty floor */
	if (!cave_floor_grid(c_ptr) &&
	                !(f_info[c_ptr->feat].flags1 & (FF1_DOOR))) return;

	/* Set flags */
	if (f_info[c_ptr->feat].flags1 & FF1_DOOR) flags = FTRAP_DOOR;
	else flags = FTRAP_FLOOR;

	/* Try 100 times */
	cnt = 100;
	while (cnt--)
	{
		trap = randint(max_t_idx - 1);
		t_ptr = &t_info[trap];

		/* No traps below their minlevel */
		if (t_ptr->minlevel > dun_level) continue;

		/* is this a correct trap now?   */
		if (!(t_ptr->flags & flags)) continue;

		/*
		 * Hack -- No trap door at the bottom of dungeon or in flat
		 * (non dungeon) places or on quest levels
		 */
		if ((trap == TRAP_OF_SINKING) &&
		    ((d_ptr->maxdepth == dun_level) ||
		     (dungeon_flags1 & DF1_FLAT) || (is_quest(dun_level))) )
		{
			continue;
		}

		/* How probable is this trap */
		if (rand_int(100) < t_ptr->probability)
		{
			c_ptr->t_idx = trap;
			break;
		}
	}

	return;
}


/*
 * Place a leveled trap at given position
 */
void place_trap_leveled(int y, int x, int lev)
{
	int prev_dun_level = dun_level;
	dun_level = lev;
	place_trap(y,x);
	dun_level = prev_dun_level;
}

/*
 * Places a random trap on the given chest.
 *
 * The object must be a valid chest.
 */
void place_trap_object(object_type *o_ptr)
{
	s16b trap;
	trap_type *t_ptr;
	int cnt;

	/* No traps in town or on first level */
	if (dun_level <= 1)
	{
		/* empty chest were already looted, therefore known */
		o_ptr->ident |= IDENT_KNOWN;
		return;
	}

	/* Try 100 times */
	cnt = 100;
	while (cnt--)
	{
		trap = randint(max_t_idx - 1);
		t_ptr = &t_info[trap];

		/* no traps below their minlevel */
		if (t_ptr->minlevel > dun_level) continue;

		/* Is this a correct trap now? */
		if (!(t_ptr->flags & FTRAP_CHEST)) continue;

		/* How probable is this trap */
		if (rand_int(100) < t_ptr->probability)
		{
			o_ptr->pval = trap;
			break;
		}
	}

	return;
}

/* Dangerous trap placing function */
void wiz_place_trap(int y, int x, int idx)
{
	cave_type *c_ptr = &cave[y][x];

	/* Dangerous enough as it is... */
	if (!cave_floor_grid(c_ptr) && (!(f_info[c_ptr->feat].flags1 & FF1_DOOR))) return;

	c_ptr->t_idx = idx;
}

/*
 * Here begin monster traps code
 */

/*
 * Hook to determine if an object is a device
 */
static bool_ item_tester_hook_device(object_type *o_ptr)
{
	if (((o_ptr->tval == TV_ROD_MAIN) && (o_ptr->pval != 0)) ||
	                (o_ptr->tval == TV_STAFF) ||
	                (o_ptr->tval == TV_WAND)) return (TRUE);

	/* Assume not */
	return (FALSE);
}

/*
 * Hook to determine if an object is a potion
 */
static bool_ item_tester_hook_potion(object_type *o_ptr)
{
	if ((o_ptr->tval == TV_POTION) ||
	                (o_ptr->tval == TV_POTION2)) return (TRUE);

	/* Assume not */
	return (FALSE);
}

/*
 * The trap setting code for rogues -MWK-
 *
 * Also, it will fail or give weird results if the tvals are resorted!
 */
void do_cmd_set_trap(void)
{
	int item_kit, item_load, i;
	int num;

	object_type *o_ptr, *j_ptr, *i_ptr;

	cptr q, s, c;

	object_type object_type_body;

	u32b f1, f2, f3, f4, f5, esp;

	/* Check some conditions */
	if (p_ptr->blind)
	{
		msg_print("You can't see anything.");
		return;
	}
	if (no_lite())
	{
		msg_print("You don't dare to set a trap in the darkness.");
		return;
	}
	if (p_ptr->confused)
	{
		msg_print("You are too confused!");
		return;
	}

	/* Only set traps on clean floor grids */
	if (!cave_clean_bold(p_ptr->py, p_ptr->px))
	{
		msg_print("You cannot set a trap on this.");
		return;
	}

	/* Restrict choices to trapkits */
	item_tester_tval = TV_TRAPKIT;

	/* Get an item */
	q = "Use which trapping kit? ";
	s = "You have no trapping kits.";
	if (!get_item(&item_kit, q, s, USE_INVEN)) return;

	o_ptr = &p_ptr->inventory[item_kit];

	/* Trap kits need a second object */
	switch (o_ptr->sval)
	{
	case SV_TRAPKIT_BOW:
		item_tester_tval = TV_ARROW;
		break;
	case SV_TRAPKIT_XBOW:
		item_tester_tval = TV_BOLT;
		break;
	case SV_TRAPKIT_SLING:
		item_tester_tval = TV_SHOT;
		break;
	case SV_TRAPKIT_POTION:
		item_tester_hook = item_tester_hook_potion;
		break;
	case SV_TRAPKIT_SCROLL:
		item_tester_tval = TV_SCROLL;
		break;
	case SV_TRAPKIT_DEVICE:
		item_tester_hook = item_tester_hook_device;
		break;
	default:
		msg_print("Unknown trapping kit type!");
		break;
	}

	/* Get the second item */
	q = "Load with what? ";
	s = "You have nothing to load that trap with.";
	if (!get_item(&item_load, q, s, USE_INVEN)) return;

	/* Get the second object */
	j_ptr = &p_ptr->inventory[item_load];

	/* Assume a single object */
	num = 1;

	/* In some cases, take multiple objects to load */
	if (o_ptr->sval != SV_TRAPKIT_DEVICE)
	{
		object_flags(o_ptr, &f1, &f2, &f3, &f4, &f5, &esp);

		if ((f3 & TR3_XTRA_SHOTS) && (o_ptr->pval > 0)) num += o_ptr->pval;

		if (f2 & (TRAP2_AUTOMATIC_5 | TRAP2_AUTOMATIC_99)) num = 99;

		if (num > j_ptr->number) num = j_ptr->number;

		c = format("How many (1-%d)? ", num);

		/* Ask for number of items to use */
		num = get_quantity(c, num);
	}

	/* Canceled */
	if (!num) return;

	/* Take a turn */
	energy_use = 100;

	/* Get local object */
	i_ptr = &object_type_body;

	/* Obtain local object for trap content */
	object_copy(i_ptr, j_ptr);

	/* Set number */
	i_ptr->number = num;

	/* Drop it here */
	cave[p_ptr->py][p_ptr->px].special = floor_carry(p_ptr->py, p_ptr->px, i_ptr);

	/* Obtain local object for trap trigger kit */
	object_copy(i_ptr, o_ptr);

	/* Set number */
	i_ptr->number = 1;

	/* Drop it here */
	cave[p_ptr->py][p_ptr->px].special2 = floor_carry(p_ptr->py, p_ptr->px, i_ptr);

	/* Modify, Describe, Optimize */
	inc_stack_size_ex(item_kit, -1, NO_OPTIMIZE, DESCRIBE);
	inc_stack_size_ex(item_load, -num, NO_OPTIMIZE, DESCRIBE);

	for (i = 0; i < INVEN_WIELD; i++)
	{
		if (inven_item_optimize(i)) break;
	}
	for (i = 0; i < INVEN_WIELD; i++)
	{
		inven_item_optimize(i);
	}

	/* Actually set the trap */
	cave_set_feat(p_ptr->py, p_ptr->px, FEAT_MON_TRAP);
}

/*
 * Monster hitting a rod trap -MWK-
 *
 * Return TRUE if the monster died
 */
bool_ mon_hit_trap_aux_rod(int m_idx, object_type *o_ptr)
{
	int dam = 0, typ = 0;
	int rad = 0;
	monster_type *m_ptr = &m_list[m_idx];
	int y = m_ptr->fy;
	int x = m_ptr->fx;

	/* Depend on rod type */
	switch (o_ptr->pval)
	{
	case SV_ROD_DETECT_TRAP:
		m_ptr->smart |= SM_NOTE_TRAP;
		break;
	case SV_ROD_DETECTION:
		m_ptr->smart |= SM_NOTE_TRAP;
		break;
	case SV_ROD_ILLUMINATION:
		typ = GF_LITE_WEAK;
		dam = damroll(2, 15);
		rad = 3;
		lite_room(y, x);
		break;
	case SV_ROD_CURING:
		typ = GF_OLD_HEAL;
		dam = damroll(3, 4);  /* and heal conf? */
		break;
	case SV_ROD_HEALING:
		typ = GF_OLD_HEAL;
		dam = 300;
		break;
	case SV_ROD_SPEED:
		typ = GF_OLD_SPEED;
		dam = 50;
		break;
	case SV_ROD_TELEPORT_AWAY:
		typ = GF_AWAY_ALL;
		dam = MAX_SIGHT * 5;
		break;
	case SV_ROD_DISARMING:
		break;
	case SV_ROD_LITE:
		typ = GF_LITE_WEAK;
		dam = damroll(6, 8);
		break;
	case SV_ROD_SLEEP_MONSTER:
		typ = GF_OLD_SLEEP;
		dam = 50;
		break;
	case SV_ROD_SLOW_MONSTER:
		typ = GF_OLD_SLOW;
		dam = 50;
		break;
	case SV_ROD_DRAIN_LIFE:
		typ = GF_OLD_DRAIN;
		dam = 75;
		break;
	case SV_ROD_POLYMORPH:
		typ = GF_OLD_POLY;
		dam = 50;
		break;
	case SV_ROD_ACID_BOLT:
		typ = GF_ACID;
		dam = damroll(6, 8);
		break;
	case SV_ROD_ELEC_BOLT:
		typ = GF_ELEC;
		dam = damroll(3, 8);
		break;
	case SV_ROD_FIRE_BOLT:
		typ = GF_FIRE;
		dam = damroll(8, 8);
		break;
	case SV_ROD_COLD_BOLT:
		typ = GF_COLD;
		dam = damroll(5, 8);
		break;
	case SV_ROD_ACID_BALL:
		typ = GF_ACID;
		dam = 60;
		rad = 2;
		break;
	case SV_ROD_ELEC_BALL:
		typ = GF_ELEC;
		dam = 32;
		rad = 2;
		break;
	case SV_ROD_FIRE_BALL:
		typ = GF_FIRE;
		dam = 72;
		rad = 2;
		break;
	case SV_ROD_COLD_BALL:
		typ = GF_COLD;
		dam = 48;
		rad = 2;
		break;
	default:
		return (FALSE);
	}

	/* Actually hit the monster */
	if (typ) (void) project( -2, rad, y, x, dam, typ, PROJECT_KILL | PROJECT_ITEM | PROJECT_JUMP);
	return (cave[y][x].m_idx == 0 ? TRUE : FALSE);
}

/*
 * Monster hitting a staff trap -MWK-
 *
 * Return TRUE if the monster died
 */
bool_ mon_hit_trap_aux_staff(int m_idx, object_type *o_ptr)
{
	return (FALSE);
}

/*
 * Monster hitting a scroll trap -MWK-
 *
 * Return TRUE if the monster died
 */
bool_ mon_hit_trap_aux_scroll(int m_idx, int sval)
{
	monster_type *m_ptr = &m_list[m_idx];
	int dam = 0, typ = 0;
	int rad = 0;
	int y = m_ptr->fy;
	int x = m_ptr->fx;
	int k;

	/* Depend on scroll type */
	switch (sval)
	{
	case SV_SCROLL_CURSE_ARMOR:
	case SV_SCROLL_CURSE_WEAPON:
	case SV_SCROLL_TRAP_CREATION:  /* these don't work :-( */
	case SV_SCROLL_WORD_OF_RECALL:  /* should these? */
	case SV_SCROLL_IDENTIFY:
	case SV_SCROLL_STAR_IDENTIFY:
	case SV_SCROLL_MAPPING:
	case SV_SCROLL_DETECT_GOLD:
	case SV_SCROLL_DETECT_ITEM:
	case SV_SCROLL_REMOVE_CURSE:
	case SV_SCROLL_STAR_REMOVE_CURSE:
	case SV_SCROLL_ENCHANT_ARMOR:
	case SV_SCROLL_ENCHANT_WEAPON_TO_HIT:
	case SV_SCROLL_ENCHANT_WEAPON_TO_DAM:
	case SV_SCROLL_STAR_ENCHANT_ARMOR:
	case SV_SCROLL_STAR_ENCHANT_WEAPON:
	case SV_SCROLL_RECHARGING:
	case SV_SCROLL_DETECT_DOOR:
	case SV_SCROLL_DETECT_INVIS:
	case SV_SCROLL_SATISFY_HUNGER:
	case SV_SCROLL_RUNE_OF_PROTECTION:
	case SV_SCROLL_TRAP_DOOR_DESTRUCTION:
	case SV_SCROLL_PROTECTION_FROM_EVIL:
		return (FALSE);
	case SV_SCROLL_DARKNESS:
		unlite_room(y, x);
		typ = GF_DARK_WEAK;
		dam = 10;
		rad = 3;
		break;
	case SV_SCROLL_AGGRAVATE_MONSTER:
		aggravate_monsters(m_idx);
		return (FALSE);
	case SV_SCROLL_SUMMON_MONSTER:
		for (k = 0; k < randint(3) ; k++) summon_specific(y, x, dun_level, 0);
		return (FALSE);
	case SV_SCROLL_SUMMON_UNDEAD:
		for (k = 0; k < randint(3) ; k++) summon_specific(y, x, dun_level, SUMMON_UNDEAD);
		return (FALSE);
	case SV_SCROLL_PHASE_DOOR:
		typ = GF_AWAY_ALL;
		dam = 10;
		break;
	case SV_SCROLL_TELEPORT:
		typ = GF_AWAY_ALL;
		dam = 100;
		break;
	case SV_SCROLL_TELEPORT_LEVEL:
		delete_monster(y, x);
		return (TRUE);
	case SV_SCROLL_LIGHT:
		lite_room(y, x);
		typ = GF_LITE_WEAK;
		dam = damroll(2, 8);
		rad = 2;
		break;
	case SV_SCROLL_DETECT_TRAP:
		m_ptr->smart |= SM_NOTE_TRAP;
		return (FALSE);
	case SV_SCROLL_BLESSING:
		typ = GF_HOLY_FIRE;
		dam = damroll(1, 4);
		break;
	case SV_SCROLL_HOLY_CHANT:
		typ = GF_HOLY_FIRE;
		dam = damroll(2, 4);
		break;
	case SV_SCROLL_HOLY_PRAYER:
		typ = GF_HOLY_FIRE;
		dam = damroll(4, 4);
		break;
	case SV_SCROLL_MONSTER_CONFUSION:
		typ = GF_OLD_CONF;
		dam = damroll(5, 10);
		break;
	case SV_SCROLL_STAR_DESTRUCTION:
		destroy_area(y, x, 15, TRUE, FALSE);
		return (FALSE);
	case SV_SCROLL_DISPEL_UNDEAD:
		typ = GF_DISP_UNDEAD;
		rad = 5;
		dam = 60;
		break;
	case SV_SCROLL_GENOCIDE:
		{
			monster_race *r_ptr = &r_info[m_ptr->r_idx];
			genocide_aux(FALSE, r_ptr->d_char);
			/* although there's no point in a multiple genocide trap... */
			return (!(r_ptr->flags1 & RF1_UNIQUE));
		}
	case SV_SCROLL_MASS_GENOCIDE:
		for (k = 0; k < 8; k++)
			delete_monster(y + ddy[k], x + ddx[k]);
		delete_monster(y, x);
		return (TRUE);
	case SV_SCROLL_ACQUIREMENT:
		acquirement(y, x, 1, TRUE, FALSE);
		return (FALSE);
	case SV_SCROLL_STAR_ACQUIREMENT:
		acquirement(y, x, randint(2) + 1, TRUE, FALSE);
		return (FALSE);
	default:
		return (FALSE);
	}

	/* Actually hit the monster */
	(void) project( -2, rad, y, x, dam, typ, PROJECT_KILL | PROJECT_ITEM | PROJECT_JUMP);
	return (cave[y][x].m_idx == 0 ? TRUE : FALSE);
}

/*
 * Monster hitting a wand trap -MWK-
 *
 * Return TRUE if the monster died
 */
bool_ mon_hit_trap_aux_wand(int m_idx, object_type *o_ptr)
{
	return (FALSE);
}

/*
 * Monster hitting a potions trap -MWK-
 *
 * Return TRUE if the monster died
 */
bool_ mon_hit_trap_aux_potion(int m_idx, object_type *o_ptr)
{
	monster_type *m_ptr = &m_list[m_idx];
	int dam = 0, typ = 0;
	int y = m_ptr->fy;
	int x = m_ptr->fx;
	int sval = o_ptr->sval;

	/* Depend on potion type */
	if (o_ptr->tval == TV_POTION)
	{
		switch (sval)
		{
			/* Nothing happens */
		case SV_POTION_WATER:
		case SV_POTION_APPLE_JUICE:
		case SV_POTION_SLIME_MOLD:
		case SV_POTION_SALT_WATER:
		case SV_POTION_DEC_STR:
		case SV_POTION_DEC_INT:
		case SV_POTION_DEC_WIS:
		case SV_POTION_DEC_DEX:
		case SV_POTION_DEC_CON:
		case SV_POTION_DEC_CHR:
		case SV_POTION_INFRAVISION:
		case SV_POTION_DETECT_INVIS:
		case SV_POTION_SLOW_POISON:
		case SV_POTION_CURE_POISON:
		case SV_POTION_RESIST_HEAT:
		case SV_POTION_RESIST_COLD:
		case SV_POTION_RESTORE_MANA:
		case SV_POTION_RESTORE_EXP:
		case SV_POTION_RES_STR:
		case SV_POTION_RES_INT:
		case SV_POTION_RES_WIS:
		case SV_POTION_RES_DEX:
		case SV_POTION_RES_CON:
		case SV_POTION_RES_CHR:
		case SV_POTION_INC_STR:
		case SV_POTION_INC_INT:
		case SV_POTION_INC_WIS:
		case SV_POTION_INC_DEX:
		case SV_POTION_INC_CON:
		case SV_POTION_INC_CHR:
		case SV_POTION_AUGMENTATION:
		case SV_POTION_RUINATION: 	/* ??? */
		case SV_POTION_ENLIGHTENMENT:
		case SV_POTION_STAR_ENLIGHTENMENT:
		case SV_POTION_SELF_KNOWLEDGE:
			return (FALSE);

		case SV_POTION_EXPERIENCE:
			if (m_ptr->level < MONSTER_LEVEL_MAX)
			{
				m_ptr->exp = monster_exp(m_ptr->level + 1);
				monster_check_experience(m_idx, FALSE);
			}
			return (FALSE);
		case SV_POTION_SLOWNESS:
			typ = GF_OLD_SLOW;
			dam = damroll(4, 6);
			break;
		case SV_POTION_POISON:
			typ = GF_POIS;
			dam = damroll(8, 6);
			break;
		case SV_POTION_CONFUSION:
			typ = GF_CONFUSION;
			dam = damroll(4, 6);
			break;
		case SV_POTION_BLINDNESS:
			typ = GF_DARK;
			dam = 10;
			break;
		case SV_POTION_SLEEP:
			typ = GF_OLD_SLEEP;
			dam = damroll (4, 6);
			break;
		case SV_POTION_LOSE_MEMORIES:
			typ = GF_OLD_CONF;
			dam = damroll(10, 10);
			break;
		case SV_POTION_DETONATIONS:
			typ = GF_DISINTEGRATE;
			dam = damroll(20, 20);
			break;
		case SV_POTION_DEATH:
			typ = GF_NETHER;
			dam = damroll(100, 20);
			break;
		case SV_POTION_BOLDNESS:
			m_ptr->monfear = 0;
			return (FALSE);
		case SV_POTION_SPEED:
			typ = GF_OLD_SPEED;
			dam = damroll(5, 10);
			break;
		case SV_POTION_HEROISM:
		case SV_POTION_BESERK_STRENGTH:
			m_ptr->monfear = 0;
			typ = GF_OLD_HEAL;
			dam = damroll(2, 10);
			break;
		case SV_POTION_CURE_LIGHT:
			typ = GF_OLD_HEAL;
			dam = damroll(3, 4);
			break;
		case SV_POTION_CURE_SERIOUS:
			typ = GF_OLD_HEAL;
			dam = damroll(4, 6);
			break;
		case SV_POTION_CURE_CRITICAL:
			typ = GF_OLD_HEAL;
			dam = damroll(6, 8);
			break;
		case SV_POTION_HEALING:
			typ = GF_OLD_HEAL;
			dam = 300;
			break;
		case SV_POTION_STAR_HEALING:
			typ = GF_OLD_HEAL;
			dam = 1000;
			break;
		case SV_POTION_LIFE:
			{
				monster_race *r_ptr = &r_info[m_ptr->r_idx];
				if (r_ptr->flags3 & RF3_UNDEAD)
				{
					typ = GF_HOLY_FIRE;
					dam = damroll(20, 20);
				}
				else
				{
					typ = GF_OLD_HEAL;
					dam = 5000;
				}
				break;
			}
		default:
			return (FALSE);

		}
	}
	else
	{}

	/* Actually hit the monster */
	(void) project_m( -2, 0, y, x, dam, typ);
	return (cave[y][x].m_idx == 0 ? TRUE : FALSE);
}

/*
 * Monster hitting a monster trap -MWK-
 * Returns True if the monster died, false otherwise
 */
bool_ mon_hit_trap(int m_idx)
{
	monster_type *m_ptr = &m_list[m_idx];
	monster_race *r_ptr = &r_info[m_ptr->r_idx];

	object_type *kit_o_ptr, *load_o_ptr, *j_ptr;

	u32b f1, f2, f3, f4, f5, esp;

	object_type object_type_body;

	int mx = m_ptr->fx;
	int my = m_ptr->fy;

	int difficulty;
	int smartness;

	char m_name[80];

	bool_ notice = FALSE;
	bool_ disarm = FALSE;
	bool_ remove = FALSE;
	bool_ dead = FALSE;
	bool_ fear = FALSE;
	s32b special = 0;

	int dam, chance, shots;
	int mul = 0;
	int breakage = -1;

	int cost = 0;

	/* Get the trap objects */
	kit_o_ptr = &o_list[cave[my][mx].special2];
	load_o_ptr = &o_list[cave[my][mx].special];
	j_ptr = &object_type_body;

	/* Get trap properties */
	object_flags(kit_o_ptr, &f1, &f2, &f3, &f4, &f5, &esp);

	/* Can set off check */
	/* Ghosts only set off Ghost traps */
	if ((r_ptr->flags2 & RF2_PASS_WALL) && !(f2 & TRAP2_KILL_GHOST)) return (FALSE);

	/* Some traps are specialized to some creatures */
	if (f2 & TRAP2_ONLY_MASK)
	{
		bool_ affect = FALSE;
		if ((f2 & TRAP2_ONLY_DRAGON) && (r_ptr->flags3 & RF3_DRAGON)) affect = TRUE;
		if ((f2 & TRAP2_ONLY_DEMON) && (r_ptr->flags3 & RF3_DEMON)) affect = TRUE;
		if ((f2 & TRAP2_ONLY_UNDEAD) && (r_ptr->flags3 & RF3_UNDEAD)) affect = TRUE;
		if ((f2 & TRAP2_ONLY_EVIL) && (r_ptr->flags3 & RF3_EVIL)) affect = TRUE;
		if ((f2 & TRAP2_ONLY_ANIMAL) && (r_ptr->flags3 & RF3_ANIMAL)) affect = TRUE;

		/* Don't set it off if forbidden */
		if (!affect) return (FALSE);
	}

	/* Get detection difficulty */
	difficulty = 25;

	/* Some traps are well-hidden */
	if (f1 & TR1_STEALTH)
	{
		difficulty += 10 * (kit_o_ptr->pval);
	}

	/* Get monster smartness for trap detection */
	/* Higher level monsters are smarter */
	smartness = r_ptr->level;

	/* Smart monsters are better at detecting traps */
	if (r_ptr->flags2 & RF2_SMART) smartness += 10;

	/* Some monsters have already noticed one of out traps */
	if (m_ptr->smart & SM_NOTE_TRAP) smartness += 20;

	/* Stupid monsters are no good at detecting traps */
	if (r_ptr->flags2 & (RF2_STUPID | RF2_EMPTY_MIND)) smartness = -150;

	/* Check if the monster notices the trap */
	if (randint(300) > (difficulty - smartness + 150)) notice = TRUE;

	/* Disarm check */
	if (notice)
	{
		/* The next traps will be easier to spot! */
		m_ptr->smart |= SM_NOTE_TRAP;

		/* Get trap disarming difficulty */
		difficulty = (kit_o_ptr->ac + kit_o_ptr->to_a);

		/* Get monster disarming ability */
		/* Higher level monsters are better */
		smartness = r_ptr->level / 5;

		/* Smart monsters are better at disarming */
		if (r_ptr->flags2 & RF2_SMART) smartness *= 2;

		/* Stupid monsters never disarm traps */
		if (r_ptr->flags2 & RF2_STUPID) smartness = -150;

		/* Nonsmart animals never disarm traps */
		if ((r_ptr->flags3 & RF3_ANIMAL) && !(r_ptr->flags2 & RF2_SMART)) smartness = -150;

		/* Check if the monster disarms the trap */
		if (randint(120) > (difficulty - smartness + 80)) disarm = TRUE;
	}

	/* If disarmed, remove the trap and print a message */
	if (disarm)
	{
		remove = TRUE;

		if (m_ptr->ml)
		{
			/* Get the name */
			monster_desc(m_name, m_ptr, 0);

			/* Print a message */
			msg_format("%^s disarms a trap!", m_name);
		}
	}

	/* Otherwise, activate the trap! */
	else
	{
		/* Message for visible monster */
		if (m_ptr->ml)
		{
			/* Get the name */
			monster_desc(m_name, m_ptr, 0);

			/* Print a message */
			msg_format("%^s sets off a trap!", m_name);
		}
		else
		{
			/* No message if monster isn't visible ? */
		}

		/* Next time be more careful */
		if (randint(100) < 80) m_ptr->smart |= SM_NOTE_TRAP;

		/* Actually activate the trap */
		switch (kit_o_ptr->sval)
		{
		case SV_TRAPKIT_BOW:
		case SV_TRAPKIT_XBOW:
		case SV_TRAPKIT_SLING:
			{
				/* Get number of shots */
				shots = 1;
				if (f3 & TR3_XTRA_SHOTS) shots += kit_o_ptr->pval;
				if (shots <= 0) shots = 1;
				if (shots > load_o_ptr->number) shots = load_o_ptr->number;

				while (shots-- && !dead)
				{
					/* Total base damage */
					dam = damroll(load_o_ptr->dd, load_o_ptr->ds) + load_o_ptr->to_d + kit_o_ptr->to_d;

					/* Total hit probability */
					chance = (kit_o_ptr->to_h + load_o_ptr->to_h + 20) * BTH_PLUS_ADJ;

					/* Damage multiplier */
					if (kit_o_ptr->sval == SV_TRAPKIT_BOW) mul = 3;
					if (kit_o_ptr->sval == SV_TRAPKIT_XBOW) mul = 4;
					if (kit_o_ptr->sval == SV_TRAPKIT_SLING) mul = 2;
					if (f3 & TR3_XTRA_MIGHT) mul += kit_o_ptr->pval;
					if (mul < 0) mul = 0;

					/* Multiply damage */
					dam *= mul;

					/* Check if we hit the monster */
					if (test_hit_fire(chance, r_ptr->ac, TRUE))
					{
						/* Assume a default death */
						cptr note_dies = " dies.";

						/* Some monsters get "destroyed" */
						if ((r_ptr->flags3 & (RF3_DEMON)) ||
						                (r_ptr->flags3 & (RF3_UNDEAD)) ||
						                (r_ptr->flags2 & (RF2_STUPID)) ||
						                (strchr("Evg", r_ptr->d_char)))
						{
							/* Special note at death */
							note_dies = " is destroyed.";
						}

						/* Message if visible */
						if (m_ptr->ml)
						{
							/* describe the monster (again, just in case :-) */
							monster_desc(m_name, m_ptr, 0);

							/* Print a message */
							msg_format("%^s is hit by a missile.", m_name);
						}

						/* Apply slays, brand, critical hits */
						dam = tot_dam_aux(load_o_ptr, dam, m_ptr, &special);
						dam = critical_shot(load_o_ptr->weight, load_o_ptr->to_h, dam, SKILL_ARCHERY);

						/* No negative damage */
						if (dam < 0) dam = 0;

						/* Hit the monster, check for death */
						if (mon_take_hit(m_idx, dam, &fear, note_dies))
						{
							/* Dead monster */
							dead = TRUE;
						}

						/* No death */
						else
						{
							/* Message */
							message_pain(m_idx, dam);

							if (special) attack_special(m_ptr, special, dam);

							/* Take note */
							if (fear && m_ptr->ml)
							{
								/* Message */
								msg_format("%^s flees in terror!", m_name);
							}
						}

					}

					/* Exploding ammo */
					if (load_o_ptr->pval2 != 0)
					{
						int rad = 0;
						int dam = (damroll(load_o_ptr->dd, load_o_ptr->ds) + load_o_ptr->to_d)*2;
						int flag = PROJECT_STOP | PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL |
						           PROJECT_JUMP;

						switch (load_o_ptr->sval)
						{
						case SV_AMMO_LIGHT:
							rad = 2;
							dam /= 2;
							break;
						case SV_AMMO_NORMAL:
							rad = 3;
							break;
						case SV_AMMO_HEAVY:
							rad = 4;
							dam *= 2;
							break;
						}

						project(0, rad, my, mx, dam, load_o_ptr->pval2, flag);

						breakage = 100;
					}
					else
					{
						breakage = breakage_chance(load_o_ptr);
					}

					/* Copy and decrease ammo */
					object_copy(j_ptr, load_o_ptr);

					j_ptr->number = 1;

					load_o_ptr->number--;

					if (load_o_ptr->number <= 0)
					{
						remove = TRUE;
						delete_object_idx(kit_o_ptr->next_o_idx);
						kit_o_ptr->next_o_idx = 0;
					}

					/* Drop (or break) near that location */
					drop_near(j_ptr, breakage, my, mx);

				}

				break;
			}

		case SV_TRAPKIT_POTION:
			{
				/* Get number of shots */
				shots = 1;
				if (f3 & TR3_XTRA_SHOTS) shots += kit_o_ptr->pval;
				if (shots <= 0) shots = 1;
				if (shots > load_o_ptr->number) shots = load_o_ptr->number;

				while (shots-- && !dead)
				{

					/* Message if visible */
					if (m_ptr->ml)
					{
						/* describe the monster (again, just in case :-) */
						monster_desc(m_name, m_ptr, 0);

						/* Print a message */
						msg_format("%^s is hit by fumes.", m_name);
					}

					/* Get the potion effect */
					dead = mon_hit_trap_aux_potion(m_idx, load_o_ptr);

					/* Copy and decrease ammo */
					object_copy(j_ptr, load_o_ptr);

					j_ptr->number = 1;

					load_o_ptr->number--;

					if (load_o_ptr->number <= 0)
					{
						remove = TRUE;
						delete_object_idx(kit_o_ptr->next_o_idx);
						kit_o_ptr->next_o_idx = 0;
					}
				}

				break;
			}

		case SV_TRAPKIT_SCROLL:
			{
				/* Get number of shots */
				shots = 1;
				if (f3 & TR3_XTRA_SHOTS) shots += kit_o_ptr->pval;
				if (shots <= 0) shots = 1;
				if (shots > load_o_ptr->number) shots = load_o_ptr->number;

				while (shots-- && !dead)
				{

					/* Message if visible */
					if (m_ptr->ml)
					{
						/* describe the monster (again, just in case :-) */
						monster_desc(m_name, m_ptr, 0);

						/* Print a message */
						msg_format("%^s activates a spell!", m_name);
					}

					/* Get the potion effect */
					dead = mon_hit_trap_aux_scroll(m_idx, load_o_ptr->sval);

					/* Copy and decrease ammo */
					object_copy(j_ptr, load_o_ptr);

					j_ptr->number = 1;

					load_o_ptr->number--;

					if (load_o_ptr->number <= 0)
					{
						remove = TRUE;
						delete_object_idx(kit_o_ptr->next_o_idx);
						kit_o_ptr->next_o_idx = 0;
					}
				}

				break;
			}

		case SV_TRAPKIT_DEVICE:
			{
				if (load_o_ptr->tval == TV_ROD_MAIN)
				{
					/* Extract mana cost of the rod tip */
					u32b tf1, tf2, tf3, tf4, tf5, tesp;
					object_kind *tip_o_ptr = &k_info[lookup_kind(TV_ROD, load_o_ptr->pval)];
					object_flags(load_o_ptr, &tf1, &tf2, &tf3, &tf4, &tf5, &tesp);
					cost = (tf4 & TR4_CHEAPNESS) ? tip_o_ptr->pval / 2 : tip_o_ptr->pval;
					if (cost <= 0) cost = 1;
				}

				/* Get number of shots */
				shots = 1;
				if (f3 & TR3_XTRA_SHOTS) shots += kit_o_ptr->pval;
				if (shots <= 0) shots = 1;

				if (load_o_ptr->tval == TV_ROD_MAIN)
				{
					if (shots > load_o_ptr->timeout / cost) shots = load_o_ptr->timeout / cost;
				}
				else
				{
					if (shots > load_o_ptr->pval) shots = load_o_ptr->pval;
				}

				while (shots-- && !dead)
				{
					/* Get the effect effect */
					switch (load_o_ptr->tval)
					{
					case TV_ROD_MAIN:
						dead = mon_hit_trap_aux_rod(m_idx, load_o_ptr);
						break;
					case TV_WAND:
						dead = mon_hit_trap_aux_wand(m_idx, load_o_ptr);
						break;
					case TV_STAFF:
						dead = mon_hit_trap_aux_staff(m_idx, load_o_ptr);
						break;
					}

					if (load_o_ptr->tval == TV_ROD_MAIN)
					{
						/* decrease stored mana (timeout) for rods */
						load_o_ptr->timeout -= cost;
					}
					else
					{
						/* decrease charges for wands and staves */
						load_o_ptr->pval--;
					}
				}

				break;
			}

		default:
			msg_print("oops! nonexistant trap!");

		}

		/* Non-automatic traps are removed */
		if (!(f2 & (TRAP2_AUTOMATIC_5 | TRAP2_AUTOMATIC_99)))
		{
			remove = TRUE;
		}
		else if (f2 & TRAP2_AUTOMATIC_5) remove = (randint(5) == 1);

	}

	/* Special trap effect -- teleport to */
	if ((f2 & TRAP2_TELEPORT_TO) && (!disarm) && (!dead))
	{
		teleport_monster_to(m_idx, p_ptr->py, p_ptr->px);
	}

	/* Remove the trap if inactive now */
	if (remove) place_floor_convert_glass(my, mx);

	/* did it die? */
	return (dead);
}