All files / js uhitm.js

60.26% Statements 1397/2318
53.2% Branches 407/765
57.69% Functions 45/78
60.26% Lines 1397/2318

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 231973x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 1225x 19x 19x 36x 14x       73x 73x 73x 73x 73x 73x           73x 73x 73x 73x 73x 73x 73x 5x 5x 30x 5x 5x 30x   5x 73x                 73x 73x 73x 73x   73x 73x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 2x 2x 2x 2x 2x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 54x 54x 54x 54x 54x 54x 6x 6x 6x 6x 6x 1x 1x 6x 5x 5x 1x 1x 5x 4x 4x 4x 6x 54x 54x 54x 54x 54x 54x 54x 23x 23x 54x 56x 56x 56x 56x 2x 2x 56x 56x 56x 56x 56x 56x 56x 56x     56x 2x 2x 2x 2x     56x     56x 56x 56x 73x 73x 73x 73x 73x 13x 13x 13x 13x 13x 13x                 13x 12x 13x 1x 1x 13x 6x 6x 6x 6x       13x 6x             6x               6x 13x 13x 13x 73x   73x 73x 73x                 73x 73x 73x 73x   73x 73x 73x 73x 73x 73x 73x 73x 74x 74x 73x 73x 23x 22x 16x       74x 73x                           73x 73x 115x 115x     115x 115x 115x 73x 73x 73x 28x 28x 28x 28x 28x 28x 5x 4x 4x     4x 5x 5x 23x 28x 73x 73x 73x             73x 73x 73x                                                                                                                     73x 73x 73x 116x 116x 116x 116x 116x 115x 116x 116x 116x 116x 116x 116x 116x 1x 1x 1x 1x 1x 114x 116x                 114x 116x 73x 73x 73x               73x 73x 73x 116x 116x 116x 116x 116x 116x 116x 116x 114x 114x 116x 116x 116x 116x 116x 7x     7x 7x 7x 7x 116x 116x 116x 116x 116x 116x 116x 115x 115x 116x     116x 116x 73x 73x 116x           116x 73x 73x 73x 40x 40x 40x 40x     40x 40x 40x 73x 73x                       73x 73x 73x 41x 41x 41x 41x     41x                         41x 73x 73x                   73x 73x 16x 16x 16x 16x 73x 73x 1x 1x 1x 1x 1x         1x 1x               1x 1x 73x 73x                     73x 73x 73x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 73x 73x 73x 161x 4x 4x 4x   161x 161x 2x 2x 2x 161x 161x 161x 161x 161x 161x 161x 161x 161x 161x 161x 161x 161x 21x 21x 69x 161x 161x 8x 8x 8x 161x 161x 161x 161x 161x 161x 161x 161x 6x 6x 79x 2x 2x 2x 2x 2x 2x 2x 2x 161x                 6x 161x 161x 161x 6x 6x 27x 2x 161x 161x 2x 2x 23x 2x 161x 161x 2x 23x                     2x 23x 161x 161x 2x 2x 2x 2x 2x 2x 2x 23x     161x 161x 2x 2x 2x       2x 2x 2x 2x 2x 2x 2x 23x                         2x 2x 2x 2x 2x 161x 73x 73x 12x 12x   12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 73x 9x 9x 9x 9x 9x 9x       9x 9x                   9x 73x 73x                   73x 68x 68x 68x 68x 68x 68x 68x 68x 12x 12x 12x 68x 68x 68x 14x 68x 54x       54x 54x 54x 1x 1x 54x 54x                               68x 5x 5x 5x 5x                         5x 68x     68x 68x 68x 68x         68x 68x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 68x 68x 68x 73x 77x 77x 77x 77x 77x 77x 77x 9x 77x 68x 68x 68x 77x 73x                                       73x                                                                                                                                                                                                                                                                         73x 89x 89x 12x 89x 77x 77x 77x             77x 77x 77x 77x 77x 77x 77x                     77x 89x 73x 89x 89x 89x 89x 89x 74x 74x 74x 74x 73x 73x 74x 74x 89x 89x 80x 80x 74x 74x 74x 80x 89x 89x 89x 73x 73x                                         73x 73x                                 73x 8x 8x 8x               8x 73x 89x 89x         89x 73x 73x 89x 89x 89x 89x 89x   89x                     89x 73x 89x 89x 89x 89x 89x 37x 15x 15x 37x 22x 20x 20x 20x 20x 20x 20x 20x 19x 37x 89x 73x                                       73x                     73x 73x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x                 89x   89x 8x 88x 81x 51x 51x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 89x 88x 88x 88x 88x 88x     88x 88x 88x         88x 52x 52x 52x 52x 52x 52x 88x               88x 88x 88x 88x 89x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 89x 89x 73x 73x 73x 89x 89x 89x 89x 88x 88x 89x 73x 73x 114x 114x 114x     114x 40x 111x 74x 74x 74x 62x 62x 62x 74x 74x 73x 21x 21x             21x       21x 74x 113x 114x 73x 5x 5x 5x 5x 5x 5x 5x       5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 15x 15x 15x 15x     15x 15x 15x 10x     10x 10x 5x 5x 5x 5x 5x 5x 5x 15x 15x 15x   15x 5x 5x 5x 15x 15x 15x 5x 5x 15x     15x 5x 5x 5x 5x 5x 5x 5x     5x 5x 5x 5x 73x 73x 18x 18x 18x 18x 18x 18x 73x 73x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 112x 5x 5x 112x 112x 107x 107x 112x 112x 107x 107x 106x 112x 106x 106x 106x 1x 1x 112x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 106x 106x 112x 73x 73x 73x 1x 1x 1x 1x 1x       1x 1x   1x 1x 1x 1x 1x 1x   1x   1x 1x 1x 1x 1x 1x         1x 73x 73x 73x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x                       1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x       2x   2x 2x 73x 73x 73x 2x 2x 2x 2x 2x 2x 2x 2x 2x       2x 2x 2x 2x 2x 2x 2x 2x 2x       2x       2x 2x 2x 2x 2x 2x   2x 73x 73x 2x 2x 2x 2x 2x 2x 2x 2x 12x 12x 12x 12x 12x 2x 2x 2x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x       1x 1x 1x 1x 3x 1x                                         3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x       1x 1x 1x 2x 2x     2x 2x 3x                 3x                           3x 3x 3x   3x 3x 3x     3x   3x 3x 2x 3x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 73x 73x 73x 180x 180x 180x 180x 180x 180x 65x 65x 65x 65x 65x 65x 65x 60x 60x 1x 1x 1x 1x 1x 1x 60x 65x 6x 6x 1x 1x 1x 5x 5x 5x 5x 5x 65x 59x     59x 59x 59x 65x 65x 115x 152x 115x 115x 180x 180x 155x 180x         114x 155x 114x 155x 152x 3x 3x 3x 3x 2x 2x 3x 3x 114x 114x 114x 180x 180x       114x 151x 2x 155x 112x 112x 112x 111x 180x 180x 180x 73x 73x 73x 116x 116x 116x 116x 116x 116x 116x 234x 234x 234x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x                     116x                               116x     116x                     116x                     116x     116x                     116x 116x 116x 116x 116x 39x 39x                                                         39x     39x     39x     39x     39x 39x 39x 39x 116x 116x 73x 73x 73x 11x           11x 11x 11x 11x 21x 21x 21x 11x 11x 11x 11x         11x         11x         11x         11x         11x 11x 11x 11x 73x 73x 73x                                         73x 73x 73x                 73x 73x 73x             73x 73x 73x           73x 73x 73x 73x 73x       73x 73x 73x             73x 73x 73x                                               73x 73x 73x                 73x 73x 73x     73x 73x 73x     73x 73x 73x 73x  
// uhitm.js — hero-vs-monster melee attack logic
// C ref: uhitm.c — hero attacking monsters
//
// Ported functions:
//   mhitm_mgc_atk_negated()   — magic cancellation check (uhitm.c:75)
//   dynamic_multi_reason()     — set multi_reason for paralysis (uhitm.c:104)
//   erode_armor()              — rust/corrode monster armor (uhitm.c:126)
//   attack_checks()            — pre-attack checks (uhitm.c:189)
//   force_attack()             — forced attack wrapper (uhitm.c:432)
//   find_roll_to_hit()         — combined to-hit calculation (uhitm.c:365)
//   mon_maybe_unparalyze()     — maybe unparalyze monster (uhitm.c:351)
//   do_attack()                — main attack entry point (uhitm.c:449)
//   hitum()                    — hit target monster (uhitm.c:759)
//   known_hitum()              — really hit target monster (uhitm.c:588)
//   hmon()                     — general damage monster (uhitm.c:820)
//   hmon_hitmon()              — guts of hmon (uhitm.c:1755)
//   hmon_hitmon_do_hit()       — dispatch to barehands/weapon/misc (uhitm.c:1388)
//   hmon_hitmon_barehands()    — bare-handed damage (uhitm.c:839)
//   hmon_hitmon_weapon()       — weapon damage dispatch (uhitm.c:1071)
//   hmon_hitmon_weapon_melee() — melee weapon damage (uhitm.c:935)
//   hmon_hitmon_weapon_ranged()— ranged weapon damage (uhitm.c:886)
//   hmon_hitmon_potion()       — potion as weapon (uhitm.c:1095)
//   hmon_hitmon_misc_obj()     — misc object as weapon (uhitm.c:1119)
//   hmon_hitmon_dmg_recalc()   — strength/skill damage bonus (uhitm.c:1437)
//   hmon_hitmon_poison()       — poisoned weapon effect (uhitm.c:1510)
//   hmon_hitmon_jousting()     — jousting damage (uhitm.c:1541)
//   hmon_hitmon_stagger()      — unarmed stagger (uhitm.c:1571)
//   hmon_hitmon_pet()          — pet abuse (uhitm.c:1589)
//   hmon_hitmon_splitmon()     — pudding splitting (uhitm.c:1605)
//   hmon_hitmon_msg_hit()      — hit message (uhitm.c:1638)
//   hmon_hitmon_msg_silver()   — silver sear message (uhitm.c:1663)
//   hmon_hitmon_msg_lightobj() — light sear message (uhitm.c:1702)
//   mhurtle_to_doom()          — hurtle check (uhitm.c:1942)
//   first_weapon_hit()         — livelog first weapon hit (uhitm.c:1963)
//   shade_aware()              — shade-affecting objects (uhitm.c:1992)
//   shade_miss()               — shade miss message (uhitm.c:2016)
//   m_slips_free()             — greased armor check (uhitm.c:2056)
//   joust()                    — jousting check (uhitm.c:2098)
//   demonpet()                 — summon demon (uhitm.c:2133)
//   theft_petrifies()          — stealing petrifying corpse (uhitm.c:2148)
//   steal_it()                 — player theft attack (uhitm.c:2174)
//   mhitm_ad_phys()            — physical damage type (uhitm.c:3981)
//   mhitm_adtyping()           — damage type dispatch (uhitm.c:4782)
//   damageum()                 — polymorphed hero damage (uhitm.c:4835)
//   m_is_steadfast()           — steadfast check (uhitm.c:5218)
//   mhitm_knockback()          — knockback mechanic (uhitm.c:5247)
//   missum()                   — miss message (uhitm.c:5198)
//   hmonas()                   — attack as monster (uhitm.c:5424)
//   passive()                  — passive attack after melee (uhitm.c:5865)
//   passive_obj()              — passive attack on weapon (uhitm.c:6127)
//   nohandglow()               — confusion touch fade (uhitm.c:6315)
//   backstabbable()            — backstab check (uhitm.c:921)
//   double_punch()             — double punch check (uhitm.c:736)
//   that_is_a_mimic()          — mimic reveal message (uhitm.c:6201)
//   stumble_onto_mimic()       — stumble into mimic (uhitm.c:6282)
//   flash_hits_mon()           — flash affects monster (uhitm.c:6341)
//   light_hits_gremlin()       — light damages gremlin (uhitm.c:6425)
import { game } from './gstate.js';
import { rn2, rnd, c_d, rne, rn1, rnl, pushRngLogEntry } from './rng.js';
import { A_DEX, A_LAWFUL, A_STR, A_WIS, A_CON, DIR_ERR, HMON_APPLIED, HMON_KICKED, HMON_MELEE, HMON_THROWN, M_ATTK_DEF_DIED, M_ATTK_AGR_DIED, M_ATTK_HIT, M_ATTK_MISS, NATTK, NO_MM_FLAGS, NO_TRAP_FLAGS, NON_PM, ARTICLE_NONE, ARTICLE_THE, SUPPRESS_SADDLE, has_mgivenname, P_BARE_HANDED_COMBAT, P_BASIC, P_ISRESTRICTED, P_NONE, P_TWO_WEAPON_COMBAT, P_UNSKILLED, P_SKILLED, P_KNIFE, P_LANCE, P_WHIP, SHOPBASE, STRAT_WAITFORU, STRAT_WAITMASK, W_AMUL, W_ARMG, W_ARMH, W_ARMC, W_ARM, W_ARMU, W_ARMS, W_ARMF, W_RINGL, W_RINGR, W_ARMOR, XKILL_NOMSG, XKILL_NOCORPSE, W_SADDLE, NOTELL, ERODE_RUST, ERODE_CORRODE, ERODE_BURN, ERODE_ROT, EF_GREASE, EF_VERBOSE, ER_NOTHING, CORPSTAT_INIT, CORPSTAT_FEMALE, CORPSTAT_MALE, CORPSTAT_NONE, ACCESSIBLE, HVY_ENCUMBER, IS_POOL, N_DIRS, engulfing_u, TEST_MOVE, IS_DOOR, DISMOUNT_KNOCKED, STR18, xdir, ydir } from './const.js';
import {
    AT_WEAP, AT_CLAW, AT_KICK, AT_NONE, AT_BUTT, AT_ENGL, AT_HUGS,
    AT_TUCH, AT_BITE, AT_STNG, AT_TENT, AT_EXPL, AT_MAGC, AT_BREA,
    AT_SPIT, AT_GAZE, AT_BOOM,
    AD_PHYS, AD_FIRE, AD_COLD, AD_ELEC, AD_ACID, AD_STUN, AD_MAGM,
    AD_PLYS, AD_STON, AD_RUST, AD_CORR, AD_ENCH, AD_STCK, AD_DRLI,
    AD_DRIN, AD_WRAP, AD_SLEE, AD_SLIM, AD_SLOW, AD_CONF, AD_POLY,
    AD_DISE, AD_SAMU, AD_DETH, AD_PEST, AD_FAMN, AD_DGST, AD_HALU,
    AD_WERE, AD_HEAL, AD_LEGS, AD_SGLD, AD_TLPT, AD_BLND, AD_CURS,
    AD_DRST, AD_DRDX, AD_DRCO, AD_DCAY, AD_DREN, AD_SEDU, AD_SITM,
    AD_SSEX, AD_SPEL,
    PM_MONK, PM_ELF, PM_SHADE, PM_WOOD_GOLEM, PM_ROGUE, PM_BARBARIAN, PM_SAMURAI,
    PM_KNIGHT, PM_HEALER,
    PM_BLACK_PUDDING, PM_BROWN_PUDDING, PM_AMOROUS_DEMON, PM_BALROG,
    PM_FLOATING_EYE, PM_GREEN_SLIME, PM_IRON_GOLEM, PM_ROPE_GOLEM, PM_STONE_GOLEM,
    S_LEPRECHAUN, S_BLOB, S_EYE, S_FUNGUS, S_LIGHT, S_KOP,
    G_NOCORPSE, M2_COLLECT, MZ_HUMAN,
    mons,
} from './monsters.js';
import { ACID_VENOM, AMULET_OF_LIFE_SAVING, BALL_CLASS, BARDICHE, BATTLE_AXE, BILL_GUISARME, BLINDING_VENOM, BOOMERANG, BOULDER, BROADSWORD, CHAIN_CLASS, CLOVE_OF_GARLIC, COIN_CLASS, CORPSE, CREAM_PIE, CROSSBOW_BOLT, DWARVISH_MATTOCK, EGG, ELVEN_ARROW, ELVEN_BOW, ELVEN_BROADSWORD, EXPENSIVE_CAMERA, FIGURINE, FLAIL, FOOD_CLASS, GAUNTLETS_OF_POWER, GEM_CLASS, greatest_erosion, GUISARME, HALBERD, HEAVY_IRON_BALL, IRON, IRON_CHAIN, is_ammo, is_flimsy, is_poisonable, is_shield, KATANA, LEATHER, LOADSTONE, LUCERN_HAMMER, MACE, METAL, MIRROR, MORNING_STAR, NO_MATERIAL, OILSKIN_CLOAK, PAPER, PARTISAN, POTION_CLASS, RANDOM_CLASS, RANSEUR, RUNESWORD, SILVER, SILVER_MACE, SPBOOK_CLASS, SPETUM, stone_missile, TRIDENT, TSURUGI, TWO_HANDED_SWORD, VEGGY, VOULGE, WAR_HAMMER, WEAPON_CLASS, YA, YUMI } from './objects.js';
import { bimanual, highc, ing_suffix, is_art, MON_WEP, mtrapped_in_pit, s_suffix, sgn, unsolid } from './hacklib.js';
import { P_SKILL, abon, ammo_and_launcher, check_caitiff, hitval, martial_bonus, use_skill, uwep_skill_type, weapon_dam_bonus, weapon_hit_bonus, weapon_type } from './weapon.js';
import { ACURR, adjalign, exercise } from './attrib.js';
import { p_coaligned } from './priest.js';
import {
    Blind,
    Confusion,
    Flying,
    Fumbling,
    Hallucination,
    Is_airlevel,
    Is_waterlevel,
    Levitation,
    Race_if,
    Role_if,
    Slimed,
    Stunned,
    Unchanging,
    Upolyd,
    maybe_polyd,
    getLuck
} from './macros.js';
import { amorphous, attacktype, bigmonst, can_be_strangled, completelyburns, completelyrots, completelyrusts, defended, dmgtype, has_head, haseyes, hides_under, humanoid, is_axe, is_blade, is_demon, is_elf, is_floater, is_flyer, is_launcher, is_longworm, is_missile, is_neuter, is_orc, is_pole, is_undead, is_unsolid, is_vampshifter, is_watch, is_weptool, is_wet_towel, is_whirly, mon_hates_blessings, mon_hates_light, mon_hates_silver, monsndx, noattacks, nohands, noncorporeal, nonliving, passes_rocks, resists_acid, resists_blnd, resists_cold, resists_drli, resists_elec, resists_fire, resists_poison, resists_ston, sticks, thick_skinned, touch_petrifies, verysmall } from './mondata.js';
import { change_luck, check_capacity, end_running, helpless, is_safemon, killed, NODIAG, test_move } from './hack.js';
import { doorless_door } from './hack.js';
import { canseemon, canspotmon, glyph_at, map_invisible, newsym, SET_BOTL, setBotl, unmap_invisible } from './display.js';
import { DEADMONSTER, Monnam, corpse_chance, failed_grab, golemeffects, level_specific_nocorpse, m_next2u, make_corpse, mhis, mon_nam, mondead, monflee, some_mon_nam, troll_baned, unstuck, wakeup, x_monnam, y_monnam } from './mon.js';
import { m_at } from './map_access.js';
import { could_seduce, getmattk } from './mhitu.js';
import { mhurtle } from './dothrow.js';
import { exclam, obj_resists, resist } from './zap.js';
import { destroy_items, ignite_items, mkcorpstat, mkobj, next_ident, obfree, place_object, splitobj } from './mkobj.js';
import { cansee } from './vision.js';
import { ndemon } from './minion.js';
import { clone_mon, makemon } from './makemon.js';
import { tamedog } from './dog.js';
import { near_capacity } from './pickup.js';
import { find_mac, which_armor } from './worn.js';
import { impossible, pline, pline_The, You, You_feel, You_hear, Your } from './pline.js';
import { eat_brains, gethungry } from './eat.js';
import { helm_simple_name, xname, cxname, yname, Yname2, vtense } from './objnam.js';
import { magic_negation } from './mhitu.js';
import { livelog_printf } from './hack.js';
import { make_slimed, potionhit, make_stunned } from './potion.js';
import { breaktest, release_camera_demon, will_hurtle, hurtle } from './dothrow.js';
import { erode_obj, mintrap } from './trap.js';
import { flooreffects } from './do.js';
import { freeinv, useup, useupall } from './invent.js';
import { carrying, delobj, stackobj } from './invent.js';
import { setmnotwielded, setuwep } from './wield.js';
import { set_twoweap, uwepgone } from './wield.js';
import { u_wipe_engr } from './engrave.js';
import { artifact_light } from './light.js';
import { m_carrying, m_useupall } from './mthrowu.js';
import { ghod_hitsu } from './priest.js';
import { isok } from './const.js';
import { experience, more_experienced, newexplevel } from './exper.js';
import { angry_guards } from './mklev.js';
import { dopay, in_rooms, tended_shop } from './shk.js';
import { fall_asleep } from './timeout.js';
import { artifact_hit as artifact_hit_impl, permapoisoned, u_wield_art } from './artifact.js';
import { set_apparxy } from './monmove.js';
import { is_pool } from './dbridge.js';
import { dismount_steed } from './steed.js';
import { ART_CLEAVER, ART_GIANTSLAYER, ART_GRIMTOOTH, ART_OGRESMASHER, ART_SNICKERSNEE, ART_STORMBRINGER } from './artifacts.js';
// erode_obj not yet ported — local stub
// ── Helpers (local, matching C macros/functions not yet centrally exported) ──
function uwep_get() { return game.u.uwep; }
function uswapwep_get() { return game.u.uswapwep; }
function uarm_get() { return game.u.uarm; }
function uarms_get() { return game.u.uarms; }
function uarmg_get() { return game.u.uarmg; }
function uarmf_get() { return game.u.uarmf; }
function uarmh_get() { return game.u.uarmh; }
function uarmc_get() { return game.u.uarmc; }
// C ref: display.h:159 — _is_safemon macro
// is_launcher: imported from mondata.js
// is_missile: imported from mondata.js
// is_axe: imported from mondata.js
// is_shield: imported from objects.js
// is_flimsy: imported from objects.js
function is_blunt_weapon(obj) {
    // C: !is_blade && !is_axe && !is_pole... simplified
    if (!obj) return true;
    return !is_blade(obj) && !is_axe(obj);
}
// ammo_and_launcher: imported from weapon.js
// artifact_hit: imported from artifact.js as artifact_hit_impl
// artifact_hit: imported from artifact.js
// stone_missile imported from objects.js
// passes_rocks imported from mondata.js
let cleave_clockwise = false;
 
