All files / js potion.js

38.32% Statements 888/2317
42.43% Branches 143/337
46.93% Functions 46/98
38.32% Lines 888/2317

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 231773x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x   73x 73x 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 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 3x 3x 3x 3x 3x 3x 3x 3x 5x 5x 5x 5x 5x         5x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x   73x 73x 73x 73x 73x 73x 4x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x   73x 73x   73x 6x   1x         73x 73x     4x 22x 3x 1x   73x 1x 73x       73x 73x 2x 2x 2x 2x 2x 2x 2x 2x 73x 73x 73x 8x 8x 8x 3x 3x 8x 8x 8x 8x 73x 2x 2x 2x 1x 1x 1x 2x 1x           1x 2x 2x 2x 2x 73x 2x 2x                     2x                     2x 2x           2x 2x 2x 2x 73x                               73x                       73x 4x 4x 4x 4x 4x 4x 4x 73x 73x 73x 14x 14x 14x 14x 14x 14x 14x 14x 14x 3x 3x 3x 3x 3x 3x 14x                         14x 3x 1x 1x 1x 1x 1x 3x 14x                         14x 14x 6x 6x 14x 73x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 73x 1x 1x 1x 1x 1x 1x 1x           1x 1x 1x 1x 1x                     1x 1x                           1x 1x 73x 6x 6x 6x 6x 1x 1x 1x 1x 6x 73x 73x     73x                                   73x                                                                                                     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                           1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x                                                                       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                     1x       1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                                                         1x 1x 1x     1x 1x 1x 1x     1x 1x 1x 1x 1x                                                                                                                                   3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                         1x 1x 1x                     1x   1x 1x 1x 1x 1x                                                   73x 73x 10x 10x 10x     10x     10x 1x 1x 10x 1x 1x 10x     10x 10x     10x 10x 1x 1x 10x 1x 1x 10x     10x 10x       10x 10x       10x     10x 1x 1x 10x     10x 10x     10x     10x     10x 3x 3x 10x 1x 1x 10x     10x 10x     10x     10x 1x 1x 10x     10x     10x   10x 10x 10x 73x 73x 73x 73x 5x 5x       5x 5x 5x 4x 4x 4x 4x 5x 5x 5x 4x 4x 4x 4x 5x 2x 2x 2x 5x 5x 73x 73x                 73x 73x 73x 73x 73x 73x 73x 73x 73x 1x 1x 1x 1x 1x 73x 73x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 73x 73x 2x 2x 2x 2x 2x 2x 2x     2x 2x                                           2x           2x 2x           2x 2x 2x 2x 2x 2x 2x 2x       2x 2x 2x                         2x     2x 2x         2x           2x                       2x                         2x           2x                 2x                   2x 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 1x 73x 73x 10x 10x 10x 10x 10x 10x 10x       10x 5x 3x 3x 3x 2x 2x 5x 10x 10x 10x 73x 73x 323x 323x 323x 306x 323x 228x 323x 73x 73x 22x       22x 22x 22x 22x 22x 5x 5x 5x 5x 5x     22x 3x 3x 2x 2x 2x 1x 1x 22x 15x 18x 10x 10x 10x 15x               10x 10x 15x 22x       15x 10x 10x         10x 22x 73x 73x                                                                                           73x 73x                                                   73x 73x               73x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x                                           1x 1x 1x 1x 1x     1x 1x       1x 1x 1x           1x 1x 1x 1x 1x 73x 73x                     73x 1x 1x 1x 1x 1x 73x 1x 1x 1x 1x                     1x 1x 73x                     73x 132x 132x 132x 126x 132x 125x 132x 125x 132x 31x 31x 31x 31x 31x 73x 2x 2x 2x 2x       2x       2x 2x 2x 1x 1x 1x 1x 1x 1x 1x                                                 1x 1x 1x 1x 1x 1x 1x 1x                       1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x     1x 1x     1x             1x 1x 1x 1x 1x 1x 1x 1x                   2x             2x                                                                                                                                       2x 2x                                                             2x 2x                                                                                                       2x 73x 73x 6x 6x 6x 6x 6x 6x 6x 6x 5x 5x 5x 6x 6x 6x 6x   5x 5x 5x 6x 5x   5x 3x 3x 3x 3x 3x 3x   5x               2x                                               5x 6x 6x 2x 2x 6x 73x 73x                               73x 73x                       73x 73x 72x 72x
// potion.js -- Port of potion.c
// Potion quaffing, effects, throwing, breathing, dipping.
import { game } from './gstate.js';
import { permapoisoned } from './artifact.js';
import { rn2, rnd, rn1, c_d } from './rng.js';
import { pline, You, Your, You_feel, You_see, You_hear, You_cant, pline_The, verbalize } from './pline.js';
import { Acid_resistance, Antimagic, B, Blind, Blind_telepat, BlindedTimeout, Blindfolded, Blindfolded_only, Breathless, Cold_resistance, Confusion, Deaf, Detect_monsters, E, Fire_resistance, Fixed_abil, Flying, Fumbling, H, Half_gas_damage, Halluc_resistance, Hallucination, has_ceiling, HBlinded, HConfusion, HFast, HHallucination, HLevitation, HStun, Infravision, Invis, Invisible, Is_airlevel, Is_waterlevel, Levitation, Maybe_Half_Phys, Poison_resistance, Punished, Role_if, See_invisible, setE, setH, Sick_resistance, Sick_val, Sleep_resistance, Strangled, Stunned, U_AP_TYPE, Unaware, Unchanging, Underwater, Upolyd, Very_fast } from './macros.js';
import { levl, m_at } from './map_access.js';
import { ABASE, ACURR, AMAX, adjattrib, exercise, poisontell } from './attrib.js';
import { canseemon, canspotmon, display_nhwindow, glyph_at, map_invisible, newsym, see_objects, see_traps, set_mimic_blocking, setBotl, tmp_at, unmap_object } from './display.js';
import { vision_recalc } from './vision.js';
import { breathless, dmgtype, eyecount, has_head, haseyes, is_human, is_silent, is_swimmer, is_vampshifter, is_weptool, is_were, likes_fire, likes_lava, mon_hates_blessings, mon_perma_blind, monstseesu, monstunseesu, objdescr_is, resists_acid, resists_poison, stagger } from './mondata.js';
import { AD_ACID, AD_DISE, AD_PEST, M2_DEMON, M2_UNDEAD, mons, PM_DJINNI, PM_FIRE_ELEMENTAL, PM_FIRE_VORTEX, PM_FLAMING_SPHERE, PM_GHOST, PM_GREEN_SLIME, PM_GREMLIN, PM_HEALER, PM_IRON_GOLEM, PM_LICHEN, PM_PESTILENCE, PM_SALAMANDER } from './monsters.js';
import { A_CHA, A_CHAOTIC, A_CON, A_DEX, A_INT, A_LAWFUL, A_MAX, A_NEUTRAL, A_NONE, A_STR, A_WIS, BLINDED, BOLT_LIM, COLNO, CONFUSION, COST_NUTRLZ, COST_UNBLSS, COST_UNCURS, CQ_CANNED, DEAF, DETECT_MONSTERS, ECMD_CANCEL, ECMD_OK, ECMD_TIME, ENL_GAMEINPROGRESS, EYE, FACE, FAST, FLYING, FOOT, FREE_ACTION, FROMOUTSIDE, G_GONE, GETOBJ_ALLOWCNT, GETOBJ_DOWNPLAY, GETOBJ_EXCLUDE, GETOBJ_EXCLUDE_INACCESS, GETOBJ_EXCLUDE_NONINVENT, GETOBJ_EXCLUDE_SELECTABLE, GETOBJ_NOFLAGS, GETOBJ_PROMPT, GETOBJ_SUGGEST, GLIB, HALLUC, HALLUC_RES, HAND, HEAD, I_SPECIAL, INTRINSIC, INVIS, IS_FOUNTAIN, IS_SINK, ismnum, KILLED_BY, KILLED_BY_AN, LEG, LEVITATION, M_AP_MONSTER, M_AP_NOTHING, M_SEEN_SLEEP, MAGICENLIGHTENMENT, MM_NOMSG, NON_PM, nothing_happens, nothing_seems_to_happen, P_BASIC, P_RIDING, POLY_CONTROLLED, POLY_LOW_CTRL, POLY_NOFLAGS, POTHIT_HERO_THROW, POTHIT_OTHER_THROW, ROWNO, SEE_INVIS, SICK, SICK_ALL, SICK_NONVOMITABLE, SICK_VOMITABLE, SINK, SLIMED, STOMACH, STONED, STRANGLED, STUNNED, TELEPAT, TIMEOUT, UNCHANGING, VOMITING, W_SADDLE, W_WEP, WARN_OF_MON, WOUNDED_LEGS } from './const.js';
import { ALCHEMY_SMOCK, AMETHYST, ARM_BOOTS, ARMOR_CLASS, COIN_CLASS, CORPSE, is_ammo, is_corrodeable, is_gloves, is_poisonable, is_rustprone, LENSES, MAGIC_LAMP, MUMMY_WRAPPING, OBJ_DESCR, OIL_LAMP, POT_ACID, POT_BLINDNESS, POT_BOOZE, POT_CONFUSION, POT_ENLIGHTENMENT, POT_EXTRA_HEALING, POT_FRUIT_JUICE, POT_FULL_HEALING, POT_GAIN_ABILITY, POT_GAIN_ENERGY, POT_GAIN_LEVEL, POT_HALLUCINATION, POT_HEALING, POT_INVISIBILITY, POT_LEVITATION, POT_MONSTER_DETECTION, POT_OBJECT_DETECTION, POT_OIL, POT_PARALYSIS, POT_POLYMORPH, POT_RESTORE_ABILITY, POT_SEE_INVISIBLE, POT_SICKNESS, POT_SLEEPING, POT_SPEED, POT_WATER, POTION_CLASS, SPBOOK_CLASS, SPE_DETECT_MONSTERS, SPE_DETECT_TREASURE, SPE_HASTE_SELF, SPE_INVISIBILITY, SPE_LEVITATION, SPE_RESTORE_ABILITY, STRANGE_OBJECT, TOWEL, UNICORN_HORN, WEAPON_CLASS } from './objects.js';
import { pluslvl, rndexp, more_experienced } from './exper.js';
import { clone_mon, makemon } from './makemon.js';
import { bcsign, bless, curse, fixup_oil, mkobj, mksobj, obfree, obj_extract_self, splitobj, unbless, uncurse } from './mkobj.js';
import { an, aobjnam, cxname, doname, is_plural, makeplural, otense, simpleonames, The, thesimpleoname, vtense, xname, yname, Yname2, Yobjnam2 } from './objnam.js';
import { docall, hcolor, hliquid, rndmonnam } from './do_name.js';
import { depth, distu, on_level, ROLL_FROM, s_suffix, surface, upstart } from './hacklib.js';
import { cansee } from './vision.js';
import { P_SKILL } from './weapon.js';
import { nomul } from './cmd.js';
import { carried, freeinv, getobj, hands_obj, hold_another_object, learn_unseen_invent, makeknown, prinv, useup, useupall, observe_object } from './invent.js';
import { enlightenment } from './insight.js';
import { near_capacity } from './pickup.js';
import { DEADMONSTER, Monnam, a_monnam, aggravate, healmon, mon_nam, mongone, see_monsters, uhis, update_inventory, wake_nearto, wakeup } from './mon.js';
import { object_detect, monster_detect } from './detect.js';
import { alter_cost, check_unpaid, costly_alteration, in_rooms, shop_keeper, stolen_value, subfrombill } from './shk.js';
import { fall_asleep, incr_itimeout, set_itimeout } from './timeout.js';
import { make_glib, cloneu } from './mhitu.js';
import { is_pool } from './dbridge.js';
import { set_bc } from './ball.js';
import { remove_worn_item } from './steal.js';
import { trycall, doup, heal_legs } from './do.js';
import { burn_away_slime, can_reach_floor, killed, livelog_printf, losehp, monkilled, spoteffects } from './hack.js';
import { goto_level } from './allmain.js';
import { rider_cant_reach } from './steed.js';
import { explode_oil, explode } from './explode.js';
import { fix_petrification as fix_petrification_impl, newuhs } from './eat.js';
import { you_unwere, you_were, set_ulycn, new_were } from './were.js';
import { body_part, float_vs_flight, polyself } from './polyself.js';
import { find_delayed_killer, delayed_killer, dealloc_killer } from './end.js';
import { bhitm, makewish, obj_resists, obj_unpolyable, poly_obj, resist } from './zap.js';
import { which_armor, mon_set_minvis, mon_adjust_speed } from './worn.js';
import { tamedog, set_malign } from './dog.js';
// observe_object: moved to invent.js import below
import { stairway_at } from './stairs.js';
import { assign_level, get_level, ledger_no } from './dungeon.js';
import { mcureblindness } from './muse.js';
import { SetVoice, Soundeffect } from './sounds.js';
import { floating_above, wash_hands } from './fountain.js';
import { cmdq_peek, y_n, ynFunction } from './input.js';
import { erode_obj, fire_damage, float_up, water_damage } from './trap.js';
import { ceiling } from './dig.js';
import { sleep_monst, slept_monst, paralyze_monst } from './mhitm.js';
import { fingers_or_gloves, hard_helmet, inaccessible_equipment } from './do_wear.js';
import { waterbody_name } from './pager.js';
import { unfixable_trouble_count } from './apply.js';
// ── Non-RNG stubs for functions not yet ported ──
// NOTE: Most stubs below differ from their canonical implementations —
// they are simplified/no-op placeholders. Canonical modules noted where applicable.
// Stubs from do_wear.js and polyself.js are also kept to avoid circular deps
// (both modules import from potion.js).
// NOTE: differs from canonical polyself.js — simplified lookup table; circular dep (polyself.js imports from potion.js)
const FINGER = 3;
// NOTE: differs from canonical mondata.js — returns verb unchanged; canonical does locomotion-based substitution
function fruitname(_juice) { return 'slime mold juice'; } // TODO: port
// eyecount: imported from mondata.js
// NOTE: differs from canonical mondata.js — always returns true; canonical checks M1_NOEYES
// breathless imported from mondata.js
// NOTE: differs from canonical mondata.js — always returns false; canonical checks MS_SILENT
// has_head imported
// C ref: mondata.h:196 likes_fire = FIRE_VORTEX || FLAMING_SPHERE || likes_lava
// dmgtype imported // TODO
// resists_poison imported // TODO
// resists_acid imported // TODO
// NOTE: differs from canonical mondata.js — checks M2_UNDEAD|M2_DEMON directly; canonical uses is_vampshifter+hates_blessings
// is_were imported from mondata.js
// is_human imported
// is_vampshifter imported from mondata.js
// canseemon imported from display.js
// canspotmon imported from display.js
// mon_perma_blind: imported from mondata.js
function s_suffix_mon(mon) { return s_suffix(mon_nam(mon)); }
// objdescr_is imported from mondata.js
// C ref: hack.h — POTION_OCCUPANT_CHANCE(n) (13 + 2 * (n))
function POTION_OCCUPANT_CHANCE(n) { return 13 + 2 * (n || 0); }
// useup imported from invent.js
// useupall imported from invent.js
// splitobj imported from mkobj.js
// remove_worn_item imported from steal.js
// NOTE: differs from canonical invent.js — simplified; canonical has exercise(A_WIS), update_inventory, gem_learned
// trycall imported from do.js
// newuhs: imported from eat.js
// exercise imported from attrib.js
// losehp imported from hack.js
// Maybe_Half_Phys imported from macros.js
// Fixed_abil: imported from macros.js
// unfixable_trouble_count imported from apply.js
// find_delayed_killer imported from end.js
// C ref: youprop.h — PermaBlind = permanently blind (OPTIONS:blind)
function PermaBlind() { return !!((game.u?.uprops?.[BLINDED]?.intrinsic || 0) & FROMOUTSIDE); }
// set_bc imported from ball.js
// see_monsters imported from mon.js
// see_objects imported
// see_traps imported
function Sting_effects(_n) { /* TODO */ }
function eatmupdate() { /* TODO */ }
// update_inventory imported from mon.js
// vision_recalc imported from display.js
async function strange_feeling(obj, txt) { await strange_feeling_fn(obj, txt); }
// set_mimic_blocking imported from display.js
// aggravate: imported from mon.js
// NOTE: differs from canonical polyself.js — no-op; circular dep (polyself.js imports from potion.js)
// float_vs_flight imported from polyself.js
// heal_legs imported from hack.js
// ceiling: imported from dig.js
function self_invis_message_fn() { /* see exported function */ }
// doup imported // TODO
// NOTE: you_unwere/you_were/set_ulycn differ from canonical were.js — no-op stubs; canonicals are full implementations
// you_were imported from were.js
// fix_petrification: imported from eat.js
// NOTE: differs from canonical polyself.js — no-op; circular dep (polyself.js imports from potion.js)
// polyself imported from polyself.js
// burn_away_slime imported from hack.js
// NOTE: monstseesu/monstunseesu differ from canonical mondata.js — no-op; canonicals iterate fmon setting seen_resistance
// resist imported from zap.js
// mon_set_minvis imported
// sleep_monst imported from mhitm.js
// paralyze_monst imported from mhitm.js
// mon_adjust_speed imported
// mcureblindness imported from muse.js
// killed imported from hack.js
// monkilled imported from hack.js
// NOTE: differs from canonical zap.js — sync no-op; canonical is async (return type differs)
// NOTE: differs from canonical were.js — no-op; canonical is full async were-change implementation
// new_were imported from were.js
function split_mon_fn(_mon, _mtmp2) { return null; } // see exported split_mon
// cloneu imported from mhitu.js
// clone_mon: imported from makemon.js
// NOTE: differs from canonical explode.js — no-op; canonical calls end_burn + splatter_burning_oil
// explode_oil imported from explode.js
// NOTE: differs from canonical hacklib.js — different signature (canonical takes player object, not just x,y)
// obfree imported from mkobj.js
// observe_object imported
// freeinv imported from invent.js
// obj_extract_self imported
// which_armor imported from worn.js
// check_unpaid imported
// costly_alteration imported from shk.js
// subfrombill imported
// stolen_value imported from shk.js
// shop_keeper imported from shk.js
// in_rooms imported from shk.js
// water_damage imported from hack.js
// erode_obj imported from trap.js
// permapoisoned imported from artifact.js
// C ref: zap.c:1678 — obj_unpolyable(obj)
// NOTE: differs from canonical do_wear.js — always false; circular dep (do_wear.js imports from potion.js)
// inaccessible_equipment imported from do_wear.js
// is_pool imported from dbridge.js
// NOTE: differs from canonical steed.js — no-op; canonical prints message
// NOTE: differs from canonical fountain.js — always false; canonical washes gloves, clears Glib
// wash_hands imported from fountain.js
// C ref: obj.h — pair_of(o) = LENSES || is_gloves || is_boots
function pair_of(obj) {
    if (!obj) return false;
    if (obj.otyp === LENSES) return true;
    if (is_gloves(obj)) return true;
    // is_boots inline (not centrally exported)
    if (obj.oclass === ARMOR_CLASS && game.objects[obj.otyp]?.oc_armcat === ARM_BOOTS) return true;
    return false;
}
function short_oname(obj, fn1, fn2, maxlen) {
    const primary = (typeof fn1 === 'function') ? fn1(obj) : xname(obj);
    if (!Number.isFinite(maxlen) || maxlen <= 0 || primary.length <= maxlen) {
        return primary;
    }
    if (typeof fn2 === 'function') {
        return fn2(obj);
    }
    return primary;
}
// is_weptool imported from mondata.js
// NOTE: differs from canonical do_wear.js — always false; circular dep (do_wear.js imports from potion.js)
// hard_helmet imported from do_wear.js
// is_swimmer imported // TODO
// P_SKILL imported // TODO
// livelog_printf imported from hack.js
// uhis: imported from mon.js
// prinv imported
// NOTE: differs from canonical zap.js — no-op; canonical is full wish implementation
// tamedog imported from dog.js
// set_malign imported
// tmp_at: imported from display.js
// Is_airlevel imported from dbridge.js
// stairway_at imported // TODO
// ledger_no: imported from dungeon.js
function Can_rise_up(_x, _y, _uz) { return false; } // TODO
// get_level: imported from dungeon.js
// NOTE: differs from canonical allmain.js — no-op; canonical is full level transition
// NOTE: differs from canonical explode.js — no-op; canonical is full explosion implementation
// near_capacity imported from pickup.js
// unmap_object: imported from display.js
// object_detect imported from detect.js
// getobj imported from invent.js
// y_n: imported from input.js
// enlightenment: imported from insight.js
function do_enlightenment_effect() { /* TODO */ }
// drinkfountain/drinksink/dipfountain/dipsink imported from fountain.js
import { drinkfountain, drinksink, dipfountain, dipsink } from './fountain.js';
// NOTE: bless/curse differ from canonical mkobj.js — use 1/0 not true/false, no COIN_CLASS guard
// NOTE: differs from canonical do_wear.js — simplified; circular dep (do_wear.js imports from potion.js)
// Punished imported from macros.js
// C ref: youprop.h:169 EWarn_of_mon
function EWarn_of_mon() { return game.u?.uprops?.[WARN_OF_MON]?.extrinsic || 0; }
// U_AP_TYPE imported from macros.js
const something = 'something';
const Something = 'Something';
const You_can_move_again = 'You can move again.';
const vision_clears = 'vision clears.';
const NOTELL = 0;
const ARTICLE_THE = 1;
const SUPPRESS_IT = 1;
const SUPPRESS_SADDLE = 16;
const DISP_ALWAYS = -1;
const DISP_END = -2;
const EXPL_FIERY = 1;
const ERODE_CORRODE = 3;
const EF_GREASE = 0x01;
const ER_NOTHING = 0;
const ER_DESTROYED = 3;
// P_RIDING: use import from const.js if needed (value 37)
const NH_AMBER = 'amber';
const NH_LIGHT_BLUE = 'light blue';
const NH_BLACK = 'black';
const NH_ORANGE = 'orange';
const NH_RED = 'red';
// GETOBJ constants — must match const.js canonical values
const SHOPBASE = 14;
const LL_CONDUCT = 0x0010;
// G_GONE imported from const.js
const BUFSZ = 256;
const QBUFSZ = 128;
// ── Property access helpers ──
// C: HFoo / EFoo / BFoo are macros accessing u.uprops[PROP].intrinsic/extrinsic/blocked
// H, E, B: imported from macros.js
// setH, setE: imported from macros.js
// HConfusion: imported from macros.js
// HStun: imported from macros.js
// HHallucination: imported from macros.js
function HSee_invisible() { return H(SEE_INVIS); }
// HFast: imported from macros.js
// HLevitation: imported from macros.js
function HInvis() { return H(INVIS); }
// HBlinded: imported from macros.js
function HDeaf() { return H(DEAF); }
function HDetect_monsters() { return H(DETECT_MONSTERS); }
function EHalluc_resistance() { return E(HALLUC_RES); }
function EBlinded() { return E(BLINDED); }
function BBlinded() { return B(BLINDED); }
function BInvis() { return B(INVIS); }
function BLevitation() { return B(LEVITATION); }
// BlindedTimeout imported from macros.js
// Sick_val: imported from macros.js
function Stoned_val() { return H(STONED); }
function Slimed_val() { return H(SLIMED); }
function Vomiting_val() { return H(VOMITING); }
function Strangled_val() { return H(STRANGLED); }
function Glib_val() { return H(GLIB); }
function Wounded_legs_val() { return H(WOUNDED_LEGS); }
function Fast_val() { return HFast() || E(FAST); }
// Very_fast: imported from macros.js (fixed: was using wrong formula)
function Free_action_val() { return !!(H(FREE_ACTION) || E(FREE_ACTION)); }
// Unchanging, Sick_resistance, Detect_monsters, Blindfolded_only imported from macros.js
function Unchanging_val() { return Unchanging(); }
function Sick_resistance_val() { return Sick_resistance(); }
function Detect_monsters_val() { return Detect_monsters(); }
// Unaware imported from macros.js
// ── Timeout manipulation (C: timeout.c) ──
function itimeout(val) {
    if (val >= TIMEOUT) val = TIMEOUT;
    else if (val < 1) val = 0;
    return val;
}
function itimeout_incr(old, incr) {
    return itimeout((old & TIMEOUT) + incr);
}
// set_itimeout imported from timeout.js
// incr_itimeout imported from timeout.js
export async function make_confused(xtime, talk) {
    const old = HConfusion();
    if (Unaware()) talk = false;
    if (!xtime && old) {
        if (talk) await You_feel(`less ${Hallucination() ? 'trippy' : 'confused'} now.`);
    }
    if ((xtime && !old) || (!xtime && old))
        setBotl('make_confused');
    set_itimeout(CONFUSION, xtime);
}
export async function make_stunned(xtime, talk) {
    const old = HStun();
    if (Unaware()) talk = false;
    if (!xtime && old) {
        if (talk)
            await You_feel(`${Hallucination() ? 'less wobbly' : 'a bit steadier'} now.`);
    }
    if (xtime && !old) {
        if (talk) {
            if (game.u.usteed)
                await You('wobble in the saddle.');
            else
                await You(`${stagger(game.player?.data, 'stagger')}...`);
        }
    }
    if ((!xtime && old) || (xtime && !old))
        setBotl('make_stunned');
    set_itimeout(STUNNED, xtime);
}
export async function make_sick(xtime, cause, talk, type) {
    const old = Sick_val();
    if (xtime > 0) {
        if (Sick_resistance_val()) return;
        if (!old) {
            await You_feel('deathly sick.');
        } else {
            if (talk)
                await You_feel(`${xtime <= Math.trunc(Sick_val() / 2) ? 'much' : 'even'} worse.`);
        }
        set_itimeout(SICK, xtime);
        game.u.usick_type = (game.u.usick_type || 0) | type;
        setBotl('make_sick');
    } else if (old && (type & (game.u.usick_type || 0))) {
        game.u.usick_type = (game.u.usick_type || 0) & ~type;
        if (game.u.usick_type) {
            if (talk) await You_feel('somewhat better.');
            set_itimeout(SICK, Sick_val() * 2);
        } else {
            if (talk) await You_feel('cured.  What a relief!');
            setH(SICK, 0);
        }
        setBotl('make_sick');
    }
    const kptr = find_delayed_killer(SICK);
    if (Sick_val()) {
        exercise(A_CON, false);
        if (xtime || !old || !kptr) {
            const kpfx = (cause && cause === '#wizintrinsic') ? KILLED_BY : KILLED_BY_AN;
            delayed_killer(SICK, kpfx, cause);
        }
    } else {
        dealloc_killer(kptr);
    }
}
export async function make_slimed(xtime, msg) {
    const old = Slimed_val();
    set_itimeout(SLIMED, xtime);
    if ((xtime !== 0) ^ (old !== 0)) {
        setBotl('make_slimed');
        if (msg) await pline(`${msg}`);
    }
    if (!Slimed_val()) {
        dealloc_killer(find_delayed_killer(SLIMED));
        if (U_AP_TYPE() === M_AP_MONSTER() &&
            game.player?.mappearance === PM_GREEN_SLIME) {
            game.player.m_ap_type = M_AP_NOTHING();
            game.player.mappearance = 0;
        }
    }
}
export async function make_stoned(xtime, msg, killedby, killername) {
    const old = Stoned_val();
    set_itimeout(STONED, xtime);
    if ((xtime !== 0) ^ (old !== 0)) {
        setBotl('make_stoned');
        if (msg) await pline(`${msg}`);
    }
    if (!Stoned_val())
        dealloc_killer(find_delayed_killer(STONED));
    else if (!old)
        delayed_killer(STONED, killedby, killername);
}
export async function make_vomiting(xtime, talk) {
    const old = Vomiting_val();
    if (Unaware()) talk = false;
    set_itimeout(VOMITING, xtime);
    setBotl('make_vomiting');
    if (!xtime && old)
        if (talk) await You_feel('much less nauseated now.');
}
const vismsg = 'vision seems to %s for a moment but is %s now.';
const eyemsg = '%s momentarily %s.';
export async function make_blinded(xtime, talk) {
    const old = BlindedTimeout();
    let u_could_see, can_see_now;
    // probe ahead
    u_could_see = !Blind();
    set_itimeout(BLINDED, xtime ? 1 : 0);
    can_see_now = !Blind();
    set_itimeout(BLINDED, old);
    if (Unaware()) talk = false;
    if (can_see_now && !u_could_see) {
        if (talk) {
            if (Hallucination())
                await pline('Far out!  Everything is all cosmic again!');
            else
                await You('can see again.');
        }
    } else if (old && !xtime) {
        if (talk) {
            if (!haseyes(game.player?.data) || PermaBlind()) {
                await strange_feeling(null, null);
            } else if (Blindfolded()) {
                let eyes = body_part(EYE);
                if (eyecount(game.player?.data) !== 1) eyes = makeplural(eyes);
                await Your(eyemsg.replace('%s', eyes).replace('%s', vtense(eyes, 'itch')));
            } else {
                await Your(vismsg.replace('%s', 'brighten').replace('%s', Hallucination() ? 'sadder' : 'normal'));
            }
        }
    }
    if (u_could_see && !can_see_now) {
        if (talk) {
            if (Hallucination())
                await pline('Oh, bummer!  Everything is dark!  Help!');
            else
                await pline('A cloud of darkness falls upon you.');
        }
        if (Punished()) set_bc(0);
    } else if (!old && xtime) {
        if (talk) {
            if (!haseyes(game.player?.data) || PermaBlind()) {
                await strange_feeling(null, null);
            } else if (Blindfolded()) {
                let eyes = body_part(EYE);
                if (eyecount(game.player?.data) !== 1) eyes = makeplural(eyes);
                await Your(eyemsg.replace('%s', eyes).replace('%s', vtense(eyes, 'twitch')));
            } else {
                await Your(vismsg.replace('%s', 'dim').replace('%s', Hallucination() ? 'happier' : 'normal'));
            }
        }
    }
    set_itimeout(BLINDED, xtime);
    if (u_could_see ^ can_see_now) {
        toggle_blindness();
    }
}
export function toggle_blindness() {
    const Stinging = (game.u.uwep && (EWarn_of_mon() & W_WEP) !== 0);
    setBotl('toggle_blindness');
    // C: gv.vision_full_recalc = 1;
    vision_recalc(0);
    // C: if (Blind_telepat || Infravision || Stinging) see_monsters();
    if (Blind_telepat() || Infravision() || Stinging)
        see_monsters();
    if (Stinging) Sting_effects(-1);
    if (!Blind()) learn_unseen_invent();
}
export async function make_hallucinated(xtime, talk, mask) {
    const old = HHallucination();
    let changed = false;
    if (Unaware()) talk = false;
    const message = (!xtime) ? 'Everything %s SO boring now.'
                             : 'Oh wow!  Everything %s so cosmic!';
    const verb = (!Blind()) ? 'looks' : 'feels';
    if (mask) {
        if (HHallucination()) changed = true;
        if (!xtime)
            setE(HALLUC_RES, EHalluc_resistance() | mask);
        else
            setE(HALLUC_RES, EHalluc_resistance() & ~mask);
    } else {
        if (!EHalluc_resistance() && (!!HHallucination() !== !!xtime))
            changed = true;
        set_itimeout(HALLUC, xtime);
        if (!changed && !HHallucination() && old && talk) {
            if (!haseyes(game.player?.data)) {
                await strange_feeling(null, null);
            } else if (Blind()) {
                let eyes = body_part(EYE);
                if (eyecount(game.player?.data) !== 1) eyes = makeplural(eyes);
                await Your(eyemsg.replace('%s', eyes).replace('%s', vtense(eyes, 'itch')));
            } else {
                await Your(vismsg.replace('%s', 'flatten').replace('%s', 'normal'));
            }
        }
    }
    if (changed) {
        if (!Hallucination()) eatmupdate();
        if (game.u.uswallow) {
            // swallowed(0);
        } else {
            see_monsters();
            see_objects();
            see_traps();
        }
        update_inventory();
        setBotl('potion');
        if (talk)
            await pline(message.replace('%s', verb));
    }
    return changed;
}
export async function make_deaf(xtime, talk) {
    const old = HDeaf();
    if (Unaware()) talk = false;
    set_itimeout(DEAF, xtime);
    if ((xtime !== 0) ^ (old !== 0)) {
        setBotl('make_deaf');
        if (talk)
            await You(old && !Deaf() ? 'can hear again.' : 'are unable to hear anything.');
    }
}
// make_glib imported from mhitu.js
export async function self_invis_message() {
    await pline(`${Hallucination() ? 'Far out, man!  You' : "Gee!  All of a sudden, you"} ${See_invisible() ? 'can see right through yourself' : "can't see yourself"}.`);
}
// ── ghost_from_bottle ──
async function ghost_from_bottle() {
    const mtmp = await makemon(mons[PM_GHOST], game.u.ux, game.u.uy, MM_NOMSG);
    if (!mtmp) {
        await pline('This bottle turns out to be empty.');
        return;
    }
    if (Blind()) {
        await pline(`As you open the bottle, ${something} emerges.`);
        return;
    }
    await pline(`As you open the bottle, an enormous ${Hallucination() ? rndmonnam(null) : 'ghost'} emerges!`);
    if (game.flags.verbose)
        await You('are frightened to death, and unable to move.');
    nomul(-3);
    game.multi_reason = 'being frightened to death';
    game.nomovemsg = 'You regain your composure.';
}
// ── peffect sub-functions ──
async function peffect_restore_ability(otmp) {
    game.potion_unkn = (game.potion_unkn || 0) + 1;
    if (otmp.cursed) {
        await pline('Ulch!  This makes you feel mediocre!');
        return;
    } else {
        const startI = rn2(A_MAX);
        let i = startI;
        await pline(`Wow!  This makes you feel ${(!otmp.blessed) ? 'good' : unfixable_trouble_count(false) ? 'better' : 'great'}!`);
        for (let ii = 0; ii < A_MAX; ii++) {
            const lim = AMAX(i);
            if (ABASE(i) < lim) {
                game.u.acurr.a[i] = lim;
                // reset stat abuse
                if (game.u.aexe?.a) {
                    game.u.aexe.a[i] = Math.max(game.u.aexe.a[i] || 0, 0);
                }
                setBotl('peffect_restore_ability');
                if (!otmp.blessed) break;
            }
            i++;
            if (i >= A_MAX) i = 0;
        }
        // Potion also restores lost levels
        if (otmp.otyp === POT_RESTORE_ABILITY && game.u.ulevel < game.u.ulevelmax) {
            do {
                await pluslvl(false);
            } while (game.u.ulevel < game.u.ulevelmax && otmp.blessed);
        }
    }
}
async function peffect_hallucination(otmp) {
    if (Halluc_resistance()) {
        game.potion_nothing = (game.potion_nothing || 0) + 1;
        return;
    } else if (Hallucination()) {
        game.potion_nothing = (game.potion_nothing || 0) + 1;
    }
    await make_hallucinated(
        itimeout_incr(HHallucination(), rn1(200, 600 - 300 * bcsign(otmp))),
        true, 0
    );
    if ((otmp.blessed && !rn2(3)) || (!otmp.cursed && !rn2(6))) {
        await You('perceive yourself...');
        await display_nhwindow(game.WIN_MESSAGE, false);
        await enlightenment(MAGICENLIGHTENMENT, ENL_GAMEINPROGRESS);
        await Your('awareness re-normalizes.');
        exercise(A_WIS, true);
    }
}
async function peffect_water(otmp) {
    if (!otmp.blessed && !otmp.cursed) {
        await pline(`This tastes like ${hliquid('water')}.`);
        game.u.uhunger = (game.u.uhunger || 0) + rnd(10);
        await newuhs(false);
        return;
    }
    game.potion_unkn = (game.potion_unkn || 0) + 1;
    if (mon_hates_blessings(game.player)
        || (game.u.ualign && game.u.ualign.type === A_CHAOTIC)) {
        if (otmp.blessed) {
            await pline(`This burns like ${hliquid('acid')}!`);
            exercise(A_CON, false);
            if (ismnum(game.u.ulycn)) {
                await Your(`affinity to ${makeplural(mons[game.u.ulycn]?.pmnames?.[1] || 'beast')} disappears!`);
                if (game.player?.data === mons[game.u.ulycn])
                    await you_unwere(false);
                set_ulycn(NON_PM);
            }
            await losehp(Maybe_Half_Phys(c_d(2, 6)), 'potion of holy water', KILLED_BY_AN);
        } else if (otmp.cursed) {
            await You_feel('quite proud of yourself.');
            await healup(c_d(2, 6), 0, 0, 0);
            if (ismnum(game.u.ulycn) && !Upolyd())
                await you_were();
            exercise(A_CON, true);
        }
    } else {
        if (otmp.blessed) {
            await You_feel('full of awe.');
            await make_sick(0, null, true, SICK_ALL);
            exercise(A_WIS, true);
            exercise(A_CON, true);
            if (ismnum(game.u.ulycn))
                await you_unwere(true);
        } else {
            if (game.u.ualign && game.u.ualign.type === A_LAWFUL) {
                await pline(`This burns like ${hliquid('acid')}!`);
                await losehp(Maybe_Half_Phys(c_d(2, 6)), 'potion of unholy water', KILLED_BY_AN);
            } else {
                await You_feel('full of dread.');
            }
            if (ismnum(game.u.ulycn) && !Upolyd())
                await you_were();
            exercise(A_CON, false);
        }
    }
}
async function peffect_booze(otmp) {
    game.potion_unkn = (game.potion_unkn || 0) + 1;
    await pline(`Ooph!  This tastes like ${otmp.odiluted ? 'watered down ' : ''}${Hallucination() ? 'dandelion wine' : 'liquid fire'}!`);
    if (!otmp.blessed) {
        await make_confused(
            itimeout_incr(HConfusion(), c_d(2 + (game.u.uhs || 0), 8)),
            false
        );
    }
    if (!otmp.odiluted)
        await healup(1, 0, false, false);
    game.u.uhunger = (game.u.uhunger || 0) + 10 * (2 + bcsign(otmp));
    await newuhs(false);
    exercise(A_WIS, false);
    if (otmp.cursed) {
        await You('pass out.');
        game.multi = -rnd(15);
        game.nomovemsg = 'You awake with a headache.';
    }
}
async function peffect_enlightenment(otmp) {
    if (otmp.cursed) {
        game.potion_unkn = (game.potion_unkn || 0) + 1;
        await You('have an uneasy feeling...');
        exercise(A_WIS, false);
    } else {
        if (otmp.blessed) {
            await adjattrib(A_INT, 1, false);
            await adjattrib(A_WIS, 1, false);
        }
        await do_enlightenment_effect();
    }
}
async function peffect_invisibility(otmp) {
    const is_spell = (otmp.oclass === SPBOOK_CLASS);
    if (is_spell && BInvis() && game.u.uarmc?.otyp === MUMMY_WRAPPING) {
        await You_feel(`rather itchy under ${xname(game.u.uarmc)}.`);
        return;
    }
    if (Invis() || Blind() || BInvis()) {
        game.potion_nothing = (game.potion_nothing || 0) + 1;
    } else {
        await self_invis_message();
    }
    if (otmp.blessed && !rn2(HInvis() ? 15 : 30))
        setH(INVIS, HInvis() | FROMOUTSIDE);
    else
        incr_itimeout(INVIS, c_d(6 - 3 * bcsign(otmp), 100) + 100);
    newsym(game.u.ux, game.u.uy);
    if (otmp.cursed) {
        await pline('For some reason, you feel your presence is known.');
        aggravate();
        setH(INVIS, HInvis() & ~FROMOUTSIDE);
    }
}
async function peffect_see_invisible(otmp) {
    const msg = Invisible() && !Blind();
    game.potion_unkn = (game.potion_unkn || 0) + 1;
    if (otmp.cursed)
        await pline(`Yecch!  This tastes ${Hallucination() ? 'overripe' : 'rotten'}.`);
    else
        await pline(
            Hallucination()
                ? `This tastes like 10% real ${otmp.odiluted ? 'reconstituted ' : ''}${fruitname(true)} all-natural beverage.`
                : `This tastes like ${otmp.odiluted ? 'reconstituted ' : ''}${fruitname(true)}.`
        );
    if (otmp.otyp === POT_FRUIT_JUICE) {
        game.u.uhunger = (game.u.uhunger || 0) + (otmp.odiluted ? 5 : 10) * (2 + bcsign(otmp));
        await newuhs(false);
        return;
    }
    if (!otmp.cursed) {
        await make_blinded(0, true);
    }
    if (otmp.blessed)
        setH(SEE_INVIS, HSee_invisible() | FROMOUTSIDE);
    else
        incr_itimeout(SEE_INVIS, rn1(100, 750));
    set_mimic_blocking();
    see_monsters();
    newsym(game.u.ux, game.u.uy);
    if (msg && !Blind()) {
        await You('can see through yourself, but you are visible!');
        game.potion_unkn = (game.potion_unkn || 0) - 1;
    }
}
async function peffect_paralysis(otmp) {
    if (Free_action_val()) {
        await You('stiffen momentarily.');
    } else {
        if (Levitation() || Is_airlevel(game.u.uz) || Is_waterlevel(game.u.uz))
            await You('are motionlessly suspended.');
        else if (game.u.usteed)
            await You('are frozen in place!');
        else
            await Your(`${makeplural(body_part(FOOT))} are frozen to the ${surface(game.u.ux, game.u.uy)}!`);
        nomul(-(rn1(10, 25 - 12 * bcsign(otmp))));
        game.multi_reason = 'frozen by a potion';
        game.nomovemsg = You_can_move_again;
        exercise(A_DEX, false);
    }
}
async function peffect_sleeping(otmp) {
    if (Sleep_resistance() || Free_action_val()) {
        monstseesu(M_SEEN_SLEEP);
        await You('yawn.');
    } else {
        await You('suddenly fall asleep!');
        monstunseesu(M_SEEN_SLEEP);
        await fall_asleep(-rn1(10, 25 - 12 * bcsign(otmp)), true);
    }
}
async function peffect_monster_detection(otmp) {
    if (otmp.blessed) {
        if (Detect_monsters_val())
            game.potion_nothing = (game.potion_nothing || 0) + 1;
        game.potion_unkn = (game.potion_unkn || 0) + 1;
        let i;
        if ((HDetect_monsters() & TIMEOUT) >= 300)
            i = 1;
        else if (otmp.oclass === SPBOOK_CLASS)
            i = rn1(40, 21);
        else
            i = rn2(100) + 100;
        incr_itimeout(DETECT_MONSTERS, i);
        for (let x = 1; x < COLNO; x++) {
            for (let y = 0; y < ROWNO; y++) {
                // TODO: check GLYPH_INVISIBLE
                if (m_at(x, y))
                    game.potion_unkn = 0;
            }
        }
        if (!game.u.uswallow && !Underwater()) {
            see_monsters();
            if (game.potion_unkn)
                await You_feel('lonely.');
            return 0;
        }
    }
    if (await monster_detect(otmp, 0))
        return 1;
    exercise(A_WIS, true);
    return 0;
}
async function peffect_object_detection(otmp) {
    if (await object_detect(otmp, 0))
        return 1;
    exercise(A_WIS, true);
    return 0;
}
async function peffect_sickness(otmp) {
    await pline('Yecch!  This stuff tastes like poison.');
    if (otmp.blessed) {
        await pline(`(But in fact it was mildly stale ${fruitname(true)}.)`);
        if (!Role_if(PM_HEALER)) {
            await losehp(1, 'mildly contaminated potion', KILLED_BY_AN);
        }
    } else {
        if (Poison_resistance())
            await pline(`(But in fact it was biologically contaminated ${fruitname(true)}.)`);
        if (Role_if(PM_HEALER)) {
            await pline('Fortunately, you have been immunized.');
        } else {
            const typ = rn2(A_MAX);
            const contaminant = `${Poison_resistance() ? 'mildly ' : ''}${otmp.fromsink ? 'contaminated tap water' : 'contaminated potion'}`;
            if (!Fixed_abil()) {
                await poisontell(typ, false);
                await adjattrib(typ, Poison_resistance() ? -1 : -rn1(4, 3), 1);
            }
            if (!Poison_resistance()) {
                if (otmp.fromsink)
                    await losehp(rnd(10) + 5 * (otmp.cursed ? 1 : 0), contaminant, KILLED_BY);
                else
                    await losehp(rnd(10) + 5 * (otmp.cursed ? 1 : 0), contaminant, KILLED_BY_AN);
            } else {
                await losehp(1 + rn2(2), contaminant,
                    otmp.fromsink ? KILLED_BY : KILLED_BY_AN);
            }
            exercise(A_CON, false);
        }
    }
    if (Hallucination()) {
        await You('are shocked back to your senses!');
        await make_hallucinated(0, false, 0);
    }
}
async function peffect_confusion(otmp) {
    if (!Confusion()) {
        if (Hallucination()) {
            await pline('What a trippy feeling!');
            game.potion_unkn = (game.potion_unkn || 0) + 1;
        } else {
            await pline('Huh, What?  Where am I?');
        }
    } else {
        game.potion_nothing = (game.potion_nothing || 0) + 1;
    }
    await make_confused(
        itimeout_incr(HConfusion(), rn1(7, 16 - 8 * bcsign(otmp))),
        false
    );
}
async function peffect_gain_ability(otmp) {
    if (otmp.cursed) {
        await pline('Ulch!  That potion tasted foul!');
        game.potion_unkn = (game.potion_unkn || 0) + 1;
    } else if (Fixed_abil()) {
        game.potion_nothing = (game.potion_nothing || 0) + 1;
    } else {
        let i = -1;
        for (let ii = A_MAX; ii > 0; ii--) {
            i = otmp.blessed ? (i + 1) : rn2(A_MAX);
            const itmp = (otmp.blessed || ii === 1) ? 0 : -1;
            if (await adjattrib(i, 1, itmp) && !otmp.blessed)
                break;
        }
    }
}
async function peffect_speed(otmp) {
    const is_speed = (otmp.otyp === POT_SPEED);
    if (is_speed && Wounded_legs_val() && !otmp.cursed && !game.u.usteed) {
        await heal_legs(0);
        game.potion_unkn = (game.potion_unkn || 0) + 1;
        return;
    }
    await speed_up(rn1(10, 100 + 60 * bcsign(otmp)));
    if (is_speed && !otmp.cursed && !(HFast() & INTRINSIC)) {
        await Your('quickness feels very natural.');
        setH(FAST, HFast() | FROMOUTSIDE);
    }
}
async function peffect_blindness(otmp) {
    if (Blind() || ((HBlinded() || EBlinded()) && BBlinded()))
        game.potion_nothing = (game.potion_nothing || 0) + 1;
    await make_blinded(
        itimeout_incr(BlindedTimeout(), rn1(200, 250 - 125 * bcsign(otmp))),
        !Blind()
    );
}
async function peffect_gain_level(otmp) {
    if (otmp.cursed) {
        const on_lvl_1 = (ledger_no(game.u.uz) === 1);
        game.potion_unkn = (game.potion_unkn || 0) + 1;
        if (on_lvl_1 ? game.u.uhave?.amulet : Can_rise_up(game.u.ux, game.u.uy, game.u.uz)) {
            let newlevel = {};
            if (on_lvl_1) {
                // assign_level(&newlevel, &earth_level);
                // TODO
            } else {
                const newlev = depth(game.u.uz) - 1;
                get_level(newlevel, newlev);
                if (on_level(newlevel, game.u.uz)) {
                    await pline('It tasted bad.');
                    return;
                }
            }
            await You(`rise up, through the ${ceiling(game.u.ux, game.u.uy)}!`);
            await goto_level(newlevel, false, false, false);
        } else {
            await You('have an uneasy feeling.');
        }
        return;
    }
    await pluslvl(false);
    if (otmp.blessed)
        game.u.uexp = rndexp(true);
}
async function peffect_healing(otmp) {
    await You_feel('better.');
    await healup(8 + c_d(4 + 2 * bcsign(otmp), 4), !otmp.cursed ? 1 : 0,
        !!otmp.blessed, !otmp.cursed);
    exercise(A_CON, true);
}
async function peffect_extra_healing(otmp) {
    await You_feel('much better.');
    await healup(16 + c_d(4 + 2 * bcsign(otmp), 8),
        otmp.blessed ? 5 : !otmp.cursed ? 2 : 0,
        !otmp.cursed, true);
    await make_hallucinated(0, true, 0);
    exercise(A_CON, true);
    exercise(A_STR, true);
    if (Wounded_legs_val() && otmp.blessed && !game.u.usteed)
        await heal_legs(0);
}
async function peffect_full_healing(otmp) {
    await You_feel('completely healed.');
    await healup(400, 4 + 4 * bcsign(otmp), !otmp.cursed, true);
    if (otmp.blessed && game.u.ulevel < game.u.ulevelmax) {
        game.u.ulevelmax -= 1;
        await pluslvl(false);
    }
    await make_hallucinated(0, true, 0);
    exercise(A_STR, true);
    exercise(A_CON, true);
    if (Wounded_legs_val() && (otmp.blessed || (!otmp.cursed && !game.u.usteed)))
        await heal_legs(0);
}
async function peffect_levitation(otmp) {
    if (!Levitation() && !BLevitation()) {
        set_itimeout(LEVITATION, 1);
        await float_up();
    } else {
        game.potion_nothing = (game.potion_nothing || 0) + 1;
    }
    if (otmp.cursed) {
        setH(LEVITATION, HLevitation() & ~I_SPECIAL);
        if (BLevitation()) {
            // rising via levitation is blocked
        } else {
            const stway = stairway_at(game.u.ux, game.u.uy);
            if (stway && stway.up) {
                await doup();
                game.potion_nothing = 0;
            } else if (has_ceiling(game.u.uz)) {
                const dmg = rnd(!game.u.uarmh ? 10 : !hard_helmet(game.u.uarmh) ? 6 : 3);
                await You(`hit your ${body_part(HEAD)} on the ${ceiling(game.u.ux, game.u.uy)}.`);
                await losehp(Maybe_Half_Phys(dmg), 'colliding with the ceiling', KILLED_BY);
                game.potion_nothing = 0;
            }
        }
    } else if (otmp.blessed) {
        incr_itimeout(LEVITATION, rn1(50, 250));
        setH(LEVITATION, HLevitation() | I_SPECIAL);
    } else {
        incr_itimeout(LEVITATION, rn1(140, 10));
    }
    const here = levl(game.u.ux, game.u.uy);
    if (Levitation() && here && here.typ === SINK)
        await spoteffects(false);
    float_vs_flight();
}
async function peffect_gain_energy(otmp) {
    if (otmp.cursed)
        await You_feel('lackluster.');
    else
        await pline('Magical energies course through your body.');
    const num_dice = otmp.blessed ? 3 : !otmp.cursed ? 2 : 1;
    let num = c_d(num_dice, 6);
    if (otmp.cursed) num = -num;
    game.u.uenmax = (game.u.uenmax || 0) + num;
    if (game.u.uenmax > (game.u.uenpeak || 0))
        game.u.uenpeak = game.u.uenmax;
    else if (game.u.uenmax <= 0)
        game.u.uenmax = 0;
    game.u.uen = (game.u.uen || 0) + 3 * num;
    if (game.u.uen > game.u.uenmax)
        game.u.uen = game.u.uenmax;
    else if (game.u.uen <= 0)
        game.u.uen = 0;
    setBotl('peffect_gain_energy');
    exercise(A_WIS, true);
}
async function peffect_oil(otmp) {
    let good_for_you = false;
    if (otmp.lamplit) {
        if (likes_fire(game.player?.data)) {
            await pline('Ahh, a refreshing drink.');
            good_for_you = true;
        } else {
            await You(`burn your ${body_part(FACE)}.`);
            const vulnerable = !Fire_resistance() || Cold_resistance();
            await losehp(c_d(vulnerable ? 4 : 2, 4),
                'quaffing a burning potion of oil', KILLED_BY);
        }
        await burn_away_slime();
    } else if (otmp.cursed) {
        await pline('This tastes like castor oil.');
    } else {
        await pline('That was smooth!');
    }
    exercise(A_WIS, good_for_you);
}
async function peffect_acid(otmp) {
    if (Acid_resistance()) {
        await pline(`This tastes ${Hallucination() ? 'tangy' : 'sour'}.`);
    } else {
        await pline(`This burns${otmp.blessed ? ' a little' : otmp.cursed ? ' a lot' : ' like acid'}!`);
        const dmg = c_d(otmp.cursed ? 2 : 1, otmp.blessed ? 4 : 8);
        await losehp(Maybe_Half_Phys(dmg), 'potion of acid', KILLED_BY_AN);
        exercise(A_CON, false);
    }
    if (Stoned_val())
        await fix_petrification();
    game.potion_unkn = (game.potion_unkn || 0) + 1;
}
async function peffect_polymorph(otmp) {
    await You_feel(`a little ${Hallucination() ? 'normal' : 'strange'}.`);
    if (!Unchanging_val()) {
        if (!otmp.blessed || (game.u.umonnum !== game.u.umonster))
            await polyself(POLY_NOFLAGS);
        else {
            await polyself(POLY_CONTROLLED | POLY_LOW_CTRL);
            if (game.u.mtimedone && game.u.umonnum !== game.u.umonster)
                game.u.mtimedone = Math.min(game.u.mtimedone, rn2(15) + 10);
        }
    }
}
// ── peffects (the big switch) ──
export async function peffects(otmp) {
    switch (otmp.otyp) {
    case POT_RESTORE_ABILITY:
    case SPE_RESTORE_ABILITY:
        await peffect_restore_ability(otmp);
        break;
    case POT_HALLUCINATION:
        await peffect_hallucination(otmp);
        break;
    case POT_WATER:
        await peffect_water(otmp);
        break;
    case POT_BOOZE:
        await peffect_booze(otmp);
        break;
    case POT_ENLIGHTENMENT:
        await peffect_enlightenment(otmp);
        break;
    case SPE_INVISIBILITY:
    case POT_INVISIBILITY:
        await peffect_invisibility(otmp);
        break;
    case POT_SEE_INVISIBLE:
    case POT_FRUIT_JUICE:
        await peffect_see_invisible(otmp);
        break;
    case POT_PARALYSIS:
        await peffect_paralysis(otmp);
        break;
    case POT_SLEEPING:
        await peffect_sleeping(otmp);
        break;
    case POT_MONSTER_DETECTION:
    case SPE_DETECT_MONSTERS:
        if (await peffect_monster_detection(otmp))
            return 1;
        break;
    case POT_OBJECT_DETECTION:
    case SPE_DETECT_TREASURE:
        if (await peffect_object_detection(otmp))
            return 1;
        break;
    case POT_SICKNESS:
        await peffect_sickness(otmp);
        break;
    case POT_CONFUSION:
        await peffect_confusion(otmp);
        break;
    case POT_GAIN_ABILITY:
        await peffect_gain_ability(otmp);
        break;
    case POT_SPEED:
    case SPE_HASTE_SELF:
        await peffect_speed(otmp);
        break;
    case POT_BLINDNESS:
        await peffect_blindness(otmp);
        break;
    case POT_GAIN_LEVEL:
        await peffect_gain_level(otmp);
        break;
    case POT_HEALING:
        await peffect_healing(otmp);
        break;
    case POT_EXTRA_HEALING:
        await peffect_extra_healing(otmp);
        break;
    case POT_FULL_HEALING:
        await peffect_full_healing(otmp);
        break;
    case POT_LEVITATION:
    case SPE_LEVITATION:
        await peffect_levitation(otmp);
        break;
    case POT_GAIN_ENERGY:
        await peffect_gain_energy(otmp);
        break;
    case POT_OIL:
        await peffect_oil(otmp);
        break;
    case POT_ACID:
        await peffect_acid(otmp);
        break;
    case POT_POLYMORPH:
        await peffect_polymorph(otmp);
        break;
    default:
        return 0;
    }
    return -1;
}
// ── healup ──
// C ref: potion.c:1427 — healup calls make_blinded, make_deaf,
// make_vomiting, make_sick which each call SET_BOTL().
export async function healup(nhp, nxtra, curesick, cureblind) {
    if (nhp) {
        if (Upolyd()) {
            game.u.mh = (game.u.mh || 0) + nhp;
            if (game.u.mh > game.u.mhmax)
                game.u.mh = (game.u.mhmax = (game.u.mhmax || 0) + nxtra);
        } else {
            game.u.uhp = (game.u.uhp || 0) + nhp;
            if (game.u.uhp > game.u.uhpmax) {
                game.u.uhp = (game.u.uhpmax = (game.u.uhpmax || 0) + nxtra);
                if (game.u.uhpmax > (game.u.uhppeak || 0))
                    game.u.uhppeak = game.u.uhpmax;
            }
        }
    }
    if (cureblind) {
        game.u.ucreamed = 0;
        await make_blinded(0, true);
        await make_deaf(0, true);
    }
    if (curesick) {
        await make_vomiting(0, true);
        await make_sick(0, null, true, SICK_ALL);
    }
    setBotl('healup');
}
// ── strange_feeling ──
export async function strange_feeling_fn(obj, txt) {
    if (game.flags?.beginner || !txt)
        await You(`have a ${Hallucination() ? 'normal' : 'strange'} feeling for a moment, then it passes.`);
    else
        await pline(txt);
    if (!obj) return;
    if (obj.dknown) await trycall(obj);
    useup(obj);
}
// ── bottlename ──
const bottlenames = ['bottle', 'phial', 'flagon', 'carafe', 'flask', 'jar', 'vial'];
const hbottlenames = [
    'jug', 'pitcher', 'barrel', 'tin', 'bag', 'box', 'glass', 'beaker',
    'tumbler', 'vase', 'flowerpot', 'pan', 'thingy', 'mug', 'teacup',
    'teapot', 'keg', 'bucket', 'thermos', 'amphora', 'wineskin', 'parcel',
    'bowl', 'ampoule',
];
export function bottlename() {
    if (Hallucination())
        return ROLL_FROM(hbottlenames);
    else
        return ROLL_FROM(bottlenames);
}
// ── potionhit ──
export async function potionhit(mon, obj, how) {
    const botlnam = bottlename();
    const isyou = (mon === game.player);
    let distance, tx, ty;
    let saddle = null;
    let hit_saddle = false;
    const your_fault = (how <= POTHIT_HERO_THROW);
    if (isyou) {
        tx = game.u.ux; ty = game.u.uy;
        distance = 0;
        await pline_The(`${botlnam} crashes on your ${body_part(HEAD)} and breaks into shards.`);
        await losehp(Maybe_Half_Phys(rnd(2)),
            (how === POTHIT_OTHER_THROW) ? 'propelled potion' : 'thrown potion',
            KILLED_BY_AN);
    } else {
        tx = mon.mx; ty = mon.my;
        if (((mon.misc_worn_check & W_SADDLE) &&
             (saddle = which_armor(mon, W_SADDLE))) &&
            (!rn2(10) ||
             (obj.otyp === POT_WATER &&
              ((rn2(10) > 7 && obj.cursed) ||  // simplified from rnl
               (rn2(10) < 4 && obj.blessed) || !rn2(3)))))
            hit_saddle = true;
        distance = distu(tx, ty);
        if (!cansee(tx, ty)) {
            Soundeffect('se_potion_crash_and_break', 60);
            await pline('Crash!');
        } else {
            const mnam = mon_nam(mon);
            let buf;
            if (hit_saddle && saddle) {
                buf = `${s_suffix(mon_nam(mon))} saddle`;
            } else if (has_head(mon.data)) {
                buf = `${s_suffix(mnam)} ${game.notonhead ? 'body' : 'head'}`;
            } else {
                buf = mnam;
            }
            Soundeffect('se_potion_crash_and_break', 60);
            await pline_The(`${botlnam} crashes on ${buf} and breaks into shards.`);
        }
        if (rn2(5) && mon.mhp > 1 && !hit_saddle)
            mon.mhp--;
    }
    if (obj.otyp !== POT_OIL && !hit_saddle && cansee(tx, ty))
        await pline(`The ${xname(obj)} evaporates.`);
    if (isyou) {
        switch (obj.otyp) {
        case POT_OIL:
            if (obj.lamplit)
                await explode_oil(obj, game.u.ux, game.u.uy);
            break;
        case POT_POLYMORPH:
            await You_feel(`a little ${Hallucination() ? 'normal' : 'strange'}.`);
            if (!Unchanging_val() && !Antimagic())
                await polyself(POLY_NOFLAGS);
            break;
        case POT_ACID:
            if (!Acid_resistance()) {
                await pline(`This burns${obj.blessed ? ' a little' : obj.cursed ? ' a lot' : ''}!`);
                const dmg = c_d(obj.cursed ? 2 : 1, obj.blessed ? 4 : 8);
                await losehp(Maybe_Half_Phys(dmg), 'potion of acid', KILLED_BY_AN);
            }
            break;
        }
    } else if (hit_saddle && saddle) {
        // Saddle hit handling (simplified)
        const useeit = !Blind() && canseemon(mon) && cansee(tx, ty);
        let affected = false;
        if (obj.otyp === POT_WATER) {
            // H2Opotion_dip simplified
        }
        if (useeit && !affected)
            await pline(`${s_suffix(Monnam(mon))} ${aobjnam(saddle, 'get')} wet.`);
    } else {
        let angermon = your_fault;
        let cureblind = false;
        switch (obj.otyp) {
        case POT_FULL_HEALING:
            cureblind = true;
            // FALLTHROUGH
        case POT_EXTRA_HEALING:
            if (!obj.cursed) cureblind = true;
            // FALLTHROUGH
        case POT_HEALING:
            if (obj.blessed) cureblind = true;
            if (mon.data === mons[PM_PESTILENCE]) {
                // do_illness
                if (mon.mhp > 2) {
                    mon.mhp = Math.trunc(mon.mhp / 2);
                    if (canseemon(mon))
                        await pline(`${Monnam(mon)} looks rather ill.`);
                }
                break;
            }
            // FALLTHROUGH
        case POT_RESTORE_ABILITY:
        case POT_GAIN_ABILITY:
            angermon = false;
            if (mon.mhp < mon.mhpmax) {
                healmon(mon, mon.mhpmax, 0);
                if (canseemon(mon))
                    await pline(`${Monnam(mon)} looks sound and hale again.`);
            }
            if (cureblind) await mcureblindness(mon, canseemon(mon));
            break;
        case POT_SICKNESS:
            if (mon.data === mons[PM_PESTILENCE]) {
                // do_healing
                angermon = false;
                if (mon.mhp < mon.mhpmax) {
                    healmon(mon, mon.mhpmax, 0);
                    if (canseemon(mon))
                        await pline(`${Monnam(mon)} looks sound and hale again.`);
                }
                break;
            }
            if (dmgtype(mon.data, AD_DISE) || dmgtype(mon.data, AD_PEST)
                || resists_poison(mon)) {
                if (canseemon(mon))
                    await pline(`${Monnam(mon)} looks unharmed.`);
                break;
            }
            // do_illness
            if (mon.mhp > 2) {
                mon.mhp = Math.trunc(mon.mhp / 2);
                if (canseemon(mon))
                    await pline(`${Monnam(mon)} looks rather ill.`);
            }
            break;
        case POT_CONFUSION:
        case POT_BOOZE:
            if (!resist(mon, POTION_CLASS, 0, NOTELL))
                mon.mconf = true;
            break;
        case POT_INVISIBILITY: {
            const sawit = canspotmon(mon);
            const cursed_potion = !!obj.cursed;
            angermon = mon.minvis && cursed_potion;
            mon_set_minvis(mon, cursed_potion);
            if (sawit && !canspotmon(mon)) {
                if (cansee(mon.mx, mon.my))
                    map_invisible(mon.mx, mon.my);
            } else if (sawit && cursed_potion) {
                await pline(`${Monnam(mon)} briefly seems to be transparent.`);
            } else if (!sawit && canspotmon(mon)) {
                await pline(`${Monnam(mon)} appears!`);
            }
            break;
        }
        case POT_SLEEPING:
            if (sleep_monst(mon, rnd(12), POTION_CLASS)) {
                await pline(`${Monnam(mon)} falls asleep.`);
                await slept_monst(mon);
            }
            break;
        case POT_PARALYSIS:
            if (mon.mcanmove) {
                paralyze_monst(mon, rnd(25));
            }
            break;
        case POT_SPEED:
            angermon = false;
            await mon_adjust_speed(mon, 1, obj);
            break;
        case POT_BLINDNESS:
            if (haseyes(mon.data) && !mon_perma_blind(mon)) {
                let btmp = 64 + rn2(32) +
                    rn2(32) * (resist(mon, POTION_CLASS, 0, NOTELL) ? 0 : 1);
                btmp += (mon.mblinded || 0);
                mon.mblinded = Math.min(btmp, 127);
                mon.mcansee = 0;
            }
            break;
        case POT_WATER:
            if (mon_hates_blessings(mon) || is_were(mon.data) || is_vampshifter(mon)) {
                if (obj.blessed) {
                    await pline(`${Monnam(mon)} ${is_silent(mon.data) ? 'writhes' : 'shrieks'} in pain!`);
                    if (!is_silent(mon.data))
                        wake_nearto(tx, ty, (mon.data.mlevel || 0) * 10);
                    mon.mhp -= c_d(2, 6);
                    if (DEADMONSTER(mon))
                        await killed(mon);
                    else if (is_were(mon.data) && !is_human(mon.data))
                        await new_were(mon);
                } else if (obj.cursed) {
                    angermon = false;
                    if (canseemon(mon))
                        await pline(`${Monnam(mon)} looks healthier.`);
                    healmon(mon, c_d(2, 6), 0);
                    if (is_were(mon.data) && is_human(mon.data))
                        await new_were(mon);
                }
            } else if (mon.data === mons[PM_GREMLIN]) {
                angermon = false;
                await split_mon(mon, null);
            } else if (mon.data === mons[PM_IRON_GOLEM]) {
                if (canseemon(mon))
                    await pline(`${Monnam(mon)} rusts.`);
                mon.mhp -= c_d(1, 6);
                if (DEADMONSTER(mon))
                    await killed(mon);
            }
            break;
        case POT_OIL:
            if (obj.lamplit)
                await explode_oil(obj, tx, ty);
            break;
        case POT_ACID:
            if (!resists_acid(mon) && !resist(mon, POTION_CLASS, 0, NOTELL)) {
                await pline(`${Monnam(mon)} ${is_silent(mon.data) ? 'writhes' : 'shrieks'} in pain!`);
                if (!is_silent(mon.data))
                    wake_nearto(tx, ty, (mon.data.mlevel || 0) * 10);
                mon.mhp -= c_d(obj.cursed ? 2 : 1, obj.blessed ? 4 : 8);
                if (DEADMONSTER(mon)) {
                    if (your_fault)
                        await killed(mon);
                    else
                        await monkilled(mon, '', AD_ACID);
                }
            }
            break;
        case POT_POLYMORPH:
            await bhitm(mon, obj);
            break;
        }
        if (!DEADMONSTER(mon)) {
            if (angermon)
                wakeup(mon, true);
            else
                mon.msleeping = 0;
        }
    }
    if ((distance === 0 || (distance < 3 && !rn2(Math.trunc((1 + ACURR(A_DEX)) / 2))))
        && (!breathless(game.player?.data) || haseyes(game.player?.data)))
        await potionbreathe(obj);
    else if (obj.dknown && cansee(tx, ty))
        await trycall(obj);
    // Shop handling for unpaid potions
    if (game.u.ushops && obj.unpaid) {
        const shkp = shop_keeper(in_rooms(game.u.ux, game.u.uy, SHOPBASE)?.[0]);
        if (!shkp)
            obj.unpaid = 0;
        else if (game.context?.mon_moving)
            subfrombill(obj, shkp);
        else
            await stolen_value(obj, game.u.ux, game.u.uy, !!shkp.mpeaceful, false);
    }
    obfree(obj, null);
}
// ── potionbreathe ──
export async function potionbreathe(obj) {
    let i, ii, isdone, kn = 0;
    let cureblind = false;
    const already_in_use = obj.in_use;
    obj.in_use = 1;
    const otyp = Half_gas_damage() ? -1 /* TOWEL sentinel */ : obj.otyp;
    switch (otyp) {
    case -1: // TOWEL
        await pline('Some vapor passes harmlessly around you.');
        break;
    case POT_RESTORE_ABILITY:
    case POT_GAIN_ABILITY:
        if (obj.cursed) {
            if (!breathless(game.player?.data)) {
                await pline('Ulch!  That potion smells terrible!');
            } else if (haseyes(game.player?.data)) {
                let eyes = body_part(EYE);
                if (eyecount(game.player?.data) !== 1) eyes = makeplural(eyes);
                await Your(`${eyes} ${vtense(eyes, 'sting')}!`);
            }
            break;
        } else {
            i = rn2(A_MAX);
            for (isdone = 0, ii = 0; !isdone && ii < A_MAX; ii++) {
                if (ABASE(i) < AMAX(i)) {
                    game.u.acurr.a[i]++;
                    isdone = !(obj.blessed);
                    setBotl('potionbreathe');
                }
                if (++i >= A_MAX) i = 0;
            }
        }
        break;
    case POT_FULL_HEALING:
        if (Upolyd() && game.u.mh < game.u.mhmax)
            { game.u.mh++; setBotl('potionbreathe'); }
        if (game.u.uhp < game.u.uhpmax)
            { game.u.uhp++; setBotl('potionbreathe'); }
        cureblind = true;
        // FALLTHROUGH
    case POT_EXTRA_HEALING:
        if (Upolyd() && game.u.mh < game.u.mhmax)
            { game.u.mh++; setBotl('potionbreathe'); }
        if (game.u.uhp < game.u.uhpmax)
            { game.u.uhp++; setBotl('potionbreathe'); }
        if (!obj.cursed) cureblind = true;
        // FALLTHROUGH
    case POT_HEALING:
        if (Upolyd() && game.u.mh < game.u.mhmax)
            { game.u.mh++; setBotl('potionbreathe'); }
        if (game.u.uhp < game.u.uhpmax)
            { game.u.uhp++; setBotl('potionbreathe'); }
        if (obj.blessed) cureblind = true;
        if (cureblind) {
            await make_blinded(0, !game.u.ucreamed);
            await make_deaf(0, true);
        }
        exercise(A_CON, true);
        break;
    case POT_SICKNESS:
        if (!Role_if(PM_HEALER)) {
            if (Upolyd()) {
                if (game.u.mh <= 5) game.u.mh = 1;
                else game.u.mh -= 5;
            } else {
                if (game.u.uhp <= 5) game.u.uhp = 1;
                else game.u.uhp -= 5;
            }
            setBotl('potionbreathe');
            exercise(A_CON, false);
        }
        break;
    case POT_HALLUCINATION:
        await You('have a momentary vision.');
        break;
    case POT_CONFUSION:
    case POT_BOOZE:
        if (!Confusion())
            await You_feel('somewhat dizzy.');
        await make_confused(itimeout_incr(HConfusion(), rnd(5)), false);
        break;
    case POT_INVISIBILITY:
        if (!Blind() && !Invis()) {
            kn++;
            await pline(`For an instant you ${See_invisible() ? 'could see right through yourself' : "couldn't see yourself"}!`);
        }
        break;
    case POT_PARALYSIS:
        kn++;
        if (!Free_action_val()) {
            await pline(`${Something} seems to be holding you.`);
            nomul(-rnd(5));
            game.multi_reason = 'frozen by a potion';
            game.nomovemsg = You_can_move_again;
            exercise(A_DEX, false);
        } else {
            await You('stiffen momentarily.');
        }
        break;
    case POT_SLEEPING:
        kn++;
        if (!Free_action_val() && !Sleep_resistance()) {
            await You_feel('rather tired.');
            nomul(-rnd(5));
            game.multi_reason = 'sleeping off a magical draught';
            game.nomovemsg = You_can_move_again;
            exercise(A_DEX, false);
        } else {
            await You('yawn.');
            monstseesu(M_SEEN_SLEEP);
        }
        break;
    case POT_SPEED:
        if (!Fast_val())
            await Your('knees seem more flexible now.');
        incr_itimeout(FAST, rnd(5));
        exercise(A_DEX, true);
        break;
    case POT_BLINDNESS:
        if (!Blind() && !Unaware()) {
            kn++;
            await pline('It suddenly gets dark.');
        }
        await make_blinded(itimeout_incr(BlindedTimeout(), rnd(5)), false);
        if (!Blind() && !Unaware())
            await Your(vision_clears);
        break;
    case POT_WATER:
        if (game.u.umonnum === PM_GREMLIN) {
            await split_mon(game.player, null);
        } else if (ismnum(game.u.ulycn)) {
            if (obj.blessed && game.player?.data === mons[game.u.ulycn])
                await you_unwere(false);
            else if (obj.cursed && !Upolyd())
                await you_were();
        }
        break;
    case POT_ACID:
    case POT_POLYMORPH:
        exercise(A_CON, false);
        break;
    }
    if (!already_in_use) obj.in_use = 0;
    if (obj.dknown) {
        if (kn)
            makeknown(obj.otyp);
        else
            await trycall(obj);
    }
}
// ── mixtype ──
export function mixtype(o1, o2) {
    let o1typ = o1.otyp, o2typ = o2.otyp;
    if (o1.oclass === POTION_CLASS &&
        (o2typ === POT_GAIN_LEVEL || o2typ === POT_GAIN_ENERGY ||
         o2typ === POT_HEALING || o2typ === POT_EXTRA_HEALING ||
         o2typ === POT_FULL_HEALING || o2typ === POT_ENLIGHTENMENT ||
         o2typ === POT_FRUIT_JUICE)) {
        o1typ = o2.otyp;
        o2typ = o1.otyp;
    }
    switch (o1typ) {
    case POT_HEALING:
        if (o2typ === POT_SPEED) return POT_EXTRA_HEALING;
        // FALLTHROUGH
    case POT_EXTRA_HEALING:
    case POT_FULL_HEALING:
        if (o2typ === POT_GAIN_LEVEL || o2typ === POT_GAIN_ENERGY)
            return (o1typ === POT_HEALING) ? POT_EXTRA_HEALING
                : (o1typ === POT_EXTRA_HEALING) ? POT_FULL_HEALING
                : POT_GAIN_ABILITY;
        // FALLTHROUGH
    case UNICORN_HORN:
        switch (o2typ) {
        case POT_SICKNESS: return POT_FRUIT_JUICE;
        case POT_HALLUCINATION:
        case POT_BLINDNESS:
        case POT_CONFUSION: return POT_WATER;
        }
        break;
    case AMETHYST:
        if (o2typ === POT_BOOZE) return POT_FRUIT_JUICE;
        break;
    case POT_GAIN_LEVEL:
    case POT_GAIN_ENERGY:
        switch (o2typ) {
        case POT_CONFUSION: return (rn2(3) ? POT_BOOZE : POT_ENLIGHTENMENT);
        case POT_HEALING: return POT_EXTRA_HEALING;
        case POT_EXTRA_HEALING: return POT_FULL_HEALING;
        case POT_FULL_HEALING: return POT_GAIN_ABILITY;
        case POT_FRUIT_JUICE: return POT_SEE_INVISIBLE;
        case POT_BOOZE: return POT_HALLUCINATION;
        }
        break;
    case POT_FRUIT_JUICE:
        switch (o2typ) {
        case POT_SICKNESS: return POT_SICKNESS;
        case POT_ENLIGHTENMENT:
        case POT_SPEED: return POT_BOOZE;
        case POT_GAIN_LEVEL:
        case POT_GAIN_ENERGY: return POT_SEE_INVISIBLE;
        }
        break;
    case POT_ENLIGHTENMENT:
        switch (o2typ) {
        case POT_LEVITATION:
            if (rn2(3)) return POT_GAIN_LEVEL;
            break;
        case POT_FRUIT_JUICE: return POT_BOOZE;
        case POT_BOOZE: return POT_CONFUSION;
        }
        break;
    }
    return STRANGE_OBJECT;
}
// ── dopotion ──
export async function dopotion(otmp) {
    otmp.in_use = true;
    game.potion_nothing = 0;
    game.potion_unkn = 0;
    const retval = await peffects(otmp);
    if (retval >= 0)
        return retval ? ECMD_TIME : ECMD_OK;
    if (game.potion_nothing) {
        game.potion_unkn++;
        await You(`have a ${Hallucination() ? 'normal' : 'peculiar'} feeling for a moment, then it passes.`);
    }
    if (otmp.dknown && !game.objects[otmp.otyp].oc_name_known) {
        if (!game.potion_unkn) {
            makeknown(otmp.otyp);
            more_experienced(0, 10);
        } else {
            await trycall(otmp);
        }
    }
    useup(otmp);
    return ECMD_TIME;
}
// ── drink_ok ──
// C: drink_ok(struct obj *obj) - callback for getobj()
function drink_ok(obj) {
    if (!obj)
        return game.drink_ok_extra ? GETOBJ_EXCLUDE_NONINVENT : GETOBJ_EXCLUDE;
    if (obj.oclass === POTION_CLASS)
        return GETOBJ_SUGGEST;
    return GETOBJ_EXCLUDE;
}
// ── dodrink ──
export async function dodrink() {
    if (Strangled_val()) {
        await pline("If you can't breathe air, how can you drink liquid?");
        return ECMD_OK;
    }
    game.drink_ok_extra = 0;
    // C ref: potion.c:543-566 — fountain/sink/water prompts before getobj
    if (!game.iflags?.menu_requested) {
        const here = game.level?.at(game.u.ux, game.u.uy)?.typ ?? 0;
        if (IS_FOUNTAIN(here) && can_reach_floor(false)) {
            if ((await ynFunction('Drink from the fountain?', 'yn',
                    'n'.charCodeAt(0))) === 'y'.charCodeAt(0)) {
                await drinkfountain();
                return ECMD_TIME;
            }
            ++game.drink_ok_extra;
        }
        if (IS_SINK(here) && can_reach_floor(false)) {
            if ((await ynFunction('Drink from the sink?', 'yn',
                    'n'.charCodeAt(0))) === 'y'.charCodeAt(0)) {
                await drinksink();
                return ECMD_TIME;
            }
            ++game.drink_ok_extra;
        }
    }
    let otmp = await getobj('drink', drink_ok, GETOBJ_NOFLAGS);
    if (!otmp) return ECMD_CANCEL;
    // C 3.7: switch back to relying on useup() unless the object is
    // actually worn. Otherwise drinking a stack of unpaid potions
    // one by one in a shop makes each one a separate used-up item.
    if (otmp.owornmask) {
        if (otmp.quan > 1) {
            otmp = splitobj(otmp, 1);
            otmp.owornmask = 0; // rest of original stack is unaffected
        } else {
            await remove_worn_item(otmp, false);
        }
    }
    otmp.in_use = true; // you've opened the stopper
    if (objdescr_is(otmp, 'milky')
        && !((game.mvitals?.[PM_GHOST]?.mvflags || 0) & G_GONE)
        && !rn2(POTION_OCCUPANT_CHANCE(game.mvitals?.[PM_GHOST]?.born || 0))) {
        await ghost_from_bottle();
        useup(otmp);
        return ECMD_TIME;
    } else if (objdescr_is(otmp, 'smoky')
        && !((game.mvitals?.[PM_DJINNI]?.mvflags || 0) & G_GONE)
        && !rn2(POTION_OCCUPANT_CHANCE(game.mvitals?.[PM_DJINNI]?.born || 0))) {
        await djinni_from_bottle(otmp);
        useup(otmp);
        return ECMD_TIME;
    }
    return await dopotion(otmp);
}
// ── djinni_from_bottle ──
export async function djinni_from_bottle(obj) {
    const mtmp = await makemon(mons[PM_DJINNI], game.u.ux, game.u.uy, MM_NOMSG);
    if (!mtmp) {
        await pline('It turns out to be empty.');
        return;
    }
    if (!Blind()) {
        await pline(`In a cloud of smoke, ${a_monnam(mtmp)} emerges!`);
        await pline(`${Monnam(mtmp)} speaks.`);
    } else {
        await You('smell acrid fumes.');
        await pline(`${Something} speaks.`);
    }
    let chance = rn2(5);
    if (obj.blessed)
        chance = (chance === 4) ? rnd(4) : 0;
    else if (obj.cursed)
        chance = (chance === 0) ? rn2(4) : 4;
    SetVoice(mtmp, 0, 80, 0);
    switch (chance) {
    case 0:
        await verbalize('I am in your debt.  I will grant one wish!');
        // mongrantswish(&mtmp);
        break;
    case 1:
        await verbalize('Thank you for freeing me!');
        await tamedog(mtmp, null, false);
        break;
    case 2:
        await verbalize('You freed me!');
        mtmp.mpeaceful = true;
        set_malign(mtmp);
        break;
    case 3:
        await verbalize('It is about time!');
        if (canspotmon(mtmp))
            await pline(`${Monnam(mtmp)} vanishes.`);
        mongone(mtmp);
        break;
    default:
        await verbalize('You disturbed me, fool!');
        mtmp.mpeaceful = false;
        set_malign(mtmp);
        break;
    }
}
// ── split_mon ──
export async function split_mon(mon, mtmp) {
    let mtmp2;
    let reason = '';
    if (mtmp)
        reason = ` from ${mtmp === game.player ? 'your' : s_suffix(mon_nam(mtmp))} heat`;
    if (mon === game.player) {
        if (game.u.mh > game.u.mhmax) game.u.mh = game.u.mhmax;
        mtmp2 = (game.u.mh > 1) ? await cloneu() : null;
        if (mtmp2) {
            mtmp2.mhpmax = Math.trunc(game.u.mhmax / 2);
            game.u.mhmax -= mtmp2.mhpmax;
            setBotl('split_mon');
            await You(`multiply${reason}!`);
        }
    } else {
        if (mon.mhp > mon.mhpmax) mon.mhp = mon.mhpmax;
        mtmp2 = (mon.mhp > 1) ? clone_mon(mon, 0, 0) : null;
        if (mtmp2) {
            mtmp2.mhpmax = Math.trunc(mon.mhpmax / 2);
            mon.mhpmax -= mtmp2.mhpmax;
            if (canspotmon(mon))
                await pline(`${Monnam(mon)} multiplies${reason}!`);
        }
    }
    return mtmp2;
}
// ── speed_up ──
export async function speed_up(duration) {
    if (!Very_fast())
        await You(`are suddenly moving ${Fast_val() ? '' : 'much '}faster.`);
    else
        await Your(`${makeplural(body_part(LEG))} get new energy.`);
    exercise(A_DEX, true);
    incr_itimeout(FAST, duration);
}
// ── H2Opotion_dip ──
async function H2Opotion_dip(potion, targobj, useeit, objphrase) {
    const COST_alter = -2;
    const COST_none = -1;
    let func = null;
    let glowcolor = null;
    let costchange = COST_none;
    let altfmt = false;
    let res = false;
    if (!potion || potion.otyp !== POT_WATER) return false;
    if (potion.blessed) {
        if (targobj.cursed) {
            func = uncurse;
            glowcolor = NH_AMBER;
            costchange = COST_UNCURS;
        } else if (!targobj.blessed) {
            func = bless;
            glowcolor = NH_LIGHT_BLUE;
            costchange = COST_alter;
            altfmt = true;
        }
    } else if (potion.cursed) {
        if (targobj.blessed) {
            func = unbless;
            glowcolor = 'brown';
            costchange = COST_UNBLSS;
        } else if (!targobj.cursed) {
            func = curse;
            glowcolor = NH_BLACK;
            costchange = COST_alter;
            altfmt = true;
        }
    } else {
        // dipping into uncursed water; carried() check skips steed saddle
        if (carried(targobj)) {
            game.mentioned_water = false;
            if (await water_damage(targobj, 0, true) !== ER_NOTHING)
                res = true;
            if (game.mentioned_water)
                makeknown(POT_WATER);
            game.mentioned_water = false;
        }
    }
    if (func) {
        if (useeit) {
            glowcolor = hcolor(glowcolor);
            if (altfmt)
                await pline(`${objphrase} with ${an(glowcolor)} aura.`);
            else
                await pline(`${objphrase} ${glowcolor}.`);
            targobj.bknown = !Hallucination();
        } else {
            if (!potion.bknown || !potion.dknown)
                targobj.bknown = 0;
        }
        // potions of water are the only shop goods whose price depends
        // on their curse/bless state
        if (targobj.unpaid && targobj.otyp === POT_WATER) {
            if (costchange === COST_alter)
                alter_cost(targobj, 0);
            else if (costchange !== COST_none)
                costly_alteration(targobj, costchange);
        }
        await func(targobj);
        res = true;
    }
    return res;
}
// ── impact_arti_light ──
export async function impact_arti_light(obj, worsen, seeit) {
    if ((worsen ? obj.cursed : obj.blessed) || obj_resists(obj, 25, 75))
        return;
    const otmp = mksobj(POT_WATER, true, false);
    if (worsen)
        curse(otmp);
    else
        bless(otmp);
    await H2Opotion_dip(otmp, obj, seeit, seeit ? Yobjnam2(obj, 'glow') : '');
    obfree(otmp, null);
}
// ── poof ──
async function poof(potion) {
    if (potion.dknown)
        await trycall(potion);
    useup(potion);
}
// ── dip_potion_explosion ──
async function dip_potion_explosion(obj, dmg) {
    if (obj.cursed || obj.otyp === POT_ACID
        || (obj.otyp === POT_OIL && obj.lamplit)
        || !rn2((game.u.uarmc && game.u.uarmc.otyp === ALCHEMY_SMOCK) ? 30 : 10)) {
        obj.in_use = 1;
        await pline(`${!Deaf() ? 'BOOM!  ' : ''}They explode!`);
        wake_nearto(game.u.ux, game.u.uy, (BOLT_LIM + 1) * (BOLT_LIM + 1));
        exercise(A_STR, false);
        if (!breathless(game.player?.data) || haseyes(game.player?.data))
            await potionbreathe(obj);
        useupall(obj);
        await losehp(dmg, 'alchemic blast', KILLED_BY_AN);
        return true;
    }
    return false;
}
// ── hold_potion ──
async function hold_potion(potobj, drop_fmt, drop_arg, hold_msg) {
    const cap = near_capacity();
    const save_pickup_burden = game.flags.pickup_burden || 0;
    if ((game.flags.pickup_burden || 0) < cap)
        game.flags.pickup_burden = cap;
    obj_extract_self(potobj);
    potobj = await hold_another_object(potobj, drop_fmt, drop_arg, hold_msg);
    game.flags.pickup_burden = save_pickup_burden;
    update_inventory();
}
// ── dip_ok / dip_hands_ok ──
async function dip_ok(obj) {
    if (!obj)
        return GETOBJ_DOWNPLAY;
    if (obj.oclass === COIN_CLASS)
        return GETOBJ_EXCLUDE;
    if (await inaccessible_equipment(obj, null, false))
        return GETOBJ_EXCLUDE_INACCESS;
    return GETOBJ_SUGGEST;
}
async function dip_hands_ok(obj) {
    if (!obj && (Glib_val() && can_reach_floor(false)))
        return GETOBJ_SUGGEST;
    return await dip_ok(obj);
}
// ── potion_dip ──
async function potion_dip(obj, potion) {
    let singlepotion;
    let mixture;
    if (potion === obj && potion.quan === 1) {
        await pline('That is a potion bottle, not a Klein bottle!');
        return ECMD_OK;
    }
    if (obj === hands_obj) {
        await You(`can't fit your ${body_part(HAND)} into the mouth of the bottle!`);
        return ECMD_OK;
    }
    obj.pickup_prev = 0;
    potion.in_use = true;
    if (potion.otyp === POT_WATER) {
        const useeit = !Blind() || (obj === game.u.ublindf && Blindfolded_only());
        const obj_glows = Yobjnam2(obj, 'glow');
        if (await H2Opotion_dip(potion, obj, useeit, obj_glows)) {
            await poof(potion);
            return ECMD_TIME;
        }
    } else if (obj.otyp === POT_POLYMORPH || potion.otyp === POT_POLYMORPH) {
        if (obj_unpolyable(obj.otyp === POT_POLYMORPH ? potion : obj)) {
            await pline(nothing_happens);
        } else {
            const save_otyp = obj.otyp;
            if (!game.u.uconduct) game.u.uconduct = {};
            if (!game.u.uconduct.polypiles++)
                livelog_printf(LL_CONDUCT, `polymorphed ${uhis()} first item`);
            obj = poly_obj(obj, STRANGE_OBJECT);
            if (!obj) {
                makeknown(POT_POLYMORPH);
                return ECMD_TIME;
            } else if (obj.otyp !== save_otyp) {
                makeknown(POT_POLYMORPH);
                useup(potion);
                await prinv(null, obj, 0);
                return ECMD_TIME;
            } else {
                await pline(nothing_seems_to_happen);
                await poof(potion);
                return ECMD_TIME;
            }
        }
        potion.in_use = false;
        return ECMD_TIME;
    } else if (obj.oclass === POTION_CLASS && obj.otyp !== potion.otyp) {
        let amt = obj.quan;
        let magic;
        mixture = mixtype(obj, potion);
        magic = (mixture !== STRANGE_OBJECT) ? game.objects[mixture].oc_magic
            : (game.objects[obj.otyp].oc_magic || game.objects[potion.otyp].oc_magic);
        let qbuf = 'The';
        if (amt > (obj.odiluted ? 2 : magic ? 3 : 7)) {
            if (obj.odiluted)
                amt = 2;
            else if (magic)
                amt = rnd(Math.min(amt, 8) - 2) + 2;
            else
                amt = rnd(amt - 6) + 6;
            if (amt < obj.quan) {
                obj = splitobj(obj, amt);
                qbuf = `${obj.quan} of the`;
            }
        }
        await pline(`${qbuf} ${simpleonames(obj)} ${otense(obj, 'mix')} with ${potion.quan > 1 ? 'one of ' : ''}${thesimpleoname(potion)}...`);
        useup(potion);
        if (await dip_potion_explosion(obj, amt + rnd(9)))
            return ECMD_TIME;
        obj.blessed = obj.cursed = obj.bknown = 0;
        if (Blind() || Hallucination())
            obj.dknown = 0;
        if (mixture !== STRANGE_OBJECT) {
            obj.otyp = mixture;
        } else {
            let otmp;
            switch (obj.odiluted ? 1 : rnd(8)) {
            case 1:
                obj.otyp = POT_WATER;
                break;
            case 2:
            case 3:
                obj.otyp = POT_SICKNESS;
                break;
            case 4:
                otmp = mkobj(POTION_CLASS, false);
                obj.otyp = otmp.otyp;
                if (obj.otyp === POT_OIL || otmp.otyp === POT_OIL)
                    fixup_oil(obj, otmp);
                obfree(otmp, null);
                break;
            default:
                useupall(obj);
                await pline_The(`mixture ${!Blind() ? 'glows brightly and ' : ''}evaporates.`);
                return ECMD_TIME;
            }
        }
        obj.odiluted = (obj.otyp !== POT_WATER) ? 1 : 0;
        if (obj.otyp === POT_WATER && !Hallucination()) {
            await pline_The(`mixture bubbles${Blind() ? '' : ', then clears'}.`);
        } else if (!Blind()) {
            await pline_The(`mixture looks ${hcolor(OBJ_DESCR(game.objects[obj.otyp]))}.`);
        }
        freeinv(obj);
        await hold_potion(obj, 'You drop %s!', doname(obj), null);
        return ECMD_TIME;
    }
    if (potion.otyp === POT_ACID && obj.otyp === CORPSE
        && obj.corpsenm === PM_LICHEN) {
        await pline(`${The(cxname(obj))} ${otense(obj, 'turn')} ${Blind() ? 'wrinkled' : potion.odiluted ? hcolor(NH_ORANGE) : hcolor(NH_RED)} around the edges.`);
        potion.in_use = false;
        if (potion.dknown)
            await trycall(potion);
        return ECMD_TIME;
    }
    if (potion.otyp === POT_WATER && obj.otyp === TOWEL) {
        await pline_The('towel soaks it up!');
        await poof(potion);
        return ECMD_TIME;
    }
    if (is_poisonable(obj)) {
        if (potion.otyp === POT_SICKNESS && !obj.opoisoned) {
            let buf;
            if (potion.quan > 1)
                buf = `One of ${The(xname(potion))}`;
            else
                buf = The(xname(potion));
            await pline(`${buf} forms a coating on ${The(xname(obj))}.`);
            obj.opoisoned = true;
            await poof(potion);
            return ECMD_TIME;
        } else if (obj.opoisoned && !permapoisoned(obj)
                   && (potion.otyp === POT_HEALING
                       || potion.otyp === POT_EXTRA_HEALING
                       || potion.otyp === POT_FULL_HEALING)) {
            await pline(`A coating wears off ${The(xname(obj))}.`);
            obj.opoisoned = 0;
            await poof(potion);
            return ECMD_TIME;
        }
    }
    if (potion.otyp === POT_ACID) {
        if (await erode_obj(obj, 0, ERODE_CORRODE, EF_GREASE) !== ER_NOTHING) {
            await poof(potion);
            return ECMD_TIME;
        }
    }
    if (potion.otyp === POT_OIL) {
        let wisx = false;
        if (potion.lamplit) {
            await fire_damage(obj, true, game.u.ux, game.u.uy);
        } else if (potion.cursed) {
            await pline_The(`potion spills and covers your ${fingers_or_gloves(true)} with oil.`);
            make_glib((Glib_val() & TIMEOUT) + c_d(2, 10));
        } else if (obj.oclass !== WEAPON_CLASS && !is_weptool(obj)) {
            // the following cases apply only to weapons - fall through to more_dips
        } else if ((!is_rustprone(obj) && !is_corrodeable(obj))
                   || is_ammo(obj) || (!obj.oeroded && !obj.oeroded2)) {
            if (!Blind())
                await pline(`${Yname2(obj)} ${otense(obj, 'gleam')} with an oily sheen.`);
            else
                await pline(`${Yname2(obj)} ${otense(obj, 'feel')} oily.`);
        } else {
            await pline(`${Yname2(obj)} ${otense(obj, !Blind() ? 'are' : 'feel')} less ${(obj.oeroded && obj.oeroded2) ? 'corroded and rusty' : obj.oeroded ? 'rusty' : 'corroded'}.`);
            if (obj.oeroded > 0)
                obj.oeroded--;
            if (obj.oeroded2 > 0)
                obj.oeroded2--;
            wisx = true;
        }
        // Check if we should skip to more_dips
        if (!(potion.lamplit || potion.cursed) && obj.oclass !== WEAPON_CLASS && !is_weptool(obj)) {
            // fall through to lamp/more_dips below
        } else {
            exercise(A_WIS, wisx);
            if (potion.dknown)
                makeknown(potion.otyp);
            useup(potion);
            return ECMD_TIME;
        }
    }
    // more_dips:
    // Allow filling of MAGIC_LAMPs to prevent identification by player
    if ((obj.otyp === OIL_LAMP || obj.otyp === MAGIC_LAMP)
        && (potion.otyp === POT_OIL)) {
        if (obj.lamplit || potion.lamplit) {
            useup(potion);
            await explode(game.u.ux, game.u.uy, 11, c_d(6, 6), 0, EXPL_FIERY);
            exercise(A_WIS, false);
            return ECMD_TIME;
        }
        if ((obj.otyp === MAGIC_LAMP) && obj.spe === 0) {
            obj.otyp = OIL_LAMP;
            obj.age = 0;
        }
        if (obj.age > 1000) {
            await pline(`${Yname2(obj)} ${otense(obj, 'are')} full.`);
            potion.in_use = false;
        } else {
            await You(`fill ${yname(obj)} with oil.`);
            check_unpaid(potion);
            obj.age += Math.trunc((!potion.odiluted ? 4 : 3) * potion.age / 2);
            if (obj.age > 1500)
                obj.age = 1500;
            useup(potion);
            exercise(A_WIS, true);
        }
        if (potion.dknown)
            makeknown(POT_OIL);
        obj.spe = 1;
        update_inventory();
        return ECMD_TIME;
    }
    // unicorn horn / amethyst dip
    potion.in_use = false;
    if ((obj.otyp === UNICORN_HORN || obj.otyp === AMETHYST)
        && (mixture = mixtype(obj, potion)) !== STRANGE_OBJECT) {
        const old_otyp = potion.otyp;
        let old_dknown = false;
        const more_than_one = potion.quan > 1;
        let oldbuf = '';
        if (potion.dknown) {
            old_dknown = true;
            oldbuf = `${hcolor(OBJ_DESCR(game.objects[potion.otyp]))} `;
        }
        if (potion.quan > 1) {
            singlepotion = splitobj(potion, 1);
        } else {
            singlepotion = potion;
        }
        costly_alteration(singlepotion, COST_NUTRLZ);
        singlepotion.otyp = mixture;
        singlepotion.blessed = 0;
        if (mixture === POT_WATER)
            singlepotion.cursed = singlepotion.odiluted = 0;
        else
            singlepotion.cursed = obj.cursed;
        singlepotion.bknown = false;
        singlepotion.dknown = false;
        if (!Blind()) {
            if (!Hallucination())
                observe_object(singlepotion);
            let newbuf = '';
            if (mixture === POT_WATER && singlepotion.dknown)
                newbuf = 'clears';
            else if (!Blind())
                newbuf = `turns ${hcolor(OBJ_DESCR(game.objects[mixture]))}`;
            if (newbuf)
                await pline_The(`${oldbuf}potion${more_than_one ? ' that you dipped into' : ''} ${newbuf}.`);
            else
                await pline('Something happens.');
            if (old_dknown
                && !game.objects[old_otyp].oc_name_known
                && !game.objects[old_otyp].oc_uname) {
                const fakeobj = {
                    dknown: 1,
                    otyp: old_otyp,
                    oclass: POTION_CLASS,
                };
                await docall(fakeobj);
            }
        }
        await hold_potion(singlepotion, 'You juggle and drop %s!',
            doname(singlepotion), null);
        return ECMD_TIME;
    }
    await pline('Interesting...');
    return ECMD_TIME;
}
// ── dodip ──
export async function dodip() {
    const here = levl(game.u.ux, game.u.uy)?.typ || 0;
    const at_pool = is_pool(game.u.ux, game.u.uy);
    const at_fountain = IS_FOUNTAIN(here);
    const at_sink = IS_SINK(here);
    const at_here = (!game.iflags?.menu_requested
                     && (at_pool || at_fountain || at_sink));
    let obj = await getobj('dip', at_here ? dip_hands_ok : dip_ok, GETOBJ_PROMPT);
    if (!obj) return ECMD_CANCEL;
    if (await inaccessible_equipment(obj, 'dip', false))
        return ECMD_OK;
    const is_hands = (obj === hands_obj);
    const shortestname = (is_hands || is_plural(obj) || pair_of(obj)) ? 'them' : 'it';
    game.drink_ok_extra = 0;
    let obuf;
    if (is_hands) {
        obuf = `your ${makeplural(body_part(HAND))}`;
    } else {
        obuf = short_oname(obj, doname, thesimpleoname, 49);
    }
    if (!game.iflags?.menu_requested) {
        if (!can_reach_floor(false)) {
            // can't dip if can't reach
        } else if (at_fountain) {
            if (await y_n(`Dip ${game.flags.verbose ? obuf : shortestname} into the fountain?`) === 'y') {
                if (!is_hands)
                    obj.pickup_prev = 0;
                await dipfountain(obj);
                return ECMD_TIME;
            }
            ++game.drink_ok_extra;
        } else if (at_sink) {
            if (await y_n(`Dip ${game.flags.verbose ? obuf : shortestname} into the sink?`) === 'y') {
                if (!is_hands)
                    obj.pickup_prev = 0;
                await dipsink(obj);
                return ECMD_TIME;
            }
            ++game.drink_ok_extra;
        } else if (at_pool) {
            const pooltype = waterbody_name(game.u.ux, game.u.uy);
            if (await y_n(`Dip ${game.flags.verbose ? obuf : shortestname} into the ${pooltype}?`) === 'y') {
                if (Levitation()) {
                    await floating_above(pooltype);
                } else if (game.u.usteed && !is_swimmer(game.u.usteed.data)
                           && P_SKILL(P_RIDING) < P_BASIC) {
                    await rider_cant_reach();
                } else if (is_hands || obj === game.u.uarmg) {
                    if (!is_hands)
                        obj.pickup_prev = 0;
                    await wash_hands();
                } else {
                    obj.pickup_prev = 0;
                    if (obj.otyp === POT_ACID)
                        obj.in_use = 1;
                    if (await water_damage(obj, 0, true) !== ER_DESTROYED
                        && obj.in_use)
                        useup(obj);
                }
                return ECMD_TIME;
            }
            ++game.drink_ok_extra;
        }
    }
    const qbuf = `dip ${game.flags.verbose ? obuf : shortestname} into`;
    const potion = await getobj(qbuf, drink_ok, GETOBJ_NOFLAGS);
    if (!potion) return ECMD_CANCEL;
    return await potion_dip(obj, potion);
}
// ── dip_into ──
export async function dip_into() {
    if (!cmdq_peek(CQ_CANNED)) {
        // impossible("dip_into: where is potion?");
        return ECMD_CANCEL;
    }
    game.drink_ok_extra = 0;
    const potion = await getobj('dip', drink_ok, GETOBJ_NOFLAGS);
    if (!potion || potion.oclass !== POTION_CLASS)
        return ECMD_CANCEL;
    const qbuf = `dip into ${is_plural(potion) ? 'one of ' : ''}${thesimpleoname(potion)}`;
    const obj = await getobj(qbuf, dip_ok, GETOBJ_PROMPT);
    if (!obj) return ECMD_CANCEL;
    if (await inaccessible_equipment(obj, 'dip', false))
        return ECMD_OK;
    return await potion_dip(obj, potion);
}
// ── mongrantswish ──
export async function mongrantswish(monp) {
    const mon = monp.mtmp || monp;
    if (!mon) return;
    const mx = mon.mx, my = mon.my;
    const gly = glyph_at(mx, my);
    mongone(mon);
    if (monp.mtmp !== undefined) monp.mtmp = null;
    await tmp_at(DISP_ALWAYS, gly);
    await tmp_at(mx, my);
    await makewish();
    await tmp_at(DISP_END, 0);
}
/*potion.c*/
export function init_potion_globals() {
    game.drink_ok_extra = 0;
}