File: spell_effect.c

package info (click to toggle)
crossfire 1.50.0-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 22,092 kB
  • ctags: 7,228
  • sloc: ansic: 77,011; sh: 9,361; perl: 2,426; lex: 2,011; makefile: 1,325
file content (3312 lines) | stat: -rw-r--r-- 116,273 bytes parent folder | download
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
/*
 * static char *rcsid_spell_effect_c =
 *   "$Id: spell_effect.c 12737 2010-03-25 22:25:55Z ryo_saeba $";
 */


/*
    CrossFire, A Multiplayer game for X-windows

    Copyright (C) 2002-2006 Mark Wedel & Crossfire Development Team
    Copyright (C) 1992 Frank Tore Johansen

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    The authors can be reached via e-mail at crossfire-devel@real-time.com
*/

/**
 * @file
 * Various spell effects, non attacks.
 * @todo use the same parameter names/orders.
 */

#include <global.h>
#include <object.h>
#include <living.h>
#ifndef __CEXTRACT__
#include <sproto.h>
#endif
#include <spells.h>
#include <sounds.h>

/**
 * This is really used mostly for spell fumbles and the like.
 *
 * @param op
 * what is casting this.
 * @param tmp
 * object to propagate.
 * @param lvl
 * how nasty should the propagation be.
 */
void cast_magic_storm(object *op, object *tmp, int lvl) {
    if (!tmp)
        return; /* error */
    tmp->level = op->level;
    tmp->range += lvl/5;  /* increase the area of destruction */
    tmp->duration += lvl/5;

    /* Put a cap on duration for this - if the player fails in their
     * apartment, don't want it to go on so long that it kills them
     * multiple times.  Also, damage already increases with level,
     * so don't really need to increase the duration as much either.
     */
    if (tmp->duration >= 40)
        tmp->duration = 40;
    tmp->stats.dam = lvl; /* nasty recoils! */
    tmp->stats.maxhp = tmp->count; /* tract single parent */
    object_insert_in_map_at(tmp, op->map, op, 0, op->x, op->y);
}

/**
 * Recharge wands.
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param spell_ob
 * spell object.
 * @retval 0
 * nothing happened.
 * @retval 1
 * wand was recharged, or destroyed.
 */
int recharge(object *op, object *caster, object *spell_ob) {
    object *wand, *tmp;
    int ncharges;
    char name[MAX_BUF];

    wand = find_marked_object(op);
    if (wand == NULL || wand->type != WAND) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                      "You need to mark the wand you want to recharge.", NULL);
        return 0;
    }
    if (!(random_roll(0, 3, op, PREFER_HIGH))) {
        query_name(wand, name, MAX_BUF);
        draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                             "The %s vibrates violently, then explodes!",
                             "The %s vibrates violently, then explodes!",
                             name);
        play_sound_map(SOUND_TYPE_ITEM, wand, 0, "explode");
        object_remove(wand);
        object_free2(wand, 0);
        tmp = create_archetype("fireball");
        tmp->stats.dam = (spell_ob->stats.dam+SP_level_dam_adjust(caster, spell_ob))/10;
        if (!tmp->stats.dam)
            tmp->stats.dam = 1;
        tmp->stats.hp = tmp->stats.dam/2;
        if (tmp->stats.hp < 2)
            tmp->stats.hp = 2;
        object_insert_in_map_at(tmp, op->map, NULL, 0, op->x, op->y);
        return 1;
    }

    ncharges = (spell_ob->stats.dam+SP_level_dam_adjust(caster, spell_ob));
    if (wand->inv && wand->inv->level)
        ncharges /= wand->inv->level;
    else {
        query_name(wand, name, MAX_BUF);
        draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                             "Your %s is broken.",
                             "Your %s is broken.",
                             name);
        return 0;
    }
    if (!ncharges)
        ncharges = 1;

    wand->stats.food += ncharges;
    query_name(wand, name, MAX_BUF);
    draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
                         "The %s glows with power.",
                         "The %s glows with power.",
                         name);

    if (wand->arch && QUERY_FLAG(&wand->arch->clone, FLAG_ANIMATE)) {
        SET_FLAG(wand, FLAG_ANIMATE);
        wand->speed = wand->arch->clone.speed;
        object_update_speed(wand);
    }
    return 1;
}

/******************************************************************************
 * Start of polymorph related functions.
 *
 * Changed around for 0.94.3 - it will now look through and use all the
 * possible choices for objects/monsters (before it was the first 80 -
 * arbitrary hardcoded limit in this file.)  Doing this will be a bit
 * slower however - while before, it traversed the archetypes once and
 * stored them into an array, it will now potentially traverse it
 * an average of 1.5 times.  This is probably more costly on the polymorph
 * item function, since it is possible a couple lookups might be needed before
 * an item of proper value is generated.
 */

/**
 * Takes a living object (op) and turns it into
 * another monster of some sort.
 * @param op
 * object to try to polymorph. Should be monster.
 * @param level
 * level of the polymorph spell.
 */
static void polymorph_living(object *op, int level) {
    archetype *at;
    int x = op->x, y = op->y, numat = 0, choice, friendly;
    mapstruct *map = op->map;
    object *owner;

    op = HEAD(op);

    /* High level creatures are immune, as are creatures immune to magic.  Otherwise,
     * give the creature a saving throw.
     */
    if (op->level >= level*2
    || did_make_save(op, op->level, op->resist[ATNR_MAGIC]/10)
    || (op->resist[ATNR_MAGIC] == 100))
        return;

    object_remove(op);

    /* First, count up the number of legal matches */
    for (at = first_archetype; at != NULL; at = at->next)
        if ((QUERY_FLAG((&at->clone), FLAG_MONSTER) == QUERY_FLAG(op, FLAG_MONSTER))
        && (object_find_free_spot(&at->clone, map, x, y, 0, SIZEOFFREE) != -1)) {
            numat++;
        }
    if (!numat) {
        object_insert_in_map_at(op, map, NULL, 0, x, y);
        return; /* no valid matches? if so, return */
    }

    /* Next make a choice, and loop through until we get to it */
    choice = rndm(0, numat-1);
    for (at = first_archetype; at != NULL; at = at->next)
        if ((QUERY_FLAG((&at->clone), FLAG_MONSTER) == QUERY_FLAG(op, FLAG_MONSTER)) && (object_find_free_spot(&at->clone, map, x, y, 0, SIZEOFFREE) != -1)) {
            if (!choice)
                break;
            else
                choice--;
        }

    /* Look through the monster.  Unapply anything they have applied,
     * and remove any spells.  Note that if this is extended
     * to players, that would need to get fixed somehow.
     */
    FOR_INV_PREPARE(op, tmp) {
        if (QUERY_FLAG(tmp, FLAG_APPLIED))
            apply_manual(op, tmp, 0);
        if (tmp->type == SPELL) {
            object_remove(tmp);
            object_free2(tmp, 0);
        }
    } FOR_INV_FINISH();

    /* Preserve some values for the new object */
    owner = object_get_owner(op);
    friendly = QUERY_FLAG(op, FLAG_FRIENDLY);
    if (friendly)
        remove_friendly_object(op);

    object_copy(&(at->clone), op);
    if (owner != NULL)
        object_set_owner(op, owner);
    if (friendly) {
        SET_FLAG(op, FLAG_FRIENDLY);
        op->attack_movement = PETMOVE;
        add_friendly_object(op);
    } else
        CLEAR_FLAG(op, FLAG_FRIENDLY);

    /* Put the new creature on the map */
    if ((op = object_insert_in_map_at(op, map, owner, 0, x, y)) == NULL)
        return;

    if (HAS_RANDOM_ITEMS(op))
        create_treasure(op->randomitems, op, GT_INVISIBLE, map->difficulty, 0);

    /* Apply any objects. */
    monster_check_apply_all(op);
}


/**
 * Destroys item from polymorph failure
 * @note
 * We should probably do something
 * more clever ala nethack - create an iron golem or
 * something.
 * @param who
 * who cast the spell.
 * @param op
 * spell victim.
 */
static void polymorph_melt(object *who, object *op) {
    /* Not unique */
    char name[MAX_BUF];

    query_name(op, name, MAX_BUF);
    if (op->nrof > 1)
        draw_ext_info_format(NDI_GREY, 0, who, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                             "The %s glow red, melt and evaporate!",
                             "The %s glow red, melt and evaporate!",
                             name);
    else
        draw_ext_info_format(NDI_GREY, 0, who, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                             "The %s glows red, melts and evaporates!",
                             "The %s glows red, melts and evaporates!",
                             name);
    play_sound_map(SOUND_TYPE_ITEM, op, 0, "evaporate");
    object_remove(op);
    object_free2(op, 0);
    return;
}

/**
 * Changes an item to another item of similar type.
 * @param who
 * spell caster.
 * @param op
 * object being polymorphed. Should be a non living thing.
 * @param level
 * spell level, required for monster resistance.
 */
static void polymorph_item(object *who, object *op, int level) {
    archetype *at;
    int max_value, difficulty, tries = 0, choice, charges = op->stats.food, numat = 0;
    object *new_ob;
    mapstruct *m;
    sint16 x, y;

    /* We try and limit the maximum value of the changed object. */
    max_value = op->value*2;
    if (max_value > 2000*(level/10))
        max_value = 2000*(level/10)+(max_value-2000*(level/10))/3;

    /* Look through and try to find matching items.  Can't turn into something
     * invisible.  Also, if the value is too high now, it would almost
     * certainly be too high below.
     */
    for (at = first_archetype; at != NULL; at = at->next) {
        if (at->clone.type == op->type
        && !at->clone.invisible
        && at->clone.value > 0
        && at->clone.value < max_value
        && !QUERY_FLAG(&at->clone, FLAG_NO_DROP)
        && !QUERY_FLAG(&at->clone, FLAG_STARTEQUIP))
            numat++;
    }

    if (!numat)
        return;

    difficulty = op->magic*5;
    if (difficulty < 0)
        difficulty = 0;
    new_ob = object_new();
    do {
        choice = rndm(0, numat-1);
        for (at = first_archetype; at != NULL; at = at->next) {
            if (at->clone.type == op->type
            && !at->clone.invisible
            && at->clone.value > 0
            && at->clone.value < max_value
            && !QUERY_FLAG(&at->clone, FLAG_NO_DROP)
            && !QUERY_FLAG(&at->clone, FLAG_STARTEQUIP)) {
                if (!choice)
                    break;
                else
                    choice--;
            }
        }
        object_copy(&(at->clone), new_ob);
        fix_generated_item(new_ob, op, difficulty, FABS(op->magic), GT_ENVIRONMENT);
        ++tries;
    } while (new_ob->value > max_value && tries < 10);
    if (new_ob->invisible) {
        LOG(llevError, "polymorph_item: fix_generated_object made %s invisible?!\n", new_ob->name);
        object_free2(new_ob, FREE_OBJ_NO_DESTROY_CALLBACK);
        return;
    }

    /* Unable to generate an acceptable item?  Melt it */
    if (tries == 10) {
        polymorph_melt(who, op);
        object_free2(new_ob, FREE_OBJ_NO_DESTROY_CALLBACK);
        return;
    }

    if (op->nrof && new_ob->nrof) {
        new_ob->nrof = op->nrof;
        /* decrease the number of items */
        if (new_ob->nrof > 2)
            new_ob->nrof -= rndm(0, op->nrof/2-1);
    }

    /* We don't want rings to keep sustenance/hungry status. There are probably
     *  other cases too that should be checked.
     */
    if (charges && op->type != RING && op->type != FOOD)
        op->stats.food = charges;

    x = op->x;
    y = op->y;
    m = op->map;
    object_remove(op);
    object_free2(op, FREE_OBJ_NO_DESTROY_CALLBACK);
    /*
     * Don't want objects merged or re-arranged, as it then messes up the
     * order
     */
    object_insert_in_map_at(new_ob, m, new_ob, INS_NO_MERGE|INS_NO_WALK_ON, x, y);
}

/**
 * Handles polymorphing an object, living or not.
 * Will avoid some specific items (flying arrows and such).
 * @param op
 * object being polymorphed.
 * @param who
 * spell caster.
 * @param level
 * spell level.
 */
void polymorph(object *op, object *who, int level) {
    int tmp;

    /* Can't polymorph players right now */
    /* polymorphing generators opens up all sorts of abuses */
    if (op->type == PLAYER || QUERY_FLAG(op, FLAG_GENERATOR))
        return;

    if (QUERY_FLAG(op, FLAG_MONSTER)) {
        polymorph_living(op, level);
        return;
    }
    /* If it is a living object of some other type, don't handle
     * it now.
     */
    if (QUERY_FLAG(op, FLAG_ALIVE))
        return;

    /* Don't want to morph flying arrows, etc... */
    if (FABS(op->speed) > 0.001 && !QUERY_FLAG(op, FLAG_ANIMATE))
        return;

    /* Do some sanity checking here.  type=0 is unknown, objects
     * without archetypes are not good.  As are a few other
     * cases.
     */
    if (op->type == 0
    || op->arch == NULL
    || QUERY_FLAG(op, FLAG_NO_PICK)
    || op->move_block
    || op->type == TREASURE)
        return;

    tmp = rndm(0, 7);
    if (tmp)
        polymorph_item(who, op, level);
    else
        polymorph_melt(who, op);
}


/**
 * Polymorph spell casting.
 * @param op
 * who is casting the spell.
 * @param caster
 * object used to cast spell.
 * @param spell_ob
 * spell itself.
 * @param dir
 * casting direction. 0 won't have any effect.
 * @return
 * Returns 0 on illegal cast, otherwise 1.
 */
