All files / js pray.js

39.22% Statements 806/2055
33.86% Branches 106/313
54.41% Functions 37/68
39.22% Lines 806/2055

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 205673x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 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 30x   6x 36x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 11x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x   73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 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 1x 1x 1x 1x       1x 73x 73x 73x 73x 1x 1x 1x       1x 1x 73x 73x 73x 73x 73x 73x           73x 73x 73x         73x 73x 73x 73x 73x 1x 1x 1x 1x 1x 1x 73x 73x 73x 73x         73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 7x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 73x 73x 14x 14x 14x 14x 42x 42x 126x 112x 112x 112x 112x 126x 126x 126x 42x 14x 14x 73x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x           14x 14x 14x 14x 14x 11x 14x 14x 14x 14x       14x 14x 14x 14x 11x 14x 14x 14x 14x 14x 14x 11x 14x 73x 11x 11x 11x 11x       11x   11x   11x   11x   11x   11x 11x   11x   11x   11x   11x   11x   11x   11x   11x   11x 11x 118x 118x 118x 118x 11x 11x 11x 73x                             73x                                                                                                                                                                                                                                                                                                                                                                                                     73x                                                                                                                                         73x           73x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 3x 3x 2x 2x 2x 2x 2x 2x 2x 3x           3x 3x 3x             3x 3x             3x       3x 3x 3x 3x 3x 73x                   73x                                                                                                                                                                                                                                                                       73x                                                                                   73x 4x 4x 4x 4x 4x 4x     4x     4x   4x 4x 4x 4x 4x 4x 4x 4x 4x 4x   4x 4x         4x     4x 3x 3x 3x 4x 1x 1x 4x   4x 4x 4x                                                                                                                                                                                                                                                                                                       4x 4x 4x 4x 4x 4x             4x 73x                                                     73x 2x 2x 2x   2x 2x 2x 2x 2x 73x       73x 3x 3x 3x 3x     3x 3x 73x 1x 1x                       1x   1x 1x 1x 1x 1x     1x 1x 73x               73x 73x                       73x                                                                                 73x               73x                                         73x 1x 1x 1x                                     1x 1x 1x 1x                                 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 73x                                                                                                                                         73x                                                                     73x 1x 1x 1x 1x 1x 1x 1x 1x 1x 73x 73x 4x 4x 4x 4x 4x 3x 3x 4x       1x 1x 1x 1x                 1x       1x 1x 1x 1x     4x 73x 1x 1x 1x 1x 1x 1x       1x                                   1x 1x 73x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x                   1x 1x       1x       1x       1x 1x 1x 1x                                                                                                                               1x 73x 73x 7x 7x 7x 7x 7x 7x         7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x                 7x 7x 7x 7x 7x 7x 73x                                   73x 73x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 5x 5x 4x 4x 4x 4x 4x 4x 4x 5x 7x 7x 7x 7x 7x 4x 4x 4x 4x 7x 7x 73x 7x 7x 7x 7x 7x                 7x                     7x           7x 3x 3x 3x 3x 3x 7x       4x               4x 4x       4x 4x 7x 7x 73x 1x 1x 1x 1x     1x                                                                   1x 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 73x 73x                       73x 73x 1x 1x 73x 73x 1x 1x 1x 1x 73x 73x 2x 2x 73x 73x 126x 126x 126x     126x 33x 33x 126x 68x 68x 126x 25x 25x 126x     126x 126x 126x 126x 73x 73x 1x             1x 1x 1x   1x 1x 1x 1x 1x 1x 1x       1x 73x 73x                       73x 73x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x 73x 62x 62x 62x 62x 62x       62x 62x 62x 62x 62x 62x   62x 62x 62x 62x         62x 73x 73x 73x 72x 72x 72x  
// pray.js — Port of pray.c
// Prayer, sacrifice, and divine intervention logic.
import { game } from './gstate.js';
import { rn2, rn2_on_display_rng, rnd, rn1, rnl, rnz, c_d } from './rng.js';
import { roles } from './roles.js';
import { pline, You, Your, You_feel, You_hear, You_cant, pline_The, verbalize } from './pline.js';
import { paranoid_query, y_n, ynFunction } from './input.js';
import { Antimagic, Blind, BlindedTimeout, Blindfolded, Cold_resistance, Confusion, Deaf, Disint_resistance, Fire_resistance, Fixed_abil, Flying, Glib, Hallucination, HBlinded, HConfusion, HHallucination, HStun, Inhell, Is_astralevel, Is_sanctum, Levitation, Luck, Passes_walls, Poison_resistance, Punished, Reflecting, Role_if, See_invisible, Shock_resistance, Sick, Sleep_resistance, Slimed, Stoned, Strangled, Stunned, Unchanging, Underwater, Upolyd, Wounded_legs } from './macros.js';
import { levl, m_at } from './map_access.js';
import { ABASE, ACURR, AMAX, adjalign, adjattrib, exercise } from './attrib.js';
import { display_nhwindow, newsym, setBotl, shieldeff } from './display.js';
import {
    mons,
    PM_CLERIC, PM_KNIGHT, PM_WIZARD, PM_MONK, PM_WRAITH,
    PM_ACID_BLOB,
    S_HUMAN, S_LICH, S_GHOST, S_VAMPIRE, S_WRAITH, S_MUMMY, S_ZOMBIE,
    S_UNICORN,
} 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, ACH_TUNE, Align2amask, ALTAR, AM_CHAOTIC, AM_MASK, AM_SANCTUM, AM_SHRINE, Amask2align, ARM, ASCENDED, BLINDED, BOLT_LIM, COLD_RES, DEAF, DIED, DISSOLVED, ECMD_OK, ECMD_TIME, ESCAPED, EXT_ENCUMBER, EYE, FACE, FAST, FIRE_RES, FOOT, FROMOUTSIDE, HAND, HEAD, HUNGRY, HVY_ENCUMBER, INTRINSIC, IS_ALTAR, IS_OBSTRUCTED, ismnum, isok, KILLED_BY, KILLED_BY_AN, LARGEST_INT, LUCKMAX, M_AP_FURNITURE, M_AP_TYPE, MAXULEV, MM_NOMSG, NON_PM, nothing_happens, OMONST, P_BROAD_SWORD, P_ISRESTRICTED, P_LONG_SWORD, POISON_RES, PROTECTION, SCORR, SDOOR, SEE_INVIS, SHOCK_RES, SLEEP_RES, STEALTH, STOMACH, TELEPAT, TIMEOUT, TT_BURIEDBALL, TT_LAVA, W_ARM, W_ARMC, W_ARMS, W_SADDLE, WEAK } from './const.js';
import { S_altar } from './symbols.js';
import { AMULET_OF_STRANGULATION, AMULET_OF_YENDOR, BOULDER, COIN_CLASS, CORPSE, FAKE_AMULET_OF_YENDOR, FOOD_CLASS, FUMBLE_BOOTS, GAUNTLETS_OF_FUMBLING, has_omonst, HELM_OF_OPPOSITE_ALIGNMENT, LEVITATION_BOOTS, LOADSTONE, LONG_SWORD, LUCKSTONE, MAGIC_MARKER, POT_WATER, POTION_CLASS, RIN_LEVITATION, RIN_SUSTAIN_ABILITY, RUNESWORD, SADDLE, SPBOOK_CLASS, SPBOOK_no_NOVEL, SPE_BLANK_PAPER, SPE_FINGER_OF_DEATH, SPE_RESTORE_ABILITY, SPE_TURN_UNDEAD, STATUE, STRANGE_OBJECT, WEAPON_CLASS } from './objects.js';
import { ART_EXCALIBUR, ART_VORPAL_BLADE, ART_STORMBRINGER, SPFX_LUCK } from './artifacts.js';
import { losexp, pluslvl } from './exper.js';
import { makemon } from './makemon.js';
import { bless, eaten_stat, mkobj, mksobj, obfree, place_object, rnd_class, set_bknown, sobj_at, uncurse } from './mkobj.js';
import { Yobjnam2, an, ansimpleoname, gloves_simple_name, makeplural, otense, vtense, xname, yname } from './objnam.js';
import { hcolor, oname, oname_deferred_untwoweapon } from './do_name.js';
import { is_art, ROLL_FROM, s_suffix, sgn, Sprintf, upstart } from './hacklib.js';
import { cansee, couldsee } from './vision.js';
import { add_weapon_skill, weapon_type } from './weapon.js';
import { nomul } from './cmd.js';
import { encumber_msg, near_capacity } from './pickup.js';
import { attacktype_fordmg, can_chant, is_demon, is_human, is_rider, is_undead, is_unicorn, is_vampshifter, is_weptool, mdistu, monstseesu, monstunseesu, nohands, resists_disint, resists_elec, throws_rocks, your_race } from './mondata.js';
import { carried, carrying, makeknown, useup, observe_object } from './invent.js';
import { Monnam, a_monnam, aggravate, mon_nam, monflee, revive, see_monsters, uhim, uhis, update_inventory, wake_nearby } from './mon.js';
// observe_object: moved to invent.js import below
import { set_itimeout } from './timeout.js';
import { make_blinded, make_sick, make_glib } from './mhitu.js';
import { change_luck, done, killed, livelog_printf, losehp } from './hack.js';
import { resist } from './zap.js';
import { angry_priest, findpriest, p_coaligned, temple_occupied } from './priest.js';
import { summon_minion, dlord } from './minion.js';
import { which_armor } from './worn.js';
import { welded } from './wield.js';
import { is_lava, is_pool, is_pool_or_lava } from './dbridge.js';
import { make_slimed, make_stoned, make_deaf, make_stunned, make_confused, make_hallucinated } from './potion.js';
import { set_malign } from './dog.js';
import { SetVoice, Soundeffect } from './sounds.js';
import { reset_utrap } from './trap.js';
import { attrcurse, rndcurse } from './sit.js';
import { unpunish } from './read.js';
import { xlev_to_rank } from './botl.js';
import { buried_ball_to_freedom } from './dig.js';
import { disintegrate_arm, stuck_ring, unchanger } from './do_wear.js';
import { dropy, heal_legs, revive_corpse } from './do.js';
import { freehand } from './engrave.js';
import { record_achievement } from './insight.js';
import { ureflects } from './muse.js';
import { body_part, mbodypart, rehumanize, setuhpmax } from './polyself.js';
import { region_danger, region_safety } from './region.js';
import { force_learn_spell, known_spell, spell_skilltype, spelleffects } from './spell.js';
import { safe_teleds } from './teleport.js';
import { xkilled } from './uhitm.js';
import { you_unwere } from './were.js';
import { artiname, exist_artifact, mk_artifact, nartifact_exist, spec_ability, u_wield_art } from './artifact.js';
import { Sokoban } from './mklev.js';
import { floorfood, init_uhunger, useupf } from './eat.js';
import { uchangealign } from './role.js';
// ── Constants ──
const Moloch = 'Moloch';
const godvoices = ['booms out', 'thunders', 'rings out', 'booms'];
const PIOUS = 20;
const DEVOUT = 14;
const FERVENT = 9;
const STRIDENT = 4;
const TROUBLE_STONED = 14;
const TROUBLE_SLIMED = 13;
const TROUBLE_STRANGLED = 12;
const TROUBLE_LAVA = 11;
const TROUBLE_SICK = 10;
const TROUBLE_STARVING = 9;
const TROUBLE_REGION = 8;
const TROUBLE_HIT = 7;
const TROUBLE_LYCANTHROPE = 6;
const TROUBLE_COLLAPSING = 5;
const TROUBLE_STUCK_IN_WALL = 4;
const TROUBLE_CURSED_LEVITATION = 3;
const TROUBLE_UNUSEABLE_HANDS = 2;
const TROUBLE_CURSED_BLINDFOLD = 1;
const TROUBLE_PUNISHED = -1;
const TROUBLE_FUMBLING = -2;
const TROUBLE_CURSED_ITEMS = -3;
const TROUBLE_SADDLE = -4;
const TROUBLE_BLIND = -5;
const TROUBLE_POISONED = -6;
const TROUBLE_WOUNDED_LEGS = -7;
const TROUBLE_HUNGRY = -8;
const TROUBLE_STUNNED = -9;
const TROUBLE_CONFUSED = -10;
const TROUBLE_HALLUCINATION = -11;
const MAXVALUE = 24;
const ALIGNLIM = 14;
const NH_AMBER = 'amber';
const NH_BLACK = 'black';
const NH_GOLDEN = 'golden';
const NH_LIGHT_BLUE = 'light blue';
const NH_WHITE = 'white';
const NH_ORANGE = 'orange';
// ── Macros ──
function ugod_is_angry() { return game.u.ualign.record < 0; }
function on_altar() { return IS_ALTAR(levl(game.u.ux, game.u.uy).typ); }
function on_shrine() { return (levl(game.u.ux, game.u.uy).altarmask & AM_SHRINE) !== 0; }
function a_align(x, y) { return Amask2align(levl(x, y).altarmask & AM_MASK); }
function Cursed_obj(obj, typ) { return obj && obj.otyp === typ && obj.cursed; }
// Luck imported from macros.js
// ── Non-RNG stubs for functions not yet ported ──
// Stoned imported from macros.js
// Slimed imported from macros.js
// Strangled, Unchanging imported from macros.js
// Sick imported from macros.js
// Passes_walls imported from macros.js
// Fixed_abil: imported from macros.js
// Wounded_legs imported from macros.js
// Reflecting imported from macros.js
// Inhell: imported from macros.js
// BlindedTimeout imported from macros.js
// HBlinded: imported from macros.js
function HDeaf() { return game.u?.uprops?.[DEAF]?.intrinsic || 0; }
// HStun: imported from macros.js
// HConfusion: imported from macros.js
// HHallucination: imported from macros.js
 // TODO
