File: attack.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 (2493 lines) | stat: -rw-r--r-- 91,557 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
/*
 * static char *rcsid_attack_c =
 *   "$Id: attack.c 12951 2010-04-26 04:17:15Z mwedel $";
 */
/*
    CrossFire, A Multiplayer game for X-windows

    Copyright (C) 2002-2010 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 to crossfire-devel@real-time.com
*/

/**
 * @file
 * This handles all attacks, magical or not.
 * @todo
 * clean functions. Are all parameters required? Seems quite a mess to send damage/wc etc in attack_ob_simple().
 */

#include <assert.h>
#include <global.h>
#include <living.h>
#include <material.h>
#include <skills.h>

#ifndef __CEXTRACT__
#include <sproto.h>
#endif

#include <sounds.h>

/*#define ATTACK_DEBUG*/

static void slow_living(object *op, object *hitter, int dam);
static void deathstrike_living(object *op, object *hitter, int *dam);
static int adj_attackroll(object *hitter, object *target);
static int is_aimed_missile(object *op);
static int did_make_save_item(object *op, int type, object *originator);
static void poison_living(object *op, object *hitter, int dam);

/**
 * Cancels object *op.  Cancellation basically means an object loses
 * its magical benefits.
 *
 * @param op
 * item to cancel. Its inventory will also be cancelled.
 */
static void cancellation(object *op) {
    if (op->invisible)
        return;

    if (QUERY_FLAG(op, FLAG_ALIVE) || op->type == CONTAINER  || op->type == THROWN_OBJ) {
        /* Recur through the inventory */
        FOR_INV_PREPARE(op, inv)
            if (!did_make_save_item(inv, AT_CANCELLATION, op))
                cancellation(inv);
        FOR_INV_FINISH();
    } else if (FABS(op->magic) <= rndm(0, 5)) {
        /* Nullify this object. This code could probably be more complete */
        /* in what abilities it should cancel */
        op->magic = 0;
        CLEAR_FLAG(op, FLAG_DAMNED);
        CLEAR_FLAG(op, FLAG_CURSED);
        CLEAR_FLAG(op, FLAG_KNOWN_MAGICAL);
        CLEAR_FLAG(op, FLAG_KNOWN_CURSED);
        if (op->env && op->env->type == PLAYER) {
            esrv_update_item(UPD_FLAGS, op->env, op);
        }
    }
}

/**
 * Checks to make sure the item actually
 * made its saving throw based on the tables.  It does not take
 * any further action (like destroying the item).
 * @param op
 * object to check.
 * @param type
 * attack type.
 * @param originator
 * what it attacking?
 * @return
 * TRUE if item saved, FALSE else.
 * @todo
 * check meaning of originator.
 */
static int did_make_save_item(object *op, int type, object *originator) {
    int i, roll, saves = 0, attacks = 0, number;
    materialtype_t *mt;

    if (op->materialname == NULL) {
        for (mt = materialt; mt != NULL && mt->next != NULL; mt = mt->next) {
            if (op->material&mt->material)
                break;
        }
    } else
        mt = name_to_material(op->materialname);
    if (mt == NULL)
        return TRUE;
    roll = rndm(1, 20);

    /* the attacktypes have no meaning for object saves
     * If the type is only magic, don't adjust type - basically, if
     * pure magic is hitting an object, it should save.  However, if it
     * is magic teamed with something else, then strip out the
     * magic type, and instead let the fire, cold, or whatever component
     * destroy the item.  Otherwise, you get the case of poisoncloud
     * destroying objects because it has magic attacktype.
     */
    if (type != AT_MAGIC)
        type &= ~(AT_CONFUSION|AT_DRAIN|AT_GHOSTHIT|AT_POISON|AT_SLOW|
                  AT_PARALYZE|AT_TURN_UNDEAD|AT_FEAR|AT_DEPLETE|AT_DEATH|
                  AT_COUNTERSPELL|AT_HOLYWORD|AT_BLIND|AT_LIFE_STEALING|
                  AT_MAGIC);

    if (type == 0)
        return TRUE;
    if (roll == 20)
        return TRUE;
    if (roll == 1)
        return FALSE;

    for (number = 0; number < NROFATTACKS; number++) {
        i = 1<<number;
        if (!(i&type))
            continue;
        attacks++;
        if (op->resist[number] == 100)
            saves++;
        else if (roll >= mt->save[number]-op->magic-op->resist[number]/100)
            saves++;
        else if ((20-mt->save[number])/3 > originator->stats.dam)
            saves++;
    }

    if (saves == attacks || attacks == 0)
        return TRUE;
    if (saves == 0 || rndm(1, attacks) > saves)
        return FALSE;
    return TRUE;
}

/**
 * Object is attacked with some attacktype (fire, ice, ...).
 * Calls did_make_save_item().  It then performs the
 * appropriate actions to the item (such as burning the item up,
 * calling cancellation(), etc.)
 * @param op
 * victim of the attack.
 * @param type
 * attacktype.
 * @param originator
 * what is attacking.
 */
void save_throw_object(object *op, uint32 type, object *originator) {
    if (!did_make_save_item(op, type, originator)) {
        object *env = op->env;
        int x = op->x, y = op->y;
        mapstruct *m = op->map;

        op = stop_item(op);
        if (op == NULL)
            return;

        /* Set off runes in the inventory of the object being destroyed. */
        FOR_INV_PREPARE(op, inv)
            if (inv->type == RUNE)
                spring_trap(inv, originator);
        FOR_INV_FINISH();

        /* Hacked the following so that type LIGHTER will work.
         * Also, objects which are potenital "lights" that are hit by
         * flame/elect attacks will be set to glow. "lights" are any
         * object with +/- glow_radius and an "other_arch" to change to.
         * (and please note that we cant fail our save and reach this
         * function if the object doesnt contain a material that can burn.
         * So forget lighting magical swords on fire with this!) -b.t.
         */
        if (type&(AT_FIRE|AT_ELECTRICITY)
        && op->other_arch
        && QUERY_FLAG(op, FLAG_IS_LIGHTABLE)) {
            const char *arch = op->other_arch->name;

            op = object_decrease_nrof(op, 1);
            if (op)
                fix_stopped_item(op, m, originator);
            op = create_archetype(arch);
            if (op != NULL) {
                if (env) {
                    op->x = env->x,
                    op->y = env->y;
                    object_insert_in_ob(op, env);
                } else
                    object_insert_in_map_at(op, m, originator, 0, x, y);
            }
            return;
        }
        if (type&AT_CANCELLATION) {         /* Cancellation. */
            cancellation(op);
            fix_stopped_item(op, m, originator);
            return;
        }
        if (op->nrof > 1) {
            op = object_decrease_nrof(op, rndm(0, op->nrof-1));
            if (op)
                fix_stopped_item(op, m, originator);
        } else {
            if (!QUERY_FLAG(op, FLAG_REMOVED))
                object_remove(op);
            object_free_drop_inventory(op);
        }
        if (type&(AT_FIRE|AT_ELECTRICITY)) {
            if (env) {
                op = create_archetype("burnout");
                op->x = env->x,
                op->y = env->y;
                object_insert_in_ob(op, env);
            } else {
                object_replace_insert_in_map("burnout", originator);
            }
        }
        return;
    }
    /* The value of 50 is arbitrary. */
    if (type&AT_COLD
    && (op->resist[ATNR_COLD] < 50)
    && !QUERY_FLAG(op, FLAG_NO_PICK)
    && (RANDOM()&2)) {
        object *tmp;
        archetype *at = find_archetype("icecube");

        if (at == NULL)
            return;
        op = stop_item(op);
        if (op == NULL)
            return;
        tmp = map_find_by_archetype(op->map, op->x, op->y, at);
        if (tmp == NULL) {
            tmp = arch_to_object(at);
            /* This was in the old (pre new movement code) -
             * icecubes have slow_move set to 1 - don't want
             * that for ones we create.
             */
            tmp->move_slow_penalty = 0;
            tmp->move_slow = 0;
            object_insert_in_map_at(tmp, op->map, originator, 0, op->x, op->y);
        }
        if (!QUERY_FLAG(op, FLAG_REMOVED))
            object_remove(op);
        (void)object_insert_in_ob(op, tmp);
        return;
    }
}

/**
 * Attack a spot on the map.
 *
 * @param op
 * object hitting the map.
 * @param dir
 * direction op is hitting/going.
 * @param type
 * attacktype.
 * @param full_hit
 * if set then monster area does not matter, it gets all damage. Else damage is proportional to
 * affected area vs full monster area.
 * @return
 * 1 if it hits something, 0 otherwise.
 */
int hit_map(object *op, int dir, uint32 type, int full_hit) {
    mapstruct *map;
    sint16 x, y;
    int retflag = 0;  /* added this flag..  will return 1 if it hits a monster */
    tag_t op_tag;

    if (QUERY_FLAG(op, FLAG_FREED)) {
        LOG(llevError, "BUG: hit_map(): free object\n");
        return 0;
    }

    if (QUERY_FLAG(op, FLAG_REMOVED) || op->env != NULL) {
        LOG(llevError, "BUG: hit_map(): hitter (arch %s, name %s) not on a map\n", op->arch->name, op->name);
        return 0;
    }

    if (!op->map) {
        LOG(llevError, "BUG: hit_map(): %s has no map\n", op->name);
        return 0;
    }

    op = HEAD(op);
    op_tag = op->count;

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

    /* peterm:  a few special cases for special attacktypes --counterspell
     * must be out here because it strikes things which are not alive
     */

    if (type&AT_COUNTERSPELL) {
        counterspell(op, dir);  /* see spell_effect.c */

        /* If the only attacktype is counterspell or magic, don't need
         * to do any further processing.
         */
        if (!(type&~(AT_COUNTERSPELL|AT_MAGIC))) {
            return 0;
        }
        type &= ~AT_COUNTERSPELL;
    }

    if (type&AT_CHAOS) {
        shuffle_attack(op, 1);  /*1 flag tells it to change the face */
        object_update(op, UP_OBJ_FACE);
        type &= ~AT_CHAOS;
    }

    FOR_MAP_PREPARE(map, x, y, tmp) {
        if (QUERY_FLAG(tmp, FLAG_FREED)) {
            LOG(llevError, "BUG: hit_map(): found freed object\n");
            break;
        }

        /* Something could have happened to 'tmp' while 'tmp->below' was processed.
         * For example, 'tmp' was put in an icecube.
         * This is one of the few cases where on_same_map should not be used.
         */
        if (tmp->map != map || tmp->x != x || tmp->y != y)
            continue;

        tmp = HEAD(tmp);

        /* Need to hit everyone in the transport with this spell */
        if (tmp->type == TRANSPORT) {
            FOR_INV_PREPARE(tmp, pl)
                if (pl->type == PLAYER)
                    hit_player(pl, op->stats.dam, op, type, full_hit);
            FOR_INV_FINISH();
        }

        if (QUERY_FLAG(tmp, FLAG_ALIVE)) {
            hit_player(tmp, op->stats.dam, op, type, full_hit);
            retflag |= 1;
            if (object_was_destroyed(op, op_tag))
                break;
        }
        /* Here we are potentially destroying an object.  If the object has
         * NO_PASS set, it is also immune - you can't destroy walls.  Note
         * that weak walls have is_alive set, which prevent objects from
         * passing over/through them.  We don't care what type of movement
         * the wall blocks - if it blocks any type of movement, can't be
         * destroyed right now.
         */
        else if ((tmp->material || tmp->materialname) && op->stats.dam > 0 && !tmp->move_block) {
            save_throw_object(tmp, type, op);
            if (object_was_destroyed(op, op_tag))
                break;
        }
    } FOR_MAP_FINISH();
    return 0;
}

