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 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 | 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 260x 260x 260x 260x 73x 6097x 6097x 6097x 2855x 2855x 6097x 42x 42x 6097x 6097x 6097x 6097x 6097x 6097x 73x 8204x 8204x 8204x 8204x 8204x 328494x 328494x 328494x 328494x 801x 801x 328494x 7403x 8204x 73x 73x 1232x 1232x 1232x 14444x 14444x 14444x 73x 73x 73x 73x 73x 73x 73x 179x 179x 179x 9x 9x 8x 8x 9x 9x 9x 179x 155x 155x 179x 179x 73x 73x 73x 73x 73x 8737x 8737x 8737x 8737x 8737x 8737x 8737x 8737x 8737x 41666x 41666x 41666x 41666x 41666x 41666x 41666x 41666x 41666x 2x 2x 2x 2x 2x 2x 2x 41666x 41666x 41666x 41640x 41640x 41666x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 68x 68x 62x 62x 62x 62x 62x 62x 68x 19367x 19367x 19368x 19x 19x 19x 19x 19x 5x 12x 19x 19368x 19348x 19348x 19353x 19353x 19368x 17x 17x 17x 17x 19353x 19353x 19344x 41616x 41616x 8728x 8728x 8737x 2x 2x 2x 8728x 8737x 73x 73x 73x 73x 73x 73x 73x 73x 73x 2076x 2076x 2076x 73x 73x 73x 73x 2064x 2064x 2064x 2064x 73x 73x 73x 571x 571x 571x 571x 571x 571x 571x 571x 14444x 14444x 14444x 14444x 14444x 14444x 1119x 1119x 14444x 14444x 74x 74x 13251x 14444x 14444x 14444x 4x 4x 4x 8x 8x 8x 8x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 73x 73x 73x 73x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14448x 14448x 14448x 14448x 14448x 14448x 43344x 129861x 129861x 129861x 115413x 115413x 115413x 129861x 114823x 115413x 129861x 115413x 129861x 105427x 105427x 105427x 115413x 129861x 115413x 115413x 129861x 844x 841x 844x 3x 844x 115413x 129861x 42893x 42893x 42893x 42893x 42893x 42893x 42893x 115413x 129861x 129861x 129861x 129861x 115413x 129861x 29880x 28293x 274x 28293x 497x 497x 28019x 27522x 27522x 27522x 27522x 29880x 115413x 115413x 115413x 129861x 2066x 2066x 2066x 2066x 2066x 2066x 2066x 2066x 2066x 115413x 115413x 129861x 129861x 129861x 115413x 115413x 115413x 115413x 115413x 115413x 115413x 115413x 115413x 115413x 129861x 14x 14x 14x 129861x 5x 5x 5x 129861x 1705x 1699x 1699x 1699x 1699x 1699x 1705x 129861x 113708x 113708x 113708x 4531x 110x 4531x 4421x 4421x 4421x 4421x 4421x 4421x 4421x 4421x 4421x 4421x 4421x 4531x 113708x 113708x 113708x 113708x 113708x 113708x 129861x 129861x 16934x 16934x 16934x 115413x 129861x 115413x 129861x 80x 80x 80x 115413x 129861x 25238x 25238x 25238x 25238x 115413x 115413x 129861x 129861x 1232x 1232x 1232x 763x 292x 292x 292x 763x 763x 1232x 129861x 62004x 129861x 129861x 43344x 14448x 4x 4x 4x 4x 14444x 14444x 14444x 14444x 14444x 4421x 4421x 4421x 73x 4421x 4421x 4421x 4421x 4421x 4421x 4421x 4421x 4421x 73x 4421x 4421x 4421x 4421x 4421x 4421x 4421x 4421x 4421x 73x 19x 19x 19x 19x 19x 19x 45x 45x 45x 45x 45x 45x 10924x 10924x 22365x 22365x 10883x 10924x 73x 73x 3629x 3629x 3629x 3629x 1x 1x 1x 73x 73x 14442x 14442x 14442x 14442x 14442x 14444x 14444x 14444x 14444x 19x 19x 14444x 14444x 14444x 14444x 14444x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 73x 8763x 8763x 8763x 8763x 8763x 8763x 19x 19x 19x 19x 19x 2x 2x 19x 19x 19x 8744x 8763x 73x 73x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 5507x 14438x 979x 8937x 7958x 7958x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 14444x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 26091x 26091x 26091x 26091x 26091x 26091x 192x 192x 26091x 26091x 26091x 26091x 8641x 8641x 26091x 26091x 26091x 73x 26108x 26108x 26108x 26108x 26108x 26108x 26108x 26108x 73x 883x 883x 883x 883x 883x 65x 65x 65x 883x 810x 810x 810x 810x 810x 810x 810x 810x 810x 810x 810x 12x 12x 12x 12x 12x 12x 12x 810x 810x 810x 810x 810x 883x 883x 23x 23x 23x 10x 10x 10x 10x 23x 23x 883x 73x 73x 73x 6097x 6097x 26108x 26108x 26108x 26108x 26108x 26108x 26108x 26108x 26108x 26108x 26108x 26108x 26108x 26108x 26108x 6097x 73x 73x 73x 73x 11x 11x 11x 11x 11x 10x 10x 10x 10x 10x 11x 11x 11x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 66x 66x 66x 311x 311x 108x 311x 311x 311x 311x 66x 66x 73x 73x 73x 73x 11x 11x 73x 73x 73x 55x 55x 73x 73x 73x 73x 63x 63x 63x 63x 63x 63x 73x 73x 73x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 2x 2x 2x 73x 73x 73x 73x 5094x 5094x 5094x 28572x 28549x 28549x 5094x 5094x 5094x 73x 73x 73x 73x 688x 688x 73x 73x 73x 73x 73x 73x 73x 73x 80x 80x 80x 73x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 73x 139x 139x 139x 139x 139x 139x 137x 139x 73x 73x 73x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 15x 15x 15x 15x 15x 15x 15x 15x 15x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 6800x 6800x 6800x 142800x 142800x 6800x 85x 85x 85x 85x 85x 85x 73x 73x 24x 24x 24x 24x 24x 73x 73x 27x 27x 27x 27x 27x 34x 34x 34x 34x 27x 27x 27x 27x 27x 27x 27x 27x 2160x 2160x 2160x 45360x 45360x 2160x 27x 27x 27x 27x 34x 34x 34x 34x 34x 34x 27x 27x 27x 27x 73x 73x 73x 73x 73x 73x 29x 29x 29x 29x 29x 29x 29x 29x 15x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 1x 1x 1x 1x 1x 1x 28x 28x 29x 28x 28x 28x 29x 29x 23x 23x 23x 29x 73x 73x 73x 73x 29x 29x 29x 29x 29x 29x 29x 29x 6x 29x 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 12x 12x 73x 73x 4x 4x 4x 1x 1x 1x 4x 73x 73x 73x 73x 73x 19368x 19368x 19368x 19368x 19368x 19368x 73x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 19368x 252x 252x 19368x 19116x 19116x 19116x 4x 4x 4x 4x 19116x 19368x 19368x 73x 73x 19x 19x 73x 73x 252x 252x 252x 73x 73x 73x 73x 30358x 30358x 30358x 30358x 30358x 30358x 73x 73x 71984x 71984x 73x 73x 588x 588x 588x 588x 428x 428x 588x 160x 160x 160x 160x 588x 588x 73x 73x 73x 73x 15x 15x 15x 15x 73x 73x 73x 264x 264x 264x 264x 264x 73x 248x 248x 248x 248x 73x 73x 5289x 5289x 73x 73x 73x 23x 23x 23x 21x 21x 21x 21x 21x 23x 73x 15x 15x 15x 15x 15x 15x 15x 15x 73x 73x 11x 11x 11x 2x 2x 660x 660x 660x 2x 2x 11x 11x 11x 11x 11x 11x 11x 11x 73x 73x 73x 73x 73x 73x 73x 73x 864x 864x 864x 864x 864x 864x 864x 63x 63x 63x 63x 63x 86x 86x 86x 86x 39x 86x 35x 35x 86x 86x 86x 28x 28x 28x 63x 63x 864x 801x 801x 801x 801x 801x 864x 864x 864x 864x 864x 864x 52x 52x 32x 32x 32x 52x 1x 1x 1x 19x 19x 19x 46x 8x 8x 8x 8x 8x 8x 2x 2x 2x 2x 2x 8x 19x 19x 19x 812x 812x 864x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 15492x 15492x 15492x 15492x 15492x 15492x 15492x 15492x 15492x 15492x 73x 73x 73x 73x 73x 73x 29x 29x 29x 73x 73x 73x 73x 53x 53x 53x 73x 73x 73x 73x 233x 233x 233x 233x 6x 6x 6x 233x 5x 1x 5x 4x 4x 5x 233x 26x 26x 26x 233x 233x 233x 196x 196x 233x 233x 233x 233x 233x 15x 15x 15x 15x 15x 15x 15x 15x 233x 233x 73x 73x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 244x 244x 248x 248x 242x 248x 248x 73x 73x 73x 73x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 1x 1x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 73x 73x 73x 73x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 73x 23x 23x 23x 23x 23x 1x 23x 22x 22x 22x 22x 22x 22x 22x 22x 22x 23x 23x 23x 23x 73x 23x 23x 23x 73x 1x 1x 1x 73x 73x 23x 23x 23x 23x 23x 23x 4x 4x 4x 4x 73x 22x 22x 22x 22x 22x 22x 9x 22x 9x 4x 4x 3x 3x 3x 4x 1x 22x 73x 73x 442x 442x 442x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 442x 73x 73x 696x 696x 696x 696x 696x 696x 696x 696x 696x 696x 696x 665x 295x 295x 295x 295x 295x 665x 217x 217x 217x 217x 69x 69x 69x 69x 69x 69x 69x 69x 217x 665x 696x 696x 696x 696x 696x 696x 696x 696x 696x 73x 73x 73x 73x 259x 259x 259x 259x 259x 259x 259x 259x 259x 65x 18x 18x 65x 23x 23x 23x 23x 1x 1x 1x 23x 65x 259x 259x 259x 259x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 259x 259x 70x 70x 70x 70x 70x 70x 70x 73x 73x 73x 8922x 8922x 8922x 8922x 8768x 8768x 8922x 6x 6x 6x 154x 148x 148x 8774x 8922x 77x 77x 8922x 3x 3x 8922x 73x 73x 73x 73x 73x 5516x 5516x 5516x 5516x 5516x 5516x 5516x 5516x 5516x 5516x 85x 85x 83x 83x 2x 2x 2x 2x 2x 2x 2x 2x 2x 85x 2x 2x 85x 1x 1x 1x 23x 23x 23x 23x 23x 23x 73x 83x 83x 83x 83x 83x 73x 73x 73x 73x 73x 73x 18287x 18287x 18287x 18287x 18287x 18287x 18287x 18287x 18287x 18287x 18287x 18287x 18287x 18287x 18287x 18287x 18287x 18287x 18287x 18287x 18287x 18287x 18287x 18287x 17331x 17331x 17331x 17331x 956x 956x 956x 956x 956x 18287x 16545x 956x 956x 956x 18287x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 943x 943x 16545x 16545x 2116x 18287x 20x 20x 943x 943x 943x 16545x 16545x 34x 34x 34x 34x 34x 34x 34x 943x 909x 909x 909x 943x 18287x 34x 34x 34x 34x 16545x 1x 1x 943x 943x 15558x 56x 56x 18287x 849x 849x 18287x 4x 4x 18287x 18287x 34x 18287x 943x 18287x 73x 73x 4x 4x 4x 4x 4x 4x 4x 4x 73x 73x 73x 73x 18153x 18153x 18153x 73x 73x 73x 73x 9x 9x 9x 9x 73x 73x 2148x 2148x 2148x 73x 73x 8x 8x 8x 73x 73x 5x 5x 5x 73x 73x 73x 73x 72x 72x 72x 72x 72x 73x 73x 73x 73x 4x 4x 4x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x | // mon.js — Monster turn processing (port of mon.c, monmove.c)
// movemon() iterates all monsters each game tick.
// Each monster with enough movement points gets a turn via dochug().
//
// Event logging:
// ^movemon_turn[mnum@x,y mv=old->new] — per monster per turn
// ^distfleeck[mnum@x,y ...] — flee/distance check per monster
// ^mcalcmove[mnum@x,y speed=N mv=old->new] — movement reallocation
import { game } from './gstate.js';
import { closed_door, distfleeck, dochug, dochugw, grow_up, m_everyturn_effect, m_move, mintrap_monster, mon_track_add, mon_track_clear, onscary, postmov, set_apparxy } from './monmove.js';
import { adj_lev, apply_init_newcham, enexto, golemhp, makemon, pick_nasty, pickvampshape, set_mimic_sym } from './makemon.js';
import { formatMonsterEventRef, rn2, rnd, rn1, c_d, pushMultiLog, pushRngLogEntry } from './rng.js';
import { assertNotInModal } from './modal_guard.js';
import { couldsee, clear_path, cansee, vision_recalc } from './vision.js';
import { midnight } from './calendar.js';
import { Conflict, Deaf, Invis, Displaced, Is_rogue_level, Is_waterlevel, is_rogue_level, In_endgame, Underwater, has_ceiling } from './macros.js';
import { A_STR, ACCESSIBLE, FLYING, LEVITATION, ACCFOOD, ALLOW_ALL, ALL_TRAPS, ALLOW_BARS, ALLOW_DIG, ALLOW_M, ALLOW_MDISP, ALLOW_ROCK, ALLOW_SANCT, ALLOW_SSM, ALLOW_TM, ALLOW_TRAPS, ALLOW_U, ALLOW_WALL, APPORT, ARTICLE_A, ARTICLE_NONE, ARTICLE_THE, ARTICLE_YOUR, AUGMENT_IT, BOLT_LIM, BUSTDOOR, CADAVER, COLD_RES, COLNO, CORPSTAT_FEMALE, CORPSTAT_INIT, CORPSTAT_MALE, CORPSTAT_NONE, CORR, D_BROKEN, D_CLOSED, D_ISOPEN, D_LOCKED, D_NODOOR, D_TRAPPED, DART_TRAP, DISINT_RES, DISPLACED, DOGFOOD, DOOR, engulfing_u, EXACT_NAME, FIRE_RES, G_GENOD, has_mgivenname, I_SPECIAL, IRONBARS, IS_DOOR, IS_LAVA, IS_OBSTRUCTED, IS_POOL, IS_ROOM, IS_STWALL, IS_TREE, IS_WATERWALL, ismnum, isok, LAVAWALL, LOW_PM, M_AP_FURNITURE, M_AP_MONSTER, M_AP_OBJECT, M_AP_TYPE, M_ATTK_AGR_DIED, M_ATTK_DEF_DIED, M_ATTK_HIT, M_ATTK_MISS, MAGIC_PORTAL, MAGIC_TRAP, MANFOOD, MAXMONNO, MFAST, MGIVENNAME, MON_FLOOR, MON_MIGRATING, M_POISONGAS_OK, MSLOW, MTSZ, NC_SHOW_MSG, NC_VIA_WAND_OR_SPELL, NEED_AXE, NEED_HTH_WEAPON, NEED_PICK_AXE, NEED_PICK_OR_AXE, NEED_RANGED_WEAPON, NEED_WEAPON, NO_NC_FLAGS, NO_TRAP, NO_WEAPON_WANTED, NOGARLIC, NON_PM, NORMAL_SPEED, NOTONL, OPENDOOR, PIT, PLNMSG_HIDE_UNDER, POISON, POISON_RES, ROOM, ROOMOFFSET, ROWNO, SHOCK_RES, SHOPBASE, SLEEP_RES, SPIKED_PIT, SQKY_BOARD, STAIRS, STATUE_TRAP, STRAT_WAITFORU, STRAT_WAITMASK, SUPPRESS_HALLUCINATION, SUPPRESS_INVISIBLE, SUPPRESS_IT, SUPPRESS_MAPPEARANCE, SUPPRESS_NAME, SUPPRESS_SADDLE, TABU, TELEP_TRAP, TEMPLE, TRAPNUM, u_at, UNDEF, UNLOCKDOOR, VIBRATING_SQUARE, W_ARM, W_ARMG, W_ARMOR, W_ARMS, W_NONDIGGABLE, W_SADDLE, WT_TOOMUCH_DIAGONAL, W_WEP, IN_SIGHT, COULD_SEE } from './const.js';
import { AMULET_CLASS, AMULET_OF_YENDOR, APPLE, ARROW, AXE, BAG_OF_HOLDING, BAG_OF_TRICKS, BALL_CLASS, BATTLE_AXE, BELL_OF_OPENING, BLINDFOLD, BOOMERANG, BOULDER, CANDELABRUM_OF_INVOCATION, CANDY_BAR, CARROT, CHAIN_CLASS, CLOAK_OF_DISPLACEMENT, CLOVE_OF_GARLIC, COIN_CLASS, CORPSE, CREDIT_CARD, CRYSKNIFE, DAGGER, DART, DWARVISH_MATTOCK, EGG, ENORMOUS_MEATBALL, FEDORA, FOOD_CLASS, FORTUNE_COOKIE, GEM_CLASS, Is_candle, is_cloak, Is_container, Is_dragon_mail, Is_dragon_scales, is_gloves, is_shirt, LEASH, LEATHER_JACKET, LEMBAS_WAFER, LOCK_PICK, LUMP_OF_ROYAL_JELLY, MAGIC_MARKER, MAGIC_WHISTLE, MEAT_RING, MEAT_STICK, MEATBALL, OILSKIN_SACK, PANCAKE, PICK_AXE, RIN_STEALTH, RING_CLASS, ROCK_CLASS, RUBBER_HOSE, SACK, SKELETON_KEY, SLING, SPE_BOOK_OF_THE_DEAD, STETHOSCOPE, TIN, TIN_OPENER, TIN_WHISTLE, TOWEL, TRIPE_RATION, VENOM_CLASS, WEAPON_CLASS } from './objects.js';
import { bot, canseemon, canspotmon, cmap_to_glyph, flush_screen, glyph_at, newsym, seemimic, sensemon, setBotl, swallowed, unmap_object } from './display.js';
import { nomul } from './cmd.js';
import { ACURR, adjalign } from './attrib.js';
import { in_your_sanctuary, p_coaligned } from './priest.js';
import { growl } from './sounds.js';
import { find_mac, bypass_obj, m_dowear, mon_break_armor, Dragon_scales_to_pm, Dragon_mail_to_pm } from './worn.js';
import { an, ansimpleoname, doname, is_plural, makeplural, Tobjnam, xname } from './objnam.js';
import { in_rooms } from './shk.js';
import { mwelded, setmnotwielded } from './wield.js';
import { which_armor } from './worn.js';
import { lined_up, select_rwep } from './mthrowu.js';
import {
mons, M1_CARNIVORE, M1_HERBIVORE, M1_POIS, M1_ACID,
M1_AMORPHOUS,
M3_DISPLACES,
G_NOCORPSE,
AD_CORR, AD_RBRE, AD_RUST,
MR_POISON, MR_ACID, MR_STONE,
PM_FLOATING_EYE, PM_HEZROU, PM_LIZARD, PM_LICHEN, PM_FOG_CLOUD, PM_STALKER, PM_VROCK, PM_GELATINOUS_CUBE,
S_FUNGUS, S_VAMPIRE, S_BAT, S_LIGHT, S_EEL,
} from './monsters.js';
import {
M1_NOHANDS, M1_NOTAKE, M1_UNSOLID, M2_STRONG, M2_ROCKTHROW, M2_GIANT, M2_UNDEAD, M2_NOPOLY, M2_SHAPESHIFTER, MZ_TINY, MZ_HUMAN, MZ_SMALL,
PM_GRID_BUG, PM_GREMLIN, PM_IRON_GOLEM, PM_VLAD_THE_IMPALER, S_LEPRECHAUN, S_DRAGON, S_NYMPH, S_GOLEM, S_LICH, S_MIMIC, G_FREQ,
AT_CLAW, AT_GAZE, AT_KICK, AT_BITE, AT_STNG, AT_TUCH, AT_BUTT, AT_TENT, AT_WEAP, AT_HUGS, AT_ENGL, AT_MAGC, AT_BREA,
AD_DGST, AD_ELEC, AD_PHYS, AD_STCK, AD_SPEL, AD_CLRC, AD_DRST, AD_DRDX, AD_DRCO, AD_SAMU, AD_WRAP,
PM_SANDESTIN, PM_DOPPELGANGER, PM_CHAMELEON, PM_VAMPIRE, PM_VAMPIRE_LEADER,
PM_ARCHON, PM_JABBERWOCK, PM_STUDENT, PM_APPRENTICE, SPECIAL_PM,
PM_GRAY_DRAGON, PM_ARCHEOLOGIST, PM_WIZARD, PM_NAZGUL, PM_ERINYS, PM_MEDUSA, PM_PURPLE_WORM, PM_BABY_PURPLE_WORM, PM_SHRIEKER, MS_SHRIEK, MS_LEADER,
PM_ORC, PM_GIANT, PM_ELF, PM_HUMAN, PM_LONG_WORM, PM_MINOTAUR,
PM_KOBOLD_ZOMBIE, PM_GNOME_ZOMBIE, PM_ORC_ZOMBIE, PM_DWARF_ZOMBIE, PM_ELF_ZOMBIE, PM_HUMAN_ZOMBIE, PM_ETTIN_ZOMBIE, PM_GIANT_ZOMBIE,
PM_KOBOLD_MUMMY, PM_GNOME_MUMMY, PM_ORC_MUMMY, PM_DWARF_MUMMY, PM_ELF_MUMMY, PM_HUMAN_MUMMY, PM_ETTIN_MUMMY, PM_GIANT_MUMMY,
S_GHOST, S_KOP,
} from './monsters.js';
import { dist2, distmin, highc, monnear, online2, s_suffix, unsolid, upstart } from './hacklib.js';
import { mkcorpstat, mksobj, mksobj_at, obj_extract_self, obfree, place_object, sobj_at, undead_to_corpse, weight } from './mkobj.js';
import { stackobj } from './invent.js';
import { stealamulet } from './steal.js';
import { done_in_by } from './end.js';
import { amorphous, attacktype, attacktype_fordmg, bigmonst, breathless, can_track, digests, dmgtype, flesh_petrifies, has_head, haseyes, hides_under, humanoid, is_animal, is_clinger, is_covetous, is_dwarf, is_elf, is_female, is_floater, is_flyer, is_giant, is_gnome, is_golem, is_hider, is_home_elemental, is_human, is_male, is_mindless, is_minion, is_neuter, is_orc, is_rider, is_shapeshifter, is_swimmer, is_undead, is_unicorn, is_vampire, is_vampshifter, is_wanderer, is_whirly, likes_lava, locomotion, m_cansee, m_canseeu, mdistu, mon_knows_traps, mon_learns_traps, mon_resistancebits, monsndx, needspick, nohands, noncorporeal, nonliving, notake, passes_bars, passes_walls, perceives, polyok, pronoun_gender, regenerates, resist_conflict, resists_poison, resists_ston, set_mon_data, slithy, sticks, strongmonst, throws_rocks, tunnels, u_pronoun_gender, verysmall } from './mondata.js';
import { tactics } from './wizard.js';
import { visible_region_at } from './region.js';
import { S_poisoncloud } from './symbols.js';
import { levl, m_at, obj_at, t_at } from './map_access.js';
import { is_lava, is_pool, is_pool_or_lava } from './dbridge.js';
import { m_harmless_trap, mselftouch } from './trap.js';
import { noteleport_level } from './teleport.js';
import { emits_light } from './light.js';
import { bad_rock, helpless, may_passwall } from './hack.js';
import { expels, gazemu } from './mhitu.js';
import { fightm } from './mhitm.js';
export { revive, revive_egg } from './zap.js';
import { obj_resists } from './zap.js';
import { is_quest_artifact } from './artifact.js';
import { gd_move } from './vault.js';
const UTSZ = 100;
export function initrack() {
game.utcnt = 0;
game.utpnt = 0;
game.utrack = new Array(UTSZ).fill(null).map(() => ({ x: 0, y: 0 }));
}
export function settrack() {
if (!Array.isArray(game.utrack) || game.utrack.length !== UTSZ) {
initrack();
}
if (game.u.uleft?.otyp === RIN_STEALTH || game.u.uright?.otyp === RIN_STEALTH) {
return;
}
if ((game.utcnt || 0) < UTSZ) {
game.utcnt = (game.utcnt || 0) + 1;
}
if ((game.utpnt || 0) === UTSZ) {
game.utpnt = 0;
}
game.utrack[game.utpnt].x = game.u?.ux ?? 0;
game.utrack[game.utpnt].y = game.u?.uy ?? 0;
game.utpnt++;
// Track each settrack call so cross-engine divergences are visible.
pushRngLogEntry(`^settrack[pos=${game.u?.ux ?? 0},${game.u?.uy ?? 0} utpnt=${game.utpnt} utcnt=${game.utcnt}]`);
}
export function gettrack(x, y) {
const utrack = Array.isArray(game.utrack) ? game.utrack : null;
if (!utrack || utrack.length !== UTSZ) {
return null;
}
let cnt = game.utcnt || 0;
let idx = game.utpnt || 0;
while (cnt-- > 0) {
idx = (idx === 0) ? (UTSZ - 1) : (idx - 1);
const tc = utrack[idx];
const ndist = distmin(x, y, tc.x, tc.y);
if (ndist <= 1) {
return ndist ? tc : null;
}
}
return null;
}
export function hastrack(x, y) {
for (let i = 0; i < (game.utcnt || 0); i++) {
if (game.utrack[i].x === x && game.utrack[i].y === y)
return true;
}
return false;
}
// C ref: trap.h:125 — teleport trap with fixed destination
function fixed_tele_trap(trap) {
return trap.ttyp === TELEP_TRAP && isok(trap.teledest?.x, trap.teledest?.y);
}
function nodiag(mtmp) {
return mtmp?.mnum === PM_GRID_BUG;
}
export function has_attacktype(mtmp, atyp) {
return !!mtmp?.data?.mattk?.some(a => a && a.aatyp === atyp);
}
export function sticks_attack(mtmp) {
return !!mtmp?.data?.mattk?.some(a => a && a.adtyp === AD_STCK);
}
function melee_attacks(mtmp) {
const attacks = mtmp?.data?.mattk || [];
return attacks.filter((mattk) => mattk && [
AT_CLAW, AT_KICK, AT_BITE, AT_STNG, AT_TUCH, AT_BUTT, AT_TENT, AT_WEAP,
AT_MAGC,
].includes(mattk.aatyp));
}
// C ref: use Monnam/mon_nam for proper pet naming via mextra.mgivenname
export function monsterSubjectName(mtmp) {
if (!mtmp) return 'Something';
return Monnam(mtmp);
}
export function monsterObjectName(mtmp) {
if (!mtmp) return 'something';
return mon_nam(mtmp);
}
function attackVerb(mattk) {
switch (mattk?.aatyp) {
case AT_BITE:
return 'bites';
case AT_KICK:
return 'kicks';
case AT_STNG:
return 'stings';
case AT_BUTT:
return 'butts';
case AT_TUCH:
return 'touches';
case AT_TENT:
return 'tentacles suck';
default:
return 'hits';
}
}
// C ref: allmain.c stop_occupation()
export async function stop_occupation() {
pushRngLogEntry('>stop_occupation');
const g = game;
if (g.occupation) {
// C: maybe_finished_meal(TRUE) — if eating is done, finish properly
if (!(await maybe_finished_meal(true))) {
if (g.occtxt) await pline('You stop ' + g.occtxt + '.');
}
g.occupation = null;
setBotl('stop_occupation');
nomul(0);
} else if ((g.multi ?? 0) >= 0) {
nomul(0);
}
pushRngLogEntry('<stop_occupation');
}
// NOTE: differs from canonical region.js — cell-by-cell RNG expansion, not simple rect region
function create_gas_cloud(x, y, cloudsize, damage) {
if (!isok(x, y) || cloudsize <= 0) return null;
const xcoords = [x];
const ycoords = [y];
let newidx = 1;
const maxCloud = 150;
if (cloudsize > maxCloud) cloudsize = maxCloud;
for (let curridx = 0; curridx < newidx; curridx++) {
if (newidx >= cloudsize) break;
const xx = xcoords[curridx];
const yy = ycoords[curridx];
const dirs = [{ x: 0, y: -1 }, { x: 0, y: 1 }, { x: -1, y: 0 }, { x: 1, y: 0 }];
for (let i = 4; i > 0; --i) {
const swapidx = rn2(i);
const tmp = dirs[swapidx];
dirs[swapidx] = dirs[i - 1];
dirs[i - 1] = tmp;
}
let nvalid = 0;
for (let i = 0; i < 4; i++) {
const nx = xx + dirs[i].x;
const ny = yy + dirs[i].y;
if (!isok(nx, ny)) continue;
nvalid++;
let isunpicked = true;
for (let j = 0; j < newidx; j++) {
if (xcoords[j] === nx && ycoords[j] === ny) {
isunpicked = false;
break;
}
}
if (nvalid === 4 && !rn2(2)) continue;
if (isunpicked) {
xcoords[newidx] = nx;
ycoords[newidx] = ny;
newidx++;
}
if (newidx >= cloudsize) break;
}
}
const ttlBase = rn1(3, 4);
const ttl = Math.trunc((ttlBase * cloudsize) / newidx);
const cloud = {
x,
y,
damage,
ttl,
cells: xcoords.map((cx, i) => ({ x: cx, y: ycoords[i] })),
};
if (Array.isArray(game.level?.gasClouds)) game.level.gasClouds.push(cloud);
return cloud;
}
// NOTE: differs from canonical monmove.js — actually creates gas clouds (canonical has TODO)
// C ref: mon.c:1307 movemon()
// Iterates all monsters, gives each a turn if they have movement points.
export async function movemon() {
const g = game;
assertNotInModal('movemon');
let somebodyCanMove = false;
// Iterate using fmon chain (newest first) — matches C's mon.c:1307
// CRITICAL: C iterates fmon linked list, not an array. The pet
// (created last by makedog) is at the HEAD of fmon, so it
// processes FIRST. Using level.monsters (array) would process
// oldest first, producing wrong RNG order.
for (let mtmp = g.fmon; mtmp;) {
const next = mtmp.nmon;
// C ref: mon.c movemon_singlemon() early exit.
// When hero is queued to leave the level, stop processing
// remaining monsters this turn.
if (g.u?.utotype) {
somebodyCanMove = false;
break;
}
// C ref: mon.c movemon_singlemon() special parked-guard handling.
// Off-map vault guards at <0,0> still receive gd_move() once per
// turn so temporary corridor teardown can complete.
if (mtmp.isgd && !mtmp.mx && !(mtmp.mstate & MON_MIGRATING)) {
if ((g.moves ?? 0) > (mtmp.mlstmv ?? 0)) {
await gd_move(mtmp);
mtmp.mlstmv = g.moves ?? 0;
}
mtmp = next;
continue;
}
if (mtmp.dead) { mtmp = next; continue; }
if ((mtmp.mstate ?? MON_FLOOR) !== MON_FLOOR)
{ mtmp = next; continue; }
await m_everyturn_effect(mtmp);
// Does this monster have enough movement to act?
if (mtmp.movement >= NORMAL_SPEED) {
const oldMv = mtmp.movement;
mtmp.movement -= NORMAL_SPEED;
pushRngLogEntry(`^movemon_turn[${formatMonsterEventRef(mtmp)} mv=${oldMv}->${mtmp.movement} see=${mtmp.mcansee ? 1 : 0} tame=${mtmp.mtame || 0} conf=${mtmp.mconf ? 1 : 0} stun=${mtmp.mstun ? 1 : 0} inv=${mtmp.minvis ? 1 : 0} mux=${mtmp.mux ?? 0} muy=${mtmp.muy ?? 0}]`);
// C ref: mon.c movemon_singlemon() sets somebody_can_move
// immediately after deducting this turn's movement.
if (mtmp.movement >= NORMAL_SPEED)
somebodyCanMove = true;
// C ref: mon.c movemon_singlemon() — before each monster acts,
// consume deferred visibility updates so couldsee/canseemon logic
// during this monster turn uses current vision state.
if (g.vision_full_recalc)
vision_recalc(0);
// C ref: mon.c:1246 — check for drowning/lava/eel effects
if (await minliquid(mtmp)) {
mtmp = next;
continue;
}
// C ref: mon.c:1254-1268 — deferred monster re-equip turn.
// Monsters with I_SPECIAL spend a turn attempting to wear
// replacement gear when hero isn't perceived as nearby.
if ((mtmp.misc_worn_check ?? 0) & I_SPECIAL) {
if (mtmp.mpeaceful || mtmp.mtame
|| dist2(mtmp.mx, mtmp.my, mtmp.mux ?? g.u?.ux ?? 0, mtmp.muy ?? g.u?.uy ?? 0) > (3 * 3)) {
mtmp.misc_worn_check &= ~I_SPECIAL;
const oldworn = mtmp.misc_worn_check;
await m_dowear(mtmp, false);
if (mtmp.misc_worn_check !== oldworn || !mtmp.mcanmove)
{ mtmp = next; continue; }
}
}
// C ref: mon.c:1267-1274 — hidden monsters (mimics/piercers)
// may consume movement without entering dochug().
if (is_hider(mtmp.data)) {
// C ref: mon.c:1269 — unwatched mimics/piercers try to hide
if (restrap(mtmp))
{ mtmp = next; continue; }
if (mtmp.m_ap_type === M_AP_FURNITURE || mtmp.m_ap_type === M_AP_OBJECT)
{ mtmp = next; continue; }
if (mtmp.mundetected)
{ mtmp = next; continue; }
// C ref: mon.c:1276-1283 — eels stuck out of water try to hide
} else if ((mtmp.data?.mlet ?? 0) === S_EEL && !mtmp.mundetected
&& (mtmp.mflee || mdistu(mtmp) > 2)
&& !canseemon(mtmp) && !rn2(4)) {
if (hideunder(mtmp))
{ mtmp = next; continue; }
}
// C ref: mon.c:1291-1303 — conflict can divert this monster turn
// into monster-vs-monster fighting instead of dochugw().
if (Conflict() && !mtmp.iswiz && m_canseeu(mtmp)) {
if (cansee(mtmp.mx, mtmp.my)
&& (mdistu(mtmp) <= BOLT_LIM * BOLT_LIM)
&& await fightm(mtmp)) {
mtmp = next;
continue;
}
}
// C ref: mon.c:1305 — dochugw wraps dochug with occupation check
await dochugw(mtmp, true);
}
mtmp = next;
}
// C ref: mon.c movemon() — monster-triggered level transition
// is resolved inside movemon() and leaves monsters dormant.
if (g.u?.utotype) {
await deferred_goto();
somebodyCanMove = false;
}
return somebodyCanMove;
}
// C ref: monmove.c:690 dochug()
// NOTE: differs from canonical monmove.js — full implementation with STRAT_WAITFORU,
// spellcasting, post-m_move distfleeck, and phase 4 attack logic
// C ref: mondata.h — monnear(mon, x, y): monster within 1 square of (x,y)
// NOTE: differs from canonical map_access.js — kept to avoid new import edge
// C ref: mon.c:1832 mpickstuff() — minimal RNG-faithful gate.
export function mpickstuff_shallow(mtmp) {
const inShop = is_shop_square(mtmp.mx, mtmp.my);
// C: prevent shopkeepers from leaving their shop door to shop around.
if (mtmp.isshk && inShop) return false;
// C ref: mon.c:1842 — non-tame monsters usually don't shop (rn2(25)).
if (!mtmp.mtame && inShop && rn2(25)) return false;
return false;
}
function is_shop_square(x, y) {
const loc = game.level?.at?.(x, y);
if (!loc) return false;
const roomIdx = (loc.roomno ?? 0) - ROOMOFFSET;
if (roomIdx < 0) return false;
const room = game.level?.rooms?.[roomIdx];
return (room?.rtype ?? 0) >= SHOPBASE;
}
// Helper: C's cansee(x,y) = couldsee(x,y) && !Blind
function cansee_fn(x, y) {
if (game.u?.createdBlind || game.u?.prop?.blinded) return false;
return couldsee(x, y);
}
// Helper: C's amorphous(ptr) — M1_AMORPHOUS
function amorphous_fn(ptr) {
return !!(ptr?.mflags1 & M1_AMORPHOUS);
}
// C ref: monmove.c stuff_prevents_passage().
// Kept conservative for door-slip checks in mfndpos.
export function stuff_prevents_passage(mtmp) {
const u_carry = (mtmp === game.youmonst || mtmp === game.player || mtmp === game.u);
const objchn = u_carry ? game.invent : mtmp?.minvent;
for (let obj = objchn; obj; obj = obj.nobj) {
const typ = obj.otyp;
if (typ === COIN_CLASS && obj.quan > 100)
return true;
if (obj.oclass !== GEM_CLASS && !(typ >= ARROW && typ <= BOOMERANG)
&& !(typ >= DAGGER && typ <= CRYSKNIFE) && typ !== SLING
&& !is_cloak(obj) && typ !== FEDORA && !is_gloves(obj)
&& typ !== LEATHER_JACKET && typ !== CREDIT_CARD && !is_shirt(obj)
&& !(typ === CORPSE && verysmall(mons[obj.corpsenm]))
&& typ !== FORTUNE_COOKIE && typ !== CANDY_BAR && typ !== PANCAKE
&& typ !== LEMBAS_WAFER && typ !== LUMP_OF_ROYAL_JELLY
&& obj.oclass !== AMULET_CLASS && obj.oclass !== RING_CLASS
&& obj.oclass !== VENOM_CLASS && typ !== SACK
&& typ !== BAG_OF_HOLDING && typ !== BAG_OF_TRICKS
&& !Is_candle(obj) && typ !== OILSKIN_SACK && typ !== LEASH
&& typ !== STETHOSCOPE && typ !== BLINDFOLD && typ !== TOWEL
&& typ !== TIN_WHISTLE && typ !== MAGIC_WHISTLE
&& typ !== MAGIC_MARKER && typ !== TIN_OPENER && typ !== SKELETON_KEY
&& typ !== LOCK_PICK)
return true;
if (Is_container(obj) && obj.cobj)
return true;
}
return false;
}
// C ref: monmove.c can_fog().
function can_fog_fn(mon) {
if (!is_vampshifter(mon)) return false;
if ((game.mvitals?.[PM_FOG_CLOUD]?.mvflags ?? 0) & G_GENOD) return false;
return !stuff_prevents_passage(mon);
}
// NOTE: differs from canonical monmove.js — shk/guard/priest movement, dig capability, backtracking
// C ref: monmove.c:m_search_items() — targeted incremental port used by m_move
// to retarget movement toward desirable nearby floor objects.
function m_search_items(mtmp, ggxIn, ggyIn, apprIn) {
const ptr = mtmp.data;
const omx = mtmp.mx;
const omy = mtmp.my;
let ggx = ggxIn;
let ggy = ggyIn;
let appr = apprIn;
let minr = 5; // SQSRCHRADIUS
if (distmin(mtmp.mux ?? omx, mtmp.muy ?? omy, omx, omy) < 5 && !mtmp.mpeaceful) {
minr--;
}
const hmx = Math.min(COLNO - 1, omx + minr);
const hmy = Math.min(ROWNO - 1, omy + minr);
const lmx = Math.max(1, omx - minr);
const lmy = Math.max(0, omy - minr);
for (let xx = lmx; xx <= hmx; xx++) {
for (let yy = lmy; yy <= hmy; yy++) {
if (minr < distmin(omx, omy, xx, yy)) continue;
if (!could_reach_item_local(mtmp, xx, yy)) continue;
if (!m_cansee(mtmp, xx, yy)) continue;
for (const otmp of (game.level?.objects || [])) {
if (!otmp || otmp.ox !== xx || otmp.oy !== yy) continue;
if (!monster_wants_item_for_search(ptr, otmp)) continue;
minr = distmin(omx, omy, xx, yy);
ggx = xx;
ggy = yy;
break;
}
}
}
if (minr < 5 && appr === -1) {
if (distmin(omx, omy, mtmp.mux ?? omx, mtmp.muy ?? omy) <= 3) {
ggx = mtmp.mux ?? ggx;
ggy = mtmp.muy ?? ggy;
} else {
appr = 1;
}
}
return { ggx, ggy, appr };
}
function monster_wants_item_for_search(ptr, otmp) {
if (!otmp) return false;
if (ptr === mons[PM_GELATINOUS_CUBE]) return otmp.otyp !== BOULDER;
return false;
}
function could_reach_item_local(mon, nx, ny) {
const loc = game.level?.at(nx, ny);
if (!loc) return false;
if (IS_POOL(loc.typ) && !is_swimmer(mon?.data)) return false;
if (IS_LAVA(loc.typ) && !is_flyer(mon?.data)) return false;
for (const obj of (game.level?.objects || [])) {
if (obj && obj.ox === nx && obj.oy === ny && obj.otyp === BOULDER && !throws_rocks(mon?.data)) {
return false;
}
}
return true;
}
function worm_cross_local(x1, y1, x2, y2) {
if (distmin(x1, y1, x2, y2) !== 1) return false;
if (x1 === x2 || y1 === y2) return false;
const worm = m_at(x1, y2);
if (!worm || m_at(x2, y1) !== worm || !worm.wormno) return false;
const wtails = game.wormtails || [];
for (let curr = wtails[worm.wormno]; curr; curr = curr.nseg) {
const wnxt = curr.nseg;
if (!wnxt) break;
if (curr.wx === x1 && curr.wy === y2)
return wnxt.wx === x2 && wnxt.wy === y1;
if (curr.wx === x2 && curr.wy === y1)
return wnxt.wx === x1 && wnxt.wy === y2;
}
return false;
}
function m_poisongas_ok_local(mtmp) {
const mdat = mtmp?.data;
if (!mdat) return 0;
if (nonliving(mdat) || is_vampshifter(mtmp)
|| breathless(mdat)
|| mdat === mons[PM_HEZROU] || mdat === mons[PM_VROCK]) {
return M_POISONGAS_OK;
}
if ((mdat.mlet === S_EEL || Is_waterlevel(game.u?.uz))
&& is_pool(mtmp.mx, mtmp.my)) {
return M_POISONGAS_OK;
}
if (attacktype_fordmg(mdat, AT_BREA, AD_DRST)
|| attacktype_fordmg(mdat, AT_BREA, AD_RBRE)) {
return M_POISONGAS_OK;
}
return resists_poison(mtmp) ? 1 : 0;
}
function curr_mon_load_local(mtmp) {
let curload = 0;
for (let obj = mtmp?.minvent; obj; obj = obj.nobj) {
if (obj.otyp !== BOULDER || !throws_rocks(mtmp?.data)) {
curload += obj.owt || weight(obj);
}
}
return curload;
}
function cant_squeeze_thru_local(mon) {
const ptr = mon?.data;
if (!ptr) return false;
if (passes_walls(ptr)) return false;
if (bigmonst(ptr)
&& !(amorphous_fn(ptr) || is_whirly(ptr) || noncorporeal(ptr)
|| slithy(ptr) || can_fog_fn(mon))) {
return true;
}
if (curr_mon_load_local(mon) > WT_TOOMUCH_DIAGONAL) return true;
return false;
}
// C ref: mon.c:2125 mfndpos()
// Find valid positions adjacent to monster. No RNG.
// C iterates nx (outer) then ny (inner), starting from max(1, x-1).
export function mfndpos(mtmp, allowflags = 0) {
pushRngLogEntry('>mfndpos');
const map = game.level;
const positions = [];
const x = mtmp.mx, y = mtmp.my;
const nowtyp = map?.at(x, y)?.typ ?? 0;
const forbidDiag = nodiag(mtmp);
const allowM = !!mtmp?.mtame;
const monseeu = !!mtmp?.mcansee && (!Invis() || perceives(mtmp?.data));
const wantpool = (mtmp?.data?.mlet === S_EEL);
// C ref: mon.c:2147-2151 — poolok/lavaok use m_in_air (flyer/floater/clinger)
const poolok = ((!Is_waterlevel(game.u?.uz) && m_in_air(mtmp))
|| (is_swimmer(mtmp?.data) && !wantpool));
let lavaok = (m_in_air(mtmp) || likes_lava(mtmp?.data));
if (mtmp?.data === mons[PM_FLOATING_EYE]) lavaok = false;
const gasGlyph = cmap_to_glyph(S_poisoncloud);
const poisongasOk = (m_poisongas_ok_local(mtmp) === M_POISONGAS_OK);
const hereGas = visible_region_at(x, y);
const inPoisongas = !!hereGas && hereGas.glyph === gasGlyph;
const { rockok, treeok } = mon_dig_capability(mtmp);
let scanFlags = allowflags;
if (mtmp.mconf) {
scanFlags = (scanFlags | ALLOW_ALL) & ~NOTONL;
}
if (!mtmp.mcansee) {
scanFlags |= ALLOW_SSM;
}
// C nexttry path: eels prefer water; if no move found while on land,
// retry once allowing non-pool squares.
let seekPool = wantpool;
let didRetry = false;
while (true) {
positions.length = 0;
// C: iterates nx from max(1, x-1) to min(x+1, COLNO-1)
// and ny from max(0, y-1) to min(y+1, ROWNO-1)
const maxx = Math.min(x + 1, COLNO - 1);
const maxy = Math.min(y + 1, ROWNO - 1);
for (let nx = Math.max(1, x - 1); nx <= maxx; nx++) {
for (let ny = Math.max(0, y - 1); ny <= maxy; ny++) {
if (nx === x && ny === y) continue;
const loc = map?.at(nx, ny);
if (!loc) continue;
const ntyp = loc.typ;
let reject = '';
// C ref: mon.c mfndpos() pool/lava/waterwall/lavawall gates.
if (!(poolok || (is_pool(nx, ny) === seekPool))) reject = 'pool';
else if (!lavaok && is_lava(nx, ny)) reject = 'lava';
// C ref: mon.c:2203-2204 — waterwall blocks non-swimmers
if (!reject && IS_WATERWALL(ntyp) && !is_swimmer(mtmp?.data)) reject = 'waterwall';
// C ref: mon.c:2221-2223 — avoid poison gas unless already in it
if (!reject && !poisongasOk && !inPoisongas) {
const gasReg = visible_region_at(nx, ny);
if (gasReg && gasReg.glyph === gasGlyph) reject = 'poisongas';
}
// C ref: mon.c:2237 — lavawall blocks unless lavaok AND ALLOW_WALL
if (!reject && ntyp === LAVAWALL && (!lavaok || !(scanFlags & ALLOW_WALL))) reject = 'lavawall';
// C ref: mon.c:2206-2211 — iron bars: ALLOW_BARS needed, and
// non-diggable bars block rust/corrosion monsters even with ALLOW_BARS
if (!reject && ntyp === IRONBARS) {
if (!(scanFlags & ALLOW_BARS)) {
reject = 'ironbars';
} else if ((loc.wall_info & W_NONDIGGABLE)
&& (dmgtype(mtmp?.data, AD_RUST) || dmgtype(mtmp?.data, AD_CORR))) {
reject = 'ironbars_ndig';
}
}
// C ref: mon.c:2197-2200 — allow obstructed terrain when digging.
if (!reject && IS_OBSTRUCTED(ntyp)) {
const canPassWall = !!(scanFlags & ALLOW_WALL)
&& may_passwall(nx, ny);
const canDigHere = !!(scanFlags & ALLOW_DIG)
&& may_dig_monmove(nx, ny)
&& ((IS_TREE(ntyp) && treeok) || (!IS_TREE(ntyp) && rockok));
if (!canPassWall && !canDigHere) reject = 'obst';
}
// C ref: mon.c:2205-2209 — intelligent peacefuls avoid digging shop/temple walls
if (!reject && IS_OBSTRUCTED(ntyp) && rockok
&& !is_mindless(mtmp?.data) && (mtmp.mpeaceful || mtmp.mtame)
&& (in_rooms(nx, ny, TEMPLE).length > 0 || in_rooms(nx, ny, SHOPBASE).length > 0)
&& !(in_rooms(x, y, TEMPLE).length > 0 || in_rooms(x, y, SHOPBASE).length > 0)) {
reject = 'peaceful_dig';
}
// C ref: mon.c:2226-2236 — diagonal restrictions
if (!reject && nx !== x && ny !== y) {
if (forbidDiag) { reject = 'nodiag'; }
else if (IS_DOOR(nowtyp) && ((map?.at(x, y)?.doormask ?? 0) & ~D_BROKEN)) {
reject = 'diag_nowdoor';
} else if (IS_DOOR(ntyp) && ((loc.doormask ?? 0) & ~D_BROKEN)) {
reject = 'diag_nextdoor';
// C ref: mon.c:2230-2231 — Rogue level: no diagonal through any door
} else if ((IS_DOOR(nowtyp) || IS_DOOR(ntyp)) && Is_rogue_level(game.u?.uz)) {
reject = 'diag_rogue';
// C ref: mon.c:2234-2235 — worm segment cross check
} else if (m_at(x, ny) && m_at(nx, y)
&& worm_cross_local(x, y, nx, ny)
&& !m_at(nx, ny)
&& !u_at(nx, ny)) {
reject = 'worm_cross';
}
}
// C ref: mon.c:2212-2219 door gating.
// C: thrudoor = (flag & (ALLOW_WALL | BUSTDOOR))
// thrudoor is also set true when rockok||treeok (C line 2174-2175)
if (!reject && IS_DOOR(ntyp)) {
const mask = loc.doormask ?? loc.flags ?? 0;
const canSlipDoor = (amorphous_fn(mtmp?.data) || can_fog_fn(mtmp))
&& !engulfing_u(mtmp);
const blockedByClosed = !!(mask & D_CLOSED) && !(scanFlags & OPENDOOR);
const blockedByLocked = !!(mask & D_LOCKED) && !(scanFlags & UNLOCKDOOR);
const thrudoor = !!((scanFlags & (ALLOW_WALL | BUSTDOOR))
|| ((rockok || treeok) && (scanFlags & ALLOW_DIG)));
if ((blockedByClosed || blockedByLocked) && !thrudoor && !canSlipDoor) reject = 'door';
}
// C ref: mon.c:2268-2283 — treat both actual hero square and the
// monster's remembered hero square (mux,muy) as ALLOW_U candidates.
const atHero = (nx === (game.u?.ux ?? -1) && ny === (game.u?.uy ?? -1));
const atApparentHero = (nx === mtmp?.mux && ny === mtmp?.muy);
if (!reject && (atHero || atApparentHero) && !(scanFlags & ALLOW_U)) reject = 'nou';
// C ref: mon.c:2125 mfndpos — per-candidate info flags
let info = 0;
// C ref: mon.c MON_AT()/m_at() occupancy check.
// Use shared map_access m_at() so non-head occupancy (worm
// segments) can be seen where appropriate.
const occupant = m_at(nx, ny);
// C ref: mon.c:2248-2267 — onscary check can block movement unless
// ALLOW_SSM is present; displaced image target remaps to real hero.
let dispx = nx;
let dispy = ny;
if (Displaced() && monseeu && mtmp?.mux === nx && mtmp?.muy === ny) {
dispx = game.u?.ux ?? nx;
dispy = game.u?.uy ?? ny;
}
if (!reject && onscary(dispx, dispy, mtmp)) {
if (!(scanFlags & ALLOW_SSM)) reject = 'scary';
info |= ALLOW_SSM;
}
if (atHero || atApparentHero) {
if (atHero) {
// C updates remembered hero position immediately once the
// monster has found the real hero square.
mtmp.mux = game.u?.ux ?? mtmp.mux;
mtmp.muy = game.u?.uy ?? mtmp.muy;
}
info |= ALLOW_U;
} else {
// C ref: mon.c:2284-2301 — occupied squares can still be valid for
// displacement (ALLOW_MDISP); otherwise reject this candidate.
if (!reject && occupant) {
if (allowM) {
info |= ALLOW_M;
} else {
const mmflag = scanFlags | mm_aggression_local(mtmp, occupant);
if (mmflag & ALLOW_M) {
info |= ALLOW_M;
if (occupant.mtame) {
if (!(mmflag & ALLOW_TM)) reject = 'tm';
info |= ALLOW_TM;
}
} else {
// C mutates flag here: flag &= ~ALLOW_MDISP; this
// affects subsequent candidates in the same mfndpos scan.
scanFlags &= ~ALLOW_MDISP;
const mmflagDisp = scanFlags | mm_displacement_local(mtmp, occupant);
if (!(mmflagDisp & ALLOW_MDISP)) reject = 'occ';
info |= ALLOW_MDISP;
}
}
}
// C ref: mon.c:2300-2308 — temple sanctuary check
if (!reject && game.level?.flags?.has_temple
&& in_rooms(nx, ny, TEMPLE).length > 0
&& !(in_rooms(x, y, TEMPLE).length > 0)
&& in_your_sanctuary(null, nx, ny)) {
if (!(scanFlags & ALLOW_SANCT)) reject = 'sanct';
info |= ALLOW_SANCT;
}
}
if (!reject && monseeu
&& online2(nx, ny, mtmp?.mux, mtmp?.muy)) {
if (scanFlags & NOTONL) reject = 'notonl';
info |= NOTONL;
}
// C ref: mon.c:2310-2314 — garlic blocks undead/vampires
if (!reject && sobj_at(CLOVE_OF_GARLIC, nx, ny)) {
if (scanFlags & NOGARLIC) reject = 'garlic';
info |= NOGARLIC;
}
// C ref: mon.c:2315-2319 — boulder blocks movement unless ALLOW_ROCK
if (!reject && sobj_at(BOULDER, nx, ny)) {
if (!(scanFlags & ALLOW_ROCK)) reject = 'boulder';
info |= ALLOW_ROCK;
}
// C ref: mon.c:2325-2328 — diagonal tight squeeze check
if (!reject && nx !== x && ny !== y) {
const mdat = mtmp.data;
if (bad_rock(mdat, x, ny) && bad_rock(mdat, nx, y)
&& cant_squeeze_thru_local(mtmp)) {
reject = 'tightsqueeze';
}
}
// C ref: mon.c:2334-2350 — trap avoidance check occurs late,
// after occupancy/sanctuary/line checks.
const trap = map?.trapAt?.(nx, ny) || null;
if (!reject && trap) {
if (trap.ttyp >= TRAPNUM || trap.ttyp === 0) {
reject = 'badtrap';
// C ref: mon.c:2342-2344 — fixed teleport trap used by hero
} else if (fixed_tele_trap(trap) && hastrack(nx, ny)) {
info |= ALLOW_TRAPS;
} else if (!m_harmless_trap(mtmp, trap)) {
if (!(scanFlags & ALLOW_TRAPS)) {
if (mon_knows_traps(mtmp, trap.ttyp))
reject = 'trap';
}
info |= ALLOW_TRAPS;
}
}
if (reject) continue;
positions.push({ x: nx, y: ny, occupant, info,
allowM: !!occupant && !!(info & ALLOW_M) });
}
}
if (!positions.length && !didRetry && wantpool && !is_pool(x, y)) {
seekPool = false;
didRetry = true;
continue;
}
break;
}
pushRngLogEntry(`<mfndpos=${positions.length}`);
return positions;
}
function is_displacer_fn(ptr) {
return !!(ptr?.mflags3 & M3_DISPLACES);
}
// C ref: mon.c:mm_aggression() — subset for mfndpos occupancy gating.
function mm_aggression_local(magr, mdef) {
const pa = magr?.data;
const pd = mdef?.data;
if (!pa || !pd) return 0;
if (magr.mtame && mdef.mtame) return 0;
if ((pa.mnum === PM_PURPLE_WORM || pa.mnum === PM_BABY_PURPLE_WORM)
&& pd.mnum === PM_SHRIEKER) {
return ALLOW_M | ALLOW_TM;
}
return 0;
}
// C ref: mon.c:mm_displacement() — subset for mfndpos occupancy gating.
function mm_displacement_local(magr, mdef) {
const pa = magr?.data;
const pd = mdef?.data;
if (!pa || !pd) return 0;
if (!is_displacer_fn(pa)) return 0;
if (is_displacer_fn(pd) && (magr.m_lev ?? 0) <= (mdef.m_lev ?? 0)) return 0;
if (magr.mx !== mdef.mx && magr.my !== mdef.my && nodiag(mdef)) return 0;
if (mdef.mtrapped) return 0;
if (mdef.wormno) return 0;
if (!(is_rider(pa) || (pa.msize ?? 0) >= (pd.msize ?? 0))) return 0;
return ALLOW_MDISP;
}
// set_apparxy imported from monmove.js
function closed_door_monmove(x, y) {
const loc = game.level?.at(x, y);
if (!loc || loc.typ !== DOOR) return false;
const mask = loc.doormask ?? loc.flags ?? 0;
return !!(mask & (D_CLOSED | D_LOCKED));
}
function may_dig_monmove(x, y) {
const loc = game.level?.at(x, y);
if (!loc) return false;
return !((IS_STWALL(loc.typ) || IS_TREE(loc.typ))
&& !!(loc.wall_info & W_NONDIGGABLE));
}
function m_carrying_local(mon, otyp) {
for (let obj = mon?.minvent; obj; obj = obj.nobj) {
if (obj.otyp === otyp) return obj;
}
return null;
}
// C ref: monmove.c:97 monhaskey()
export function monhaskey(mon, for_unlocking) {
if (for_unlocking && m_carrying_local(mon, CREDIT_CARD))
return true;
return !!m_carrying_local(mon, SKELETON_KEY) || !!m_carrying_local(mon, LOCK_PICK);
}
function is_pick_otyp(otyp) {
return otyp === PICK_AXE || otyp === DWARVISH_MATTOCK;
}
function is_axe_otyp(otyp) {
return otyp === AXE || otyp === BATTLE_AXE;
}
// C ref: monmove.c:134 m_can_break_boulder()
export function m_can_break_boulder(mtmp) {
return is_rider(mtmp.data)
|| (!mtmp.mspec_used
&& (mtmp.isshk || mtmp.ispriest
|| (mtmp.data?.msound === MS_LEADER)));
}
function mon_dig_capability(mon) {
if (!needspick(mon.data)) return { rockok: true, treeok: true };
const mw = mon.mw || null;
if (mw && mw.cursed && mon.weapon_check === NO_WEAPON_WANTED) {
return { rockok: is_pick_otyp(mw.otyp), treeok: is_axe_otyp(mw.otyp) };
}
const hasShield = !!which_armor(mon, W_ARMS);
const rockok = !!m_carrying_local(mon, PICK_AXE)
|| (!!m_carrying_local(mon, DWARVISH_MATTOCK) && !hasShield);
const treeok = !!m_carrying_local(mon, AXE)
|| (!!m_carrying_local(mon, BATTLE_AXE) && !hasShield);
return { rockok, treeok };
}
function mon_wield_item_dig(mon) {
let obj = null;
const hasShield = !!which_armor(mon, W_ARMS);
switch (mon.weapon_check) {
case NEED_PICK_AXE:
obj = m_carrying_local(mon, PICK_AXE);
if (!obj && !hasShield) obj = m_carrying_local(mon, DWARVISH_MATTOCK);
break;
case NEED_AXE:
obj = m_carrying_local(mon, BATTLE_AXE);
if (!obj || hasShield) obj = m_carrying_local(mon, AXE);
break;
case NEED_PICK_OR_AXE:
obj = m_carrying_local(mon, DWARVISH_MATTOCK);
if (!obj) obj = m_carrying_local(mon, BATTLE_AXE);
if (!obj || hasShield) {
obj = m_carrying_local(mon, PICK_AXE);
if (!obj) obj = m_carrying_local(mon, AXE);
}
break;
default:
return false;
}
const mw = mon.mw || null;
if (obj) {
if (mw && mw.otyp === obj.otyp) {
mon.weapon_check = NEED_WEAPON;
return false;
}
if (mw && mwelded(mw)) {
mon.weapon_check = NO_WEAPON_WANTED;
return true;
}
if (mw) mw.owornmask &= ~W_WEP;
mon.mw = obj;
obj.owornmask = (obj.owornmask || 0) | W_WEP;
mon.weapon_check = NEED_WEAPON;
return true;
}
mon.weapon_check = NEED_WEAPON;
return false;
}
export function m_digweapon_check(mon, nix, niy) {
let can_tunnel = false;
if (!is_rogue_level(game.u?.uz))
can_tunnel = tunnels(mon.data);
const mw = mon.mw || null;
if (can_tunnel && needspick(mon.data) && !mwelded(mw)
&& (may_dig_monmove(nix, niy) || closed_door_monmove(nix, niy))) {
if (closed_door_monmove(nix, niy)) {
// C ref: monmove.c:1122 uses || here (bug-compatible).
if (!mw || !is_pick_otyp(mw.otyp) || !is_axe_otyp(mw.otyp))
mon.weapon_check = NEED_PICK_OR_AXE;
} else {
const typ = game.level?.at(nix, niy)?.typ ?? 0;
if (IS_TREE(typ)) {
if (!mw || !is_axe_otyp(mw.otyp)) mon.weapon_check = NEED_AXE;
} else if (IS_STWALL(typ)) {
if (!mw || !is_pick_otyp(mw.otyp)) mon.weapon_check = NEED_PICK_AXE;
}
}
return mon.weapon_check >= NEED_PICK_AXE && mon_wield_item_dig(mon);
}
return false;
}
// C ref: mon.c:2052 mon_allowflags() — compute movement capability flags
export function mon_allowflags(mtmp) {
const mdat = mtmp.data;
const can_open = !(nohands(mdat) || verysmall(mdat));
const can_unlock = (can_open && monhaskey(mtmp, true))
|| mtmp.iswiz || is_rider(mdat);
const doorbuster = is_giant(mdat);
// C ref: mon.c:2061-2066 — tunnel if capable and not on rogue level
let can_tunnel = tunnels(mdat) && !Is_rogue_level(game.u?.uz);
if (can_tunnel && needspick(mdat)
&& ((!mtmp.mpeaceful || Conflict())
&& dist2(mtmp.mx, mtmp.my, mtmp.mux ?? 0, mtmp.muy ?? 0) <= 8))
can_tunnel = false;
let flags = 0;
if (mtmp.mtame) {
flags |= ALLOW_M | ALLOW_TRAPS | ALLOW_SANCT | ALLOW_SSM;
} else if (mtmp.mpeaceful) {
flags |= ALLOW_SANCT | ALLOW_SSM;
} else {
flags |= ALLOW_U;
}
if (Conflict() && !resist_conflict(mtmp)) flags |= ALLOW_U;
if (mtmp.isshk) flags |= ALLOW_SSM;
if (mtmp.ispriest) flags |= ALLOW_SSM | ALLOW_SANCT;
if (passes_walls(mdat)) flags |= (ALLOW_ROCK | ALLOW_WALL);
if (throws_rocks(mdat) || m_can_break_boulder(mtmp)) flags |= ALLOW_ROCK;
if (doorbuster) flags |= BUSTDOOR;
if (passes_bars(mdat)
&& (mtmp !== game.u?.ustuck
|| (unsolid(game.youmonst?.data) || verysmall(game.youmonst?.data))))
flags |= ALLOW_BARS;
if (can_tunnel) flags |= ALLOW_DIG;
if (can_open) flags |= OPENDOOR;
if (can_unlock) flags |= UNLOCKDOOR;
// C ref: mon.c:2102-2108 — additional flags for special monster types
if (is_minion(mdat) || is_rider(mdat)) flags |= ALLOW_SANCT;
if (is_unicorn(mdat) && !noteleport_level(mtmp)) flags |= NOTONL;
if (is_human(mdat) || mdat === mons[PM_MINOTAUR]) flags |= ALLOW_SSM;
if ((is_undead(mdat) && mdat.mlet !== S_GHOST) || is_vampshifter(mtmp))
flags |= NOGARLIC;
return flags;
}
// is_rogue_level: imported from macros.js
// m_cansee: imported from mondata.js
// is_undead, is_rider, flesh_petrifies imported from mondata.js
// mon_resistancebits, resists_poison imported from mondata.js
// mon_track_add, mon_knows_traps, mon_learns_traps imported from monmove.js/mondata.js
// dog_move, dog_invent, dog_goal imported from dogmove.js (via dynamic import)
// distfleeck imported from monmove.js
async function mon_note_trap_at(mtmp, x, y) {
const trap = game.level?.trapAt?.(x, y) || null;
if (trap && !m_harmless_trap(mtmp, trap)) {
mon_learns_traps(mtmp, trap.ttyp);
// C ref: trap.c:1467 — squeaky board produces audible message
if (trap.ttyp === SQKY_BOARD) {
const noteNames = [
'C note', 'D flat', 'D note', 'E flat',
'E note', 'F note', 'F sharp', 'G note',
'G sharp', 'A note', 'B flat', 'B note',
];
const note = noteNames[(trap.tnote ?? 0) % 12] || 'C note';
const range = couldsee(mtmp.mx, mtmp.my)
? (BOLT_LIM + 1) : (BOLT_LIM - 3);
const nearby = dist2(game.u?.ux ?? 0, game.u?.uy ?? 0, mtmp.mx, mtmp.my)
<= range * range;
await pline(`You hear ${an(note)} squeak ${nearby ? 'nearby' : 'in the distance'}.`);
}
// Pit/spiked pit damage is handled by postmov/mintrap, not here.
// mon_note_trap_at handles squeaky boards and other non-damage effects.
// TODO: bear trap notification, other non-damage trap effects
}
}
// NOTE: differs from canonical monmove.js — handles PIT/SPIKED_PIT in addition to DART_TRAP
// C ref: mon.c — mcalcmove()
// Calculate movement points for a monster.
export function mcalcmove(mtmp, m_moving = false) {
if (!mtmp?.data) return NORMAL_SPEED;
// C allows mmove==0 (non-moving monsters). Preserve 0; only
// default when mmove is actually absent.
let mmove = mtmp.data.mmove ?? NORMAL_SPEED;
if (mtmp.mspeed === MSLOW) {
if (mmove < NORMAL_SPEED) {
mmove = Math.trunc((2 * mmove + 1) / 3);
} else {
mmove = 4 + Math.trunc(mmove / 3);
}
} else if (mtmp.mspeed === MFAST) {
mmove = Math.trunc((4 * mmove + 2) / 3);
}
if (m_moving) {
const mmove_adj = mmove % NORMAL_SPEED;
mmove -= mmove_adj;
if (rn2(NORMAL_SPEED) < mmove_adj) {
mmove += NORMAL_SPEED;
}
}
return mmove;
}
// C ref: monmove.c mon_regen()
function mon_regen_local(mon, digest_meal) {
if (!mon) return;
if (game.moves % 20 === 0 || regenerates(mon.data))
mon.mhp = Math.min((mon.mhp ?? 0) + 1, mon.mhpmax ?? mon.mhp ?? 0);
if ((mon.mspec_used ?? 0) > 0)
mon.mspec_used--;
if (digest_meal && (mon.meating ?? 0) > 0) {
mon.meating--;
}
}
// C ref: mon.c:4850 decide_to_shapeshift()
async function decide_to_shapeshift(mon) {
let ptr = null; // C: struct permonst *ptr = 0;
const was_female = mon.female;
let dochng = false;
if (!is_vampshifter(mon)) {
// C ref: mon.c:4858-4861 — regular shapeshifter; ptr stays null
if (!rn2(6))
dochng = true;
} else if (!(mon.mstrategy & STRAT_WAITFORU)) {
if ((mon.data?.mlet ?? 0) !== S_VAMPIRE) {
// C ref: mon.c:4870-4888 — shifted vampire
if ((mon.mhp <= Math.trunc(((mon.mhpmax ?? 0) + 5) / 6))
&& rn2(4)
&& (mon.cham ?? NON_PM) >= 0
&& (mon.cham ?? NON_PM) < mons.length) {
// Low HP: revert to base vampire form
ptr = mons[mon.cham];
dochng = true;
} else if (mon.data === mons[PM_FOG_CLOUD]
&& mon.mhp === mon.mhpmax
&& !rn2(4)
&& (!canseemon(mon)
|| mdistu(mon) > BOLT_LIM * BOLT_LIM)) {
// Fog cloud at full HP: pick a different vampire shape
const mndx = pickvampshape(mon);
if (mndx >= 0 && mndx < mons.length) {
ptr = mons[mndx];
dochng = (ptr !== mon.data);
}
}
// C ref: mon.c:4889-4896 — blocked by closed door
if (dochng && amorphous_fn(mon.data)
&& closed_door(mon.mx, mon.my)) {
// C calls enexto()+rloc_to(); rloc_to() is unported.
// For RNG parity, the key is that dochng stays true.
}
} else {
// C ref: mon.c:4897-4901 — S_VAMPIRE at good health
// 'ptr' stays null — newcham picks random shape
if (mon.mhp >= Math.trunc((9 * (mon.mhpmax ?? 0)) / 10)
&& !rn2(6)
&& (!canseemon(mon)
|| mdistu(mon) > BOLT_LIM * BOLT_LIM)) {
dochng = true; // ptr stays null
}
}
}
// C ref: mon.c:4904-4913 — execute shape change
if (dochng) {
if (await newcham(mon, ptr, NC_SHOW_MSG)) {
// C ref: mon.c:4908-4912 — vampshift: override sex change
if (is_vampshifter(mon)) {
const curptr = mon.data;
if (!is_male(curptr) && !is_female(curptr) && !is_neuter(curptr))
mon.female = was_female;
}
}
}
}
// C ref: mon.c — mcalcdistress()
// Adjust monster trap/blind/etc states between turns.
export async function mcalcdistress() {
assertNotInModal('mcalcdistress');
for (let mtmp = game.fmon; mtmp; mtmp = mtmp.nmon) {
// C: m_calcdistress() once per monster per turn
mon_regen_local(mtmp, false);
// C ref: mon.c:1177-1178 m_calcdistress — shapeshifters decide form
if ((mtmp.cham ?? NON_PM) >= 0 && (mtmp.cham ?? NON_PM) < mons.length)
await decide_to_shapeshift(mtmp);
// C ref: were.c were_change()
await were_change(mtmp);
// C: gradually time out temporary problems
if ((mtmp.mblinded ?? 0) > 0 && --mtmp.mblinded === 0)
mtmp.mcansee = 1;
if ((mtmp.mfrozen ?? 0) > 0 && --mtmp.mfrozen === 0)
mtmp.mcanmove = 1;
if ((mtmp.mfleetim ?? 0) > 0 && --mtmp.mfleetim === 0)
mtmp.mflee = 0;
}
}
// ── monflee: make monster flee ──
// C ref: monmove.c:462
// RNG: rn2(25) for vrock stench cloud
export async function monflee(mtmp, fleetime, first, fleemsg) {
if (!mtmp || mtmp.dead) return;
if (game.u?.ustuck === mtmp) {
unstuck_fn(mtmp);
}
if (!first || !mtmp.mflee) {
if (!fleetime)
mtmp.mfleetim = 0;
else if (!mtmp.mflee || mtmp.mfleetim) {
fleetime += (mtmp.mfleetim || 0);
if (fleetime === 1) fleetime++;
mtmp.mfleetim = Math.min(fleetime, 127);
}
if (!mtmp.mflee && fleemsg && canseemon(mtmp)
&& M_AP_TYPE(mtmp) !== M_AP_FURNITURE
&& M_AP_TYPE(mtmp) !== M_AP_OBJECT) {
if (!mtmp.mcanmove || !mtmp.data?.mmove) {
await pline_mon_fn(mtmp, `${Adjmonnam(mtmp, 'immobile')} seems to flinch.`);
} else {
await pline_mon_fn(mtmp, `${Monnam(mtmp)} turns to flee.`);
}
}
// C: message + vrock stench check
// vrock: rn2(25) — this is core RNG
// PM_VROCK imported from monsters.js
if (mtmp.data === mons[PM_VROCK] && !mtmp.mspec_used) {
mtmp.mspec_used = 75 + rn2(25);
// C ref: monmove.c:522 await monflee() — vrocks emit a noxious cloud
// as part of fleeing.
void await create_gas_cloud(mtmp.mx, mtmp.my, 5, 8);
}
mtmp.mflee = 1;
}
// C: mon_track_clear(mtmp) — clear tracking history when fleeing
mon_track_clear(mtmp);
}
function wake_nearto_core(x, y, distance, petcall) {
const g = game;
for (let mtmp = g.fmon; mtmp; mtmp = mtmp.nmon) {
if (mtmp.dead) continue;
if (distance !== 0 && dist2(mtmp.mx, mtmp.my, x, y) >= distance) continue;
mtmp.msleeping = 0;
if ((mtmp.data?.geno & G_UNIQ) === 0)
mtmp.mstrategy = (mtmp.mstrategy || 0) & ~STRAT_WAITMASK;
if (g.context?.mon_moving || !petcall) continue;
if (mtmp.mtame) {
if (!mtmp.isminion && mtmp.mextra?.edog)
mtmp.mextra.edog.whistletime = g.moves || 0;
mon_track_clear(mtmp);
}
}
// C also calls disturb_buried_zombies(x, y).
}
// ── wake_nearto: wake monsters near a position ──
// C ref: mon.c:4381
// No core RNG.
export function wake_nearto(x, y, distance) {
wake_nearto_core(x, y, distance, false);
}
// ── wake_nearby: wake monsters near hero ──
// C ref: mon.c:4346
export function wake_nearby(petcall) {
wake_nearto_core(game.u.ux, game.u.uy, (game.u.ulevel || 1) * 20, !!petcall);
}
// ── wakeup: wake a specific monster ──
// C ref: mon.c:4350
// No core RNG.
export function wakeup(mtmp, via_attack) {
if (!mtmp) return;
mtmp.msleeping = 0;
if (mtmp.m_ap_type) {
// seemimic
mtmp.m_ap_type = 0;
mtmp.mappearance = 0;
}
if (via_attack && mtmp.mcanmove && !mtmp.mconf)
mtmp.mstrategy &= ~STRAT_WAITMASK;
}
// ── setmangry: make a peaceful monster angry ──
// C ref: mon.c:4252
export async function setmangry(mtmp, via_attack) {
if (!mtmp) return;
const g = game;
// C ref: mon.c:4254-4271 — Elbereth hypocrisy check
// (simplified: skip Elbereth check for now)
// C ref: mon.c:4275
mtmp.mstrategy = (mtmp.mstrategy ?? 0) & ~STRAT_WAITMASK;
if (!mtmp.mpeaceful)
return;
if (mtmp.mtame)
return;
mtmp.mpeaceful = 0;
// C ref: mon.c:4284-4290 — alignment penalty for attacking peacefuls
if (mtmp.ispriest) {
if (p_coaligned(mtmp))
adjalign(-5);
else
adjalign(2);
} else {
adjalign(-1);
}
// C ref: mon.c:4291-4296 — anger message
if (humanoid(mtmp.data) || mtmp.isshk || mtmp.isgd) {
if (couldsee(mtmp.mx, mtmp.my))
await pline(`${Monnam(mtmp)} gets angry!`);
} else {
await growl(mtmp);
}
// C ref: mon.c:4303-4304 — peacefuls_respond
// TODO: port peacefuls_respond
}
// ── see_monsters: update display for all visible monsters ──
// C ref: display.c
// No core RNG.
export function see_monsters() {
// C ref: display.c:1478 see_monsters()
// Iterate all monsters and call newsym at their positions.
for (let mon = game.fmon; mon; mon = mon.nmon) {
if (DEADMONSTER(mon)) continue;
newsym(mon.mx, mon.my);
}
// Also newsym the hero position to show the hero glyph
if (game.u?.ux) newsym(game.u.ux, game.u.uy);
}
// ── update_inventory: signal that inventory display needs refresh ──
// C ref: invent.c
// No RNG, display only.
export function update_inventory() {
// TODO: trigger perm_invent update in browser mode
}
// stop_occupation already defined above (line 234)
// ============================================================================
// mondead / mondied — moved from bottleneck.js
// C ref: mon.c — mondead(), mondied()
// ============================================================================
// Imports for mondead/mondied are at top of file (added to existing imports).
// bigmonst, is_golem from mondata.js; G_FREQ etc from monsters.js;
// CORPSTAT_* from const.js — all added via top-level import edits.
function verysmall_mondied(mdat) {
return (mdat?.msize ?? MZ_SMALL) < MZ_SMALL;
}
export function corpse_chance(mon) {
const mdat = mon?.data;
if (!mon || !mdat) return false;
if (level_specific_nocorpse(mdat))
return false;
if (mon.mnum === PM_VLAD_THE_IMPALER || mdat.mlet === S_LICH) return false;
if (((bigmonst(mdat) || mon.mnum === PM_LIZARD) && !mon.mcloned)
|| is_golem(mdat) || is_rider(mdat) || mon.isshk) return true;
const tmp = 2 + ((mdat.geno & G_FREQ) < 2 ? 1 : 0) + (verysmall_mondied(mdat) ? 1 : 0);
return !rn2(tmp);
}
export function level_specific_nocorpse(mdat) {
if (Is_rogue_level(game.u?.uz))
return true;
if (game.level?.flags?.deathdrops === false)
return true;
if (game.level?.flags?.graveyard && is_undead(mdat) && rn2(3))
return true;
return false;
}
// C ref: mon.c — mondead()
// All monster deaths. Event: ^die[mnum@x,y]
export function mondead(mon) {
if (!mon) return;
const mx = mon.mx, my = mon.my;
pushRngLogEntry(`^die[${formatMonsterEventRef(mon, mon.mnum, mx, my)}]`);
// C ref: mon.c mondead() increments mvitals[mndx].died up to 255.
const mndx = monsndx(mon.data);
if (mndx >= 0) {
if (!Array.isArray(game.mvitals))
game.mvitals = [];
if (!game.mvitals[mndx])
game.mvitals[mndx] = { mvflags: 0, born: 0, died: 0 };
const died = game.mvitals[mndx].died || 0;
if (died < 255)
game.mvitals[mndx].died = died + 1;
}
// C ref: mon.c mondead() — if the map shows GLYPH_INVISIBLE here,
// clear it back to underlying terrain/object state before detaching.
if (glyph_is_invisible(glyph_at(mx, my)))
unmap_object(mx, my);
// C ref: mon.c m_detach() -> unstuck(mon)
unstuck_fn(mon);
// C ref: mon.c m_detach(..., due_to_death=TRUE) -> relobj(mtmp, 1, FALSE).
// Drop all carried objects at death location before redraw, emitting
// canonical ^place/^drop events in inventory-chain order.
while (mon.minvent) {
const otmp = mon.minvent;
mon.minvent = otmp.nobj || null;
otmp.nobj = null;
otmp.where = 0;
otmp.owornmask = 0;
place_object(otmp, mx, my);
stackobj(otmp);
pushRngLogEntry(`^drop[${formatMonsterEventRef(mon, monsndx(mon.data), mx, my)},${otmp.otyp}]`);
}
// C ref: mon.c mondead() sets mhp=0 and keeps dead monsters on fmon
// until dmonsfree() at turn cleanup.
mon.mhp = 0;
mon.dead = true;
if (game.level) {
// m_detach clears map occupancy, but does not immediately remove the
// monster from fmon/level chain. Clear all monMap cells pointing to
// this monster (head + possible worm-segment occupancy).
const monMap = game.level.monMap;
if (Array.isArray(monMap)) {
for (let x = 0; x < monMap.length; x++) {
const col = monMap[x];
if (!col) continue;
for (let y = 0; y < col.length; y++) {
if (col[y] === mon) col[y] = null;
}
}
}
}
// C ref: mon.c:2703 mon_leaving_level → newsym(mx, my)
// Redraw the cell so the dead monster's glyph disappears.
if (isok(mx, my)) newsym(mx, my);
}
// C ref: mon.c:3942 mnexto — move monster to nearby location
export async function mnexto(mtmp, rlocflags) {
if (mtmp === game.u?.usteed) {
mtmp.mx = game.u.ux;
mtmp.my = game.u.uy;
return;
}
const mm = {};
if (!enexto(mm, game.u.ux, game.u.uy, mtmp.data) || !isok(mm.x, mm.y)) {
// deal_with_overcrowding — just remove for now
mtmp.dead = true;
return;
}
await rloc_to_flag(mtmp, mm.x, mm.y, rlocflags);
}
// C ref: mon.c mongone — remove monster from game
export function mongone(mtmp) {
if (!mtmp) return;
// C ref: steal.c:851 mdrop_special_objs(mon) as called by mon.c:mongone.
// This consumes obj_resists RNG for each carried object and preserves
// special items by dropping them instead of deleting them.
for (let obj = mtmp.minvent; obj; ) {
const next = obj.nobj || null;
if (obj_resists(obj, 0, 0) || is_quest_artifact(obj)) {
if (mtmp.mx) {
const omx = mtmp.mx;
const omy = mtmp.my;
obj_extract_self(obj);
obj.owornmask = 0;
place_object(obj, omx, omy);
stackobj(obj);
pushRngLogEntry(`^drop[${formatMonsterEventRef(mtmp, monsndx(mtmp.data), omx, omy)},${obj.otyp}]`);
} else {
// C calls rloco() for off-map monsters. Keep object alive
// rather than deleting it so the protected-item semantics
// remain C-shaped for mongone() callers.
obj_extract_self(obj);
obj.owornmask = 0;
}
}
obj = next;
}
// C ref: mon.c:mongone() -> m_detach(..., FALSE):
// mark gone (mhp=0), detach from map occupancy, but don't unlink from
// fmon chain yet (dmonsfree handles deferred removal).
const mx = mtmp.mx, my = mtmp.my;
mtmp.mhp = 0;
mtmp.dead = true;
if (game.level?.monMap) {
for (let x = 0; x < game.level.monMap.length; x++) {
const col = game.level.monMap[x];
if (!col) continue;
for (let y = 0; y < col.length; y++) {
if (col[y] === mtmp) col[y] = null;
}
}
}
// C ref: mkobj.c:discard_minvent(mtmp, FALSE) from mon.c:mongone().
// Release remaining inventory from game via object teardown.
for (let obj = mtmp.minvent; obj; ) {
const next = obj.nobj || null;
obj_extract_self(obj);
obj.owornmask = 0;
obfree(obj, null);
obj = next;
}
mtmp.minvent = null;
// C ref: m_detach → mon_leaving_level → newsym(mx, my)
if (isok(mx, my)) newsym(mx, my);
}
// C ref: hack.h — age when corpses go bad
const TAINT_AGE = 50;
// C ref: mon.c:546 make_corpse() — create corpse or special leavings.
// Returns the created object (may be null).
export async function make_corpse(mtmp, corpseflags) {
if (!mtmp || !mtmp.data) return null;
const mdat = mtmp.data;
const mndx = monsndx(mdat);
const x = mtmp.mx;
const y = mtmp.my;
let corpstatflags = corpseflags | 0;
let obj = null;
if (mtmp.female) corpstatflags |= CORPSTAT_FEMALE;
else if (!is_neuter(mdat)) corpstatflags |= CORPSTAT_MALE;
// Zombies/mummies → old corpse of living counterpart (regardless of G_NOCORPSE).
// C ref: mon.c:611-630 — PM_*_MUMMY / PM_*_ZOMBIE cases.
const undeadCases = new Set([
PM_KOBOLD_MUMMY, PM_DWARF_MUMMY, PM_GNOME_MUMMY, PM_ORC_MUMMY,
PM_ELF_MUMMY, PM_HUMAN_MUMMY, PM_GIANT_MUMMY, PM_ETTIN_MUMMY,
PM_KOBOLD_ZOMBIE, PM_DWARF_ZOMBIE, PM_GNOME_ZOMBIE, PM_ORC_ZOMBIE,
PM_ELF_ZOMBIE, PM_HUMAN_ZOMBIE, PM_GIANT_ZOMBIE, PM_ETTIN_ZOMBIE,
]);
if (undeadCases.has(mndx)) {
const num = undead_to_corpse(mndx);
corpstatflags |= CORPSTAT_INIT;
obj = mkcorpstat(CORPSE, mtmp, mons[num], x, y, corpstatflags);
if (obj) obj.age -= (TAINT_AGE + 1);
return obj;
}
// Vampires → old corpse of living human. C ref: mon.c:603-610.
if (mndx === PM_VAMPIRE || mndx === PM_VAMPIRE_LEADER) {
const num = undead_to_corpse(mndx);
corpstatflags |= CORPSTAT_INIT;
obj = mkcorpstat(CORPSE, mtmp, mons[num], x, y, corpstatflags);
if (obj) obj.age -= (TAINT_AGE + 1);
return obj;
}
// Default: G_NOCORPSE gate, then mkcorpstat.
// C ref: mon.c:874 default_1 branch.
const mflags = (mndx >= 0 && game.mvitals?.[mndx]) ? (game.mvitals[mndx].mvflags || 0) : 0;
if (mflags & G_NOCORPSE) return null;
corpstatflags |= CORPSTAT_INIT;
obj = mkcorpstat(CORPSE, mtmp, mdat, x, y, corpstatflags);
return obj;
}
// C ref: mon.c — mondied()
// Death + corpse creation.
export async function mondied(mon) {
if (!mon) return;
mondead(mon);
if (!mon.dead) return;
const x = mon.mx;
const y = mon.my;
const loc = isok(x, y) ? levl(x, y) : null;
const cc = corpse_chance(mon);
if (!cc || !(loc && (ACCESSIBLE(loc.typ) || IS_POOL(loc.typ)))) return;
await make_corpse(mon, CORPSTAT_NONE);
}
// ============================================================================
// Monster naming functions (port of do_name.c x_monnam family)
// ============================================================================
//
// These are the canonical exported implementations. Local stubs in other
// files should be replaced with imports from this module.
// Additional imports for monster naming functions.
// (Some symbols may already be imported above; ES6 allows duplicate import
// statements for the same module, but not duplicate bindings. We import
// only symbols not already bound at the top of this file.)
import {
G_UNIQ, M1_MINDLESS, M2_NEUTER,
PM_WIZARD_OF_YENDOR, PM_GHOST, PM_SHOPKEEPER,
} from './monsters.js';
import { Hallucination, Blind } from './macros.js';
import { type_is_pname, pmname, rndmonnam, bogon_is_pname } from './do_name.js';
import { shkname } from './shk.js';
import { You, You_see, pline, pline_mon, set_msg_xy } from './pline.js';
import { deferred_goto } from './allmain.js';
import { castmu } from './mcastu.js';
import { were_change } from './were.js';
import { rloc_to_flag } from './teleport.js';
import { mhitm_knockback, xkilled } from './uhitm.js';
import { maybe_finished_meal } from './eat.js';
import { glyph_is_invisible } from './glyph.js';
// C ref: mondata.c — pronoun_gender()
// Returns 0=male, 1=female, 2=it, 3=they (hallu random)
const GENDER_TABLE = [
{ he: 'he', him: 'him', his: 'his', himself: 'himself' }, // 0 = male
{ he: 'she', him: 'her', his: 'her', himself: 'herself' }, // 1 = female
{ he: 'it', him: 'it', his: 'its', himself: 'itself' }, // 2 = neuter
{ he: 'they', him: 'them', his: 'their', himself: 'themselves'}, // 3 = plural/hallu
];
// C ref: you.h — mhe/mhim/mhis macros
const PRONOUN_HALLU = 2;
export function mhe(mtmp) { return GENDER_TABLE[pronoun_gender(mtmp, PRONOUN_HALLU)].he; }
export function mhim(mtmp) { return GENDER_TABLE[pronoun_gender(mtmp, PRONOUN_HALLU)].him; }
export function mhis(mtmp) { return GENDER_TABLE[pronoun_gender(mtmp, PRONOUN_HALLU)].his; }
export function mhimself(mtmp) { return GENDER_TABLE[pronoun_gender(mtmp, PRONOUN_HALLU)].himself; }
// C ref: youprop.h — uhe/uhim/uhis macros
export function uhe() { return GENDER_TABLE[u_pronoun_gender()].he; }
export function uhim() { return GENDER_TABLE[u_pronoun_gender()].him; }
export function uhis() { return GENDER_TABLE[u_pronoun_gender()].his; }
export function uhimself() { return GENDER_TABLE[u_pronoun_gender()].himself; }
// C ref: hack.h — #define m_next2u(mtmp) monnear(mtmp, u.ux, u.uy)
export function m_next2u(mtmp) {
return monnear(mtmp, game.u?.ux ?? 0, game.u?.uy ?? 0);
}
// C ref: mon.c:3400 set_ustuck() — set or clear stuck monster
export function set_ustuck(mtmp) {
setBotl('set_ustuck');
game.u.ustuck = mtmp;
if (!mtmp) {
game.u.uswallow = 0;
game.u.uswldtim = 0;
}
}
// C ref: mon.c:3417 unstuck() — release hero from monster's grasp/swallow
// Simplified: full C version handles unswallowing, vision recalc, docrt.
export function unstuck(mtmp) {
if (game.u?.ustuck === mtmp) {
set_ustuck(null);
}
}
// C ref: mon.c:928 minliquid() — check if monster drowns in water/lava
export async function minliquid(mtmp) {
game.iflags = game.iflags || {};
game.iflags.sad_feeling = !!(mtmp.mtame && !canspotmon(mtmp));
const res = await minliquid_core(mtmp);
game.iflags.sad_feeling = false;
return res;
}
// C ref: mon.c:942 minliquid_core() — guts of minliquid
async function minliquid_core(mtmp) {
const inpool = is_pool(mtmp.mx, mtmp.my)
&& (!(is_flyer(mtmp.data) || is_floater(mtmp.data))
|| Is_waterlevel(game.u?.uz));
const inlava = is_lava(mtmp.mx, mtmp.my)
&& !(is_flyer(mtmp.data) || is_floater(mtmp.data));
const loc = levl(mtmp.mx, mtmp.my);
const infountain = loc && loc.typ === IS_FOUNTAIN_TYP;
// C ref: mon.c:961 — steed flying/levitating
if (mtmp === game.u?.usteed && (Flying_fn() || Levitation_fn()))
return 0;
// C ref: mon.c:968 — gremlin multiplication in pool/fountain
if (mtmp.data === mons[PM_GREMLIN] && (inpool || infountain) && rn2(3)) {
// split_mon not fully ported — consume rn2(3) only
return 0;
// C ref: mon.c:974 — iron golem rust in pool
} else if (mtmp.data === mons[PM_IRON_GOLEM] && inpool && !rn2(5)) {
const dam = c_d(2, 6);
if (cansee(mtmp.mx, mtmp.my))
await pline(`${Monnam(mtmp)} rusts.`);
mtmp.mhp -= dam;
if ((mtmp.mhpmax || 0) > dam)
mtmp.mhpmax -= dam;
if (mtmp.mhp <= 0) {
await mondied(mtmp);
if (mtmp.mhp <= 0) return 1;
}
return 0;
}
if (inlava) {
// C ref: mon.c:997-1048 — lava effects
if (!is_clinger(mtmp.data) && !likes_lava(mtmp.data)) {
if (can_teleport_fn(mtmp.data)) {
// rloc(mtmp) — simplified: just consume RNG
return 0;
}
if (!resists_fire_fn(mtmp.data)) {
if (cansee(mtmp.mx, mtmp.my))
await pline(`${Monnam(mtmp)} burns to a crisp.`);
if (game.context?.mon_moving)
await mondead(mtmp);
else
await xkilled(mtmp, 0);
} else {
mtmp.mhp -= 1;
if (mtmp.mhp <= 0) {
if (cansee(mtmp.mx, mtmp.my))
await pline(`${Monnam(mtmp)} surrenders to the fire.`);
await mondead(mtmp);
}
}
if (mtmp.mhp > 0) return 0;
return 1;
}
} else if (inpool) {
// C ref: mon.c:1049-1091 — drowning
if (!is_clinger(mtmp.data) && !cant_drown_fn(mtmp.data)) {
if (can_teleport_fn(mtmp.data)) {
// rloc(mtmp) — simplified
return 0;
}
if (cansee(mtmp.mx, mtmp.my)) {
if (game.context?.mon_moving)
await pline(`${Monnam(mtmp)} drowns.`);
else
await You(`drown ${mon_nam(mtmp)}.`);
}
if (game.context?.mon_moving)
await mondied(mtmp);
else
await xkilled(mtmp, 0);
if (mtmp.mhp > 0) return 0;
return 1;
}
} else {
// C ref: mon.c:1093-1100 — eels out of water
if ((mtmp.data?.mlet ?? 0) === S_EEL && !Is_waterlevel(game.u?.uz)
&& !breathless_fn(mtmp.data)) {
if (mtmp.mhp > 1 && rn2(mtmp.mhp) > rn2(8))
mtmp.mhp--;
await monflee(mtmp, 2, false, false);
}
}
return 0;
}
// Helper constants/functions for minliquid
const IS_FOUNTAIN_TYP = 15; // FOUNTAIN typ value
function Flying_fn() { return !!(game.u?.uprops?.[FLYING]?.intrinsic); }
function Levitation_fn() { return !!(game.u?.uprops?.[LEVITATION]?.intrinsic); }
function can_teleport_fn(data) { return !!(data?.mflags1 & 0x00000200); } // M1_TPORT
function resists_fire_fn(data) { return !!(data?.mresists & 0x01); } // MR_FIRE
function cant_drown_fn(data) {
return is_swimmer(data) || amphibious_fn(data) || breathless_fn(data);
}
function breathless_fn(data) { return !!(data?.mflags1 & 0x04000000); } // M1_BREATHLESS
function amphibious_fn(data) { return !!(data?.mflags1 & 0x00000800); } // M1_AMPHIBIOUS
// C ref: mon.c — m_in_air()
export function m_in_air(mtmp) {
if (!mtmp) return false;
return (is_flyer(mtmp.data)
|| is_floater(mtmp.data)
|| (is_clinger(mtmp.data)
&& has_ceiling(game.u?.uz) && mtmp.mundetected));
}
// C ref: monst.h — #define DEADMONSTER(mon) ((mon)->mhp < 1)
export function DEADMONSTER(mon) {
return mon && (mon.mhp < 1 || mon.dead);
}
// C ref: mon.c — healmon()
export function healmon(mtmp, amt, overheal) {
// For youmonst case, we would call healup — skip for now (rare path)
if (!mtmp) return 0;
const oldhp = mtmp.mhp;
if (mtmp.mhp + amt > mtmp.mhpmax + overheal) {
mtmp.mhpmax += overheal;
mtmp.mhp = mtmp.mhpmax;
} else {
mtmp.mhp += amt;
if (mtmp.mhp > mtmp.mhpmax)
mtmp.mhpmax = mtmp.mhp;
}
return mtmp.mhp - oldhp;
}
// ============================================================
// newcham and helpers — C ref: mon.c
// ============================================================
// C ref: mon.c:4968 isspecmon()
function isspecmon(mon) {
return !!(mon.isshk || mon.ispriest || mon.isgd
|| (mon.m_id && game?.quest_status?.leader_m_id === mon.m_id));
}
// C ref: mondata.h — polyok(): M2_NOPOLY check
// polyok: imported from mondata.js
export function is_mplayer(ptr) {
// Player monsters range from PM_ARCHEOLOGIST..PM_WIZARD
if (!ptr) return false;
const mndx = monsndx(ptr);
return mndx >= PM_ARCHEOLOGIST && mndx <= PM_WIZARD;
}
// C ref: mondata.h — is_placeholder()
function is_placeholder_fn(mndx) {
return mndx === PM_ORC || mndx === PM_GIANT
|| mndx === PM_ELF || mndx === PM_HUMAN;
}
// C ref: makemon.c — pm_to_cham()
export function pm_to_cham(mndx) {
return (mndx >= 0 && mndx < mons.length && is_shapeshifter(mons[mndx])) ? mndx : NON_PM;
}
// C ref: makemon.c:1542 mbirth_limit()
function mbirth_limit_fn(mndx) {
return mndx === PM_NAZGUL ? 9 : mndx === PM_ERINYS ? 3 : MAXMONNO;
}
// C ref: mon.c:5241 mgender_from_permonst()
export function mgender_from_permonst(mtmp, mdat) {
if (is_male(mdat)) {
mtmp.female = false;
} else if (is_female(mdat)) {
mtmp.female = true;
} else if (!is_neuter(mdat)) {
// same chance to change as polymorphing hero;
// vampires use controlled shapechange and don't undergo gender change
if (!rn2(10) && !(is_vampire(mdat) || is_vampshifter(mtmp)))
mtmp.female = !mtmp.female;
}
}
// C ref: mon.c:4978 validspecmon()
function validspecmon(mon, mndx) {
if (mndx === NON_PM)
return true; // caller wants random
if (!accept_newcham_form(mon, mndx))
return false; // geno'd or !polyok
if (isspecmon(mon)) {
const ptr = mons[mndx];
// reject notake because object manipulation is expected
// and nohead because speech capability is expected
if (notake(ptr) || !has_head(ptr))
return false;
}
return true;
}
// C ref: mon.c:4842 pick_animal()
// Builds animal list lazily and picks a random animal.
function pick_animal() {
// Build the animal list if not yet done
if (!game._animal_list) {
const list = [];
for (let i = LOW_PM; i < SPECIAL_PM; i++) {
if (is_animal(mons[i]))
list.push(i);
}
game._animal_list = list;
}
const list = game._animal_list;
if (!list.length) return NON_PM;
let res = list[rn2(list.length)];
// rogue level should use monsters represented by uppercase letters only
if (Is_rogue_level(game.u?.uz) && !isupper_fn(monsym_fn(mons[res])))
res = list[rn2(list.length)];
return res;
}
// C ref: mhitm.c:597 — check for failed grab due to unsolid defender
export async function failed_grab(magr, mdef, mattk) {
const unsolid_data = noncorporeal(mdef.data) || amorphous(mdef.data);
if ((unsolid_data || game.notonhead)
&& (mattk.aatyp === AT_HUGS || mattk.adtyp === AD_WRAP
|| mattk.adtyp === AD_STCK || mattk.adtyp === AD_DGST)) {
if ((game.vis && canspotmon(mdef))
|| magr === game.youmonst || mdef === game.youmonst) {
const verb = (mattk.adtyp === AD_DGST) ? "gulp"
: (mattk.adtyp === AD_STCK) ? "adhere"
: "grab";
const magrnam = (magr === game.youmonst) ? "Your"
: s_suffix(Monnam(magr));
let mdefnam;
if (!game.notonhead) {
mdefnam = (mdef === game.youmonst) ? "you" : mon_nam(mdef);
} else {
mdefnam = s_suffix(mon_nam(mdef)) + " tail";
}
await pline(`${magrnam} ${verb} attempt ${!game.notonhead ? "passes right through" : "fails to hold"} ${mdefnam}!`);
}
return true;
}
return false;
}
// C: itsstuck(mtmp) — check if hero is stuck to monster
export function itsstuck(mtmp) {
return game.u.ustuck === mtmp && !game.u.uswallow && sticks(game.player.data);
}
// C: mswingsm(magr, mdef, mwep) — message for monster swinging weapon at monster
export async function mswingsm(magr, mdef, mwep) {
if (game.vis && canseemon(magr) && mwep) {
let weapon_name = (mwep.otyp === RUBBER_HOSE) ? "hose" : xname(mwep);
if (mwep.quan > 1) weapon_name = makeplural(weapon_name);
await pline(`${Monnam(magr)} swings ${mhis(magr)} ${weapon_name} at ${mon_nam(mdef)}.`);
}
}
// C: mon_wield_item(mtmp) — monster tries to wield a better weapon
export async function mon_wield_item(mtmp) {
// C ref: weapon.c:801 mon_wield_item()
if (!mtmp || mtmp.weapon_check === NO_WEAPON_WANTED)
return 0;
let obj = null;
let exclaim = true; // assume mon is planning to attack
switch (mtmp.weapon_check) {
case NEED_HTH_WEAPON:
// C select_hwep() equivalent: choose a wieldable non-ammo hand weapon.
{
const strong = strongmonst(mtmp.data);
const wearingShield = !!which_armor(mtmp, W_ARMS);
for (let otmp = mtmp.minvent; otmp; otmp = otmp.nobj) {
if (otmp.oclass !== WEAPON_CLASS)
continue;
const skill = game.objects?.[otmp.otyp]?.oc_skill ?? 0;
if (skill < 0) continue; // ammo
// Launchers are ranged-only in C select_hwep().
if (skill >= 20 && skill <= 22) continue;
// C ref: weapon.c select_hwep(): bimanual hand weapons require
// a strong monster and no shield.
if ((game.objects?.[otmp.otyp]?.oc_bimanual ?? false)
&& !(strong && !wearingShield))
continue;
obj = otmp;
break;
}
}
break;
case NEED_RANGED_WEAPON:
await select_rwep(mtmp);
obj = game.propellor || null;
if (obj && obj._sentinel === 'hands')
obj = null;
break;
case NEED_PICK_AXE:
obj = m_carrying_local(mtmp, PICK_AXE);
if (!obj && !which_armor(mtmp, W_ARMS))
obj = m_carrying_local(mtmp, DWARVISH_MATTOCK);
exclaim = false;
break;
case NEED_AXE:
obj = m_carrying_local(mtmp, BATTLE_AXE);
if (!obj || which_armor(mtmp, W_ARMS))
obj = m_carrying_local(mtmp, AXE);
exclaim = false;
break;
case NEED_PICK_OR_AXE:
obj = m_carrying_local(mtmp, DWARVISH_MATTOCK);
if (!obj)
obj = m_carrying_local(mtmp, BATTLE_AXE);
if (!obj || which_armor(mtmp, W_ARMS)) {
obj = m_carrying_local(mtmp, PICK_AXE);
if (!obj)
obj = m_carrying_local(mtmp, AXE);
}
exclaim = false;
break;
default:
return 0;
}
if (obj) {
const mw = mtmp.mw || null;
if (mw && mw.otyp === obj.otyp) {
mtmp.weapon_check = NEED_WEAPON;
return 0;
}
if (mw && mwelded(mw)) {
mtmp.weapon_check = NO_WEAPON_WANTED;
return 1;
}
mtmp.mw = obj;
setmnotwielded(mtmp, mw);
mtmp.weapon_check = NEED_WEAPON;
if (canseemon(mtmp)) {
await pline_mon(mtmp, `%s wields %s${exclaim ? '!' : '.'}`, Monnam(mtmp), doname(obj));
// C temporarily sets W_WEP before mwelded() test, then clears it.
obj.owornmask |= W_WEP;
const newlyWelded = mwelded(obj);
obj.owornmask &= ~W_WEP;
if (newlyWelded) {
await pline('%s %s to %s hand!', Tobjnam(obj, 'weld'),
is_plural(obj) ? 'themselves' : 'itself',
s_suffix(mon_nam(mtmp)));
obj.bknown = 1;
}
}
obj.owornmask = W_WEP;
return 1;
}
mtmp.weapon_check = NEED_WEAPON;
return 0;
}
// C: golemeffects(mtmp, adtype, dam) — golem healing from damage types
export async function golemeffects(mtmp, adtype, dam) {
// TODO: port healing logic from mon.c
}
// C: monstone(mtmp) — monster is petrified
export function monstone(mtmp) {
// TODO: port from mon.c
}
// C: mon_to_stone(mtmp) — monster polymorphs to stone
export function mon_to_stone(mtmp) {
// TODO: port from were.c
}
// C: drain_item(obj, by_you) — item is drained of charges/levels
// C ref: zap.c:1382 drain_item
export function drain_item(obj, by_you) {
if (!obj) return;
// Simplified drain: port of Teleport zap.js logic
if (obj.spe > 0) obj.spe--;
}
// C: acid_damage(obj) — item is damaged by acid
export function acid_damage(obj) {
// TODO: port from mhitu.c/mhitm.c
}
// C: aggravate() — aggravate monsters
export function aggravate() {
// TODO: port from mon.c
}
// C ref: mon.c:4101 — m_respond(mtmp)
// Monster responds to player action; called from dochug and zap paths.
export async function m_respond(mtmp) {
const data = mtmp?.data;
if (!data) return;
// shrieker: shriek, maybe summon, aggravate
if (data.msound === MS_SHRIEK && monnear(mtmp, game.u.ux, game.u.uy)) {
if (!Deaf()) {
await pline(`${Monnam(mtmp)} shrieks.`);
await stop_occupation();
}
if (!rn2(10)) {
// C uses montoostrong() fallback to baby worm for low levels.
// JS currently keeps the same RNG shape and picks random-or-worm.
await makemon(rn2(13) ? null : mons[PM_PURPLE_WORM], 0, 0, 0);
}
aggravate();
}
// medusa: gaze at hero if visible
if (data === mons[PM_MEDUSA] && couldsee(mtmp.mx, mtmp.my)) {
const attacks = data.mattk || [];
for (const atk of attacks) {
if (atk?.aatyp === AT_GAZE) {
await gazemu(mtmp, atk);
break;
}
}
}
// C ref: mon.c:4119 — Erinyes aggravate nearby monsters
if (data === mons[PM_ERINYS] && !mtmp.mpeaceful && m_canseeu(mtmp))
aggravate();
}
// C: impact_disturbs_zombies(obj, verbose) — check if impact disturbs nearby zombies
export function impact_disturbs_zombies(obj, verbose) {
// TODO: port from mon.c
}
// C: zombie_maker(mtmp) — check if monster is a zombie maker
export function zombie_maker(mtmp) {
// TODO: port from mon.c
return false;
}
// C: zombie_form(ptr) — get zombie form for monster type
export function zombie_form(ptr) {
// TODO: port from mon.c
return -1; // NON_PM
}
// C: troll_baned(mdef, mwep) — check if monster is a troll being hit by Trollsbane
export function troll_baned(mdef, mwep) {
// TODO: port from mhitm.c
return false;
}
// C: unturn_dead(mon) — un-turn undead monsters
export function unturn_dead(mon) {
// TODO: port from zap.c
return 0;
}
// C ref: mon.c:5122 select_newcham_form()
export function select_newcham_form(mon) {
let mndx = NON_PM;
let tryct;
switch (mon.cham) {
case PM_SANDESTIN:
if (rn2(7))
mndx = pick_nasty(mons[PM_ARCHON].difficulty - 1);
break;
case PM_DOPPELGANGER:
if (!rn2(7)) {
mndx = pick_nasty(mons[PM_JABBERWOCK].difficulty - 1);
} else if (rn2(3)) { // role monsters
mndx = tt_doppel_stub(mon);
} else if (!rn2(3)) { // quest guardians
mndx = rn1(PM_APPRENTICE - PM_STUDENT + 1, PM_STUDENT);
// avoid own role's guardian
if (mndx === (game.urole?.guardnum ?? -1))
mndx = NON_PM;
} else { // general humanoids
tryct = 5;
do {
mndx = rn1(SPECIAL_PM - LOW_PM, LOW_PM);
if (humanoid(mons[mndx]) && polyok(mons[mndx]))
break;
} while (--tryct > 0);
if (!tryct)
mndx = NON_PM;
}
break;
case PM_CHAMELEON:
if (!rn2(3))
mndx = pick_animal();
break;
case PM_VLAD_THE_IMPALER:
case PM_VAMPIRE_LEADER:
case PM_VAMPIRE:
mndx = pickvampshape(mon);
break;
case NON_PM: // ordinary
{
const m_armr = which_armor(mon, W_ARM);
if (m_armr && Is_dragon_scales(m_armr))
mndx = Dragon_scales_to_pm(m_armr);
else if (m_armr && Is_dragon_mail(m_armr))
mndx = Dragon_mail_to_pm(m_armr);
}
break;
}
// skip wizard polycontrol (iflags.mon_polycontrol)
// if no form was specified above, pick one at random now
if (mndx === NON_PM) {
tryct = 50;
do {
mndx = rn1(SPECIAL_PM - LOW_PM, LOW_PM);
} while (--tryct > 0 && !validspecmon(mon, mndx)
// try harder to select uppercase monster on rogue level
&& (tryct > 40 && Is_rogue_level(game.u?.uz)
&& !isupper_fn(monsym_fn(mons[mndx]))));
}
return mndx;
}
// C ref: mon.c:5214 accept_newcham_form()
export function accept_newcham_form(mon, mndx) {
if (mndx === NON_PM)
return null;
const mdat = mons[mndx];
if (!mdat)
return null;
if (((game.mvitals?.[mndx]?.mvflags ?? 0) & G_GENOD) !== 0)
return null;
if (is_placeholder_fn(mndx))
return null;
// select_newcham_form() might deliberately pick a player character type
if (is_mplayer(mdat))
return mdat;
// shapeshifters are rejected by polyok() but allow natural form
if (is_shapeshifter(mdat)
&& ismnum(mon.cham) && mdat === mons[mon.cham])
return mdat;
// polyok() rules out M2_PNAME, M2_WERE, and all humans except Kops
return polyok(mdat) ? mdat : null;
}
// C ref: mon.c:5263 newcham()
// Make a chameleon take on another shape, or a polymorph target become a different monster.
// Returns 1 if it actually changes form.
export async function newcham(mtmp, mdat, ncflags) {
const polyspot = (ncflags & NC_VIA_WAND_OR_SPELL) !== 0;
let msg = (ncflags & NC_SHOW_MSG) !== 0;
let seenorsensed = canspotmon(mtmp);
let mndx;
let tryct;
const olddata = mtmp.data;
// Riders are immune to polymorph and green slime
// (but apparent Rider might actually be a doppelganger)
if ((mtmp.cham ?? NON_PM) === NON_PM) { // not a shapechanger
if (is_rider(olddata))
return 0;
// make Nazgul and erinyes immune too
if (mbirth_limit_fn(monsndx(olddata)) < MAXMONNO)
return 0;
// cancelled shapechangers become uncancelled prior to being given a new shape
if (mtmp.mcan && !Protection_from_shape_changers_fn()) {
mtmp.cham = pm_to_cham(monsndx(mtmp.data));
if (mtmp.cham !== NON_PM)
mtmp.mcan = 0;
}
}
let oldname = '';
if (msg) {
// like YMonnam() but never mention saddle
oldname = x_monnam(mtmp, mtmp.mtame ? ARTICLE_YOUR : ARTICLE_THE,
null, SUPPRESS_SADDLE, false);
oldname = highc(oldname[0]) + oldname.slice(1);
}
// we need this one whether msg is true or not
const l_oldname = x_monnam(mtmp, ARTICLE_THE, null,
has_mgivenname(mtmp) ? SUPPRESS_SADDLE : 0, false);
// mdat = null -> caller wants a random monster shape
if (mdat == null) {
tryct = 20;
do {
mndx = select_newcham_form(mtmp);
mdat = accept_newcham_form(mtmp, mndx);
// for the first several tries require upper-case on rogue level
if (tryct > 15 && Is_rogue_level(game.u?.uz)
&& mdat && !isupper_fn(monsym_fn(mdat)))
mdat = null;
if (mdat)
break;
} while (--tryct > 0);
if (!tryct)
return 0;
} else if ((game.mvitals?.[monsndx(mdat)]?.mvflags ?? 0) & G_GENOD) {
return 0; // passed in mdat is genocided
}
if (mdat === olddata)
return 0; // still the same monster
mgender_from_permonst(mtmp, mdat);
// Endgame mplayers: drop rank title when polymorphed
if (In_endgame() && is_mplayer(olddata)
&& has_mgivenname(mtmp)) {
const name = MGIVENNAME(mtmp);
const theIdx = name.indexOf(' the ');
if (theIdx >= 0) {
// Truncate the name at " the "
if (mtmp.mextra) mtmp.mextra.mgivenname = name.slice(0, theIdx);
else if (mtmp.mgivenname) mtmp.mgivenname = name.slice(0, theIdx);
}
}
// worm handling: not fully ported. C's get_wormno() + initworm(rn2(5))
// + place_worm_tail_randomly() are skipped. When wormno==0 (get_wormno
// fails), C also skips, so no RNG mismatch in the common case.
// if (mtmp.wormno) { wormgone(mtmp); place_monster(mtmp, mx, my); }
if (M_AP_TYPE(mtmp) && mdat.mlet !== S_MIMIC)
seemimic(mtmp); // revert to normal monster
// HP calculation: same proportion of max as before
const hpn = mtmp.mhp;
const hpd = mtmp.mhpmax;
// set level and hit points
newmonhp_for_newcham(mtmp, monsndx(mdat));
// new hp: same fraction of max as before
if (hpd > 0) {
mtmp.mhp = Math.trunc((hpn * mtmp.mhp) / hpd);
}
// sanity check (potential overflow)
if (mtmp.mhp < 0 || mtmp.mhp > mtmp.mhpmax)
mtmp.mhp = mtmp.mhpmax;
if (!mtmp.mhp)
mtmp.mhp = 1;
// take on the new form...
set_mon_data(mtmp, mdat);
// leash handling
if (mtmp.mleashed) {
if (!leashable_fn(mtmp))
m_unleash_fn(mtmp, true);
else
update_inventory();
}
// light source handling
if (emits_light(olddata) !== emits_light(mtmp.data)) {
if (emits_light(olddata))
del_light_source_fn(mtmp);
if (emits_light(mtmp.data))
new_light_source_fn(mtmp);
}
if (!mtmp.perminvis || pm_invisible_fn(olddata))
mtmp.perminvis = pm_invisible_fn(mdat);
mtmp.minvis = mtmp.invis_blkd ? 0 : mtmp.perminvis;
if (mtmp.mundetected)
hideunder(mtmp);
if (game.u?.ustuck === mtmp) {
if (game.u?.uswallow) {
if (!attacktype(mdat, AT_ENGL)) {
if (!noncorporeal(mdat) && !is_whirly(mdat)
&& !(amorphous(mdat) || mdat.mlet === S_LIGHT)) {
let msgtrail = '';
if (is_vampshifter(mtmp)) {
msgtrail = ` which was a shapeshifted ${noname_monnam(mtmp, ARTICLE_NONE)}`;
} else if (digests(mdat)) {
msgtrail = "'s stomach";
}
await You(`${(amorphous(olddata) || is_whirly(olddata))
? 'emerge from' : 'break out of'} ${l_oldname}${msgtrail}!`);
msg = false;
mtmp.mhp = 1; // almost dead
}
await expels(mtmp, olddata, false);
} else {
await swallowed(0);
}
} else if ((!sticks(mdat) && !sticks(game.youmonst?.data))
|| unsolid(mdat)) {
unstuck_fn(mtmp);
}
}
// C ref: mon.c:5439-5442 (patched) — long worm gets segments.
// get_wormno() normally succeeds (free worm slots available), so
// consume rn2(5) for initworm's segment count when target is LONG_WORM.
if (mdat === mons[PM_LONG_WORM]) {
rn2(5); // initworm segment count
}
mtmp.meverseen = 0;
newsym(mtmp.mx, mtmp.my);
if (msg) {
if (!canspotmon(mtmp)) {
if (seenorsensed)
await pline_mon_fn(mtmp, `${oldname} disappears!`);
usmellmon_fn(mdat);
} else if (!seenorsensed) {
let mnm = x_monnam(mtmp, mtmp.mtame ? ARTICLE_YOUR : ARTICLE_A,
null, 0, false);
mnm = upstart(mnm);
await pline_mon_fn(mtmp, `${mnm} appears!`);
} else {
await pline_mon_fn(mtmp, `${oldname} turns into ${noname_monnam(mtmp, ARTICLE_A)}!`);
}
}
// when polymorph produces a vampire, turn into a full-fledged vampshifter
if ((mtmp.cham ?? NON_PM) === NON_PM && mdat.mlet === S_VAMPIRE
&& !Protection_from_shape_changers_fn())
mtmp.cham = pm_to_cham(monsndx(mdat));
possibly_unwield_fn(mtmp, polyspot);
await mon_break_armor(mtmp, polyspot);
if (!(mtmp.misc_worn_check & W_ARMG))
mselftouch(mtmp, 'No longer petrify-resistant, ',
!game.context?.mon_moving);
check_gear_next_turn(mtmp);
// former giants can't continue carrying boulders
if (mtmp.minvent && !throws_rocks(mdat)) {
let otmp = mtmp.minvent;
while (otmp && !DEADMONSTER(mtmp)) {
const otmp2 = otmp.nobj;
if (otmp.otyp === BOULDER) {
if (polyspot)
bypass_obj(otmp);
obj_extract_self(otmp);
if (flooreffects_fn(otmp, mtmp.mx, mtmp.my, ''))
{ otmp = otmp2; continue; }
place_object(otmp, mtmp.mx, mtmp.my);
}
otmp = otmp2;
}
}
// poly_steed: skip (steed polymorph not yet needed)
if (game.context?.mon_moving) {
if (!u_at_fn(mtmp.mux, mtmp.muy))
set_apparxy(mtmp);
if (!mtmp.mpeaceful
&& onscary_fn(mtmp.mux, mtmp.muy, mtmp)
&& monnear_fn(mtmp, mtmp.mux, mtmp.muy))
await monflee(mtmp, rn1(9, 2), true, true); // 2..10 turns
}
return 1;
}
// ── Stubs for unported functions used by newcham ──
// These are non-RNG-consuming stubs to be replaced when their subsystems are ported.
// C ref: topten.c:1445 tt_doppel() — picks role monster from top ten entries
// RNG: rn2(13) then either rn1() or reads from topten entries
function tt_doppel_stub(mon) {
// C ref: topten.c:1446 — rn2(13) gates get_rnd_toptenentry() call.
// When nonzero (12/13 chance), C calls get_rnd_toptenentry which
// consumes rnd(sysopt.tt_oname_maxrank) even when no score entries
// exist (returns NULL). We must consume the same RNG.
const maxrank = game.sysopt?.tt_oname_maxrank ?? 10;
if (rn2(13)) {
rnd(maxrank); // get_rnd_toptenentry rank roll (no entries → NULL)
}
// tt==NULL fallback: pick random role monster
return rn1(PM_WIZARD - PM_ARCHEOLOGIST + 1, PM_ARCHEOLOGIST);
}
// C ref: makemon.c newmonhp() — re-implement locally since not exported
function newmonhp_for_newcham(mtmp, mndx) {
const ptr = mons[mndx];
if (!ptr) return;
mtmp.m_lev = adj_lev_fn(ptr);
if (is_golem(ptr)) {
mtmp.mhpmax = mtmp.mhp = golem_hp(ptr);
} else if (is_rider(ptr)) {
mtmp.mhpmax = mtmp.mhp = c_d(10, 8);
} else if (ptr.mlevel > 49) {
mtmp.mhpmax = mtmp.mhp = 2 * (ptr.mlevel - 6);
mtmp.m_lev = Math.trunc(mtmp.mhp / 4);
} else if (ptr.mlet === S_DRAGON && mndx >= PM_GRAY_DRAGON) {
const basehp = mtmp.m_lev;
const inEndgame = !!game.astral_level && game.u?.uz?.dnum === game.astral_level.dnum;
mtmp.mhpmax = mtmp.mhp = inEndgame ? (8 * basehp) : (4 * basehp + c_d(basehp, 4));
} else if (!mtmp.m_lev) {
mtmp.mhpmax = mtmp.mhp = rnd(4);
} else {
const basehp = mtmp.m_lev;
mtmp.mhpmax = mtmp.mhp = c_d(basehp, 8);
if (is_home_elemental(ptr))
mtmp.mhpmax = (mtmp.mhp *= 3);
}
// Ensure at least basehp+1
const basehp2 = mtmp.m_lev || 1;
if (mtmp.mhpmax === basehp2) {
mtmp.mhpmax += 1;
mtmp.mhp = mtmp.mhpmax;
}
}
// Use real adj_lev from makemon.js (C: makemon.c:2013)
function adj_lev_fn(ptr) {
return adj_lev(ptr);
}
// Use real golemhp from makemon.js (C: makemon.c:2228)
function golem_hp(ptr) {
return golemhp(monsndx(ptr));
}
// Helper: monsym — get monster symbol character
function monsym_fn(ptr) {
return ptr?.mlet != null ? String.fromCharCode(ptr.mlet) : '?';
}
function isupper_fn(ch) {
return ch >= 'A' && ch <= 'Z';
}
function Protection_from_shape_changers_fn() {
return false; // TODO: port from worn/artifact
}
// In_endgame: imported from macros.js
function pm_invisible_fn(ptr) {
if (!ptr) return false;
// C: pm_invisible checks for stalker
const mndx = monsndx(ptr);
return mndx === PM_STALKER;
}
function leashable_fn(_mtmp) {
return true; // TODO: port from apply.c
}
function m_unleash_fn(mtmp, _spoil) {
mtmp.mleashed = 0; // simplified
}
function del_light_source_fn(_mtmp) {
// TODO: port from light.c
}
function new_light_source_fn(_mtmp) {
// TODO: port from light.c
}
function ceiling_hider(mptr) {
return is_hider(mptr) && (((is_clinger(mptr) && mptr?.mlet !== S_MIMIC)
|| is_flyer(mptr)));
}
// C ref: mon.c:4641 restrap — mimic/hider tries to hide
function restrap(mtmp) {
if (mtmp.mcan || M_AP_TYPE(mtmp) || cansee(mtmp.mx, mtmp.my)
|| rn2(3) || mtmp === game.u?.ustuck
|| (ceiling_hider(mtmp.data) && !has_ceiling())
|| (sensemon(mtmp) && m_next2u(mtmp)))
return false;
if (mtmp.mtrapped) {
const trap = t_at(mtmp.mx, mtmp.my);
if (trap && !is_pit(trap.ttyp)) return false;
}
if (mtmp.data?.mlet === S_MIMIC) {
if (mtmp.msleeping || mtmp.mfrozen) return false;
set_mimic_sym(mtmp);
return true;
} else {
const loc = levl(mtmp.mx, mtmp.my);
if (loc?.typ === ROOM) {
mtmp.mundetected = 1;
return true;
}
}
return false;
}
// C ref: mon.c:4785 hide_monst — re-hide monster on level restore
export function hide_monst(mon) {
const hider_under = hides_under(mon.data) || (mon.data?.mlet === S_EEL);
if ((is_hider(mon.data) || hider_under)
&& !(mon.mundetected || M_AP_TYPE(mon))) {
const x = mon.mx;
const y = mon.my;
const viz = game.viz_array;
const saved_viz = viz?.[y]?.[x] ?? 0;
if (viz?.[y]) {
viz[y][x] &= ~(IN_SIGHT | COULD_SEE);
}
if (is_hider(mon.data))
restrap(mon);
// retry for mimics
if (mon.data?.mlet === S_MIMIC && !M_AP_TYPE(mon))
restrap(mon);
if (viz?.[y]) {
viz[y][x] = saved_viz;
}
if (hider_under)
hideunder(mon);
}
}
// C: hideunder(mtmp) — monster hides under something at its location
export function hideunder(mtmp) {
if (!mtmp || !mtmp.data) return false;
const is_u = (mtmp === game.youmonst);
const x = is_u ? (game.u?.ux ?? 0) : (mtmp.mx ?? 0);
const y = is_u ? (game.u?.uy ?? 0) : (mtmp.my ?? 0);
let undetected = false;
// C ref: mon.c hideunder() — can't hide while holding hero.
if (mtmp !== game.u?.ustuck) {
const trapped = is_u ? !!(game.u?.utrap) : !!mtmp.mtrapped;
const trap = t_at(x, y);
// C: while trapped or on non-pit trap, hiding fails.
if (!trapped && !(trap && trap.ttyp !== PIT && trap.ttyp !== SPIKED_PIT)) {
if (mtmp.data.mlet === S_EEL) {
// C eel rule: hide in water (except water level), and not when
// both sides are underwater with clear LOS.
undetected = !!(is_pool(x, y)
&& !Is_waterlevel(game.u?.uz)
&& (!Underwater() || !couldsee(x, y)));
} else if (hides_under(mtmp.data) && !is_pool_or_lava(x, y)) {
const top = obj_at(x, y);
if (top
&& can_hide_under_obj_local(top)
&& (!mtmp.mtame || !cursed_object_at_local(x, y))) {
let candidate = top;
if (!is_u && !resists_ston(mtmp)) {
while (candidate
&& candidate.otyp === CORPSE
&& touch_petrifies(mons[candidate.corpsenm])) {
candidate = candidate.nexthere || null;
}
}
undetected = !!candidate;
}
}
}
}
if (is_u) {
const old = !!(game.u?.uundetected);
game.u.uundetected = undetected ? 1 : 0;
if (old !== undetected) newsym(x, y);
} else {
const old = !!mtmp.mundetected;
mtmp.mundetected = undetected ? 1 : 0;
if (old !== undetected) newsym(x, y);
}
return undetected;
}
// C ref: mon.c hideunder() message side-effect when a visible monster
// newly conceals itself under an object or in water.
export async function hideunder_with_message(mtmp) {
if (!mtmp || !mtmp.data) return false;
const is_u = (mtmp === game.youmonst);
const x = is_u ? (game.u?.ux ?? 0) : (mtmp.mx ?? 0);
const y = is_u ? (game.u?.uy ?? 0) : (mtmp.my ?? 0);
const seeit = game.in_mklev ? 0 : (cansee(x, y) ? 1 : 0);
let seenobj = null;
let locomo = null;
if (!is_u && seeit) {
if (mtmp.data.mlet === S_EEL) {
seenobj = 'the water';
locomo = 'dive';
} else if (hides_under(mtmp.data) && !is_pool_or_lava(x, y)) {
const top = obj_at(x, y);
if (top
&& can_hide_under_obj_local(top)
&& (!mtmp.mtame || !cursed_object_at_local(x, y))) {
seenobj = ansimpleoname(top);
locomo = locomotion(mtmp.data, 'hide');
}
}
}
const old = !!mtmp.mundetected;
const undetected = !!hideunder(mtmp);
if (undetected && !old && seenobj) {
let seenmon = y_monnam(mtmp);
if (seenmon === 'it') {
seenmon = `the ${pmname(mtmp.data, mtmp.female ? 1 : 0)}`;
}
if (typeof seenobj === 'string' && seenobj.startsWith('a statue of ')) {
seenobj = 'a statue';
}
set_msg_xy(x, y);
await You_see('%s %s under %s.', seenmon, locomo || 'hide', seenobj);
if (game.iflags) game.iflags.last_msg = PLNMSG_HIDE_UNDER;
}
return undetected;
}
function can_hide_under_obj_local(obj) {
if (!obj) return false;
const trap = t_at(obj.ox, obj.oy);
if (trap && trap.ttyp !== PIT && trap.ttyp !== SPIKED_PIT) return false;
if ((obj.oclass ?? 0) === COIN_CLASS) {
let coinquan = 0;
let cur = obj;
while (cur && (cur.oclass ?? 0) === COIN_CLASS) {
coinquan += (cur.quan ?? 0);
if (coinquan >= 10) break;
cur = cur.nexthere || null;
}
if (coinquan < 10) return false;
}
return true;
}
function cursed_object_at_local(x, y) {
for (let otmp = obj_at(x, y); otmp; otmp = otmp.nexthere || null) {
if (otmp.cursed) return true;
}
return false;
}
// C: maybe_unhide_at(x, y) — reveal hidden monster/object at location
export function maybe_unhide_at(x, y) {
let mtmp = m_at(x, y);
let undetected = false;
let trapped = false;
if (mtmp) {
undetected = !!mtmp.mundetected;
trapped = !!mtmp.mtrapped;
} else if (u_at(x, y)) {
mtmp = game.youmonst;
undetected = !!(game.u?.uundetected);
trapped = !!(game.u?.utrap);
} else {
return;
}
if (undetected
&& ((hides_under(mtmp.data)
&& (!obj_at(x, y) || trapped
|| !can_hide_under_obj_local(obj_at(x, y))))
|| (mtmp.data?.mlet === S_EEL && !is_pool(x, y)))) {
hideunder(mtmp);
}
}
// C ref: mon.c:1711 mon_give_prop
export async function mon_give_prop(mtmp, prop) {
let msg = null;
switch (prop) {
case FIRE_RES: msg = '%s shivers slightly.'; break;
case COLD_RES: msg = '%s looks quite warm.'; break;
case SLEEP_RES: msg = '%s looks wide awake.'; break;
case DISINT_RES: msg = '%s looks very firm.'; break;
case SHOCK_RES: msg = '%s looks very grounded.'; break;
case POISON_RES: msg = '%s looks much healthier.'; break;
default: return;
}
if (msg && canseemon(mtmp)) {
await pline(msg, Monnam(mtmp));
}
}
// C: calc_mattacku_vars(mtmp) — helper for monster attacking player
export function calc_mattacku_vars(mtmp) {
const u = game.u;
if (!game.bhitpos) game.bhitpos = {};
game.bhitpos.x = u.ux; game.bhitpos.y = u.uy; game.notonhead = false;
return {
ranged: (mdistu(mtmp) > 3),
range2: !monnear(mtmp, mtmp.mux ?? u.ux, mtmp.muy ?? u.uy),
foundyou: u_at(mtmp.mux ?? u.ux, mtmp.muy ?? u.uy),
youseeit: canseemon(mtmp)
};
}
function unstuck_fn(mtmp) {
if (!game.u || game.u.ustuck !== mtmp) {
return;
}
// C ref: set_ustuck(NULL)
setBotl('set_ustuck');
game.u.ustuck = null;
game.u.uswallow = 0;
game.u.uswldtim = 0;
// C only applies this cooldown for holding/engulfing attackers.
if (!mtmp.mspec_used
&& (dmgtype(mtmp.data, AD_STCK)
|| attacktype(mtmp.data, AT_ENGL)
|| attacktype(mtmp.data, AT_HUGS))) {
mtmp.mspec_used = rnd(2);
}
}
async function pline_mon_fn(_mtmp, ...args) {
await pline(...args);
}
function usmellmon_fn(_mdat) {
// TODO: port from mon.c
}
function possibly_unwield_fn(_mtmp, _polyspot) {
// TODO: port from wield.c
}
export function check_gear_next_turn(mtmp) {
// C ref: mon.c check_gear_next_turn() — set I_SPECIAL flag so monster
// reassesses and possibly re-equips gear on the next monster turn.
if (!mtmp) return;
mtmp.misc_worn_check = (mtmp.misc_worn_check || 0) | I_SPECIAL;
}
function flooreffects_fn(_otmp, _x, _y, _msg) {
return false; // TODO: port from trap.c
}
function u_at_fn(x, y) {
return game.u?.ux === x && game.u?.uy === y;
}
function onscary_fn(x, y, mtmp) {
return onscary(x, y, mtmp);
}
function monnear_fn(mtmp, x, y) {
return mtmp && Math.abs(mtmp.mx - x) <= 1 && Math.abs(mtmp.my - y) <= 1;
}
// Is_dragon_scales: imported from objects.js
// Is_dragon_mail: imported from objects.js
// Dragon_scales_to_pm, Dragon_mail_to_pm: imported from worn.js
// C ref: do_name.c — x_monnam()
// Core monster naming function. All other naming functions delegate here.
export function x_monnam(mtmp, article, adjective, suppress, called) {
if (!mtmp) return 'it';
const mdat = mtmp.data;
if (!mdat) return 'it';
// C: if (mtmp == &gy.youmonst) return "you"
if (game && mtmp === game.u) return 'you';
const u = game?.u;
const gameover = game?.program_state?.gameover;
if (gameover) suppress |= SUPPRESS_HALLUCINATION;
if (article === ARTICLE_YOUR && !mtmp.mtame) article = ARTICLE_THE;
// If swallowed by this monster, use ARTICLE_THE
if (u?.uswallow && u?.ustuck === mtmp) {
article = ARTICLE_THE;
suppress |= SUPPRESS_INVISIBLE;
}
const do_hallu = Hallucination() && !(suppress & SUPPRESS_HALLUCINATION);
const do_invis = mtmp.minvis && !(suppress & SUPPRESS_INVISIBLE);
const do_it = !canspotmon(mtmp) && article !== ARTICLE_YOUR
&& !gameover && mtmp !== u?.usteed
&& !(u?.uswallow && u?.ustuck === mtmp)
&& !(suppress & SUPPRESS_IT);
const do_saddle = !(suppress & SUPPRESS_SADDLE);
const mappear_as_mon = (mtmp.m_ap_type || 0) === M_AP_MONSTER;
const do_mappear = mappear_as_mon && !(suppress & SUPPRESS_MAPPEARANCE);
const do_name = !(suppress & SUPPRESS_NAME) || type_is_pname(mdat);
const augment_it = !!(suppress & AUGMENT_IT);
// Unseen monsters: "it" (or "someone"/"something" with AUGMENT_IT)
if (do_it) {
const is_mindless = !!(mdat.mflags1 & M1_MINDLESS);
const s_one = humanoid(mdat) && !is_animal(mdat) && !is_mindless;
if (!augment_it) return 'it';
if (!do_hallu ? s_one : !rn2(2)) return 'someone';
return 'something';
}
// Priests/minions: simplified — use priestname if available
// (Full priestname() support would require importing priest.js;
// for now fall through to normal naming)
// pm_name: base monster type name
let pm_name;
if (do_mappear && typeof mtmp.mappearance === 'number' && mons[mtmp.mappearance]) {
pm_name = pmname(mons[mtmp.mappearance], mtmp.female ? 1 : 0);
} else {
pm_name = pmname(mdat, mtmp.female ? 1 : 0);
}
// Shopkeepers: use shopkeeper name
if (mtmp.isshk && !do_hallu && !do_mappear) {
let buf = '';
const sname = shkname(mtmp);
const mnum = Number.isInteger(mtmp.mnum) ? mtmp.mnum : (mdat?.mnum ?? -1);
if (adjective && article === ARTICLE_THE) {
buf = 'the ' + adjective + ' ' + sname;
} else {
buf = sname;
if (mnum !== PM_SHOPKEEPER || do_invis) {
buf += ' the ';
if (do_invis) buf += 'invisible ';
buf += pm_name;
}
}
return buf;
}
// Build up adjectives
let buf = '';
if (adjective) buf += adjective + ' ';
if (do_invis) buf += 'invisible ';
if (do_saddle && (mtmp.misc_worn_check & W_SADDLE)
&& !Blind() && !Hallucination()) {
buf += 'saddled ';
}
const has_adjectives = buf.length > 0;
// Actual monster name/type
let name_at_start = false;
if (do_hallu) {
const codeRef = { value: '\0' };
const rname = rndmonnam(codeRef);
buf += rname;
name_at_start = bogon_is_pname(codeRef.value);
} else if (do_name && has_mgivenname(mtmp)) {
const mname = MGIVENNAME(mtmp);
if (mdat === mons[PM_GHOST] || mdat?.mnum === PM_GHOST) {
buf += s_suffix_inline(mname) + ' ghost';
name_at_start = true;
} else if (called) {
buf += pm_name + ' called ' + mname;
name_at_start = type_is_pname(mdat);
} else {
buf += mname;
name_at_start = true;
}
} else {
buf += pm_name;
name_at_start = type_is_pname(mdat);
}
// Article adjustments based on name_at_start
if (name_at_start && (article === ARTICLE_YOUR || !has_adjectives)) {
if (mdat === mons[PM_WIZARD_OF_YENDOR] || mdat?.mnum === PM_WIZARD_OF_YENDOR) {
article = ARTICLE_THE;
} else {
article = ARTICLE_NONE;
}
} else if ((mdat?.geno & G_UNIQ) && article === ARTICLE_A) {
article = ARTICLE_THE;
}
// Prepend article
switch (article) {
case ARTICLE_YOUR:
buf = 'your ' + buf;
break;
case ARTICLE_THE:
buf = 'the ' + buf;
break;
case ARTICLE_A:
buf = an_prefix(buf) + buf;
break;
case ARTICLE_NONE:
default:
break;
}
return buf;
}
// Inline s_suffix for use within x_monnam (avoids circular import risk at load time)
function s_suffix_inline(s) {
if (!s || !s.length) return "'s";
if (s.endsWith('s') || s.endsWith('x') || s.endsWith('z')) return s + "'";
return s + "'s";
}
// Inline "a "/"an " prefix — matches C just_an() logic
function an_prefix(str) {
if (!str || !str.length) return 'a ';
const c0 = str[0].toLowerCase();
if (str.length === 1 || str[1] === ' ') {
return 'aefhilmnosx'.includes(c0) ? 'an ' : 'a ';
}
if (str.toLowerCase().startsWith('the ')) return '';
if ('aeioAEIO'.includes(str[0])) {
// C just_an: starts with vowel → check exceptions (uni-, eu-, etc.)
if (str.toLowerCase().startsWith('useful')
|| str.toLowerCase().startsWith('unicorn')
|| str.toLowerCase().startsWith('uranium')
|| str.toLowerCase().startsWith('one')
|| str.toLowerCase().startsWith('eucalyptus')) {
return 'a ';
}
return 'an ';
}
return 'a ';
}
// C ref: do_name.c — l_monnam()
export function l_monnam(mtmp) {
return x_monnam(mtmp, ARTICLE_NONE, null,
has_mgivenname(mtmp) ? SUPPRESS_SADDLE : 0, true);
}
// C ref: do_name.c — mon_nam()
export function mon_nam(mtmp) {
return x_monnam(mtmp, ARTICLE_THE, null,
has_mgivenname(mtmp) ? SUPPRESS_SADDLE : 0, false);
}
// C: some_mon_nam(mtmp) — alias for mon_nam
export function some_mon_nam(mtmp) {
return mon_nam(mtmp);
}
// C ref: do_name.c — noit_mon_nam()
export function noit_mon_nam(mtmp) {
return x_monnam(mtmp, ARTICLE_YOUR, null,
has_mgivenname(mtmp) ? (SUPPRESS_SADDLE | SUPPRESS_IT) : SUPPRESS_IT,
false);
}
// C ref: do_name.c — Monnam()
export function Monnam(mtmp) {
const bp = mon_nam(mtmp);
return highc(bp[0]) + bp.slice(1);
}
// C ref: do_name.c — noit_Monnam()
export function noit_Monnam(mtmp) {
const bp = noit_mon_nam(mtmp);
return highc(bp[0]) + bp.slice(1);
}
// C ref: do_name.c — a_monnam()
export function a_monnam(mtmp) {
return x_monnam(mtmp, ARTICLE_A, null,
has_mgivenname(mtmp) ? SUPPRESS_SADDLE : 0, false);
}
// C ref: do_name.c — m_monnam()
export function m_monnam(mtmp) {
return x_monnam(mtmp, ARTICLE_NONE, null, EXACT_NAME, false);
}
// C ref: do_name.c — y_monnam()
export function y_monnam(mtmp) {
const prefix = mtmp.mtame ? ARTICLE_YOUR : ARTICLE_THE;
const suppression_flag = (has_mgivenname(mtmp) || mtmp === game?.u?.usteed)
? SUPPRESS_SADDLE : 0;
return x_monnam(mtmp, prefix, null, suppression_flag, false);
}
// C ref: do_name.c — Adjmonnam()
export function Adjmonnam(mtmp, adj) {
const bp = x_monnam(mtmp, ARTICLE_THE, adj,
has_mgivenname(mtmp) ? SUPPRESS_SADDLE : 0, false);
return highc(bp[0]) + bp.slice(1);
}
// C ref: do_name.c — Amonnam()
export function Amonnam(mtmp) {
const bp = a_monnam(mtmp);
return highc(bp[0]) + bp.slice(1);
}
// C ref: do_name.c — Some_Monnam()
// Returns Monnam(mtmp) if visible, else "some creature".
export function Some_Monnam(mtmp) {
if (canseemon(mtmp)) return Monnam(mtmp);
return "some creature";
}
// C ref: mhis/mhim helpers
export function noit_mhis(mtmp) { return mtmp?.female ? 'her' : 'his'; }
export function noit_mhim(mtmp) { return mtmp?.female ? 'her' : 'him'; }
// C ref: Mgender helper
// C ref: sounds.c:1412 responsive_mon_at
export function responsive_mon_at(x, y) {
const mtmp = isok(x, y) ? m_at(x, y) : null;
if (mtmp && (helpless(mtmp)
|| !mtmp.mcansee || !haseyes(mtmp.data)
|| (Invis() && !perceives(mtmp.data))
|| (x !== mtmp.mx || y !== mtmp.my)))
return null;
return mtmp;
}
// C ref: do_name.c — noname_monnam()
export function noname_monnam(mtmp, article) {
return x_monnam(mtmp, article, null, SUPPRESS_NAME, false);
}
export function has_oname(obj) {
return !!obj?.oextra?.oname;
}
|