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
|
{$INCLUDE Switches.inc}
// {$DEFINE TEXTLOG}
// {$DEFINE LOADPERF}
unit Database;
interface
uses
SysUtils, Protocol, CmdList;
const
// additional test flags
//{$DEFINE FastContact} { extra small world with railroad everywhere }
lNoObserve = 0;
lObserveUnhidden = 1;
lObserveAll = 2;
lObserveSuper = 3; // observe levels
TerrType_Canalable = [fGrass, fDesert, fPrairie, fTundra, fSwamp,
fForest, fHills];
nStartUn = 1;
StartUn: array [0 .. nStartUn - 1] of integer = (0); // mix of start units
CityOwnTile = 13;
type
TGameMode = (moLoading_Fast, moLoading, moMovie, moPlaying);
var
GAlive: Integer; { players alive; bitset of 1 shl p }
GWatching: Integer;
GInitialized: Integer;
GAI: Integer;
RND: Cardinal; { world map randseed }
lx: Integer;
ly: Integer;
MapSize: Integer; // = lx*ly
LandMass: Integer;
{$IFOPT O-}InvalidTreatyMap, {$ENDIF}
SaveMapCenterLoc: Integer;
PeaceEnded: Integer;
GTurn: Integer; { current turn }
GTestFlags: Integer;
Mode: TGameMode;
GWonder: array [0 .. nWonder - 1] of TWonderInfo;
ServerVersion: array [0 .. nPl - 1] of integer;
ProcessClientData: array [0 .. nPl - 1] of boolean;
CL: TCmdList;
{$IFDEF TEXTLOG}CmdInfo: string;
TextLog: TextFile; {$ENDIF}
{$IFDEF LOADPERF}time_total, time_total0, time_x0, time_x1, time_a, time_b, time_c: int64; {$ENDIF}
// map data
RealMap: array [0 .. lxmax * lymax - 1] of Cardinal;
Continent: array [0 .. lxmax * lymax - 1] of integer;
{ continent id for each tile }
Occupant: array [0 .. lxmax * lymax - 1] of ShortInt;
{ occupying player for each tile }
ZoCMap: array [0 .. lxmax * lymax - 1] of ShortInt;
ObserveLevel: array [0 .. lxmax * lymax - 1] of Cardinal;
{ Observe Level of player p in bits 2*p and 2*p+1 }
UsedByCity: array [0 .. lxmax * lymax - 1] of integer;
{ location of exploiting city for
each tile, =-1 if not exploited }
// player data
RW: array [0 .. nPl - 1] of TPlayerContext; { player data }
Difficulty: array [0 .. nPl - 1] of integer;
GShip: array [0 .. nPl - 1] of TShipInfo;
ResourceMask: array [0 .. nPl - 1] of Cardinal;
Founded: array [0 .. nPl - 1] of integer; { number of cities founded }
TerritoryCount: array [0 .. nPl] of integer;
LastValidStat, Researched, Discovered, // number of tiles discovered
GrWallContinent: array [0 .. nPl - 1] of integer;
RWemix: array [0 .. nPl - 1, 0 .. nPl - 1, 0 .. nmmax - 1] of SmallInt;
// [p1,p2,mix] -> index of p2's model mix in p1's enemy model list
Destroyed: array [0 .. nPl - 1, 0 .. nPl - 1, 0 .. nmmax - 1] of SmallInt;
// [p1,p2,mix] -> number of p2's units with model mix that p1 has destroyed
nTech: array [0 .. nPl - 1] of integer; { number of known techs }
// NewContact: array[0..nPl-1,0..nPl-1] of boolean;
type
TVicinity8Loc = array [0 .. 7] of integer;
TVicinity21Loc = array [0 .. 27] of integer;
procedure MaskD(var x: array of Cardinal; Count, Mask: Cardinal);
procedure IntServer(Command, Player, Subject: integer; var Data);
procedure CompactLists(p: integer);
procedure ClearTestFlags(ClearFlags: integer);
procedure SetTestFlags(p, SetFlags: integer);
// Tech Related Functions
function TechBaseCost(nTech, diff: integer): integer;
function TechCost(p: integer): integer;
procedure CalculateModel(var m: TModel);
procedure CheckSpecialModels(p, pre: integer);
procedure EnableDevModel(p: integer);
procedure SeeTech(p, ad: integer);
procedure DiscoverTech(p, ad: integer);
procedure CheckExpiration(Wonder: integer);
// Location Navigation
function dLoc(Loc, dx, dy: integer): integer;
procedure dxdy(Loc0, Loc1: integer; var dx, dy: integer);
function Distance(Loc0, Loc1: integer): integer;
procedure V8_to_Loc(Loc0: integer; var VicinityLoc: TVicinity8Loc);
procedure V21_to_Loc(Loc0: integer; var VicinityLoc: TVicinity21Loc);
// Game Initialization
procedure InitRandomGame;
procedure InitMapGame(Human: integer);
procedure ReleaseGame;
// Map Editor
function MapGeneratorAvailable: boolean;
procedure CreateElevation;
procedure CreateMap(preview: boolean);
procedure InitMapEditor;
procedure ReleaseMapEditor;
procedure EditTile(Loc, NewTile: integer);
// Map Revealing
function GetTileInfo(p, cix, Loc: integer; var Info: TTileInfo): integer;
procedure Strongest(Loc: integer; var uix, Strength, Bonus, Cnt: integer);
function UnitSpeed(p, mix, Health: integer): integer;
procedure GetUnitReport(p, uix: integer; var UnitReport: TUnitReport);
procedure SearchCity(Loc: integer; var p, cix: integer);
procedure TellAboutModel(p, taOwner, tamix: integer);
function emixSafe(p, taOwner, tamix: integer): integer;
function Discover9(Loc, p, Level: integer;
TellAllied, EnableContact: boolean): boolean;
function Discover21(Loc, p, AdjacentLevel: integer;
TellAllied, EnableContact: boolean): boolean;
procedure DiscoverAll(p, Level: integer);
procedure DiscoverViewAreas(p: integer);
function GetUnitStack(p, Loc: integer): integer;
procedure UpdateUnitMap(Loc: integer; CityChange: boolean = false);
procedure RecalcV8ZoC(p, Loc: integer);
procedure RecalcMapZoC(p: integer);
procedure RecalcPeaceMap(p: integer);
// Territory Calculation
procedure CheckBorders(OriginLoc: integer; PlayerLosingCity: integer = -1);
procedure LogCheckBorders(p, cix: integer; PlayerLosingCity: integer = -1);
// Map Processing
procedure CreateUnit(p, mix: integer);
procedure FreeUnit(p, uix: integer);
procedure PlaceUnit(p, uix: integer);
procedure RemoveUnit(p, uix: integer; Enemy: integer = -1);
procedure RemoveUnit_UpdateMap(p, uix: integer);
procedure RemoveAllUnits(p, Loc: integer; Enemy: integer = -1);
procedure RemoveDomainUnits(d, p, Loc: integer);
procedure FoundCity(p, FoundLoc: integer);
procedure DestroyCity(p, cix: integer; SaveUnits: boolean);
procedure ChangeCityOwner(pOld, cixOld, pNew: integer);
procedure CompleteJob(p, Loc, Job: integer);
// Diplomacy
procedure IntroduceEnemy(p1, p2: integer);
procedure GiveCivilReport(p, pAbout: integer);
procedure GiveMilReport(p, pAbout: integer);
procedure ShowPrice(pSender, pTarget, Price: integer);
function PayPrice(pSender, pTarget, Price: integer; execute: boolean): boolean;
procedure CancelTreaty(p, pWith: integer; DecreaseCredibility: boolean = true);
function DoSpyMission(p, pCity, cix, Mission: integer): Cardinal;
implementation
uses
{$IFDEF LOADPERF}SysUtils, Windows, {$ENDIF} IPQ;
type
tMapType = array [0 .. lxmax * lymax - 1] of integer;
var
UnBuilt: array [0 .. nPl - 1] of integer; { number of units built }
procedure MaskD(var x: array of Cardinal; Count, Mask: Cardinal);
var
I: Integer;
begin
for I := 0 to Count - 1 do
x[I] := x[I] and Mask;
end;
procedure CompactLists(p: integer);
var
uix, uix1, cix: integer;
{$IFOPT O-}V21: integer;
Radius: TVicinity21Loc; {$ENDIF}
begin
with RW[p] do
begin
// compact unit list
uix := 0;
while uix < nUn do
if Un[uix].Loc < 0 then
begin
dec(nUn);
Un[uix] := Un[nUn]; { replace removed unit by last }
if (Un[uix].TroopLoad > 0) or (Un[uix].AirLoad > 0) then
for uix1 := 0 to nUn - 1 do
if Un[uix1].Master = nUn then
Un[uix1].Master := uix;
// index of last unit changes
end
else
inc(uix);
// compact city list
cix := 0;
while cix < nCity do
if City[cix].Loc < 0 then
begin
dec(nCity);
City[cix] := City[nCity]; { replace city by last }
for uix1 := 0 to nUn - 1 do
if Un[uix1].Home = nCity then
Un[uix1].Home := cix;
{ index of last city changes }
end
else
inc(cix);
// compact enemy city list
cix := 0;
while cix < nEnemyCity do
if EnemyCity[cix].Loc < 0 then
begin
dec(nEnemyCity);
EnemyCity[cix] := EnemyCity[nEnemyCity]; { replace city by last }
end
else
inc(cix);
{$IFOPT O-}
for cix := 0 to nCity - 1 do
with City[cix] do
begin
V21_to_Loc(Loc, Radius);
for V21 := 1 to 26 do
if Tiles and (1 shl V21) <> 0 then
assert(UsedByCity[Radius[V21]] = Loc);
end;
{$ENDIF}
end;
end;
{
Tech Related Functions
____________________________________________________________________
}
function TechBaseCost(nTech, diff: integer): integer;
var
c0: single;
begin
c0 := TechFormula_M[diff] * (nTech + 4) *
exp((nTech + 4) / TechFormula_D[diff]);
if c0 >= $10000000 then
result := $10000000
else
result := trunc(c0);
end;
function TechCost(p: integer): integer;
begin
with RW[p] do
begin
result := TechBaseCost(nTech[p], Difficulty[p]);
if ResearchTech >= 0 then
if (ResearchTech = adMilitary) or (Tech[ResearchTech] = tsSeen) then
result := result shr 1
else if ResearchTech in FutureTech then
if Government = gFuture then
result := result * 2
else
result := result * 4;
end;
end;
procedure SetModelFlags(var m: TModel);
begin
m.Flags := 0;
if (m.Domain = dGround) and (m.Kind <> mkDiplomat) then
m.Flags := m.Flags or mdZOC;
if (m.Kind = mkDiplomat) or (m.Attack + m.Cap[mcBombs] = 0) then
m.Flags := m.Flags or mdCivil;
if (m.Cap[mcOver] > 0) or (m.Domain = dSea) and (m.Weight >= 6) then
m.Flags := m.Flags or mdDoubleSupport;
end;
procedure CalculateModel(var m: TModel);
{ calculate attack, defense, cost... of a model by features }
var
i: integer;
begin
with m do
begin
Attack := (Cap[mcOffense] + Cap[mcOver]) * MStrength;
Defense := (Cap[mcDefense] + Cap[mcOver]) * MStrength;
case Domain of
dGround:
Speed := 150 + Cap[mcMob] * 50;
dSea:
begin
Speed := 350 + 200 * Cap[mcNP] + 200 * Cap[mcTurbines];
if Cap[mcNP] = 0 then
inc(Speed, 100 * Cap[mcSE]);
end;
dAir:
Speed := 850 + 400 * Cap[mcJet];
end;
Cost := 0;
for i := 0 to nFeature - 1 do
if 1 shl Domain and Feature[i].Domains <> 0 then
inc(Cost, Cap[i] * Feature[i].Cost);
Cost := Cost * MCost;
Weight := 0;
for i := 0 to nFeature - 1 do
if 1 shl Domain and Feature[i].Domains <> 0 then
if (Domain = dGround) and (i = mcDefense) then
inc(Weight, Cap[i] * 2)
else
inc(Weight, Cap[i] * Feature[i].Weight);
end;
SetModelFlags(m);
end;
procedure CheckSpecialModels(p, pre: integer);
var
i, mix1: integer;
HasAlready: boolean;
begin
for i := 0 to nSpecialModel -
1 do { check whether new special model available }
if (SpecialModelPreq[i] = pre) and (RW[p].nModel < nmmax) then
begin
HasAlready := false;
for mix1 := 0 to RW[p].nModel - 1 do
if (RW[p].Model[mix1].Kind = SpecialModel[i].Kind) and
(RW[p].Model[mix1].Attack = SpecialModel[i].Attack) and
(RW[p].Model[mix1].Speed = SpecialModel[i].Speed) then
HasAlready := true;
if not HasAlready then
begin
RW[p].Model[RW[p].nModel] := SpecialModel[i];
SetModelFlags(RW[p].Model[RW[p].nModel]);
with RW[p].Model[RW[p].nModel] do
begin
Status := 0;
SavedStatus := 0;
IntroTurn := GTurn;
Built := 0;
Lost := 0;
ID := p shl 12 + RW[p].nModel;
if (Kind = mkSpecial_Boat) and (ServerVersion[p] < $000EF0) then
Speed := 350; // old longboat
end;
inc(RW[p].nModel);
end;
end;
end;
procedure EnableDevModel(p: integer);
begin
with RW[p] do
if nModel < nmmax then
begin
Model[nModel] := DevModel;
with Model[nModel] do
begin
Status := 0;
SavedStatus := 0;
IntroTurn := GTurn;
Built := 0;
Lost := 0;
ID := p shl 12 + nModel;
end;
inc(nModel);
inc(Researched[p]);
end;
end;
procedure SeeTech(p, ad: integer);
begin
{$IFDEF TEXTLOG}CmdInfo := CmdInfo + Format(' P%d:A%d', [p, ad]); {$ENDIF}
RW[p].Tech[ad] := tsSeen;
// inc(nTech[p]);
inc(Researched[p]);
end;
procedure FreeSlaves;
var
p1, uix: integer;
begin
for p1 := 0 to nPl - 1 do
if (GAlive and (1 shl p1) <> 0) then
for uix := 0 to RW[p1].nUn - 1 do
if RW[p1].Model[RW[p1].Un[uix].mix].Kind = mkSlaves then
RW[p1].Un[uix].Job := jNone;
end;
procedure DiscoverTech(p, ad: integer);
procedure TellAboutKeyTech(p, Source: integer);
var
i, p1: integer;
begin
for i := 1 to 3 do
if ad = AgePreq[i] then
for p1 := 0 to nPl - 1 do
if (p1 <> p) and ((GAlive or GWatching) and (1 shl p1) <> 0) then
RW[p1].EnemyReport[p].Tech[ad] := Source;
end;
var
i: integer;
begin
if ad in FutureTech then
begin
if RW[p].Tech[ad] < tsApplicable then
RW[p].Tech[ad] := 1
else
inc(RW[p].Tech[ad]);
if ad <> futResearchTechnology then
inc(nTech[p], 2);
inc(Researched[p], 8);
exit;
end;
if RW[p].Tech[ad] = tsSeen then
begin
inc(nTech[p]);
inc(Researched[p]);
end
else
begin
inc(nTech[p], 2);
inc(Researched[p], 2);
end;
RW[p].Tech[ad] := tsResearched;
TellAboutKeyTech(p, tsResearched);
CheckSpecialModels(p, ad);
if ad = adScience then
ResourceMask[p] := ResourceMask[p] or fSpecial2;
if ad = adMassProduction then
ResourceMask[p] := ResourceMask[p] or fModern;
for i := 0 to nWonder - 1 do { check whether wonders expired }
if (GWonder[i].EffectiveOwner <> GWonder[woEiffel].EffectiveOwner) and
(Imp[i].Expiration = ad) then
begin
GWonder[i].EffectiveOwner := -1;
if i = woPyramids then
FreeSlaves;
end;
end;
procedure CheckExpiration(Wonder: integer);
// GWonder[Wonder].EffectiveOwner must be set before!
var
p: integer;
begin
if (Imp[Wonder].Expiration >= 0) and
(GWonder[woEiffel].EffectiveOwner <> GWonder[Wonder].EffectiveOwner) then
for p := 0 to nPl - 1 do // check if already expired
if (1 shl p and GAlive <> 0) and
(RW[p].Tech[Imp[Wonder].Expiration] >= tsApplicable) then
begin
GWonder[Wonder].EffectiveOwner := -1;
if Wonder = woPyramids then
FreeSlaves;
end;
end;
{
Location Navigation
____________________________________________________________________
}
function dLoc(Loc, dx, dy: integer): integer;
{ relative location, dx in hor and dy in ver direction from Loc }
var
y0: integer;
begin
if not ((Loc >= 0) and (Loc < MapSize) and (dx + lx >= 0)) then
raise Exception.Create('Relative location error');
assert((Loc >= 0) and (Loc < MapSize) and (dx + lx >= 0));
y0 := Loc div lx;
result := (Loc + (dx + y0 and 1 + lx + lx) shr 1) mod lx + lx * (y0 + dy);
if (result < 0) or (result >= MapSize) then
result := -1;
end;
procedure dxdy(Loc0, Loc1: integer; var dx, dy: integer);
begin
dx := ((Loc1 mod lx * 2 + Loc1 div lx and 1) -
(Loc0 mod lx * 2 + Loc0 div lx and 1) + 3 * lx) mod (2 * lx) - lx;
dy := Loc1 div lx - Loc0 div lx;
end;
function Distance(Loc0, Loc1: integer): integer;
var
dx, dy: integer;
begin
dxdy(Loc0, Loc1, dx, dy);
dx := abs(dx);
dy := abs(dy);
result := dx + dy + abs(dx - dy) shr 1;
end;
procedure V8_to_Loc(Loc0: integer; var VicinityLoc: TVicinity8Loc);
var
x0, y0, lx0: integer;
begin
lx0 := lx; // put in register!
y0 := Loc0 div lx0;
x0 := Loc0 - y0 * lx0; // Loc0 mod lx;
y0 := y0 and 1;
VicinityLoc[1] := Loc0 + lx0 * 2;
VicinityLoc[3] := Loc0 - 1;
VicinityLoc[5] := Loc0 - lx0 * 2;
VicinityLoc[7] := Loc0 + 1;
inc(Loc0, y0);
VicinityLoc[0] := Loc0 + lx0;
VicinityLoc[2] := Loc0 + lx0 - 1;
VicinityLoc[4] := Loc0 - lx0 - 1;
VicinityLoc[6] := Loc0 - lx0;
// world is round!
if x0 < lx0 - 1 then
begin
if x0 = 0 then
begin
inc(VicinityLoc[3], lx0);
if y0 = 0 then
begin
inc(VicinityLoc[2], lx0);
inc(VicinityLoc[4], lx0);
end;
end;
end
else
begin
dec(VicinityLoc[7], lx0);
if y0 = 1 then
begin
dec(VicinityLoc[0], lx0);
dec(VicinityLoc[6], lx0);
end;
end;
end;
procedure V21_to_Loc(Loc0: integer; var VicinityLoc: TVicinity21Loc);
var
dx, dy, bit, y0, xComp, yComp, xComp0, xCompSwitch: integer;
dst: ^integer;
begin
y0 := Loc0 div lx;
xComp0 := Loc0 - y0 * lx - 1; // Loc0 mod lx -1
xCompSwitch := xComp0 - 1 + y0 and 1;
if xComp0 < 0 then
inc(xComp0, lx);
if xCompSwitch < 0 then
inc(xCompSwitch, lx);
xCompSwitch := xCompSwitch xor xComp0;
yComp := lx * (y0 - 3);
dst := @VicinityLoc;
bit := 1;
for dy := 0 to 6 do
begin
xComp0 := xComp0 xor xCompSwitch;
xComp := xComp0;
for dx := 0 to 3 do
begin
if bit and $67F7F76 <> 0 then
dst^ := xComp + yComp
else
dst^ := -1;
inc(xComp);
if xComp >= lx then
dec(xComp, lx);
inc(dst);
bit := bit shl 1;
end;
inc(yComp, lx);
end;
end;
{
Map Creation
____________________________________________________________________
}
var
primitive: integer;
StartLoc, StartLoc2: array [0 .. nPl - 1] of integer; { starting coordinates }
Elevation: array [0 .. lxmax * lymax - 1] of Byte; { map elevation }
ElCount: array [Byte] of integer; { count of elevation occurance }
procedure CalculatePrimitive;
var
i, j: integer;
begin
primitive := 1;
i := 2;
while i * i <= MapSize + 1 do // test whether prime
begin
if (MapSize + 1) mod i = 0 then
primitive := 0;
inc(i);
end;
if primitive > 0 then
repeat
inc(primitive);
i := 1;
j := 0;
repeat
inc(j);
i := i * primitive mod (MapSize + 1);
until (i = 1) or (j = MapSize + 1);
until j = MapSize;
end;
function MapGeneratorAvailable: boolean;
begin
result := (primitive > 0) and (lx >= 20) and (ly >= 40);
end;
procedure CreateElevation;
const
d = 64;
Smooth = 0.049; { causes low amplitude of short waves }
Detail = 0.095; { causes short period of short waves }
Merge = 5; { elevation merging range at the connection line of the
round world,in relation to lx }
var
sa, ca, f1, f2: array [1 .. d] of single;
imerge, x, y: integer;
v, maxv: single;
function Value(x, y: integer): single; { elevation formula }
var
i: integer;
begin
result := 0;
for i := 1 to d do
result := result + sin(f1[i] * ((x * 2 + y and 1) * sa[i] + y * 1.5 *
ca[i])) * f2[i];
{ x values effectively multiplied with 2 to get 2 horizantal periods
of the prime waves }
end;
begin
for x := 1 to d do { prepare formula parameters }
begin
{$IFNDEF SCR} if x = 1 then
v := pi / 2 { first wave goes horizontal }
else {$ENDIF} v := DelphiRandom * 2 * pi;
sa[x] := sin(v) / lx;
ca[x] := cos(v) / ly;
f1[x] := 2 * pi * exp(Detail * (x - 1));
f2[x] := exp(-x * Smooth);
end;
imerge := 2 * lx div Merge;
FillChar(ElCount, SizeOf(ElCount), 0);
maxv := 0;
for x := 0 to lx - 1 do
for y := 0 to ly - 1 do
begin
v := Value(x, y);
if x * 2 < imerge then
v := (x * 2 * v + (imerge - x * 2) * Value(x + lx, y)) / imerge;
v := v - sqr(sqr(2 * y / ly - 1)); { soft cut at poles }
if v > maxv then
maxv := v;
if v < -4 then
Elevation[x + lx * y] := 0
else if v > 8.75 then
Elevation[x + lx * y] := 255
else
Elevation[x + lx * y] := Round((v + 4) * 20);
inc(ElCount[Elevation[x + lx * y]]);
end;
end;
procedure FindContinents;
procedure ReplaceCont(a, b, Stop: integer);
{ replace continent name a by b }
// make sure always continent[loc]<=loc
var
i: integer;
begin
if a < b then
begin
i := a;
a := b;
b := i
end;
if a > b then
for i := a to Stop do
if Continent[i] = a then
Continent[i] := b;
end;
var
x, y, Loc, Wrong: integer;
begin
for y := 1 to ly - 2 do
for x := 0 to lx - 1 do
begin
Loc := x + lx * y;
Continent[Loc] := -1;
if RealMap[Loc] and fTerrain >= fGrass then
begin
if (y - 2 >= 1) and (RealMap[Loc - 2 * lx] and fTerrain >= fGrass) then
Continent[Loc] := Continent[Loc - 2 * lx];
if (x - 1 + y and 1 >= 0) and (y - 1 >= 1) and
(RealMap[Loc - 1 + y and 1 - lx] and fTerrain >= fGrass) then
Continent[Loc] := Continent[Loc - 1 + y and 1 - lx];
if (x + y and 1 < lx) and (y - 1 >= 1) and
(RealMap[Loc + y and 1 - lx] and fTerrain >= fGrass) then
Continent[Loc] := Continent[Loc + y and 1 - lx];
if (x - 1 >= 0) and (RealMap[Loc - 1] and fTerrain >= fGrass) then
if Continent[Loc] = -1 then
Continent[Loc] := Continent[Loc - 1]
else
ReplaceCont(Continent[Loc - 1], Continent[Loc], Loc);
if Continent[Loc] = -1 then
Continent[Loc] := Loc;
end;
end;
{ connect continents due to round earth }
for y := 1 to ly - 2 do
if RealMap[lx * y] and fTerrain >= fGrass then
begin
Wrong := -1;
if RealMap[lx - 1 + lx * y] and fTerrain >= fGrass then
Wrong := Continent[lx - 1 + lx * y];
if (y and 1 = 0) and (y - 1 >= 1) and
(RealMap[lx - 1 + lx * (y - 1)] and fTerrain >= fGrass) then
Wrong := Continent[lx - 1 + lx * (y - 1)];
if (y and 1 = 0) and (y + 1 < ly - 1) and
(RealMap[lx - 1 + lx * (y + 1)] and fTerrain >= fGrass) then
Wrong := Continent[lx - 1 + lx * (y + 1)];
if Wrong >= 0 then
ReplaceCont(Wrong, Continent[lx * y], MapSize - 1);
end;
end;
procedure RarePositions;
// distribute rare resources
// must be done after FindContinents
type
tRareLoc = array [0 .. 11] of integer;
tRare = array [0 .. 7, 0 .. 4] of integer;
var
i, j, Cnt, x, y, dx, dy, Loc0, Loc1, xworst, yworst, totalrare, RareMaxWater,
RareType, iBest, jbest, MinDist, xBlock, yBlock, V8: integer;
AreaCount, RareByArea, RareAdjacent: tRare;
RareLoc: tRareLoc;
Dist: array [0 .. 11, 0 .. 11] of integer;
Adjacent: TVicinity8Loc;
begin
RareMaxWater := 0;
RareByArea := default (tRare);
repeat
FillChar(AreaCount, SizeOf(AreaCount), 0);
for y := 1 to ly - 2 do
begin
yBlock := y * 5 div ly;
if yBlock = (y + 1) * 5 div ly then
for x := 0 to lx - 1 do
begin
xBlock := x * 8 div lx;
if xBlock = (x + 1) * 8 div lx then
begin
Loc0 := x + lx * y;
if RealMap[Loc0] and fTerrain >= fGrass then
begin
Cnt := 0;
V8_to_Loc(Loc0, Adjacent);
for V8 := 0 to 7 do
begin
Loc1 := Adjacent[V8];
if (Loc1 >= 0) and (Loc1 < MapSize) and
(RealMap[Loc1] and fTerrain < fGrass) then
inc(Cnt); // count adjacent water
end;
if Cnt <= RareMaxWater then // inner land
begin
inc(AreaCount[xBlock, yBlock]);
if DelphiRandom(AreaCount[xBlock, yBlock]) = 0 then
RareByArea[xBlock, yBlock] := Loc0;
end;
end;
end;
end;
end;
totalrare := 0;
for x := 0 to 7 do
for y := 0 to 4 do
if AreaCount[x, y] > 0 then
inc(totalrare);
inc(RareMaxWater);
until totalrare >= 12;
while totalrare > 12 do
begin // remove rarebyarea resources too close to each other
FillChar(RareAdjacent, SizeOf(RareAdjacent), 0);
for x := 0 to 7 do
for y := 0 to 4 do
if AreaCount[x, y] > 0 then
begin
if (AreaCount[(x + 1) mod 8, y] > 0) and
(Continent[RareByArea[x, y]] = Continent
[RareByArea[(x + 1) mod 8, y]]) then
begin
inc(RareAdjacent[x, y]);
inc(RareAdjacent[(x + 1) mod 8, y]);
end;
if y < 4 then
begin
if (AreaCount[x, y + 1] > 0) and
(Continent[RareByArea[x, y]] = Continent[RareByArea[x, y + 1]])
then
begin
inc(RareAdjacent[x, y]);
inc(RareAdjacent[x, y + 1]);
end;
if (AreaCount[(x + 1) mod 8, y + 1] > 0) and
(Continent[RareByArea[x, y]] = Continent[RareByArea[(x + 1) mod 8,
y + 1]]) then
begin
inc(RareAdjacent[x, y]);
inc(RareAdjacent[(x + 1) mod 8, y + 1]);
end;
if (AreaCount[(x + 7) mod 8, y + 1] > 0) and
(Continent[RareByArea[x, y]] = Continent[RareByArea[(x + 7) mod 8,
y + 1]]) then
begin
inc(RareAdjacent[x, y]);
inc(RareAdjacent[(x + 7) mod 8, y + 1]);
end;
end;
end;
xworst := 0;
yworst := 0;
Cnt := 0;
for x := 0 to 7 do
for y := 0 to 4 do
if AreaCount[x, y] > 0 then
begin
if (Cnt = 0) or (RareAdjacent[x, y] > RareAdjacent[xworst, yworst])
then
begin
xworst := x;
yworst := y;
Cnt := 1;
end
else if (RareAdjacent[x, y] = RareAdjacent[xworst, yworst]) then
begin
inc(Cnt);
if DelphiRandom(Cnt) = 0 then
begin
xworst := x;
yworst := y;
end;
end;
end;
AreaCount[xworst, yworst] := 0;
dec(totalrare);
end;
Cnt := 0;
RareLoc := default (tRareLoc);
for x := 0 to 7 do
for y := 0 to 4 do
if AreaCount[x, y] > 0 then
begin
RareLoc[Cnt] := RareByArea[x, y];
inc(Cnt);
end;
for i := 0 to 11 do
begin
RealMap[RareLoc[i]] := RealMap[RareLoc[i]] and not(fTerrain or fSpecial) or
(fDesert or fDeadLands);
for dy := -1 to 1 do
for dx := -1 to 1 do
if (dx + dy) and 1 = 0 then
begin
Loc1 := dLoc(RareLoc[i], dx, dy);
if (Loc1 >= 0) and (RealMap[Loc1] and fTerrain = fMountains) then
RealMap[Loc1] := RealMap[Loc1] and not fTerrain or fHills;
end;
end;
for i := 0 to 11 do
for j := 0 to 11 do
Dist[i, j] := Distance(RareLoc[i], RareLoc[j]);
ibest := 0;
jbest := 0;
MinDist := Distance(0, MapSize - lx shr 1) shr 1;
for RareType := 1 to 3 do
begin
Cnt := 0;
for i := 0 to 11 do
if RareLoc[i] >= 0 then
for j := 0 to 11 do
if RareLoc[j] >= 0 then
if (Cnt > 0) and (Dist[iBest, jbest] >= MinDist) then
begin
if Dist[i, j] >= MinDist then
begin
inc(Cnt);
if DelphiRandom(Cnt) = 0 then
begin
iBest := i;
jbest := j;
end;
end;
end
else if (Cnt = 0) or (Dist[i, j] > Dist[iBest, jbest]) then
begin
iBest := i;
jbest := j;
Cnt := 1;
end;
RealMap[RareLoc[iBest]] := RealMap[RareLoc[iBest]] or
Cardinal(RareType) shl 25;
RealMap[RareLoc[jbest]] := RealMap[RareLoc[jbest]] or
Cardinal(RareType) shl 25;
RareLoc[iBest] := -1;
RareLoc[jbest] := -1;
end;
end;
function CheckShore(Loc: integer): boolean;
var
Loc1, OldTile, V21: integer;
Radius: TVicinity21Loc;
begin
result := false;
OldTile := RealMap[Loc];
if OldTile and fTerrain < fGrass then
begin
RealMap[Loc] := RealMap[Loc] and not fTerrain or fOcean;
V21_to_Loc(Loc, Radius);
for V21 := 1 to 26 do
begin
Loc1 := Radius[V21];
if (Loc1 >= 0) and (Loc1 < MapSize) and
(RealMap[Loc1] and fTerrain >= fGrass) and
(RealMap[Loc1] and fTerrain <> fArctic) then
RealMap[Loc] := RealMap[Loc] and not fTerrain or fShore;
end;
if (RealMap[Loc] xor Cardinal(OldTile)) and fTerrain <> 0 then
result := true;
end;
end;
function ActualSpecialTile(Loc: integer): Cardinal;
begin
result := SpecialTile(Loc, RealMap[Loc] and fTerrain, lx);
end;
procedure CreateMap(preview: boolean);
const
ShHiHills = 6; { of land }
ShMountains = 6; { of land }
ShRandHills = 12; { of land }
ShTestRiver = 40;
ShSwamp = 25; { of grassland }
MinRivLen = 3;
unification = 70;
hotunification = 50; // min. 25
Zone: array [0 .. 3, 2 .. 9] of single = { terrain distribution }
((0.25, 0, 0, 0.4, 0, 0, 0, 0.35), (0.55, 0, 0.1, 0, 0, 0, 0, 0.35),
(0.4, 0, 0.35, 0, 0, 0, 0, 0.25), (0, 0.7, 0, 0, 0, 0, 0, 0.3));
{ Grs Dst Pra Tun - - - For }
function RndLow(y: integer): Cardinal;
{ random lowland appropriate to climate }
var
z0, i: integer;
p, p0, ZPlus: single;
begin
RndLow := 0;
if ly - 1 - y > y then
begin
z0 := 6 * y div ly;
ZPlus := 6 * y / ly - z0;
end
else
begin
z0 := 6 * (ly - 1 - y) div ly;
ZPlus := 6 * (ly - 1 - y) / ly - z0;
end;
p0 := 1;
for i := 2 to 9 do
begin
p := Zone[z0, i] * (1 - ZPlus) + Zone[z0 + 1, i] * ZPlus;
{ weight between zones z0 and z0+1 }
if DelphiRandom * p0 < p then
begin
RndLow := i;
Break;
end;
p0 := p0 - p;
end;
end;
function RunRiver(Loc0: integer): integer;
{ runs river from start point Loc0; return value: length }
var
Dir, T, Loc, Loc1, Cost: integer;
Q: TIPQ;
From,
Time: tMapType;
OneTileLake: boolean;
begin
FillChar(Time, SizeOf(Time), 255); { -1 }
From := default (tMapType);
Q := TIPQ.Create(MapSize);
Q.Put(Loc0, 0);
while Q.Get(Loc, T) and (RealMap[Loc] and fRiver = 0) do
begin
if (RealMap[Loc] and fTerrain < fGrass) then
begin
OneTileLake := true;
for Dir := 0 to 3 do
begin
Loc1 := dLoc(Loc, Dir and 1 * 2 - 1, Dir shr 1 * 2 - 1);
if (Loc1 >= 0) and (RealMap[Loc1] and fTerrain < fGrass) then
OneTileLake := false;
end;
if not OneTileLake then
Break;
end;
Time[Loc] := T;
for Dir := 0 to 3 do
begin
Loc1 := dLoc(Loc, Dir and 1 * 2 - 1, Dir shr 1 * 2 - 1);
if (Loc1 >= lx) and (Loc1 < lx * (ly - 1)) and (Time[Loc1] < 0) then
begin
if RealMap[Loc1] and fRiver = 0 then
begin
Cost := Elevation[Loc1] - Elevation[Loc];
if Cost < 0 then
Cost := 0;
end
else
Cost := 0;
if Q.Put(Loc1, T + Cost shl 8 + 1) then
From[Loc1] := Loc;
end;
end;
end;
Loc1 := Loc;
result := 0;
while Loc <> Loc0 do
begin
Loc := From[Loc];
inc(result);
end;
if (result > 1) and ((result >= MinRivLen) or
(RealMap[Loc1] and fTerrain >= fGrass)) then
begin
Loc := Loc1;
while Loc <> Loc0 do
begin
Loc := From[Loc];
if RealMap[Loc] and fTerrain in [fHills, fMountains] then
RealMap[Loc] := fGrass or fRiver
else if RealMap[Loc] and fTerrain >= fGrass then
RealMap[Loc] := RealMap[Loc] or fRiver;
end;
end
else
result := 0;
FreeAndNil(Q);
end;
var
x, y, n, Dir, plus, Count, Loc0, Loc1, bLand, bHills, bMountains, V8: integer;
CopyFrom: tMapType;
Adjacent: TVicinity8Loc;
begin
FillChar(RealMap, MapSize * SizeOf(Cardinal), 0);
plus := 0;
bMountains := 256;
while plus < MapSize * LandMass * ShMountains div 10000 do
begin
dec(bMountains);
inc(plus, ElCount[bMountains]);
end;
Count := plus;
plus := 0;
bHills := bMountains;
while plus < MapSize * LandMass * ShHiHills div 10000 do
begin
dec(bHills);
inc(plus, ElCount[bHills]);
end;
inc(Count, plus);
bLand := bHills;
while Count < MapSize * LandMass div 100 do
begin
dec(bLand);
inc(Count, ElCount[bLand]);
end;
for Loc0 := lx to lx * (ly - 1) - 1 do
if Elevation[Loc0] >= bMountains then
RealMap[Loc0] := fMountains
else if Elevation[Loc0] >= bHills then
RealMap[Loc0] := fHills
else if Elevation[Loc0] >= bLand then
RealMap[Loc0] := fGrass;
// remove one-tile islands
for Loc0 := 0 to MapSize - 1 do
if RealMap[Loc0] >= fGrass then
begin
Count := 0;
V8_to_Loc(Loc0, Adjacent);
for V8 := 0 to 7 do
begin
Loc1 := Adjacent[V8];
if (Loc1 < 0) or (Loc1 >= MapSize) or
(RealMap[Loc1] and fTerrain < fGrass) or
(RealMap[Loc1] and fTerrain = fArctic) then
inc(Count); // count adjacent water
end;
if Count = 8 then
RealMap[Loc0] := fOcean;
end;
if not preview then
begin
plus := 36 * 56 * 20 * ShTestRiver div (LandMass * 100);
if plus > MapSize then
plus := MapSize;
Loc0 := DelphiRandom(MapSize);
for n := 0 to plus - 1 do
begin
if (RealMap[Loc0] and fTerrain >= fGrass) and (Loc0 >= lx) and
(Loc0 < MapSize - lx) then
RunRiver(Loc0);
Loc0 := (Loc0 + 1) * primitive mod (MapSize + 1) - 1;
end;
end;
for Loc0 := 0 to MapSize - 1 do
if (RealMap[Loc0] = fGrass) and (DelphiRandom(100) < ShRandHills) then
RealMap[Loc0] := RealMap[Loc0] or fHills;
// make terrain types coherent
CopyFrom := default (tMapType);
for Loc0 := 0 to MapSize - 1 do
CopyFrom[Loc0] := Loc0;
for n := 0 to unification * MapSize div 100 do
begin
y := DelphiRandom(ly);
if abs(y - (ly shr 1)) > ly div 4 + DelphiRandom(ly * hotunification div 100) then
if y < ly shr 1 then
y := ly shr 1 - y
else
y := 3 * ly shr 1 - y;
Loc0 := lx * y + DelphiRandom(lx);
if RealMap[Loc0] and fTerrain = fGrass then
begin
Dir := DelphiRandom(4);
Loc1 := dLoc(Loc0, Dir and 1 * 2 - 1, Dir shr 1 * 2 - 1);
if (Loc1 >= 0) and (RealMap[Loc1] and fTerrain = fGrass) then
begin
while CopyFrom[Loc0] <> Loc0 do
Loc0 := CopyFrom[Loc0];
while CopyFrom[Loc1] <> Loc1 do
Loc1 := CopyFrom[Loc1];
if Loc1 < Loc0 then
CopyFrom[Loc0] := Loc1
else
CopyFrom[Loc1] := Loc0;
end;
end;
end;
for Loc0 := 0 to MapSize - 1 do
if (RealMap[Loc0] and fTerrain = fGrass) and (CopyFrom[Loc0] = Loc0) then
RealMap[Loc0] := RealMap[Loc0] and not fTerrain or RndLow(Loc0 div lx);
for Loc0 := 0 to MapSize - 1 do
if RealMap[Loc0] and fTerrain = fGrass then
begin
Loc1 := Loc0;
while CopyFrom[Loc1] <> Loc1 do
Loc1 := CopyFrom[Loc1];
RealMap[Loc0] := RealMap[Loc0] and not fTerrain or
RealMap[Loc1] and fTerrain;
end;
for Loc0 := 0 to MapSize - 1 do
if RealMap[Loc0] and fTerrain = fGrass then
begin // change grassland to swamp
if DelphiRandom(100) < ShSwamp then
RealMap[Loc0] := RealMap[Loc0] and not fTerrain or fSwamp;
end;
for Loc0 := 0 to MapSize - 1 do // change desert to prairie 1
if RealMap[Loc0] and fTerrain = fDesert then
begin
if RealMap[Loc0] and fRiver <> 0 then
Count := 5
else
begin
Count := 0;
for Dir := 0 to 3 do
begin
Loc1 := dLoc(Loc0, Dir and 1 * 2 - 1, Dir shr 1 * 2 - 1);
if Loc1 >= 0 then
if RealMap[Loc1] and fTerrain < fGrass then
inc(Count, 2);
end;
end;
if Count >= 4 then
RealMap[Loc0] := RealMap[Loc0] and not fTerrain or fPrairie;
end;
for Loc0 := 0 to MapSize - 1 do // change desert to prairie 2
if RealMap[Loc0] and fTerrain = fDesert then
begin
Count := 0;
for Dir := 0 to 3 do
begin
Loc1 := dLoc(Loc0, Dir and 1 * 2 - 1, Dir shr 1 * 2 - 1);
if Loc1 >= 0 then
if RealMap[Loc1] and fTerrain <> fDesert then
inc(Count);
end;
if Count >= 4 then
RealMap[Loc0] := RealMap[Loc0] and not fTerrain or fPrairie;
end;
for Loc0 := 0 to MapSize - 1 do
CheckShore(Loc0); // change ocean to shore
for x := 0 to lx - 1 do
begin
RealMap[x + lx * 0] := fArctic;
if RealMap[x + lx * 1] >= fGrass then
RealMap[x + lx * 1] := RealMap[x + lx * 1] and not fTerrain or fTundra;
if RealMap[x + lx * (ly - 2)] >= fGrass then
RealMap[x + lx * (ly - 2)] := RealMap[x + lx * (ly - 2)] and
not fTerrain or fTundra;
RealMap[x + lx * (ly - 1)] := fArctic;
end;
for Loc0 := 0 to MapSize - 1 do // define special terrain tiles
RealMap[Loc0] := RealMap[Loc0] or ActualSpecialTile(Loc0) shl 5 or
($F shl 27);
if not preview then
begin
FindContinents;
RarePositions;
end;
end;
procedure StartPositions;
// define nation start positions
// must be done after FindContinents
var
CountGood: (cgBest, cgFlat, cgLand);
function IsGoodTile(Loc: integer): boolean;
var
xLoc, yLoc: integer;
begin
xLoc := Loc mod lx;
yLoc := Loc div lx;
if RealMap[Loc] and fDeadLands <> 0 then
result := false
else
case CountGood of
cgBest:
result := (RealMap[Loc] and fTerrain in [fGrass, fPrairie, fTundra,
fSwamp, fForest]) and Odd((lymax + xLoc - yLoc shr 1) shr 1 + xLoc +
(yLoc + 1) shr 1);
cgFlat:
result := (RealMap[Loc] and fTerrain in [fGrass, fPrairie, fTundra,
fSwamp, fForest]);
cgLand:
result := RealMap[Loc] and fTerrain >= fGrass;
otherwise
result := false;
end;
end;
const
MaxCityLoc = 64;
type
tIrrLoc = array [0 .. 20] of integer;
tAIPlayers = array [1 .. nPl] of integer;
tRestLoc = array [0 .. MaxCityLoc - 1] of integer;
tCityLoc = array [1 .. nPl, 0 .. MaxCityLoc - 1] of integer;
var
p1, p2, nAlive, c, Loc, Loc1, CntGood, CntGoodGrass, MinDist, i, j, n,
nsc, V21, V8, BestDist, TestDist, MinGood, nIrrLoc,
FineDistSQR, nRest: integer;
ccount: array [0 .. lxmax * lymax - 1] of word;
sc, StartLoc0, sccount: tAIPlayers;
TestStartLoc: array [0 .. nPl - 1] of integer;
CityLoc: tCityLoc;
nCityLoc: array [1 .. nPl] of integer;
RestLoc: tRestLoc;
IrrLoc: tIrrLoc;
Radius: TVicinity21Loc;
Adjacent: TVicinity8Loc;
ok: boolean;
// RestLoc CityLoc
begin
nAlive := 0;
for p1 := 0 to nPl - 1 do
if 1 shl p1 and GAlive <> 0 then
inc(nAlive);
if nAlive = 0 then
exit;
// Default initialisation
StartLoc0 := default (tAIPlayers);
sc := default (tAIPlayers);
sccount := default (tAIPlayers);
CityLoc := default (tCityLoc);
RestLoc := default (tRestLoc);
BestDist := 0;
n := 0;
{ count good tiles }
FillChar(ccount, MapSize * 2, 0);
for Loc := 0 to MapSize - 1 do
if RealMap[Loc] and fTerrain = fGrass then
if ActualSpecialTile(Loc) = 1 then
inc(ccount[Continent[Loc]], 3)
else
inc(ccount[Continent[Loc]], 2)
else if RealMap[Loc] and fTerrain in [fPrairie, fSwamp, fForest, fHills]
then
inc(ccount[Continent[Loc]]);
Loc := 0;
while ccount[Loc] > 0 do
inc(Loc);
for i := 1 to nAlive do
begin
sc[i] := Loc;
sccount[i] := 1
end;
{ init with zero size start continents, then search bigger ones }
for Loc := 0 to MapSize - 1 do
if ccount[Loc] > 0 then
begin // search biggest continents
p1 := nAlive + 1;
while (p1 > 1) and (ccount[Loc] > ccount[sc[p1 - 1]]) do
begin
if p1 < nAlive + 1 then
sc[p1] := sc[p1 - 1];
dec(p1);
end;
if p1 < nAlive + 1 then
sc[p1] := Loc;
end;
nsc := nAlive;
repeat
c := 1; // search least crowded continent after smallest
for i := 2 to nsc - 1 do
if ccount[sc[i]] * (2 * sccount[c] + 1) > ccount[sc[c]] *
(2 * sccount[i] + 1) then
c := i;
if ccount[sc[nsc]] * (2 * sccount[c] + 1) > ccount[sc[c]] then
Break; // even least crowded continent is more crowded than smallest
inc(sccount[c]);
dec(nsc);
until sccount[nsc] > 1;
MinGood := 7;
CountGood := cgBest;
repeat
dec(MinGood);
if (MinGood = 3) and (CountGood < cgLand) then // too demanding!
begin
inc(CountGood);
MinGood := 6;
end;
FillChar(nCityLoc, SizeOf(nCityLoc), 0);
Loc := DelphiRandom(MapSize);
for i := 0 to MapSize - 1 do
begin
if ((Loc >= 4 * lx) and (Loc < MapSize - 4 * lx) or (CountGood >= cgLand))
and IsGoodTile(Loc) then
begin
c := nsc;
while (c > 0) and (Continent[Loc] <> sc[c]) do
dec(c);
if (c > 0) and (nCityLoc[c] < MaxCityLoc) then
begin
CntGood := 1;
V21_to_Loc(Loc, Radius);
for V21 := 1 to 26 do
if V21 <> CityOwnTile then
begin
Loc1 := Radius[V21];
if (Loc1 >= 0) and (Loc1 < MapSize) and IsGoodTile(Loc1) then
inc(CntGood);
end;
if CntGood >= MinGood then
begin
CityLoc[c, nCityLoc[c]] := Loc;
inc(nCityLoc[c]);
end;
end;
end;
Loc := (Loc + 1) * primitive mod (MapSize + 1) - 1;
end;
ok := true;
for c := 1 to nsc do
if nCityLoc[c] < sccount[c] * (8 - MinGood) div (7 - MinGood) then
ok := false;
until ok;
FineDistSQR := MapSize * LandMass * 9 div (nAlive * 100);
p1 := 1;
for c := 1 to nsc do
begin // for all start continents
if sccount[c] = 1 then
StartLoc0[p1] := CityLoc[c, DelphiRandom(nCityLoc[c])]
else
begin
BestDist := 0;
n := 1 shl sccount[c] * 32; // number of tries to find good distribution
if n > 1 shl 12 then
n := 1 shl 12;
while (n > 0) and (BestDist * BestDist < FineDistSQR) do
begin
MinDist := MaxInt;
nRest := nCityLoc[c];
for i := 0 to nRest - 1 do
RestLoc[i] := CityLoc[c, i];
for i := 0 to sccount[c] - 1 do
begin
if nRest = 0 then
Break;
j := DelphiRandom(nRest);
TestStartLoc[i] := RestLoc[j];
RestLoc[j] := RestLoc[nRest - 1];
dec(nRest);
for j := 0 to i - 1 do
begin
TestDist := Distance(TestStartLoc[i], TestStartLoc[j]);
if TestDist < MinDist then
MinDist := TestDist;
end;
if i = sccount[c] - 1 then
begin
assert(MinDist > BestDist, IntToStr(MinDist) + ' ' + IntToStr(BestDist));
BestDist := MinDist;
for j := 0 to sccount[c] - 1 do
StartLoc0[p1 + j] := TestStartLoc[j];
end
else if BestDist > 0 then
begin
j := 0;
while j < nRest do
begin // remove all locs from rest which have too little distance to this one
TestDist := Distance(TestStartLoc[i], RestLoc[j]);
if TestDist <= BestDist then
begin
RestLoc[j] := RestLoc[nRest - 1];
dec(nRest);
end
else
inc(j);
end;
end;
end;
dec(n)
end;
end;
p1 := p1 + sccount[c]
end;
// make start locs fertile
for p1 := 1 to nAlive do
begin
RealMap[StartLoc0[p1]] := RealMap[StartLoc0[p1]] and
not(fTerrain or fSpecial) or fGrass or fSpecial1;
CntGood := 1;
CntGoodGrass := 1;
V21_to_Loc(StartLoc0[p1], Radius);
for V21 := 1 to 26 do
if V21 <> CityOwnTile then
begin
Loc1 := Radius[V21];
if (Loc1 >= 0) and (Loc1 < MapSize) and IsGoodTile(Loc1) then
if RealMap[Loc1] and fTerrain = fGrass then
inc(CntGoodGrass)
else
inc(CntGood);
end;
for V21 := 1 to 26 do
if V21 <> CityOwnTile then
begin
Loc1 := Radius[V21];
if (Loc1 >= 0) and (Loc1 < MapSize) and
(RealMap[Loc1] and fDeadLands = 0) then
if IsGoodTile(Loc1) and (DelphiRandom(CntGood) < MinGood - CntGoodGrass + 1)
then
begin
RealMap[Loc1] := RealMap[Loc1] and not(fTerrain or fSpecial)
or fGrass;
RealMap[Loc1] := RealMap[Loc1] or ActualSpecialTile(Loc1) shl 5;
end
else if RealMap[Loc1] and fTerrain = fDesert then
RealMap[Loc1] := RealMap[Loc1] and not fTerrain or fPrairie
else if (RealMap[Loc1] and fTerrain in [fPrairie, fTundra, fSwamp])
and (DelphiRandom(2) = 0) then
RealMap[Loc1] := RealMap[Loc1] and not fTerrain or fForest;
end;
// first irrigation
nIrrLoc := 0;
IrrLoc := default (tIrrLoc);
for V21 := 1 to 26 do
if V21 <> CityOwnTile then
begin
Loc1 := Radius[V21];
if (Loc1 >= 0) and (Loc1 < MapSize) and
(RealMap[Loc1] and (fTerrain or fSpecial) = fGrass or fSpecial1) then
begin
IrrLoc[nIrrLoc] := Loc1;
inc(nIrrLoc);
end;
end;
i := 2;
if i > nIrrLoc then
i := nIrrLoc;
while i > 0 do
begin
j := DelphiRandom(nIrrLoc);
RealMap[IrrLoc[j]] := RealMap[IrrLoc[j]] or tiIrrigation;
IrrLoc[j] := IrrLoc[nIrrLoc - 1];
dec(nIrrLoc);
dec(i);
end;
end;
StartLoc[0] := 0;
for p1 := 0 to nPl - 1 do
if 1 shl p1 and GAlive <> 0 then
begin
repeat
i := DelphiRandom(nAlive) + 1
until StartLoc0[i] >= 0;
StartLoc[p1] := StartLoc0[i];
StartLoc0[i] := -1
end;
SaveMapCenterLoc := StartLoc[0];
// second unit starting position
for p1 := 0 to nPl - 1 do
if 1 shl p1 and GAlive <> 0 then
begin
StartLoc2[p1] := StartLoc[p1];
V8_to_Loc(StartLoc[p1], Adjacent);
for V8 := 0 to 7 do
begin
Loc1 := Adjacent[V8];
for p2 := 0 to nPl - 1 do
if (1 shl p2 and GAlive <> 0) and (StartLoc[p2] = Loc1) then
Loc1 := -1;
for p2 := 0 to p1 - 1 do
if (1 shl p2 and GAlive <> 0) and (StartLoc2[p2] = Loc1) then
Loc1 := -1;
if (Loc1 < 0) or (Loc1 >= MapSize) or
(RealMap[Loc1] and fTerrain in [fOcean, fShore, fDesert, fArctic,
fMountains]) or (RealMap[Loc1] and fDeadLands <> 0) then
TestDist := -1
else if RealMap[Loc1] and fTerrain = fGrass then
TestDist := 2
else if Terrain[RealMap[Loc1] and fTerrain].IrrEff > 0 then
TestDist := 1
else
TestDist := 0;
if (StartLoc2[p1] = StartLoc[p1]) or (TestDist > BestDist) then
begin
StartLoc2[p1] := Loc1;
BestDist := TestDist;
n := 1;
end
else if TestDist = BestDist then
begin
inc(n);
if DelphiRandom(n) = 0 then
StartLoc2[p1] := Loc1;
end;
end;
end;
end;
procedure PredefinedStartPositions(Human: integer);
// use predefined nation start positions
var
i, p1, Loc1, nAlive, nStartLoc0, nPrefStartLoc0, imax: integer;
StartLoc0: array [0 .. lxmax * lymax - 1] of integer;
ishuman: boolean;
begin
nAlive := 0;
for p1 := 0 to nPl - 1 do
if 1 shl p1 and GAlive <> 0 then
inc(nAlive);
if nAlive = 0 then
exit;
for I := 0 to Length(StartLoc0) - 1 do
StartLoc0[I] := 0;
// calculate starting positions
nStartLoc0 := 0;
nPrefStartLoc0 := 0;
for Loc1 := 0 to MapSize - 1 do
if RealMap[Loc1] and fPrefStartPos <> 0 then
begin
StartLoc0[nStartLoc0] := StartLoc0[nPrefStartLoc0];
StartLoc0[nPrefStartLoc0] := Loc1;
inc(nPrefStartLoc0);
inc(nStartLoc0);
RealMap[Loc1] := RealMap[Loc1] and not fPrefStartPos;
end
else if RealMap[Loc1] and fStartPos <> 0 then
begin
StartLoc0[nStartLoc0] := Loc1;
inc(nStartLoc0);
RealMap[Loc1] := RealMap[Loc1] and not fStartPos;
end;
assert(nStartLoc0 >= nAlive);
StartLoc[0] := 0;
for ishuman := true downto false do
for p1 := 0 to nPl - 1 do
if (1 shl p1 and GAlive <> 0) and ((1 shl p1 and Human <> 0) = ishuman)
then
begin
dec(nStartLoc0);
imax := nStartLoc0;
if nPrefStartLoc0 > 0 then
begin
dec(nPrefStartLoc0);
imax := nPrefStartLoc0;
end;
i := DelphiRandom(imax + 1);
StartLoc[p1] := StartLoc0[i];
StartLoc2[p1] := StartLoc0[i];
StartLoc0[i] := StartLoc0[imax];
StartLoc0[imax] := StartLoc0[nStartLoc0];
end;
SaveMapCenterLoc := StartLoc[0];
end;
procedure InitGame;
var
i, p, p1, uix, Loc1: integer;
begin
{$IFDEF FastContact}
{ Railroad everywhere }
for Loc1 := 0 to MapSize - 1 do
if RealMap[Loc1] and fTerrain >= fGrass then
RealMap[Loc1] := RealMap[Loc1] or fRR;
{$ENDIF}
{ !!!for Loc1:=0 to MapSize-1 do
if RealMap[Loc1] and fterrain>=fGrass then
if Delphirandom(3)=0 then RealMap[Loc1]:=RealMap[Loc1] or fRoad
else if Delphirandom(3)=0 then RealMap[Loc1]:=RealMap[Loc1] or fRR;
{random Road and Railroad }
{ !!!for Loc1:=0 to MapSize-1 do
if (RealMap[Loc1] and fterrain>=fGrass) and (Delphirandom(20)=0) then
RealMap[Loc1]:=RealMap[Loc1] or fPoll; }
FillChar(Occupant, MapSize, Byte(-1));
FillChar(ZoCMap, MapSize, 0);
FillChar(ObserveLevel, MapSize * 4, 0);
FillChar(UsedByCity, MapSize * 4, Byte(-1));
GTestFlags := 0;
GInitialized := GAlive or GWatching;
for p := 0 to nPl - 1 do
if 1 shl p and GInitialized <> 0 then
with RW[p] do
begin
Researched[p] := 0;
Discovered[p] := 0;
TerritoryCount[p] := 0;
nTech[p] := 0;
if Difficulty[p] = 0 then
ResourceMask[p] := $FFFFFFFF
else
ResourceMask[p] := $FFFFFFFF and not(fSpecial2 or fModern);
GrWallContinent[p] := -1;
GetMem(Map, 4 * MapSize);
GetMem(MapObservedLast, 2 * MapSize);
FillChar(MapObservedLast^, 2 * MapSize, Byte(-1));
GetMem(Territory, MapSize);
FillChar(Territory^, MapSize, $FF);
New (Un);
New (Model); // draft needs one model behind last
New (City);
New (EnemyUn);
New (EnemyModel);
New (EnemyCity);
for p1 := 0 to nPl - 1 do
begin
if 1 shl p1 and GInitialized <> 0 then
begin
FillChar(RWemix[p, p1], SizeOf(RWemix[p, p1]), 255); { -1 }
FillChar(Destroyed[p, p1], SizeOf(Destroyed[p, p1]), 0);
end;
Attitude[p1] := atNeutral;
Treaty[p1] := trNoContact;
LastCancelTreaty[p1] := -CancelTreatyTurns - 1;
EvaStart[p1] := -PeaceEvaTurns - 1;
Tribute[p1] := 0;
TributePaid[p1] := 0;
if (p1 <> p) and (1 shl p1 and GAlive <> 0) then
begin // initialize enemy report
GetMem(EnemyReport[p1], SizeOf(TEnemyReport) - 2 * (INFIN + 1 - nmmax));
FillChar(EnemyReport[p1].Tech, nAdv, Byte(tsNA));
EnemyReport[p1].TurnOfContact := -1;
EnemyReport[p1].TurnOfCivilReport := -1;
EnemyReport[p1].TurnOfMilReport := -1;
EnemyReport[p1].Attitude := atNeutral;
EnemyReport[p1].Government := gDespotism;
if 1 shl p and GAlive = 0 then
Treaty[p1] := trNone // supervisor
end
else
EnemyReport[p1] := nil;
end;
TestFlags := GTestFlags;
Credibility := InitialCredibility;
MaxCredibility := 100;
nUn := 0;
nModel := 0;
nCity := 0;
nEnemyUn := 0;
nEnemyCity := 0;
nEnemyModel := 0;
for Loc1 := 0 to MapSize - 1 do
Map[Loc1] := fUNKNOWN;
FillChar(Tech, nAdv, Byte(tsNA));
FillChar(NatBuilt, SizeOf(NatBuilt), 0);
end;
// create initial models and units
for p := 0 to nPl - 1 do
if (1 shl p and GAlive <> 0) then
with RW[p] do
begin
nModel := 0;
for i := 0 to nSpecialModel - 1 do
if SpecialModelPreq[i] = preNone then
begin
Model[nModel] := SpecialModel[i];
Model[nModel].Status := 0;
Model[nModel].IntroTurn := 0;
Model[nModel].Built := 0;
Model[nModel].Lost := 0;
Model[nModel].ID := p shl 12 + nModel;
SetModelFlags(Model[nModel]);
inc(nModel);
end;
nUn := 0;
UnBuilt[p] := 0;
for uix := 0 to nStartUn - 1 do
begin
CreateUnit(p, StartUn[uix]);
dec(Model[StartUn[uix]].Built);
Un[uix].Loc := StartLoc2[p];
PlaceUnit(p, uix);
end;
FoundCity(p, StartLoc[p]); // capital
Founded[p] := 1;
with City[0] do
begin
ID := p shl 12;
Flags := chFounded;
end;
end;
TerritoryCount[nPl] := MapSize;
// fillchar(NewContact, sizeof(NewContact), false);
end;
procedure InitRandomGame;
begin
DelphiRandSeed := RND;
CalculatePrimitive;
CreateElevation;
CreateMap(false);
StartPositions;
InitGame;
end;
procedure InitMapGame(Human: integer);
begin
DelphiRandSeed := RND;
FindContinents;
PredefinedStartPositions(Human);
InitGame;
end;
procedure ReleaseGame;
var
p1, p2: integer;
begin
for p1 := 0 to nPl - 1 do
if 1 shl p1 and GInitialized <> 0 then
begin
for p2 := 0 to nPl - 1 do
if RW[p1].EnemyReport[p2] <> nil then
FreeMem(RW[p1].EnemyReport[p2]);
FreeMem(RW[p1].EnemyUn);
FreeMem(RW[p1].EnemyCity);
FreeMem(RW[p1].EnemyModel);
FreeMem(RW[p1].Un);
FreeMem(RW[p1].City);
FreeMem(RW[p1].Model);
FreeMem(RW[p1].Territory);
FreeMem(RW[p1].MapObservedLast);
FreeMem(RW[p1].Map);
end;
end;
procedure InitMapEditor;
var
p1: integer;
begin
CalculatePrimitive;
FillChar(Occupant, MapSize, Byte(-1));
FillChar(ObserveLevel, MapSize * 4, 0);
with RW[0] do
begin
ResourceMask[0] := $FFFFFFFF;
GetMem(Map, 4 * MapSize);
GetMem(MapObservedLast, 2 * MapSize);
FillChar(MapObservedLast^, 2 * MapSize, Byte(-1));
GetMem(Territory, MapSize);
FillChar(Territory^, MapSize, $FF);
Un := nil;
Model := nil;
City := nil;
EnemyUn := nil;
EnemyCity := nil;
EnemyModel := nil;
for p1 := 0 to nPl - 1 do
EnemyReport[p1] := nil;
nUn := 0;
nModel := 0;
nCity := 0;
nEnemyUn := 0;
nEnemyCity := 0;
nEnemyModel := 0;
end;
end;
procedure ReleaseMapEditor;
begin
FreeMem(RW[0].Territory);
FreeMem(RW[0].MapObservedLast);
FreeMem(RW[0].Map);
end;
procedure EditTile(Loc, NewTile: integer);
var
Loc1, V21: integer;
Radius: TVicinity21Loc;
begin
if NewTile and fDeadLands <> 0 then
NewTile := NewTile and (fDeadLands or fModern or fRiver) or fDesert;
case NewTile and fTerrain of
fOcean, fShore:
NewTile := NewTile and (fTerrain or fSpecial);
fMountains, fArctic:
NewTile := NewTile and not fRiver;
end;
with Terrain[NewTile and fTerrain] do
if (ClearTerrain >= 0) or (AfforestTerrain >= 0) or (TransTerrain >= 0) then
NewTile := NewTile or fSpecial;
// only automatic special resources for transformable tiles
if NewTile and fRR <> 0 then
NewTile := NewTile and not fRoad;
if not((NewTile and fTerrain) in TerrType_Canalable) then
NewTile := NewTile and not fCanal;
if Terrain[NewTile and fTerrain].IrrEff = 0 then
begin
NewTile := NewTile and not(fPrefStartPos or fStartPos);
if (NewTile and fTerImp = tiIrrigation) or (NewTile and fTerImp = tiFarm)
then
NewTile := NewTile and not fTerImp;
end;
if (Terrain[NewTile and fTerrain].MineEff = 0) and
(NewTile and fTerImp = tiMine) then
NewTile := NewTile and not fTerImp;
RealMap[Loc] := NewTile;
if NewTile and fSpecial = fSpecial then
// standard special resource distribution
RealMap[Loc] := RealMap[Loc] and not fSpecial or
ActualSpecialTile(Loc) shl 5;
// automatic shore tiles
V21_to_Loc(Loc, Radius);
for V21 := 1 to 26 do
begin
Loc1 := Radius[V21];
if (Loc1 >= 0) and (Loc1 < MapSize) then
begin
if CheckShore(Loc1) then
RealMap[Loc1] := RealMap[Loc1] and not fSpecial or
ActualSpecialTile(Loc1) shl 5;
RealMap[Loc1] := RealMap[Loc1] or ($F shl 27);
RW[0].Map[Loc1] := RealMap[Loc1] and $07FFFFFF or fObserved;
end;
end;
// RealMap[Loc]:=RealMap[Loc] and not fSpecial;
// RW[0].Map[Loc]:=RealMap[Loc] or fObserved;
end;
{
Map Revealing
____________________________________________________________________
}
function GetTileInfo(p, cix, Loc: integer; var Info: TTileInfo): integer;
// cix>=0 - known city index of player p -- only core internal!
// cix=-1 - search city, player unknown, only if permission for p
// cix=-2 - don't search city, don't calculate city benefits, just government of player p
var
p0, Tile, special: integer;
begin
with Info do
begin
p0 := p;
if cix >= 0 then
Tile := RealMap[Loc]
else
begin
Tile := RW[p].Map[Loc];
if Tile and fTerrain = fUNKNOWN then
begin
result := eNoPreq;
exit;
end;
end;
if (cix = -1) and (UsedByCity[Loc] >= 0) then
begin // search exploiting player and city
SearchCity(UsedByCity[Loc], p, cix);
if not((p = p0) or (ObserveLevel[UsedByCity[Loc]] shr (2 * p0) and
3 = lObserveSuper)) then
cix := -1
end;
if cix = -1 then
begin
result := eInvalid;
exit;
end; // no city found here
special := Tile and fSpecial and ResourceMask[p] shr 5;
with Terrain[Tile and fTerrain] do
begin
Food := FoodRes[special];
Prod := ProdRes[special];
Trade := TradeRes[special];
if (special > 0) and (Tile and fTerrain <> fGrass) and
(RW[p].NatBuilt[imSpacePort] > 0) then
begin // GeoSat effect
Food := 2 * Food - FoodRes[0];
Prod := 2 * Prod - ProdRes[0];
Trade := 2 * Trade - TradeRes[0];
end;
if (Tile and fTerImp = tiIrrigation) or (Tile and fTerImp = tiFarm) or
(Tile and fCity <> 0) then
inc(Food, IrrEff); { irrigation effect }
if Tile and fTerImp = tiMine then
inc(Prod, MineEff); { mining effect }
if (Tile and fRiver <> 0) and (RW[p].Tech[adMapMaking] >= tsApplicable)
then
inc(Trade); { river effect }
if (Tile and (fRoad or fRR) <> 0) and (MoveCost = 1) and
(RW[p].Tech[adWheel] >= tsApplicable) then
inc(Trade); { road effect }
if (Tile and (fRR or fCity) <> 0) and
(RW[p].Tech[adRailroad] >= tsApplicable) then
inc(Prod, Prod shr 1); { railroad effect }
ExplCity := -1;
if (cix >= 0) and (p = p0) then
ExplCity := cix;
if cix >= 0 then
if Tile and fTerrain >= fGrass then
begin
if ((Tile and fTerImp = tiFarm) or (Tile and fCity <> 0)) and
(RW[p].City[cix].Built[imSupermarket] > 0) then
inc(Food, Food shr 1); { farmland effect }
if (Tile and (fRoad or fRR) <> 0) and (MoveCost = 1) and
(RW[p].City[cix].Built[imHighways] > 0) then
inc(Trade, 1); { superhighway effect }
end
else
begin
if RW[p].City[cix].Built[imHarbor] > 0 then
inc(Food); { harbour effect }
if RW[p].City[cix].Built[imPlatform] > 0 then
inc(Prod); { oil platform effect }
if GWonder[woLighthouse].EffectiveOwner = p then
inc(Prod);
end;
end;
{ good government influence }
if (RW[p].Government in [gRepublic, gDemocracy, gFuture]) and (Trade > 0)
then
inc(Trade);
if (RW[p].Government = gCommunism) and (Prod > 1) then
inc(Prod);
if RW[p].Government in [gAnarchy, gDespotism] then
begin { bad government influence }
if Food > 3 then
Food := 3;
if Prod > 2 then
Prod := 2;
if Trade > 2 then
Trade := 2;
end;
if Tile and (fTerrain or fPoll) > fPoll then
begin { pollution - decrease ressources }
dec(Food, Food shr 1);
dec(Prod, Prod shr 1);
dec(Trade, Trade shr 1);
end;
if Tile and fCity <> 0 then
Trade := 0
else if (cix >= 0) and (RW[p].City[cix].Built[imCourt] + RW[p].City[cix]
.Built[imPalace] = 0) then
if RW[p].City[cix].Built[imTownHall] = 0 then
Trade := 0
else if Trade > 3 then
Trade := 3;
end;
result := eOK;
end;
procedure Strongest(Loc: integer; var uix, Strength, Bonus, Cnt: integer);
{ find strongest defender at Loc }
var
Defender, uix1, Det, Cost, TestStrength, TestBonus, TestDet, TestCost,
Domain: integer;
PUn: ^TUn;
PModel: ^TModel;
begin
Defender := Occupant[Loc];
Cost := 0;
Cnt := 0;
Det := -1;
for uix1 := 0 to RW[Defender].nUn - 1 do
begin
PUn := @RW[Defender].Un[uix1];
PModel := @RW[Defender].Model[PUn.mix];
if PModel.Kind = mkSpecial_Glider then
Domain := dGround
else
Domain := PModel.Domain;
if PUn.Loc = Loc then
begin
inc(Cnt);
if PUn.Master < 0 then
begin
if Domain < dSea then
begin
TestBonus := Terrain[RealMap[Loc] and fTerrain].Defense;
if RealMap[Loc] and fTerImp = tiFort then
inc(TestBonus, 4);
if PUn.Flags and unFortified <> 0 then
inc(TestBonus, 2);
if (PModel.Kind = mkSpecial_TownGuard) and
(RealMap[Loc] and fCity <> 0) then
inc(TestBonus, 4);
end
else
TestBonus := 4;
inc(TestBonus, PUn.exp div ExpCost);
TestStrength := PModel.Defense * TestBonus * PUn.Health;
if (Domain = dAir) and ((RealMap[Loc] and fCity <> 0) or
(RealMap[Loc] and fTerImp = tiBase)) then
TestStrength := 0;
if (Domain = dSea) and (RealMap[Loc] and fTerrain >= fGrass) then
TestStrength := TestStrength shr 1;
TestDet := TestStrength;
if PModel.Cap[mcStealth] > 0 then
else if PModel.Cap[mcSub] > 0 then
inc(TestDet, 1 shl 28)
else if (Domain = dGround) and (PModel.Cap[mcFanatic] > 0) and
not(RW[Defender].Government in [gRepublic, gDemocracy, gFuture]) then
inc(TestDet, 4 shl 28) // fanatic ground units always defend
else if PModel.Flags and mdZOC <> 0 then
inc(TestDet, 3 shl 28)
else
inc(TestDet, 2 shl 28);
TestCost := RW[Defender].Model[PUn.mix].Cost;
if (TestDet > Det) or (TestDet = Det) and (TestCost < Cost) then
begin
uix := uix1;
Strength := TestStrength;
Bonus := TestBonus;
Det := TestDet;
Cost := TestCost;
end;
end;
end;
end;
end;
function UnitSpeed(p, mix, Health: integer): integer;
begin
with RW[p].Model[mix] do
begin
result := Speed;
if Domain = dSea then
begin
if GWonder[woMagellan].EffectiveOwner = p then
inc(result, 200);
if Health < 100 then
result := ((result - 250) * Health div 5000) * 50 + 250;
end;
end;
end;
procedure GetUnitReport(p, uix: integer; var UnitReport: TUnitReport);
var
TerrOwner: integer;
PModel: ^TModel;
begin
UnitReport.FoodSupport := 0;
UnitReport.ProdSupport := 0;
UnitReport.ReportFlags := 0;
if RW[p].Government <> gAnarchy then
with RW[p].Un[uix] do
begin
PModel := @RW[p].Model[mix];
if (PModel.Kind = mkSettler)
{ and (GWonder[woFreeSettlers].EffectiveOwner<>p) } then
UnitReport.FoodSupport := SettlerFood[RW[p].Government]
else if Flags and unConscripts <> 0 then
UnitReport.FoodSupport := 1;
if RW[p].Government <> gFundamentalism then
begin
if GTestFlags and tfImmImprove = 0 then
begin
if PModel.Flags and mdDoubleSupport <> 0 then
UnitReport.ProdSupport := 2
else
UnitReport.ProdSupport := 1;
if PModel.Kind = mkSpecial_TownGuard then
UnitReport.ReportFlags := UnitReport.ReportFlags or
urfAlwaysSupport;
end;
if PModel.Flags and mdCivil = 0 then
begin
TerrOwner := RealMap[Loc] shr 27;
case RW[p].Government of
gRepublic, gFuture:
if (TerrOwner <> p) and (TerrOwner < nPl) and
(RW[p].Treaty[TerrOwner] < trAlliance) then
UnitReport.ReportFlags := UnitReport.ReportFlags or urfDeployed;
gDemocracy:
if (TerrOwner >= nPl) or (TerrOwner <> p) and
(RW[p].Treaty[TerrOwner] < trAlliance) then
UnitReport.ReportFlags := UnitReport.ReportFlags or urfDeployed;
end;
end;
end;
end;
end;
procedure SearchCity(Loc: integer; var p, cix: integer);
// set p to supposed owner before call
var
i: integer;
begin
if RealMap[Loc] < nPl shl 27 then
p := RealMap[Loc] shr 27;
for i := 0 to nPl - 1 do
begin
if 1 shl p and GAlive <> 0 then
with RW[p] do
begin
cix := nCity - 1;
while (cix >= 0) and (City[cix].Loc <> Loc) do
dec(cix);
if cix >= 0 then
exit;
end;
assert(i < nPl - 1);
p := (p + 1) mod nPl;
end;
end;
procedure MakeCityInfo(p, cix: integer; var ci: TCityInfo);
begin
assert((p >= 0) and (p < nPl));
assert((cix >= 0) and (cix < RW[p].nCity));
with RW[p].City[cix] do
begin
ci.Loc := Loc;
ci.ID := ID;
ci.Owner := p;
ci.Size := Size;
ci.Flags := 0;
if Built[imPalace] > 0 then
inc(ci.Flags, ciCapital);
if (Built[imWalls] > 0) or (Continent[Loc] = GrWallContinent[p]) then
inc(ci.Flags, ciWalled);
if Built[imCoastalFort] > 0 then
inc(ci.Flags, ciCoastalFort);
if Built[imMissileBat] > 0 then
inc(ci.Flags, ciMissileBat);
if Built[imBunker] > 0 then
inc(ci.Flags, ciBunker);
if Built[imSpacePort] > 0 then
inc(ci.Flags, ciSpacePort);
end;
end;
procedure TellAboutModel(p, taOwner, tamix: integer);
var
i: integer;
begin
if (p = taOwner) or (Mode < moPlaying) then
exit;
i := 0;
while (i < RW[p].nEnemyModel) and ((RW[p].EnemyModel[i].Owner <> taOwner) or
(RW[p].EnemyModel[i].mix <> tamix)) do
inc(i);
if i = RW[p].nEnemyModel then
IntServer(sIntTellAboutModel + p shl 4, taOwner, tamix, nil^);
end;
function emixSafe(p, taOwner, tamix: integer): integer;
begin
result := RWemix[p, taOwner, tamix];
if result < 0 then
begin // sIntTellAboutModel comes too late
assert(Mode = moMovie);
result := $FFFF;
end;
end;
procedure IntroduceEnemy(p1, p2: integer);
begin
RW[p1].Treaty[p2] := trNone;
RW[p2].Treaty[p1] := trNone;
end;
function DiscoverTile(Loc, p, pTell, Level: integer; EnableContact: boolean;
euix: integer = -2): boolean;
// euix = -2: full discover
// euix = -1: unit and city only, append units in EnemyUn
// euix >= 0: unit and city only, replace EnemyUn[euix]
procedure SetContact(p1, p2: integer);
begin
if (Mode < moPlaying) or (p1 = p2) or (RW[p1].Treaty[p2] > trNoContact) then
exit;
IntServer(sIntTellAboutNation, p1, p2, nil^);
// NewContact[p1,p2]:=true
end;
var
i, uix, cix, TerrOwner, TerrOwnerTreaty, Strength, Bonus, Cnt, pFoundCity,
cixFoundCity, MinLevel, Loc1, V8: integer;
Tile, AddFlags: Cardinal;
Adjacent: TVicinity8Loc;
unx: ^TUn;
mox: ^TModel;
begin
result := false;
with RW[pTell] do
begin
Tile := RealMap[Loc] and ResourceMask[pTell];
if Mode = moLoading_Fast then
AddFlags := 0 // don't discover units
else
begin
AddFlags := Map[Loc] and fInEnemyZoC // always preserve this flag!
or fObserved;
if Level = lObserveSuper then
AddFlags := AddFlags or fSpiedOut;
if (GrWallContinent[pTell] >= 0) and
(Continent[Loc] = GrWallContinent[pTell]) then
AddFlags := AddFlags or fGrWall;
if (Mode = moPlaying) and ((Tile and (nPl shl 27) <> nPl shl 27) and
(pTell = p)) then
begin // set fPeace flag?
TerrOwner := Tile shr 27;
if TerrOwner <> pTell then
begin
TerrOwnerTreaty := RW[pTell].Treaty[TerrOwner];
if 1 shl TerrOwnerTreaty and
(1 shl trPeace or 1 shl TrFriendlyContact) <> 0 then
AddFlags := AddFlags or fPeace;
end;
end;
if Occupant[Loc] >= 0 then
if Occupant[Loc] = pTell then
begin
AddFlags := AddFlags or (fOwned or fUnit);
if ZoCMap[Loc] > 0 then
AddFlags := AddFlags or fOwnZoCUnit;
// Level:=lObserveSuper // always see own units
end
else if Map[Loc] and fUnit <> 0 then
AddFlags := AddFlags or fUnit
else
begin
Strongest(Loc, uix, Strength, Bonus, Cnt);
unx := @RW[Occupant[Loc]].Un[uix];
mox := @RW[Occupant[Loc]].Model[unx.mix];
assert((ZoCMap[Loc] <> 0) = (mox.Flags and mdZOC <> 0));
if (mox.Cap[mcStealth] > 0) and (Tile and fCity = 0) and
(Tile and fTerImp <> tiBase) then
MinLevel := lObserveSuper
else if (mox.Cap[mcSub] > 0) and (Tile and fTerrain < fGrass) then
MinLevel := lObserveAll
else
MinLevel := lObserveUnhidden;
if Level >= MinLevel then
begin
AddFlags := AddFlags or fUnit;
if euix >= 0 then
uix := euix
else
begin
uix := nEnemyUn;
inc(nEnemyUn);
assert(nEnemyUn < neumax);
end;
MakeUnitInfo(Occupant[Loc], unx^, EnemyUn[uix]);
if Cnt > 1 then
EnemyUn[uix].Flags := EnemyUn[uix].Flags or unMulti;
if (mox.Flags and mdZOC <> 0) and (pTell = p) and
(Treaty[Occupant[Loc]] < trAlliance) then
begin // set fInEnemyZoC flags of surrounding tiles
V8_to_Loc(Loc, Adjacent);
for V8 := 0 to 7 do
begin
Loc1 := Adjacent[V8];
if (Loc1 >= 0) and (Loc1 < MapSize) then
Map[Loc1] := Map[Loc1] or fInEnemyZoC
end;
end;
if EnableContact and (mox.Domain = dGround) then
SetContact(pTell, Occupant[Loc]);
if Mode >= moMovie then
begin
TellAboutModel(pTell, Occupant[Loc], unx.mix);
EnemyUn[uix].emix := emixSafe(pTell, Occupant[Loc], unx.mix);
end;
// Level:=lObserveSuper; // don't discover unit twice
if (pTell = p) and
((Tile and fCity = 0) or (1 shl pTell and GAI <> 0)) then
result := true;
end
else
AddFlags := AddFlags or Map[Loc] and (fStealthUnit or fHiddenUnit);
end;
end; // if Mode>moLoading_Fast
if Tile and fCity <> 0 then
if ObserveLevel[Loc] shr (2 * pTell) and 3 > 0 then
AddFlags := AddFlags or Map[Loc] and fOwned
else
begin
pFoundCity := Tile shr 27;
if pFoundCity = pTell then
AddFlags := AddFlags or fOwned
else
begin
if EnableContact then
SetContact(pTell, pFoundCity);
cixFoundCity := RW[pFoundCity].nCity - 1;
while (cixFoundCity >= 0) and
(RW[pFoundCity].City[cixFoundCity].Loc <> Loc) do
dec(cixFoundCity);
assert(cixFoundCity >= 0);
i := 0;
while (i < nEnemyCity) and (EnemyCity[i].Loc <> Loc) do
inc(i);
if i = nEnemyCity then
begin
inc(nEnemyCity);
assert(nEnemyCity < necmax);
EnemyCity[i].Status := 0;
EnemyCity[i].SavedStatus := 0;
if pTell = p then
result := true;
end;
MakeCityInfo(pFoundCity, cixFoundCity, EnemyCity[i]);
end;
end
else if Map[Loc] and fCity <> 0 then // remove enemycity
for cix := 0 to nEnemyCity - 1 do
if EnemyCity[cix].Loc = Loc then
EnemyCity[cix].Loc := -1;
if Map[Loc] and fTerrain = fUNKNOWN then
inc(Discovered[pTell]);
if euix >= -1 then
Map[Loc] := Map[Loc] and not(fUnit or fCity or fOwned or fOwnZoCUnit) or
(Tile and $07FFFFFF or AddFlags) and
(fUnit or fCity or fOwned or fOwnZoCUnit)
else
begin
Map[Loc] := Tile and $07FFFFFF or AddFlags;
if Tile and $78000000 = $78000000 then
Territory[Loc] := -1
else
Territory[Loc] := Tile shr 27;
MapObservedLast[Loc] := GTurn
end;
ObserveLevel[Loc] := ObserveLevel[Loc] and not(3 shl (2 * pTell)) or
Cardinal(Level) shl (2 * pTell);
end;
end;
function Discover9(Loc, p, Level: integer;
TellAllied, EnableContact: boolean): boolean;
var
V9, Loc1, pTell, OldLevel: integer;
Radius: TVicinity8Loc;
begin
assert((Mode > moLoading_Fast) or (RW[p].nEnemyUn = 0));
result := false;
V8_to_Loc(Loc, Radius);
for V9 := 0 to 8 do
begin
if V9 = 8 then
Loc1 := Loc
else
Loc1 := Radius[V9];
if (Loc1 >= 0) and (Loc1 < MapSize) then
if TellAllied then
begin
for pTell := 0 to nPl - 1 do
if (pTell = p) or (1 shl pTell and GAlive <> 0) and
(RW[p].Treaty[pTell] = trAlliance) then
begin
OldLevel := ObserveLevel[Loc1] shr (2 * pTell) and 3;
if Level > OldLevel then
result := DiscoverTile(Loc1, p, pTell, Level, EnableContact)
or result;
end;
end
else
begin
OldLevel := ObserveLevel[Loc1] shr (2 * p) and 3;
if Level > OldLevel then
result := DiscoverTile(Loc1, p, p, Level, EnableContact) or result;
end;
end;
end;
function Discover21(Loc, p, AdjacentLevel: integer;
TellAllied, EnableContact: boolean): boolean;
var
V21, Loc1, pTell, Level, OldLevel, AdjacentFlags: integer;
Radius: TVicinity21Loc;
begin
assert((Mode > moLoading_Fast) or (RW[p].nEnemyUn = 0));
result := false;
AdjacentFlags := $00267620 shr 1;
V21_to_Loc(Loc, Radius);
for V21 := 1 to 26 do
begin
Loc1 := Radius[V21];
if (Loc1 >= 0) and (Loc1 < MapSize) then
begin
if AdjacentFlags and 1 <> 0 then
Level := AdjacentLevel
else
Level := lObserveUnhidden;
if TellAllied then
begin
for pTell := 0 to nPl - 1 do
if (pTell = p) or (1 shl pTell and GAlive <> 0) and
(RW[p].Treaty[pTell] = trAlliance) then
begin
OldLevel := ObserveLevel[Loc1] shr (2 * pTell) and 3;
if Level > OldLevel then
result := DiscoverTile(Loc1, p, pTell, Level, EnableContact)
or result;
end;
end
else
begin
OldLevel := ObserveLevel[Loc1] shr (2 * p) and 3;
if Level > OldLevel then
result := DiscoverTile(Loc1, p, p, Level, EnableContact) or result;
end;
end;
AdjacentFlags := AdjacentFlags shr 1;
end;
end;
procedure DiscoverAll(p, Level: integer);
{ player p discovers complete playground (for supervisor) }
var
Loc, OldLevel: integer;
begin
assert((Mode > moLoading_Fast) or (RW[p].nEnemyUn = 0));
for Loc := 0 to MapSize - 1 do
begin
OldLevel := ObserveLevel[Loc] shr (2 * p) and 3;
if Level > OldLevel then
DiscoverTile(Loc, p, p, Level, false);
end;
end;
procedure DiscoverViewAreas(p: integer);
var
pTell, uix, cix, ecix, Loc, RealOwner: integer;
PModel: ^TModel;
begin // discover unit and city view areas
for pTell := 0 to nPl - 1 do
if (pTell = p) or (RW[p].Treaty[pTell] = trAlliance) then
begin
for uix := 0 to RW[pTell].nUn - 1 do
with RW[pTell].Un[uix] do
if (Loc >= 0) and (Master < 0) and (RealMap[Loc] and fCity = 0) then
begin
PModel := @RW[pTell].Model[mix];
if (PModel.Kind = mkDiplomat) or (PModel.Cap[mcSpy] > 0) then
Discover21(Loc, p, lObserveSuper, false, true)
else if (PModel.Cap[mcRadar] > 0) or (PModel.Domain = dAir) then
Discover21(Loc, p, lObserveAll, false, false)
else if (RealMap[Loc] and fTerrain = fMountains) or
(RealMap[Loc] and fTerImp = tiFort) or
(RealMap[Loc] and fTerImp = tiBase) or (PModel.Cap[mcAcademy] > 0)
then
Discover21(Loc, p, lObserveUnhidden, false,
PModel.Domain = dGround)
else
Discover9(Loc, p, lObserveUnhidden, false,
PModel.Domain = dGround);
end;
for cix := 0 to RW[pTell].nCity - 1 do
if RW[pTell].City[cix].Loc >= 0 then
Discover21(RW[pTell].City[cix].Loc, p, lObserveUnhidden, false, true);
for ecix := 0 to RW[pTell].nEnemyCity - 1 do
begin // players know territory, so no use in hiding city owner
Loc := RW[pTell].EnemyCity[ecix].Loc;
if Loc >= 0 then
begin
RealOwner := (RealMap[Loc] shr 27) and $F;
if RealOwner < nPl then
RW[pTell].EnemyCity[ecix].Owner := RealOwner
else
begin
RW[pTell].EnemyCity[ecix].Loc := -1;
RW[pTell].Map[Loc] := RW[pTell].Map[Loc] and not fCity;
end;
end;
end;
end;
end;
function GetUnitStack(p, Loc: integer): integer;
var
uix: integer;
unx: ^TUn;
begin
result := 0;
if Occupant[Loc] < 0 then
exit;
for uix := 0 to RW[Occupant[Loc]].nUn - 1 do
begin
unx := @RW[Occupant[Loc]].Un[uix];
if unx.Loc = Loc then
begin
MakeUnitInfo(Occupant[Loc], unx^, RW[p].EnemyUn[RW[p].nEnemyUn + result]);
TellAboutModel(p, Occupant[Loc], unx.mix);
RW[p].EnemyUn[RW[p].nEnemyUn + result].emix :=
RWemix[p, Occupant[Loc], unx.mix];
inc(result);
end;
end;
end;
procedure UpdateUnitMap(Loc: integer; CityChange: boolean = false);
// update maps and enemy units of all players after unit change
var
p, euix, OldLevel: integer;
AddFlags, ClearFlags: Cardinal;
begin
if (Mode = moLoading_Fast) and not CityChange then
exit;
for p := 0 to nPl - 1 do
if 1 shl p and (GAlive or GWatching) <> 0 then
begin
OldLevel := ObserveLevel[Loc] shr (2 * p) and 3;
if OldLevel > lNoObserve then
begin
if RW[p].Map[Loc] and (fUnit or fOwned) = fUnit then
begin
// replace unit located here in EnemyUn
// do not just set loc:=-1 because total number would be unlimited
euix := RW[p].nEnemyUn - 1;
while euix >= 0 do
begin
if RW[p].EnemyUn[euix].Loc = Loc then
begin
RW[p].EnemyUn[euix].Loc := -1;
Break;
end;
dec(euix);
end;
RW[p].Map[Loc] := RW[p].Map[Loc] and not fUnit
end
else
begin // look for empty slot in EnemyUn
euix := RW[p].nEnemyUn - 1;
while (euix >= 0) and (RW[p].EnemyUn[euix].Loc >= 0) do
dec(euix);
end;
if (Occupant[Loc] < 0) and not CityChange then
begin // calling DiscoverTile not necessary, only clear map flags
ClearFlags := fUnit or fHiddenUnit or fStealthUnit or fOwnZoCUnit;
if RealMap[Loc] and fCity = 0 then
ClearFlags := ClearFlags or fOwned;
RW[p].Map[Loc] := RW[p].Map[Loc] and not ClearFlags;
end
else if (Occupant[Loc] <> p) or CityChange then
begin // city or enemy unit update necessary, call DiscoverTile
ObserveLevel[Loc] := ObserveLevel[Loc] and not(3 shl (2 * p));
DiscoverTile(Loc, p, p, OldLevel, false, euix);
end
else { if (Occupant[Loc]=p) and not CityChange then }
begin // calling DiscoverTile not necessary, only set map flags
ClearFlags := 0;
AddFlags := fUnit or fOwned;
if ZoCMap[Loc] > 0 then
AddFlags := AddFlags or fOwnZoCUnit
else
ClearFlags := ClearFlags or fOwnZoCUnit;
RW[p].Map[Loc] := RW[p].Map[Loc] and not ClearFlags or AddFlags;
end;
end;
end;
end;
procedure RecalcV8ZoC(p, Loc: integer);
// recalculate fInEnemyZoC flags around single tile
var
V8, V8V8, Loc1, Loc2, p1, ObserveMask: integer;
Tile1: ^Cardinal;
Adjacent, AdjacentAdjacent: TVicinity8Loc;
begin
if Mode = moLoading_Fast then
exit;
ObserveMask := 3 shl (2 * p);
V8_to_Loc(Loc, Adjacent);
for V8 := 0 to 7 do
begin
Loc1 := Adjacent[V8];
if (Loc1 >= 0) and (Loc1 < MapSize) then
begin
Tile1 := @RW[p].Map[Loc1];
Tile1^ := Tile1^ and not fInEnemyZoC;
V8_to_Loc(Loc1, AdjacentAdjacent);
for V8V8 := 0 to 7 do
begin
Loc2 := AdjacentAdjacent[V8V8];
if (Loc2 >= 0) and (Loc2 < MapSize) and (ZoCMap[Loc2] > 0) and
(ObserveLevel[Loc2] and ObserveMask <> 0) then
begin
p1 := Occupant[Loc2];
assert(p1 <> nPl);
if (p1 <> p) and (RW[p].Treaty[p1] < trAlliance) then
begin
Tile1^ := Tile1^ or fInEnemyZoC;
Break;
end;
end;
end;
end;
end;
end;
procedure RecalcMapZoC(p: integer);
// recalculate fInEnemyZoC flags for the whole map
var
Loc, Loc1, V8, p1, ObserveMask: integer;
Adjacent: TVicinity8Loc;
begin
if Mode = moLoading_Fast then
exit;
MaskD(RW[p].Map^, MapSize, Cardinal(not Cardinal(fInEnemyZoC)));
ObserveMask := 3 shl (2 * p);
for Loc := 0 to MapSize - 1 do
if (ZoCMap[Loc] > 0) and (ObserveLevel[Loc] and ObserveMask <> 0) then
begin
p1 := Occupant[Loc];
assert(p1 <> nPl);
if (p1 <> p) and (RW[p].Treaty[p1] < trAlliance) then
begin // this non-allied enemy ZoC unit is known to this player -- set flags!
V8_to_Loc(Loc, Adjacent);
for V8 := 0 to 7 do
begin
Loc1 := Adjacent[V8];
if (Loc1 >= 0) and (Loc1 < MapSize) then
RW[p].Map[Loc1] := RW[p].Map[Loc1] or fInEnemyZoC;
end;
end;
end;
end;
procedure RecalcPeaceMap(p: integer);
// recalculate fPeace flags for the whole map
var
Loc, p1: integer;
PeacePlayer: array [-1 .. nPl - 1] of boolean;
begin
if Mode <> moPlaying then
exit;
MaskD(RW[p].Map^, MapSize, Cardinal(not Cardinal(fPeace)));
for p1 := -1 to nPl - 1 do
PeacePlayer[p1] := (p1 >= 0) and (p1 <> p) and (1 shl p1 and GAlive <> 0)
and (RW[p].Treaty[p1] in [trPeace, TrFriendlyContact]);
for Loc := 0 to MapSize - 1 do
if PeacePlayer[RW[p].Territory[Loc]] then
RW[p].Map[Loc] := RW[p].Map[Loc] or fPeace;
end;
{
Territory Calculation
____________________________________________________________________
}
var
BorderChanges: array [0 .. sIntExpandTerritory and $F - 1] of Cardinal;
procedure ChangeTerritory(Loc, p: integer);
var
p1: integer;
begin
Assert(p >= 0); // no player's territory indicated by p=nPl
Dec(TerritoryCount[RealMap[Loc] shr 27]);
Inc(TerritoryCount[p]);
RealMap[Loc] := RealMap[Loc] and not($F shl 27) or Cardinal(p) shl 27;
if p = $F then
p := -1;
for p1 := 0 to nPl - 1 do
if 1 shl p1 and (GAlive or GWatching) <> 0 then
if RW[p1].Map[Loc] and fTerrain <> fUNKNOWN then
begin
RW[p1].Territory[Loc] := p;
if (p < nPl) and (p <> p1) and (1 shl p and GAlive <> 0) and
(RW[p1].Treaty[p] in [trPeace, TrFriendlyContact]) then
RW[p1].Map[Loc] := RW[p1].Map[Loc] or fPeace
else
RW[p1].Map[Loc] := RW[p1].Map[Loc] and not fPeace;
end;
end;
procedure ExpandTerritory(OriginLoc: integer);
var
i, dx, dy, dxMax, dyMax, Loc, NewOwner: integer;
begin
if OriginLoc = -1 then
raise Exception.Create('Location error (negative)');
if OriginLoc >= MapSize then
raise Exception.Create('Location error (range)');
i := 0;
dyMax := 0;
while (dyMax + 1) + (dyMax + 1) shr 1 <= CountryRadius do
inc(dyMax);
for dy := -dyMax to dyMax do
begin
dxMax := dy and 1;
while abs(dy) + (dxMax + 2) + abs(abs(dy) - (dxMax + 2)) shr 1 <=
CountryRadius do
inc(dxMax, 2);
for dx := -dxMax to dxMax do
if (dy + dx) and 1 = 0 then
begin
NewOwner := BorderChanges[i div 8] shr (i mod 8 * 4) and $F;
Loc := dLoc(OriginLoc, dx, dy);
if (Loc >= 0) and (Cardinal(NewOwner) <> RealMap[Loc] shr 27) then
ChangeTerritory(Loc, NewOwner);
inc(i);
end;
end;
end;
procedure CheckBorders(OriginLoc, PlayerLosingCity: integer);
// OriginLoc: only changes in CountryRadius around this location possible,
// -1 for complete map, -2 for double-check (no more changes allowed)
// PlayerLosingCity: do nothing but remove tiles no longer in reach from this
// player's territory, -1 for full border recalculation
var
i, r, Loc, Loc1, dx, dy, p1, p2, cix, NewDist, dxMax, dyMax, OldOwner, V8: Integer;
NewOwner: Cardinal;
Adjacent: TVicinity8Loc;
AtPeace: array [0 .. nPl, 0 .. nPl] of boolean;
{ to who's country a tile belongs }
Country, FormerCountry: array [0 .. lxmax * lymax - 1] of ShortInt;
{ Maybe these distances go over 127 on large maps? }
Dist, FormerDist, StolenDist: array [0 .. lxmax * lymax - 1] of Integer;
begin
FillChar (Dist, Sizeof(Dist), 0);
FillChar (StolenDist, Sizeof(StolenDist), 0);
if PlayerLosingCity >= 0 then
begin
for Loc := 0 to MapSize - 1 do
StolenDist[Loc] := CountryRadius + 1;
for cix := 0 to RW[PlayerLosingCity].nCity - 1 do
if RW[PlayerLosingCity].City[cix].Loc >= 0 then
StolenDist[RW[PlayerLosingCity].City[cix].Loc] := 0;
for r := 1 to CountryRadius shr 1 do
begin
move(StolenDist, FormerDist, Sizeof(FormerDist));
for Loc := 0 to MapSize - 1 do
if (FormerDist[Loc] <= CountryRadius - 2)
// use same conditions as below!
and ((1 shl (RealMap[Loc] and fTerrain)) and
(1 shl fShore + 1 shl fMountains + 1 shl fArctic) = 0) then
begin
V8_to_Loc(Loc, Adjacent);
for V8 := 0 to 7 do
begin
Loc1 := Adjacent[V8];
NewDist := FormerDist[Loc] + 2 + V8 and 1;
if (Loc1 >= 0) and (Loc1 < MapSize) and (NewDist < StolenDist[Loc1])
then
StolenDist[Loc1] := NewDist;
end;
end;
end;
end;
FillChar(Country, MapSize, Byte(-1));
for Loc := 0 to MapSize - 1 do
Dist[Loc] := CountryRadius + 1;
for p1 := 0 to nPl - 1 do
if 1 shl p1 and GAlive <> 0 then
for cix := 0 to RW[p1].nCity - 1 do
if RW[p1].City[cix].Loc >= 0 then
begin
Country[RW[p1].City[cix].Loc] := p1;
Dist[RW[p1].City[cix].Loc] := 0;
end;
for r := 1 to CountryRadius shr 1 do
begin
move(Country, FormerCountry, Sizeof(FormerCountry));
move(Dist, FormerDist, Sizeof(FormerDist));
for Loc := 0 to MapSize - 1 do
if (FormerDist[Loc] <= CountryRadius - 2) // use same conditions as above!
and ((1 shl (RealMap[Loc] and fTerrain)) and
(1 shl fShore + 1 shl fMountains + 1 shl fArctic) = 0) then
begin
assert(FormerCountry[Loc] >= 0);
V8_to_Loc(Loc, Adjacent);
for V8 := 0 to 7 do
begin
Loc1 := Adjacent[V8];
NewDist := FormerDist[Loc] + 2 + V8 and 1;
if (Loc1 >= 0) and (Loc1 < MapSize) and (NewDist < Dist[Loc1]) then
begin
Country[Loc1] := FormerCountry[Loc];
Dist[Loc1] := NewDist;
end;
end;
end;
end;
FillChar(AtPeace, SizeOf(AtPeace), false);
for p1 := 0 to nPl - 1 do
if 1 shl p1 and GAlive <> 0 then
for p2 := 0 to nPl - 1 do
if (p2 <> p1) and (1 shl p2 and GAlive <> 0) and
(RW[p1].Treaty[p2] >= trPeace) then
AtPeace[p1, p2] := true;
if OriginLoc >= 0 then
begin // update area only
i := 0;
FillChar(BorderChanges, SizeOf(BorderChanges), 0);
dyMax := 0;
while (dyMax + 1) + (dyMax + 1) shr 1 <= CountryRadius do
inc(dyMax);
for dy := -dyMax to dyMax do
begin
dxMax := dy and 1;
while abs(dy) + (dxMax + 2) + abs(abs(dy) - (dxMax + 2)) shr 1 <=
CountryRadius do
inc(dxMax, 2);
for dx := -dxMax to dxMax do
if (dy + dx) and 1 = 0 then
begin
Loc := dLoc(OriginLoc, dx, dy);
if Loc >= 0 then
begin
OldOwner := RealMap[Loc] shr 27;
NewOwner := Country[Loc] and $F;
if NewOwner <> OldOwner then
if AtPeace[NewOwner, OldOwner] and
not((OldOwner = PlayerLosingCity) and
(StolenDist[Loc] > CountryRadius)) then
NewOwner := OldOwner // peace fixes borders
else
ChangeTerritory(Loc, NewOwner);
BorderChanges[i shr 3] := BorderChanges[i shr 3] or
((NewOwner shl ((i and 7) * 4)) and $ffffffff);
end;
inc(i);
end;
end;
end
else
for Loc := 0 to MapSize - 1 do // update complete map
begin
OldOwner := RealMap[Loc] shr 27;
NewOwner := Country[Loc] and $F;
if (NewOwner <> OldOwner) and (not AtPeace[NewOwner, OldOwner] or
((OldOwner = PlayerLosingCity) and (StolenDist[Loc] > CountryRadius)))
then
begin
assert(OriginLoc <> -2); // test if border saving works
ChangeTerritory(Loc, NewOwner);
end;
end;
{$IFOPT O-} if OriginLoc <> -2 then
CheckBorders(-2); {$ENDIF} // check: single pass should do!
end;
procedure LogCheckBorders(p, cix, PlayerLosingCity: integer);
begin
CheckBorders(RW[p].City[cix].Loc, PlayerLosingCity);
IntServer(sIntExpandTerritory, p, cix, BorderChanges);
end;
{
Map Processing
____________________________________________________________________
}
procedure CreateUnit(p, mix: integer);
begin
with RW[p] do
begin
Un[nUn].mix := mix;
with Un[nUn] do
begin
ID := UnBuilt[p];
inc(UnBuilt[p]);
Status := 0;
SavedStatus := 0;
inc(Model[mix].Built);
Home := -1;
Health := 100;
Flags := 0;
Movement := 0;
if Model[mix].Domain = dAir then
begin
Fuel := Model[mix].Cap[mcFuel];
Flags := Flags or unBombsLoaded;
end;
Job := jNone;
exp := ExpCost shr 1;
TroopLoad := 0;
AirLoad := 0;
Master := -1;
end;
inc(nUn);
end
end;
procedure FreeUnit(p, uix: integer);
// loc or master should be set after call
// implementation is critical for loading performance, change carefully
var
Loc0, uix1: integer;
Occ, ZoC: boolean;
begin
with RW[p].Un[uix] do
begin
Job := jNone;
Flags := Flags and not(unFortified or unMountainDelay);
Loc0 := Loc;
end;
if Occupant[Loc0] >= 0 then
begin
assert(Occupant[Loc0] = p);
Occ := false;
ZoC := false;
for uix1 := 0 to RW[p].nUn - 1 do
with RW[p].Un[uix1] do
if (Loc = Loc0) and (Master < 0) and (uix1 <> uix) then
begin
Occ := true;
if RW[p].Model[mix].Flags and mdZOC <> 0 then
begin
ZoC := true;
Break;
end;
end;
if not Occ then
Occupant[Loc0] := -1;
if not ZoC then
ZoCMap[Loc0] := 0;
end;
end;
procedure PlaceUnit(p, uix: integer);
begin
with RW[p].Un[uix] do
begin
Occupant[Loc] := p;
if RW[p].Model[mix].Flags and mdZOC <> 0 then
ZoCMap[Loc] := 1;
end;
end;
procedure CountLost(p, mix, Enemy: integer);
begin
Inc(RW[p].Model[mix].Lost);
TellAboutModel(Enemy, p, mix);
Inc(Destroyed[Enemy, p, mix]);
end;
procedure RemoveUnit(p, uix: integer; Enemy: integer = -1);
// use enemy only from inside sMoveUnit if attack
var
uix1: integer;
begin
with RW[p].Un[uix] do
begin
assert((Loc >= 0) or (RW[p].Model[mix].Kind = mkDiplomat));
// already freed when spy mission
if Loc >= 0 then
FreeUnit(p, uix);
if Master >= 0 then
if RW[p].Model[mix].Domain = dAir then
dec(RW[p].Un[Master].AirLoad)
else
dec(RW[p].Un[Master].TroopLoad);
if (TroopLoad > 0) or (AirLoad > 0) then
for uix1 := 0 to RW[p].nUn - 1 do
if (RW[p].Un[uix1].Loc >= 0) and (RW[p].Un[uix1].Master = uix) then
{ unit mastered by removed unit -- remove too }
begin
RW[p].Un[uix1].Loc := -1;
if Enemy >= 0 then
CountLost(p, RW[p].Un[uix1].mix, Enemy);
end;
Loc := -1;
if Enemy >= 0 then
CountLost(p, mix, Enemy);
end;
end;
procedure RemoveUnit_UpdateMap(p, uix: integer);
var
Loc0: Integer;
begin
Loc0 := RW[p].Un[uix].Loc;
RemoveUnit(p, uix);
if Mode > moLoading_Fast then
UpdateUnitMap(Loc0);
end;
procedure RemoveAllUnits(p, Loc: integer; Enemy: integer = -1);
var
uix: integer;
begin
for uix := 0 to RW[p].nUn - 1 do
if RW[p].Un[uix].Loc = Loc then
begin
if Enemy >= 0 then
CountLost(p, RW[p].Un[uix].mix, Enemy);
RW[p].Un[uix].Loc := -1;
end;
Occupant[Loc] := -1;
ZoCMap[Loc] := 0;
end;
procedure RemoveDomainUnits(d, p, Loc: integer);
var
uix: integer;
begin
for uix := 0 to RW[p].nUn - 1 do
if (RW[p].Model[RW[p].Un[uix].mix].Domain = d) and (RW[p].Un[uix].Loc = Loc)
then
RemoveUnit(p, uix);
end;
procedure FoundCity(p, FoundLoc: integer);
var
p1, cix1, V21, dx, dy: integer;
begin
if RW[p].nCity = ncmax then
exit;
inc(RW[p].nCity);
with RW[p].City[RW[p].nCity - 1] do
begin
Size := 2;
Status := 0;
SavedStatus := 0;
FillChar(Built, SizeOf(Built), 0);
Food := 0;
Project := cpImp + imTrGoods;
Prod := 0;
Project0 := Project;
Prod0 := 0;
Pollution := 0;
N1 := 0;
Loc := FoundLoc;
if UsedByCity[FoundLoc] >= 0 then
begin { central tile is exploited - toggle in exploiting city }
p1 := p;
SearchCity(UsedByCity[FoundLoc], p1, cix1);
dxdy(UsedByCity[FoundLoc], FoundLoc, dx, dy);
V21 := (dy + 3) shl 2 + (dx + 3) shr 1;
RW[p1].City[cix1].Tiles := RW[p1].City[cix1].Tiles and not(1 shl V21);
end;
Tiles := 1 shl 13; { exploit central tile }
UsedByCity[FoundLoc] := FoundLoc;
RealMap[FoundLoc] := RealMap[FoundLoc] and
(fTerrain or fSpecial or fRiver or nPl shl 27) or fCity;
ChangeTerritory(Loc, p);
end;
end;
procedure StealCity(p, cix: integer; SaveUnits: boolean);
var
i, j, uix1, cix1, nearest: integer;
begin
for i := 0 to nWonder - 1 do
if RW[p].City[cix].Built[i] = 1 then
begin
GWonder[i].EffectiveOwner := -1;
if i = woPyramids then
FreeSlaves;
if i = woEiffel then // deactivate expired wonders
for j := 0 to nWonder - 1 do
if GWonder[j].EffectiveOwner = p then
CheckExpiration(j);
end;
for i := nWonder to nImp - 1 do
if (Imp[i].Kind <> ikCommon) and (RW[p].City[cix].Built[i] > 0) then
begin { destroy national projects }
RW[p].NatBuilt[i] := 0;
if i = imGrWall then
GrWallContinent[p] := -1;
end;
for uix1 := 0 to RW[p].nUn - 1 do
with RW[p].Un[uix1] do
if (Loc >= 0) and (Home = cix) then
if SaveUnits then
begin // support units by nearest other city
nearest := -1;
for cix1 := 0 to RW[p].nCity - 1 do
if (cix1 <> cix) and (RW[p].City[cix1].Loc >= 0) and
((nearest < 0) or (Distance(RW[p].City[cix1].Loc, Loc) <
Distance(RW[p].City[nearest].Loc, Loc))) then
nearest := cix1;
Home := nearest;
end
else
RemoveUnit(p, uix1); // destroy supported units
end;
procedure DestroyCity(p, cix: integer; SaveUnits: boolean);
var
i, V21: integer;
Radius: TVicinity21Loc;
begin
StealCity(p, cix, SaveUnits);
with RW[p].City[cix] do
begin
for i := 0 to nWonder - 1 do
if Built[i] > 0 then
GWonder[i].CityID := WonderDestroyed;
V21_to_Loc(Loc, Radius);
for V21 := 1 to 26 do
if 1 shl V21 and Tiles <> 0 then
UsedByCity[Radius[V21]] := -1;
RealMap[Loc] := RealMap[Loc] and not fCity;
Loc := -1
end;
end;
procedure ChangeCityOwner(pOld, cixOld, pNew: integer);
var
i, j, cix1, Loc1, V21: integer;
Radius: TVicinity21Loc;
begin
inc(RW[pNew].nCity);
RW[pNew].City[RW[pNew].nCity - 1] := RW[pOld].City[cixOld];
StealCity(pOld, cixOld, false);
RW[pOld].City[cixOld].Loc := -1;
with RW[pNew].City[(RW[pNew].nCity - 1)] do
begin
Food := 0;
Project := cpImp + imTrGoods;
Prod := 0;
Project0 := Project;
Prod0 := 0;
Status := 0;
SavedStatus := 0;
N1 := 0;
// check for siege
V21_to_Loc(Loc, Radius);
for V21 := 1 to 26 do
if Tiles and (1 shl V21) and not(1 shl CityOwnTile) <> 0 then
begin
Loc1 := Radius[V21];
assert((Loc1 >= 0) and (Loc1 < MapSize) and (UsedByCity[Loc1] = Loc));
if (ZoCMap[Loc1] > 0) and (Occupant[Loc1] <> pNew) and
(RW[pNew].Treaty[Occupant[Loc1]] < trAlliance) then
begin // tile can't remain exploited
Tiles := Tiles and not(1 shl V21);
UsedByCity[Loc1] := -1;
end;
// don't check for siege by peace territory here, because territory
// might not be up to date -- done in turn beginning anyway
end;
Built[imTownHall] := 0;
Built[imCourt] := 0;
for i := nWonder to nImp - 1 do
if Imp[i].Kind <> ikCommon then
Built[i] := 0; { destroy national projects }
for i := 0 to nWonder - 1 do
if Built[i] = 1 then
begin // new wonder owner!
GWonder[i].EffectiveOwner := pNew;
if i = woEiffel then // reactivate expired wonders
begin
for j := 0 to nWonder - 1 do
if Imp[j].Expiration >= 0 then
for cix1 := 0 to (RW[pNew].nCity - 1) do
if RW[pNew].City[cix1].Built[j] = 1 then
GWonder[j].EffectiveOwner := pNew;
end
else
CheckExpiration(i);
case i of
woLighthouse:
CheckSpecialModels(pNew, preLighthouse);
woLeo:
CheckSpecialModels(pNew, preLeo);
woPyramids:
CheckSpecialModels(pNew, preBuilder);
end;
end;
// remove city from enemy cities
// not done by Discover, because fCity still set!
cix1 := RW[pNew].nEnemyCity - 1;
while (cix1 >= 0) and (RW[pNew].EnemyCity[cix1].Loc <> Loc) do
dec(cix1);
assert(cix1 >= 0);
RW[pNew].EnemyCity[cix1].Loc := -1;
ChangeTerritory(Loc, pNew);
end;
end;
procedure CompleteJob(p, Loc, Job: integer);
var
ChangedTerrain, p1: integer;
begin
assert(Job <> jCity);
ChangedTerrain := -1;
case Job of
jRoad:
RealMap[Loc] := RealMap[Loc] or fRoad;
jRR:
RealMap[Loc] := RealMap[Loc] and not fRoad or fRR;
jClear:
begin
ChangedTerrain := Terrain[RealMap[Loc] and fTerrain].ClearTerrain;
RealMap[Loc] := RealMap[Loc] and not fTerrain or
Cardinal(ChangedTerrain);
RealMap[Loc] := RealMap[Loc] and not(3 shl 5) or
ActualSpecialTile(Loc) shl 5;
end;
jIrr:
RealMap[Loc] := RealMap[Loc] and not fTerImp or tiIrrigation;
jFarm:
RealMap[Loc] := RealMap[Loc] and not fTerImp or tiFarm;
jAfforest:
begin
ChangedTerrain := Terrain[RealMap[Loc] and fTerrain].AfforestTerrain;
RealMap[Loc] := RealMap[Loc] and not fTerrain or
Cardinal(ChangedTerrain);
RealMap[Loc] := RealMap[Loc] and not(3 shl 5) or
ActualSpecialTile(Loc) shl 5;
end;
jMine:
RealMap[Loc] := RealMap[Loc] and not fTerImp or tiMine;
jFort:
RealMap[Loc] := RealMap[Loc] and not fTerImp or tiFort;
jCanal:
RealMap[Loc] := RealMap[Loc] or fCanal;
jTrans:
begin
ChangedTerrain := Terrain[RealMap[Loc] and fTerrain].TransTerrain;
RealMap[Loc] := RealMap[Loc] and not fTerrain or
Cardinal(ChangedTerrain);
RealMap[Loc] := RealMap[Loc] and not(3 shl 5) or
ActualSpecialTile(Loc) shl 5;
if not(RealMap[Loc] and fTerrain in TerrType_Canalable) then
begin
RemoveDomainUnits(dSea, p, Loc);
RealMap[Loc] := RealMap[Loc] and not fCanal;
end;
end;
jPoll:
RealMap[Loc] := RealMap[Loc] and not fPoll;
jBase:
RealMap[Loc] := RealMap[Loc] and not fTerImp or tiBase;
jPillage:
if RealMap[Loc] and fTerImp <> 0 then
begin
if RealMap[Loc] and fTerImp = tiBase then
RemoveDomainUnits(dAir, p, Loc);
RealMap[Loc] := RealMap[Loc] and not fTerImp
end
else if RealMap[Loc] and fCanal <> 0 then
begin
RemoveDomainUnits(dSea, p, Loc);
RealMap[Loc] := RealMap[Loc] and not fCanal
end
else if RealMap[Loc] and fRR <> 0 then
RealMap[Loc] := RealMap[Loc] and not fRR or fRoad
else if RealMap[Loc] and fRoad <> 0 then
RealMap[Loc] := RealMap[Loc] and not fRoad;
end;
if ChangedTerrain >= 0 then
begin // remove terrain improvements if not possible on new terrain
if ((RealMap[Loc] and fTerImp = tiIrrigation) or
(RealMap[Loc] and fTerImp = tiFarm)) and
((Terrain[ChangedTerrain].IrrClearWork = 0) or
(Terrain[ChangedTerrain].ClearTerrain >= 0)) then
RealMap[Loc] := RealMap[Loc] and not fTerImp;
if (RealMap[Loc] and fTerImp = tiMine) and
((Terrain[ChangedTerrain].MineAfforestWork = 0) or
(Terrain[ChangedTerrain].AfforestTerrain >= 0)) then
RealMap[Loc] := RealMap[Loc] and not fTerImp;
end;
// update map of all observing players
if Mode > moLoading_Fast then
for p1 := 0 to nPl - 1 do
if (1 shl p1 and (GAlive or GWatching) <> 0) and
(ObserveLevel[Loc] shr (2 * p1) and 3 > lNoObserve) then
RW[p1].Map[Loc] := RW[p1].Map[Loc] and
not(fTerrain or fSpecial or fTerImp or fRoad or fRR or fCanal or
fPoll) or RealMap[Loc] and (fTerrain or fSpecial or fTerImp or
fRoad or fRR or fCanal or fPoll);
end;
{
Diplomacy
____________________________________________________________________
}
procedure GiveCivilReport(p, pAbout: integer);
begin
with RW[p].EnemyReport[pAbout]^ do
begin
// general info
TurnOfCivilReport := LastValidStat[pAbout];
move(RW[pAbout].Treaty, Treaty, SizeOf(Treaty));
Government := RW[pAbout].Government;
Money := RW[pAbout].Money;
// tech info
ResearchTech := RW[pAbout].ResearchTech;
ResearchDone := RW[pAbout].Research * 100 div TechCost(pAbout);
if ResearchDone > 100 then
ResearchDone := 100;
move(RW[pAbout].Tech, Tech, nAdv);
end;
end;
procedure GiveMilReport(p, pAbout: integer);
var
uix, mix: integer;
begin
with RW[p].EnemyReport[pAbout]^ do
begin
TurnOfMilReport := LastValidStat[pAbout];
nModelCounted := RW[pAbout].nModel;
for mix := 0 to RW[pAbout].nModel - 1 do
begin
TellAboutModel(p, pAbout, mix);
UnCount[mix] := 0
end;
for uix := 0 to RW[pAbout].nUn - 1 do
if RW[pAbout].Un[uix].Loc >= 0 then
inc(UnCount[RW[pAbout].Un[uix].mix]);
end;
end;
procedure ShowPrice(pSender, pTarget, Price: integer);
begin
case Price and opMask of
opTech: // + advance
with RW[pTarget].EnemyReport[pSender]^ do
if Tech[Price - opTech] < tsApplicable then
Tech[Price - opTech] := tsApplicable;
opModel: // + model index
TellAboutModel(pTarget, pSender, Price - opModel);
{ opCity: // + city ID
begin
end; }
end;
end;
function CopyCivilReport(pSender, pTarget, pAbout: integer): boolean;
var
i: integer;
rSender, rTarget: ^TEnemyReport;
begin // copy third nation civil report
result := false;
if RW[pTarget].Treaty[pAbout] = trNoContact then
IntroduceEnemy(pTarget, pAbout);
rSender := pointer(RW[pSender].EnemyReport[pAbout]);
rTarget := pointer(RW[pTarget].EnemyReport[pAbout]);
if rSender.TurnOfCivilReport > rTarget.TurnOfCivilReport then
begin // only if newer than current information
rTarget.TurnOfCivilReport := rSender.TurnOfCivilReport;
rTarget.Treaty := rSender.Treaty;
rTarget.Government := rSender.Government;
rTarget.Money := rSender.Money;
rTarget.ResearchTech := rSender.ResearchTech;
rTarget.ResearchDone := rSender.ResearchDone;
result := true;
end;
for i := 0 to nAdv - 1 do
if rTarget.Tech[i] < rSender.Tech[i] then
begin
rTarget.Tech[i] := rSender.Tech[i];
result := true;
end;
end;
function CopyMilReport(pSender, pTarget, pAbout: integer): boolean;
var
mix: integer;
rSender, rTarget: ^TEnemyReport;
begin // copy third nation military report
result := false;
if RW[pTarget].Treaty[pAbout] = trNoContact then
IntroduceEnemy(pTarget, pAbout);
rSender := pointer(RW[pSender].EnemyReport[pAbout]);
rTarget := pointer(RW[pTarget].EnemyReport[pAbout]);
if rSender.TurnOfMilReport > rTarget.TurnOfMilReport then
begin // only if newer than current information
rTarget.TurnOfMilReport := rSender.TurnOfMilReport;
rTarget.nModelCounted := rSender.nModelCounted;
move(rSender.UnCount, rTarget.UnCount, 2 * rSender.nModelCounted);
for mix := 0 to rTarget.nModelCounted - 1 do
TellAboutModel(pTarget, pAbout, mix);
result := true;
end;
end;
procedure CopyModel(pSender, pTarget, mix: integer);
var
i: integer;
miSender, miTarget: TModelInfo;
ok: boolean;
begin
// only if target doesn't already have a model like this
ok := RW[pTarget].nModel < nmmax;
MakeModelInfo(pSender, mix, RW[pSender].Model[mix], miSender);
for i := 0 to RW[pTarget].nModel - 1 do
begin
MakeModelInfo(pTarget, i, RW[pTarget].Model[i], miTarget);
if IsSameModel(miSender, miTarget) then
ok := false;
end;
if ok then
begin
RW[pTarget].Model[RW[pTarget].nModel] := RW[pSender].Model[mix];
with RW[pTarget].Model[RW[pTarget].nModel] do
begin
IntroTurn := GTurn;
if Kind = mkSelfDeveloped then
Kind := mkEnemyDeveloped;
Status := 0;
SavedStatus := 0;
Built := 0;
Lost := 0;
end;
inc(RW[pTarget].nModel);
inc(Researched[pTarget]);
TellAboutModel(pSender, pTarget, RW[pTarget].nModel - 1);
end;
end;
procedure CopyMap(pSender, pTarget: integer);
var
Loc, i, cix: integer;
Tile: Cardinal;
begin
for Loc := 0 to MapSize - 1 do
if (RW[pSender].MapObservedLast[Loc] > RW[pTarget].MapObservedLast[Loc])
then
begin
Tile := RW[pSender].Map[Loc];
if Tile and fCity <> 0 then
begin
i := 0;
while (i < RW[pTarget].nEnemyCity) and
(RW[pTarget].EnemyCity[i].Loc <> Loc) do
inc(i);
if i = RW[pTarget].nEnemyCity then
begin
inc(RW[pTarget].nEnemyCity);
assert(RW[pTarget].nEnemyCity < necmax);
RW[pTarget].EnemyCity[i].Status := 0;
RW[pTarget].EnemyCity[i].SavedStatus := 0;
end;
if Tile and fOwned <> 0 then
begin // city owned by sender -- create new info
cix := RW[pSender].nCity - 1;
while (cix >= 0) and (RW[pSender].City[cix].Loc <> Loc) do
dec(cix);
MakeCityInfo(pSender, cix, RW[pTarget].EnemyCity[i]);
end
else // city not owned by sender -- copy old info
begin
cix := RW[pSender].nEnemyCity - 1;
while (cix >= 0) and (RW[pSender].EnemyCity[cix].Loc <> Loc) do
dec(cix);
RW[pTarget].EnemyCity[i] := RW[pSender].EnemyCity[cix];
end;
end
else if RW[pTarget].Map[Loc] and fCity <> 0 then // remove enemycity
for cix := 0 to RW[pTarget].nEnemyCity - 1 do
if RW[pTarget].EnemyCity[cix].Loc = Loc then
RW[pTarget].EnemyCity[cix].Loc := -1;
Tile := Tile and (not(fSpecial or fModern) or ResourceMask[pTarget]);
Tile := Tile or (RW[pTarget].Map[Loc] and fModern);
if (Tile and fTerrain = RW[pTarget].Map[Loc] and fTerrain) then
Tile := Tile or (RW[pTarget].Map[Loc] and fSpecial);
if RW[pTarget].Map[Loc] and fTerrain = fUNKNOWN then
inc(Discovered[pTarget]);
RW[pTarget].Map[Loc] := RW[pTarget].Map[Loc] and fInEnemyZoC
// always preserve this flag!
or Tile and not(fUnit or fHiddenUnit or fStealthUnit or fObserved or
fSpiedOut or fOwned or fInEnemyZoC or fOwnZoCUnit or fPeace or fGrWall);
if RW[pSender].Territory[Loc] <> RW[pTarget].Territory[Loc] then
begin
RW[pTarget].Territory[Loc] := RW[pSender].Territory[Loc];
{ if RW[pTarget].BorderHelper<>nil then
RW[pTarget].BorderHelper[Loc]:=0; }
end;
RW[pTarget].Territory[Loc] := RW[pSender].Territory[Loc];
RW[pTarget].MapObservedLast[Loc] := RW[pSender].MapObservedLast[Loc];
end;
end;
function PayPrice(pSender, pTarget, Price: integer; execute: boolean): boolean;
var
pSubject, i, n, NewTreaty: integer;
begin
result := true;
case Price and opMask of
opCivilReport: // + turn + concerned player shl 16
begin
pSubject := Price shr 16 and $F;
if pTarget = pSubject then
result := false
else if pSender = pSubject then
begin
if execute then
GiveCivilReport(pTarget, pSender);
end
else if RW[pSender].EnemyReport[pSubject].TurnOfCivilReport < 0 then
result := false
else if execute then
CopyCivilReport(pSender, pTarget, pSubject);
end;
opMilReport: // + turn + concerned player shl 16
begin
pSubject := Price shr 16 and $F;
if pTarget = pSubject then
result := false
else if pSender = pSubject then
begin
if execute then
GiveMilReport(pTarget, pSender);
end
else if RW[pSender].EnemyReport[pSubject].TurnOfMilReport < 0 then
result := false
else if execute then
CopyMilReport(pSender, pTarget, pSubject);
end;
opMap:
if execute then
begin
CopyMap(pSender, pTarget);
RecalcPeaceMap(pTarget);
end;
opTreaty .. opTreaty + trAlliance: // + nation treaty
begin
if Price - opTreaty = RW[pSender].Treaty[pTarget] - 1 then
begin // agreed treaty end
if execute then
CancelTreaty(pSender, pTarget, false);
end
else
begin
NewTreaty := -1;
if Price - opTreaty = RW[pSender].Treaty[pTarget] + 1 then
NewTreaty := Price - opTreaty
else if (RW[pSender].Treaty[pTarget] = trNone) and
(Price - opTreaty = trPeace) then
NewTreaty := trPeace;
if NewTreaty < 0 then
result := false
else if execute then
begin
assert(NewTreaty > RW[pSender].Treaty[pTarget]);
RW[pSender].Treaty[pTarget] := NewTreaty;
RW[pTarget].Treaty[pSender] := NewTreaty;
if NewTreaty >= TrFriendlyContact then
begin
GiveCivilReport(pTarget, pSender);
GiveCivilReport(pSender, pTarget);
end;
if NewTreaty = trAlliance then
begin
GiveMilReport(pTarget, pSender);
GiveMilReport(pSender, pTarget);
CopyMap(pSender, pTarget);
CopyMap(pTarget, pSender);
RecalcMapZoC(pSender);
RecalcMapZoC(pTarget);
end;
if not(NewTreaty in [trPeace, TrFriendlyContact]) then
begin
RW[pSender].EvaStart[pTarget] := -PeaceEvaTurns - 1;
RW[pTarget].EvaStart[pSender] := -PeaceEvaTurns - 1;
end;
RecalcPeaceMap(pSender);
RecalcPeaceMap(pTarget);
end;
end;
end;
opShipParts: // + number + part type shl 16
begin
n := Price and $FFFF; // number
i := Price shr 16 and $F; // type
if (i < nShipPart) and (GShip[pSender].Parts[i] >= n) then
begin
if execute then
begin
dec(GShip[pSender].Parts[i], n);
RW[pSender].Ship[pSender].Parts[i] := GShip[pSender].Parts[i];
RW[pTarget].Ship[pSender].Parts[i] := GShip[pSender].Parts[i];
if RW[pTarget].NatBuilt[imSpacePort] > 0 then
begin // space ship control requires space port
inc(GShip[pTarget].Parts[i], n);
RW[pSender].Ship[pTarget].Parts[i] := GShip[pTarget].Parts[i];
RW[pTarget].Ship[pTarget].Parts[i] := GShip[pTarget].Parts[i];
end;
end;
end
else
result := false;
end;
opMoney: // + value
if (Price - opMoney <= MaxMoneyPrice) and
(RW[pSender].Money >= Price - opMoney) then
begin
if execute then
begin
dec(RW[pSender].Money, Price - opMoney);
inc(RW[pTarget].Money, Price - opMoney);
end;
end
else
result := false;
opTribute: // + value
if execute then
begin
end;
opTech: // + advance
if RW[pSender].Tech[Price - opTech] >= tsApplicable then
begin
if execute and (RW[pTarget].Tech[Price - opTech] = tsNA) then
begin
SeeTech(pTarget, Price - opTech);
RW[pSender].EnemyReport[pTarget].Tech[Price - opTech] := tsSeen;
end;
end
else
result := false;
opAllTech:
if execute then
for i := 0 to nAdv - 1 do
if (RW[pSender].Tech[i] >= tsApplicable) and
(RW[pTarget].Tech[i] = tsNA) then
begin
SeeTech(pTarget, i);
RW[pSender].EnemyReport[pTarget].Tech[i] := tsSeen;
RW[pTarget].EnemyReport[pSender].Tech[i] := tsApplicable;
end;
opModel: // + model index
if Price - opModel < RW[pSender].nModel then
begin
if execute then
CopyModel(pSender, pTarget, Price - opModel);
end
else
result := false;
opAllModel:
if execute then
for i := 0 to RW[pSender].nModel - 1 do
begin
TellAboutModel(pTarget, pSender, i);
CopyModel(pSender, pTarget, i);
end;
{ opCity: // + city ID
begin
result:=false
end; }
end;
end;
procedure CancelTreaty(p, pWith: integer; DecreaseCredibility: boolean);
// side effect: PeaceEnded := bitarray of players with which peace treaty was canceled
var
p1, OldTreaty: integer;
begin
OldTreaty := RW[p].Treaty[pWith];
PeaceEnded := 0;
if OldTreaty >= trPeace then
RW[p].LastCancelTreaty[pWith] := GTurn;
if DecreaseCredibility then
begin
case OldTreaty of
trPeace:
begin
RW[p].Credibility := RW[p].Credibility shr 1;
if RW[p].MaxCredibility > 0 then
dec(RW[p].MaxCredibility, 10);
if RW[p].Credibility > RW[p].MaxCredibility then
RW[p].Credibility := RW[p].MaxCredibility;
end;
trAlliance:
RW[p].Credibility := RW[p].Credibility * 3 div 4;
end;
RW[pWith].EnemyReport[p].Credibility := RW[p].Credibility;
end;
if OldTreaty = trPeace then
begin
for p1 := 0 to nPl - 1 do
if (p1 = pWith) or DecreaseCredibility and (p1 <> p) and
(RW[pWith].Treaty[p1] = trAlliance) and (RW[p].Treaty[p1] >= trPeace)
then
begin
RW[p].Treaty[p1] := trNone;
RW[p1].Treaty[p] := trNone;
RW[p].EvaStart[p1] := -PeaceEvaTurns - 1;
RW[p1].EvaStart[p] := -PeaceEvaTurns - 1;
inc(PeaceEnded, 1 shl p1);
end;
CheckBorders(-1);
if (Mode > moLoading_Fast) and (PeaceEnded > 0) then
RecalcMapZoC(p);
end
else
begin
RW[p].Treaty[pWith] := OldTreaty - 1;
RW[pWith].Treaty[p] := OldTreaty - 1;
if OldTreaty = TrFriendlyContact then
begin // necessary for loading
GiveCivilReport(p, pWith);
GiveCivilReport(pWith, p);
end
else if OldTreaty = trAlliance then
begin // necessary for loading
GiveMilReport(p, pWith);
GiveMilReport(pWith, p);
end;
if (Mode > moLoading_Fast) and (OldTreaty = trAlliance) then
begin
RecalcMapZoC(p);
RecalcMapZoC(pWith);
end;
end;
if OldTreaty in [trPeace, trAlliance] then
begin
RecalcPeaceMap(p);
RecalcPeaceMap(pWith);
end;
end;
function DoSpyMission(p, pCity, cix, Mission: integer): Cardinal;
var
p1: integer;
begin
result := 0;
case Mission of
smSabotageProd:
RW[pCity].City[cix].Flags := RW[pCity].City[cix].Flags or
chProductionSabotaged;
smStealMap:
begin
CopyMap(pCity, p);
RecalcPeaceMap(p);
end;
smStealCivilReport:
begin
if RW[p].Treaty[pCity] = trNoContact then
IntroduceEnemy(p, pCity);
GiveCivilReport(p, pCity);
end;
smStealMilReport:
begin
if RW[p].Treaty[pCity] = trNoContact then
IntroduceEnemy(p, pCity);
GiveMilReport(p, pCity);
end;
smStealForeignReports:
begin
for p1 := 0 to nPl - 1 do
if (p1 <> p) and (p1 <> pCity) and (RW[pCity].EnemyReport[p1] <> nil)
then
begin
if RW[pCity].EnemyReport[p1].TurnOfCivilReport >= 0 then
if CopyCivilReport(pCity, p, p1) then
result := result or (1 shl (2 * p1));
if RW[pCity].EnemyReport[p1].TurnOfMilReport >= 0 then
if CopyMilReport(pCity, p, p1) then
result := result or (2 shl (2 * p1));
end;
end;
end;
end;
{
Test Flags
____________________________________________________________________
}
procedure ClearTestFlags(ClearFlags: integer);
var
p1: integer;
begin
GTestFlags := GTestFlags and (not ClearFlags or tfTested or tfAllTechs or
tfAllContact);
for p1 := 0 to nPl - 1 do
if 1 shl p1 and (GAlive or GWatching) <> 0 then
RW[p1].TestFlags := GTestFlags;
end;
procedure SetTestFlags(p, SetFlags: integer);
var
i, p1, p2, MoreFlags: integer;
begin
MoreFlags := SetFlags and not GTestFlags;
GTestFlags := GTestFlags or (SetFlags and $7FF);
for p1 := 0 to nPl - 1 do
if 1 shl p1 and (GAlive or GWatching) <> 0 then
RW[p1].TestFlags := GTestFlags;
if MoreFlags and (tfUncover or tfAllContact) <> 0 then
for p1 := 0 to nPl - 2 do
if 1 shl p1 and GAlive <> 0 then
for p2 := p1 + 1 to nPl - 1 do
if 1 shl p2 and GAlive <> 0 then
begin // make p1 and p2 know each other
if RW[p1].Treaty[p2] = trNoContact then
IntroduceEnemy(p1, p2);
end;
if MoreFlags and tfAllTechs <> 0 then
for p1 := 0 to nPl - 1 do
begin
ResourceMask[p1] := $FFFFFFFF;
if 1 shl p1 and GAlive <> 0 then
begin
for i := 0 to nAdv - 1 do // give all techs to player p1
if not(i in FutureTech) and (RW[p1].Tech[i] < tsApplicable) then
begin
RW[p1].Tech[i] := tsCheat;
CheckSpecialModels(p1, i);
end;
for p2 := 0 to nPl - 1 do
if (p2 <> p1) and (1 shl p2 and (GAlive or GWatching) <> 0) then
for i := 1 to 3 do
if RW[p2].EnemyReport[p1].Tech[AgePreq[i]] < tsApplicable then
RW[p2].EnemyReport[p1].Tech[AgePreq[i]] := tsCheat;
end;
end;
if MoreFlags and tfUncover <> 0 then
begin
DiscoverAll(p, lObserveSuper);
for p1 := 0 to nPl - 1 do
if 1 shl p1 and GAlive <> 0 then
begin
ResourceMask[p1] := $FFFFFFFF;
if p1 <> p then
begin
GiveCivilReport(p, p1);
GiveMilReport(p, p1);
end;
end;
end;
end;
{
Internal Command Processing
____________________________________________________________________
}
procedure IntServer(Command, Player, Subject: integer; var Data);
var
i, p1: integer;
begin
if Mode = moPlaying then
CL.Put(Command, Player, Subject, @Data);
case Command of
sIntTellAboutNation:
begin
{$IFDEF TEXTLOG}CmdInfo := Format('IntTellAboutNation P%d+P%d', [Player, Subject]); {$ENDIF}
assert((Player >= 0) and (Player < nPl) and (Subject >= 0) and
(Subject < nPl));
IntroduceEnemy(Player, Subject);
end;
sIntHaveContact:
begin
{$IFDEF TEXTLOG}CmdInfo := Format('IntHaveContact P%d+P%d', [Player, Subject]); {$ENDIF}
assert(RW[Player].Treaty[Subject] > trNoContact);
RW[Player].EnemyReport[Subject].TurnOfContact := GTurn;
RW[Subject].EnemyReport[Player].TurnOfContact := GTurn;
end;
sIntCancelTreaty:
begin
{$IFDEF TEXTLOG}CmdInfo := Format('IntCancelTreaty P%d with P%d', [Player, Subject]); {$ENDIF}
CancelTreaty(Player, Subject);
end;
(* sIntChoosePeace:
begin
{$IFDEF TEXTLOG}CmdInfo:=Format('IntChoosePeace P%d+P%d', [Player,Subject]);{$ENDIF}
RW[Player].Treaty[Subject]:=trPeace;
RW[Subject].Treaty[Player]:=trPeace;
end; *)
sIntTellAboutModel .. sIntTellAboutModel + (nPl - 1) shl 4:
begin
p1 := (Command - sIntTellAboutModel) shr 4; // told player
{$IFDEF TEXTLOG}CmdInfo := Format('IntTellAboutModel P%d about P%d Mod%d', [p1, Player, Subject]); {$ENDIF}
assert((Player >= 0) and (Player < nPl));
assert((Subject >= 0) and (Subject < RW[Player].nModel), 'Incompatible Saved Game');
MakeModelInfo(Player, Subject, RW[Player].Model[Subject],
RW[p1].EnemyModel[RW[p1].nEnemyModel]);
RWemix[p1, Player, Subject] := RW[p1].nEnemyModel;
inc(RW[p1].nEnemyModel);
assert(RW[p1].nEnemyModel < nemmax);
end;
sIntDiscoverZOC:
begin
{$IFDEF TEXTLOG}CmdInfo := Format('IntDiscoverZOC P%d Loc%d', [Player, integer(Data)]); {$ENDIF}
Discover9(integer(Data), Player, lObserveUnhidden, true, false);
end;
sIntExpandTerritory:
if Mode < moPlaying then
begin
{$IFDEF TEXTLOG}CmdInfo := Format('IntExpandTerritory P%d Loc%d', [Player, RW[Player].City[Subject].Loc]); {$ENDIF}
move(Data, BorderChanges, SizeOf(BorderChanges));
ExpandTerritory(RW[Player].City[Subject].Loc);
end;
sIntBuyMaterial:
with RW[Player].City[Subject] do
begin
{$IFDEF TEXTLOG}CmdInfo := Format('IntBuyMaterial P%d Loc%d Cost%d', [Player, Loc, integer(Data)]); {$ENDIF}
dec(RW[Player].Money, integer(Data));
if (GWonder[woMich].EffectiveOwner = Player) and (Project and cpImp <> 0)
then
inc(Prod, integer(Data) div 2)
else
inc(Prod, integer(Data) div 4);
if Project0 and not cpAuto <> Project and not cpAuto then
Project0 := Project;
Prod0 := Prod;
end;
sIntPayPrices .. sIntPayPrices + 12:
begin
{$IFDEF TEXTLOG}CmdInfo := Format('IntPayPrices P%d+P%d', [Player, Subject]); {$ENDIF}
for i := 0 to TOffer(Data).nDeliver - 1 do
PayPrice(Player, Subject, TOffer(Data).Price[i], true);
for i := 0 to TOffer(Data).nCost - 1 do
PayPrice(Subject, Player, TOffer(Data).Price[TOffer(Data).nDeliver
+ i], true);
for i := 0 to TOffer(Data).nDeliver + TOffer(Data).nCost - 1 do
if TOffer(Data).Price[i] = opTreaty + trAlliance then
begin // add view area of allied player
DiscoverViewAreas(Player);
DiscoverViewAreas(Subject);
Break;
end;
end;
sIntSetDevModel:
if Mode < moPlaying then
Move(Data, RW[Player].DevModel.Kind, sIntSetDevModel and $F * 4);
sIntSetModelStatus:
if ProcessClientData[Player] then
begin
{$IFDEF TEXTLOG}CmdInfo := Format('IntSetModelStatus P%d S%d D%d', [Player, Subject, integer(Data)]);
{$ENDIF}
RW[Player].Model[Subject].Status := integer(Data);
end;
sIntSetUnitStatus:
if ProcessClientData[Player] then
begin
{$IFDEF TEXTLOG}CmdInfo := Format('IntSetUnitStatus P%d S%d D%d', [Player, Subject, integer(Data)]);
{$ENDIF}
RW[Player].Un[Subject].Status := integer(Data);
end;
sIntSetCityStatus:
if ProcessClientData[Player] then
begin
{$IFDEF TEXTLOG}CmdInfo := Format('IntSetCityStatus P%d S%d D%d', [Player, Subject, integer(Data)]);
{$ENDIF}
RW[Player].City[Subject].Status := integer(Data);
end;
sIntSetECityStatus:
if ProcessClientData[Player] then
begin
{$IFDEF TEXTLOG}CmdInfo := Format('IntSetECityStatus P%d', [Player]);
{$ENDIF}
RW[Player].EnemyCity[Subject].Status := integer(Data);
end;
end;
end;
end.
|