int cast_polymorph(object *op, object *caster, object *spell_ob, int dir) {
    object *tmp;
    int range, mflags, maxrange, level;
    mapstruct *m;

    if (dir == 0)
        return 0;

    maxrange = spell_ob->range+SP_level_range_adjust(caster, spell_ob);
    level = caster_level(caster, spell_ob);
    for (range = 1; range < maxrange; range++) {
        sint16 x = op->x+freearr_x[dir]*range, y = op->y+freearr_y[dir]*range;
        object *image;

        m = op->map;
        mflags = get_map_flags(m, &m, x, y, &x, &y);

        if (mflags&(P_NO_MAGIC|P_OUT_OF_MAP))
            break;

        if (GET_MAP_MOVE_BLOCK(m, x, y)&MOVE_FLY_LOW)
            break;

        /* Get the top most object */
        for (tmp = GET_MAP_OB(m, x, y); tmp != NULL && tmp->above != NULL; tmp = tmp->above)
            ;

        /* Now start polymorphing the objects, top down */
        FOR_OB_AND_BELOW_PREPARE(tmp) {
            /* Once we find the floor, no need to go further */
            if (QUERY_FLAG(tmp, FLAG_IS_FLOOR))
                break;
            polymorph(tmp, op, level);
        } FOR_OB_AND_BELOW_FINISH();
        image = arch_to_object(spell_ob->other_arch);
        image->stats.food = 5;
        image->speed_left = 0.1;
        object_insert_in_map_at(image, m, op, 0, x, y);
    }
    return 1;
}

/**
 * Create a missile (nonmagic - magic +4). Will either create bolts or arrows
 * based on whether a crossbow or bow is equiped. If neither, it defaults to
 * arrows.
 * Sets the plus based on the casters level. It is also settable with the
 * invoke command. If the caster attempts to create missiles with too
 * great a plus, the default is used.
 * The # of arrows created also goes up with level, so if a 30th level mage
 * wants LOTS of arrows, and doesn't care what the plus is he could
 * create nonnmagic arrows, or even -1, etc...
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param spell
 * actual spell object.
 * @param dir
 * casting direction.
 * @param stringarg
 * optional parameter specifying what kind of items to create.
 * @retval 0
 * no missile created.
 * @retval
 * missiles were created.
 */
int cast_create_missile(object *op, object *caster, object *spell, int dir, const char *stringarg) {
    int missile_plus = 0, bonus_plus = 0;
    const char *missile_name;
    object *tmp, *missile;
    tag_t tag;

    tmp = object_find_by_type_applied(op, BOW);
    missile_name = tmp != NULL ? tmp->race : "arrow";

    missile_plus = spell->stats.dam+SP_level_dam_adjust(caster, spell);

    if (!strcmp(missile_name, "arrows"))
        missile_name = "arrow";
    else if (!strcmp(missile_name, "crossbow bolts"))
        missile_name = "bolt";

    if (find_archetype(missile_name) == NULL) {
        LOG(llevDebug, "Cast create_missile: could not find archetype %s\n", missile_name);
        return 0;
    }
    missile = create_archetype(missile_name);

    if (stringarg) {
        /* If it starts with a letter, presume it is a description */
        if (isalpha(*stringarg)) {
            artifact *al = find_artifactlist(missile->type)->items;

            for (; al != NULL; al = al->next)
                if (!strcasecmp(al->item->name, stringarg))
                    break;

            if (!al) {
                object_free2(missile, FREE_OBJ_NO_DESTROY_CALLBACK);
                draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                                     "No such object %ss of %s",
                                     "No such object %ss of %s",
                                     missile_name, stringarg);
                return 0;
            }
            if (al->item->slaying) {
                object_free2(missile, FREE_OBJ_NO_DESTROY_CALLBACK);
                draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                                     "You are not allowed to create %ss of %s",
                                     "You are not allowed to create %ss of %s",
                                     missile_name, stringarg);
                return 0;
            }
            give_artifact_abilities(missile, al->item);
            /* These special arrows cost something extra.  Don't have them also be
            * magical - otherwise, in most cases, not enough will be created.
            * I don't want to get into the parsing both plus and type.
            */
            bonus_plus = 1+(al->item->value/5);
            missile_plus = 0;
        } else if (atoi(stringarg) < missile_plus)
            missile_plus = atoi(stringarg);
    }
    if (missile_plus > 4)
        missile_plus = 4;
    else if (missile_plus < -4)
        missile_plus = -4;

    missile->nrof = spell->duration+SP_level_duration_adjust(caster, spell);
    if (missile->nrof <= 3*(missile_plus+bonus_plus)) {
        object_free2(missile, FREE_OBJ_NO_DESTROY_CALLBACK);
        draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                             "This item is too powerful for you to create!",
                             NULL);
        return 0;
    }
    missile->nrof -= 3*(missile_plus+bonus_plus);
    if (missile->nrof < 1)
        missile->nrof = 1;

    missile->magic = missile_plus;
    /* Can't get any money for these objects */
    missile->value = 0;

    SET_FLAG(missile, FLAG_IDENTIFIED);
    tag = missile->count;

    if (!cast_create_obj(op, missile, dir)
    && op->type == PLAYER
    && !object_was_destroyed(missile, tag)) {
        pick_up(op, missile);
    }
    return 1;
}


/**
 * Create food.
 * Allows the choice of what sort of food object to make.
 * If stringarg is NULL, it will create food dependent on level  --PeterM
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param spell_ob
 * actual spell object.
 * @param dir
 * casting direction.
 * @param stringarg
 * optional parameter specifying what kind of items to create.
 * @retval 0
 * no food created.
 * @retval
 * food was created.
 */
int cast_create_food(object *op, object *caster, object *spell_ob, int dir, const char *stringarg) {
    int food_value;
    archetype *at = NULL;
    object *new_op;

    food_value = spell_ob->stats.food+50*SP_level_duration_adjust(caster, spell_ob);

    if (stringarg) {
        at = find_archetype_by_object_type_name(FOOD, stringarg);
        if (at == NULL)
            at = find_archetype_by_object_type_name(DRINK, stringarg);
        if (at == NULL || at->clone.stats.food > food_value)
            stringarg = NULL;
    }

    if (!stringarg) {
        archetype *at_tmp;

        /* We try to find the archetype with the maximum food value.
         * This removes the dependency of hard coded food values in this
         * function, and addition of new food types is automatically added.
         * We don't use flesh types because the weight values of those need
         * to be altered from the donor.
         */

        /* We assume the food items don't have multiple parts */
        for (at_tmp = first_archetype; at_tmp != NULL; at_tmp = at_tmp->next) {
            if (at_tmp->clone.type == FOOD || at_tmp->clone.type == DRINK) {
                /* Basically, if the food value is something that is creatable
                 * under the limits of the spell and it is higher than
                 * the item we have now, take it instead.
                 */
                if (at_tmp->clone.stats.food <= food_value
                && (!at || at_tmp->clone.stats.food > at->clone.stats.food))
                    at = at_tmp;
            }
        }
    }
    /* Pretty unlikely (there are some very low food items), but you never
     * know
     */
    if (!at) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                      "You don't have enough experience to create any food.", NULL);
        return 0;
    }

    food_value /= at->clone.stats.food;
    new_op = object_new();
    object_copy(&at->clone, new_op);
    new_op->nrof = food_value;

    new_op->value = 0;
    if (new_op->nrof < 1)
        new_op->nrof = 1;

    cast_create_obj(op, new_op, dir);
    return 1;
}

/**
 * Try to get information about a living thing.
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param spell_ob
 * spell object being cast.
 * @param dir
 * cast direction.
 * @retval 0
 * nothing probed.
 * @retval 1
 * something was probed.
 */
int probe(object *op, object *caster, object *spell_ob, int dir) {
    int r, mflags, maxrange;
    mapstruct *m;

    if (!dir) {
        examine_monster(op, op);
        return 1;
    }
    maxrange = spell_ob->range+SP_level_range_adjust(caster, spell_ob);
    for (r = 1; r < maxrange; r++) {
        sint16 x = op->x+r*freearr_x[dir], y = op->y+r*freearr_y[dir];

        m = op->map;
        mflags = get_map_flags(m, &m, x, y, &x, &y);

        if (mflags&P_OUT_OF_MAP)
            break;

        if (!QUERY_FLAG(op, FLAG_WIZCAST) && (mflags&P_NO_MAGIC)) {
            draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                          "Something blocks your magic.", NULL);
            return 0;
        }
        if (mflags&P_IS_ALIVE) {
            FOR_MAP_PREPARE(m, x, y, tmp)
                if (QUERY_FLAG(tmp, FLAG_ALIVE) && (tmp->type == PLAYER || QUERY_FLAG(tmp, FLAG_MONSTER))) {
                    draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
                                  "You detect something.", NULL);
                    examine_monster(op, HEAD(tmp));
                    return 1;
                }
            FOR_MAP_FINISH();
        }
    }
    draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                  "You detect nothing.", NULL);
    return 1;
}


/**
 * This checks to see if 'pl' is invisible to 'mon'.
 * Does race check, undead check, etc
 * Returns TRUE if mon can't see pl, false
 * otherwise.  This doesn't check range, walls, etc.  It
 * only checks the racial adjustments, and in fact that
 * pl is invisible.
 *
 * @param pl
 * potentially invisible object.
 * @param mon
 * who may see pl.
 * @retval 0
 * mon can see pl.
 * @retval 1
 * mon can't see pl.
 */
int makes_invisible_to(object *pl, object *mon) {
    if (!pl->invisible)
        return 0;
    if (pl->type == PLAYER) {
        /* If race isn't set, then invisible unless it is undead */
        if (!pl->contr->invis_race) {
            if (QUERY_FLAG(mon, FLAG_UNDEAD))
                return 0;
            return 1;
        }
        /* invis_race is set if we get here */
        if (!strcmp(pl->contr->invis_race, "undead") && is_true_undead(mon))
            return 1;
        /* No race, can't be invisible to it */
        if (!mon->race)
            return 0;
        if (strstr(mon->race, pl->contr->invis_race))
            return 1;
        /* Nothing matched above, return 0 */
        return 0;
    } else {
        /* monsters are invisible to everything */
        return 1;
    }
}

/**
 * Makes the player or character invisible.
 * Note the spells to 'stack', but perhaps in odd ways.
 * the duration for all is cumulative.
 * In terms of invis undead/normal invis, it is the last one cast that
 * will determine if you are invisible to undead or normal monsters.
 * For improved invis, if you cast it with a one of the others, you
 * lose the improved part of it, and the above statement about undead/
 * normal applies.
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param spell_ob
 * actual spell object.
 * @retval 0
 * invisibility was already in action.
 * @retval 1
 * op is now invisible.
 */
int cast_invisible(object *op, object *caster, object *spell_ob) {
    object *tmp;

    if (op->invisible > 1000) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                      "You can not extend the duration of your invisibility any further", NULL);
        return 0;
    }

    /* Remove the switch with 90% duplicate code - just handle the differences with
     * and if statement or two.
     */
    op->invisible += spell_ob->duration+SP_level_duration_adjust(caster, spell_ob);
    /* max duration */
    if (op->invisible > 1000)
        op->invisible = 1000;

    if (op->type == PLAYER) {
        if (op->contr->invis_race)
            FREE_AND_CLEAR_STR(op->contr->invis_race);
        if (spell_ob->race)
            op->contr->invis_race = add_refcount(spell_ob->race);
        if (QUERY_FLAG(spell_ob, FLAG_MAKE_INVIS))
            op->contr->tmp_invis = 0;
        else
            op->contr->tmp_invis = 1;

        op->contr->hidden = 0;
    }
    if (makes_invisible_to(op, op))
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
                      "You can't see your hands!", NULL);
    else
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
                      "You feel more transparent!", NULL);

    object_update(op, UP_OBJ_FACE);

    /* Only search the active objects - only these should actually do
     * harm to the player.
     */
    for (tmp = active_objects; tmp != NULL; tmp = tmp->active_next)
        if (tmp->enemy == op)
            object_set_enemy(tmp, NULL);
    return 1;
}

/**
 * Basically destroys earthwalls in the area.
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param spell_ob
 * actual spell object.
 * @retval 0
 * op isn't a player.
 * @retval 1
 * op is a player.
 */
int cast_earth_to_dust(object *op, object *caster, object *spell_ob) {
    int range, i, j, mflags;
    sint16 sx, sy;
    mapstruct *m;

    if (op->type != PLAYER)
        return 0;

    range = spell_ob->range+SP_level_range_adjust(caster, spell_ob);

    for (i = -range; i < range; i++)
        for (j = -range; j < range; j++) {
            sx = op->x+i;
            sy = op->y+j;
            m = op->map;
            mflags = get_map_flags(m, &m, sx, sy, &sx, &sy);

            if (mflags&P_OUT_OF_MAP)
                continue;

            /* If the space doesn't block, no wall here to remove
             * Don't care too much what it blocks - this allows for
             * any sort of earthwall/airwall/waterwall, etc
             * type effects.
             */
            if (GET_MAP_MOVE_BLOCK(m, sx, sy)) {
                FOR_MAP_PREPARE(m, sx, sy, tmp)
                    if (tmp && QUERY_FLAG(tmp, FLAG_TEAR_DOWN))
                        hit_player(tmp, 9998, op, AT_PHYSICAL, 0);
                FOR_MAP_FINISH();
            }
        }
    return 1;
}

/**
 * Word of recall causes the player to return 'home'.
 * we put a force into the player object, so that there is a
 * time delay effect.
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param spell_ob
 * actual spell object.
 * @retval 0
 * op isn't a player.
 * @retval 1
 * word of recall initiated.
 */
