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 | 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 5302x 5302x 73x 73x 53x 53x 53x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4181x 4180x 4181x 18x 18x 18x 18x 18x 18x 18x 18x 4181x 4181x 4181x 4180x 4180x 4180x 4180x 4180x 4180x 4180x 4178x 4178x 4178x 4181x 73x 73x 73x 812x 812x 812x 812x 812x 812x 812x 812x 812x 812x 812x 812x 812x 812x 812x 9x 9x 9x 9x 812x 3x 3x 3x 3x 812x 812x 812x 9x 9x 9x 9x 812x 6x 6x 6x 6x 6x 812x 812x 812x 812x 812x 73x 73x 73x 73x 73x 73x 73x 73x 4370x 4370x 4370x 4370x 4370x 4370x 4370x 4370x 4370x 4370x 4370x 4370x 4370x 4370x 4370x 4370x 4370x 4370x 4370x 1830x 1830x 1830x 1830x 1830x 1830x 1830x 1830x 22x 22x 22x 22x 1830x 4370x 4370x 4370x 4370x 4370x 4370x 4370x 21x 21x 21x 4370x 4370x 73x 73x 73x 73x 73x 4370x 4370x 4370x 4370x 4370x 4370x 4370x 1x 1x 4370x 4370x 4370x 266x 266x 4370x 2x 2x 266x 266x 4370x 266x 266x 266x 266x 266x 266x 266x 266x 266x 266x 1x 1x 1x 266x 266x 266x 266x 266x 266x 266x 3x 3x 3x 266x 262x 266x 266x 266x 266x 266x 266x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 27x 27x 27x 27x 27x 27x 27x 20x 27x 5x 5x 5x 27x 1x 1x 266x 4370x 73x 73x 73x 1995x 1970x 1970x 1970x 1970x 1970x 1970x 1970x 1970x 1970x 1995x 73x 73x 73x 73x 73x 73x 73x 73x 1590585x 1590585x 1590585x 1590585x 73x 73x 73x 73x 73x 877716x 877716x 877716x 877716x 877716x 72008x 877716x 877716x 73x 73x 73x 73x 79x 79x 79x 79x 73x 73x 73x 73x 66792x 66792x 66792x 66792x 66792x 73x 73x 73x 73x 2x 2x 2x 2x 2x 2x 2x 2x 73x 73x 73x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 3x 3x 3x 3x 6x 73x 73x 73x 73x 3105812x 3105812x 3105812x 3105812x 3105812x 3105812x 3105812x 3105812x 3105812x 3105812x 1190923x 1190923x 26x 1190923x 1190897x 1x 1x 1x 1x 1x 1190897x 1190896x 1190896x 1x 1x 1x 1x 1190895x 1190895x 1190895x 1190895x 1190895x 3105812x 84888x 10521x 10521x 10521x 10521x 10521x 10521x 10521x 113x 113x 88x 88x 88x 88x 88x 113x 25x 6x 6x 6x 6x 6x 6x 6x 6x 6x 25x 19x 19x 25x 10521x 10408x 10408x 113x 113x 84888x 74367x 74367x 84888x 1889993x 3105812x 6x 6x 3x 3x 3x 3x 3x 3x 3105804x 1889990x 3105812x 1757354x 1757354x 1757354x 1757354x 1757354x 1757354x 3105804x 942123x 3105812x 3105812x 3105812x 8898x 8898x 8898x 8898x 933225x 3105812x 1080x 1080x 1080x 20x 20x 20x 20x 20x 1080x 1060x 1060x 1060x 1060x 1060x 1060x 1060x 1080x 1080x 933222x 933222x 3105812x 73x 84775x 84775x 84775x 19659x 5x 5x 5x 5x 19659x 19659x 65116x 84775x 73x 73x 73x 73x 1245x 1245x 1245x 1245x 1245x 1245x 1245x 1245x 1245x 1245x 1245x 1245x 1245x 1245x 1245x 1245x 1245x 123x 1244x 1122x 1122x 1245x 1245x 1245x 1257x 1257x 1257x 1257x 1257x 50527x 50527x 198256x 198256x 198256x 1580666x 1580666x 1580666x 1580666x 1580666x 1580666x 1580666x 44374x 29929x 12476x 12476x 12476x 29929x 29929x 44374x 1550135x 1580666x 922438x 1165x 1165x 1165x 1165x 1165x 20x 20x 20x 20x 1165x 1165x 1165x 922438x 188131x 188131x 188131x 922438x 1580666x 197091x 49362x 49362x 92x 1204x 13x 13x 13x 13x 13x 21567x 21567x 712x 712x 59x 59x 712x 16x 16x 16x 712x 21567x 13x 1x 1x 1x 1x 12x 12x 12x 12x 79x 79x 1245x 1245x 73x 73x 73x 73x 135x 110x 110x 110x 110x 110x 135x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 1057x 1057x 1057x 1057x 1057x 9424x 7966x 9424x 9424x 9424x 9424x 135x 135x 135x 9424x 1057x 1057x 1057x 73x 73x 73x 73x 73x 94x 94x 94x 94x 94x 94x 94x 94x 94x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 70x 70x 94x 70x 94x 73x 73x 73x 73x 73x 73x 73x 1622x 1622x 1622x 1622x 1622x 1622x 1622x 1622x 1622x 1622x 1622x 1622x 1622x 1622x 1622x 1622x 73x 73x 701x 701x 701x 701x 701x 701x 701x 73x 73x 73x 59x 59x 59x 59x 73x 69x 69x 69x 69x 73x 73x 73x 73x 2232x 2232x 2232x 2198x 2232x 2198x 2232x 2232x 2232x 2232x 2232x 2232x 6574x 19703x 19703x 19703x 17514x 17514x 17514x 19703x 19703x 319x 319x 19x 19x 319x 17495x 17495x 19703x 19703x 8583x 8583x 8583x 8583x 8583x 19703x 18x 18x 18x 19703x 8583x 8583x 8583x 8583x 5529x 5529x 8583x 3054x 3054x 45x 45x 23x 3054x 2698x 2999x 311x 3x 3x 3x 311x 308x 308x 172x 2x 2x 2x 2x 2x 2x 308x 8583x 2857x 2857x 2857x 19703x 2805x 2805x 2805x 2805x 2804x 2805x 2209x 2209x 2261x 2261x 6552x 2176x 2176x 2232x 2176x 2176x 2232x 2232x 315x 315x 213x 213x 50x 50x 315x 315x 315x 2232x 73x 73x 73x 83x 83x 83x 83x 80x 83x 83x 83x 83x 83x 83x 83x 73x 73x 143x 143x 143x 73x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 73x 73x 135x 135x 135x 73x 6x 135x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 135x 73x 73x 43x 43x 43x 43x 43x 43x 5x 5x 5x 5x 5x 5x 5x 38x 43x 43x 43x 40x 5x 5x 5x 5x 4x 4x 4x 4x 4x 2x 40x 43x 73x 73x 73x 73x 73x 73x 1003x 1003x 73x 73x 73x 73x 346x 346x 73x 73x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 73x 73x 78x 78x 78x 78x 78x 78x 78x 73x 73x 15028x 15028x 15028x 15028x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 54x 54x 54x 73x 73x 73x 73x 32x 32x 32x 32x 32x 32x 32x 24x 24x 24x 21x 29x 29x 29x 32x 32x 32x 32x 29x 29x 32x 73x 73x 73x 73x 73x 73x 73x 73x 2683x 2683x 2683x 2683x 2683x 2683x 2683x 19x 19x 2664x 2680x 2680x 2649x 2672x 2328x 2328x 4x 4x 2328x 2645x 2645x 2683x 73x 73x 73x 73x 4x 4x 4x 4x 73x 73x 73x 4x 4x 4x 4x 4x 4x 4x 4x 73x 73x 16x 16x 16x 16x 16x 16x 16x 73x 73x 16x 16x 16x 16x 16x 16x 5x 5x 5x 16x 16x 16x 16x 16x 16x 16x 5x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 73x 73x 4x 4x 4x 4x 4x 4x 4x 4x 4x 73x 73x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 4x 3x 3x 4x 73x 73x 73x 73x 20x 20x 20x 20x 20x 453x 453x 453x 20x 73x 73x 73x 20x 20x 20x 20x 20x 20x 20x 73x 73x 16x 16x 73x 73x 73x 73x 73x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 180x 20x 20x 20x 180x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 5x 5x 5x 16x 16x 20x 4x 4x 4x 20x 16x 20x 73x 73x 72x 72x 72x 72x 72x 72x 72x | // hack.js -- Movement/state helpers (port of hack.c)
// C ref: hack.c — 4540 lines
//
// Ported: end_running, unmul, losehp, showdamage, saving_grace,
// maybe_wail, invocation_pos, check_capacity, rounddiv,
// urgent_pline, spot_checks, inv_cnt, money_cnt,
// test_move, findtravelpath,
// monster_nearby, pickup_checks, lookaround,
// doorless_door, crawl_destination
// domove/domove_core live in cmd.js as the canonical port; this file
// holds the domove-leaf helpers (carrying_too_much, move_out_of_bounds,
// escape_from_sticky_mon, swim_move_danger) imported by cmd.js.
import { game } from './gstate.js';
import { done as end_done } from './end.js';
import { dotrap, float_up, uescaped_shaft, uteetering_at_seen_pit, sokoban_guilt, seetrap, fill_pit, blow_up_landmine, deltrap } from './trap.js';
import { SET_BOTL, setBotl, setTimeBotl, feel_location, glyph_at, map_invisible, map_object, mon_visible, canspotmon, newsym, unmap_object } from './display.js';
import { glyph_is_invisible } from './glyph.js';
import { cansee, couldsee } from './vision.js';
import { amorphous, attacktype, bigmonst, digests, is_hider, is_pick, is_whirly, monsndx, needspick, noattacks, noncorporeal, nonliving, passes_walls, slithy, sticks, throws_rocks, tunnels, verysmall } from './mondata.js';
import { float_vs_flight, rehumanize } from './polyself.js';
import { pushCtxMoveLog, pushMultiLog, pushRngLogEntry, rn2, rnd } from './rng.js';
import { engr_at, wipe_engr_at } from './engrave.js';
import { custompline, pline, pline_The, There, You, You_cant, You_hear } from './pline.js';
import {
Blind,
Confusion,
Deaf,
Flying,
Fumbling,
Hallucination,
Is_airlevel,
Is_waterlevel,
Levitation,
Passes_walls,
Race_if,
Role_if,
Slimed,
Stunned,
Sokoban,
u_locomotion,
Underwater,
Upolyd
} from './macros.js';
import { PM_WIZARD, PM_VALKYRIE, PM_ELF, PM_GRID_BUG, PM_HEZROU, PM_ORACLE, PM_STEAM_VORTEX, AD_FIRE, AD_DGST, AD_RBRE, AT_HUGS, mons } from './monsters.js';
import { COIN_CLASS, BOULDER, PICK_AXE, DWARVISH_MATTOCK, WAN_DIGGING } from './objects.js';
import { A_DEX, A_STR, ANTHOLE, BARRACKS, BEEHIVE, COCKNEST, COLD_RES, COLNO, CORR, COURT, D_BROKEN, D_ISOPEN, D_NODOOR, def_warnsyms, DIED, DISINT_RES, DO_MOVE, EXT_ENCUMBER, FAILEDUNTRAP, FAST, FIRE_RES, FLYING, FROMOUTSIDE, HEADSTONE, HOLE, ICE, IN_SIGHT, INTRINSIC, INVIS, IRONBARS, IS_AIR, IS_ALTAR, IS_DOOR, IS_FOUNTAIN, IS_GRAVE, IS_OBSTRUCTED, is_pit, IS_SINK, IS_STWALL, IS_THRONE, IS_WATERWALL, isok, LANDMINE, LAVAPOOL, LAVAWALL, LEPREHALL, LEVEL_TELEP, LEVITATION, M_AP_FURNITURE, M_AP_OBJECT, MIGR_RANDOM, MOAT, MORGUE, N_DIRS, NO_TRAP, OBJ_AT, OROOM, P_BASIC, P_RIDING, PIT, POISON_RES, POOL, ROOM, ROOMOFFSET, ROWNO, SEE_INVIS, SHOCK_RES, SHOPBASE, SLEEP_RES, SPIKED_PIT, STAIRS, STEALTH, STONE, SWAMP, TELEPORT, TELEPORT_CONTROL, TELEP_TRAP, TEMPLE, TEST_MOVE, TEST_TRAP, TEST_TRAV, TRAPDOOR, TRAVP_GUESS, TRAVP_TRAVEL, TRAVP_VALID, VIBRATING_SQUARE, W_NONDIGGABLE, W_NONPASSWALL, WATER, WT_SQUEEZABLE_INV, xdir, ydir, ZOO } from './const.js';
import { depth, dist2, distmin, on_level, s_suffix, sgn, surface, upstart } from './hacklib.js';
import { m_at, t_at } from './map_access.js';
import { goodpos } from './makemon.js';
import { closed_door, onscary } from './monmove.js';
import { use_pick_axe2, bury_objs } from './dig.js';
import { xkilled } from './uhitm.js';
import { doopen_indir } from './lock.js';
import { ACURR, exercise } from './attrib.js';
import { obj_extract_self, place_object, remove_object, rloco, sobj_at } from './mkobj.js';
import { autopick_testobj, inv_weight, near_capacity, pickup, weight_cap } from './pickup.js';
import { waterbody_name } from './pager.js';
import { intemple } from './priest.js';
import { nomul as cmd_nomul } from './cmd.js';
import { xname, the, The, Tobjnam, otense } from './objnam.js';
import { a_monnam } from './mon.js';
import { boulder_hits_pool, flooreffects } from './do.js';
import { addtobill, costly_spot, find_objowner, in_rooms, onshopbill, shop_keeper, stolen_value, subfrombill } from './shk.js';
import { useupf } from './eat.js';
import { carrying } from './invent.js';
import { P_SKILL } from './weapon.js';
import { Soundeffect } from './sounds.js';
import { get_level } from './dungeon.js';
// ── Local constants ──
// C: dirs_ord[] = { DIR_W, DIR_N, DIR_E, DIR_S, DIR_NW, DIR_NE, DIR_SE, DIR_SW }
const dirs_ord = [0, 2, 4, 6, 1, 3, 5, 7];
export function NODIAG(monnum) { return monnum === PM_GRID_BUG; }
// C ref: hack.c:2398 — u_maybe_impaired()
export function u_maybe_impaired() {
return !!(Stunned() || (Confusion() && !rn2(5)));
}
export function next2u(x, y) {
return Math.abs(x - game.u.ux) <= 1 && Math.abs(y - game.u.uy) <= 1
&& !(x === game.u.ux && y === game.u.uy);
}
// gbuf-cleanup Phase 5: feel_location retired from hack.js — the
// canonical implementation lives in display.js (C ref: display.c:751).
// Phase 8: unmap_invisible likewise moved to display.js (C ref: display.c:1098).
// Callers import them directly from display.js now.
export function reset_occupations() { }
// C: wait_synch() — synchronize display/input
export function wait_synch() { }
// ── spoteffects ──
// C ref: hack.c:3231
// Called after hero moves onto a new square. Handles traps, object display,
// and autopickup. Order matters: non-pit traps get pickup BEFORE dotrap;
// pit traps get dotrap BEFORE pickup.
// Reset spoteffects state between sessions
export function resetSpoteffectsState() {
game.inspoteffects = 0;
game.spotloc_x = game.spotloc_y = 0;
game.spotterrain = STONE;
game.spottrap = null;
game.spottraptyp = NO_TRAP;
}
export async function spoteffects(pick) {
const g = game;
const u = g.u;
if (!u || !g.level) return;
const trap = t_at(u.ux, u.uy);
const trapflag = (g.iflags?.failing_untrap) ? FAILEDUNTRAP : 0;
/* prevent recursion from affecting the hero all over again */
const curterrain = g.level.at(u.ux, u.uy)?.typ ?? STONE;
if (game.inspoteffects && game.spotloc_x === u.ux && game.spotloc_y === u.uy
&& game.spotterrain === curterrain
&& (!game.spottrap || !trap || trap.ttyp === game.spottraptyp))
return;
/* defer if in_lava_effects */
if (g.iflags?.in_lava_effects)
return;
++game.inspoteffects;
game.spotterrain = curterrain;
game.spotloc_x = u.ux;
game.spotloc_y = u.uy;
/* moving onto different terrain might cause Lev or Fly to toggle */
const priorTerrain = g.level.at(u.ux0, u.uy0)?.typ ?? STONE;
const terrainChanged = (curterrain !== priorTerrain) || !on_level(u.uz, u.uz0);
if (terrainChanged)
await switch_terrain();
/* pooleffects — TODO: not yet ported; returns false for now */
// if (pooleffects(TRUE)) goto spotdone;
await check_special_room(false);
/* dosinkfall — TODO: not yet ported */
// if (IS_SINK(...) && Levitation) dosinkfall();
if (!g.in_steed_dismounting) {
/* levitation timeout adjustment — TODO: float_down not yet ported */
// skipped: rn2(2) + float_down path
/*
* If not a pit, pickup before triggering trap.
* If pit, trigger trap before pickup.
*/
const pit = (trap && is_pit(trap.ttyp));
if (pick && !pit)
await pickup(1);
if (trap) {
if (!game.spottrap || game.spottraptyp !== trap.ttyp) {
game.spottrap = trap;
game.spottraptyp = trap.ttyp;
await dotrap(trap, trapflag);
game.spottrap = null;
game.spottraptyp = NO_TRAP;
}
}
if (pick && pit)
await pickup(1);
}
/* ice warning — TODO: Warning intrinsic + spot_time_left not yet ported */
/* monster at location — TODO: piercer/surprise handling not yet ported */
// spotdone:
if (!--game.inspoteffects) {
game.spotterrain = STONE;
game.spotloc_x = game.spotloc_y = 0;
}
}
// ── switch_terrain — C ref: hack.c:3103 ──
// Moving onto obstructed terrain blocks levitation/flight; leaving restores.
export async function switch_terrain() {
const g = game;
const u = g.u;
const lev = g.level?.at(u.ux, u.uy);
if (!lev) return;
const blocklev = IS_OBSTRUCTED(lev.typ) || closed_door(u.ux, u.uy)
|| IS_WATERWALL(lev.typ) || lev.typ === LAVAWALL;
const was_levitating = !!Levitation();
const was_flying = !!Flying();
const uprops = u.uprops;
if (!uprops) return;
// C ref: hack.c:3111-3122 — levitation blocking
if (blocklev) {
if (Levitation())
await You_cant('levitate in here.');
if (!uprops[LEVITATION]) uprops[LEVITATION] = {};
uprops[LEVITATION].blocked = (uprops[LEVITATION].blocked || 0) | FROMOUTSIDE;
} else if (uprops[LEVITATION]?.blocked) {
uprops[LEVITATION].blocked &= ~FROMOUTSIDE;
if (Levitation() || uprops[LEVITATION].blocked)
await float_up();
}
// C ref: hack.c:3124-3136 — flight blocking
if (blocklev) {
if (Flying())
await You_cant('fly in here.');
if (!uprops[FLYING]) uprops[FLYING] = {};
uprops[FLYING].blocked = (uprops[FLYING].blocked || 0) | FROMOUTSIDE;
} else if (uprops[FLYING]?.blocked) {
uprops[FLYING].blocked &= ~FROMOUTSIDE;
float_vs_flight();
if (Flying())
await You('start flying.');
}
// C ref: hack.c:3137-3138 — update status if Lev/Fly changed
if ((!!Levitation() ^ was_levitating) || (!!Flying() ^ was_flying))
setBotl('switch_terrain');
}
// float_vs_flight: imported from polyself.js
// C ref: hack.c float_up — start levitating
// float_up: imported from trap.js
// ── move_update — C ref: hack.c:3504 ──
// Updates u.urooms, u.ushops, u.uentered, u.ushops_entered based on
// the hero's current position. Called from check_special_room.
function move_update(newlev) {
const g = game;
const u = g.u;
// Save previous room state
u.urooms0 = u.urooms || '';
u.ushops0 = u.ushops || '';
if (newlev) {
u.urooms = '';
u.uentered = '';
u.ushops = '';
u.ushops_entered = '';
u.ushops_left = u.ushops0;
return;
}
// C ref: hack.c:3518 — get all rooms at hero's current position
const allRooms = in_rooms(u.ux, u.uy, 0);
// in_rooms returns room indices (0-based). C stores room chars = idx + ROOMOFFSET.
u.urooms = allRooms.map(idx => String.fromCharCode(idx + ROOMOFFSET)).join('');
let uentered = '';
let ushops = '';
let ushops_entered = '';
for (let i = 0; i < u.urooms.length; i++) {
const c = u.urooms[i];
// newly entered?
if (!(u.urooms0 || '').includes(c))
uentered += c;
// is it a shop?
const roomIdx = c.charCodeAt(0) - ROOMOFFSET;
const rm = g.level?.rooms?.[roomIdx];
if (rm && rm.rtype >= SHOPBASE) {
ushops += c;
if (!(u.ushops0 || '').includes(c))
ushops_entered += c;
}
}
u.uentered = uentered;
u.ushops = ushops;
u.ushops_entered = ushops_entered;
// C ref: hack.c:3534-3537 — shops left
let ushops_left = '';
for (let i = 0; i < (u.ushops0 || '').length; i++) {
if (!ushops.includes(u.ushops0[i]))
ushops_left += u.ushops0[i];
}
u.ushops_left = ushops_left;
}
// ── check_special_room — C ref: hack.c:3542 ──
// Called from spoteffects after hero moves. Delivers room entry messages
// and tracks shop entry/exit.
export async function check_special_room(newlev) {
const g = game;
const u = g.u;
move_update(newlev);
// C ref: hack.c:3549-3550 — u_left_shop for shops we left
if (u.ushops_left) {
// TODO: u_left_shop (bills, anger, etc.) — complex, skip for now
}
if (!u.uentered && !u.ushops_entered)
return; // no entry messages
// C ref: hack.c:3574-3575 — u_entered_shop for shops we entered
if (u.ushops_entered) {
await u_entered_shop(u.ushops_entered);
}
// C ref: hack.c:3577-3694 — room entry messages
for (let i = 0; i < (u.uentered || '').length; i++) {
const c = u.uentered[i];
const roomno = c.charCodeAt(0) - ROOMOFFSET;
const rm = g.level?.rooms?.[roomno];
const rt = rm?.rtype ?? 0;
let resetRtypeAfterEntry = false;
switch (rt) {
case ZOO:
await pline("Welcome to David's treasure zoo!");
resetRtypeAfterEntry = true;
break;
case SWAMP:
await pline(`It ${Blind() ? 'feels' : 'looks'} rather ${Blind() ? 'humid' : 'muddy'} down here.`);
resetRtypeAfterEntry = true;
break;
case COURT:
await You('enter an opulent throne room!');
resetRtypeAfterEntry = true;
break;
case LEPREHALL:
await You('enter a leprechaun hall!');
resetRtypeAfterEntry = true;
break;
case MORGUE:
await You('have an uncanny feeling...');
resetRtypeAfterEntry = true;
break;
case BEEHIVE:
await You('enter a giant beehive!');
resetRtypeAfterEntry = true;
break;
case COCKNEST:
await You('enter a disgusting nest!');
resetRtypeAfterEntry = true;
break;
case ANTHOLE:
await You('enter an anthole!');
resetRtypeAfterEntry = true;
break;
case BARRACKS:
await You('enter a military barracks!');
resetRtypeAfterEntry = true;
break;
case TEMPLE:
// C ref: hack.c check_special_room() calls intemple(roomno + ROOMOFFSET).
await intemple(roomno + ROOMOFFSET);
break;
default:
break;
}
// C ref: hack.c room-entry switch:
// only specific special-room message types are reset to OROOM.
// Non-message room types (for example VAULT) are left intact.
if (resetRtypeAfterEntry && rm) {
const oldType = rt;
rm.rtype = OROOM;
if (!search_special(oldType)) {
switch (oldType) {
case COURT:
g.level.flags.has_court = 0;
break;
case SWAMP:
g.level.flags.has_swamp = 0;
break;
case MORGUE:
g.level.flags.has_morgue = 0;
break;
case ZOO:
g.level.flags.has_zoo = 0;
break;
case BARRACKS:
g.level.flags.has_barracks = 0;
break;
case TEMPLE:
g.level.flags.has_temple = 0;
break;
case BEEHIVE:
g.level.flags.has_beehive = 0;
break;
default:
break;
}
}
if (oldType === COURT || oldType === SWAMP || oldType === MORGUE || oldType === ZOO) {
const stealth = !!(((u.uprops?.[STEALTH]?.intrinsic || 0)
|| (u.uprops?.[STEALTH]?.extrinsic || 0))
&& !(u.uprops?.[STEALTH]?.blocked || 0));
let roomMonsters = 0;
for (let mtmp = g.fmon; mtmp; mtmp = mtmp.nmon) {
if (DEADMONSTER(mtmp))
continue;
if (!isok(mtmp.mx, mtmp.my))
continue;
// C compares roomno directly against levl[][] roomno.
if ((g.level.at(mtmp.mx, mtmp.my)?.roomno ?? 0) !== roomno)
continue;
roomMonsters++;
if (!stealth && !rn2(3)) {
wakeup(mtmp, false);
mtmp.msleeping = 0;
}
}
}
}
}
}
function invocation_message() { }
// C ref: hack.c:3032 — check if walking smudges engravings at old/new positions
export function maybe_smudge_engr(x1, y1, x2, y2) {
if (can_reach_floor(true)) {
const ep1 = engr_at(x1, y1);
if (ep1 && ep1.engr_type !== HEADSTONE)
wipe_engr_at(x1, y1, rnd(5), false);
if ((x2 !== x1 || y2 !== y1)) {
const ep2 = engr_at(x2, y2);
if (ep2 && ep2.engr_type !== HEADSTONE)
wipe_engr_at(x2, y2, rnd(5), false);
}
}
}
// maybe_adjust_hero_bubble: imported from mkmaze.js
// C ref: monmove.c:694 m_postmove_effect — effects after monster/hero moves
async function m_postmove_effect(mtmp) {
const is_u = (mtmp === game.youmonst || mtmp === game.player);
const x = is_u ? game.u.ux0 : mtmp?.mx;
const y = is_u ? game.u.uy0 : mtmp?.my;
if (mtmp?.data === mons[PM_HEZROU]) {
await create_gas_cloud(x, y, 1, 8);
} else if (mtmp?.data === mons[PM_STEAM_VORTEX] && !mtmp.mcan) {
await create_gas_cloud(x, y, 1, 0);
}
}
// gbuf-cleanup Phase 4: glyph_at / glyph_is_invisible / glyph_is_warning
// retired from hack.js in favor of the canonical versions in display.js
// (glyph_at) and glyph.js (predicates). Importers migrated via
// scripts/dedup-move-import.mjs.
// helpless: canonical export at end of file
// C ref: monmove.c:2354 can_ooze() — hero can ooze through gaps if amorphous
function can_ooze() {
const ptr = game.u?.data || game.youmonst?.data;
return !!(amorphous(ptr) && !stuff_prevents_passage());
}
// C ref: monmove.c:2316 stuff_prevents_passage() — inventory too bulky to ooze
// stuff_prevents_passage: imported from mon.js
// C ref: hack.c:919 bad_rock()// Returns true if the tile at (x,y) is a rock/wall/boulder that mdat can't pass through.
export function bad_rock(mdat, x, y) {
const loc = game.level?.at(x, y);
if (!loc) return true;
return !!((Sokoban() && sobj_at(BOULDER, x, y))
|| (IS_OBSTRUCTED(loc.typ)
&& (!tunnels(mdat) || needspick(mdat)
|| !!(loc.wall_info & W_NONDIGGABLE))
&& !(passes_walls(mdat) && may_passwall(x, y))));
}
// C ref: hack.c:912 may_passwall()
// Returns true unless the tile is a special non-passable stone wall.
export function may_passwall(x, y) {
const loc = game.level?.at(x, y);
if (!loc) return false;
return !(IS_STWALL(loc.typ) && (loc.wall_info & W_NONPASSWALL));
}
function still_chewing() { return false; }
// ── doorless_door ──
// C ref: hack.c:3978
export function doorless_door(x, y) {
const loc = game.level?.at(x, y);
if (!loc || !IS_DOOR(loc.typ)) return false;
const mask = loc.doormask ?? loc.flags ?? 0;
return !(mask & ~(D_NODOOR | D_BROKEN));
}
// ── crawl_destination ──
// C ref: hack.c:3994
export async function crawl_destination(x, y) {
if (!goodpos(x, y, game.u, 0)) return false;
if (x === game.u.ux || y === game.u.uy) return true;
if (NODIAG(game.u.umonnum ?? -1)) return false;
if (Passes_walls()) return true;
const loc = game.level?.at(x, y);
if (IS_DOOR(loc?.typ ?? 0) && (!doorless_door(x, y) || await block_door(x, y)))
return false;
return !(bad_rock(game.u.data, game.u.ux, y)
&& bad_rock(game.u.data, x, game.u.uy));
}
// C ref: hack.c:932 cant_squeeze_thru()
// Returns: 0=can squeeze, 1=too big, 2=too much stuff, 3=sokoban
function cant_squeeze_thru() {
const ptr = game.u?.data || game.youmonst?.data;
if (Passes_walls()) return 0;
// too big?
if (bigmonst(ptr)
&& !(amorphous(ptr) || is_whirly(ptr) || noncorporeal(ptr)
|| slithy(ptr)))
return 1;
// lugging too much junk?
const amt = inv_weight() + weight_cap();
if (amt > 600) // WT_TOOMUCH_DIAGONAL
return 2;
// Sokoban restriction applies to hero only
if (Sokoban())
return 3;
return 0;
}
// ── test_move ──
// C ref: hack.c:973
export async function test_move(ux, uy, dx, dy, mode) {
const g = game;
const x = ux + dx;
const y = uy + dy;
g.context.door_opened = false;
if (!isok(x, y)) return false;
const loc = g.level?.at(x, y);
if (!loc) return false;
const tmpr_typ = loc.typ ?? 0;
if (IS_OBSTRUCTED(tmpr_typ) || tmpr_typ === IRONBARS) {
if (Blind() && mode === DO_MOVE) feel_location(x, y);
if (Passes_walls() && may_passwall(x, y)) {
/* pass through */
} else if (Underwater()) {
if (mode === DO_MOVE) await pline('There is an obstacle there.');
return false;
} else if (tmpr_typ === IRONBARS) {
if (!Passes_walls()) {
if (mode === DO_MOVE && g.flags?.mention_walls)
await pline('You cannot pass through the bars.');
return false;
}
} else if (tunnels(g.u?.data) && !needspick(g.u?.data)) {
if (mode === DO_MOVE && still_chewing(x, y)) return false;
} else if (g.flags?.autodig && !g.context.run && !g.context.nopick
&& g.u.uwep && is_pick(g.u.uwep)) {
// C ref: hack.c:1022-1027 — autodig when wielding pick-axe
if (mode === DO_MOVE)
await use_pick_axe2(g.u.uwep);
return false;
} else {
if (mode === DO_MOVE && g.flags?.mention_walls)
await pline("It's a wall.");
return false;
}
} else if (IS_DOOR(tmpr_typ)) {
if (closed_door(x, y)) {
if (Blind() && mode === DO_MOVE) feel_location(x, y);
if (Passes_walls()) {
/* pass through */
} else if (can_ooze(g.u)) {
if (mode === DO_MOVE) await pline('You ooze under the door.');
} else if (Underwater()) {
if (mode === DO_MOVE) await pline('There is an obstacle there.');
return false;
} else if (tunnels(g.u?.data) && !needspick(g.u?.data)) {
if (mode === DO_MOVE && still_chewing(x, y)) return false;
} else {
if (mode === DO_MOVE) {
if (g.flags?.autoopen !== false && !g.context.run
&& !Confusion() && !Stunned() && !Fumbling()) {
// C ref: hack.c:1097 — actually open the door via doopen_indir
await doopen_indir(x, y);
g.context.door_opened = !closed_door(x, y);
g.context.move = (ux !== g.u.ux || uy !== g.u.uy);
pushCtxMoveLog('test_move', g.context.move ? 1 : 0);
} else if (x === ux || y === uy) {
if (Blind() || Stunned() || ACURR(A_DEX) < 10 || Fumbling()) {
if (g.u.usteed) {
await You_cant(`lead ${y_monnam(g.u.usteed)} through that closed door.`);
} else {
await pline('Ouch! You bump into a door.');
exercise(A_DEX, false);
}
g.context.door_opened = true;
g.context.move = true;
pushCtxMoveLog('test_move', g.context.move ? 1 : 0);
nomul0();
} else {
await pline('That door is closed.');
}
}
} else if (mode === TEST_TRAV || mode === TEST_TRAP) {
return !await _testdiag_blocks(dx, dy, x, y, mode);
}
return false;
}
} else {
if (await _testdiag_blocks(dx, dy, x, y, mode)) return false;
}
}
if (dx && dy && bad_rock(g.u?.data, ux, y) && bad_rock(g.u?.data, x, uy)) {
const squeeze = cant_squeeze_thru();
if (squeeze) {
if (mode === DO_MOVE) {
if (squeeze === 1) await pline('Your body is too large to fit through.');
else if (squeeze === 2) await pline('You are carrying too much to get through.');
else await pline('You cannot pass that way.');
}
return false;
}
} else if (dx && dy && worm_cross(ux, uy, x, y)) {
return false;
}
if (g.context.run === 8 && mode !== DO_MOVE && !(x === g.u.ux && y === g.u.uy)) {
const trap = t_at(x, y);
if (trap && trap.tseen && trap.ttyp !== VIBRATING_SQUARE)
return mode === TEST_TRAP;
if (g.level?.at(x, y)?.seenv && is_pool_or_lava(x, y))
return mode === TEST_TRAP;
}
if (mode === TEST_TRAP) return false;
const ust_typ = g.level?.at(ux, uy)?.typ ?? 0;
if (dx && dy && !Passes_walls() && IS_DOOR(ust_typ)
&& (!doorless_door(ux, uy) || block_entry(x, y))) {
if (mode === DO_MOVE && g.flags?.mention_walls)
await You_cant("move diagonally out of an intact doorway.");
return false;
}
if (sobj_at(BOULDER, x, y) && (Sokoban() || !Passes_walls())) {
if (mode !== TEST_TRAV && g.context.run >= 2
&& !(Blind() || Hallucination()) && !could_move_onto_boulder(x, y)) {
if (mode === DO_MOVE && g.flags?.mention_walls)
await pline('A boulder blocks your path.');
return false;
}
if (mode === DO_MOVE) {
/* tunneling monsters will chew before pushing */
if (tunnels(g.youmonst?.data) && !needspick(g.youmonst?.data)
&& !Sokoban()) {
if (still_chewing(x, y))
return false;
} else if (await moverock() < 0)
return false;
} else if (mode === TEST_TRAV) {
/* never travel through boulders in Sokoban */
if (Sokoban())
return false;
/* don't pick two boulders in a row, unless there's a way thru */
if (sobj_at(BOULDER, ux, uy) && !Sokoban()) {
if (!Passes_walls()
&& !could_move_onto_boulder(ux, uy)
&& !(tunnels(g.youmonst?.data)
&& !needspick(g.youmonst?.data))
&& !carrying(PICK_AXE) && !carrying(DWARVISH_MATTOCK)
&& !carrying(WAN_DIGGING))
return false;
}
}
/* assume you'll be able to push it when you get there... */
}
return true;
}
async function _testdiag_blocks(dx, dy, x, y, mode) {
if (dx && dy && !Passes_walls()
&& (!doorless_door(x, y) || await block_door(x, y))) {
if (mode === DO_MOVE) {
if (Blind()) feel_location(x, y);
if (Underwater() || game.flags?.mention_walls)
await You_cant("move diagonally into an intact doorway.");
}
return true;
}
return false;
}
// ── findtravelpath ──
// C ref: hack.c:1267
export async function findtravelpath(mode) {
const g = game;
const u = g.u;
if (!g.travelmap) g.travelmap = {};
pushRngLogEntry(`^travel_start[mode=${mode} ux=${u.ux} uy=${u.uy} tx=${u.tx} ty=${u.ty} travel1=${g.context?.travel1 ? 1 : 0}]`);
if ((mode === TRAVP_TRAVEL || mode === TRAVP_VALID) && g.context.travel1
&& next2u(u.tx, u.ty) && await crawl_destination(u.tx, u.ty)) {
end_running(false);
if (await test_move(u.ux, u.uy, u.tx - u.ux, u.ty - u.uy, TEST_MOVE)) {
if (mode === TRAVP_TRAVEL) {
u.dx = u.tx - u.ux;
u.dy = u.ty - u.uy;
nomul0();
if (!g.iflags) g.iflags = {};
g.iflags.travelcc = { x: 0, y: 0 };
}
return true;
}
if (mode === TRAVP_TRAVEL) g.context.run = 8;
}
if (u.tx !== u.ux || u.ty !== u.uy) {
const travel = [];
for (let i = 0; i < COLNO; i++) travel[i] = new Int16Array(ROWNO);
const tsx = [new Int16Array(COLNO * ROWNO), new Int16Array(COLNO * ROWNO)];
const tsy = [new Int16Array(COLNO * ROWNO), new Int16Array(COLNO * ROWNO)];
let tx, ty, bux, buy, n = 1, set = 0, radius = 1;
const dirmax = NODIAG(u.umonnum ?? -1) ? 4 : N_DIRS;
if (mode === TRAVP_GUESS || mode === TRAVP_VALID) {
tx = u.ux; ty = u.uy; bux = u.tx; buy = u.ty;
} else {
tx = u.tx; ty = u.ty; bux = u.ux; buy = u.uy;
}
let doLoop = true;
while (doLoop) {
doLoop = false;
for (let i = 0; i < COLNO; i++) travel[i].fill(0);
tsx[0][0] = tx; tsy[0][0] = ty;
while (n !== 0) {
let nn = 0;
for (let idx = 0; idx < n; idx++) {
const cx = tsx[set][idx], cy = tsy[set][idx];
let alreadyrepeated = false;
for (let dir = 0; dir < dirmax; dir++) {
const nx = cx + xdir[dirs_ord[dir]];
const ny = cy + ydir[dirs_ord[dir]];
if (!isok(nx, ny)) continue;
if (mode === TRAVP_GUESS && !couldsee(nx, ny)) continue;
if ((!Passes_walls() && !can_ooze(g.u) && closed_door(cx, cy))
|| sobj_at(BOULDER, cx, cy)
|| await test_move(cx, cy, nx - cx, ny - cy, TEST_TRAP)) {
if (travel[cx][cy] > radius - 3) {
if (!alreadyrepeated) {
tsx[1 - set][nn] = cx; tsy[1 - set][nn] = cy;
nn++; alreadyrepeated = true;
}
continue;
}
}
if (await test_move(cx, cy, nx - cx, ny - cy, TEST_TRAV)
&& (g.level?.at(nx, ny)?.seenv || (!Blind() && couldsee(nx, ny)))) {
if (nx === bux && ny === buy) {
if (mode === TRAVP_TRAVEL || mode === TRAVP_VALID) {
const visited = !!(g.travelmap && g.travelmap[cx + ',' + cy]);
u.dx = cx - bux; u.dy = cy - buy;
if (mode === TRAVP_TRAVEL
&& ((cx === u.tx && cy === u.ty) || visited)) {
nomul0(); g.context.run = 8;
if (visited) await pline('You stop, unsure which way to go.');
else { if (!g.iflags) g.iflags = {}; g.iflags.travelcc = { x: 0, y: 0 }; }
}
if (g.travelmap) g.travelmap[u.ux + ',' + u.uy] = 1;
return true;
}
} else if (!travel[nx][ny]) {
tsx[1 - set][nn] = nx; tsy[1 - set][nn] = ny;
travel[nx][ny] = radius; nn++;
}
}
}
}
n = nn; set = 1 - set; radius++;
}
if (mode === TRAVP_GUESS) {
let px = tx, py = ty;
let bd = distmin(bux, buy, tx, ty), bd2 = dist2(bux, buy, tx, ty);
let ptrav = COLNO * ROWNO;
for (let sx = 1; sx < COLNO; sx++)
for (let sy = 0; sy < ROWNO; sy++) {
const ct = travel[sx][sy];
if (ct > 0 && couldsee(sx, sy)) {
const nd = distmin(bux, buy, sx, sy);
if (nd === bd && ct < ptrav) {
const nd2 = dist2(bux, buy, sx, sy);
if (nd2 < bd2) { px = sx; py = sy; bd2 = nd2; ptrav = ct; }
} else if (nd < bd) {
px = sx; py = sy; bd = nd;
bd2 = dist2(bux, buy, sx, sy); ptrav = ct;
}
}
}
if (px === u.ux && py === u.uy) {
u.dx = sgn(u.tx - u.ux); u.dy = sgn(u.ty - u.uy);
if (await test_move(u.ux, u.uy, u.dx, u.dy, TEST_MOVE)) {
if (g.travelmap) g.travelmap[u.ux + ',' + u.uy] = 1;
return true;
}
u.dx = 0; u.dy = 0; nomul0(); return false;
}
tx = px; ty = py; bux = u.ux; buy = u.uy;
set = 0; n = 1; radius = 1; mode = TRAVP_TRAVEL;
doLoop = true; continue;
}
return false;
}
}
u.dx = 0; u.dy = 0; nomul0(); return false;
}
// ── is_valid_travelpt ──
// C ref: hack.c:1526
export async function is_valid_travelpt(x, y) {
if (x === game.u.ux && y === game.u.uy) return true;
const oldtx = game.u.tx, oldty = game.u.ty;
game.u.tx = x; game.u.ty = y;
const ret = await findtravelpath(TRAVP_VALID);
game.u.tx = oldtx; game.u.ty = oldty;
return ret;
}
async function carrying_too_much() {
if (near_capacity() >= 5) { await pline('You collapse under your load.'); nomul0(); return true; }
return false;
}
function move_out_of_bounds(x, y) {
if (!isok(x, y)) {
nomul0(); game.context.move = 0;
pushCtxMoveLog('move_out_of_bounds', game.context.move);
return true;
}
return false;
}
function escape_from_sticky_mon(x, y) {
const g = game;
if (g.u.ustuck && (x !== g.u.ustuck.mx || y !== g.u.ustuck.my)) {
if (g.u.ustuck.mtame) {
g.u.ustuck = null;
} else {
const roll = rn2(g.u.ustuck.mcanmove === false ? 8 : 40);
if (roll <= 2) { g.u.ustuck = null; }
else { nomul0(); return true; }
}
}
return false;
}
// C ref: hack.c:1865 swim_move_danger() — avoid stepping into water/lava.
// C only blocks if ParanoidSwim is configured; otherwise warns but allows.
async function swim_move_danger(x, y) {
const g = game;
const u = g.u;
if (Underwater() && is_pool(x, y)) return false;
const loc = g.level?.at(x, y);
const here = g.level?.at(u.ux, u.uy);
if (!loc || !here) return false;
const newtyp = loc.typ ?? 0;
const heretyp = here.typ ?? 0;
if (newtyp !== heretyp && !Stunned() && !Confusion()
&& loc.seenv && (is_pool(x, y) || is_lava(x, y))) {
// C ref: hack.c:1881-1886 — inner check: is this move actually dangerous?
// Pool: dangerous if no known water walking
// Lava: dangerous if no known lava walking AND not already on lava
const dangerous = (is_pool(x, y) /* && !Known_wwalking — TODO */)
|| (is_lava(x, y) /* && !Known_lwalking */ && !is_lava(u.ux, u.uy));
if (!dangerous) return false;
// C ref: hack.c:1887 — m-prefix bypasses
if (g.context.nopick) return false;
// C ref: hack.c:1891 — only block if ParanoidSwim is configured
// Without ParanoidSwim, C falls through to return FALSE (allow move).
// ParanoidSwim is a user option; default is off.
if (g.flags?.paranoid_swim) {
await pline(`You avoid stepping into the ${waterbody_name(x, y)}.`);
return true;
}
// Not paranoid: allow the move (hero may drown/burn without protection)
return false;
}
return false;
}
// ── monster_nearby ──
// C ref: hack.c:4022
export function monster_nearby() {
pushRngLogEntry('>monster_nearby');
const u = game.u;
let found = 0;
for (let x = u.ux - 1; x <= u.ux + 1 && !found; x++)
for (let y = u.uy - 1; y <= u.uy + 1; y++) {
if (!isok(x, y) || (x === u.ux && y === u.uy)) continue;
const mtmp = m_at(x, y);
if (mtmp && mtmp.m_ap_type !== M_AP_FURNITURE && mtmp.m_ap_type !== M_AP_OBJECT
&& (Hallucination() || (!mtmp.mpeaceful && !noattacks(mtmp.data)))
&& (!is_hider(mtmp.data) || !mtmp.mundetected)
&& !helpless(mtmp) && !onscary(u.ux, u.uy, mtmp) && canspotmon(mtmp)) {
found = 1;
break;
}
}
pushRngLogEntry(`<monster_nearby=${found}`);
return found;
}
// C ref: hack.c:3703 pickup_checks
// Returns -1 if normal pickup can proceed, 0 if handled (no time),
// 1 if handled (took time), -2 if swallowed (loot monster).
export async function pickup_checks() {
const g = game;
const u = g.u;
// C ref: hack.c:3709-3721 — swallowed
if (u.uswallow) {
if (!u.ustuck?.minvent) {
if (digests(u.ustuck?.data))
await You('pick up %s tongue.', s_suffix(mon_nam(u.ustuck)));
else
await You("don't %s anything in here to pick up.", Blind() ? 'feel' : 'see');
return 1;
}
return -2; // loot monster inventory
}
// C ref: hack.c:3722-3741 — pool/lava checks
if (is_pool(u.ux, u.uy) && !Underwater()) {
await You_cant('even see the bottom, let alone pick up %s.', 'something');
return 0;
}
if (is_lava(u.ux, u.uy)) {
await You_cant('reach the bottom to pick things up.');
return 0;
}
// C ref: hack.c:3743-3762 — no objects here
if (!OBJ_AT(u.ux, u.uy)) {
const loc = g.level?.at(u.ux, u.uy);
const typ = loc?.typ ?? 0;
if (IS_THRONE(typ))
await pline('It must weigh%s a ton!', loc?.looted ? ' almost' : '');
else if (IS_SINK(typ))
await pline_The('plumbing connects it to the floor.');
else if (IS_GRAVE(typ))
await You("don't need a gravestone. Yet.");
else if (IS_FOUNTAIN(typ))
await You('could drink the water...');
else if (IS_DOOR(typ) && ((loc?.doormask ?? 0) & D_ISOPEN))
await pline("It won't come off the hinges.");
else if (IS_ALTAR(typ))
await pline('Moving the altar would be a very bad idea.');
else if (typ === STAIRS)
await pline_The('stairs are solidly affixed.');
else
await There('is nothing here to pick up.');
return 0;
}
// C ref: hack.c:3764-3786 — can't reach floor (levitation, riding, etc.)
const traphere = t_at(u.ux, u.uy);
if (!can_reach_floor(traphere && is_pit(traphere?.ttyp))) {
if (Blind())
await You('cannot reach anything here.');
else
await You('cannot reach the %s.', surface(u.ux, u.uy));
return 0;
}
return -1; // can do normal pickup
}
// ──────────────────────────────────────────────────────────────
// Existing functions below
// ──────────────────────────────────────────────────────────────
// C ref: hack.c end_running()
export function end_running(and_travel) {
const g = game;
if (g.flags?.time && g.context?.run) setTimeBotl('end_running');
g.context.run = 0;
if (and_travel) {
g.context.travel = false;
g.context.travel1 = false;
g.context.mv = false;
if (g.iflags) {
g.iflags.getloc_travelmode = false;
g.iflags.getloc_tip_active = false;
}
g._menuActive = false;
}
if (g.travelmap) g.travelmap = null;
if (g.multi > 0) { g.multi = 0; pushMultiLog('end_running', g.multi); }
}
export function is_safemon(mtmp) {
return !!(game.flags?.safe_dog
&& mtmp?.mpeaceful
&& canspotmon(mtmp)
&& !Confusion()
&& !Hallucination()
&& !Stunned());
}
// C ref: monst.h:225 mundisplaceable — monsters that won't swap places
export function mundisplaceable(mon) {
return !!(mon.ispriest || mon.isshk || mon.isgd
|| monsndx(mon.data) === PM_ORACLE
|| mon.m_id === game.quest_status?.leader_m_id);
}
function nomul0() {
// Use canonical cmd.c nomul() semantics (including CQ_CANNED clear).
cmd_nomul(0);
}
// C ref: hack.c:3814 lookaround()
// Uses goto-state pattern to match C's goto bcorr / goto stop structure.
export async function lookaround() {
const g = game;
const u = g.u;
if (!u || Blind() || (g.context?.run ?? 0) === 0) return;
if (NODIAG(u.umonnum ?? -1) && u.dx && u.dy) {
await pline('You cannot move diagonally.');
nomul0();
return;
}
const ux = u.ux ?? 0, uy = u.uy ?? 0;
const dx = u.dx ?? 0, dy = u.dy ?? 0;
let x0 = 0, y0 = 0, m0 = 1, i0 = 9, corrct = 0, noturn = 0;
const levelAt = (lx, ly) => g.level?.at?.(lx, ly) || null;
for (let x = ux - 1; x <= ux + 1; x++) {
for (let y = uy - 1; y <= uy + 1; y++) {
const infront = (x === ux + dx && y === uy + dy);
if (!isok(x, y) || (x === ux && y === uy)) continue;
if (NODIAG(u.umonnum ?? -1) && x !== ux && y !== uy) continue;
// C ref: hack.c:3842-3856 — visible monster check
let mtmp = m_at(x, y);
if (mtmp && mtmp.m_ap_type !== M_AP_FURNITURE && mtmp.m_ap_type !== M_AP_OBJECT
&& mon_visible(mtmp)) {
if ((g.context.run !== 1 && !is_safemon(mtmp))
|| (infront && !g.context.travel)) {
nomul0(); return; // goto stop
}
}
const loc = levelAt(x, y);
if (!loc || loc.typ === STONE) continue;
if (x === ux - dx && y === uy - dy) continue;
// C ref: hack.c:3866-3872 — avoid_moving_on_trap
let go_bcorr = false;
{
const trap = t_at(x, y);
if (trap && trap.tseen && trap.ttyp !== VIBRATING_SQUARE) {
if (g.context.run === 1) { go_bcorr = true; }
else if (infront) { nomul0(); return; } // goto stop
}
}
if (!go_bcorr) {
// C ref: hack.c:3874-3935 — tile type dispatch
if (IS_OBSTRUCTED(loc.typ) || loc.typ === ROOM || IS_AIR(loc.typ) || loc.typ === ICE) {
continue;
// C ref: hack.c:3878 — closed door or monster mimicking door
} else if (closed_door(x, y)
|| (mtmp && mtmp.m_ap_type === M_AP_FURNITURE
&& (mtmp.mappearance === 15 /*S_vcdoor*/ || mtmp.mappearance === 16 /*S_hcdoor*/))) {
if (x !== ux && y !== uy) continue;
if (g.context.run !== 1 && !g.context.travel) { nomul0(); return; } // goto stop
go_bcorr = true; // goto bcorr
} else if (loc.typ === CORR) {
go_bcorr = true; // fall into bcorr
} else if (loc.typ === POOL || loc.typ === MOAT || loc.typ === WATER
|| loc.typ === LAVAPOOL) {
// C ref: hack.c:3921-3924 — avoid_moving_on_liquid
if (infront && g.context.run > 1) { nomul0(); return; } // goto stop
continue;
} else {
// C ref: hack.c:3925-3935 — other features (stairs, altar, etc.)
if (g.context.run === 1) { go_bcorr = true; } // goto bcorr
else if (g.context.run === 8) { continue; }
else {
if (mtmp) continue;
if (((x === ux - dx) && (y !== uy + dy))
|| ((y === uy - dy) && (x !== ux + dx))) continue;
nomul0(); return; // goto stop
}
}
}
// bcorr: C ref: hack.c:3894-3920
const uloc = levelAt(ux, uy);
if (uloc && uloc.typ !== ROOM) {
if (g.context.run === 1 || g.context.run === 3 || g.context.run === 8) {
const i = dist2(x, y, ux + dx, uy + dy);
if (i > 2) continue;
if (corrct === 1 && dist2(x, y, x0, y0) !== 1) noturn = 1;
if (i < i0) { i0 = i; x0 = x; y0 = y; m0 = mtmp ? 1 : 0; }
}
corrct++;
}
continue; // C: after bcorr block, continues loop
}
}
// C ref: hack.c:3941-3944
if (corrct > 1 && g.context.run === 2) { nomul0(); return; }
// C ref: hack.c:3946-3973 — corridor turn logic
if ((g.context.run === 1 || g.context.run === 3 || g.context.run === 8)
&& !noturn && !m0 && i0 && (corrct === 1 || (corrct === 2 && i0 === 1))) {
let turn;
if (i0 === 2) turn = (dx === y0 - uy && dy === ux - x0) ? 2 : -2;
else if (dx && dy)
turn = ((dx === dy && y0 === uy) || (dx !== dy && y0 !== uy)) ? -1 : 1;
else
turn = ((x0 - ux === y0 - uy && !dy) || (x0 - ux !== y0 - uy && dy)) ? 1 : -1;
turn += (u.last_str_turn ?? 0);
if (turn <= 2 && turn >= -2) { u.last_str_turn = turn; u.dx = x0 - ux; u.dy = y0 - uy; }
}
}
// C ref: hack.c unmul()
export async function unmul(msg_override) {
const g = game;
setBotl('unmul');
g.multi = 0;
if (msg_override != null) g.nomovemsg = msg_override;
else if (g.nomovemsg == null) g.nomovemsg = 'You can move again.';
if (g.nomovemsg && g.nomovemsg.length > 0) await pline(g.nomovemsg);
g.nomovemsg = null;
if (g.u) g.u.usleep = 0;
g.multi_reason = null;
if (typeof g.multireasonbuf === 'string') g.multireasonbuf = '';
if (g.afternmv) { const f = g.afternmv; g.afternmv = null; await f(); }
}
export async function showdamage(dmg) {
if (!game.iflags?.showdamage || !dmg) return;
await pline('[HP ' + (-dmg) + ', ' + (Upolyd() ? game.u.mh : game.u.uhp) + ' left]');
}
async function maybe_wail() {
const powers = [TELEPORT, SEE_INVIS, POISON_RES, COLD_RES,
SHOCK_RES, FIRE_RES, SLEEP_RES, DISINT_RES,
TELEPORT_CONTROL, STEALTH, FAST, INVIS];
if ((game.moves || 0) <= (game.wailmsg || 0) + 50) return;
game.wailmsg = game.moves;
if (Role_if(PM_WIZARD) || Race_if(PM_ELF) || Role_if(PM_VALKYRIE)) {
const who = (Role_if(PM_WIZARD) || Role_if(PM_VALKYRIE))
? (game.urole?.name?.m || 'Wizard') : 'Elf';
if (game.u.uhp === 1) await pline(who + ' is about to die.');
else {
let powercnt = 0;
for (const p of powers)
if ((game.u.uprops?.[p]?.intrinsic ?? 0) & INTRINSIC) powercnt++;
await pline(powercnt >= 4
? who + ', all your powers will be lost...'
: who + ', your life force is running out.');
}
} else {
await You_hear(game.u.uhp === 1
? 'the wailing of the Banshee...'
: 'the howling of the CwnAnnwn...');
}
}
export async function saving_grace(dmg) {
if (dmg < 0) return 0;
if (!game.context?.mon_moving) return dmg;
if (dmg < game.u.uhp || game.u.uhp <= 0) return dmg;
if (game.saving_grace_turn) return game.u.uhp - 1;
if (!game.u.usaving_grace
&& (Math.trunc((game.uhp_at_start_of_monster_turn || 0) * 100 / game.u.uhpmax) >= 90)) {
livelog_printf(0, '%s (%d damage, %d/%d HP)',
'survived one-shot death via saving-grace',
(game.uhp_at_start_of_monster_turn || 0) - game.u.uhp + dmg,
game.uhp_at_start_of_monster_turn || 0, game.u.uhpmax);
dmg = game.u.uhp - 1;
game.u.usaving_grace = 1;
game.saving_grace_turn = true;
end_running(true);
if (game.u.usleep) await unmul('Suddenly you wake up!');
}
return dmg;
}
export async function losehp(n, knam, k_format) {
const u = game.u;
if (!u) return;
game.disp = game.disp || {};
setBotl('losehp');
end_running(true);
if (Upolyd()) {
u.mh = (u.mh || 0) - n;
await showdamage(n);
if ((u.mhmax || 0) < u.mh) u.mhmax = u.mh;
if (u.mh < 1) {
await rehumanize();
} else if (n > 0 && u.mh * 10 < (u.mhmax || 0)) { await maybe_wail(); }
return;
}
n = await saving_grace(n);
u.uhp = (u.uhp || 0) - n;
await showdamage(n);
if (u.uhp > (u.uhpmax || 0)) u.uhpmax = u.uhp;
if (u.uhp < 1) {
if (!game.killer) game.killer = {};
game.killer.format = k_format;
game.killer.name = knam || '';
await custompline('You die...');
// C ref: "You die..." pline leaves toplin=NEED_MORE so that
// paybill/inherits' pline can concatenate via addtopl.
// C harness parity expects one bones-gate RNG call on death.
rn2(1);
await end_done(DIED);
return;
} else if (n > 0 && u.uhp * 10 < (u.uhpmax || 0)) { await maybe_wail(); }
}
// urgent_pline canonical in pline.js (C ref: pline.c:319).
export function done(how) {
game.program_state = game.program_state || {};
game.program_state.gameover = true;
// C ref: done() → savebones() → can_make_bones(bones.c:377)
// calls rn2(1) to decide if bones file is saved. Always returns 0.
// Full done() port would handle death screen, scoring, bones.
rn2(1);
}
export function invocation_pos(x, y) {
return x === game.inv_pos?.x && y === game.inv_pos?.y;
}
// C ref: hack.c:4363 check_capacity — too encumbered to act?
// Note: synchronous (callers don't await). Message is skipped.
export function check_capacity(msg) {
return near_capacity() >= EXT_ENCUMBER;
}
export function rounddiv(x, y) {
// C ref: hack.c:4480 — round to nearest, halves round up
if (y === 0) return x;
let divsgn = 1;
if (y < 0) { divsgn = -divsgn; y = -y; }
if (x < 0) { divsgn = -divsgn; x = -x; }
let r = Math.trunc(x / y);
const m = x % y;
if (2 * m >= y) r++;
return divsgn * r;
}
export function inv_cnt(incl_gold) {
let ct = 0;
for (let otmp = game.invent; otmp; otmp = otmp.nobj) if (otmp.invlet) ct++;
if (!incl_gold)
for (let otmp = game.invent; otmp; otmp = otmp.nobj)
if (otmp.oclass === COIN_CLASS) ct--;
return ct;
}
export function money_cnt(otmp) {
let total = 0;
while (otmp) { if (otmp.oclass === COIN_CLASS) total += (otmp.quan || 1); otmp = otmp.nobj; }
return total;
}
// ── Utility stubs ──
export function livelog_printf(flags, ...args) { }
// C ref: timeout.c:448 burn_away_slime
export async function burn_away_slime() {
if (Slimed()) {
await make_slimed(0, 'The slime that covers you is burned away!');
}
}
// water_damage canonical in trap.js — importers now pull from there directly.
// heal_legs: canonical implementation in do.js
import { DEADMONSTER, mon_nam, mondead, mondied, Monnam, stuff_prevents_passage, wakeup, y_monnam } from './mon.js';
import { is_lava, is_pool, is_pool_or_lava } from './dbridge.js';
import { create_gas_cloud } from './region.js';
import { block_door, block_entry, u_entered_shop } from './shk.js';
import { worm_cross } from './worm.js';
import { make_slimed } from './potion.js';
import { search_special } from './mkroom.js';
export async function killed(mtmp) {
// C ref: mon.c killed() → xkilled(mtmp, XKILL_GIVEMSG)
if (xkilled) await xkilled(mtmp, 0 /* XKILL_GIVEMSG = 0 */);
else { if (mtmp) { mtmp.mhp = 0; mondead(mtmp); } }
}
// C ref: mon.c:3363 monkilled — monster death with optional corpse
// Simplified: uses mondied (with corpse) for most cases, mondead for
// disintegration/digestion. Full xkilled path not ported yet.
export async function monkilled(mtmp, reason, how) {
if (!mtmp) return;
const mptr = mtmp.data;
// C ref: mon.c:3363-3367 — death message when visible
// C: fltxt is a char* — non-NULL even if empty; JS '' is falsy
// so we check reason !== undefined/null explicitly
if (reason != null && cansee(mtmp.mx, mtmp.my)) {
const verb = nonliving(mptr) ? 'destroyed' : 'killed';
const byPart = reason ? ` by the ${reason}` : '';
await pline(`${Monnam(mtmp)} is ${verb}${byPart}!`);
}
// C ref: mon.c:3377 — no corpse for digestion/disintegration/burnt golem
const disintegested = (how === AD_DGST || how === -AD_RBRE
|| (how === AD_FIRE && false /* completelyburns stub */));
mtmp.mhp = 0;
if (disintegested)
mondead(mtmp);
else
await mondied(mtmp);
}
// Canonical exports — duplicated in 8+ files
// C ref: hack.h — helpless(mon) = (mon->msleeping || !mon->mcanmove)
// mcanmove defaults to true (1) in C; check explicitly for 0/false
export function helpless(mon) { return !!(mon?.msleeping || (mon?.mcanmove === 0 || mon?.mcanmove === false)); }
export function change_luck(n) { const g = (typeof game !== 'undefined' ? game : null); if (g?.u) g.u.uluck = (g.u.uluck || 0) + n; }
// C ref: engrave.c:187 can_reach_floor
export function can_reach_floor(check_pit) {
const g = game;
const u = g.u;
if (!u) return true;
if (u.uswallow
|| (u.ustuck && !sticks(g.youmonst?.data)
&& attacktype(u.ustuck.data, AT_HUGS))
|| (Levitation() && !(Is_airlevel(u.uz) || Is_waterlevel(u.uz)))) {
return false;
}
// C: restricted/unskilled riders can't reach floor
if (u.usteed) return false;
if (Flying()) return true;
if (check_pit) {
const trap = t_at(u.ux, u.uy);
if (trap && (uteetering_at_seen_pit(trap) || uescaped_shaft(trap))) {
return false;
}
}
return true;
}
// ── moverock and helpers (hack.c:145-618) ──
// C ref: hack.c:139 squeezeablylightinvent()
function squeezeablylightinvent() {
const g = game;
return !g.invent || inv_weight() <= (WT_SQUEEZABLE_INV * -1);
}
// C ref: hack.c:145 could_move_onto_boulder()
export function could_move_onto_boulder(sx, sy) {
const g = game;
const u = g.u;
if (Passes_walls()) return true;
if (u.usteed) return false;
if (throws_rocks(g.youmonst?.data)) {
return (!u.dx || !u.dy
|| !(IS_OBSTRUCTED(g.level.at(u.ux, sy)?.typ ?? STONE)
&& IS_OBSTRUCTED(g.level.at(sx, u.uy)?.typ ?? STONE)));
}
if (verysmall(g.youmonst?.data)) return true;
return squeezeablylightinvent();
}
// C ref: hack.c:805 movobj()
function movobj(obj, ox, oy) {
remove_object(obj);
// maybe_unhide_at(obj.ox, obj.oy); // TODO
newsym(obj.ox, obj.oy);
place_object(obj, ox, oy);
newsym(ox, oy);
}
// C ref: hack.c:166 dopush()
async function dopush(sx, sy, rx, ry, otmp, costly) {
const g = game;
const u = g.u;
{
let givemesg, easypush;
if (otmp.o_id !== g.bldrpush_oid) {
g.bldrpushtime = g.moves + 1;
g.bldrpush_oid = otmp.o_id;
}
givemesg = (g.moves > g.bldrpushtime + 2
|| g.moves < g.bldrpushtime);
const what = givemesg ? the(xname(otmp)) : null;
if (!u.usteed) {
easypush = throws_rocks(g.youmonst?.data);
if (givemesg)
await pline("With %s effort you move %s.",
easypush ? "little" : "great", what);
if (!easypush)
exercise(A_STR, true);
} else {
if (givemesg)
await pline("%s moves %s.", y_monnam(u.usteed), what);
}
g.bldrpushtime = g.moves;
}
/* Move the boulder *after* the message. */
if (glyph_is_invisible(glyph_at(rx, ry)))
unmap_object(rx, ry);
otmp.next_boulder = 0;
movobj(otmp, rx, ry); /* does newsym(rx,ry) */
if (Blind()) {
feel_location(rx, ry);
feel_location(sx, sy);
} else {
newsym(sx, sy);
}
/* maybe adjust bill if boulder was pushed across shop boundary */
if (costly && !costly_spot(rx, ry)) {
await addtobill(otmp, false, false, false);
} else if (!costly && costly_spot(rx, ry) && otmp.unpaid) {
const shkp = shop_keeper(in_rooms(rx, ry, SHOPBASE)?.[0]);
if (shkp && onshopbill(otmp, shkp, true)) {
subfrombill(otmp, shkp);
}
} else if (otmp.unpaid) {
const shkp = find_objowner(otmp, sx, sy);
if (shkp) {
const rooms = in_rooms(rx, ry, SHOPBASE);
const shoproom = shkp.eshk?.shoproom;
if (rooms && shoproom && !rooms.includes(shoproom)) {
await stolen_value(otmp, sx, sy, true, false);
}
}
}
}
// C ref: hack.c:247 cannot_push_msg()
async function cannot_push_msg(otmp, sx, sy) {
const what = the(xname(otmp));
if (game.u.usteed)
await pline("%s tries to move %s, but cannot.",
y_monnam(game.u.usteed), what);
else
await You("try to move %s, but in vain.", what);
if (Blind())
feel_location(sx, sy);
}
// C ref: hack.c:262 cannot_push()
async function cannot_push(otmp, sx, sy) {
const g = game;
const u = g.u;
if (throws_rocks(g.youmonst?.data)) {
const invlet_basic_count = 52;
const canpickup = (!Sokoban()
&& (inv_cnt(false) < invlet_basic_count
|| !carrying(BOULDER)));
const willpickup = (canpickup
&& (g.flags?.pickup && !g.context.nopick)
&& autopick_testobj(otmp, true));
if (u.usteed && P_SKILL(P_RIDING) < P_BASIC) {
await You("aren't skilled enough to %s %s from %s.",
willpickup ? "pick up" : "push aside",
the(xname(otmp)), y_monnam(u.usteed));
} else {
await pline("However, you %s%s.",
willpickup ? "easily pick it up"
: "maneuver over it",
(canpickup && !willpickup)
? " and could pick it up"
: "");
sokoban_guilt();
}
return 0;
}
if (could_move_onto_boulder(sx, sy)) {
await pline(
"However, you can squeeze yourself into a small opening.");
sokoban_guilt();
return 0;
} else {
return -1;
}
}
// C ref: hack.c:315 rock_disappear_msg()
async function rock_disappear_msg(otmp) {
if (game.u.usteed)
await pline("%s pushes %s and suddenly it disappears!",
y_monnam(game.u.usteed), the(xname(otmp)));
else
await You("push %s and suddenly it disappears!",
the(xname(otmp)));
}
// C ref: hack.c:327 moverock_done()
function moverock_done(sx, sy) {
const g = game;
// JS uses flat array; iterate objects at (sx,sy) via nexthere chain
let otmp = null;
for (const obj of (g.level?.objects || [])) {
if (obj.ox === sx && obj.oy === sy && obj.otyp === BOULDER)
obj.next_boulder = 0;
}
}
// C ref: hack.c:336 moverock()
export async function moverock() {
const u = game.u;
const sx = u.ux + u.dx;
const sy = u.uy + u.dy;
const ret = await moverock_core(sx, sy);
moverock_done(sx, sy);
return ret;
}
// Local stubs for not-yet-ported functions used in moverock_core
function revive_nasty(_rx, _ry, _msg) { return 0; }
function disturb_buried_zombies(_x, _y) { /* TODO */ }
function random_teleport_level() {
// C ref: teleport.c:2190 — simplified: return current depth
const uz = game.u?.uz;
return depth(uz);
}
function add_to_migration(obj) {
// C ref: mkobj.c:2697
obj.where = 5; // OBJ_MIGRATING
obj.nobj = game.migrating_objs || null;
obj.omigr_from_dnum = game.u?.uz?.dnum ?? 0;
obj.omigr_from_dlevel = game.u?.uz?.dlevel ?? 0;
game.migrating_objs = obj;
}
// C ref: display.h glyph_to_obj() — simplified for JS glyph representation
// JS glyphs are {ch, color} objects; check if ch is boulder display char.
function glyph_to_obj_is_boulder(glyph) {
const ch = (typeof glyph === 'string') ? glyph : glyph?.ch;
return ch === '`' || ch === '0'; // boulder display chars
}
// C ref: hack.c:348 moverock_core()
async function moverock_core(sx, sy) {
const g = game;
const u = g.u;
let rx, ry;
let otmp;
let ttmp;
let mtmp;
let costly;
let firstboulder = true;
while ((otmp = sobj_at(BOULDER, sx, sy)) !== null) {
if (Blind() && !glyph_to_obj_is_boulder(glyph_at(sx, sy))) {
await pline("That feels like a boulder.");
map_object(otmp, true);
nomul0();
return -1;
}
otmp.next_boulder = firstboulder ? 0 : 1;
firstboulder = false;
/* make sure that this boulder is visible as the top object */
// C: if (otmp != level.objects[sx][sy]) movobj(otmp, sx, sy);
// JS uses flat array — check if another object at same position
// is more recently placed (later in array). If so, re-link.
{
const objs = g.level?.objects || [];
let lastAtPos = null;
for (let i = objs.length - 1; i >= 0; i--) {
if (objs[i].ox === sx && objs[i].oy === sy) {
lastAtPos = objs[i];
break;
}
}
if (lastAtPos && lastAtPos !== otmp)
movobj(otmp, sx, sy);
}
rx = u.ux + 2 * u.dx;
ry = u.uy + 2 * u.dy;
nomul0();
/* using m<dir> towards an adjacent boulder */
if (g.context.nopick) {
const oldglyph = glyph_at(sx, sy);
let res;
feel_location(sx, sy);
if (throws_rocks(g.youmonst?.data)) {
await You("%s over a boulder here.", u_locomotion("step"));
sokoban_guilt();
res = 0;
} else if (could_move_onto_boulder(sx, sy)) {
await You("squeeze yourself %s the boulder.",
Flying() ? "over" : "against");
sokoban_guilt();
res = 0;
} else {
await There("is a boulder in your way.");
if (glyph_at(sx, sy) !== oldglyph)
g.context.door_opened = g.context.move = true;
res = -1;
}
return res;
}
if (Levitation() || Is_airlevel(u.uz)) {
if (Blind())
feel_location(sx, sy);
await You("don't have enough leverage to push %s.", the(xname(otmp)));
return -1;
}
if (verysmall(g.youmonst?.data) && !u.usteed) {
if (Blind())
feel_location(sx, sy);
await pline("You're too small to push that %s.", xname(otmp));
return await cannot_push(otmp, sx, sy);
}
if (isok(rx, ry) && !IS_OBSTRUCTED(g.level.at(rx, ry)?.typ ?? STONE)
&& (g.level.at(rx, ry)?.typ ?? STONE) !== IRONBARS
&& (!IS_DOOR(g.level.at(rx, ry)?.typ ?? STONE) || !(u.dx && u.dy)
|| doorless_door(rx, ry)) && !sobj_at(BOULDER, rx, ry)) {
ttmp = t_at(rx, ry);
mtmp = m_at(rx, ry);
costly = (costly_spot(sx, sy)
&& shop_keeper(in_rooms(sx, sy, SHOPBASE)?.[0]));
/* KMH -- Sokoban doesn't let you push boulders diagonally */
if (Sokoban() && u.dx && u.dy) {
if (Blind())
feel_location(sx, sy);
await pline("%s won't roll diagonally on this %s.",
The(xname(otmp)), surface(sx, sy));
return await cannot_push(otmp, sx, sy);
}
if (revive_nasty(rx, ry,
"You sense movement on the other side.")) {
return -1;
}
if (mtmp && !noncorporeal(mtmp.data)
&& (!mtmp.mtrapped || !(ttmp && is_pit(ttmp.ttyp)))) {
let deliver_part1 = false;
if (Blind())
feel_location(sx, sy);
if (canspotmon(mtmp)) {
await pline("There's %s on the other side.", a_monnam(mtmp));
deliver_part1 = true;
} else {
Soundeffect('se_monster_behind_boulder', 50);
await You_hear("a monster behind %s.", the(xname(otmp)));
if (!Deaf())
deliver_part1 = true;
map_invisible(rx, ry);
}
if (g.flags?.verbose) {
let you_or_steed = u.usteed ? y_monnam(u.usteed) : "you";
await pline("%s%s cannot move %s.",
deliver_part1 ? "Perhaps that's why " : "",
deliver_part1 ? you_or_steed
: upstart(you_or_steed),
deliver_part1 ? "it" : the(xname(otmp)));
}
return await cannot_push(otmp, sx, sy);
}
if (closed_door(rx, ry)) {
await cannot_push_msg(otmp, sx, sy);
return await cannot_push(otmp, sx, sy);
}
/* rumbling disturbs buried zombies */
disturb_buried_zombies(sx, sy);
if (ttmp) {
let newlev = 0;
switch (ttmp.ttyp) {
case LANDMINE:
if (rn2(10)) {
obj_extract_self(otmp);
place_object(otmp, rx, ry);
newsym(sx, sy);
await pline("%s! %s %s land mine.",
(!Deaf() || !Blind()) ? "KAABLAMM!!"
: "Gadzooks",
Tobjnam(otmp, "trigger"),
ttmp.madeby_u ? "your" : "a");
blow_up_landmine(ttmp);
fill_pit(u.ux, u.uy);
if (cansee(rx, ry))
newsym(rx, ry);
return sobj_at(BOULDER, sx, sy) ? -1 : 0;
}
break;
case SPIKED_PIT:
case PIT:
obj_extract_self(otmp);
if (!Blind() && g.viz_array?.[ry])
g.viz_array[ry][rx] |= IN_SIGHT;
if (!await flooreffects(otmp, rx, ry, "fall")) {
place_object(otmp, rx, ry);
}
if (mtmp && !Blind())
newsym(rx, ry);
return sobj_at(BOULDER, sx, sy) ? -1 : 0;
case HOLE:
case TRAPDOOR:
Soundeffect('se_kerplunk_boulder_gone', 40);
if (Blind())
await pline("Kerplunk! You no longer feel %s.",
the(xname(otmp)));
else
await pline("%s%s and %s a %s in the %s!",
Tobjnam(otmp, (ttmp.ttyp === TRAPDOOR)
? "trigger"
: "fall"),
(ttmp.ttyp === TRAPDOOR) ? "" : " into",
otense(otmp, "plug"),
(ttmp.ttyp === TRAPDOOR) ? "trap door" : "hole",
surface(rx, ry));
deltrap(ttmp);
useupf(otmp, 1);
await bury_objs(rx, ry);
{
const loc = g.level.at(rx, ry);
if (loc) {
loc.wall_info &= ~W_NONDIGGABLE;
loc.candig = 1;
}
}
if (cansee(rx, ry))
newsym(rx, ry);
return sobj_at(BOULDER, sx, sy) ? -1 : 0;
case LEVEL_TELEP:
newlev = random_teleport_level();
if (newlev === depth(u.uz)) {
await dopush(sx, sy, rx, ry, otmp, costly);
continue;
}
/* FALLTHROUGH */
case TELEP_TRAP:
await rock_disappear_msg(otmp);
otmp.next_boulder = 0;
if (ttmp.ttyp === TELEP_TRAP) {
await rloco(otmp);
} else {
if (costly)
await stolen_value(otmp, rx, ry, !ttmp.tseen, false);
obj_extract_self(otmp);
add_to_migration(otmp);
const dest = {};
get_level(dest, newlev);
otmp.ox = dest.dnum;
otmp.oy = dest.dlevel;
otmp.owornmask = MIGR_RANDOM;
}
seetrap(ttmp);
return sobj_at(BOULDER, sx, sy) ? -1 : 0;
default:
break; /* boulder not affected by this trap */
}
}
if (await boulder_hits_pool(otmp, rx, ry, true))
continue;
/*
* Re-link at top of fobj chain so that pile order is preserved
* when level is restored.
* C: if (otmp != fobj) { remove_object(otmp); place_object(otmp, otmp->ox, otmp->oy); }
* In JS, game.fobj doesn't exist as a head pointer. The flat
* array always has the boulder somewhere; this re-link is only
* needed in C to maintain list order. Since JS's place_object
* emits ^place events matching C, we must do the same re-link.
* C's fobj is the most-recently-placed object; in JS the
* equivalent is the last element of level.objects.
*/
{
const objs = g.level?.objects || [];
const lastObj = objs.length > 0 ? objs[objs.length - 1] : null;
if (otmp !== lastObj) {
remove_object(otmp);
place_object(otmp, otmp.ox, otmp.oy);
}
}
await dopush(sx, sy, rx, ry, otmp, costly);
} else {
await cannot_push_msg(otmp, sx, sy);
return await cannot_push(otmp, sx, sy);
}
}
return 0;
}
export function init_hack_globals() {
game.inspoteffects = 0;
game.spotloc_x = 0;
game.spotloc_y = 0;
game.spotterrain = 0;
game.spottrap = null;
game.spottraptyp = 0;
}
|