/**
 * Send an attack message to someone.
 *
 * @param dam
 * amount of damage done.
 * @param type
 * attack type.
 * @param op
 * victim of the attack.
 * @param hitter
 * who is hitting.
 * @todo
 * move check for player at function start? this function seems called for everyone.
 * use string safe functions.
 */
static void attack_message(int dam, int type, object *op, object *hitter) {
    char buf[MAX_BUF], buf1[MAX_BUF], buf2[MAX_BUF];
    int i, found = 0;
    mapstruct *map;
    object *owner;

    /* put in a few special messages for some of the common attacktypes
     *  a player might have.  For example, fire, electric, cold, etc
     *  [garbled 20010919]
     */

    if (dam == 9998 && op->type == DOOR) {
        snprintf(buf1, sizeof(buf1), "unlock %s", op->name);
        snprintf(buf2, sizeof(buf2), " unlocks");
        found++;
    }
    if (dam < 0) {
        snprintf(buf1, sizeof(buf1), "hit %s", op->name);
        snprintf(buf2, sizeof(buf2), " hits");
        found++;
    } else if (dam == 0) {
        snprintf(buf1, sizeof(buf1), "missed %s", op->name);
        snprintf(buf2, sizeof(buf2), " misses");
        found++;
    } else if ((hitter->type == DISEASE
        || hitter->type == SYMPTOM
        || hitter->type == POISONING
        || (type&AT_POISON && IS_LIVE(op))) && !found) {
        for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_SUFFER][i].level != -1; i++)
            if (dam < attack_mess[ATM_SUFFER][i].level
            || attack_mess[ATM_SUFFER][i+1].level == -1) {
                snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_SUFFER][i].buf1, op->name, attack_mess[ATM_SUFFER][i].buf2);
                snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_SUFFER][i].buf3);
                found++;
                break;
            }
    } else if (op->type == DOOR && !found) {
        for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_DOOR][i].level != -1; i++)
            if (dam < attack_mess[ATM_DOOR][i].level
            || attack_mess[ATM_DOOR][i+1].level == -1) {
                snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_DOOR][i].buf1, op->name, attack_mess[ATM_DOOR][i].buf2);
                snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_DOOR][i].buf3);
                found++;
                break;
            }
    } else if (hitter->type == PLAYER && IS_LIVE(op)) {
        if (USING_SKILL(hitter, SK_KARATE)) {
            for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_KARATE][i].level != -1; i++)
                if (dam < attack_mess[ATM_KARATE][i].level
                || attack_mess[ATM_KARATE][i+1].level == -1) {
                    snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_KARATE][i].buf1, op->name, attack_mess[ATM_KARATE][i].buf2);
                    snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_KARATE][i].buf3);
                    found++;
                    break;
                }
        } else if (USING_SKILL(hitter, SK_CLAWING)) {
            for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_CLAW][i].level != -1; i++)
                if (dam < attack_mess[ATM_CLAW][i].level
                || attack_mess[ATM_CLAW][i+1].level == -1) {
                    snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_CLAW][i].buf1, op->name, attack_mess[ATM_CLAW][i].buf2);
                    snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_CLAW][i].buf3);
                    found++;
                    break;
                }
        } else if (USING_SKILL(hitter, SK_PUNCHING)) {
            for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_PUNCH][i].level != -1; i++)
                if (dam < attack_mess[ATM_PUNCH][i].level
                || attack_mess[ATM_PUNCH][i+1].level == -1) {
                    snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_PUNCH][i].buf1, op->name, attack_mess[ATM_PUNCH][i].buf2);
                    snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_PUNCH][i].buf3);
                    found++;
                    break;
                }
        } else if (USING_SKILL(hitter, SK_WRAITH_FEED)) {
            for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_WRAITH_FEED][i].level != -1; i++)
                if (dam < attack_mess[ATM_WRAITH_FEED][i].level) {
                    snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_WRAITH_FEED][i].buf1, op->name, attack_mess[ATM_WRAITH_FEED][i].buf2);
                    snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_WRAITH_FEED][i].buf3);
                    found++;
                    break;
                }
        }
    }
    if (found) {
        /* done */
    } else if (IS_ARROW(hitter) && (type == AT_PHYSICAL || type == AT_MAGIC)) {
        snprintf(buf1, sizeof(buf1), "hit"); /* just in case */
        for (i = 0; i < MAXATTACKMESS; i++)
            if (dam < attack_mess[ATM_ARROW][i].level
            || attack_mess[ATM_ARROW][i+1].level == -1) {
                snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_ARROW][i].buf3);
                found++;
                break;
            }
    } else if (type&AT_DRAIN && IS_LIVE(op)) {
        /* drain is first, because some items have multiple attypes */
        for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_DRAIN][i].level != -1; i++)
            if (dam < attack_mess[ATM_DRAIN][i].level
            || attack_mess[ATM_DRAIN][i+1].level == -1) {
                snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_DRAIN][i].buf1, op->name, attack_mess[ATM_DRAIN][i].buf2);
                snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_DRAIN][i].buf3);
                found++;
                break;
            }
    } else if (type&AT_ELECTRICITY && IS_LIVE(op)) {
        for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_ELEC][i].level != -1; i++)
            if (dam < attack_mess[ATM_ELEC][i].level
            || attack_mess[ATM_ELEC][i+1].level == -1) {
                snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_ELEC][i].buf1, op->name, attack_mess[ATM_ELEC][i].buf2);
                snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_ELEC][i].buf3);
                found++;
                break;
            }
    } else if (type&AT_COLD && IS_LIVE(op)) {
        for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_COLD][i].level != -1; i++)
            if (dam < attack_mess[ATM_COLD][i].level
            || attack_mess[ATM_COLD][i+1].level == -1) {
                snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_COLD][i].buf1, op->name, attack_mess[ATM_COLD][i].buf2);
                snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_COLD][i].buf3);
                found++;
                break;
            }
    } else if (type&AT_FIRE) {
        for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_FIRE][i].level != -1; i++)
            if (dam < attack_mess[ATM_FIRE][i].level
            || attack_mess[ATM_FIRE][i+1].level == -1) {
                snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_FIRE][i].buf1, op->name, attack_mess[ATM_FIRE][i].buf2);
                snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_FIRE][i].buf3);
                found++;
                break;
            }
    } else if (hitter->current_weapon != NULL) {
        int mtype;

        switch (hitter->current_weapon->weapontype) {
        case WEAP_HIT: mtype = ATM_BASIC; break;
        case WEAP_SLASH: mtype = ATM_SLASH; break;
        case WEAP_PIERCE: mtype = ATM_PIERCE; break;
        case WEAP_CLEAVE: mtype = ATM_CLEAVE; break;
        case WEAP_SLICE: mtype = ATM_SLICE; break;
        case WEAP_STAB: mtype = ATM_STAB; break;
        case WEAP_WHIP: mtype = ATM_WHIP; break;
        case WEAP_CRUSH: mtype = ATM_CRUSH; break;
        case WEAP_BLUD: mtype = ATM_BLUD; break;
        default: mtype = ATM_BASIC; break;
        }
        for (i = 0; i < MAXATTACKMESS && attack_mess[mtype][i].level != -1; i++)
            if (dam < attack_mess[mtype][i].level
            || attack_mess[mtype][i+1].level == -1) {
                snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[mtype][i].buf1, op->name, attack_mess[mtype][i].buf2);
                snprintf(buf2, sizeof(buf2), "%s", attack_mess[mtype][i].buf3);
                found++;
                break;
            }
    } else {
        for (i = 0; i < MAXATTACKMESS && attack_mess[ATM_BASIC][i].level != -1; i++)
            if (dam < attack_mess[ATM_BASIC][i].level
            || attack_mess[ATM_BASIC][i+1].level == -1) {
                snprintf(buf1, sizeof(buf1), "%s %s%s", attack_mess[ATM_BASIC][i].buf1, op->name, attack_mess[ATM_BASIC][i].buf2);
                snprintf(buf2, sizeof(buf2), "%s", attack_mess[ATM_BASIC][i].buf3);
                found++;
                break;
            }
    }

    if (!found) {
        snprintf(buf1, sizeof(buf1), "hit");
        snprintf(buf2, sizeof(buf2), "hits");
    }

    /* bail out if a monster is casting spells */
    owner = object_get_owner(hitter);
    if (hitter->type != PLAYER && (owner == NULL || owner->type != PLAYER))
        return;

    /* scale down magic considerably. */
    if (type&AT_MAGIC && rndm(0, 5))
        return;

    /* Did a player hurt another player?  Inform both! */
    /* only show half the player->player combat messages */
    if (op->type == PLAYER
    && rndm(0, 1)
    && ((owner != NULL ? owner : hitter)->type) == PLAYER) {
        if (owner != NULL)
            snprintf(buf, sizeof(buf), "%s's %s %s you.", owner->name, hitter->name, buf2);
        else {
            snprintf(buf, sizeof(buf), "%s%s you.", hitter->name, buf2);
            if (dam != 0) {
                if (hitter->chosen_skill)
                    play_sound_player_only(op->contr, SOUND_TYPE_HIT_BY, op, 0, hitter->chosen_skill->name);
                else if (dam < 10)
                    play_sound_player_only(op->contr, SOUND_TYPE_HIT_BY, op, 0, "low");
                else if (dam < 20)
                    play_sound_player_only(op->contr, SOUND_TYPE_HIT_BY, op, 0, "medium");
                else
                    play_sound_player_only(op->contr, SOUND_TYPE_HIT_BY, op, 0, "high");
            }
        }
        draw_ext_info(NDI_BLACK, 0, op, MSG_TYPE_VICTIM, MSG_TYPE_VICTIM_WAS_HIT, buf, NULL);
    } /* end of player hitting player */

    /* scale down these messages too */
    /*if(hitter->type == PLAYER && rndm(0, 2) == 0) {*/
    if (hitter->type == PLAYER) {
        snprintf(buf, sizeof(buf), "You %s.", buf1);
        if (dam != 0) {
            if (hitter->chosen_skill)
                play_sound_player_only(hitter->contr, SOUND_TYPE_HIT, hitter, 0, hitter->chosen_skill->name);
            else if (dam < 10)
                play_sound_player_only(hitter->contr, SOUND_TYPE_HIT, hitter, 0, "low");
            else if (dam < 20)
                play_sound_player_only(hitter->contr, SOUND_TYPE_HIT, hitter, 0, "medium");
            else
                play_sound_player_only(hitter->contr, SOUND_TYPE_HIT, hitter, 0, "high");
        }
        draw_ext_info(NDI_BLACK, 0, hitter, MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_DID_HIT,
                      buf, NULL);
    } else if (owner != NULL && owner->type == PLAYER) {
        /* look for stacked spells and start reducing the message chances */
        if (hitter->type == SPELL_EFFECT
        && (hitter->subtype == SP_EXPLOSION || hitter->subtype == SP_BULLET || hitter->subtype == SP_CONE)) {
            i = 4;
            map = hitter->map;
            if (out_of_map(map, hitter->x, hitter->y))
                return;
            FOR_MAP_PREPARE(map, hitter->x, hitter->y, next)
                if (next->type == SPELL_EFFECT
                && (next->subtype == SP_EXPLOSION || next->subtype == SP_BULLET || next->subtype == SP_CONE))
                    i *= 3;
            FOR_MAP_FINISH();
            if (i < 0)
                return;
            if (rndm(0, i) != 0)
                return;
        } else if (rndm(0, 5) != 0)
            return;
        play_sound_map(SOUND_TYPE_HIT, owner, 0, "hit");
        draw_ext_info_format(NDI_BLACK, 0, owner, MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_PET_HIT,
                             "Your %s%s %s.",
                             "Your %s%s %s.",
                             hitter->name, buf2, op->name);
    }
}