int cast_word_of_recall(object *op, object *caster, object *spell_ob) {
    object *dummy;
    int time;

    if (op->type != PLAYER)
        return 0;

    if (object_find_by_type_subtype(op, SPELL_EFFECT, SP_WORD_OF_RECALL)) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
                      "You feel a force starting to build up inside you.", NULL);
        return 1;
    }

    dummy = create_archetype(FORCE_NAME);
    if (dummy == NULL) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                      "Oops, program error!", NULL);
        LOG(llevError, "cast_word_of_recall: create_archetype(force) failed!\n");
        return 0;
    }
    time = spell_ob->duration-SP_level_duration_adjust(caster, spell_ob);
    if (time < 1)
        time = 1;

    /* value of speed really doesn't make much difference, as long as it is
     * positive.  Lower value may be useful so that the problem doesn't
     * do anything really odd if it say a -1000 or something.
     */
    dummy->speed = 0.002;
    object_update_speed(dummy);
    dummy->speed_left = -dummy->speed*time;
    dummy->type = SPELL_EFFECT;
    dummy->subtype = SP_WORD_OF_RECALL;

    /* If we could take advantage of enter_player_savebed() here, it would be
     * nice, but until the map load fails, we can't.
     */
    EXIT_PATH(dummy) = add_string(op->contr->savebed_map);
    EXIT_X(dummy) = op->contr->bed_x;
    EXIT_Y(dummy) = op->contr->bed_y;

    (void)object_insert_in_ob(dummy, op);
    draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
                  "You feel a force starting to build up inside you.", NULL);
    return 1;
}

/**
 * wonder is really just a spell that will likely cast another
 * spell.
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param dir
 * casting direction.
 * @param spell_ob
 * actual spell object.
 * @todo
 * doesn't it decrease sp without checking?
 */
int cast_wonder(object *op, object *caster, int dir, object *spell_ob) {
    object *newspell;

    if (!rndm(0, 3))
        return cast_cone(op, caster, dir, spell_ob);

    if (spell_ob->randomitems) {
        newspell = generate_treasure(spell_ob->randomitems, caster->level);
        if (!newspell) {
            LOG(llevError, "cast_wonder: Unable to get a spell!\n");
            return 0;
        }
        if (newspell->type != SPELL) {
            LOG(llevError, "cast_wonder: spell returned is not a spell (%d, %s)!\n", newspell->type, newspell->name);
            return 0;
        }
        /* Prevent inifinite recursion */
        if (newspell->subtype == SP_WONDER) {
            LOG(llevError, "cast_wonder: spell returned is another wonder spell!\n");
            return 0;
        }
        return cast_spell(op, caster, dir, newspell, NULL);
    }
    return 1;
}

/**
 * Living thing wants to know information.
 *
 * @param op
 * who wants information.
 * @return
 * 1.
 */
int perceive_self(object *op) {
    char cp[VERY_BIG_BUF], buf[MAX_BUF];
    archetype *at = find_archetype(ARCH_DEPLETION);
    object *tmp;
    const object *god;
    int i;

    describe_item(op, op, cp, VERY_BIG_BUF);

    god = find_god(determine_god(op));
    if (god)
        draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_PERCEIVE_SELF,
                             "You worship %s",
                             "You worship %s",
                             god->name);
    else
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_PERCEIVE_SELF,
                      "You worship no god", NULL);

    tmp = arch_present_in_ob(at, op);

    if (*cp == '\0' && tmp == NULL)
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_PERCEIVE_SELF,
                      "You feel very mundane", NULL);
    else {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_PERCEIVE_SELF,
                      "You have:", NULL);
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_PERCEIVE_SELF,
                      cp, cp);
        if (tmp != NULL) {
            for (i = 0; i < NUM_STATS; i++) {
                if (get_attr_value(&tmp->stats, i) < 0) {
                    draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_PERCEIVE_SELF,
                                         "Your %s is depleted by %d",
                                         "Your %s is depleted by %d",
                                         statname[i], -(get_attr_value(&tmp->stats, i)));
                }
            }
        }
    }
    if (op->glow_radius > 0)
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_PERCEIVE_SELF,
                      "You glow in the dark.", NULL);

    if (is_dragon_pl(op)) {
        /* now grab the 'dragon_ability'-force from the player's inventory */
        tmp = object_find_by_type_and_arch_name(op, FORCE, "dragon_ability_force");
        if (tmp != NULL) {
            if (tmp->stats.exp == 0) {
                snprintf(buf, sizeof(buf), "Your metabolism isn't focused on anything.");
            } else {
                snprintf(buf, sizeof(buf), "Your metabolism is focused on %s.", change_resist_msg[tmp->stats.exp]);
            }
            draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_PERCEIVE_SELF,
                          buf, buf);
        }
    }
    return 1;
}

/**
 * This function cast the spell of town portal for op.
 *
 * The spell operates in two passes. During the first one a place
 * is marked as a destination for the portal. During the second one,
 * 2 portals are created, one in the position the player cast it and
 * one in the destination place. The portal are synchronized and 2 forces
 * are inserted in the player to destruct the portal next time player
 * creates a new portal pair.
 * This spell has a side effect that it allows people to meet each other
 * in a permanent, private, apartments by making a town portal from it
 * to the town or another public place. So, check if the map is unique and if
 * so return an error
 *
 * Code by Tchize (david.delbecq@usa.net)
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param spell
 * actual spell object.
 * @param dir
 * casting direction.
 * @retval 0
 * spell was insuccessful for some reason.
 * @retval 1
 * spell worked.
 */
int cast_create_town_portal(object *op, object *caster, object *spell, int dir) {
    object *dummy, *force, *old_force, *tmp;
    archetype *perm_portal;
    char portal_name [1024], portal_message [1024];
    sint16 exitx, exity;
    mapstruct *exitmap;
    int op_level, x, y;

    /* Check to see if the map the player is currently on is a per player unique
     * map.  This can be determined in that per player unique maps have the
     * full pathname listed. Ignore if settings.create_home_portals is true.
     */
    if (!settings.create_home_portals) {
        if (!strncmp(op->map->path, settings.localdir, strlen(settings.localdir))) {
            draw_ext_info(NDI_UNIQUE|NDI_NAVY, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                          "You can't cast that here.", NULL);
            return 0;
        }
    }

    /* Check to see if the player is on a transport */
    if (op->contr && op->contr->transport) {
        draw_ext_info(NDI_UNIQUE|NDI_NAVY, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                      "You need to exit the transport to cast that.", NULL);
        return 0;
    }

    /* The first thing to do is to check if we have a marked destination
     * dummy is used to make a check inventory for the force
     */
    dummy = arch_to_object(spell->other_arch);
    if (dummy == NULL) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                      "Oops, program error!", NULL);
        LOG(llevError, "object_new failed (force in cast_create_town_portal for %s!\n", op->name);
        return 0;
    }
    force = check_inv_recursive(op, dummy);

    if (force == NULL) {
        /* Here we know there is no destination marked up.
         * We have 2 things to do:
         * 1. Mark the destination in the player inventory.
         * 2. Let the player know it worked.
         */
        free_string(dummy->name);
        dummy->name = add_string(op->map->path);
        EXIT_X(dummy) = op->x;
        EXIT_Y(dummy) = op->y;
        dummy->weapontype = op->map->last_reset_time.tv_sec;
        object_insert_in_ob(dummy, op);
        draw_ext_info(NDI_UNIQUE|NDI_NAVY, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
                      "You fix this place in your mind and feel that you "
                      "can come here from anywhere.",
                      NULL);
        return 1;
    }
    object_free2(dummy, FREE_OBJ_NO_DESTROY_CALLBACK);

    /* Here we know where the town portal should go to
     * We should kill any existing portal associated with the player.
     * Than we should create the 2 portals.
     * For each of them, we need:
     *    - To create the portal with the name of the player+destination map
     *    - set the owner of the town portal
     *    - To mark the position of the portal in the player's inventory
     *      for easier destruction.
     *
     *  The mark works has follow:
     *   slaying: Existing town portal
     *   hp, sp : x & y of the associated portal
     *   name   : name of the portal
     *   race   : map the portal is in
     */

    /* First step: killing existing town portals */
    dummy = create_archetype(spell->race);
    if (dummy == NULL) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                      "Oops, program error!", NULL);
        LOG(llevError, "object_new failed (force) in cast_create_town_portal for %s!\n", op->name);
        return 0;
    }
    perm_portal = find_archetype(spell->slaying);

    /* To kill a town portal, we go trough the player's inventory,
     * for each marked portal in player's inventory,
     *   -We try load the associated map (if impossible, consider the portal destructed)
     *   -We find any portal in the specified location.
     *      If it has the good name, we destruct it.
     *   -We destruct the force indicating that portal.
     */
    while ((old_force = check_inv_recursive(op, dummy))) {
        exitx = EXIT_X(old_force);
        exity = EXIT_Y(old_force);
        LOG(llevDebug, "Trying to kill a portal in %s (%d,%d)\n", old_force->race, exitx, exity);

        if (!strncmp(old_force->race, settings.localdir, strlen(settings.localdir)))
            exitmap = ready_map_name(old_force->race, MAP_PLAYER_UNIQUE);
        else
            exitmap = ready_map_name(old_force->race, 0);

        if (exitmap) {
            tmp = map_find_by_archetype(exitmap, exitx, exity, perm_portal);
            FOR_OB_AND_ABOVE_PREPARE(tmp)
                if (tmp->name == old_force->name) {
                    object_remove(tmp);
                    object_free2(tmp, 0);
                    break;
                }
            FOR_OB_AND_ABOVE_FINISH();

            /* kill any opening animation there is */
            tmp = map_find_by_archetype(exitmap, exitx, exity, find_archetype("town_portal_open"));
            FOR_OB_AND_ABOVE_PREPARE(tmp)
                if (tmp->name == old_force->name) {
                object_remove(tmp);
                object_free2(tmp, FREE_OBJ_FREE_INVENTORY);
                break;
            }
            FOR_OB_AND_ABOVE_FINISH();
        }
        object_remove(old_force);
        object_free2(old_force, 0);
        LOG(llevDebug, "\n");
    }
    object_free2(dummy, FREE_OBJ_NO_DESTROY_CALLBACK);

    /* Creating the portals.
     * The very first thing to do is to ensure
     * access to the destination map.
     * If we can't, don't fizzle. Simply warn player.
     * This ensure player pays his mana for the spell
     * because HE is responsible for forgetting.
     * 'force' is the destination of the town portal, which we got
     * from the players inventory above.
     */

    /* Ensure exit map is loaded*/
    if (!strncmp(force->name, settings.localdir, strlen(settings.localdir)))
        exitmap = ready_map_name(force->name, MAP_PLAYER_UNIQUE);
    else
        exitmap = ready_map_name(force->name, 0);

    /* If we were unable to load (ex. random map deleted), warn player*/
    if (exitmap == NULL) {
        draw_ext_info(NDI_UNIQUE|NDI_NAVY, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                      "Something strange happens. You can't remember where to go!?",
                      NULL);
        object_remove(force);
        object_free2(force, 0);
        return 1;
    } else if (exitmap->last_reset_time.tv_sec != force->weapontype) {
        draw_ext_info(NDI_UNIQUE|NDI_NAVY, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                      "The spell effect has expired.", NULL);
        object_remove(force);
        object_free2(force, 0);
        return 1;
    }

    op_level = caster_level(caster, spell);
    if (op_level < 15)
        snprintf(portal_message, 1024, "\nThe air moves around you and\na huge smell of ammonia\nsurounds you as you pass\nthrough %s's tiny portal\nPouah!\n", op->name);
    else if (op_level < 30)
        snprintf(portal_message, 1024, "\n%s's portal smells of ozone.\nYou do a lot of movements and finally pass\nthrough the small hole in the air\n", op->name);
    else if (op_level < 60)
        snprintf(portal_message, 1024, "\nA shining door opens in the air in front of you,\nshowing you the path to another place.\n");
    else
        snprintf(portal_message, 1024, "\nAs you walk through %s's portal, flowers come out\nfrom the ground around you.\nYou feel awed.\n", op->name);

    /* Create a portal in front of player
     * dummy contain the portal and
     * force contain the track to kill it later
     */

    snprintf(portal_name, 1024, "%s's portal to %s", op->name, force->name);
    dummy = create_archetype(spell->slaying); /*The portal*/
    if (dummy == NULL) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                      "Oops, program error!", NULL);
        LOG(llevError, "object_new failed (perm_magic_portal) in cast_create_town_portal for %s!\n", op->name);
        return 0;
    }
    EXIT_PATH(dummy) = add_string(force->name);
    EXIT_X(dummy) = EXIT_X(force);
    EXIT_Y(dummy) = EXIT_Y(force);
    FREE_AND_COPY(dummy->name, portal_name);
    FREE_AND_COPY(dummy->name_pl, portal_name);
    object_set_msg(dummy, portal_message);
    dummy->race = add_string(op->name);   /*Save the owner of the portal*/

    /* create a nice animation */
    tmp = create_archetype("town_portal_open");
    FREE_AND_COPY(tmp->name, portal_name);
    FREE_AND_COPY(tmp->name_pl, portal_name);
    object_insert_in_ob(dummy, tmp);
    /* and put it on the floor, when it ends the portal will be on the ground */
    cast_create_obj(op, tmp, 0);
    x = tmp->x;
    y = tmp->y;

    /* Now we need to to create a town portal marker inside the player
     * object, so on future castings, we can know that he has an active
     * town portal.
     */
    tmp = create_archetype(spell->race);
    if (tmp == NULL) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                      "Oops, program error!", NULL);
        LOG(llevError, "object_new failed (force) in cast_create_town_portal for %s!\n", op->name);
        return 0;
    }
    tmp->race = add_string(op->map->path);
    FREE_AND_COPY(tmp->name, portal_name);
    EXIT_X(tmp) = x;
    EXIT_Y(tmp) = y;
    object_insert_in_ob(tmp, op);

    /* Create a portal in the destination map
     * dummy contain the portal and
     * force the track to kill it later
     * the 'force' variable still contains the 'reminder' of
     * where this portal goes to.
     */
    snprintf(portal_name, 1024, "%s's portal to %s", op->name, op->map->path);
    dummy = create_archetype(spell->slaying);  /*The portal*/
    if (dummy == NULL) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                      "Oops, program error!", NULL);
        LOG(llevError, "object_new failed (perm_magic_portal) in cast_create_town_portal for %s!\n", op->name);
        return 0;
    }
    EXIT_PATH(dummy) = add_string(op->map->path);
    EXIT_X(dummy) = op->x;
    EXIT_Y(dummy) = op->y;
    FREE_AND_COPY(dummy->name, portal_name);
    FREE_AND_COPY(dummy->name_pl, portal_name);
    object_set_msg(dummy, portal_message);
    dummy->race = add_string(op->name);   /*Save the owner of the portal*/

    /* animation here too */
    tmp = create_archetype("town_portal_open");
    FREE_AND_COPY(tmp->name, portal_name);
    FREE_AND_COPY(tmp->name_pl, portal_name);
    object_insert_in_ob(dummy, tmp);
    /* and put it on the floor, when it ends the portal will be on the ground */
    object_insert_in_map_at(tmp, exitmap, op, 0, EXIT_X(force), EXIT_Y(force));
    x = tmp->x;
    y = tmp->y;

    /* Now we create another town portal marker that
     * points back to the one we just made
     */
    tmp = create_archetype(spell->race);
    if (tmp == NULL) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                      "Oops, program error!", NULL);
        LOG(llevError, "object_new failed (force) in cast_create_town_portal for %s!\n", op->name);
        return 0;
    }
    tmp->race = add_string(force->name);
    FREE_AND_COPY(tmp->name, portal_name);
    EXIT_X(tmp) = x;
    EXIT_Y(tmp) = y;
    object_insert_in_ob(tmp, op);

    /* Describe the player what happened
     */
    draw_ext_info(NDI_UNIQUE|NDI_NAVY, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
                  "You see air moving and showing you the way home.", NULL);
    object_remove(force); /* Delete the force inside the player*/
    object_free2(force, 0);
    return 1;
}