function xytodir(dx, dy) {
    for (let i = 0; i < N_DIRS; i++) {
        if (xdir[i] === dx && ydir[i] === dy) {
            return i;
        }
    }
    return DIR_ERR;
}
// C ref: artifact.c:555 shade_glare — does weapon affect shades?
function shade_glare(obj) {
    if (!obj) return false;
    // Any silver object is effective against shades
    if ((game.objects[obj.otyp]?.oc_material ?? 0) === SILVER) return true;
    // Non-silver artifacts with anti-undead bonus also work
    // TODO: check artifact spfx when artifact system is fully ported
    return false;
}
// greatest_erosion: imported from objects.js
// Stubs for deps not yet fully ported
// permapoisoned imported from artifact.js
// clone_mon: imported from makemon.js
function mhitm_adtyping(_magr, _mattk, _mdef, _mhm) { /* see full port below */ }
// troll_baned: imported from mon.js
export async function xkilled(mdef, xkill_flags) {
    if (!mdef) return;
    const XKILL_NOMSG = 1;
    const XKILL_NOCORPSE = 2;
    const XKILL_NOCONDUCT = 4;
    const mx = mdef.mx, my = mdef.my;
    const wasinside = engulfing_u(mdef);
    const nocorpse = (xkill_flags & XKILL_NOCORPSE) !== 0;
    const nomsg = (xkill_flags & XKILL_NOMSG) !== 0;
    const noconduct = (xkill_flags & XKILL_NOCONDUCT) !== 0;
    if (!(xkill_flags & XKILL_NOMSG)) {
        // C ref: mon.c:3485-3490 — "destroy" for nonliving, "kill" otherwise
        const verb = nonliving(mdef.data) ? 'destroy' : 'kill';
        const namedpet = has_mgivenname(mdef) && !Hallucination();
        const target = !(wasinside || canspotmon(mdef))
            ? 'it'
            : !mdef.mtame
                ? mon_nam(mdef)
                : x_monnam(
                    mdef,
                    namedpet ? ARTICLE_NONE : ARTICLE_THE,
                    'poor',
                    namedpet ? SUPPRESS_SADDLE : 0,
                    false
                );
        await You(`${verb} ${target}!`);
    }
    mdef.mhp = 0;
    // C ref: mon.c:3479-3482 — conduct counter for first kill and onward.
    if (!noconduct) {
        if (!game.u.uconduct) game.u.uconduct = {};
        game.u.uconduct.killer = (game.u.uconduct.killer || 0) + 1;
    }
    // C ref: mon.c:3475-3608 xkilled
    mondead(mdef);
    if (!nocorpse) {
        const mndx = monsndx(mdef.data);
        const noCorpseVital = !!(game.mvitals?.[mndx]?.mvflags & G_NOCORPSE);
        const levelNoCorpse = level_specific_nocorpse(mdef.data);
        const loc = game.level?.at?.(mx, my);
        if (!levelNoCorpse && loc && (ACCESSIBLE(loc.typ) || IS_POOL(loc.typ))) {
            // C ref: mon.c:3566-3574 — roll is evaluated first, then other guards.
            if (!rn2(6)
                && !noCorpseVital
                && (mx !== game.u.ux || my !== game.u.uy)
                && mdef.data?.mlet !== S_KOP
                && !mdef.mcloned) {
                let otmp = mkobj(RANDOM_CLASS, true);
                const otyp = otmp?.otyp ?? 0;
                if (otmp?.oclass === FOOD_CLASS
                    && !(mdef.data?.mflags2 & M2_COLLECT)
                    && !otmp.oartifact) {
                    delobj(otmp);
                    otmp = null;
                } else if ((mdef.data?.msize ?? MZ_HUMAN) < MZ_HUMAN
                    && otyp !== FIGURINE
                    && (((otmp?.owt ?? 0) > 30) || !!game.objects?.[otyp]?.oc_big)) {
                    delobj(otmp);
                    otmp = null;
                } else if (otmp && !(await flooreffects(otmp, mx, my, nomsg ? '' : 'fall'))) {
                    place_object(otmp, mx, my);
                    stackobj(otmp);
                }
            }
            // C ref: mon.c:3597 — corpse creation is skipped when hero was inside target.
            // C ref: mon.c xkilled() calls corpse_chance() first, then
            // make_corpse(). make_corpse() itself suppresses G_NOCORPSE
            // species (for example, grid bugs), but the corpse_chance RNG
            // roll has already happened.
            const shouldMakeCorpse = corpse_chance(mdef);
            if (!wasinside && shouldMakeCorpse) {
                await make_corpse(mdef, CORPSTAT_NONE);
            }
        }
    }
    // C ref: mon.c:3629 — monster is gone, corpse or object might be visible
    newsym(mx, my);
    if ((mdef.mpeaceful && !rn2(2)) || mdef.mtame) {
        change_luck(-1);
    }
    // C ref: mon.c:3650-3653 — XP award using experience() formula
    const mndx2 = monsndx(mdef.data);
    const nk = (game.mvitals?.[mndx2]?.died ?? 0);
    const xp = experience(mdef, nk);
    more_experienced(xp, 0);
    await newexplevel();
    // C ref: mon.c:3663-3712 — alignment adjustment on kill
    if (mdef.ispriest) {
        adjalign(p_coaligned(mdef) ? -2 : 2);
        if (p_coaligned(mdef)) game.u.ublessed = 0;
    } else if (mdef.mtame) {
        adjalign(-15);
        if (!Hallucination()) {
            await You_hear("the rumble of distant thunder...");
        } else {
            await You_hear("the studio audience applaud!");
        }
    } else if (mdef.mpeaceful) {
        adjalign(-5);
    }
    // C ref: mon.c:3712 — malign already set by set_malign for alignment type
    adjalign(mdef.malign ?? 0);
}
// is_watch: imported from mondata.js
// angry_guards: imported from mklev.js
// u_wield_art: imported from artifact.js
// C ref: weapon.c:361 special_dmgval — bonus damage from blessed/silver armor/rings
export function special_dmgval(magr, mdef, armask, silverhitRef) {
    let obj = null;
    const left_ring = !!(armask & W_RINGL);
    const right_ring = !!(armask & W_RINGR);
    let silverhit = 0;
    let bonus = 0;
    if (armask & (W_ARMC | W_ARM | W_ARMU)) {
        if ((armask & W_ARMC) && (obj = which_armor(magr, W_ARMC)))
            armask = W_ARMC;
        else if ((armask & W_ARM) && (obj = which_armor(magr, W_ARM)))
            armask = W_ARM;
        else if ((armask & W_ARMU) && (obj = which_armor(magr, W_ARMU)))
            armask = W_ARMU;
        else
            armask = 0;
    } else if (armask & (W_ARMG | W_RINGL | W_RINGR)) {
        armask = (obj = which_armor(magr, W_ARMG)) ? W_ARMG : 0;
    } else {
        obj = which_armor(magr, armask);
    }
    if (obj) {
        if (obj.blessed && mon_hates_blessings(mdef))
            bonus += rnd(4);
        if ((game.objects[obj.otyp]?.oc_material ?? 0) === SILVER
            && mon_hates_silver(mdef)) {
            bonus += rnd(20);
            silverhit |= armask;
        }
    } else if ((left_ring || right_ring) && magr === game.youmonst) {
        if (left_ring && game.u.uleft) {
            if ((game.objects[game.u.uleft.otyp]?.oc_material ?? 0) === SILVER
                && mon_hates_silver(mdef)) {
                bonus += rnd(20);
                silverhit |= W_RINGL;
            }
        }
        if (right_ring && game.u.uright) {
            if ((game.objects[game.u.uright.otyp]?.oc_material ?? 0) === SILVER
                && mon_hates_silver(mdef)) {
                if (!(silverhit & W_RINGL))
                    bonus += rnd(20);
                silverhit |= W_RINGR;
            }
        }
    }
    if (silverhitRef) silverhitRef.value = silverhit;
    return bonus;
}
// failed_grab: imported from mon.js
function silver_sears(_magr, _mdef, _silverhit) { /* TODO */ }
// setmnotwielded imported from wield.js
// is_vampshifter imported from mondata.js
// C ref: mon.c:2813 mlifesaver — does monster have life-saving amulet?
function mlifesaver(mon) {
    if (!nonliving(mon?.data) || is_vampshifter(mon)) {
        const otmp = which_armor(mon, W_AMUL);
        if (otmp && otmp.otyp === AMULET_OF_LIFE_SAVING)
            return otmp;
    }
    return null;
}
// defended imported from mondata.js
// golemeffects: imported from mon.js
// destroy_items: imported from mkobj.js
// C ref: mondata.h — hug_throttles: rope golem throttles with hugs
function hug_throttles(ptr) { return ptr === mons[PM_ROPE_GOLEM]; }
// mtrapped_in_pit: imported from hacklib.js
// dmgval moved to weapon.js (matching C's weapon.c) to break circular
// dependency between uhitm.js and mhitu.js.
import { dmgval } from './weapon.js';
import { glyph_is_invisible, glyph_is_warning } from './glyph.js';
// C ref: weapon.c — dbon(): strength damage bonus
// C ref: weapon.c:988 dbon() — strength damage bonus using raw encoded STR
export function dbon() {
    const str = ACURR(A_STR);
    if (Upolyd()) return 0;
    if (str < 6) return -1;
    if (str < 16) return 0;
    if (str < 18) return 1;
    if (str === 18) return 2;
    if (str <= STR18(75)) return 3;   // up to 18/75 (=93)
    if (str <= STR18(90)) return 4;   // up to 18/90 (=108)
    if (str < STR18(100)) return 5;   // up to 18/99 (=117)
    return 6;
}
// uwep_skill_type: imported from weapon.js
async function overexert_hp() {
    const hpRef = Upolyd ? 'mh' : 'uhp';
    const hp = game.u?.[hpRef] ?? 0;
    if (hp > 1) {
        game.u[hpRef] = hp - 1;
        // C: disp.botl = TRUE; use tagged path for parity event visibility.
        setBotl('overexert_hp');
    } else {
        await You('pass out from exertion!');
        exercise(A_CON, false);
        await fall_asleep(-10, false);
    }
}
// C ref: hack.c:3031 overexertion() — combat increases metabolism
export async function overexertion() {
    await gethungry();
    if (((game.moves || 0) % 3) !== 0 && near_capacity() >= HVY_ENCUMBER) {
        await overexert_hp();
    }
    // C: return (boolean) (gm.multi < 0)
    return (game.multi || 0) < 0;
}
// ── mhitm_mgc_atk_negated ──
// C ref: uhitm.c:75
export async function mhitm_mgc_atk_negated(magr, mdef, verbosely) {
    // mcan doesn't apply to youmonst; hero can't be cancelled
    if (magr !== game.youmonst && magr.mcan)
        return true;
    const armpro = magic_negation(mdef);
    const negated = !(rn2(10) >= 3 * armpro);
    if (negated) {
        if (verbosely) {
            if (mdef === game.youmonst)
                await You("avoid harm.");
            else if (game.vis && canseemon(mdef))
                await pline(`${Monnam(mdef)} avoids harm.`);
        }
        return true;
    }
    return false;
}
// ── dynamic_multi_reason ──
// C ref: uhitm.c:104
export function dynamic_multi_reason(mon, verb, by_gaze) {
    const who = mon_nam(mon);
    const reason = by_gaze
        ? `${verb} by ${s_suffix(who)} gaze`
        : `${verb} by ${who}`;
    game.multi_reason = reason;
}
// ── erode_armor ──
// C ref: uhitm.c:126
export async function erode_armor(mdef, hurt) {
    while (1) {
        switch (rn2(5)) {
        case 0: {
            const target = which_armor(mdef, W_ARMH);
            if (!target
                || await erode_obj(target, xname(target), hurt, EF_GREASE)
                       === ER_NOTHING)
                continue;
            break;
        }
        case 1: {
            let target = which_armor(mdef, W_ARMC);
            if (target) {
                await erode_obj(target, xname(target), hurt,
                          EF_GREASE | EF_VERBOSE);
                break;
            }
            target = which_armor(mdef, W_ARM);
            if (target) {
                await erode_obj(target, xname(target), hurt,
                          EF_GREASE | EF_VERBOSE);
            } else {
                target = which_armor(mdef, W_ARMU);
                if (target) {
                    await erode_obj(target, xname(target), hurt,
                              EF_GREASE | EF_VERBOSE);
                }
            }
            break;
        }
        case 2: {
            const target = which_armor(mdef, W_ARMS);
            if (!target
                || await erode_obj(target, xname(target), hurt, EF_GREASE)
                       === ER_NOTHING)
                continue;
            break;
        }
        case 3: {
            const target = which_armor(mdef, W_ARMG);
            if (!target
                || await erode_obj(target, xname(target), hurt, EF_GREASE)
                       === ER_NOTHING)
                continue;
            break;
        }
        case 4: {
            const target = which_armor(mdef, W_ARMF);
            if (!target
                || await erode_obj(target, xname(target), hurt, EF_GREASE)
                       === ER_NOTHING)
                continue;
            break;
        }
        }
        break; // Out of while loop
    }
}
// ── attack_checks ──
// C ref: uhitm.c:189 — FALSE means it's OK to attack
export async function attack_checks(mtmp, wep) {
    mtmp.mstrategy = (mtmp.mstrategy || 0) & ~STRAT_WAITMASK;
    if (game.u.uswallow && game.u.ustuck === mtmp)
        return false;
    if (game.context.forcefight)
        return false;
    // C ref: uhitm.c:220-252 — unseen monster warning and early return.
    const tx = game.bhitpos?.x ?? (game.u.ux + game.u.dx);
    const ty = game.bhitpos?.y ?? (game.u.uy + game.u.dy);
    const glyph = glyph_at(tx, ty);
    if (!canspotmon(mtmp)
        && !glyph_is_warning(glyph)
        && !glyph_is_invisible(glyph)
        && !(!Blind() && mtmp.mundetected && hides_under(mtmp.data))) {
        await pline("Wait!  There's something there you can't see!");
        map_invisible(tx, ty);
        wakeup(mtmp, true);
        return true;
    }
    // peaceful confirmation
    if (game.flags?.confirm && mtmp.mpeaceful) {
        if (is_art(wep, ART_STORMBRINGER)) {
            game.override_confirmation = true;
            return false;
        }
        if (canspotmon(mtmp)) {
            // In C this is a paranoid_query; for now auto-confirm
        }
    }
    return false;
}
// ── force_attack ──
// C ref: uhitm.c:432
export async function force_attack(mtmp, pets_too) {
    const save_Forcefight = game.context.forcefight;
    if (pets_too || !mtmp.mtame)
        game.context.forcefight = true;
    const attacked = await do_attack(mtmp);
    game.context.forcefight = save_Forcefight;
    return attacked;
}
// ── find_roll_to_hit ──
// C ref: uhitm.c:365
export async function find_roll_to_hit(mtmp, aatyp, weapon, attk_count) {
    const g = game;
    const u = g.u;
    let role_roll_penalty = 0;
    const luck = getLuck();
    let tmp = 1 + abon() + find_mac(mtmp) + (u.uhitinc || 0)
          + (sgn(luck) * Math.trunc((Math.abs(luck) + 2) / 3))
          + maybe_polyd(g.player?.data?.mlevel || 0, u.ulevel || 0);
    if (!attk_count.value++) {
        await check_caitiff(mtmp);
    }
    if (mtmp.mstun) tmp += 2;
    if (mtmp.mflee) tmp += 2;
    if (mtmp.msleeping) tmp += 2;
    if (!mtmp.mcanmove) tmp += 4;
    if (Role_if(PM_MONK) && !Upolyd()) {
        if (uarm_get()) {
            role_roll_penalty = g.urole?.spelarmr || 0;
            tmp -= role_roll_penalty;
        } else if (!uwep_get() && !uarms_get()) {
            tmp += Math.trunc(u.ulevel / 3) + 2;
        }
    }
    if (is_orc(mtmp.data)
        && maybe_polyd(is_elf(g.player?.data), Race_if(PM_ELF)))
        tmp++;
    const cap = near_capacity();
    if (cap !== 0) tmp -= (cap * 2) - 1;
    if (u.utrap) tmp -= 3;
    if (aatyp === AT_WEAP || aatyp === AT_CLAW) {
        if (weapon) tmp += hitval(weapon, mtmp);
        tmp += weapon_hit_bonus(weapon);
    } else if (aatyp === AT_KICK && martial_bonus()) {
        tmp += weapon_hit_bonus(null);
    }
    return { tohit: tmp, armorpenalty: role_roll_penalty };
}
// C ref: uhitm.c:351
export function mon_maybe_unparalyze(mtmp) {
    if (!mtmp.mcanmove) {
        if (!rn2(10)) {
            mtmp.mcanmove = 1;
            mtmp.mfrozen = 0;
        }
    }
}
// ── missum ──
// C ref: uhitm.c:5199
export async function missum(mdef, _mattk, wouldhavehit) {
    if (wouldhavehit)
        await pline('Your armor is rather cumbersome...');
    if (canspotmon(mdef) && game.flags?.verbose)
        await You(`miss ${mon_nam(mdef)}.`);
    else
        await You('miss it.');
    if (!helpless(mdef))
        wakeup(mdef, true);
}
// ── shade_aware ──
// C ref: uhitm.c:1992
function shade_aware(obj) {
    if (!obj) return false;
    if (obj.otyp === BOULDER
        || obj.otyp === HEAVY_IRON_BALL
        || obj.otyp === IRON_CHAIN
        || obj.otyp === MIRROR
        || obj.otyp === CLOVE_OF_GARLIC
        || (game.objects[obj.otyp]?.oc_material || 0) === SILVER)
        return true;
    return false;
}
// ── shade_miss ──
// C ref: uhitm.c:2016
export async function shade_miss(magr, mdef, obj, thrown, verbose) {
    const youagr = (magr === game.youmonst);
    const youdef = (mdef === game.youmonst);
    if (mdef.data !== mons[PM_SHADE] || (obj && dmgval(obj, mdef)))
        return false;
    if (verbose
        && ((youdef || cansee(mdef.mx, mdef.my))
            || (magr === game.youmonst && m_next2u(mdef)))) {
        const what = (!obj || shade_aware(obj)) ? "attack" : cxname(obj);
        const target = youdef ? "you" : mon_nam(mdef);
        if (!thrown) {
            const whose = youagr ? "Your" : s_suffix(Monnam(magr));
            await pline(`${whose} ${what} ${vtense(what, "pass")} harmlessly through ${target}.`);
        } else {
            await pline(`The ${what} ${vtense(what, "pass")} harmlessly through ${target}.`);
        }
    }
    if (!youdef)
        mdef.msleeping = 0;
    return true;
}
// ── mhurtle_to_doom ──
// C ref: uhitm.c:1942
async function mhurtle_to_doom(mon, tmp, mdatRef) {
    if (tmp < mon.mhp) {
        await mhurtle(mon, game.u.dx, game.u.dy, 1);
        mdatRef.val = mon.data;
        if (DEADMONSTER(mon))
            return true;
    }
    return false;
}
// ── first_weapon_hit ──
// C ref: uhitm.c:1963
function first_weapon_hit(weapon) {
    // livelog message for breaking never-hit-with-wielded-weapon conduct
    livelog_printf(0, "hit with a wielded weapon for the first time");
}
// ── m_slips_free ──
// C ref: uhitm.c:2056
async function m_slips_free(mdef, mattk) {
    let obj;
    if (mattk.adtyp === AD_DRIN) {
        obj = which_armor(mdef, W_ARMH);
    } else {
        obj = which_armor(mdef, W_ARMC);
        if (!obj) obj = which_armor(mdef, W_ARM);
        if (!obj) obj = which_armor(mdef, W_ARMU);
    }
    if (obj && (obj.greased || obj.otyp === OILSKIN_CLOAK)
        && (!obj.cursed || rn2(3))) {
        await You(`grab, but cannot hold onto ${s_suffix(mon_nam(mdef))} ${obj.greased ? "greased" : "slippery"} ${xname(obj)}!`);
        if (obj.greased && !rn2(2)) {
            await pline_The("grease wears off.");
            obj.greased = 0;
        }
        return true;
    }
    return false;
}
// ── nohandglow ──
// C ref: uhitm.c:6315
async function nohandglow(mon) {
    if (!game.u.umconf || mon.mconf)
        return;
    if (game.u.umconf === 1) {
        await Your("hands stop glowing.");
    } else {
        await Your("hands no longer glow so brightly.");
    }
    game.u.umconf--;
}
// ── m_is_steadfast ──
// C ref: uhitm.c:5218
export function m_is_steadfast(mtmp) {
    const is_u = (mtmp === game.youmonst);
    const otmp = is_u ? uwep_get() : MON_WEP(mtmp);
    // must be on the ground (or in water), and not in air/water bubble level
    if ((is_u ? (Flying() || Levitation()) : (is_flyer(mtmp.data) || is_floater(mtmp.data)))
        || Is_airlevel(game.u?.uz)
        || (Is_waterlevel(game.u?.uz) && !is_pool(game.u?.ux ?? 0, game.u?.uy ?? 0)))
        return false;
    if (is_art(otmp, ART_GIANTSLAYER))
        return true;
    // steadfast if carrying loadstone
    if (m_carrying(mtmp, LOADSTONE))
        return true;
    // mounted hero's loadstone can steady steed against knockback
    if (game.u?.usteed && mtmp === game.u.usteed && carrying(LOADSTONE))
        return true;
    return false;
}
// ── mhitm_knockback ── (full version)
// C ref: uhitm.c:5247
export async function mhitm_knockback(magr, mdef, mattk, hitflagsRef, weapon_used) {
    const getHitflags = () => {
        if (!hitflagsRef || typeof hitflagsRef !== 'object') return 0;
        if (Number.isInteger(hitflagsRef.value)) return hitflagsRef.value;
        if (Number.isInteger(hitflagsRef.hitflags)) return hitflagsRef.hitflags;
        return 0;
    };
    const setHitflags = (flags) => {
        if (!hitflagsRef || typeof hitflagsRef !== 'object') return;
        if ('value' in hitflagsRef) hitflagsRef.value = flags;
        if ('hitflags' in hitflagsRef) hitflagsRef.hitflags = flags;
    };
    const knockdistance = rn2(3) ? 1 : 2;
    let chance = 6;
    const u_agr = (magr === game.youmonst);
    const u_def = (mdef === game.youmonst);
    let was_u = false;
    let dismount = false;
    let otmp;
    const wep = weapon_used ? (u_agr ? uwep_get() : MON_WEP(magr)) : null;
    if (wep && is_art(wep, ART_OGRESMASHER))
        chance = 2;
    if (rn2(chance))
        return false;
    // only certain attacks qualify
    if (!((mattk.adtyp === AD_PHYS)
          && (mattk.aatyp === AT_CLAW || mattk.aatyp === AT_KICK
              || mattk.aatyp === AT_BUTT || mattk.aatyp === AT_WEAP)))
        return false;
    // don't knockback if attacker wants to grab/engulf
    if (attacktype(magr.data, AT_ENGL)
        || attacktype(magr.data, AT_HUGS)
        || sticks(magr.data))
        return false;
    const defx = u_def ? game.u.ux : mdef.mx;
    const defy = u_def ? game.u.uy : mdef.my;
    const dx = sgn(defx - (u_agr ? game.u.ux : magr.mx));
    const dy = sgn(defy - (u_agr ? game.u.uy : magr.my));
    // basic movement check
    if (u_def) {
        if (!await test_move(defx, defy, dx, dy, TEST_MOVE))
            return false;
    } else {
        if (!isok(defx + dx, defy + dy))
            return false;
        const defLoc = game.level?.at(defx, defy);
        if (IS_DOOR(defLoc?.typ ?? 0)
            && (defx - magr.mx && defy - magr.my)
            && !doorless_door(defx, defy))
            return false;
    }
    if (u_def && game.u?.usteed) {
        otmp = which_armor(game.u.usteed, W_SADDLE);
        if (otmp && otmp.cursed) {
            mdef = game.u.usteed;
            was_u = true;
        } else {
            dismount = true;
        }
    }
    // monsters must be alive
    if ((!u_agr && DEADMONSTER(magr))
        || (!u_def && DEADMONSTER(mdef)))
        return false;
    // attacker must be much larger than defender
    if (!(magr.data.msize > (mdef.data.msize + 1)))
        return false;
    // no knockback with flimsy or non-blunt weapon
    if (wep && (is_flimsy(wep) || !is_blunt_weapon(wep)))
        return false;
    // needs solid physical hit
    if (unsolid(magr.data))
        return false;
    // attack must have hit
    if ((u_agr || u_def) && !(getHitflags() & M_ATTK_HIT))
        return false;
    // steadfast check
    if (m_is_steadfast(mdef)) {
        if (u_def || (game.u?.usteed && mdef === game.u.usteed)) {
            let mdefbuf = '';
            if (game.u?.usteed)
                mdefbuf = `and ${y_monnam(game.u.usteed)} `;
            await You(`${mdefbuf}don't budge.`);
        } else if (canseemon(mdef)) {
            await pline(`${Monnam(mdef)} doesn't budge.`);
        }
        return false;
    }
    // message
    const knockedhow = dismount ? 'out of your saddle'
        : (will_hurtle(mdef, defx + dx, defy + dy) ? 'backward' : 'back');
    if (u_def || canseemon(mdef)) {
        const magrbuf = u_agr ? 'You' : Monnam(magr);
        let mdefbuf = (u_def || was_u) ? 'you' : y_monnam(mdef);
        if (was_u)
            mdefbuf += ` and ${y_monnam(game.u.usteed)}`;
        const adj = rn2(2) ? "forceful" : "powerful";
        const noun = rn2(2) ? "blow" : "strike";
        await pline(`${magrbuf} ${vtense(magrbuf, 'knock')} ${mdefbuf} ${knockedhow} with a ${adj} ${noun}!`);
    } else if (u_agr) {
        await You_feel(`${some_mon_nam(mdef)} be knocked ${knockedhow}!`);
    }
    if (game.u?.ustuck && (u_def || u_agr))
        unstuck(game.u.ustuck);
    // do the actual knockback
    if (u_def) {
        if (dismount) {
            game.u.dx = dx;
            game.u.dy = dy;
            await dismount_steed(DISMOUNT_KNOCKED);
        } else {
            await hurtle(dx, dy, knockdistance, false);
            setHitflags(getHitflags() | M_ATTK_HIT);
        }
        set_apparxy(magr);
        if (!Stunned() && !rn2(4))
            await make_stunned(knockdistance + 1, true);
    } else {
        await mhurtle(mdef, dx, dy, knockdistance);
        if (!u_agr)
            setHitflags(getHitflags() | M_ATTK_HIT);
        if (DEADMONSTER(mdef)) {
            if (!was_u)
                setHitflags(getHitflags() | M_ATTK_DEF_DIED);
        } else if (!rn2(4)) {
            mdef.mstun = 1;
            if (mdef === game.u?.usteed)
                set_apparxy(magr);
        }
    }
    if (!u_agr) {
        if (DEADMONSTER(magr))
            setHitflags(getHitflags() | M_ATTK_AGR_DIED);
    }
    return true;
}
// ── hmon_hitmon sub-functions ──
// C ref: uhitm.c:839
function hmon_hitmon_barehands(hmd, mon) {
    if (hmd.mdat === mons[PM_SHADE]) {
        hmd.dmg = 0;
    } else {
        hmd.dmg = rnd(!martial_bonus() ? 2 : 4);
        hmd.use_weapon_skill = true;
        hmd.train_weapon_skill = (hmd.dmg > 1);
    }
    // Blessed gloves/silver rings
    const spcdmgflg = uarmg_get() ? W_ARMG
          : (((hmd.twohits === 0 || hmd.twohits === 1) ? W_RINGR : 0)
             | ((hmd.twohits === 0 || hmd.twohits === 2) ? W_RINGL : 0));
    hmd.dmg += special_dmgval(game.youmonst, mon, spcdmgflg, { value: 0 });
    hmd.barehand_silver_rings = 0;
    // Note: special_dmgval stub returns 0, so silverhit tracking is deferred
}
// C ref: uhitm.c:886
async function hmon_hitmon_weapon_ranged(hmd, mon, obj) {
    if (hmd.mdat === mons[PM_SHADE] && !shade_glare(obj))
        hmd.dmg = 0;
    else
        hmd.dmg = rnd(2);
    if (hmd.material === SILVER && mon_hates_silver(mon)) {
        hmd.silvermsg = hmd.silverobj = true;
        hmd.dmg += rnd(hmd.dmg ? 20 : 10);
    }
    if (!hmd.thrown && obj === uwep_get() && obj.otyp === BOOMERANG
        && rnl(4) === 4 - 1) {
        const more_than_1 = ((obj.quan || 1) > 1);
        // pline message about boomerang breaking
        if (!more_than_1)
            await uwepgone();
        useup(obj);
        hmd.hittxt = true;
        if (hmd.mdat !== mons[PM_SHADE])
            hmd.dmg++;
    }
}
// C ref: uhitm.c:921
export function backstabbable(mon) {
    return !amorphous(mon.data)
        && !is_whirly(mon.data)
        && !noncorporeal(mon.data)
        && mon.data.mlet !== S_BLOB
        && mon.data.mlet !== S_EYE
        && mon.data.mlet !== S_FUNGUS
        && canseemon(mon)
        && (mon.mflee || helpless(mon));
}
// C ref: uhitm.c:935
async function hmon_hitmon_weapon_melee(hmd, mon, obj) {
    hmd.use_weapon_skill = true;
    hmd.dmg = dmgval(obj, mon);
    hmd.train_weapon_skill = (hmd.dmg > 1);
    // Healer with anatomy knowledge
    if (Role_if(PM_HEALER) && hmd.hand_to_hand
        && obj.oclass === WEAPON_CLASS
        && (game.objects[obj.otyp]?.oc_skill || 0) === P_KNIFE) {
        const died_count = (game.mvitals?.[monsndx(mon.data)]?.died || 0);
        hmd.dmg += Math.min(3, Math.trunc(died_count / 6));
    }
    // special attack actions
    if (!hmd.train_weapon_skill || mon === game.u.ustuck || game.u.twoweap
        || (hmd.hand_to_hand && is_art(obj, ART_CLEAVER))) {
        ; // no special bonuses
    } else if (Role_if(PM_ROGUE) && backstabbable(mon) && !Upolyd()
               && hmd.hand_to_hand) {
        // You("strike %s from behind!", mon_nam(mon));
        hmd.dmg += rnd(game.u.ulevel || 1);
        hmd.hittxt = true;
    } else if (hmd.dieroll === 2 && obj === uwep_get()
               && obj.oclass === WEAPON_CLASS
               && (bimanual(obj)
                   || (Role_if(PM_SAMURAI) && obj.otyp === KATANA
                       && !uarms_get()))
               && ((uwep_skill_type()) !== P_NONE
                   && P_SKILL(uwep_skill_type()) >= P_SKILLED)) {
        // weapon shattering of monster's weapon — simplified
        // requires MON_WEP, setmnotwielded, etc.
        const monwep = MON_WEP(mon);
        if (monwep && !is_flimsy(monwep)
            && !obj_resists(monwep,
                            50 + 15 * (greatest_erosion(obj) - greatest_erosion(monwep)),
                            100)) {
            setmnotwielded(mon, monwep);
            await m_useupall(mon, monwep);
            if (rn2(4)) {
                await monflee(mon, c_d(2, 3), true, true);
            }
            hmd.hittxt = true;
        }
    }
    if (obj.oartifact) {
        const dmgRef = { value: hmd.dmg };
        const artKilled = artifact_hit_impl(game.youmonst, mon, obj, dmgRef, hmd.dieroll);
        hmd.dmg = dmgRef.value;
        if (artKilled) {
            if (DEADMONSTER(mon)) {
                hmd.doreturn = true;
                hmd.retval = false;
                return;
            }
            if (hmd.dmg === 0) {
                hmd.doreturn = true;
                hmd.retval = true;
                return;
            }
            hmd.hittxt = true;
        }
    }
    if (hmd.material === SILVER && mon_hates_silver(mon)) {
        hmd.silvermsg = hmd.silverobj = true;
    }
    if (artifact_light(obj) && obj.lamplit && mon_hates_light(mon))
        hmd.lightobj = true;
    if (game.u.usteed && !hmd.thrown && hmd.dmg > 0
        && weapon_type(obj) === P_LANCE && mon !== game.u.ustuck) {
        hmd.jousting = joust(mon, obj);
        if (hmd.jousting)
            hmd.train_weapon_skill = true;
    }
    if (hmd.thrown === HMON_THROWN
        && (is_ammo(obj) || is_missile(obj))) {
        if (ammo_and_launcher(obj, uwep_get())) {
            if (Role_if(PM_SAMURAI) && obj.otyp === YA
                && uwep_get().otyp === YUMI)
                hmd.dmg++;
            else if (Race_if(PM_ELF) && obj.otyp === ELVEN_ARROW
                     && uwep_get().otyp === ELVEN_BOW)
                hmd.dmg++;
            hmd.train_weapon_skill = (hmd.dmg > 0);
        }
        if (obj.opoisoned && is_poisonable(obj))
            hmd.ispoisoned = true;
    }
    if (permapoisoned(obj) && hmd.dieroll <= 5)
        hmd.ispoisoned = true;
}
// C ref: uhitm.c:1071
async function hmon_hitmon_weapon(hmd, mon, obj) {
    if (is_launcher(obj)
        || (!hmd.thrown && (is_missile(obj) || is_ammo(obj)))
        || (!hmd.thrown && !game.u.usteed && is_pole(obj)
            && !is_art(obj, ART_SNICKERSNEE))
        || (is_ammo(obj) && (hmd.thrown !== HMON_THROWN
                             || !ammo_and_launcher(obj, uwep_get())))) {
        await hmon_hitmon_weapon_ranged(hmd, mon, obj);
    } else {
        await hmon_hitmon_weapon_melee(hmd, mon, obj);
        if (hmd.doreturn) return;
    }
}
// C ref: uhitm.c:1095
async function hmon_hitmon_potion(hmd, mon, obj) {
    if ((obj.quan || 1) > 1) {
        const newobj = splitobj(obj, 1);
        // continue with obj being the single potion
    } else {
        await setuwep(null);
    }
    freeinv(obj);
    await potionhit(mon, obj,
              hmd.hand_to_hand ? 1 /* POTHIT_HERO_BASH */ : 2 /* POTHIT_HERO_THROW */);
    if (DEADMONSTER(mon)) {
        hmd.doreturn = true;
        hmd.retval = false;
        return;
    }
    hmd.hittxt = true;
    hmd.mdat = mon.data;
    hmd.dmg = (hmd.mdat === mons[PM_SHADE]) ? 0 : 1;
}
// C ref: uhitm.c:1119
async function hmon_hitmon_misc_obj(hmd, mon, obj) {
    switch (obj.otyp) {
    case BOULDER:
    case HEAVY_IRON_BALL:
    case IRON_CHAIN:
        hmd.dmg = dmgval(obj, mon);
        break;
    case MIRROR:
        if (breaktest(obj)) {
            await You(`break ${yname(obj)}.  That's bad luck!`);
            change_luck(-2);
            useup(obj);
            hmd.unarmed = false;
            hmd.get_dmg_bonus = false;
            hmd.hittxt = true;
        }
        hmd.dmg = 1;
        break;
    case EXPENSIVE_CAMERA:
        await You(`succeed in destroying ${yname(obj)}.  Congratulations!`);
        release_camera_demon(obj, game.u.ux, game.u.uy);
        useup(obj);
        hmd.doreturn = true;
        hmd.retval = true;
        return;
    case CORPSE:
        if (touch_petrifies(mons[obj.corpsenm])) {
            hmd.dmg = 1;
            hmd.hittxt = true;
            await You(`hit ${mon_nam(mon)} with a corpse.`);
            if (resists_ston(mon)) break;
            hmd.doreturn = true;
            hmd.retval = !DEADMONSTER(mon);
            return;
        }
        hmd.dmg = ((obj.corpsenm >= 0 && obj.corpsenm < mons.length)
                    ? (mons[obj.corpsenm]?.msize || 0)
                    : 0) + 1;
        break;
    case EGG: {
        const cnt = obj.quan || 1;
        hmd.dmg = 1;
        hmd.get_dmg_bonus = false;
        hmd.hittxt = true;
        if (obj === uwep_get())
            game.unweapon = true;
        if (obj.spe && obj.corpsenm >= 0) {
            if (cnt < 5) change_luck(-cnt);
            else change_luck(-5);
        }
        if (obj.corpsenm >= 0 && touch_petrifies(mons[obj.corpsenm])) {
            await pline(`Splat!  You hit ${mon_nam(mon)} with a petrifying egg!`);
            if (resists_ston(mon)) break;
            hmd.doreturn = true;
            hmd.retval = !DEADMONSTER(mon);
            return;
        } else {
            await You(`hit ${mon_nam(mon)} with an egg.`);
            await pline("Splat!");
            if (hmd.thrown)
                obfree(obj, null);
            else
                useupall(obj);
            exercise(A_WIS, false);
        }
        break;
    }
    case CLOVE_OF_GARLIC:
        if (is_undead(hmd.mdat) || is_vampshifter(mon)) {
            await monflee(mon, c_d(2, 4), false, true);
        }
        hmd.dmg = 1;
        break;
    case CREAM_PIE:
    case BLINDING_VENOM:
        mon.msleeping = 0;
        await pline(obj.otyp === CREAM_PIE ? "Splat!" : "Splash!");
        {
            const more_than_1 = ((obj.quan || 1) > 1);
            if (hmd.thrown)
                obfree(obj, null);
            else
                useup(obj);
        }
        hmd.hittxt = true;
        hmd.get_dmg_bonus = false;
        hmd.dmg = 0;
        break;
    case ACID_VENOM:
        if (resists_acid(mon)) {
            await Your(`venom hits ${mon_nam(mon)} harmlessly.`);
            hmd.dmg = 0;
        } else {
            await Your(`venom burns ${mon_nam(mon)}!`);
            hmd.dmg = dmgval(obj, mon);
        }
        {
            const more_than_1 = ((obj.quan || 1) > 1);
            if (hmd.thrown)
                obfree(obj, null);
            else
                useup(obj);
        }
        hmd.hittxt = true;
        hmd.get_dmg_bonus = false;
        break;
    default: {
        const mat = game.objects[obj.otyp]?.oc_material || 0;
        if ((mat === VEGGY || mat === PAPER)
            && obj.oclass !== SPBOOK_CLASS) {
            hmd.dmg = 0;
            hmd.get_dmg_bonus = false;
            break;
        }
        hmd.dmg = Math.trunc((obj.owt + 99) / 100);
        hmd.dmg = (hmd.dmg <= 1) ? 1 : rnd(hmd.dmg);
        if (hmd.dmg > 6)
            hmd.dmg = 6;
        if (is_wet_towel(obj)) {
            const doubld = (mon.data === mons[PM_IRON_GOLEM]);
            hmd.dmg += (obj.spe || 0) * (doubld ? 2 : 1);
            hmd.dmg = rnd(hmd.dmg);
            hmd.dryit = (rn2((obj.spe || 0) + 1) > 0);
        }
        if (hmd.material === SILVER && mon_hates_silver(mon)) {
            hmd.dmg += rnd(20);
            hmd.silvermsg = hmd.silverobj = true;
        }
        break;
    }
    }
}
// C ref: uhitm.c:1388
async function hmon_hitmon_do_hit(hmd, mon, obj) {
    if (!obj) {
        hmon_hitmon_barehands(hmd, mon);
    } else {
        // stone missile check
        if ((hmd.thrown === HMON_THROWN || hmd.thrown === HMON_KICKED)
            && stone_missile(obj) && passes_rocks(hmd.mdat)) {
            // hit but no harm
            wakeup(mon, true);
            hmd.doreturn = true;
            hmd.retval = true;
            return;
        }
        // save obj name
        hmd.saved_oname = cxname(obj);
        if (obj.oclass === WEAPON_CLASS || is_weptool(obj)
            || obj.oclass === GEM_CLASS) {
            await hmon_hitmon_weapon(hmd, mon, obj);
            if (hmd.doreturn) return;
        } else if (obj.oclass === POTION_CLASS) {
            await hmon_hitmon_potion(hmd, mon, obj);
            if (hmd.doreturn) return;
        } else {
            if (hmd.mdat === mons[PM_SHADE] && !shade_aware(obj)) {
                hmd.dmg = 0;
            } else {
                await hmon_hitmon_misc_obj(hmd, mon, obj);
                if (hmd.doreturn) return;
            }
        }
    }
}
// C ref: uhitm.c:1437
function hmon_hitmon_dmg_recalc(hmd, obj) {
    let dmgbonus = 0;
    if (hmd.get_dmg_bonus) {
        dmgbonus = game.u.udaminc || 0;
        if (hmd.thrown !== HMON_THROWN || !obj || !uwep_get()) {
            let strbonus = dbon();
            const absbonus = Math.abs(strbonus);
            if (hmd.twohits)
                strbonus = Math.trunc((3 * absbonus + 2) / 4) * sgn(strbonus);
            else if (hmd.thrown === HMON_MELEE && uwep_get() && bimanual(uwep_get()))
                strbonus = Math.trunc((3 * absbonus + 1) / 2) * sgn(strbonus);
            dmgbonus += strbonus;
        }
    }
    if (hmd.use_weapon_skill) {
        dmgbonus += weapon_dam_bonus(obj);
        if (hmd.train_weapon_skill) {
            const wtype = hmd.thrown ? weapon_type(obj) : uwep_skill_type();
            use_skill(wtype, 1);
        }
    }
    hmd.dmg += dmgbonus;
    if (hmd.dmg < 1) hmd.dmg = 1;
}
// C ref: uhitm.c:1511
export function hmon_hitmon_poison(hmd, mon, obj) {
    let nopoison = 10 - Math.trunc((obj.owt || 0) / 10);
    if (nopoison < 2) nopoison = 2;
    if (Role_if(PM_SAMURAI)) {
        // You("dishonorably use a poisoned weapon!");
        adjalign(-sgn(game.u.ualign.type));
    } else if (game.u.ualign.type === A_LAWFUL && game.u.ualign.record > -10) {
        // You_feel("like an evil coward for using a poisoned weapon.");
        adjalign(-1);
    }
    if (!permapoisoned(obj) && !rn2(nopoison)) {
        obj.opoisoned = false;
        hmd.unpoisonmsg = true;
    }
    if (resists_poison(mon))
        hmd.needpoismsg = true;
    else if (rn2(10))
        hmd.dmg += rnd(6);
    else
        hmd.poiskilled = true;
}
// C ref: uhitm.c:1542
export async function hmon_hitmon_jousting(hmd, mon, obj) {
    hmd.dmg += c_d(2, (obj === uwep_get()) ? 10 : 2);
    await You(`joust ${mon_nam(mon)}${canseemon(mon) ? exclam(hmd.dmg) : "."}`);
    if ((game.u.uconduct?.weaphit ?? 0) <= 1)
        first_weapon_hit(obj);
    if (hmd.jousting < 0) {
        await pline(`${Yname2(obj)} shatters on impact!`);
        set_twoweap(false);
        if (obj === uwep_get())
            await uwepgone();
        useup(obj);
        obj = null;
    }
    if (await mhurtle_to_doom(mon, hmd.dmg, { val: hmd.mdat }))
        hmd.already_killed = true;
    hmd.hittxt = true;
}
// C ref: uhitm.c:1571
async function hmon_hitmon_stagger(hmd, mon) {
    if (rnd(100) < P_SKILL(P_BARE_HANDED_COMBAT) && !bigmonst(hmd.mdat)
        && !thick_skinned(hmd.mdat)) {
        if (hmd.dmg < mon.mhp) {
            await mhurtle(mon, game.u.dx, game.u.dy, 1);
            hmd.mdat = mon.data;
            if (DEADMONSTER(mon)) hmd.already_killed = true;
        }
        hmd.hittxt = true;
    }
}
// C ref: uhitm.c:1589
async function hmon_hitmon_pet(hmd, mon) {
    if (mon.mtame && hmd.dmg > 0) {
        if (mon.mtame > 0) mon.mtame--;
        if (mon.mtame && !hmd.destroyed)
            await monflee(mon, 10 * rnd(hmd.dmg), false, false);
    }
}
// C ref: uhitm.c:1605
export async function hmon_hitmon_splitmon(hmd, mon, obj) {
    if ((hmd.mdat === mons[PM_BLACK_PUDDING]
         || hmd.mdat === mons[PM_BROWN_PUDDING])
        && mon.mhp > 1 && !mon.mcan && !hmd.offmap
        && obj && (obj === uwep_get() || (game.u.twoweap && obj === uswapwep_get()))
        && ((hmd.material === IRON || hmd.material === METAL)
            && !(is_ammo(obj) || is_missile(obj)))
        && hmd.hand_to_hand) {
        const mclone = clone_mon(mon, 0, 0);
        if (mclone !== null) {
            let withwhat = '';
            if (game.u.twoweap && game.flags?.verbose)
                withwhat = ` with ${yname(obj)}`;
            await pline(`${Monnam(mon)} divides as you hit it${withwhat}!`);
            hmd.hittxt = true;
            await mintrap(mclone, NO_TRAP_FLAGS);
        }
    }
}
// C ref: uhitm.c:1638
async function hmon_hitmon_msg_hit(hmd, mon, obj) {
    if (!hmd.hittxt
        && (!hmd.destroyed
            || (hmd.thrown && (game.m_shot?.n || 0) > 1
                && game.m_shot?.o === obj?.otyp))) {
        if (hmd.thrown) {
            // hit(mshot_xname(obj), mon, exclam(hmd.dmg));
            await pline(`The ${obj ? xname(obj) : "weapon"} hits ${mon_nam(mon)}${exclam(hmd.dmg)}`);
        } else if (!game.flags?.verbose)
            await You('hit it.');
        else {
            const verb = (obj && is_shield(obj)) ? "bash"
                : (obj && (game.objects[obj.otyp]?.oc_skill === P_WHIP
                           || is_wet_towel(obj))) ? "lash"
                  : Role_if(PM_BARBARIAN) ? "smite"
                    : "hit";
            await You(`${verb} ${mon_nam(mon)}${canseemon(mon) ? exclam(hmd.dmg) : '.'}`);
        }
    }
}
// C ref: uhitm.c:1663
async function hmon_hitmon_msg_silver(hmd, mon) {
    if (canspotmon(mon)) {
        let fmt;
        const whom = mon_nam(mon);
        if (hmd.barehand_silver_rings === 1)
            fmt = `Your silver ring sears ${whom}!`;
        else if (hmd.barehand_silver_rings === 2)
            fmt = `Your silver rings sear ${whom}!`;
        else if (hmd.silverobj && hmd.saved_oname) {
            const sn = hmd.saved_oname;
            const hassilver = sn.includes("silver");
            fmt = `Your ${hassilver ? "" : "silver "}${sn} ${vtense(sn, "sear")} ${whom}!`;
        } else
            fmt = `The silver sears ${whom}!`;
        await pline(fmt);
    } else {
        await pline(`${highc(mon_nam(mon))} is seared!`);
    }
}
// C ref: uhitm.c:1702
async function hmon_hitmon_msg_lightobj(hmd, mon) {
    if (canspotmon(mon)) {
        if (hmd.saved_oname)
            await pline(`${s_suffix(hmd.saved_oname)} radiance penetrates deep into ${mon_nam(mon)}!`);
        else
            await pline(`The light sears ${mon_nam(mon)}!`);
    } else {
        await pline(`${highc(mon_nam(mon))} is seared!`);
    }
}
// ── hmon_hitmon: guts of hmon ──
// C ref: uhitm.c:1755 — returns true if mon survives
async function hmon_hitmon(mon, obj, thrown, dieroll) {
    let maybe_knockback = false;
    const hmd = {
        dmg: 0, thrown, twohits: thrown ? 0 : (game.twohits || 0),
        dieroll, mdat: mon.data,
        use_weapon_skill: false, train_weapon_skill: false,
        barehand_silver_rings: 0, silvermsg: false, silverobj: false, lightobj: false,
        material: obj ? (game.objects[obj.otyp]?.oc_material || NO_MATERIAL) : NO_MATERIAL,
        jousting: 0, hittxt: false, get_dmg_bonus: true,
        unarmed: !uwep_get() && !uarm_get() && !uarms_get(),
        hand_to_hand: (thrown === HMON_MELEE
                       || (thrown === HMON_APPLIED && uwep_get() && is_pole(uwep_get()))),
        ispoisoned: false, unpoisonmsg: false, needpoismsg: false,
        poiskilled: false, already_killed: false, offmap: false,
        destroyed: false, dryit: false, doreturn: false, retval: false,
        saved_oname: '',
    };
    await hmon_hitmon_do_hit(hmd, mon, obj);
    if (hmd.doreturn) return hmd.retval;
    if (hmd.dmg > 0) hmon_hitmon_dmg_recalc(hmd, obj);
    if (hmd.ispoisoned)
        hmon_hitmon_poison(hmd, mon, obj);
    if (hmd.dmg < 1) {
        const mon_is_shade = (mon.data === mons[PM_SHADE]);
        hmd.dmg = (hmd.get_dmg_bonus && !mon_is_shade) ? 1 : 0;
        if (mon_is_shade && !hmd.hittxt
            && thrown !== HMON_THROWN && thrown !== HMON_KICKED) {
            const did_miss = await shade_miss(game.youmonst, mon, obj, false, true);
            if (did_miss) hmd.hittxt = true;
        }
    }
    if (hmd.jousting) {
        await hmon_hitmon_jousting(hmd, mon, obj);
    } else if (hmd.unarmed && hmd.dmg > 1 && !thrown && !obj && !Upolyd()) {
        await hmon_hitmon_stagger(hmd, mon, obj);
    } else if (!hmd.unarmed && hmd.dmg > 1 && !thrown && !Upolyd()
               && !game.u.twoweap && uwep_get()) {
        maybe_knockback = true;
    }
    if (!hmd.already_killed) {
        if (obj && (obj === uwep_get() || (obj === uswapwep_get() && game.u.twoweap))
            && (obj.oclass === WEAPON_CLASS || is_weptool(obj))
            && (thrown === HMON_MELEE || thrown === HMON_APPLIED)
            && !hmd.jousting
            && hmd.dmg > 0 && (game.u.uconduct?.weaphit || 0) <= 1)
            first_weapon_hit(obj);
        mon.mhp -= hmd.dmg;
    }
    if (mon.mhp > mon.mhpmax)
        mon.mhp = mon.mhpmax;
    if (mon.mx === 0)
        hmd.offmap = true;
    if (DEADMONSTER(mon))
        hmd.destroyed = true;
    await hmon_hitmon_pet(hmd, mon, obj);
    await hmon_hitmon_splitmon(hmd, mon, obj);
    await hmon_hitmon_msg_hit(hmd, mon, obj);
    if (hmd.silvermsg)
        await hmon_hitmon_msg_silver(hmd, mon, obj);
    if (hmd.lightobj)
        await hmon_hitmon_msg_lightobj(hmd, mon, obj);
    if (hmd.unpoisonmsg) {
        // saved_oname already set
    }
    if (hmd.needpoismsg)
        await pline_The(`poison doesn't seem to affect ${mon_nam(mon)}.`);
    if (hmd.poiskilled) {
        await pline_The("poison was deadly...");
        if (!hmd.already_killed)
            await xkilled(mon, XKILL_NOMSG);
        hmd.destroyed = true;
    } else if (hmd.destroyed) {
        if (!hmd.already_killed) {
            if (troll_baned(mon, obj))
                game.mkcorpstat_norevive = true;
            await killed(mon);
            game.mkcorpstat_norevive = false;
        }
    } else if (game.u.umconf && hmd.hand_to_hand) {
        await nohandglow(mon);
        if (!mon.mconf && !resist(mon, SPBOOK_CLASS, 0, NOTELL)) {
            mon.mconf = 1;
            if (!mon.mstun && !helpless(mon) && canseemon(mon))
                await pline(`${Monnam(mon)} appears confused.`);
        }
    }
    if (hmd.unpoisonmsg)
        await Your(`${hmd.saved_oname || "weapon"} ${vtense(hmd.saved_oname || "weapon", "are")} no longer poisoned.`);
    // C ref: uhitm.c:1924-1933 — mhitm_knockback guarded by maybe_knockback
    // (only for one-handed weapon melee hits with dmg > 1, not unarmed/thrown)
    if (maybe_knockback && !hmd.destroyed && !hmd.offmap) {
        const hitflags = { value: M_ATTK_HIT };
        wakeup(mon, true);
        const udata = game.player?.data || game.youmonst?.data;
        if (udata?.mattk) {
            const did_kb = await mhitm_knockback(game.youmonst, mon,
                udata.mattk[0] || { aatyp: AT_WEAP, adtyp: AD_PHYS },
                hitflags, true);
            if (did_kb && (hitflags.value & M_ATTK_DEF_DIED) !== 0)
                hmd.destroyed = true;
        }
    }
    return hmd.destroyed ? false : true;
}
// ── hmon ──
// C ref: uhitm.c:820
export async function hmon(mon, obj, thrown, dieroll) {
    const anger_guards = (mon.mpeaceful
                          && (mon.ispriest || mon.isshk || is_watch(mon.data)));
    const result = await hmon_hitmon(mon, obj, thrown, dieroll);
    if (mon.ispriest && !rn2(2)) await ghod_hitsu(mon);
    if (anger_guards) angry_guards(0);
    return result;
}
// ── known_hitum ──
// C ref: uhitm.c:588 — returns true if mon still lives
async function known_hitum(mon, weapon, mhit_ref, rollneeded, armorpenalty, uattk, dieroll) {
    let malive = true;
    if (game.override_confirmation) {
        // Stormbringer override — TODO message
    }
    if (!mhit_ref.value) {
        await missum(mon, uattk, (rollneeded + armorpenalty > dieroll));
    } else {
        const oldhp = mon.mhp;
        const oldweaphit = game.u.uconduct?.weaphit || 0;
        if (weapon && (weapon.oclass === WEAPON_CLASS || is_weptool(weapon))) {
            if (!game.u.uconduct) game.u.uconduct = {};
            game.u.uconduct.weaphit = (game.u.uconduct.weaphit || 0) + 1;
        }
        game.notonhead = (mon.mx !== game.bhitpos?.x || mon.my !== game.bhitpos?.y);
        malive = await hmon(mon, weapon, HMON_MELEE, dieroll);
        if (malive) {
            if (!rn2(25) && mon.mhp < Math.trunc(mon.mhpmax / 2)
                && !(game.u.uswallow && game.u.ustuck === mon)) {
                await monflee(mon, !rn2(3) ? rnd(100) : 0, false, true);
                if (game.u.ustuck === mon && !game.u.uswallow
                    && !(game.player?.data && sticks(game.player.data))) {
                    game.u.ustuck = null;
                }
            }
            if (mon.mhp === oldhp) {
                mhit_ref.value = 0;
                if (game.u.uconduct) game.u.uconduct.weaphit = oldweaphit;
            }
        }
    }
    return malive;
}
// C ref: uhitm.c:651 — await hitum_cleave()
async function hitum_cleave(target, uattk) {
    const u = game.u;
    const x = u.ux;
    const y = u.uy;
    let i = xytodir(u.dx, u.dy);
 
    if (i === DIR_ERR) {
        impossible(`hitum_cleave: unknown target direction [${u.dx},${u.dy},${u.dz}]?`);
        return true;
    }
 
    i = cleave_clockwise ? ((i + 6) % N_DIRS) : ((i + 2) % N_DIRS);
    const umort = u.umortality || 0;
    const savedBhit = {
        x: game.bhitpos?.x ?? null,
        y: game.bhitpos?.y ?? null,
        had: !!game.bhitpos
    };
    const savedNotonhead = !!game.notonhead;
 
    for (let count = 3; count > 0; --count) {
        i = cleave_clockwise ? ((i + 1) % N_DIRS) : ((i + N_DIRS - 1) % N_DIRS);
        const tx = x + xdir[i];
        const ty = y + ydir[i];
        if (!isok(tx, ty)) {
            continue;
        }
 
        const mtmp = m_at(tx, ty);
        if (!mtmp) {
            if (glyph_is_invisible(glyph_at(tx, ty))) {
                unmap_invisible(tx, ty);
            }
            continue;
        }
 
        const attk_count = { value: 0 };
        const { tohit: tmp, armorpenalty } = await find_roll_to_hit(
            mtmp, uattk.aatyp, uwep_get(), attk_count
        );
        mon_maybe_unparalyze(mtmp);
        const dieroll = rnd(20);
        const mhit_ref = { value: (tmp > dieroll) ? 1 : 0 };
 
        if (!game.bhitpos) {
            game.bhitpos = { x: tx, y: ty };
        } else {
            game.bhitpos.x = tx;
            game.bhitpos.y = ty;
        }
        game.notonhead = (mtmp.mx !== tx || mtmp.my !== ty);
 
        await known_hitum(mtmp, uwep_get(), mhit_ref, tmp, armorpenalty, uattk, dieroll);
        await passive(mtmp, uwep_get(), mhit_ref.value, !DEADMONSTER(mtmp), AT_WEAP, !uwep_get());
 
        if (!uwep_get() || (game.multi || 0) < 0 || (u.umortality || 0) > umort) {
            break;
        }
    }
 
    cleave_clockwise = !cleave_clockwise;
    if (savedBhit.had) {
        game.bhitpos = game.bhitpos || {};
        game.bhitpos.x = savedBhit.x;
        game.bhitpos.y = savedBhit.y;
    } else {
        game.bhitpos = null;
    }
    game.notonhead = savedNotonhead;
 
    return !(target && DEADMONSTER(target));
}
 