// NOTE: differs from canonical do_wear.js — simplified; no nolimbs/welded/uarmg/Glib checks
// welded imported from wield.js
// nohands imported from mondata.js
// NOTE: differs from canonical do_wear.js — always returns null; canonical checks uamul for AMULET_OF_UNCHANGING
 // TODO
// unchanger imported // TODO
// which_armor imported from worn.js
// NOTE: differs from canonical mondata.js — no likes_gems(ptr) check
// your_race imported from mondata.js
// NOTE: differs from canonical mondata.js — checks mlet===S_HUMAN; canonical checks mflags2 & M2_HUMAN
// is_vampshifter imported from mondata.js
// is_weptool imported from mondata.js
// Sokoban: imported from mklev.js
// sobj_at imported from mkobj.js
// throws_rocks imported from mondata.js
// resists_elec imported // TODO
// resists_disint imported // TODO
// NOTE: differs from canonical polyself.js — simplified lookup table; canonical calls mbodypart(game.player, part)
// NOTE: differs from canonical polyself.js — ignores monster type; canonical has full type-specific part tables
// uhim, uhis: imported from mon.js
// make_stoned imported
// make_slimed imported from potion.js
// make_sick imported from mhitu.js
// make_deaf imported
// make_stunned imported
// make_confused imported
// make_hallucinated imported // TODO
// make_glib imported from mhitu.js
// init_uhunger: imported from eat.js
 // TODO
// reset_utrap: imported from trap.js
function rescued_from_terrain(cause) { /* TODO */ }
// setuhpmax: imported from polyself.js
// set_itimeout imported from timeout.js
// unpunish imported from ball.js
// NOTE: differs from canonical dig.js — empty stub; also circular dep (dig.js imports from pray.js)
// gloves_simple_name: imported from objnam.js
// NOTE: differs from canonical polyself.js — empty stub; canonical has full rehumanize logic
// NOTE: differs from canonical were.js — empty stub; canonical has purify/rehumanize logic
// heal_legs imported from hack.js
// shieldeff imported from display.js
// NOTE: differs from canonical muse.js — both return false stubs, but NEW import edge
// NOTE: differs from canonical mondata.js — empty stub; canonical iterates fmon setting seen_resistance
// NOTE: differs from canonical mondata.js — empty stub; canonical iterates fmon clearing seen_resistance
// NOTE: differs from canonical do_wear.js — empty stub; canonical has full armor destruction logic
// summon_minion imported from minion.js
// wake_nearby imported from mon.js
// aggravate: imported from mon.js
// update_inventory imported from mon.js
// NOTE: differs from canonical mkobj.js — missing COIN_CLASS guard, uses 1/0 not true/false
// NOTE: differs from canonical mkobj.js — missing COIN_CLASS guard, uses 1/0 not true/false
// exercise imported from attrib.js
// losehp imported from hack.js
// losehp imported from hack.js
// losehp imported from hack.js
// losehp imported from hack.js
// done imported from hack.js
// useup imported from invent.js
// useupf: imported from eat.js
// NOTE: differs from canonical do.js — simplified; just places object, no weapon/engulfer/floor-effects handling
// obfree imported from mkobj.js
// makeknown imported from invent.js
function discover_artifact(artid) { /* TODO */ }
function bare_artifactname(obj) { return obj?.oartifact ? artiname(obj.oartifact) : ''; }
// u_wield_art: imported from artifact.js
function unrestrict_weapon_skill(skill) { /* TODO */ }
// weapon_type imported // TODO
// add_weapon_skill: imported from weapon.js
 // TODO
 // TODO
// C ref: skills.h — P_RESTRICTED: weapon skill is restricted for this role
function P_RESTRICTED(skill) { return game.u?.weapon_skills?.[skill]?.skill === P_ISRESTRICTED; }
// NOTE: differs from canonical mkobj.js — uniform random; canonical uses probability-weighted selection
// livelog_printf imported from hack.js
// NOTE: differs from canonical insight.js — both empty stubs; NEW import edge not worth adding
// floorfood: imported from eat.js
function feel_cockatrice(obj, force) { /* TODO */ }
// C ref: pickup.c:302 rider_corpse_revival
async function rider_corpse_revival(obj, remotely) {
    if (!obj || obj.otyp !== CORPSE || !is_rider(mons[obj.corpsenm]))
        return false;
    await pline(`At your ${remotely ? 'attempted acquisition' : 'touch'}, the corpse suddenly moves...`);
    await revive_corpse(obj);
    exercise(A_WIS, false);
    return true;
}
// has_omonst imported from objects.js
function get_mtraits(obj, force) { return null; } // TODO
// OMONST imported // TODO
// C ref: mkobj.c:2426 peek_at_iced_corpse_age
function peek_at_iced_corpse_age(obj) {
    let retval = obj.age || 0;
    if (obj.otyp === CORPSE && obj.on_ice) {
        const age = game.moves - (obj.age || 0);
        retval += Math.trunc(age * (2 - 1) / 2); // ROT_ICE_ADJUSTMENT=2
    }
    return retval;
}
// eaten_stat: imported from mkobj.js
// findpriest imported from priest.js
// temple_occupied imported from priest.js
// angry_priest imported from priest.js
// uchangealign: imported from role.js
// C ref: artifact.c:526 confers_luck
function confers_luck(obj) {
    if (!obj) return false;
    if (obj.otyp === LUCKSTONE) return true;
    return !!(obj.oartifact && spec_ability(obj, SPFX_LUCK));
}
// Is_sanctum: imported from macros.js
// paranoid_query: imported from input.js
// y_n: imported from input.js
async function YN(query) {
    // C: yn_function(query, ynchars, 'n') — uppercase variant
    return await y_n(query);
}
// NOTE: differs from canonical mondata.js — always returns true; canonical checks Strangled/is_silent/has_head
// resist imported from zap.js
// killed imported from hack.js
// set_malign imported
// Must be async: callback may call pline -> more -> nhgetch. See #297.
async function iter_mons(callback) {
    for (let mtmp = game.fmon; mtmp; mtmp = mtmp.nmon) {
        if (mtmp.dead) continue;
        await callback(mtmp);
    }
}
// mdistu imported from display.js
// attrcurse imported // TODO
// NOTE: differs from canonical sit.js — empty stub; canonical has full item-cursing logic
// C ref: role.c:719 — randrole: pick random role index
function randrole(for_display) {
    const res = roles.length; // C: SIZE(roles) - 1, but JS roles has no sentinel
    return for_display ? rn2_on_display_rng(res) : rn2(res);
}
// revive: imported from mon.js
function animate_statue(obj, x, y, spell, dummy) { return null; } // TODO
 // TODO