/**
 * This creates magic walls.  Really, it can create most any object,
 * within some reason.
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param dir
 * casting direction.
 * @param spell_ob
 * actual spell object.
 * @retval 0
 * spell failed.
 * @retval 1
 * spell was successful.
 */
int magic_wall(object *op, object *caster, int dir, object *spell_ob) {
    object *tmp, *tmp2;
    int i, posblocked, negblocked, maxrange;
    sint16 x, y;
    mapstruct *m;
    const char *name;
    archetype *at;

    if (!dir) {
        dir = op->facing;
        x = op->x;
        y = op->y;
    } else {
        x = op->x+freearr_x[dir];
        y = op->y+freearr_y[dir];
    }
    m = op->map;

    if ((spell_ob->move_block || x != op->x || y != op->y)
    && (get_map_flags(m, &m, x, y, &x, &y)&(P_OUT_OF_MAP|P_IS_ALIVE)
    || ((spell_ob->move_block&GET_MAP_MOVE_BLOCK(m, x, y)) == spell_ob->move_block))) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                      "Something is in the way.", NULL);
        return 0;
    }
    if (spell_ob->other_arch) {
        tmp = arch_to_object(spell_ob->other_arch);
    } else if (spell_ob->race) {
        char buf1[MAX_BUF];

        snprintf(buf1, sizeof(buf1), spell_ob->race, dir);
        at = find_archetype(buf1);
        if (!at) {
            LOG(llevError, "summon_wall: Unable to find archetype %s\n", buf1);
            draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                          "This spell is broken.", NULL);
            return 0;
        }
        tmp = arch_to_object(at);
    } else {
        LOG(llevError, "magic_wall: spell %s lacks other_arch\n", spell_ob->name);
        return 0;
    }

    if (tmp->type == SPELL_EFFECT) {
        tmp->attacktype = spell_ob->attacktype;
        tmp->duration = spell_ob->duration+SP_level_duration_adjust(caster, spell_ob);
        tmp->stats.dam = spell_ob->stats.dam+SP_level_dam_adjust(caster, spell_ob);
        tmp->range = 0;
    } else if (QUERY_FLAG(tmp, FLAG_ALIVE)) {
        tmp->stats.hp = spell_ob->duration+SP_level_duration_adjust(caster, spell_ob);
        tmp->stats.maxhp = tmp->stats.hp;
        object_set_owner(tmp, op);
        set_spell_skill(op, caster, spell_ob, tmp);
    }
    if (QUERY_FLAG(spell_ob, FLAG_IS_USED_UP) || QUERY_FLAG(tmp, FLAG_IS_USED_UP)) {
        tmp->stats.food = spell_ob->duration+SP_level_duration_adjust(caster, spell_ob);
        SET_FLAG(tmp, FLAG_IS_USED_UP);
    }
    if (QUERY_FLAG(spell_ob, FLAG_TEAR_DOWN)) {
        tmp->stats.hp = spell_ob->stats.dam+SP_level_dam_adjust(caster, spell_ob);
        tmp->stats.maxhp = tmp->stats.hp;
        SET_FLAG(tmp, FLAG_TEAR_DOWN);
        SET_FLAG(tmp, FLAG_ALIVE);
    }

    /* This can't really hurt - if the object doesn't kill anything,
     * these fields just won't be used.
     */
    object_set_owner(tmp, op);
    set_spell_skill(op, caster, spell_ob, tmp);
    tmp->level = caster_level(caster, spell_ob)/2;

    name = tmp->name;
    if ((tmp = object_insert_in_map_at(tmp, m, op, 0, x, y)) == NULL) {
        draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                             "Something destroys your %s",
                             "Something destroys your %s",
                             name);
        return 0;
    }
    /* If this is a spellcasting wall, need to insert the spell object */
    if (tmp->other_arch && tmp->other_arch->clone.type == SPELL)
        object_insert_in_ob(arch_to_object(tmp->other_arch), tmp);

    /*  This code causes the wall to extend some distance in
     * each direction, or until an obstruction is encountered.
     * posblocked and negblocked help determine how far the
     * created wall can extend, it won't go extend through
     * blocked spaces.
     */
    maxrange = spell_ob->range+SP_level_range_adjust(caster, spell_ob);
    posblocked = 0;
    negblocked = 0;

    for (i = 1; i <= maxrange; i++) {
        int dir2;

        dir2 = (dir < 4) ? (dir+2) : dir-2;

        x = tmp->x+i*freearr_x[dir2];
        y = tmp->y+i*freearr_y[dir2];
        m = tmp->map;

        if (!(get_map_flags(m, &m, x, y, &x, &y)&(P_OUT_OF_MAP|P_IS_ALIVE))
        && ((spell_ob->move_block&GET_MAP_MOVE_BLOCK(m, x, y)) != spell_ob->move_block)
        && !posblocked) {
            tmp2 = object_new();
            object_copy(tmp, tmp2);
            object_insert_in_map_at(tmp2, m, op, 0, x, y);
            /* If this is a spellcasting wall, need to insert the spell object */
            if (tmp2->other_arch && tmp2->other_arch->clone.type == SPELL)
                object_insert_in_ob(arch_to_object(tmp2->other_arch), tmp2);
        } else
            posblocked = 1;

        x = tmp->x-i*freearr_x[dir2];
        y = tmp->y-i*freearr_y[dir2];
        m = tmp->map;

        if (!(get_map_flags(m, &m, x, y, &x, &y)&(P_OUT_OF_MAP|P_IS_ALIVE))
        && ((spell_ob->move_block&GET_MAP_MOVE_BLOCK(m, x, y)) != spell_ob->move_block)
        && !negblocked) {
            tmp2 = object_new();
            object_copy(tmp, tmp2);
            object_insert_in_map_at(tmp2, m, op, 0, x, y);
            if (tmp2->other_arch && tmp2->other_arch->clone.type == SPELL)
                object_insert_in_ob(arch_to_object(tmp2->other_arch), tmp2);
        } else
            negblocked = 1;
    }

    if (QUERY_FLAG(tmp, FLAG_BLOCKSVIEW))
        update_all_los(op->map, op->x, op->y);

    return 1;
}

/**
 * Teleport through some doors and space.
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param spob
 * actual spell object.
 * @param dir
 * casting direction.
 * @retval 0
 * spell failure.
 * @retval 1
 * spell was successful.
 */
int dimension_door(object *op, object *caster, object *spob, int dir) {
    uint32 dist, maxdist;
    int  mflags;
    mapstruct *m;
    sint16  sx, sy;

    if (op->type != PLAYER)
        return 0;

    if (!dir) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                      "In what direction?", NULL);
        return 0;
    }

    /* Given the new outdoor maps, can't let players dimension door for
     * ever, so put limits in.
     */
    maxdist = spob->range+SP_level_range_adjust(caster, spob);

    if (op->contr->count) {
        if (op->contr->count > maxdist) {
            draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                          "You can't dimension door that far!", NULL);
            return 0;
        }

        for (dist = 0; dist < op->contr->count; dist++) {
            mflags = get_map_flags(op->map, &m,
                                   op->x+freearr_x[dir]*(dist+1),
                                   op->y+freearr_y[dir]*(dist+1),
                                   &sx, &sy);

            if (mflags&(P_NO_MAGIC|P_OUT_OF_MAP))
                break;

            if ((mflags&P_BLOCKSVIEW)
            && OB_TYPE_MOVE_BLOCK(op, GET_MAP_MOVE_BLOCK(m, sx, sy)))
                break;
        }

        if (dist < op->contr->count) {
            draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                          "Something blocks the magic of the spell.", NULL);
            op->contr->count = 0;
            return 0;
        }
        op->contr->count = 0;

        /* Remove code that puts player on random space on maps.  IMO,
         * a lot of maps probably have areas the player should not get to,
         * but may not be marked as NO_MAGIC (as they may be bounded
         * by such squares).  Also, there are probably treasure rooms and
         * lots of other maps that protect areas with no magic, but the
         * areas themselves don't contain no magic spaces.
         */
        /* This call here is really just to normalize the coordinates */
        mflags = get_map_flags(op->map, &m, op->x+freearr_x[dir]*dist, op->y+freearr_y[dir]*dist,
                               &sx, &sy);
        if (mflags&P_IS_ALIVE || OB_TYPE_MOVE_BLOCK(op, GET_MAP_MOVE_BLOCK(m, sx, sy))) {
            draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                          "You cast your spell, but nothing happens.", NULL);
            return 1; /* Maybe the penalty should be more severe... */
        }
    } else {
        /* Player didn't specify a distance, so lets see how far
         * we can move the player.  Don't know why this stopped on
         * spaces that blocked the players view.
         */

        for (dist = 0; dist < maxdist; dist++) {
            mflags = get_map_flags(op->map, &m,
                                   op->x+freearr_x[dir]*(dist+1),
                                   op->y+freearr_y[dir]*(dist+1),
                                   &sx, &sy);

            if (mflags&(P_NO_MAGIC|P_OUT_OF_MAP))
                break;

            if ((mflags&P_BLOCKSVIEW)
            && OB_TYPE_MOVE_BLOCK(op, GET_MAP_MOVE_BLOCK(m, sx, sy)))
                break;
        }

        /* If the destination is blocked, keep backing up until we
         * find a place for the player.
         */
        for (; dist > 0; dist--) {
            if (get_map_flags(op->map, &m, op->x+freearr_x[dir]*dist, op->y+freearr_y[dir]*dist, &sx, &sy)&(P_OUT_OF_MAP|P_IS_ALIVE))
                continue;

            if (!OB_TYPE_MOVE_BLOCK(op, GET_MAP_MOVE_BLOCK(m, sx, sy)))
                break;
        }
        if (!dist) {
            draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                          "Your spell failed!", NULL);
            return 0;
        }
    }

    if (op->contr->transport && op->contr->transport->type == TRANSPORT) {
        ob_apply(op->contr->transport, op, 0);
        if (op->contr->transport) {
            draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                          "Your spell failed!", NULL);
            return 0;
        }
    }

    /* Actually move the player now */
    object_remove(op);
    if ((op = object_insert_in_map_at(op, op->map, op, 0, op->x+freearr_x[dir]*dist, op->y+freearr_y[dir]*dist)) == NULL)
        return 1;

    if (op->type == PLAYER)
        map_newmap_cmd(&op->contr->socket);
    op->speed_left = -FABS(op->speed)*5; /* Freeze them for a short while */
    return 1;
}


/**
 * Heals something.
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param spell
 * actual spell object.
 * @param dir
 * casting direction.
 * @todo check spurious cure_disease call (shouldn't the spell's level be sent?) and return check value (always 1).
 */