/**
 * Find correct parameters for attack, do some sanity checks.
 * @param target
 * will point to victim's head.
 * @param hitter
 * will point to hitter's head.
 * @param simple_attack
 * will be 1 if one of victim or target isn't on a map, 0 else.
 * @return
 * 0 if hitter can attack target, 1 else.
 */
static int get_attack_mode(object **target, object **hitter,
                           int *simple_attack) {
    if (QUERY_FLAG(*target, FLAG_FREED) || QUERY_FLAG(*hitter, FLAG_FREED)) {
        LOG(llevError, "BUG: get_attack_mode(): freed object\n");
        return 1;
    }
    *target = HEAD(*target);
    *hitter = HEAD(*hitter);
    if ((*hitter)->env != NULL || (*target)->env != NULL) {
        *simple_attack = 1;
        return 0;
    }
    if (QUERY_FLAG(*target, FLAG_REMOVED)
    || QUERY_FLAG(*hitter, FLAG_REMOVED)
    || (*hitter)->map == NULL
    || !on_same_map((*hitter), (*target))) {
        LOG(llevError, "BUG: hitter (arch %s, name %s) with no relation to target\n", (*hitter)->arch->name, (*hitter)->name);
        return 1;
    }
    *simple_attack = 0;
    return 0;
}

/**
 * Check if target and hitter are still in a relation similar to the one
 * determined by get_attack_mode().
 *
 * @param target
 * who is attacked.
 * @param hitter
 * who is attacking.
 * @param simple_attack
 * previous mode as returned by get_attack_mode().
 * @return
 * 1 if the relation has changed, 0 else.
 */
static int abort_attack(object *target, object *hitter, int simple_attack) {
    int new_mode;

    if (hitter->env == target || target->env == hitter)
        new_mode = 1;
    else if (QUERY_FLAG(hitter, FLAG_REMOVED)
    || QUERY_FLAG(target, FLAG_REMOVED)
    || hitter->map == NULL || !on_same_map(hitter, target))
        return 1;
    else
        new_mode = 0;
    return new_mode != simple_attack;
}

static void thrown_item_effect(object *, object *);

/**
 * Handles simple attack cases.
 * @param op
 * victim. Should be the head part.
 * @param hitter
 * attacked. Should be the head part.
 * @param base_dam
 * damage to do.
 * @param base_wc
 * wc to hit with.
 * @return
 * dealt damage.
 * @todo
 * fix void return values. Try to remove gotos. Better document when it's called.
 */
static int attack_ob_simple(object *op, object *hitter, int base_dam, int base_wc) {
    int simple_attack, roll, dam;
    uint32 type;
    tag_t op_tag, hitter_tag;

    if (get_attack_mode(&op, &hitter, &simple_attack))
        return 1;

    /* Lauwenmark: Handle for plugin attack event */
    if (execute_event(op, EVENT_ATTACK, hitter, hitter->current_weapon ? hitter->current_weapon : hitter, NULL, SCRIPT_FIX_ALL) != 0)
        return 0;

    /* Lauwenmark: This is used to handle script_weapons with weapons.
     * Only used for players.
     */
    if (hitter->type == PLAYER) {
        if (hitter->current_weapon != NULL) {
            /* Lauwenmark: Handle for plugin attack event */
            if (execute_event(hitter->current_weapon, EVENT_ATTACK,
                              hitter, op, NULL, SCRIPT_FIX_ALL) != 0)
                return 0;
            if (hitter->current_weapon->anim_suffix)
                apply_anim_suffix(hitter, hitter->current_weapon->anim_suffix);
        }
    }
    op_tag = op->count;
    hitter_tag = hitter->count;
    /*
     * A little check to make it more difficult to dance forward and back
     * to avoid ever being hit by monsters.
     */
    if (!simple_attack && QUERY_FLAG(op, FLAG_MONSTER)
        && op->speed_left > -(FABS(op->speed))*0.3) {
        /* Decrease speed BEFORE calling process_object.  Otherwise, an
         * infinite loop occurs, with process_object calling monster_move(),
         * which then gets here again.  By decreasing the speed before
         * we call process_object, the 'if' statement above will fail.
         */
        op->speed_left--;
        process_object(op);
        if (object_was_destroyed(op, op_tag)
        || object_was_destroyed(hitter, hitter_tag)
        || abort_attack(op, hitter, simple_attack))
            return 1;
    }

    roll = random_roll(1, 20, hitter, PREFER_HIGH);

    /* Adjust roll for various situations. */
    if (!simple_attack)
        roll += adj_attackroll(hitter, op);

    /* See if we hit the creature */
    if (roll >= 20 || op->stats.ac >= base_wc-roll) {
        int hitdam = base_dam;
        if (settings.casting_time == TRUE) {
            if (hitter->type == PLAYER && hitter->casting_time > -1) {
                hitter->casting_time = -1;
                draw_ext_info(NDI_UNIQUE, 0, hitter, MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_FUMBLE,
                              "You attacked and lost your spell!", NULL);
            }
            if (op->casting_time > -1 && hitdam > 0) {
                op->casting_time = -1;
                if (op->type == PLAYER)  {
                    draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_FUMBLE,
                                  "You were hit and lost your spell!", NULL);
                    draw_ext_info_format(NDI_ALL|NDI_UNIQUE, 5, NULL,
                                         MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_FUMBLE,
                                         "%s was hit by %s and lost a spell.",
                                         "%s was hit by %s and lost a spell.",
                                         op->name, hitter->name);
                }
            }
        }
        if (!simple_attack) {
            /* If you hit something, the victim should *always *wake up.
             * Before, invisible hitters could avoid doing this.
             * -b.t. */
            if (QUERY_FLAG(op, FLAG_SLEEP))
                CLEAR_FLAG(op, FLAG_SLEEP);

            /* If the victim can't see the attacker, it may alert others
             * for help. */
            if (op->type != PLAYER && !monster_can_see_enemy(op, hitter)
                && !object_get_owner(op) && rndm(0, op->stats.Int))
                monster_npc_call_help(op);

            /* if you were hidden and hit by a creature, you are discovered*/
            if (op->hide && QUERY_FLAG(hitter, FLAG_ALIVE)) {
                make_visible(op);
                if (op->type == PLAYER)
                    draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_VICTIM, MSG_TYPE_VICTIM_WAS_HIT,
                                  "You were hit by a wild attack. You are no longer hidden!",
                                  NULL);
            }

            /* thrown items (hitter) will have various effects
             * when they hit the victim.  For things like thrown daggers,
             * this sets 'hitter' to the actual dagger, and not the
             * wrapper object.
             */
            thrown_item_effect(hitter, op);
            if (object_was_destroyed(hitter, hitter_tag)
                || object_was_destroyed(op, op_tag)
                || abort_attack(op, hitter, simple_attack)) {
                return 0;
            }
        }

        /* Need to do at least 1 damage, otherwise there is no point
         * to go further and it will cause FPE's below.
         */
        if (hitdam <= 0)
            hitdam = 1;

        type = hitter->attacktype;
        if (!type)
            type = AT_PHYSICAL;
        /* Handle monsters that hit back */
        if (!simple_attack && QUERY_FLAG(op, FLAG_HITBACK)
            && QUERY_FLAG(hitter, FLAG_ALIVE)) {
            if (op->attacktype&AT_ACID && hitter->type == PLAYER)
                draw_ext_info(NDI_UNIQUE, 0, hitter, MSG_TYPE_VICTIM, MSG_TYPE_VICTIM_WAS_HIT,
                              "You are splashed by acid!\n", NULL);
            hit_player(hitter, random_roll(0, (op->stats.dam), hitter, PREFER_LOW), op, op->attacktype, 1);
            if (object_was_destroyed(op, op_tag)
            || object_was_destroyed(hitter, hitter_tag)
            || abort_attack(op, hitter, simple_attack)) {
                return 0;
            }
        }

        /* In the new attack code, it should handle multiple attack
         * types in its area, so remove it from here.
         */
        dam = hit_player(op, random_roll(1, hitdam, hitter, PREFER_HIGH), hitter, type, 1);
        if (object_was_destroyed(op, op_tag)
        || object_was_destroyed(hitter, hitter_tag)
        || abort_attack(op, hitter, simple_attack)) {
            return 0;
        }
    } else {/* end of if hitter hit op */
        dam = 0; /* if we missed, dam=0 */
    }

    /*attack_message(dam, type, op, hitter);*/

    return dam;
}

/**
 * Simple wrapper for attack_ob_simple(), will use hitter's values.
 * @param op
 * victim.
 * @param hitter
 * attacker.
 * @return
 * dealt damage.
 */
int attack_ob(object *op, object *hitter) {
    hitter = HEAD(hitter);
    return attack_ob_simple(op, hitter, hitter->stats.dam, hitter->stats.wc);
}

/**
 * Try to put an arrow in inventory.
 *
 * @param op
 * arrow to try to insert.
 * @param tmp
 * what is stopping the arrow.
 * @return
 * 1 if op was inserted into tmp's inventory, 0 otherwise.
 */
static int stick_arrow(object *op, object *tmp) {
    /* If the missile hit a player, we insert it in their inventory.
     * However, if the missile is heavy, we don't do so (assume it falls
     * to the ground after a hit).  What a good value for this is up to
     * debate - 5000 is 5 kg, so arrows, knives, and other light weapons
     * stick around.
     */
    if (op->weight <= 5000 && tmp->stats.hp >= 0) {
        object_remove(op);
        op = object_insert_in_ob(op, HEAD(tmp));
        return 1;
    } else
        return 0;
}

/**
 * hit_with_arrow() disassembles the missile, attacks the victim and
 * reassembles the missile.
 *
 * @param op
 * missile hitting.
 * @param victim
 * who is hit by op.
 * @return
 * pointer to the reassembled missile, or NULL if the missile
 * isn't available anymore.
 */