// C ref: uhitm.c:737
function double_punch() {
    const skl_lvl = P_SKILL(P_BARE_HANDED_COMBAT);
    if (!uwep_get() && !uarms_get() && skl_lvl > P_BASIC)
        return (skl_lvl - P_BASIC) > rn2(5);
    return false;
}
// ── hitum ──
// C ref: uhitm.c:759 — returns true if mon still lives
async function hitum(mon, uattk) {
    let malive;
    let wep_was_destroyed = false;
    const wepbefore = uwep_get();
    const secondwep = game.u.twoweap ? uswapwep_get() : null;
    const x = game.u.ux + game.u.dx;
    const y = game.u.uy + game.u.dy;
    const oldumort = game.u.umortality || 0;
    const attk_count = { value: 0 };
    if (u_wield_art(ART_CLEAVER)
        && !game.u.twoweap
        && !game.u.uswallow
        && !game.u.ustuck
        && !NODIAG(game.u.umonnum)) {
        return await hitum_cleave(mon, uattk);
    }
    game.twohits = (uwep_get() ? (game.u.twoweap ? 1 : 0) : (double_punch() ? 1 : 0));
    const { tohit: tmp, armorpenalty } = await find_roll_to_hit(mon, uattk.aatyp, uwep_get(), attk_count);
    mon_maybe_unparalyze(mon);
    const dieroll = rnd(20);
    let mhit = (tmp > dieroll || game.u.uswallow) ? 1 : 0;
    if (tmp > dieroll) exercise(A_DEX, true);
    const mhit_ref = { value: mhit };
    malive = await known_hitum(mon, uwep_get(), mhit_ref, tmp, armorpenalty, uattk, dieroll);
    mhit = mhit_ref.value;
    if (wepbefore && !uwep_get()) wep_was_destroyed = true;
    await passive(mon, uwep_get(), mhit, malive, AT_WEAP, wep_was_destroyed);
    // Second attack for two-weapon or skilled unarmed
    if (game.twohits && !(game.override_confirmation
                          || (game.multi || 0) < 0
                          || (game.u.umortality || 0) > oldumort
                          || !malive || m_at(x, y) !== mon)) {
        game.twohits = 2;
        const attk_count2 = { value: attk_count.value };
        const { tohit: tmp2, armorpenalty: ap2 } = await find_roll_to_hit(mon, uattk.aatyp, uswapwep_get(), attk_count2);
        mon_maybe_unparalyze(mon);
        const dieroll2 = rnd(20);
        let mhit2 = (tmp2 > dieroll2 || game.u.uswallow) ? 1 : 0;
        const mhit2_ref = { value: mhit2 };
        malive = await known_hitum(mon, secondwep, mhit2_ref, tmp2, ap2, uattk, dieroll2);
        mhit2 = mhit2_ref.value;
        if (mhit2)
            await passive(mon, secondwep, mhit2, malive, AT_WEAP,
                          secondwep && !uswapwep_get());
    }
    game.twohits = 0;
    return malive;
}
// ── mhitm_ad_phys ──
// C ref: uhitm.c:3981
export async function mhitm_ad_phys(magr, mattk, mdef, mhm) {
    const pa = magr.data;
    const pd = mdef.data;
    if (magr === game.youmonst) {
        // uhitm path
        if (pd === mons[PM_SHADE]) {
            mhm.damage = 0;
            // if (!mhm.specialdmg) impossible(...)
        }
        mhm.damage += (mhm.specialdmg || 0);
        if (mattk.aatyp === AT_WEAP) {
            mhm.damage = 0;
        } else if (mattk.aatyp === AT_KICK
                   || mattk.aatyp === AT_CLAW
                   || mattk.aatyp === AT_TUCH
                   || mattk.aatyp === AT_HUGS) {
            if (thick_skinned(pd))
                mhm.damage = (mattk.aatyp === AT_KICK) ? 0
                              : Math.trunc((mhm.damage + 1) / 2);
            if (game.u.udaminc > 0) {
                mhm.damage += game.u.udaminc;
            } else if (mhm.damage > 0) {
                mhm.damage += game.u.udaminc;
                if (mhm.damage < 1) mhm.damage = 1;
            }
        }
    } else if (mdef === game.youmonst) {
        // mhitu path — simplified
    } else {
        // mhitm path — simplified
    }
}
// ── mhitm_adtyping (full dispatch) ──
// C ref: uhitm.c:4782
export async function mhitm_adtyping_full(magr, mattk, mdef, mhm) {
    switch (mattk.adtyp) {
    case AD_PHYS: await mhitm_ad_phys(magr, mattk, mdef, mhm); break;
    // All other damage types — stub damage to 0 for now
    // These need full ports from mhitm.c for each case
    case AD_STUN: case AD_LEGS: case AD_WERE: case AD_HEAL:
    case AD_FIRE: case AD_COLD: case AD_ELEC: case AD_ACID:
    case AD_STON: case AD_SSEX: case AD_SITM: case AD_SEDU:
    case AD_SGLD: case AD_TLPT: case AD_BLND: case AD_CURS:
    case AD_DRLI: case AD_RUST: case AD_CORR: case AD_DCAY:
    case AD_DREN: case AD_DRST: case AD_DRDX: case AD_DRCO:
    case AD_DRIN:
        if (magr === game.youmonst) {
            // hero mind flayer eating monster's brain (C ref: uhitm.c:3186)
            const pd_drin = mdef.data;
            if (game.notonhead || !has_head(pd_drin)) {
                await pline("%s doesn't seem harmed.", Monnam(mdef));
                game.skipdrin = true;
                mhm.damage = 0;
                if (!Unchanging() && pd_drin === mons[PM_GREEN_SLIME]) {
                    if (!Slimed()) {
                        await You("suck in some slime and don't feel very well.");
                        await make_slimed(10, null);
                    }
                }
                break;
            }
            if (await m_slips_free(mdef, mattk))
                break;
            { const helmet = which_armor(mdef, W_ARMH);
            if (helmet && rn2(8)) {
                await pline('%s %s blocks your attack to %s head.',
                    s_suffix(Monnam(mdef)), helm_simple_name(helmet), mhis(mdef));
                break;
            } }
            { const amu_drin = which_armor(mdef, W_AMUL);
            const lifsav = amu_drin && amu_drin.otyp === AMULET_OF_LIFE_SAVING;
            const dmg_ref = { value: mhm.damage };
            await eat_brains(game.youmonst, mdef, true, dmg_ref);
            mhm.damage = dmg_ref.value;
            if (lifsav && !which_armor(mdef, W_AMUL))
                game.skipdrin = true;
            }
        }
        break;
    case AD_STCK: case AD_WRAP: case AD_PLYS:
    case AD_SLEE: case AD_SLIM: case AD_ENCH: case AD_SLOW:
    case AD_CONF: case AD_POLY: case AD_DISE: case AD_SAMU:
    case AD_DETH: case AD_PEST: case AD_FAMN: case AD_DGST:
    case AD_HALU:
        // These are hero-as-monster attack types; for the uhitm
        // path (magr === youmonst), most require full mhitm.c ports.
        break;
    default:
        mhm.damage = 0;
    }
}
// ── damageum ──
// C ref: uhitm.c:4836
export async function damageum(mdef, mattk, specialdmg) {
    let mhm = {
        damage: c_d(mattk.damn || 0, mattk.damd || 0),
        hitflags: M_ATTK_MISS,
        permdmg: 0,
        specialdmg: specialdmg,
        done: false,
    };
    if (is_demon(game.youmonst?.data) && !rn2(13) && !uwep_get()
        && game.u.umonnum !== PM_AMOROUS_DEMON && game.u.umonnum !== PM_BALROG) {
        await demonpet();
        return M_ATTK_MISS;
    }
    await mhitm_adtyping_full(game.youmonst, mattk, mdef, mhm);
    if (mhm.done)
        return mhm.hitflags;
    mdef.mstrategy = (mdef.mstrategy || 0) & ~STRAT_WAITFORU;
    mdef.mhp -= mhm.damage;
    if (DEADMONSTER(mdef)) {
        if (mattk.aatyp === AT_WEAP || mattk.aatyp === AT_CLAW)
            game.mkcorpstat_norevive = troll_baned(mdef, uwep_get()) ? true : false;
        if (mdef.mtame && !cansee(mdef.mx, mdef.my)) {
            await You_feel("embarrassed for a moment.");
            if (mhm.damage)
                await xkilled(mdef, XKILL_NOMSG);
        } else if (!game.flags?.verbose) {
            await You("destroy it!");
            if (mhm.damage)
                await xkilled(mdef, XKILL_NOMSG);
        } else if (mhm.damage) {
            await killed(mdef);
        }
        game.mkcorpstat_norevive = false;
        return M_ATTK_DEF_DIED;
    }
    return M_ATTK_HIT;
}
// ── hmonas ──
// C ref: uhitm.c:5424 — attack monster as a monster; returns True if mon survives
async function hmonas(mon) {
    let weapon;
    let weapon_used = false, odd_claw = true;
    let dhit = 0, attknum = 0;
    let sum = new Array(NATTK).fill(M_ATTK_MISS);
    let multi_claw = 0, multi_weap = 0;
    game.vis = (canseemon(mon) || m_next2u(mon));
    for (let i = 0; i < NATTK; i++) {
        const mattk = getmattk(game.youmonst, mon, i, sum, {});
        if (mattk.aatyp === AT_WEAP) ++multi_weap;
        if (mattk.aatyp === AT_WEAP || mattk.aatyp === AT_CLAW || mattk.aatyp === AT_TUCH)
            ++multi_claw;
    }
    multi_claw = (multi_claw > 1) ? 1 : 0;
    game.twohits = 0;
    for (let i = 0; i < NATTK; i++) {
        if (i > 0 && (m_at(game.bhitpos.x, game.bhitpos.y) !== mon
                      || DEADMONSTER(mon)))
            continue;
        const mattk = getmattk(game.youmonst, mon, i, sum, {});
        weapon = null;
        switch (mattk.aatyp) {
        case AT_WEAP: {
            if (weapon_used && (sum[i - 1] > M_ATTK_MISS)
                && uwep_get() && bimanual(uwep_get()))
                continue;
            weapon_used = true;
            weapon = uwep_get();
            const attk_count_obj = { value: attknum };
            const { tohit: tmp, armorpenalty } = await find_roll_to_hit(mon, AT_WEAP, weapon, attk_count_obj);
            attknum = attk_count_obj.value;
            mon_maybe_unparalyze(mon);
            const dieroll = rnd(20);
            dhit = (tmp > dieroll || game.u.uswallow) ? 1 : 0;
            if (multi_weap > 1) ++game.twohits;
            const dhit_ref = { value: dhit };
            const monster_survived = await known_hitum(mon, weapon, dhit_ref, tmp,
                                                       armorpenalty, mattk, dieroll);
            dhit = dhit_ref.value;
            weapon = uwep_get(); // might have been destroyed
            if (!monster_survived) {
                sum[i] = M_ATTK_DEF_DIED;
                break;
            } else
                sum[i] = dhit ? M_ATTK_HIT : M_ATTK_MISS;
            if (m_at(game.u.ux + game.u.dx, game.u.uy + game.u.dy) !== mon) {
                i = NATTK;
                break;
            }
            if (dhit && mattk.adtyp !== AD_SPEL && mattk.adtyp !== AD_PHYS)
                sum[i] = await damageum(mon, mattk, 0);
            break;
        }
        case AT_CLAW:
            if (uwep_get() && !weapon_used) {
                // use weapon instead (goto use_weapon in C)
                weapon_used = true;
                weapon = uwep_get();
                const attk_count_obj = { value: attknum };
                const { tohit: tmp, armorpenalty } = await find_roll_to_hit(mon, AT_WEAP, weapon, attk_count_obj);
                attknum = attk_count_obj.value;
                mon_maybe_unparalyze(mon);
                const dieroll = rnd(20);
                dhit = (tmp > dieroll || game.u.uswallow) ? 1 : 0;
                const dhit_ref = { value: dhit };
                const monster_survived = await known_hitum(mon, weapon, dhit_ref, tmp,
                                                           armorpenalty, mattk, dieroll);
                dhit = dhit_ref.value;
                weapon = uwep_get();
                if (!monster_survived) { sum[i] = M_ATTK_DEF_DIED; break; }
                sum[i] = dhit ? M_ATTK_HIT : M_ATTK_MISS;
                if (dhit && mattk.adtyp !== AD_SPEL && mattk.adtyp !== AD_PHYS)
                    sum[i] = await damageum(mon, mattk, 0);
                break;
            }
            // AT_CLAW without weapon falls through to AT_TUCH
        case AT_TUCH:
        case AT_KICK:
        case AT_BITE:
        case AT_STNG:
        case AT_BUTT:
        case AT_TENT: {
            const attk_count_obj = { value: attknum };
            const { tohit: tmp, armorpenalty } = await find_roll_to_hit(mon, mattk.aatyp, null, attk_count_obj);
            attknum = attk_count_obj.value;
            mon_maybe_unparalyze(mon);
            const dieroll = rnd(20);
            dhit = (tmp > dieroll || game.u.uswallow) ? 1 : 0;
            if (dhit) {
                wakeup(mon, true);
                if (mattk.aatyp === AT_TENT) {
                    await Your(`tentacles suck ${mon_nam(mon)}.`);
                } else {
                    const verb = (mattk.aatyp === AT_CLAW) ? "hit"
                        : (mattk.aatyp === AT_KICK) ? "kick"
                        : (mattk.aatyp === AT_BITE) ? "bite"
                        : (mattk.aatyp === AT_STNG) ? "sting"
                        : (mattk.aatyp === AT_BUTT) ? "head butt"
                        : "hit";
                    await You(`${verb} ${mon_nam(mon)}.`);
                }
                sum[i] = await damageum(mon, mattk, 0);
            } else {
                await missum(mon, mattk, (tmp + armorpenalty > dieroll));
            }
            break;
        }
        case AT_HUGS:
            // simplified hugs
            dhit = 1;
            wakeup(mon, true);
            if (mon === game.u.ustuck) {
                await pline(`${Monnam(mon)} is being crushed.`);
                sum[i] = await damageum(mon, mattk, 0);
            }
            break;
        case AT_ENGL: {
            const attk_count_obj = { value: attknum };
            const { tohit: tmp } = await find_roll_to_hit(mon, mattk.aatyp, null, attk_count_obj);
            attknum = attk_count_obj.value;
            mon_maybe_unparalyze(mon);
            dhit = (tmp > rnd(20 + i)) ? 1 : 0;
            if (dhit) {
                wakeup(mon, true);
                // gulpum — TODO
            } else {
                await missum(mon, mattk, false);
            }
            break;
        }
        case AT_NONE:
        case AT_BOOM:
        case AT_MAGC:
            continue;
        case AT_BREA:
        case AT_SPIT:
        case AT_GAZE:
            dhit = 0;
            break;
        default:
            break;
        }
        if (sum[i] === M_ATTK_DEF_DIED) {
            await passive(mon, weapon, 1, 0, mattk.aatyp, false);
        } else {
            await passive(mon, weapon, (sum[i] !== M_ATTK_MISS) ? 1 : 0, 1,
                          mattk.aatyp, false);
        }
        const hitflagsRef = { value: sum[i] };
        if (await mhitm_knockback(game.youmonst, mon, mattk, hitflagsRef, weapon_used))
            break;
        if (DEADMONSTER(mon)) break;
        if (!Upolyd()) break;
        if ((game.multi || 0) < 0) break;
    }
    game.vis = false;
    game.twohits = 0;
    return !DEADMONSTER(mon);
}
// ── do_attack ──
// C ref: uhitm.c:449 — main entry point for hero attacking monster
export async function do_attack(mtmp) {
    const g = game;
    const u = g.u;
    const mdat = mtmp.data;
    // Safe monster check — pet/peaceful displacement
    // C ref: uhitm.c:457-507
    if (is_safemon(mtmp) && !g.context.forcefight) {
        if (!u_wield_art(ART_STORMBRINGER)) {
            // C ref: uhitm.c:475-478 — multiple conditions for blocking
            const foo = (/* Punished || */ !rn2(7)
                         || (mtmp.wormno && is_longworm(mdat)));
            let inshop = false;
            // C ref: uhitm.c:483-488 — check if monster is in a tended shop
            if (!foo) {
                const shopRooms = in_rooms(mtmp.mx, mtmp.my, SHOPBASE);
                for (let i = 0; i < shopRooms.length; i++) {
                    const rm = g.level?.rooms?.[shopRooms[i]];
                    if (rm && tended_shop(rm)) {
                        inshop = true;
                        break;
                    }
                }
            }
            if (inshop || foo) {
                // C ref: uhitm.c:493-495 — auto-pay when bumping shopkeeper
                if (!g.context.travel && !g.context.run && canspotmon(mtmp) && mtmp.isshk) {
                    await dopay();
                    return true;
                }
                if (mtmp.mtame)
                    await monflee(mtmp, rnd(6), false, false);
                await You(`stop.  ${y_monnam(mtmp).charAt(0).toUpperCase() + y_monnam(mtmp).slice(1)} is in the way!`);
                end_running(true);
                return true;
            } else if (mtmp.mfrozen || helpless(mtmp)
                       || (mdat?.mmove === 0 && rn2(6))) {
                end_running(true);
                return true;
            } else {
                return false; // displacement in caller
            }
        }
    }
    g.override_confirmation = false;
    if (!g.bhitpos) g.bhitpos = {};
    g.bhitpos.x = u.ux + u.dx;
    g.bhitpos.y = u.uy + u.dy;
    g.notonhead = (g.bhitpos.x !== mtmp.mx || g.bhitpos.y !== mtmp.my);
    if (await attack_checks(mtmp, uwep_get()))
        return true;
    if (Upolyd() && noattacks(g.player?.data)) {
        await You('have no way to attack monsters physically.');
        mtmp.mstrategy = (mtmp.mstrategy || 0) & ~STRAT_WAITMASK;
        return true;
    }
    if (check_capacity('You cannot fight while so heavily loaded.'))
        return true;
    if (await overexertion())
        return true;
    if (g.unweapon) {
        g.unweapon = false;
        if (g.flags?.verbose) {
            if (uwep_get())
                await You(`begin bashing monsters with ${yname(uwep_get())}.`);
            else if (!nohands(g.youmonst?.data) && !verysmall(g.youmonst?.data))
                await You(`begin ${ing_suffix(Role_if(PM_MONK) ? 'strike' : 'bash')} monsters with your ${uarmg_get() ? 'gloved' : 'bare'} hands.`);
        }
    }
    exercise(A_STR, true);
    u_wipe_engr(3);
    // Leprechaun evasion
    if (mdat?.mlet === S_LEPRECHAUN && !mtmp.mfrozen && !helpless(mtmp)
        && !mtmp.mconf && mtmp.mcansee && !rn2(7)) {
        await You('miss wildly and stumble forwards.');
        return false;
    }
    // The actual attack
    if (Upolyd()) {
        await hmonas(mtmp);
    } else {
        const uattk = g.player?.data?.mattk?.[0]
            || { aatyp: AT_WEAP, adtyp: AD_PHYS, damn: 0, damd: 0 };
        await hitum(mtmp, uattk);
    }
    mtmp.mstrategy = (mtmp.mstrategy || 0) & ~STRAT_WAITMASK;
    return true;
}
// ── passive ──
// C ref: uhitm.c:5866
export async function passive(mon, weapon, mhitb, maliveb, aatyp, wep_was_destroyed) {
    if (!mon || !mon.data) return 0;
    const ptr = mon.data;
    const mattk = ptr.mattk || [];
    let mhit = mhitb ? M_ATTK_HIT : M_ATTK_MISS;
    let malive = maliveb ? M_ATTK_HIT : M_ATTK_MISS;
    let i;
    for (i = 0; ; i++) {
        if (i >= NATTK) return (malive | mhit);
        if (i >= mattk.length || (mattk[i]?.aatyp ?? 0) === AT_NONE) break;
    }
    let tmp;
    const atk = mattk[i] || {};
    if (atk.damn)
        tmp = c_d(atk.damn, atk.damd);
    else if (atk.damd)
        tmp = c_d((mon.m_lev || 0) + 1, atk.damd);
    else
        tmp = 0;
    // Effects that apply even if monster just died
    switch (atk.adtyp) {
    case AD_FIRE:
        if (mhitb && !mon.mcan && weapon) {
            if (aatyp === AT_KICK) {
                if (uarmf_get() && !rn2(6))
                    await erode_obj(uarmf_get(), xname(uarmf_get()), ERODE_BURN,
                              EF_GREASE | EF_VERBOSE);
            } else if (aatyp === AT_WEAP || aatyp === AT_CLAW
                       || aatyp === AT_MAGC || aatyp === AT_TUCH)
                await passive_obj(mon, weapon, atk);
        }
        break;
    case AD_ACID:
        if (mhitb && rn2(2)) {
            if (!rn2(30))
                await erode_armor(game.youmonst, ERODE_CORRODE);
        }
        if (mhitb && weapon) {
            if (aatyp === AT_KICK) {
                if (uarmf_get() && !rn2(6))
                    await erode_obj(uarmf_get(), xname(uarmf_get()), ERODE_CORRODE,
                              EF_GREASE | EF_VERBOSE);
            } else if (aatyp === AT_WEAP || aatyp === AT_CLAW
                       || aatyp === AT_MAGC || aatyp === AT_TUCH)
                await passive_obj(mon, weapon, atk);
        }
        exercise(A_STR, false);
        break;
    case AD_STON:
        // petrification passive — simplified, no stone resistance check yet
        break;
    case AD_RUST:
        if (mhitb && !mon.mcan && weapon) {
            if (aatyp === AT_KICK) {
                if (uarmf_get())
                    await erode_obj(uarmf_get(), xname(uarmf_get()), ERODE_RUST,
                              EF_GREASE | EF_VERBOSE);
            } else if (aatyp === AT_WEAP || aatyp === AT_CLAW
                       || aatyp === AT_MAGC || aatyp === AT_TUCH)
                await passive_obj(mon, weapon, atk);
        }
        break;
    case AD_CORR:
        if (mhitb && !mon.mcan && weapon) {
            if (aatyp === AT_KICK) {
                if (uarmf_get())
                    await erode_obj(uarmf_get(), xname(uarmf_get()), ERODE_CORRODE,
                              EF_GREASE | EF_VERBOSE);
            } else if (aatyp === AT_WEAP || aatyp === AT_CLAW
                       || aatyp === AT_MAGC || aatyp === AT_TUCH)
                await passive_obj(mon, weapon, atk);
        }
        break;
    case AD_MAGM:
        // Oracle wrath — simplified
        break;
    case AD_ENCH:
        if (mhitb) {
            if (aatyp === AT_KICK) {
                if (!weapon) break;
            } else if (aatyp === AT_BITE || aatyp === AT_BUTT
                       || (aatyp >= AT_STNG && aatyp < AT_WEAP)) {
                break;
            }
            await passive_obj(mon, weapon, atk);
        }
        break;
    default:
        break;
    }
    // Effects only if monster still alive
    if (maliveb && !mon.mcan && rn2(3)) {
        switch (atk.adtyp) {
        case AD_PLYS:
            if (ptr === mons[PM_FLOATING_EYE]) {
                if (!canseemon(mon)) break;
                if (mon.mcansee) {
                    // floating eye gaze — simplified
                    // In C: ureflects check, Hallucination check, Free_action check
                    // then nomul() for paralysis
                    if (false /* Free_action */) {
                        await You(`momentarily stiffen under ${s_suffix(mon_nam(mon))} gaze!`);
                    } else {
                        await You(`are frozen by ${s_suffix(mon_nam(mon))} gaze!`);
                        const dur = (ACURR(A_WIS) > 12 || rn2(4)) ? -tmp : -127;
                        game.multi = dur;
                        dynamic_multi_reason(mon, "frozen", true);
                        game.nomovemsg = 0;
                    }
                } else {
                    await pline(`The blind ${mon_nam(mon)} cannot defend itself.`);
                    if (!rn2(500))
                        change_luck(-1);
                }
            } else {
                // gelatinous cube etc
                await You(`are frozen by ${mon_nam(mon)}!`);
                game.multi = -tmp;
                dynamic_multi_reason(mon, "frozen", false);
                exercise(A_DEX, false);
            }
            break;
        case AD_COLD:
            // brown mold/blue jelly passive cold
            break;
        case AD_STUN:
            // yellow mold passive stun
            break;
        case AD_FIRE:
            // passive fire
            break;
        case AD_ELEC:
            // passive electricity
            break;
        default:
            break;
        }
    }
    return (malive | mhit);
}
// ── passive_obj ──
// C ref: uhitm.c:6128
export async function passive_obj(mon, obj, mattk) {
    if (!obj) {
        obj = (game.u.twoweap && uswapwep_get() && !rn2(2)) ? uswapwep_get() : uwep_get();
        if (!obj && mattk && mattk.adtyp === AD_ENCH)
            obj = uarmg_get();
        if (!obj) return;
    }
    if (!mattk) {
        const mk = mon.data?.mattk || [];
        let i;
        for (i = 0; ; i++) {
            if (i >= NATTK) return;
            if (i >= mk.length || (mk[i]?.aatyp ?? 0) === AT_NONE) break;
        }
        mattk = mk[i] || {};
    }
    switch (mattk.adtyp) {
    case AD_FIRE:
        if (!rn2(6) && !mon.mcan) {
            await erode_obj(obj, xname(obj), ERODE_BURN, EF_GREASE | EF_VERBOSE);
        }
        break;
    case AD_ACID:
        if (!rn2(6)) {
            await erode_obj(obj, xname(obj), ERODE_CORRODE, EF_GREASE | EF_VERBOSE);
        }
        break;
    case AD_RUST:
        if (!mon.mcan) {
            await erode_obj(obj, xname(obj), ERODE_RUST, EF_GREASE | EF_VERBOSE);
        }
        break;
    case AD_CORR:
        if (!mon.mcan) {
            await erode_obj(obj, xname(obj), ERODE_CORRODE, EF_GREASE | EF_VERBOSE);
        }
        break;
    case AD_ENCH:
        if (!mon.mcan) {
            // drain_item — TODO
        }
        break;
    default:
        break;
    }
}
// ── joust ──
// C ref: uhitm.c:2098
export function joust(mon, obj) {
    let skill_rating, joust_dieroll;
    // C ref: uhitm.c — can't joust while fumbling or stunned
    if (Fumbling() || Stunned()) return 0;
    if (game.u.utrap)
        return 0;
    if (obj !== uwep_get() && (obj !== uswapwep_get() || !game.u.twoweap))
        return 0;
    skill_rating = P_SKILL(weapon_type(obj));
    if (game.u.twoweap && P_SKILL(P_TWO_WEAPON_COMBAT) < skill_rating)
        skill_rating = P_SKILL(P_TWO_WEAPON_COMBAT);
    if (skill_rating === P_ISRESTRICTED)
        skill_rating = P_UNSKILLED;
    if ((joust_dieroll = rn2(5)) < skill_rating) {
        if (joust_dieroll === 0 && rnl(50) === (50 - 1) && !is_unsolid(mon.data)
            && !obj_resists(obj, 0, 100))
            return -1;
        return 1;
    }
    return 0;
}
// ── demonpet ──
// C ref: uhitm.c:2133
export async function demonpet() {
    await pline("Some hell-p has arrived!");
    const i = !rn2(6) ? ndemon(game.u.ualign.type) : NON_PM;
    const pm = i !== NON_PM ? mons[i] : game.youmonst?.data;
    const dtmp = await makemon(pm, game.u.ux, game.u.uy, NO_MM_FLAGS);
    if (dtmp !== null)
        await tamedog(dtmp, null, false);
    exercise(A_WIS, true);
}
// ── theft_petrifies ──
// C ref: uhitm.c:2148
export function theft_petrifies(otmp) {
    if (uarmg_get() || otmp.otyp !== CORPSE
        || !touch_petrifies(mons[otmp.corpsenm]))
        return false;
    // instapetrify — TODO
    return true;
}
// ── steal_it ──
// C ref: uhitm.c:2174
export function steal_it(mdef, mattk) {
    let otmp = mdef.minvent;
    if (!otmp || (otmp.oclass === COIN_CLASS && !otmp.nobj))
        return;
    // TODO: full steal_it implementation
}
// ── nohandglow (exported) ──
export { nohandglow };
// ── that_is_a_mimic ──
// C ref: uhitm.c:6201
export async function that_is_a_mimic(mtmp, mimic_flags) {
    // simplified: just reveal the mimic
    await pline("Wait!  That's a monster!");
}
// ── stumble_onto_mimic ──
// C ref: uhitm.c:6282
export async function stumble_onto_mimic(mtmp) {
    await that_is_a_mimic(mtmp, 1 /* MIM_REVEAL */);
    if (!game.u.ustuck && !mtmp.mflee && dmgtype(mtmp.data, AD_STCK)
        && m_next2u(mtmp))
        game.u.ustuck = mtmp;
    wakeup(mtmp, false);
}
// ── flash_hits_mon ──
// C ref: uhitm.c:6341
export async function flash_hits_mon(mtmp, otmp) {
    if (game.notonhead) return 0;
    const useeit = canseemon(mtmp);
    if (mtmp.msleeping && haseyes(mtmp.data)) {
        mtmp.msleeping = 0;
        return useeit ? 1 : 0;
    } else if (mtmp.data.mlet !== S_LIGHT) {
        if (!resists_blnd(mtmp)) {
            const tmp = ((otmp.ox - mtmp.mx) ** 2 + (otmp.oy - mtmp.my) ** 2);
            if (mtmp.data === mons[10 /* PM_GREMLIN — approximate */]) {
                const amt = rnd(Math.min(mtmp.mhp, 4));
                await light_hits_gremlin(mtmp, amt);
            }
            if (!DEADMONSTER(mtmp)) {
                if (tmp < 9 && !mtmp.isshk && rn2(4))
                    await monflee(mtmp, rn2(4) ? rnd(100) : 0, false, true);
                mtmp.mcansee = 0;
                mtmp.mblinded = (tmp < 3) ? 0 : rnd(1 + Math.trunc(50 / tmp));
            }
            return useeit ? 1 : 0;
        }
    }
    return 0;
}
// ── light_hits_gremlin ──
// C ref: uhitm.c:6425
export async function light_hits_gremlin(mon, dmg) {
    if (canseemon(mon)) {
        await pline(`${Monnam(mon)} recoils from the light!`);
    }
    mon.mhp -= dmg;
    if (DEADMONSTER(mon)) {
        await killed(mon);
    }
}
// ── disguised_as_non_mon ──
// C ref: uhitm.c:6300
export function disguised_as_non_mon(mtmp) {
    return false; // simplified — needs M_AP_TYPE
}
// ── disguised_as_mon ──
// C ref: uhitm.c:6308
export function disguised_as_mon(mtmp) {
    return false; // simplified — needs M_AP_TYPE
}
// ── m_is_steadfast (exported) ──
// already exported above
// ── xkilled_full (internal) ──
// kept for backward compat; callers should now use killed() or xkilled()