int cast_heal(object *op, object *caster, object *spell, int dir) {
    object *tmp;
    archetype *at;
    object *poison;
    int heal = 0, success = 0;

    tmp = find_target_for_friendly_spell(op, dir);

    if (tmp == NULL)
        return 0;

    /* Figure out how many hp this spell might cure.
     * could be zero if this spell heals effects, not damage.
     */
    heal = spell->stats.dam;
    if (spell->stats.hp)
        heal += random_roll(spell->stats.hp, 6, op, PREFER_HIGH)+spell->stats.hp;

    if (heal) {
        if (tmp->stats.hp >= tmp->stats.maxhp) {
            draw_ext_info(NDI_UNIQUE, 0, tmp, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                          "You are already fully healed.", NULL);
        } else {
            /* See how many points we actually heal.  Instead of messages
             * based on type of spell, we instead do messages based
             * on amount of damage healed.
             */
            if (heal > (tmp->stats.maxhp-tmp->stats.hp))
                heal = tmp->stats.maxhp-tmp->stats.hp;
            tmp->stats.hp += heal;

            if (tmp->stats.hp >= tmp->stats.maxhp) {
                draw_ext_info(NDI_UNIQUE, 0, tmp, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
                              "You feel just fine!", NULL);
            } else if (heal > 50) {
                draw_ext_info(NDI_UNIQUE, 0, tmp, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
                              "Your wounds close!", NULL);
            } else if (heal > 25) {
                draw_ext_info(NDI_UNIQUE, 0, tmp, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
                              "Your wounds mostly close.", NULL);
            } else if (heal > 10) {
                draw_ext_info(NDI_UNIQUE, 0, tmp, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
                              "Your wounds start to fade.", NULL);
            } else {
                draw_ext_info(NDI_UNIQUE, 0, tmp, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
                              "Your wounds start to close.", NULL);
            }
            success = 1;
        }
    }
    if (spell->attacktype&AT_DISEASE)
        if (cure_disease(tmp, op))
            success = 1;

    if (spell->attacktype&AT_POISON) {
        at = find_archetype("poisoning");
        poison = arch_present_in_ob(at, tmp);
        if (poison) {
            success = 1;
            draw_ext_info(NDI_UNIQUE, 0, tmp, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
                          "Your body feels cleansed", NULL);
            poison->stats.food = 1;
        }
    }
    if (spell->attacktype&AT_CONFUSION) {
        poison = object_present_in_ob_by_name(FORCE, "confusion", tmp);
        if (poison) {
            success = 1;
            draw_ext_info(NDI_UNIQUE, 0, tmp, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
                          "Your mind feels clearer", NULL);
            poison->duration = 1;
        }
    }
    if (spell->attacktype&AT_BLIND) {
        at = find_archetype("blindness");
        poison = arch_present_in_ob(at, tmp);
        if (poison) {
            success = 1;
            draw_ext_info(NDI_UNIQUE, 0, tmp, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
                          "Your vision begins to return.", NULL);
            poison->stats.food = 1;
        }
    }
    if (spell->last_sp && tmp->stats.sp < tmp->stats.maxsp) {
        tmp->stats.sp += spell->last_sp;
        if (tmp->stats.sp > tmp->stats.maxsp)
            tmp->stats.sp = tmp->stats.maxsp;
        success = 1;
        draw_ext_info(NDI_UNIQUE, 0, tmp, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
                      "Magical energies surge through your body!", NULL);
    }
    if (spell->last_grace && tmp->stats.grace < tmp->stats.maxgrace) {
        tmp->stats.grace += spell->last_grace;
        if (tmp->stats.grace > tmp->stats.maxgrace)
            tmp->stats.grace = tmp->stats.maxgrace;
        success = 1;
        draw_ext_info(NDI_UNIQUE, 0, tmp, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
                      "You feel redeemed with you god!", NULL);
    }
    if (spell->stats.food && tmp->stats.food < 999) {
        tmp->stats.food += spell->stats.food;
        if (tmp->stats.food > 999)
            tmp->stats.food = 999;
        success = 1;
        /* We could do something a bit better like the messages for healing above */
        draw_ext_info(NDI_UNIQUE, 0, tmp, MSG_TYPE_SPELL, MSG_TYPE_SPELL_HEAL,
                      "You feel your belly fill with food", NULL);
    }
    return success;
}


/**
 * This is used for the spells that gain stats.  There are no spells
 * right now that icnrease wis/int/pow on a temp basis, so no
 * good comments for those.
 */
static const char *const no_gain_msgs[NUM_STATS] = {
    "You grow no stronger.",
    "You grow no more agile.",
    "You don't feel any healthier.",
    "no wis",
    "You are no easier to look at.",
    "no int",
    "no pow"
};

/**
 * Cast some stat-improving spell.
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param spell_ob
 * actual spell object.
 * @param dir
 * casting direction.
 * @param silent
 * if non zero, don't say when the spell is already is effect.
 * @retval 0
 * spell failed.
 * @retval 1
 * spell was successful.
 * @todo weird check on duration? since you'll never get there since a force would have been found?
 */
int cast_change_ability(object *op, object *caster, object *spell_ob, int dir, int silent) {
    object *tmp;
    object *force = NULL;
    int i;

    /* if dir = 99 op defaults to tmp, eat_special_food() requires this. */
    if (dir != 0) {
        tmp = find_target_for_friendly_spell(op, dir);
    } else {
        tmp = op;
    }

    if (tmp == NULL)
        return 0;

    /* If we've already got a force of this type, don't add a new one. */
    FOR_INV_PREPARE(tmp, tmp2)
        if (tmp2->type == FORCE && tmp2->subtype == FORCE_CHANGE_ABILITY)  {
            if (tmp2->name == spell_ob->name) {
                force = tmp2;    /* the old effect will be "refreshed" */
                break;
            } else if (spell_ob->race && spell_ob->race == tmp2->name) {
                if (!silent)
                    draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                                         "You can not cast %s while %s is in effect",
                                         "You can not cast %s while %s is in effect",
                                         spell_ob->name, tmp2->name_pl);
                return 0;
            }
        }
    FOR_INV_FINISH();
    if (force == NULL) {
        force = create_archetype(FORCE_NAME);
        force->subtype = FORCE_CHANGE_ABILITY;
        free_string(force->name);
        if (spell_ob->race)
            force->name = add_refcount(spell_ob->race);
        else
            force->name = add_refcount(spell_ob->name);
        free_string(force->name_pl);
        force->name_pl = add_refcount(spell_ob->name);
    } else {
        int duration;

        duration = spell_ob->duration+SP_level_duration_adjust(caster, spell_ob)*50;
        if (duration > force->duration) {
            force->duration = duration;
            draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
                          "You recast the spell while in effect.", NULL);
        } else {
            draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                          "Recasting the spell had no effect.", NULL);
        }
        return 1;
    }
    force->duration = spell_ob->duration+SP_level_duration_adjust(caster, spell_ob)*50;
    if (op->type == PLAYER)
        store_spell_expiry(force);
    force->speed = 1.0;
    force->speed_left = -1.0;
    SET_FLAG(force, FLAG_APPLIED);

    /* Now start processing the effects.  First, protections */
    for (i = 0; i < NROFATTACKS; i++) {
        if (spell_ob->resist[i]) {
            force->resist[i] = spell_ob->resist[i]+SP_level_dam_adjust(caster, spell_ob);
            if (force->resist[i] > 100)
                force->resist[i] = 100;
        }
    }
    if (spell_ob->stats.hp)
        force->stats.hp = spell_ob->stats.hp+SP_level_dam_adjust(caster, spell_ob);

    if (tmp->type == PLAYER) {
        /* Stat adjustment spells */
        for (i = 0; i < NUM_STATS; i++) {
            sint8 stat = get_attr_value(&spell_ob->stats, i), k, sm;

            if (stat) {
                sm = 0;
                for (k = 0; k < stat; k++)
                    sm += rndm(1, 3);

                if ((get_attr_value(&tmp->stats, i)+sm) > (15+5*stat)) {
                    sm = (15+5*stat)-get_attr_value(&tmp->stats, i);
                    if (sm < 0)
                        sm = 0;
                }
                set_attr_value(&force->stats, i, sm);
                if (!sm)
                    draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                                  no_gain_msgs[i], no_gain_msgs[i]);
            }
        }
    }

    force->move_type = spell_ob->move_type;

    if (QUERY_FLAG(spell_ob, FLAG_SEE_IN_DARK))
        SET_FLAG(force, FLAG_SEE_IN_DARK);

    if (QUERY_FLAG(spell_ob, FLAG_XRAYS))
        SET_FLAG(force, FLAG_XRAYS);

    /* Haste/bonus speed */
    if (spell_ob->stats.exp) {
        if (op->speed > 0.5)
            force->stats.exp = (float)spell_ob->stats.exp/(op->speed+0.5);
        else
            force->stats.exp = spell_ob->stats.exp;
    }

    force->stats.wc = spell_ob->stats.wc;
    force->stats.ac = spell_ob->stats.ac;
    force->attacktype = spell_ob->attacktype;

    SET_FLAG(tmp, FLAG_NO_FIX_PLAYER); /* we don't want object_insert_in_ob() to call fix_object. */
    object_insert_in_ob(force, tmp);
    CLEAR_FLAG(tmp, FLAG_NO_FIX_PLAYER);
    change_abil(tmp, force); /* Display any relevant messages, and call fix_object to update the player */

    return 1;
}

/**
 * Improve statistics of some living object.
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param spell_ob
 * actual spell object.
 * @param dir
 * casting direction.
 * @retval 0
 * spell failed.
 * @retval 1
 * spell was successful.
 */
int cast_bless(object *op, object *caster, object *spell_ob, int dir) {
    int i;
    const object *god = find_god(determine_god(op));
    object *force = NULL, *tmp;

    /* if dir = 99 op defaults to tmp, eat_special_food() requires this. */
    if (dir != 0) {
        tmp = find_target_for_friendly_spell(op, dir);
    } else {
        tmp = op;
    }

    /* If we've already got a force of this type, don't add a new one. */
    FOR_INV_PREPARE(tmp, tmp2)
        if (tmp2->type == FORCE && tmp2->subtype == FORCE_CHANGE_ABILITY)  {
            if (tmp2->name == spell_ob->name) {
                force = tmp2;    /* the old effect will be "refreshed" */
                break;
            } else if (spell_ob->race && spell_ob->race == tmp2->name) {
                draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                                     "You can not cast %s while %s is in effect",
                                     "You can not cast %s while %s is in effect",
                                     spell_ob->name, tmp2->name_pl);
                return 0;
            }
        }
    FOR_INV_FINISH();
    if (force == NULL) {
        force = create_archetype(FORCE_NAME);
        force->subtype = FORCE_CHANGE_ABILITY;
        free_string(force->name);
        if (spell_ob->race)
            force->name = add_refcount(spell_ob->race);
        else
            force->name = add_refcount(spell_ob->name);
        free_string(force->name_pl);
        force->name_pl = add_refcount(spell_ob->name);
    } else {
        int duration;

        duration = spell_ob->duration+SP_level_duration_adjust(caster, spell_ob)*50;
        if (duration > force->duration) {
            force->duration = duration;
            draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
                          "You recast the spell while in effect.", NULL);
        } else {
            draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                          "Recasting the spell had no effect.", NULL);
        }
        return 0;
    }
    force->duration = spell_ob->duration+SP_level_duration_adjust(caster, spell_ob)*50;
    force->speed = 1.0;
    force->speed_left = -1.0;
    SET_FLAG(force, FLAG_APPLIED);

    if (!god) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                      "Your blessing seems empty.", NULL);
    } else {
        /* Only give out good benefits, and put a max on it */
        for (i = 0; i < NROFATTACKS; i++) {
            if (god->resist[i] > 0) {
                force->resist[i] = MIN(god->resist[i], spell_ob->resist[ATNR_GODPOWER]);
            }
        }
        force->path_attuned |= god->path_attuned;
        if (spell_ob->attacktype) {
            force->attacktype |= god->attacktype|AT_PHYSICAL;
            if (god->slaying)
                force->slaying = add_string(god->slaying);
        }
        if (tmp != op) {
            draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
                                 "You bless %s.",
                                 "You bless %s.",
                                 tmp->name);
            draw_ext_info_format(NDI_UNIQUE, 0, tmp, MSG_TYPE_SPELL, MSG_TYPE_SPELL_TARGET,
                                 "%s blessed you.",
                                 "%s blessed you.",
                                 op->name);
        } else {
            draw_ext_info_format(NDI_UNIQUE, 0, tmp, MSG_TYPE_SPELL, MSG_TYPE_SPELL_TARGET,
                                 "You are blessed by %s!",
                                 "You are blessed by %s!",
                                 god->name);
        }
    }
    force->stats.wc = spell_ob->stats.wc;
    force->stats.ac = spell_ob->stats.ac;

    change_abil(tmp, force); /* Mostly to display any messages */
    object_insert_in_ob(force, tmp);
    fix_object(tmp);
    return 1;
}



/*
 * Alchemy code by Mark Wedel
 *
 * This code adds a new spell, called alchemy.  Alchemy will turn
 * objects to gold nuggets, the value of the gold nuggets being
 * from 5% to 40% of that of the item itself depending on casting level.
 * It uses the value of the object before charisma adjustments, because
 * the nuggets themselves will be will be adjusted by charisma when sold.
 *
 * Large nuggets are worth 25 gp each (base).  You will always get
 * the maximum number of large nuggets you could get.
 * Small nuggets are worth 1 gp each (base).  You will get from 0
 * to the max amount of small nuggets as you could get.
 *
 * For example, if an item is worth 110 gold, you will get
 * 4 large nuggets, and from 0-10 small nuggets.
 *
 * There is also a chance (1:30) that you will get nothing at all
 * for the object.  There is also a maximum weight that will be
 * alchemied.
 */

/* I didn't feel like passing these as arguments to the
 * two functions that need them.  Real values are put in them
 * when the spell is cast, and these are freed when the spell
 * is finished.
 */
static object *small, *large; /**< Nugget items. */

static uint64 small_value, large_value; /**< Value of nuggets. */

/**
 * Compute how many nuggets an object is worth, and remove it.
 *
 * @param value_adj
 * how much to adjust the cost of obj.
 * @param obj
 * object to convert.
 * @param[out] small_nuggets
 * how many small nuggets obj gives.
 * @param[out] large_nuggets
 * how many large nuggets obj gives.
 * @param[out] weight
 * the weight of the object.
 */