object *hit_with_arrow(object *op, object *victim) {
    object *container, *hitter;
    int hit_something = 0;
    tag_t victim_tag, hitter_tag;
    sint16 victim_x, victim_y;
    mapstruct *victim_map;
    const char *old_skill = NULL;

    /* Disassemble missile */
    hitter = op->inv;
    FOR_OB_AND_BELOW_PREPARE(hitter)
        if (hitter->type != EVENT_CONNECTOR) {
            break;
        }
    FOR_OB_AND_BELOW_FINISH();
    if (!hitter) {
        container = NULL;
        hitter = op;
        if (free_no_drop(hitter))
            return NULL;
    } else {
        container = op;
        object_remove(hitter);
        if (free_no_drop(hitter))
            return NULL;

        object_insert_in_map_at(hitter, container->map, hitter, INS_NO_MERGE|INS_NO_WALK_ON, container->x, container->y);
        /* Note that we now have an empty THROWN_OBJ on the map.  Code that
         * might be called until this THROWN_OBJ is either reassembled or
         * removed at the end of this function must be able to deal with empty
         * THROWN_OBJs. */
    }

    /* Try to hit victim */
    victim_x = victim->x;
    victim_y = victim->y;
    victim_map = victim->map;
    victim_tag = victim->count;
    hitter_tag = hitter->count;
    /* Lauwenmark: Handling plugin attack event for thrown items */
    /* FIXME provide also to script the skill? hitter is the throwed
       items, but there is no information about the fact it was
       thrown
    */
    if (execute_event(op, EVENT_ATTACK, hitter, victim, NULL, SCRIPT_FIX_ALL) == 0) {
        /*
         * temporary set the hitter's skill to the one associated with the
         * throw wrapper. This is needed to that thrower gets it's xp at the
         * correct level. This might proves an awfull hack :/ We should really
         * provide attack_ob_simple with the skill to use...
         */
        if (container != NULL) {
            old_skill = hitter->skill;
            hitter->skill = add_refcount(container->skill);
        }
        hit_something = attack_ob_simple(victim, hitter, op->stats.dam, op->stats.wc);
    }
    /* Arrow attacks door, rune of summoning is triggered, demon is put on
     * arrow, move_apply() calls this function, arrow sticks in demon,
     * attack_ob_simple() returns, and we've got an arrow that still exists
     * but is no longer on the map. Ugh. (Beware: Such things can happen at
     * other places as well!)
     */
    if (object_was_destroyed(hitter, hitter_tag) || hitter->env != NULL) {
        if (container) {
            object_remove(container);
            object_free_drop_inventory(container);
        }
        return NULL;
    }
    if (container != NULL) {
        free_string(hitter->skill);
        hitter->skill = old_skill;
    }
    /* Missile hit victim */
    /* if the speed is > 10, then this is a fast moving arrow, we go straight
     * through the target
     */
    if (hit_something && op->speed <= 10.0) {
        /* Stop arrow */
        if (container == NULL) {
            hitter = fix_stopped_arrow(hitter);
            if (hitter == NULL)
                return NULL;
        } else {
            object_remove(container);
            object_free_drop_inventory(container);
        }

        /* Try to stick arrow into victim */
        if (!object_was_destroyed(victim, victim_tag)
        && stick_arrow(hitter, victim))
            return NULL;

        /* Else try to put arrow on victim's map square
        * remove check for P_WALL here.  If the arrow got to this
        * space, that is good enough - with the new movement code,
        * there is now the potential for lots of spaces where something
        * can fly over but not otherwise move over.  What is the correct
        * way to handle those otherwise?
        */
        if (victim_x != hitter->x || victim_y != hitter->y) {
            object_remove(hitter);
            object_insert_in_map_at(hitter, victim_map, hitter, 0, victim_x, victim_y);
        } else {
            /* Else leave arrow where it is */
            object_merge(hitter, NULL);
        }
        return NULL;
    }

    if (hit_something && op->speed >= 10.0)
        op->speed -= 1.0;

    /* Missile missed victim - reassemble missile */
    if (container) {
        object_remove(hitter);
        object_insert_in_ob(hitter, container);
    }
    return op;
}
/**
 * Handles wall tearing animation. Will change the face according to the hp/maxhp ration.
 *
 * If the wall reaches its last animation, either free it or set it non living so it doesn't block anymore.
 * @param op
 * wall to update.
 */
static void tear_down_wall(object *op) {
    int perc = 0;

    if (!op->stats.maxhp) {
        LOG(llevError, "TEAR_DOWN wall %s had no maxhp.\n", op->name);
        perc = 1;
    } else if (!GET_ANIM_ID(op)) {
        /* Object has been called - no animations, so remove it */
        if (op->stats.hp < 0) {
            object_remove(op); /* Should update LOS */
            object_free_drop_inventory(op);
            /* Don't know why this is here - object_remove() should do it for us */
            /*update_position(m, x, y);*/
        }
        return; /* no animations, so nothing more to do */
    }
    perc = NUM_ANIMATIONS(op)-((int)NUM_ANIMATIONS(op)*op->stats.hp)/op->stats.maxhp;
    if (perc >= (int)NUM_ANIMATIONS(op))
        perc = NUM_ANIMATIONS(op)-1;
    else if (perc < 1)
        perc = 1;
    SET_ANIMATION(op, perc);
    object_update(op, UP_OBJ_FACE);
    if (perc == NUM_ANIMATIONS(op)-1) { /* Reached the last animation */
        if (op->face == blank_face) {
            /* If the last face is blank, remove the ob */
            object_remove(op); /* Should update LOS */
            object_free_drop_inventory(op);

            /* object_remove() should call update_position for us */
            /*update_position(m, x, y);*/
        } else { /* The last face was not blank, leave an image */
            CLEAR_FLAG(op, FLAG_BLOCKSVIEW);
            update_all_los(op->map, op->x, op->y);
            op->move_block = 0;
            CLEAR_FLAG(op, FLAG_ALIVE);
        }
    }
}

/**
 * Creature is scared, update its values.
 * @param target
 * scared creature.
 * @param hitter
 * who scared target.
 */
static void scare_creature(object *target, object *hitter) {
    object *owner = object_get_owner(hitter);

    if (!owner)
        owner = hitter;

    SET_FLAG(target, FLAG_SCARED);
    if (!target->enemy)
        object_set_enemy(target, owner);
}

/**
 * Handles one attacktype's damage.
 * This doesn't damage the creature, but returns how much it should
 * take. However, it will do other effects (paralyzation, slow, etc.).
 * @param op
 * victim of the attack.
 * @param hitter
 * attacker.
 * @param dam
 * maximum dealt damage.
 * @param attacknum
 * number of the attacktype of the attack. See @ref Attacktypes "the ATNR_xxx"
 * values.
 * @return
 * damage to actually do.
 */
