Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 | 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 56x 55x 7x 11x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 3x 1x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 73x 6x 73x 2x 2x 2x 2x 2x 2x 1x 2x 2x 1x 2x 2x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 838x 838x 838x 838x 838x 838x 838x 838x 838x 838x 838x 290x 290x 838x 838x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 9x 3x 29x 48728x 889x 18x 73x 73x 73x 73x 73x 73x 73x 73x 5x 5x 5x 5x 5x 2x 2x 5x 5x 5x 5x 5x 73x 73x 73x 73x 73x 73x 73x 23x 23x 37x 37x 37x 37x 23x 11x 4x 4x 4x 11x 23x 23x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 27x 27x 27x 27x 27x 27x 73x 4x 4x 27x 27x 27x 4x 73x 73x 73x 1x 1x 1x 1x 1x 73x 2x 2x 1x 1x 1x 2x 73x 1x 1x 1x 4x 1x 73x 73x 73x 73x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 73x 73x 73x 73x 73x 73x 73x 12x 12x 12x 12x 12x 12x 73x 73x 73x 568x 568x 42x 42x 42x 42x 10x 7x 7x 7x 7x 10x 35x 568x 568x 561x 568x 73x 73x 73x 73x 73x 73x 73x 73x 1121x 1121x 73x 73x 73x 73x 120x 114x 114x 114x 114x 120x 73x 73x 73x 73x 526x 526x 526x 526x 526x 526x 526x 526x 526x 526x 526x 526x 526x 526x 526x 526x 526x 526x 73x 73x 73x 3x 3x 3x 3x 3x 3x 3x 73x 73x 73x 3x 3x 3x 3x 3x 3x 3x 3x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 73x 73x 73x 7x 7x 7x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 1x 1x 1x 1x 1x 1x 1x 73x 73x 73x 73x 73x 73x 73x 73x 105x 105x 105x 105x 105x 105x 105x 73x 73x 73x 73x 557x 557x 557x 31x 31x 4x 4x 4x 31x 31x 557x 557x 73x 73x 73x 73x 73x 73x 73x 73x 4x 4x 4x 73x 73x 73x 73x 2x 2x 2x 2x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 48169x 48169x 48169x 48169x 48169x 48169x 35019x 33189x 35019x 35019x 48169x 48169x 48169x 35019x 35019x 5453x 5453x 48169x 48169x 48169x 48169x 48169x 48169x 13156x 48169x 2x 2x 48169x 48169x 35011x 35011x 48169x 2x 2x 2x 6739x 6739x 2x 2x 2x 6739x 6739x 2x 6739x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 48169x 73x 73x 73x 73x 3402x 3402x 80x 334x 7x 3402x 3402x 7x 7x 3402x 3402x 73x 73x 73x 73x 73x 73x 73x 73x 20x 20x 20x 20x 73x 73x 73x 73x 50x 50x 73x 73x 73x 73x 28x 28x 28x 28x 28x 73x 73x 73x 73x 30x 30x 30x 30x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 73x 73x 73x 18x 18x 18x 18x 18x 18x 18x 3x 3x 3x 18x 18x 9x 9x 9x 18x 18x 18x 18x 73x 73x 73x 73x 73x 73x 73x 73x 73x 18x 18x 18x 18x 18x 18x 14x 14x 3x 3x 3x 14x 18x 18x 18x 18x 9x 9x 18x 18x 18x 18x 18x 9x 9x 9x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 25x 25x 25x 25x 25x 25x 25x 73x 73x 73x 73x 25x 25x 73x 73x 73x 73x 8x 8x 4x 4x 4x 4x 4x 8x 8x 8x 8x 8x 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 4x 4x 4x 73x 73x 73x 73x 73x 73x 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 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 73x 73x 73x 73x 73x 73x 73x 1x 1x 1x 1x 1x 1x 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 8x 1x 1x 1x 8x 1x 1x 8x 1x 1x 1x 8x 73x 73x 73x 73x 73x 73x 73x 73x 11x 11x 11x 11x 73x 73x 73x 73x 73x 73x 73x 11x 11x 11x 11x 11x 11x 11x 73x 73x 73x 73x 11x 11x 11x 73x 73x 73x 73x 73x 73x 187x 187x 187x 187x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 187x 187x 187x 73x 73x 73x 73x 73x 73x 73x 73x 24612x 24612x 24612x 24612x 24612x 24612x 24612x 24612x 24612x 73x 73x 73x 73x 13623x 13623x 13623x 73x 73x 73x 73x 23x 23x 23x 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 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 30x 30x 30x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 73x 73x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 73x 73x 73x 73x 73x 73x 73x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 73x 73x 73x 5x 5x 5x 5x 1x 5x 5x 73x 73x 73x 5x 5x 5x 5x 5x 5x 5x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 5x 5x 5x 5x 5x 73x 73x 73x 4x 4x 4x 4x 4x 4x 4x 4x 5x 4x 4x 4x 4x 4x 4x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 73x 73x 73x 73x 73x 73x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 3x 4x 73x 73x 73x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 4x 4x 4x 4x 73x 73x 73x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 73x 73x 73x 73x 73x 73x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 4x 5x 5x 5x 4x 4x 4x 4x 4x 4x 3x 3x 3x 4x 4x 3x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 4x 4x 4x 3x 3x 3x 3x 3x 3x 4x 4x 2x 5x 73x 73x 73x 73x 8x 8x 8x 8x 8x 8x 8x 3x 3x 3x 3x 8x 8x 8x 8x 8x 8x 7x 7x 4x 4x 4x 4x 7x 7x 7x 7x 8x 8x 8x 4x 4x 2x 2x 2x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 8x 8x 5x 5x 5x 8x 5x 5x 8x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 8x 2x 2x 2x 2x 2x 5x 5x 5x 5x 8x 8x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 4x 4x 4x 4x 4x 73x 73x 73x 73x 73x 73x 3x 3x 3x 3x 3x 73x 73x 73x 73x 73x 73x 73x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 73x 73x 73x 73x 73x 73x | // shk.js — Shopkeeper mechanics (full port of shk.c)
// C ref: shk.c — 6127 lines
//
// Port of all shopkeeper functions: billing, pricing, shop AI,
// theft handling, damage repair, payment, selling, and predicates.
import { game } from './gstate.js';
import { enexto } from './makemon.js';
import {
SHOPBASE, SHOP_DOOR_COST, BILLSZ, ROOMOFFSET, NO_ROOM, SHARED, SHARED_PLUS,
ESHK, has_eshk,
OBJ_FREE, OBJ_FLOOR, OBJ_CONTAINED, OBJ_INVENT, OBJ_MINVENT,
OBJ_ONBILL, OBJ_BURIED, OBJ_DELETED,
CANDLESHOP,
TT_PIT, ACH_SHOP,
} from './const.js';
import { COIN_CLASS, FOOD_CLASS, BALL_CLASS, CHAIN_CLASS, WAND_CLASS, ARMOR_CLASS, WEAPON_CLASS, TOOL_CLASS, POTION_CLASS, SCROLL_CLASS, SPBOOK_CLASS, GEM_CLASS, AMULET_CLASS, RING_CLASS, GEMSTONE, GLASS, DIAMOND, RUBY, SAPPHIRE, EMERALD, AMETHYST, OPAL, JACINTH, BLACK_OPAL, CITRINE, AQUAMARINE, AMBER, TOPAZ, JET, CHRYSOBERYL, JASPER, FLUORITE, AGATE, JADE, FIRST_REAL_GEM, FIRST_GLASS_GEM, GOLD_PIECE, SchroedingersBox } from './objects.js';
import { Norep } from './do.js';
import { shk_move, shtypes } from './shknam.js';
import { NO_COLOR } from './terminal.js';
import { mons, PM_KNIGHT, PM_SAMURAI, PM_SHOPKEEPER, PM_TOURIST, PM_VALKYRIE } from './monsters.js';
import { sobj_at } from './mkobj.js';
import { BAG_OF_TRICKS, CANDELABRUM_OF_INVOCATION, DWARVISH_MATTOCK, HORN_OF_PLENTY, Is_container, MAGIC_LAMP, MAGIC_MARKER, OIL_LAMP, PICK_AXE } from './objects.js';
import { pushRngLogEntry, rn2, rnd } from './rng.js';
import { There, You, You_cant, You_feel, You_hear, Your, pline, pline1, pline_The, verbalize } from './pline.js';
import { impossible } from './pline.js';
import { ROLL_FROM, Sprintf, Strcat, Strcpy, dist2, distu, eos, nh_delay_output, on_level, plur, s_suffix, sgn } from './hacklib.js';
import { add_to_minv, dealloc_obj, next_ident, obj_extract_self, splitobj, weight } from './mkobj.js';
import { findgold, mpickobj, remove_worn_item } from './steal.js';
import { check_special_room, helpless, money_cnt, wait_synch } from './hack.js';
import { Blind, Blind_telepat, Deaf, Detect_monsters, Fast, Hallucination, Invis, Role_if, um_dist } from './macros.js';
import { hidden_gold } from './vault.js';
import { addinv, carried, carrying, count_unpaid, currency, freeinv, makeknown, merged, o_on, observe_object } from './invent.js';
import { block_point, canseemon, canspotmon, map_invisible, sensemon } from './display.js';
import { cansee } from './vision.js';
import { y_n, ynFunction } from './input.js';
import { MON_AT, m_at } from './map_access.js';
import { growl, SetVoice, Soundeffect, yelp } from './sounds.js';
import { A_CHA, EYE, Has_contents, IS_DOOR, PICK_ANY, isok } from './const.js';
import { a_monnam, DEADMONSTER, m_next2u, mnexto, mon_nam, mongone, Monnam, noit_mhim, noit_mhis, update_inventory, wake_nearto, x_monnam, y_monnam } from './mon.js';
import { ACURR, adjalign } from './attrib.js';
import { Doname2, The, ansimpleoname, doname, safe_qbuf, simpleonames, the, xname } from './objnam.js';
import { record_achievement } from './insight.js';
import { search_special } from './mkroom.js';
import { is_demon, is_pick, m_canseeu, mdistu, nolimbs, unique_corpstat } from './mondata.js';
import { dochug, mnearto } from './monmove.js';
import { mbodypart, poly_gender } from './polyself.js';
import { obj_stop_timers } from './timeout.js';
import { create_nhwindow_menu, start_menu, add_menu, add_menu_str, add_menu_heading, end_menu, select_menu, destroy_nhwindow_menu } from './menu.js';
import { bot, setBotl } from './display.js';
import { setnotworn } from './worn.js';
import { angry_guards, in_town } from './mklev.js';
import { artilist } from './artifacts.js';
import { ledger_no } from './dungeon.js';
// ── Local constants matching C macros ──
const PAY_BUY = 1;
const PAY_CANT = 0;
const PAY_SKIP = -1;
const PAY_BROKE = -2;
// billitem_status enum
const FullyUsedUp = 1;
const PartlyUsedUp = 2;
const PartlyIntact = 3;
const FullyIntact = 4;
const KnownContainer = 5;
const UndisclosedContainer = 6;
// ── Local helper macros ──
function NOTANGRY(mon) { return mon.mpeaceful; }
function ANGRY(mon) { return !NOTANGRY(mon); }
function IS_SHOP(x) { return game.level.rooms[x].rtype >= SHOPBASE; }
function muteshk(shkp) { return helpless(shkp) || (shkp.data?.msound ?? 0) <= 1; /* MS_ANIMAL=1 */ }
// DEADMONSTER: imported from mon.js
// ── Local stubs for dependencies not yet ported ──
// These are no-ops or minimal implementations for functions called
// by shk.c but not yet fully ported in the JS codebase. They allow
// the full shk.js module to load and export correct signatures.
// As each dependency gets ported, remove the stub and import instead.
// SET_BOTL: use setBotl(site) from display.js — logs ^botl[site] event
// add_to_minv: imported from mkobj.js
function invlet_basic() { return 52; }
// get_obj_location: imported from mkobj.js
function Shknam(shkp) { return shkname(shkp); }
function noit_mhe(mon) { return mon?.female ? 'she' : 'he'; }
// noit_mhis: imported from mon.js
// noit_mhim: imported from mon.js
// x_monnam: imported from mon.js
// y_monnam: imported from mon.js
// a_monnam: imported from mon.js
// mon_nam: imported from mon.js
// Doname2: imported from objnam.js
// simpleonames: imported from objnam.js
// ansimpleoname: imported from objnam.js
// C ref: objnam.c:2311 paydoname() — suppress invent-style price for billing
function paydoname(obj) {
const g = game;
const save_cknown = obj.cknown;
if (Has_contents(obj)) obj.cknown = 0;
// suppress invent-style price; caller adds billing-style price
g.iflags.suppress_price = (g.iflags.suppress_price || 0) + 1;
let p = doname(obj);
g.iflags.suppress_price--;
if (Has_contents(obj)) {
obj.cknown = save_cknown;
if (!obj.no_charge) {
if (p.startsWith('a ')) p = p.substring(2);
else if (p.startsWith('an ')) p = p.substring(3);
p = (obj.unpaid ? 'an unpaid ' : 'your ') + p;
}
}
return p;
}
// pline1 imported from pline.js
function set_voice(_shk, _a, _b, _c) {}
// C ref: role.c:2120 Hello()
function Hello(mtmp) {
const mnum = game.urole?.mnum;
switch (mnum) {
case PM_KNIGHT:
return 'Salutations';
case PM_SAMURAI:
return (mtmp && mtmp.data === mons[PM_SHOPKEEPER])
? 'Irasshaimase'
: 'Konnichi wa';
case PM_TOURIST:
return 'Aloha';
case PM_VALKYRIE:
return 'Velkommen';
default:
return 'Hello';
}
}
// adjalign: imported from attrib.js
// um_dist: imported from macros.js
// C ref: shknam.c:908 is_izchak — tribute shopkeeper check
function is_izchak(shkp, override_hallucination) {
if (Hallucination() && !override_hallucination) return false;
if (!shkp.isshk) return false;
if (!in_town(shkp.mx, shkp.my)) return false;
let shknm = shkp.eshk?.shknam || '';
if (shknm.length > 0 && shknm[0] === '+') shknm = shknm.slice(1);
return shknm === 'Izchak';
}
// is_pick: imported from mondata.js
function choose_stairs(_sx, _sy, _pref) {}
// angry_guards: imported from mklev.js
function pacify_guards() {}
// enexto: imported below
// check_special_room: imported from hack.js
// block_point: imported from display.js
// wait_synch: imported from hack.js
function mark_synch() {}
// nh_delay_output: imported from hacklib.js
// simplified
function newobj() { return {}; }
function unpaid_cost(_obj, _type) { return 0; }
function contained_cost_fn(obj, shkp, price, usell, unpaid_only) { return contained_cost(obj, shkp, price, usell, unpaid_only); }
function saleable(_shkp, _obj) { return true; } // simplified
// SchroedingersBox: imported from objects.js
// costly_gold is defined as a full export below
function food_disappears(_obj) {}
function drop_upon_death(_mon, _obj, _x, _y) {}
// ledger_no: imported from dungeon.js
function intrinsic_possible(_prop, _ptr) { return false; }
// unique_corpstat: imported from mondata.js
// C ref: artifact.c:2309 arti_cost — get artifact's price
function arti_cost(obj) {
if (!obj.oartifact) return game.objects[obj.otyp]?.oc_cost || 0;
const artCost = artilist[obj.oartifact]?.cost;
if (artCost) return artCost;
return 100 * (game.objects[obj.otyp]?.oc_cost || 0);
}
function oid_price_adjustment(obj, oid) {
const otyp = obj.otyp;
let res = 0;
const od = game.objects[otyp];
// C ref: shk.c:1187, o_init.c — items with no alternate description (OBJ_DESCR==NULL
// in C, oc_descr==null/undefined in JS) are always oc_name_known=1 from init_objects().
// JS uses oc_descr (the description string) to detect this, NOT oc_descr_idx (which
// is set to the item's own otyp for all items by init_objects() regardless).
const name_known = od?.oc_name_known || (od?.oc_descr == null ? 1 : 0);
if (!(obj.dknown && name_known)
&& (obj.oclass !== GEM_CLASS || od?.oc_material !== GLASS)) {
res = ((oid % 4) === 0) ? 1 : 0;
}
return res;
}
// m_canseeu: imported from mondata.js
function onlineu(_x, _y) { return false; }
// Fast: imported from macros.js
// y_n: imported from input.js
function yn_function(_prompt, _resp, _def, _force) { return _def; }
function ynaq(_prompt) { return 'y'; }
function nyaq(_prompt) { return 'y'; }
function create_nhwindow(_type) { return 0; }
function destroy_nhwindow(_win) {}
function putstr(_win, _attr, _str) {}
// Sprintf, Strcpy imported from hacklib.js
// Strcat: imported from hacklib.js
// PM_KNIGHT, PM_SAMURAI, PM_TOURIST, PM_VALKYRIE: imported from roles.js
const PM_ROGUE = 7; // approximate
const PM_ELF = 2; // approximate
const MAXULEV = 30;
const PL_NSIZ = 32;
const ANY_SHOP = -1;
const MIGR_APPROX_XY = 2;
const REPAIR_DELAY = 5;
const BOLT_LIM = 8;
const SELL_NORMAL = 0;
const SELL_DONTSELL = 3;
const MM_NOMSG = 0x04;
const RLOC_MSG = 0;
const RLOC_NOMSG = 1;
const SVALL = 0xff;
const COST_NOCONTENTS = 0;
const COST_CONTENTS = 1;
const COST_SINGLEOBJ = 2;
const D_BROKEN = 0x10;
const D_CLOSED = 0x04;
const S_KOP = 'K';
const W_SWAPWEP = 0x4000;
const W_QUIVER = 0x8000;
const NHW_MENU = 2;
const MENU_BEHAVE_STANDARD = 0;
const MENU_TRADITIONAL = 0;
const MENU_ITEMFLAGS_NONE = 0;
const ATR_NONE = 0;
// NO_COLOR: import from terminal.js (was incorrectly 0 = CLR_BLACK)
// PICK_ANY=2 (from const.js), NOT 1. Was incorrectly 1, causing PICK_ONE behavior.
// PICK_AXE, DWARVISH_MATTOCK: imported from objects.js
// These old _OTYP constants had WRONG values. Use canonical imports.
const CRYSTAL_BALL_OTYP = 192;
const BRASS_LANTERN_OTYP = 187;
const MAGIC_FLUTE_OTYP = 0;
const DRUM_OF_EARTHQUAKE_OTYP = 0;
const CAN_OF_GREASE_OTYP = 200;
const TINNING_KIT_OTYP = 199;
const EXPENSIVE_CAMERA_OTYP = 194;
const POT_OIL_OTYP = 0;
const POT_WATER_OTYP = 0;
const MIRROR_OTYP = 0;
const TIN_OTYP = 0;
const EGG_OTYP = 0;
const CORPSE_OTYP = 0;
const BOULDER_OTYP = 470;
const ROCK_OTYP = 471;
const LAND_MINE_OTYP = 0;
const BEARTRAP_OTYP = 0;
const LEASH_OTYP = 0;
const DUNCE_CAP_OTYP = 0;
const STRANGE_OBJECT = 0;
const G_GONE = 0x04;
const PM_KEYSTONE_KOP = 0;
const PM_KOP_SERGEANT = 0;
const PM_KOP_LIEUTENANT = 0;
const PM_KOP_KAPTAIN = 0;
const PM_GRID_BUG = 0;
const BEFORE = 0;
const NOW = 1;
const LANDMINE = 0;
const BEAR_TRAP = 0;
const HOLE = 0;
const PIT = 0;
const SPIKED_PIT = 0;
// TT_PIT imported from const.js
const LL_ACHIEVE = 0;
const LOW_PM = 0;
const ROWNO = 21;
const COLNO = 80;
const A_WIS = 5;
const FIRE_RES = 0;
const SLEEP_RES = 0;
const COLD_RES = 0;
const DISINT_RES = 0;
const SHOCK_RES = 0;
const POISON_RES = 0;
const ACID_RES = 0;
const STONE_RES = 0;
const TELEPORT = 0;
const TELEPORT_CONTROL = 0;
const TELEPAT = 0;
const Is_candle = (obj) => false; // simplified
const CONTAINED_TOO = 1;
const BURIED_TOO = 2;
// C strings used in messages
const and_its_contents = " and its contents";
const the_contents_of = "the contents of ";
const angrytexts = ["quite upset", "ticked off", "furious"];
const no_money = "Moreover, you%s have no gold.";
const not_enough_money = "Besides, you don't have enough to interest %s.";
const the_your = ["the", "your"];
// ── Access to game state matching C globals ──
// C: fmon, fobj, gi.invent, svr.rooms, svl.level, u, etc.
function get_fmon() { return game.fmon; }
function get_fobj() { return game.level?.objects_chain || null; }
function get_invent() { return game.invent; }
function get_rooms() { return game.level?.rooms || []; }
function get_nroom() { return game.level?.nroom || 0; }
function get_u() { return game.u; }
function get_levl(x, y) { return game.level?.at?.(x, y) || {}; }
// ═══════════════════════════���════════════════════════════════��═══
// EXPORTED FUNCTIONS — full C-faithful ports
// ════════════════════════════════════════════════════════════════
// ── money2mon: transfer gold from hero to monster ──
// C ref: shk.c:156
export async function money2mon(mon, amount) {
let ygold = findgold(get_invent());
if (amount <= 0) {
impossible(`${amount ? "negative" : "zero"} payment in money2mon!`);
return 0;
}
if (!ygold || ygold.quan < amount) {
impossible(`Paying without ${ygold ? "enough" : ""} gold?`);
return 0;
}
if (ygold.quan > amount)
ygold = splitobj(ygold, amount);
else if (ygold.owornmask)
await remove_worn_item(ygold, false);
freeinv(ygold);
await add_to_minv(mon, ygold);
setBotl('money2mon');
return amount;
}
// ── money2u: transfer gold from monster to hero ──
// C ref: shk.c:185
export function money2u(mon, amount) {
let mongold = findgold(mon.minvent);
if (amount <= 0) {
impossible(`${amount ? "negative" : "zero"} payment in money2u!`);
return;
}
if (!mongold || mongold.quan < amount) {
impossible(`${a_monnam(mon)} paying without ${mongold ? "enough" : ""} gold?`);
return;
}
if (mongold.quan > amount)
mongold = splitobj(mongold, amount);
obj_extract_self(mongold);
addinv(mongold);
setBotl('money2u');
}
// ── next_shkp: find next shopkeeper on monster list ──
// C ref: shk.c:214
function next_shkp(shkp, withbill) {
for (; shkp; shkp = shkp.nmon) {
if (DEADMONSTER(shkp)) continue;
if (shkp.isshk && (ESHK(shkp).billct || !withbill))
break;
}
if (shkp) {
if (ANGRY(shkp)) {
if (!ESHK(shkp).surcharge)
rile_shk(shkp);
}
}
return shkp;
}
// ── shkgone: called when shopkeeper dies ──
// C ref: shk.c:234
export function shkgone(mtmp) {
const eshk = ESHK(mtmp);
const sroom = get_rooms()[eshk.shoproom - ROOMOFFSET];
if (!sroom) return;
if (on_level(eshk.shoplevel, get_u().uz)) {
discard_damage_owned_by(mtmp);
sroom.resident = null;
if (!search_special(ANY_SHOP))
game.level.flags.has_shop = 0;
for (let sx = sroom.lx; sx <= sroom.hx; sx++)
for (let sy = sroom.ly; sy <= sroom.hy; sy++) {
const loc = get_levl(sx, sy);
for (let otmp = loc?.objects; otmp; otmp = otmp.nexthere)
otmp.no_charge = 0;
}
const ushops = get_u().ushops || '';
const p = ushops.indexOf(String.fromCharCode(eshk.shoproom));
if (p >= 0) {
setpaid(mtmp);
eshk.bill_p = null;
get_u().ushops = ushops.slice(0, p) + ushops.slice(p + 1);
}
}
}
// ── set_residency ──
// C ref: shk.c:271
export function set_residency(shkp, zero_out) {
if (on_level(ESHK(shkp).shoplevel, get_u().uz)) {
const rooms = get_rooms();
const idx = ESHK(shkp).shoproom - ROOMOFFSET;
if (rooms[idx])
rooms[idx].resident = zero_out ? null : shkp;
}
}
// ── replshk ──
// C ref: shk.c:280
export function replshk(mtmp, mtmp2) {
const rooms = get_rooms();
const idx = ESHK(mtmp2).shoproom - ROOMOFFSET;
if (rooms[idx]) rooms[idx].resident = mtmp2;
if (inhishop(mtmp) && (get_u().ushops || '').charCodeAt(0) === ESHK(mtmp).shoproom) {
ESHK(mtmp2).bill_p = ESHK(mtmp2).bill;
}
}
// ── restshk ──
// C ref: shk.c:289
export function restshk(shkp, ghostly) {
if (get_u().uz?.dlevel) {
const eshkp = ESHK(shkp);
if (eshkp.bill_p !== -1000)
eshkp.bill_p = eshkp.bill;
if (ghostly) {
eshkp.shoplevel = { ...get_u().uz };
if (ANGRY(shkp) && eshkp.customer !== game.plname)
pacify_shk(shkp, true);
}
}
}
// ── clear_unpaid_obj / clear_unpaid ──
// C ref: shk.c:308-325
function clear_unpaid_obj(shkp, otmp) {
if (Has_contents(otmp))
clear_unpaid(shkp, otmp.cobj);
if (onbill(otmp, shkp, true))
otmp.unpaid = 0;
}
function clear_unpaid(shkp, list) {
while (list) {
clear_unpaid_obj(shkp, list);
list = list.nobj;
}
}
// ── clear_no_charge_obj / clear_no_charge / clear_no_charge_pets ──
// C ref: shk.c:328-396
function clear_no_charge_obj(shkp, otmp) {
if (Has_contents(otmp))
clear_no_charge(shkp, otmp.cobj);
if (otmp.no_charge) {
if (!shkp
|| (otmp.where !== OBJ_FLOOR && otmp.where !== OBJ_CONTAINED && otmp.where !== OBJ_BURIED))
otmp.no_charge = 0;
}
}
function clear_no_charge(shkp, list) {
while (list) {
clear_no_charge_obj(shkp, list);
list = list.nobj;
}
}
function clear_no_charge_pets(shkp) {
for (let mtmp = get_fmon(); mtmp; mtmp = mtmp.nmon)
if (mtmp.mtame && mtmp.minvent)
clear_no_charge(shkp, mtmp.minvent);
}
// ── setpaid ──
// C ref: shk.c:399
export function setpaid(shkp) {
clear_unpaid(shkp, get_invent());
clear_unpaid(shkp, get_fobj());
// clear billobjs
while (game.billobjs) {
const obj = game.billobjs;
obj_extract_self(obj);
dealloc_obj(obj);
}
if (shkp) {
const eshkp = ESHK(shkp);
eshkp.billct = 0;
eshkp.credit = 0;
eshkp.debit = 0;
eshkp.loan = 0;
}
}
// ── addupbill ──
// C ref: shk.c:496
function addupbill(shkp) {
let ct = ESHK(shkp).billct;
const bp = ESHK(shkp).bill_p || ESHK(shkp).bill;
let total = 0;
for (let i = 0; i < ct; i++) {
total += bp[i].price * bp[i].bquan;
}
return total;
}
// ── inside_shop ──
// C ref: shk.c:567
export function inside_shop(x, y) {
const loc = get_levl(x, y);
let rno = loc?.roomno ?? 0;
if (rno < ROOMOFFSET || loc?.edge || !IS_SHOP(rno - ROOMOFFSET))
rno = NO_ROOM;
return rno;
}
// ── onbill ──
// C ref: shk.c:1135
function onbill(obj, shkp, silent) {
if (shkp) {
const eshkp = ESHK(shkp);
const bp = eshkp.bill_p || eshkp.bill;
if (!bp) return null;
for (let ct = 0; ct < eshkp.billct; ct++) {
if (bp[ct].bo_id === obj.o_id) {
if (!obj.unpaid)
impossible("onbill: paid obj on bill?");
return bp[ct];
}
}
}
if (obj.unpaid && !silent)
impossible(`onbill: unpaid obj ${!shkp ? "without shopkeeper" : "not on shk's bill"}`);
return null;
}
// ── onshopbill ──
// C ref: shk.c:1159
export function onshopbill(obj, shkp, silent) {
return onbill(obj, shkp, silent) ? true : false;
}
// ── is_unpaid ──
// C ref: shk.c:1167
export function is_unpaid(obj) {
return !!(obj.unpaid || (Has_contents(obj) && count_unpaid(obj.cobj)));
}
// ── delete_contents ──
// C ref: shk.c:1174
export function delete_contents(obj) {
while (obj.cobj) {
const curr = obj.cobj;
obj_extract_self(curr);
obfree(curr, null);
}
}
// ── obfree ──
// C ref: shk.c:1187
export function obfree(obj, merge) {
if (Has_contents(obj))
delete_contents(obj);
let shkp = null;
if (obj.unpaid) {
for (shkp = next_shkp(get_fmon(), true); shkp; shkp = next_shkp(shkp.nmon, true))
if (onbill(obj, shkp, true))
break;
}
if (!shkp)
shkp = shop_keeper((get_u().ushops || '').charCodeAt(0));
const bp = onbill(obj, shkp, false);
if (bp) {
if (!merge) {
bp.useup = true;
obj.unpaid = 0;
add_to_billobjs(obj);
return;
}
const bpm = onbill(merge, shkp, false);
if (!bpm) {
impossible("obfree: not on bill");
return;
} else {
const eshkp = ESHK(shkp);
bpm.bquan += bp.bquan;
eshkp.billct--;
const arr = eshkp.bill_p || eshkp.bill;
Object.assign(bp, arr[eshkp.billct]);
}
} else {
// C ref: shk.c:1229 — transfer oid if freed obj has higher price adjustment
if (merge && oid_price_adjustment(obj, obj.o_id) > oid_price_adjustment(merge, merge.o_id))
merge.o_id = obj.o_id;
}
if (obj.owornmask) {
impossible(`obfree: deleting worn obj (${obj.otyp}: ${obj.owornmask})`);
setnotworn(obj);
}
dealloc_obj(obj);
}
// ── check_credit ──
// C ref: shk.c:1277
async function check_credit(tmp, shkp) {
const credit = ESHK(shkp).credit;
if (credit === 0) {
; // nothing
} else if (credit >= tmp) {
await pline_The("price is deducted from your credit.");
ESHK(shkp).credit -= tmp;
tmp = 0;
} else {
await pline_The("price is partially covered by your credit.");
ESHK(shkp).credit = 0;
tmp -= credit;
}
return tmp;
}
// ── pay ──
// C ref: shk.c:1296
async function pay(tmp, shkp) {
let robbed = ESHK(shkp).robbed;
const balance = (tmp <= 0) ? tmp : await check_credit(tmp, shkp);
if (balance > 0)
await money2mon(shkp, balance);
else if (balance < 0)
money2u(shkp, -balance);
setBotl('pay');
if (robbed) {
robbed -= tmp;
if (robbed < 0) robbed = 0;
ESHK(shkp).robbed = robbed;
}
}
// ── home_shk ──
// C ref: shk.c:1316
async function home_shk(shkp, killkops) {
const x = ESHK(shkp).shk.x, y = ESHK(shkp).shk.y;
await mnearto(shkp, x, y, true, RLOC_NOMSG);
game.level.flags.has_shop = 1;
if (killkops) {
await kops_gone(true);
pacify_guards();
}
await after_shk_move(shkp);
}
// ── angry_shk_exists ──
// C ref: shk.c:1330
function angry_shk_exists() {
for (let shkp = next_shkp(get_fmon(), false); shkp; shkp = next_shkp(shkp.nmon, false))
if (ANGRY(shkp)) return true;
return false;
}
// ── pacify_shk ──
// C ref: shk.c:1343
function pacify_shk(shkp, clear_surcharge) {
shkp.mpeaceful = true;
if (clear_surcharge && ESHK(shkp).surcharge) {
const eshkp = ESHK(shkp);
const bp = eshkp.bill_p || eshkp.bill;
let ct = eshkp.billct;
eshkp.surcharge = false;
for (let i = 0; i < ct; i++) {
const reduction = Math.trunc((bp[i].price + 3) / 4);
bp[i].price -= reduction;
}
}
}
// ── rile_shk ──
// C ref: shk.c:1362
export function rile_shk(shkp) {
shkp.mpeaceful = false;
if (!ESHK(shkp).surcharge) {
const eshkp = ESHK(shkp);
const bp = eshkp.bill_p || eshkp.bill;
let ct = eshkp.billct;
eshkp.surcharge = true;
for (let i = 0; i < ct; i++) {
const surcharge = Math.trunc((bp[i].price + 2) / 3);
bp[i].price += surcharge;
}
}
}
// ── rouse_shk ──
// C ref: shk.c:1380
async function rouse_shk(shkp, verbosely) {
if (helpless(shkp)) {
if (verbosely && canspotmon(shkp))
await pline(`${Shknam(shkp)} ${shkp.msleeping ? "wakes up" : "can move again"}.`);
shkp.msleeping = 0;
shkp.mfrozen = 0;
shkp.mcanmove = 1;
}
}
// ── make_happy_shk ──
// C ref: shk.c:1394
export async function make_happy_shk(shkp, silentkops) {
const wasmad = ANGRY(shkp);
const eshkp = ESHK(shkp);
pacify_shk(shkp, false);
eshkp.following = 0;
eshkp.robbed = 0;
if (!Role_if(PM_ROGUE))
adjalign(sgn(get_u().ualign?.type || 0));
if (!inhishop(shkp)) {
if (on_level(eshkp.shoplevel, get_u().uz)) {
await home_shk(shkp, false);
if (canspotmon(shkp))
await pline(`${Shknam(shkp)} returns to ${noit_mhis(shkp)} shop.`);
}
} else if (wasmad) {
await pline(`${Shknam(shkp)} calms down.`);
}
await make_happy_shoppers(silentkops);
}
// ── make_happy_shoppers ──
// C ref: shk.c:1439
export async function make_happy_shoppers(silentkops) {
if (!angry_shk_exists()) {
await kops_gone(silentkops);
pacify_guards();
}
}
// ── hot_pursuit ──
// C ref: shk.c:1448
export function hot_pursuit(shkp) {
if (!shkp.isshk) return;
rile_shk(shkp);
ESHK(shkp).customer = game.plname || '';
ESHK(shkp).following = 1;
clear_no_charge(null, get_fobj());
clear_no_charge_pets(shkp);
}
// ── make_angry_shk ──
// C ref: shk.c:1470
export async function make_angry_shk(shkp, ox, oy) {
const eshkp = ESHK(shkp);
if (eshkp.billct || eshkp.debit || eshkp.loan || eshkp.credit) {
eshkp.robbed += (addupbill(shkp) + eshkp.debit + eshkp.loan);
eshkp.robbed -= eshkp.credit;
if (eshkp.robbed < 0) eshkp.robbed = 0;
setpaid(shkp);
}
await pline(`${Shknam(shkp)} ${!ANGRY(shkp) ? "gets angry" : "is furious"}!`);
hot_pursuit(shkp);
}
// ── inhishop ──
// C ref: shk.c:1038
export function inhishop(shkp) {
const eshkp = ESHK(shkp);
if (!eshkp) return false;
if (!on_level(eshkp.shoplevel, get_u().uz))
return false;
const shkrooms = in_rooms(shkp.mx, shkp.my, SHOPBASE);
return shkrooms.includes(eshkp.shoproom - ROOMOFFSET);
}
// ── shop_keeper ──
// C ref: shk.c:1052
export function shop_keeper(rmno) {
const rooms = get_rooms();
let shkp = (rmno >= ROOMOFFSET) ? rooms[rmno - ROOMOFFSET]?.resident : null;
if (shkp) {
if (has_eshk(shkp)) {
if (ANGRY(shkp)) {
if (!ESHK(shkp).surcharge)
rile_shk(shkp);
}
} else {
impossible("shop resident not shopkeeper");
return null;
}
}
return shkp;
}
// ── find_objowner ──
// C ref: shk.c:1083
export function find_objowner(obj, x, y) {
let deflt_shkp = null;
if (obj.where === OBJ_ONBILL) {
for (let shkp = next_shkp(get_fmon(), true); shkp; shkp = next_shkp(shkp.nmon, true))
if (onshopbill(obj, shkp, true))
return shkp;
} else {
const where = in_rooms(x, y, SHOPBASE);
for (const roomidx of where) {
const shkp = shop_keeper(roomidx + ROOMOFFSET);
if (shkp) {
if (onshopbill(obj, shkp, true))
return shkp;
if (!deflt_shkp) deflt_shkp = shkp;
}
}
}
return deflt_shkp;
}
// ── tended_shop ──
// C ref: shk.c:1117
export function tended_shop(sroom) {
const mtmp = sroom?.resident;
return !mtmp ? false : !!inhishop(mtmp);
}
// ── noisy_shop ──
// C ref: shk.c:1126
export function noisy_shop(sroom) {
const mtmp = sroom?.resident;
if (mtmp && inhishop(mtmp))
wake_nearto(mtmp.mx, mtmp.my, 11 * 11);
}
// ── same_price ──
// C ref: shk.c:954
export function same_price(obj1, obj2) {
let shkp1 = null, shkp2 = null;
let bp1 = null, bp2 = null;
for (shkp1 = next_shkp(get_fmon(), true); shkp1; shkp1 = next_shkp(shkp1.nmon, true))
if ((bp1 = onbill(obj1, shkp1, true)) != null)
break;
if (shkp1 && (bp2 = onbill(obj2, shkp1, true)) != null) {
shkp2 = shkp1;
} else {
for (shkp2 = next_shkp(get_fmon(), true); shkp2; shkp2 = next_shkp(shkp2.nmon, true))
if ((bp2 = onbill(obj2, shkp2, true)) != null)
break;
}
if (!bp1 || !bp2) {
impossible("same_price: object wasn't on any bill!");
return false;
}
return shkp1 === shkp2 && bp1.price === bp2.price;
}
// ── shop_debt ──
// C ref: shk.c:989
function shop_debt(eshkp) {
const bp = eshkp.bill_p || eshkp.bill;
let debt = eshkp.debit;
for (let i = 0; i < eshkp.billct; i++)
debt += bp[i].price * bp[i].bquan;
return debt;
}
// ── shopper_financial_report ──
// C ref: shk.c:1002
export async function shopper_financial_report() {
let this_shkp = shop_keeper(inside_shop(get_u().ux, get_u().uy));
let eshkp = this_shkp ? ESHK(this_shkp) : null;
if (eshkp && !(eshkp.credit || shop_debt(eshkp))) {
await You("have no credit or debt in here.");
this_shkp = null;
}
for (let pass = this_shkp ? 0 : 1; pass <= 1; pass++)
for (let shkp = next_shkp(get_fmon(), false); shkp; shkp = next_shkp(shkp.nmon, false)) {
if ((shkp !== this_shkp) !== (pass === 1)) continue;
eshkp = ESHK(shkp);
const amt_credit = eshkp.credit;
if (amt_credit)
await You(`have ${amt_credit} ${currency(amt_credit)} credit at ${s_suffix(shkname(shkp))} ${shtypes[eshkp.shoptype - SHOPBASE]?.name || 'shop'}.`);
else if (shkp === this_shkp)
await You("have no credit in here.");
const amt_debt = shop_debt(eshkp);
if (amt_debt)
await You(`owe ${shkname(shkp)} ${amt_debt} ${currency(amt_debt)}.`);
else if (shkp === this_shkp)
await You("don't owe any gold here.");
}
}
// ── in_rooms: find which room indices contain (x,y) ──
// C ref: mkroom.c:734 (was in old shk.js, now C-faithful)
export function in_rooms(x, y, type) {
const rooms = get_rooms();
const result = [];
const lev = game.level?.at?.(x, y);
if (!lev) return result;
const goodtype = (roomno) => {
if (!type || type === 0) return true;
const idx = roomno - ROOMOFFSET;
const found = rooms[idx]?.rtype ?? 0;
return found === type || (type === SHOPBASE && found > SHOPBASE);
};
const maybe_push_roomno = (roomno) => {
if (roomno < ROOMOFFSET) return;
if (!goodtype(roomno)) return;
const idx = roomno - ROOMOFFSET; // preserve existing JS API
if (!result.includes(idx)) result.push(idx);
};
const roomno = lev.roomno ?? NO_ROOM;
let step = 0;
switch (roomno) {
case NO_ROOM:
return result;
case SHARED:
step = 2;
break;
case SHARED_PLUS:
step = 1;
break;
default:
maybe_push_roomno(roomno);
return result;
}
let min_x = x - 1;
let max_x = x + 1;
if (x < 1) {
min_x += step;
} else if (x >= COLNO) {
max_x -= step;
}
let min_y = y - 1;
let max_y_offset = 2;
if (min_y < 0) {
min_y += step;
max_y_offset -= step;
} else if ((min_y + max_y_offset) >= ROWNO) {
max_y_offset -= step;
}
for (let xx = min_x; xx <= max_x; xx += step) {
const loc0 = game.level?.at?.(xx, min_y);
maybe_push_roomno(loc0?.roomno ?? NO_ROOM);
let yy_off = step;
if (yy_off > max_y_offset) continue;
const loc1 = game.level?.at?.(xx, min_y + yy_off);
maybe_push_roomno(loc1?.roomno ?? NO_ROOM);
yy_off += step;
if (yy_off > max_y_offset) continue;
const loc2 = game.level?.at?.(xx, min_y + yy_off);
maybe_push_roomno(loc2?.roomno ?? NO_ROOM);
}
return result;
}
// ── costly_spot ──
// C ref: shk.c:5351
export function costly_spot(x, y) {
if (!game.level?.flags?.has_shop)
return false;
const rooms_here = in_rooms(x, y, SHOPBASE);
if (rooms_here.length === 0) return false;
const shkp = shop_keeper(rooms_here[0] + ROOMOFFSET);
if (!shkp || !inhishop(shkp))
return false;
const eshkp = ESHK(shkp);
const rno = inside_shop(x, y);
return rno !== NO_ROOM && !(x === eshkp.shk.x && y === eshkp.shk.y);
}
// ── costly_adjacent ──
// C ref: shk.c:5369
export function costly_adjacent(shkp, x, y) {
if (!shkp || !inhishop(shkp) || !isok(x, y))
return false;
const eshkp = ESHK(shkp);
const loc = get_levl(x, y);
return !!(loc?.edge || (x === eshkp.shk.x && y === eshkp.shk.y));
}
// ── shkname ──
// C ref: shknam.c
export function shkname(shk) {
if (!shk) return 'shopkeeper';
const eshk = ESHK(shk);
return eshk?.shknam || shk.mname || 'shopkeeper';
}
// ── is_fshk ──
// C ref: shk.c:5012
export function is_fshk(mtmp) {
return !!(mtmp.isshk && ESHK(mtmp)?.following);
}
// ── costly_alteration ──
// C ref: shk.c:3760 (simplified — charges for altering shop items)
export function costly_alteration(obj, alter_type) {
// No-op when no shops are active — full implementation requires
// alter_cost() and cost_per_charge() which depend on many helpers.
// The skeleton is here; it gets called but currently does nothing
// beyond what the stub did.
}
// ── shk_your ──
// C ref: shk.c:5862
export function shk_your(buf, obj) {
// simplified — returns ownership prefix
if (obj?.unpaid) {
const ushops = get_u().ushops || '';
if (ushops) {
const shkp = shop_keeper(ushops.charCodeAt(0));
if (shkp) return s_suffix(shkname(shkp)) + ' ';
}
return "the shopkeeper's ";
}
return carried(obj) ? 'your ' : 'the ';
}
// ── Shk_Your ──
// C ref: shk.c:5877
export function Shk_Your(buf, obj) {
const result = shk_your(buf, obj);
return result.charAt(0).toUpperCase() + result.slice(1);
}
// ── shk_his (not in C — convenience) ──
export function shk_his(obj) {
// For compatibility with callers that expect this function
return shk_your('', obj);
}
// ── add_damage ──
// C ref: shk.c:4399
export function add_damage(x, y, cost) {
if (!game.level) return;
if (!game.level.damagelist) game.level.damagelist = [];
// Check if existing damage at this location
for (const dam of game.level.damagelist) {
if (dam.place.x === x && dam.place.y === y) {
dam.cost += cost;
dam.when = game.moves || 0;
return;
}
}
const loc = get_levl(x, y);
const dam = {
when: game.moves || 0,
place: { x, y },
cost: cost,
typ: loc?.typ || 0,
flags: loc?.flags || 0,
next: null,
};
game.level.damagelist.push(dam);
}
// ── getprice ──
// C ref: shk.c:4319
function getprice(obj, shk_buying) {
let tmp = game.objects[obj.otyp]?.oc_cost || 0;
if (obj.oartifact) {
tmp = arti_cost(obj);
if (shk_buying) tmp = Math.trunc(tmp / 4);
}
switch (obj.oclass) {
case FOOD_CLASS:
tmp += corpsenm_price_adj(obj);
if ((get_u().uhs || 0) >= 2 && !shk_buying) // HUNGRY=2
tmp *= get_u().uhs;
if (obj.oeaten) tmp = 0;
break;
case WAND_CLASS:
if (obj.spe === -1) tmp = 0;
break;
case POTION_CLASS:
if (obj.otyp === POT_WATER_OTYP && !obj.blessed && !obj.cursed)
tmp = 0;
break;
case ARMOR_CLASS:
case WEAPON_CLASS:
if (obj.spe > 0)
tmp += 10 * obj.spe;
break;
case TOOL_CLASS:
if (Is_candle(obj) && obj.age < 20 * (game.objects[obj.otyp]?.oc_cost || 0))
tmp = Math.trunc(tmp / 2);
break;
}
return tmp;
}
// ── corpsenm_price_adj ──
// C ref: shk.c:4275
function corpsenm_price_adj(obj) {
// Simplified — full implementation requires mons[] and intrinsic checks
return 0;
}
// ── get_pricing_units ──
// C ref: shk.c:2847
function get_pricing_units(obj) {
let units = obj.quan;
if (obj.globby) {
const unit_weight = game.objects[obj.otyp]?.oc_wt || 1;
const wt = obj.owt > 0 ? obj.owt : weight(obj);
if (unit_weight)
units = Math.trunc((wt + unit_weight - 1) / unit_weight);
}
return units;
}
// ── get_cost: price shopkeeper charges for one unit ──
// C ref: shk.c:2877
function get_cost(obj, shkp) {
let tmp = getprice(obj, false);
let multiplier = 1, divisor = 1;
if (!tmp) tmp = 5;
if (!obj.dknown || !game.objects[obj.otyp]?.oc_name_known) {
if (obj.oclass === GEM_CLASS && game.objects[obj.otyp]?.oc_material === GLASS) {
const pseudorand = ((get_u().ubirthday || 0) % obj.otyp) >= Math.trunc(obj.otyp / 2);
let i;
switch (obj.otyp - FIRST_GLASS_GEM) {
case 0: i = pseudorand ? DIAMOND : OPAL; break;
case 1: i = pseudorand ? SAPPHIRE : AQUAMARINE; break;
case 2: i = pseudorand ? RUBY : JASPER; break;
case 3: i = pseudorand ? AMBER : TOPAZ; break;
case 4: i = pseudorand ? JACINTH : AGATE; break;
case 5: i = pseudorand ? CITRINE : CHRYSOBERYL; break;
case 6: i = pseudorand ? BLACK_OPAL : JET; break;
case 7: i = pseudorand ? EMERALD : JADE; break;
case 8: i = pseudorand ? AMETHYST : FLUORITE; break;
default: i = STRANGE_OBJECT; break;
}
tmp = game.objects[i]?.oc_cost || tmp;
} else if (oid_price_adjustment(obj, obj.o_id) > 0) {
multiplier *= 4;
divisor *= 3;
}
}
// Dunce cap / Tourist surcharge
if (get_u().uarmh?.otyp === DUNCE_CAP_OTYP) {
multiplier *= 4; divisor *= 3;
} else if ((Role_if(PM_TOURIST) && (get_u().ulevel || 0) < Math.trunc(MAXULEV / 2))
|| (get_u().uarmu && !get_u().uarm && !get_u().uarmc)) {
multiplier *= 4; divisor *= 3;
}
const cha = ACURR(A_CHA);
if (cha > 18) divisor *= 2;
else if (cha === 18) { multiplier *= 2; divisor *= 3; }
else if (cha >= 16) { multiplier *= 3; divisor *= 4; }
else if (cha <= 5) multiplier *= 2;
else if (cha <= 7) { multiplier *= 3; divisor *= 2; }
else if (cha <= 10) { multiplier *= 4; divisor *= 3; }
tmp *= multiplier;
if (divisor > 1) {
tmp *= 10;
tmp = Math.trunc(tmp / divisor);
tmp += 5;
tmp = Math.trunc(tmp / 10);
}
if (tmp <= 0) tmp = 1;
if (obj.oartifact) tmp *= 4;
if (shkp && ESHK(shkp).surcharge)
tmp += Math.trunc((tmp + 2) / 3);
return tmp;
}
// ── set_cost: price shk pays when buying ──
// C ref: shk.c:3148
export function set_cost(obj, shkp) {
let unit_price = getprice(obj, true);
let tmp = get_pricing_units(obj) * unit_price;
let multiplier = 1, divisor = 1;
if (get_u().uarmh?.otyp === DUNCE_CAP_OTYP)
divisor *= 3;
else if ((Role_if(PM_TOURIST) && (get_u().ulevel || 0) < Math.trunc(MAXULEV / 2))
|| (get_u().uarmu && !get_u().uarm && !get_u().uarmc))
divisor *= 3;
else
divisor *= 2;
if (!obj.dknown || !game.objects[obj.otyp]?.oc_name_known) {
if (obj.oclass === GEM_CLASS) {
const mat = game.objects[obj.otyp]?.oc_material;
if (mat === GEMSTONE || mat === GLASS) {
tmp = ((obj.otyp - FIRST_REAL_GEM) % (6 - shkp.m_id % 3));
tmp = (tmp + 3) * obj.quan;
divisor = 1;
}
} else if (tmp > 1 && !(shkp.m_id % 4)) {
multiplier *= 3; divisor *= 4;
}
}
if (tmp >= 1) {
tmp *= multiplier;
if (divisor > 1) {
tmp *= 10;
tmp = Math.trunc(tmp / divisor);
tmp += 5;
tmp = Math.trunc(tmp / 10);
}
if (tmp < 1) tmp = 1;
}
return tmp;
}
// ── cost_per_charge ──
// C ref: shk.c:5627
export function cost_per_charge(shkp, otmp, altusage) {
if (!shkp || !inhishop(shkp))
return 0;
let tmp = get_cost(otmp, shkp);
if (otmp.otyp === MAGIC_LAMP) {
if (!altusage)
tmp = game.objects[OIL_LAMP]?.oc_cost || 10;
else
tmp += Math.trunc(tmp / 3);
} else if (otmp.otyp === MAGIC_MARKER) {
tmp = Math.trunc(tmp / 2);
} else if (otmp.otyp === BAG_OF_TRICKS || otmp.otyp === HORN_OF_PLENTY) {
if (!altusage) tmp = Math.trunc(tmp / 5);
} else if (otmp.oclass === WAND_CLASS) {
if (otmp.spe > 1) tmp = Math.trunc(tmp / 4);
} else if (otmp.oclass === SPBOOK_CLASS) {
tmp -= Math.trunc(tmp / 5);
} else if (otmp.otyp === CAN_OF_GREASE_OTYP || otmp.otyp === TINNING_KIT_OTYP
|| otmp.otyp === EXPENSIVE_CAMERA_OTYP) {
tmp = Math.trunc(tmp / 10);
} else if (otmp.otyp === POT_OIL_OTYP) {
tmp = Math.trunc(tmp / 5);
}
return tmp;
}
// ── check_unpaid_usage ──
// C ref: shk.c:5688
export function check_unpaid_usage(otmp, altusage) {
if (!otmp.unpaid || !(get_u().ushops || ''))
return;
if (otmp.spe <= 0 && game.objects[otmp.otyp]?.oc_charged)
return;
const shkp = shop_keeper((get_u().ushops || '').charCodeAt(0));
if (!shkp || !inhishop(shkp)) return;
const tmp = cost_per_charge(shkp, otmp, altusage);
if (tmp === 0) return;
ESHK(shkp).debit += tmp;
}
// ── check_unpaid ──
// C ref: shk.c:5740
export function check_unpaid(otmp) {
check_unpaid_usage(otmp, false);
}
// ── billable ──
// C ref: shk.c:3451
export function billable(shkp_ref, obj, roomno, reset_nocharge) {
let shkp = shkp_ref.shkp;
if (!shkp) {
if (!roomno) return false;
shkp = shop_keeper(roomno);
if (!shkp || !inhishop(shkp)) return false;
shkp_ref.shkp = shkp;
}
if (onbill(obj, shkp, false) || (obj.oclass === FOOD_CLASS && obj.oeaten))
return false;
if (obj.no_charge) {
if (!Has_contents(obj))
return false;
}
return true;
}
// ── add_one_tobill ──
// C ref: shk.c:3309
async function add_one_tobill(obj, dummy, shkp) {
const eshkp = ESHK(shkp);
if (!eshkp.bill_p)
eshkp.bill_p = eshkp.bill;
if (!eshkp.bill) eshkp.bill = [];
const shkp_ref = { shkp };
if (!billable(shkp_ref, obj, (get_u().ushops || '').charCodeAt(0), true)) {
if (obj.where === OBJ_FREE)
dealloc_obj(obj);
return;
}
if (eshkp.billct >= BILLSZ) {
await You("got that for free!");
if (obj.where === OBJ_FREE)
dealloc_obj(obj);
return;
}
const bct = eshkp.billct;
const bp_arr = eshkp.bill_p || eshkp.bill;
if (!bp_arr[bct]) bp_arr[bct] = {};
const bp = bp_arr[bct];
bp.bo_id = obj.o_id;
bp.bquan = obj.quan;
if (dummy) {
bp.useup = true;
add_to_billobjs(obj);
} else {
bp.useup = false;
}
bp.price = get_cost(obj, shkp);
if (obj.globby) {
bp.price *= get_pricing_units(obj);
}
eshkp.billct++;
obj.unpaid = 1;
}
// ── add_to_billobjs ──
// C ref: shk.c:3366
function add_to_billobjs(obj) {
if (obj.where !== OBJ_FREE)
impossible("add_to_billobjs: obj not free");
if (obj.timed) obj_stop_timers(obj);
obj.nobj = game.billobjs;
game.billobjs = obj;
obj.where = OBJ_ONBILL;
obj.in_use = 0;
obj.bypass = 0;
}
// ── bill_box_content ──
// C ref: shk.c:3387
async function bill_box_content(obj, ininv, dummy, shkp) {
if (SchroedingersBox(obj)) return;
for (let otmp = obj.cobj; otmp; otmp = otmp.nobj) {
if (otmp.oclass === COIN_CLASS) continue;
if (!otmp.no_charge)
await add_one_tobill(otmp, dummy, shkp);
if (Has_contents(otmp))
await bill_box_content(otmp, ininv, dummy, shkp);
}
}
// ── addtobill ──
// C ref: shk.c:3490
export async function addtobill(obj, ininv, dummy, silent) {
const shkp_ref = { shkp: null };
// C ref: shk.c:3494 — uses u.ushops[0] as the room number.
// u.ushops is maintained by check_special_room (not yet ported).
// Fallback: use in_rooms on the hero's position to find the shop.
let shopRoomNo = (get_u().ushops || '').charCodeAt(0);
if (!shopRoomNo || isNaN(shopRoomNo)) {
const u = get_u();
const heroRooms = in_rooms(u.ux, u.uy, SHOPBASE);
shopRoomNo = heroRooms.length > 0 ? heroRooms[0] + ROOMOFFSET : 0;
}
if (!billable(shkp_ref, obj, shopRoomNo, true))
return;
const shkp = shkp_ref.shkp;
if (obj.oclass === COIN_CLASS) {
await costly_gold(obj.ox, obj.oy, obj.quan, silent);
return;
}
if (ESHK(shkp).billct >= BILLSZ) {
if (!silent) await You("got that for free!");
return;
}
let ltmp = 0, cltmp = 0, gltmp = 0;
const container = Has_contents(obj);
if (!obj.no_charge) {
ltmp = get_cost(obj, shkp);
if (obj.globby) ltmp *= get_pricing_units(obj);
}
if (obj.no_charge && !container) {
obj.no_charge = 0;
return;
}
if (container) {
cltmp = contained_cost(obj, shkp, cltmp, false, false);
gltmp = contained_gold(obj, true);
if (ltmp) await add_one_tobill(obj, dummy, shkp);
if (cltmp) await bill_box_content(obj, ininv, dummy, shkp);
picked_container(obj);
ltmp += cltmp;
if (gltmp) {
await costly_gold(obj.ox, obj.oy, gltmp, silent);
if (!ltmp) return;
}
if (obj.no_charge) obj.no_charge = 0;
} else {
await add_one_tobill(obj, dummy, shkp);
}
// C ref: shk.c:3553-3599 — shopkeeper voice feedback
const contentscount = Has_contents(obj) ? count_unpaid(obj.cobj) : 0;
if (!Deaf() && !muteshk(shkp) && !silent) {
if (!ltmp) {
await pline(Shknam(shkp) + ' has no interest in ' + the(xname(obj)) + '.');
return;
}
if (!ininv) {
await pline(The(xname(obj)) + ' will cost you ' + ltmp + ' ' + currency(ltmp) + (obj.quan > 1 ? ' each' : '') + '.');
} else {
const save_quan = obj.quan;
let buf = '"For you,';
if (ANGRY(shkp)) {
buf += ' scum;';
} else if (!ESHK(shkp).surcharge) {
buf += ' ' + append_honorific('') + '; only';
}
obj.quan = 1;
const and_its_contents = contentscount && obj.unpaid ? ' and its contents' : '';
const per_or_for = save_quan > 1 ? 'per'
: (contentscount && !obj.unpaid) ? 'for the contents of this'
: 'for this';
set_voice(shkp, 0, 80, 0);
await pline(buf + ' ' + ltmp + ' ' + currency(ltmp) + ' ' + per_or_for + ' ' + xname(obj) + and_its_contents + '."');
obj.quan = save_quan;
}
} else if (!silent) {
if (ltmp) {
set_voice(shkp, 0, 80, 0);
const the_contents_of = contentscount && !obj.unpaid ? 'the contents of ' : '';
const and_its_contents = contentscount && obj.unpaid ? ' and its contents' : '';
await pline_The('list price of ' + the_contents_of + the(xname(obj)) + and_its_contents + ' is ' + ltmp + ' ' + currency(ltmp) + (obj.quan > 1 ? ' each' : '') + '.');
} else {
await pline(Shknam(shkp) + ' does not notice.');
}
}
}
// ── splitbill ──
// C ref: shk.c:3623
export function splitbill(obj, otmp) {
const shkp = shop_keeper((get_u().ushops || '').charCodeAt(0));
if (!shkp || !inhishop(shkp)) {
impossible("splitbill: no resident shopkeeper??");
return;
}
const bp = onbill(obj, shkp, false);
if (!bp) {
impossible("splitbill: not on bill?");
return;
}
if (bp.bquan < otmp.quan) impossible("Negative quantity on bill??");
if (bp.bquan === otmp.quan) impossible("Zero quantity on bill??");
bp.bquan -= otmp.quan;
const eshkp = ESHK(shkp);
if (eshkp.billct >= BILLSZ) {
otmp.unpaid = 0;
} else {
const tmp = bp.price;
const arr = eshkp.bill_p || eshkp.bill;
if (!arr[eshkp.billct]) arr[eshkp.billct] = {};
const nbp = arr[eshkp.billct];
nbp.bo_id = otmp.o_id;
nbp.bquan = otmp.quan;
nbp.useup = false;
nbp.price = tmp;
eshkp.billct++;
}
}
// ── sub_one_frombill ──
// C ref: shk.c:3661
function sub_one_frombill(obj, shkp) {
const bp = onbill(obj, shkp, false);
if (bp) {
obj.unpaid = 0;
if (bp.bquan > obj.quan) {
const otmp = { ...obj };
otmp.o_id = next_ident();
otmp.where = OBJ_FREE;
otmp.quan = bp.bquan - obj.quan;
bp.bo_id = otmp.o_id;
bp.bquan -= obj.quan;
bp.useup = true;
add_to_billobjs(otmp);
return;
}
const eshkp = ESHK(shkp);
eshkp.billct--;
const arr = eshkp.bill_p || eshkp.bill;
Object.assign(bp, arr[eshkp.billct]);
return;
} else if (obj.unpaid) {
impossible("sub_one_frombill: unpaid object not on bill");
obj.unpaid = 0;
}
}
// ── subfrombill ──
// C ref: shk.c:3694
export function subfrombill(obj, shkp) {
sub_one_frombill(obj, shkp);
if (Has_contents(obj))
for (let otmp = obj.cobj; otmp; otmp = otmp.nobj) {
if (otmp.oclass === COIN_CLASS) continue;
if (Has_contents(otmp))
subfrombill(otmp, shkp);
else
sub_one_frombill(otmp, shkp);
}
}
// ── stolen_container ──
// C ref: shk.c:3713
function stolen_container(obj, shkp, price, ininv) {
for (let otmp = obj.cobj; otmp; otmp = otmp.nobj) {
if (otmp.oclass === COIN_CLASS) continue;
let billamt = 0;
const shkp_ref = { shkp };
if (!billable(shkp_ref, otmp, ESHK(shkp).shoproom, true)) {
const bp = onbill(otmp, shkp, false);
if (!bp) continue;
billamt = bp.bquan * bp.price;
sub_one_frombill(otmp, shkp);
}
if (billamt)
price += billamt;
else if (ininv ? otmp.unpaid : !otmp.no_charge)
price += get_pricing_units(otmp) * get_cost(otmp, shkp);
if (Has_contents(otmp))
price = stolen_container(otmp, shkp, price, ininv);
}
return price;
}
// ── stolen_value ──
// C ref: shk.c:3754
export async function stolen_value(obj, x, y, peaceful, silent) {
let value = 0, gvalue = 0, billamt = 0;
let shkp = find_objowner(obj, x, y);
let roomno;
if (shkp) {
roomno = ESHK(shkp).shoproom;
} else {
const rooms_here = in_rooms(x, y, SHOPBASE);
roomno = rooms_here.length > 0 ? rooms_here[0] + ROOMOFFSET : 0;
}
const was_unpaid = !!obj.unpaid;
const u_count = Has_contents(obj) ? count_unpaid(obj.cobj) : 0;
shkp = null;
const shkp_ref = { shkp: null };
if (!billable(shkp_ref, obj, roomno, true)) {
shkp = shkp_ref.shkp;
const bp = shkp ? onbill(obj, shkp, false) : null;
if (bp) {
billamt = bp.bquan * bp.price;
sub_one_frombill(obj, shkp);
}
if (!bp && !u_count) return 0;
}
shkp = shkp_ref.shkp;
if (obj.oclass === COIN_CLASS) {
gvalue += obj.quan;
} else {
if (billamt) value += billamt;
else if (!obj.no_charge) value += get_pricing_units(obj) * get_cost(obj, shkp);
if (Has_contents(obj)) {
const ininv = (obj.where === OBJ_INVENT || obj.where === OBJ_FREE);
value += stolen_container(obj, shkp, 0, ininv);
if (!ininv) gvalue += contained_gold(obj, true);
}
}
if (gvalue + value === 0) return 0;
value += gvalue;
if (peaceful) {
value = await check_credit(value, shkp);
if (ANGRY(shkp))
ESHK(shkp).robbed += value;
else
ESHK(shkp).debit += value;
if (!silent && value) {
await You(`owe ${shkname(shkp)} ${value} ${currency(value)}!`);
}
} else {
ESHK(shkp).robbed += value;
if (!silent) {
if (canseemon(shkp))
await pline(`${Shknam(shkp)} booms: "${game.plname || 'you'}, you are a thief!"`);
}
hot_pursuit(shkp);
}
return value;
}
// ── contained_cost ──
// C ref: shk.c:2995
export function contained_cost(obj, shkp, price, usell, unpaid_only) {
for (let otmp = obj.cobj; otmp; otmp = otmp.nobj) {
if (otmp.oclass === COIN_CLASS) continue;
if (usell) {
if (saleable(shkp, otmp) && !otmp.unpaid
&& otmp.oclass !== BALL_CLASS
&& !(otmp.oclass === FOOD_CLASS && otmp.oeaten))
price += set_cost(otmp, shkp);
} else {
if (otmp.unpaid || !unpaid_only)
price += get_cost(otmp, shkp) * get_pricing_units(otmp);
}
if (Has_contents(otmp))
price = contained_cost(otmp, shkp, price, usell, unpaid_only);
}
return price;
}
// ── contained_gold ──
// C ref: shk.c:3046
export function contained_gold(obj, even_if_unknown) {
let value = 0;
for (let otmp = obj.cobj; otmp; otmp = otmp.nobj)
if (otmp.oclass === COIN_CLASS)
value += otmp.quan;
else if (Has_contents(otmp) && (otmp.cknown || even_if_unknown))
value += contained_gold(otmp, even_if_unknown);
return value;
}
// ── dropped_container ──
// C ref: shk.c:3064
function dropped_container(obj, shkp, sale) {
for (let otmp = obj.cobj; otmp; otmp = otmp.nobj) {
if (otmp.oclass === COIN_CLASS) continue;
if (!otmp.unpaid && !(sale && saleable(shkp, otmp)))
otmp.no_charge = 1;
if (Has_contents(otmp))
dropped_container(otmp, shkp, sale);
}
}
// ── picked_container ──
// C ref: shk.c:3086
export function picked_container(obj) {
for (let otmp = obj.cobj; otmp; otmp = otmp.nobj) {
if (otmp.oclass === COIN_CLASS) continue;
if (otmp.no_charge) otmp.no_charge = 0;
if (Has_contents(otmp))
picked_container(otmp);
}
}
// ── sellobj ──
// C ref: shk.c:3928
export function sellobj(obj, x, y) {
if (!(get_u().ushops || '')) return;
const rooms_here = in_rooms(x, y, SHOPBASE);
if (rooms_here.length === 0) return;
const shkp = shop_keeper(rooms_here[0] + ROOMOFFSET);
if (!shkp || !inhishop(shkp)) return;
if (!costly_spot(x, y)) return;
if (obj.unpaid && !Has_contents(obj) && obj.oclass !== COIN_CLASS) {
sub_one_frombill(obj, shkp);
return;
}
// Simplified: for unpaid container contents, just remove from bill
if (Has_contents(obj)) {
subfrombill(obj, shkp);
return;
}
// For owned items, mark no_charge
if (!obj.unpaid)
obj.no_charge = 1;
subfrombill(obj, shkp);
}
// ── donate_gold ──
// C ref: shk.c:3877
export async function donate_gold(gltmp, shkp, selling) {
const eshkp = ESHK(shkp);
if (eshkp.debit >= gltmp) {
if (eshkp.loan) {
if (eshkp.loan > gltmp) eshkp.loan -= gltmp;
else eshkp.loan = 0;
}
eshkp.debit -= gltmp;
await Your(`debt is ${eshkp.debit ? "partially " : ""}paid off.`);
} else {
const delta = gltmp - eshkp.debit;
eshkp.credit += delta;
if (eshkp.debit) {
eshkp.debit = 0;
eshkp.loan = 0;
await Your("debt is paid off.");
}
if (eshkp.credit === delta)
await You(`have ${selling ? "" : "re-"}established ${delta} ${currency(delta)} credit.`);
else
await pline(`${delta} ${currency(delta)} added${selling ? "" : " back"} to your credit; total is now ${eshkp.credit} ${currency(eshkp.credit)}.`);
}
}
// ── sellobj_state ──
// C ref: shk.c:3914
export function sellobj_state(deliberate) {
game.sell_response = (deliberate !== SELL_NORMAL) ? '' : 'a';
game.sell_how = deliberate;
game.auto_credit = false;
}
// ── doinvbill ──
// C ref: shk.c:4197
export function doinvbill(mode) {
const shkp = shop_keeper((get_u().ushops || '').charCodeAt(0));
if (!shkp || !inhishop(shkp)) {
if (mode !== 0) impossible("doinvbill: no shopkeeper?");
return 0;
}
const eshkp = ESHK(shkp);
if (mode === 0) {
let cnt = !eshkp.debit ? 0 : 1;
const bp = eshkp.bill_p || eshkp.bill;
for (let i = 0; i < eshkp.billct; i++) {
if (bp[i].useup) cnt++;
else {
const obj = bp_to_obj(bp[i]);
if (obj && obj.quan < bp[i].bquan) cnt++;
}
}
return cnt;
}
// mode 1: display — simplified for now
return 0;
}
// ── bp_to_obj ──
// C ref: shk.c:2758
function bp_to_obj(bp) {
const id = bp.bo_id;
if (bp.useup)
return o_on(id, game.billobjs);
else
return find_oid(id);
}
// ── find_oid ──
// C ref: shk.c:2777
export function find_oid(id) {
let obj;
if ((obj = o_on(id, get_invent())) != null) return obj;
if ((obj = o_on(id, get_fobj())) != null) return obj;
// check monster inventories
for (let mon = get_fmon(); mon; mon = mon.nmon)
if ((obj = o_on(id, mon.minvent)) != null) return obj;
return null;
}
// ── get_cost_of_shop_item ──
// C ref: shk.c:2809
// Returns price for floor items in shops, 0 if not applicable.
// nochrg: positive = no charge, 0 = shop owned, negative = not in shop
export function get_cost_of_shop_item(obj, nochrg_out) {
const u = get_u();
let nochrg = -1;
if (u.ushops && obj.oclass !== COIN_CLASS
&& obj !== u.uball && obj !== u.uchain) {
const x = obj.ox, y = obj.oy;
if (x != null && y != null) {
const rooms_here = in_rooms(x, y, SHOPBASE);
// in_rooms returns raw indices; u.ushops stores chars = idx + ROOMOFFSET
const shopChar = (u.ushops || '').charCodeAt(0);
if (rooms_here.length && (rooms_here[0] + ROOMOFFSET) === shopChar) {
const shkp = shop_keeper(shopChar);
if (shkp && inhishop(shkp)) {
const eshkp = ESHK(shkp);
const freespot = (x === eshkp.shk?.x && y === eshkp.shk?.y);
nochrg = (obj.no_charge || freespot) ? 1 : 0;
if (!nochrg) {
const per_unit = get_cost(obj, shkp);
const units = obj.quan || 1;
if (nochrg_out) nochrg_out.value = nochrg;
return units * per_unit;
}
}
}
}
}
if (nochrg_out) nochrg_out.value = nochrg;
return nochrg > 0 ? -1 : 0; // -1 = no charge, 0 = not in shop
}
// ── costly_gold ──
// C ref: shk.c:5746
export async function costly_gold(x, y, amount, silent) {
if (!costly_spot(x, y)) return;
const rooms_here = in_rooms(x, y, SHOPBASE);
const shkp = shop_keeper(rooms_here[0] + ROOMOFFSET);
if (!shkp) return;
const eshkp = ESHK(shkp);
if (eshkp.credit >= amount) {
if (!silent) {
if (eshkp.credit > amount)
await Your(`credit is reduced by ${amount} ${currency(amount)}.`);
else
await Your("credit is erased.");
}
eshkp.credit -= amount;
} else {
const delta = amount - eshkp.credit;
if (!silent) {
if (eshkp.credit) await Your("credit is erased.");
if (eshkp.debit)
await Your(`debt increases by ${delta} ${currency(delta)}.`);
else
await You(`owe ${shkname(shkp)} ${delta} ${currency(delta)}.`);
}
eshkp.debit += delta;
eshkp.loan += delta;
eshkp.credit = 0;
}
}
// ── block_door ──
// C ref: shk.c:5791
export async function block_door(x, y) {
const rooms_here = in_rooms(x, y, SHOPBASE);
if (rooms_here.length === 0) return false;
const roomno = rooms_here[0];
if (!IS_SHOP(roomno)) return false;
const loc = get_levl(x, y);
if (!IS_DOOR(loc?.typ)) return false;
if (roomno + ROOMOFFSET !== (get_u().ushops || '').charCodeAt(0)) return false;
const shkp = shop_keeper(roomno + ROOMOFFSET);
if (!shkp || !inhishop(shkp)) return false;
const eshkp = ESHK(shkp);
if (shkp.mx === eshkp.shk.x && shkp.my === eshkp.shk.y
&& eshkp.shd.x === x && eshkp.shd.y === y
&& !helpless(shkp)
&& (eshkp.debit || eshkp.billct || eshkp.robbed)) {
await pline(`${Shknam(shkp)} blocks your way!`);
return true;
}
return false;
}
// ── block_entry ──
// C ref: shk.c:5825
export function block_entry(x, y) {
// simplified — full implementation requires door checks
return false;
}
// ── shop_object ──
// C ref: shk.c:5386
export function shop_object(x, y) {
// simplified — returns null for now
return null;
}
// ── price_quote ──
// C ref: shk.c:5406
export function price_quote(first_obj) {
// simplified — full implementation requires windowing
}
// ── shk_chat ──
// C ref: shk.c:5521
export async function shk_chat(shkp) {
if (!shkp.isshk) {
await pline(`${Monnam(shkp)} asks whether you've seen any untended shops recently.`);
return;
}
const eshk = ESHK(shkp);
if (ANGRY(shkp)) {
await pline(`${Shknam(shkp)} mentions how much ${noit_mhe(shkp)} dislikes ${eshk.robbed ? "non-paying" : "rude"} customers.`);
} else if (eshk.following) {
if (eshk.customer !== game.plname) {
eshk.following = 0;
} else {
await pline(`${Shknam(shkp)} says: "Didn't you forget to pay?"`);
}
} else if (eshk.billct) {
const total = addupbill(shkp) + eshk.debit;
await pline(`${Shknam(shkp)} says that your bill comes to ${total} ${currency(total)}.`);
} else if (eshk.debit) {
await pline(`${Shknam(shkp)} reminds you that you owe ${noit_mhim(shkp)} ${eshk.debit} ${currency(eshk.debit)}.`);
} else if (eshk.credit) {
await pline(`${Shknam(shkp)} encourages you to use your ${eshk.credit} ${currency(eshk.credit)} of credit.`);
} else if (eshk.robbed) {
await pline(`${Shknam(shkp)} complains about a recent robbery.`);
} else if (eshk.surcharge) {
await pline(`${Shknam(shkp)} warns you that ${noit_mhe(shkp)} is watching you carefully.`);
} else {
const shkmoney = money_cnt(shkp.minvent);
if (shkmoney < 50)
await pline(`${Shknam(shkp)} complains that business is bad.`);
else if (shkmoney > 4000)
await pline(`${Shknam(shkp)} says that business is good.`);
else
await pline(`${Shknam(shkp)} talks about the problem of shoplifters.`);
}
}
// ── shopdig ──
// C ref: shk.c:5019
export async function shopdig(fall) {
const shkp = shop_keeper((get_u().ushops || '').charCodeAt(0));
if (!shkp) return;
if (!inhishop(shkp)) {
if (Role_if(PM_KNIGHT)) {
await You_feel("like a common thief.");
adjalign(-sgn(get_u().ualign?.type || 0));
}
return;
}
if (!fall) {
if (Role_if(PM_KNIGHT)) {
await You_feel("like a common thief.");
adjalign(-sgn(get_u().ualign?.type || 0));
}
}
// Full shopdig implementation with backpack grabbing omitted — requires
// complex item iteration and worn mask checks
}
// ── bill_dummy_object ──
// C ref: shk.c (called from many places to bill used-up items)
export async function bill_dummy_object(obj) {
if (!(get_u().ushops || '')) return;
const shkp = shop_keeper((get_u().ushops || '').charCodeAt(0));
if (!shkp || !inhishop(shkp)) return;
// Create a dummy bill entry for a used-up object
const dummy = { ...obj };
dummy.where = OBJ_FREE;
dummy.quan = obj.quan;
await add_one_tobill(dummy, true, shkp);
}
// ── alter_cost ──
// C ref: shk.c:3237
export function alter_cost(obj, amt) {
for (let shkp = next_shkp(get_fmon(), true); shkp; shkp = next_shkp(shkp.nmon, true)) {
const bp = onbill(obj, shkp, true);
if (bp) {
const new_price = !amt ? get_cost(obj, shkp) : (amt < 0 ? -amt : amt);
if (new_price > bp.price || amt < 0)
bp.price = new_price;
break;
}
}
}
// ── gem_learned ──
// C ref: shk.c:3198
export function gem_learned(oindx) {
for (let shkp = next_shkp(get_fmon(), true); shkp; shkp = next_shkp(shkp.nmon, true)) {
const eshkp = ESHK(shkp);
const bp = eshkp.bill_p || eshkp.bill;
for (let ct = eshkp.billct - 1; ct >= 0; ct--) {
const obj = find_oid(bp[ct].bo_id);
if (!obj) continue;
if (oindx !== STRANGE_OBJECT ? (obj.otyp === oindx) : (obj.oclass === GEM_CLASS))
bp[ct].price = get_cost(obj, shkp);
}
}
}
// ── record_price_quote / append_price_quote ──
// C ref: shk.c:438-493
export function record_price_quote(otyp, price, buyprice) {
// Tracking of min/max seen prices — simplified
}
export function append_price_quote(buf, eos, otyp) {
// Price quote formatting — simplified
}
// ── credit_report ──
// C ref: shk.c:628
const credit_snap = [[0, 0, 0], [0, 0, 0]];
export async function credit_report(shkp, idx, silent) {
const eshkp = ESHK(shkp);
if (!idx) {
credit_snap[0] = [0, 0, 0];
credit_snap[1] = [0, 0, 0];
} else {
idx = 1;
}
credit_snap[idx][0] = eshkp.credit;
credit_snap[idx][1] = eshkp.debit;
credit_snap[idx][2] = eshkp.loan;
if (idx && !silent) {
let amt = 0;
let msg = "debt has increased";
if (credit_snap[1][0] < credit_snap[0][0]) {
amt = credit_snap[0][0] - credit_snap[1][0];
msg = "credit has been reduced";
} else if (credit_snap[1][1] > credit_snap[0][1]) {
amt = credit_snap[1][1] - credit_snap[0][1];
} else if (credit_snap[1][2] > credit_snap[0][2]) {
amt = credit_snap[1][2] - credit_snap[0][2];
}
if (amt) await Your(`${msg} by ${amt} ${currency(amt)}.`);
}
}
// ── remote_burglary ──
// C ref: shk.c:663
export async function remote_burglary(x, y) {
const rooms_here = in_rooms(x, y, SHOPBASE);
if (rooms_here.length === 0) return;
const shkp = shop_keeper(rooms_here[0] + ROOMOFFSET);
if (!shkp || !inhishop(shkp)) return;
const eshkp = ESHK(shkp);
if (!eshkp.billct && !eshkp.debit) return;
if (await rob_shop(shkp)) {
await call_kops(shkp, false);
}
}
// ── rob_shop ──
// C ref: shk.c:687
async function rob_shop(shkp) {
const eshkp = ESHK(shkp);
await rouse_shk(shkp, true);
let total = addupbill(shkp) + eshkp.debit;
if (eshkp.credit >= total) {
await Your(`credit of ${eshkp.credit} ${currency(eshkp.credit)} is used to cover your shopping bill.`);
total = 0;
} else {
await You("escaped the shop without paying!");
total -= eshkp.credit;
}
setpaid(shkp);
if (!total) return false;
eshkp.robbed += total;
await You(`stole ${total} ${currency(total)} worth of merchandise.`);
if (!Role_if(PM_ROGUE))
adjalign(-sgn(get_u().ualign?.type || 0));
hot_pursuit(shkp);
return true;
}
// ── call_kops ──
// C ref: shk.c:510
async function call_kops(shkp, nearshop) {
if (!shkp) return;
await pline("An alarm sounds!");
// Simplified — full Keystone Kops spawning requires makekops
}
// ── kops_gone ──
// C ref: shk.c:5606
async function kops_gone(silent) {
let cnt = 0;
for (let mtmp = get_fmon(); mtmp; mtmp = mtmp.nmon) {
if (DEADMONSTER(mtmp)) continue;
if (mtmp.data?.mlet === S_KOP) {
if (canspotmon(mtmp)) cnt++;
mongone(mtmp);
}
}
if (cnt && !silent)
await pline_The(`Kop${plur(cnt)} (disappointed) vanish${cnt === 1 ? "es" : ""} into thin air.`);
}
// ── deserted_shop ──
// C ref: shk.c:723
async function deserted_shop(enterstring) {
const g = game;
const roomno = enterstring.charCodeAt(0) - ROOMOFFSET;
const r = get_rooms()[roomno];
if (!r) return;
let n = 0, m = 0;
for (let x = r.lx; x <= r.hx; x++) {
for (let y = r.ly; y <= r.hy; y++) {
if (g.u.ux === x && g.u.uy === y) continue;
const mtmp = m_at(x, y);
if (mtmp) {
n++;
if (sensemon(mtmp)
|| ((mtmp.m_ap_type === 0 /* M_AP_NOTHING */
|| mtmp.m_ap_type === 3 /* M_AP_MONSTER */)
&& canseemon(mtmp)))
m++;
}
}
}
if (Blind() && !(Blind_telepat() || Detect_monsters()))
n++; /* force feedback to be less specific */
await pline(`This shop ${(m < n) ? 'seems to be' : 'is'} ${!n ? 'deserted' : 'untended'}.`);
}
// ── u_entered_shop ──
// C ref: shk.c:751
export async function u_entered_shop(enterstring) {
const g = game;
const u = g.u;
if (!enterstring || !enterstring.length) return;
const shkp = shop_keeper(enterstring.charCodeAt(0));
if (!shkp) {
// C ref: shk.c:762-769 — shop with no keeper
if ((u.ushops_empty || '').indexOf(enterstring[0]) < 0
&& in_rooms(u.ux, u.uy, SHOPBASE)
!== in_rooms(u.ux0, u.uy0, SHOPBASE))
await deserted_shop(enterstring);
u.ushops_empty = u.ushops || '';
u.ushops = '';
return;
}
const eshkp = ESHK(shkp);
if (!inhishop(shkp)) {
// C ref: shk.c:774-781 — shopkeeper not in their shop
eshkp.bill_p = -1000; // sentinel for dump core
if ((u.ushops_empty || '').indexOf(enterstring[0]) < 0)
await deserted_shop(enterstring);
u.ushops_empty = u.ushops || '';
u.ushops = '';
return;
}
record_achievement(ACH_SHOP);
eshkp.bill_p = eshkp.bill;
// C ref: shk.c:787-794 — new customer initialization
if ((!eshkp.visitct || eshkp.customer)
&& (eshkp.customer || '').toLowerCase() !== (g.plname || '').toLowerCase()) {
eshkp.visitct = 0;
eshkp.following = 0;
eshkp.customer = g.plname || '';
pacify_shk(shkp, true);
}
if (muteshk(shkp) || eshkp.following)
return; /* no dialog */
// C ref: shk.c:799-809 — invisible hero
if (Invis()) {
await pline(`${Shknam(shkp)} senses your presence.`);
if (!Deaf() && !muteshk(shkp)) {
SetVoice(shkp, 0, 80, 0);
await verbalize('Invisible customers are not welcome!');
} else {
await pline(`${Shknam(shkp)} stands firm as if ${noit_mhe(shkp)} knows you are there.`);
}
return;
}
const rt = get_rooms()[enterstring.charCodeAt(0) - ROOMOFFSET]?.rtype || SHOPBASE;
// C ref: shk.c:813-853 — greeting based on shopkeeper state
if (ANGRY(shkp)) {
if (!Deaf() && !muteshk(shkp)) {
SetVoice(shkp, 0, 80, 0);
await verbalize(`So, ${g.plname}, you dare return to ${s_suffix(shkname(shkp))} ${shtypes[rt - SHOPBASE]?.name || 'shop'}?!`);
} else {
const texts = angrytexts;
const angryText = texts[rn2(texts.length)];
await pline(`${Shknam(shkp)} seems ${angryText} over your return to ${noit_mhis(shkp)} ${shtypes[rt - SHOPBASE]?.name || 'shop'}!`);
}
} else if (eshkp.surcharge) {
if (!Deaf() && !muteshk(shkp)) {
SetVoice(shkp, 0, 80, 0);
await verbalize(`Back again, ${g.plname}? I've got my ${mbodypart(shkp, EYE)} on you.`);
} else {
await pline_The(`atmosphere at ${s_suffix(shkname(shkp))} ${shtypes[rt - SHOPBASE]?.name || 'shop'} seems unwelcoming.`);
}
} else if (eshkp.robbed) {
if (!Deaf()) {
Soundeffect('se_mutter_imprecations', 50);
await pline(`${Shknam(shkp)} mutters imprecations against shoplifters.`);
} else {
await pline(`${Shknam(shkp)} is combing through ${noit_mhis(shkp)} inventory list.`);
}
} else {
// C ref: shk.c:841-852 — normal welcome greeting
if (!Deaf() && !muteshk(shkp)) {
set_voice(shkp, 0, 80, 0);
await verbalize(`${Hello(shkp)}, ${g.plname}! Welcome${eshkp.visitct++ ? ' again' : ''} to ${s_suffix(shkname(shkp))} ${shtypes[rt - SHOPBASE]?.name || 'shop'}!`);
} else {
await You(`enter ${s_suffix(shkname(shkp))} ${shtypes[rt - SHOPBASE]?.name || 'shop'}${eshkp.visitct++ ? ' again' : ''}!`);
}
}
// C ref: shk.c:854-916 — blocking at shop entrance
if (!inside_shop(u.ux, u.uy)) {
let should_block = false;
const not_upset = !eshkp.surcharge;
let cnt;
let tool;
let pick = carrying(PICK_AXE);
let mattock = carrying(DWARVISH_MATTOCK);
if (pick || mattock) {
cnt = 1;
if (pick && mattock) {
tool = 'digging tool';
cnt = 2;
} else if (pick) {
tool = 'pick-axe';
let p = pick;
while ((p = p.nobj) != null)
if (p.otyp === PICK_AXE) cnt++;
} else {
tool = 'mattock';
let m = mattock;
while ((m = m.nobj) != null)
if (m.otyp === DWARVISH_MATTOCK) cnt++;
if (!Blind())
makeknown(DWARVISH_MATTOCK);
}
if (!Deaf() && !muteshk(shkp)) {
SetVoice(shkp, 0, 80, 0);
await verbalize(not_upset
? `Will you please leave your ${tool}${plur(cnt)} outside?`
: `Leave the ${tool}${plur(cnt)} outside.`);
} else {
await pline(`${Shknam(shkp)} ${not_upset ? 'is hesitant' : 'refuses'} to let you in with your ${tool}${plur(cnt)}.`);
}
should_block = true;
} else if (u.usteed) {
if (!Deaf() && !muteshk(shkp)) {
SetVoice(shkp, 0, 80, 0);
await verbalize(not_upset
? `Will you please leave ${y_monnam(u.usteed)} outside?`
: `Leave ${y_monnam(u.usteed)} outside.`);
} else {
await pline(`${Shknam(shkp)} ${not_upset ? "doesn't want" : 'refuses'} to let you in while you're riding ${y_monnam(u.usteed)}.`);
}
should_block = true;
} else {
should_block = (Fast()
&& (sobj_at(PICK_AXE, u.ux, u.uy)
|| sobj_at(DWARVISH_MATTOCK, u.ux, u.uy)));
}
if (should_block)
await dochug(shkp); /* shk gets extra move */
}
}
// ── u_left_shop ──
// C ref: shk.c:578
export async function u_left_shop(leavestring, newlev) {
if (!leavestring) return;
const shkp = shop_keeper(leavestring.charCodeAt(0) || (get_u().ushops0 || '').charCodeAt(0));
if (!shkp || !inhishop(shkp)) return;
const eshkp = ESHK(shkp);
if (!eshkp.billct && !eshkp.debit) return;
if (await rob_shop(shkp)) {
await call_kops(shkp, false);
}
}
// ── pick_pick ──
// C ref: shk.c:920
export function pick_pick(obj) {
if (obj.unpaid || !is_pick(obj)) return;
const shkp = shop_keeper((get_u().ushops || '').charCodeAt(0));
if (shkp && inhishop(shkp)) {
// message about pick-axe in shop — simplified
}
}
// ── shk_move ──
// C ref: shk.c:4880
// ── after_shk_move ──
// C ref: shk.c:4997
export async function after_shk_move(shkp) {
const eshkp = ESHK(shkp);
if (eshkp.bill_p === -1000 && inhishop(shkp)) {
eshkp.bill_p = eshkp.bill;
await check_special_room(false);
}
}
// ── shk_fixes_damage ──
// C ref: shk.c:4556
function shk_fixes_damage(shkp) {
// Simplified — full repair logic requires damage list traversal
}
// ── discard_damage_owned_by ──
// C ref: shk.c:4530
function discard_damage_owned_by(shkp) {
if (!game.level?.damagelist) return;
const eshkp = ESHK(shkp);
game.level.damagelist = game.level.damagelist.filter(dam => {
const rooms_here = in_rooms(dam.place.x, dam.place.y, SHOPBASE);
return !rooms_here.includes(eshkp.shoproom - ROOMOFFSET);
});
}
// ── fix_shop_damage ──
// C ref: shk.c:4851
export function fix_shop_damage() {
// Simplified — requires full repair_damage implementation
}
// ── paybill ──
// C ref: shk.c:2485
// C ref: shk.c:2486 paybill — settle hero's shop bills at death/escape/quit
export async function paybill(croaked, silently) {
if (croaked < 0) return false;
const g = game;
let taken = false;
let numsk = 0;
let resident = null, creditor = null, hostile = null, localshk = null;
// Scan shopkeepers to find highest-priority one
for (let mtmp = next_shkp(g.fmon, false); mtmp;
mtmp = next_shkp(mtmp.nmon, false)) {
const eshkp = ESHK(mtmp);
if (!eshkp) continue;
const local = on_level(eshkp.shoplevel, g.u.uz);
if (local && (g.u.ushops || '').includes(
String.fromCharCode((eshkp.shoproom ?? 0)))) {
if (!resident || eshkp.billct || eshkp.debit || eshkp.robbed)
resident = mtmp;
} else if (eshkp.billct || eshkp.debit || eshkp.robbed) {
if (!creditor) creditor = mtmp;
} else if (eshkp.following || ANGRY(mtmp)) {
if (!hostile) hostile = mtmp;
} else if (local) {
if (!localshk) localshk = mtmp;
}
}
const firstshk = resident || creditor || hostile || localshk;
if (firstshk) {
numsk++;
taken = await inherits(firstshk, numsk, croaked, silently);
}
// Handle remaining shopkeepers
for (let mtmp = next_shkp(g.fmon, false); mtmp; ) {
const mtmp2 = mtmp.nmon;
if (mtmp !== firstshk) {
numsk++;
taken = (await inherits(mtmp, numsk, croaked, silently)) || taken;
}
mtmp = next_shkp(mtmp2, false);
}
return taken;
}
// C ref: shk.c:2577 inherits — shopkeeper takes dying hero's inventory
async function inherits(shkp, numsk, croaked, silently) {
const g = game;
const eshkp = ESHK(shkp);
if (!eshkp) return false;
let loss = 0;
let take = false, taken = false;
const uinshop = !!(g.u.ushops || '').includes(
String.fromCharCode(eshkp.shoproom ?? 0));
shkp.minvis = shkp.perminvis = 0;
if (numsk > 1) {
if (cansee(shkp.mx, shkp.my) && croaked && !silently) {
await pline(`${Shknam(shkp)} looks at your corpse and sighs.`);
}
taken = uinshop;
// skip to rouse/home
await rouse_shk(shkp, false);
if (!inhishop(shkp)) await home_shk(shkp, false);
setpaid(shkp);
return taken;
}
// Simple case: die in shop, peaceful, nothing owed
if (uinshop && inhishop(shkp) && !eshkp.billct
&& !eshkp.robbed && !eshkp.debit && NOTANGRY(shkp)
&& !eshkp.following && (g.u.ugrave_arise ?? -1) < 0) {
taken = !!g.invent;
if (taken && !silently)
await pline(`${Shknam(shkp)} gratefully inherits all your possessions.`);
setpaid(shkp);
return taken;
}
if (eshkp.billct || eshkp.debit || eshkp.robbed) {
if (uinshop && inhishop(shkp))
loss = addupbill(shkp) + eshkp.debit;
if (loss < eshkp.robbed)
loss = eshkp.robbed;
take = true;
}
if (eshkp.following || ANGRY(shkp) || take) {
if (!g.invent) {
await rouse_shk(shkp, false);
if (!inhishop(shkp)) await home_shk(shkp, false);
setpaid(shkp);
return taken;
}
const umoney = money_cnt(get_invent());
let takes = '';
if (helpless(shkp)) takes += 'wakes up and ';
if (!m_next2u(shkp)) takes += 'comes and ';
takes += 'takes';
if (loss > umoney || !loss || uinshop) {
eshkp.robbed -= umoney;
if (eshkp.robbed < 0) eshkp.robbed = 0;
if (umoney > 0) {
await money2mon(shkp, umoney);
setBotl('inherits');
}
if (!silently)
await pline(`${Shknam(shkp)} ${takes} all your possessions.`);
taken = true;
} else {
await money2mon(shkp, loss);
setBotl('inherits');
if (!silently)
await pline(`${Shknam(shkp)} ${takes} the ${loss} ${currency(loss)} you owed ${noit_mhim(shkp)}.`);
pacify_shk(shkp, false);
eshkp.following = 0;
eshkp.robbed = 0;
}
await rouse_shk(shkp, false);
if (!inhishop(shkp)) await home_shk(shkp, false);
}
setpaid(shkp);
return taken;
}
// ── finish_paybill ──
// C ref: shk.c:2723
export function finish_paybill() {
// Simplified
}
// ── sortbill_cmp ──
// C ref: shk.c:1498-1518
function sortbill_cmp(sbi1, sbi2) {
const cost1 = sbi1.cost, cost2 = sbi2.cost;
const bidx1 = sbi1.bidx, bidx2 = sbi2.bidx;
// used-up items (FullyUsedUp, PartlyUsedUp) sort before intact
const used1 = (sbi1.usedup <= PartlyUsedUp) ? 1 : 0;
const used2 = (sbi2.usedup <= PartlyUsedUp) ? 1 : 0;
if (used1 !== used2)
return used2 - used1; // bigger (used) comes first
if (cost1 !== cost2)
return cost2 - cost1; // more expensive comes first
return bidx1 - bidx2;
}
// ── cheapest_item ──
// C ref: shk.c:1521-1539
function cheapest_item(ibillct, ibill) {
let gmin = ibill[0].cost;
for (let i = 1; i < ibillct; ++i)
if (ibill[i].cost < gmin)
gmin = ibill[i].cost;
return gmin;
}
// ── make_itemized_bill ──
// C ref: shk.c:1544-1663
function make_itemized_bill(shkp) {
const eshkp = ESHK(shkp);
const ebillct = eshkp.billct;
const bp_arr = eshkp.bill_p;
const ibill = [];
for (let i = 0; i < ebillct; ++i) {
const bp = bp_arr[i];
let otmp = bp_to_obj(bp);
if (!otmp) {
impossible(`Can't find shop bill entry for #${bp.bo_id}`);
continue;
}
let bidx = i;
if (otmp.quan === 0 || otmp.where === OBJ_ONBILL) {
// completely used up
otmp.quan = bp.bquan;
bp.useup = true;
} else if (otmp.quan < bp.bquan) {
// partly used up — create entry for used-up portion
const usedquan = bp.bquan - otmp.quan;
ibill.push({
obj: otmp,
quan: usedquan,
cost: bp.price * usedquan,
bidx: bidx,
usedup: PartlyUsedUp,
queuedpay: false,
});
// intact portion will be a separate entry, next
}
let quan, cost, used;
if (otmp.where === OBJ_ONBILL) {
// completely used up
quan = bp.bquan;
cost = bp.price * quan;
used = FullyUsedUp;
} else if (otmp.where === OBJ_CONTAINED || Has_contents(otmp)) {
const item = otmp;
let cknown = true;
// find outermost container
while (otmp.where === OBJ_CONTAINED) {
otmp = otmp.ocontainer;
if (!otmp.cknown)
cknown = false;
}
// check if container already in ibill[]
let j;
for (j = 0; j < ibill.length; ++j)
if (otmp === ibill[j].obj)
break;
if (j < ibill.length) {
// already present — update if needed
if (ibill[j].usedup === FullyIntact)
ibill[j].usedup = cknown ? KnownContainer : UndisclosedContainer;
continue;
}
// include 1 container containing unpaid item(s)
quan = 1;
cost = unpaid_cost(otmp, COST_CONTENTS);
if (!otmp.unpaid)
bidx = -1;
used = (otmp === item) ? FullyIntact
: cknown ? KnownContainer
: UndisclosedContainer;
} else {
// ordinary unpaid
quan = otmp.quan;
cost = bp.price * quan;
used = (quan < bp.bquan) ? PartlyIntact : FullyIntact;
}
ibill.push({
obj: otmp,
quan: quan,
cost: cost,
bidx: bidx,
usedup: used,
queuedpay: false,
});
}
if (ibill.length > 1)
ibill.sort(sortbill_cmp);
return ibill;
}
// ── menu_pick_pay_items ──
// C ref: shk.c:1668-1739
async function menu_pick_pay_items(ibillct, ibill) {
const menu = create_nhwindow_menu();
start_menu(menu);
// first pass: find largest cost for formatting width
let largest_amt = 0;
for (let i = 0; i < ibillct; ++i)
if (ibill[i].cost > largest_amt)
largest_amt = ibill[i].cost;
const amt_width = String(largest_amt).length;
// "Used up items:" header if first entry is used-up
if (ibill[0].usedup <= PartlyUsedUp) {
const hdr = `Used up item${(ibillct > 1 && ibill[1].usedup <= PartlyUsedUp) ? 's' : ''}:`;
add_menu_str(menu, hdr);
}
for (let i = 0; i < ibillct; ++i) {
// "Unpaid items:" header at transition from used-up to intact
if (i > 0 && ibill[i - 1].usedup <= PartlyUsedUp
&& ibill[i].usedup >= PartlyIntact) {
const hdr = `Unpaid item${(i < ibillct - 1) ? 's' : ''}:`;
add_menu_str(menu, hdr);
}
const otmp = ibill[i].obj;
const save_quan = otmp.quan;
otmp.quan = ibill[i].quan;
const p = paydoname(otmp);
otmp.quan = save_quan;
const amt = ibill[i].cost;
const buf = `${String(amt).padStart(amt_width)} Zm, ${p}`;
add_menu(menu, i + 1, 0, 0, ATR_NONE, NO_COLOR, buf, MENU_ITEMFLAGS_NONE);
}
end_menu(menu, "Pay for which items?");
const result = await select_menu(menu, PICK_ANY);
destroy_nhwindow_menu(menu);
const n = result.count;
for (let j = 0; j < Math.max(n, 0); ++j) {
const idx = result.items[j].identifier - 1;
ibill[idx].queuedpay = true;
}
return Math.max(n, 0);
}
// ── reject_purchase ──
// C ref: shk.c:2419-2452
async function reject_purchase(shkp, obj, billed_quan) {
const intact_quan = obj.quan;
obj.quan = billed_quan - intact_quan;
if (!Deaf() && !muteshk(shkp)) {
let which;
if (obj.where === OBJ_CONTAINED)
which = `the one${plur(intact_quan)} in ${the(simpleonames(obj.ocontainer))}`;
else
which = (intact_quan > 1) ? "these" : "this one";
SetVoice(shkp, 0, 80, 0);
await verbalize(`${ANGRY(shkp) ? 'Pay' : 'Please pay'} for the other ${simpleonames(obj)} before buying ${which}.`);
} else {
await pline(`${Shknam(shkp)} ${ANGRY(shkp) ? 'angrily ' : ''}${nolimbs(shkp.data) ? 'motions to' : 'points out'} your bill for the other ${simpleonames(obj)} first.`);
}
obj.quan = intact_quan;
}
// ── insufficient_funds ──
// C ref: shk.c:2455-2482
async function insufficient_funds(shkp, item, cost) {
const umoney = money_cnt(get_invent());
const ecredit = ESHK(shkp).credit || 0;
if (!cost && umoney + ecredit === 0) {
const stashed_gold = hidden_gold(true);
await You(`${(stashed_gold > 0) ? 'seem to ' : ''}have no gold or credit left.`);
return true;
}
if (cost && umoney + ecredit < cost) {
const stashed_gold = hidden_gold(true);
await You("don't%s have gold%s enough to pay for %s.",
(stashed_gold > 0) ? " seem to" : "",
(ecredit > 0) ? " or credit" : "",
paydoname(item));
return true;
}
return false;
}
// ── dopayobj ──
// C ref: shk.c:2221-2303
async function dopayobj(shkp, bp, obj, which, itemize, unseen) {
const consumed = (which === 0);
if (!obj.unpaid && !bp.useup
&& !(Has_contents(obj) && unpaid_cost(obj, COST_CONTENTS))) {
impossible("Paid object on bill??");
return PAY_BUY;
}
if (itemize && await insufficient_funds(shkp, obj, 0)) {
return PAY_BROKE;
}
const save_quan = obj.quan;
let quan;
if (consumed) {
quan = bp.bquan;
if (quan > obj.quan) quan -= obj.quan;
} else {
quan = obj.quan;
}
const ltmp = bp.price * quan;
obj.quan = quan;
let buy = PAY_BUY;
if (itemize) {
// C ref: shk.c:2269-2275
const qsfx = ` for ${ltmp} ${currency(ltmp)}. Pay?`;
const desc = (quan === 1) ? Doname2(obj) : doname(obj);
const qbuf = desc + qsfx;
const ans = await ynFunction(qbuf, 'yn', 'n');
if (ans === 'n') buy = PAY_SKIP;
}
if (quan < bp.bquan && !consumed) {
// partly used goods — shk won't sell intact until used-up is paid
await reject_purchase(shkp, obj, bp.bquan);
buy = PAY_SKIP;
}
if (buy === PAY_BUY && await insufficient_funds(shkp, obj, ltmp)) {
buy = itemize ? PAY_SKIP : PAY_CANT;
}
if (buy === PAY_BUY) {
await pay(ltmp, shkp);
if (!unseen)
await shk_names_obj(shkp, obj,
consumed
? "paid for %s at a cost of %ld gold piece%s.%s"
: "bought %s for %ld gold piece%s.%s",
ltmp, "");
}
obj.quan = save_quan;
return buy;
}
// ── update_bill ──
// C ref: shk.c:2170-2212
function update_bill(indx, ibillct, ibill, eshkp, bp, paiditem) {
if (indx >= 0 && ibill[indx].usedup === PartlyUsedUp) {
// paid for used-up portion of partly used item
bp.bquan = paiditem.quan;
for (let j = 0; j < ibillct; ++j)
if (ibill[j].obj === paiditem && ibill[j].usedup === PartlyIntact) {
ibill[j].usedup = FullyIntact;
break;
}
} else {
// item bought — remove from bill
paiditem.unpaid = 0;
if (paiditem.where === OBJ_ONBILL) {
obj_extract_self(paiditem);
dealloc_obj(paiditem);
}
const newebillct = eshkp.billct - 1;
const bp_arr = eshkp.bill_p;
const vacated_idx = bp_arr.indexOf(bp);
// move last entry into vacated slot (C: *bp = eshkp->bill_p[newebillct])
if (vacated_idx >= 0 && vacated_idx < newebillct) {
const old_last_idx = newebillct;
bp_arr[vacated_idx] = bp_arr[old_last_idx];
// update ibill[] indices that pointed to the moved entry
for (let j = 0; j < ibillct; ++j)
if (ibill[j].bidx === old_last_idx)
ibill[j].bidx = vacated_idx;
}
eshkp.billct = newebillct;
}
}
// ── buy_container ──
// C ref: shk.c:2309-2412
async function buy_container(shkp, indx, ibillct, ibill) {
const eshkp = ESHK(shkp);
let ebillct = eshkp.billct;
const container = ibill[indx].obj;
const unpaidcontainer = container.unpaid;
const totalcost = ibill[indx].cost;
const sightunseen = ibill[indx].usedup === UndisclosedContainer
|| ibill[indx].usedup === KnownContainer;
// check for no-gold then not-enough-gold
if (await insufficient_funds(shkp, container, 0)
|| await insufficient_funds(shkp, container, totalcost))
return 1;
// check for partly intact portion of not-yet-paid partly used item
const boids = [];
for (let i = 0; i < ebillct; ++i) {
const bp = eshkp.bill_p[i];
const otmp = bp_to_obj(bp);
if (!otmp) {
impossible(`Can't find contained item on shop bill (#${bp.bo_id}).`);
return 2;
}
if (otmp.where !== OBJ_CONTAINED && !Has_contents(otmp))
continue;
// find outermost container
let otop = otmp;
while (otop.where === OBJ_CONTAINED)
otop = otop.ocontainer;
if (otop !== container)
continue;
// check for partly intact portion of partly used item
if (otmp.quan < bp.bquan) {
await reject_purchase(shkp, otmp, bp.bquan);
return 1;
}
// record for second pass (container itself deferred to last)
if (bp.bo_id !== container.o_id)
boids.push(bp.bo_id);
}
if (unpaidcontainer)
boids.push(container.o_id);
// purchasing pass
let buycount = 0;
for (let j = 0; j < boids.length; ++j) {
const boid = boids[j];
let i, bp;
for (i = 0, bp = eshkp.bill_p[0]; i < ebillct; ++i, bp = eshkp.bill_p[i]) {
if (eshkp.bill_p[i].bo_id === boid) {
bp = eshkp.bill_p[i];
break;
}
}
if (i === ebillct) {
impossible(`Buying ${simpleonames(container)} contents: item #${boid} disappeared from bill.`);
return 2;
}
const otmp = bp_to_obj(bp);
const buy = await dopayobj(shkp, bp, otmp, 1, false, sightunseen);
if (buy !== PAY_BUY) {
impossible(`Buying ${simpleonames(container)} contents failed unexpectedly (#${otmp.o_id} ${buy}).`);
continue;
}
ibill[indx].cost -= (bp.price * bp.bquan);
update_bill((boid === container.o_id) ? indx : -1,
ibillct, ibill, eshkp, bp, otmp);
++buycount;
// ebillct may have changed
ebillct = eshkp.billct;
}
if (buycount && sightunseen) {
if (unpaidcontainer)
container.unpaid = container.no_charge = 1;
await shk_names_obj(shkp, container,
"bought %s for %ld gold piece%s.%s",
totalcost, "");
container.unpaid = container.no_charge = 0;
}
return buycount ? 0 : 2;
}
// ── pay_billed_items ──
// C ref: shk.c:2045-2168
async function pay_billed_items(shkp, ibillct, ibill, stashed_gold, paid_ref) {
const eshkp = ESHK(shkp);
let umoney = money_cnt(get_invent());
if (!umoney && !eshkp.credit) {
await You(`${stashed_gold ? 'seem to ' : ''}have no gold or credit${paid_ref.val ? ' left' : ''}.`);
return true;
}
const bp = eshkp.bill_p;
const otmp = bp_to_obj(bp[0]);
const ebillct = eshkp.billct;
const more_than_one = (ebillct > 1 || otmp.quan < bp[0].bquan
|| ibill[0].usedup === UndisclosedContainer);
if ((umoney + eshkp.credit) < cheapest_item(ibillct, ibill)) {
await You("don't have enough gold to buy%s the item%s %s.",
more_than_one ? " any of" : "", plur(more_than_one ? 2 : 1),
(ebillct > 1) ? "you've picked" : "on your bill");
if (stashed_gold)
await pline("Maybe you have some gold stashed away?");
return true;
}
let via_menu = (game.flags?.menu_style !== MENU_TRADITIONAL);
if (game.iflags?.menu_requested)
via_menu = !via_menu;
let itemize, queuedpay = false;
do {
if (via_menu) {
if (!await menu_pick_pay_items(ibillct, ibill))
return true;
queuedpay = true;
itemize = false;
via_menu = false;
} else {
const iprompt = !more_than_one ? 'y'
: await ynFunction("Itemized billing?", "ynq m", 'q');
if (iprompt === 'q')
return true;
itemize = (iprompt === 'y');
via_menu = (iprompt === 'm');
}
} while (via_menu);
for (let indx = 0; indx < ibillct; ++indx) {
if (queuedpay && !ibill[indx].queuedpay)
continue;
const otmp_i = ibill[indx].obj;
let buy;
if (ibill[indx].usedup >= KnownContainer) {
const boxbag_result = await buy_container(shkp, indx, ibillct, ibill);
if (boxbag_result === 0) {
buy = PAY_BUY;
} else {
if (boxbag_result === 2)
await verbalize("You need to remove any unpaid items from"
+ ` that ${simpleonames(otmp_i)} and buy them separately.`);
buy = PAY_CANT;
}
} else {
const bidx = ibill[indx].bidx;
const bp_i = eshkp.bill_p[bidx];
const pass = (ibill[indx].usedup <= PartlyUsedUp) ? 0 : 1;
buy = await dopayobj(shkp, bp_i, otmp_i, pass, itemize, false);
if (buy === PAY_BUY)
update_bill(indx, ibillct, ibill, eshkp, bp_i, otmp_i);
}
switch (buy) {
case PAY_CANT:
return false;
case PAY_BROKE:
paid_ref.val = true;
return true;
case PAY_SKIP:
continue;
case PAY_BUY:
paid_ref.val = true;
if (itemize || queuedpay) {
update_inventory();
await bot();
}
break;
}
}
return true;
}
// ── dopay ──
// C ref: shk.c:1742-2036
export async function dopay() {
const u = get_u();
game.multi = 0;
pushRngLogEntry('^multi[dopay=0]');
// Ensure u.ushops is current (check_special_room may not have run
// if the hero didn't move this turn, e.g., pressing 'p' to pay).
if (!u.ushops) {
const shopRooms = in_rooms(u.ux, u.uy, SHOPBASE);
if (shopRooms.length > 0)
u.ushops = String.fromCharCode(shopRooms[0] + ROOMOFFSET);
}
let sk = 0, seensk = 0, nexttosk = 0;
let nxtm = null, resident = null;
const stashed_gold = (hidden_gold(true) > 0);
for (let shkp = next_shkp(get_fmon(), false); shkp; shkp = next_shkp(shkp.nmon, false)) {
sk++;
if (m_next2u(shkp)) {
if (nxtm && ANGRY(nxtm)) continue;
nexttosk++;
nxtm = shkp;
}
if (canspotmon(shkp)) seensk++;
if (inhishop(shkp) && (u.ushops || '').charCodeAt(0) === ESHK(shkp).shoproom)
resident = shkp;
}
let shkp;
if (nxtm && nexttosk === 1) {
shkp = nxtm;
} else if ((!sk && (!Blind() || Blind_telepat())) || (!Blind() && !seensk)) {
await There("appears to be no shopkeeper here to receive your payment.");
return 0;
} else if (!seensk) {
await You_cant("see...");
return 0;
} else if (sk === 1 && resident) {
shkp = resident;
} else if (seensk === 1) {
for (shkp = next_shkp(get_fmon(), false); shkp; shkp = next_shkp(shkp.nmon, false))
if (canspotmon(shkp))
break;
if (shkp !== resident && !m_next2u(shkp)) {
await pline(`${Shknam(shkp)} is not near enough to receive your payment.`);
return 0;
}
} else {
// multiple visible shopkeepers — would need getpos; simplified
shkp = resident;
}
if (!shkp) return 0;
const eshkp = ESHK(shkp);
let ltmp = eshkp.robbed;
if (ltmp || eshkp.billct || eshkp.debit)
await rouse_shk(shkp, true);
if (helpless(shkp)) {
await pline(`${Shknam(shkp)} ${rn2(2) ? "seems to be napping" : "doesn't respond"}.`);
return 0;
}
// C ref: shk.c:1870-1897 — shkp != resident and NOTANGRY
if (shkp !== resident && NOTANGRY(shkp)) {
let umoney = money_cnt(get_invent());
if (!ltmp) {
await You(`do not owe ${shkname(shkp)} anything.`);
} else if (!umoney) {
await You(`${stashed_gold ? 'seem to ' : ''}have no gold.`);
if (stashed_gold)
await pline("But you have some gold stashed away.");
} else {
if (umoney > ltmp) {
await You(`give ${shkname(shkp)} the ${ltmp} gold piece${plur(ltmp)} ${noit_mhe(shkp)} asked for.`);
await pay(ltmp, shkp);
} else {
await You(`give ${shkname(shkp)} all your${stashed_gold ? " openly kept" : ""} gold.`);
await pay(umoney, shkp);
if (stashed_gold)
await pline("But you have hidden gold!");
}
if ((umoney < Math.trunc(ltmp / 2)) || (umoney < ltmp && stashed_gold))
await pline(`Unfortunately, ${noit_mhe(shkp)} doesn't look satisfied.`);
else
await make_happy_shk(shkp, false);
}
return 1;
}
// C ref: shk.c:1900-1945 — no bill, no debit
if (!eshkp.billct && !eshkp.debit) {
let umoney = money_cnt(get_invent());
if (!ltmp && NOTANGRY(shkp)) {
await You(`do not owe ${shkname(shkp)} anything.`);
if (!umoney)
await pline(`Moreover, you${stashed_gold ? " seem to" : ""} have no gold.`);
} else if (ltmp) {
await pline(`${shkname(shkp)} is after blood, not gold!`);
if (umoney < Math.trunc(ltmp / 2) || (umoney < ltmp && stashed_gold)) {
if (!umoney)
await pline(`Moreover, you${stashed_gold ? " seem to" : ""} have no gold.`);
else
await pline(`Besides, you don't have enough to interest ${noit_mhim(shkp)}.`);
return 1;
}
await pline(`But since ${noit_mhis(shkp)} shop has been robbed recently,`);
await pline(`you ${(umoney < ltmp) ? "partially " : ""}compensate ${shkname(shkp)} for ${noit_mhis(shkp)} losses.`);
await pay(umoney < ltmp ? umoney : ltmp, shkp);
await make_happy_shk(shkp, false);
} else {
// angry but not robbed
await pline(`${Shknam(shkp)} is after your hide, not your gold!`);
if (umoney < 1000) {
if (!umoney)
await pline(`Moreover, you${stashed_gold ? " seem to" : ""} have no gold.`);
else
await pline(`Besides, you don't have enough to interest ${noit_mhim(shkp)}.`);
return 1;
}
await You(`try to appease ${canspotmon(shkp) ? x_monnam(shkp, 2 /*ARTICLE_THE*/, "angry", 0, false) : shkname(shkp)} by giving ${noit_mhim(shkp)} 1000 gold pieces.`);
await pay(1000, shkp);
if ((eshkp.customer || '') !== (game.plname || '').substring(0, PL_NSIZ) || rn2(3))
await make_happy_shk(shkp, false);
else
await pline(`But ${shkname(shkp)} is as angry as ever.`);
}
return 1;
}
// C ref: shk.c:1946-1951
if (shkp !== resident) {
impossible("dopay: not to shopkeeper?");
if (resident) setpaid(resident);
return 0;
}
// C ref: shk.c:1952-2000 — pay debt first
let paid = false;
if (eshkp.debit) {
let dtmp = eshkp.debit;
const loan = eshkp.loan;
let umoney = money_cnt(get_invent());
let sbuf = `You owe ${shkname(shkp)} ${dtmp} ${currency(dtmp)} `;
if (loan) {
if (loan === dtmp)
sbuf += "you picked up in the store.";
else
sbuf += "for gold picked up and the use of merchandise.";
} else {
sbuf += "for the use of merchandise.";
}
await pline(sbuf);
if (umoney + eshkp.credit < dtmp) {
await pline(`But you don't${stashed_gold ? " seem to" : ""} have enough gold${eshkp.credit ? " or credit" : ""}.`);
return 1;
} else {
if (eshkp.credit >= dtmp) {
eshkp.credit -= dtmp;
eshkp.debit = 0;
eshkp.loan = 0;
await Your("debt is covered by your credit.");
} else if (!eshkp.credit) {
await money2mon(shkp, dtmp);
eshkp.debit = 0;
eshkp.loan = 0;
await You("pay that debt.");
setBotl('dopay');
} else {
dtmp -= eshkp.credit;
eshkp.credit = 0;
await money2mon(shkp, dtmp);
eshkp.debit = 0;
eshkp.loan = 0;
await pline("That debt is partially offset by your credit.");
await You("pay the remainder.");
setBotl('dopay');
}
paid = true;
}
}
// C ref: shk.c:2002-2009 — pay bill items
let pay_done = true;
if (eshkp.billct) {
const ibill = make_itemized_bill(shkp);
const ibillct = ibill.length;
const paid_ref = { val: paid };
if (!await pay_billed_items(shkp, ibillct, ibill, stashed_gold, paid_ref))
pay_done = false;
paid = paid_ref.val;
}
// C ref: shk.c:2011-2025 — thank you message
if (pay_done && !ANGRY(shkp) && paid) {
if (!Deaf() && !muteshk(shkp)) {
SetVoice(shkp, 0, 80, 0);
await verbalize(`Thank you for shopping in ${s_suffix(shkname(shkp))} ${shtypes[eshkp.shoptype - SHOPBASE]?.name || 'shop'}${!eshkp.surcharge ? '!' : '.'}`);
} else {
await pline(`${Shknam(shkp)} nods${!eshkp.surcharge ? " appreciatively" : ""} at you for shopping in ${noit_mhis(shkp)} ${shtypes[eshkp.shoptype - SHOPBASE]?.name || 'shop'}${!eshkp.surcharge ? "!" : "."}`);
}
}
if (paid)
update_inventory();
if (game.iflags) game.iflags.menu_requested = false;
return paid ? 1 : 0;
}
// C ref: shk.c:5139 getcad — shopkeeper reacts angrily to damage
async function getcad(shkp, dmgstr, x, y, uinshp, animal, pursue) {
const dugwall = (dmgstr === 'dig into' || dmgstr === 'damage');
if (muteshk(shkp)) {
if (animal && !helpless(shkp))
await yelp(shkp);
} else if (pursue || uinshp || !um_dist(x, y, 1)) {
if (!Deaf()) {
SetVoice(shkp, 0, 80, 0);
await verbalize(`How dare you ${dmgstr} my ${dugwall ? 'shop' : 'door'}?`);
} else {
await pline(`${Shknam(shkp)} is ${ROLL_FROM(angrytexts)} that you decided to ${dmgstr} ${noit_mhis(shkp)} ${dugwall ? 'shop' : 'door'}!`);
}
} else {
if (!Deaf()) {
await pline(`${Shknam(shkp)} shouts:`);
SetVoice(shkp, 0, 80, 0);
await verbalize(`Who dared ${dmgstr} my ${dugwall ? 'shop' : 'door'}?`);
} else {
await pline(`${Shknam(shkp)} is ${ROLL_FROM(angrytexts)} that someone decided to ${dmgstr} ${noit_mhis(shkp)} ${dugwall ? 'shop' : 'door'}!`);
}
}
hot_pursuit(shkp);
}
// ── pay_for_damage ──
// C ref: shk.c:5174
export async function pay_for_damage(dmgstr, cant_mollify) {
const g = game;
let shkp = null;
const uinshp = !!(get_u().ushops && get_u().ushops.length);
let appear_here = null;
let cost_of_damage = 0;
let nearest_shk = (80 * 80) + (21 * 21); // ROWNO*ROWNO + COLNO*COLNO
let nearest_damage = nearest_shk;
let picks = 0;
const damagelist = g.level?.damagelist;
if (!damagelist || !damagelist.length) return;
// C ref: shk.c:5189 — find the nearest shopkeeper and damage site
for (const tmp_dam of damagelist) {
if (tmp_dam.when !== (g.moves || 0) || !tmp_dam.cost)
continue;
cost_of_damage += tmp_dam.cost;
const shops_affected = in_rooms(tmp_dam.place.x, tmp_dam.place.y, SHOPBASE);
for (let si = 0; si < shops_affected.length; si++) {
const shp = shops_affected[si] + ROOMOFFSET;
const tmp_shk = shop_keeper(shp);
if (!tmp_shk) continue;
if (tmp_shk === shkp) {
const damage_distance = distu(tmp_dam.place.x, tmp_dam.place.y);
if (damage_distance < nearest_damage) {
nearest_damage = damage_distance;
appear_here = tmp_dam;
}
continue;
}
if (!inhishop(tmp_shk)) continue;
const shk_distance = mdistu(tmp_shk);
if (shk_distance > nearest_shk) continue;
if (shk_distance === nearest_shk && picks) {
if (rn2(++picks)) continue;
} else {
picks = 1;
}
shkp = tmp_shk;
nearest_shk = shk_distance;
appear_here = tmp_dam;
nearest_damage = distu(tmp_dam.place.x, tmp_dam.place.y);
}
}
if (!cost_of_damage || !shkp) return;
const animal = (shkp.data?.msound ?? 0) <= 1; // MS_ANIMAL
let pursue = false;
const x = appear_here.place.x;
const y = appear_here.place.y;
// C ref: shk.c:5239
ESHK(shkp).customer = g.plname || '';
// C ref: shk.c:5242 — if already angry or following
if (ANGRY(shkp) || ESHK(shkp).following) {
hot_pursuit(shkp);
return;
}
// C ref: shk.c:5248 — if shk is not in their shop
if (!in_rooms(shkp.mx, shkp.my, SHOPBASE).length) {
if (!cansee(shkp.mx, shkp.my))
return;
pursue = true;
await getcad(shkp, dmgstr, x, y, uinshp, animal, pursue);
return;
}
if (uinshp) {
// C ref: shk.c:5257 — hero is in shop
if (um_dist(shkp.mx, shkp.my, 1)
&& !um_dist(shkp.mx, shkp.my, 3)) {
await pline(`${Shknam(shkp)} leaps towards you!`);
await mnexto(shkp, 0x0001); // RLOC_NOMSG
}
pursue = um_dist(shkp.mx, shkp.my, 1);
if (pursue) {
await getcad(shkp, dmgstr, x, y, uinshp, animal, pursue);
return;
}
} else {
// C ref: shk.c:5268 — hero NOT in shop, make shkp show up at door
if (MON_AT(x, y)) {
if (!animal) {
if (!Deaf() && !muteshk(shkp)) {
await You_hear("an angry voice:");
SetVoice(shkp, 0, 80, 0);
await verbalize("Out of my way, scum!");
}
await wait_synch();
} else {
await growl(shkp);
}
}
await mnearto(shkp, x, y, true, 0x0002); // RLOC_MSG
}
// C ref: shk.c:5296 — decide whether to demand payment or get angry
if ((um_dist(x, y, 1) && !uinshp) || cant_mollify
|| (money_cnt(game.invent) + (ESHK(shkp).credit || 0)) < cost_of_damage
|| !rn2(50)) {
await getcad(shkp, dmgstr, x, y, uinshp, animal, pursue);
return;
}
// C ref: shk.c:5303-5331 — offer to pay
if (Invis())
await Your(`invisibility does not fool ${shkname(shkp)}!`);
const qbuf = `${!animal ? cad(true) : ''}You did ${cost_of_damage} ${currency(cost_of_damage)} worth of damage!${!animal ? '"' : ''} Pay?`;
if ((await y_n(qbuf)) !== 'n') {
const was_seen = canseemon(shkp);
const was_outside = !inhishop(shkp);
const sx = shkp.mx, sy = shkp.my;
cost_of_damage = await check_credit(cost_of_damage, shkp);
if (cost_of_damage > 0) {
await money2mon(shkp, cost_of_damage);
setBotl('pay_for_damage');
}
await pline(`Mollified, ${shkname(shkp)} accepts your restitution.`);
await home_shk(shkp, false);
pacify_shk(shkp, false);
if (shkp.mx !== sx || shkp.my !== sy) {
if (was_outside && canspotmon(shkp))
await pline(`${Shknam(shkp)} returns to ${noit_mhis(shkp)} shop.`);
else {
const is_seen = canseemon(shkp);
if (is_seen || was_seen)
await pline(`${Shknam(shkp)} ${!was_seen ? 'appears' : is_seen ? 'shifts location' : 'disappears'}.`);
}
}
} else {
if (!animal) {
if (!Deaf() && !muteshk(shkp)) {
SetVoice(shkp, 0, 80, 0);
await verbalize("Oh, yes! You'll pay!");
} else {
await pline(`${Shknam(shkp)} lunges ${noit_mhis(shkp)} fist toward your ${mbodypart(game.youmonst, EYE)}!`);
}
} else {
await growl(shkp);
}
hot_pursuit(shkp);
adjalign(-sgn(get_u().ualign?.type || 0));
}
}
// ── shkcatch ──
// C ref: shk.c:4362
export async function shkcatch(obj, x, y) {
const shkp = shop_keeper(inside_shop(x, y));
if (!shkp || !inhishop(shkp)) return null;
if (!helpless(shkp)
&& ((get_u().ushops || '').charCodeAt(0) !== ESHK(shkp).shoproom || !inside_shop(get_u().ux, get_u().uy))
&& dist2(shkp.mx, shkp.my, x, y) < 3
&& (shkp.mx !== x || shkp.my !== y)) {
subfrombill(obj, shkp);
await mpickobj(shkp, obj);
return shkp;
}
return null;
}
// ── globby_bill_fixup ──
// C ref: shk.c:5977
export function globby_bill_fixup(obj_absorber, obj_absorbed) {
// Simplified glob billing
}
// ── use_unpaid_trapobj ──
// C ref: shk.c:6101
export async function use_unpaid_trapobj(otmp, x, y) {
if (otmp.unpaid)
await bill_dummy_object(otmp);
}
// ── special_stock ──
// C ref: shk.c:3103
async function special_stock(obj, shkp, quietly) {
if (ESHK(shkp).shoptype === CANDLESHOP && obj.otyp === CANDELABRUM_OF_INVOCATION) {
if (!quietly)
await pline(`${Shknam(shkp)} won't stock that.`);
return true;
}
return false;
}
// ── append_honorific ──
// C ref: shk.c:3602
function append_honorific(buf) {
const honored = ["good", "honored", "most gracious", "esteemed", "most renowned and sacred"];
const idx = rn2(honored.length - 1) + (get_u().uevent?.udemigod ? 1 : 0);
return buf + honored[Math.min(idx, honored.length - 1)] + (game.flags?.female ? " lady" : " sir");
}
// ── shk_embellish ──
// C ref: shk.c:5468
function shk_embellish(itm, cost) {
if (!rn2(3)) {
let choice = rn2(5);
if (choice === 0)
choice = (cost < 100 ? 1 : cost < 500 ? 2 : 3);
switch (choice) {
case 4: return ", superb craftsmanship!";
case 3: return ", finest quality.";
case 2: return ", an excellent choice.";
case 1: return ", a real bargain.";
default: break;
}
} else if (itm.oartifact) {
return ", one of a kind!";
}
return ".";
}
// ── shk_names_obj ──
// C ref: shk.c:3412
async function shk_names_obj(shkp, obj, fmt, amt, arg) {
observe_object(obj);
const obj_name = paydoname(obj);
await You(`${fmt.replace('%s', obj_name).replace('%ld', amt).replace('%s', plur(amt)).replace('%s', arg)}`);
}
// ── cad ──
// C ref: shk.c:5908
function cad(altusage) {
const genders = ["cad", "minx", "beast", "fiend"];
const idx = is_demon(null) ? 3 : poly_gender();
let res = genders[Math.min(idx, 3)];
if (altusage)
res = `"${res.charAt(0).toUpperCase() + res.slice(1)}! `;
return res;
}
// ── unpaid_cost (exported version) ──
// C ref: shk.c:3259
export function unpaid_cost_fn(unp_obj, cost_type) {
let amt = 0;
const ushops = get_u().ushops || '';
for (let i = 0; i < ushops.length; i++) {
const shkp = shop_keeper(ushops.charCodeAt(i));
if (shkp) {
const bp = onbill(unp_obj, shkp, true);
if (bp) {
amt = bp.price;
if (cost_type !== COST_SINGLEOBJ)
amt *= unp_obj.quan;
}
if (cost_type === COST_CONTENTS && Has_contents(unp_obj))
amt = contained_cost(unp_obj, shkp, amt, false, true);
if (bp || (!unp_obj.unpaid && amt))
break;
}
}
return amt;
}
// ── shk_move_goods (simplified) ──
// C ref: not a single function, but the combination of litter_scatter etc.
export function shk_move_goods() {
// No-op stub — full implementation requires repair_damage path
}
/*shk.c*/
|