static void alchemy_object(float value_adj, object *obj, int *small_nuggets, int *large_nuggets, int *weight) {
    uint64 value = query_cost(obj, NULL, F_TRUE);

    /* Multiply the value of the object by value_adj, which should range
     * from 0.05 to 0.40. Set value to 0 instead if unpaid.
     */
    if (QUERY_FLAG(obj, FLAG_UNPAID))
        value = 0;
    else
        value *= value_adj;


    /* Give half of what value_adj says when we alchemy money (This should
     * hopefully make it so that it isn't worth it to alchemy money, sell
     * the nuggets, alchemy the gold from that, etc.
     */
    if (value && (obj->type == MONEY || obj->type == GEM))
        value /= 2;

    if ((obj->value > 0) && rndm(0, 29)) {
        int count;

        count = value/large_value;
        *large_nuggets += count;
        value -= (uint64)count*large_value;
        count = value/small_value;
        *small_nuggets += count;
    }

    /* Turn 25 small nuggets into 1 large nugget.  If the value
     * of large nuggets is not evenly divisable by the small nugget
     * value, take off an extra small_nugget (Assuming small_nuggets!=0)
     */
    if (*small_nuggets*small_value >= large_value) {
        (*large_nuggets)++;
        *small_nuggets -= large_value/small_value;
        if (*small_nuggets && large_value%small_value)
            (*small_nuggets)--;
    }
    weight += obj->weight;
    object_remove(obj);
    object_free2(obj, FREE_OBJ_NO_DESTROY_CALLBACK);
}

/**
 * Place gold nuggets on the map.
 * @param op
 * player who is casting the spell. Just used so nuggets are inserted below her.
 * @param m
 * map to insert to.
 * @param small_nuggets
 * @param large_nuggets
 * how many nuggets to place.
 * @param x
 * @param y
 * where to place the nuggets.
 */
static void place_alchemy_objects(object *op, mapstruct *m, int small_nuggets, int large_nuggets, int x, int y) {
    object *tmp;
    int flag = 0;

    /* Put any nuggets below the player, but we can only pass this
     * flag if we are on the same space as the player
     */
    if (x == op->x && y == op->y && op->map == m)
        flag = INS_BELOW_ORIGINATOR;

    if (small_nuggets) {
        tmp = object_new();
        object_copy(small, tmp);
        tmp-> nrof = small_nuggets;
        object_insert_in_map_at(tmp, m, op, flag, x, y);
    }
    if (large_nuggets) {
        tmp = object_new();
        object_copy(large, tmp);
        tmp-> nrof = large_nuggets;
        object_insert_in_map_at(tmp, m, op, flag, x, y);
    }
}

/**
 * Change items to gold nuggets. Only works for players.
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param spell_ob
 * actual spell object.
 * @retval 0
 * op isn't a player.
 * @retval 1
 * op is a player.
 */
int alchemy(object *op, object *caster, object *spell_ob) {
    int x, y, weight = 0, weight_max, large_nuggets, small_nuggets, mflags;
    sint16 nx, ny;
    float value_adj;
    mapstruct *mp;

    if (op->type != PLAYER)
        return 0;

    /* Put a maximum weight of items that can be alchemied.  Limits the power
     * some, and also prevents people from alcheming every table/chair/clock
     * in sight
     */
    weight_max = spell_ob->duration+SP_level_duration_adjust(caster, spell_ob);
    weight_max *= 1000;
    small = create_archetype("smallnugget"),
    large = create_archetype("largenugget");
    small_value = query_cost(small, NULL, F_TRUE);
    large_value = query_cost(large, NULL, F_TRUE);

    /* Set value_adj to be a multiplier for how much of the original value
     * will be in the nuggets. Starts at 0.05, increasing by 0.01 per casting
     * level, maxing out at 0.40.
     */
    value_adj = (SP_level_dam_adjust(caster, spell_ob)/100.00)+0.05;

    if (value_adj > 0.40)
        value_adj = 0.40;

    for (y = op->y-1; y <= op->y+1; y++) {
        for (x = op->x-1; x <= op->x+1; x++) {
            nx = x;
            ny = y;

            mp = op->map;

            mflags = get_map_flags(mp, &mp, nx, ny, &nx, &ny);

            if (mflags&(P_OUT_OF_MAP|P_NO_MAGIC))
                continue;

            /* Treat alchemy a little differently - most spell effects
             * use fly as the movement type - for alchemy, consider it
             * ground level effect.
             */
            if (GET_MAP_MOVE_BLOCK(mp, nx, ny)&MOVE_WALK)
                continue;

            small_nuggets = 0;
            large_nuggets = 0;

            FOR_MAP_PREPARE(mp, nx, ny, tmp) {
                if (tmp->weight > 0 && !QUERY_FLAG(tmp, FLAG_NO_PICK)
                && !QUERY_FLAG(tmp, FLAG_ALIVE)
                && !QUERY_FLAG(tmp, FLAG_IS_CAULDRON)) {
                    if (tmp->inv) {
                        FOR_INV_PREPARE(tmp, tmp1)
                            if (tmp1->weight > 0 && !QUERY_FLAG(tmp1, FLAG_NO_PICK)
                            && !QUERY_FLAG(tmp1, FLAG_ALIVE)
                            && !QUERY_FLAG(tmp1, FLAG_IS_CAULDRON))
                                alchemy_object(value_adj, tmp1, &small_nuggets, &large_nuggets, &weight);
                        FOR_INV_FINISH();
                    }
                    alchemy_object(value_adj, tmp, &small_nuggets, &large_nuggets, &weight);

                    if (weight > weight_max) {
                        place_alchemy_objects(op, mp, small_nuggets, large_nuggets, nx, ny);
                        object_free2(large, FREE_OBJ_NO_DESTROY_CALLBACK);
                        object_free2(small, FREE_OBJ_NO_DESTROY_CALLBACK);
                        return 1;
                    }
                } /* is alchemable object */
            } FOR_MAP_FINISH(); /* process all objects on this space */

            /* Insert all the nuggets at one time.  This probably saves time, but
             * it also prevents us from alcheming nuggets that were just created
             * with this spell.
             */
            place_alchemy_objects(op, mp, small_nuggets, large_nuggets, nx, ny);
        }
    }
    /** @todo fix global variables */
    object_free2(large, FREE_OBJ_NO_DESTROY_CALLBACK);
    object_free2(small, FREE_OBJ_NO_DESTROY_CALLBACK);
    /* reset this so that if player standing on a big pile of stuff,
     * it is redrawn properly.
     */
    op->contr->socket.look_position = 0;
    return 1;
}


/**
 * This function removes the cursed/damned status on equipped
 * items.
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param spell
 * actual spell object.
 * @return
 * how many items were affected.
 * @todo why is the value set to 0?
 */
int remove_curse(object *op, object *caster, object *spell) {
    int success = 0, was_one = 0;

    FOR_INV_PREPARE(op, tmp)
        if (QUERY_FLAG(tmp, FLAG_APPLIED)
        && ((QUERY_FLAG(tmp, FLAG_CURSED) && QUERY_FLAG(spell, FLAG_CURSED))
            || (QUERY_FLAG(tmp, FLAG_DAMNED) && QUERY_FLAG(spell, FLAG_DAMNED)))) {
            was_one++;
            if (tmp->level <= caster_level(caster, spell)) {
                success++;
                if (QUERY_FLAG(spell, FLAG_DAMNED))
                    CLEAR_FLAG(tmp, FLAG_DAMNED);

                CLEAR_FLAG(tmp, FLAG_CURSED);
                CLEAR_FLAG(tmp, FLAG_KNOWN_CURSED);
                tmp->value = 0; /* Still can't sell it */
                if (op->type == PLAYER)
                    esrv_update_item(UPD_FLAGS, op, tmp);
            }
        }
    FOR_INV_FINISH();

    if (op->type == PLAYER) {
        if (success) {
            draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
                          "You feel like some of your items are looser now.", NULL);
        } else {
            if (was_one)
                draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                              "You failed to remove the curse.", NULL);
            else
                draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                              "You are not using any cursed items.", NULL);
        }
    }
    return success;
}

/**
 * This alters player's marked item's cursed or blessed status, based on the spell_ob's fields.
 *
 * @param op
 * player casting the spell.
 * @param caster
 * what object was used to cast the spell.
 * @param spell_ob
 * spell itself.
 * @return
 * 1 if item was changed, 0 else.
 */
int cast_item_curse_or_curse(object *op, object *caster, object *spell_ob) {
    object *marked = find_marked_object(op);
    char name[HUGE_BUF];

    if (!marked) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                      "You need to mark an item first!", NULL);
        return 0;
    }

    if ((QUERY_FLAG(marked, FLAG_CURSED) && QUERY_FLAG(spell_ob, FLAG_CURSED))
        || (QUERY_FLAG(marked, FLAG_BLESSED) && QUERY_FLAG(spell_ob, FLAG_BLESSED))) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                      "The spell has no effect", NULL);
        return 0;
    }

    query_short_name(marked, name, HUGE_BUF);
    if (QUERY_FLAG(spell_ob, FLAG_CURSED)) {
        draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
                             "Your %s emits a dark light for a few seconds.", "Your %s emits a dark light for a few seconds.", name);
        SET_FLAG(marked, FLAG_CURSED);
        CLEAR_FLAG(marked, FLAG_KNOWN_CURSED);
        CLEAR_FLAG(marked, FLAG_IDENTIFIED);
        esrv_update_item(UPD_FLAGS, op, marked);
        return 1;
    }

    draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
                         "Your %s glows blue for a few seconds.", "Your %s glows blue for a few seconds.", name);
    SET_FLAG(marked, FLAG_BLESSED);
    SET_FLAG(marked, FLAG_KNOWN_BLESSED);
    SET_FLAG(marked, FLAG_STARTEQUIP);
    esrv_update_item(UPD_FLAGS, op, marked);
    return 1;
}

/**
 * Identifies objects in the players inventory/on the ground.
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param spell
 * actual spell object.
 * @retval 0
 * nothing was identified.
 * @retval 1
 * at least one object was identified.
 */
int cast_identify(object *op, object *caster, object *spell) {
    int success = 0, num_ident;
    char desc[MAX_BUF];

    num_ident = spell->stats.dam+SP_level_dam_adjust(caster, spell);

    if (num_ident < 1)
        num_ident = 1;

    FOR_INV_PREPARE(op, tmp)
        if (!QUERY_FLAG(tmp, FLAG_IDENTIFIED) && !tmp->invisible &&  need_identify(tmp)) {
            identify(tmp);
            if (op->type == PLAYER) {
                draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_ITEM, MSG_TYPE_ITEM_INFO,
                                     "You have %s.",
                                     "You have %s.",
                                     ob_describe(tmp, op, desc, sizeof(desc)));
                if (tmp->msg) {
                    draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_ITEM, MSG_TYPE_ITEM_INFO,
                                         "The item has a story:\n%s",
                                         "The item has a story:\n%s",
                                         tmp->msg);
                }
            }
            num_ident--;
            success = 1;
            if (!num_ident)
                break;
        }
    FOR_INV_FINISH();
    /* If all the power of the spell has been used up, don't go and identify
     * stuff on the floor.  Only identify stuff on the floor if the spell
     * was not fully used.
     */
    if (num_ident) {
        FOR_MAP_PREPARE(op->map, op->x, op->y, tmp)
            if (!QUERY_FLAG(tmp, FLAG_IDENTIFIED)
            && !tmp->invisible
            && need_identify(tmp)) {
                identify(tmp);
                if (op->type == PLAYER) {
                    draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_ITEM, MSG_TYPE_ITEM_INFO,
                                         "On the ground is %s.",
                                         "On the ground is %s.",
                                         ob_describe(tmp, op, desc, sizeof(desc)));
                    if (tmp->msg) {
                        draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_ITEM, MSG_TYPE_ITEM_INFO,
                                             "The item has a story:\n%s",
                                             "The item has a story:\n%s",
                                             tmp->msg);
                    }
                    esrv_update_item(UPD_FLAGS|UPD_NAME, op, tmp);
                }
                num_ident--;
                success = 1;
                if (!num_ident)
                    break;
            }
        FOR_MAP_FINISH();
    }
    if (!success)
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                      "You can't reach anything unidentified.", NULL);
    else {
        spell_effect(spell, op->x, op->y, op->map, op);
    }
    return success;
}

/**
 * Detect magic or invisible items.
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param spell
 * actual spell object.
 * @return
 * 1.
 */