static int hit_with_one_attacktype(object *op, object *hitter, int dam, uint32 attacknum) {
    int doesnt_slay = 1;
    char name_hitter[MAX_BUF], name_op[MAX_BUF];

    /* Catch anyone that may be trying to send us a bitmask instead of the number */
    if (attacknum >= NROFATTACKS) {
        LOG(llevError, "hit_with_one_attacktype: Invalid attacknumber passed: %u\n", attacknum);
        return 0;
    }

    if (dam < 0) {
        LOG(llevError, "hit_with_one_attacktype called with negative damage: %d\n", dam);
        return 0;
    }

    if (hitter->current_weapon && hitter->current_weapon->discrete_damage != NULL)
        dam = hitter->current_weapon->discrete_damage[attacknum];
    else if (hitter->discrete_damage != NULL)
        dam = hitter->discrete_damage[attacknum];

    /* AT_INTERNAL is supposed to do exactly dam.  Put a case here so
     * people can't mess with that or it otherwise get confused.  */
    if (attacknum == ATNR_INTERNAL)
        return dam;

    if (hitter->slaying) {
        if ((op->race != NULL && strstr(hitter->slaying, op->race))
        || (op->arch && op->arch->name != NULL && strstr(op->arch->name, hitter->slaying))) {
            doesnt_slay = 0;
            dam *= 3;
        }
    }

    /* Adjust the damage for resistance. Note that neg. values increase damage. */
    if (op->resist[attacknum]) {
        /* basically:  dam = dam*(100-op->resist[attacknum])/100;
         * in case 0>dam>1, we try to "simulate" a float value-effect */
        dam *= (100-op->resist[attacknum]);
        if (dam >= 100)
            dam /= 100;
        else
            dam = (dam > (random_roll(0, 99, op, PREFER_LOW))) ? 1 : 0;
    }

    /* Special hack.  By default, if immune to something, you
     * shouldn't need to worry.  However, acid is an exception, since
     * it can still damage your items.  Only include attacktypes if
     * special processing is needed */

    if (op->resist[attacknum] >= 100
    && doesnt_slay
    && attacknum != ATNR_ACID)
        return 0;

    /* Keep this in order - makes things easier to find */

    switch (attacknum) {
    case ATNR_PHYSICAL:
        /*  here also check for diseases */
        check_physically_infect(op, hitter);
        break;

        /* Don't need to do anything for:
           magic,
           fire,
           electricity,
           cold */

    case ATNR_CONFUSION:
    case ATNR_POISON:
    case ATNR_SLOW:
    case ATNR_PARALYZE:
    case ATNR_FEAR:
    case ATNR_CANCELLATION:
    case ATNR_DEPLETE:
    case ATNR_BLIND: {
            /* chance for inflicting a special attack depends on the
             * difference between attacker's and defender's level
             */
            int level_diff = MIN(110, MAX(0, op->level-hitter->level));

            /* First, only creatures/players with speed can be affected.
             * Second, just getting hit doesn't mean it always affects
             * you.  Third, you still get a saving through against the
             * effect.
             */
            if (op->speed
            && (QUERY_FLAG(op, FLAG_MONSTER) || op->type == PLAYER)
            && !(rndm(0, (attacknum == ATNR_SLOW ? 6 : 3)-1))
            && !did_make_save(op, level_diff, op->resist[attacknum]/10)) {
                /* Player has been hit by something */
                if (attacknum == ATNR_CONFUSION)
                    confuse_living(op, hitter, dam);
                else if (attacknum == ATNR_POISON)
                    poison_living(op, hitter, dam);
                else if (attacknum == ATNR_SLOW)
                    slow_living(op, hitter, dam);
                else if (attacknum == ATNR_PARALYZE)
                    paralyze_living(op, hitter, dam);
                else if (attacknum == ATNR_FEAR)
                    scare_creature(op, hitter);
                else if (attacknum == ATNR_CANCELLATION)
                    cancellation(op);
                else if (attacknum == ATNR_DEPLETE)
                    drain_stat(op);
                else if (attacknum == ATNR_BLIND
                && !QUERY_FLAG(op, FLAG_UNDEAD)
                && !QUERY_FLAG(op, FLAG_GENERATOR))
                    blind_living(op, hitter, dam);
            }
            dam = 0; /* These are all effects and don't do real damage */
        }
        break;

    case ATNR_ACID: {
            int flag = 0;

            /* Items only get corroded if you're not on a battleground and
             * if your acid resistance is below 50%. */
            if (!op_on_battleground(op, NULL, NULL, NULL)
            && (op->resist[ATNR_ACID] < 50)) {
                FOR_INV_PREPARE(op, tmp) {
                    if (tmp->invisible)
                        continue;
                    if (!QUERY_FLAG(tmp, FLAG_APPLIED)
                    || (tmp->resist[ATNR_ACID] >= 10))
                        /* >= 10% acid res. on itmes will protect these */
                        continue;
                    if (!(tmp->material&M_IRON))
                        continue;
                    if (tmp->magic < -4) /* Let's stop at -5 */
                        continue;
                    if (tmp->type == RING
                    /* removed boots and gloves from exclusion list in PR */
                    || tmp->type == GIRDLE
                    || tmp->type == AMULET
                    || tmp->type == WAND
                    || tmp->type == ROD)
                        continue; /* To avoid some strange effects */

                    /* High damage acid has better chance of corroding objects */
                    if (rndm(0, dam+4) > random_roll(0, 39, op, PREFER_HIGH)+2*tmp->magic) {
                        if (op->type == PLAYER) {
                            /* Make this more visible */
                            query_name(hitter, name_hitter, MAX_BUF);
                            query_name(tmp, name_op, MAX_BUF);
                            draw_ext_info_format(NDI_UNIQUE|NDI_RED, 0, op,
                                                 MSG_TYPE_VICTIM, MSG_TYPE_VICTIM_WAS_HIT,
                                                 "The %s's acid corrodes your %s!",
                                                 "The %s's acid corrodes your %s!",
                                                 name_hitter, name_op);
                        }
                        flag = 1;
                        tmp->magic--;
                        if (op->type == PLAYER)
                            esrv_update_item(UPD_NAME, op, tmp);
                    }
                } FOR_INV_FINISH();
                if (flag)
                    fix_object(op); /* Something was corroded */
            }
        }
        break;

    case ATNR_DRAIN: {
            /* rate is the proportion of exp drained.  High rate means
             * not much is drained, low rate means a lot is drained.
             */
            int rate;

            if (op->resist[ATNR_DRAIN] >= 0)
                rate = 50+op->resist[ATNR_DRAIN]/2;
            else
                rate = 5000/(100-op->resist[ATNR_DRAIN]);

            /* full protection has no effect.  Nothing else in this
             * function needs to get done, so just return.  */
            if (!rate)
                return 0;

            if (op->stats.exp <= rate) {
                if (op->type == GOLEM)
                    dam = 999; /* Its force is "sucked" away. 8) */
                else
                    /* If we can't drain, lets try to do physical damage */
                    dam = hit_with_one_attacktype(op, hitter, dam, ATNR_PHYSICAL);
            } else {
                /* Randomly give the hitter some hp */
                if (hitter->stats.hp < hitter->stats.maxhp
                && (op->level > hitter->level)
                && random_roll(0, (op->level-hitter->level+2), hitter, PREFER_HIGH) > 3)
                    hitter->stats.hp++;

                /* Can't do drains on battleground spaces.
                 * Move the wiz check up here - before, the hitter wouldn't gain exp
                 * exp, but the wiz would still lose exp! If drainee is a wiz,
                 * nothing happens.
                 * Try to credit the owner.  We try to display player -> player drain
                 * attacks, hence all the != PLAYER checks.
                 */
                if (!op_on_battleground(hitter, NULL, NULL, NULL) && !QUERY_FLAG(op, FLAG_WAS_WIZ)) {
                    object *owner = object_get_owner(hitter);
                    sint64 orig_exp = op->stats.exp;

                    change_exp(op, -op->stats.exp/rate, NULL, 0);

                    if (owner && owner != hitter) {
                        if (op->type != PLAYER || owner->type != PLAYER)
                            change_exp(owner, MIN(op->stats.exp/(rate*2), orig_exp - op->stats.exp),
                                       hitter->chosen_skill ? hitter->chosen_skill->skill : NULL, SK_EXP_TOTAL);
                    } else if (op->type != PLAYER || hitter->type != PLAYER) {
                        change_exp(hitter, MIN(op->stats.exp/(rate*2), orig_exp - op->stats.exp),
                                   hitter->chosen_skill ? hitter->chosen_skill->skill : NULL, 0);
                    }
                }
                dam = 1; /* Drain is an effect.  Still return 1 - otherwise, if you have pure
                          * drain attack, you won't know that you are actually sucking out EXP,
                          * as the messages will say you missed
                          */
            }
        }
        break;

    case ATNR_TURN_UNDEAD: {
            if (QUERY_FLAG(op, FLAG_UNDEAD)) {
                object *owner = object_get_owner(hitter) == NULL ? hitter : object_get_owner(hitter);
                const object *god = find_god(determine_god(owner));
                int div = 1;

                /* if undead are not an enemy of your god, you turn them
                        * at half strength */
                if (!god
                || !god->slaying
                || strstr(god->slaying, undead_name) == NULL)
                    div = 2;
                /* Give a bonus if you resist turn undead */
                if (op->level*div < get_turn_bonus(owner->stats.Wis)+owner->level+(op->resist[ATNR_TURN_UNDEAD]/100))
                    scare_creature(op, owner);
            } else
                dam = 0; /* don't damage non undead - should we damage undead? */
        }
        break;

    case ATNR_DEATH:
        deathstrike_living(op, hitter, &dam);
        break;

    case ATNR_CHAOS:
        query_name(op, name_op, MAX_BUF);
        query_name(hitter, name_hitter, MAX_BUF);
        LOG(llevError, "%s was hit by %s with non-specific chaos.\n", name_op, name_hitter);
        dam = 0;
        break;

    case ATNR_COUNTERSPELL:
        query_name(op, name_op, MAX_BUF);
        query_name(hitter, name_hitter, MAX_BUF);
        LOG(llevError, "%s was hit by %s with counterspell attack.\n", name_op, name_hitter);
        dam = 0;
        /* This should never happen.  Counterspell is handled
         * seperately and filtered out.  If this does happen,
         * Counterspell has no effect on anything but spells, so it
         * does no damage. */
        break;

    case ATNR_HOLYWORD: {
            /* This has already been handled by hit_player,
             *  no need to check twice  -- DAMN */

            object *owner = object_get_owner(hitter) == NULL ? hitter : object_get_owner(hitter);

            /* As with turn undead above, give a bonus on the saving throw */
            if (op->level+(op->resist[ATNR_HOLYWORD]/100) < owner->level+get_turn_bonus(owner->stats.Wis))
                scare_creature(op, owner);
        }
        break;

    case ATNR_LIFE_STEALING: {
            int new_hp;
            /* this is replacement to drain for players, instead of taking
             * exp it takes hp. It is geared for players, probably not
             * much use giving it to monsters
             *
             * life stealing doesn't do a lot of damage, but it gives the
             * damage it does do to the player.  Given that,
             * it only does 1/30'th normal damage (hence the divide by
             * 3000). Wraith get 1/2 of the damage, and therefore divide
             * by 200. This number may need tweaking for game balance.
             */

            int dam_modifier = is_wraith_pl(hitter) ? 200 : 3000;

            /* You can't steal life from something undead or not alive. */
            if (op->type == GOLEM
            || (QUERY_FLAG(op, FLAG_UNDEAD))
            || !(QUERY_FLAG(op, FLAG_ALIVE))
            || (op->type == DOOR))
                return 0;
            /* If drain protection is higher than life stealing, use that */
            if (op->resist[ATNR_DRAIN] >= op->resist[ATNR_LIFE_STEALING])
                dam = (dam*(100-op->resist[ATNR_DRAIN]))/dam_modifier;
            else
                dam = (dam*(100-op->resist[ATNR_LIFE_STEALING]))/dam_modifier;
            /* You die at -1 hp, not zero. */
            if (dam > op->stats.hp+1)
                dam = op->stats.hp+1;
            new_hp = hitter->stats.hp+dam;
            if (new_hp > hitter->stats.maxhp)
                new_hp = hitter->stats.maxhp;
            if (new_hp > hitter->stats.hp)
                hitter->stats.hp = new_hp;

            /* Wraith also get food through life stealing */
            if (is_wraith_pl(hitter)) {
                if (hitter->stats.food+dam >= 999)
                    hitter->stats.food = 999;
                else
                    hitter->stats.food += dam;
                fix_object(hitter);
            }
        }
    }
    return dam;
}

/**
 * An object was killed, handle various things (logging, messages, ...).
 *
 * GROS: This code comes from hit_player. It has been made external to
 * allow script procedures to "kill" objects in a combat-like fashion.
 * It was initially used by (kill-object) developed for the Collector's
 * Sword. Note that nothing has been changed from the original version
 * of the following code.
 *
 * Will LOG pk, handles battleground, and so on.
 *
 * This function was a bit of a mess with hitter getting changed,
 * values being stored away but not used, etc.  I've cleaned it up
 * a bit - I think it should be functionally equivalant.
 * MSW 2002-07-17
 *
 * @param op
 * what is being killed.
 * @param dam
 * damage done to it.
 * @param hitter
 * what is hitting it.
 * @param type
 * the attacktype.
 * @return
 * dealt damage.
 * @todo
 * finish commenting what it does exactly.
 */
