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 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873
|
/* File: util.c */
/* Purpose: Angband utilities -BEN- */
#include "angband.h"
#ifndef HAS_MEMSET
/*
* For those systems that don't have "memset()"
*
* Set the value of each of 'n' bytes starting at 's' to 'c', return 's'
* If 'n' is negative, you will erase a whole lot of memory.
*/
char *memset(char *s, int c, huge n)
{
char *t;
for (t = s; len--; ) *t++ = c;
return (s);
}
#endif
#ifndef HAS_STRICMP
/*
* For those systems that don't have "stricmp()"
*
* Compare the two strings "a" and "b" ala "strcmp()" ignoring case.
*/
int stricmp(cptr a, cptr b)
{
cptr s1, s2;
char z1, z2;
/* Scan the strings */
for (s1 = a, s2 = b; TRUE; s1++, s2++)
{
z1 = FORCEUPPER(*s1);
z2 = FORCEUPPER(*s2);
if (z1 < z2) return ( -1);
if (z1 > z2) return (1);
if (!z1) return (0);
}
}
#endif
#ifdef SET_UID
# ifndef HAS_USLEEP
/*
* For those systems that don't have "usleep()" but need it.
*
* Fake "usleep()" function grabbed from the inl netrek server -cba
*/
int usleep(huge usecs)
{
struct timeval Timer;
int nfds = 0;
#ifdef FD_SET
fd_set *no_fds = NULL;
#else
int *no_fds = NULL;
#endif
/* Was: int readfds, writefds, exceptfds; */
/* Was: readfds = writefds = exceptfds = 0; */
/* Paranoia -- No excessive sleeping */
if (usecs > 4000000L) core("Illegal usleep() call");
/* Wait for it */
Timer.tv_sec = (usecs / 1000000L);
Timer.tv_usec = (usecs % 1000000L);
/* Wait for it */
if (select(nfds, no_fds, no_fds, no_fds, &Timer) < 0)
{
/* Hack -- ignore interrupts */
if (errno != EINTR) return -1;
}
/* Success */
return 0;
}
# endif
/*
* Hack -- External functions
*/
extern struct passwd *getpwuid();
extern struct passwd *getpwnam();
/*
* Find a default user name from the system.
*/
void user_name(char *buf, int id)
{
#ifdef SET_UID
struct passwd *pw;
/* Look up the user name */
if ((pw = getpwuid(id)))
{
(void)strcpy(buf, pw->pw_name);
buf[16] = '\0';
#ifdef CAPITALIZE_USER_NAME
/* Hack -- capitalize the user name */
if (islower(buf[0])) buf[0] = toupper(buf[0]);
#endif /* CAPITALIZE_USER_NAME */
return;
}
#endif /* SET_UID */
/* Oops. Hack -- default to "PLAYER" */
strcpy(buf, "PLAYER");
}
#endif /* SET_UID */
/*
* The concept of the "file" routines below (and elsewhere) is that all
* file handling should be done using as few routines as possible, since
* every machine is slightly different, but these routines always have the
* same semantics.
*
* In fact, perhaps we should use the "path_parse()" routine below to convert
* from "canonical" filenames (optional leading tilde's, internal wildcards,
* slash as the path seperator, etc) to "system" filenames (no special symbols,
* system-specific path seperator, etc). This would allow the program itself
* to assume that all filenames are "Unix" filenames, and explicitly "extract"
* such filenames if needed (by "path_parse()", or perhaps "path_canon()").
*
* Note that "path_temp" should probably return a "canonical" filename.
*
* Note that "my_fopen()" and "my_open()" and "my_make()" and "my_kill()"
* and "my_move()" and "my_copy()" should all take "canonical" filenames.
*
* Note that "canonical" filenames use a leading "slash" to indicate an absolute
* path, and a leading "tilde" to indicate a special directory, and default to a
* relative path, but MSDOS uses a leading "drivename plus colon" to indicate the
* use of a "special drive", and then the rest of the path is parsed "normally",
* and MACINTOSH uses a leading colon to indicate a relative path, and an embedded
* colon to indicate a "drive plus absolute path", and finally defaults to a file
* in the current working directory, which may or may not be defined.
*
* We should probably parse a leading "~~/" as referring to "ANGBAND_DIR". (?)
*/
#ifdef ACORN
/*
* Most of the "file" routines for "ACORN" should be in "main-acn.c"
*/
#else /* ACORN */
#ifdef SET_UID
/*
* Extract a "parsed" path from an initial filename
* Normally, we simply copy the filename into the buffer
* But leading tilde symbols must be handled in a special way
* Replace "~user/" by the home directory of the user named "user"
* Replace "~/" by the home directory of the current user
*/
errr path_parse(char *buf, int max, cptr file)
{
cptr u, s;
struct passwd *pw;
char user[128];
/* Assume no result */
buf[0] = '\0';
/* No file? */
if (!file) return ( -1);
/* File needs no parsing */
if (file[0] != '~')
{
strcpy(buf, file);
return (0);
}
/* Point at the user */
u = file + 1;
/* Look for non-user portion of the file */
s = strstr(u, PATH_SEP);
/* Hack -- no long user names */
if (s && (s >= u + sizeof(user))) return (1);
#ifdef GETLOGIN_BROKEN
/* Ask the environment for the home directory */
u = getenv("HOME");
if (!u) return (1);
(void)strcpy(buf, u);
#else
/* Extract a user name */
if (s)
{
int i;
for (i = 0; u < s; ++i) user[i] = *u++;
user[i] = '\0';
u = user;
}
/* Look up the "current" user */
if (u[0] == '\0') u = getlogin();
/* Look up a user (or "current" user) */
if (u) pw = getpwnam(u);
else pw = getpwuid(getuid());
/* Nothing found? */
if (!pw) return (1);
/* Make use of the info */
(void)strcpy(buf, pw->pw_dir);
#endif
/* Append the rest of the filename, if any */
if (s) (void)strcat(buf, s);
/* Success */
return (0);
}
#else /* SET_UID */
/*
* Extract a "parsed" path from an initial filename
*
* This requires no special processing on simple machines,
* except for verifying the size of the filename.
*/
errr path_parse(char *buf, int max, cptr file)
{
/* Accept the filename */
strnfmt(buf, max, "%s", file);
/* Success */
return (0);
}
#endif /* SET_UID */
/*
* Hack -- acquire a "temporary" file name if possible
*
* This filename is always in "system-specific" form.
*/
errr path_temp(char *buf, int max)
{
#ifdef WINDOWS
static u32b tmp_counter;
static char valid_characters[] =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char rand_ext[4];
rand_ext[0] = valid_characters[rand_int(sizeof (valid_characters))];
rand_ext[1] = valid_characters[rand_int(sizeof (valid_characters))];
rand_ext[2] = valid_characters[rand_int(sizeof (valid_characters))];
rand_ext[3] = '\0';
strnfmt(buf, max, "%s/t_%ud.%s", ANGBAND_DIR_XTRA, tmp_counter, rand_ext);
tmp_counter++;
#else
cptr s;
/* Temp file */
s = tmpnam(NULL);
/* Oops */
if (!s) return ( -1);
/* Format to length */
strnfmt(buf, max, "%s", s);
#endif
/* Success */
return (0);
}
/*
* Create a new path by appending a file (or directory) to a path
*
* This requires no special processing on simple machines, except
* for verifying the size of the filename, but note the ability to
* bypass the given "path" with certain special file-names.
*
* Note that the "file" may actually be a "sub-path", including
* a path and a file.
*
* Note that this function yields a path which must be "parsed"
* using the "parse" function above.
*/
errr path_build(char *buf, int max, cptr path, cptr file)
{
/* Special file */
if (file[0] == '~')
{
/* Use the file itself */
strnfmt(buf, max, "%s", file);
}
/* Absolute file, on "normal" systems */
else if (prefix(file, PATH_SEP) && !streq(PATH_SEP, ""))
{
/* Use the file itself */
strnfmt(buf, max, "%s", file);
}
/* No path given */
else if (!path[0])
{
/* Use the file itself */
strnfmt(buf, max, "%s", file);
}
/* Path and File */
else
{
/* Build the new path */
strnfmt(buf, max, "%s%s%s", path, PATH_SEP, file);
}
/* Success */
return (0);
}
/*
* Hack -- replacement for "fopen()"
*/
FILE *my_fopen(cptr file, cptr mode)
{
#ifndef MACH_O_CARBON
char buf[1024];
/* Hack -- Try to parse the path */
if (path_parse(buf, 1024, file)) return (NULL);
/* Attempt to fopen the file anyway */
return (fopen(buf, mode));
#else /* MACH_O_CARBON */
char buf[1024];
FILE *s;
/* Hack -- Try to parse the path */
if (path_parse(buf, 1024, file)) return (NULL);
/* Attempt to fopen the file anyway */
s = fopen(buf, mode);
/* Set creator and type if the file is successfully opened */
if (s) fsetfileinfo(buf, _fcreator, _ftype);
/* Done */
return (s);
#endif /* MACH_O_CARBON */
}
/*
* Hack -- replacement for "fclose()"
*/
errr my_fclose(FILE *fff)
{
/* Require a file */
if (!fff) return ( -1);
/* Close, check for error */
if (fclose(fff) == EOF) return (1);
/* Success */
return (0);
}
#endif /* ACORN */
/*
* Like "fgets()" but for strings
*
* Process tabs, strip internal non-printables
*/
errr my_str_fgets(cptr full_text, char *buf, huge n)
{
static huge last_read = 0;
static huge full_size;
huge i = 0;
char *s;
char tmp[1024];
/* Initialize */
if (!buf)
{
last_read = 0;
full_size = strlen(full_text);
return 0;
}
/* The end! */
if (last_read >= full_size)
{
return 1;
}
while (last_read < full_size)
{
char c = full_text[last_read++];
tmp[i++] = c;
/* Dont overflow */
if (i >= 1024)
{
/* Nothing */
buf[0] = '\0';
/* Failure */
return (1);
}
if ((c == '\n') || (c == '\r'))
{
break;
}
}
tmp[i++] = '\n';
tmp[i++] = '\0';
/* We need it again */
i = 0;
/* Convert weirdness */
for (s = tmp; *s; s++)
{
/* Handle newline */
if (*s == '\n')
{
/* Terminate */
buf[i] = '\0';
/* Success */
return (0);
}
/* Handle tabs */
else if (*s == '\t')
{
/* Hack -- require room */
if (i + 8 >= n) break;
/* Append a space */
buf[i++] = ' ';
/* Append some more spaces */
while (!(i % 8)) buf[i++] = ' ';
}
/* Handle printables */
else if (isprint(*s))
{
/* Copy */
buf[i++] = *s;
/* Check length */
if (i >= n) break;
}
}
/* Nothing */
buf[0] = '\0';
/* Failure */
return (1);
}
/*
* Hack -- replacement for "fgets()"
*
* Read a string, without a newline, to a file
*
* Process tabs, strip internal non-printables
*/
errr my_fgets(FILE *fff, char *buf, huge n)
{
huge i = 0;
while (TRUE)
{
int c = fgetc(fff);
if (c == EOF)
{
/* Terminate */
buf[i] = '\0';
/* Success (0) if some characters were read */
return (i == 0);
}
/* Handle newline -- DOS (\015\012), Mac (\015), UNIX (\012) */
else if (c == '\r')
{
c = fgetc(fff);
if (c != '\n') ungetc(c, fff);
/* Terminate */
buf[i] = '\0';
/* Success */
return (0);
}
else if (c == '\n')
{
c = fgetc(fff);
if (c != '\r') ungetc(c, fff);
/* Terminate */
buf[i] = '\0';
/* Success */
return (0);
}
/* Handle tabs */
else if (c == '\t')
{
/* Hack -- require room */
if (i + 8 >= n) break;
/* Append 1-8 spaces */
do { buf[i++] = ' '; } while (i % 8);
}
/* Handle printables */
else if (isprint(c))
{
/* Copy */
buf[i++] = c;
/* Check length */
if (i >= n) break;
}
}
/* Nothing */
buf[0] = '\0';
/* Failure */
return (1);
}
/*
* Hack -- replacement for "fputs()"
*
* Dump a string, plus a newline, to a file
*
* XXX XXX XXX Process internal weirdness?
*/
errr my_fputs(FILE *fff, cptr buf, huge n)
{
/* XXX XXX */
n = n ? n : 0;
/* Dump, ignore errors */
(void)fprintf(fff, "%s\n", buf);
/* Success */
return (0);
}
#ifdef ACORN
/*
* Most of the "file" routines for "ACORN" should be in "main-acn.c"
*
* Many of them can be rewritten now that only "fd_open()" and "fd_make()"
* and "my_fopen()" should ever create files.
*/
#else /* ACORN */
/*
* Code Warrior is a little weird about some functions
*/
#ifdef BEN_HACK
extern int open(const char *, int, ...);
extern int close(int);
extern int read(int, void *, unsigned int);
extern int write(int, const void *, unsigned int);
extern long lseek(int, long, int);
#endif /* BEN_HACK */
/*
* The Macintosh is a little bit brain-dead sometimes
*/
#ifdef MACINTOSH
# define open(N, F, M) \
((M), open((char*)(N), F))
# define write(F, B, S) \
write(F, (char*)(B), S)
#endif /* MACINTOSH */
/*
* Several systems have no "O_BINARY" flag
*/
#ifndef O_BINARY
# define O_BINARY 0
#endif /* O_BINARY */
/*
* Hack -- attempt to delete a file
*/
errr fd_kill(cptr file)
{
char buf[1024];
/* Hack -- Try to parse the path */
if (path_parse(buf, 1024, file)) return ( -1);
/* Remove */
(void)remove(buf);
/* XXX XXX XXX */
return (0);
}
/*
* Hack -- attempt to move a file
*/
errr fd_move(cptr file, cptr what)
{
char buf[1024];
char aux[1024];
/* Hack -- Try to parse the path */
if (path_parse(buf, 1024, file)) return ( -1);
/* Hack -- Try to parse the path */
if (path_parse(aux, 1024, what)) return ( -1);
/* Rename */
(void)rename(buf, aux);
/* XXX XXX XXX */
return (0);
}
/*
* Hack -- attempt to copy a file
*/
errr fd_copy(cptr file, cptr what)
{
char buf[1024];
char aux[1024];
/* Hack -- Try to parse the path */
if (path_parse(buf, 1024, file)) return ( -1);
/* Hack -- Try to parse the path */
if (path_parse(aux, 1024, what)) return ( -1);
/* Copy XXX XXX XXX */
/* (void)rename(buf, aux); */
/* XXX XXX XXX */
return (1);
}
/*
* Hack -- attempt to open a file descriptor (create file)
*
* This function should fail if the file already exists
*
* Note that we assume that the file should be "binary"
*
* XXX XXX XXX The horrible "BEN_HACK" code is for compiling under
* the CodeWarrior compiler, in which case, for some reason, none
* of the "O_*" flags are defined, and we must fake the definition
* of "O_RDONLY", "O_WRONLY", and "O_RDWR" in "A-win-h", and then
* we must simulate the effect of the proper "open()" call below.
*/
int fd_make(cptr file, int mode)
{
char buf[1024];
/* Hack -- Try to parse the path */
if (path_parse(buf, 1024, file)) return ( -1);
#ifdef BEN_HACK
/* Check for existance */
/* if (fd_close(fd_open(file, O_RDONLY | O_BINARY))) return (1); */
/* Mega-Hack -- Create the file */
(void)my_fclose(my_fopen(file, "wb"));
/* Re-open the file for writing */
return (open(buf, O_WRONLY | O_BINARY, mode));
#else /* BEN_HACK */
#ifdef MACH_O_CARBON
{
int fdes;
/* Create the file, fail if exists, write-only, binary */
fdes = open(buf, O_CREAT | O_EXCL | O_WRONLY | O_BINARY, mode);
/* Set creator and type if the file is successfully opened */
if (fdes >= 0) fsetfileinfo(buf, _fcreator, _ftype);
/* Return the descriptor */
return (fdes);
}
#else /* MACH_O_CARBON */
/* Create the file, fail if exists, write-only, binary */
return (open(buf, O_CREAT | O_EXCL | O_WRONLY | O_BINARY, mode));
#endif /* MACH_O_CARBON */
#endif /* BEN_HACK */
}
/*
* Hack -- attempt to open a file descriptor (existing file)
*
* Note that we assume that the file should be "binary"
*/
int fd_open(cptr file, int flags)
{
char buf[1024];
/* Hack -- Try to parse the path */
if (path_parse(buf, 1024, file)) return ( -1);
#ifdef MACH_O_CARBON
{
int fdes;
/* Attempt to open the file */
fdes = open(buf, flags | O_BINARY, 0);
/* Set creator and type if the file is successfully opened */
if (fdes >= 0) fsetfileinfo(buf, _fcreator, _ftype);
/* Return the descriptor */
return (fdes);
}
#else /* MACH_O_CARBON */
/* Attempt to open the file */
return (open(buf, flags | O_BINARY, 0));
#endif /* MACH_O_CARBON */
}
/*
* Hack -- attempt to lock a file descriptor
*
* Legal lock types -- F_UNLCK, F_RDLCK, F_WRLCK
*/
errr fd_lock(int fd, int what)
{
/* XXX XXX */
what = what ? what : 0;
/* Verify the fd */
if (fd < 0) return ( -1);
#ifdef SET_UID
# ifdef USG
# if defined(F_ULOCK) && defined(F_LOCK)
/* Un-Lock */
if (what == F_UNLCK)
{
/* Unlock it, Ignore errors */
lockf(fd, F_ULOCK, 0);
}
/* Lock */
else
{
/* Lock the score file */
if (lockf(fd, F_LOCK, 0) != 0) return (1);
}
# endif
# else
# if defined(LOCK_UN) && defined(LOCK_EX)
/* Un-Lock */
if (what == F_UNLCK)
{
/* Unlock it, Ignore errors */
(void)flock(fd, LOCK_UN);
}
/* Lock */
else
{
/* Lock the score file */
if (flock(fd, LOCK_EX) != 0) return (1);
}
# endif
# endif
#endif
/* Success */
return (0);
}
/*
* Hack -- attempt to seek on a file descriptor
*/
errr fd_seek(int fd, huge n)
{
s32b p;
/* Verify fd */
if (fd < 0) return ( -1);
/* Seek to the given position */
p = lseek(fd, n, SEEK_SET);
/* Failure */
if (p < 0) return (1);
/* Failure */
if ((huge)p != n) return (1);
/* Success */
return (0);
}
/*
* Hack -- attempt to truncate a file descriptor
*/
errr fd_chop(int fd, huge n)
{
/* XXX XXX */
n = n ? n : 0;
/* Verify the fd */
if (fd < 0) return ( -1);
#if defined(SUNOS) || defined(ULTRIX) || defined(NeXT)
/* Truncate */
ftruncate(fd, n);
#endif
/* Success */
return (0);
}
/*
* Hack -- attempt to read data from a file descriptor
*/
errr fd_read(int fd, char *buf, huge n)
{
/* Verify the fd */
if (fd < 0) return ( -1);
#ifndef SET_UID
/* Read pieces */
while (n >= 16384)
{
/* Read a piece */
if (read(fd, buf, 16384) != 16384) return (1);
/* Shorten the task */
buf += 16384;
/* Shorten the task */
n -= 16384;
}
#endif
/* Read the final piece */
if ((huge)read(fd, buf, n) != n) return (1);
/* Success */
return (0);
}
/*
* Hack -- Attempt to write data to a file descriptor
*/
errr fd_write(int fd, cptr buf, huge n)
{
/* Verify the fd */
if (fd < 0) return ( -1);
#ifndef SET_UID
/* Write pieces */
while (n >= 16384)
{
/* Write a piece */
if (write(fd, buf, 16384) != 16384) return (1);
/* Shorten the task */
buf += 16384;
/* Shorten the task */
n -= 16384;
}
#endif
/* Write the final piece */
if ((huge)write(fd, buf, n) != n) return (1);
/* Success */
return (0);
}
/*
* Hack -- attempt to close a file descriptor
*/
errr fd_close(int fd)
{
/* Verify the fd */
if (fd < 0) return ( -1);
/* Close */
(void)close(fd);
/* XXX XXX XXX */
return (0);
}
#endif /* ACORN */
/*
* XXX XXX XXX Important note about "colors" XXX XXX XXX
*
* The "TERM_*" color definitions list the "composition" of each
* "Angband color" in terms of "quarters" of each of the three color
* components (Red, Green, Blue), for example, TERM_UMBER is defined
* as 2/4 Red, 1/4 Green, 0/4 Blue.
*
* The following info is from "Torbjorn Lindgren" (see "main-xaw.c").
*
* These values are NOT gamma-corrected. On most machines (with the
* Macintosh being an important exception), you must "gamma-correct"
* the given values, that is, "correct for the intrinsic non-linearity
* of the phosphor", by converting the given intensity levels based
* on the "gamma" of the target screen, which is usually 1.7 (or 1.5).
*
* The actual formula for conversion is unknown to me at this time,
* but you can use the table below for the most common gamma values.
*
* So, on most machines, simply convert the values based on the "gamma"
* of the target screen, which is usually in the range 1.5 to 1.7, and
* usually is closest to 1.7. The converted value for each of the five
* different "quarter" values is given below:
*
* Given Gamma 1.0 Gamma 1.5 Gamma 1.7 Hex 1.7
* ----- ---- ---- ---- ---
* 0/4 0.00 0.00 0.00 #00
* 1/4 0.25 0.27 0.28 #47
* 2/4 0.50 0.55 0.56 #8f
* 3/4 0.75 0.82 0.84 #d7
* 4/4 1.00 1.00 1.00 #ff
*
* Note that some machines (i.e. most IBM machines) are limited to a
* hard-coded set of colors, and so the information above is useless.
*
* Also, some machines are limited to a pre-determined set of colors,
* for example, the IBM can only display 16 colors, and only 14 of
* those colors resemble colors used by Angband, and then only when
* you ignore the fact that "Slate" and "cyan" are not really matches,
* so on the IBM, we use "orange" for both "Umber", and "Light Umber"
* in addition to the obvious "Orange", since by combining all of the
* "indeterminate" colors into a single color, the rest of the colors
* are left with "meaningful" values.
*/
/*
* Move the cursor
*/
void move_cursor(int row, int col)
{
Term_gotoxy(col, row);
}
/*
* Convert a decimal to a single digit octal number
*/
static char octify(uint i)
{
return (hexsym[i % 8]);
}
/*
* Convert a decimal to a single digit hex number
*/
static char hexify(uint i)
{
return (hexsym[i % 16]);
}
/*
* Convert a octal-digit into a decimal
*/
static int deoct(char c)
{
if (isdigit(c)) return (D2I(c));
return (0);
}
/*
* Convert a hexidecimal-digit into a decimal
*/
static int dehex(char c)
{
if (isdigit(c)) return (D2I(c));
if (islower(c)) return (A2I(c) + 10);
if (isupper(c)) return (A2I(tolower(c)) + 10);
return (0);
}
static int my_stricmp(cptr a, cptr b)
{
cptr s1, s2;
char z1, z2;
/* Scan the strings */
for (s1 = a, s2 = b; TRUE; s1++, s2++)
{
z1 = FORCEUPPER(*s1);
z2 = FORCEUPPER(*s2);
if (z1 < z2) return ( -1);
if (z1 > z2) return (1);
if (!z1) return (0);
}
}
static int my_strnicmp(cptr a, cptr b, int n)
{
cptr s1, s2;
char z1, z2;
/* Scan the strings */
for (s1 = a, s2 = b; n > 0; s1++, s2++, n--)
{
z1 = FORCEUPPER(*s1);
z2 = FORCEUPPER(*s2);
if (z1 < z2) return ( -1);
if (z1 > z2) return (1);
if (!z1) return (0);
}
return 0;
}
static void trigger_text_to_ascii(char **bufptr, cptr *strptr)
{
char *s = *bufptr;
cptr str = *strptr;
bool mod_status[MAX_MACRO_MOD];
int i, len = 0;
int shiftstatus = 0;
cptr key_code;
if (macro_template == NULL)
return;
for (i = 0; macro_modifier_chr[i]; i++)
mod_status[i] = FALSE;
str++;
/* Examine modifier keys */
while (1)
{
for (i = 0; macro_modifier_chr[i]; i++)
{
len = strlen(macro_modifier_name[i]);
if (!my_strnicmp(str, macro_modifier_name[i], len))
break;
}
if (!macro_modifier_chr[i]) break;
str += len;
mod_status[i] = TRUE;
if ('S' == macro_modifier_chr[i])
shiftstatus = 1;
}
for (i = 0; i < max_macrotrigger; i++)
{
len = strlen(macro_trigger_name[i]);
if (!my_strnicmp(str, macro_trigger_name[i], len) && ']' == str[len])
{
/* a trigger name found */
break;
}
}
/* Invalid trigger name? */
if (i == max_macrotrigger)
{
str = strchr(str, ']');
if (str)
{
*s++ = (char)31;
*s++ = (char)13;
*bufptr = s;
*strptr = str; /* where **strptr == ']' */
}
return;
}
key_code = macro_trigger_keycode[shiftstatus][i];
str += len;
*s++ = (char)31;
for (i = 0; macro_template[i]; i++)
{
char ch = macro_template[i];
int j;
switch (ch)
{
case '&':
for (j = 0; macro_modifier_chr[j]; j++)
{
if (mod_status[j])
*s++ = macro_modifier_chr[j];
}
break;
case '#':
strcpy(s, key_code);
s += strlen(key_code);
break;
default:
*s++ = ch;
break;
}
}
*s++ = (char)13;
*bufptr = s;
*strptr = str; /* where **strptr == ']' */
return;
}
/*
* Hack -- convert a printable string into real ascii
*
* I have no clue if this function correctly handles, for example,
* parsing "\xFF" into a (signed) char. Whoever thought of making
* the "sign" of a "char" undefined is a complete moron. Oh well.
*/
void text_to_ascii(char *buf, cptr str)
{
char *s = buf;
/* Analyze the "ascii" string */
while (*str)
{
/* Backslash codes */
if (*str == '\\')
{
/* Skip the backslash */
str++;
/* Macro Trigger */
if (*str == '[')
{
trigger_text_to_ascii(&s, &str);
}
/* Hex-mode XXX */
else if (*str == 'x')
{
*s = 16 * dehex(*++str);
*s++ += dehex(*++str);
}
/* Hack -- simple way to specify "backslash" */
else if (*str == '\\')
{
*s++ = '\\';
}
/* Hack -- simple way to specify "caret" */
else if (*str == '^')
{
*s++ = '^';
}
/* Hack -- simple way to specify "space" */
else if (*str == 's')
{
*s++ = ' ';
}
/* Hack -- simple way to specify Escape */
else if (*str == 'e')
{
*s++ = ESCAPE;
}
/* Backspace */
else if (*str == 'b')
{
*s++ = '\b';
}
/* Newline */
else if (*str == 'n')
{
*s++ = '\n';
}
/* Return */
else if (*str == 'r')
{
*s++ = '\r';
}
/* Tab */
else if (*str == 't')
{
*s++ = '\t';
}
/* Octal-mode */
else if (*str == '0')
{
*s = 8 * deoct(*++str);
*s++ += deoct(*++str);
}
/* Octal-mode */
else if (*str == '1')
{
*s = 64 + 8 * deoct(*++str);
*s++ += deoct(*++str);
}
/* Octal-mode */
else if (*str == '2')
{
*s = 64 * 2 + 8 * deoct(*++str);
*s++ += deoct(*++str);
}
/* Octal-mode */
else if (*str == '3')
{
*s = 64 * 3 + 8 * deoct(*++str);
*s++ += deoct(*++str);
}
/* Skip the final char */
str++;
}
/* Normal Control codes */
else if (*str == '^')
{
str++;
*s++ = (*str++ & 037);
}
/* Normal chars */
else
{
*s++ = *str++;
}
}
/* Terminate */
*s = '\0';
}
bool trigger_ascii_to_text(char **bufptr, cptr *strptr)
{
char *s = *bufptr;
cptr str = *strptr;
char key_code[100];
int i;
cptr tmp;
if (macro_template == NULL)
return FALSE;
*s++ = '\\';
*s++ = '[';
for (i = 0; macro_template[i]; i++)
{
int j;
char ch = macro_template[i];
switch (ch)
{
case '&':
while ((tmp = strchr(macro_modifier_chr, *str)))
{
j = (int)(tmp - macro_modifier_chr);
tmp = macro_modifier_name[j];
while (*tmp) *s++ = *tmp++;
str++;
}
break;
case '#':
for (j = 0; *str && *str != (char)13; j++)
key_code[j] = *str++;
key_code[j] = '\0';
break;
default:
if (ch != *str) return FALSE;
str++;
}
}
if (*str++ != (char)13) return FALSE;
for (i = 0; i < max_macrotrigger; i++)
{
if (!my_stricmp(key_code, macro_trigger_keycode[0][i])
|| !my_stricmp(key_code, macro_trigger_keycode[1][i]))
break;
}
if (i == max_macrotrigger)
return FALSE;
tmp = macro_trigger_name[i];
while (*tmp) *s++ = *tmp++;
*s++ = ']';
*bufptr = s;
*strptr = str;
return TRUE;
}
/*
* Hack -- convert a string into a printable form
*/
void ascii_to_text(char *buf, cptr str)
{
char *s = buf;
/* Analyze the "ascii" string */
while (*str)
{
byte i = (byte)(*str++);
/* Macro Trigger */
if (i == 31)
{
if (!trigger_ascii_to_text(&s, &str))
{
*s++ = '^';
*s++ = '_';
}
}
else if (i == ESCAPE)
{
*s++ = '\\';
*s++ = 'e';
}
else if (i == ' ')
{
*s++ = '\\';
*s++ = 's';
}
else if (i == '\b')
{
*s++ = '\\';
*s++ = 'b';
}
else if (i == '\t')
{
*s++ = '\\';
*s++ = 't';
}
else if (i == '\n')
{
*s++ = '\\';
*s++ = 'n';
}
else if (i == '\r')
{
*s++ = '\\';
*s++ = 'r';
}
else if (i == '^')
{
*s++ = '\\';
*s++ = '^';
}
else if (i == '\\')
{
*s++ = '\\';
*s++ = '\\';
}
else if (i < 32)
{
*s++ = '^';
*s++ = i + 64;
}
else if (i < 127)
{
*s++ = i;
}
else if (i < 64)
{
*s++ = '\\';
*s++ = '0';
*s++ = octify(i / 8);
*s++ = octify(i % 8);
}
else
{
*s++ = '\\';
*s++ = 'x';
*s++ = hexify(i / 16);
*s++ = hexify(i % 16);
}
}
/* Terminate */
*s = '\0';
}
/*
* The "macro" package
*
* Functions are provided to manipulate a collection of macros, each
* of which has a trigger pattern string and a resulting action string
* and a small set of flags.
*/
/*
* Determine if any macros have ever started with a given character.
*/
static bool macro__use[256];
/*
* Find the macro (if any) which exactly matches the given pattern
*/
sint macro_find_exact(cptr pat)
{
int i;
/* Nothing possible */
if (!macro__use[(byte)(pat[0])])
{
return ( -1);
}
/* Scan the macros */
for (i = 0; i < macro__num; ++i)
{
/* Skip macros which do not match the pattern */
if (!streq(macro__pat[i], pat)) continue;
/* Found one */
return (i);
}
/* No matches */
return ( -1);
}
/*
* Find the first macro (if any) which contains the given pattern
*/
static sint macro_find_check(cptr pat)
{
int i;
/* Nothing possible */
if (!macro__use[(byte)(pat[0])])
{
return ( -1);
}
/* Scan the macros */
for (i = 0; i < macro__num; ++i)
{
/* Skip macros which do not contain the pattern */
if (!prefix(macro__pat[i], pat)) continue;
/* Found one */
return (i);
}
/* Nothing */
return ( -1);
}
/*
* Find the first macro (if any) which contains the given pattern and more
*/
static sint macro_find_maybe(cptr pat)
{
int i;
/* Nothing possible */
if (!macro__use[(byte)(pat[0])])
{
return ( -1);
}
/* Scan the macros */
for (i = 0; i < macro__num; ++i)
{
/* Skip macros which do not contain the pattern */
if (!prefix(macro__pat[i], pat)) continue;
/* Skip macros which exactly match the pattern XXX XXX */
if (streq(macro__pat[i], pat)) continue;
/* Found one */
return (i);
}
/* Nothing */
return ( -1);
}
/*
* Find the longest macro (if any) which starts with the given pattern
*/
static sint macro_find_ready(cptr pat)
{
int i, t, n = -1, s = -1;
/* Nothing possible */
if (!macro__use[(byte)(pat[0])])
{
return ( -1);
}
/* Scan the macros */
for (i = 0; i < macro__num; ++i)
{
/* Skip macros which are not contained by the pattern */
if (!prefix(pat, macro__pat[i])) continue;
/* Obtain the length of this macro */
t = strlen(macro__pat[i]);
/* Only track the "longest" pattern */
if ((n >= 0) && (s > t)) continue;
/* Track the entry */
n = i;
s = t;
}
/* Result */
return (n);
}
/*
* Add a macro definition (or redefinition).
*
* We should use "act == NULL" to "remove" a macro, but this might make it
* impossible to save the "removal" of a macro definition. XXX XXX XXX
*
* We should consider refusing to allow macros which contain existing macros,
* or which are contained in existing macros, because this would simplify the
* macro analysis code. XXX XXX XXX
*
* We should consider removing the "command macro" crap, and replacing it
* with some kind of "powerful keymap" ability, but this might make it hard
* to change the "roguelike" option from inside the game. XXX XXX XXX
*/
errr macro_add(cptr pat, cptr act)
{
int n;
/* Paranoia -- require data */
if (!pat || !act) return ( -1);
/* Look for any existing macro */
n = macro_find_exact(pat);
/* Replace existing macro */
if (n >= 0)
{
/* Free the old macro action */
string_free(macro__act[n]);
}
/* Create a new macro */
else
{
/* Acquire a new index */
n = macro__num++;
/* Save the pattern */
macro__pat[n] = string_make(pat);
}
/* Save the action */
macro__act[n] = string_make(act);
/* Efficiency */
macro__use[(byte)(pat[0])] = TRUE;
/* Success */
return (0);
}
/*
* Initialize the "macro" package
*/
errr macro_init(void)
{
/* Macro patterns */
C_MAKE(macro__pat, MACRO_MAX, cptr);
/* Macro actions */
C_MAKE(macro__act, MACRO_MAX, cptr);
/* Success */
return (0);
}
/*
* Local "need flush" variable
*/
static bool flush_later = FALSE;
/*
* Local variable -- we are inside a "macro action"
*
* Do not match any macros until "ascii 30" is found.
*/
static bool parse_macro = FALSE;
/*
* Local variable -- we are inside a "macro trigger"
*
* Strip all keypresses until a low ascii value is found.
*/
static bool parse_under = FALSE;
/*
* Flush all input chars. Actually, remember the flush,
* and do a "special flush" before the next "inkey()".
*
* This is not only more efficient, but also necessary to make sure
* that various "inkey()" codes are not "lost" along the way.
*/
void flush(void)
{
/* Do it later */
flush_later = TRUE;
}
/*
* Flush the screen, make a noise
*/
void bell(void)
{
/* Mega-Hack -- Flush the output */
Term_fresh();
/* Make a bell noise (if allowed) */
if (ring_bell) Term_xtra(TERM_XTRA_NOISE, 0);
/* Flush the input (later!) */
flush();
}
/*
* Hack -- Make a (relevant?) sound
*/
void sound(int val)
{
/* No sound */
if (!use_sound) return;
/* Make a sound (if allowed) */
Term_xtra(TERM_XTRA_SOUND, val);
}
/*
* Helper function called only from "inkey()"
*
* This function does almost all of the "macro" processing.
*
* We use the "Term_key_push()" function to handle "failed" macros, as well
* as "extra" keys read in while choosing the proper macro, and also to hold
* the action for the macro, plus a special "ascii 30" character indicating
* that any macro action in progress is complete. Embedded macros are thus
* illegal, unless a macro action includes an explicit "ascii 30" character,
* which would probably be a massive hack, and might break things.
*
* Only 500 (0+1+2+...+29+30) milliseconds may elapse between each key in
* the macro trigger sequence. If a key sequence forms the "prefix" of a
* macro trigger, 500 milliseconds must pass before the key sequence is
* known not to be that macro trigger. XXX XXX XXX
*/
static char inkey_aux(void)
{
int k = 0, n, p = 0, w = 0;
char ch;
cptr pat, act;
char buf[1024];
/* Wait for a keypress */
(void)(Term_inkey(&ch, TRUE, TRUE));
/* End "macro action" */
if (ch == 30) parse_macro = FALSE;
/* Inside "macro action" */
if (ch == 30) return (ch);
/* Inside "macro action" */
if (parse_macro) return (ch);
/* Inside "macro trigger" */
if (parse_under) return (ch);
/* Save the first key, advance */
buf[p++] = ch;
buf[p] = '\0';
/* Check for possible macro */
k = macro_find_check(buf);
/* No macro pending */
if (k < 0) return (ch);
/* Wait for a macro, or a timeout */
while (TRUE)
{
/* Check for pending macro */
k = macro_find_maybe(buf);
/* No macro pending */
if (k < 0) break;
/* Check for (and remove) a pending key */
if (0 == Term_inkey(&ch, FALSE, TRUE))
{
/* Append the key */
buf[p++] = ch;
buf[p] = '\0';
/* Restart wait */
w = 0;
}
/* No key ready */
else
{
/* Increase "wait" */
w += 10;
/* Excessive delay */
if (w >= 100) break;
/* Delay */
Term_xtra(TERM_XTRA_DELAY, w);
}
}
/* Check for available macro */
k = macro_find_ready(buf);
/* No macro available */
if (k < 0)
{
/* Push all the keys back on the queue */
while (p > 0)
{
/* Push the key, notice over-flow */
if (Term_key_push(buf[--p])) return (0);
}
/* Wait for (and remove) a pending key */
(void)Term_inkey(&ch, TRUE, TRUE);
/* Return the key */
return (ch);
}
/* Get the pattern */
pat = macro__pat[k];
/* Get the length of the pattern */
n = strlen(pat);
/* Push the "extra" keys back on the queue */
while (p > n)
{
/* Push the key, notice over-flow */
if (Term_key_push(buf[--p])) return (0);
}
/* Begin "macro action" */
parse_macro = TRUE;
/* Push the "end of macro action" key */
if (Term_key_push(30)) return (0);
/* Access the macro action */
act = macro__act[k];
/* Get the length of the action */
n = strlen(act);
/* Push the macro "action" onto the key queue */
while (n > 0)
{
/* Push the key, notice over-flow */
if (Term_key_push(act[--n])) return (0);
}
/* Hack -- Force "inkey()" to call us again */
return (0);
}
/*
* Mega-Hack -- special "inkey_next" pointer. XXX XXX XXX
*
* This special pointer allows a sequence of keys to be "inserted" into
* the stream of keys returned by "inkey()". This key sequence will not
* trigger any macros, and cannot be bypassed by the Borg. It is used
* in Angband to handle "keymaps".
*/
static cptr inkey_next = NULL;
#ifdef ALLOW_BORG
/*
* Mega-Hack -- special "inkey_hack" hook. XXX XXX XXX
*
* This special function hook allows the "Borg" (see elsewhere) to take
* control of the "inkey()" function, and substitute in fake keypresses.
*/
char (*inkey_hack)(int flush_first) = NULL;
#endif /* ALLOW_BORG */
/*
* Get a keypress from the user.
*
* This function recognizes a few "global parameters". These are variables
* which, if set to TRUE before calling this function, will have an effect
* on this function, and which are always reset to FALSE by this function
* before this function returns. Thus they function just like normal
* parameters, except that most calls to this function can ignore them.
*
* If "inkey_xtra" is TRUE, then all pending keypresses will be flushed,
* and any macro processing in progress will be aborted. This flag is
* set by the "flush()" function, which does not actually flush anything
* itself, but rather, triggers delayed input flushing via "inkey_xtra".
*
* If "inkey_scan" is TRUE, then we will immediately return "zero" if no
* keypress is available, instead of waiting for a keypress.
*
* If "inkey_base" is TRUE, then all macro processing will be bypassed.
* If "inkey_base" and "inkey_scan" are both TRUE, then this function will
* not return immediately, but will wait for a keypress for as long as the
* normal macro matching code would, allowing the direct entry of macro
* triggers. The "inkey_base" flag is extremely dangerous!
*
* If "inkey_flag" is TRUE, then we will assume that we are waiting for a
* normal command, and we will only show the cursor if "hilite_player" is
* TRUE (or if the player is in a store), instead of always showing the
* cursor. The various "main-xxx.c" files should avoid saving the game
* in response to a "menu item" request unless "inkey_flag" is TRUE, to
* prevent savefile corruption.
*
* If we are waiting for a keypress, and no keypress is ready, then we will
* refresh (once) the window which was active when this function was called.
*
* Note that "back-quote" is automatically converted into "escape" for
* convenience on machines with no "escape" key. This is done after the
* macro matching, so the user can still make a macro for "backquote".
*
* Note the special handling of "ascii 30" (ctrl-caret, aka ctrl-shift-six)
* and "ascii 31" (ctrl-underscore, aka ctrl-shift-minus), which are used to
* provide support for simple keyboard "macros". These keys are so strange
* that their loss as normal keys will probably be noticed by nobody. The
* "ascii 30" key is used to indicate the "end" of a macro action, which
* allows recursive macros to be avoided. The "ascii 31" key is used by
* some of the "main-xxx.c" files to introduce macro trigger sequences.
*
* Hack -- we use "ascii 29" (ctrl-right-bracket) as a special "magic" key,
* which can be used to give a variety of "sub-commands" which can be used
* any time. These sub-commands could include commands to take a picture of
* the current screen, to start/stop recording a macro action, etc.
*
* If "angband_term[0]" is not active, we will make it active during this
* function, so that the various "main-xxx.c" files can assume that input
* is only requested (via "Term_inkey()") when "angband_term[0]" is active.
*
* Mega-Hack -- This function is used as the entry point for clearing the
* "signal_count" variable, and of the "character_saved" variable.
*
* Hack -- Note the use of "inkey_next" to allow "keymaps" to be processed.
*
* Mega-Hack -- Note the use of "inkey_hack" to allow the "Borg" to steal
* control of the keyboard from the user.
*/
char inkey(void)
{
int v;
char kk;
char ch = 0;
bool done = FALSE;
term *old = Term;
/* Hack -- Use the "inkey_next" pointer */
if (inkey_next && *inkey_next && !inkey_xtra)
{
/* Get next character, and advance */
ch = *inkey_next++;
/* Cancel the various "global parameters" */
inkey_base = inkey_xtra = inkey_flag = inkey_scan = FALSE;
/* Accept result */
macro_recorder_add(ch);
return (ch);
}
/* Forget pointer */
inkey_next = NULL;
#ifdef ALLOW_BORG
/* Mega-Hack -- Use the special hook */
if (inkey_hack && ((ch = (*inkey_hack)(inkey_xtra)) != 0))
{
/* Cancel the various "global parameters" */
inkey_base = inkey_xtra = inkey_flag = inkey_scan = FALSE;
/* Accept result */
macro_recorder_add(ch);
return (ch);
}
#endif /* ALLOW_BORG */
/* Hack -- handle delayed "flush()" */
if (inkey_xtra)
{
/* End "macro action" */
parse_macro = FALSE;
/* End "macro trigger" */
parse_under = FALSE;
/* Forget old keypresses */
Term_flush();
}
/* Access cursor state */
(void)Term_get_cursor(&v);
/* Show the cursor if waiting, except sometimes in "command" mode */
if (!inkey_scan && (!inkey_flag || hilite_player || character_icky))
{
/* Show the cursor */
(void)Term_set_cursor(1);
}
/* Hack -- Activate main screen */
Term_activate(angband_term[0]);
/* Get a key */
while (!ch)
{
/* Hack -- Handle "inkey_scan" */
if (!inkey_base && inkey_scan &&
(0 != Term_inkey(&kk, FALSE, FALSE)))
{
break;
}
/* Hack -- Flush output once when no key ready */
if (!done && (0 != Term_inkey(&kk, FALSE, FALSE)))
{
/* Hack -- activate proper term */
Term_activate(old);
/* Flush output */
Term_fresh();
/* Hack -- activate main screen */
Term_activate(angband_term[0]);
/* Mega-Hack -- reset saved flag */
character_saved = FALSE;
/* Mega-Hack -- reset signal counter */
signal_count = 0;
/* Only once */
done = TRUE;
}
/* Hack -- Handle "inkey_base" */
if (inkey_base)
{
int w = 0;
/* Wait forever */
if (!inkey_scan)
{
/* Wait for (and remove) a pending key */
if (0 == Term_inkey(&ch, TRUE, TRUE))
{
/* Done */
break;
}
/* Oops */
break;
}
/* Wait */
while (TRUE)
{
/* Check for (and remove) a pending key */
if (0 == Term_inkey(&ch, FALSE, TRUE))
{
/* Done */
break;
}
/* No key ready */
else
{
/* Increase "wait" */
w += 10;
/* Excessive delay */
if (w >= 100) break;
/* Delay */
Term_xtra(TERM_XTRA_DELAY, w);
}
}
/* Done */
break;
}
/* Get a key (see above) */
ch = inkey_aux();
/* Handle "control-right-bracket" */
if ((ch == 29) || ((!rogue_like_commands) && (ch == KTRL('D'))))
{
/* Strip this key */
ch = 0;
if (!do_movies)
/* Do an html dump */
do_cmd_html_dump();
else
/* Do a text box in the cmovie */
do_cmovie_insert();
/* Continue */
continue;
}
/* Treat back-quote as escape */
if (ch == '`') ch = ESCAPE;
/* End "macro trigger" */
if (parse_under && (ch <= 32))
{
/* Strip this key */
ch = 0;
/* End "macro trigger" */
parse_under = FALSE;
}
/* Handle "control-caret" */
if (ch == 30)
{
/* Strip this key */
ch = 0;
}
/* Handle "control-underscore" */
else if (ch == 31)
{
/* Strip this key */
ch = 0;
/* Begin "macro trigger" */
parse_under = TRUE;
}
/* Inside "macro trigger" */
else if (parse_under)
{
/* Strip this key */
ch = 0;
}
}
/* Hack -- restore the term */
Term_activate(old);
/* Restore the cursor */
Term_set_cursor(v);
/* Cancel the various "global parameters" */
inkey_base = inkey_xtra = inkey_flag = inkey_scan = FALSE;
/* Return the keypress */
macro_recorder_add(ch);
return (ch);
}
/*
* We use a global array for all inscriptions to reduce the memory
* spent maintaining inscriptions. Of course, it is still possible
* to run out of inscription memory, especially if too many different
* inscriptions are used, but hopefully this will be rare.
*
* We use dynamic string allocation because otherwise it is necessary
* to pre-guess the amount of quark activity. We limit the total
* number of quarks, but this is much easier to "expand" as needed.
*
* Any two items with the same inscription will have the same "quark"
* index, which should greatly reduce the need for inscription space.
*
* Note that "quark zero" is NULL and should not be "dereferenced".
*/
/*
* Add a new "quark" to the set of quarks.
*/
s16b quark_add(cptr str)
{
int i;
/* Look for an existing quark */
for (i = 1; i < quark__num; i++)
{
/* Check for equality */
if (streq(quark__str[i], str)) return (i);
}
/* Paranoia -- Require room */
if (quark__num == QUARK_MAX) return (0);
/* New maximal quark */
quark__num = i + 1;
/* Add a new quark */
quark__str[i] = string_make(str);
/* Return the index */
return (i);
}
/*
* This function looks up a quark
*/
cptr quark_str(s16b i)
{
cptr q;
/* Verify */
if ((i < 0) || (i >= quark__num)) i = 0;
/* Access the quark */
q = quark__str[i];
/* Return the quark */
return (q);
}
/*
* Second try for the "message" handling routines.
*
* Each call to "message_add(s)" will add a new "most recent" message
* to the "message recall list", using the contents of the string "s".
*
* The messages will be stored in such a way as to maximize "efficiency",
* that is, we attempt to maximize the number of sequential messages that
* can be retrieved, given a limited amount of storage space.
*
* We keep a buffer of chars to hold the "text" of the messages, not
* necessarily in "order", and an array of offsets into that buffer,
* representing the actual messages. This is made more complicated
* by the fact that both the array of indexes, and the buffer itself,
* are both treated as "circular arrays" for efficiency purposes, but
* the strings may not be "broken" across the ends of the array.
*
* The "message_add()" function is rather "complex", because it must be
* extremely efficient, both in space and time, for use with the Borg.
*/
/*
* How many messages are "available"?
*/
s16b message_num(void)
{
int last, next, n;
/* Extract the indexes */
last = message__last;
next = message__next;
/* Handle "wrap" */
if (next < last) next += MESSAGE_MAX;
/* Extract the space */
n = (next - last);
/* Return the result */
return (n);
}
/*
* Recall the "text" of a saved message
*/
cptr message_str(int age)
{
static char buf[1024];
s16b x;
s16b o;
cptr s;
/* Forgotten messages have no text */
if ((age < 0) || (age >= message_num())) return ("");
/* Acquire the "logical" index */
x = (message__next + MESSAGE_MAX - (age + 1)) % MESSAGE_MAX;
/* Get the "offset" for the message */
o = message__ptr[x];
/* Access the message text */
s = &message__buf[o];
/* Hack -- Handle repeated messages */
if (message__count[x] > 1)
{
strnfmt(buf, 1024, "%s <%dx>", s, message__count[x]);
s = buf;
}
/* Return the message text */
return (s);
}
/*
* Recall the color of a saved message
*/
byte message_color(int age)
{
s16b x;
byte color = TERM_WHITE;
/* Forgotten messages have no text */
if ((age < 0) || (age >= message_num())) return (TERM_WHITE);
/* Acquire the "logical" index */
x = (message__next + MESSAGE_MAX - (age + 1)) % MESSAGE_MAX;
/* Get the "offset" for the message */
color = message__color[x];
/* Return the message text */
return (color);
}
/*
* Recall the type of a saved message
*/
byte message_type(int age)
{
s16b x;
byte type;
/* Forgotten messages have no text */
if ((age < 0) || (age >= message_num())) return (MESSAGE_NONE);
/* Acquire the "logical" index */
x = (message__next + MESSAGE_MAX - (age + 1)) % MESSAGE_MAX;
/* Get the "offset" for the message */
type = message__type[x];
/* Return the message text */
return (type);
}
/*
* Add a new message, with great efficiency
*/
void message_add(byte type, cptr str, byte color)
{
int i, k, x, n;
cptr s;
/*** Step 1 -- Analyze the message ***/
/* Hack -- Ignore "non-messages" */
if (!str) return;
/* Message length */
n = strlen(str);
/* Important Hack -- Ignore "long" messages */
if (n >= MESSAGE_BUF / 4) return;
/*** Step 2 -- Handle repeated messages ***/
/* Acquire the "logical" last index */
x = (message__next + MESSAGE_MAX - 1) % MESSAGE_MAX;
/* Get the last message text */
s = &message__buf[message__ptr[x]];
/* Last message repeated? */
if (streq(str, s))
{
/* Increase the message count */
message__count[x]++;
/* Success */
return;
}
/*** Step 3 -- Attempt to optimize ***/
/* Limit number of messages to check */
k = message_num() / 4;
/* Limit number of messages to check */
if (k > MESSAGE_MAX / 32) k = MESSAGE_MAX / 32;
/* Check the last few messages (if any to count) */
for (i = message__next; k; k--)
{
u16b q;
cptr old;
/* Back up and wrap if needed */
if (i-- == 0) i = MESSAGE_MAX - 1;
/* Stop before oldest message */
if (i == message__last) break;
/* Extract "distance" from "head" */
q = (message__head + MESSAGE_BUF - message__ptr[i]) % MESSAGE_BUF;
/* Do not optimize over large distance */
if (q > MESSAGE_BUF / 2) continue;
/* Access the old string */
old = &message__buf[message__ptr[i]];
/* Compare */
if (!streq(old, str)) continue;
/* Get the next message index, advance */
x = message__next++;
/* Handle wrap */
if (message__next == MESSAGE_MAX) message__next = 0;
/* Kill last message if needed */
if (message__next == message__last) message__last++;
/* Handle wrap */
if (message__last == MESSAGE_MAX) message__last = 0;
/* Assign the starting address */
message__ptr[x] = message__ptr[i];
message__color[x] = color;
message__type[x] = type;
message__count[x] = 1;
/* Success */
return;
}
/*** Step 4 -- Ensure space before end of buffer ***/
/* Kill messages and Wrap if needed */
if (message__head + n + 1 >= MESSAGE_BUF)
{
/* Kill all "dead" messages */
for (i = message__last; TRUE; i++)
{
/* Wrap if needed */
if (i == MESSAGE_MAX) i = 0;
/* Stop before the new message */
if (i == message__next) break;
/* Kill "dead" messages */
if (message__ptr[i] >= message__head)
{
/* Track oldest message */
message__last = i + 1;
}
}
/* Wrap "tail" if needed */
if (message__tail >= message__head) message__tail = 0;
/* Start over */
message__head = 0;
}
/*** Step 5 -- Ensure space before next message ***/
/* Kill messages if needed */
if (message__head + n + 1 > message__tail)
{
/* Grab new "tail" */
message__tail = message__head + n + 1;
/* Advance tail while possible past first "nul" */
while (message__buf[message__tail - 1]) message__tail++;
/* Kill all "dead" messages */
for (i = message__last; TRUE; i++)
{
/* Wrap if needed */
if (i == MESSAGE_MAX) i = 0;
/* Stop before the new message */
if (i == message__next) break;
/* Kill "dead" messages */
if ((message__ptr[i] >= message__head) &&
(message__ptr[i] < message__tail))
{
/* Track oldest message */
message__last = i + 1;
}
}
}
/*** Step 6 -- Grab a new message index ***/
/* Get the next message index, advance */
x = message__next++;
/* Handle wrap */
if (message__next == MESSAGE_MAX) message__next = 0;
/* Kill last message if needed */
if (message__next == message__last) message__last++;
/* Handle wrap */
if (message__last == MESSAGE_MAX) message__last = 0;
/*** Step 7 -- Insert the message text ***/
/* Assign the starting address */
message__ptr[x] = message__head;
message__color[x] = color;
message__type[x] = type;
message__count[x] = 1;
/* Append the new part of the message */
for (i = 0; i < n; i++)
{
/* Copy the message */
message__buf[message__head + i] = str[i];
}
/* Terminate */
message__buf[message__head + i] = '\0';
/* Advance the "head" pointer */
message__head += n + 1;
}
/*
* Hack -- flush
*/
static void msg_flush(int x)
{
byte a = TERM_L_BLUE;
/* Hack -- fake monochrome */
if (!use_color) a = TERM_WHITE;
/* Pause for response */
Term_putstr(x, 0, -1, a, "-more-");
/* Get an acceptable keypress */
while (1)
{
int cmd = inkey();
if (quick_messages) break;
if ((cmd == ESCAPE) || (cmd == ' ')) break;
if ((cmd == '\n') || (cmd == '\r')) break;
bell();
}
/* Clear the line */
Term_erase(0, 0, 255);
}
/* Display a message */
void display_message(int x, int y, int split, byte color, cptr t)
{
int i = 0, j = 0;
while (i < split)
{
if (t[i] == '#')
{
if (t[i + 1] == '#')
{
Term_putstr(x + j, y, 1, color, "#");
i += 2;
j++;
}
else
{
color = color_char_to_attr(t[i + 1]);
i += 2;
}
}
else
{
Term_putstr(x + j, y, 1, color, t + i);
i++;
j++;
}
}
}
/*
* Output a message to the top line of the screen.
*
* Break long messages into multiple pieces (40-72 chars).
*
* Allow multiple short messages to "share" the top line.
*
* Prompt the user to make sure he has a chance to read them.
*
* These messages are memorized for later reference (see above).
*
* We could do "Term_fresh()" to provide "flicker" if needed.
*
* The global "msg_flag" variable can be cleared to tell us to
* "erase" any "pending" messages still on the screen.
*
* XXX XXX XXX Note that we must be very careful about using the
* "msg_print()" functions without explicitly calling the special
* "msg_print(NULL)" function, since this may result in the loss
* of information if the screen is cleared, or if anything is
* displayed on the top line.
*
* XXX XXX XXX Note that "msg_print(NULL)" will clear the top line
* even if no messages are pending. This is probably a hack.
*/
void cmsg_print(byte color, cptr msg)
{
static int p = 0;
int n;
char *t;
char buf[1024];
int lim = Term->wid - 8;
/* Hack -- Reset */
if (!msg_flag) p = 0;
/* Message Length */
n = (msg ? strlen(msg) : 0);
/* Hack -- flush when requested or needed */
if (p && (!msg || ((p + n) > lim)))
{
/* Flush */
msg_flush(p);
/* Forget it */
msg_flag = FALSE;
/* Reset */
p = 0;
}
/* No message */
if (!msg) return;
/* Paranoia */
if (n > 1000) return;
/* Memorize the message */
if (character_generated) message_add(MESSAGE_MSG, msg, color);
/* Handle "auto_more" */
if (auto_more)
{
/* Window stuff */
p_ptr->window |= (PW_MESSAGE);
/* Force window update */
window_stuff();
/* Done */
return;
}
/* Copy it */
strcpy(buf, msg);
/* Analyze the buffer */
t = buf;
/* Split message */
while (n > lim)
{
char oops;
int check, split;
/* Default split */
split = lim;
/* Find the "best" split point */
for (check = 40; check < lim; check++)
{
/* Found a valid split point */
if (t[check] == ' ') split = check;
}
/* Save the split character */
oops = t[split];
/* Split the message */
t[split] = '\0';
/* Display part of the message */
display_message(0, 0, split, color, t);
/* Flush it */
msg_flush(split + 1);
/* Memorize the piece */
/* if (character_generated) message_add(t); */
/* Restore the split character */
t[split] = oops;
/* Insert a space */
t[--split] = ' ';
/* Prepare to recurse on the rest of "buf" */
t += split;
n -= split;
}
/* Display the tail of the message */
display_message(p, 0, n, color, t);
/* Memorize the tail */
/* if (character_generated) message_add(t); */
/* Window stuff */
p_ptr->window |= (PW_MESSAGE);
/* Remember the message */
msg_flag = TRUE;
/* Remember the position */
p += n + 1;
/* Optional refresh */
if (fresh_message) Term_fresh();
}
/* Hack -- for compatibility and easy sake */
void msg_print(cptr msg)
{
cmsg_print(TERM_WHITE, msg);
}
/*
* Hack -- prevent "accidents" in "screen_save()" or "screen_load()"
*/
static int screen_depth = 0;
/*
* Save the screen, and increase the "icky" depth.
*
* This function must match exactly one call to "screen_load()".
*/
void screen_save(void)
{
/* Hack -- Flush messages */
msg_print(NULL);
/* Save the screen (if legal) */
if (screen_depth++ == 0) Term_save();
/* Increase "icky" depth */
character_icky++;
}
/*
* Load the screen, and decrease the "icky" depth.
*
* This function must match exactly one call to "screen_save()".
*/
void screen_load(void)
{
/* Hack -- Flush messages */
msg_print(NULL);
/* Load the screen (if legal) */
if (--screen_depth == 0) Term_load();
/* Decrease "icky" depth */
character_icky--;
}
/*
* Display a formatted message, using "vstrnfmt()" and "msg_print()".
*/
void msg_format(cptr fmt, ...)
{
va_list vp;
char buf[1024];
/* Begin the Varargs Stuff */
va_start(vp, fmt);
/* Format the args, save the length */
(void)vstrnfmt(buf, 1024, fmt, vp);
/* End the Varargs Stuff */
va_end(vp);
/* Display */
cmsg_print(TERM_WHITE, buf);
}
void cmsg_format(byte color, cptr fmt, ...)
{
va_list vp;
char buf[1024];
/* Begin the Varargs Stuff */
va_start(vp, fmt);
/* Format the args, save the length */
(void)vstrnfmt(buf, 1024, fmt, vp);
/* End the Varargs Stuff */
va_end(vp);
/* Display */
cmsg_print(color, buf);
}
/*
* Display a string on the screen using an attribute.
*
* At the given location, using the given attribute, if allowed,
* add the given string. Do not clear the line.
*/
void c_put_str(byte attr, cptr str, int row, int col)
{
/* Hack -- fake monochrome */
if (!use_color) attr = TERM_WHITE;
/* Position cursor, Dump the attr/text */
Term_putstr(col, row, -1, attr, str);
}
/*
* As above, but in "white"
*/
void put_str(cptr str, int row, int col)
{
/* Spawn */
Term_putstr(col, row, -1, TERM_WHITE, str);
}
/*
* Display a string on the screen using an attribute, and clear
* to the end of the line.
*/
void c_prt(byte attr, cptr str, int row, int col)
{
/* Hack -- fake monochrome */
if (!use_color) attr = TERM_WHITE;
/* Clear line, position cursor */
Term_erase(col, row, 255);
/* Dump the attr/text */
Term_addstr( -1, attr, str);
}
/*
* As above, but in "white"
*/
void prt(cptr str, int row, int col)
{
/* Spawn */
c_prt(TERM_WHITE, str, row, col);
}
/*
* Print some (colored) text to the screen at the current cursor position,
* automatically "wrapping" existing text (at spaces) when necessary to
* avoid placing any text into the last column, and clearing every line
* before placing any text in that line. Also, allow "newline" to force
* a "wrap" to the next line. Advance the cursor as needed so sequential
* calls to this function will work correctly.
*
* Once this function has been called, the cursor should not be moved
* until all the related "text_out()" calls to the window are complete.
*
* This function will correctly handle any width up to the maximum legal
* value of 256, though it works best for a standard 80 character width.
*/
void text_out_to_screen(byte a, cptr str)
{
int x, y;
int wid, h;
int wrap;
cptr s;
/* Obtain the size */
(void)Term_get_size(&wid, &h);
/* Obtain the cursor */
(void)Term_locate(&x, &y);
/* Use special wrapping boundary? */
if ((text_out_wrap > 0) && (text_out_wrap < wid))
wrap = text_out_wrap;
else
wrap = wid;
/* Process the string */
for (s = str; *s; s++)
{
char ch;
/* Force wrap */
if (*s == '\n')
{
/* Wrap */
x = text_out_indent;
y++;
/* Clear line, move cursor */
Term_erase(x, y, 255);
continue;
}
/* Clean up the char */
ch = (isprint((unsigned char) * s) ? *s : ' ');
/* Wrap words as needed */
if ((x >= wrap - 1) && (ch != ' '))
{
int i, n = 0;
byte av[256];
char cv[256];
/* Wrap word */
if (x < wrap)
{
/* Scan existing text */
for (i = wrap - 2; i >= 0; i--)
{
/* Grab existing attr/char */
Term_what(i, y, &av[i], &cv[i]);
/* Break on space */
if (cv[i] == ' ') break;
/* Track current word */
n = i;
}
}
/* Special case */
if (n == 0) n = wrap;
/* Clear line */
Term_erase(n, y, 255);
/* Wrap */
x = text_out_indent;
y++;
/* Clear line, move cursor */
Term_erase(x, y, 255);
/* Wrap the word (if any) */
for (i = n; i < wrap - 1; i++)
{
/* Dump */
Term_addch(av[i], cv[i]);
/* Advance (no wrap) */
if (++x > wrap) x = wrap;
}
}
/* Dump */
Term_addch(a, ch);
/* Advance */
if (++x > wrap) x = wrap;
}
}
/*
* Write text to the given file and apply line-wrapping.
*
* Hook function for text_out(). Make sure that text_out_file points
* to an open text-file.
*
* Long lines will be wrapped at text_out_wrap, or at column 75 if that
* is not set; or at a newline character.
*
* You must be careful to end all file output with a newline character
* to "flush" the stored line position.
*/
void text_out_to_file(byte a, cptr str)
{
/* Current position on the line */
static int pos = 0;
/* Wrap width */
int wrap = (text_out_wrap ? text_out_wrap : 75);
/* Current location within "str" */
cptr s = str;
/* Unused parameter */
(void)a;
/* Process the string */
while (*s)
{
char ch;
int n = 0;
int len = wrap - pos;
int l_space = 0;
/* If we are at the start of the line... */
if (pos == 0)
{
int i;
/* Output the indent */
for (i = 0; i < text_out_indent; i++)
{
fputc(' ', text_out_file);
pos++;
}
}
/* Find length of line up to next newline or end-of-string */
while ((n < len) && !((s[n] == '\n') || (s[n] == '\0')))
{
/* Mark the most recent space in the string */
if (s[n] == ' ') l_space = n;
/* Increment */
n++;
}
/* If we have encountered no spaces */
if ((l_space == 0) && (n == len))
{
/* If we are at the start of a new line */
if (pos == text_out_indent)
{
len = n;
}
else
{
/* Begin a new line */
fputc('\n', text_out_file);
/* Reset */
pos = 0;
continue;
}
}
else
{
/* Wrap at the newline */
if ((s[n] == '\n') || (s[n] == '\0')) len = n;
/* Wrap at the last space */
else len = l_space;
}
/* Write that line to file */
for (n = 0; n < len; n++)
{
/* Ensure the character is printable */
ch = (isprint(s[n]) ? s[n] : ' ');
/* Write out the character */
fputc(ch, text_out_file);
/* Increment */
pos++;
}
/* Move 's' past the stuff we've written */
s += len;
/* If we are at the end of the string, end */
if (*s == '\0') return;
/* Skip newlines */
if (*s == '\n') s++;
/* Begin a new line */
fputc('\n', text_out_file);
/* Reset */
pos = 0;
/* Skip whitespace */
while (*s == ' ') s++;
}
/* We are done */
return;
}
/*
* Output text to the screen or to a file depending on the selected
* text_out hook.
*/
void text_out(cptr str)
{
text_out_c(TERM_WHITE, str);
}
/*
* Output text to the screen (in color) or to a file depending on the
* selected hook.
*/
void text_out_c(byte a, cptr str)
{
text_out_hook(a, str);
}
/*
* Clear part of the screen
*/
void clear_from(int row)
{
int y;
/* Erase requested rows */
for (y = row; y < Term->hgt; y++)
{
/* Erase part of the screen */
Term_erase(0, y, 255);
}
}
/*
* Try to find a matching command completion.
* Note that this is not so friendly since it doesn't give
* a list of possible completions.
*
* First arg is the string to be completed, second is it's length,
* third is it's maximum length.
*/
static int complete_where = 0;
static char complete_buf[100];
static int complete_command(char *buf, int clen, int mlen)
{
int i, j = 1, max = clen;
bool gotone = FALSE;
/* Forget the characters after the end of the string. */
complete_buf[clen] = '\0';
for (i = 0; i < cli_total; i++)
{
cli_comm *cli_ptr = cli_info + i;
if (!strncmp(cli_ptr->comm, complete_buf, clen))
{
Term_erase(0, j, 80);
Term_putstr(0, j++, -1, TERM_WHITE, cli_ptr->comm);
/* For the first match, copy the whole string to buf. */
if (!gotone)
{
sprintf(buf, "%.*s", mlen, cli_ptr->comm);
gotone = TRUE;
}
/* For later matches, simply notice how much of buf it
* matches. */
else
{
for (max = clen; max < mlen; max++)
{
if (cli_ptr->comm[max] == '\0') break;
if (cli_ptr->comm[max] != buf[max]) break;
}
if (max < mlen) buf[max] = '\0';
}
}
}
return strlen(buf) + 1;
}
/*
* Get some input at the cursor location.
* Assume the buffer is initialized to a default string.
* Note that this string is often "empty" (see below).
* The default buffer is displayed in yellow until cleared.
* Pressing RETURN right away accepts the default entry.
* Normal chars clear the default and append the char.
* Backspace clears the default or deletes the final char.
* ESCAPE clears the buffer and the window and returns FALSE.
* RETURN accepts the current buffer contents and returns TRUE.
*/
bool askfor_aux_complete = FALSE;
bool askfor_aux(char *buf, int len)
{
int y, x;
int i = 0;
int k = 0;
bool done = FALSE;
/* Locate the cursor */
Term_locate(&x, &y);
/* Paranoia -- check len */
if (len < 1) len = 1;
/* Paranoia -- check column */
if ((x < 0) || (x >= 80)) x = 0;
/* Restrict the length */
if (x + len > 80) len = 80 - x;
/* Paranoia -- Clip the default entry */
buf[len] = '\0';
/* Display the default answer */
Term_erase(x, y, len);
Term_putstr(x, y, -1, TERM_YELLOW, buf);
if (askfor_aux_complete)
{
screen_save();
complete_where = 0;
strncpy(complete_buf, buf, 100);
}
/* Process input */
while (!done)
{
/* Place cursor */
Term_gotoxy(x + k, y);
/* Get a key */
i = inkey();
/* Analyze the key */
switch (i)
{
case ESCAPE:
k = 0;
done = TRUE;
break;
case '\n':
case '\r':
k = strlen(buf);
done = TRUE;
break;
case '\t':
if (askfor_aux_complete && k)
{
screen_load();
screen_save();
k = complete_command(buf, k, len);
}
else
{
bell();
}
case 0x7F:
case '\010':
if (k > 0) k--;
strncpy(complete_buf, buf, k);
break;
default:
if ((k < len) && (isprint(i)))
{
buf[k++] = i;
strncpy(complete_buf, buf, k);
}
else
{
bell();
}
break;
}
/* Terminate */
buf[k] = '\0';
/* Update the entry */
Term_erase(x, y, len);
Term_putstr(x, y, -1, TERM_WHITE, buf);
}
if (askfor_aux_complete)
{
screen_load();
}
/* Aborted */
if (i == ESCAPE) return (FALSE);
/* Success */
return (TRUE);
}
/*
* Get a string from the user
*
* The "prompt" should take the form "Prompt: "
*
* Note that the initial contents of the string is used as
* the default response, so be sure to "clear" it if needed.
*
* We clear the input, and return FALSE, on "ESCAPE".
*/
bool get_string(cptr prompt, char *buf, int len)
{
bool res;
/* Paranoia XXX XXX XXX */
msg_print(NULL);
/* Display prompt */
prt(prompt, 0, 0);
/* Ask the user for a string */
res = askfor_aux(buf, len);
/* Clear prompt */
prt("", 0, 0);
/* Result */
return (res);
}
/*
* Verify something with the user
*
* The "prompt" should take the form "Query? "
*
* Note that "[y/n]" is appended to the prompt.
*/
bool get_check(cptr prompt)
{
int i;
char buf[80];
/* Paranoia XXX XXX XXX */
msg_print(NULL);
/* Hack -- Build a "useful" prompt */
strnfmt(buf, 78, "%.70s[y/n] ", prompt);
/* Prompt for it */
prt(buf, 0, 0);
/* Get an acceptable answer */
while (TRUE)
{
i = inkey();
if (quick_messages) break;
if (i == ESCAPE) break;
if (strchr("YyNn", i)) break;
bell();
}
/* Erase the prompt */
prt("", 0, 0);
/* Normal negation */
if ((i != 'Y') && (i != 'y')) return (FALSE);
/* Success */
return (TRUE);
}
/*
* Prompts for a keypress
*
* The "prompt" should take the form "Command: "
*
* Returns TRUE unless the character is "Escape"
*/
bool get_com(cptr prompt, char *command)
{
/* Paranoia XXX XXX XXX */
msg_print(NULL);
/* Display a prompt */
prt(prompt, 0, 0);
/* Get a key */
*command = inkey();
/* Clear the prompt */
prt("", 0, 0);
/* Handle "cancel" */
if (*command == ESCAPE) return (FALSE);
/* Success */
return (TRUE);
}
/*
* Request a "quantity" from the user
*
* Hack -- allow "command_arg" to specify a quantity
*/
s32b get_quantity(cptr prompt, s32b max)
{
s32b amt;
int aamt;
char tmp[80];
char buf[80];
/* Use "command_arg" */
if (command_arg)
{
/* Extract a number */
amt = command_arg;
/* Clear "command_arg" */
command_arg = 0;
/* Enforce the maximum */
if (amt > max) amt = max;
/* Use it */
return (amt);
}
#ifdef ALLOW_REPEAT /* TNB */
/* Get the item index */
if ((max != 1) && repeat_pull(&aamt))
{
amt = aamt;
/* Enforce the maximum */
if (amt > max) amt = max;
/* Enforce the minimum */
if (amt < 0) amt = 0;
/* Use it */
return (amt);
}
#endif /* ALLOW_REPEAT -- TNB */
/* Build a prompt if needed */
if (!prompt)
{
/* Build a prompt */
sprintf(tmp, "Quantity (1-%ld): ", max);
/* Use that prompt */
prompt = tmp;
}
/* Default to one */
amt = 1;
/* Build the default */
sprintf(buf, "%ld", amt);
/* Ask for a quantity */
if (!get_string(prompt, buf, 9)) return (0);
/* Extract a number */
amt = atoi(buf);
/* A letter means "all" */
if (isalpha(buf[0])) amt = max;
/* Enforce the maximum */
if (amt > max) amt = max;
/* Enforce the minimum */
if (amt < 0) amt = 0;
#ifdef ALLOW_REPEAT /* TNB */
if (amt) repeat_push(amt);
#endif /* ALLOW_REPEAT -- TNB */
/* Return the result */
return (amt);
}
/*
* Pause for user response XXX XXX XXX
*/
void pause_line(int row)
{
int i;
prt("", row, 0);
put_str("[Press any key to continue]", row, 23);
i = inkey();
prt("", row, 0);
}
/*
* Hack -- special buffer to hold the action of the current keymap
*/
static char request_command_buffer[256];
/*
* Mega-Hack -- characters for which keymaps should be ignored in
* request_command(). This MUST have at least twice as many characters as
* there are building actions in the actions[] array in store_info_type.
*/
#define MAX_IGNORE_KEYMAPS 12
char request_command_ignore_keymaps[MAX_IGNORE_KEYMAPS];
/*
* Mega-Hack -- flag set by do_cmd_{inven,equip}() to allow keymaps in
* auto-command mode.
*/
bool request_command_inven_mode = FALSE;
/*
* Request a command from the user.
*
* Sets p_ptr->command_cmd, p_ptr->command_dir, p_ptr->command_rep,
* p_ptr->command_arg. May modify p_ptr->command_new.
*
* Note that "caret" ("^") is treated specially, and is used to
* allow manual input of control characters. This can be used
* on many machines to request repeated tunneling (Ctrl-H) and
* on the Macintosh to request "Control-Caret".
*
* Note that "backslash" is treated specially, and is used to bypass any
* keymap entry for the following character. This is useful for macros.
*
* Note that this command is used both in the dungeon and in
* stores, and must be careful to work in both situations.
*
* Note that "p_ptr->command_new" may not work any more. XXX XXX XXX
*/
void request_command(int shopping)
{
int i;
s16b cmd;
char cmd_char;
int mode;
cptr act;
/* Roguelike */
if (rogue_like_commands)
{
mode = KEYMAP_MODE_ROGUE;
}
/* Original */
else
{
mode = KEYMAP_MODE_ORIG;
}
/* No command yet */
command_cmd = 0;
/* No "argument" yet */
command_arg = 0;
/* No "direction" yet */
command_dir = 0;
/* Get command */
while (1)
{
/* Hack -- auto-commands */
if (command_new)
{
/* Flush messages */
msg_print(NULL);
/* Use auto-command */
cmd = command_new;
/* Forget it */
command_new = 0;
/* Hack - bypass keymaps, unless asked not to */
if (!inkey_next && !request_command_inven_mode)
{
inkey_next = "";
}
/* Mega-Hack -- turn off this flag immediately */
request_command_inven_mode = FALSE;
}
/* Get a keypress in "command" mode */
else
{
/* Hack -- no flush needed */
msg_flag = FALSE;
/* Activate "command mode" */
inkey_flag = TRUE;
/* Get a command */
cmd = inkey();
}
/* Clear top line */
prt("", 0, 0);
/* Command Count */
if (cmd == '0')
{
int old_arg = command_arg;
/* Reset */
command_arg = 0;
/* Begin the input */
prt("Count: ", 0, 0);
/* Get a command count */
while (1)
{
/* Get a new keypress */
cmd = inkey();
/* Simple editing (delete or backspace) */
if ((cmd == 0x7F) || (cmd == KTRL('H')))
{
/* Delete a digit */
command_arg = command_arg / 10;
/* Show current count */
prt(format("Count: %d", command_arg), 0, 0);
}
/* Actual numeric data */
else if (cmd >= '0' && cmd <= '9')
{
/* Stop count at 9999 */
if (command_arg >= 1000)
{
/* Warn */
bell();
/* Limit */
command_arg = 9999;
}
/* Increase count */
else
{
/* Incorporate that digit */
command_arg = command_arg * 10 + D2I(cmd);
}
/* Show current count */
prt(format("Count: %d", command_arg), 0, 0);
}
/* Exit on "unusable" input */
else
{
break;
}
}
/* Hack -- Handle "zero" */
if (command_arg == 0)
{
/* Default to 99 */
command_arg = 99;
/* Show current count */
prt(format("Count: %d", command_arg), 0, 0);
}
/* Hack -- Handle "old_arg" */
if (old_arg != 0)
{
/* Restore old_arg */
command_arg = old_arg;
/* Show current count */
prt(format("Count: %d", command_arg), 0, 0);
}
/* Hack -- white-space means "enter command now" */
if ((cmd == ' ') || (cmd == '\n') || (cmd == '\r'))
{
/* Get a real command */
bool temp = get_com("Command: ", &cmd_char);
cmd = cmd_char;
if (!temp)
{
/* Clear count */
command_arg = 0;
/* Continue */
continue;
}
}
}
/* Allow "keymaps" to be bypassed */
if (cmd == '\\')
{
/* Get a real command */
(void)get_com("Command: ", &cmd_char);
cmd = cmd_char;
/* Hack -- bypass keymaps */
if (!inkey_next) inkey_next = "";
}
/* Allow "control chars" to be entered */
if (cmd == '^')
{
/* Get a new command and controlify it */
if (get_com("Control: ", &cmd_char)) cmd = KTRL(cmd_char);
else cmd = 0;
}
/* Look up applicable keymap */
act = keymap_act[mode][(byte)(cmd)];
/* Mega-Hack -- Ignore certain keymaps */
if (shopping && cmd > 0)
{
for (i = 0; i < MAX_IGNORE_KEYMAPS; i++)
if (cmd == request_command_ignore_keymaps[i])
{
act = NULL;
break;
}
}
/* Apply keymap if not inside a keymap already */
if (act && !inkey_next)
{
/* Install the keymap (limited buffer size) */
strnfmt(request_command_buffer, 256, "%s", act);
/* Start using the buffer */
inkey_next = request_command_buffer;
/* Continue */
continue;
}
/* Paranoia */
if (!cmd) continue;
/* Use command */
command_cmd = cmd;
/* Done */
break;
}
/* Hack -- Auto-repeat certain commands */
if (always_repeat && (command_arg <= 0))
{
/* Hack -- auto repeat certain commands */
if (strchr("TBDoc+", command_cmd))
{
/* Repeat 99 times */
command_arg = 99;
}
}
/* Hack -- Scan equipment */
for (i = INVEN_WIELD; i < INVEN_TOTAL; i++)
{
cptr s;
object_type *o_ptr = &p_ptr->inventory[i];
/* Skip non-objects */
if (!o_ptr->k_idx) continue;
/* No inscription */
if (!o_ptr->note) continue;
/* Obtain the inscription */
s = quark_str(o_ptr->note);
/* Find a '^' */
s = strchr(s, '^');
/* Process preventions */
while (s)
{
/* Check the "restriction" character */
if ((s[1] == command_cmd) || (s[1] == '*'))
{
/* Hack -- Verify command */
if (!get_check("Are you sure? "))
{
/* Hack -- Use space */
command_cmd = ' ';
}
}
/* Find another '^' */
s = strchr(s + 1, '^');
}
}
/* Hack -- erase the message line. */
prt("", 0, 0);
}
/*
* Check a char for "vowel-hood"
*/
bool is_a_vowel(int ch)
{
switch (ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
return (TRUE);
}
return (FALSE);
}
#if 0
/*
* Replace the first instance of "target" in "buf" with "insert"
* If "insert" is NULL, just remove the first instance of "target"
* In either case, return TRUE if "target" is found.
*
* XXX Could be made more efficient, especially in the
* case where "insert" is smaller than "target".
*/
static bool insert_str(char *buf, cptr target, cptr insert)
{
int i, len;
int b_len, t_len, i_len;
/* Attempt to find the target (modify "buf") */
buf = strstr(buf, target);
/* No target found */
if (!buf) return (FALSE);
/* Be sure we have an insertion string */
if (!insert) insert = "";
/* Extract some lengths */
t_len = strlen(target);
i_len = strlen(insert);
b_len = strlen(buf);
/* How much "movement" do we need? */
len = i_len - t_len;
/* We need less space (for insert) */
if (len < 0)
{
for (i = t_len; i < b_len; ++i) buf[i + len] = buf[i];
}
/* We need more space (for insert) */
else if (len > 0)
{
for (i = b_len - 1; i >= t_len; --i) buf[i + len] = buf[i];
}
/* If movement occured, we need a new terminator */
if (len) buf[b_len + len] = '\0';
/* Now copy the insertion string */
for (i = 0; i < i_len; ++i) buf[i] = insert[i];
/* Successful operation */
return (TRUE);
}
#endif
/*
* GH
* Called from cmd4.c and a few other places. Just extracts
* a direction from the keymap for ch (the last direction,
* in fact) byte or char here? I'm thinking that keymaps should
* generally only apply to single keys, which makes it no more
* than 128, so a char should suffice... but keymap_act is 256...
*/
int get_keymap_dir(char ch)
{
int d = 0;
int mode;
cptr act;
cptr s;
/* Already a direction? */
if (isdigit(ch))
{
d = D2I(ch);
}
else
{
/* Roguelike */
if (rogue_like_commands)
{
mode = KEYMAP_MODE_ROGUE;
}
/* Original */
else
{
mode = KEYMAP_MODE_ORIG;
}
/* Extract the action (if any) */
act = keymap_act[mode][(byte)(ch)];
/* Analyze */
if (act)
{
/* Convert to a direction */
for (s = act; *s; ++s)
{
/* Use any digits in keymap */
if (isdigit(*s)) d = D2I(*s);
}
}
}
/* Paranoia */
if (d == 5) d = 0;
/* Return direction */
return (d);
}
#ifdef ALLOW_REPEAT /* TNB */
#define REPEAT_MAX 20
/* Number of chars saved */
static int repeat__cnt = 0;
/* Current index */
static int repeat__idx = 0;
/* Saved "stuff" */
static int repeat__key[REPEAT_MAX];
void repeat_push(int what)
{
/* Too many keys */
if (repeat__cnt == REPEAT_MAX) return;
/* Push the "stuff" */
repeat__key[repeat__cnt++] = what;
/* Prevents us from pulling keys */
++repeat__idx;
}
bool repeat_pull(int *what)
{
/* All out of keys */
if (repeat__idx == repeat__cnt) return (FALSE);
/* Grab the next key, advance */
*what = repeat__key[repeat__idx++];
/* Success */
return (TRUE);
}
void repeat_check(void)
{
int what;
/* Ignore some commands */
if (command_cmd == ESCAPE) return;
if (command_cmd == ' ') return;
if (command_cmd == '\r') return;
if (command_cmd == '\n') return;
/* Repeat Last Command */
if (command_cmd == 'n')
{
/* Reset */
repeat__idx = 0;
/* Get the command */
if (repeat_pull(&what))
{
/* Save the command */
command_cmd = what;
}
}
/* Start saving new command */
else
{
/* Reset */
repeat__cnt = 0;
repeat__idx = 0;
what = command_cmd;
/* Save this command */
repeat_push(what);
}
}
#endif /* ALLOW_REPEAT -- TNB */
/*
* Read a number at a specific location on the screen
*
* Allow numbers of any size and save the last keypress.
*/
u32b get_number(u32b def, u32b max, int y, int x, char *cmd)
{
u32b res = def;
/* Player has not typed anything yet */
bool no_keys = TRUE;
/* Begin the input with default */
prt(format("%lu", def), y, x);
/* Get a command count */
while (1)
{
/* Get a new keypress */
*cmd = inkey();
/* Simple editing (delete or backspace) */
if ((*cmd == 0x7F) || (*cmd == KTRL('H')))
{
/* Override the default */
no_keys = FALSE;
/* Delete a digit */
res = res / 10;
prt(format("%lu", res), y, x);
}
/* Actual numeric data */
else if (*cmd >= '0' && *cmd <= '9')
{
/* Override the default */
if (no_keys)
{
no_keys = FALSE;
res = 0;
}
/* Don't overflow */
if (((u32b)(0 - 1) - D2I(*cmd)) / 10 < res)
{
/* Warn */
bell();
/* Limit */
res = (max + 1 == 0) ? (u32b)(0 - 1) : max;
}
/* Stop count at maximum */
else if (res * 10 + D2I(*cmd) > max)
{
/* Warn */
bell();
/* Limit */
res = max;
}
/* Increase count */
else
{
/* Incorporate that digit */
res = res * 10 + D2I(*cmd);
}
/* Show current count */
prt(format("%lu", res), y, x);
}
/* Escape cancels */
else if (*cmd == ESCAPE)
{
res = 0;
break;
}
/* Exit on "unusable" input */
else
{
break;
}
}
return res;
}
/*
* Allow the user to select multiple items without pressing '0'
*/
void get_count(int number, int max)
{
char cmd;
/* Use the default */
command_arg = number;
/* Hack -- Optional flush */
if (flush_command) flush();
/* Clear top line */
prt("", 0, 0);
/* Begin the input */
prt("How many?", 0, 0);
/* Actually get a number */
command_arg = get_number(command_arg, max, 0, 10, &cmd);
prt("", 0, 0);
}
byte count_bits(u32b array)
{
byte k = 0, i;
if (array)
for (i = 0; i < 32; i++)
if (array & (1 << i)) k++;
return k;
}
/* Return the lowered string */
void strlower(char *buf)
{
u16b i;
for (i = 0; (buf[i] != 0) && (i < 256) ;i++)
{
if (isupper(buf[i])) buf[i] = tolower(buf[i]);
}
}
/*
* Given monster name as string, return the index in r_info array. Name
* must exactly match (look out for commas and the like!), or else 0 is
* returned. Case doesn't matter. -GSN-
*/
int test_monster_name(cptr name)
{
int i;
/* Scan the monsters */
for (i = 1; i < max_r_idx; i++)
{
monster_race *r_ptr = &r_info[i];
cptr mon_name = r_name + r_ptr->name;
/* If name matches, give us the number */
if (stricmp(name, mon_name) == 0) return (i);
}
return (0);
}
int test_mego_name(cptr name)
{
int i;
/* Scan the monsters */
for (i = 1; i < max_re_idx; i++)
{
monster_ego *re_ptr = &re_info[i];
cptr mon_name = re_name + re_ptr->name;
/* If name matches, give us the number */
if (stricmp(name, mon_name) == 0) return (i);
}
return (0);
}
/*
* Given item name as string, return the index in k_info array. Name
* must exactly match (look out for commas and the like!), or else 0 is
* returned. Case doesn't matter. -DG-
*/
int test_item_name(cptr name)
{
int i;
/* Scan the items */
for (i = 1; i < max_k_idx; i++)
{
object_kind *k_ptr = &k_info[i];
cptr obj_name = k_name + k_ptr->name;
/* If name matches, give us the number */
if (stricmp(name, obj_name) == 0) return (i);
}
return (0);
}
/*
* Break scalar time
*/
s32b bst(s32b what, s32b t)
{
s32b turns = t + (10 * DAY_START);
switch (what)
{
case MINUTE:
return ((turns / 10 / MINUTE) % 60);
case HOUR:
return (turns / 10 / (HOUR) % 24);
case DAY:
return (turns / 10 / (DAY) % 365);
case YEAR:
return (turns / 10 / (YEAR));
default:
return (0);
}
}
cptr get_month_name(int day, bool full, bool compact)
{
int i = 8;
static char buf[40];
/* Find the period name */
while ((i > 0) && (day < month_day[i]))
{
i--;
}
switch (i)
{
/* Yestare/Mettare */
case 0:
case 8:
{
char buf2[20];
sprintf(buf2, get_day(day + 1));
if (full) sprintf(buf, "%s (%s day)", month_name[i], buf2);
else sprintf(buf, "%s", month_name[i]);
break;
}
/* 'Normal' months + Enderi */
default:
{
char buf2[20];
char buf3[20];
sprintf(buf2, get_day(day + 1 - month_day[i]));
sprintf(buf3, get_day(day + 1));
if (full) sprintf(buf, "%s day of %s (%s day)", buf2, month_name[i], buf3);
else if (compact) sprintf(buf, "%s day of %s", buf2, month_name[i]);
else sprintf(buf, "%s %s", buf2, month_name[i]);
break;
}
}
return (buf);
}
cptr get_day(int day)
{
static char buf[20];
cptr p = "th";
if ((day / 10) == 1) ;
else if ((day % 10) == 1) p = "st";
else if ((day % 10) == 2) p = "nd";
else if ((day % 10) == 3) p = "rd";
sprintf(buf, "%d%s", day, p);
return (buf);
}
cptr get_player_race_name(int pr, int ps)
{
static char buf[50];
if (ps)
{
if (race_mod_info[ps].place) sprintf(buf, "%s %s", race_info[pr].title + rp_name, race_mod_info[ps].title + rmp_name);
else sprintf(buf, "%s %s", race_mod_info[ps].title + rmp_name, race_info[pr].title + rp_name);
}
else
{
sprintf(buf, "%s", race_info[pr].title + rp_name);
}
return (buf);
}
#ifdef SUPPORT_GAMMA
/* Table of gamma values */
byte gamma_table[256];
/* Table of ln(x / 256) * 256 for x going from 0 -> 255 */
static const s16b gamma_helper[256] =
{
0, -1420, -1242, -1138, -1065, -1007, -961, -921, -887, -857, -830,
-806, -783, -762, -744, -726, -710, -694, -679, -666, -652, -640,
-628, -617, -606, -596, -586, -576, -567, -577, -549, -541, -532,
-525, -517, -509, -502, -495, -488, -482, -475, -469, -463, -457,
-451, -455, -439, -434, -429, -423, -418, -413, -408, -403, -398,
-394, -389, -385, -380, -376, -371, -367, -363, -359, -355, -351,
-347, -343, -339, -336, -332, -328, -325, -321, -318, -314, -311,
-308, -304, -301, -298, -295, -291, -288, -285, -282, -279, -276,
-273, -271, -268, -265, -262, -259, -257, -254, -251, -248, -246,
-243, -241, -238, -236, -233, -231, -228, -226, -223, -221, -219,
-216, -214, -212, -209, -207, -205, -203, -200, -198, -196, -194,
-192, -190, -188, -186, -184, -182, -180, -178, -176, -174, -172,
-170, -168, -166, -164, -162, -160, -158, -156, -155, -153, -151,
-149, -147, -146, -144, -142, -140, -139, -137, -135, -134, -132,
-130, -128, -127, -125, -124, -122, -120, -119, -117, -116, -114,
-112, -111, -109, -108, -106, -105, -103, -102, -100, -99, -97, -96,
-95, -93, -92, -90, -89, -87, -86, -85, -83, -82, -80, -79, -78,
-76, -75, -74, -72, -71, -70, -68, -67, -66, -65, -63, -62, -61,
-59, -58, -57, -56, -54, -53, -52, -51, -50, -48, -47, -46, -45,
-44, -42, -41, -40, -39, -38, -37, -35, -34, -33, -32, -31, -30,
-29, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16,
-14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1
};
/*
* Build the gamma table so that floating point isn't needed.
*
* Note gamma goes from 0->256. The old value of 100 is now 128.
*/
void build_gamma_table(int gamma)
{
int i, n;
/*
* value is the current sum.
* diff is the new term to add to the series.
*/
long value, diff;
/* Hack - convergence is bad in these cases. */
gamma_table[0] = 0;
gamma_table[255] = 255;
for (i = 1; i < 255; i++)
{
/*
* Initialise the Taylor series
*
* value and diff have been scaled by 256
*/
n = 1;
value = 256 * 256;
diff = ((long)gamma_helper[i]) * (gamma - 256);
while (diff)
{
value += diff;
n++;
/*
* Use the following identiy to calculate the gamma table.
* exp(x) = 1 + x + x^2/2 + x^3/(2*3) + x^4/(2*3*4) +...
*
* n is the current term number.
*
* The gamma_helper array contains a table of
* ln(x/256) * 256
* This is used because a^b = exp(b*ln(a))
*
* In this case:
* a is i / 256
* b is gamma.
*
* Note that everything is scaled by 256 for accuracy,
* plus another factor of 256 for the final result to
* be from 0-255. Thus gamma_helper[] * gamma must be
* divided by 256*256 each itteration, to get back to
* the original power series.
*/
diff = (((diff / 256) * gamma_helper[i]) * (gamma - 256)) / (256 * n);
}
/*
* Store the value in the table so that the
* floating point pow function isn't needed.
*/
gamma_table[i] = ((long)(value / 256) * i) / 256;
}
}
#endif /* SUPPORT_GAMMA */
/*
* Ask to select an item in a list
*/
int ask_menu(cptr ask, char **items, int max)
{
int ret = -1, i, start = 0;
char c;
/* Enter "icky" mode */
character_icky = TRUE;
/* Save the screen */
Term_save();
while (TRUE)
{
/* Display list */
Term_load();
Term_save();
prt(ask, 0, 0);
for (i = start; (i < max) && (i < start + 20); i++)
{
prt(format("%c) %s", I2A(i - start), items[i]), i - start + 1, 0);
}
/* Wait for user input */
c = inkey();
/* Leave the screen */
if (c == ESCAPE) break;
/* Scroll */
else if (c == '+')
{
if (start + 20 < max)
start += 20;
continue;
}
/* Scroll */
else if (c == '-')
{
start -= 20;
if (start < 0) start = 0;
continue;
}
/* Good selection */
else
{
c = tolower(c);
if (A2I(c) + start >= max)
{
bell();
continue;
}
if (A2I(c) + start < 0)
{
bell();
continue;
}
ret = A2I(c) + start;
break;
}
}
/* Load the screen */
Term_load();
/* Leave "icky" mode */
character_icky = FALSE;
return ret;
}
/*
* Determine if string "t" is a prefix of string "s"
*/
bool prefix(cptr s, cptr t)
{
/* Paranoia */
if (!s || !t)
{
if (alert_failure) message_add(MESSAGE_MSG, "prefix() called with null argument!", TERM_RED);
return FALSE;
}
/* Scan "t" */
while (*t)
{
/* Compare content and length */
if (*t++ != *s++) return (FALSE);
}
/* Matched, we have a prefix */
return (TRUE);
}
/*
* Rescale a value
*/
s32b value_scale(int value, int vmax, int max, int min)
{
s32b full_max = max - min;
value = (value * full_max) / vmax;
value += min;
return value;
}
/*
* Displays a box
*/
void draw_box(int y, int x, int h, int w)
{
int i, j;
for (i = x + 1; i < x + w; i++)
for (j = y + 1; j < y + h; j++)
Term_putch(i, j, TERM_L_BLUE, ' ');
for (i = x; i < x + w; i++)
{
c_put_str(TERM_L_BLUE, "-", y, i);
c_put_str(TERM_L_BLUE, "-", y + h, i);
}
for (i = y; i < y + h; i++)
{
c_put_str(TERM_L_BLUE, "|", i, x);
c_put_str(TERM_L_BLUE, "|", i, x + w);
}
Term_putch(x, y, TERM_L_BLUE, '/');
Term_putch(x + w, y, TERM_L_BLUE, '\\');
Term_putch(x, y + h, TERM_L_BLUE, '\\');
Term_putch(x + w, y + h, TERM_L_BLUE, '/');
}
/*
* Displays a scrollable boxed list with a selected item
*/
void display_list(int y, int x, int h, int w, cptr title, cptr *list, int max, int begin, int sel, byte sel_color)
{
int i;
draw_box(y, x, h, w);
c_put_str(TERM_L_BLUE, title, y, x + ((w - strlen(title)) / 2));
for (i = 0; i < h - 1; i++)
{
byte color = TERM_WHITE;
if (i + begin >= max) break;
if (i + begin == sel) color = sel_color;
c_put_str(color, list[i + begin], y + 1 + i, x + 1);
}
}
/*
* Creates an input box
*/
bool input_box(cptr text, int y, int x, char *buf, int max)
{
int smax = strlen(text);
if (max > smax) smax = max;
smax++;
draw_box(y - 1, x - (smax / 2), 3, smax);
c_put_str(TERM_WHITE, text, y, x - (strlen(text) / 2));
Term_gotoxy(x - (smax / 2) + 1, y + 1);
return askfor_aux(buf, max);
}
/*
* Creates a msg bbox and ask a question
*/
char msg_box(cptr text, int y, int x)
{
if (x == -1)
{
int wid = 0, hgt = 0;
Term_get_size(&wid, &hgt);
x = wid / 2;
y = hgt / 2;
}
draw_box(y - 1, x - ((strlen(text) + 1) / 2), 2, strlen(text) + 1);
c_put_str(TERM_WHITE, text, y, x - ((strlen(text) + 1) / 2) + 1);
return inkey();
}
/* Rescale a value */
s32b rescale(s32b x, s32b max, s32b new_max)
{
return (x * new_max) / max;
}
/* Nicer wrapper around TERM_XTRA_SCANSUBDIR */
void scansubdir(cptr dir)
{
strnfmt(scansubdir_dir, 1024, "%s", dir);
Term_xtra(TERM_XTRA_SCANSUBDIR, 0);
}
/*
* Timers
*/
timer_type *new_timer(cptr callback, s32b delay)
{
timer_type *t_ptr;
MAKE(t_ptr, timer_type);
t_ptr->next = gl_timers;
gl_timers = t_ptr;
t_ptr->callback = string_make(callback);
t_ptr->delay = delay;
t_ptr->countdown = delay;
t_ptr->enabled = FALSE;
return t_ptr;
}
void del_timer(timer_type *t_ptr)
{
timer_type *i, *old;
old = NULL;
for (i = gl_timers; (i != NULL) && (i != t_ptr); old = i, i = i->next)
;
if (i)
{
if (old == NULL)
gl_timers = t_ptr->next;
else
old->next = t_ptr->next;
string_free(t_ptr->callback);
FREE(t_ptr, timer_type);
}
else
cmsg_print(TERM_VIOLET, "Unknown timer!");
}
|