All files / js polyself.js

65.49% Statements 1253/1913
52.5% Branches 293/558
78.57% Functions 66/84
65.49% Lines 1253/1913

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 191373x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 174x 88x 88x 79x 74x 88x 88x 92x 92x 25x 25x 73x 24x 9x 24x 2x 36x   22x 73x 73x 73x 73x 73x 27x 73x 73x 73x 73x 73x 73x 73x 23x 13x 23x     73x 73x 24x 24x 2x 2x 2x 2x 23x 24x 24x 21x 73x 73x 73x 73x 73x 73x 73x 73x 73x 3x 1x 73x 73x 73x 24x 24x 24x 73x 73x 73x 73x 73x 73x 24x 73x 73x 73x 73x 1x 1x 1x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x   73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 5x 5x 12x 5x 5x 5x 12x 5x 73x 73x 73x 73x 73x 73x   73x 73x 73x 73x 73x 73x 73x 73x   73x 73x 73x 73x 73x 73x 73x 8x 4x 73x 73x 73x 73x 73x 73x 1x 1x 1x 1x 1x 1x 1x         1x 1x     1x 1x 73x 73x 73x 13x 13x 64x 64x 64x 12x 13x 73x 339x 339x 339x 339x 339x 339x 339x 339x 339x       339x                   339x 73x 73x 73x 73x 73x   73x 73x 73x 73x 73x 73x 73x 73x 73x   73x   73x 73x 73x 73x 73x 73x 73x 73x 24x 24x 24x 24x 24x 24x 24x   24x 1x 24x 22x 22x 24x 24x 648x 610x 648x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 73x 73x 88x 88x 88x 88x 88x 79x 79x 88x 88x 88x 88x 88x 88x 88x 88x 73x 73x 92x 92x 88x 88x 92x 73x 36x 36x 23x 23x 23x         36x 13x       13x 36x 73x 10x 10x 10x 10x 10x 10x 10x 8x 8x 8x 8x 8x 8x 8x 8x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x 1x 1x 1x 1x 10x 10x 10x 10x           10x 10x 10x 10x 10x     10x 1x 1x 1x 1x 10x 10x 10x 10x 10x 73x 73x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 73x 73x 2x 2x 73x 9x 9x 9x 9x 9x                 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x                         9x 9x 9x 9x 8x 9x 9x 9x 9x 9x 2x 2x 9x       9x 9x 9x 9x 9x 9x 9x 9x 73x 73x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x       23x 23x 5x 1x 1x 1x 1x 1x 5x 22x 22x 23x         23x 23x 23x 23x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 23x 17x 17x 17x 17x 17x 17x               17x 17x         17x 17x 17x 17x 17x                 17x 17x 17x               17x         17x 17x 9x 9x 17x       17x   17x           17x 17x 7x   17x             17x 17x 17x 17x 17x 17x 23x                                                               22x 4x 4x 4x 4x 4x 4x 4x 23x 23x 23x 9x 23x 12x 12x 21x 21x 23x 22x 22x 22x               22x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 73x 73x 73x 13x 13x 13x 13x 13x 13x 13x         13x 13x 13x 4x 4x 4x 4x 13x 9x 9x 13x 13x 13x 10x 10x 10x 10x 12x 3x 3x 3x 3x 3x 13x 13x 13x 13x 13x 12x 12x 12x 13x   13x   13x 11x 11x 11x 13x 13x 13x 1x 1x 1x 1x 13x 13x 13x       13x 13x 13x 13x 13x 13x 8x 8x 12x 5x 5x 5x 5x 13x     13x       13x             13x 13x 13x 13x 13x 2x 13x   11x 11x 11x 11x 11x 11x 11x 11x 13x 13x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x     13x         13x 13x 4x 4x 4x 13x                             13x 13x     13x     13x               13x 13x 13x 13x     13x 13x               13x 13x       13x 13x 1x     1x       1x 13x 13x     13x       13x       13x 13x 13x 13x 13x 13x 13x 13x 13x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 10x 10x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 1x 12x 13x 13x 73x 73x 13x 13x 13x 13x     13x   13x   13x 1x 1x 13x 13x 8x 8x 13x 5x 5x 13x 73x 13x 13x 13x 13x 8x 1x 1x 1x 1x 1x 1x 1x 8x 8x 2x 2x 2x 2x 8x       13x 3x           3x 3x 1x 1x 1x 1x 1x 1x 1x 3x               3x 13x                         13x 9x 1x 1x 1x 1x 1x 1x 9x         9x 1x 1x 1x 1x 1x 9x 13x 13x 9x                 9x 13x           13x   73x 14x 14x 14x 14x 14x 4x 3x 3x 2x 2x 2x 2x       2x 2x 2x 2x 3x           3x 3x 3x 3x 3x 4x     4x 14x 73x 73x 1x 1x 1x                 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 73x 73x 12x 12x 12x 12x 12x 12x 10x 10x 9x 9x 9x 9x 9x 9x 9x 9x 8x 8x 8x 8x 7x       7x           7x 1x 6x     6x 3x 3x 3x 3x 3x 3x 12x 73x 73x 2x       2x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 1x 1x 2x 73x 73x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 73x 73x                       73x 73x                                                                                                                                                         73x 73x                           73x 73x                                                                                                                                                                                                           73x 73x 1x 1x 1x                           1x               1x                 1x         1x 1x         1x       1x 1x 1x 1x     1x 1x 1x 1x 73x 73x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 73x 73x 1x       1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 1x 1x               1x 4x 4x 1x 1x 73x 73x                 73x 73x 10x 10x               10x 73x 73x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x       18x 18x 18x               18x     18x 18x 2x 2x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 9x 10x 9x 10x 9x 18x 18x 18x 10x           18x 18x 18x 18x 9x 10x 9x 10x 18x 18x 9x 10x 9x 18x 18x 18x 18x 9x 10x 9x 9x   18x 73x 73x 18x 18x 73x 73x 61x 61x 61x 61x 73x 73x                                             73x                               73x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x     24x 1x 1x 1x 24x 24x         24x 73x 73x 10x 10x 10x 73x 73x               73x 73x 17x 17x
// polyself.js -- Port of polyself.c
// Polymorph self routine: transformations, special abilities, body parts.
import { game } from './gstate.js';
import { rn2, rnd, rn1, c_d } from './rng.js';
import { pline, urgent_pline, You, Your, You_feel, You_cant, pline_The, impossible } from './pline.js';
import { B, Blind, Confusion, E, Flying, Free_action, H, Hallucination, has_ceiling, HLevitation, In_endgame, Invis, Is_airlevel, Is_waterlevel, Levitation, Passes_walls, Polymorph_control, Punished, See_invisible, Sick_resistance, Stone_resistance, Stunned, U_AP_TYPE, uarm, uarmc, uarmf, uarmg, uarmh, uarms, uarmu, Ugender, Unaware, Underwater, Upolyd } from './macros.js';
import { levl, m_at, t_at } from './map_access.js';
import { ABASE, ACURR, AMAX, adjabil, exercise, newhp, redist_attr } from './attrib.js';
import { newsym, find_ac, setBotl, status_initialize, canspotmon, canseemon, set_mimic_blocking } from './display.js';
import { rndexp, newpw } from './exper.js';
import { getdir, nomul } from './cmd.js';
import { getlin } from './input.js';
import { destroy_items, ignite_items, mksobj } from './mkobj.js';
import { an, cloak_simple_name, helm_simple_name, makeplural, simpleonames, simple_typename, the, the_unique_pm, vtense, xname, yname } from './objnam.js';
import { pmname, type_is_pname, hliquid } from './do_name.js';
import { mungspaces, plur, s_suffix, strstri, strsubst, surface, unsolid } from './hacklib.js';
import { encumber_msg } from './pickup.js';
import { amorphous, attacktype, attacktype_fordmg, breakarm, can_be_strangled, can_teleport, control_teleport, dmgtype, dmgtype_fromattack, flaming, has_horns, haseyes, hides_under, humanoid, infravision, is_animal, is_clinger, is_dwarf, is_elf, is_female, is_floater, is_flyer, is_giant, is_gnome, is_golem, is_hider, is_home_elemental, is_human, is_male, is_mind_flayer, is_mindless as mindless, is_neuter, is_orc, is_swimmer, is_sword, is_undead, is_unicorn, is_vampire, is_vampshifter, is_were, is_whirly, lays_eggs, likes_lava, mdistu, name_to_mon, name_to_monclass, nohands, nonliving, num_horns, passes_walls, perceives, poly_when_stoned, polyok, regenerates, resists_drli, resists_fire, set_mon_data, sliparm, slithy, sticks, strongmonst, telepathic, touch_petrifies, verysmall, webmaker, WrappingAllowed, your_race } from './mondata.js';
import { deltrap, dotrap, feeltrap, instapetrify, maketrap, reset_utrap, set_utrap } from './trap.js';
import { done, helpless, killed, livelog_printf, losehp, rounddiv, unmul } from './hack.js';
import { couldsee } from './vision.js';
import { donning, cancel_don, Armor_gone, Cloak_off, Helmet_off, Gloves_off, Shield_off, Boots_off, Blindf_off } from './do_wear.js';
import { canletgo, dropx } from './do.js';
import { untwoweapon, uwepgone, uswapwepgone } from './wield.js';
import { setworn, racial_exception } from './worn.js';
import { make_stoned, make_slimed } from './potion.js';
import { buried_ball_to_freedom, bury_objs } from './dig.js';
import { were_summon, were_beastie, counter_were } from './were.js';
import { On_stairs } from './stairs.js';
import { BZ_OFS_AD } from './const.js';
import { A_CON, A_DEX, A_STR, A_WIS, ACID_RES, ANTI_MAGIC, ANTIMAGIC, ARM, ARROW_TRAP, BEAR_TRAP, BLINDED, BLND_RES, BLOOD, BOLT_LIM, COLD_RES, DART_TRAP, DIED, DISINT_RES, DISMOUNT_POLY, DRAIN_RES, ECMD_CANCEL, ECMD_OK, ECMD_TIME, EYE, FACE, FEMALE, FINGER, FINGERTIP, FIRE_RES, FIRE_TRAP, FLYING, FOOT, FREE_ACTION, FROMFORM, FROMOUTSIDE, FROMRACE, G_EXTINCT, G_GENOD, GENOCIDED, GLIB, HAIR, HALLUC_RES, HAND, HANDED, HEAD, HOLE, I_SPECIAL, INFRAVISION, INVIS, IS_AIR, IS_FOUNTAIN, ismnum, KILLED_BY, KILLED_BY_AN, LANDMINE, LEG, LEVEL_TELEP, LEVITATION, LIGHT_HEADED, LL_CONDUCT, LL_MINORAC, LOW_PM, LS_MONSTER, LUNG, M_AP_FURNITURE, M_AP_MONSTER, M_AP_NOTHING, M_AP_OBJECT, M_AP_TYPE, MAGIC_PORTAL, MAGIC_TRAP, MALE, MAXULEV, NATTK, NECK, NO_KILLER_PREFIX, NO_TRAP_FLAGS, NON_PM, NOSE, P_SABER, P_SHORT_SWORD, PASSES_WALLS, PIT, POISON_RES, POLY_CONTROLLED, POLY_LOW_CTRL, POLY_MONSTER, POLY_NOFLAGS, POLY_REVERT, POLY_TRAP, REFLECTING, REGENERATION, ROCKTRAP, ROLLING_BOULDER_TRAP, RUST_TRAP, SEE_INVIS, SHOCK_RES, SHOP_WEB_COST, SHOPBASE, SICK, SICK_ALL, SICK_RES, SLEEP_RES, SLIMED, SLP_GAS_TRAP, SPIKED_PIT, SPINE, SQKY_BOARD, STAIRS, STEALTH, STOMACH, STONE_RES, STONED, STONING, STR18, STR19, STRANGLED, STUNNED, SWIMMING, TELEP_TRAP, TELEPAT, TELEPORT, TELEPORT_CONTROL, TOE, TRAPDOOR, TT_BEARTRAP, TT_BURIEDBALL, TT_INFLOOR, TT_LAVA, TT_PIT, TT_WEB, UNCHANGING, VIBRATING_SQUARE, W_ARM, W_ARMC, W_ARMF, W_ARMG, W_ARMH, W_ARMS, W_ARMU, WARN_OF_MON, WEB } from './const.js';
import { useup, makeknown, observe_object } from './invent.js';
import { DEADMONSTER, Monnam, Some_Monnam, aggravate, hideunder, l_monnam, mon_nam, see_monsters, set_ustuck, setmangry, update_inventory, wakeup, y_monnam } from './mon.js';
// observe_object: moved to invent.js import below
import { add_damage, in_rooms } from './shk.js';
import { set_itimeout } from './timeout.js';
import { rank_of } from './botl.js';
import { can_ride, dismount_steed } from './steed.js';
import { ubuzz } from './zap.js';
import { selftouch, make_blinded, make_sick, make_glib, expels } from './mhitu.js';
import { newuhs } from './eat.js';
import { artifact_light, arti_light_radius, emits_light, del_light_source, new_light_source, end_burn } from './light.js';
import { is_pool, is_pool_or_lava } from './dbridge.js';
import { throwit } from './dothrow.js';
import { races } from './roles.js';
import { golemhp } from './makemon.js';
import {
    mons, SPECIAL_PM,
    PM_STONE_GOLEM, PM_FLESH_GOLEM, PM_IRON_GOLEM,
    PM_GREEN_SLIME, PM_STALKER,
    PM_FLOATING_EYE, PM_MEDUSA,
    PM_GRAY_DRAGON, PM_SILVER_DRAGON, PM_GOLD_DRAGON,
    PM_RED_DRAGON, PM_ORANGE_DRAGON, PM_WHITE_DRAGON,
    PM_BLACK_DRAGON, PM_BLUE_DRAGON, PM_GREEN_DRAGON, PM_YELLOW_DRAGON,
    PM_VAMPIRE, PM_VAMPIRE_LEADER, PM_VAMPIRE_BAT,
    PM_WOLF, PM_FOG_CLOUD,
    PM_HUMAN, PM_ORC, PM_ELF, PM_DWARF, PM_GNOME, PM_GIANT,
    PM_HILL_ORC, PM_MORDOR_ORC, PM_URUK_HAI, PM_ORC_CAPTAIN,
    PM_GREEN_ELF, PM_GREY_ELF, PM_STONE_GIANT, PM_HILL_GIANT,
    PM_ALIGNED_CLERIC, PM_CLERIC,
    PM_GREMLIN, PM_QUEEN_BEE,
    PM_PURPLE_WORM, PM_BABY_PURPLE_WORM, PM_SHRIEKER,
    PM_BABY_GRAY_DRAGON, PM_AMOROUS_DEMON,
    PM_MUMAK, PM_MASTODON, PM_SHARK, PM_JELLYFISH, PM_KRAKEN,
    PM_RAVEN, PM_KI_RIN, PM_ROTHE, PM_OWLBEAR, PM_LONG_WORM_TAIL,
    PM_GIANT_EEL, PM_ELECTRIC_EEL,
    PM_MIND_FLAYER, PM_MASTER_MIND_FLAYER,
    PM_FIRE_VORTEX, PM_FLAMING_SPHERE, PM_FIRE_ELEMENTAL, PM_SALAMANDER,
    PM_AIR_ELEMENTAL, PM_EARTH_ELEMENTAL, PM_WATER_ELEMENTAL,
    PM_CAVE_SPIDER, PM_GIANT_SPIDER, PM_BLACK_LIGHT,
    PM_STRAW_GOLEM, PM_PAPER_GOLEM, PM_ROPE_GOLEM, PM_LEATHER_GOLEM,
    PM_GOLD_GOLEM, PM_WOOD_GOLEM, PM_CLAY_GOLEM, PM_GLASS_GOLEM,
    PM_WINGED_GARGOYLE, PM_MARILITH, PM_GHOUL,
    M1_NOHEAD, S_GHOST,
    S_MIMIC, S_DRAGON, S_NYMPH, S_CENTAUR, S_UNICORN,
    S_HUMAN, S_MUMMY, S_ZOMBIE, S_ANGEL,
    S_LEPRECHAUN, S_QUANTMECH, S_VAMPIRE, S_ORC, S_GIANT,
    S_COCKATRICE, S_DOG, S_FELINE, S_RODENT, S_YETI,
    S_LIGHT, S_EEL, S_WORM, S_SPIDER, S_EYE,
    S_JELLY, S_PUDDING, S_BLOB, S_VORTEX, S_ELEMENTAL,
    S_FUNGUS,
    M2_NOPOLY, M2_UNDEAD, M2_HUMAN, M2_ELF, M2_STRONG,
    MR_FIRE, MR_COLD, MR_SLEEP, MR_DISINT, MR_ELEC, MR_POISON, MR_ACID, MR_STONE,
    AD_MAGM, AD_RBRE, AD_HALU, AD_BLND, AD_STUN, AD_FIRE, AD_COLD, AD_ELEC,
    AD_ACID, AD_DRST, AD_CONF, AD_ANY, AD_PHYS,
    AT_WEAP, AT_CLAW, AT_SPIT, AT_BREA, AT_GAZE, AT_EXPL, AT_ENGL,
    G_UNIQ, G_NOGEN, MS_SHRIEK, MZ_HUGE, MZ_SMALL,
} from './monsters.js';
import { ACID_VENOM, AMULET_OF_STRANGULATION, AMULET_OF_UNCHANGING, BLACK_DRAGON_SCALE_MAIL, BLACK_DRAGON_SCALES, BLINDING_VENOM, BLUE_DRAGON_SCALE_MAIL, BLUE_DRAGON_SCALES, CORPSE, GOLD_DRAGON_SCALE_MAIL, GOLD_DRAGON_SCALES, GRAY_DRAGON_SCALE_MAIL, GRAY_DRAGON_SCALES, GREEN_DRAGON_SCALE_MAIL, GREEN_DRAGON_SCALES, Is_dragon_armor, Is_dragon_scales, is_flimsy, LEATHER, MUMMY_WRAPPING, ORANGE_DRAGON_SCALE_MAIL, ORANGE_DRAGON_SCALES, RED_DRAGON_SCALE_MAIL, RED_DRAGON_SCALES, RUBBER_HOSE, SILVER_DRAGON_SCALE_MAIL, SILVER_DRAGON_SCALES, STRANGE_OBJECT, WEAPON_CLASS, WHITE_DRAGON_SCALE_MAIL, WHITE_DRAGON_SCALES, YELLOW_DRAGON_SCALE_MAIL, YELLOW_DRAGON_SCALES } from './objects.js';
import { Soundeffect } from './sounds.js';
import { unpunish } from './read.js';
import { weapon_descr } from './weapon.js';
import { retouch_equipment } from './artifact.js';
import { pet_ranged_attk } from './dogmove.js';
// ── Non-RNG stubs for functions not yet ported ──
const NO_PART = -1;
// H, E, B: imported from macros.js
// HLevitation: imported from macros.js
function ELevitation() { return E(LEVITATION); }
function BLevitation_get() { return B(LEVITATION); }
function BLevitation_set(v) { game.u.uprops[LEVITATION].blocked = v; }
function HFlying() { return H(FLYING); }
function EFlying() { return E(FLYING); }
function BFlying_get() { return B(FLYING); }
function BFlying_set(v) { game.u.uprops[FLYING].blocked = v; }
function BStealth_get() { return B(STEALTH); }
function BStealth_set(v) { game.u.uprops[STEALTH].blocked = v; }
function HWarn_of_mon_get() { return H(WARN_OF_MON); }
function HWarn_of_mon_set(v) { game.u.uprops[WARN_OF_MON].intrinsic = v; }
// Polymorph_control imported from macros.js (uses correct POLYMORPH_CONTROL constant)
function Unchanging_prop() { return !!(H(UNCHANGING) || E(UNCHANGING)); }
function Sick_prop() { return !!H(SICK); }
function Stoned_prop() { return !!H(STONED); }
function Strangled_prop() { return !!H(STRANGLED); }
function Strangled_get() { return H(STRANGLED); }
function Strangled_set(v) { game.u.uprops[STRANGLED].intrinsic = v; }
function Slimed_prop() { return !!H(SLIMED); }
// Punished imported from macros.js
// Passes_walls imported from macros.js
// Sick_resistance imported from macros.js
// Unaware imported from macros.js
// Access macros
function uwep() { return game.u?.uwep; }
// uarm: imported from macros.js
// uarmc: imported from macros.js
// uarmh: imported from macros.js
// uarms: imported from macros.js
// uarmg: imported from macros.js
// uarmf: imported from macros.js
// uarmu: imported from macros.js
function uamul() { return game.u?.uamul; }
function ublindf() { return game.u?.ublindf; }
function uskin_get() { return game.u?.uskin; }
function uskin_set(v) { game.u.uskin = v; }
function uswapwep() { return game.u?.uswapwep; }
// Monster helper functions imported from mondata.js
// is_vampshifter imported from mondata.js
function valid_vampshiftform(cham, mndx) {
    if (cham >= LOW_PM && is_vampire(mons[cham])) {
        if (mndx === PM_VAMPIRE_BAT || mndx === PM_FOG_CLOUD
            || (mndx === PM_WOLF && cham !== PM_VAMPIRE))
            return true;
    }
    return false;
}
function is_bat(ptr) { return ptr && (ptr.mname === 'bat' || ptr.mname === 'giant bat' || ptr.mname === 'vampire bat'); }
function is_placeholder(ptr) { return ptr && (ptr.geno & G_NOGEN) !== 0 && (ptr.mflags2 & M2_NOPOLY) !== 0; }
// polyok: imported from mondata.js
// the_unique_pm: imported from objnam.js
// type_is_pname imported from do_name.js
// breakarm, sliparm, nohands, verysmall, slithy, has_horns, num_horns imported from mondata.js
// is_whirly imported from mondata.js
// amorphous imported from mondata.js
// flaming: imported from mondata.js
// is_home_elemental: imported from mondata.js
// sticks imported from mondata.js
function cantwield(ptr) { return nohands(ptr) || verysmall(ptr); }
function could_twoweap(ptr) { return humanoid(ptr) && (ptr.mattk?.some(a => a.aatyp === AT_WEAP) ?? false); }
// mindless: imported from mondata.js as is_mindless
// telepathic: imported from mondata.js
// infravision imported from mondata.js
function pm_invisible(ptr) {
    return ptr === mons[PM_STALKER] || ptr === mons[PM_BLACK_LIGHT];
}
// can_teleport imported from mondata.js
// control_teleport imported // simplified
// is_floater, is_flyer, is_swimmer imported from mondata.js
// passes_walls imported from mondata.js
// regenerates imported // simplified
// haseyes, dmgtype, dmgtype_fromattack, resists_drli imported from mondata.js
function can_breathe(ptr) { return attacktype(ptr, AT_BREA); }
// attacktype, attacktype_fordmg imported from mondata.js
// is_hider imported // simplified
// hides_under imported from mondata.js
// is_unicorn, is_mind_flayer, lays_eggs imported from mondata.js
function eggs_in_water(ptr) {
    return lays_eggs(ptr) && ptr.mlet === S_EEL && is_swimmer(ptr);
}
// likes_lava imported from mondata.js
// poly_when_stoned imported from mondata.js
// is_pool imported from dbridge.js
// touch_petrifies imported from mondata.js
// can_ride imported from steed.js
// is_animal imported from mondata.js
// webmaker: imported from mondata.js
// is_flimsy: imported from objects.js
// donning imported // simplified
// racial_exception imported // simplified
// is_sword: imported from mondata.js
// weapon_descr: imported from weapon.js
// golemhp imported from makemon.js
// emits_light imported from light.js
// resists_fire imported // simplified
// canseemon imported from display.js
// canspotmon imported from display.js
// couldsee imported // simplified
// mdistu imported from display.js
// U_AP_TYPE imported from macros.js
function weirdnonliving(ptr) { return is_golem(ptr) || (ptr && ptr.mlet === S_VORTEX); }
// Stub functions (no RNG)
// set_mon_data imported from mondata.js
// exercise imported from attrib.js
// losehp imported from hack.js
// set_ustuck imported from mon.js
// make_sick imported from mhitu.js
// make_stoned imported
// make_slimed imported from potion.js
// make_blinded imported from mhitu.js
// make_glib imported from mhitu.js
// selftouch imported from mhitu.js
// retouch_equipment: imported from artifact.js
// encumber_msg imported from pickup.js
// newuhs: imported from eat.js
// see_monsters imported from mon.js
// unpunish imported from ball.js
// untwoweapon imported
// setworn imported
// cancel_don imported
// useup imported from invent.js
// Armor_gone imported
// Cloak_off imported
// Helmet_off imported
// Gloves_off imported
// Shield_off imported
// Boots_off imported
// Blindf_off imported
async function dropp(obj) {
    for (let otmp = game.invent; otmp; otmp = otmp.nobj) {
        if (otmp === obj) {
            await dropx(obj);
            break;
        }
    }
}
// dropx imported from invent.js
// uwepgone, uswapwepgone imported from wield.js
// update_inventory imported from mon.js
// makeknown imported from invent.js
// del_light_source imported from light.js
// new_light_source imported from light.js
function monst_to_any(mon) { return mon; }
// expels imported
// dismount_steed imported from steed.js
// instapetrify imported from trap.js
// buried_ball_to_freedom imported
// were_summon imported
// killed imported
// wakeup imported from mon.js
// destroy_items: imported from mkobj.js
function mkclass_poly(_class) { return NON_PM; }
// mungspaces imported from hacklib.js
// cloak_simple_name: imported from objnam.js
// helm_simple_name: imported from objnam.js
// plur imported from hacklib.js
// Ugender imported from macros.js
// done imported from hack.js
// hideunder: imported from mon.js
function learn_egg_type(_mndx) { /* TODO */ }
function egg_type_from_parent(_mndx, _flag) { return _mndx; }
// add_damage imported from shk.js
// in_rooms imported from shk.js
// On_stairs imported
// IS_AIR imported // simplified
// Is_airlevel imported from dbridge.js
// IS_FOUNTAIN imported // simplified
async function youhiding(via_enlghtmt, msgflag) {
    let buf = 'hiding';
    if (U_AP_TYPE() !== M_AP_NOTHING) {
        buf = 'mimicking';
        if (U_AP_TYPE() === M_AP_OBJECT) {
            buf += ` ${an(simple_typename(game.player.mappearance))}`;
        } else if (U_AP_TYPE() === M_AP_FURNITURE) {
            buf += ' something';
        } else if (U_AP_TYPE() === M_AP_MONSTER) {
            buf += ' someone';
        }
    }
    if (via_enlghtmt) {
        return;
    }
    await You(`are ${msgflag ? 'already' : 'now'} ${buf}.`);
}
// Is_dragon_armor, Is_dragon_scales: imported from objects.js
// WrappingAllowed imported from mondata.js
// rank_of imported from botl.js
function character_race(mndx) {
    for (const r of races) {
        if (!r.noun) break;
        if (r.mnum === mndx) return r;
    }
    return null;
}
export function setuhpmax(newmax, even_when_polyd) {
    const u = game.u;
    if (!Upolyd() || even_when_polyd) {
        if (newmax !== u.uhpmax) {
            u.uhpmax = newmax;
            if (u.uhpmax > (u.uhppeak || 0))
                u.uhppeak = u.uhpmax;
            setBotl('setuhpmax');
        }
        if (u.uhp > u.uhpmax) {
            u.uhp = u.uhpmax;
            setBotl('setuhpmax');
        }
    } else {
        if (newmax !== u.mhmax) {
            u.mhmax = newmax;
            setBotl('setuhpmax');
        }
        if (u.mh > u.mhmax) {
            u.mh = u.mhmax;
            setBotl('setuhpmax');
        }
    }
}
// rounddiv imported from hack.js
// livelog_printf imported
// strsubst imported from hacklib.js
// artifact_light imported from light.js
// arti_light_radius imported from light.js
function maybe_adjust_light(_obj, _old) { /* TODO */ }
// end_burn imported from light.js
// unmul imported
// strstri imported from hacklib.js
// dotrap imported from trap.js
// deltrap imported
// bury_objs imported
// maketrap imported from bottleneck.js
// t_at imported // simplified
// ubuzz imported from zap.js
function ubreatheu(_mattk) { /* TODO */ }
// hliquid imported from do_name.js
function wizard() { return game.flags?.debug; }
// can_be_strangled imported from mondata.js
const shudder_for_moment = 'shudder for a moment.';
const thats_enough_tries = "That's enough tries!";
const Never_mind = 'Never mind.';
const no_longer_petrify_resistant = 'No longer petrify-resistant, you';
// ── Exported functions ──
/* update the youmonst data structure pointer and intrinsics */
export function set_uasmon() {
    const u = game.u;
    const mdat = mons[u.umonnum];
    if (!mdat) return;
    const was_vampshifter = valid_vampshiftform(game.player.cham, u.umonnum);
    set_mon_data(game.player, mdat);
    game.player.m_id = 1;
    if (false /* Protection_from_shape_changers */) {
        game.player.cham = NON_PM;
    } else if (is_vampire(game.player.data)) {
        game.player.cham = game.player.mnum;
    } else if (!was_vampshifter) {
        game.player.cham = NON_PM;
    }
    u.mcham = game.player.cham;
    function PROPSET(propIndx, on) {
        if (on) u.uprops[propIndx].intrinsic |= FROMFORM;
        else u.uprops[propIndx].intrinsic &= ~FROMFORM;
    }
    function resist_from_form(mrTyp) { return (game.player.data.mresists & mrTyp) !== 0; }
    PROPSET(FIRE_RES, resist_from_form(MR_FIRE));
    PROPSET(COLD_RES, resist_from_form(MR_COLD));
    PROPSET(SLEEP_RES, resist_from_form(MR_SLEEP));
    PROPSET(DISINT_RES, resist_from_form(MR_DISINT));
    PROPSET(SHOCK_RES, resist_from_form(MR_ELEC));
    PROPSET(POISON_RES, resist_from_form(MR_POISON));
    PROPSET(ACID_RES, resist_from_form(MR_ACID));
    PROPSET(STONE_RES, resist_from_form(MR_STONE));
    {
        const save_uwep = game.u.uwep;
        game.u.uwep = null;
        PROPSET(DRAIN_RES, resists_drli(game.player));
        game.u.uwep = save_uwep;
    }
    PROPSET(ANTIMAGIC, (dmgtype(mdat, AD_MAGM)
        || mdat === mons[PM_BABY_GRAY_DRAGON]
        || dmgtype(mdat, AD_RBRE)));
    PROPSET(SICK_RES, (mdat.mlet === S_FUNGUS || mdat === mons[PM_GHOUL]));
    PROPSET(STUNNED, (mdat === mons[PM_STALKER] || is_bat(mdat)));
    PROPSET(HALLUC_RES, dmgtype(mdat, AD_HALU));
    PROPSET(SEE_INVIS, perceives(mdat));
    PROPSET(TELEPAT, telepathic(mdat));
    PROPSET(INFRAVISION, infravision(Upolyd() ? mdat : mons[game.u?.urace?.mnum ?? 0]));
    PROPSET(INVIS, pm_invisible(mdat));
    PROPSET(TELEPORT, can_teleport(mdat));
    PROPSET(TELEPORT_CONTROL, control_teleport(mdat));
    PROPSET(LEVITATION, is_floater(mdat));
    PROPSET(FLYING, (is_flyer(mdat) && !is_floater(mdat)));
    PROPSET(SWIMMING, is_swimmer(mdat));
    PROPSET(PASSES_WALLS, passes_walls(mdat));
    PROPSET(REGENERATION, regenerates(mdat));
    PROPSET(REFLECTING, (mdat === mons[PM_SILVER_DRAGON]));
    PROPSET(BLINDED, !haseyes(mdat));
    PROPSET(BLND_RES, (dmgtype_fromattack(mdat, AD_BLND, AT_EXPL)
        || dmgtype_fromattack(mdat, AD_BLND, AT_GAZE)));
    if (!game.program_state?.restoring)
        float_vs_flight();
    polysense();
    status_initialize(true);
    game.were_changes = 0;
}
/* Levitation overrides Flying; set or clear BFlying|I_SPECIAL */
export function float_vs_flight() {
    const u = game.u;
    const stuck_in_floor = (u.utrap && u.utraptype !== TT_PIT);
    if ((HLevitation() || ELevitation())
        || ((HFlying() || EFlying()) && stuck_in_floor))
        BFlying_set(BFlying_get() | I_SPECIAL);
    else
        BFlying_set(BFlying_get() & ~I_SPECIAL);
    if ((HLevitation() || ELevitation()) && stuck_in_floor)
        BLevitation_set(BLevitation_get() | I_SPECIAL);
    else
        BLevitation_set(BLevitation_get() & ~I_SPECIAL);
    steed_vs_stealth();
    game.disp = game.disp || {};
    setBotl('float_vs_flight');
}
/* riding blocks stealth unless hero+steed fly */
export function steed_vs_stealth() {
    if (game.u.usteed && !Flying() && !Levitation())
        BStealth_set(BStealth_get() | FROMOUTSIDE);
    else
        BStealth_set(BStealth_get() & ~FROMOUTSIDE);
}
/* for changing into form that's immune to strangulation */
function check_strangling(on) {
    if (on) {
        const was_strangled = (Strangled_get() !== 0);
        if (uamul() && uamul().otyp === AMULET_OF_STRANGULATION
            && can_be_strangled(game.player)) {
            Strangled_set(6);
            setBotl('check_strangling');
            // Your("%s %s your %s!", ...) — would need await, skipping for sync context
        }
    } else {
        if (Strangled_get() && !can_be_strangled(game.player)) {
            Strangled_set(0);
            setBotl('check_strangling');
        }
    }
}
/* make a (new) human out of the player */
async function polyman(fmt, arg) {
    const u = game.u;
    const sticking = sticks(game.player.data) && u.ustuck && !u.uswallow;
    const was_mimicking = U_AP_TYPE() !== M_AP_NOTHING;
    const was_blind = !!Blind();
    const had_see_invis = !!See_invisible();
    if (Upolyd()) {
        // Deep copy: C uses memcpy which copies array contents.
        // Object.assign only copies references to .a arrays, so
        // subsequent redist_attr would corrupt the saved macurr/mamax.
        u.acurr = { a: [...u.macurr.a] };
        u.amax = { a: [...u.mamax.a] };
        u.umonnum = u.umonster;
        game.flags.female = u.mfemale;
    }
    set_uasmon();
    u.mh = u.mhmax = 0;
    u.mtimedone = 0;
    skinback(false);
    u.uundetected = 0;
    if (sticking)
        await uunstick();
    find_ac();
    if (was_mimicking) {
        if (game.multi < 0)
            await unmul('');
        game.player.m_ap_type = M_AP_NOTHING;
        game.player.mappearance = 0;
    }
    newsym(u.ux, u.uy);
    await urgent_pline(fmt, arg);
    // check whether player foolishly genocided self while poly'd
    if (ugenocided()) {
        game.killer = game.killer || {};
        game.killer.format = KILLED_BY;
        game.killer.name = 'self-genocide';
        await done(GENOCIDED);
    }
    if (!!See_invisible() !== had_see_invis)
        set_mimic_blocking();
    if (u.twoweap && !could_twoweap(game.player.data))
        await untwoweapon();
    if (u.utrap && u.utraptype === TT_PIT) {
        set_utrap(rn1(6, 2), TT_PIT);
    }
    if (was_blind && !Blind()) {
        // C ref: polyself.c polyman() — set_itimeout(&HBlinded, 1L)
        set_itimeout(BLINDED, 1);
        await make_blinded(0, true);
    }
    check_strangling(true);
    if (!Levitation() && !u.ustuck && is_pool_or_lava(u.ux, u.uy))
        /* spoteffects(true) */ ;
    see_monsters();
}
/* change hero's sex */
export function change_sex() {
    const u = game.u;
    if (!Upolyd()
        || (!is_male(game.player.data) && !is_female(game.player.data)
            && !is_neuter(game.player.data)))
        game.flags.female = !game.flags.female;
    if (Upolyd())
        u.mfemale = !u.mfemale;
    // max_rank_sz() — superfluous
    if ((Upolyd() ? u.mfemale : game.flags.female) && game.urole?.name?.f)
        game.pl_character = game.urole.name.f;
    else
        game.pl_character = game.urole?.name?.m || 'Adventurer';
    if (!Upolyd()) {
        u.umonnum = u.umonster;
    } else if (u.umonnum === PM_AMOROUS_DEMON) {
        game.flags.female = !game.flags.female;
        /* disabled: succubus/incubus swap since merged to PM_AMOROUS_DEMON */
    }
    set_uasmon();
}
/* livelog_newform — stub */
export function livelog_newform(_viapoly, _oldgend, _newgend) {
    /* TODO: livelog */
}
/* newman -- poly fails, get a "new" human form */
async function newman() {
    const u = game.u;
    const oldlvl = u.ulevel;
    let newlvl = oldlvl + rn1(5, -2); /* new = old + {-2,-1,0,+1,+2} */
    if (newlvl > 127 || newlvl < 1) {
        /* level went below 0 */
        await urgent_pline("Your new form doesn't seem healthy enough to survive.");
        game.killer = { format: KILLED_BY_AN, name: 'unsuccessful polymorph' };
        await done(DIED);
        await newuhs(false);
        await encumber_msg();
        return;
    }
    if (newlvl > MAXULEV)
        newlvl = MAXULEV;
    if (newlvl < oldlvl)
        u.ulevelmax -= (oldlvl - newlvl);
    if (u.ulevelmax < newlvl)
        u.ulevelmax = newlvl;
    u.ulevel = newlvl;
    const oldgend = poly_gender();
    if (game.sex_change_ok && !rn2(10))
        change_sex();
    await adjabil(oldlvl, u.ulevel);
    u.uexp = rndexp(false);
    redist_attr();
    /* New hit points */
    let hpmax = u.uhpmax;
    for (let i = 0; i < oldlvl; i++)
        hpmax -= (u.uhpinc?.[i] || 0);
    const hpRoll = rn1(4, 8);
    hpmax = rounddiv(hpmax * hpRoll, 10);
    for (let i = 0; (u.ulevel = i) < newlvl; i++)
        hpmax += newhp();
    if (hpmax < u.ulevel)
        hpmax = u.ulevel;
    u.uhp = rounddiv(u.uhp * hpmax, u.uhpmax);
    setuhpmax(hpmax, true);
    /* Do the same for spell power */
    let enmax = u.uenmax;
    for (let i = 0; i < oldlvl; i++)
        enmax -= (u.ueninc?.[i] || 0);
    const enRoll = rn1(4, 8);
    enmax = rounddiv(enmax * enRoll, 10);
    for (let i = 0; (u.ulevel = i) < newlvl; i++)
        enmax += newpw();
    if (enmax < u.ulevel)
        enmax = u.ulevel;
    u.uen = rounddiv(u.uen * enmax, (u.uenmax < 1) ? 1 : u.uenmax);
    u.uenmax = enmax;
    u.uhunger = rn1(500, 500);
    if (Sick_prop())
        await make_sick(0, null, false, SICK_ALL);
    if (Stoned_prop())
        await make_stoned(0, null, 0, null);
    if (u.uhp <= 0) {
        if (Polymorph_control()) {
            if (u.uhp <= 0)
                u.uhp = 1;
        } else {
            await urgent_pline("Your new form doesn't seem healthy enough to survive.");
            game.killer = { format: KILLED_BY_AN, name: 'unsuccessful polymorph' };
            await done(DIED);
            await newuhs(false);
            await encumber_msg();
            return;
        }
    }
    await newuhs(false);
    const newform = ((Upolyd() ? u.mfemale : game.flags.female) && game.urace?.individual?.f)
        ? game.urace.individual.f
        : (game.urace?.individual?.m)
            ? game.urace.individual.m
            : game.urace?.noun || 'human';
    await polyman('You feel like a new %s!', newform);
    const newgend = poly_gender();
    if (newlvl !== oldlvl)
        livelog_printf(LL_MINORAC, 'became experience level %d as a new %s', newlvl, newform);
    else
        livelog_newform(true, oldgend, newgend);
    if (Slimed_prop()) {
        await Your('body transforms, but there is still slime on you.');
        await make_slimed(10, null);
    }
    game.disp = game.disp || {};
    setBotl('newman');
    see_monsters();
    await encumber_msg();
    await retouch_equipment(2);
    if (!uarmg())
        await selftouch(no_longer_petrify_resistant);
}
/* polyself -- main entry point for polymorphing the hero */
export async function polyself(psflags) {
    const u = game.u;
    let mntmp;
    let gvariant = 0; // NEUTRAL
    const forcecontrol_init = (psflags & POLY_CONTROLLED) !== 0;
    let forcecontrol = forcecontrol_init;
    const low_control = (psflags & POLY_LOW_CTRL) !== 0;
    const monsterpoly_init = (psflags & POLY_MONSTER) !== 0;
    let monsterpoly = monsterpoly_init;
    const formrevert = (psflags & POLY_REVERT) !== 0;
    const draconian = uarm() && Is_dragon_armor(uarm());
    const iswere = ismnum(u.ulycn);
    const isvamp = is_vampire(game.player.data)
        || is_vampshifter(game.player);
    let controllable_poly = Polymorph_control() && !(Stunned() || Unaware());
    if (Unchanging_prop()) {
        await You('fail to transform!');
        return;
    }
    if (!Polymorph_control() && !forcecontrol && !draconian && !iswere
        && !isvamp) {
        if (rn2(20) > ACURR(A_CON)) {
            await pline(shudder_for_moment);
            await losehp(rnd(30), 'system shock', KILLED_BY_AN);
            exercise(A_CON, false);
            return;
        }
    }
    const old_light = emits_light(game.player.data);
    mntmp = NON_PM;
    if (formrevert) {
        mntmp = game.player.cham;
        monsterpoly = true;
        controllable_poly = false;
    }
    if (forcecontrol && low_control
        && (draconian || monsterpoly || isvamp || iswere))
        forcecontrol = false;
    if (monsterpoly && isvamp) {
        /* goto do_vampyr */
        mntmp = do_vampyr(mntmp, controllable_poly, gvariant);
        if (mntmp === null) return; // cancelled
        if (mntmp === PM_HUMAN)
            await newman();
        else
            await polymon(mntmp);
        /* goto made_change */
        handle_light_change(old_light);
        return;
    }
    if (controllable_poly || forcecontrol) {
        let tryct = 5;
        do {
            mntmp = NON_PM;
            let buf = await getlin('Become what kind of monster? [type the name]', null, { logHookedLifecycle: true });
            buf = mungspaces(buf || '');
            if (buf.charCodeAt(0) === 27) {
                // user canceled controlled poly
                if (forcecontrol) {
                    await pline(Never_mind);
                    return;
                }
                buf = '*'; // resort to random
            }
            const lower = buf.toLowerCase();
            if (lower === '*' || lower === 'random') {
                // explicitly requesting random result
                tryct = 0;
                continue;
            }
            let monClass = 0;
            const gvariantRef = { val: gvariant };
            mntmp = name_to_mon(buf, gvariantRef);
            gvariant = gvariantRef.val;
            if (mntmp < LOW_PM) {
                const mndxRef = { val: mntmp };
                monClass = name_to_monclass(buf, mndxRef);
                mntmp = mndxRef.val;
                if (monClass && mntmp === NON_PM) {
                    mntmp = (draconian && monClass === S_DRAGON)
                        ? armor_to_dragon(uarm().otyp)
                        : mkclass_poly(monClass);
                }
            } else if (is_placeholder(mons[mntmp])
                       && !your_race(mons[mntmp])
                       && mntmp !== PM_HUMAN) {
                if (mntmp === PM_ORC)
                    mntmp = rn2(3) ? PM_HILL_ORC : PM_MORDOR_ORC;
                else if (mntmp === PM_ELF)
                    mntmp = rn2(3) ? PM_GREEN_ELF : PM_GREY_ELF;
                else if (mntmp === PM_GIANT)
                    mntmp = rn2(3) ? PM_STONE_GIANT : PM_HILL_GIANT;
            }
            if (mntmp < LOW_PM) {
                if (!monClass)
                    await pline("I've never heard of such monsters.");
                else
                    await You_cant('polymorph into any of those.');
            } else if (game.flags?.debug && Upolyd()
                       && (mntmp === u.umonster
                           || (u.umonster === PM_CLERIC
                               && mntmp === PM_ALIGNED_CLERIC
                               && !strstri(buf, 'aligned')))) {
                await rehumanize();
                handle_light_change(0);
                return;
            } else if (iswere && (were_beastie(mntmp) === u.ulycn
                                  || mntmp === counter_were(u.ulycn)
                                  || (Upolyd() && mntmp === PM_HUMAN))) {
                if (Upolyd() && were_beastie(mntmp) !== u.ulycn)
                    mntmp = PM_HUMAN;
                else
                    mntmp = u.ulycn;
                break;
            } else if (!polyok(mons[mntmp])
                       && !(mntmp === PM_HUMAN
                            || (your_race(mons[mntmp])
                                && (mons[mntmp].geno & G_UNIQ) === 0)
                            || mntmp === game.urole?.mnum)) {
                let pm_name = pmname(mons[mntmp], game.flags.female ? FEMALE : MALE);
                if (the_unique_pm(mons[mntmp]))
                    pm_name = the(pm_name);
                else if (!type_is_pname(mons[mntmp]))
                    pm_name = an(pm_name);
                await You_cant(`polymorph into ${pm_name}.`);
            } else {
                break;
            }
        } while (--tryct > 0);
        if (!tryct)
            await pline(thats_enough_tries);
    } else if (draconian || iswere || isvamp) {
        if (draconian) {
            mntmp = armor_to_dragon(uarm().otyp);
            if (!(game.mvitals?.[mntmp]?.mvflags & G_GENOD)) {
                if (Is_dragon_scales(uarm())) {
                    await You('merge with your scaly armor.');
                } else {
                    await Your('armor reverts to scales as you merge with them.');
                    uarm().otyp += GRAY_DRAGON_SCALES - GRAY_DRAGON_SCALE_MAIL;
                    setBotl('polyself');
                }
                uskin_set(uarm());
                game.u.uarm = null;
                uskin_get().owornmask |= I_SPECIAL;
                update_inventory();
            }
        } else if (iswere) {
            if (Upolyd() && were_beastie(mntmp) !== u.ulycn)
                mntmp = PM_HUMAN;
            else
                mntmp = u.ulycn;
        } else if (isvamp) {
            mntmp = do_vampyr(mntmp, controllable_poly, gvariant);
            if (mntmp === null) return;
        }
        if (mntmp === PM_HUMAN)
            await newman();
        else
            await polymon(mntmp);
        handle_light_change(old_light);
        return;
    }
    if (mntmp < LOW_PM) {
        let tryct = 200;
        do {
            mntmp = rn1(SPECIAL_PM - LOW_PM, LOW_PM);
            if (polyok(mons[mntmp]) && !is_placeholder(mons[mntmp]))
                break;
        } while (--tryct > 0);
    }
    game.sex_change_ok = (game.sex_change_ok || 0) + 1;
    if (!polyok(mons[mntmp]) || (!forcecontrol && !rn2(5))
        || your_race(mons[mntmp])) {
        await newman();
    } else {
        await polymon(mntmp);
    }
    game.sex_change_ok--;
    handle_light_change(old_light);
}
function handle_light_change(old_light) {
    const new_light_val = emits_light(game.player.data);
    if (old_light !== new_light_val) {
        if (old_light)
            del_light_source(LS_MONSTER, monst_to_any(game.player));
        let nl = new_light_val;
        if (nl === 1) ++nl;
        if (nl)
            new_light_source(game.u.ux, game.u.uy, nl, 0, monst_to_any(game.player));
    }
}
function do_vampyr(mntmp, controllable_poly, gvariant) {
    if (mntmp < LOW_PM || (mons[mntmp]?.geno & G_UNIQ)) {
        mntmp = (game.player.data === mons[PM_VAMPIRE_LEADER]
            && !rn2(10)) ? PM_WOLF
            : !rn2(4) ? PM_FOG_CLOUD
                : PM_VAMPIRE_BAT;
        if (ismnum(game.player.cham)
            && !is_vampire(game.player.data) && !rn2(2))
            mntmp = game.player.cham;
    }
    if (controllable_poly) {
        // Would prompt y_n — simplified: always yes
    }
    return mntmp;
}
// were_beastie imported from were.js
/* (try to) make a mntmp monster out of the player; return 1 if successful */
export async function polymon(mntmp) {
    const u = game.u;
    const sticking = sticks(game.player.data) && u.ustuck && !u.uswallow;
    const was_blind = !!Blind();
    let dochange = false;
    let was_expelled = false;
    const was_hiding_under = u.uundetected && hides_under(game.player.data);
    if (game.mvitals?.[mntmp]?.mvflags & G_GENOD) {
        await You_feel(`rather ${pmname(mons[mntmp], game.flags.female ? FEMALE : MALE)}-ish.`);
        exercise(A_WIS, true);
        return 0;
    }
    /* conduct */
    u.uconduct = u.uconduct || {};
    if (!u.uconduct.polyselfs) {
        u.uconduct.polyselfs = 1;
        livelog_printf(LL_CONDUCT,
            'changed form for the first time, becoming %s',
            an(pmname(mons[mntmp], game.flags.female ? FEMALE : MALE)));
    } else {
        u.uconduct.polyselfs++;
    }
    exercise(A_CON, false);
    exercise(A_WIS, true);
    if (!Upolyd()) {
        // Save current attrs before polymorph (deep copy .a arrays)
        u.macurr = { a: [...u.acurr.a] };
        u.mamax = { a: [...u.amax.a] };
        u.mfemale = game.flags.female;
    } else {
        // Restore saved attrs (re-polymorph while already poly'd)
        u.acurr = { a: [...u.macurr.a] };
        u.amax = { a: [...u.mamax.a] };
        game.flags.female = u.mfemale;
    }
    /* if stuck mimicking gold, stop immediately */
    if (game.multi < 0 && U_AP_TYPE() === M_AP_OBJECT
        && game.player.data?.mlet !== S_MIMIC)
        await unmul('');
    if (mons[mntmp].mlet !== S_MIMIC) {
        game.player.m_ap_type = M_AP_NOTHING;
        game.player.mappearance = 0;
    }
    if (is_male(mons[mntmp])) {
        if (game.flags.female) dochange = true;
    } else if (is_female(mons[mntmp])) {
        if (!game.flags.female) dochange = true;
    } else if (!is_neuter(mons[mntmp]) && mntmp !== u.ulycn) {
        if (game.sex_change_ok && !rn2(10))
            dochange = true;
    }
    const ustuckNam = u.ustuck ? Some_Monnam(u.ustuck) : '';
    let buf = (u.umonnum !== mntmp) ? '' : 'new ';
    if (dochange) {
        game.flags.female = !game.flags.female;
        buf += (is_male(mons[mntmp]) || is_female(mons[mntmp]))
            ? '' : game.flags.female ? 'female ' : 'male ';
    }
    buf += pmname(mons[mntmp], game.flags.female ? FEMALE : MALE);
    await You(`${(u.umonnum !== mntmp) ? 'turn into' : 'feel like'} ${an(buf)}!`);
    if (Stoned_prop() && poly_when_stoned(mons[mntmp])) {
        mntmp = PM_STONE_GOLEM;
        await make_stoned(0, 'You turn to stone!', 0, null);
    }
    u.mtimedone = rn1(500, 500);
    u.umonnum = mntmp;
    set_uasmon();
    /* New stats for monster — currently only strength */
    const newMaxStr = uasmon_maxStr();
    if (strongmonst(mons[mntmp])) {
        u.acurr.a[A_STR] = newMaxStr;
        u.amax.a[A_STR] = newMaxStr;
    } else {
        u.amax.a[A_STR] = newMaxStr;
        if (u.acurr.a[A_STR] > u.amax.a[A_STR])
            u.acurr.a[A_STR] = u.amax.a[A_STR];
    }
    if (Stone_resistance() && Stoned_prop()) {
        await make_stoned(0, 'You no longer seem to be petrifying.', 0, null);
    }
    if (Sick_resistance() && Sick_prop()) {
        await make_sick(0, null, false, SICK_ALL);
        // You("no longer feel sick.") — would need await in sync
    }
    if (Slimed_prop()) {
        if (flaming(game.player.data)) {
            await make_slimed(0, 'The slime burns away!');
        } else if (mntmp === PM_GREEN_SLIME) {
            await make_slimed(0, null);
        }
    }
    check_strangling(false);
    if (nohands(game.player.data))
        make_glib(0);
    const mlvl = mons[mntmp].mlevel || 0;
    if (game.player.data?.mlet === S_DRAGON && mntmp >= PM_GRAY_DRAGON) {
        u.mhmax = In_endgame(u.uz) ? (8 * mlvl) : (4 * mlvl + c_d(mlvl, 4));
    } else if (is_golem(game.player.data)) {
        u.mhmax = golemhp(mntmp);
    } else {
        if (!mlvl)
            u.mhmax = rnd(4);
        else
            u.mhmax = c_d(mlvl, 8);
        if (is_home_elemental(mons[mntmp]))
            u.mhmax *= 3;
    }
    u.mh = u.mhmax;
    if (u.ulevel < mlvl) {
        u.mtimedone = Math.trunc(u.mtimedone * u.ulevel / mlvl);
    }
    if (uskin_get() && mntmp !== armor_to_dragon(uskin_get().otyp))
        skinback(false);
    await break_armor_fn();
    await drop_weapon_fn(1);
    find_ac();
    if (was_hiding_under)
        hideunder(game.player);
    if (u.utrap && u.utraptype === TT_PIT) {
        set_utrap(rn1(6, 2), TT_PIT);
    }
    if (was_blind && !Blind()) {
        // C ref: polyself.c polymon() — set_itimeout(&HBlinded, 1L)
        set_itimeout(BLINDED, 1);
        await make_blinded(0, true);
    }
    newsym(u.ux, u.uy);
    if (lays_eggs(game.player.data)) {
        learn_egg_type(u.umonnum);
        learn_egg_type(egg_type_from_parent(u.umonnum, true));
    }
    if (u.uswallow) {
        const usiz = game.player.data?.msize || 0;
        if (unsolid(game.player.data)
            || usiz >= MZ_HUGE
            || (u.ustuck?.data?.msize < usiz && !is_whirly(u.ustuck?.data))) {
            let expels_mesg = true;
            if (unsolid(game.player.data)) {
                if (canspotmon(u.ustuck))
                    ; // ustuckNam update
                await pline(`${ustuckNam} can no longer contain you.`);
                expels_mesg = false;
            }
            await expels(u.ustuck, u.ustuck?.data, expels_mesg);
            was_expelled = true;
        }
    } else if (u.ustuck && !sticking
        && (sticks(game.player.data) || unsolid(game.player.data))) {
        set_ustuck(null);
        await pline(`${ustuckNam} loses its grip on you.`);
    } else if (sticking && !sticks(game.player.data)) {
        await uunstick();
    }
    if (u.usteed) {
        if (touch_petrifies(u.usteed?.data) && !Stone_resistance() && rn2(3) /* rnl(3) */) {
            await pline(`${no_longer_petrify_resistant} touch ${mon_nam(u.usteed)}.`);
            await instapetrify(`riding ${an(pmname(u.usteed.data, Ugender()))}`);
        }
        if (!can_ride(u.usteed))
            await dismount_steed(DISMOUNT_POLY);
    }
    find_ac();
    if (((!Levitation() && !u.ustuck && !Flying() && is_pool_or_lava(u.ux, u.uy))
        || (Underwater() && !is_swimmer(game.player.data)))
        && !was_expelled) {
        /* spoteffects(true) */;
    }
    if (Passes_walls() && u.utrap
        && (u.utraptype === TT_INFLOOR || u.utraptype === TT_BURIEDBALL)) {
        if (u.utraptype === TT_INFLOOR) {
            await pline_The('rock seems to no longer trap you.');
        } else {
            await pline_The('buried ball is no longer bound to you.');
            await buried_ball_to_freedom();
        }
        await reset_utrap(true);
    } else if (likes_lava(game.player.data) && u.utrap
        && u.utraptype === TT_LAVA) {
        await pline_The(`${hliquid('lava')} now feels soothing.`);
        await reset_utrap(true);
    }
    if (amorphous(game.player.data) || is_whirly(game.player.data)
        || unsolid(game.player.data)) {
        if (Punished()) {
            await You('slip out of the iron chain.');
            unpunish();
        } else if (u.utrap && u.utraptype === TT_BURIEDBALL) {
            await You('slip free of the buried ball and chain.');
            await buried_ball_to_freedom();
        }
    }
    if (u.utrap && (u.utraptype === TT_WEB || u.utraptype === TT_BEARTRAP)
        && (amorphous(game.player.data) || is_whirly(game.player.data)
            || unsolid(game.player.data)
            || (game.player.data?.msize <= MZ_SMALL
                && u.utraptype === TT_BEARTRAP))) {
        await You(`are no longer stuck in the ${u.utraptype === TT_WEB ? 'web' : 'bear trap'}.`);
        await reset_utrap(true);
    }
    if (webmaker(game.player.data) && u.utrap && u.utraptype === TT_WEB) {
        await You('orient yourself on the web.');
        await reset_utrap(true);
    }
    check_strangling(true);
    setBotl('polymon');
    game.vision_full_recalc = 1;
    see_monsters();
    await encumber_msg();
    await retouch_equipment(2);
    if (!uarmg())
        await selftouch(no_longer_petrify_resistant);
    if (game.flags.verbose) {
        const use_thec = 'Use the command #%s to %s.';
        const monsterc = 'monster';
        const uptr = game.player.data;
        const might_hide = is_hider(uptr) || hides_under(uptr);
        if (can_breathe(uptr))
            await pline(use_thec.replace('%s', monsterc).replace('%s', 'use your breath weapon'));
        if (attacktype(uptr, AT_SPIT))
            await pline(use_thec.replace('%s', monsterc).replace('%s', 'spit venom'));
        if (uptr?.mlet === S_NYMPH)
            await pline(use_thec.replace('%s', monsterc).replace('%s', 'remove an iron ball'));
        if (attacktype(uptr, AT_GAZE))
            await pline(use_thec.replace('%s', monsterc).replace('%s', 'gaze at monsters'));
        if (might_hide && webmaker(uptr))
            await pline(use_thec.replace('%s', monsterc).replace('%s', 'hide or to spin a web'));
        else if (might_hide)
            await pline(use_thec.replace('%s', monsterc).replace('%s', 'hide'));
        else if (webmaker(uptr))
            await pline(use_thec.replace('%s', monsterc).replace('%s', 'spin a web'));
        if (is_were(uptr))
            await pline(use_thec.replace('%s', monsterc).replace('%s', 'summon help'));
        if (u.umonnum === PM_GREMLIN)
            await pline(use_thec.replace('%s', monsterc).replace('%s', 'multiply in a fountain'));
        if (is_unicorn(uptr))
            await pline(use_thec.replace('%s', monsterc).replace('%s', 'use your horn'));
        if (is_mind_flayer(uptr))
            await pline(use_thec.replace('%s', monsterc).replace('%s', 'emit a mental blast'));
        if (uptr?.msound === MS_SHRIEK)
            await pline(use_thec.replace('%s', monsterc).replace('%s', 'shriek'));
        if (is_vampire(uptr) || is_vampshifter(game.player))
            await pline(use_thec.replace('%s', monsterc).replace('%s', 'change shape'));
        if (lays_eggs(uptr) && game.flags.female
            && !(uptr === mons[PM_GIANT_EEL]
                || uptr === mons[PM_ELECTRIC_EEL]))
            await pline(use_thec.replace('%s', 'sit').replace('%s',
                eggs_in_water(uptr) ? 'spawn in the water' : 'lay an egg'));
    }
    return 1;
}
/* determine hero's temporary strength value used while polymorphed */
export function uasmon_maxStr() {
    const u = game.u;
    let mndx = u.umonnum;
    const ptr = mons[mndx];
    if (is_orc(ptr)) {
        if (mndx !== PM_URUK_HAI && mndx !== PM_ORC_CAPTAIN)
            mndx = PM_ORC;
    } else if (is_elf(ptr)) {
        mndx = PM_ELF;
    } else if (is_dwarf(ptr)) {
        mndx = PM_DWARF;
    } else if (is_gnome(ptr)) {
        mndx = PM_GNOME;
    }
    const R = character_race(mndx);
    if (strongmonst(ptr)) {
        const live_H = is_giant(ptr) && !is_undead(ptr);
        return R ? (R.attrmax?.[A_STR] ?? STR18(100)) : live_H ? STR19(19) : STR18(100);
    } else {
        return R ? (R.attrmax?.[A_STR] ?? 18) : 18;
    }
}
/* break_armor -- break/drop armor on polymorph */
async function break_armor_fn() {
    const uptr = game.player.data;
    let otmp;
    if (breakarm(uptr)) {
        if ((otmp = uarm()) !== null && otmp !== undefined) {
            if (donning(otmp)) cancel_don();
            if (otmp.lamplit) end_burn(otmp, false);
            await You('break out of your armor!');
            exercise(A_STR, false);
            await Armor_gone();
            useup(otmp);
        }
        if ((otmp = uarmc()) !== null && otmp !== undefined
            && (otmp.otyp !== MUMMY_WRAPPING || !WrappingAllowed(uptr))) {
            await pline_The(`clasp on your ${cloak_simple_name(otmp)} breaks open!`);
            await Cloak_off();
            await dropp(otmp);
        }
        if (uarmu()) {
            await Your('shirt rips to shreds!');
            useup(uarmu());
        }
    } else if (sliparm(uptr)) {
        if ((otmp = uarm()) !== null && otmp !== undefined && racial_exception(game.player, otmp) < 1) {
            if (donning(otmp)) cancel_don();
            await Your('armor falls around you!');
            await Armor_gone();
            await dropp(otmp);
        }
        if ((otmp = uarmc()) !== null && otmp !== undefined
            && (otmp.otyp !== MUMMY_WRAPPING || !WrappingAllowed(uptr))) {
            if (is_whirly(uptr))
                await Your(`${cloak_simple_name(otmp)} falls, unsupported!`);
            else
                await You(`shrink out of your ${cloak_simple_name(otmp)}!`);
            await Cloak_off();
            await dropp(otmp);
        }
        if ((otmp = uarmu()) !== null && otmp !== undefined) {
            if (is_whirly(uptr))
                await You('seep right through your shirt!');
            else
                await You('become much too small for your shirt!');
            setworn(null, otmp.owornmask & W_ARMU);
            await dropp(otmp);
        }
    }
    if (has_horns(uptr)) {
        if ((otmp = game.u.uarmh) !== null && otmp !== undefined) {
            if (is_flimsy(otmp) && !donning(otmp)) {
                const hornbuf = `horn${plur(num_horns(uptr))}`;
                await Your(`${hornbuf} ${vtense(hornbuf, 'pierce')} ${yname(otmp)}.`);
            } else {
                if (donning(otmp)) cancel_don();
                await Your(`${helm_simple_name(otmp)} falls to the ${surface(game.u.ux, game.u.uy)}!`);
                Helmet_off();
                await dropp(otmp);
            }
        }
    }
    if (nohands(uptr) || verysmall(uptr)) {
        if ((otmp = uarmg()) !== null && otmp !== undefined) {
            if (donning(otmp)) cancel_don();
            await You(`drop your gloves${uwep() ? ' and weapon' : ''}!`);
            await drop_weapon_fn(0);
            await Gloves_off();
            await dropp(otmp);
        }
        if ((otmp = uarms()) !== null && otmp !== undefined) {
            await You('can no longer hold your shield!');
            Shield_off();
            await dropp(otmp);
        }
        if ((otmp = game.u.uarmh) !== null && otmp !== undefined) {
            if (donning(otmp)) cancel_don();
            await Your(`${helm_simple_name(otmp)} falls to the ${surface(game.u.ux, game.u.uy)}!`);
            Helmet_off();
            await dropp(otmp);
        }
    }
    if (nohands(uptr) || verysmall(uptr)
        || slithy(uptr) || uptr?.mlet === S_CENTAUR) {
        if ((otmp = uarmf()) !== null && otmp !== undefined) {
            if (donning(otmp)) cancel_don();
            if (is_whirly(uptr))
                await Your('boots fall away!');
            else
                await Your(`boots ${verysmall(uptr) ? 'slide' : 'are pushed'} off your feet!`);
            await Boots_off();
            await dropp(otmp);
        }
    }
    if ((otmp = ublindf()) !== null && otmp !== undefined && !has_head_fn(uptr)) {
        const eyewear = simpleonames(otmp);
        await Your(`${eyewear} ${vtense(eyewear, 'fall')} off!`);
        await Blindf_off(null);
        await dropp(otmp);
    }
}
function has_head_fn(ptr) { return !ptr || (ptr.mflags1 & M1_NOHEAD) === 0; }
/* drop_weapon -- drop weapon on polymorph */
async function drop_weapon_fn(alone) {
    const u = game.u;
    let otmp;
    let updateinv = true;
    if (uwep()) {
        if (!alone || cantwield(game.player.data)) {
            const candropwep = await canletgo(uwep(), '');
            const candropswapwep = !u.twoweap || await canletgo(uswapwep(), '');
            if (alone) {
                let what = (candropwep && candropswapwep) ? 'drop' : 'release';
                let which = is_sword(uwep()) ? 'sword' : weapon_descr(uwep());
                if (u.twoweap) {
                    const whichtoo = is_sword(uswapwep()) ? 'sword' : weapon_descr(uswapwep());
                    if (which !== whichtoo) which = 'weapon';
                }
                if ((uwep().quan || 1) !== 1 || u.twoweap)
                    which = makeplural(which);
                await You(`find you must ${what} your ${which}!`);
            }
            if (u.twoweap) {
                otmp = uswapwep();
                uswapwepgone();
                if (otmp?.in_use) updateinv = false;
                else if (candropswapwep) await dropx(otmp);
            }
            otmp = uwep();
            await uwepgone();
            if (otmp?.in_use) updateinv = false;
            else if (candropwep) await dropx(otmp);
            if (updateinv) update_inventory();
        } else if (!could_twoweap(game.player.data)) {
            await untwoweapon();
        }
    }
}
/* rehumanize -- return to original form */
export async function rehumanize() {
    const u = game.u;
    const was_flying = !!Flying();
    if (Unchanging_prop()) {
        if (u.mh < 1) {
            game.killer = { format: NO_KILLER_PREFIX, name: 'killed while stuck in creature form' };
            await done(DIED);
            return;
        } else if (uamul() && uamul().otyp === AMULET_OF_UNCHANGING) {
            // Your("%s %s!", ...) — would need await
        }
    }
    if (emits_light(game.player.data))
        del_light_source(0, monst_to_any(game.player));
    await polyman('You return to %s form!', game.urace?.adj || 'human');
    if (u.uhp < 1) {
        await Your('old form was not healthy enough to survive.');
        game.killer = { format: KILLED_BY, name: `reverting to unhealthy ${game.urace?.adj || 'human'} form` };
        await done(DIED);
    }
    nomul(0);
    game.disp = game.disp || {};
    setBotl('rehumanize');
    game.vision_full_recalc = 1;
    await encumber_msg();
    if (was_flying && !Flying() && u.usteed)
        await You(`and ${mon_nam(u.usteed)} return gently to the ${surface(u.ux, u.uy)}.`);
    await retouch_equipment(2);
    if (!uarmg())
        await selftouch(no_longer_petrify_resistant);
}
// C ref: cmd.c:890 domonability — dispatch #monster to the right ability
export async function domonability() {
    const uptr = game.youmonst?.data || game.player?.data;
    if (!uptr) return ECMD_OK;
    const might_hide = is_hider(uptr) || hides_under(uptr);
    // C: if might_hide && webmaker → yn_function prompt; simplified for now
    if (can_breathe(uptr))
        return await dobreathe();
    else if (attacktype(uptr, AT_SPIT))
        return await dospit();
    else if (uptr.mlet === S_NYMPH)
        return await doremove();
    else if (attacktype(uptr, AT_GAZE))
        return await dogaze();
    else if (is_were(uptr))
        return await dosummon();
    else if (might_hide)
        return await dohide();
    else if (webmaker(uptr))
        return await dospinweb();
    else if (is_mind_flayer(uptr))
        return await domindblast();
    else if (is_unicorn(uptr)) {
        // C: use_unicorn_horn(NULL) — TODO: port
        await pline("You don't feel very effective.");
        return ECMD_TIME;
    } else if (uptr.msound === MS_SHRIEK) {
        await You('shriek.');
        if (game.u.uburied)
            await pline('Unfortunately sound does not carry well through rock.');
        else
            aggravate();
    } else if (is_vampire(uptr) || is_vampshifter(game.player)) {
        return await dopoly();
    } else if (game.u.usteed && can_breathe(game.u.usteed.data)) {
        pet_ranged_attk(game.u.usteed);
        return ECMD_TIME;
    } else if (Upolyd()) {
        await pline('Any special ability you may have is purely reflexive.');
    } else {
        await You("don't have a special ability in your normal form!");
        return ECMD_OK;
    }
    return ECMD_OK;
}
/* dobreathe -- #monster breath weapon */
export async function dobreathe() {
    if (Strangled_prop()) {
        await You_cant("breathe.  Sorry.");
        return ECMD_OK;
    }
    if (game.u.uen < 15) {
        await You("don't have enough energy to breathe!");
        return ECMD_OK;
    }
    game.u.uen -= 15;
    setBotl('dobreathe');
    if (!await getdir(null))
        return ECMD_CANCEL;
    const mattk = attacktype_fordmg(game.youmonst?.data || game.player?.data, AT_BREA, AD_ANY);
    const _data = game.youmonst?.data || game.player?.data;
    if (!mattk)
        impossible('bad breath attack?');
    else if (!game.u.dx && !game.u.dy && !game.u.dz)
        ubreatheu(mattk);
    else
        await ubuzz(20 + BZ_OFS_AD(mattk.adtyp), mattk.damn || 0); // BZ_U_BREATH
    return ECMD_TIME;
}
/* dospit -- #monster spit venom */
export async function dospit() {
    if (!await getdir(null))
        return ECMD_CANCEL;
    const mattk = attacktype_fordmg(game.player.data, AT_SPIT, AD_ANY);
    if (!mattk) {
        impossible('bad spit attack?');
    } else {
        let otmp;
        switch (mattk.adtyp) {
        case AD_BLND:
        case AD_DRST:
            otmp = mksobj(BLINDING_VENOM, true, false);
            break;
        default:
            /* fall through */
        case AD_ACID:
            otmp = mksobj(ACID_VENOM, true, false);
            break;
        }
        otmp.spe = 1;
        await throwit(otmp, 0, false, null);
    }
    return ECMD_TIME;
}
/* doremove -- #monster remove ball and chain (nymph) */
export async function doremove() {
    if (!Punished()) {
        if (game.u.utrap && game.u.utraptype === TT_BURIEDBALL) {
            await pline_The(`ball and chain are buried firmly in the ${surface(game.u.ux, game.u.uy)}.`);
            return ECMD_OK;
        }
        await You('are not chained to anything!');
        return ECMD_OK;
    }
    unpunish();
    return ECMD_TIME;
}
/* dospinweb -- #monster spin a web */
export async function dospinweb() {
    const x = game.u.ux, y = game.u.uy;
    const ttmp = t_at(x, y);
    const lev = levl(x, y);
    const reject_terrain = is_pool_or_lava(x, y) || IS_AIR(lev?.typ);
    if (Levitation() || reject_terrain) {
        await You(`must be on ${reject_terrain ? 'solid' : 'the'} ground to spin a web.`);
        return ECMD_OK;
    }
    if (game.u.uswallow) {
        await You(`release web fluid inside ${mon_nam(game.u.ustuck)}.`);
        if (is_animal(game.u.ustuck?.data)) {
            await expels(game.u.ustuck, game.u.ustuck.data, true);
            return ECMD_OK;
        }
        if (is_whirly(game.u.ustuck?.data)) {
            /* simplified sweep logic */
            await pline_The('web is swept away!');
            return ECMD_OK;
        }
        await pline_The(`web dissolves into ${mon_nam(game.u.ustuck)}.`);
        return ECMD_OK;
    }
    if (game.u.utrap) {
        await You('cannot spin webs while stuck in a trap.');
        return ECMD_OK;
    }
    exercise(A_DEX, true);
    if (ttmp) {
        switch (ttmp.ttyp) {
        case PIT: case SPIKED_PIT:
            await You('spin a web, covering up the pit.');
            deltrap(ttmp); await bury_objs(x, y); newsym(x, y);
            return ECMD_TIME;
        case SQKY_BOARD:
            await pline_The('squeaky board is muffled.');
            deltrap(ttmp); newsym(x, y);
            return ECMD_TIME;
        case TELEP_TRAP: case LEVEL_TELEP: case MAGIC_PORTAL: case VIBRATING_SQUARE:
            await Your('webbing vanishes!');
            return ECMD_OK;
        case WEB:
            await You('make the web thicker.');
            return ECMD_TIME;
        case HOLE: case TRAPDOOR:
            await You(`web over the ${ttmp.ttyp === TRAPDOOR ? 'trap door' : 'hole'}.`);
            deltrap(ttmp); newsym(x, y);
            return ECMD_TIME;
        case ROLLING_BOULDER_TRAP:
            await You('spin a web, jamming the trigger.');
            deltrap(ttmp); newsym(x, y);
            return ECMD_TIME;
        case ARROW_TRAP: case DART_TRAP: case BEAR_TRAP: case ROCKTRAP:
        case FIRE_TRAP: case LANDMINE: case SLP_GAS_TRAP: case RUST_TRAP:
        case MAGIC_TRAP: case ANTI_MAGIC: case POLY_TRAP:
            await You('have triggered a trap!');
            await dotrap(ttmp, NO_TRAP_FLAGS);
            return ECMD_TIME;
        default:
            impossible(`Webbing over trap type ${ttmp.ttyp}?`);
            return ECMD_OK;
        }
    } else if (On_stairs(x, y)) {
        const ltyp = levl(x, y)?.typ;
        await Your(`web fails to impede access to the ${ltyp === STAIRS ? 'stairs' : 'ladder'}.`);
        return ECMD_TIME;
    }
    const newTrap = await maketrap(x, y, WEB);
    if (newTrap) {
        await You('spin a web.');
        newTrap.madeby_u = 1;
        feeltrap(newTrap);
        if (in_rooms(x, y, SHOPBASE).length > 0)
            add_damage(x, y, SHOP_WEB_COST);
    }
    return ECMD_TIME;
}
/* dosummon -- #monster summon help (were-creature) */
export async function dosummon() {
    if (game.u.uen < 10) {
        await You('lack the energy to send forth a call for help!');
        return ECMD_OK;
    }
    game.u.uen -= 10;
    setBotl('dosummon');
    await You('call upon your brethren for help!');
    exercise(A_WIS, true);
    let placeholder = 0;
    if (!await were_summon(game.player.data, true, placeholder, null))
        await pline('But none arrive.');
    return ECMD_TIME;
}
/* dogaze -- #monster gaze attack */
export async function dogaze() {
    let looked = 0;
    let adtyp = 0;
    for (let i = 0; i < NATTK; i++) {
        if (game.player.data?.mattk?.[i]?.aatyp === AT_GAZE) {
            adtyp = game.player.data.mattk[i].adtyp;
            break;
        }
    }
    if (adtyp !== AD_CONF && adtyp !== AD_FIRE) {
        impossible(`gaze attack ${adtyp}?`);
        return ECMD_OK;
    }
    if (Blind()) {
        await You_cant('see anything to gaze at.');
        return ECMD_OK;
    } else if (Hallucination()) {
        await You_cant('gaze at anything you can see.');
        return ECMD_OK;
    }
    if (game.u.uen < 15) {
        await You('lack the energy to use your special gaze!');
        return ECMD_OK;
    }
    game.u.uen -= 15;
    setBotl('dogaze');
    let mtmp = game.fmon;
    while (mtmp) {
        if (!DEADMONSTER(mtmp) && canseemon(mtmp) && couldsee(mtmp.mx, mtmp.my)) {
            looked++;
            if (Invis() && !perceives(mtmp.data)) {
                await pline(`${Monnam(mtmp)} seems not to notice your gaze.`);
            } else if (mtmp.minvis && !See_invisible()) {
                await You_cant(`see where to gaze at ${Monnam(mtmp)}.`);
            } else if (M_AP_TYPE(mtmp) === M_AP_FURNITURE
                || M_AP_TYPE(mtmp) === M_AP_OBJECT) {
                looked--;
            } else if (game.flags.safe_dog && mtmp.mtame && !Confusion()) {
                await You(`avoid gazing at ${y_monnam(mtmp)}.`);
            } else {
                /* simplified: skip confirmation */
                await setmangry(mtmp, true);
                if (helpless(mtmp) || mtmp.mstun
                    || !mtmp.mcansee || !haseyes(mtmp.data)) {
                    looked--;
                    mtmp = mtmp.nmon;
                    continue;
                }
                if (adtyp === AD_CONF) {
                    if (!mtmp.mconf)
                        await Your(`gaze confuses ${mon_nam(mtmp)}!`);
                    else
                        await pline(`${Monnam(mtmp)} is getting more and more confused.`);
                    mtmp.mconf = 1;
                } else if (adtyp === AD_FIRE) {
                    const orig_dmg = c_d(2, 6);
                    let dmg = orig_dmg;
                    const lev = game.u.ulevel;
                    await You(`attack ${mon_nam(mtmp)} with a fiery gaze!`);
                    if (resists_fire(mtmp)) {
                        await pline_The(`fire doesn't burn ${mon_nam(mtmp)}!`);
                        dmg = 0;
                    }
                    if (lev > rn2(20)) {
                        dmg += await destroy_items(mtmp, AD_FIRE, orig_dmg);
                        ignite_items(mtmp.minvent);
                    }
                    if (dmg) mtmp.mhp -= dmg;
                    if (DEADMONSTER(mtmp)) await killed(mtmp);
                }
                if (DEADMONSTER(mtmp)) {
                    mtmp = mtmp.nmon;
                    continue;
                }
                if (mtmp.data === mons[PM_FLOATING_EYE] && !mtmp.mcan) {
                    if (!Free_action()) {
                        await You(`are frozen by ${s_suffix(mon_nam(mtmp))} gaze!`);
                        const nomulval = (game.u.ulevel > 6 || rn2(4))
                            ? -c_d((mtmp.m_lev || 0) + 1, mtmp.data?.mattk?.[0]?.damd || 1)
                            : -200;
                        nomul(nomulval);
                        game.multi_reason = "frozen by a monster's gaze";
                        game.nomovemsg = null;
                        return ECMD_TIME;
                    } else {
                        await You(`stiffen momentarily under ${s_suffix(mon_nam(mtmp))} gaze.`);
                    }
                }
                if (mtmp.data === mons[PM_MEDUSA] && !mtmp.mcan) {
                    await pline(`Gazing at the awake ${l_monnam(mtmp)} is not a very good idea.`);
                    await pline('You turn to stone...');
                    game.killer = { format: KILLED_BY, name: "deliberately meeting Medusa's gaze" };
                    await done(STONING);
                }
            }
        }
        mtmp = mtmp.nmon;
    }
    if (!looked)
        await You('gaze at no place in particular.');
    return ECMD_TIME;
}
/* dohide -- #monster hide */
export async function dohide() {
    const ismimic = game.player.data?.mlet === S_MIMIC;
    const on_ceiling = is_clinger(game.player.data) || !!Flying();
    if (game.u.ustuck || (game.u.utrap && (game.u.utraptype !== TT_PIT || on_ceiling))) {
        let reason;
        if (!game.u.ustuck) reason = 'trapped';
        else if (game.u.uswallow) reason = 'swallowed';
        else if (!sticks(game.player.data)) reason = 'being held';
        else reason = 'holding someone';
        await You_cant(`hide while you're ${reason}.`);
        if (game.u.uundetected || (ismimic && U_AP_TYPE() !== M_AP_NOTHING)) {
            game.u.uundetected = 0;
            game.player.m_ap_type = M_AP_NOTHING;
            newsym(game.u.ux, game.u.uy);
        }
        return ECMD_OK;
    }
    if (game.player.data?.mlet === S_EEL && !is_pool(game.u.ux, game.u.uy)) {
        if (IS_FOUNTAIN(levl(game.u.ux, game.u.uy)?.typ))
            await pline_The('fountain is not deep enough to hide in.');
        else
            await pline(`There is no ${hliquid('water')} to hide in here.`);
        game.u.uundetected = 0;
        return ECMD_OK;
    }
    if (hides_under(game.player.data)) {
        /* simplified: check for objects to hide under */
        const otop = null; // level.objects[x][y] — TODO
        if (!otop) {
            await pline('There is nothing to hide under here.');
            game.u.uundetected = 0;
            return ECMD_OK;
        }
    }
    if (on_ceiling && !has_ceiling(game.u.uz)) {
        await pline('There is nowhere to hide above you.');
        game.u.uundetected = 0;
        return ECMD_OK;
    }
    if ((is_hider(game.player.data) && !Flying())
        && (Is_airlevel(game.u.uz) || Is_waterlevel(game.u.uz))) {
        await pline('There is nowhere to hide beneath you.');
        game.u.uundetected = 0;
        return ECMD_OK;
    }
    if (game.u.uundetected || (ismimic && U_AP_TYPE() !== M_AP_NOTHING)) {
        await youhiding(false, 1);
        return ECMD_OK;
    }
    if (ismimic) {
        game.player.m_ap_type = M_AP_OBJECT;
        game.player.mappearance = STRANGE_OBJECT;
    } else {
        game.u.uundetected = 1;
    }
    newsym(game.u.ux, game.u.uy);
    await youhiding(false, 0);
    return ECMD_TIME;
}
/* dopoly -- #monster vampire shape change */
export async function dopoly() {
    const savedat = game.player.data;
    if (is_vampire(game.player.data) || is_vampshifter(game.player)) {
        await polyself(POLY_MONSTER);
        if (savedat !== game.player.data) {
            await You(`transform into ${an(pmname(game.player.data, Ugender()))}.`);
            newsym(game.u.ux, game.u.uy);
        }
    }
    return ECMD_TIME;
}
/* domindblast -- #monster mind flayer psychic blast */
export async function domindblast() {
    if (game.u.uen < 10) {
        await You('concentrate but lack the energy to maintain doing so.');
        return ECMD_OK;
    }
    game.u.uen -= 10;
    setBotl('domindblast');
    await You('concentrate.');
    await pline('A wave of psychic energy pours out.');
    let mtmp = game.fmon;
    while (mtmp) {
        const nmon = mtmp.nmon;
        if (!DEADMONSTER(mtmp)
            && mdistu(mtmp) <= BOLT_LIM * BOLT_LIM
            && !mtmp.mpeaceful
            && !mindless(mtmp.data)) {
            const u_sen = telepathic(mtmp.data) && !mtmp.mcansee;
            if (u_sen || (telepathic(mtmp.data) && rn2(2)) || !rn2(10)) {
                const dmg = rnd(15);
                wakeup(mtmp, (dmg > mtmp.mhp) ? true : false);
                await You(`lock in on ${s_suffix(mon_nam(mtmp))} ${u_sen ? 'telepathy' : telepathic(mtmp.data) ? 'latent telepathy' : 'mind'}.`);
                mtmp.mhp -= dmg;
                if (DEADMONSTER(mtmp))
                    await killed(mtmp);
            }
        }
        mtmp = nmon;
    }
    return ECMD_TIME;
}
/* uunstick -- release a monster from hero's grasp */
export async function uunstick() {
    const mtmp = game.u.ustuck;
    if (!mtmp) {
        impossible('uunstick: no ustuck?');
        return;
    }
    set_ustuck(null);
    await pline(`${Monnam(mtmp)} is no longer in your clutches.`);
}
/* skinback -- revert dragon skin to armor */
export function skinback(silently) {
    const sk = uskin_get();
    if (sk) {
        if (!silently) {
            // Your("skin returns to its original form.") — sync context
        }
        game.u.uarm = sk;
        uskin_set(null);
        game.u.uarm.owornmask &= ~I_SPECIAL;
    }
}
/* mbodypart -- body part name for a monster */
export function mbodypart(mon, part) {
    const humanoid_parts = ['arm', 'eye', 'face', 'finger', 'fingertip', 'foot',
        'hand', 'handed', 'head', 'leg', 'light headed', 'neck', 'spine', 'toe',
        'hair', 'blood', 'lung', 'nose', 'stomach'];
    const jelly_parts = ['pseudopod', 'dark spot', 'front', 'pseudopod extension',
        'pseudopod extremity', 'pseudopod root', 'grasp', 'grasped', 'cerebral area',
        'lower pseudopod', 'viscous', 'middle', 'surface', 'pseudopod extremity',
        'ripples', 'juices', 'surface', 'sensor', 'stomach'];
    const animal_parts = ['forelimb', 'eye', 'face', 'foreclaw', 'claw tip',
        'rear claw', 'foreclaw', 'clawed', 'head', 'rear limb', 'light headed',
        'neck', 'spine', 'rear claw tip', 'fur', 'blood', 'lung', 'nose', 'stomach'];
    const bird_parts = ['wing', 'eye', 'face', 'wing', 'wing tip', 'foot', 'wing',
        'winged', 'head', 'leg', 'light headed', 'neck', 'spine', 'toe', 'feathers',
        'blood', 'lung', 'bill', 'stomach'];
    const horse_parts = ['foreleg', 'eye', 'face', 'forehoof', 'hoof tip',
        'rear hoof', 'forehoof', 'hooved', 'head', 'rear leg', 'light headed',
        'neck', 'backbone', 'rear hoof tip', 'mane', 'blood', 'lung', 'nose', 'stomach'];
    const sphere_parts = ['appendage', 'optic nerve', 'body', 'tentacle',
        'tentacle tip', 'lower appendage', 'tentacle', 'tentacled', 'body',
        'lower tentacle', 'rotational', 'equator', 'body', 'lower tentacle tip',
        'cilia', 'life force', 'retina', 'olfactory nerve', 'interior'];
    const fungus_parts = ['mycelium', 'visual area', 'front', 'hypha', 'hypha',
        'root', 'strand', 'stranded', 'cap area', 'rhizome', 'sporulated', 'stalk',
        'root', 'rhizome tip', 'spores', 'juices', 'gill', 'gill', 'interior'];
    const vortex_parts = ['region', 'eye', 'front', 'minor current', 'minor current',
        'lower current', 'swirl', 'swirled', 'central core', 'lower current', 'addled',
        'center', 'currents', 'edge', 'currents', 'life force', 'center',
        'leading edge', 'interior'];
    const snake_parts = ['vestigial limb', 'eye', 'face', 'large scale',
        'large scale tip', 'rear region', 'scale gap', 'scale gapped', 'head',
        'rear region', 'light headed', 'neck', 'length', 'rear scale', 'scales',
        'blood', 'lung', 'forked tongue', 'stomach'];
    const worm_parts = ['anterior segment', 'light sensitive cell', 'clitellum',
        'setae', 'setae', 'posterior segment', 'segment', 'segmented',
        'anterior segment', 'posterior', 'over stretched', 'clitellum', 'length',
        'posterior setae', 'setae', 'blood', 'skin', 'prostomium', 'stomach'];
    const spider_parts = ['pedipalp', 'eye', 'face', 'pedipalp', 'tarsus', 'claw',
        'pedipalp', 'palped', 'cephalothorax', 'leg', 'spun out', 'cephalothorax',
        'abdomen', 'claw', 'hair', 'hemolymph', 'book lung', 'labrum',
        'digestive tract'];
    const fish_parts = ['fin', 'eye', 'premaxillary', 'pelvic axillary',
        'pelvic fin', 'anal fin', 'pectoral fin', 'finned', 'head', 'peduncle',
        'played out', 'gills', 'dorsal fin', 'caudal fin', 'scales', 'blood',
        'gill', 'nostril', 'stomach'];
    const not_claws = [S_HUMAN, S_MUMMY, S_ZOMBIE, S_ANGEL, S_NYMPH,
        S_LEPRECHAUN, S_QUANTMECH, S_VAMPIRE, S_ORC, S_GIANT];
    const mptr = mon?.data;
    if (!mptr) return humanoid_parts[part] || 'body part';
    if (part <= NO_PART) {
        impossible(`mbodypart: bad part ${part}`);
        return 'mystery part';
    }
    // special cases
    if (mptr.mlet === S_DOG || mptr.mlet === S_FELINE
        || mptr.mlet === S_RODENT || mptr === mons[PM_OWLBEAR]) {
        switch (part) {
        case HAND: return 'paw';
        case HANDED: return 'pawed';
        case FOOT: return 'rear paw';
        case ARM: case LEG: return horse_parts[part];
        default: break;
        }
    } else if (mptr.mlet === S_YETI) {
        return humanoid_parts[part];
    }
    if ((part === HAND || part === HANDED)
        && (humanoid(mptr) && attacktype(mptr, AT_CLAW)
            && !not_claws.includes(mptr.mlet)
            && mptr !== mons[PM_STONE_GOLEM]
            && mptr !== mons[PM_AMOROUS_DEMON]))
        return (part === HAND) ? 'claw' : 'clawed';
    if ((mptr === mons[PM_MUMAK] || mptr === mons[PM_MASTODON])
        && part === NOSE)
        return 'trunk';
    if (mptr === mons[PM_SHARK] && part === HAIR)
        return 'skin';
    if ((mptr === mons[PM_JELLYFISH] || mptr === mons[PM_KRAKEN])
        && (part === ARM || part === FINGER || part === HAND || part === FOOT
            || part === TOE))
        return 'tentacle';
    if (mptr === mons[PM_FLOATING_EYE] && part === EYE)
        return 'cornea';
    if (humanoid(mptr) && (part === ARM || part === FINGER || part === FINGERTIP
        || part === HAND || part === HANDED))
        return humanoid_parts[part];
    if (mptr.mlet === S_COCKATRICE)
        return (part === HAIR) ? snake_parts[part] : bird_parts[part];
    if (mptr === mons[PM_RAVEN])
        return bird_parts[part];
    if (mptr.mlet === S_CENTAUR || mptr.mlet === S_UNICORN
        || mptr === mons[PM_KI_RIN]
        || (mptr === mons[PM_ROTHE] && part !== HAIR))
        return horse_parts[part];
    if (mptr.mlet === S_LIGHT) {
        if (part === HANDED) return 'rayed';
        else if (part === ARM || part === FINGER || part === FINGERTIP
            || part === HAND) return 'ray';
        else return 'beam';
    }
    if (mptr === mons[PM_STALKER] && part === HEAD)
        return 'head';
    if (mptr.mlet === S_EEL && mptr !== mons[PM_JELLYFISH])
        return fish_parts[part];
    if (mptr.mlet === S_WORM)
        return worm_parts[part];
    if (mptr.mlet === S_SPIDER)
        return spider_parts[part];
    if (slithy(mptr) || (mptr.mlet === S_DRAGON && part === HAIR))
        return snake_parts[part];
    if (mptr.mlet === S_EYE)
        return sphere_parts[part];
    if (mptr.mlet === S_JELLY || mptr.mlet === S_PUDDING
        || mptr.mlet === S_BLOB || mptr === mons[PM_JELLYFISH])
        return jelly_parts[part];
    if (mptr.mlet === S_VORTEX || mptr.mlet === S_ELEMENTAL)
        return vortex_parts[part];
    if (mptr.mlet === S_FUNGUS)
        return fungus_parts[part];
    if (humanoid(mptr))
        return humanoid_parts[part];
    return animal_parts[part];
}
/* body_part -- body part name for the hero */
export function body_part(part) {
    return mbodypart(game.player, part);
}
/* poly_gender -- gender of polymorphed player */
export function poly_gender() {
    if (is_neuter(game.player.data) || !humanoid(game.player.data))
        return 2;
    return game.flags.female ? 1 : 0;
}
/* ugolemeffects -- golem healing from damage types */
export async function ugolemeffects(damtype, dam) {
    let heal = 0;
    if (game.u.umonnum !== PM_FLESH_GOLEM && game.u.umonnum !== PM_IRON_GOLEM)
        return;
    switch (damtype) {
    case AD_ELEC:
        if (game.u.umonnum === PM_FLESH_GOLEM)
            heal = Math.trunc((dam + 5) / 6);
        break;
    case AD_FIRE:
        if (game.u.umonnum === PM_IRON_GOLEM)
            heal = dam;
        break;
    }
    if (heal && game.u.mh < game.u.mhmax) {
        game.u.mh += heal;
        if (game.u.mh > game.u.mhmax)
            game.u.mh = game.u.mhmax;
        setBotl('ugolemeffects');
        await pline('Strangely, you feel better than before.');
        exercise(A_STR, true);
    }
}
/* armor_to_dragon -- map dragon armor to dragon monster */
function armor_to_dragon(atyp) {
    switch (atyp) {
    case GRAY_DRAGON_SCALE_MAIL: case GRAY_DRAGON_SCALES: return PM_GRAY_DRAGON;
    case SILVER_DRAGON_SCALE_MAIL: case SILVER_DRAGON_SCALES: return PM_SILVER_DRAGON;
    case GOLD_DRAGON_SCALE_MAIL: case GOLD_DRAGON_SCALES: return PM_GOLD_DRAGON;
    case RED_DRAGON_SCALE_MAIL: case RED_DRAGON_SCALES: return PM_RED_DRAGON;
    case ORANGE_DRAGON_SCALE_MAIL: case ORANGE_DRAGON_SCALES: return PM_ORANGE_DRAGON;
    case WHITE_DRAGON_SCALE_MAIL: case WHITE_DRAGON_SCALES: return PM_WHITE_DRAGON;
    case BLACK_DRAGON_SCALE_MAIL: case BLACK_DRAGON_SCALES: return PM_BLACK_DRAGON;
    case BLUE_DRAGON_SCALE_MAIL: case BLUE_DRAGON_SCALES: return PM_BLUE_DRAGON;
    case GREEN_DRAGON_SCALE_MAIL: case GREEN_DRAGON_SCALES: return PM_GREEN_DRAGON;
    case YELLOW_DRAGON_SCALE_MAIL: case YELLOW_DRAGON_SCALES: return PM_YELLOW_DRAGON;
    default: return NON_PM;
    }
}
/* polysense -- some species have awareness of other species */
function polysense() {
    let warnidx = NON_PM;
    const ctx = game.context = game.context || {};
    ctx.warntype = ctx.warntype || {};
    ctx.warntype.speciesidx = NON_PM;
    ctx.warntype.species = null;
    ctx.warntype.polyd = 0;
    HWarn_of_mon_set(HWarn_of_mon_get() & ~FROMRACE);
    switch (game.u.umonnum) {
    case PM_PURPLE_WORM: case PM_BABY_PURPLE_WORM:
        warnidx = PM_SHRIEKER;
        break;
    case PM_VAMPIRE: case PM_VAMPIRE_LEADER:
        ctx.warntype.polyd = M2_HUMAN | M2_ELF;
        HWarn_of_mon_set(HWarn_of_mon_get() | FROMRACE);
        return;
    }
    if (ismnum(warnidx)) {
        ctx.warntype.speciesidx = warnidx;
        ctx.warntype.species = mons[warnidx];
        HWarn_of_mon_set(HWarn_of_mon_get() | FROMRACE);
    }
}
/* ugenocided -- hero's role or race has been genocided */
export function ugenocided() {
    return !!((game.mvitals?.[game.urole?.mnum]?.mvflags & G_GENOD)
        || (game.mvitals?.[game.urace?.mnum]?.mvflags & G_GENOD));
}
/* udeadinside -- how hero feels after self-genocide */
export function udeadinside() {
    if (!nonliving(game.player.data))
        return 'dead';
    else if (!weirdnonliving(game.player.data))
        return 'condemned';
    else
        return 'empty';
}
/* dopolyself -- wizard mode #polyself command */
export async function dopolyself() {
    await polyself(POLY_CONTROLLED);
}