static int kill_object(object *op, int dam, object *hitter, int type) {
    char buf[MAX_BUF];
    const char *skill;
    int maxdam = 0;
    int battleg = 0;  /* true if op standing on battleground */
    int pk = 0;       /* true if op and what controls hitter are both players*/
    object *owner = NULL;
    object *skop = NULL;
    sstring death_animation;

    if (op->stats.hp >= 0)
        return -1;

    /* Lauwenmark: Handle for plugin death event */
    if (execute_event(op, EVENT_DEATH, hitter, NULL, NULL, SCRIPT_FIX_ALL) != 0)
        return 0;
    /* Lauwenmark: Handle for the global kill event */
    execute_global_event(EVENT_GKILL, op, hitter);

    if (op->map) {
        death_animation = object_get_value(op, "death_animation");
        if (death_animation != NULL) {
            object *death = create_archetype(death_animation);

            if (death)
                object_insert_in_map_at(death, op->map, op, 0, op->x, op->y);
        }
    }

    /* maxdam needs to be the amount of damage it took to kill
     * this creature.  The function(s) that call us have already
     * adjusted the creatures HP total, so that is negative.
     */
    maxdam = dam+op->stats.hp+1;

    if (QUERY_FLAG(op, FLAG_BLOCKSVIEW))
        update_all_los(op->map, op->x, op->y); /* makes sure los will be recalculated */

    if (op->type == DOOR) {
        op->speed = 0.1;
        object_update_speed(op);
        op->speed_left = -0.05;
        return maxdam;
    }
    if (QUERY_FLAG(op, FLAG_FRIENDLY) && op->type != PLAYER) {
        object *owner;

        remove_friendly_object(op);
        owner = object_get_owner(op);
        if (owner != NULL
        && owner->type == PLAYER
        && owner->contr->ranges[range_golem] == op) {
            owner->contr->ranges[range_golem] = NULL;
            owner->contr->golem_count = 0;
        } else
            LOG(llevError, "BUG: hit_player(): Encountered golem without owner.\n");

        object_remove(op);
        object_free_drop_inventory(op);
        return maxdam;
    }

    /* Now lets start dealing with experience we get for killing something */

    owner = object_get_owner(hitter);
    if (owner == NULL)
        owner = hitter;

    /* is the victim (op) standing on battleground? */
    if (op_on_battleground(op, NULL, NULL, NULL))
        battleg = 1;

    /* is this player killing?*/
    if (op->type == PLAYER && owner->type == PLAYER)
        pk = 1;

    /* Player killed something */
    if (owner->type == PLAYER) {
        char name_op[MAX_BUF], name_hitter[MAX_BUF];

        query_name(op, name_op, MAX_BUF);
        if (hitter)
            query_name(hitter, name_hitter, MAX_BUF);
        else
            name_hitter[0] = '\0';

        /* Log players killing other players - makes it easier to detect
         * and filter out malicious player killers - that is why the
         * ip address is included.
         */
        if (op->type == PLAYER && !battleg)  {
            time_t t = time(NULL);
            struct tm *tmv;
            char buf[256];
            char name[MAX_BUF];

            tmv = localtime(&t);
            strftime(buf, sizeof(buf), "%a %b %d %H:%M:%S %Y", tmv);
            query_name(op, name, MAX_BUF);

            LOG(llevInfo, "%s PLAYER_KILL_PLAYER: %s (%s) killed %s\n", buf, owner->name, owner->contr->socket.host, name);
        }

        /* try to filter some things out - basically, if you are
         * killing a level 1 creature and your level 20, you
         * probably don't want to see that.
         */
        if (owner->level < op->level*2 ||  op->stats.exp > 1000) {
            if (owner != hitter) {
                char killed[MAX_BUF], with[MAX_BUF];

                query_name(op, killed, MAX_BUF);
                query_name(hitter, with, MAX_BUF);
                draw_ext_info_format(NDI_BLACK, 0, owner, MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_DID_KILL,
                                     "You killed %s with %s.",
                                     "You killed %s with %s.",
                                     killed, with);
            } else {
                char killed[MAX_BUF];

                query_name(op, killed, MAX_BUF);
                draw_ext_info_format(NDI_BLACK, 0, owner, MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_DID_KILL,
                                     "You killed %s.",
                                     "You killed %s.",
                                     killed);
            }
            /* Only play sounds for melee kills */
            if (hitter->type == PLAYER)
                play_sound_map(SOUND_TYPE_HIT, owner, 0, "kill");
        }

        /* If a player kills another player, not on
         * battleground, the "killer" looses 1 luck. Since this is
         * not reversible, it's actually quite a pain IMHO. -AV
         * Fix bug in that we were changing the luck of the hitter, not
         * player that the object belonged to - so if you killed another player
         * with spells, pets, whatever, there was no penalty.
         * Changed to make luck penalty configurable in settings.
         */
        if (op->type == PLAYER && owner != op && !battleg)
            change_luck(owner, -settings.pk_luck_penalty);

        /* This code below deals with finding the appropriate skill
         * to credit exp to.  This is a bit problematic - we should
                * probably never really have to look at current_weapon->skill
         */
        skill = NULL;
        if (hitter->skill && hitter->type != PLAYER)
            skill = hitter->skill;
        else if (owner->chosen_skill) {
            skill = owner->chosen_skill->skill;
            skop = owner->chosen_skill;
        } else if (QUERY_FLAG(owner, FLAG_READY_WEAPON))
            skill = owner->current_weapon->skill;
        else
            LOG(llevError, "kill_object - unable to find skill that killed monster\n");

        /* We have the skill we want to credit to - now find the object this goes
         * to.  Make sure skop is an actual skill, and not a skill tool!
         */
        if ((!skop || skop->type != SKILL) && skill) {
            int i;

            for (i = 0; i < NUM_SKILLS; i++)
                if (owner->contr->last_skill_ob[i]
                && !strcmp(owner->contr->last_skill_ob[i]->skill, skill)) {
                    skop = owner->contr->last_skill_ob[i];
                    break;
                }
        }
    } /* Was it a player that hit somethign */
    else {
        skill = NULL;
    }

    /* Pet (or spell) killed something. */
    if (owner != hitter) {
        char name_op[MAX_BUF], name_hitter[MAX_BUF];
        const char *owner_prefix;
        const char *op_prefix;

        owner_prefix = !battleg && pk && owner->contr != NULL && !owner->contr->peaceful ? "hostile " : "";
        op_prefix = !battleg && pk && op->contr != NULL && !op->contr->peaceful ? "hostile " : "";

        query_name(op, name_op, MAX_BUF);
        query_name(hitter, name_hitter, MAX_BUF);
        snprintf(buf, sizeof(buf), "%s%s killed %s%s with %s%s.", owner_prefix, owner->name, op_prefix, name_op, name_hitter, battleg ? " (duel)" : (pk ? " (pk)" : ""));
    } else {
        const char *hitter_prefix;
        const char *op_prefix;

        hitter_prefix = !battleg && pk && hitter->contr != NULL && !hitter->contr->peaceful ? "hostile " : "";
        op_prefix = !battleg && pk && op->contr != NULL && !op->contr->peaceful ? "hostile " : "";

        snprintf(buf, sizeof(buf), "%s%s killed %s%s%s%s.", hitter_prefix, hitter->name, op_prefix, op->name,
                      (QUERY_FLAG(hitter, FLAG_MONSTER)) || hitter->type == PLAYER ?
                      " in hand to hand combat" : "", battleg ? " (duel)" : (pk ? " (pk)" : ""));
    }
    /* These may have been set in the player code section above */
    if (!skop)
        skop = hitter->chosen_skill;
    if (!skill && skop)
        skill = skop->skill;

    draw_ext_info(NDI_ALL, op->type == PLAYER ? 1 : 10, NULL, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_PLAYER,
                  buf, NULL);


    /* If you didn't kill yourself, and your not the wizard */
    if (owner != op && !QUERY_FLAG(op, FLAG_WAS_WIZ)) {
        sint64 exp;

        exp = calc_skill_exp(owner, op, skop);

        /* Really don't give much experience for killing other players */
        if (op->type == PLAYER) {
            if (battleg) {
                draw_ext_info(NDI_UNIQUE, 0, owner, MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_DID_KILL,
                              "Your foe has fallen!\nVICTORY!!!", NULL);
            } else {
                exp = settings.pk_max_experience_percent*exp/100;
                if (settings.pk_max_experience >= 0)
                    exp = MIN(settings.pk_max_experience, exp);
                /* Never exceed what victim can lose considering permanent exp. */
                exp = check_exp_loss(op, exp);
            }
        }

        /* Don't know why this is set this way - doesn't make
         * sense to just divide everything by two for no reason.
         */

        if (!settings.simple_exp)
            exp = exp/2;

        /* if op is standing on "battleground" (arena), no way to gain
         * exp by killing him
         */
        if (battleg)
            exp = 0;

#ifdef PARTY_KILL_LOG
        if (owner->type == PLAYER && owner->contr->party != NULL) {
            char name[MAX_BUF];
            char op_name[MAX_BUF];

            query_name(owner, name, MAX_BUF);
            query_name(op, op_name, sizeof(op_name));
            party_add_kill(owner->contr->party, name, op_name, exp);
        }
#endif
        share_exp(owner, exp, skill, SK_EXP_TOTAL);
    } /* end if person didn't kill himself */

    if (op->type != PLAYER) {
        if (QUERY_FLAG(op, FLAG_FRIENDLY)) {
            object *owner1 = object_get_owner(op);

            if (owner1 != NULL && owner1->type == PLAYER) {
                /*play_sound_player_only(owner1->contr, SOUND_PET_IS_KILLED, 0, 0);*/
                /* Maybe we should include the owner that killed this, maybe not */
                draw_ext_info_format(NDI_UNIQUE, 0, owner1, MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_PET_DIED,
                                     "Your pet, the %s, is killed by %s.",
                                     "Your pet, the %s, is killed by %s.",
                                     op->name, hitter->name);
            }
            remove_friendly_object(op);
        }
        object_remove(op);
        object_free_drop_inventory(op);
    /* Player has been killed! */
    } else {
        if (owner->type == PLAYER) {
            snprintf(op->contr->killer, BIG_NAME, "%s the %s", owner->name, owner->contr->title);
        } else {
            strncpy(op->contr->killer, hitter->name, BIG_NAME);
            op->contr->killer[BIG_NAME-1] = '\0';
        }
        /* Need to run kill_player (just in case, make sure is not wiz) */
        if (!QUERY_FLAG(op, FLAG_WIZ))
            kill_player(op);
    }
    /* This was return -1 - that doesn't seem correct - if we return -1, process
     * continues in the calling function.
     */
    return maxdam;
}

/**
 * Find out if this is friendly fire (PVP and attacker is peaceful) or not
 *
 * @param op
 * victim.
 * @param hitter
 * attacker.
 * @return
 * 0 this is not friendly fire, 1 if hitter is a peaceful player, 2 if hitter is a pet of a peaceful player.
 */
int friendly_fire(object *op, object *hitter) {
    object *owner;
    int friendlyfire;

    hitter = HEAD(hitter);
    friendlyfire = 0;

    if (op->type == PLAYER) {
        if (hitter->type == PLAYER && hitter->contr->peaceful == 1)
            return 1;

        owner = object_get_owner(hitter);
        if (owner != NULL) {
            if (owner->type == PLAYER && owner->contr->peaceful == 1)
                friendlyfire = 2;
        }

        if (hitter->type == SPELL
        || hitter->type == POISONING
        || hitter->type == DISEASE
        || hitter->type == RUNE)
            friendlyfire = 0;
    }
    return friendlyfire;
}

/**
 * Object is attacked by something.
 *
 * This isn't used just for players, but in fact most objects.
 *
 * Oct 95 - altered the following slightly for MULTIPLE_GODS hack
 * which needs new attacktype AT_HOLYWORD to work . b.t.
 *
 * @param op
 * object to be hit
 * @param dam
 * base damage - protections/vulnerabilities/slaying matches can modify it.
 * @param hitter
 * what is hitting the object
 * @param type
 * attacktype
 * @param full_hit
 * set if monster area does not matter.
 * @return
 * dealt damage.
 * @todo
 * rename to something more meaningful.
 */