// Sprintf imported from hacklib.js
const voice_deity = 0; // placeholder
const LL_CONDUCT = 0x0001;
const LL_DIVINEGIFT = 0x0002;
const LL_ARTIFACT = 0x0004;
const LL_SPOILER = 0x0008;
const ANIMATE_SPELL = 1;
const spe_Unknown = -1;
const spe_Fresh = 2;
const spe_Forgotten = 0;
const You_can_move_again = 'You can move again.';
const Something = 'Something';
const XKILL_NOMSG = 0x1;
const XKILL_NOCORPSE = 0x2;
const XKILL_NOCONDUCT = 0x4;
const M_SEEN_REFL = 0x00100000;
const M_SEEN_ELEC = 0x00200000;
const M_SEEN_DISINT = 0x00400000;
const TELEDS_NO_FLAGS = 0;
const SICK_ALL = 0x03;
const STRAT_APPEARMSG = 0x40000000;
const A_CG_CONVERT = 0;
const EReflecting = 0; // TODO
const EDisint_resistance = 0; // TODO
const HAggravate_monster = 0; // TODO
function ParanoidPray() { return game.flags?.confirm !== false; }
const ParanoidConfirm = false; // TODO
const PLNMSG_OBJ_GLOWS = 1; // placeholder
// ── turn_undead state ──
// ── Helper: Hallu gods ──
const hallu_gods = [
    'the Flying Spaghetti Monster', 'Eris', 'the Martians', 'Xom',
    'AnDoR dRaKoN', 'the Central Bank of Yendor', 'Tooth Fairy', 'Om',
    'Yawgmoth', 'Morgoth', 'Cthulhu', 'the Ori', 'destiny',
    'your Friend the Computer',
];
// ══════════════════════════════════════════════════════
// Functions
// ══════════════════════════════════════════════════════
// C ref: pray.c:116
export function critically_low_hp(only_if_injured) {
    const u = game.u;
    let curhp = Upolyd() ? u.mh : u.uhp;
    let maxhp = Upolyd() ? u.mhmax : u.uhpmax;
    if (only_if_injured && !(curhp < maxhp))
        return false;
    let hplim = 15 * u.ulevel;
    if (maxhp > hplim) maxhp = hplim;
    let divisor;
    switch (xlev_to_rank(u.ulevel)) {
    case 0: case 1: divisor = 5; break;
    case 2: case 3: divisor = 6; break;
    case 4: case 5: divisor = 7; break;
    case 6: case 7: divisor = 8; break;
    default: divisor = 9; break;
    }
    return (curhp <= 5 || curhp * divisor <= maxhp);
}
// C ref: pray.c:160
export function stuck_in_wall() {
    const u = game.u;
    if (Passes_walls()) return false;
    let count = 0;
    for (let i = -1; i <= 1; i++) {
        const x = u.ux + i;
        for (let j = -1; j <= 1; j++) {
            if (!i && !j) continue;
            const y = u.uy + j;
            if (!isok(x, y)
                || (IS_OBSTRUCTED(levl(x, y).typ)
                    && (levl(x, y).typ !== SDOOR && levl(x, y).typ !== SCORR))
                || (blocked_boulder(i, j) && !throws_rocks(game.player?.data)))
                ++count;
        }
    }
    return count === 8;
}
// C ref: pray.c:197
function in_trouble() {
    const u = game.u;
    let i;
    if (Stoned()) return TROUBLE_STONED;
    if (Slimed()) return TROUBLE_SLIMED;
    if (Strangled()) return TROUBLE_STRANGLED;
    if (u.utrap && u.utraptype === TT_LAVA) return TROUBLE_LAVA;
    if (Sick()) return TROUBLE_SICK;
    if (u.uhs >= WEAK) return TROUBLE_STARVING;
    if (region_danger()) return TROUBLE_REGION;
    if ((!Upolyd() || Unchanging()) && critically_low_hp(false))
        return TROUBLE_HIT;
    if (ismnum(u.ulycn)) return TROUBLE_LYCANTHROPE;
    if (near_capacity() >= EXT_ENCUMBER && AMAX(A_STR) - ABASE(A_STR) > 3)
        return TROUBLE_COLLAPSING;
    if (stuck_in_wall()) return TROUBLE_STUCK_IN_WALL;
    if (Cursed_obj(u.uarmf, LEVITATION_BOOTS)
        || stuck_ring(u.uleft, RIN_LEVITATION)
        || stuck_ring(u.uright, RIN_LEVITATION))
        return TROUBLE_CURSED_LEVITATION;
    if (nohands(game.player?.data) || !freehand()) {
        if (welded(u.uwep)) return TROUBLE_UNUSEABLE_HANDS;
        if (Upolyd() && nohands(game.player?.data)
            && (!Unchanging() || ((unchanger()) && unchanger()?.cursed)))
            return TROUBLE_UNUSEABLE_HANDS;
    }
    if (Blindfolded() && u.ublindf?.cursed)
        return TROUBLE_CURSED_BLINDFOLD;
    // minor troubles
    if (Punished() || (u.utrap && u.utraptype === TT_BURIEDBALL))
        return TROUBLE_PUNISHED;
    if (Cursed_obj(u.uarmg, GAUNTLETS_OF_FUMBLING)
        || Cursed_obj(u.uarmf, FUMBLE_BOOTS))
        return TROUBLE_FUMBLING;
    if (worst_cursed_item()) return TROUBLE_CURSED_ITEMS;
    if (u.usteed) {
        const otmp = which_armor(u.usteed, W_SADDLE);
        if (Cursed_obj(otmp, SADDLE)) return TROUBLE_SADDLE;
    }
    if (BlindedTimeout() > 1 && !(HBlinded() & ~TIMEOUT)
        && (!u.uswallow))
        return TROUBLE_BLIND;
    if ((HDeaf() & TIMEOUT) > 1) return TROUBLE_BLIND;
    for (i = 0; i < A_MAX; i++)
        if (ABASE(i) < AMAX(i)) return TROUBLE_POISONED;
    if (Wounded_legs() && !u.usteed) return TROUBLE_WOUNDED_LEGS;
    if (u.uhs >= HUNGRY) return TROUBLE_HUNGRY;
    if (HStun() & TIMEOUT) return TROUBLE_STUNNED;
    if (HConfusion() & TIMEOUT) return TROUBLE_CONFUSED;
    if (HHallucination() & TIMEOUT) return TROUBLE_HALLUCINATION;
    return 0;
}
// C ref: pray.c:287
function worst_cursed_item() {
    const u = game.u;
    let otmp;
    if (near_capacity() >= HVY_ENCUMBER) {
        for (otmp = game.invent; otmp; otmp = otmp.nobj)
            if (Cursed_obj(otmp, LOADSTONE)) return otmp;
    }
    if (welded(u.uwep) && (u.uright || (u.uwep && game.objects[u.uwep.otyp]?.oc_bimanual))) {
        otmp = u.uwep;
    } else if (u.uarmg && u.uarmg.cursed) {
        otmp = u.uarmg;
    } else if (u.uarms && u.uarms.cursed) {
        otmp = u.uarms;
    } else if (u.uarmc && u.uarmc.cursed) {
        otmp = u.uarmc;
    } else if (u.uarm && u.uarm.cursed) {
        otmp = u.uarm;
    } else if (u.uarmh && u.uarmh.cursed
               && u.uarmh.otyp !== HELM_OF_OPPOSITE_ALIGNMENT) {
        otmp = u.uarmh;
    } else if (u.uarmf && u.uarmf.cursed) {
        otmp = u.uarmf;
    } else if (u.uarmu && u.uarmu.cursed) {
        otmp = u.uarmu;
    } else if (u.uamul && u.uamul.cursed) {
        otmp = u.uamul;
    } else if (u.uleft && u.uleft.cursed) {
        otmp = u.uleft;
    } else if (u.uright && u.uright.cursed) {
        otmp = u.uright;
    } else if (u.ublindf && u.ublindf.cursed) {
        otmp = u.ublindf;
    } else if (welded(u.uwep)) {
        otmp = u.uwep;
    } else if (u.uswapwep && u.uswapwep.cursed && u.twoweap) {
        otmp = u.uswapwep;
    } else {
        for (otmp = game.invent; otmp; otmp = otmp.nobj) {
            if (!otmp.cursed) continue;
            if (otmp.otyp === LOADSTONE || confers_luck(otmp))
                break;
        }
    }
    return otmp;
}
// C ref: pray.c:348
async function fix_curse_trouble(otmp, what) {
    if (!otmp) return;
    if (otmp === game.u.uarmg && Glib()) {
        make_glib(0);
        await Your('gloves are no longer slippery.');
        if (!otmp.cursed) return;
    }
    if (!Blind() || (otmp === game.u.ublindf && Blindfolded())) {
        await pline(`${what ? what : (Yobjnam2(otmp, 'softly glow'))} ${hcolor(NH_AMBER)}.`);
        otmp.bknown = !Hallucination() ? 1 : 0;
    }
    uncurse(otmp);
    update_inventory();
}
// C ref: pray.c:372
async function fix_worst_trouble(trouble) {
    const u = game.u;
    let i, maxhp;
    let otmp = null;
    let what = null;
    const leftglow = 'Your left ring softly glows';
    const rightglow = 'Your right ring softly glows';
    switch (trouble) {
    case TROUBLE_STONED:
        await make_stoned(0, 'You feel more limber.', 0, null);
        break;
    case TROUBLE_SLIMED:
        await make_slimed(0, 'The slime disappears.');
        break;
    case TROUBLE_STRANGLED:
        if (u.uamul && u.uamul.otyp === AMULET_OF_STRANGULATION) {
            await Your('amulet vanishes!');
            useup(u.uamul);
        }
        await You('can breathe again.');
        // Strangled = 0; TODO
        setBotl('fix_worst_trouble');
        break;
    case TROUBLE_LAVA:
        if (!await safe_teleds(TELEDS_NO_FLAGS))
            await reset_utrap(true);
        rescued_from_terrain(DISSOLVED);
        break;
    case TROUBLE_STARVING:
        // fall through
    case TROUBLE_HUNGRY:
        await Your(`${body_part(STOMACH)} feels content.`);
        await init_uhunger();
        setBotl('pray');
        break;
    case TROUBLE_SICK:
        await You_feel('better.');
        await make_sick(0, null, false, SICK_ALL);
        break;
    case TROUBLE_REGION:
        await region_safety();
        break;
    case TROUBLE_HIT:
        await You_feel('much better.');
        if (Upolyd()) {
            maxhp = u.mhmax + rnd(5);
            setuhpmax(Math.max(maxhp, 6), false);
            u.mh = u.mhmax;
        }
        maxhp = u.uhpmax;
        if (maxhp < u.ulevel * 5 + 11)
            maxhp += rnd(5);
        setuhpmax(Math.max(maxhp, 6), true);
        u.uhp = u.uhpmax;
        setBotl('pray');
        break;
    case TROUBLE_COLLAPSING:
        await You_feel(`${(AMAX(A_STR) - ABASE(A_STR) > 6) ? 'much ' : ''}stronger.`);
        game.u.acurr.a[A_STR] = AMAX(A_STR);
        setBotl('pray');
        if (Fixed_abil()) {
            otmp = stuck_ring(u.uleft, RIN_SUSTAIN_ABILITY);
            if (otmp) {
                if (otmp === u.uleft) what = leftglow;
            } else {
                otmp = stuck_ring(u.uright, RIN_SUSTAIN_ABILITY);
                if (otmp && otmp === u.uright) what = rightglow;
            }
            if (otmp) {
                await fix_curse_trouble(otmp, what);
                break;
            }
        }
        break;
    case TROUBLE_STUCK_IN_WALL:
        if (await safe_teleds(TELEDS_NO_FLAGS)) {
            await Your('surroundings change.');
        } else {
            const dur = c_d(4, 4) + 4;
            // set_itimeout(&HPasses_walls, dur);
            await You_feel('much slimmer.');
        }
        break;
    case TROUBLE_CURSED_LEVITATION:
        if (Cursed_obj(u.uarmf, LEVITATION_BOOTS)) {
            otmp = u.uarmf;
        } else if ((otmp = stuck_ring(u.uleft, RIN_LEVITATION))) {
            if (otmp === u.uleft) what = leftglow;
        } else if ((otmp = stuck_ring(u.uright, RIN_LEVITATION))) {
            if (otmp === u.uright) what = rightglow;
        }
        await fix_curse_trouble(otmp, what);
        break;
    case TROUBLE_UNUSEABLE_HANDS:
        if (welded(u.uwep)) {
            otmp = u.uwep;
            await fix_curse_trouble(otmp, what);
            break;
        }
        if (Upolyd() && nohands(game.player?.data)) {
            if (!Unchanging()) {
                await Your('shape becomes uncertain.');
                await rehumanize();
            } else {
                otmp = unchanger();
                if (otmp && otmp.cursed) {
                    await fix_curse_trouble(otmp, what);
                    break;
                }
            }
        }
        break;
    case TROUBLE_CURSED_BLINDFOLD:
        otmp = u.ublindf;
        await fix_curse_trouble(otmp, what);
        break;
    case TROUBLE_LYCANTHROPE:
        await you_unwere(true);
        break;
    case TROUBLE_PUNISHED:
        await Your('chain disappears.');
        if (u.utrap && u.utraptype === TT_BURIEDBALL)
            await buried_ball_to_freedom();
        else
            unpunish();
        break;
    case TROUBLE_FUMBLING:
        if (Cursed_obj(u.uarmg, GAUNTLETS_OF_FUMBLING))
            otmp = u.uarmg;
        else if (Cursed_obj(u.uarmf, FUMBLE_BOOTS))
            otmp = u.uarmf;
        await fix_curse_trouble(otmp, what);
        break;
    case TROUBLE_CURSED_ITEMS:
        otmp = worst_cursed_item();
        if (otmp === u.uright) what = rightglow;
        else if (otmp === u.uleft) what = leftglow;
        await fix_curse_trouble(otmp, what);
        break;
    case TROUBLE_POISONED:
        if (Hallucination())
            await pline("There's a tiger in your tank.");
        else
            await You_feel('in good health again.');
        for (i = 0; i < A_MAX; i++) {
            if (ABASE(i) < AMAX(i)) {
                game.u.acurr.a[i] = AMAX(i);
                setBotl('pray');
            }
        }
        await encumber_msg();
        break;
    case TROUBLE_BLIND: {
        let msgbuf = '';
        let eyes = body_part(EYE);
        let cure_deaf = (HDeaf() & TIMEOUT) ? true : false;
        if (Blind()) {
            eyes = makeplural(eyes);
            msgbuf = `Your ${eyes} feel better`;
            game.u.ucreamed = 0;
            await make_blinded(0, false);
        }
        if (cure_deaf) {
            await make_deaf(0, false);
            if (!Deaf())
                msgbuf += `${!msgbuf ? 'You' : ' and you'} can hear again`;
        }
        if (msgbuf)
            await pline(`${msgbuf}.`);
        break;
    }
    case TROUBLE_WOUNDED_LEGS:
        await heal_legs(0);
        break;
    case TROUBLE_STUNNED:
        await make_stunned(0, true);
        break;
    case TROUBLE_CONFUSED:
        await make_confused(0, true);
        break;
    case TROUBLE_HALLUCINATION:
        await pline('Looks like you are back in Kansas.');
        await make_hallucinated(0, false, 0);
        break;
    case TROUBLE_SADDLE:
        otmp = which_armor(u.usteed, W_SADDLE);
        if (!Blind()) {
            await pline(`${Yobjnam2(otmp, 'softly glow')} ${hcolor(NH_AMBER)}.`);
            set_bknown(otmp, 1);
        }
        uncurse(otmp);
        break;
    }
}
// C ref: pray.c:609
async function god_zaps_you(resp_god) {
    const u = game.u;
    if (u.uswallow) {
        await pline('Suddenly a bolt of lightning comes down at you from the heavens!');
        await pline(`It strikes ${mon_nam(u.ustuck)}!`);
        if (!resists_elec(u.ustuck)) {
            await pline(`${Monnam(u.ustuck)} fries to a crisp!`);
            await xkilled(u.ustuck, XKILL_NOMSG | XKILL_NOCONDUCT);
        } else {
            await pline(`${Monnam(u.ustuck)} seems unaffected.`);
        }
    } else {
        await pline('Suddenly, a bolt of lightning strikes you!');
        if (Reflecting()) {
            shieldeff(u.ux, u.uy);
            if (Blind())
                await pline("For some reason you're unaffected.");
            else
                await ureflects('%s reflects from your %s.', 'It');
            monstseesu(M_SEEN_REFL);
        } else if (Shock_resistance()) {
            shieldeff(u.ux, u.uy);
            await pline('It seems not to affect you.');
            monstseesu(M_SEEN_ELEC);
            monstunseesu(M_SEEN_REFL);
        } else {
            await fry_by_god(resp_god, false);
            monstunseesu(M_SEEN_REFL | M_SEEN_ELEC);
        }
    }
    await pline(`${align_gname(resp_god)} is not deterred...`);
    if (u.uswallow) {
        await pline(`A wide-angle disintegration beam aimed at you hits ${mon_nam(u.ustuck)}!`);
        if (!resists_disint(u.ustuck)) {
            await pline(`${Monnam(u.ustuck)} disintegrates into a pile of dust!`);
            await xkilled(u.ustuck, XKILL_NOMSG | XKILL_NOCORPSE | XKILL_NOCONDUCT);
        } else {
            await pline(`${Monnam(u.ustuck)} seems unaffected.`);
        }
    } else {
        await pline('A wide-angle disintegration beam hits you!');
        if (u.uarms && !(EReflecting & W_ARMS) && !(EDisint_resistance & W_ARMS))
            await disintegrate_arm(u.uarms);
        if (u.uarmc && !(EReflecting & W_ARMC) && !(EDisint_resistance & W_ARMC))
            await disintegrate_arm(u.uarmc);
        if (u.uarm && !(EReflecting & W_ARM) && !(EDisint_resistance & W_ARM) && !u.uarmc)
            await disintegrate_arm(u.uarm);
        if (u.uarmu && !u.uarm && !u.uarmc)
            await disintegrate_arm(u.uarmu);
        if (!Disint_resistance()) {
            await fry_by_god(resp_god, true);
            monstunseesu(M_SEEN_DISINT);
        } else {
            await You(`bask in its ${NH_BLACK} glow for a minute...`);
            await godvoice_fn(resp_god, 'I believe it not!');
            monstseesu(M_SEEN_DISINT);
        }
        if (Is_astralevel(u.uz) || Is_sanctum(u.uz)) {
            SetVoice(null, 0, 80, voice_deity);
            await verbalize('Thou cannot escape my wrath, mortal!');
            await summon_minion(resp_god, false);
            await summon_minion(resp_god, false);
            await summon_minion(resp_god, false);
            SetVoice(null, 0, 80, voice_deity);
            await verbalize(`Destroy ${uhim()}, my servants!`);
        }
    }
}
// C ref: pray.c:693
async function fry_by_god(resp_god, via_disintegration) {
    await You(`${!via_disintegration ? 'fry to a crisp' : 'disintegrate into a pile of dust'}!`);
    // killer setup
    await done(DIED);
}
// C ref: pray.c:703
async function angrygods(resp_god) {
    const u = game.u;
    let maxanger, new_ublesscnt;
    if (Inhell()) resp_god = A_NONE;
    u.ublessed = 0;
    if (resp_god !== u.ualign.type)
        maxanger = Math.trunc(u.ualign.record / 2) + (Luck() > 0 ? Math.trunc(-Luck() / 3) : -Luck());
    else
        maxanger = 3 * u.ugangr + ((Luck() > 0 || u.ualign.record >= STRIDENT)
                                   ? Math.trunc(-Luck() / 3)
                                   : -Luck());
    if (maxanger < 1) maxanger = 1;
    else if (maxanger > 15) maxanger = 15;
    switch (rn2(maxanger)) {
    case 0:
    case 1:
        await You_feel(`that ${align_gname(resp_god)} is ${Hallucination() ? 'bummed' : 'displeased'}.`);
        break;
    case 2:
    case 3:
        await godvoice_fn(resp_god, null);
        await pline(`"Thou ${(ugod_is_angry() && resp_god === u.ualign.type) ? 'hast strayed from the path' : 'art arrogant'}, ${game.player?.data?.mlet === S_HUMAN ? 'mortal' : 'creature'}."`);
        SetVoice(null, 0, 80, voice_deity);
        await verbalize('Thou must relearn thy lessons!');
        await adjattrib(A_WIS, -1, false);
        await losexp(null);
        break;
    case 6:
        if (!Punished()) {
            await gods_angry_fn(resp_god);
            // punish(); TODO
            break;
        }
        // fall through
    case 4:
    case 5:
        await gods_angry_fn(resp_god);
        if (!Blind() && !Antimagic())
            await pline(`${an(hcolor(NH_BLACK))} glow surrounds you.`);
        if (rn2(2) || !await attrcurse())
            await rndcurse();
        break;
    case 7:
    case 8:
        await godvoice_fn(resp_god, null);
        SetVoice(null, 0, 80, voice_deity);
        await verbalize(`Thou durst ${(on_altar() && (a_align(game.u.ux, game.u.uy) !== resp_god)) ? 'scorn' : 'call upon'} me?`);
        await pline(`"Then die, ${(game.player?.data?.mlet === S_HUMAN) ? 'mortal' : 'creature'}!"`);
        await summon_minion(resp_god, false);
        break;
    default:
        await gods_angry_fn(resp_god);
        await god_zaps_you(resp_god);
        break;
    }
    new_ublesscnt = rnz(300);
    if (new_ublesscnt > u.ublesscnt)
        u.ublesscnt = new_ublesscnt;
}
// C ref: pray.c:787
async function at_your_feet(str) {
    if (Blind()) str = Something;
    const u = game.u;
    if (u.uswallow) {
        await pline(`${str} ${vtense(str, 'drop')} into ${s_suffix(mon_nam(u.ustuck))} ${mbodypart(u.ustuck, STOMACH)}.`);
    } else {
        await pline(`${str} ${vtense(str, Blind() ? 'land' : 'appear')} ${Levitation() ? 'beneath' : 'at'} your ${makeplural(body_part(FOOT))}!`);
    }
}
// C ref: pray.c:804
async function gcrownu() {
    const u = game.u;
    let obj;
    let what;
    let already_exists, in_hand;
    let class_gift;
    const ok_wep = (o) => o && (o.oclass === WEAPON_CLASS || is_weptool(o));
    // Grant resistances
    // HSee_invisible |= FROMOUTSIDE; etc.
    // TODO: set intrinsic properties
    await godvoice_fn(u.ualign.type, null);
    class_gift = STRANGE_OBJECT;
    if (Role_if(PM_WIZARD)
        && !u_wield_art(ART_VORPAL_BLADE)
        && !u_wield_art(ART_STORMBRINGER)
        && !carrying(SPE_FINGER_OF_DEATH)) {
        class_gift = SPE_FINGER_OF_DEATH;
    } else if (Role_if(PM_MONK) && (!u.uwep || !u.uwep.oartifact)
               && !carrying(SPE_RESTORE_ABILITY)) {
        class_gift = SPE_RESTORE_ABILITY;
    }
    obj = ok_wep(u.uwep) ? u.uwep : null;
    already_exists = in_hand = false;
    switch (u.ualign.type) {
    case A_LAWFUL:
        u.uevent.uhand_of_elbereth = 1;
        SetVoice(null, 0, 80, voice_deity);
        await verbalize('I crown thee...  The Hand of Elbereth!');
        break;
    case A_NEUTRAL:
        u.uevent.uhand_of_elbereth = 2;
        in_hand = u_wield_art(ART_VORPAL_BLADE);
        already_exists = exist_artifact(LONG_SWORD, artiname(ART_VORPAL_BLADE));
        SetVoice(null, 0, 80, voice_deity);
        await verbalize('Thou shalt be my Envoy of Balance!');
        break;
    case A_CHAOTIC:
        u.uevent.uhand_of_elbereth = 3;
        in_hand = u_wield_art(ART_STORMBRINGER);
        already_exists = exist_artifact(RUNESWORD, artiname(ART_STORMBRINGER));
        what = (((already_exists && !in_hand) || class_gift !== STRANGE_OBJECT)
                ? 'take lives' : 'steal souls');
        SetVoice(null, 0, 80, voice_deity);
        await verbalize(`Thou art chosen to ${what} for My Glory!`);
        break;
    }
    if (game.objects[class_gift]?.oc_class === SPBOOK_CLASS) {
        obj = mksobj(class_gift, true, false);
        bless(obj);
        obj.bknown = 1;
        observe_object(obj);
        await at_your_feet(upstart(ansimpleoname(obj)));
        await dropy(obj);
        u.ugifts++;
        if (known_spell(class_gift) !== spe_Unknown && ok_wep(u.uwep))
            obj = u.uwep;
    }
    switch (u.ualign.type) {
    case A_LAWFUL:
        if (class_gift !== STRANGE_OBJECT) {
            ; // already got bonus
        } else if (obj && obj.otyp === LONG_SWORD && !obj.oartifact) {
            if (!Blind())
                await Your('sword shines brightly for a moment.');
            obj = oname(obj, artiname(ART_EXCALIBUR), 0);
            await oname_deferred_untwoweapon(obj);
            if (is_art(obj, ART_EXCALIBUR))
                u.ugifts++;
        }
        unrestrict_weapon_skill(P_LONG_SWORD);
        if (is_art(obj, ART_EXCALIBUR))
            discover_artifact(ART_EXCALIBUR);
        break;
    case A_NEUTRAL:
        if (class_gift !== STRANGE_OBJECT) {
            ;
        } else if (obj && in_hand) {
            await Your(`${xname(obj)} goes snicker-snack!`);
            observe_object(obj);
        } else if (!already_exists) {
            obj = mksobj(LONG_SWORD, false, false);
            obj = oname(obj, artiname(ART_VORPAL_BLADE), 0);
            await oname_deferred_untwoweapon(obj);
            obj.spe = 1;
            await at_your_feet('A sword');
            await dropy(obj);
            u.ugifts++;
        }
        unrestrict_weapon_skill(P_LONG_SWORD);
        if (is_art(obj, ART_VORPAL_BLADE))
            discover_artifact(ART_VORPAL_BLADE);
        break;
    case A_CHAOTIC: {
        const swordbuf = `${hcolor(NH_BLACK)} sword`;
        if (class_gift !== STRANGE_OBJECT) {
            ;
        } else if (obj && in_hand) {
            await Your(`${swordbuf} hums ominously!`);
            observe_object(obj);
        } else if (!already_exists) {
            obj = mksobj(RUNESWORD, false, false);
            obj = oname(obj, artiname(ART_STORMBRINGER), 0);
            await oname_deferred_untwoweapon(obj);
            obj.spe = 1;
            await at_your_feet(an(swordbuf));
            await dropy(obj);
            u.ugifts++;
        }
        unrestrict_weapon_skill(P_BROAD_SWORD);
        if (is_art(obj, ART_STORMBRINGER))
            discover_artifact(ART_STORMBRINGER);
        break;
    }
    default:
        obj = null;
        break;
    }
    // enhance weapon
    if (ok_wep(obj)) {
        bless(obj);
        obj.oeroded = obj.oeroded2 = 0;
        obj.oerodeproof = true;
        obj.bknown = obj.rknown = 1;
        if (obj.spe < 1) obj.spe = 1;
        unrestrict_weapon_skill(weapon_type(obj));
    } else if (class_gift === STRANGE_OBJECT) {
        await You_feel('unworthy.');
    }
    update_inventory();
    await add_weapon_skill(1);
}
// C ref: pray.c:998
async function give_spell() {
    const u = game.u;
    let otmp;
    let trycnt = u.ulevel + 1;
    otmp = mkobj(SPBOOK_no_NOVEL, true);
    while (--trycnt > 0) {
        if (otmp.otyp !== SPE_BLANK_PAPER) {
            if (known_spell(otmp.otyp) <= spe_Unknown
                && !P_RESTRICTED(spell_skilltype(otmp.otyp)))
                break;
        } else {
            if (!game.objects[SPE_BLANK_PAPER]?.oc_name_known
                || carrying(MAGIC_MARKER))
                break;
        }
        otmp.otyp = rnd_class(game.bases?.[SPBOOK_CLASS] || SPE_FINGER_OF_DEATH, SPE_BLANK_PAPER);
    }
    if (otmp.otyp !== SPE_BLANK_PAPER && !rn2(4)
        && known_spell(otmp.otyp) !== spe_Fresh) {
        // C ref: pray.c — god grants spell knowledge
        // force_learn_spell returns null on failure (C returns '\0')
        const spe_let = force_learn_spell(otmp.otyp);
        if (spe_let !== null) {
            const spe_knowledge = known_spell(otmp.otyp);
            const spe_name = game.objects[otmp.otyp]?.oc_name || 'spell';
            if (spe_knowledge === spe_Unknown)
                await pline(`Divine knowledge of ${spe_name} fills your mind!  Spell '${spe_let}'.`);
            else
                await Your(`knowledge of spell '${spe_let}' - ${spe_name} is ${(spe_knowledge === spe_Forgotten) ? 'restored' : 'refreshed'}.`);
        }
        obfree(otmp, null);
    } else {
        observe_object(otmp);
        if (otmp.otyp === SPE_BLANK_PAPER || !rn2(100))
            makeknown(otmp.otyp);
        bless(otmp);
        await at_your_feet(upstart(ansimpleoname(otmp)));
        place_object(otmp, u.ux, u.uy);
        newsym(u.ux, u.uy);
    }
}
// C ref: pray.c:1070
async function pleased(g_align) {
    const u = game.u;
    let trouble = in_trouble();
    let pat_on_head = 0, kick_on_butt;
    await You_feel(`that ${align_gname(g_align)} is ${(u.ualign.record >= DEVOUT) ? (Hallucination() ? 'pleased as punch' : 'well-pleased') : (u.ualign.record >= STRIDENT) ? (Hallucination() ? 'ticklish' : 'pleased') : (Hallucination() ? 'full' : 'satisfied')}.`);
    if (on_altar() && game.p_aligntyp !== u.ualign.type) {
        adjalign(-1);
        return;
    } else if (u.ualign.record < 2 && trouble <= 0) {
        adjalign(1);
    }
    if (!trouble && u.ualign.record >= DEVOUT) {
        if (game.p_trouble === 0) pat_on_head = 1;
    } else {
        let action;
        let tryct = 0;
        const prayer_luck = Math.max(Luck(), -1);
        action = rn1(prayer_luck + (on_altar() ? 3 + (on_shrine() ? 1 : 0) : 2), 1);
        if (!on_altar()) action = Math.min(action, 3);
        if (u.ualign.record < STRIDENT)
            action = (u.ualign.record > 0 || !rnl(2)) ? 1 : 0;
        switch (Math.min(action, 5)) {
        case 5:
            pat_on_head = 1;
            // fall through
        case 4:
            do {
                await fix_worst_trouble(trouble);
            } while ((trouble = in_trouble()) !== 0);
            break;
        case 3:
            await fix_worst_trouble(trouble);
            // fall through
        case 2:
            while ((trouble = in_trouble()) > 0 && (++tryct < 10))
                await fix_worst_trouble(trouble);
            break;
        case 1:
            if (trouble > 0) await fix_worst_trouble(trouble);
            break;
        case 0:
            break;
        }
    }
    if (pat_on_head) {
        switch (rn2((Luck() + 6) >> 1)) {
        case 0:
            break;
        case 1:
            if (u.uwep && (welded(u.uwep) || u.uwep.oclass === WEAPON_CLASS
                         || is_weptool(u.uwep))) {
                let repair_buf = '';
                if (u.uwep.oeroded || u.uwep.oeroded2)
                    repair_buf = ` and ${otense(u.uwep, 'are')} now as good as new`;
                if (u.uwep.cursed) {
                    if (!Blind()) {
                        await pline(`${Yobjnam2(u.uwep, 'softly glow')} ${hcolor(NH_AMBER)}${repair_buf}.`);
                    } else {
                        await You_feel(`the power of ${u_gname()} over ${yname(u.uwep)}.`);
                    }
                    uncurse(u.uwep);
                    u.uwep.bknown = 1;
                    repair_buf = '';
                } else if (!u.uwep.blessed) {
                    if (!Blind()) {
                        await pline(`${Yobjnam2(u.uwep, 'softly glow')} with ${an(hcolor(NH_LIGHT_BLUE))} aura${repair_buf}.`);
                    } else {
                        await You_feel(`the blessing of ${u_gname()} over ${yname(u.uwep)}.`);
                    }
                    bless(u.uwep);
                    u.uwep.bknown = 1;
                    repair_buf = '';
                }
                if (u.uwep.oeroded || u.uwep.oeroded2) {
                    u.uwep.oeroded = u.uwep.oeroded2 = 0;
                    if (repair_buf)
                        await pline(`${Yobjnam2(u.uwep, Blind() ? 'feel' : 'look')} as good as new!`);
                }
                update_inventory();
            }
            break;
        case 3:
            if (!u.uevent.uopened_dbridge && !u.uevent.gehennom_entered) {
                if ((u.uevent.uheard_tune || 0) < 1) {
                    await godvoice_fn(g_align, null);
                    SetVoice(null, 0, 80, voice_deity);
                    await verbalize(`Hark, ${is_human(game.player?.data) ? 'mortal' : 'creature'}!`);
                    SetVoice(null, 0, 80, voice_deity);
                    await verbalize('To enter the castle, thou must play the right tune!');
                    u.uevent.uheard_tune = (u.uevent.uheard_tune || 0) + 1;
                    break;
                } else if ((u.uevent.uheard_tune || 0) < 2) {
                    Soundeffect(0, 50);
                    await You_hear('a divine music...');
                    await pline(`It sounds like:  "${game.tune || 'passtune'}".`);
                    u.uevent.uheard_tune = (u.uevent.uheard_tune || 0) + 1;
                    record_achievement(ACH_TUNE);
                    break;
                }
            }
            // fall through
        case 2:
            if (!Blind())
                await You(`are surrounded by ${an(hcolor(NH_GOLDEN))} glow.`);
            if (u.ulevel < u.ulevelmax) {
                u.ulevelmax -= 1;
                await pluslvl(false);
            } else {
                u.uhpmax += 5;
                if (u.uhpmax > (u.uhppeak || 0))
                    u.uhppeak = u.uhpmax;
                if (Upolyd())
                    u.mhmax += 5;
            }
            u.uhp = u.uhpmax;
            if (Upolyd()) u.mh = u.mhmax;
            if (ABASE(A_STR) < AMAX(A_STR)) {
                game.u.acurr.a[A_STR] = AMAX(A_STR);
                setBotl('pray');
                await encumber_msg();
            }
            if (u.uhunger < 900)
                await init_uhunger();
            if (Luck() < 0) u.uluck = 0;
            u.ucreamed = 0;
            await make_blinded(0, true);
            setBotl('pray');
            break;
        case 4: {
            let any = 0;
            if (Blind())
                await You_feel(`the power of ${u_gname()}.`);
            else
                await You(`are surrounded by ${an(hcolor(NH_LIGHT_BLUE))} aura.`);
            for (let otmp = game.invent; otmp; otmp = otmp.nobj) {
                if (otmp.cursed
                    && (otmp !== u.uarmh
                        || u.uarmh.otyp !== HELM_OF_OPPOSITE_ALIGNMENT)) {
                    if (!Blind()) {
                        await pline(`${Yobjnam2(otmp, 'softly glow')} ${hcolor(NH_AMBER)}.`);
                        otmp.bknown = 1;
                        ++any;
                    }
                    uncurse(otmp);
                }
            }
            if (any) update_inventory();
            break;
        }
        case 5: {
            const msg = '"and thus I grant thee the gift of %s!"';
            await godvoice_fn(u.ualign.type, 'Thou hast pleased me with thy progress,');
            // Check intrinsics
            const grantMsg = (gift) => `"and thus I grant thee the gift of ${gift}!"`;
            if (!(game.u.uprops?.[TELEPAT]?.intrinsic & INTRINSIC)) {
                game.u.uprops[TELEPAT].intrinsic |= FROMOUTSIDE;
                await pline(grantMsg('Telepathy'));
                if (Blind()) see_monsters();
            } else if (!(game.u.uprops?.[FAST]?.intrinsic & INTRINSIC)) {
                game.u.uprops[FAST].intrinsic |= FROMOUTSIDE;
                await pline(grantMsg('Speed'));
            } else if (!(game.u.uprops?.[STEALTH]?.intrinsic & INTRINSIC)) {
                game.u.uprops[STEALTH].intrinsic |= FROMOUTSIDE;
                await pline(grantMsg('Stealth'));
            } else {
                if (!(game.u.uprops?.[PROTECTION]?.intrinsic & INTRINSIC)) {
                    game.u.uprops[PROTECTION].intrinsic |= FROMOUTSIDE;
                    if (!u.ublessed)
                        u.ublessed = rn1(3, 2);
                } else {
                    u.ublessed++;
                }
                await pline(grantMsg('my protection'));
            }
            SetVoice(null, 0, 80, voice_deity);
            await verbalize('Use it wisely in my name!');
            break;
        }
        case 7:
        case 8:
            if (u.ualign.record >= PIOUS && !u.uevent.uhand_of_elbereth) {
                await gcrownu();
                break;
            }
            // fall through
        case 6:
            await give_spell();
            break;
        default:
            break;
        }
    }
    u.ublesscnt = rnz(350);
    kick_on_butt = u.uevent.udemigod ? 1 : 0;
    if (u.uevent.uhand_of_elbereth) kick_on_butt++;
    if (kick_on_butt)
        u.ublesscnt += kick_on_butt * rnz(1000);
    if (game.moves > 100000) {
        let incr = Math.trunc((game.moves - 100000) / 100);
        const largest_ublesscnt_incr = LARGEST_INT - u.ublesscnt;
        if (incr > largest_ublesscnt_incr)
            incr = largest_ublesscnt_incr;
        u.ublesscnt += incr;
    }
}
// C ref: pray.c:1386
function water_prayer(bless_water) {
    const u = game.u;
    const loc = levl(u.ux, u.uy);
    let changed = 0;
    let other = false;
    const bc_known = !(Blind() || Hallucination());
    // Iterate objects at location
    const objs = game.level?.objects?.[u.ux]?.[u.uy];
    for (let otmp = objs; otmp; otmp = otmp.nexthere) {
        if (otmp.otyp === POT_WATER
            && (bless_water ? !otmp.blessed : !otmp.cursed)) {
            otmp.blessed = bless_water ? 1 : 0;
            otmp.cursed = bless_water ? 0 : 1;
            otmp.bknown = bc_known ? 1 : 0;
            changed += (otmp.quan || 1);
        } else if (otmp.oclass === POTION_CLASS) {
            other = true;
        }
    }
    // Note: this is sync in C; we don't need pline here since it's called
    // from prayer_done which is async — but C's water_prayer isn't async.
    // The pline would need to be awaited, but since C doesn't await, keep sync.
    // Actually C does call pline... we need to handle that.
    // For now, keep return value correct.
    return changed > 0;
}
// C ref: pray.c:1414
async function godvoice_fn(g_align, words) {
    let quot = '';
    if (words) {
        quot = '"';
    } else {
        words = '';
    }
    await pline_The(`voice of ${align_gname(g_align)} ${ROLL_FROM(godvoices)}: ${quot}${words}${quot}`);
}
// C ref: pray.c:1428
async function gods_angry_fn(g_align) {
    await godvoice_fn(g_align, 'Thou hast angered me.');
}
// C ref: pray.c:1435
async function gods_upset(g_align) {
    const u = game.u;
    if (g_align === u.ualign.type)
        u.ugangr++;
    else if (u.ugangr)
        u.ugangr--;
    await angrygods(g_align);
}
// C ref: pray.c:1445
async function consume_offering(otmp) {
    if (Hallucination()) {
        switch (rn2(3)) {
        case 0:
            await Your('sacrifice sprouts wings and a propeller and roars away!');
            break;
        case 1:
            await Your('sacrifice puffs up, swelling bigger and bigger, and pops!');
            break;
        case 2:
            await Your('sacrifice collapses into a cloud of dancing particles and fades away!');
            break;
        }
    } else if (Blind() && game.u.ualign.type === A_LAWFUL) {
        await Your('sacrifice disappears!');
    } else {
        await Your(`sacrifice is consumed in a ${(game.u.ualign.type === A_LAWFUL) ? 'flash of light' : (game.u.ualign.type === A_NEUTRAL) ? 'plume of smoke' : 'burst of flame'}!`);
    }
    if (carried(otmp))
        useup(otmp);
    else
        useupf(otmp, 1);
    exercise(A_WIS, true);
}
// C ref: pray.c:1479
async function offer_too_soon(altaralign) {
    if (altaralign === A_NONE && Inhell()) {
        await gods_upset(A_NONE);
        return;
    }
    await You_feel(`${Hallucination() ? 'homesick' : (altaralign === game.u.ualign.type) ? 'an urge to return to the surface' : 'ashamed'}.`);
}
// C ref: pray.c:1500
export async function desecrate_altar(highaltar, altaralign) {
    const u = game.u;
    if (altaralign === u.ualign.type) {
        adjalign(-20);
        u.ugangr += 5;
    }
    await You_feel('the air around you grow charged...');
    await pline(`Suddenly, you realize that ${align_gname(altaralign)} has noticed you...`);
    const gvbuf = `So, mortal!  You dare desecrate my ${highaltar ? 'High Temple' : 'altar'}!`;
    await godvoice_fn(altaralign, gvbuf);
    await god_zaps_you(altaralign);
}
// C ref: pray.c:1529
async function offer_real_amulet(otmp, altaralign) {
    const u = game.u;
    const cloud_of_smoke = (color) => `A cloud of ${color} smoke surrounds you...`;
    if (u.uamul === otmp) {
        // Amulet_off(); TODO
    }
    if (carried(otmp))
        useup(otmp);
    else
        useupf(otmp, 1);
    await You(`offer the Amulet of Yendor to ${a_gname()}...`);
    if (altaralign === A_NONE) {
        if (u.ualign.record > -99) u.ualign.record = -99;
        await pline('An invisible choir chants, and you are bathed in darkness...');
        await pline(`${Moloch} shrugs and retains dominion over ${u_gname()},`);
        await pline('then mercilessly snuffs out your life.');
        await done(DIED);
        await pline(`${Moloch} snarls and tries again...`);
        await fry_by_god(A_NONE, true);
        await pline(cloud_of_smoke(hcolor(NH_BLACK)));
        await done(ESCAPED);
    } else if (u.ualign.type !== altaralign) {
        adjalign(-99);
        await pline(`${a_gname()} accepts your gift, and gains dominion over ${u_gname()}...`);
        await pline(`${u_gname()} is enraged...`);
        await pline(`Fortunately, ${a_gname()} permits you to live...`);
        await pline(cloud_of_smoke(hcolor(NH_ORANGE)));
        await done(ESCAPED);
    } else {
        u.uevent.ascended = 1;
        adjalign(10);
        await pline('An invisible choir sings, and you are bathed in radiance...');
        await godvoice_fn(altaralign, 'Mortal, thou hast done well!');
        await display_nhwindow(game.WIN_MESSAGE, false);
        SetVoice(null, 0, 80, voice_deity);
        await verbalize('In return for thy service, I grant thee the gift of Immortality!');
        await You(`ascend to the status of Demigod${game.flags?.female ? 'dess' : ''}...`);
        await done(ASCENDED);
    }
}
// C ref: pray.c:1591
async function offer_negative_valued(highaltar, altaralign) {
    if (altaralign !== game.u.ualign.type && highaltar) {
        await desecrate_altar(highaltar, altaralign);
    } else {
        await gods_upset(altaralign);
    }
}
// C ref: pray.c:1601
async function offer_fake_amulet(otmp, highaltar, altaralign) {
    if (!highaltar && !otmp.known) {
        await offer_too_soon(altaralign);
        return;
    }
    Soundeffect(0, 100);
    await You_hear('a nearby thunderclap.');
    if (!otmp.known) {
        await You(`realize you have made a ${Hallucination() ? 'boo-boo' : 'mistake'}.`);
        otmp.known = true;
        change_luck(-1);
    } else {
        if (Deaf())
            await pline('Oh, no.');
        change_luck(-3);
        adjalign(-1);
        game.u.ugangr += 3;
        await offer_negative_valued(highaltar, altaralign);
    }
}
// C ref: pray.c:1630
async function offer_different_alignment_altar(otmp, altaralign) {
    const u = game.u;
    if (ugod_is_angry() || (altaralign === A_NONE && Inhell())) {
        if (u.ualignbase?.[0] === u.ualignbase?.[1] /* A_CURRENT === A_ORIGINAL */
            && altaralign !== A_NONE) {
            await You(`have a strong feeling that ${u_gname()} is angry...`);
            await consume_offering(otmp);
            await pline(`${a_gname()} accepts your allegiance.`);
            uchangealign(altaralign, A_CG_CONVERT);
            change_luck(-3);
            u.ublesscnt += 300;
        } else {
            u.ugangr += 3;
            adjalign(-5);
            await pline(`${a_gname()} rejects your sacrifice!`);
            await godvoice_fn(altaralign, 'Suffer, infidel!');
            change_luck(-5);
            await adjattrib(A_WIS, -2, true);
            if (!Inhell())
                await angrygods(u.ualign.type);
        }
    } else {
        await consume_offering(otmp);
        await You(`sense a conflict between ${u_gname()} and ${a_gname()}.`);
        if (rn2(8 + u.ulevel) > 5) {
            await You_feel(`the power of ${u_gname()} increase.`);
            exercise(A_WIS, true);
            change_luck(1);
            const shrine = on_shrine();
            const loc = levl(u.ux, u.uy);
            loc.altarmask = Align2amask(u.ualign.type);
            if (shrine) loc.altarmask |= AM_SHRINE;
            newsym(u.ux, u.uy);
            if (!Blind())
                await pline_The(`altar glows ${hcolor((u.ualign.type === A_LAWFUL) ? NH_WHITE : u.ualign.type ? NH_BLACK : 'gray')}.`);
            if (rnl(u.ulevel) > 6 && u.ualign.record > 0
                && rnd(u.ualign.record) > Math.trunc((3 * ALIGNLIM) / 4))
                await summon_minion(altaralign, true);
            const pri = findpriest(temple_occupied(u.urooms));
            if (pri && !p_coaligned(pri))
                await angry_priest();
        } else {
            await pline(`Unluckily, you feel the power of ${u_gname()} decrease.`);
            change_luck(-1);
            exercise(A_WIS, false);
            if (rnl(u.ulevel) > 6 && u.ualign.record > 0
                && rnd(u.ualign.record) > Math.trunc((7 * ALIGNLIM) / 8))
                await summon_minion(altaralign, true);
        }
    }
}
// C ref: pray.c:1697
async function sacrifice_your_race(otmp, highaltar, altaralign) {
    const u = game.u;
    if (is_demon(game.player?.data)) {
        await You('find the idea very satisfying.');
        exercise(A_WIS, true);
    } else if (u.ualign.type !== A_CHAOTIC) {
        await pline("You'll regret this infamous offense!");
        exercise(A_WIS, false);
    }
    if (highaltar
        && (altaralign !== A_CHAOTIC || u.ualign.type !== A_CHAOTIC)) {
        await desecrate_altar(highaltar, altaralign);
        return;
    } else if (altaralign !== A_CHAOTIC && altaralign !== A_NONE) {
        await pline_The(`altar is stained with ${game.urace?.adj || 'human'} blood.`);
        levl(u.ux, u.uy).altarmask = AM_CHAOTIC;
        newsym(u.ux, u.uy);
        await angry_priest();
    } else {
        let demonless_msg;
        if (altaralign === A_CHAOTIC && u.ualign.type !== A_CHAOTIC) {
            await pline(`The blood floods the altar, which vanishes in ${an(hcolor(NH_BLACK))} cloud!`);
            levl(u.ux, u.uy).typ = 0; // ROOM
            levl(u.ux, u.uy).altarmask = 0;
            newsym(u.ux, u.uy);
            await angry_priest();
            demonless_msg = 'cloud dissipates';
        } else {
            await pline_The('blood covers the altar!');
            change_luck(altaralign === A_NONE ? -2 : 2);
            demonless_msg = 'blood coagulates';
        }
        const pm = dlord(altaralign);
        if (pm !== NON_PM) {
            const dmon = await makemon(mons[pm], u.ux, u.uy, MM_NOMSG);
            if (dmon) {
                let dbuf = a_monnam(dmon);
                if (dbuf.toLowerCase() === 'it') dbuf = 'something dreadful';
                else dmon.mstrategy = (dmon.mstrategy || 0) & ~STRAT_APPEARMSG;
                await You(`have summoned ${dbuf}!`);
                if (sgn(u.ualign.type) === sgn(dmon.data.maligntyp))
                    dmon.mpeaceful = true;
                await You('are terrified, and unable to move.');
                nomul(-3);
                game.multi_reason = 'being terrified of a demon';
                game.nomovemsg = null;
            } else {
                await pline_The(`${demonless_msg}.`);
            }
        } else {
            await pline_The(`${demonless_msg}.`);
        }
    }
    if (u.ualign.type !== A_CHAOTIC) {
        adjalign(-5);
        u.ugangr += 3;
        await adjattrib(A_WIS, -1, true);
        if (!Inhell())
            await angrygods(u.ualign.type);
        change_luck(-5);
    } else {
        adjalign(5);
    }
    if (carried(otmp))
        useup(otmp);
    else
        useupf(otmp, 1);
}
// C ref: pray.c:1780
async function bestow_artifact(max_giftvalue) {
    const u = game.u;
    const nartifacts = nartifact_exist();
    let do_bestow = u.ulevel > 2 && Luck() >= 0;
    if (do_bestow) {
        do_bestow = !rn2(6 + (2 * u.ugifts * nartifacts));
    }
    if (do_bestow) {
        const otmp = mk_artifact(null, a_align(u.ux, u.uy), max_giftvalue, true);
        if (otmp) {
            artifact_origin(otmp, 0);
            if (otmp.spe < 0) otmp.spe = 0;
            if (otmp.cursed) uncurse(otmp);
            otmp.oerodeproof = true;
            let buf = Hallucination() ? 'a doodad' : Blind() ? 'an object' : ansimpleoname(otmp);
            if (!Blind()) buf += ` named ${bare_artifactname(otmp)}`;
            // at_your_feet is async but bestow_artifact is sync in C...
            // For now, simplified.
            await dropy(otmp);
            // godvoice is async too... simplified
            u.ugifts++;
            u.ublesscnt = rnz(300 + (50 * nartifacts));
            exercise(A_WIS, true);
            unrestrict_weapon_skill(weapon_type(otmp));
            if (!Hallucination() && !Blind()) {
                observe_object(otmp);
                makeknown(otmp.otyp);
                discover_artifact(otmp.oartifact);
            }
            return true;
        }
    }
    return false;
}
// C ref: pray.c:1838
function sacrifice_value(otmp) {
    let value = 0;
    if (otmp.corpsenm === PM_ACID_BLOB
        || (game.moves <= peek_at_iced_corpse_age(otmp) + 50)) {
        value = mons[otmp.corpsenm].difficulty + 1;
        if (otmp.oeaten) value = eaten_stat(value, otmp);
    }
    return value;
}
// C ref: pray.c:1852
export async function dosacrifice() {
    const u = game.u;
    let otmp;
    let highaltar;
    const altaralign = a_align(u.ux, u.uy);
    if (!on_altar() || u.uswallow) {
        await You(`are not ${(Levitation() || Flying()) ? 'over' : 'on'} an altar.`);
        return ECMD_OK;
    } else if (Confusion() || Stunned()) {
        await You('are too impaired to perform the rite.');
        return ECMD_OK;
    }
    highaltar = !!(levl(u.ux, u.uy).altarmask & AM_SANCTUM);
    otmp = await floorfood('sacrifice', 1);
    if (!otmp) return ECMD_OK;
    if (otmp.otyp === AMULET_OF_YENDOR) {
        if (!highaltar) {
            await offer_too_soon(altaralign);
            return ECMD_TIME;
        } else {
            await offer_real_amulet(otmp, altaralign);
            // NOTREACHED
        }
    }
    if (otmp.otyp === FAKE_AMULET_OF_YENDOR) {
        await offer_fake_amulet(otmp, highaltar, altaralign);
        return ECMD_TIME;
    }
    if (otmp.otyp === CORPSE) {
        await offer_corpse(otmp, highaltar, altaralign);
        return ECMD_TIME;
    }
    await pline(nothing_happens);
    return ECMD_TIME;
}
// C ref: pray.c:1898
async function eval_offering(otmp, altaralign) {
    const u = game.u;
    let value = sacrifice_value(otmp);
    if (!value) return 0;
    const ptr = mons[otmp.corpsenm];
    if (is_undead(ptr)) {
        if (u.ualign.type !== A_CHAOTIC
            || (ptr === mons[PM_WRAITH] && u.uconduct?.unvegetarian))
            value += 1;
    } else if (is_unicorn(ptr)) {
        const unicalign = sgn(ptr.maligntyp);
        if (unicalign === altaralign) {
            // insult
            await adjattrib(A_WIS, -1, true);
            return -1;
        } else if (u.ualign.type === altaralign) {
            if (u.ualign.record < ALIGNLIM)
                ; // You_feel message - sync issue, skip for now
            adjalign(5);
            value += 3;
        } else if (unicalign === u.ualign.type) {
            u.ualign.record = -1;
            value = 1;
        } else {
            value += 3;
        }
    }
    return value;
}
// C ref: pray.c:1958
async function offer_corpse(otmp, highaltar, altaralign) {
    const u = game.u;
    let value;
    if (!u.uconduct) u.uconduct = {};
    if (!u.uconduct.gnostic) u.uconduct.gnostic = 0;
    u.uconduct.gnostic++;
    feel_cockatrice(otmp, true);
    if (await rider_corpse_revival(otmp, false)) return;
    const ptr = mons[otmp.corpsenm];
    if (your_race(ptr)) {
        await sacrifice_your_race(otmp, highaltar, altaralign);
        return;
    }
    if (has_omonst(otmp)) {
        const mtmp = get_mtraits(otmp, false);
        if (mtmp && mtmp.mtame) {
            await pline('So this is how you repay loyalty?');
            adjalign(-3);
            // HAggravate_monster |= FROMOUTSIDE; TODO
            await offer_negative_valued(highaltar, altaralign);
            return;
        }
    }
    value = await eval_offering(otmp, altaralign);
    if (value === 0) {
        await pline(nothing_happens);
        return;
    }
    if (value < 0) {
        await offer_negative_valued(highaltar, altaralign);
        return;
    }
    if (altaralign !== u.ualign.type && highaltar) {
        await desecrate_altar(highaltar, altaralign);
        return;
    }
    if (u.ualign.type !== altaralign) {
        await offer_different_alignment_altar(otmp, altaralign);
        return;
    }
    await consume_offering(otmp);
    // Brownie points
    if (u.ugangr) {
        const saved_anger = u.ugangr;
        u.ugangr -= Math.trunc((value * (u.ualign.type === A_CHAOTIC ? 2 : 3)) / MAXVALUE);
        if (u.ugangr < 0) u.ugangr = 0;
        if (u.ugangr !== saved_anger) {
            if (u.ugangr) {
                await pline(`${u_gname()} seems ${Hallucination() ? 'groovy' : 'slightly mollified'}.`);
                if (Luck() < 0) change_luck(1);
            } else {
                await pline(`${u_gname()} seems ${Hallucination() ? 'cosmic (not a new fact)' : 'mollified'}.`);
                if (Luck() < 0) u.uluck = 0;
            }
        } else {
            if (Hallucination())
                await pline_The('gods seem tall.');
            else
                await You('have a feeling of inadequacy.');
        }
    } else if (ugod_is_angry()) {
        if (value > MAXVALUE) value = MAXVALUE;
        if (value > -u.ualign.record) value = -u.ualign.record;
        adjalign(value);
        await You_feel('partially absolved.');
    } else if (u.ublesscnt > 0) {
        const saved_cnt = u.ublesscnt;
        u.ublesscnt -= Math.trunc((value * (u.ualign.type === A_CHAOTIC ? 500 : 300)) / MAXVALUE);
        if (u.ublesscnt < 0) u.ublesscnt = 0;
        if (u.ublesscnt !== saved_cnt) {
            if (u.ublesscnt) {
                if (Hallucination())
                    await You('realize that the gods are not like you and I.');
                else
                    await You('have a hopeful feeling.');
                if (Luck() < 0) change_luck(1);
            } else {
                if (Hallucination())
                    await pline('Overall, there is a smell of fried onions.');
                else
                    await You('have a feeling of reconciliation.');
                if (Luck() < 0) u.uluck = 0;
            }
        }
    } else {
        if (await bestow_artifact(value)) return;
        const orig_luck = Luck();
        let luck_increase = Math.trunc((value * LUCKMAX) / (MAXVALUE * 2));
        if (orig_luck > value)
            luck_increase = 0;
        else if (orig_luck + luck_increase > value)
            luck_increase = value - orig_luck;
        change_luck(luck_increase);
        if (Luck() < 0) u.uluck = 0;
        if (Luck() !== orig_luck) {
            if (Blind())
                await You(`think ${Something.toLowerCase()} brushed your ${body_part(FOOT)}.`);
            else
                await You(Hallucination()
                    ? `see crabgrass at your ${makeplural(body_part(FOOT))}.  A funny thing in a dungeon.`
                    : `glimpse a four-leaf clover at your ${makeplural(body_part(FOOT))}.`);
        }
    }
}
// C ref: pray.c:2122
export async function can_pray(praying) {
    const u = game.u;
    let alignment;
    game.p_aligntyp = on_altar() ? a_align(u.ux, u.uy) : u.ualign.type;
    game.p_trouble = in_trouble();
    if (is_demon(game.player?.data)
        && (game.p_aligntyp === A_LAWFUL || game.p_aligntyp !== A_NEUTRAL)) {
        if (praying)
            await pline_The(`very idea of praying to a ${game.p_aligntyp ? 'lawful' : 'neutral'} god is repugnant to you.`);
        return false;
    }
    if (praying)
        await You(`begin praying to ${align_gname(game.p_aligntyp)}.`);
    if (u.ualign.type && u.ualign.type === -game.p_aligntyp)
        alignment = -u.ualign.record;
    else if (u.ualign.type !== game.p_aligntyp)
        alignment = Math.trunc(u.ualign.record / 2);
    else
        alignment = u.ualign.record;
    if (game.p_aligntyp === A_NONE)
        game.p_type = -2;
    else if ((game.p_trouble > 0) ? (u.ublesscnt > 200)
             : (game.p_trouble < 0) ? (u.ublesscnt > 100)
               : (u.ublesscnt > 0))
        game.p_type = 0;
    else if (Luck() < 0 || u.ugangr || alignment < 0)
        game.p_type = 1;
    else {
        if (on_altar() && u.ualign.type !== game.p_aligntyp)
            game.p_type = 2;
        else
            game.p_type = 3;
    }
    if (is_undead(game.player?.data) && !Inhell()
        && (game.p_aligntyp === A_LAWFUL
            || (game.p_aligntyp === A_NEUTRAL && !rn2(10))))
        game.p_type = -1;
    return !praying ? (game.p_type === 3 && !Inhell()) : true;
}
// C ref: pray.c:2176
async function pray_revive() {
    // Iterate objects at hero location
    const u = game.u;
    const objs = game.level?.objects?.[u.ux]?.[u.uy];
    let otmp;
    for (otmp = objs; otmp; otmp = otmp.nexthere) {
        if ((otmp.otyp === CORPSE || otmp.otyp === STATUE)
            && has_omonst(otmp)
            && OMONST(otmp)?.mtame && !OMONST(otmp)?.isminion)
            break;
    }
    if (!otmp) return false;
    if (otmp.otyp === CORPSE)
        return (await revive(otmp, true)) !== null;
    else
        return animate_statue(otmp, u.ux, u.uy, ANIMATE_SPELL, null) !== null;
}
// C ref: pray.c:2197
export async function dopray() {
    const u = game.u;
    let ok;
    if (ParanoidPray()) {
        ok = await paranoid_query(ParanoidConfirm, 'Are you sure you want to pray?');
        if (!ok) return ECMD_OK;
    }
    if (!u.uconduct) u.uconduct = {};
    if (!u.uconduct.gnostic) u.uconduct.gnostic = 0;
    u.uconduct.gnostic++;
    if (!(await can_pray(true)))
        return ECMD_OK;
    // C ref: pray.c:2233 — wizard mode force-please override
    if (game.flags?.debug && game.p_type >= 0) {
        const ok = (await y_n('Force the gods to be pleased?')) === 'y';
        if (ok) {
            u.ublesscnt = 0;
            if (u.uluck < 0) u.uluck = 0;
            if (u.ualign.record <= 0) u.ualign.record = 1;
            u.ugangr = 0;
            if (game.p_type < 2)
                game.p_type = 3;
        }
    }
    nomul(-3);
    game.multi_reason = 'praying';
    game.nomovemsg = 'You finish your prayer.';
    game.afternmv = prayer_done;
    if (game.p_type === 3 && !Inhell()) {
        if (!Blind())
            await You('are surrounded by a shimmering light.');
        u.uinvulnerable = true;
    }
    return ECMD_TIME;
}
// C ref: pray.c:2275
async function prayer_done() {
    const u = game.u;
    const alignment = game.p_aligntyp;
    u.uinvulnerable = false;
    if (game.p_type === -2) {
        await You(`${!Deaf() ? 'hear' : 'intuit'} diabolical laughter all around you...`);
        wake_nearby(false);
        adjalign(-2);
        exercise(A_WIS, false);
        if (!Inhell()) {
            await pline('Nothing else happens.');
            return 1;
        }
    } else if (game.p_type === -1) {
        await godvoice_fn(alignment,
            (alignment === A_LAWFUL)
                ? 'Vile creature, thou durst call upon me?'
                : 'Walk no more, perversion of nature!');
        await You_feel('like you are falling apart.');
        await rehumanize();
        await losehp(rnd(20), 'residual undead turning effect', KILLED_BY_AN);
        exercise(A_CON, false);
        return 1;
    }
    if (Inhell()) {
        await pline(`Since you are in Gehennom, ${align_gname(alignment)} can't help you.`);
        if (u.ualign.record <= 0 || rnl(u.ualign.record))
            await angrygods(u.ualign.type);
        return 0;
    }
    if (game.p_type === 0) {
        if (on_altar() && u.ualign.type !== alignment)
            water_prayer(false);
        u.ublesscnt += rnz(250);
        change_luck(-3);
        await gods_upset(u.ualign.type);
    } else if (game.p_type === 1) {
        if (on_altar() && u.ualign.type !== alignment)
            water_prayer(false);
        await angrygods(u.ualign.type);
    } else if (game.p_type === 2) {
        if (water_prayer(false)) {
            u.ublesscnt += rnz(250);
            change_luck(-3);
            await gods_upset(u.ualign.type);
        } else {
            await pleased(alignment);
        }
    } else {
        if (on_altar()) {
            await pray_revive();
            water_prayer(true);
        }
        await pleased(alignment);
    }
    return 1;
}
// C ref: pray.c:2345
async function maybe_turn_mon_iter(mtmp) {
    if (!couldsee(mtmp.mx, mtmp.my)
        || mdistu(mtmp) > game.turn_undead_range)
        return;
    if (!mtmp.mpeaceful
        && (is_undead(mtmp.data) || is_vampshifter(mtmp)
            || (is_demon(mtmp.data) && (game.u.ulevel > Math.trunc(MAXULEV / 2))))) {
        mtmp.msleeping = 0;
        if (Confusion()) {
            if (!game.turn_undead_msg_cnt++) {
                // pline('Unfortunately, your voice falters.'); // sync
            }
            mtmp.mflee = 0;
            mtmp.mfrozen = 0;
            mtmp.mcanmove = 1;
        } else if (!resist(mtmp, '\0', 0, 1 /* TELL */)) {
            let xlev = 6;
            switch (mtmp.data.mlet) {
            case S_LICH: xlev += 2; // fall through
            case S_GHOST: xlev += 2; // fall through
            case S_VAMPIRE: xlev += 2; // fall through
            case S_WRAITH: xlev += 2; // fall through
            case S_MUMMY: xlev += 2; // fall through
            case S_ZOMBIE:
                if (game.u.ulevel >= xlev && !resist(mtmp, '\0', 0, 0 /* NOTELL */)) {
                    if (game.u.ualign.type === A_CHAOTIC) {
                        mtmp.mpeaceful = 1;
                        set_malign(mtmp);
                    } else {
                        await killed(mtmp);
                    }
                    break;
                }
                // fall through
            default:
                await monflee(mtmp, 0, false, true);
                break;
            }
        }
    }
}
// C ref: pray.c:2412
export async function doturn() {
    const u = game.u;
    let Gname;
    if (!Role_if(PM_CLERIC) && !Role_if(PM_KNIGHT)) {
        if (known_spell(SPE_TURN_UNDEAD) >= 0)
            return await spelleffects(SPE_TURN_UNDEAD, false, false);
        await You("don't know how to turn undead!");
        return ECMD_OK;
    }
    if (!u.uconduct) u.uconduct = {};
    if (!u.uconduct.gnostic) u.uconduct.gnostic = 0;
    u.uconduct.gnostic++;
    Gname = halu_gname(u.ualign.type);
    if (!can_chant(game.player)) {
        await You(`are ${Strangled() ? 'not able to call' : 'incapable of calling'} upon ${Gname} to turn aside evilness.`);
        return (u.uconduct.gnostic === 1) ? ECMD_TIME : ECMD_OK;
    }
    if ((u.ualign.type !== A_CHAOTIC
         && (is_demon(game.player?.data)
             || is_undead(game.player?.data) || is_vampshifter(game.player)))
        || u.ugangr > 6) {
        await pline(`For some reason, ${Gname} seems to ignore you.`);
        aggravate();
        exercise(A_WIS, false);
        return ECMD_TIME;
    }
    if (Inhell()) {
        await pline(`Since you are in Gehennom, ${Gname} ${Gname === Moloch ? "won't" : "can't"} help you.`);
        aggravate();
        return ECMD_TIME;
    }
    await pline(`Calling upon ${Gname}, you chant an arcane formula.`);
    exercise(A_WIS, true);
    game.turn_undead_range = BOLT_LIM + Math.trunc(u.ulevel / 5);
    game.turn_undead_range *= game.turn_undead_range;
    game.turn_undead_msg_cnt = 0;
    await iter_mons(maybe_turn_mon_iter);
    nomul(-(5 - Math.trunc((u.ulevel - 1) / 6)));
    game.multi_reason = 'trying to turn the monsters';
    game.nomovemsg = You_can_move_again;
    return ECMD_TIME;
}
// C ref: pray.c:2489
export function altarmask_at(x, y) {
    let res = 0;
    if (isok(x, y)) {
        const mon = m_at(x, y);
        if (mon && M_AP_TYPE(mon) === M_AP_FURNITURE
            && mon.mappearance === S_altar)
            res = mon.mcorpsenm || 0;
        else if (IS_ALTAR(levl(x, y).typ))
            res = levl(x, y).altarmask;
    }
    return res;
}
// C ref: pray.c:2506
export function a_gname() {
    return a_gname_at(game.u.ux, game.u.uy);
}
// C ref: pray.c:2512
export function a_gname_at(x, y) {
    if (!IS_ALTAR(levl(x, y).typ))
        return null;
    return align_gname(a_align(x, y));
}
// C ref: pray.c:2522
export function u_gname() {
    return align_gname(game.u.ualign.type);
}
// C ref: pray.c:2529
export function align_gname(alignment) {
    let gnam;
    switch (alignment) {
    case A_NONE:
        gnam = Moloch;
        break;
    case A_LAWFUL:
        gnam = game.urole?.lgod || 'Anu';
        break;
    case A_NEUTRAL:
        gnam = game.urole?.ngod || 'Ishtar';
        break;
    case A_CHAOTIC:
        gnam = game.urole?.cgod || 'Anshar';
        break;
    default:
        gnam = 'someone';
        break;
    }
    if (gnam.charAt(0) === '_') gnam = gnam.slice(1);
    return gnam;
}
// C ref: pray.c:2557
export function halu_gname(alignment) {
    if (!Hallucination()) return align_gname(alignment);
    let gnam = null;
    let which;
    // Use display rng for hallucination
    // Simplified: just use rn2 since rn2_on_display_rng not available
    do {
        which = randrole(true);
    } while (!game.roles?.[which]?.lgod);
    // If roles not available, fall back
    if (!game.roles?.[which]?.lgod) return align_gname(alignment);
    switch (rn2(9)) { // should be rn2_on_display_rng
    case 0: case 1: gnam = game.roles[which].lgod; break;
    case 2: case 3: gnam = game.roles[which].ngod; break;
    case 4: case 5: gnam = game.roles[which].cgod; break;
    case 6: case 7: gnam = hallu_gods[rn2(hallu_gods.length)]; break;
    case 8: gnam = Moloch; break;
    default: gnam = 'your Friend the Computer'; break;
    }
    if (!gnam) gnam = 'your Friend the Computer';
    if (gnam.charAt(0) === '_') gnam = gnam.slice(1);
    return gnam;
}
// C ref: pray.c:2627
export function align_gtitle(alignment) {
    let gnam;
    const result = 'god';
    switch (alignment) {
    case A_LAWFUL: gnam = game.urole?.lgod; break;
    case A_NEUTRAL: gnam = game.urole?.ngod; break;
    case A_CHAOTIC: gnam = game.urole?.cgod; break;
    default: gnam = null; break;
    }
    if (gnam && gnam.charAt(0) === '_') return 'goddess';
    return result;
}
// C ref: pray.c:2651
export async function altar_wrath(x, y) {
    const u = game.u;
    const altaralign = a_align(x, y);
    if (u.ualign.type === altaralign && u.ualign.record > -rn2(4)) {
        await godvoice_fn(altaralign, 'How darest thou desecrate my altar!');
        await adjattrib(A_WIS, -1, false);
        u.ualign.record--;
    } else {
        await pline(`${!Deaf() ? 'A voice (could it be ' : 'Despite your deafness, you seem to hear '}${align_gname(altaralign)}${!Deaf() ? '?) whispers' : ' say'}:`);
        SetVoice(null, 0, 80, voice_deity);
        await verbalize('Thou shalt pay, infidel!');
        if (Luck() > -5 && rn2(Luck() + 6))
            change_luck(rn2(20) ? -1 : -2);
    }
}
// C ref: pray.c:2676
function blocked_boulder(dx, dy) {
    const u = game.u;
    let count = 0;
    const objs = game.level?.objects?.[u.ux + dx]?.[u.uy + dy];
    for (let otmp = objs; otmp; otmp = otmp.nexthere) {
        if (otmp.otyp === BOULDER)
            count += (otmp.quan || 1);
    }
    const nx = u.ux + 2 * dx;
    const ny = u.uy + 2 * dy;
    switch (count) {
    case 0: return false;
    case 1: break;
    case 2:
        if (is_pool_or_lava(nx, ny)) break;
        // fall through
    default: return true;
    }
    if (dx && dy && Sokoban()) return true;
    if (!isok(nx, ny)) return true;
    if (IS_OBSTRUCTED(levl(nx, ny).typ)) return true;
    if (sobj_at(BOULDER, nx, ny)) return true;
    return false;
}
// Alias for backward compat (C name is godvoice, but we used godvoice_fn internally)
export { godvoice_fn as godvoice };
export function init_pray_globals() {
    game.turn_undead_range = 0;
    game.turn_undead_msg_cnt = 0;
}