int cast_detection(object *op, object *caster, object *spell) {
    object *tmp, *last, *detect;
    const object *god;
    int done_one, range, mflags, floor, level;
    sint16 x, y, nx, ny;
    mapstruct *m;

    /* We precompute some values here so that we don't have to keep
     * doing it over and over again.
     */
    god = find_god(determine_god(op));
    level = caster_level(caster, spell);
    range = spell->range+SP_level_range_adjust(caster, spell);

    for (x = op->x-range; x <= op->x+range; x++)
        for (y = op->y-range; y <= op->y+range; y++) {
            m = op->map;
            mflags = get_map_flags(m, &m, x, y, &nx, &ny);
            if (mflags&P_OUT_OF_MAP)
                continue;

            /* For most of the detections, we only detect objects above the
             * floor.  But this is not true for show invisible.
             * Basically, we just go and find the top object and work
             * down - that is easier than working up.
             */

            last = NULL;
            FOR_MAP_PREPARE(m, nx, ny, tmp)
                last = tmp;
            FOR_MAP_FINISH();
            /* Shouldn't happen, but if there are no objects on a space, this
             * would happen.
             */
            if (!last)
                continue;

            done_one = 0;
            floor = 0;
            detect = NULL;
            tmp = last;
            FOR_OB_AND_BELOW_PREPARE(tmp) {
                /* show invisible */
                if (QUERY_FLAG(spell, FLAG_MAKE_INVIS)
                    /* Might there be other objects that we can make visibile? */
                && (tmp->invisible && (QUERY_FLAG(tmp, FLAG_MONSTER) ||
                                        (tmp->type == PLAYER && !QUERY_FLAG(tmp, FLAG_WIZ)) ||
                                        tmp->type == CF_HANDLE ||
                                        tmp->type == TRAPDOOR || tmp->type == EXIT || tmp->type == HOLE ||
                                        tmp->type == BUTTON || tmp->type == TELEPORTER ||
                                        tmp->type == GATE || tmp->type == LOCKED_DOOR ||
                                        tmp->type == WEAPON || tmp->type == ALTAR || tmp->type == SIGN ||
                                        tmp->type == TRIGGER_PEDESTAL || tmp->type == SPECIAL_KEY ||
                                        tmp->type == TREASURE || tmp->type == BOOK ||
                                        tmp->type == HOLY_ALTAR))) {
                    if (random_roll(0, level-1, op, PREFER_HIGH) > tmp->level) {
                        tmp->invisible = 0;
                        done_one = 1;
                    }
                }
                if (QUERY_FLAG(tmp, FLAG_IS_FLOOR))
                    floor = 1;

                /* All detections below this point don't descend beneath the floor,
                 * so just continue on.  We could be clever and look at the type of
                 * detection to completely break out if we don't care about objects beneath
                 * the floor, but once we get to the floor, not likely a very big issue anyways.
                 */
                if (floor)
                    continue;

                /* I had thought about making detect magic and detect curse
                 * show the flash the magic item like it does for detect monster.
                 * however, if the object is within sight, this would then make it
                 * difficult to see what object is magical/cursed, so the
                 * effect wouldn't be as apparent.
                 */

                /* detect magic */
                if (QUERY_FLAG(spell, FLAG_KNOWN_MAGICAL)
                && !QUERY_FLAG(tmp, FLAG_KNOWN_MAGICAL)
                && !QUERY_FLAG(tmp, FLAG_IDENTIFIED)
                && is_magical(tmp)) {
                    SET_FLAG(tmp, FLAG_KNOWN_MAGICAL);
                    /* make runes more visible */
                    if (tmp->type == RUNE && tmp->attacktype&AT_MAGIC)
                        tmp->stats.Cha /= 4;
                    done_one = 1;
                }
                /* detect monster */
                if (QUERY_FLAG(spell, FLAG_MONSTER)
                && (QUERY_FLAG(tmp, FLAG_MONSTER) || (tmp->type == PLAYER && !QUERY_FLAG(tmp, FLAG_WIZ)))) {
                    done_one = 2;
                    if (!detect)
                        detect = tmp;
                }
                /* Basically, if race is set in the spell, then the creatures race must
                 * match that.  if the spell race is set to GOD, then the gods opposing
                 * race must match.
                 */
                if (spell->race
                && QUERY_FLAG(tmp, FLAG_MONSTER)
                && tmp->race
                && ((!strcmp(spell->race, "GOD") && god && god->slaying && strstr(god->slaying, tmp->race)) || (strstr(spell->race, tmp->race)))) {
                    done_one = 2;
                    if (!detect)
                        detect = tmp;
                }
                if (QUERY_FLAG(spell, FLAG_KNOWN_CURSED)
                && !QUERY_FLAG(tmp, FLAG_KNOWN_CURSED)
                && (QUERY_FLAG(tmp, FLAG_CURSED) || QUERY_FLAG(tmp, FLAG_DAMNED))) {
                    SET_FLAG(tmp, FLAG_KNOWN_CURSED);
                    done_one = 1;
                }
            } FOR_OB_AND_BELOW_FINISH(); /* for stack of objects on this space */

            /* Code here puts an effect of the spell on the space, so you can see
             * where the magic is.
             */
            if (done_one) {
                object *detect_ob = arch_to_object(spell->other_arch);

                /* if this is set, we want to copy the face */
                if (done_one == 2 && detect) {
                    detect_ob->face = detect->face;
                    detect_ob->animation_id = detect->animation_id;
                    detect_ob->anim_speed = detect->anim_speed;
                    detect_ob->last_anim = 0;
                    /* by default, the detect_ob is already animated */
                    if (!QUERY_FLAG(detect, FLAG_ANIMATE))
                        CLEAR_FLAG(detect_ob, FLAG_ANIMATE);
                }
                object_insert_in_map_at(detect_ob, m, op, 0, nx, ny);
            }
        } /* for processing the surrounding spaces */


    /* Now process objects in the players inventory if detect curse or magic */
    if (QUERY_FLAG(spell, FLAG_KNOWN_CURSED) || QUERY_FLAG(spell, FLAG_KNOWN_MAGICAL)) {
        done_one = 0;
        FOR_INV_PREPARE(op, tmp) {
            if (!tmp->invisible && !QUERY_FLAG(tmp, FLAG_IDENTIFIED)) {
                if (QUERY_FLAG(spell, FLAG_KNOWN_MAGICAL)
                && is_magical(tmp)
                && !QUERY_FLAG(tmp, FLAG_KNOWN_MAGICAL)) {
                    SET_FLAG(tmp, FLAG_KNOWN_MAGICAL);
                    if (op->type == PLAYER)
                        esrv_send_item(op, tmp);
                }
                if (QUERY_FLAG(spell, FLAG_KNOWN_CURSED)
                && !QUERY_FLAG(tmp, FLAG_KNOWN_CURSED)
                && (QUERY_FLAG(tmp, FLAG_CURSED) || QUERY_FLAG(tmp, FLAG_DAMNED))) {
                    SET_FLAG(tmp, FLAG_KNOWN_CURSED);
                    if (op->type == PLAYER)
                        esrv_send_item(op, tmp);
                }
            } /* if item is not identified */
        } FOR_INV_FINISH(); /* for the players inventory */
    } /* if detect magic/curse and object is a player */
    return 1;
}

/**
 * Checks if victim has overcharged mana, and if so does some fireball.
 * @param victim
 * who may have overcharged.
 * @param caster_level
 * caster's (skill) level whose spell did cause the overcharge.
 */
static void charge_mana_effect(object *victim, int caster_level) {
    /* Prevent explosions for objects without mana. Without this check, doors
     * will explode, too.
     */
    if (victim->stats.maxsp <= 0)
        return;

    draw_ext_info(NDI_UNIQUE, 0, victim, MSG_TYPE_SPELL, MSG_TYPE_SPELL_TARGET,
                  "You feel energy course through you.", NULL);

    if (victim->stats.sp >= victim->stats.maxsp*2) {
        object *tmp;

        draw_ext_info(NDI_UNIQUE, 0, victim, MSG_TYPE_VICTIM, MSG_TYPE_VICTIM_SPELL,
                      "Your head explodes!", NULL);

        /* Explodes a fireball centered at player */
        tmp = create_archetype(EXPLODING_FIREBALL);
        tmp->dam_modifier = random_roll(1, caster_level, victim, PREFER_LOW)/5+1;
        tmp->stats.maxhp = random_roll(1, caster_level, victim, PREFER_LOW)/10+2;
        object_insert_in_map_at(tmp, victim->map, NULL, 0, victim->x, victim->y);
        victim->stats.sp = 2*victim->stats.maxsp;
    } else if (victim->stats.sp >= victim->stats.maxsp*1.88) {
        draw_ext_info(NDI_UNIQUE, NDI_ORANGE, victim, MSG_TYPE_SPELL, MSG_TYPE_SPELL_TARGET,
                      "You feel like your head is going to explode.", NULL);
    } else if (victim->stats.sp >= victim->stats.maxsp*1.66) {
        draw_ext_info(NDI_UNIQUE, 0, victim, MSG_TYPE_SPELL, MSG_TYPE_SPELL_TARGET,
                      "You get a splitting headache!", NULL);
    } else if (victim->stats.sp >= victim->stats.maxsp*1.5) {
        draw_ext_info(NDI_UNIQUE, 0, victim, MSG_TYPE_SPELL, MSG_TYPE_SPELL_TARGET,
                      "Chaos fills your world.", NULL);
        confuse_living(victim, victim, 99);
    } else if (victim->stats.sp >= victim->stats.maxsp*1.25) {
        draw_ext_info(NDI_UNIQUE, 0, victim, MSG_TYPE_SPELL, MSG_TYPE_SPELL_TARGET,
                      "You start hearing voices.", NULL);
    }
}

/**
 * This spell transfers sp from the player to another person.
 * We let the target go above their normal maximum SP.
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param spell
 * actual spell object.
 * @param dir
 * casting direction.
 * @retval 0
 * no transfer happened.
 * @retval 1
 * transfer happened.
 */
int cast_transfer(object *op, object *caster, object *spell, int dir) {
    object *plyr = NULL;
    sint16 x, y;
    mapstruct *m;
    int mflags;

    m = op->map;
    x =  op->x+freearr_x[dir];
    y = op->y+freearr_y[dir];

    mflags = get_map_flags(m, &m, x, y, &x, &y);
    if (!(mflags&P_OUT_OF_MAP) && mflags&P_IS_ALIVE) {
        FOR_MAP_PREPARE(m, x, y, tmp)
            plyr = tmp;
            if (plyr != op && QUERY_FLAG(plyr, FLAG_ALIVE))
                break;
        FOR_MAP_FINISH();
    }

    /* If we did not find a player in the specified direction, transfer
     * to anyone on top of us. This is used for the rune of transference mostly.
     */
    if (plyr == NULL)
        FOR_MAP_PREPARE(op->map, op->x, op->y, tmp)
            plyr = tmp;
            if (plyr != op && QUERY_FLAG(plyr, FLAG_ALIVE))
                break;
        FOR_MAP_FINISH();

    if (!plyr) {
        draw_ext_info(NDI_BLACK, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                      "There is no one there.", NULL);
        return 0;
    }

    /* give sp */
    if (spell->stats.dam > 0) {
        plyr->stats.sp += spell->stats.dam+SP_level_dam_adjust(caster, spell);
        charge_mana_effect(plyr, caster_level(caster, spell));
        return 1;
    /* suck sp away.  Can't suck sp from yourself */
    } else if (op != plyr) {
        /* old dragin magic used floats.  easier to just use ints and divide by 100 */

        int rate = -spell->stats.dam+SP_level_dam_adjust(caster, spell), sucked;

        if (rate > 95)
            rate = 95;

        sucked = (plyr->stats.sp*rate)/100;
        plyr->stats.sp -= sucked;
        if (QUERY_FLAG(op, FLAG_ALIVE)) {
            /* Player doesn't get full credit */
            sucked = (sucked*rate)/100;
            op->stats.sp += sucked;
            if (sucked > 0) {
                charge_mana_effect(op, caster_level(caster, spell));
            }
        }
        return 1;
    }
    return 0;
}

/**
 * Nullifies spell effects.
 * Basically, if the object has a magic attacktype, this may nullify it.
 *
 * @param op
 * counterspell object.
 * @param dir
 * direction it was cast in.
 */
void counterspell(object *op, int dir) {
    object *head;
    int mflags;
    mapstruct *m;
    sint16  sx, sy;

    sx = op->x+freearr_x[dir];
    sy = op->y+freearr_y[dir];
    m = op->map;
    mflags = get_map_flags(m, &m, sx, sy, &sx, &sy);
    if (mflags&P_OUT_OF_MAP)
        return;

    FOR_MAP_PREPARE(m, sx, sy, tmp) {
        object *owner;

        /* Need to look at the head object - otherwise, if tmp
         * points to a monster, we don't have all the necessary
         * info for it.
         */
        head = HEAD(tmp);

        /* don't attack our own spells */
        owner = object_get_owner(tmp);
        if (owner != NULL && owner == object_get_owner(op))
            continue;

        /* Basically, if the object is magical and not counterspell,
         * we will more or less remove the object.  Don't counterspell
         * monsters either.
         */

        if (head->attacktype&AT_MAGIC
        && !(head->attacktype&AT_COUNTERSPELL)
        && !QUERY_FLAG(head, FLAG_MONSTER)
        && (op->level > head->level)) {
            object_remove(head);
            object_free2(head, 0);
        } else switch (head->type) {
            case SPELL_EFFECT:
                if ((op->level > head->level) && !op->stats.food && !op->speed_left) {
                    object_remove(head);
                    object_free2(head, 0);
                }
                break;

                /* I really don't get this rune code that much - that
                 * random chance seems really low.
                 */
            case RUNE:
                if (rndm(0, 149) == 0) {
                    head->stats.hp--;  /* weaken the rune */
                    if (!head->stats.hp) {
                        object_remove(head);
                        object_free2(head, 0);
                    }
                }
                break;
            }
    } FOR_MAP_FINISH();
}

/**
 * A spell to make an altar your god's.
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param spell
 * actual spell object.
 * @retval 0
 * no consecration happened.
 * @retval 1
 * an altar waas consecrated.
 */