int hit_player(object *op, int dam, object *hitter, uint32 type, int full_hit) {
    int maxdam = 0, ndam = 0, magic = (type&AT_MAGIC);
    int maxattacktype, attacknum;
    int body_attack = op->head != NULL;   /* Did we hit op's head? */
    int simple_attack;
    tag_t op_tag, hitter_tag;
    int rtn_kill = 0;
    int friendlyfire;
    object *owner;

    if (get_attack_mode(&op, &hitter, &simple_attack))
        return 0;

    /* very simple: if our target has no_damage 1 set or is wiz, we can't hurt him */
    if (QUERY_FLAG(op, FLAG_WIZ) || QUERY_FLAG(op, FLAG_NO_DAMAGE))
        return 0;

    op_tag = op->count;
    hitter_tag = hitter->count;

    if (body_attack) {
        /* slow and paralyze must hit the head.  But we don't want to just
         * return - we still need to process other attacks the spell still
         * might have.  So just remove the paralyze and slow attacktypes,
         * and keep on processing if we have other attacktypes.
         * return if only magic or nothing is left - under normal code
         * we don't attack with pure magic if there is another attacktype.
         * Only do processing if the initial attacktype includes one of those
         * attack so we don't cancel out things like magic bullet.
         */
        if (type&(AT_PARALYZE|AT_SLOW)) {
            type &= ~(AT_PARALYZE|AT_SLOW);
            if (!type || type == AT_MAGIC)
                return 0;
        }
    }

    if (!simple_attack && op->type == DOOR) {
        object *tmp;

        tmp = object_find_by_type2(op, RUNE, TRAP);
        if (tmp != NULL) {
            spring_trap(tmp, hitter);
            if (object_was_destroyed(hitter, hitter_tag)
            || object_was_destroyed(op, op_tag)
            || abort_attack(op, hitter, simple_attack))
                return 0;
        }
    }

    if (!QUERY_FLAG(op, FLAG_ALIVE) || op->stats.hp < 0) {
        /* FIXME: If a player is killed by a rune in a door, the
         * object_was_destroyed() check above doesn't return, and might get here.
         */
        LOG(llevDebug, "victim (arch %s, name %s) already dead in hit_player()\n", op->arch->name, op->name);
        return 0;
    }

#ifdef ATTACK_DEBUG
    LOG(llevDebug, "hit player: attacktype %d, dam %d\n", type, dam);
#endif

    if (magic) {
        /* basically:  dam = dam*(100-op->resist[attacknum])/100;
         * in case 0>dam>1, we try to "simulate" a float value-effect */
        dam = dam*(100-op->resist[ATNR_MAGIC]);
        if (dam >= 100)
            dam /= 100;
        else
            dam = (dam > (rndm(0, 99))) ? 1 : 0;
    }

    /* AT_CHAOS here is a weapon or monster.  Spells are handled by hit_map
     * We don't use shuffle_attack(), because that changes the it in the
     * creature structure, and thus is permanent until fix_object() is
     * called again.  Chaos should change on each attack.
     */
    if (type&AT_CHAOS) {
        type = ATTACKS[RANDOM()%(sizeof(ATTACKS)/sizeof(*ATTACKS))].attacktype|AT_MAGIC;
    }

    /* Holyword is really an attacktype modifier (like magic is).  If
     * holyword is part of an attacktype, then make sure the creature is
     * a proper match, otherwise no damage.
     */
    if (type&AT_HOLYWORD) {
        const object *god;

        if ((!hitter->slaying || (!(op->race && strstr(hitter->slaying, op->race))
                    && !(op->name && strstr(hitter->slaying, op->name))))
        && (!QUERY_FLAG(op, FLAG_UNDEAD) || (hitter->title != NULL
                    && (god = find_god(determine_god(hitter))) != NULL
                    && god->race != NULL
                    && strstr(god->race, undead_name) != NULL)))
            return 0;
    }

    maxattacktype = type; /* initialize this to something */
    for (attacknum = 0; attacknum < NROFATTACKS; attacknum++) {
        int attacktype;

        attacktype = 1<<attacknum;

        /* Magic isn't really a true attack type - it gets combined with other
         * attack types.  As such, skip it over.  However, if magic is
         * the only attacktype in the group, then still attack with it
         */
        if (attacktype == AT_MAGIC && (type&~AT_MAGIC))
            continue;

        /* Go through and hit the player with each attacktype, one by one.
         * hit_with_one_attacktype only figures out the damage, doesn't inflict
         * it.  It will do the appropriate action for attacktypes with
         * effects (slow, paralization, etc.
                */
        if (type&attacktype) {
            ndam = hit_with_one_attacktype(op, hitter, dam, attacknum);
            /* the >= causes us to prefer messages from special attacks, if
             * the damage is equal.
             */
            if (ndam >= maxdam) {
                maxdam = ndam;
                maxattacktype = 1<<attacknum;
            }
            /* Special case: death attack always deals all damage, as it should kill the monster
             * right away. */
            if (attacktype == AT_DEATH && ndam > 0)
                full_hit = 1;
        }
    }


    /* if this is friendly fire then do a set % of damage only
     * Note - put a check in to make sure this attack is actually
     * doing damage - otherwise, the +1 in the coe below will make
     * an attack do damage before when it otherwise didn't
     * Only reduce damage if not on battlground - if in arena, do
     * full damage.  Note that it is intentional that the check for
     * battleground is inside the friendlyfire if statement - op_on_battleground()
     * is a fairly costly function to call, and we don't want to call it for
     * every attack - by doing it only for friendlyfire, it shouldn't get called
     * that often
     */
    friendlyfire = friendly_fire(op, hitter);
    if (friendlyfire && maxdam) {
        if (!op_on_battleground(op, NULL, NULL, NULL)) {
            maxdam = ((dam*settings.set_friendly_fire)/100)+1;
#ifdef ATTACK_DEBUG
            LOG(llevDebug, "Friendly fire (type:%d setting: %d%) did %d damage dropped to %d\n", friendlyfire, settings.set_friendly_fire, dam, maxdam);
#endif
        }
    }

    if (!full_hit) {
        archetype *at;
        int area;
        int remainder;

        area = 0;
        for (at = op->arch; at != NULL; at = at->more)
            area++;
        assert(area > 0);

        /* basically: maxdam /= area; we try to "simulate" a float
           value-effect */
        remainder = 100*(maxdam%area)/area;
        maxdam /= area;
        if (RANDOM()%100 < remainder)
            maxdam++;
    }

#ifdef ATTACK_DEBUG
    LOG(llevDebug, "Attacktype %d did %d damage\n", type, maxdam);
#endif

    owner = object_get_owner(hitter);
    if (owner != NULL) {
        if (op->enemy != hitter)
            object_set_enemy(op, owner);
    } else if (QUERY_FLAG(hitter, FLAG_ALIVE))
        if (op->enemy == NULL || rndm(1, 20) == 0)
            object_set_enemy(op, hitter);

    if (QUERY_FLAG(op, FLAG_UNAGGRESSIVE) && op->type != PLAYER) {
        /* The unaggressives look after themselves 8) */
        CLEAR_FLAG(op, FLAG_UNAGGRESSIVE);
        monster_npc_call_help(op);
    }

    if (magic && did_make_save(op, op->level, 0))
        maxdam = maxdam/2;

    attack_message(maxdam, maxattacktype, op, hitter);

    op->stats.hp -= maxdam;

    /* Eneq(@csd.uu.se): Check to see if monster runs away. */
    if (op->stats.hp >= 0
    && (QUERY_FLAG(op, FLAG_MONSTER) || op->type == PLAYER)
    && op->stats.hp < (signed short)(((float)op->run_away/(float)100)*(float)op->stats.maxhp)) {
        if (QUERY_FLAG(op, FLAG_MONSTER))
            SET_FLAG(op, FLAG_RUN_AWAY);
        else
            scare_creature(op, hitter);
    }

    if (QUERY_FLAG(op, FLAG_TEAR_DOWN)) {
        if (maxdam)
            tear_down_wall(op);
        return maxdam; /* nothing more to do for wall */
    }

    /* See if the creature has been killed */
    rtn_kill = kill_object(op, maxdam, hitter, type);
    if (rtn_kill != -1)
        return rtn_kill;

    /* Used to be ghosthit removal - we now use the ONE_HIT flag.  Note
     * that before if the player was immune to ghosthit, the monster
     * remained - that is no longer the case.
     */
    if (QUERY_FLAG(hitter, FLAG_ONE_HIT)) {
        if (QUERY_FLAG(hitter, FLAG_FRIENDLY))
            remove_friendly_object(hitter);
        object_remove(hitter);
        object_free_drop_inventory(hitter);
    /* Lets handle creatures that are splitting now */
    } else if (type&AT_PHYSICAL && !QUERY_FLAG(op, FLAG_FREED) && QUERY_FLAG(op, FLAG_SPLITTING)) {
        change_object(op);
    } else if (type&AT_DRAIN && hitter->type == GRIMREAPER && hitter->value++ > 10) {
        object_remove(hitter);
        object_free_drop_inventory(hitter);
    }
    return maxdam;
}

/**
 * Poison a living thing.
 *
 * @param op
 * victim.
 * @param hitter
 * who is attacking.
 * @param dam
 * damage to deal.
 */
static void poison_living(object *op, object *hitter, int dam) {
    archetype *at = find_archetype("poisoning");
    object *tmp = arch_present_in_ob(at, op);
    const char *skill;

    if (tmp == NULL) {
        tmp = arch_to_object(at);
        if (tmp == NULL)
            LOG(llevError, "Failed to clone arch poisoning.\n");
        else {
            tmp = object_insert_in_ob(tmp, op);
            /*  peterm:  give poisoning some teeth.  It should
             * be able to kill things better than it does:
             * damage should be dependent something--I choose to
             * do this:  if it's a monster, the damage from the
             * poisoning goes as the level of the monster/2.
             * If anything else, goes as damage.
             */

            if (QUERY_FLAG(hitter, FLAG_ALIVE))
                tmp->stats.dam += hitter->level/2;
            else
                tmp->stats.dam = dam;

            object_copy_owner(tmp, hitter);   /*  so we get credit for poisoning kills */
            skill = hitter->skill;
            if (!skill && hitter->chosen_skill)
                skill = hitter->chosen_skill->name;

            if (skill && skill != tmp->skill) {
                if (tmp->skill)
                    free_string(tmp->skill);
                tmp->skill = add_refcount(skill);
            }

            tmp->stats.food += dam;  /*  more damage, longer poisoning */

            if (op->type == PLAYER) {
                /* player looses stats, maximum is -10 of each */
                tmp->stats.Con = MAX(-(dam/4+1), -10);
                tmp->stats.Str = MAX(-(dam/3+2), -10);
                tmp->stats.Dex = MAX(-(dam/6+1), -10);
                tmp->stats.Int = MAX(-dam/7, -10);
                SET_FLAG(tmp, FLAG_APPLIED);
                fix_object(op);
                draw_ext_info(NDI_UNIQUE, 0, op,
                              MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_BAD_EFFECT_START,
                              "You suddenly feel very ill.", NULL);
            }
            if (hitter->type == PLAYER)
                draw_ext_info_format(NDI_UNIQUE, 0, hitter,
                                     MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_DID_HIT,
                                     "You poison %s.",
                                     "You poison %s.",
                                     op->name);
            else {
                object *owner;

                owner = object_get_owner(hitter);
                if (owner != NULL && owner->type == PLAYER)
                    draw_ext_info_format(NDI_UNIQUE, 0, owner,
                        MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_PET_HIT,
                        "Your %s poisons %s.",
                        "Your %s poisons %s.",
                        hitter->name, op->name);
            }
            tmp->speed_left = 0;
        }
    } else
        tmp->stats.food++;
}

/**
 * Slow a living thing.
 *
 * @param op
 * victim.
 * @param hitter
 * who is attacking.
 * @param dam
 * damage to deal.
 */
