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 | 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 6296x 73x 73x 73x 73x 73x 24321x 24321x 24321x 24321x 24321x 24321x 24321x 24321x 24321x 23949x 23949x 24321x 24321x 24321x 24321x 24321x 24321x 24321x 24321x 24321x 372x 372x 372x 24321x 23949x 24321x 510x 510x 510x 23949x 24321x 73x 73x 73x 208x 208x 208x 208x 208x 208x 208x 84x 8x 8x 8x 208x 208x 200x 200x 208x 208x 208x 208x 2x 2x 206x 208x 73x 73x 7x 7x 7x 7x 7x 7x 7x 2x 2x 7x 1x 1x 1x 1x 1x 1x 1x 7x 7x 73x 226x 226x 225x 226x 225x 224x 179x 179x 179x 225x 225x 225x 225x 225x 225x 225x 225x 225x 225x 225x 225x 226x 73x 5289x 5289x 5289x 222x 222x 222x 4773x 220x 220x 220x 220x 220x 220x 4773x 5289x 73x 2516x 2516x 2516x 2516x 2516x 2516x 760908x 760908x 760908x 830280x 830280x 830280x 73x 423184x 423184x 423184x 423184x 423184x 423184x 423184x 423184x 423184x 423184x 242249x 242249x 423184x 73x 73x 73x 73x 73x 73x 73x 335134x 335134x 335134x 335134x 335134x 149x 149x 149x 149x 149x 149x 4433x 4433x 43x 43x 43x 4433x 149x 149x 335134x 335134x 335134x 335134x 335134x 335134x 335134x 7741x 335134x 7315x 335134x 320078x 335134x 335134x 73x 335134x 335134x 335134x 335134x 335134x 48133x 335134x 73x 73x 73x 14981x 3x 3x 3x 3x 14978x 14981x 14969x 14969x 14981x 14654x 14981x 14981x 14969x 14981x 14981x 14981x 73x 73x 73x 4476x 3925x 3925x 3925x 3925x 12x 12x 3925x 3925x 3925x 3925x 4476x 73x 73x 73x 5002x 5002x 5002x 5002x 73x 73x 73x 73x 73x 73x 73x 73x 1108x 1108x 73x 73x 73x 2649x 2649x 2649x 2649x 2649x 2649x 2649x 133x 133x 133x 2516x 2516x 2516x 2516x 2649x 2649x 2649x 2649x 830280x 830280x 830280x 830280x 830280x 830280x 830280x 420668x 830280x 830280x 830280x 335134x 335134x 335134x 335134x 830280x 593815x 312206x 312206x 312206x 312206x 312206x 830280x 2649x 2649x 2516x 2649x 73x 73x 73x 73x 73x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 73x 73x 5514x 5514x 5514x 5514x 5514x 40x 40x 5514x 3x 3x 5474x 9x 9x 5471x 62x 62x 62x 5462x 359x 359x 5389x 5041x 5041x 5041x 5041x 5041x 5514x 164x 164x 164x 5514x 73x 73x 73x 73x 73x 212x 212x 212x 212x 212x 212x 730x 730x 730x 730x 730x 212x 212x 212x 212x 212x 212x 212x 212x 212x 212x 212x 212x 212x 212x 73x 73x 2419x 2419x 2419x 2419x 2419x 2419x 2419x 2419x 2419x 2419x 2419x 2419x 2419x 2419x 2418x 1972x 1972x 1972x 1972x 1972x 249x 249x 249x 235x 249x 1972x 22x 1685x 6x 6x 6x 6x 6x 1972x 1972x 1972x 1972x 1646x 1972x 1457x 1972x 1972x 1972x 1972x 2419x 487x 487x 487x 487x 2419x 2419x 2419x 2419x 1484x 2419x 1302x 1302x 1302x 1302x 95x 95x 95x 95x 95x 95x 95x 1302x 1302x 1431x 1431x 1431x 1431x 1431x 1431x 1350x 1431x 1350x 1431x 1431x 1431x 1431x 243x 243x 243x 243x 243x 243x 243x 729x 729x 729x 729x 3636x 3636x 19981x 19981x 19981x 11489x 11489x 11489x 3619x 729x 729x 729x 10760x 10760x 9131x 9131x 9131x 9131x 10760x 10760x 10760x 729x 243x 243x 73x 243x 243x 243x 443x 443x 443x 443x 443x 243x 73x 73x 33x 33x 33x 68x 68x 68x 68x 35x 35x 35x 35x 68x 68x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 5367x 5367x 5367x 5367x 5367x 5367x 5367x 5367x 5367x 5367x 1279x 5360x 1279x 5360x 5360x 5360x 5367x 5367x 5367x 5367x 5367x 5367x 5367x 5367x 5367x 5367x 5367x 73x 73x 73x 42x 42x 42x 42x 42x 42x 42x 78x 78x 77x 77x 77x 77x 77x 77x 77x 78x 42x 73x 73x 172x 172x 172x 172x 172x 172x 172x 172x 172x 73x 73x 73x 654x 654x 654x 654x 654x 654x 958x 958x 958x 958x 958x 958x 958x 958x 73x 73x 150x 150x 150x 150x 150x 150x 150x 150x 150x 150x 7x 7x 7x 150x 150x 15x 150x 6x 150x 14x 150x 15x 150x 16x 150x 13x 150x 150x 18x 150x 8x 150x 11x 150x 11x 150x 12x 150x 150x 73x 276x 276x 276x 276x 276x 276x 276x 276x 276x 276x 276x 73x 73x 73x 73x 73x 73x 79x 79x 79x 79x 79x 79x 79x 79x 27x 756x 756x 756x 756x 18x 79x 79x 79x 79x 29x 79x 96x 96x 96x 96x 73x 259x 259x 259x 259x 73x 73x 767x 767x 767x 767x 767x 767x 366x 366x 725x 444x 444x 444x 79x 43x 43x 79x 26x 444x 444x 84x 444x 444x 60x 42x 444x 50x 444x 43x 444x 45x 444x 45x 444x 38x 38x 38x 444x 444x 444x 767x 73x 73x 348x 348x 348x 348x 348x 172x 172x 348x 348x 4x 4x 172x 348x 50x 50x 348x 63x 63x 348x 59x 348x 348x 348x 73x 5002x 5002x 5002x 5002x 5002x 5002x 5002x 5002x 4999x 5002x 596x 236x 236x 236x 236x 236x 236x 236x 236x 236x 236x 236x 236x 236x 30x 236x 19x 206x 154x 187x 33x 33x 236x 236x 236x 236x 236x 80x 80x 236x 236x 236x 236x 236x 88x 88x 236x 236x 236x 236x 236x 83x 83x 236x 236x 236x 236x 236x 112x 112x 236x 236x 236x 14x 236x 45x 45x 222x 1x 1x 1x 1x 1x 177x 176x 176x 176x 176x 176x 176x 176x 176x 596x 54x 54x 54x 12x 12x 35x 22x 22x 52x 41x 41x 54x 54x 54x 360x 306x 85x 85x 85x 85x 85x 306x 2x 2x 596x 5002x 38x 38x 38x 38x 38x 5002x 97x 23x 23x 97x 66x 66x 161x 161x 161x 161x 161x 161x 161x 66x 97x 5002x 259x 1x 1x 1x 1x 1x 1x 259x 5002x 36x 36x 35x 36x 5002x 98x 98x 98x 5002x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 5002x 60x 60x 5002x 168x 4x 168x 2x 2x 2x 168x 5002x 216x 216x 216x 13x 13x 13x 13x 13x 13x 13x 13x 216x 216x 5002x 3422x 5002x 4999x 5002x 5002x 4871x 5002x 767x 767x 767x 767x 767x 5002x 348x 348x 348x 348x 5002x 5002x 5002x 73x 73x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 13x 13x 87x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 78x 77x 64x 77x 77x 29x 29x 73x 8x 8x 48x 2x 2x 40x 38x 38x 31x 31x 31x 31x 31x 4x 31x 27x 27x 1x 1x 1x 1x 1x 26x 5x 5x 5x 5x 5x 26x 17x 17x 27x 31x 31x 25x 25x 6x 24x 1x 19x 18x 18x 18x 25x 38x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 85x 85x 85x 85x 91x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 91x 73x 73x 73x 73x 73x 73x 5330x 5330x 5330x 5330x 5330x 5330x 5330x 5330x 5330x 5330x 5330x 5330x 5330x 5330x 5330x 5330x 5330x 5330x 5316x 5330x 212x 212x 212x 212x 212x 5330x 72x 72x 72x 72x 72x 72x 5330x 5316x 5316x 5316x 5330x 27x 27x 27x 27x 5289x 5289x 5330x 4282x 4282x 4282x 4282x 5330x 1007x 1007x 1007x 1108x 1108x 1108x 1007x 1007x 1007x 1007x 1007x 5289x 5289x 5289x 5289x 5289x 5330x 5289x 5330x 5289x 5330x 5289x 5330x 5289x 5330x 5289x 5289x 5289x 5289x 5289x 5330x 14x 14x 14x 5289x 5289x 5289x 5289x 5289x 5289x 5330x 5330x 5330x 5330x 5167x 5167x 5135x 5135x 5135x 5135x 5135x 5135x 5135x 5289x 5289x 5289x 5289x 5289x 5289x 5289x 5289x 5330x 5330x 5289x 5289x 5289x 5289x 5330x 91x 91x 5330x 5330x 212x 210x 210x 210x 210x 212x 5330x 5330x 166x 27x 27x 27x 166x 5330x 219x 219x 219x 219x 219x 219x 5330x 106x 106x 106x 106x 17x 17x 17x 106x 106x 5330x 4495x 5330x 5289x 5289x 5289x 5330x 5289x 5289x 5330x 5289x 5330x 3x 3x 3x 3x 3x 3x 3x 3x 5330x 5286x 8x 5286x 1x 1x 5330x 5330x 5289x 5330x 4x 4x 4x 4x 4x 4x 4x 5289x 5330x 5131x 5131x 60x 60x 5131x 5131x 38x 38x 38x 5131x 5131x 5131x 5131x 5131x 177x 177x 5330x 158x 158x 72x 72x 158x 5289x 5330x 3x 3x 3x 3x 3x 5289x 5330x 630x 630x 596x 8x 8x 4x 4x 8x 630x 5330x 204x 204x 204x 5289x 5330x 5002x 5002x 5002x 5002x 5002x 5002x 55x 55x 5002x 5289x 5330x 3968x 3968x 3968x 3968x 3968x 3968x 3968x 5289x 5330x 7x 7x 5289x 5330x 158x 158x 5289x 5330x 158x 150x 150x 150x 150x 150x 150x 4x 4x 4x 150x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 150x 158x 158x 158x 9x 9x 158x 5289x 5289x 5289x 5289x 5289x 5289x 5289x 5289x 5330x 5330x 5330x 5330x 73x 73x 1421x 1421x 1421x 1421x 1421x 1421x 1421x 1421x 1421x 1421x 74x 74x 74x 74x 74x 74x 1421x 569x 236x 236x 236x 236x 182x 67x 67x 67x 67x 182x 115x 182x 236x 17x 17x 236x 15x 15x 236x 236x 21x 21x 236x 1x 1x 1x 1x 1x 236x 236x 236x 236x 236x 236x 236x 569x 38x 38x 38x 38x 19x 19x 38x 38x 38x 38x 11x 11x 11x 11x 11x 11x 11x 38x 14x 14x 14x 14x 38x 13x 4x 4x 4x 13x 38x 38x 6x 6x 6x 6x 6x 333x 295x 85x 85x 85x 85x 85x 85x 85x 295x 9x 9x 210x 106x 106x 106x 106x 106x 106x 106x 51x 51x 51x 51x 51x 51x 51x 51x 51x 106x 106x 106x 106x 30x 30x 30x 30x 30x 30x 30x 13x 13x 13x 30x 106x 8x 8x 8x 8x 8x 8x 106x 9x 9x 9x 9x 9x 9x 106x 8x 8x 8x 106x 106x 569x 1421x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 1421x 122x 15x 15x 6x 6x 15x 6x 6x 15x 3x 3x 3x 15x 15x 15x 15x 15x 122x 60x 60x 60x 60x 60x 17x 17x 17x 7x 7x 7x 7x 17x 17x 17x 60x 43x 43x 60x 122x 1421x 1421x 100x 100x 100x 100x 100x 100x 100x 100x 100x 19x 19x 19x 19x 19x 19x 19x 19x 19x 100x 12x 12x 12x 12x 12x 12x 12x 5x 5x 5x 12x 12x 12x 100x 69x 69x 35x 100x 100x 100x 1421x 81x 81x 70x 70x 81x 1421x 61x 61x 29x 29x 29x 29x 29x 61x 1421x 29x 29x 29x 1421x 66x 66x 39x 23x 23x 37x 16x 16x 16x 39x 66x 1421x 4x 4x 4x 1421x 10x 10x 10x 10x 10x 1421x 5x 5x 5x 1421x 73x 73x 2x 2x 2x 73x 2x 2x 73x 18x 18x 73x 73x 73x 73x 73x 73x 1421x 1421x 259x 259x 259x 259x 259x 259x 24x 24x 23x 23x 24x 259x 22x 22x 22x 22x 22x 22x 22x 259x 19x 19x 19x 259x 18x 18x 16x 16x 18x 259x 29x 29x 27x 27x 29x 259x 147x 259x 259x 259x 1421x 1421x 1421x 1421x 1421x 73x 11873x 11873x 11873x 11873x 11873x 11786x 11873x 10533x 11873x 10515x 11873x 73x 10533x 10533x 10533x 10533x 73x 5289x 5289x 5289x 73x 5289x 5289x 5289x 5289x 5289x 5289x 5289x 5289x 5289x 5289x 5289x 5289x 5289x 5289x 5289x 5289x 5289x 5289x 5289x 5289x 73x 73x 1531x 1531x 22x 22x 22x 1531x 8426x 8426x 8426x 8426x 8426x 8426x 22x 22x 22x 22x 22x 22x 16302x 16302x 16302x 22x 22x 1531x 73x 73x 73x 1531x 1531x 73x 1531x 1531x 1531x 1531x 1531x 1531x 1531x 1531x 1531x 1531x 1531x 1531x 1531x 311661x 311661x 1531x 1531x 1531x 33x 33x 33x 1531x 1531x 11873x 11873x 11873x 11873x 11873x 11873x 11873x 10515x 10515x 10515x 10515x 10033x 10515x 10476x 9444x 9444x 9444x 10515x 11873x 1531x 1531x 1531x 1531x 5961x 5961x 1531x 1531x 5961x 1531x 73x 73x 71x 71x 3x 3x 3x 68x 68x 68x 68x 68x 31x 31x 31x 29x 24x 22x 31x 68x 68x 68x 68x 16x 16x 68x 68x 68x 68x 68x 68x 68x 5x 5x 68x 68x 68x 71x 73x 73x 73x 73x 72x 72x 72x | // makemon.js — Monster creation (port of makemon.c)
// Every monster placed during level generation flows through makemon().
// Heavy RNG consumer: monster selection, placement, HP, speed, inventory.
//
// Key RNG functions:
// rndmonst_adj() — weighted reservoir sampling for monster type
// makemon_rnd_goodpos() — random position within level
// newmonhp() — HP calculation
// m_initinv() — starting inventory decisions
// m_initweap() — weapons/armor for armed monsters
import { game } from './gstate.js';
import { formatMonsterEventRef, rn2, rnd, rn1, c_d, pushRngLogEntry } from './rng.js';
import { A_CHA, A_NONE, ACCESSIBLE, ALIGNWEIGHT, AM_CHAOTIC, AM_LAWFUL, AM_NEUTRAL, AM_NONE, BLCORNER, BOLT_LIM, COLNO, CROSSWALL, D_CLOSED, D_LOCKED, DELPHI, DOOR, FODDERSHOP, GP_ALLOW_U, GP_AVOID_MONPOS, GP_CHECKSCARY, HWALL, IS_ALTAR, IS_DOOR, Is_earthlevel, Is_firelevel, Is_rogue_level, IS_WALL, isok, LAVAPOOL, LAVAWALL, LR_MONGEN, M_AP_FURNITURE, M_AP_MONSTER, M_AP_NOTHING, M_AP_OBJECT, MFAST, MM_ADJACENTOK, MM_ANGRY, MM_ASLEEP, MM_EDOG, MM_EGD, MM_EMIN, MM_EPRI, MM_FEMALE, MM_IGNORELAVA, MM_IGNOREWATER, MM_MALE, MM_MINVIS, MM_NOTAIL, MM_NOCOUNTBIRTH, MM_NOEXCLAM, MM_NOGRP, MM_NONAME, MM_NOMSG, MM_NOWAIT, MOAT, MTSZ, NO_MINVENT, NON_PM, OBJ_MINVENT, P_POLEARMS, POOL, ROOMOFFSET, ROT_CORPSE, ROWNO, SCORR, SDOOR, SHOPBASE, STONE, STRAT_APPEARMSG, STRAT_CLOSE, STRAT_WAITFORU, TDWALL, TEMPLE, TLCORNER, TRWALL, TUWALL, u_at, VAULT, VWALL, WATER, ZOO } from './const.js';
import { AT_EXPL, AT_WEAP, M2_GREEDY, M2_MINION, M2_NASTY, mons, MR_COLD, MR_FIRE, MS_BRIBE, MS_GUARDIAN, MS_LEADER, MS_NEMESIS, MS_PRIEST, PM_ABBOT, PM_ACOLYTE, PM_AIR_ELEMENTAL, PM_ALEAX, PM_ALIGNED_CLERIC, PM_APPRENTICE, PM_ARCH_LICH, PM_ARCHEOLOGIST, PM_ARCHON, PM_ASMODEUS, PM_ATTENDANT, PM_BALROG, PM_BALUCHITHERIUM, PM_BARBARIAN, PM_BARBED_DEVIL, PM_BAT, PM_BLACK_DRAGON, PM_BLACK_LIGHT, PM_CAPTAIN, PM_CARNIVOROUS_APE, PM_CAVE_DWELLER, PM_CHAMELEON, PM_CHIEFTAIN, PM_CLAY_GOLEM, PM_CLERIC, PM_COCKATRICE, PM_COUATL, PM_CROESUS, PM_DISENCHANTER, PM_DISPATER, PM_DISPLACER_BEAST, PM_EARTH_ELEMENTAL, PM_ELF, PM_ELF_NOBLE, PM_ELVEN_MONARCH, PM_ERINYS, PM_ETTIN, PM_FIRE_ELEMENTAL, PM_FIRE_GIANT, PM_FLESH_GOLEM, PM_FLOATING_EYE, PM_FOG_CLOUD, PM_FOREST_CENTAUR, PM_GENETIC_ENGINEER, PM_GHOST, PM_GIANT, PM_GIANT_BAT, PM_GIANT_EEL, PM_GLASS_GOLEM, PM_GOBLIN, PM_GOLD_GOLEM, PM_GRAY_DRAGON, PM_GREEN_DRAGON, PM_GREEN_SLIME, PM_GREMLIN, PM_GUARD, PM_GUARDIAN_NAGA, PM_GUIDE, PM_HIGH_CLERIC, PM_HOBBIT, PM_HORNED_DEVIL, PM_HOUSECAT, PM_HUMAN, PM_HUNTER, PM_ICE_DEVIL, PM_IRON_GOLEM, PM_JABBERWOCK, PM_KITTEN, PM_LEATHER_GOLEM, PM_LEOCROTTA, PM_LIEUTENANT, PM_LITTLE_DOG, PM_LONG_WORM, PM_LONG_WORM_TAIL, PM_MASTER_LICH, PM_MASTER_MIND_FLAYER, PM_MINOTAUR, PM_MONK, PM_MORDOR_ORC, PM_NAZGUL, PM_NEANDERTHAL, PM_NINJA, PM_OCHRE_JELLY, PM_OGRE_LEADER, PM_OGRE_TYRANT, PM_OLOG_HAI, PM_ORANGE_DRAGON, PM_ORC, PM_ORC_CAPTAIN, PM_ORC_SHAMAN, PM_ORCUS, PM_OWLBEAR, PM_PAGE, PM_PAPER_GOLEM, PM_PESTILENCE, PM_PONY, PM_PURPLE_WORM, PM_QUANTUM_MECHANIC, PM_RANGER, PM_RED_DRAGON, PM_ROPE_GOLEM, PM_ROSHI, PM_SALAMANDER, PM_SAMURAI, PM_SANDESTIN, PM_SERGEANT, PM_SHOPKEEPER, PM_SILVER_DRAGON, PM_SOLDIER, PM_STALKER, PM_STONE_GOLEM, PM_STORM_GIANT, PM_STRAW_GOLEM, PM_STUDENT, PM_THUG, PM_UMBER_HULK, PM_URUK_HAI, PM_VAMPIRE, PM_VAMPIRE_BAT, PM_VAMPIRE_LEADER, PM_VLAD_THE_IMPALER, PM_WARRIOR, PM_WATCH_CAPTAIN, PM_WATCHMAN, PM_WATER_ELEMENTAL, PM_WINGED_GARGOYLE, PM_WIZARD, PM_WIZARD_OF_YENDOR, PM_WOLF, PM_WOOD_GOLEM, PM_WUMPUS, PM_XAN, PM_XORN, PM_YEENOGHU, PM_YELLOW_DRAGON, PM_ZRUTY, S_ANGEL, S_BAT, S_CENTAUR, S_DEMON, S_DRAGON, S_EEL, S_ELEMENTAL, S_GHOST, S_GIANT, S_GNOME, S_GOLEM, S_HUMAN, S_HUMANOID, S_JABBERWOCK, S_KOBOLD, S_KOP, S_LEPRECHAUN, S_LICH, S_LIGHT, S_LIZARD, S_MIMIC, S_MIMIC_DEF, S_MUMMY, S_NYMPH, S_OGRE, S_ORC, S_QUANTMECH, S_SNAKE, S_SPIDER, S_TRAPPER, S_TROLL, S_UNICORN, S_VAMPIRE, S_WRAITH, S_ZOMBIE } from './monsters.js';
import { accessible, closed_door, onscary, set_apparxy } from './monmove.js';
import { quest_info } from './questpgr.js';
import { add_to_container, next_ident, mksobj, mkobj, curse, bless, rnd_class, set_corpsenm, weight, mkobj_at, rndmonnum, can_be_hatched } from './mkobj.js';
import { sobj_at } from './mkobj.js';
import { findgold, mpickobj } from './steal.js';
import { christen_monst, rndghostname } from './do_name.js';
import { newepri } from './priest.js';
import { newemin } from './minion.js';
import { amorphous, attacktype, big_to_little, humanoid, is_amphibious, is_animal, is_covetous, is_demon, is_dwarf, is_elf, is_female, is_floater, is_flyer, is_giant, is_golem, is_home_elemental, is_lord, is_male, is_mercenary, is_mindless, is_neuter, is_prince, is_rider, is_shapeshifter, is_swimmer, is_undead, is_vampire, is_vampshifter, is_whirly, likes_gold, likes_lava, noncorporeal, nonliving, passes_walls, set_mon_data, strongmonst, throws_rocks } from './mondata.js';
import { m_at, MON_AT, obj_at, t_at } from './map_access.js';
import { In_endgame, In_sokoban, Is_airlevel, Is_astralevel, Is_waterlevel, Role_if, See_invisible } from './macros.js';
import { cansee } from './vision.js';
import {
def_monsyms, S_hwall, S_vwall, S_hcdoor, S_vcdoor, S_fountain,
S_altar, S_upstair, S_dnstair, S_grave, S_throne, S_sink,
} from './symbols.js';
import { AKLYS, AMULET_CLASS, AMULET_OF_LIFE_SAVING, ARMOR_CLASS, ARROW, ATHAME, AXE, BANDED_MAIL, BATTLE_AXE, BEC_DE_CORBIN, BELL_OF_OPENING, BOULDER, BOW, BROADSWORD, BUGLE, BULLWHIP, C_RATION, CANDELABRUM_OF_INVOCATION, CHAIN_MAIL, CLOAK_OF_MAGIC_RESISTANCE, CLOAK_OF_PROTECTION, CLUB, COIN_CLASS, CORPSE, CREAM_PIE, CROSSBOW, CROSSBOW_BOLT, CRYSTAL_BALL, CRYSTAL_PLATE_MAIL, DAGGER, DART, DENTED_POT, DILITHIUM_CRYSTAL, DWARVISH_CLOAK, DWARVISH_IRON_HELM, DWARVISH_MATTOCK, DWARVISH_MITHRIL_COAT, DWARVISH_ROUNDSHIELD, DWARVISH_SHORT_SWORD, DWARVISH_SPEAR, EGG, ELVEN_ARROW, ELVEN_BOOTS, ELVEN_BOW, ELVEN_BROADSWORD, ELVEN_CLOAK, ELVEN_DAGGER, ELVEN_LEATHER_HELM, ELVEN_MITHRIL_COAT, ELVEN_SHIELD, ELVEN_SHORT_SWORD, ELVEN_SPEAR, FIGURINE, FLAIL, FLINT, FOOD_CLASS, GEM_CLASS, GLAIVE, GOLD_PIECE, HELMET, HIGH_BOOTS, IRON_SHOES, K_RATION, KNIFE, LARGE_BOX, LARGE_SHIELD, LEATHER_ARMOR, LEATHER_CLOAK, LEATHER_GLOVES, LEATHER_JACKET, LONG_SWORD, LOW_BOOTS, LUCERN_HAMMER, LUCKSTONE, LUMP_OF_ROYAL_JELLY, MACE, MAXOCLASSES, MIRROR, MUMMY_WRAPPING, ORCISH_ARROW, ORCISH_BOW, ORCISH_CHAIN_MAIL, ORCISH_CLOAK, ORCISH_DAGGER, ORCISH_HELM, ORCISH_SHIELD, ORCISH_SHORT_SWORD, PARTISAN, PICK_AXE, PLATE_MAIL, POT_ACID, POT_BLINDNESS, POT_CONFUSION, POT_EXTRA_HEALING, POT_FULL_HEALING, POT_GAIN_LEVEL, POT_HEALING, POT_INVISIBILITY, POT_OBJECT_DETECTION, POT_PARALYSIS, POT_POLYMORPH, POT_SICKNESS, POT_SLEEPING, POT_SPEED, POTION_CLASS, QUARTERSTAFF, RANDOM_CLASS, RANSEUR, RIN_INVISIBILITY, RING_CLASS, RING_MAIL, ROBE, ROCK, ROCK_CLASS, RUBBER_HOSE, SCIMITAR, SCR_CREATE_MONSTER, SCR_EARTH, SCR_SCARE_MONSTER, SCR_TELEPORTATION, SCROLL_CLASS, SHIELD_OF_REFLECTION, SHORT_SWORD, SHURIKEN, SILVER_MACE, SILVER_SABER, SKELETON_KEY, SLIME_MOLD, SLING, SMALL_SHIELD, SPBOOK_CLASS, SPE_DIG, SPEAR, SPETUM, SPLINT_MAIL, STATUE, STILETTO, STRANGE_OBJECT, STUDDED_LEATHER_ARMOR, TALLOW_CANDLE, TIN, TIN_WHISTLE, TOOL_CLASS, TRIDENT, TWO_HANDED_SWORD, URUK_HAI_SHIELD, WAN_COLD, WAN_CREATE_MONSTER, WAN_DEATH, WAN_DIGGING, WAN_FIRE, WAN_LIGHTNING, WAN_MAGIC_MISSILE, WAN_MAKE_INVISIBLE, WAN_NOTHING, WAN_POLYMORPH, WAN_SLEEP, WAN_SPEED_MONSTER, WAN_STRIKING, WAN_TELEPORTATION, WAND_CLASS, WAX_CANDLE, WEAPON_CLASS } from './objects.js';
const nasties = [
PM_COCKATRICE, PM_ETTIN, PM_STALKER, PM_MINOTAUR,
PM_OWLBEAR, PM_PURPLE_WORM, PM_XAN, PM_UMBER_HULK,
PM_XORN, PM_ZRUTY, PM_LEOCROTTA, PM_BALUCHITHERIUM,
PM_CARNIVOROUS_APE, PM_FIRE_ELEMENTAL, PM_JABBERWOCK,
PM_IRON_GOLEM, PM_OCHRE_JELLY, PM_GREEN_SLIME,
PM_DISPLACER_BEAST, PM_GENETIC_ENGINEER,
PM_BLACK_DRAGON, PM_RED_DRAGON, PM_ARCH_LICH, PM_VAMPIRE_LEADER,
PM_MASTER_MIND_FLAYER, PM_DISENCHANTER, PM_WINGED_GARGOYLE,
PM_STORM_GIANT, PM_OLOG_HAI, PM_ELF_NOBLE, PM_ELVEN_MONARCH,
PM_OGRE_TYRANT, PM_CAPTAIN, PM_GREMLIN,
PM_SILVER_DRAGON, PM_ORANGE_DRAGON, PM_GREEN_DRAGON,
PM_YELLOW_DRAGON, PM_GUARDIAN_NAGA, PM_FIRE_GIANT,
PM_ALEAX, PM_COUATL, PM_HORNED_DEVIL, PM_BARBED_DEVIL,
];
import { get_shop_item } from './shknam.js';
import { qt_montype } from './questpgr.js';
import { is_lava, is_pool, is_pool_or_lava, is_waterwall } from './dbridge.js';
import { newegd } from './vault.js';
import { m_dowear } from './worn.js';
import { custompline, set_msg_xy } from './pline.js';
import { newsym, canseemon, sensemon } from './display.js';
import { Amonnam, hideunder, m_in_air, pm_to_cham, select_newcham_form, accept_newcham_form } from './mon.js';
import { vtense } from './objnam.js';
import { dist2 } from './hacklib.js';
import { may_passwall, next2u } from './hack.js';
import { dochugw } from './monmove.js';
import { put_saddle_on_mon } from './steed.js';
import { initedog, newedog, set_malign } from './dog.js';
import { deliver_obj_to_mon } from './dokick.js';
import { in_town } from './mklev.js';
import { count_wsegs, get_wormno, initworm, place_worm_tail_randomly } from './worm.js';
import { stop_timer } from './timeout.js';
// Monster geno flags (from monflag.h)
const G_FREQ = 0x0007;
const G_NOCORPSE = 0x0010;
const G_LGROUP = 0x0040;
const G_SGROUP = 0x0080;
const G_NOGEN = 0x0200;
const G_HELL = 0x0400;
const G_NOHELL = 0x0800;
const G_UNIQ = 0x1000;
const G_GENOD = 0x02;
const G_EXTINCT = 0x01;
const G_GONE = G_GENOD | G_EXTINCT;
const M3_WAITFORU = 0x0040;
const M3_CLOSE = 0x0080;
const M3_WAITMASK = 0x00c0;
const M3_COVETOUS = 0x001f;
// Alignment constants
const A_NEUTRAL = 0;
// Monster range
const LOW_PM = 0;
// C: SPECIAL_PM = PM_LONG_WORM_TAIL. Monsters after this index are
// quest denizens, player monsters, and other special types that
// mkclass() and rndmonst() must not pick.
const SPECIAL_PM = PM_LONG_WORM_TAIL;
const MAXMONNO = 120;
// Minimal monster structure matching C's struct monst
export function makeMonst() {
return {
mx: 0, my: 0,
mnum: 0,
data: null,
mhp: 0, mhpmax: 0,
m_lev: 0,
malign: 0,
movement: 0,
minvent: null,
mpeaceful: false,
msleeping: 0,
mtame: 0,
mflee: 0,
mleashed: 0,
mcanmove: true,
mcansee: true,
mfrozen: 0,
mundetected: false,
dead: false,
female: false,
mtrapped: false,
mnamed: false,
wormno: 0,
m_id: 0,
cham: NON_PM,
mstrategy: 0,
seen_resistance: 0,
mwandexp: false,
perminvis: false,
minvis: false,
isminion: false,
iswiz: false,
mtrapseen: 0,
mtrack: Array.from({ length: MTSZ }, () => ({ x: 0, y: 0 })),
};
}
// ============================================================
// Helper functions (no RNG)
// ============================================================
// C ref: dungeon.c:2021 level_difficulty()
export function level_difficulty() {
const g = game;
const uz = g.u?.uz;
if (!uz) return 1;
const depth_of = (lev) => {
if (!lev) return 1;
const d = g.dungeons?.[lev.dnum];
return (d?.depth_start ?? 1) + (lev.dlevel ?? 1) - 1;
};
const builds_up = (lev) => {
const d = lev ? g.dungeons?.[lev.dnum] : null;
return !!d && (d.entry_lev ?? 1) > 1;
};
const deepest_lev_reached = (noquest) => {
let ret = 0;
const qd = g.quest_dnum ?? -1;
for (let i = 0; i < (g.dungeons?.length ?? 0); i++) {
if (noquest && i === qd) continue;
const d = g.dungeons[i];
const dl = d?.dunlev_ureached ?? 0;
if (!dl) continue;
const dep = (d.depth_start ?? 1) + dl - 1;
if (dep > ret) ret = dep;
}
return ret;
};
const in_endgame = (lev) => {
const astral = g.astral_level;
return !!lev && !!astral && lev.dnum === astral.dnum;
};
// C ref: dungeon.c:2024-2058 level_difficulty()
if (in_endgame(uz)) {
const sanctumDepth = depth_of(g.sanctum_level);
return sanctumDepth + Math.trunc((g.u?.ulevel ?? 1) / 2);
}
if (g.u?.uhave?.amulet) {
const deepest = deepest_lev_reached(false);
if (deepest > 0) return deepest;
}
let res = depth_of(uz);
if (builds_up(uz)) {
const entryLev = g.dungeons?.[uz.dnum]?.entry_lev ?? uz.dlevel ?? 1;
res += 2 * (entryLev - (uz.dlevel ?? 1) + 1);
}
return res;
}
// pm_to_cham: imported from mon.js
// C ref: mon.c pickvampshape()
export function pickvampshape(mtmp) {
let mndx = mtmp.cham;
let wolfchance = 10;
const uppercase_only = Is_rogue_level(game.u?.uz);
switch (mndx) {
case PM_VLAD_THE_IMPALER:
// mon_has_special() not yet ported; keep default Vlad handling simple.
wolfchance = 3;
/* falls through */
case PM_VAMPIRE_LEADER:
if (!rn2(wolfchance) && !uppercase_only && !is_pool_or_lava(mtmp.mx, mtmp.my)) {
mndx = PM_WOLF;
break;
}
/* falls through */
case PM_VAMPIRE:
mndx = (!rn2(4) && !uppercase_only) ? PM_FOG_CLOUD : PM_VAMPIRE_BAT;
break;
default:
break;
}
if (((game.mvitals?.[mndx]?.mvflags ?? 0) & G_GENOD)
|| (mtmp.data !== mons[mtmp.cham] && !rn2(4))) {
return mtmp.cham;
}
return mndx;
}
// C ref: wizard.c:538 pick_nasty()
export function pick_nasty(difcap) {
let res = nasties[rn2(nasties.length)];
const uz = game.u?.uz;
const inHell = !!uz && !!(game.dungeons?.[uz.dnum]?.flags?.hellish);
let alt = res;
if (((game.mvitals?.[res]?.mvflags ?? 0) & G_GENOD)
|| (difcap > 0 && (mons[res]?.difficulty ?? 0) >= difcap)
|| (((mons[res]?.geno ?? 0) & (inHell ? G_NOHELL : G_HELL)) !== 0)) {
alt = big_to_little(res);
}
if (alt !== res && ((game.mvitals?.[alt]?.mvflags ?? 0) & G_GENOD) === 0) {
const mnam = mons[alt]?.mname ?? '';
const juvenile = mnam.startsWith('baby ')
|| mnam.endsWith(' hatchling')
|| mnam.endsWith(' pup')
|| mnam.endsWith(' cub');
if (!juvenile) res = alt;
}
return res;
}
export function apply_init_newcham(mtmp, mndx) {
if (!(mndx >= 0 && mndx < mons.length) || !mons[mndx] || mons[mndx] === mtmp.data)
return false;
const mdat = mons[mndx];
if (is_male(mdat)) mtmp.female = false;
else if (is_female(mdat)) mtmp.female = true;
else if (!is_male(mdat) && !is_female(mdat) && !is_neuter(mdat)) {
if (!rn2(10) && !(is_vampire(mdat) || is_vampshifter(mtmp)))
mtmp.female = !mtmp.female;
}
const hpn = mtmp.mhp;
const hpd = mtmp.mhpmax;
newmonhp(mtmp, mndx);
if (hpd > 0) {
mtmp.mhp = Math.trunc((hpn * mtmp.mhp) / hpd);
if (mtmp.mhp < 0 || mtmp.mhp > mtmp.mhpmax)
mtmp.mhp = mtmp.mhpmax;
if (!mtmp.mhp)
mtmp.mhp = 1;
}
set_mon_data(mtmp, mdat);
return true;
}
// C-shaped subset of newcham() for initial makemon() shapechanger setup.
function init_shapechanger_form(mtmp) {
const pm = pm_to_cham(mtmp.mnum);
if (pm === NON_PM) return false;
mtmp.cham = pm;
// C ref: makemon.c:1360-1368 — Vlad stays in normal form so he can
// carry the Candelabrum; initial newcham() is explicitly skipped.
if (mtmp.mnum === PM_VLAD_THE_IMPALER) return false;
let tryct = 20;
do {
const mndx = select_newcham_form(mtmp);
const mdat = accept_newcham_form(mtmp, mndx);
if (!mdat) continue;
return apply_init_newcham(mtmp, mndx);
} while (--tryct > 0);
return false;
}
// C ref: monst.h macros
function monmax_difficulty(levdif) {
return Math.trunc((levdif + (game.u?.ulevel ?? 1)) / 2);
}
function monmin_difficulty(levdif) {
return Math.trunc(levdif / 6);
}
function montoostrong(mndx, lev) {
return (mons[mndx]?.difficulty ?? 0) > lev;
}
function montooweak(mndx, lev) {
return (mons[mndx]?.difficulty ?? 0) < lev;
}
// C ref: makemon.c:1593 uncommon()
function uncommon(mndx) {
const pm = mons[mndx];
if (!pm) return true;
if (pm.geno & (G_NOGEN | G_UNIQ)) return true;
const mvflags = game.mvitals?.[mndx]?.mvflags ?? 0;
if (mvflags & G_GONE) return true;
const uz = game.u?.uz;
const inHell = !!uz && !!game.dungeons?.[uz.dnum]?.flags?.hellish;
if (inHell)
return pm.maligntyp > A_NEUTRAL;
else
return !!(pm.geno & G_HELL);
}
// C ref: makemon.c:1611 align_shift()
// C checks: (lev) ? lev->flags.align : svd.dungeons[dnum].flags.align
// where lev = Is_special(&u.uz). For special levels, the s_level's
// C ref: makemon.c:1612-1639 — exact port of align_shift().
// C ref: makemon.c:1612-1622 — exact port of align_shift().
// C uses two statics: oldmoves (init 0) and lev (cached Is_special).
// JS stores these on the game object so they reset per session.
function align_shift(ptr) {
const g = game;
const moves = g.moves ?? 0;
// C ref: makemon.c:1619-1621
if ((g._as_oldmoves ?? 0) !== moves) {
// C: lev = Is_special(&u.uz)
const uz = g.u?.uz;
const dnum = uz?.dnum ?? -1;
const dlevel = uz?.dlevel ?? -1;
g._as_lev = null;
for (let slev = g.sp_levchn; slev; slev = slev.next) {
const d = slev.dlevel;
if (d && d.dnum === dnum && d.dlevel === dlevel) {
g._as_lev = slev;
break;
}
}
g._as_oldmoves = moves;
}
// C ref: makemon.c:1623
const alignVal = g._as_lev
? (g._as_lev.flags?.align ?? AM_NONE)
: (g.dungeons?.[g.u?.uz?.dnum ?? 0]?.flags?.align ?? AM_NONE);
switch (alignVal) {
case AM_LAWFUL:
return Math.trunc((ptr.maligntyp + 20) / (2 * ALIGNWEIGHT));
case AM_NEUTRAL:
return Math.trunc((20 - Math.abs(ptr.maligntyp)) / ALIGNWEIGHT);
case AM_CHAOTIC:
return Math.trunc((-(ptr.maligntyp - 20)) / (2 * ALIGNWEIGHT));
default:
return 0;
}
}
// C ref: makemon.c:1641 temperature_shift()
function temperature_shift(ptr) {
const temperature = game.level?.flags?.temperature || 0;
if (!temperature || !ptr) return 0;
if (temperature > 0 && (ptr.mresists & MR_FIRE)) return 3;
if (temperature < 0 && (ptr.mresists & MR_COLD)) return 3;
return 0;
}
// C ref: makemon.c wrong_elem_type()
function wrong_elem_type(ptr) {
if (!ptr) return false;
if (ptr.mlet === S_ELEMENTAL)
return !is_home_elemental(ptr);
if (Is_earthlevel(game.u?.uz))
return false; // no restrictions
if (Is_waterlevel(game.u?.uz))
return !is_swimmer(ptr);
if (Is_firelevel(game.u?.uz))
return !(ptr.mresists & MR_FIRE);
if (Is_airlevel(game.u?.uz)) {
return !((is_flyer(ptr) && ptr.mlet !== S_TRAPPER)
|| is_floater(ptr)
|| amorphous(ptr)
|| noncorporeal(ptr)
|| is_whirly(ptr));
}
return false;
}
// C ref: makemon.c:2013 adj_lev()
export function adj_lev(ptr) {
if (ptr === mons[PM_WIZARD_OF_YENDOR]) {
let tmp = ptr.mlevel + (game.mvitals?.[PM_WIZARD_OF_YENDOR]?.died ?? 0);
if (tmp > 49) tmp = 49;
return tmp;
}
let tmp = ptr.mlevel;
if (tmp > 49) return 50;
const ldiff = level_difficulty();
let tmp2 = ldiff - tmp;
if (tmp2 < 0) tmp--;
else tmp += Math.trunc(tmp2 / 5);
tmp2 = (game.u?.ulevel ?? 1) - ptr.mlevel;
if (tmp2 > 0) tmp += Math.trunc(tmp2 / 4);
tmp2 = Math.trunc((3 * ptr.mlevel) / 2);
if (tmp2 > 49) tmp2 = 49;
return (tmp > tmp2) ? tmp2 : (tmp > 0 ? tmp : 0);
}
// C ref: makemon.c:2178 mongets() — give a specific item to a monster
// RNG: all from mksobj(otyp, TRUE, FALSE) — item creation with properties
export async function mongets(mtmp, otyp) {
if (!otyp) return null;
const otmp = mksobj(otyp, true, false);
if (otmp) {
// Demon check: never get blessed objects
if (mtmp.data?.mlet === S_DEMON && otmp.blessed) {
curse(otmp);
}
// Add to monster inventory via bottleneck (emits ^pickup event)
await mpickobj(mtmp, otmp);
}
return otmp;
}
// is_male, is_female, is_neuter, is_golem — imported from mondata.js
// is_rider imported from mondata.js
// is_home_elemental: imported from mondata.js
function is_armed(ptr) {
// C ref: mondata.h — monster has AT_WEAP attack
return ptr.mattk?.some(a => a && a.aatyp === AT_WEAP);
}
// u_at: imported from const.js
// m_at — imported from map_access.js
// newedog: imported from dog.js
// ============================================================
// rndmonst — random monster selection (weighted reservoir sampling)
// ============================================================
// C ref: makemon.c:1652 rndmonst()
export function rndmonst() {
return rndmonst_adj(0, 0);
}
// C ref: makemon.c:1658 rndmonst_adj()
// RNG: rn2(7) for quest check, then rn2(totalweight) per eligible monster
export function rndmonst_adj(minadj, maxadj) {
const uz = game.u?.uz;
const inHell = !!uz && !!game.dungeons?.[uz.dnum]?.flags?.hellish;
const debugRndmon = typeof process !== 'undefined'
&& process?.env?.RNDMON_DEBUG === '1';
let debugLogged = 0;
// C ref: makemon.c:1666-1667
if (uz?.dnum === (game.quest_dnum ?? -1) && rn2(7)) {
const qptr = qt_montype();
if (qptr) return qptr;
}
const zlevel = level_difficulty();
const minmlev = monmin_difficulty(zlevel) + minadj;
const maxmlev = monmax_difficulty(zlevel) + maxadj;
const upper = Is_rogue_level(uz);
const elemlevel = In_endgame(uz) && !Is_astralevel(uz);
let totalweight = 0;
let selected_mndx = NON_PM;
for (let mndx = LOW_PM; mndx < SPECIAL_PM && mndx < mons.length; mndx++) {
const ptr = mons[mndx];
if (!ptr) continue;
if (montooweak(mndx, minmlev) || montoostrong(mndx, maxmlev))
continue;
if (upper) {
const msym = def_monsyms[ptr.mlet]?.sym || '';
if (!(msym >= 'A' && msym <= 'Z'))
continue;
}
if (elemlevel && wrong_elem_type(ptr))
continue;
if (uncommon(mndx))
continue;
if (inHell && (ptr.geno & G_NOHELL))
continue;
const freq = (ptr.geno & G_FREQ);
const ashift = align_shift(ptr);
const tshift = temperature_shift(ptr);
let weight = freq + ashift + tshift;
if (weight < 0 || weight > 127) weight = 0;
if (weight > 0) {
if (debugRndmon && debugLogged < 20) {
pushRngLogEntry(`^dbg_rndmon[mndx=${mndx} mname=${ptr.mname || ''} geno=${ptr.geno ?? 0} freq=${freq} ash=${ashift} tsh=${tshift} w=${weight} tot0=${totalweight}]`);
debugLogged++;
}
totalweight += weight;
if (rn2(totalweight) < weight)
selected_mndx = mndx;
}
}
if (selected_mndx === NON_PM || uncommon(selected_mndx))
return null;
return mons[selected_mndx];
}
// ============================================================
// newmonhp — HP calculation
// ============================================================
// C ref: makemon.c:2228 golemhp()
export function golemhp(type) {
switch (type) {
case PM_STRAW_GOLEM: return 20;
case PM_PAPER_GOLEM: return 20;
case PM_ROPE_GOLEM: return 30;
case PM_LEATHER_GOLEM: return 40;
case PM_GOLD_GOLEM: return 60;
case PM_WOOD_GOLEM: return 50;
case PM_FLESH_GOLEM: return 40;
case PM_CLAY_GOLEM: return 70;
case PM_STONE_GOLEM: return 100;
case PM_GLASS_GOLEM: return 80;
case PM_IRON_GOLEM: return 120;
default: return 0;
}
}
// C ref: makemon.c:1013 newmonhp()
// RNG: d(level, 8) or rnd(4) depending on monster type
function newmonhp(mtmp, mndx) {
const ptr = mons[mndx];
let basehp = 0;
mtmp.m_lev = adj_lev(ptr);
if (is_golem(ptr)) {
// Fixed HP by golem type — no RNG
mtmp.mhpmax = mtmp.mhp = golemhp(mndx);
} else if (is_rider(ptr)) {
basehp = 10;
mtmp.mhpmax = mtmp.mhp = c_d(basehp, 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) {
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) {
basehp = 1;
mtmp.mhpmax = mtmp.mhp = rnd(4);
} else {
basehp = mtmp.m_lev;
mtmp.mhpmax = mtmp.mhp = c_d(basehp, 8);
if (is_home_elemental(ptr))
mtmp.mhpmax = (mtmp.mhp *= 3);
}
if (mtmp.mhpmax === basehp) {
mtmp.mhpmax += 1;
mtmp.mhp = mtmp.mhpmax;
}
}
// ============================================================
// makemon_rnd_goodpos — random placement
// ============================================================
// C ref: makemon.c:1077 makemon_rnd_goodpos()
// RNG: rn1(COLNO-3, 2), rn2(ROWNO) per attempt (up to 50)
function makemon_rnd_goodpos(mon, gpflags, cc) {
let tryct = 0;
let nx, ny;
let good;
gpflags |= GP_AVOID_MONPOS;
do {
nx = rn1(COLNO - 3, 2); // rn2(COLNO-3) + 2
ny = rn2(ROWNO);
good = (!game.in_mklev && cansee(nx, ny))
? false
: goodpos(nx, ny, mon, gpflags);
} while (++tryct < 50 && !good);
// Per-call tracing so cross-engine position-selection divergences are
// visible in the event stream. Parity with C's makemon_rnd_goodpos
// instrumentation (patch 033).
pushRngLogEntry(`^mk_rnd_goodpos[try=${tryct} good=${good ? 1 : 0} pos=${nx},${ny} mklev=${game.in_mklev ? 1 : 0}]`);
if (good) {
cc.x = nx;
cc.y = ny;
return true;
}
// C ref: makemon.c:1095+ — exhaustive search fallback.
const xofs = nx;
const yofs = ny;
// In mklev (common for this call), C skips the "avoid visible squares" pass.
const blind = !!(game.Blind || game.blind || game.u?.blind);
let bl = (game.in_mklev || blind) ? 1 : 0;
for (; bl < 2; bl++) {
if (!bl) gpflags &= ~GP_CHECKSCARY;
for (let dx = 0; dx < COLNO; dx++) {
for (let dy = 0; dy < ROWNO; dy++) {
nx = ((dx + xofs) % (COLNO - 1)) + 1;
ny = ((dy + yofs) % (ROWNO - 1)) + 1;
if (bl === 0 && cansee(nx, ny))
continue;
if (goodpos(nx, ny, mon, gpflags)) {
pushRngLogEntry(`^mk_rnd_goodpos_scan[bl=${bl} pos=${nx},${ny}]`);
cc.x = nx;
cc.y = ny;
return true;
}
}
}
if (bl === 0 && (!mon || mon.data?.mmove)) {
for (let stway = game.stairs; stway; stway = stway.next) {
if (stway.tolev?.dnum === game.u?.uz?.dnum && !rn2(2)) {
nx = stway.sx;
ny = stway.sy;
break;
}
}
if (goodpos(nx, ny, mon, gpflags)) {
pushRngLogEntry(`^mk_rnd_goodpos_stairs[pos=${nx},${ny}]`);
cc.x = nx;
cc.y = ny;
return true;
}
}
}
pushRngLogEntry(`^mk_rnd_goodpos_fail[]`);
return false;
}
// C ref: teleport.c:86 goodpos()
export function goodpos(x, y, mon, gpflags) {
const g = game;
if (!isok(x, y)) return false;
const loc = g.level?.at(x, y);
if (!loc) return false;
const allow_u = (gpflags & GP_ALLOW_U) !== 0;
const avoid_monpos = (gpflags & GP_AVOID_MONPOS) !== 0;
const checkscary = (gpflags & GP_CHECKSCARY) !== 0;
const ignorewater = (gpflags & MM_IGNOREWATER) !== 0;
const ignorelava = (gpflags & MM_IGNORELAVA) !== 0;
const mdat = mon?.data || null;
if (!allow_u && u_at(x, y) && mon !== g.youmonst)
return false;
if (MON_AT(x, y) && avoid_monpos)
return false;
if (mon) {
const mtmp2 = m_at(x, y);
// C ref: teleport.c:130 — worm check (mtmp2 != mtmp || mtmp->wormno)
if (mtmp2 && (mtmp2 !== mon || mon.wormno))
return false;
if (is_pool(x, y) && !ignorewater) {
// C ref: teleport.c:144-147 — monsters in water
return !!(is_swimmer(mdat)
|| (!Is_waterlevel(g.u?.uz)
&& !is_waterwall(x, y)
&& m_in_air(mon)));
} else if (mdat?.mlet === S_EEL && rn2(13) && !ignorewater) {
return false;
} else if (is_lava(x, y) && !ignorelava) {
// C ref: teleport.c:153 — floating eye avoids lava
if (mdat === mons[PM_FLOATING_EYE])
return false;
return !!(m_in_air(mon) || likes_lava(mdat));
}
if (passes_walls(mdat) && may_passwall(x, y))
return true;
if (amorphous(mdat) && closed_door(x, y))
return true;
// C: avoid onscary()/goodpos_onscary() for monster placement probes.
if (checkscary && (mon.m_id
? onscary(x, y, mon)
: goodpos_onscary(x, y, mdat)))
return false;
}
// C ref: teleport.c:172-176 — accessible check
if (!accessible(x, y)) {
if (!(is_pool(x, y) && ignorewater)
&& !(is_lava(x, y) && ignorelava))
return false;
}
if (sobj_at(BOULDER, x, y) && (!mdat || !throws_rocks(mdat)))
return false;
if (avoid_monpos && is_exclusion_zone(LR_MONGEN, x, y))
return false;
return true;
}
function is_exclusion_zone(zonetype, x, y) {
const zones = game.level?.exclusionZones;
if (!Array.isArray(zones)) return false;
for (const zone of zones) {
if (!zone || zone.zonetype !== zonetype) continue;
const lx = Math.min(zone.lx ?? 0, zone.hx ?? 0);
const hx = Math.max(zone.lx ?? 0, zone.hx ?? 0);
const ly = Math.min(zone.ly ?? 0, zone.hy ?? 0);
const hy = Math.max(zone.ly ?? 0, zone.hy ?? 0);
if (x >= lx && x <= hx && y >= ly && y <= hy) return true;
}
return false;
}
function goodpos_onscary(x, y, mptr) {
if (!mptr) return false;
if (mptr.mlet === S_HUMAN || mptr.mlet === S_ANGEL || is_rider(mptr))
return false;
if (IS_ALTAR(game.level?.at(x, y)?.typ ?? STONE) && mptr.mlet === S_VAMPIRE)
return true;
if (sobj_at(SCR_SCARE_MONSTER, x, y))
return true;
if (mptr === mons[PM_MINOTAUR])
return false;
const ep = game.level?.engravingAt?.(x, y);
return !!(ep?.text && /elbereth/i.test(String(ep.text)));
}
function collect_coords(ccc, cx, cy, maxradius) {
let n = 0;
const rowrange = (cy < Math.trunc(ROWNO / 2)) ? (ROWNO - 1 - cy) : cy;
const colrange = (cx < Math.trunc(COLNO / 2)) ? (COLNO - 1 - cx) : cx;
const k = Math.max(rowrange, colrange);
maxradius = maxradius ? Math.min(maxradius, k) : k;
for (let radius = 1; radius <= maxradius; radius++) {
const passStart = ccc.length;
const lox = cx - radius, hix = cx + radius;
const loy = cy - radius, hiy = cy + radius;
for (let y = Math.max(loy, 0); y <= hiy; ++y) {
if (y > ROWNO - 1) break;
for (let x = Math.max(lox, 1); x <= hix; ++x) {
if (x > COLNO - 1) break;
if (x !== lox && x !== hix && y !== loy && y !== hiy)
continue;
ccc.push({ x, y });
++n;
}
}
let passIndex = passStart;
let remaining = ccc.length - passStart;
while (remaining > 1) {
const kswap = rn2(remaining);
if (kswap) {
const tmp = ccc[passIndex];
ccc[passIndex] = ccc[passIndex + kswap];
ccc[passIndex + kswap] = tmp;
}
++passIndex;
--remaining;
}
}
return n;
}
export function enexto_core(cc, xx, yy, ptr, gpflags) {
const candy = [];
const nearcandyct = collect_coords(candy, xx, yy, 3);
for (let i = 0; i < nearcandyct; ++i) {
cc.x = candy[i].x;
cc.y = candy[i].y;
if (goodpos(cc.x, cc.y, { data: ptr }, gpflags))
return true;
}
const allcandy = [];
const allcandyct = collect_coords(allcandy, xx, yy, 0);
for (let i = nearcandyct; i < allcandyct; ++i) {
cc.x = allcandy[i].x;
cc.y = allcandy[i].y;
if (goodpos(cc.x, cc.y, { data: ptr }, gpflags))
return true;
}
cc.x = xx;
cc.y = yy;
return false;
}
// C ref: makemon.c:2652 enexto()
export function enexto(cc, xx, yy, ptr) {
return enexto_core(cc, xx, yy, ptr, GP_CHECKSCARY)
|| enexto_core(cc, xx, yy, ptr, 0);
}
function pet_type() {
const g = game;
if (g.urole?.petnum !== undefined && g.urole.petnum !== NON_PM)
return g.urole.petnum;
if (g.preferred_pet === 'c')
return PM_KITTEN;
if (g.preferred_pet === 'd')
return PM_LITTLE_DOG;
return rn2(2) ? PM_KITTEN : PM_LITTLE_DOG;
}
const ghostnames = [
'Adri', 'Andries', 'Andreas', 'Bert', 'David', 'Dirk',
'Emile', 'Frans', 'Fred', 'Greg', 'Hether', 'Jay',
'John', 'Jon', 'Karnov', 'Kay', 'Kenny', 'Kevin',
'Maud', 'Michiel', 'Mike', 'Peter', 'Robert', 'Ron',
'Tom', 'Wilmar', 'Nick Danger', 'Phoenix', 'Jiro', 'Mizue',
'Stephan', 'Lance Braccus', 'Shadowhawk', 'Murphy',
];
// C ref: makemon.c:2265 peace_minded()
// RNG: rn2(16+record) && rn2(2+abs(mal)) for co-aligned non-trivial monsters.
function peace_minded(ptr) {
const g = game;
const M2_PEACEFUL = 0x00200000;
const M2_HOSTILE = 0x00100000;
const raceLove = g.urace?.lovemask || 0;
const raceHate = g.urace?.hatemask || 0;
if (ptr.mflags2 & M2_PEACEFUL) return true;
if (ptr.mflags2 & M2_HOSTILE) return false;
if (ptr.msound === MS_LEADER || ptr.msound === MS_GUARDIAN)
return true;
if (ptr.msound === MS_NEMESIS)
return false;
if (ptr === mons[PM_ERINYS])
return !(g.u?.ualign?.abuse || 0);
if (ptr.mflags2 & raceLove) return true;
if (ptr.mflags2 & raceHate) return false;
const mal = ptr.maligntyp || 0;
const ual = g.u?.ualign?.type || 0;
function sgn(x) { return x > 0 ? 1 : x < 0 ? -1 : 0; }
if (sgn(mal) !== sgn(ual)) return false;
if (mal < 0 && g.u?.uhave?.amulet) return false;
const record = g.u?.ualign?.record ?? 0;
if (ptr.mflags2 & M2_MINION)
return record >= 0;
const clampedRecord = record < -15 ? -15 : record;
return (!!rn2(16 + clampedRecord) && !!rn2(2 + Math.abs(mal)));
}
// Local initedog removed — use canonical from dog.js
// (The local version had a broken CHA calculation that didn't use ACURR)
// C ref: makemon.c:80 m_initgrp()
async function m_initgrp(mtmp, x, y, n, mmflags) {
const mm = { x, y };
let cnt = rnd(n);
const ulevel = game.u?.ulevel ?? 1;
cnt = Math.trunc(cnt / (ulevel < 3 ? 4 : (ulevel < 5 ? 2 : 1)));
if (!cnt) cnt++;
while (cnt-- > 0) {
if (peace_minded(mtmp.data))
continue;
if (enexto_core(mm, mm.x, mm.y, mtmp.data, mmflags)) {
const mon = await makemon(mtmp.data, mm.x, mm.y, mmflags | MM_NOGRP, true);
if (mon) {
mon.mpeaceful = false;
mon.mavenge = 0;
}
}
}
}
// C ref: makemon.c:149 m_initthrow()
// RNG: mksobj(otyp, TRUE, FALSE), rn1(oquan, 3)
async function m_initthrow(mtmp, otyp, oquan) {
const otmp = mksobj(otyp, true, false);
if (!otmp) return;
otmp.quan = rn1(oquan, 3);
otmp.owt = weight(otmp);
if (otyp === ORCISH_ARROW)
otmp.opoisoned = true;
await mpickobj(mtmp, otmp);
}
// likes_gold: imported from mondata.js
// NOTE: differs from canonical steal.js — local checks oclass === COIN_CLASS || otyp === GOLD_PIECE; canonical checks otyp only
// C ref: makemon.c quest_mon_represents_role macro
function quest_mon_represents_role(ptr, role_pm) {
// mlet == S_HUMAN && Role_if(role_pm) && (msound == MS_LEADER || MS_NEMESIS)
if (!ptr || ptr.mlet !== S_HUMAN) return false;
if (!Role_if(role_pm)) return false;
return ptr.msound === MS_LEADER || ptr.msound === MS_NEMESIS;
}
function arm_bonus(obj) {
if (!obj) return 0;
const od = game.objects[obj.otyp];
if (!od) return 0;
const a_ac = od.a_ac ?? od.oc_oc1 ?? 0;
const erosion = Math.max(obj.oeroded || 0, obj.oeroded2 || 0);
return a_ac + (obj.spe || 0) - Math.min(erosion, a_ac);
}
// NOTE: differs from canonical muse.js — local has full RNG-consuming implementation; canonical returns 0
// C ref: muse.c:2035 rnd_offensive_item()
function rnd_offensive_item(mtmp) {
const pm = mtmp.data;
const difficulty = (mons[mtmp.mnum]?.difficulty ?? pm?.difficulty ?? 0);
if (is_animal(pm) || attacktype(pm, AT_EXPL) || is_mindless(pm)
|| pm?.mlet === S_GHOST || pm?.mlet === S_KOP)
return 0;
if (difficulty > 7 && !rn2(35))
return WAN_DEATH;
switch (rn2(9 - (difficulty < 4 ? 1 : 0) + 4 * (difficulty > 6 ? 1 : 0))) {
case 0:
// C checks hard_helmet, amorphous, passes_walls, noncorporeal, unsolid
if (amorphous(pm) || passes_walls(pm) || noncorporeal(pm))
return SCR_EARTH;
/* falls through */
case 1:
return WAN_STRIKING;
case 2:
return POT_ACID;
case 3:
return POT_CONFUSION;
case 4:
return POT_BLINDNESS;
case 5:
return POT_SLEEPING;
case 6:
return POT_PARALYSIS;
case 7:
case 8:
return WAN_MAGIC_MISSILE;
case 9:
return WAN_SLEEP;
case 10:
return WAN_FIRE;
case 11:
return WAN_COLD;
case 12:
return WAN_LIGHTNING;
}
return 0;
}
export function mkmonmoney(mtmp, amount) {
if (!(amount > 0)) return;
const gold = mksobj(GOLD_PIECE, false, false);
if (!gold) return;
gold.quan = amount;
gold.owt = weight(gold);
// C ref: makemon.c:586 mkmonmoney() uses add_to_minv(), not mpickobj();
// this must not emit ^pickup.
gold.where = OBJ_MINVENT;
gold.nobj = mtmp.minvent;
mtmp.minvent = gold;
}
// ============================================================
// m_initinv — monster starting inventory
// ============================================================
// C ref: makemon.c:590 m_initinv()
// RNG: varies by monster type. For basic level 1 monsters:
// rn2(50) for defensive item, rn2(100) for misc item
function noteleport_level(mtmp) {
// C ref: teleport.c:30 noteleport_level()
const monData = mtmp?.data || null;
const uz = game.u?.uz;
const inHell = !!uz && !!game.dungeons?.[uz.dnum]?.flags?.hellish;
// demon court in Gehennom prevents non-prince/lord demons from teleporting
if (inHell
&& !(monData && is_demon(monData) && (is_lord(monData) || is_prince(monData)))) {
for (let mon = game.fmon; mon; mon = mon.nmon) {
const d = mon.data;
if (d && is_demon(d) && (is_lord(d) || is_prince(d)))
return true;
}
}
if (game.level?.flags?.noteleport && !is_covetous(mtmp?.data))
return true;
if ((game.level?.flags?.stasis_until ?? -1) >= (game.moves ?? 0))
return true;
return false;
}
function in_sokoban() {
const uz = game.u?.uz;
return !!uz && game.sokoban_dnum != null && uz.dnum === game.sokoban_dnum;
}
// NOTE: differs from canonical mondata.js — local checks ptr === mons[PM_FLOATING_EYE]; canonical checks mlet S_EYE/S_LIGHT
function extra_nasty(ptr) {
// C ref: mondata.c extra_nasty() — M2_NASTY
return !!(ptr?.mflags2 & M2_NASTY);
}
// NOTE: differs from canonical muse.js — local has full RNG-consuming implementation; canonical returns 0
// C ref: muse.c:1222 rnd_defensive_item()
function rnd_defensive_item(mtmp) {
const pm = mtmp.data;
const difficulty = (mons[mtmp.mnum]?.difficulty ?? pm?.difficulty ?? 0);
let trycnt = 0;
if (is_animal(pm) || attacktype(pm, AT_EXPL) || is_mindless(pm)
|| pm?.mlet === S_GHOST || pm?.mlet === S_KOP) {
return 0;
}
while (true) {
switch (rn2(8 + (difficulty > 3 ? 1 : 0) + (difficulty > 6 ? 1 : 0) + (difficulty > 8 ? 1 : 0))) {
case 6:
case 9:
if (noteleport_level(mtmp) && ++trycnt < 2) {
continue;
}
if (!rn2(3)) return WAN_TELEPORTATION;
return SCR_TELEPORTATION;
case 0:
case 1:
return SCR_TELEPORTATION;
case 8:
case 10:
if (!rn2(3)) return WAN_CREATE_MONSTER;
return SCR_CREATE_MONSTER;
case 2:
return SCR_CREATE_MONSTER;
case 3:
return POT_HEALING;
case 4:
return POT_EXTRA_HEALING;
case 5:
return (pm !== mons[PM_PESTILENCE]) ? POT_FULL_HEALING : POT_SICKNESS;
case 7:
if (in_sokoban() && rn2(4)) {
continue;
}
if (is_floater(pm) || mtmp.isshk || mtmp.isgd || mtmp.ispriest) {
return 0;
}
return WAN_DIGGING;
default:
return 0;
}
}
}
// NOTE: differs from canonical muse.js — local has full RNG-consuming implementation; canonical returns 0
// C ref: muse.c:2652 rnd_misc_item()
function rnd_misc_item(mtmp) {
const pm = mtmp.data;
const difficulty = (mons[mtmp.mnum]?.difficulty ?? pm?.difficulty ?? 0);
if (is_animal(pm) || attacktype(pm, AT_EXPL) || is_mindless(pm)
|| pm?.mlet === S_GHOST || pm?.mlet === S_KOP) {
return 0;
}
if (difficulty < 6 && !rn2(30)) {
return rn2(6) ? POT_POLYMORPH : WAN_POLYMORPH;
}
if (!rn2(40) && !nonliving(pm) && !is_vampshifter(mtmp)) {
return AMULET_OF_LIFE_SAVING;
}
switch (rn2(3)) {
case 0:
if (mtmp.isgd) return 0;
return rn2(6) ? POT_SPEED : WAN_SPEED_MONSTER;
case 1:
if (mtmp.mpeaceful && !See_invisible()) return 0;
return rn2(6) ? POT_INVISIBILITY : WAN_MAKE_INVISIBLE;
case 2:
return POT_GAIN_LEVEL;
default:
return 0;
}
}
// C ref: makemon.c:590 m_initinv()
async function m_initinv(mtmp) {
let cnt;
let otmp;
const ptr = mtmp.data;
const mm = mons.indexOf(ptr);
if (!ptr) return;
if (Is_rogue_level(game.u?.uz))
return;
switch (ptr.mlet) {
case S_HUMAN:
if (is_mercenary(ptr)) {
let mac;
switch (mm) {
case PM_GUARD: mac = -1; break;
case PM_SOLDIER: mac = 3; break;
case PM_SERGEANT: mac = 0; break;
case PM_LIEUTENANT: mac = -2; break;
case PM_CAPTAIN: mac = -3; break;
case PM_WATCHMAN: mac = 3; break;
case PM_WATCH_CAPTAIN: mac = -2; break;
default: mac = 0; break;
}
// Round 1: body armor
if (mac < -1 && rn2(5)) {
otmp = await mongets(mtmp, rn2(5) ? PLATE_MAIL : CRYSTAL_PLATE_MAIL);
} else if (mac < 3 && rn2(5)) {
otmp = await mongets(mtmp, rn2(3) ? SPLINT_MAIL : BANDED_MAIL);
} else if (rn2(5)) {
otmp = await mongets(mtmp, rn2(3) ? RING_MAIL : STUDDED_LEATHER_ARMOR);
} else {
otmp = await mongets(mtmp, LEATHER_ARMOR);
}
if (otmp) mac += arm_bonus(otmp);
otmp = null;
// Round 2: helmets
if (mac < 10 && rn2(3))
otmp = await mongets(mtmp, HELMET);
else if (mac < 10 && rn2(2))
otmp = await mongets(mtmp, DENTED_POT);
if (otmp) mac += arm_bonus(otmp);
otmp = null;
// Round 3: shields
if (mac < 10 && rn2(3))
otmp = await mongets(mtmp, SMALL_SHIELD);
else if (mac < 10 && rn2(2))
otmp = await mongets(mtmp, LARGE_SHIELD);
if (otmp) mac += arm_bonus(otmp);
otmp = null;
// Round 4: boots
if (mac < 10 && rn2(3))
otmp = await mongets(mtmp, LOW_BOOTS);
else if (mac < 10 && rn2(2))
otmp = await mongets(mtmp, HIGH_BOOTS);
if (otmp) mac += arm_bonus(otmp);
otmp = null;
// Round 5: gloves + cloak
if (mac < 10 && rn2(3))
otmp = await mongets(mtmp, LEATHER_GLOVES);
else if (mac < 10 && rn2(2))
otmp = await mongets(mtmp, LEATHER_CLOAK);
if (otmp) mac += arm_bonus(otmp);
otmp = null;
if (ptr === mons[PM_WATCH_CAPTAIN]) {
; // better weapon rather than extra gear
} else if (ptr === mons[PM_WATCHMAN]) {
if (rn2(3))
await mongets(mtmp, TIN_WHISTLE);
} else if (ptr === mons[PM_GUARD]) {
otmp = mksobj(TIN_WHISTLE, true, false);
if (otmp) {
curse(otmp);
await mpickobj(mtmp, otmp);
}
} else {
// soldiers and their officers
if (!rn2(3))
await mongets(mtmp, K_RATION);
if (!rn2(2))
await mongets(mtmp, C_RATION);
if (ptr !== mons[PM_SOLDIER] && !rn2(3))
await mongets(mtmp, BUGLE);
}
} else if (ptr === mons[PM_SHOPKEEPER]) {
await mongets(mtmp, SKELETON_KEY);
switch (rn2(4)) {
case 0:
await mongets(mtmp, WAN_MAGIC_MISSILE);
/* falls through */
case 1:
await mongets(mtmp, POT_EXTRA_HEALING);
/* falls through */
case 2:
await mongets(mtmp, POT_HEALING);
/* falls through */
case 3:
await mongets(mtmp, WAN_STRIKING);
}
} else if (ptr.msound === MS_PRIEST
|| quest_mon_represents_role(ptr, PM_CLERIC)) {
await mongets(mtmp, rn2(7) ? ROBE
: rn2(3) ? CLOAK_OF_PROTECTION
: CLOAK_OF_MAGIC_RESISTANCE);
await mongets(mtmp, SMALL_SHIELD);
mkmonmoney(mtmp, rn1(10, 20));
} else if (quest_mon_represents_role(ptr, PM_MONK)) {
await mongets(mtmp, rn2(11) ? ROBE : CLOAK_OF_MAGIC_RESISTANCE);
}
break;
case S_NYMPH:
if (!rn2(2))
await mongets(mtmp, MIRROR);
if (!rn2(2))
await mongets(mtmp, POT_OBJECT_DETECTION);
break;
case S_GIANT:
if (ptr === mons[PM_MINOTAUR]) {
if (!rn2(3) || (game.in_mklev && Is_earthlevel()))
await mongets(mtmp, WAN_DIGGING);
} else if (is_giant(ptr)) {
cnt = rn2(Math.trunc(mtmp.m_lev / 2));
for (; cnt; cnt--) {
otmp = mksobj(rnd_class(DILITHIUM_CRYSTAL, LUCKSTONE - 1), false, false);
if (otmp) {
otmp.quan = rn1(2, 3);
otmp.owt = weight(otmp);
await mpickobj(mtmp, otmp);
}
}
}
break;
case S_WRAITH:
if (ptr === mons[PM_NAZGUL]) {
otmp = mksobj(RIN_INVISIBILITY, false, false);
if (otmp) {
curse(otmp);
await mpickobj(mtmp, otmp);
}
}
break;
case S_LICH:
if (ptr === mons[PM_MASTER_LICH] && !rn2(13))
await mongets(mtmp, rn2(7) ? ATHAME : WAN_NOTHING);
else if (ptr === mons[PM_ARCH_LICH] && !rn2(3)) {
otmp = mksobj(rn2(3) ? ATHAME : QUARTERSTAFF, true,
rn2(13) ? false : true);
if (otmp) {
if (otmp.spe < 2)
otmp.spe = rnd(3);
if (!rn2(4))
otmp.oerodeproof = true;
await mpickobj(mtmp, otmp);
}
}
break;
case S_MUMMY:
if (rn2(7))
await mongets(mtmp, MUMMY_WRAPPING);
break;
case S_QUANTMECH:
// C ref: makemon.c:778-796 — Schroedinger's cat
if (!rn2(20) && ptr === mons[PM_QUANTUM_MECHANIC]) {
otmp = mksobj(LARGE_BOX, false, false);
if (otmp) {
const catcorpse = mksobj(CORPSE, true, false);
if (catcorpse) {
otmp.spe = 1;
set_corpsenm(catcorpse, PM_HOUSECAT);
stop_timer(ROT_CORPSE, catcorpse);
add_to_container(otmp, catcorpse);
otmp.owt = weight(otmp);
}
await mpickobj(mtmp, otmp);
}
}
break;
case S_LEPRECHAUN:
mkmonmoney(mtmp, c_d(level_difficulty(), 30));
break;
case S_DEMON:
if (ptr === mons[PM_ICE_DEVIL] && !rn2(4)) {
await mongets(mtmp, SPEAR);
} else if (ptr === mons[PM_ASMODEUS]) {
await mongets(mtmp, WAN_COLD);
await mongets(mtmp, WAN_FIRE);
}
break;
case S_GNOME:
{
const inMines = (game.u?.uz?.dnum ?? -1) === (game.mines_dnum ?? -2);
if (!rn2((inMines && game.in_mklev) ? 20 : 60)) {
otmp = mksobj(rn2(4) ? TALLOW_CANDLE : WAX_CANDLE, true, false);
if (otmp) {
otmp.quan = 1;
otmp.owt = weight(otmp);
// C: mpickobj then begin_burn — we skip begin_burn
await mpickobj(mtmp, otmp);
}
}
}
break;
default:
break;
}
// C ref: makemon.c:825-826 — ordinary soldiers rarely get magic
if (ptr === mons[PM_SOLDIER] && rn2(13))
return;
// C ref: makemon.c:828-834 — common end of m_initinv
if (mtmp.m_lev > rn2(50)) {
const item = rnd_defensive_item(mtmp);
if (process?.env?.DEBUG_INITINV && mtmp.mnum === 172) console.error(` def_item=${item} ident_before=${game.context?.ident}`);
await mongets(mtmp, item);
if (process?.env?.DEBUG_INITINV && mtmp.mnum === 172) console.error(` ident_after=${game.context?.ident}`);
}
if (mtmp.m_lev > rn2(100)) {
const item = rnd_misc_item(mtmp);
if (process?.env?.DEBUG_INITINV && mtmp.mnum === 172) console.error(` misc_item=${item} ident_before=${game.context?.ident}`);
await mongets(mtmp, item);
}
if (likes_gold(ptr) && !findgold(mtmp.minvent) && !rn2(5))
mkmonmoney(mtmp, c_d(level_difficulty(), mtmp.minvent ? 5 : 10));
}
// C ref: makemon.c:2390 set_mimic_sym()
export function set_mimic_sym(mtmp) {
// TODO: hook real worn/intrinsic protection check when centralized.
const Protection_from_shape_changers = false;
if (!mtmp || Protection_from_shape_changers) return;
const mx = mtmp.mx;
const my = mtmp.my;
const loc = game.level?.at?.(mx, my);
if (!loc) return;
const typ = loc.typ;
let roomno = (loc.roomno ?? 0) - ROOMOFFSET;
let rt = 0;
if (roomno >= 0)
rt = game.level?.rooms?.[roomno]?.rtype ?? 0;
const syms = [
MAXOCLASSES, MAXOCLASSES, RING_CLASS, WAND_CLASS, WEAPON_CLASS,
FOOD_CLASS, COIN_CLASS, SCROLL_CLASS, POTION_CLASS, ARMOR_CLASS,
AMULET_CLASS, TOOL_CLASS, ROCK_CLASS, GEM_CLASS, SPBOOK_CLASS,
S_MIMIC_DEF, S_MIMIC_DEF,
];
const furnsyms = [
S_upstair, S_upstair, S_dnstair, S_dnstair,
S_altar, S_grave, S_throne, S_sink,
];
let ap_type;
let appear;
if (obj_at(mx, my)) {
ap_type = M_AP_OBJECT;
appear = obj_at(mx, my).otyp;
} else if (IS_DOOR(typ) || IS_WALL(typ) || typ === SDOOR || typ === SCORR) {
ap_type = M_AP_FURNITURE;
if (mx !== 0 && (
game.level?.at?.(mx - 1, my)?.typ === HWALL
|| game.level?.at?.(mx - 1, my)?.typ === TLCORNER
|| game.level?.at?.(mx - 1, my)?.typ === TRWALL
|| game.level?.at?.(mx - 1, my)?.typ === BLCORNER
|| game.level?.at?.(mx - 1, my)?.typ === TDWALL
|| game.level?.at?.(mx - 1, my)?.typ === CROSSWALL
|| game.level?.at?.(mx - 1, my)?.typ === TUWALL
)) {
appear = Is_rogue_level(game.u?.uz) ? S_hwall : S_hcdoor;
} else {
appear = Is_rogue_level(game.u?.uz) ? S_vwall : S_vcdoor;
}
} else if (game.level?.flags?.is_maze_lev
&& !((game.u?.uz?.dnum ?? -1) === (game.mines_dnum ?? -2)
&& in_town(game.u?.ux ?? 0, game.u?.uy ?? 0))
&& !in_sokoban()
&& rn2(2)) {
ap_type = M_AP_OBJECT;
appear = STATUE;
} else if (roomno < 0 && !t_at(mx, my)) {
ap_type = M_AP_OBJECT;
appear = BOULDER;
} else if (rt === ZOO || rt === VAULT) {
ap_type = M_AP_OBJECT;
appear = GOLD_PIECE;
} else if (rt === DELPHI) {
if (rn2(2)) {
ap_type = M_AP_OBJECT;
appear = STATUE;
} else {
ap_type = M_AP_FURNITURE;
appear = S_fountain;
}
} else if (rt === TEMPLE) {
ap_type = M_AP_FURNITURE;
appear = S_altar;
} else if (rt >= SHOPBASE) {
const dnum = game.u?.uz?.dnum ?? 0;
const dlevel = game.u?.uz?.dlevel ?? 1;
const depth = (game.dungeons?.[dnum]?.depth_start ?? 1) + dlevel - 1;
let s_sym;
if (rn2(10) >= depth) {
s_sym = S_MIMIC_DEF;
} else {
s_sym = get_shop_item(rt - SHOPBASE);
if (s_sym < 0) {
ap_type = M_AP_OBJECT;
appear = -s_sym;
mtmp.m_ap_type = ap_type;
mtmp.mappearance = appear;
return;
} else if (rt === FODDERSHOP && s_sym > MAXOCLASSES) {
ap_type = M_AP_OBJECT;
appear = rn2(2) ? LUMP_OF_ROYAL_JELLY : SLIME_MOLD;
mtmp.m_ap_type = ap_type;
mtmp.mappearance = appear;
return;
} else if (s_sym === RANDOM_CLASS || s_sym >= MAXOCLASSES) {
s_sym = syms[rn2(syms.length - 2) + 2];
}
}
if (s_sym === MAXOCLASSES) {
ap_type = M_AP_FURNITURE;
appear = furnsyms[rn2(furnsyms.length)];
} else {
ap_type = M_AP_OBJECT;
if (s_sym === S_MIMIC_DEF) {
appear = STRANGE_OBJECT;
} else if (s_sym === COIN_CLASS) {
appear = GOLD_PIECE;
} else {
const otmp = mkobj(s_sym, false);
appear = otmp?.otyp ?? STRANGE_OBJECT;
}
}
} else {
let s_sym = syms[rn2(syms.length)];
if (s_sym === MAXOCLASSES) {
ap_type = M_AP_FURNITURE;
appear = furnsyms[rn2(furnsyms.length)];
} else {
ap_type = M_AP_OBJECT;
if (s_sym === S_MIMIC_DEF) {
appear = STRANGE_OBJECT;
} else if (s_sym === COIN_CLASS) {
appear = GOLD_PIECE;
} else {
const otmp = mkobj(s_sym, false);
appear = otmp?.otyp ?? STRANGE_OBJECT;
}
}
}
mtmp.m_ap_type = ap_type;
mtmp.mappearance = appear;
if (ap_type === M_AP_OBJECT
&& (appear === STATUE || appear === FIGURINE
|| appear === CORPSE || appear === EGG || appear === TIN)) {
pushRngLogEntry('>rndmonnum');
let mndx = rndmonnum();
pushRngLogEntry(`<rndmonnum=${mndx}`);
const nocorpse = ((game.mvitals?.[mndx]?.mvflags ?? 0) & G_NOCORPSE) !== 0;
if (appear === CORPSE && nocorpse) {
mndx = rn1(PM_WIZARD - PM_ARCHEOLOGIST + 1, PM_ARCHEOLOGIST);
} else if ((appear === EGG && !can_be_hatched(mndx))
|| (appear === TIN && nocorpse)) {
mndx = NON_PM;
}
if (!mtmp.mextra) mtmp.mextra = {};
mtmp.mextra.mcorpsenm = mndx;
}
}
// ============================================================
// makemon — main entry point
// ============================================================
// C ref: makemon.c:1148 makemon()
// RNG: placement, type selection, HP, gender, inventory
export async function makemon(ptr, x, y, mmflags, suppressMidlog = false) {
const debugMakemonCallsite = typeof process !== 'undefined'
&& process?.env?.MAKEMON_CALLSITE_DEBUG === '1';
if (debugMakemonCallsite) {
const stack = new Error().stack?.split('\n') || [];
const caller = stack.find((line) => line.includes('.js:') && !line.includes('makemon.js'))
|| stack[2]
|| 'unknown';
pushRngLogEntry(`^dbg_makemon_call[${caller.trim()}]`);
}
if (!suppressMidlog) pushRngLogEntry('>makemon');
try {
const g = game;
const anymon = !ptr;
const byyou = u_at(x, y);
let allow_minvent = (mmflags & NO_MINVENT) === 0;
const allowtail = (mmflags & MM_NOTAIL) === 0;
const countbirth = (mmflags & MM_NOCOUNTBIRTH) === 0;
const cc = { x: 0, y: 0 };
const gpflags = (((mmflags & MM_IGNOREWATER) !== 0) ? MM_IGNOREWATER : 0)
| GP_CHECKSCARY | GP_AVOID_MONPOS;
// C ref: makemon.c:1172 — disable random generation when level
// forbids random monsters; explicit ptr creation is still allowed.
if (g.iflags?.debug_mongen || (!(g.level?.flags?.rndmongen) && !ptr))
return null;
// Random location
if (x === 0 && y === 0) {
const probeMon = ptr ? { data: ptr } : null;
if (!makemon_rnd_goodpos(probeMon, gpflags, cc))
return null;
x = cc.x;
y = cc.y;
} else if (byyou && !g.in_mklev) {
if (!enexto_core(cc, g.u.ux, g.u.uy, ptr, gpflags)
&& !enexto_core(cc, g.u.ux, g.u.uy, ptr, gpflags & ~GP_CHECKSCARY))
return null;
x = cc.x;
y = cc.y;
}
if (!isok(x, y)) return null;
// C ref: makemon.c:1194-1201 — occupied destination handling.
// Any requested placement (specific or random) must either relocate
// with MM_ADJACENTOK or fail before consuming next_ident().
if (m_at(x, y)) {
if ((mmflags & MM_ADJACENTOK) === 0
|| !enexto_core(cc, x, y, ptr, gpflags)) {
return null;
}
x = cc.x;
y = cc.y;
}
// Random monster type
let mndx;
if (ptr) {
mndx = mons.indexOf(ptr);
if (mndx < 0) return null;
if ((g.mvitals?.[mndx]?.mvflags ?? 0) & G_GENOD)
return null;
} else {
let tryct = 0;
const fakemon = makeMonst();
do {
ptr = rndmonst();
if (!ptr) return null;
fakemon.data = ptr;
} while (++tryct <= 50
&& ((tryct === 1 && throws_rocks(ptr) && In_sokoban(g.u?.uz))
|| !goodpos(x, y, fakemon, gpflags)));
mndx = mons.indexOf(ptr);
}
// C ref: makemon.c:1234 — propagate() return value is ignored here.
propagate(mndx, countbirth, false);
// Create monster
const mtmp = makeMonst();
if (mmflags & MM_EPRI)
newepri(mtmp);
if (mmflags & MM_EMIN)
newemin(mtmp);
if (mmflags & MM_EDOG)
newedog(mtmp);
if (mmflags & MM_EGD)
newegd(mtmp);
if (mmflags & MM_ASLEEP)
mtmp.msleeping = 1;
mtmp.data = ptr;
mtmp.mnum = mndx;
// C ref: makemon.c:1252 — assign unique ID via next_ident() (rnd(2))
mtmp.m_id = next_ident();
// C ref: makemon.c:1254-1257 — cache current quest leader monster id.
if (ptr.msound === MS_LEADER && (g.urole?.ldrnum ?? -1) === mndx) {
g.quest_status = g.quest_status || {};
g.quest_status.leader_m_id = mtmp.m_id;
}
// Link into monster chain
mtmp.nmon = g.fmon;
g.fmon = mtmp;
// HP
newmonhp(mtmp, mndx);
// Gender
const femaleok = !is_male(ptr) && !is_neuter(ptr);
const maleok = !is_female(ptr) && !is_neuter(ptr);
if (is_female(ptr) || ((mmflags & MM_FEMALE) !== 0 && femaleok))
mtmp.female = true;
else if (is_male(ptr) || ((mmflags & MM_MALE) !== 0 && maleok))
mtmp.female = false;
// C ref: makemon.c:1271-1274 — quest leader/nemesis gender from role_init
else if (ptr.msound === MS_LEADER && quest_info(MS_LEADER) === mndx)
mtmp.female = !!(g.quest_status?.ldrgend);
else if (ptr.msound === MS_NEMESIS && quest_info(MS_NEMESIS) === mndx)
mtmp.female = !!(g.quest_status?.nemgend);
else
mtmp.female = femaleok ? !!rn2(2) : false;
// Place monster
mtmp.mx = x;
mtmp.my = y;
// Register on level.monsters array AND monMap AFTER mx/my are set.
// Must come after mx/my assignment so addMonster → setMonAt works.
if (g.level) g.level.addMonster(mtmp);
mtmp.mcansee = mtmp.mcanmove = true;
// C ref: makemon.c:1299 — MM_ANGRY forces hostile
mtmp.mpeaceful = (mmflags & MM_ANGRY) ? false : peace_minded(ptr);
if ((mmflags & MM_MINVIS) !== 0) {
mtmp.perminvis = true;
if (!mtmp.invis_blkd)
mtmp.minvis = true;
}
// ^makemon event emitted below, AFTER inventory creation (matches C ordering)
// C ref: makemon.c:1303-1338 switch(ptr->mlet)
// Keep this in the same phase before group spawning/inventory.
switch (ptr.mlet) {
case S_MIMIC:
set_mimic_sym(mtmp);
break;
case S_SPIDER:
case S_SNAKE:
if (g.in_mklev) {
if (x && y)
mkobj_at(RANDOM_CLASS, x, y, true);
hideunder(mtmp);
}
break;
case S_LIGHT:
case S_ELEMENTAL:
if (mndx === PM_STALKER || mndx === PM_BLACK_LIGHT) {
mtmp.perminvis = true;
mtmp.minvis = true;
}
break;
case S_EEL: {
// C ref: makemon.c S_EEL branch -> hideunder(mtmp) during level gen.
if (g.in_mklev) {
hideunder(mtmp);
}
break;
}
case S_BAT: {
// C ref: makemon.c:1343-1346 — hell bats are permanently fast.
const uz = g.u?.uz;
const inHell = !!uz && !!(g.dungeons?.[uz.dnum]?.flags?.hellish);
if (inHell && (mndx === PM_BAT || mndx === PM_GIANT_BAT || mndx === PM_VAMPIRE_BAT)) {
mtmp.permspeed = MFAST;
mtmp.mspeed = MFAST;
}
break;
}
default:
break;
}
// C ref: makemon.c:1352-1385 — special initial inventory item for this monster.
let mitem = STRANGE_OBJECT;
if (mndx === PM_VLAD_THE_IMPALER)
mitem = CANDELABRUM_OF_INVOCATION;
// C ref: makemon.c:1355-1368 — initialize shapechangers and initial form.
if (init_shapechanger_form(mtmp))
allow_minvent = false;
// C ref: makemon.c:1369-1383 — wizard bookkeeping and mitem special cases.
if (allow_minvent && mndx === PM_WIZARD_OF_YENDOR) {
mtmp.iswiz = true;
if (!g.context) g.context = {};
const prevWizCount = (typeof g.context.no_of_wizards === 'number')
? g.context.no_of_wizards
: 0;
g.context.no_of_wizards = prevWizCount + 1;
if (g.context.no_of_wizards === 1 && Is_earthlevel())
mitem = SPE_DIG;
} else if (mndx === PM_CROESUS) {
mitem = TWO_HANDED_SWORD;
} else if (ptr.msound === MS_NEMESIS) {
mitem = BELL_OF_OPENING;
} else if (mndx === PM_PESTILENCE) {
mitem = POT_SICKNESS;
}
if (allow_minvent && mitem !== STRANGE_OBJECT)
await mongets(mtmp, mitem);
// C ref: makemon.c:1398-1403 — demon princes with MS_BRIBE are peaceful+invisible
if (is_demon(ptr) && is_prince(ptr) && ptr.msound === MS_BRIBE) {
mtmp.mpeaceful = true;
mtmp.minvis = true;
mtmp.perminvis = true;
mtmp.mavenge = 0;
// C: if wielding Excalibur or Demonbane, demon is hostile
// TODO: u_wield_art check
}
// Sleep for some types during level generation
if (g.in_mklev) {
switch (ptr.mlet) {
case S_LEPRECHAUN:
mtmp.msleeping = 1;
break;
case S_JABBERWOCK:
case S_NYMPH:
if (rn2(5))
mtmp.msleeping = 1;
break;
}
// C ref: makemon.c:1387-1390
const isNDemon = is_demon(ptr) && !is_lord(ptr) && !is_prince(ptr);
if ((isNDemon || mndx === PM_WUMPUS || mndx === PM_LONG_WORM || mndx === PM_GIANT_EEL)
&& !g.u?.uhave?.amulet && rn2(5)) {
mtmp.msleeping = 1;
}
} else {
// C ref: makemon.c:1393-1396 — byyou during gameplay (not mklev)
if (byyou) {
set_apparxy(mtmp);
}
}
// C ref: makemon.c:1405-1408 — initialize long worm tail and placement.
if (mndx === PM_LONG_WORM && (mtmp.wormno = get_wormno()) !== 0) {
initworm(mtmp, allowtail ? rn2(5) : 0);
if (count_wsegs(mtmp)) {
place_worm_tail_randomly(mtmp, x, y);
}
}
// Group spawning
if (anymon && !(mmflags & MM_NOGRP)) {
if ((ptr.geno & G_SGROUP) && rn2(2))
await m_initgrp(mtmp, mtmp.mx, mtmp.my, 3, mmflags);
else if (ptr.geno & G_LGROUP) {
if (rn2(3))
await m_initgrp(mtmp, mtmp.mx, mtmp.my, 10, mmflags);
else
await m_initgrp(mtmp, mtmp.mx, mtmp.my, 3, mmflags);
}
}
if (mndx === PM_GHOST && !(mmflags & MM_NONAME)) {
mtmp.mname = rndghostname();
mtmp.mnamelth = mtmp.mname.length;
}
// Inventory
if (allow_minvent) {
if (is_armed(ptr))
await m_initweap(mtmp);
await m_initinv(mtmp);
await m_dowear(mtmp, true);
// Saddle check
if (!rn2(100)) {
// domestic + can_saddle check — very rare, skip for now
}
}
// C ref: makemon.c:1460-1469 — initialize strategy bits for waiters/covetous.
if (ptr.mflags3 && !(mmflags & MM_NOWAIT)) {
if (ptr.mflags3 & M3_WAITFORU)
mtmp.mstrategy |= STRAT_WAITFORU;
if (ptr.mflags3 & M3_CLOSE)
mtmp.mstrategy |= STRAT_CLOSE;
if (ptr.mflags3 & (M3_WAITMASK | M3_COVETOUS))
mtmp.mstrategy |= STRAT_APPEARMSG;
}
// C ref: makemon.c:1469-1470
if (allow_minvent && game.migrating_objs) {
await deliver_obj_to_mon(mtmp, 1, 0);
}
// C ref: makemon.c:1474 — ensure monster is shown during gameplay.
if (!g.in_mklev) {
newsym(mtmp.mx, mtmp.my);
}
// C ref: makemon.c:1475+ — appearance messaging path.
if (!g.in_mklev) {
if (!(mmflags & MM_NOMSG)) {
let what = null;
let exclaim = !(mmflags & MM_NOEXCLAM);
if ((canseemon(mtmp)
&& ((mtmp.m_ap_type ?? M_AP_NOTHING) === M_AP_NOTHING
|| (mtmp.m_ap_type ?? M_AP_NOTHING) === M_AP_MONSTER))
|| sensemon(mtmp)) {
what = Amonnam(mtmp);
if ((mtmp.m_ap_type ?? M_AP_NOTHING) === M_AP_MONSTER) {
exclaim = true;
}
}
if (what) {
set_msg_xy(mtmp.mx, mtmp.my);
const closeBy = dist2(x, y, g.u?.ux ?? 0, g.u?.uy ?? 0) <= (BOLT_LIM * BOLT_LIM);
const msg = `${what}${exclaim ? ' suddenly' : ''} ${vtense(what, 'appear')}`
+ `${next2u(x, y) ? ' next to you' : (closeBy ? ' close by' : '')}`
+ `${exclaim ? '!' : '.'}`;
// C ref: makemon.c uses Norep(...), not plain custompline().
// Suppress immediate repeats to avoid duplicate appearance
// messages shifting topline/botl event ordering.
if (msg !== g._pending_message && msg !== g._last_toplines) {
await custompline(msg);
}
}
}
// C ref: makemon.c:1504-1505 — if fiddling with an occupation,
// stop when a discernable threat appears.
if (g.occupation) {
await dochugw(mtmp, false);
}
}
// C ref: makemon.c:1430 — compute malign after all peaceful changes
set_malign(mtmp);
// C emits ^makemon AFTER inventory creation and appearance message, BEFORE <makemon
// C ref: makemon.c:1510 — uses mndx (requested kind) with mtmp->mx,my
// (actual placement). mndx and mtmp->mnum can diverge when the
// monster was polymorphed during init (e.g. chameleon-rolled).
pushRngLogEntry(`^makemon[${formatMonsterEventRef(mtmp, mndx, mtmp.mx, mtmp.my)}]`);
return mtmp;
} finally {
if (!suppressMidlog) pushRngLogEntry('<makemon');
}
}
// C ref: makemon.c:162 m_initweap() — equip monster with weapons/armor
// RNG: varies heavily by monster type. Each mongets calls mksobj.
async function m_initweap(mtmp) {
const ptr = mtmp.data;
if (!ptr) return;
const mm = mons.indexOf(ptr);
let otmp;
// C ref: makemon.c:170-171
if (Is_rogue_level(game.u?.uz))
return;
switch (ptr.mlet) {
case S_GIANT:
// C ref: makemon.c:182-187
if (rn2(2))
await mongets(mtmp, (mm !== PM_ETTIN) ? BOULDER : CLUB);
if ((mm !== PM_ETTIN) && !rn2(5))
await mongets(mtmp, rn2(2) ? TWO_HANDED_SWORD : BATTLE_AXE);
break;
case S_HUMAN:
if (is_mercenary(ptr)) {
let w1 = 0, w2 = 0;
switch (mm) {
case PM_WATCHMAN:
case PM_SOLDIER:
if (!rn2(3)) {
do {
w1 = rn1(BEC_DE_CORBIN - PARTISAN + 1, PARTISAN);
} while (game.objects[w1].oc_skill !== P_POLEARMS);
w2 = rn2(2) ? DAGGER : KNIFE;
} else
w1 = rn2(2) ? SPEAR : SHORT_SWORD;
break;
case PM_SERGEANT:
w1 = rn2(2) ? FLAIL : MACE;
break;
case PM_LIEUTENANT:
w1 = rn2(2) ? BROADSWORD : LONG_SWORD;
break;
case PM_CAPTAIN:
case PM_WATCH_CAPTAIN:
w1 = rn2(2) ? LONG_SWORD : SILVER_SABER;
break;
default:
if (!rn2(4))
w1 = DAGGER;
if (!rn2(7))
w2 = SPEAR;
break;
}
if (w1)
await mongets(mtmp, w1);
if (!w2 && w1 !== DAGGER && !rn2(4))
w2 = KNIFE;
if (w2)
await mongets(mtmp, w2);
} else if (is_elf(ptr)) {
if (rn2(2))
await mongets(mtmp, rn2(2) ? ELVEN_MITHRIL_COAT : ELVEN_CLOAK);
if (rn2(2))
await mongets(mtmp, ELVEN_LEATHER_HELM);
else if (!rn2(4))
await mongets(mtmp, ELVEN_BOOTS);
if (rn2(2))
await mongets(mtmp, ELVEN_DAGGER);
switch (rn2(3)) {
case 0:
if (!rn2(4))
await mongets(mtmp, ELVEN_SHIELD);
if (rn2(3))
await mongets(mtmp, ELVEN_SHORT_SWORD);
await mongets(mtmp, ELVEN_BOW);
await m_initthrow(mtmp, ELVEN_ARROW, 12);
break;
case 1:
await mongets(mtmp, ELVEN_BROADSWORD);
if (rn2(2))
await mongets(mtmp, ELVEN_SHIELD);
break;
case 2:
if (rn2(2)) {
await mongets(mtmp, ELVEN_SPEAR);
await mongets(mtmp, ELVEN_SHIELD);
}
break;
}
if (mm === PM_ELVEN_MONARCH) {
if (rn2(3) || (game.in_mklev && Is_earthlevel()))
await mongets(mtmp, PICK_AXE);
if (!rn2(50))
await mongets(mtmp, CRYSTAL_BALL);
}
} else if (ptr.msound === MS_PRIEST
|| quest_mon_represents_role(ptr, PM_CLERIC)) {
otmp = mksobj(MACE, false, false);
if (otmp) {
otmp.spe = rnd(3);
if (!rn2(2))
curse(otmp);
await mpickobj(mtmp, otmp);
}
} else if (mm === PM_NINJA) {
await mongets(mtmp, rn2(4) ? SHURIKEN : DART);
await mongets(mtmp, rn2(4) ? SHORT_SWORD : AXE);
} else if (ptr.msound === MS_GUARDIAN) {
switch (mm) {
case PM_STUDENT:
case PM_ATTENDANT:
case PM_ABBOT:
case PM_ACOLYTE:
case PM_GUIDE:
case PM_APPRENTICE:
if (rn2(2))
await mongets(mtmp, rn2(3) ? DAGGER : KNIFE);
if (rn2(5))
await mongets(mtmp, rn2(3) ? LEATHER_JACKET : LEATHER_CLOAK);
if (rn2(3))
await mongets(mtmp, rn2(3) ? LOW_BOOTS : HIGH_BOOTS);
if (rn2(3))
await mongets(mtmp, POT_HEALING);
break;
case PM_CHIEFTAIN:
case PM_PAGE:
case PM_ROSHI:
case PM_WARRIOR:
await mongets(mtmp, rn2(3) ? LONG_SWORD : SHORT_SWORD);
await mongets(mtmp, rn2(3) ? CHAIN_MAIL : LEATHER_ARMOR);
if (rn2(2))
await mongets(mtmp, rn2(2) ? LOW_BOOTS : HIGH_BOOTS);
if (!rn2(3))
await mongets(mtmp, LEATHER_CLOAK);
if (!rn2(3)) {
await mongets(mtmp, BOW);
await m_initthrow(mtmp, ARROW, 12);
}
break;
case PM_HUNTER:
await mongets(mtmp, rn2(3) ? SHORT_SWORD : DAGGER);
if (rn2(2))
await mongets(mtmp, rn2(2) ? LEATHER_JACKET : LEATHER_ARMOR);
await mongets(mtmp, BOW);
await m_initthrow(mtmp, ARROW, 12);
break;
case PM_THUG:
await mongets(mtmp, CLUB);
await mongets(mtmp, rn2(3) ? DAGGER : KNIFE);
if (rn2(2))
await mongets(mtmp, LEATHER_GLOVES);
await mongets(mtmp, rn2(2) ? LEATHER_JACKET : LEATHER_ARMOR);
break;
case PM_NEANDERTHAL:
await mongets(mtmp, CLUB);
await mongets(mtmp, LEATHER_ARMOR);
break;
}
}
break;
case S_ANGEL:
if (humanoid(ptr)) {
{
const typ = rn2(3) ? LONG_SWORD : SILVER_MACE;
otmp = mksobj(typ, false, false);
if (otmp) {
// C: maybe promote to artifact — skipped, no oname() yet
// Consume C's promotion gate RNG even though oname() isn't ported yet.
const artifactGate = !rn2(20) || is_lord(ptr);
void artifactGate;
bless(otmp);
otmp.oerodeproof = true;
otmp.spe = rn2(4);
if (typ === SILVER_MACE)
otmp.spe += 3;
await mpickobj(mtmp, otmp);
}
}
otmp = mksobj((!rn2(4) || is_lord(ptr)) ? SHIELD_OF_REFLECTION
: LARGE_SHIELD,
false, false);
if (otmp) {
otmp.oerodeproof = true;
otmp.spe = 0;
await mpickobj(mtmp, otmp);
}
}
break;
case S_HUMANOID:
if (mm === PM_HOBBIT) {
switch (rn2(3)) {
case 0:
await mongets(mtmp, DAGGER);
break;
case 1:
await mongets(mtmp, ELVEN_DAGGER);
break;
case 2:
await mongets(mtmp, SLING);
await m_initthrow(mtmp, !rn2(4) ? FLINT : ROCK, 6);
break;
}
if (!rn2(10))
await mongets(mtmp, ELVEN_MITHRIL_COAT);
if (!rn2(10))
await mongets(mtmp, DWARVISH_CLOAK);
} else if (is_dwarf(ptr)) {
if (rn2(7))
await mongets(mtmp, DWARVISH_CLOAK);
if (rn2(7))
await mongets(mtmp, IRON_SHOES);
if (!rn2(4)) {
await mongets(mtmp, DWARVISH_SHORT_SWORD);
if (rn2(2))
await mongets(mtmp, DWARVISH_MATTOCK);
else {
await mongets(mtmp, rn2(2) ? AXE : DWARVISH_SPEAR);
await mongets(mtmp, DWARVISH_ROUNDSHIELD);
}
await mongets(mtmp, DWARVISH_IRON_HELM);
if (!rn2(3))
await mongets(mtmp, DWARVISH_MITHRIL_COAT);
} else {
await mongets(mtmp, !rn2(3) ? PICK_AXE : DAGGER);
}
}
break;
case S_KOP:
if (!rn2(4))
await m_initthrow(mtmp, CREAM_PIE, 2);
if (!rn2(3))
await mongets(mtmp, rn2(2) ? CLUB : RUBBER_HOSE);
break;
case S_ORC:
// C ref: makemon.c:412-447
if (rn2(2))
await mongets(mtmp, ORCISH_HELM);
{
const switchVal = (mm !== PM_ORC_CAPTAIN)
? mm
: (rn2(2) ? PM_MORDOR_ORC : PM_URUK_HAI);
switch (switchVal) {
case PM_MORDOR_ORC:
if (!rn2(3))
await mongets(mtmp, SCIMITAR);
if (!rn2(3))
await mongets(mtmp, ORCISH_SHIELD);
if (!rn2(3))
await mongets(mtmp, KNIFE);
if (!rn2(3))
await mongets(mtmp, ORCISH_CHAIN_MAIL);
break;
case PM_URUK_HAI:
if (!rn2(3))
await mongets(mtmp, ORCISH_CLOAK);
if (!rn2(3))
await mongets(mtmp, ORCISH_SHORT_SWORD);
if (!rn2(3))
await mongets(mtmp, IRON_SHOES);
if (!rn2(3)) {
await mongets(mtmp, ORCISH_BOW);
await m_initthrow(mtmp, ORCISH_ARROW, 12);
}
if (!rn2(3))
await mongets(mtmp, URUK_HAI_SHIELD);
break;
default:
if (mm !== PM_ORC_SHAMAN && rn2(2))
await mongets(mtmp, (mm === PM_GOBLIN || rn2(2) === 0)
? ORCISH_DAGGER : SCIMITAR);
}
}
break;
case S_OGRE:
if (!rn2(mm === PM_OGRE_TYRANT ? 3 : mm === PM_OGRE_LEADER ? 6 : 12))
await mongets(mtmp, BATTLE_AXE);
else
await mongets(mtmp, CLUB);
break;
case S_TROLL:
if (!rn2(2))
switch (rn2(4)) {
case 0: await mongets(mtmp, RANSEUR); break;
case 1: await mongets(mtmp, PARTISAN); break;
case 2: await mongets(mtmp, GLAIVE); break;
case 3: await mongets(mtmp, SPETUM); break;
}
break;
case S_KOBOLD:
if (!rn2(4))
await m_initthrow(mtmp, DART, 12);
break;
case S_CENTAUR:
// C ref: makemon.c:476-486
if (rn2(2)) {
if (ptr === mons[PM_FOREST_CENTAUR]) {
await mongets(mtmp, BOW);
await m_initthrow(mtmp, ARROW, 12);
} else {
await mongets(mtmp, CROSSBOW);
await m_initthrow(mtmp, CROSSBOW_BOLT, 12);
}
}
break;
case S_WRAITH:
await mongets(mtmp, KNIFE);
await mongets(mtmp, LONG_SWORD);
break;
case S_ZOMBIE:
if (!rn2(4))
await mongets(mtmp, LEATHER_ARMOR);
if (!rn2(4))
await mongets(mtmp, rn2(3) ? KNIFE : SHORT_SWORD);
break;
case S_LIZARD:
if (mm === PM_SALAMANDER)
await mongets(mtmp, rn2(7) ? SPEAR : rn2(3) ? TRIDENT : STILETTO);
break;
case S_DEMON:
switch (mm) {
case PM_BALROG:
await mongets(mtmp, BULLWHIP);
await mongets(mtmp, BROADSWORD);
break;
case PM_ORCUS:
await mongets(mtmp, WAN_DEATH);
break;
case PM_HORNED_DEVIL:
await mongets(mtmp, rn2(4) ? TRIDENT : BULLWHIP);
break;
case PM_DISPATER:
await mongets(mtmp, WAN_STRIKING);
break;
case PM_YEENOGHU:
await mongets(mtmp, FLAIL);
break;
}
// C: if (!is_demon(ptr)) break; FALLTHROUGH to default
if (!is_demon(ptr))
break;
/* falls through */
default: {
// C ref: makemon.c:528-568 — general weapon assignment
const bias = (is_lord(ptr) ? 1 : 0)
+ (is_prince(ptr) ? 2 : 0)
+ (extra_nasty(ptr) ? 1 : 0);
switch (rnd(14 - (2 * bias))) {
case 1:
if (strongmonst(ptr))
await mongets(mtmp, BATTLE_AXE);
else
await m_initthrow(mtmp, DART, 12);
break;
case 2:
if (strongmonst(ptr))
await mongets(mtmp, TWO_HANDED_SWORD);
else {
await mongets(mtmp, CROSSBOW);
await m_initthrow(mtmp, CROSSBOW_BOLT, 12);
}
break;
case 3:
await mongets(mtmp, BOW);
await m_initthrow(mtmp, ARROW, 12);
break;
case 4:
if (strongmonst(ptr))
await mongets(mtmp, LONG_SWORD);
else
await m_initthrow(mtmp, DAGGER, 3);
break;
case 5:
if (strongmonst(ptr))
await mongets(mtmp, LUCERN_HAMMER);
else
await mongets(mtmp, AKLYS);
break;
default:
break;
}
break;
}
}
// C ref: makemon.c:572 — offensive item chance
if (mtmp.m_lev > rn2(75))
await mongets(mtmp, rnd_offensive_item(mtmp));
}
// C ref: makemon.c:1736 mk_gen_ok()
function mk_gen_ok(mndx, mvflagsmask, genomask) {
const ptr = mons[mndx];
if (!ptr) return false;
if ((game.mvitals?.[mndx]?.mvflags ?? 0) & mvflagsmask)
return false;
if (ptr.geno & genomask)
return false;
if (is_placeholder(mndx))
return false;
return true;
}
// C ref: mondata.h — placeholder monster types
function is_placeholder(mndx) {
return mndx === PM_ORC || mndx === PM_GIANT
|| mndx === PM_ELF || mndx === PM_HUMAN;
}
// C ref: makemon.c:1542 mbirth_limit()
function mbirth_limit(mndx) {
return mndx === PM_NAZGUL ? 9 : mndx === PM_ERINYS ? 3 : MAXMONNO;
}
// C ref: makemon.c:959 propagate()
function propagate(mndx, tally, ghostly) {
if (!Array.isArray(game.mvitals))
game.mvitals = [];
if (!game.mvitals[mndx])
game.mvitals[mndx] = { mvflags: mons[mndx]?.geno & G_NOCORPSE, born: 0 };
const mv = game.mvitals[mndx];
const lim = mbirth_limit(mndx);
const born = mv.born ?? 0;
const gone = !!(mv.mvflags & G_GONE);
const result = (born < lim && !gone);
if ((mons[mndx].geno & G_UNIQ) !== 0 && mndx !== PM_HIGH_CLERIC)
mv.mvflags |= G_EXTINCT;
if (born < 255 && tally && (!ghostly || result))
mv.born = born + 1;
if ((mv.born ?? 0) >= lim
&& !(mons[mndx].geno & G_NOGEN)
&& !(mv.mvflags & G_EXTINCT))
mv.mvflags |= G_EXTINCT;
return result;
}
// C ref: makemon.c:1804 init_mongen_order()
// Sorts monster indices by (mlet << 8 | difficulty) for mkclass
function init_mongen_order() {
if (game.mongen_order) return;
// C: initialize all NUMMONS entries, compute mclass_maxf over all
game.mongen_order = [];
game.mclass_maxf = {};
for (let i = LOW_PM; i < mons.length; i++) {
game.mongen_order.push(i);
const mlet = mons[i].mlet;
const freq = mons[i].geno & G_FREQ;
if (freq > (game.mclass_maxf[mlet] || 0))
game.mclass_maxf[mlet] = freq;
}
// C: qsort(mongen_order, SPECIAL_PM, ...) — only sort regular monsters
// Entries from SPECIAL_PM onwards (quest/player monsters) stay in
// original mons[] order and are never reached by mkclass/rndmonst.
const regularPart = game.mongen_order.slice(0, SPECIAL_PM);
const specialPart = game.mongen_order.slice(SPECIAL_PM);
regularPart.sort((a, b) => {
const da = (mons[a].difficulty | (mons[a].mlet << 8));
const db = (mons[b].difficulty | (mons[b].mlet << 8));
return da - db;
});
game.mongen_order = regularPart.concat(specialPart);
}
const G_IGNORE = 0x8000;
// C ref: makemon.c:1870 mkclass()
export function mkclass(monclass, spc) {
return mkclass_aligned(monclass, spc, A_NONE);
}
// C ref: makemon.c:1877 mkclass_aligned()
function mkclass_aligned(monclass, spc, atyp) {
init_mongen_order();
const MONSi = (i) => game.mongen_order[i];
let num = 0;
const nums = new Array(SPECIAL_PM + 1).fill(0);
const maxmlev = level_difficulty() >> 1;
const dnum = game.u?.uz?.dnum ?? 0;
const gehennom = !!(game.dungeons?.[dnum]?.flags?.hellish);
const zero_freq_for_entire_class = !game.mclass_maxf[monclass];
if (monclass < 1) return null;
// Find first and last monster of this class in sorted order
let first;
for (first = LOW_PM; first < SPECIAL_PM; first++) {
if (mons[MONSi(first)]?.mlet === monclass) break;
}
if (first >= SPECIAL_PM) return null;
let mv_mask = G_GONE;
if (spc & G_IGNORE) {
mv_mask = 0;
spc &= ~G_IGNORE;
}
let last;
for (last = first; last < SPECIAL_PM && mons[MONSi(last)]?.mlet === monclass; last++) {
if (atyp !== A_NONE && Math.sign(mons[MONSi(last)].maligntyp || 0) !== Math.sign(atyp))
continue;
let gn_mask = G_NOGEN | G_UNIQ;
if (rn2(9) || monclass === S_LICH)
gn_mask |= (gehennom ? G_NOHELL : G_HELL);
gn_mask &= ~spc;
if (mk_gen_ok(MONSi(last), mv_mask, gn_mask)) {
if (num && montoostrong(MONSi(last), maxmlev)
&& mons[MONSi(last)].difficulty > mons[MONSi(last - 1)].difficulty
&& rn2(2))
break;
let k = mons[MONSi(last)].geno & G_FREQ;
if (!k && zero_freq_for_entire_class) k = 1;
if (k > 0) {
nums[MONSi(last)] = k + 1 - (adj_lev(mons[MONSi(last)]) > (game.u?.ulevel ?? 1) * 2 ? 1 : 0);
num += nums[MONSi(last)];
}
}
}
if (!num) return null;
// Select from weighted candidates
let pick = rnd(num);
for (let i = first; i < last; i++) {
pick -= nums[MONSi(i)];
if (pick <= 0) {
return nums[MONSi(i)] ? mons[MONSi(i)] : null;
}
}
return null;
}
// C ref: dog.c — makedog()
export async function makedog() {
const g = game;
if (g.preferred_pet === 'n') {
g.context.startingpet_typ = NON_PM;
return null;
}
const pettype = pet_type();
g.context.startingpet_typ = pettype;
// C ref: dog.c:232-248 — default pet names by role
let petname = '';
if (pettype === PM_LITTLE_DOG) {
// Role-specific dog names
// C ref: dog.c:239 — role-specific dog names
if (Role_if(PM_CAVE_DWELLER)) petname = 'Slasher';
else if (Role_if(PM_SAMURAI)) petname = 'Hachi';
else if (Role_if(PM_BARBARIAN)) petname = 'Idefix';
else if (Role_if(PM_RANGER)) petname = 'Sirius';
}
const mtmp = await makemon(mons[pettype], g.u.ux, g.u.uy, MM_EDOG | NO_MINVENT);
if (!mtmp) return null;
// C ref: dog.c:279 — christen_monst sets mextra.mgivenname
if (petname) {
christen_monst(mtmp, petname);
}
// C ref: dog.c:450 — set pet's knowledge of hero position
mtmp.mux = g.u.ux;
mtmp.muy = g.u.uy;
if (!g.context.startingpet_mid) {
g.context.startingpet_mid = mtmp.m_id;
// C ref: dog.c:263-267 — knight's pony starts with a saddle
if (!g.u.uroleplay?.pauper && pettype === PM_PONY) {
await put_saddle_on_mon(null, mtmp);
}
}
initedog(mtmp, true); // everything=true for initial pet
return mtmp;
}
// C: clone_mon(mon, x, y) — create a clone of a monster
export function clone_mon(mon, x, y) {
// TODO: port from makemon.c
return null;
}
export default { makeMonst, makemon, rndmonst, mkclass, makedog, mongets, clone_mon };
export function init_makemon_globals() {
game.mongen_order = null;
game.mclass_maxf = null;
}
|