int cast_consecrate(object *op, object *caster, object *spell) {
    char buf[MAX_BUF];
    const object *god = find_god(determine_god(op));

    if (!god) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                      "You can't consecrate anything if you don't worship a god!", NULL);
        return 0;
    }

    FOR_BELOW_PREPARE(op, tmp) {
        if (QUERY_FLAG(tmp, FLAG_IS_FLOOR))
            break;
        if (tmp->type == HOLY_ALTAR) {
            if (tmp->level > caster_level(caster, spell)) {
                draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                                     "You are not powerful enough to reconsecrate the %s",
                                     "You are not powerful enough to reconsecrate the %s",
                                     tmp->name);
                return 0;
            } else {
                /* If we got here, we are consecrating an altar */
                object *new_altar;
                size_t letter;
                archetype *altar_arch;

                snprintf(buf, MAX_BUF, "altar_");
                letter = strlen(buf);
                strncpy(buf+letter, god->name, MAX_BUF-letter);
                for (; letter < strlen(buf); letter++)
                    buf[letter] = tolower(buf[letter]);
                altar_arch = find_archetype(buf);
                if (!altar_arch) {
                    draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                                         "You fail to consecrate the altar.", NULL);
                    LOG(llevError, "cast_consecrate: can't find altar %s for god %s\n", buf, god->name);
                    return 0;
                }
                new_altar = arch_to_object(altar_arch);
                new_altar->level = tmp->level;
                object_insert_in_map_at(new_altar, tmp->map, tmp, INS_BELOW_ORIGINATOR, tmp->x, tmp->y);
                object_remove(tmp);
                draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
                                     "You consecrated the altar to %s!",
                                     "You consecrated the altar to %s!",
                                     god->name);
                return 1;
            }
        }
    } FOR_BELOW_FINISH();
    draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                  "You are not standing over an altar!", NULL);
    return 0;
}

/**
 * Generalization of staff_to_snake().  Makes a golem out of the caster's weapon.
 * The golem is based on the archetype specified, modified by the caster's level
 * and the attributes of the weapon.  The weapon is inserted in the golem's
 * inventory so that it falls to the ground when the golem dies.
 * This code was very odd - code early on would only let players use the spell,
 * yet the code was full of player checks.  I've presumed that the code
 * that only let players use it was correct, and removed all the other
 * player checks. MSW 2003-01-06
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param spell
 * actual spell object.
 * @param dir
 * casting direction.
 * @retval 0
 * spell failure.
 * @retval 1
 * spell was successful.
 */
int animate_weapon(object *op, object *caster, object *spell, int dir) {
    object *weapon, *tmp;
    char buf[MAX_BUF];
    int a, i;
    sint16 x, y;
    mapstruct *m;
    materialtype_t *mt;

    if (!spell->other_arch) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                      "Oops, program error!", NULL);
        LOG(llevError, "animate_weapon failed: spell %s missing other_arch!\n", spell->name);
        return 0;
    }

    /* exit if it's not a player using this spell. */
    if (op->type != PLAYER)
        return 0;

    /* if player already has a golem, abort */
    if (op->contr->ranges[range_golem] != NULL && op->contr->golem_count == op->contr->ranges[range_golem]->count) {
        pets_control_golem(op->contr->ranges[range_golem], dir);
        return 0;
    }

    /* if no direction specified, pick one */
    if (!dir)
        dir = object_find_free_spot(NULL, op->map, op->x, op->y, 1, 9);

    m = op->map;
    x = op->x+freearr_x[dir];
    y = op->y+freearr_y[dir];

    /* if there's no place to put the golem, abort */
    if ((dir == -1)
    || (get_map_flags(m, &m, x, y, &x, &y)&P_OUT_OF_MAP)
    || ((spell->other_arch->clone.move_type&GET_MAP_MOVE_BLOCK(m, x, y)) == spell->other_arch->clone.move_type)) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                      "There is something in the way.", NULL);
        return 0;
    }

    /* Use the weapon marked by the player. */
    weapon = find_marked_object(op);

    if (!weapon) {
        draw_ext_info(NDI_BLACK, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                      "You must mark a weapon to use with this spell!", NULL);
        return 0;
    }
    if (spell->race && strcmp(weapon->arch->name, spell->race)) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                      "The spell fails to transform your weapon.", NULL);
        return 0;
    }
    if (weapon->type != WEAPON) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                      "You need to mark a weapon to animate it.", NULL);
        return 0;
    }
    if (QUERY_FLAG(weapon, FLAG_UNPAID)) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                      "You need to pay for the weapon to animate it.", NULL);
        return 0;
    }
    if (QUERY_FLAG(weapon, FLAG_APPLIED)) {
        char wn[MAX_BUF];

        query_name(weapon, wn, MAX_BUF);
        draw_ext_info_format(NDI_BLACK, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                             "You need to unequip %s before using it in this spell",
                             "You need to unequip %s before using it in this spell",
                             wn);
        return 0;
    }

    if (weapon->nrof > 1) {
        tmp = object_split(weapon, 1, NULL, 0);
        esrv_update_item(UPD_NROF, op, weapon);
        weapon = tmp;
    }

    /* create the golem object */
    tmp = arch_to_object(spell->other_arch);

    /* if animated by a player, give the player control of the golem */
    CLEAR_FLAG(tmp, FLAG_MONSTER);
    SET_FLAG(tmp, FLAG_FRIENDLY);
    tmp->stats.exp = 0;
    add_friendly_object(tmp);
    tmp->type = GOLEM;
    object_set_owner(tmp, op);
    set_spell_skill(op, caster, spell, tmp);
    op->contr->ranges[range_golem] = tmp;
    op->contr->shoottype = range_golem;
    op->contr->golem_count = tmp->count;

    /* Give the weapon to the golem now.  A bit of a hack to check the
     * removed flag - it should only be set if object_split() was
     * used above.
     */
    if (!QUERY_FLAG(weapon, FLAG_REMOVED))
        object_remove(weapon);
    object_insert_in_ob(weapon, tmp);

    /* To do everything necessary to let a golem use the weapon is a pain,
     * so instead, just set it as equipped (otherwise, we need to update
     * body_info, skills, etc)
     */
    SET_FLAG(tmp, FLAG_USE_WEAPON);
    SET_FLAG(weapon, FLAG_APPLIED);
    fix_object(tmp);

    /* There used to be 'odd' code that basically seemed to take the absolute
     * value of the weapon->magic an use that.  IMO, that doesn't make sense -
     * if you're using a crappy weapon, it shouldn't be as good.
     */

    /* modify weapon's animated wc */
    tmp->stats.wc = tmp->stats.wc
        -SP_level_range_adjust(caster, spell)
        -5*weapon->stats.Dex
        -2*weapon->stats.Str
        -weapon->magic;
    if (tmp->stats.wc < -127)
        tmp->stats.wc = -127;

    /* Modify hit points for weapon */
    tmp->stats.maxhp = tmp->stats.maxhp
        +spell->duration
        +SP_level_duration_adjust(caster, spell)
        +8*weapon->magic
        +12*weapon->stats.Con;
    if (tmp->stats.maxhp < 0)
        tmp->stats.maxhp = 10;
    tmp->stats.hp = tmp->stats.maxhp;

    /* Modify weapon's damage */
    tmp->stats.dam = spell->stats.dam
        +SP_level_dam_adjust(caster, spell)
        +weapon->stats.dam
        +weapon->magic
        +5*weapon->stats.Str;
    if (tmp->stats.dam < 0)
        tmp->stats.dam = 127;

    /* attacktype */
    if (!tmp->attacktype)
        tmp->attacktype = AT_PHYSICAL;

    mt = NULL;
    if (op->materialname != NULL)
        mt = name_to_material(op->materialname);
    if (mt != NULL) {
        for (i = 0; i < NROFATTACKS; i++)
            tmp->resist[i] = 50-(mt->save[i]*5);
        a = mt->save[0];
    } else {
        for (i = 0; i < NROFATTACKS; i++)
            tmp->resist[i] = 5;
        a = 10;
    }
    /* Set weapon's immunity */
    tmp->resist[ATNR_CONFUSION] = 100;
    tmp->resist[ATNR_POISON] = 100;
    tmp->resist[ATNR_SLOW] = 100;
    tmp->resist[ATNR_PARALYZE] = 100;
    tmp->resist[ATNR_TURN_UNDEAD] = 100;
    tmp->resist[ATNR_FEAR] = 100;
    tmp->resist[ATNR_DEPLETE] = 100;
    tmp->resist[ATNR_DEATH] = 100;
    tmp->resist[ATNR_BLIND] = 100;

    /* Improve weapon's armour value according to best save vs. physical of its material */

    if (a > 14)
        a = 14;
    tmp->resist[ATNR_PHYSICAL] = 100-(int)((100.0-(float)tmp->resist[ATNR_PHYSICAL])/(30.0-2.0*a));

    /* Determine golem's speed */
    tmp->speed = 0.4+0.1*SP_level_range_adjust(caster, spell);

    if (tmp->speed > 3.33)
        tmp->speed = 3.33;

    if (!spell->race) {
        snprintf(buf, sizeof(buf), "animated %s", weapon->name);
        if (tmp->name)
            free_string(tmp->name);
        tmp->name = add_string(buf);

        tmp->face = weapon->face;
        tmp->animation_id = weapon->animation_id;
        tmp->anim_speed = weapon->anim_speed;
        tmp->last_anim = weapon->last_anim;
        tmp->state = weapon->state;
        if (QUERY_FLAG(weapon, FLAG_ANIMATE)) {
            SET_FLAG(tmp, FLAG_ANIMATE);
        } else {
            CLEAR_FLAG(tmp, FLAG_ANIMATE);
        }
        object_update_speed(tmp);
    }

    /*  make experience increase in proportion to the strength of the summoned creature. */
    tmp->stats.exp *= 1+(MAX(spell->stats.maxgrace, spell->stats.sp)/caster_level(caster, spell));

    tmp->speed_left = -1;
    tmp->direction = dir;
    object_insert_in_map_at(tmp, m, op, 0, x, y);
    return 1;
}

/**
 * This changes the light level for the entire map.
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param spell
 * actual spell object.
 * @retval 0
 * light not affected.
 * @retval 1
 * light changed.
 */
int cast_change_map_lightlevel(object *op, object *caster, object *spell) {
    int success;

    if (!op->map)
        return 0;  /* shouldnt happen */

    success = change_map_light(op->map, spell->stats.dam);
    if (!success) {
        if (spell->stats.dam < 0)
            draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                          "It can be no brighter here.", NULL);
        else
            draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_FAILURE,
                          "It can be no darker here.", NULL);
    }
    return success;
}

/**
 * Create an aura spell object and put it in the player's inventory.
 * This is also used for elemental shields - the creation is the same
 * just the 'move' code is different.
 *
 * @param op
 * who is casting.
 * @param caster
 * what is casting.
 * @param spell
 * actual spell object.
 * @return
 * 1.
 */
int create_aura(object *op, object *caster, object *spell) {
    int refresh = 0, i;
    object *new_aura;

    new_aura = arch_present_in_ob(spell->other_arch, op);
    if (new_aura)
        refresh = 1;
    else
        new_aura = arch_to_object(spell->other_arch);

    new_aura->duration = spell->duration+10*SP_level_duration_adjust(caster, spell);
    if (op->type == PLAYER)
        store_spell_expiry(new_aura);

    new_aura->stats.dam = spell->stats.dam+SP_level_dam_adjust(caster, spell);

    object_set_owner(new_aura, op);
    set_spell_skill(op, caster, spell, new_aura);
    new_aura->attacktype = spell->attacktype;

    new_aura->level = caster_level(caster, spell);

    /* Code below is so that auras can also provide resistances.  For
     * example, fire shield both does damage to nearby enemies and also
     * provides some protection to fire.  We need to use a different
     * FORCE object for this, as putting it in with the aura object
     * just puts too many meanings into that one object.  Because
     * the duration of this force object is the same, we don't need
     * to set up spell expiry on it - this second object is really
     * an internal mechanic that should be invisible to the player.
     */
    for (i = 0; i < NROFATTACKS; i++) {
        if (spell->resist[i]) {
            int refresh1=1;
            object *force;

            force = object_present_in_ob_by_name(FORCE, spell->name, op);
            if (!force) {
                force=create_archetype(FORCE_NAME);
                force->subtype = FORCE_CHANGE_ABILITY;
                free_string(force->name);
                force->name = add_refcount(spell->name);
                free_string(force->name_pl);
                force->name_pl = add_refcount(spell->name);
                refresh1=0;
            }
            force->duration = new_aura->duration;
            force->speed = new_aura->speed;
            memcpy(&force->resist, spell->resist, sizeof(spell->resist));
            SET_FLAG(force, FLAG_APPLIED);

            if (!refresh1)
                object_insert_in_ob(force, op);
            change_abil(op, new_aura);
            fix_object(op);
            break;
        }
    }

    if (refresh)
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_SUCCESS,
                      "You recast the spell while in effect.", NULL);
    else
        object_insert_in_ob(new_aura, op);
    return 1;
}

/**
 * This writes a rune that contains the appropriate message.
 * There really aren't any adjustments we make.
 *
 * @param op
 * who is casting.
 * @param spell
 * actual spell cast.
 * @param msg
 * message to write.
 * @retval 0
 * failure.
 * @retval 1
 * success.
 */
int write_mark(object *op, object *spell, const char *msg) {
    char rune[HUGE_BUF];
    object *tmp;

    if (!msg || msg[0] == 0) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                      "Write what?", NULL);
        return 0;
    }

    if (strcasestr_local(msg, "endmsg")) {
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_SPELL, MSG_TYPE_SPELL_ERROR,
                      "Trying to cheat are we?", NULL);
        LOG(llevInfo, "write_rune: player %s tried to write bogus rune %s\n", op->name, msg);
        return 0;
    }

    if (!spell->other_arch)
        return 0;
    tmp = arch_to_object(spell->other_arch);
    strncpy(rune, msg, HUGE_BUF-2);
    rune[HUGE_BUF-2] = 0;
    strcat(rune, "\n");
    tmp->race = add_string(op->name);   /*Save the owner of the rune*/
    object_set_msg(tmp, rune);
    object_insert_in_map_at(tmp, op->map, op, INS_BELOW_ORIGINATOR, op->x, op->y);
    return 1;
}