static void slow_living(object *op, object *hitter, int dam) {
    archetype *at = find_archetype("slowness");
    object *tmp;

    if (at == NULL) {
        LOG(llevError, "Can't find slowness archetype.\n");
    }
    tmp = arch_present_in_ob(at, op);
    if (tmp == NULL) {
        tmp = arch_to_object(at);
        tmp = object_insert_in_ob(tmp, op);
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_BAD_EFFECT_START,
                      "The world suddenly moves very fast!", NULL);
    } else
        tmp->stats.food++;
    SET_FLAG(tmp, FLAG_APPLIED);
    tmp->speed_left = 0;
    fix_object(op);
}

/**
 * Confuse a living thing.
 *
 * @param op
 * victim.
 * @param hitter
 * who is attacking.
 * @param dam
 * damage to deal.
 */
void confuse_living(object *op, object *hitter, int dam) {
    object *tmp;
    int maxduration;

    tmp = object_present_in_ob_by_name(FORCE, "confusion", op);
    if (!tmp) {
        tmp = create_archetype(FORCE_NAME);
        tmp = object_insert_in_ob(tmp, op);
    }

    /* Duration added per hit and max. duration of confusion both depend
     *  on the player's resistance
     */
    tmp->speed = 0.05;
    tmp->subtype = FORCE_CONFUSION;
    tmp->duration = 8+MAX(1, 5*(100-op->resist[ATNR_CONFUSION])/100);
    if (tmp->name)
        free_string(tmp->name);
    tmp->name = add_string("confusion");
    maxduration = MAX(2, 30*(100-op->resist[ATNR_CONFUSION])/100);
    if (tmp->duration > maxduration)
        tmp->duration = maxduration;

    if (op->type == PLAYER && !QUERY_FLAG(op, FLAG_CONFUSED))
        draw_ext_info(NDI_UNIQUE, 0, op, MSG_TYPE_ATTRIBUTE, MSG_TYPE_ATTRIBUTE_BAD_EFFECT_START,
                      "You suddenly feel very confused!", NULL);
    SET_FLAG(op, FLAG_CONFUSED);
}

/**
 * Blind a living thing.
 *
 * @param op
 * victim.
 * @param hitter
 * who is attacking.
 * @param dam
 * damage to deal.
 */
void blind_living(object *op, object *hitter, int dam) {
    object *tmp, *owner;
    char victim[MAX_BUF];

    /* Save some work if we know it isn't going to affect the player */
    if (op->resist[ATNR_BLIND] == 100)
        return;

    tmp = object_present_in_ob(BLINDNESS, op);
    if (!tmp) {
        tmp = create_archetype("blindness");
        SET_FLAG(tmp, FLAG_BLIND);
        SET_FLAG(tmp, FLAG_APPLIED);
        /* use floats so we don't lose too much precision due to rounding errors.
         * speed is a float anyways.
         */
        tmp->speed = tmp->speed*(100.0-(float)op->resist[ATNR_BLIND])/100;

        tmp = object_insert_in_ob(tmp, op);
        change_abil(op, tmp);  /* Mostly to display any messages */
        fix_object(op);        /* This takes care of some other stuff */

        owner = object_get_owner(hitter);
        if (owner == NULL)
            owner = hitter;

        query_name(op, victim, MAX_BUF);
        draw_ext_info_format(NDI_UNIQUE, 0, owner, MSG_TYPE_ATTACK, MSG_TYPE_ATTACK_DID_HIT,
                             "Your attack blinds %s!",
                             "Your attack blinds %s!",
                             victim);
    }
    tmp->stats.food += dam;
    if (tmp->stats.food > 10)
        tmp->stats.food = 10;
}

/**
 * Paralyze a living thing.
 *
 * @param op
 * victim.
 * @param hitter
 * who is attacking.
 * @param dam
 * damage to deal.
 */
void paralyze_living(object *op, object *hitter, int dam) {
    float effect, max;
    /* object *tmp; */

    /* This is strange stuff... someone knows for what this is
     * written? Well, i think this can and should be removed
    */

/*
    tmp = map_find_by_type(op->map, op->x, op->y, PARAIMAGE);
    if (tmp == NULL) {
        tmp = clone_arch(PARAIMAGE);
        object_insert_in_map_at(tmp, op->map, tmp, INS_NO_MERGE|INS_NO_WALK_ON, op->x, op->y);
    }
*/

    /* Do this as a float - otherwise, rounding might very well reduce this to 0 */
    effect = (float)dam*3.0*(100.0-op->resist[ATNR_PARALYZE])/100;

    if (effect == 0)
        return;

    op->speed_left -= FABS(op->speed)*effect;
/*  tmp->stats.food += (signed short)
        effect/op->speed;*/

    /* max number of ticks to be affected for. */
    max = (100-op->resist[ATNR_PARALYZE])/2;
    if (op->speed_left < -(FABS(op->speed)*max))
        op->speed_left = (float)-(FABS(op->speed)*max);

/*  tmp->stats.food = (signed short)(max/FABS(op->speed)); */
}

/**
 * Attempts to kill 'op'.
 *
 * The intention of a death attack is to kill outright things
 * that are a lot weaker than the attacker, have a chance of killing
 * things somewhat weaker than the caster, and no chance of
 * killing something equal or stronger than the attacker.
 *
 * If a deathstrike attack has a slaying, only a monster
 * whose name or race matches a comma-delimited list in the slaying
 * field of the deathstriking object is affected (this includes undead).
 * If no slaying set, only undead are unaffected.
 *
 * @param op
 * victim.
 * @param hitter
 * attacker.
 * @param[out] dam
 * damage to deal, will contain computed damage or 0 if strike failed.
 */
static void deathstrike_living(object *op, object *hitter, int *dam) {
    int atk_lev, def_lev, kill_lev;

    if (hitter->slaying) {
        if (!((QUERY_FLAG(op, FLAG_UNDEAD) && strstr(hitter->slaying, undead_name))
        || (op->race && strstr(hitter->slaying, op->race))))
            return;
    } else
        if (QUERY_FLAG(op, FLAG_UNDEAD))
            return;

    def_lev = op->level;
    if (def_lev < 1) {
        LOG(llevError, "BUG: arch %s, name %s with level < 1\n", op->arch->name, op->name);
        def_lev = 1;
    }
    atk_lev = (hitter->chosen_skill ? hitter->chosen_skill->level : hitter->level)/2;
    /* LOG(llevDebug, "Deathstrike - attack level %d, defender level %d\n", atk_lev, def_lev); */

    if (atk_lev >= def_lev) {
        kill_lev = random_roll(0, atk_lev-1, hitter, PREFER_HIGH);

        /* Note that the below effectively means the ratio of the atk vs
         * defener level is important - if level 52 character has very little
         * chance of killing a level 50 monster.  This should probably be
         * redone.
         */
        if (kill_lev >= def_lev) {
            *dam = op->stats.hp+10; /* take all hp. they can still save for 1/2 */
            /* I think this doesn't really do much.  Because of
             * integer rounding, this only makes any difference if the
             * attack level is double the defender level.
             */
            *dam *= kill_lev/def_lev;
        }
    } else {
        *dam = 0;  /* no harm done */
    }
}

/**
 * Handles any special effects of thrown
 * items (like attacking living creatures--a potion thrown at a
 * monster).
 *
 * @param hitter
 * thrown item.
 * @param victim
 * object that is hit by hitter.
 * @todo
 * invert parameters for coherence with other functions?
 */
static void thrown_item_effect(object *hitter, object *victim) {
    if (!QUERY_FLAG(hitter, FLAG_ALIVE)) {
        /* May not need a switch for just 2 types, but this makes it
         * easier for expansion.
         */
        switch (hitter->type) {
        case POTION:
            /* should player get a save throw instead of checking magic protection? */
            if (QUERY_FLAG(victim, FLAG_ALIVE)
            && !QUERY_FLAG(victim, FLAG_UNDEAD)
            && (victim->resist[ATNR_MAGIC] < 60))
                (void)ob_apply(hitter, victim, 0);
            break;

        case POISON: /* poison drinks */
            /* As with potions, should monster get a save? */
            if (QUERY_FLAG(victim, FLAG_ALIVE)
            && !QUERY_FLAG(victim, FLAG_UNDEAD)
            && (victim->resist[ATNR_POISON] < 60))
                (void)ob_apply(victim, hitter, 0);
            break;

            /* Removed case statements that did nothing.
             * food may be poisonous, but monster must be willing to eat it,
             * so we don't handle it here.
             * Containers should perhaps break open, but that code was disabled.
             */
        }
    }
}

/**
 * Adjustments to attack rolls by various conditions
 * @param hitter
 * who is hitting.
 * @param target
 * victim of the attack.
 * @return
 * adjustment to attack roll.
 */
static int adj_attackroll(object *hitter, object *target) {
    object *attacker = hitter;
    int adjust = 0;

    /* safety */
    if (!target || !hitter || !hitter->map || !target->map || !on_same_map(hitter, target)) {
        LOG(llevError, "BUG: adj_attackroll(): hitter and target not on same map\n");
        return 0;
    }

    /* aimed missiles use the owning object's sight */
    if (is_aimed_missile(hitter)) {
        attacker = object_get_owner(hitter);
        if (attacker == NULL)
            attacker = hitter;
        /* A player who saves but hasn't quit still could have objects
         * owned by him - need to handle that case to avoid crashes.
         */
        if (QUERY_FLAG(attacker, FLAG_REMOVED))
            attacker = hitter;
    } else if (!QUERY_FLAG(hitter, FLAG_ALIVE))
        return 0;

    /* determine the condtions under which we make an attack.
     * Add more cases, as the need occurs. */

    if (!monster_can_see_enemy(attacker, target)) {
        /* target is unseen */
        if (target->invisible || QUERY_FLAG(attacker, FLAG_BLIND))
            adjust -= 10;
        /* dark map penalty for the hitter (lacks infravision if we got here). */
        else if (target->map && target->map->darkness > 0 && !monster_stand_in_light(target))
            adjust -= target->map->darkness;
    }

    if (QUERY_FLAG(attacker, FLAG_SCARED))
        adjust -= 3;

    if (QUERY_FLAG(target, FLAG_UNAGGRESSIVE))
        adjust += 1;

    if (QUERY_FLAG(target, FLAG_SCARED))
        adjust += 1;

    if (QUERY_FLAG(attacker, FLAG_CONFUSED))
        adjust -= 3;

    /* if we attack at a different 'altitude' its harder
     * Note - only make this adjustment if the target actually
     * has a move type.  Doors don't (they don't move), and
     * this would evaluate as true.  If anything, something without
     * a move type should be easier to hit.
     */
    if (target->move_type && (attacker->move_type&target->move_type) == 0)
        adjust -= 2;

    return adjust;
}

/**
 * Determine if the object is an 'aimed' missile.
 *
 * @param op
 * object to check.
 * @return
 * 1 if aimed missile, 0 else.
 */
static int is_aimed_missile(object *op) {
    /* I broke what used to be one big if into a few nested
     * ones so that figuring out the logic is at least possible.
     */
    if (op && (op->move_type&MOVE_FLYING)) {
        if (op->type == ARROW || op->type == THROWN_OBJ)
            return 1;
        else if (op->type == SPELL_EFFECT
        && (op->subtype == SP_BULLET || op->subtype == SP_EXPLOSION))
            return 1;
    }
    return 0;
}