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
|
/* Graphics support not specific to any Xconq interface.
Copyright (C) 1992-1998 Stanley T. Shebs.
Xconq is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version. See the file COPYING. */
/* This file includes some very general graphics-related functionality
that interfaces can (but are not required to) use. For instance,
the size and shapes of hex cells have been precalculated to provide
a reasonable appearance at several magnifications. Note that some
of the algorithms in this file are abstracted from code that has been
tuned and tweaked over many years, so it is strongly recommended that
all new graphical interfaces use these. */
#include "conq.h"
#include "kpublic.h"
#include "imf.h"
#include "ui.h"
extern void generic_do_doctrine PARAMS ((Side *side, char *str));
#ifdef MAC /* temporary */
#include <Types.h>
#include <Resources.h>
#endif
extern int error_is_bug;
static void compute_q PARAMS ((void));
static void calc_view_misc PARAMS ((VP *vp));
static int blocking_utype PARAMS ((int u, int block));
static ImageFamily *get_generic_images PARAMS ((Side *side, char *name));
static ImageFamily *add_default_terrain_image PARAMS ((ImageFamily *imf, int t));
static ImageFamily *add_default_unit_image PARAMS ((ImageFamily *imf, int u));
static ImageFamily *add_default_emblem_image PARAMS ((ImageFamily *imf, int s2));
/* The two games that should be always be available. */
char *first_game_name = INTRO_GAME;
char *second_game_name = STANDARD_GAME;
/* The following magical arrays set all the sizes at each magnification. */
/* This is the basic cell size. */
short mags[NUMPOWERS] = { 1, 2, 4, 8, 16, 32, 64, 128 };
/* These give the total dimensions of each hex cell, plus the vertical
distance center-to-center. This is all carefully calculated to make
the cells overlap perfectly at each different magnification, assuming
that the icons have the right shape and size. */
short hws[NUMPOWERS] = { 1, 2, 4, 10, 24, 44, 88, 174 };
short hhs[NUMPOWERS] = { 1, 2, 4, 10, 26, 48, 96, 192 };
short hcs[NUMPOWERS] = { 1, 2, 4, 10, 20, 37, 75, 148 };
/* The sizes of the unit subcells. This is available drawing area, exact
unit icon sizes depends on what's available. */
short uhs[NUMPOWERS] = { 1, 1, 3, 8, 16, 32, 64, 128 };
short uws[NUMPOWERS] = { 1, 1, 3, 8, 16, 32, 64, 128 };
/* Widths of borders and connections (0 implies don't draw at all). */
/* Full border width. */
short bwid[NUMPOWERS] = { 0, 0, 1, 1, 3, 5, 7, 9 };
/* Half-width, for narrower inset borders. */
short bwid2[NUMPOWERS] = { 0, 0, 1, 1, 2, 3, 4, 5 };
/* Full connection width. */
short cwid[NUMPOWERS] = { 0, 0, 1, 1, 3, 5, 7, 9 };
/* Coordinates of the hex borders. */
/* Note that array has extra column so don't need to wrap index. */
short bsx[NUMPOWERS][7] = {
{ 0 },
{ 0 },
{ 2, 4, 4, 2, 0, 0, 2 },
{ 5, 10, 10, 5, 0, 0, 5 },
{ 12, 24, 24, 12, 0, 0, 12 },
{ 22, 44, 44, 22, 0, 0, 22 },
{ 44, 88, 88, 44, 0, 0, 44 },
{ 87, 174, 174, 87, 0, 0, 87 }
};
short bsy[NUMPOWERS][7] = {
{ 0 },
{ 0 },
{ 0, 0, 4, 4, 4, 0, 0 },
{ 0, 0, 10, 10, 10, 0, 0 },
{ 0, 6, 20, 26, 20, 6, 0 },
{ 0, 11, 37, 48, 37, 11, 0 },
{ 0, 21, 75, 96, 75, 21, 0 },
{ 0, 44, 148, 192, 148, 44, 0 }
};
/* Coords of middles of each hex border (half a connection, basically). */
short lsx[NUMPOWERS][6] = {
{ 0 },
{ 0 },
{ 1, 2, 1, -1, -2, -1 },
{ 2, 5, 2, -2, -5, -2 },
{ 6, 12, 6, -6, -12, -6 },
{ 11, 22, 11, -11, -22, -11 },
{ 22, 44, 22, -22, -44, -22 },
{ 44, 87, 44, -44, -87, -44 }
};
short lsy[NUMPOWERS][6] = {
{ 0 },
{ 0 },
{ -2, 0, 2, 2, 0, -2 },
{ -5, 0, 5, 5, 0, -5 },
{ -9, 0, 9, 9, 0, -9 },
{ -18, 0, 18, 18, 0, -18 },
{ -36, 0, 36, 36, 0, -36 },
{ -74, 0, 74, 74, 0, -74 }
};
short qx[NUMPOWERS][7], qy[NUMPOWERS][7];
static int q_computed;
int extracells = 3;
/* The traditional direction characters. */
char *dirchars = "ulnbhy";
/* The image family for regions that are not yet discovered. */
ImageFamily *unseen_image = NULL;
char *unitchars = NULL;
char *terrchars = NULL;
char unseen_char_1;
char unseen_char_2;
ImageFamily **recorded_imfs;
int num_recorded_imfs;
int max_recorded_imfs;
/* Machinery to find and list all the games that should be listed as
choices for the user. We don't actually scan library folders
looking for all possible game designs therein, that would pick up
experimental games and modules that are only for other modules'
use. */
Module **possible_games = NULL;
int numgames = 0;
/* The comparison function for the game list puts un-formally-named
modules at the end, plus the default sorting puts initial-lowercased
names after uppercased ones. */
static int special_strcmp PARAMS ((char *str1, char *str2));
static int
module_name_compare(a1, a2)
CONST void *a1, *a2;
{
Module *mp1, *mp2;
int rslt;
mp1 = *((Module **) a1);
mp2 = *((Module **) a2);
if (mp1->basemodulename == NULL) {
if (mp2->basemodulename == NULL) {
/* Modules must always have a non-NULL name. */
return special_strcmp(mp1->name, mp2->name);
} else {
rslt = special_strcmp(mp1->name, mp2->basemodulename);
if (rslt == 0)
rslt = -1;
return rslt;
}
} else {
if (mp2->basemodulename == NULL) {
rslt = special_strcmp(mp1->basemodulename, mp2->name);
if (rslt == 0)
rslt = 1;
return rslt;
} else {
rslt = special_strcmp(mp1->basemodulename, mp2->basemodulename);
if (rslt != 0)
return rslt;
if (mp1->title == NULL) {
if (mp2->title == NULL) {
/* Modules must always have a non-NULL name. */
return special_strcmp(mp1->name, mp2->name);
} else {
return 1;
}
} else {
if (mp2->title == NULL) {
return (-1);
} else {
return strcmp(mp1->title, mp2->title);
}
}
}
}
}
static int
special_strcmp(str1, str2)
char *str1, *str2;
{
if (strcmp(str1, second_game_name) == 0) {
if (strcmp(str2, second_game_name) == 0)
return 0;
else
return (-1);
} else {
if (strcmp(str2, second_game_name) == 0)
return 1;
else
return strcmp(str1, str2);
}
}
static int max_possible_games;
void
collect_possible_games()
{
int len, numresources;
char *modulename = NULL, *modulecontents = NULL;
Obj *lis;
Module *module, *basemodule;
FILE *fp;
int startline = 0, endline = 0;
if (numgames == 0 && numutypes == 0 /* !game_already_loaded() */) {
len = numresources = 0;
lis = lispnil;
fp = open_library_file("game.dir");
if (fp != NULL) {
lis = read_form(fp, &startline, &endline);
if (consp(lis)) {
len = length(lis);
} else {
init_warning("Game directory has bad format, no games found");
}
fclose(fp);
}
#ifdef MAC
numresources = CountResources('XCgm');
#endif /* MAC */
max_possible_games = 2 + (len + numresources) * 2;
/* Make enough room to record all the possible games. */
possible_games =
(Module **) xmalloc(max_possible_games * sizeof(Module *));
/* Collect the intro and standard game modules and put at head
of list. */
module = get_game_module(first_game_name);
add_to_possible_games(module);
module = get_game_module(second_game_name);
add_to_possible_games(module);
/* Pick up game modules that are specified as resources. */
for (; lis != lispnil; lis = cdr(lis)) {
if (!(symbolp(car(lis)) || stringp(car(lis)))) {
init_warning("Bad name in game dir list, ignoring");
continue;
}
modulename = c_string(car(lis));
if (modulename != NULL) {
module = get_game_module(modulename);
module->contents = modulecontents;
add_to_possible_games(module);
if (module->basemodulename != NULL) {
basemodule = get_game_module(module->basemodulename);
add_to_possible_games(basemodule);
}
}
}
#ifdef MAC
{
int i;
Handle modulehandle;
short moduleid;
ResType restype;
Str255 resname;
/* Pick up game modules that are specified as resources. */
for (i = 0; i < numresources; ++i) {
modulehandle = GetIndResource('XCgm', i + 1);
/* (should test for resource validity?) */
if (0 /* size > 0 */) {
/* set modulecontents from resource */
modulecontents = NULL;
}
/* Try to pick up module name from its resource name, otherwise
assume its name in its content. */
GetResInfo(modulehandle, &moduleid, &restype, resname);
if (resname[0] > 0) {
resname[resname[0]+1] = '\0';
modulename = copy_string((char *) resname+1);
} else {
modulename = NULL;
}
if (modulename != NULL) {
module = get_game_module(modulename);
module->contents = modulecontents;
add_to_possible_games(module);
if (module->basemodulename != NULL) {
basemodule = get_game_module(module->basemodulename);
add_to_possible_games(basemodule);
}
}
}
}
#endif /* MAC */
if (numgames > 1) {
/* Sort all but the first game into alphabetical order
by displayed name. */
qsort(&(possible_games[1]), numgames - 1, sizeof(Module *),
module_name_compare);
}
}
}
/* Load a game's description and add it to the list of games. */
void
add_to_possible_games(module)
Module *module;
{
int i;
if (module != NULL) {
if (load_game_description(module)) {
/* It might be that the module description supplies the real name,
and that the module already exists. (work on this) */
/* Don't add duplicate modules. */
for (i = 0; i < numgames; ++i) {
if (possible_games[i] == module)
return;
}
if (numgames < max_possible_games) {
possible_games[numgames++] = module;
}
}
}
}
/* Choose and return a reasonable location for map displays to start out
centered on. */
void
pick_a_focus(side, xp, yp)
Side *side;
int *xp, *yp;
{
int tmpx, tmpy, dist, closest = area.maxdim;
Unit *unit, *closestunit = NULL;
/* Explicit setting overrides any guesses. */
if (in_area(side->init_center_x, side->init_center_y)) {
*xp = side->init_center_x; *yp = side->init_center_y;
return;
}
if (side->startx < 0 || side->starty < 0)
calc_start_xy(side);
if (side->startx < 0 || side->starty < 0) {
*xp = area.width / 2 - area.height / 4; *yp = area.halfheight;
} else {
tmpx = side->startx; tmpy = side->starty;
/* Rescan the units to find a closest one. */
for_all_side_units(side, unit) {
if (in_play(unit)) {
/* If already got one right there, just return. */
if (unit->x == tmpx && unit->y == tmpy) {
*xp = tmpx; *yp = tmpy;
return;
} else {
dist = distance(unit->x, unit->y, tmpx, tmpy);
if (dist < closest) {
closest = dist;
closestunit = unit;
}
}
}
}
if (closestunit != NULL) {
/* Return the position of the unit closest to the avg position. */
*xp = closestunit->x; *yp = closestunit->y;
} else {
*xp = tmpx; *yp = tmpy;
}
}
}
int
num_active_displays()
{
int n = 0;
Side *side;
for_all_sides(side) {
if (active_display(side))
++n;
}
return n;
}
/* Collect a command name and an argument string from the given
string, discarding excess whitespace. */
void
parse_long_name_command(cmdstr, prefixp, namep, argp, buf)
char *cmdstr, **namep, **argp, *buf;
int *prefixp;
{
int j, prefixarg = -1;
char *cmdname, *cmdarg;
if (empty_string(cmdstr)) {
*namep = *argp = NULL;
return;
}
strcpy(buf, cmdstr);
/* Look for the first nonwhite char, make it start of command name. */
cmdname = buf;
while ((*cmdname == ' ' || *cmdname == '\t') && *cmdname != '\0')
++cmdname;
/* If this is a digit, then it's a prefix arg; extract it. */
if (isdigit(*cmdname)) {
prefixarg = *cmdname - '0';
++cmdname;
while (isdigit(*cmdname)) {
prefixarg = prefixarg * 10 + (*cmdname - '0');
++cmdname;
}
/* Skip over any space between prefix arg and cmdname. */
while ((*cmdname == ' ' || *cmdname == '\t') && *cmdname != '\0')
++cmdname;
}
/* Scan over command name, which is delimited by whitespace or end of line. */
for (j = 0; cmdname[j] != ' ' && cmdname[j] != '\t' && cmdname[j] != '\0'; ++j)
;
/* If there's more than just the command name, extract an argument. */
if (cmdname[j] != '\0') {
cmdarg = cmdname + j + 1;
while ((*cmdarg == ' ' || *cmdname == '\t') && *cmdarg != '\0')
++cmdarg;
if (*cmdarg == '\0')
cmdarg = NULL;
} else {
cmdarg = NULL;
}
/* Terminate the command name. */
cmdname[j] = '\0';
/* Remove trailing whitespace from the argument. */
if (!empty_string(cmdarg)) {
for (j = strlen(cmdarg) - 1; j >= 0 && (cmdarg[j] == ' ' || cmdarg[j] == '\t'); --j)
;
cmdarg[j + 1] = '\0';
}
DGprintf("Command is \"%s\", argument is \"%s\", prefix is %d\n",
(cmdname != NULL ? cmdname : "<null>"),
(cmdarg != NULL ? cmdarg : "<null>"),
prefixarg);
*prefixp = prefixarg;
*namep = cmdname;
*argp = cmdarg;
}
/* Compute positions at each hex corner, slightly inset. */
static void
compute_q()
{
int d, p, w;
for (p = 0; p < NUMPOWERS; ++p) {
if (p < 2)
continue;
w = bwid[p] + 1;
for_all_directions(d) {
qx[p][d] = bsx[p][d] + ((hws[p] - 2 * bsx[p][d]) * w) / (2 * mags[p]);
qy[p][d] = bsy[p][d] + ((hhs[p] - 2 * bsy[p][d]) * w) / (2 * mags[p]);
}
qx[p][NUMDIRS] = qx[p][0];
qy[p][NUMDIRS] = qy[p][0];
}
}
/* Viewport handling. */
VP *
new_vp()
{
int t, thickest;
VP *vp;
if (!q_computed) {
compute_q();
q_computed = TRUE;
}
vp = (VP *) xmalloc(sizeof(VP));
/* View at a 90 degree angle by default. */
vp->angle = 90;
/* No vertical exaggeration by default. */
vp->vertscale = 1;
vp->cellwidth = area.cellwidth;
/* If the cellwidth is not reasonable for drawing elevations, use an
approximation based on the range of elevations and terrain thicknesses. */
if (vp->cellwidth <= 1) {
thickest = 0;
for_all_terrain_types(t) {
if (t_thickness(t) > thickest)
thickest = t_thickness(t);
}
vp->cellwidth = ((area.maxelev + thickest) - area.avgelev) / 2;
}
if (vp->cellwidth < 1)
vp->cellwidth = 1;
return vp;
}
/* Given a viewport and a cell, figure out where its UL corner will be. */
void
xform_cell(vp, x, y, sxp, syp)
VP *vp;
int x, y, *sxp, *syp;
{
int xnw, xw, elev, offset;
if (in_area(x, y)) {
if (vp->wide_viewport) {
/* This version "unwraps" x, currently appropriate for Mac interface. */
xnw = x;
if (area.xwrap
&& between(area.width - y / 2, xnw, area.width - 1)
&& vp->sx < (vp->totsh - vp->sy) / 2
)
xnw -= area.width;
*sxp = xnw * vp->hw + (y * vp->hw) / 2 - vp->sx;
} else {
/* This calculation is currently appropriate for the Unix interface. */
/* Compute the scaled x. */
*sxp = x * vp->hw + (y * vp->hw) / 2 - vp->sx;
/* If the world is cylindrical, then we want to come up with a
transformed point that is in the viewport if possible;
either add or subtract the scaled width of the world if
that will help. */
if (area.xwrap) {
if (*sxp < - vp->hw
&& between(0, *sxp + (area.width * vp->hw), vp->pxw))
*sxp += area.width * vp->hw;
if (*sxp > vp->pxw
&& between(0, *sxp - (area.width * vp->hw), vp->pxw))
*sxp -= area.width * vp->hw;
}
}
/* Compute the scaled y. */
*syp = (vp->totsh - (vp->hh + y * vp->hch)) - vp->sy;
if (vp->angle != 90 /* && vp->draw_elevations */) {
/* Wrapping should not be necessary, should find out why this
fixes probs. */
xw = wrapx(x);
elev = (elevations_defined() ? elev_at(xw, y) : 0) - area.avgelev;
/* We see the top of the terrain in the cell, not the bottom. */
elev += t_thickness(terrain_at(xw, y));
/* Exaggerate the vertical scale if requested. */
elev *= vp->vertscale;
offset = (elev * vp->hh) / vp->cellwidth;
*syp -= offset;
}
} else {
/* Always die on this, indicates bugs that must be fixed. */
error_is_bug = TRUE;
run_error("attempting to xform %d,%d", x, y);
}
}
/* Similarly, but allowing 1/1000ths of cells as input. Note that
since .001 is at the "bottom" of the cell as it appears on the
screen, we must subtract the fraction from 1000, similarly to
how we do the main y value. */
void
xform_cell_fractional(vp, x, y, xf, yf, sxp, syp)
VP *vp;
int x, y, xf, yf, *sxp, *syp;
{
xform_cell(vp, x, y, sxp, syp);
*sxp += (xf * vp->hw) / 1000; *syp += ((1000 - yf) * vp->hch) / 1000;
}
void
xform_unit(vp, unit, sxp, syp, swp, shp)
VP *vp;
Unit *unit;
int *sxp, *syp, *swp, *shp;
{
int num = 0, n = -1, sq, sx, sy, sx1, sy1, sw1, sh1;
int x = unit->x, y = unit->y;
Unit *unit2;
if (unit->transport == NULL) {
xform_cell(vp, x, y, &sx, &sy);
/* Adjust to the unit box within the cell. */
sx += (vp->hw - vp->uw) / 2; sy += (vp->hh - vp->uh) / 2;
/* Figure out our position in this cell's stack. */
for_all_stack(x, y, unit2) {
/* (should only count units visible to a given side) */
if (unit == unit2)
n = num;
++num;
}
if (n < 0) {
run_warning("xform_unit weirdness with %s", unit_desig(unit));
*sxp = *syp = 0;
*swp = *shp = 1;
return;
}
if (num <= 1) {
sq = 1;
} else if (num <= 4) {
sq = 2;
} else if (num <= 16) {
sq = 4;
} else if (num <= 256) {
sq = 8;
} else {
/* This is room for 65,536 units in a stack. */
sq = 16;
}
*swp = vp->uw / sq; *shp = vp->uh / sq;
*sxp = sx + *swp * (n / sq); *syp = sy + *shp * (n % sq);
} else {
/* Go up the transport chain to get the bounds for this unit. */
xform_unit(vp, unit->transport, &sx1, &sy1, &sw1, &sh1);
xform_occupant(vp, unit->transport, unit, sx1, sy1, sw1, sh1, sxp, syp, swp, shp);
}
}
void
xform_unit_self(vp, unit, sxp, syp, swp, shp)
VP *vp;
Unit *unit;
int *sxp, *syp, *swp, *shp;
{
int sx1, sy1, sw1, sh1;
if (unit->transport == NULL) {
if (unit->occupant == NULL) {
xform_unit(vp, unit, sxp, syp, swp, shp);
} else {
xform_unit(vp, unit, &sx1, &sy1, &sw1, &sh1);
xform_occupant(vp, unit, unit, sx1, sy1, sw1, sh1, sxp, syp, swp, shp);
}
} else {
xform_unit(vp, unit->transport, &sx1, &sy1, &sw1, &sh1);
xform_occupant(vp, unit->transport, unit, sx1, sy1, sw1, sh1, sxp, syp, swp, shp);
}
}
void
xform_occupant(vp, transport, unit, sx, sy, sw, sh, sxp, syp, swp, shp)
VP *vp;
Unit *transport, *unit;
int sx, sy, sw, sh, *sxp, *syp, *swp, *shp;
{
int num = 0, n = -1, nmx, nmy;
Unit *unit2;
/* Figure out the position of this unit amongst all the occupants. */
for_all_occupants(transport, unit2) {
if (unit2 == unit)
n = num;
++num;
}
if (unit == transport) {
if (num > 0) {
/* Transport image shrinks by half in each dimension. */
*swp = sw / 2; *shp = sh / 2;
}
/* Transport is always in the UL corner. */
*sxp = sx; *syp = sy;
} else {
if (n < 0)
run_error("xform_occupant weirdness");
/* Compute how the half-box will be subdivided. Only use
powers of two, so image scaling works better. */
if (num <= 2) {
nmx = 2;
} else if (num <= 8) {
nmx = 4;
} else if (num <= 128) {
nmx = 8;
} else {
/* This is room for 32,768 units in a stack. */
nmx = 16;
}
nmy = nmx / 2;
*swp = sw / nmx; *shp = (sh / 2) / nmy;
*sxp = sx + *swp * (n / nmy); *syp = sy + sh / 2 + *shp * (n % nmy);
}
}
/* Scale one viewport box to its position in another. */
void
scale_vp(vp, vp2, sxp, syp, swp, shp)
VP *vp, *vp2;
int *sxp, *syp, *swp, *shp;
{
*sxp = (vp2->sx * vp->hw) / vp2->hw - vp->sx;
*syp = (vp2->sy * vp->hch) / vp2->hch - vp->sy;
*swp = (vp2->pxw * vp->hw) / vp2->hw;
*shp = (vp2->pxh * vp->hch) / vp2->hch;
}
int
nearest_cell(vp, sx, sy, xp, yp, xfp, yfp)
VP *vp;
int sx, sy, *xp, *yp, *xfp, *yfp;
{
int sxadj, sxfrac, syflipped, syfrac;
/* Flip the raw y and then scale to hex coords. */
syflipped = vp->totsh - (vp->sy + sy);
*yp = syflipped / vp->hch;
if (yfp) {
syfrac = syflipped - (*yp * vp->hch) - (vp->hh - vp->hch);
*yfp = (syfrac * 1000) / vp->hch;
}
/* Adjust scaled x. */
sxadj = (sx + vp->sx - (*yp * vp->hw) / 2);
/* The division by hw below might round towards 0, so wrap negative numbers
around to positive values. This should only ever happens for cylinder
areas, but doesn't hurt to just adjust all negative values. */
if (sxadj < 0)
sxadj += (2 * area.width * vp->hw);
*xp = sxadj / vp->hw;
if (xfp) {
sxfrac = sxadj - (*xp * vp->hw);
*xfp = (sxfrac * 1000) / vp->hw;
}
/* If the magnification of the map is large enough that the top and bottom
edges of a hex are visibly sloping, then we have to take those edges
int account, and accurately. */
if ((vp->hh - vp->hch) / 2 > 1) {
/* (should adjust according to hex boundaries correctly here) */
}
/* Wrap coords as usual. */
if (area.xwrap)
*xp = wrapx(*xp);
DGprintf("Pixel %d,%d -> hex %d.%03d,%d.%03d\n", sx, sy, *xp, *xfp, *yp, *yfp);
return (in_area(*xp, *yp));
}
int
nearest_boundary(vp, sx, sy, xp, yp, dirp)
VP *vp;
int sx, sy, *xp, *yp, *dirp;
{
int sx2, sy2, ydelta, hexslope;
/* Get the nearest cell... */
if (nearest_cell(vp, sx, sy, xp, yp, NULL, NULL)) {
/* ... and xform it back to get the pixel coords. */
xform_cell(vp, *xp, *yp, &sx2, &sy2);
ydelta = sy - sy2;
hexslope = (vp->hh - vp->hch) / 2;
if (sx - sx2 > vp->hw / 2) {
*dirp = ((ydelta < hexslope) ? NORTHEAST : (ydelta > vp->hch ? SOUTHEAST : EAST));
} else {
*dirp = ((ydelta < hexslope) ? NORTHWEST : (ydelta > vp->hch ? SOUTHWEST : WEST));
}
DGprintf("Pixel %d,%d -> hex %d,%d dir %d\n", sx, sy, *xp, *yp, *dirp);
return TRUE;
} else {
return FALSE;
}
}
Unit *
find_unit_or_occ(vp, unit, usx, usy, usw, ush, sx, sy)
VP *vp;
Unit *unit;
int usx, usy, usw, ush, sx, sy;
{
int usx1, usy1, usw1, ush1;
Unit *occ, *rslt;
/* See if the point might be over an occupant. */
if (unit->occupant != NULL) {
for_all_occupants(unit, occ) {
xform_unit(vp, occ, &usx1, &usy1, &usw1, &ush1);
rslt = find_unit_or_occ(vp, occ, usx1, usy1, usw1, ush1, sx, sy);
if (rslt)
return rslt;
}
}
/* Otherwise see if it could be the unit itself. This has the effect of
"giving" the transport everything in its box that is not in an occ. */
xform_unit(vp, unit, &usx1, &usy1, &usw1, &ush1);
if (between(usx1, sx, usx1 + usw1) && between(usy1, sy, usy1 + ush1))
return unit;
return NULL;
}
Unit *
find_unit_at(vp, x, y, sx, sy)
VP *vp;
int x, y, sx, sy;
{
int usx, usy, usw, ush;
Unit *unit, *rslt;
for_all_stack(x, y, unit) {
xform_unit(vp, unit, &usx, &usy, &usw, &ush);
rslt = find_unit_or_occ(vp, unit, usx, usy, usw, ush, sx, sy);
if (rslt)
return rslt;
}
return NULL;
}
int
nearest_unit(vp, sx, sy, unitp)
VP *vp;
int sx, sy;
Unit **unitp;
{
int x, y;
if (!nearest_cell(vp, sx, sy, &x, &y, NULL, NULL)) {
*unitp = NULL;
DGprintf("Pixel %d,%d -> outside area\n", sx, sy);
return FALSE;
}
if (vp->power > 4) {
*unitp = find_unit_at(vp, x, y, sx, sy);
} else {
*unitp = unit_at(x, y);
}
DGprintf("Pixel %d,%d -> unit %s\n", sx, sy, unit_desig(*unitp));
return TRUE;
}
int
cell_is_visible(vp, x, y)
VP *vp;
int x, y;
{
int sx, sy;
if (!in_area(x, y))
return FALSE;
xform_cell(vp, x, y, &sx, &sy);
if (area.xwrap && sx > vp->totsw)
sx -= vp->totsw;
if (sx + vp->hw < 0)
return FALSE;
if (sx > vp->pxw)
return FALSE;
if (sy + vp->hh < 0)
return FALSE;
if (sy > vp->pxh)
return FALSE;
return TRUE;
}
/* Decide whether given location is away from the edge of the map's window. */
int
cell_is_in_middle(vp, x, y)
VP *vp;
int x, y;
{
int sx, sy, insetx1, insety1, insetx2, insety2;
if (!in_area(x, y))
return FALSE;
xform_cell(vp, x, y, &sx, &sy);
/* Adjust to be the center of the cell, more reasonable if large. */
sx += vp->hw / 2; sy += vp->hh / 2;
insetx1 = min(vp->pxw / 4, 1 * vp->hw);
insety1 = min(vp->pxh / 4, 1 * vp->hch);
insetx2 = min(vp->pxw / 4, 2 * vp->hw);
insety2 = min(vp->pxh / 4, 2 * vp->hch);
if (sx < insetx2)
return FALSE;
if (sx > vp->pxw - insetx2)
return FALSE;
if (sy < (between(2, y, area.height-3) ? insety2 : insety1))
return FALSE;
if (sy > vp->pxh - (between(2, y, area.height-3) ? insety2 : insety1))
return FALSE;
return TRUE;
}
/* Set vcx/vcy to point to the center of the view. */
void
focus_on_center(vp)
VP *vp;
{
vp->vcy = (vp->totsh - (vp->sy + vp->pxh / 2)) / vp->hch;
vp->vcx = vp->sx / vp->hw - (vp->vcy / 2) + (vp->pxw / vp->hch) / 2;
/* Restrict the focus to be *inside* the area. */
vp->vcy = limitn(1, vp->vcy, area.height - 2);
if (area.xwrap) {
vp->vcx = wrapx(vp->vcx);
} else {
vp->vcx = limitn(1, vp->vcx, area.width - 2);
if (vp->vcx + vp->vcy < area.halfheight + 1)
vp->vcx = area.halfheight + 1;
if (vp->vcx + vp->vcy > area.width + area.halfheight - 1)
vp->vcx = area.width + area.halfheight - 1;
}
}
void
center_on_focus(vp)
VP *vp;
{
int sx, sy, xnw = vp->vcx;
if (area.xwrap && xnw >= (area.width - vp->vcy / 2))
xnw -= area.width;
/* Scale, add hex offset adjustment, translate to get left edge. */
sx = xnw * vp->hw + (vp->vcy * vp->hw) / 2 - vp->pxw / 2 + vp->hw / 2;
/* Scale, translate to top edge, flip. */
sy = vp->totsh - (vp->vcy * vp->hch + vp->pxh / 2 + vp->hh / 2);
set_view_position(vp, sx, sy);
DGprintf("View at %d,%d, focused at %d,%d\n",
vp->sx, vp->sy, vp->vcx, vp->vcy);
}
int
set_view_size(vp, w, h)
VP *vp;
int w, h;
{
if (w < 1 || h < 1)
run_error("Bad viewport size %dx%d", w, h);
vp->pxw = w; vp->pxh = h;
calc_view_misc(vp);
return TRUE;
}
int
set_view_position(vp, sx, sy)
VP *vp;
int sx, sy;
{
vp->sx = sx; vp->sy = sy;
/* Clip to rational limits. */
vp->sx = limitn(vp->sxmin, vp->sx, vp->sxmax);
vp->sy = limitn(vp->symin, vp->sy, vp->symax);
return TRUE;
}
/* Given a magnification power, calculate and cache the sizes within a cell,
and the scaled size in pixels of the entire world. */
int
set_view_power(vp, power)
VP *vp;
int power;
{
vp->power = power;
vp->mag = mags[power]; /* is this used?? */
vp->hw = hws[power]; vp->hh = hhs[power];
vp->hch = hcs[power];
vp->uw = uws[power]; vp->uh = uhs[power];
if (vp->angle == 30) {
vp->hh /= 2;
vp->hch /= 2;
} else if (vp->angle == 15) {
vp->hh /= 4;
vp->hch /= 4;
}
calc_view_misc(vp);
DGprintf("Power is now %d, total scaled area is %d x %d\n",
vp->power, vp->totsw, vp->totsh);
return TRUE;
}
int
set_view_focus(vp, x, y)
VP *vp;
int x, y;
{
if (!in_area(x, y))
run_error("View focus of %d,%d not in area", x, y);
vp->vcx = x; vp->vcy = y;
return TRUE;
}
int
set_view_angle(vp, angle)
VP *vp;
int angle;
{
if (!(angle == 90 || angle == 30 || angle == 15)) {
run_warning("Bad angle %d, setting to 90", angle);
angle = 90;
}
vp->angle = angle;
vp->hh = hhs[vp->power];
vp->hch = hcs[vp->power];
vp->uh = uhs[vp->power];
if (vp->angle == 30) {
vp->hh /= 2;
vp->hch /= 2;
vp->uh /= 2;
} else if (vp->angle == 15) {
vp->hh /= 4;
vp->hch /= 4;
vp->uh /= 4;
}
calc_view_misc(vp);
DGprintf("Angle is now %d, total scaled area is %d x %d\n",
vp->angle, vp->totsw, vp->totsh);
return TRUE;
}
int
set_view_direction(vp, dir)
VP *vp;
int dir;
{
return TRUE;
}
static void
calc_view_misc(vp)
VP *vp;
{
/* Calculate and cache the width in pixels of the whole area, adding an
an adjustment to account for the "bulge" of hexagon-shaped areas. */
vp->totsw = area.width * vp->hw + hexagon_adjust(vp);
if (area.xwrap && (vp->totsw > (vp->pxw - vp->hw)))
vp->totsw += (vp->pxw - vp->hw);
/* Total scaled height is based on center-to-center height, plus an adjustment
to include the bottom parts of the bottom row. */
vp->totsh = area.height * vp->hch + (vp->hh - vp->hch);
vp->sxmin = hexagon_adjust(vp); vp->symin = 0;
/* Special-case wide viewports (a la Mac) for cylindrical worlds. */
if (vp->wide_viewport && area.xwrap)
vp->sxmax = area.width * vp->hw;
else
vp->sxmax = max(vp->sxmin, vp->totsw - vp->pxw);
vp->symax = max(vp->symin, vp->totsh - vp->pxh);
vp->sx = limitn(vp->sxmin, vp->sx, vp->sxmax);
vp->sy = limitn(vp->symin, vp->sy, vp->symax);
}
void
free_vp(vp)
VP *vp;
{
free(vp);
}
void
compute_fire_line_segment(sx1, sy1, sx2, sy2, i, n, xx, yy, dx, dy)
int sx1, sy1, sx2, sy2, i, n, *xx, *yy, *dx, *dy;
{
/* Position one segment of a line between the locations. */
*dx = (sx2 - sx1) / n; *dy = (sy2 - sy1) / n;
*xx = sx1 + ((i / 2) % n) * *dx; *yy = sy1 + ((i / 2) % n) * *dy;
}
/* This routine can be used by the interface to place legends */
/* orient==0 : E (horizontal) only;
orient==1 : E, SE, NE;
orient==2 : E, SE, NE, ESE, ENE, N; */
/* block==0 : write over any unit;
block==1 : don't write over "city-like" units;
block==2 : don't write over visible units. */
void
place_feature_legends(leg, nf, side, orient, block)
Legend *leg;
int nf;
Side *side;
int orient, block;
{
int x, y, x1, y1, dx, dy, f, i, i3, id, d, nd, d1, dc;
double dist;
static int ndt[] = { 1, 3, 6 },
dt[] = { EAST, SOUTHEAST, NORTHEAST, NORTHEAST, SOUTHEAST, EAST },
da[] = { 0, -60, 60, 90, -30, 30 };
unsigned char *auxf_layer, dmask;
if (!features_defined())
return;
orient = min(orient, 2);
nd = ndt[orient];
for (f = 1; f <= nf; f++) {
leg[f-1].ox = 0;
leg[f-1].oy = 0;
leg[f-1].dx = 0;
leg[f-1].dy = 0;
leg[f-1].angle = 0;
leg[f-1].dist = -1;
}
/* Speedup: in auxf_layer we keep this information:
the cell is unseen or hosts a blocking unit (bit 7);
the cell has already been reached from direction id (bit id)
[this avoids repeating the same path over and over;
note that directions 3,4,5 zig-zag with step 3,
so this bit is set/checked only every 3 steps.] */
auxf_layer = (unsigned char *)
malloc(area.height * area.width * sizeof(unsigned char));
if (!auxf_layer)
return;
for_all_cells(x, y) {
if (terrain_seen_at(side, x, y) == NONTTYPE ||
blocking_utype(utype_seen_at(side, x, y), block)) {
aset(auxf_layer, x, y, '\200');
} else {
aset(auxf_layer, x, y, '\0');
}
}
for_all_cells(x, y) {
f = raw_feature_at(x, y);
if (f < 1 || f > nf)
continue;
for (id = 0; id < nd; id++) {
dmask = '\001' << id;
d = dt[id];
d1 = ((id < 3) ? d : left_dir(left_dir(d)));
x1 = x; y1 = y;
dx = dy = 0;
i3 = i = 0;
dist = 0;
while (raw_feature_at(x1, y1) == f &&
!(aref(auxf_layer, x1, y1) &
((id < 3 || !i3) ? ('\200' | dmask) : '\200'))) {
if (dist > leg[f-1].dist && (id < 3 || !i3)) {
leg[f-1].ox = x; leg[f-1].oy = y;
leg[f-1].dx = dx; leg[f-1].dy = dy;
leg[f-1].angle = da[id];
leg[f-1].dist = dist;
}
if (id < 3 || !i3) {
auxf_layer[area.width * y1 + x1] |= dmask;
}
dc = ((i3 == 1) ? d1 : d);
dx += dirx[dc];
x1 = wrapx(x1 + dirx[dc]);
dy += diry[dc];
y1 += diry[dc];
dist += ((id < 3) ? 1.0 : (i3 ? 0.5 * 1.73205080756888 : 0.0));
++i;
i3 = i % 3;
}
}
}
free(auxf_layer);
}
static int
blocking_utype(u, block)
int u, block;
{
if (u == NONUTYPE || block == 0)
return 0;
if (block > 1)
return 1;
/* block==1: only visible see-always unmovable units */
return ((u_already_seen(u) > 99 || u_see_always(u)) && !mobile(u));
}
void
plot_meridians(vp, line_callback, text_callback)
VP *vp;
void (*line_callback) PARAMS ((int x1, int y1, int x1f, int y1f, int x2, int y2, int x2f, int y2f));
void (*text_callback) PARAMS ((int x1, int y1, int x1f, int y1f, char *str));
{
int xxmin, ymin, xmax, ymax;
int lat1, lon1, lat2, lon2, latmin, latmax, lonmin, lonmax, incr, lat, lon;
int x1, y1, x2, y2, x1f, y1f, x2f, y2f;
int sx1, sy1, sx2, sy2;
incr = vp->latlong_interval;
/* Draw only if the interval is not too small. */
xy_to_latlong(1, area.height - 2, 0, 0, &lat, &lon);
latlong_to_xy(lat, lon, &x1, &y1, &x1f, &y1f);
latlong_to_xy(lat - incr, lon, &x2, &y2, &x2f, &y2f);
/* If a single interval down from the NW corner is off the map, then there
won't be any lines to plot anyway, so OK to escape. */
if (!in_area(x2, y2))
return;
xform_cell_fractional(vp, x1, y1, x1f, y1f, &sx1, &sy1);
xform_cell_fractional(vp, x2, y2, x2f, y2f, &sx2, &sy2);
if (sy2 - sy1 < 20)
return;
if (nearest_cell(vp, 0, vp->pxh, &xxmin, &ymin, NULL, NULL)
&& nearest_cell(vp, vp->pxw, 0, &xmax, &ymax, NULL, NULL)) {
xy_to_latlong(xxmin, ymin, 0, 0, &lat1, &lon1);
xy_to_latlong(xmax, ymax, 0, 0, &lat2, &lon2);
} else if (area.width < world.circumference) {
xxmin = 0; ymin = 0;
xmax = area.width - 1; ymax = area.height - 1;
xy_to_latlong(xxmin, ymin, 0, 0, &lat1, &lon1);
xy_to_latlong(xmax, ymax, 0, 0, &lat2, &lon2);
} else {
lat1 = -75 * 60 + incr - 1; lon1 = -180 * 60 + incr - 1;
lat2 = 75 * 60 - (incr - 1); lon2 = 180 * 60 - (incr - 1);
}
latmin = (lat1 / incr) * incr;
latmax = (lat2 / incr) * incr + incr;
lonmin = (lon1 / incr) * incr;
lonmax = (lon2 / incr) * incr + incr;
if (lonmax <= lonmin)
lonmax += 21600;
for (lat = latmax; lat >= latmin; lat -= incr) {
for (lon = lonmin; lon <= lonmax; lon += incr) {
latlong_to_xy(lat, lon, &x1, &y1, &x1f, &y1f);
if (in_area(x1, y1)) {
latlong_to_xy(lat - incr, lon, &x2, &y2, &x2f, &y2f);
if (line_callback) {
if (in_area(x2, y2)) {
(*line_callback)(x1, y1, x1f, y1f, x2, y2, x2f, y2f);
}
latlong_to_xy(lat, lon + incr, &x2, &y2, &x2f, &y2f);
if (in_area(x2, y2)) {
(*line_callback)(x1, y1, x1f, y1f, x2, y2, x2f, y2f);
}
}
if (text_callback) {
if (lon == lonmin || lon == lonmax) {
char minbuf[10];
int latdeg, latminu;
latdeg = abs(lat) / 60;
latminu = abs(lat) % 60;
minbuf[0] = '\0';
if (latminu != 0)
sprintf(minbuf, "%dm", latminu);
sprintf(tmpbuf, "%dd%s%c",
latdeg, minbuf, (lat >= 0 ? 'N' : 'S'));
(*text_callback)(x1, y1, x1f, y1f, tmpbuf);
}
if (lat == latmin || lat == latmax) {
char minbuf[10];
int londeg, lonminu;
londeg = abs(lon) / 60;
lonminu = abs(lon) % 60;
minbuf[0] = '\0';
if (lonminu != 0)
sprintf(minbuf, "%dm", lonminu);
sprintf(tmpbuf, "%dm%s%c",
londeg, minbuf, (lon >= 0 ? 'E' : 'W'));
(*text_callback)(x1, y1, x1f, y1f, tmpbuf);
}
}
#if 0 /* use to debug meridian plotting */
sprintf(tmpbuf, "%d %d", lat, lon);
(*text_callback)(x1, y1, x1f, y1f, tmpbuf);
latlong_desc(tmpbuf, x1, y1, x1f, y1f, 3);
(*text_callback)(x1, y1, x1f, y1f, tmpbuf);
#endif
}
}
}
}
/* The theory of contour lines is that each hex can be considered as
six triangles, each of which has a vertex at the center and two
on adjacent corners of the hex. The elevation of the center vertex
is the overall elevation of the cell, the elevations of the corners
are averages with the adjacent cells. If a particular contour
elevation is between any pair of vertex elevations, then the contour
line must cross that side of the triangle - and one of the other two
sides. We decide which of the two it is, interpolate to get the
actual positions of each endpoint of the line segment, then draw it. */
int num_contours;
int contour_interval = -1;
LineSegment *tmplines;
void
init_contour_lines()
{
/* (should be user-settable map parm) */
num_contours = max(1, min(20, area.maxelev - area.minelev));
contour_interval = (area.maxelev - area.minelev) / num_contours;
}
void
contour_lines_at(vp, x, y, sx, sy, lines, numlinesp)
VP *vp;
int x, y, sx, sy, *numlinesp;
LineSegment **lines;
{
int el, dir, x1, y1, sum, n, lowest, liq0, liq, ecor[NUMDIRS], ec;
int sxcor[NUMDIRS], sycor[NUMDIRS], sxc, syc;
int power = vp->power;
int ecorr, ecorl, sxcorr, sycorr, sxcorl, sycorl;
int sx1, sy1, sx2, sy2;
if (1 /* contour_interval < 0 should always calculate, in case min/max changed */) {
/* (should be user-settable map parm) */
/* (should depend on magnification) */
num_contours = max(1, min(20, area.maxelev - area.minelev));
contour_interval = (area.maxelev - area.minelev) / num_contours;
}
*numlinesp = 0;
if (contour_interval < 1)
return;
if (tmplines == NULL)
tmplines = (LineSegment *) xmalloc ((num_contours + 2) * 2 * NUMDIRS * sizeof(LineSegment));
*lines = tmplines;
el = elev_at(x, y);
sxc = sx + vp->hw / 2; syc = sy + vp->hh / 2;
/* Compute the elevation at each corner of the cell. */
liq0 = t_liquid(terrain_at(x, y));
for_all_directions(dir) {
sum = el;
n = 1;
lowest = el;
liq = liq0;
if (point_in_dir(x, y, dir, &x1, &y1)) {
sum += elev_at(x1, y1);
++n;
lowest = min(lowest, elev_at(x1, y1));
if (t_liquid(terrain_at(x1, y1)))
liq = TRUE;
}
if (point_in_dir(x, y, left_dir(dir), &x1, &y1)) {
sum += elev_at(x1, y1);
++n;
lowest = min(lowest, elev_at(x1, y1));
if (t_liquid(terrain_at(x1, y1)))
liq = TRUE;
}
/* Pick lowest, in the case of liquids, or average. */
if (liq)
ecor[dir] = lowest;
else
ecor[dir] = sum / n;
sxcor[dir] = sx + bsx[power][dir]; sycor[dir] = sy + bsy[power][dir];
}
for (ec = area.minelev + contour_interval; ec < area.maxelev; ec += contour_interval) {
for_all_directions(dir) {
ecorr = ecor[dir];
ecorl = ecor[left_dir(dir)];
sxcorr = sxcor[dir]; sycorr = sycor[dir];
sxcorl = sxcor[left_dir(dir)]; sycorl = sycor[left_dir(dir)];
if (el != ecorr && between(min(el, ecorr), ec, max(el, ecorr))) {
if (el != ecorl
&& between(min(el, ecorl), ec, max(el, ecorl))) {
sx1 = sxc + ((sxcorr - sxc) * (ec - el)) / (ecorr - el);
sy1 = syc + ((sycorr - syc) * (ec - el)) / (ecorr - el);
sx2 = sxc + ((sxcorl - sxc) * (ec - el)) / (ecorl - el);
sy2 = syc + ((sycorl - syc) * (ec - el)) / (ecorl - el);
tmplines[*numlinesp].sx1 = sx1;
tmplines[*numlinesp].sy1 = sy1;
tmplines[*numlinesp].sx2 = sx2;
tmplines[*numlinesp].sy2 = sy2;
++(*numlinesp);
} else if (ecorl != ecorr) {
sx1 = sxc + ((sxcorr - sxc) * (ec - el)) / (ecorr - el);
sy1 = syc + ((sycorr - syc) * (ec - el)) / (ecorr - el);
/* By inverting odd directions before the calculation
we ensure that the endpoints are calculated in exactly
the same way for the two line segments from adjacent cells
that should end on the same point. This eliminates the
small jumps in contour lines as they cross from one cell
to another. */
/* Line ends on outer edge */
if (dir == NORTHEAST || dir == SOUTHEAST || dir == WEST) {
sx2 = sxcorr + ((sxcorl - sxcorr) * (ec - ecorr)) / (ecorl - ecorr);
sy2 = sycorr + ((sycorl - sycorr) * (ec - ecorr)) / (ecorl - ecorr);
} else {
sx2 = sxcorl + ((sxcorl - sxcorr) * (ec - ecorl)) / (ecorl - ecorr);
sy2 = sycorl + ((sycorl - sycorr) * (ec - ecorl)) / (ecorl - ecorr);
}
#if 0
sx2 = sxcorr + ((sxcorl - sxcorr) * (ec - ecorr))
/ (ecorl - ecorr);
sy2 = sycorr + ((sycorl - sycorr) * (ec - ecorr))
/ (ecorl - ecorr);
#endif
tmplines[*numlinesp].sx1 = sx1;
tmplines[*numlinesp].sy1 = sy1;
tmplines[*numlinesp].sx2 = sx2;
tmplines[*numlinesp].sy2 = sy2;
++(*numlinesp);
}
}
if (el != ecorl && between(min(el, ecorl), ec, max(el, ecorl))) {
if (ecorl != ecorr
&& between(min(ecorr, ecorl), ec, max(ecorr, ecorl))) {
sx1 = sxc + ((sxcorl - sxc) * (ec - el)) / (ecorl - el);
sy1 = syc + ((sycorl - syc) * (ec - el)) / (ecorl - el);
/* Line ends on outer edge */
if (dir == NORTHEAST || dir == SOUTHEAST || dir == WEST) {
sx2 = sxcorr + ((sxcorl - sxcorr) * (ec - ecorr)) / (ecorl - ecorr);
sy2 = sycorr + ((sycorl - sycorr) * (ec - ecorr)) / (ecorl - ecorr);
} else {
sx2 = sxcorl + ((sxcorl - sxcorr) * (ec - ecorl)) / (ecorl - ecorr);
sy2 = sycorl + ((sycorl - sycorr) * (ec - ecorl)) / (ecorl - ecorr);
}
#if 0
sx2 = sxcorr + ((sxcorl - sxcorr) * (ec - ecorr))
/ (ecorl - ecorr);
sy2 = sycorr + ((sycorl - sycorr) * (ec - ecorr))
/ (ecorl - ecorr);
#endif
tmplines[*numlinesp].sx1 = sx1;
tmplines[*numlinesp].sy1 = sy1;
tmplines[*numlinesp].sx2 = sx2;
tmplines[*numlinesp].sy2 = sy2;
++(*numlinesp);
}
}
}
}
}
/* Don't draw the temperature in every cell, only do ones with even
coords or ones where the temperature in any adjacent cell is
different. */
int
draw_temperature_here(side, x, y)
Side *side;
int x, y;
{
int dir, x1, y1, temphere = temperature_view(side, x, y);
/* Designers should see temperature in every cell. */
if (is_designer(side))
return TRUE;
for_all_directions(dir) {
if (interior_point_in_dir(x, y, dir, &x1, &y1)) {
if (temphere != temperature_view(side, x1, y1))
return TRUE;
/* Always show temperature around edge of known area. */
if (terrain_view(side, x1, y1) == UNSEEN)
return TRUE;
}
}
return (x % 2 == 0 && y % 2 == 0);
}
/* Don't draw the winds in every cell, only do ones with odd coords or
ones where the wind in any adjacent cell is different. */
int
draw_winds_here(side, x, y)
Side *side;
int x, y;
{
int dir, x1, y1, windhere = wind_view(side, x, y);
/* Designers should see wind in every cell. */
if (is_designer(side))
return TRUE;
for_all_directions(dir) {
if (interior_point_in_dir(x, y, dir, &x1, &y1)) {
if (windhere != wind_view(side, x1, y1))
return TRUE;
/* Always show wind around edge of known area. */
if (terrain_view(side, x1, y1) == UNSEEN)
return TRUE;
}
}
return (x % 2 == 1 && y % 2 == 1);
}
#define cell_terrain(side, x, y) \
(side->see_all ? terrain_at(x, y) \
: (terrain_visible(side, x, y) ? vterrain(terrain_view(side, x, y)) : NONTTYPE))
void
oneliner(side, vp, sx, sy)
Side *side;
VP *vp;
int sx, sy;
{
int x, y, xf, yf;
Unit *unit;
int t2, uview, u, s, ps = NOBODY, cs = NOBODY, dep, sayin = FALSE;
char *peopdesc = NULL, *sidedesc, *str;
char descbuf[80], ctrlbuf[80], buf[BUFSIZE];
Side *side2, *side3;
Feature *feature;
nearest_cell(vp, sx, sy, &x, &y, &xf, &yf);
nearest_unit(vp, sx, sy, &unit);
if (!in_area(x, y)) {
strcpy(tmpbuf, "(nothing)");
return;
} else if (terrain_visible(side, x, y)) {
strcpy(tmpbuf, " ");
/* Describe the side of the people here. */
if (people_sides_defined()) {
ps = people_side_at(x, y);
if (ps != NOBODY) {
side2 = side_n(ps);
if (side2 == NULL) {
peopdesc = "indep";
} else if (side2 == side) {
peopdesc = "your";
} else {
peopdesc = side_adjective(side2);
if (peopdesc[0] == '\0') {
sprintf(descbuf, "s%d", side2->id);
peopdesc = descbuf;
}
}
}
}
if (control_sides_defined()) {
cs = control_side_at(x, y);
if (cs != ps) {
side3 = side_n(cs);
if (side3 == NULL) {
strcpy(ctrlbuf, "uncontrolled");
} else if (side3 == side) {
strcpy(ctrlbuf, "your");
} else {
strcpy(ctrlbuf, side_adjective(side3));
if (ctrlbuf[0] == '\0') {
sprintf(ctrlbuf, "s%d", side3->id);
}
strcat(ctrlbuf, "-controlled");
}
if (peopdesc != NULL) {
strcat(ctrlbuf, " ");
strcat(ctrlbuf, peopdesc);
}
peopdesc = ctrlbuf;
}
}
if (units_visible(side, x, y)) {
if (unit != NULL) {
if (unit->side != side) {
sidedesc = side_adjective(unit->side);
if (ps != NOBODY && ps == side_number(unit->side)) {
peopdesc = "own";
}
} else {
sidedesc = "your";
}
strcat(tmpbuf, sidedesc);
if (unit->name) {
strcat(tmpbuf, " ");
strcat(tmpbuf, u_type_name(unit->type));
strcat(tmpbuf, " ");
strcat(tmpbuf, unit->name);
} else if (unit->number > 0) {
tprintf(tmpbuf, " %d%s %s",
unit->number, ordinal_suffix(unit->number),
u_type_name(unit->type));
} else {
strcat(tmpbuf, " ");
strcat(tmpbuf, u_type_name(unit->type));
}
if (Debug || DebugG || DebugM) {
tprintf(tmpbuf, " #%d", unit->id);
}
sayin = TRUE;
}
} else {
if ((uview = unit_view(side, x, y)) != EMPTY) {
u = vtype(uview); s = vside(uview);
if (ps != NOBODY && ps == s) {
peopdesc = "own";
}
strcat(tmpbuf, side_adjective(side_n(s)));
strcat(tmpbuf, " ");
strcat(tmpbuf, u_type_name(u));
sayin = TRUE;
}
}
if (sayin) {
strcat(tmpbuf, " (in ");
}
if (peopdesc != NULL) {
strcat(tmpbuf, peopdesc);
strcat(tmpbuf, " ");
}
t2 = cell_terrain(side, x, y);
strcat(tmpbuf, t_type_name(t2));
if (sayin) {
strcat(tmpbuf, ")");
}
if (elevations_defined()) {
tprintf(tmpbuf, " Elev %d", elev_at(x, y));
}
if (temperatures_defined()) {
tprintf(tmpbuf, " T %ddeg", temperature_at(x, y));
}
if (numcoattypes > 0) {
for_all_terrain_types(t2) {
if (t_is_coating(t2)
&& aux_terrain_defined(t2)
&& ((dep = aux_terrain_view(side, x, y, t2)) > 0)) {
tprintf(tmpbuf, " %s %d", t_type_name(t2), dep);
}
}
}
} else {
sprintf(tmpbuf, "(unknown)");
}
strcat(tmpbuf, " @");
if (1 /* drawxy */) {
tprintf(tmpbuf, "%d,%d", x, y);
} else if (vp->draw_latlong) {
latlong_desc(descbuf, x, y, xf, yf, 3);
strcat(tmpbuf, descbuf);
}
if (terrain_visible(side, x, y)) {
feature = feature_at(x, y);
if (feature != NULL) {
if (feature->size > 0) {
str = feature_desc(feature, buf);
if (str != NULL) {
strcat(tmpbuf, " (");
strcat(tmpbuf, str);
strcat(tmpbuf, ")");
}
}
}
}
if (1 /* drawxy */ && vp->draw_latlong) {
latlong_desc(descbuf, x, y, xf, yf, 3);
strcat(tmpbuf, " (");
strcat(tmpbuf, descbuf);
strcat(tmpbuf, ")");
}
if (vp->draw_ai && side_has_ai(side)) {
strcat(tmpbuf, " ");
strcat(tmpbuf, ai_at_desig(side, x, y));
}
}
/* (needs a better home?) */
/* Given a side and a unit, calculate the correct "next unit". Typically
used by autonext options, thus the name. */
Unit *
autonext_unit(side, unit)
Side *side;
Unit *unit;
{
int i, uniti = -1, n;
Unit *nextunit;
if (!side->ingame
|| side->finishedturn
|| side->actionvector == NULL)
return NULL;
if (could_be_next_unit(unit) && side_controls_unit(side, unit))
return unit;
for (i = 0; i < side->actionvector->numunits; ++i) {
nextunit = (side->actionvector->units)[i].unit;
if (in_play(nextunit) && side_controls_unit(side, nextunit)) {
if (unit == NULL || unit == nextunit) {
uniti = i;
break;
}
}
}
if (uniti < 0)
return NULL;
/* (should scan for both a preferred and an alternate - preferred
could be within a supplied bbox so as to avoid scrolling) */
for (i = uniti; i < uniti + side->actionvector->numunits; ++i) {
n = i % side->actionvector->numunits;
nextunit = (side->actionvector->units)[n].unit;
if (could_be_next_unit(nextunit) && side_controls_unit(side, nextunit))
return nextunit;
}
return NULL;
}
#if 0
int
in_box(x, y, lx, ly, w, h)
int x, y, lx, ly, w, h;
{
if ( !between(ly, y, ly+h) )
return FALSE;
lx -= (y - ly) / 2;
return between(lx, x, lx+w);
}
#endif
/*
* This should really be called autonext_unit and the decision
* whether to check inbox or not should depend on the bbox being
* valid. i.e. could be called with -1,-1,-1,-1 to disable the bbox.
*/
Unit *
autonext_unit_inbox(side, unit, vp)
Side *side;
Unit *unit;
VP *vp;
{
int i, u, mx, my, val, prefval = -999, v = 10;
Unit *nextunit = NULL, *prefunit = NULL;
if (!side->ingame || side->finishedturn || side->actionvector == NULL)
return NULL;
/* degenerate case... this unit still has stuff to do. */
if (could_be_next_unit(unit) && side_controls_unit(side, unit))
return unit;
if (unit == NULL) {
u = 0;
if (!nearest_cell(vp, vp->sx + vp->pxw / 2, vp->sy + vp->pxh / 2, &mx, &my, NULL, NULL)) {
mx = area.width / 2; my = area.halfheight;
}
} else {
u = unit->type;
mx = unit->x; my = unit->y;
}
for (i = 0; i < side->actionvector->numunits; ++i) {
nextunit = (side->actionvector->units)[i].unit;
if (side_controls_unit(side, nextunit) && could_be_next_unit(nextunit)) {
val = v - distance(nextunit->x, nextunit->y, mx, my);
if (cell_is_in_middle(vp, nextunit->x, nextunit->y))
val += v;
if (nextunit->type == u)
val += 2;
if (val > prefval) {
prefval = val;
prefunit = nextunit;
}
}
}
return prefunit;
}
int
could_be_next_unit(unit)
Unit *unit;
{
return (unit != NULL
&& alive(unit)
&& inside_area(unit->x, unit->y)
&& (unit->act
&& unit->act->acp > 0 /*
&& !has_pending_action(unit) */)
&& (unit->plan
&& !unit->plan->asleep
&& !unit->plan->reserve
&& !unit->plan->delayed
&& unit->plan->waitingfortasks));
}
Unit *
find_next_occupant(unit)
Unit *unit;
{
Unit *nextup;
if (unit->occupant != NULL) {
return unit->occupant;
} else if (unit->nexthere != NULL) {
return unit->nexthere;
} else {
nextup = unit->transport;
if (nextup != NULL) {
while (nextup->transport != NULL && nextup->nexthere == NULL) {
nextup = nextup->transport;
}
if (nextup->nexthere != NULL)
return nextup->nexthere;
if (nextup->transport == NULL)
return nextup;
} else {
/* This is a no-op if there is no stacking within a hex. */
return unit_at(unit->x, unit->y);
}
}
return unit;
}
/* Given a character, compute the direction(s) that it represents.
Return the number of directions. */
int
char_to_dir(ch, dir1p, dir2p, modp)
int ch, *dir1p, *dir2p, *modp;
{
char basech, *rawdir;
int ndirs = 0;
if (isupper(ch)) {
basech = tolower(ch);
if (modp)
*modp = 1;
} else if (ch < ' ') {
basech = ch + 0x60;
if (modp)
*modp = 2;
} else {
basech = ch;
if (modp)
*modp = 0;
}
rawdir = strchr(dirchars, basech);
if (rawdir) {
*dir1p = rawdir - dirchars;
ndirs = 1;
} else if (basech == 'k') {
if (flip_coin()) {
*dir1p = NORTHEAST;
if (dir2p)
*dir2p = NORTHWEST;
} else {
*dir1p = NORTHWEST;
if (dir2p)
*dir2p = NORTHEAST;
}
ndirs = 2;
} else if (basech == 'j') {
if (flip_coin()) {
*dir1p = SOUTHEAST;
if (dir2p)
*dir2p = SOUTHWEST;
} else {
*dir1p = SOUTHWEST;
if (dir2p)
*dir2p = SOUTHEAST;
}
ndirs = 2;
}
return ndirs;
}
/* Given that the player desires to move the given unit into the given
cell/other unit, prepare a "most appropriate" action. */
/* (should share diff cell and same cell interaction code) */
int
advance_into_cell(side, unit, x, y, other)
Side *side;
Unit *unit, *other;
int x, y;
{
#ifdef DESIGNERS
/* Designers use this function to push units around, bound only by the
limits on occupancy. */
if (side->designer) {
return net_designer_teleport(unit, x, y, other);
}
#endif /* DESIGNERS */
if (x != unit->x || y != unit->y) {
if (unit->act && unit->plan) { /* (should be more sophisticated test?) */
if (distance(unit->x, unit->y, x, y) == 1) {
if (other == NULL) {
if (can_occupy_cell(unit, x, y)
&& valid(check_move_action(unit, unit, x, y, unit->z))) {
net_prep_move_action(unit, unit, x, y, unit->z);
return TRUE;
} else {
return FALSE;
}
}
if (unit_trusts_unit(unit, other)
|| (other->side == NULL && sides_allow_entry(unit, other))) {
/* A friend, maybe get on it. */
if (can_occupy(unit, other)) {
if (valid(check_enter_action(unit, unit, other))) {
net_prep_enter_action(unit, unit, other);
} else {
/* (should schedule for next turn?) */
}
} else if (can_occupy(other, unit)) {
if (u_acp(other->type) > 0) {
/* Have other unit do an enter action,
then move. */
/* (not quite right, move should happen
after other unit is actually inside, in
case it fills dest) */
net_prep_enter_action(other, other, unit);
net_set_move_to_task(unit, x, y);
} else {
net_prep_enter_action(unit, other, unit);
net_set_move_to_task(unit, x, y);
}
} else if (other->transport != NULL
&& can_occupy(unit, other->transport)) {
if (valid(check_enter_action(unit, unit, other->transport))) {
net_prep_enter_action(unit, unit, other->transport);
} else {
/* (should schedule for next turn?) */
}
} else if (other->transport != NULL
&& other->transport->transport != NULL
&& can_occupy(unit, other->transport->transport)) {
/* two levels up should be sufficient */
if (valid(check_enter_action(unit, unit, other->transport->transport))) {
net_prep_enter_action(unit, unit, other->transport->transport);
} else {
/* (should schedule for next turn?) */
}
} else if (valid(check_transfer_part_action(unit, unit, unit->hp, other))) {
net_prep_transfer_part_action(unit, unit, unit->hp, other);
} else if (can_occupy_cell(unit, x, y)
&& valid(check_move_action(unit, unit, x, y, unit->z))) {
net_prep_move_action(unit, unit, x, y, unit->z);
} else {
return FALSE;
}
} else {
/* Somebody else's unit, try to victimize it in various ways,
trying coexistence only as a last resort. */
if (valid(check_capture_action(unit, unit, other))) {
net_prep_capture_action(unit, unit, other);
} else if (valid(check_overrun_action(unit, unit, x, y, unit->z, 100))) {
net_prep_overrun_action(unit, unit, x, y, unit->z, 100);
} else if (valid(check_attack_action(unit, unit, other, 100))) {
net_prep_attack_action(unit, unit, other, 100);
} else if (valid(check_fire_at_action(unit, unit, other, -1))) {
net_prep_fire_at_action(unit, unit, other, -1);
} else if (valid(check_detonate_action(unit, unit, x, y, unit->z))) {
net_prep_detonate_action(unit, unit, x, y, unit->z);
} else if (valid(check_move_action(unit, unit, x, y, unit->z))) {
net_prep_move_action(unit, unit, x, y, unit->z);
} else {
return FALSE;
}
}
} else {
/* We're not adjacent to the destination, set up a move task. */
net_set_move_to_task(unit, x, y);
}
} else {
/* ??? can't act ??? */
}
} else {
/* Destination is in the unit's own cell. */
if (other != NULL) {
if (unit_trusts_unit(unit, other)) {
if (valid(check_transfer_part_action(unit, unit, unit->hp, other))) {
net_prep_transfer_part_action(unit, unit, unit->hp, other);
} else if (valid(check_enter_action(unit, unit, other))) {
net_prep_enter_action(unit, unit, other);
} else {
return FALSE;
}
} else {
/* Somebody else's unit, try to victimize it in various ways,
trying coexistence only as a last resort. */
if (valid(check_capture_action(unit, unit, other))) {
net_prep_capture_action(unit, unit, other);
} else if (valid(check_attack_action(unit, unit, other, 100))) {
net_prep_attack_action(unit, unit, other, 100);
} else if (valid(check_fire_at_action(unit, unit, other, -1))) {
net_prep_fire_at_action(unit, unit, other, -1);
} else if (valid(check_detonate_action(unit, unit, x, y, unit->z))) {
net_prep_detonate_action(unit, unit, x, y, unit->z);
} else {
return FALSE;
}
}
} else if (unit->transport != NULL) {
/* Unit is an occupant wanting to leave, but yet remain in
the same cell as the transport. */
if (valid(check_move_action(unit, unit, x, y, unit->z))) {
net_prep_move_action(unit, unit, x, y, unit->z);
} else {
return FALSE;
}
} else {
/* This is a no-op, don't do anything. */
}
}
return TRUE;
}
/* Given a unit and amounts of supplies desired to transfer, move as much of them
as possible into the unit's transport. */
int
give_supplies(unit, amts, rslts)
Unit *unit;
short *amts, *rslts;
{
int m, gift, maxgift, actual, didsome;
Unit *unit2;
didsome = FALSE;
unit2 = unit->transport;
if (!(in_play(unit2) && completed(unit2)))
return didsome;
for_all_material_types(m) {
if (rslts)
rslts[m] = 0;
maxgift = min(unit->supply[m], um_storage_x(unit2->type, m) - unit2->supply[m]);
gift = ((amts == NULL || amts[m] == -1) ? (maxgift / 2) : amts[m]);
if (gift > 0) {
if (1 /* can do immed transfer */) {
/* Be stingy if giver is low */
if (2 * unit->supply[m] < um_storage_x(unit->type, m))
gift = max(1, gift/2);
actual = transfer_supply(unit, unit2, m, gift);
if (rslts)
rslts[m] = actual;
if (actual > 0)
didsome = TRUE;
}
}
}
return didsome;
}
/* Attempt to transfer the given amounts of material from the unit's
transport into the unit. */
int
take_supplies(unit, amts, rslts)
Unit *unit;
short *amts, *rslts;
{
int m, want, actual, neededsome;
Unit *unit2;
neededsome = FALSE;
for_all_material_types(m) {
if (rslts)
rslts[m] = 0;
want = ((amts == NULL || amts[m] == -1)
? (um_storage_x(unit->type, m) - unit->supply[m])
: amts[m]);
if (want > 0) {
neededsome = TRUE;
unit2 = unit->transport;
if (in_play(unit2) && completed(unit2)) {
/* Be stingy if transport is low */
if (2 * unit2->supply[m] < um_storage_x(unit2->type, m))
want = max(1, want/2);
actual = transfer_supply(unit2, unit, m, want);
if (rslts)
rslts[m] = actual;
}
}
}
return neededsome;
}
/* Return the type to build that dialogs should highlight initially. */
int
favored_type(unit)
Unit *unit;
{
int u;
if (unit == NULL)
return NONUTYPE;
if (unit->plan
&& unit->plan->tasks
&& unit->plan->tasks->type == TASK_BUILD)
return unit->plan->tasks->args[0];
for_all_unit_types(u) {
if (uu_acp_to_create(unit->type, u) > 0)
return u;
}
return NONUTYPE;
}
void
force_global_replan(side)
Side *side;
{
Unit *unit;
for_all_side_units(side, unit) {
if (in_play(unit) && unit->plan != NULL) {
net_force_replan(side, unit, FALSE);
}
}
}
/* Generic image setup. */
static short imf_dir_loaded;
static ImageFamily *
get_generic_images(side, name)
Side *side;
char *name;
{
FILE *fp;
int cloned = FALSE;
ImageFamily *imf;
imf = get_imf(name);
if (imf == NULL)
return NULL;
if (imf->numsizes > 0 && imf_interp_hook != NULL)
imf = (*imf_interp_hook)(imf, NULL, FALSE);
if (imf_load_hook != NULL)
imf = (*imf_load_hook)(imf);
if (imf->numsizes == 0) {
/* Maybe collect the names/locations of all image families. */
if (!imf_dir_loaded) {
/* (should let name be decided by platform?) */
fp = open_library_file("imf.dir");
if (fp != NULL) {
load_image_families(fp, FALSE, NULL);
fclose(fp);
} else {
init_warning("Cannot open \"%s\", will not use it", "imf.dir");
}
imf_dir_loaded = TRUE;
}
/* Get a (possibly empty) family. */
imf = get_imf(name);
if (imf == NULL)
return NULL;
if (imf->location != NULL) {
/* Load data filling in the family. */
/* (should use library path list) */
make_pathname(xconq_libs->path, imf->location->name, "", spbuf);
if (load_imf_file(spbuf, NULL)) {
} else if (load_imf_file(imf->location->name, NULL)) {
} else {
/* complain here, or not? */
}
if (imf_interp_hook != NULL)
imf = (*imf_interp_hook)(imf, NULL, FALSE);
}
}
return imf;
}
ImageFamily *
get_unit_type_images(side, u, stylepref)
Side *side;
int u;
char *stylepref;
{
char *name, styledname[BUFSIZE];
ImageFamily *imf;
if (!empty_string(u_image_name(u)))
name = u_image_name(u);
else
name = u_internal_name(u);
/* Look for an image in the given style, such as "c" for color
or "s" for silhouette, using <name>-<style> as the name, and
return that image if found. */
if (!empty_string(stylepref)) {
strcpy(styledname, name);
strcat(styledname, "-");
strcat(styledname, stylepref);
imf = get_generic_images(side, styledname);
if (imf != NULL && imf->numsizes > 0) {
record_imf_get(imf);
return imf;
}
}
imf = get_generic_images(side, name);
if (imf != NULL && imf->numsizes == 0) {
imf->ersatz = TRUE;
imf = add_default_unit_image(imf, u);
}
record_imf_get(imf);
return imf;
}
static ImageFamily *
add_default_unit_image(ImageFamily *imf, int u)
{
int i, hi, lo;
Image *img;
img = get_img(imf, 16, 16);
if (img == NULL)
return imf;
img->rawmonodata = xmalloc(32);
img->rawmaskdata = xmalloc(32);
hi = u >> 4;
lo = (u & 0xf) << 4;
for (i = 4; i < 28; i += 2) {
(img->rawmonodata)[i] = hi;
(img->rawmonodata)[i + 1] = lo;
(img->rawmaskdata)[i] = 0x7f;
(img->rawmaskdata)[i + 1] = 0xfe;
}
(img->rawmonodata)[14] = 0x1f;
(img->rawmonodata)[15] = 0xf8;
(img->rawmonodata)[16] = 0x1f;
(img->rawmonodata)[17] = 0xf8;
(img->rawmaskdata)[2] = 0x7f;
(img->rawmaskdata)[3] = 0xfe;
(img->rawmaskdata)[28] = 0x7f;
(img->rawmaskdata)[29] = 0xfe;
if (imf_interp_hook)
imf = (*imf_interp_hook)(imf, NULL, FALSE);
return imf;
}
ImageFamily *
get_terrain_type_images(side, t)
Side *side;
int t;
{
char *name;
ImageFamily *imf;
if (!empty_string(t_image_name(t)))
name = t_image_name(t);
else
name = t_type_name(t);
imf = get_generic_images(side, name);
if (imf != NULL && imf->numsizes == 0) {
imf->ersatz = TRUE;
imf = add_default_terrain_image(imf, t);
}
record_imf_get(imf);
return imf;
}
/* The default terrain image is ugly but functional; basically a binary
encoding of the terrain type number. */
static ImageFamily *
add_default_terrain_image(imf, t)
ImageFamily *imf;
int t;
{
Image *img;
img = get_img(imf, 8, 8);
img->istile = TRUE;
img->rawmonodata = xmalloc(8);
(img->rawmonodata)[1] = (t << 2);
(img->rawmonodata)[2] = (t << 2);
(img->rawmonodata)[3] = 0x7e;
(img->rawmonodata)[4] = (t << 2);
(img->rawmonodata)[5] = (t << 2);
if (imf_interp_hook)
imf = (*imf_interp_hook)(imf, NULL, FALSE);
return imf;
}
ImageFamily *
get_unseen_images(side)
Side *side;
{
if (!empty_string(g_unseen_image_name())) {
unseen_image = get_generic_images(side, g_unseen_image_name());
if (unseen_image != NULL && unseen_image->numsizes == 0) {
/* Appears to have failed - clear the unseen image then. */
unseen_image = NULL;
/* Note that we shouldn't try to free the imf, because it
may be in use elsewhere. */
}
}
record_imf_get(unseen_image);
return unseen_image;
}
ImageFamily *
get_emblem_images(side, side2)
Side *side, *side2;
{
char *name, tmpbuf[BUFSIZE];
int s2 = side_number(side2);
ImageFamily *imf;
if (side2 != NULL && !empty_string(side2->emblemname))
name = side2->emblemname;
else {
/* There is a set of default emblems named "s0", "s1", etc. */
sprintf(tmpbuf, "s%d", s2);
name = copy_string(tmpbuf);
}
imf = get_generic_images(side, name);
/* If we must have an image, and none were acquired, invoke the default
image getter. */
if (imf != NULL && imf->numsizes == 0 && strcmp(name, "none") != 0) {
imf->ersatz = TRUE;
imf = add_default_emblem_image(imf, s2);
}
record_imf_get(imf);
return imf;
}
/* Make a solid white square with stripes at top and bottom, with the encoding
of the side number in the middle. */
static ImageFamily *
add_default_emblem_image(ImageFamily *imf, int s2)
{
int i;
Image *img;
img = get_img(imf, 8, 8);
if (img == NULL)
return imf;
img->rawmonodata = xmalloc(8);
img->rawmaskdata = xmalloc(8);
for (i = 0; i < 8; ++i) {
(img->rawmonodata)[i] = s2;
(img->rawmaskdata)[i] = 0xff;
}
(img->rawmonodata)[0] = 0xff;
(img->rawmonodata)[7] = 0xff;
if (imf_interp_hook)
imf = (*imf_interp_hook)(imf, NULL, FALSE);
return imf;
}
void
record_imf_get(imf)
ImageFamily *imf;
{
int i;
ImageFamily **new_record;
if (imf == NULL)
return;
/* Estimate and allocate the usual amount of space needed. */
if (max_recorded_imfs == 0)
max_recorded_imfs = numutypes + numttypes + (MAXSIDES + 1) + 1;
if (recorded_imfs == NULL) {
recorded_imfs =
(ImageFamily **) xmalloc(max_recorded_imfs * sizeof(ImageFamily *));
}
/* Allocate more space if needed. */
if (num_recorded_imfs >= max_recorded_imfs) {
max_recorded_imfs += max_recorded_imfs / 2;
new_record =
(ImageFamily **) xmalloc(max_recorded_imfs * sizeof(ImageFamily *));
for (i = 0; i < num_recorded_imfs; ++i) {
new_record[i] = recorded_imfs[i];
}
recorded_imfs = new_record;
}
for (i = 0; i < num_recorded_imfs; ++i) {
if (strcmp(imf->name, recorded_imfs[i]->name) == 0)
return;
}
recorded_imfs[num_recorded_imfs++] = imf;
/* Expand any interface-specific data into its all-interface form, so that
saved games will include it. This needs to be done now, because game
saving may occur in a low-memory situation and there may not be enough
memory available then. */
make_generic_image_data(imf);
}
/* Compute and cache single-char representations for things. */
void
init_ui_chars()
{
int u, t;
char *str;
if (unitchars == NULL) {
unitchars = xmalloc(numutypes);
for_all_unit_types(u) {
str = u_uchar(u);
unitchars[u] = (!empty_string(str) ? str[0] : utype_name_n(u, 1)[0]);
}
}
if (terrchars == NULL) {
terrchars = xmalloc(numttypes);
for_all_terrain_types(t) {
str = t_char(t);
terrchars[t] = (!empty_string(str) ? str[0] : t_type_name(t)[0]);
}
}
unseen_char_1 = unseen_char_2 = ' ';
str = g_unseen_char();
if (strlen(str) >= 1) {
unseen_char_1 = unseen_char_2 = str[0];
if (strlen(str) >= 2) {
unseen_char_2 = str[1];
}
}
}
/* Write the side's view of the world, as ASCII. */
/* (should be intelligent enough to cut into pages, or else document
how to do it) */
/* (maybe display names too somehow, perhaps as second layer?) */
#define VIEWFILE "view.ccq"
void
dump_text_view(side, use_both_chars)
Side *side;
int use_both_chars;
{
char ch1, ch2;
int x, y, t, uview, u, s, draw, i;
Side *side2;
Unit *unit;
FILE *fp;
fp = fopen(VIEWFILE, "w");
if (fp != NULL) {
for (y = area.height-1; y >= 0; --y) {
for (i = 0; i < y; ++i)
fputc(' ', fp);
for (x = 0; x < area.width; ++x) {
ch1 = ch2 = ' ';
if (in_area(x, y) && terrain_visible(side, x, y)) {
t = terrain_at(x, y);
ch1 = terrchars[t];
ch2 = (use_both_chars ? ch1 : ' ');
draw = FALSE;
if (units_visible(side, x, y)) {
unit = unit_at(x, y);
if (unit != NULL) {
u = unit->type;
s = side_number(unit->side);
draw = TRUE;
}
} else {
uview = unit_view(side, wrapx(x), y);
if (uview != EMPTY) {
u = vtype(uview);
s = vside(uview);
draw = TRUE;
}
}
if (draw) {
ch1 = unitchars[u];
ch2 = ' ';
if (between(1, s, 9))
ch2 = s + '0';
else if (s >= 10)
/* This could get weird if s > 36, but not much
chance of that because MAXSIDES < 31 always. */
ch2 = s - 10 + 'A';
}
}
fputc(ch1, fp);
fputc(ch2, fp);
}
fprintf(fp, "\n");
}
fprintf(fp, "\n\nTerrain Types:\n");
for_all_terrain_types(t) {
fprintf(fp, " %c%c %s\n",
terrchars[t], terrchars[t], t_type_name(t));
}
fprintf(fp, "\n\nUnit Types:\n");
for_all_unit_types(u) {
fprintf(fp, " %c %s\n", unitchars[u], u_type_name(u));
}
fprintf(fp, "\n\nSides:\n");
for_all_sides(side2) {
fprintf(fp, " %d %s\n", side_number(side2), side_name(side2));
}
fclose(fp);
notify(side, "Dumped area view to \"%s\".", VIEWFILE);
} else {
notify(side, "Can't open \"%s\"!!", VIEWFILE);
}
}
/* Return the type of cell terrain that the given side sees at the given
location. */
int
terrain_seen_at(side, x, y)
Side *side;
int x, y;
{
if (in_area(x, y)
&& (side == NULL
|| all_see_all
#ifdef DESIGNERS
|| side->designer
#endif /* DESIGNERS */
|| terrain_view(side, x, y) != UNSEEN)) {
return terrain_at(x, y);
} else {
return NONTTYPE;
}
}
/* Return the unit seen by the given side at the given location. Note
that this should not be used casually by interfaces, since the result
is a pointer to a real unit, not a view of one. */
/* (should result depend on contents of stack?) */
Unit *
unit_seen_at(side, x, y)
Side *side;
int x, y;
{
if (!in_area(x, y))
return NULL;
if (side == NULL
|| all_see_all
#ifdef DESIGNERS
|| side->designer
#endif /* DESIGNERS */
|| cover(side, x, y) > 0)
return unit_at(x, y);
return NULL;
}
int
utype_seen_at(side, x, y)
Side *side;
int x, y;
{
Unit *unit;
short view;
if (!in_area(x, y))
return NONUTYPE;
unit = unit_seen_at(side, x, y);
if (unit)
return unit->type;
view = unit_view(side, x, y);
if (view != EMPTY)
return vtype(view);
return NONUTYPE;
}
/* Generic versions of commands. */
void
generic_do_doctrine(side, str)
Side *side;
char *str;
{
if (str) {
if (strcmp(str, "set") == 0) {
notify(side, "Set what?");
} else if (strncmp(str, "set ", 4) == 0) {
net_set_doctrine(side, str + 4);
} else if (strcmp(str, "help") == 0
|| strcmp(str, "?") == 0) {
notify(side, "doctrine set <doct-name> <property> <value>");
notify(side, "\"doctrine\" alone to see current settings");
}
return;
}
notify_doctrine(side, str);
}
|