1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533
|
{$INCLUDE Switches.inc}
// {$DEFINE TEXTLOG}
// {$DEFINE LOADPERF}
unit GameServer;
interface
uses
Protocol, Database, dynlibs, Platform, dateutils, LazFileUtils, Graphics,
UBrain, Global;
const
FirstAICompatibleVersion = $000D00;
FirstBookCompatibleVersion = $010103;
maxBrain = 255;
type
// notifications
TNotify = (
ntCreateWorld,
ntInitModule,
ntInitLocalHuman,
ntDLLError,
ntAIError,
ntClientError,
ntInitPlayers,
ntDeactivationMissing,
ntSetAIName,
ntException,
ntLoadBegin,
ntLoadState,
ntEndInfo,
ntBackOn,
ntBackOff,
ntLoadError,
ntStartDone,
ntStartGo,
ntStartGoRefresh,
ntStartGoRefreshMaps,
ntChangeClient,
ntNextPlayer,
ntDeinitModule
);
TNotifyFunction = procedure(ID: TNotify; Index: Integer = 0);
var
// PARAMETERS
PlayersBrain: TBrains; { brain of the players view }
Difficulty: array [0 .. nPl - 1] of integer absolute Database.Difficulty;
{ difficulty }
// READ ONLY
DotNetClient: TClientCall;
Brains: TBrains; // { available brains }
NotifyMessage: string;
BrainNoTerm: TBrain;
BrainSuperVirtual: TBrain;
BrainTerm: TBrain;
BrainRandom: TBrain;
BrainNetworkClient: TBrain;
BrainNetworkServer: TBrain;
NetworkEnabled: Boolean;
procedure Init(NotifyFunction: TNotifyFunction);
procedure Done;
procedure StartNewGame(const Path, FileName, Map: string;
Newlx, Newly, NewLandMass, NewMaxTurn: integer);
function LoadGame(const Path, FileName: string; Turn: integer;
MovieMode: boolean): boolean;
procedure EditMap(const Map: string; Newlx, Newly, NewLandMass: integer);
procedure DirectHelp(Command: integer);
procedure ChangeClient;
procedure NextPlayer;
function PreviewMap(lm: integer): pointer;
implementation
uses
Directories, CityProcessing, UnitProcessing, CmdList, LCLIntf, LCLType,
LMessages, Classes, SysUtils;
resourcestring
SNoAiFound = 'No AI libraries found in directory %s';
var
MaxTurn: Integer;
LoadTurn: Integer; { turn where to stop loading }
nLogOpened: Integer; { nLog of opened book }
{$IFOPT O-}nHandoverStack, {$ENDIF}
LastEndClientCommand: Integer;
pContacted: Integer; // player contacted for negotiation
pDipActive: Integer; // player who's to speak in a negotiation
pTurn: Integer; { player who's turn it is }
GWinner: Integer;
GColdWarStart: Integer;
GStealFrom: Integer;
SpyMission: Integer;
ZOCTile: Integer;
CCCommand: Integer;
CCPlayer: Integer;
DebugMap: array [0 .. nPl - 1] of Pointer;
ExeInfo: TSearchRec;
Stat: array [0 .. nStat - 1, 0 .. nPl - 1] of ^TChart;
AutoSaveState: TCmdListState;
MapField: ^Cardinal; // predefined map
LastOffer: TOffer;
CCData: array [0 .. 14] of integer;
bix: TBrains; { brain of the players }
DevModelTurn: array [0 .. nPl - 1] of Integer; { turn of last call to sResetModel }
OriginalDataVersion: array [0 .. nPl - 1] of Integer;
SavedTiles { , SavedResourceWeights } : array [0 .. ncmax - 1] of Cardinal;
SavedData: array [0 .. nPl - 1] of pointer;
LogFileName: string;
SavePath: string; { name of file for saving the current game }
MapFileName: string; // name of map to use, empty for random
AICredits: string;
AIInfo: array [0 .. nPl - 1] of string;
Notify: TNotifyFunction;
LastClientTime: TDateTime;
{$IFOPT O-}HandoverStack: array [0 .. 31] of Cardinal; {$ENDIF}
AutoSaveExists: Boolean;
LoadOK: Boolean;
WinOnAlone: Boolean;
PreviewElevation: Boolean;
MovieStopped: Boolean;
const
PreviewRND = 41601260; { randseed for preview map }
function Server(Command, Player, Subject: integer; var Data): integer;
stdcall; forward;
procedure CallPlayer(Command, p: integer; var Data);
begin
if ((Mode <> moMovie) or (p = 0)) then
begin
{$IFOPT O-}
HandoverStack[nHandoverStack] := p;
HandoverStack[nHandoverStack + 1] := Command;
inc(nHandoverStack, 2);
bix[p].Client(Command, p, Data);
dec(nHandoverStack, 2);
{$ELSE}
try
bix[p].Client(Command, p, Data);
except
Notify(ntException + bix[p]);
end;
{$ENDIF}
end;
end;
procedure CallAllPlayers(Command: Integer; var Data);
var
I: Integer;
begin
for I := 0 to nPl - 1 do
if Assigned(bix[I]) then
CallPlayer(Command, I, Data);
end;
procedure CallClient(bix, Command: integer; var Data);
begin
if ((Mode <> moMovie) or (bix = Brains.IndexOf(GameServer.bix[0]))) then
begin
{$IFOPT O-}
HandoverStack[nHandoverStack] := bix;
HandoverStack[nHandoverStack + 1] := Command;
inc(nHandoverStack, 2);
Brains[bix].Client(Command, -1, Data);
dec(nHandoverStack, 2);
{$ELSE}
try
Brain[bix].Client(Command, -1, Data);
except
Notify(ntException + bix);
end;
{$ENDIF}
end
end;
procedure Init(NotifyFunction: TNotifyFunction);
var
f: TSearchRec;
BasePath: string;
NewBrain: TBrain;
I: Integer;
begin
Notify := NotifyFunction;
PreviewElevation := false;
PlayersBrain := TBrains.Create(False);
PlayersBrain.Count := nPl;
for I := 0 to nPl - 1 do
PlayersBrain[I] := nil;
bix := TBrains.Create(False);
bix.Count := nPl;
for I := 0 to nPl - 1 do
bix[I] := nil;
{ get available brains }
Brains := TBrains.Create;
BrainNoTerm := Brains.AddNew;
BrainNoTerm.FileName := ':AIT';
BrainNoTerm.Flags := 0;
BrainNoTerm.Initialized := False;
BrainNoTerm.Kind := btNoTerm;
BrainSuperVirtual := Brains.AddNew;
BrainSuperVirtual.FileName := ':Supervisor';
BrainSuperVirtual.Flags := 0;
BrainSuperVirtual.Initialized := False;
BrainSuperVirtual.Kind := btSuperVirtual;
if NetworkEnabled then begin
BrainNetworkClient := Brains.AddNew;
BrainNetworkClient.FileName := ':NetworkClient';
BrainNetworkClient.Flags := fMultiple;
BrainNetworkClient.Initialized := False;
BrainNetworkClient.ServerVersion := CevoVersion;
BrainNetworkClient.Kind := btNetworkClient;
end;
BrainTerm := Brains.AddNew;
BrainTerm.FileName := ':StdIntf';
BrainTerm.Flags := fMultiple;
BrainTerm.Initialized := False;
BrainTerm.ServerVersion := CevoVersion;
BrainTerm.Kind := btTerm;
BrainRandom := Brains.AddNew;
BrainRandom.FileName := ':Random';
BrainRandom.Flags := fMultiple;
BrainRandom.Initialized := False;
BrainRandom.Kind := btRandom;
if NetworkEnabled then begin
BrainNetworkServer := Brains.AddNew;
BrainNetworkServer.FileName := ':NetworkServer';
BrainNetworkServer.Flags := fMultiple;
BrainNetworkServer.Initialized := False;
BrainNetworkServer.ServerVersion := CevoVersion;
BrainNetworkServer.Kind := btNetworkServer;
end;
if FindFirst(GetAiDir + DirectorySeparator + '*', faDirectory or faArchive or faReadOnly, f) = 0 then
repeat
BasePath := GetAiDir + DirectorySeparator + f.Name;
if (f.Name <> '.') and (f.Name <> '..') and DirectoryExists(BasePath) then begin
NewBrain := Brains.AddNew;
NewBrain.Kind := btAI;
NewBrain.LoadFromFile(BasePath + DirectorySeparator + F.Name + '.ai.txt');
if (NewBrain.ServerVersion >= FirstAICompatibleVersion) and
(NewBrain.ServerVersion <= CevoVersion) and
((NewBrain.Flags and fDotNet = 0) or (@DotNetClient <> nil)) then begin
end else Brains.Delete(Brains.Count - 1);
end;
until FindNext(f) <> 0;
FindClose(F);
if Brains.GetKindCount(btAI) = 0 then
raise Exception.Create(Format(SNoAiFound, [GetAiDir]));
end;
procedure Done;
var
I: Integer;
begin
for I := 0 to Brains.Count - 1 do
with Brains[I] do
if Initialized then begin
CallClient(I, cReleaseModule, nil^);
if (Kind = btAI) and ((Flags and fDotNet) = 0) then
FreeLibrary(hm);
end;
FreeAndNil(PlayersBrain);
FreeAndNil(bix);
FreeAndNil(Brains);
end;
function PreviewMap(lm: integer): pointer;
begin
lx := lxmax;
ly := lymax;
MapSize := lx * ly;
LandMass := lm;
DelphiRandSeed := PreviewRND;
if not PreviewElevation then
begin
CreateElevation;
PreviewElevation := true;
end;
CreateMap(true);
Result := @RealMap;
end;
procedure ChangeClientWhenDone(Command, Player: integer; var Data;
DataSize: integer);
begin
CCCommand := Command;
CCPlayer := Player;
if DataSize > 0 then
move(Data, CCData, DataSize);
Notify(ntChangeClient);
end;
procedure PutMessage(Level: integer; Text: string);
begin
bix[0].Client(cDebugMessage, Level, pchar(Text)^);
end;
procedure ForceClientDeactivation;
var
NullOffer: TOffer;
begin
if pDipActive < 0 then
Server(sTurn, pTurn, 0, nil^) // no nego mode
else
case LastEndClientCommand of // nego mode
scContact:
Server(scReject, pDipActive, 0, nil^);
scDipCancelTreaty, scDipBreak:
Server(scDipNotice, pDipActive, 0, nil^);
else
begin // make null offer
NullOffer.nDeliver := 0;
NullOffer.nCost := 0;
Server(scDipOffer, pDipActive, 0, NullOffer);
end
end
end;
procedure ChangeClient;
// hand over control to other client (as specified by CC...)
var
p: integer;
T: TDateTime;
begin
T := NowPrecise;
PutMessage(1 shl 16 + 2, Format('CLIENT: took %.1f ms',
[(T - LastClientTime) / OneMillisecond]));
LastClientTime := T;
PutMessage(1 shl 16 + 2, Format('CLIENT: calling %d (%s)',
[CCPlayer, bix[CCPlayer].Name]));
if CCCommand = cTurn then
for p := 0 to nPl - 1 do
if (p <> CCPlayer) and (1 shl p and GWatching <> 0) then
CallPlayer(cShowTurnChange, p, CCPlayer);
p := CCPlayer;
CCPlayer := -1;
CallPlayer(CCCommand, p, CCData);
if (Mode = moPlaying) and (bix[p].Flags and aiThreaded = 0) and
(CCPlayer < 0) then
begin
Notify(ntDeactivationMissing, p);
ForceClientDeactivation;
end;
end;
procedure Inform(p: integer);
var
i, p1: integer;
begin
RW[p].Turn := GTurn;
if (GTurn = MaxTurn) and (p = pTurn) and (p = 0) then
RW[p].Happened := RW[p].Happened or phTimeUp;
if (GWinner > 0) and (p = pTurn) and (p = 0) then
RW[p].Happened := RW[p].Happened or phShipComplete;
RW[p].Alive := GAlive;
move(GWonder, RW[p].Wonder, SizeOf(GWonder));
move(GShip, RW[p].Ship, SizeOf(GShip));
for p1 := 0 to nPl - 1 do
if (p1 <> p) and Assigned(bix[p1]) and (Difficulty[p1] > 0) then
RW[p].EnemyReport[p1].Credibility := RW[p1].Credibility;
for p1 := 0 to nPl - 1 do
if (p1 <> p) and (1 shl p1 and GAlive <> 0) then
begin
if (GTestFlags and tfUncover <> 0) or (Difficulty[p] = 0) or
(RW[p].Treaty[p1] >= trFriendlyContact) then
GiveCivilReport(p, p1);
if (GTestFlags and tfUncover <> 0) or (Difficulty[p] = 0) or
(RW[p].Treaty[p1] = trAlliance) then
GiveMilReport(p, p1)
end;
for i := 0 to RW[p].nEnemyModel - 1 do
with RW[p].EnemyModel[i] do
Lost := Destroyed[p, Owner, mix];
end;
procedure LogChanges;
var
p, ix: integer;
begin
for p := 0 to nPl - 1 do
if (1 shl p and GWatching <> 0) and ProcessClientData[p] then
begin
// log unit status changes
for ix := 0 to RW[p].nUn - 1 do
with RW[p].Un[ix] do
if (Loc >= 0) and (SavedStatus <> Status) then
begin
CL.Put(sIntSetUnitStatus, p, ix, @Status);
SavedStatus := Status;
end;
// log city status changes
for ix := 0 to RW[p].nCity - 1 do
with RW[p].City[ix] do
if (Loc >= 0) and (SavedStatus <> Status) then
begin
CL.Put(sIntSetCityStatus, p, ix, @Status);
SavedStatus := Status;
end;
// log model status changes
for ix := 0 to RW[p].nModel - 1 do
with RW[p].Model[ix] do
if SavedStatus <> Status then
begin
CL.Put(sIntSetModelStatus, p, ix, @Status);
SavedStatus := Status;
end;
// log enemy city status changes
for ix := 0 to RW[p].nEnemyCity - 1 do
with RW[p].EnemyCity[ix] do
if (Loc >= 0) and (SavedStatus <> Status) then
begin
CL.Put(sIntSetECityStatus, p, ix, @Status);
SavedStatus := Status;
end;
// log data changes
if bix[p].DataSize > 0 then
begin
CL.PutDataChanges(sIntDataChange, p, SavedData[p], RW[p].Data,
bix[p].DataSize);
Move(RW[p].Data^, SavedData[p]^, bix[p].DataSize * 4);
end;
end;
end;
procedure NoLogChanges;
var
p, ix: integer;
begin
for p := 0 to nPl - 1 do
if (1 shl p and GWatching <> 0) and ProcessClientData[p] then
begin
for ix := 0 to RW[p].nUn - 1 do
with RW[p].Un[ix] do
SavedStatus := Status;
for ix := 0 to RW[p].nCity - 1 do
with RW[p].City[ix] do
SavedStatus := Status;
for ix := 0 to RW[p].nModel - 1 do
with RW[p].Model[ix] do
SavedStatus := Status;
for ix := 0 to RW[p].nEnemyCity - 1 do
with RW[p].EnemyCity[ix] do
SavedStatus := Status;
if bix[p].DataSize > 0 then
Move(RW[p].Data^, SavedData[p]^, bix[p].DataSize * 4);
end;
end;
function HasChanges(p: integer): boolean;
type
TDWordList = array [0 .. INFIN] of Cardinal;
PDWortList = ^TDWordList;
var
ix: integer;
begin
Result := False;
for ix := 0 to RW[p].nUn - 1 do
with RW[p].Un[ix] do
if (Loc >= 0) and (SavedStatus <> Status) then
Result := True;
for ix := 0 to RW[p].nCity - 1 do
with RW[p].City[ix] do
if (Loc >= 0) and (SavedStatus <> Status) then
Result := True;
for ix := 0 to RW[p].nModel - 1 do
with RW[p].Model[ix] do
if SavedStatus <> Status then
Result := True;
for ix := 0 to RW[p].nEnemyCity - 1 do
with RW[p].EnemyCity[ix] do
if (Loc >= 0) and (SavedStatus <> Status) then
Result := True;
if RW[p].Data <> nil then
for ix := 0 to bix[p].DataSize - 1 do
if PDWortList(SavedData[p])[ix] <> PDWortList(RW[p].Data)[ix] then
Result := True;
end;
procedure InitBrain(bix: TBrain);
var
InitModuleData: TInitModuleData;
begin
assert(bix.Kind <> btSuperVirtual);
with bix do begin
if Initialized then
exit;
if Kind = btAI then
begin { get client function }
Notify(ntInitModule, Brains.IndexOf(bix));
if Flags and fDotNet > 0 then
Client := DotNetClient
else
begin
hm := LoadLibrary(pchar(DLLName));
if hm = 0 then
begin
Client := nil;
Notify(ntDLLError, Brains.IndexOf(bix));
end
else
begin
Client := GetProcAddress(hm, 'client');
if @Client = nil then
Notify(ntClientError, Brains.IndexOf(bix));
end;
end;
end;
if @Client <> nil then
begin
Initialized := true;
InitModuleData.Server := @Server;
InitModuleData.DataVersion := 0;
InitModuleData.DataSize := 0;
InitModuleData.Flags := 0;
CallClient(Brains.IndexOf(bix), cInitModule, InitModuleData);
DataVersion := InitModuleData.DataVersion;
DataSize := (InitModuleData.DataSize + 3) div 4;
if DataSize > MaxDataSize then
DataSize := 0;
Flags := Flags or InitModuleData.Flags;
end;
end;
end;
procedure SaveMap(FileName: string);
var
i: integer;
MapFile: TFileStream;
s: string[255];
begin
MapFile := TFileStream.Create(FileName, fmCreate or fmShareExclusive);
try
MapFile.Position := 0;
s := 'cEvoMap'#0;
MapFile.write(s[1], 8); { file id }
i := 0;
MapFile.write(i, 4); { format id }
MapFile.write(MaxTurn, 4);
MapFile.write(lx, 4);
MapFile.write(ly, 4);
MapFile.write(RealMap, MapSize * 4);
finally
FreeAndNil(MapFile);
end;
end;
function LoadMap(FileName: string): boolean;
var
i, Loc1: integer;
MapFile: TFileStream;
s: string[255];
begin
result := false;
MapFile := nil;
try
MapFile := TFileStream.Create(FileName, fmOpenRead or fmShareExclusive);
MapFile.Position := 0;
MapFile.read(s[1], 8); { file id }
MapFile.read(i, 4); { format id }
if i = 0 then
begin
MapFile.read(i, 4); // MaxTurn
MapFile.read(lx, 4);
MapFile.read(ly, 4);
ly := ly and not 1;
if lx > lxmax then
lx := lxmax;
if ly > lymax then
ly := lymax;
MapSize := lx * ly;
MapFile.read(RealMap, MapSize * 4);
for Loc1 := 0 to MapSize - 1 do
begin
RealMap[Loc1] := RealMap[Loc1] and
($7F01FFFF or fPrefStartPos or fStartPos) or ($F shl 27);
if RealMap[Loc1] and (fTerrain or fSpecial) = fSwamp or fSpecial2 then
RealMap[Loc1] := RealMap[Loc1] and not(fTerrain or fSpecial) or
(fSwamp or fSpecial1);
if (RealMap[Loc1] and fDeadLands <> 0) and
(RealMap[Loc1] and fTerrain <> fArctic) then
RealMap[Loc1] := RealMap[Loc1] and not(fTerrain or fSpecial)
or fDesert;
end;
result := true;
end;
FreeAndNil(MapFile);
except
{IFDEF DEBUG}
Writeln ('failed to load map ', FileName);
{ENDIF}
if MapFile <> nil then
FreeAndNil(MapFile);
end;
end;
procedure SaveGame(FileName: string; auto: boolean);
type
tMapBytes = array [0 .. lxmax * lymax - 1] of Byte;
var
x, y, i, zero, Tile, nLocal: integer;
LogFile: TFileStream;
s: string[255];
SaveMap: tMapBytes;
begin
SaveMap := default (tMapBytes);
nLocal := 0;
for i := 0 to nPl - 1 do
if Assigned(bix[i]) and (bix[i].Kind = btTerm) then
inc(nLocal);
if Difficulty[0] = 0 then
nLocal := 0;
if nLocal <= 1 then
for y := 0 to ly - 1 do
for x := 0 to lx - 1 do
begin
Tile := RW[0].Map[(x + SaveMapCenterLoc + lx shr 1) mod lx + lx * y];
SaveMap[x + lx * y] := Tile and fTerrain + Tile and
(fCity or fUnit or fOwned) shr 16;
end;
if auto and AutoSaveExists then // append to existing file
LogFile := TFileStream.Create(SavePath + FileName, fmOpenReadWrite or
fmShareExclusive)
else // create new file
LogFile := TFileStream.Create(SavePath + FileName,
fmCreate or fmShareExclusive);
zero := 0;
LogFile.Position := 0;
s := 'cEvoBook';
LogFile.write(s[1], 8); { file id }
i := CevoVersion;
LogFile.write(i, 4); { c-evo version }
LogFile.write(ExeInfo.Time, 4);
LogFile.write(lx, 4);
LogFile.write(ly, 4);
LogFile.write(LandMass, 4);
if LandMass = 0 then
LogFile.write(MapField^, MapSize * 4);
LogFile.write(MaxTurn, 4);
LogFile.write(RND, 4);
LogFile.write(GTurn, 4);
if nLocal > 1 then // multiplayer game -- no quick view
begin
i := $80;
LogFile.write(i, 4);
end
else
LogFile.write(SaveMap, ((MapSize - 1) div 4 + 1) * 4);
for i := 0 to nPl - 1 do
if not Assigned(bix[i]) then
LogFile.write(zero, 4)
else
begin
if PlayersBrain[i].Kind in [btRandom, btAI] then
s := bix[i].FileName
else
s := PlayersBrain[i].FileName;
move(zero, s[Length(s) + 1], 4);
LogFile.write(s, (Length(s) div 4 + 1) * 4);
LogFile.write(OriginalDataVersion[i], 4);
s := ''; { behavior }
move(zero, s[Length(s) + 1], 4);
LogFile.write(s, (Length(s) div 4 + 1) * 4);
LogFile.write(Difficulty[i], 4);
end;
if auto and AutoSaveExists then
CL.AppendToFile(LogFile, AutoSaveState)
else
CL.SaveToFile(LogFile);
FreeAndNil(LogFile);
if auto then
begin
AutoSaveState := CL.State;
AutoSaveExists := true;
end
end;
procedure StartGame;
var
i, p, p1, Human, nAlive, bixUni: integer;
Game: TNewGameData;
// GameEx: TNewGameExData;
Path: shortstring;
BrainUsed: Set of 0 .. 254; { used brains }
AIBrains: TBrains;
begin
for p1 := 0 to nPl - 1 do begin
if Assigned(PlayersBrain[p1]) and (PlayersBrain[p1].Kind = btSuperVirtual) then
bix[p1] := BrainTerm // supervisor and local human use same module
else if Assigned(PlayersBrain[p1]) and (PlayersBrain[p1].Kind = btRandom) then
if Brains.GetKindCount(btAI) = 0 then
bix[p1] := nil
else begin
AIBrains := TBrains.Create(False);
Brains.GetByKind(btAI, AIBrains);
bix[p1] := AIBrains[DelphiRandom(AIBrains.Count)];
FreeAndNil(AIBrains);
end
else
bix[p1] := PlayersBrain[p1];
if not Assigned(PlayersBrain[p1]) then
Difficulty[p1] := -1;
end;
if bix[0].Kind <> btNoTerm then
Notify(ntInitLocalHuman);
BrainUsed := [];
for p := 0 to nPl - 1 do
if Assigned(bix[p]) and ((Mode <> moMovie) or (p = 0)) then
begin { initiate selected control module }
AIInfo[p] := bix[p].Name + #0;
InitBrain(bix[p]);
if Mode = moPlaying then
begin // new game, this data version is original
OriginalDataVersion[p] := bix[p].DataVersion;
ProcessClientData[p] := true;
end
else // loading game, compare with data version read from file
ProcessClientData[p] := ProcessClientData[p] and
(OriginalDataVersion[p] = bix[p].DataVersion);
if @bix[p].Client = nil then // client function not found
if bix[0].Kind = btNoTerm then
bix[p] := nil
else
begin
bix[p] := BrainTerm;
OriginalDataVersion[p] := -1;
ProcessClientData[p] := false;
end;
if Assigned(bix[p]) then
include(BrainUsed, Brains.IndexOf(bix[p]));
end;
Notify(ntCreateWorld);
nAlive := 0;
GAlive := 0;
if Mode = moMovie then
GWatching := 1
else
GWatching := 0;
GAI := 0;
for p1 := 0 to nPl - 1 do
if Assigned(bix[p1]) then
begin
if Mode <> moMovie then
inc(GWatching, 1 shl p1);
if bix[p1].Kind = btAI then
inc(GAI, 1 shl p1);
if Difficulty[p1] > 0 then
begin
inc(GAlive, 1 shl p1);
inc(nAlive);
end;
ServerVersion[p1] := bix[p1].ServerVersion;
end;
WinOnAlone := (bix[0].Kind = btNoTerm) and (nAlive > 1);
GWinner := 0;
GColdWarStart := -ColdWarTurns - 1;
uixSelectedTransport := -1;
SpyMission := smSabotageProd;
for p1 := 0 to nPl - 1 do
DebugMap[p1] := nil;
GTurn := 0;
for i := 0 to nWonder - 1 do
with GWonder[i] do
begin
CityID := -1;
EffectiveOwner := -1
end;
FillChar(GShip, SizeOf(GShip), 0);
for p := 0 to nPl - 1 do
if 1 shl p and (GAlive or GWatching) <> 0 then
with RW[p] do
begin
Government := gDespotism;
Money := StartMoney;
TaxRate := 30;
LuxRate := 0;
Research := 0;
ResearchTech := -2;
AnarchyStart := -AnarchyTurns - 1;
Happened := 0;
LastValidStat[p] := -1;
Worked[p] := 0;
Founded[p] := 0;
DevModelTurn[p] := -1;
OracleIncome := 0;
if bix[p].DataSize > 0 then
begin
GetMem(SavedData[p], bix[p].DataSize * 4);
GetMem(Data, bix[p].DataSize * 4);
FillChar(SavedData[p]^, bix[p].DataSize * 4, 0);
FillChar(Data^, bix[p].DataSize * 4, 0);
end
else
begin
Data := nil;
SavedData[p] := nil;
end;
nBattleHistory := 0;
BattleHistory := nil;
{ if bix[p]=bixTerm then
begin
GetMem(BorderHelper,MapSize);
FillChar(BorderHelper^,MapSize,0);
end
else } BorderHelper := nil;
for i := 0 to nStat - 1 do
GetMem(Stat[i, p], 4 * (MaxTurn + 1));
if bix[p].Flags and fDotNet <> 0 then
begin
GetMem(RW[p].DefaultDebugMap, MapSize * 4);
FillChar(RW[p].DefaultDebugMap^, MapSize * 4, 0);
DebugMap[p] := RW[p].DefaultDebugMap;
end
else
RW[p].DefaultDebugMap := nil;
{ !!!for i:=0 to nShipPart-1 do GShip[p].Parts[i]:=Delphirandom((3-i)*2); }
end;
if LandMass > 0 then
begin // random map
InitRandomGame;
PreviewElevation := false;
MapField := nil;
end
else
begin // predefined map
if Mode = moPlaying then
LoadMap(GetMapsDir + DirectorySeparator + MapFileName); // new game -- load map from file
GetMem(MapField, MapSize * 4);
move(RealMap, MapField^, MapSize * 4);
Human := 0;
for p1 := 0 to nPl - 1 do
if Assigned(bix[p1]) and (bix[p1].Kind = btTerm) then
inc(Human, 1 shl p1);
InitMapGame(Human);
end;
CityProcessing.InitGame;
UnitProcessing.InitGame;
for p := 0 to nPl - 1 do
if 1 shl p and (GAlive or GWatching) <> 0 then
Inform(p);
pTurn := -1;
if bix[0].Kind <> btNoTerm then
Notify(ntInitLocalHuman);
Game.lx := lx;
Game.ly := ly;
Game.LandMass := LandMass;
Game.MaxTurn := MaxTurn;
move(Difficulty, Game.Difficulty, SizeOf(Difficulty));
// GameEx.lx:=lx; GameEx.ly:=ly; GameEx.LandMass:=LandMass;
// GameEx.MaxTurn:=MaxTurn; GameEx.RND:=RND;
// move(Difficulty,GameEx.Difficulty,SizeOf(Difficulty));
AICredits := '';
for i := 0 to Brains.Count - 1 do
with Brains[I] do begin
if Initialized then
if i in BrainUsed then
begin
if Kind = btAI then
Notify(ntInitPlayers);
for p := 0 to nPl - 1 do
begin
if Brains.IndexOf(bix[p]) = i then
Game.RO[p] := @RW[p]
else
Game.RO[p] := nil;
if (Kind = btTerm) and (Difficulty[0] = 0) and Assigned(bix[p]) then
Game.SuperVisorRO[p] := @RW[p]
else
Game.SuperVisorRO[p] := nil;
end;
if Flags and fDotNet > 0 then
begin
Path := DLLName;
move(Path[1], Game.AssemblyPath, Length(Path));
Game.AssemblyPath[Length(Path)] := #0;
end
else
Game.AssemblyPath[0] := #0;
case Mode of
moLoading, moLoading_Fast:
CallClient(i, cLoadGame, Game);
moMovie:
CallClient(i, cMovie, Game);
moPlaying:
CallClient(i, cNewGame, Game);
end;
if (Kind = btAI) and (Credits <> '') then
if AICredits = '' then
AICredits := Credits
else
AICredits := AICredits + '\' + Credits;
end
else
begin { module no longer used -- unload }
CallClient(i, cReleaseModule, nil^);
if Kind = btAI then
begin
if Flags and fDotNet = 0 then
FreeLibrary(hm);
Client := nil;
end;
Initialized := false;
end;
end;
AICredits := AICredits + #0;
if bix[0].Kind <> btNoTerm then
begin
// uni ai?
bixUni := -1;
for p1 := 0 to nPl - 1 do
if Assigned(bix[p1]) and (bix[p1].Kind = btAI) then
if bixUni = -1 then
bixUni := Brains.IndexOf(bix[p1])
else if bixUni <> Brains.IndexOf(bix[p1]) then
bixUni := -2;
for p1 := 0 to nPl - 1 do
if Assigned(bix[p1]) and (bix[p1].Kind = btAI) then
begin
if bixUni = -2 then
NotifyMessage := bix[p1].FileName
else
NotifyMessage := '';
Notify(ntSetAIName, p1);
end;
end;
CheckBorders(-1);
{$IFOPT O-}InvalidTreatyMap := 0; {$ENDIF}
AutoSaveExists := false;
pDipActive := -1;
pTurn := 0;
if Mode >= moMovie then
Notify(ntEndInfo);
end;
procedure EndGame;
var
i, p1: integer;
begin
if LandMass = 0 then
FreeMem(MapField);
for p1 := 0 to nPl - 1 do
if Assigned(bix[p1]) then
begin
for i := 0 to nStat - 1 do
FreeMem(Stat[i, p1]);
if RW[p1].BattleHistory <> nil then
FreeMem(RW[p1].BattleHistory);
{ if RW[p1].BorderHelper<>nil then FreeMem(RW[p1].BorderHelper); }
if RW[p1].Data <> nil then
FreeMem(RW[p1].Data);
if SavedData[p1] <> nil then
FreeMem(SavedData[p1]);
if RW[p1].DefaultDebugMap <> nil then
FreeMem(RW[p1].DefaultDebugMap);
end;
UnitProcessing.ReleaseGame;
CityProcessing.ReleaseGame;
Database.ReleaseGame;
FreeAndNil(CL);
end;
procedure GenerateStat(p: integer);
var
cix, uix: integer;
begin
if Difficulty[p] > 0 then
with RW[p] do
begin
Stat[stPop, p, GTurn] := 0;
for cix := 0 to nCity - 1 do
if City[cix].Loc >= 0 then
inc(Stat[stPop, p, GTurn], City[cix].Size);
Stat[stScience, p, GTurn] := Researched[p] * 50;
if (RW[p].ResearchTech >= 0) and (RW[p].ResearchTech <> adMilitary) then
inc(Stat[stScience, p, GTurn], Research * 100 div TechBaseCost(nTech[p],
Difficulty[p]));
Stat[stMil, p, GTurn] := 0;
for uix := 0 to nUn - 1 do
if Un[uix].Loc >= 0 then
with Model[Un[uix].mix] do
begin
if (Kind <= mkEnemyDeveloped) and (Un[uix].mix <> 1) then
inc(Stat[stMil, p, GTurn], Weight * MStrength *
Un[uix].Health div 100)
else if Domain = dGround then
inc(Stat[stMil, p, GTurn], (Attack + 2 * Defense) *
Un[uix].Health div 100)
else
inc(Stat[stMil, p, GTurn], (Attack + Defense) *
Un[uix].Health div 100);
case Kind of
mkSlaves:
inc(Stat[stPop, p, GTurn]);
mkSettler:
inc(Stat[stPop, p, GTurn], 2);
end;
end;
Stat[stMil, p, GTurn] := Stat[stMil, p, GTurn] div 16;
Stat[stExplore, p, GTurn] := Discovered[p];
Stat[stTerritory, p, GTurn] := TerritoryCount[p];
Stat[stWork, p, GTurn] := Worked[p];
LastValidStat[p] := GTurn;
end;
end;
procedure LogCityTileChanges;
var
cix: integer;
begin
for cix := 0 to RW[pTurn].nCity - 1 do
with RW[pTurn].City[cix] do
if Loc >= 0 then
begin
{ if SavedResourceWeights[cix]<>ResourceWeights then
begin // log city resource weight changes
CL.Put(sSetCityResourceWeights, pTurn, cix, @ResourceWeights);
SavedResourceWeights[cix]:=ResourceWeights;
end; }
if SavedTiles[cix] <> Tiles then
begin // log city tile changes
CL.Put(sSetCityTiles, pTurn, cix, @Tiles);
SavedTiles[cix] := Tiles;
end;
end;
end;
procedure NoLogCityTileChanges;
var
cix: integer;
begin
for cix := 0 to RW[pTurn].nCity - 1 do
with RW[pTurn].City[cix] do
if Loc >= 0 then
begin
// SavedResourceWeights[cix]:=ResourceWeights;
SavedTiles[cix] := Tiles;
end;
end;
function HasCityTileChanges: boolean;
var
cix: integer;
begin
result := false;
for cix := 0 to RW[pTurn].nCity - 1 do
with RW[pTurn].City[cix] do
if Loc >= 0 then
begin
// if SavedResourceWeights[cix]<>ResourceWeights then result:=true;
if SavedTiles[cix] <> Tiles then
result := true;
end;
end;
procedure BeforeTurn0;
var
p1, uix: integer;
begin
for uix := 0 to RW[pTurn].nUn - 1 do { init movement points for first turn }
with RW[pTurn].Un[uix] do
Movement := RW[pTurn].Model[mix].Speed;
if Difficulty[pTurn] > 0 then
DiscoverViewAreas(pTurn)
else { supervisor }
begin
DiscoverAll(pTurn, lObserveSuper);
for p1 := 1 to nPl - 1 do
if 1 shl p1 and GAlive <> 0 then
begin
GiveCivilReport(pTurn, p1);
GiveMilReport(pTurn, p1);
end;
end;
// CheckContact;
end;
function LoadGame(const Path, FileName: string; Turn: integer;
MovieMode: boolean): boolean;
var
J: TBrain;
i, ix, d, p1, Command, Subject: integer;
K: Integer;
{$IFDEF TEXTLOG}LoadPos0: integer; {$ENDIF}
Data: pointer;
LogFile: TFileStream;
FormerCLState: TCmdListState;
s: string[255];
SaveMap: array [0 .. lxmax * lymax - 1] of Byte;
Started, StatRequest: boolean;
begin
SavePath := Path;
LogFileName := FileName;
LoadTurn := Turn;
LogFile := TFileStream.Create(SavePath + LogFileName, fmOpenRead or
fmShareExclusive);
LogFile.Position := 0;
LogFile.Read(s[1], 8); { file id }
LogFile.Read(i, 4); { c-evo version }
LogFile.Read(J, 4); { exe time }
if (i >= FirstBookCompatibleVersion) and (i <= CevoVersion) then
begin
result := true;
LogFile.Read(lx, 4);
LogFile.Read(ly, 4);
MapSize := lx * ly;
LogFile.Read(LandMass, 4);
if LandMass = 0 then
LogFile.Read(RealMap, MapSize * 4); // use predefined map
LogFile.Read(MaxTurn, 4);
LogFile.Read(RND, 4);
LogFile.Read(GTurn, 4);
LogFile.Read(SaveMap, 4);
if SaveMap[0] <> $80 then
LogFile.read(SaveMap[4], ((MapSize - 1) div 4 + 1) * 4 - 4);
for p1 := 0 to nPl - 1 do
begin
LogFile.Read(s[0], 4);
if s[0] = #0 then
PlayersBrain[p1] := nil
else
begin
LogFile.Read(s[4], Byte(s[0]) div 4 * 4);
LogFile.Read(OriginalDataVersion[p1], 4);
LogFile.Read(d, 4); { behavior }
LogFile.Read(Difficulty[p1], 4);
J := Brains.Last;
while Assigned(J) and (AnsiCompareFileName(J.FileName, s) <> 0) do begin
K := Brains.IndexOf(J) - 1;
if K >= 0 then J := Brains[K]
else J := nil;
end;
if not Assigned(J) then
begin // ai not found -- replace by local player
ProcessClientData[p1] := false;
NotifyMessage := s;
Notify(ntAIError);
J := BrainTerm;
end
else
ProcessClientData[p1] := true;
if j.Kind = btNoTerm then
j := BrainSuperVirtual;
// crashed tournament -- load as supervisor
PlayersBrain[p1] := j;
end;
end;
end
else
Result := false;
if Result then begin
CL := TCmdList.Create;
CL.LoadFromFile(LogFile);
end;
FreeAndNil(LogFile);
if not result then
Exit;
Notify(ntStartDone);
if LoadTurn < 0 then
LoadTurn := GTurn;
if MovieMode then
Mode := moMovie
else if LoadTurn = 0 then
Mode := moLoading
else
Mode := moLoading_Fast;
{$IFDEF TEXTLOG}AssignFile(TextLog, SavePath + LogFileName + '.txt');
Rewrite(TextLog); {$ENDIF}
LoadOK := true;
StartGame;
if MovieMode then
begin
bix[0].Client(cShowGame, 0, nil^);
Notify(ntBackOff);
end
else
Notify(ntLoadBegin);
started := false;
StatRequest := false;
MovieStopped := false;
{$IFDEF LOADPERF}QueryPerformanceCounter(time_total0);
time_a := 0;
time_b := 0;
time_c := 0; {$ENDIF}
while not MovieStopped and (CL.Progress < 1000) do
begin
FormerCLState := CL.State;
CL.Get(Command, p1, Subject, Data);
if p1 < 0 then
p1 := pTurn;
if StatRequest and (Command and (sctMask or sExecute) <> sctInternal or
sExecute) then
begin
GenerateStat(pTurn);
StatRequest := false;
end;
// complete all internal commands following an sTurn before generating statistics
if (Command = sTurn) and not started then
begin
{$IFDEF TEXTLOG}WriteLn(TextLog, '---Turn 0 P0---'); {$ENDIF}
for p1 := 0 to nPl - 1 do
if Assigned(bix[p1]) and ((Mode <> moMovie) or (p1 = 0)) then
CallPlayer(cReplay, p1, nil^);
BeforeTurn0;
if MovieMode then
begin
Inform(pTurn);
CallPlayer(cMovieTurn, 0, nil^);
end;
StatRequest := true;
started := true;
end
else if (Command = sTurn) and (pTurn = 0) and (GTurn = LoadTurn) then
begin
assert(CL.State.LoadPos = FormerCLState.LoadPos + 4); // size of sTurn
CL.State := FormerCLState;
CL.Cut;
Break;
end
else if Command = sIntDataChange then
begin
{$IFDEF TEXTLOG}LoadPos0 := CL.State.LoadPos; {$ENDIF}
if ProcessClientData[p1] then
CL.GetDataChanges(RW[p1].Data, bix[p1].DataSize)
else
CL.GetDataChanges(nil, 0);
{$IFDEF TEXTLOG}WriteLn(TextLog, Format('Data Changes P%d (%d Bytes)', [p1, CL.State.LoadPos - LoadPos0])); {$ENDIF}
end
else
begin
{$IFDEF TEXTLOG}CmdInfo := Format('Command %x', [Command]); {$ENDIF}
if Command and (sctMask or sExecute) = sctInternal or sExecute then
IntServer(Command, p1, Subject, Data^) // internal command
else
begin
StatRequest := Command = sTurn;
Server(Command, p1, Subject, Data^);
end;
{$IFDEF TEXTLOG}WriteLn(TextLog, CmdInfo); {$ENDIF}
end;
if not MovieMode then
Notify(ntLoadState, CL.Progress * 128 div 1000);
end;
if MovieMode then
begin
Notify(ntBackOn);
bix[0].Client(cBreakGame, -1, nil^);
EndGame;
Notify(ntStartGo);
result := false;
exit;
end;
if StatRequest then
GenerateStat(pTurn);
assert(started);
{$IFDEF TEXTLOG}CloseFile(TextLog); {$ENDIF}
{$IFDEF LOADPERF}QueryPerformanceCounter(time_total); { time in s is: (time_total-time_total0)/PerfFreq }{$ENDIF}
NoLogChanges;
NoLogCityTileChanges;
if LogFileName[1] = '~' then
begin
Delete(LogFileName, 1, 1);
nLogOpened := -1;
end
else
nLogOpened := CL.State.nLog;
Mode := moPlaying;
LastEndClientCommand := -1;
if (GTestFlags and tfUncover <> 0) or (Difficulty[pTurn] = 0) then
DiscoverAll(pTurn, lObserveSuper) { supervisor - all tiles visible }
else
DiscoverViewAreas(pTurn);
for p1 := 0 to nPl - 1 do
if 1 shl p1 and (GAlive or GWatching) <> 0 then
begin
RecalcPeaceMap(p1);
for ix := 0 to RW[p1].nEnemyUn - 1 do
with RW[p1].EnemyUn[ix] do
emix := RWemix[p1, Owner, mix];
Inform(p1);
end;
{$IFOPT O-}CheckBorders(-2); {$ENDIF} // for testing only
Notify(ntEndInfo);
if not LoadOK then
begin
NotifyMessage := SavePath + LogFileName;
Notify(ntLoadError);
end;
bix[0].Client(cShowGame, 0, nil^);
Notify(ntBackOff);
Inform(pTurn);
ChangeClientWhenDone(cResume, 0, nil^, 0);
end;
procedure InsertTerritoryUpdateCommands;
var
p1, Command, Subject: integer;
Data: pointer;
FormerCLState: TCmdListState;
begin
while CL.Progress < 1000 do
begin
FormerCLState := CL.State;
CL.Get(Command, p1, Subject, Data);
if (Command = sIntExpandTerritory) and (p1 = pTurn) then
begin
IntServer(Command, p1, Subject, Data^);
{$IFDEF TEXTLOG}WriteLn(TextLog, 'AfterTurn - ExpandTerritory'); {$ENDIF}
end
else
begin
CL.State := FormerCLState;
Break;
end;
end;
{$IFOPT O-}InvalidTreatyMap := 0; {$ENDIF}
end;
procedure StartNewGame(const Path, FileName, Map: string;
Newlx, Newly, NewLandMass, NewMaxTurn: integer);
var
I: Integer;
begin
Notify(ntStartDone);
SavePath := Path;
LogFileName := FileName;
MapFileName := Map;
{$IFDEF FastContact}
lx := 24;
ly := 42;
{$ELSE}
lx := Newlx;
ly := Newly;
{$ENDIF}
MapSize := lx * ly;
if MapFileName <> '' then
LandMass := 0
else
LandMass := NewLandMass;
MaxTurn := NewMaxTurn;
DelphiRandomize;
RND := DelphiRandSeed;
Mode := moPlaying;
CL := TCmdList.Create;
StartGame;
NoLogChanges;
CallAllPlayers(cGetReady, nil^);
LogChanges;
CL.Put(sTurn, 0, 0, nil);
BeforeTurn0;
NoLogCityTileChanges;
GenerateStat(pTurn);
nLogOpened := -1;
LastEndClientCommand := -1;
CallPlayer(cShowGame, 0, nil^);
for I := 0 to nPl - 1 do
if Assigned(bix[I]) and (bix[I].Kind = btNetworkServer) then
CallPlayer(cShowGame, I, nil^);
Notify(ntBackOff);
Inform(pTurn);
ChangeClientWhenDone(cTurn, 0, nil^, 0)
end;
procedure DirectHelp(Command: integer);
begin
InitBrain(BrainTerm);
BrainTerm.Client(Command, -1, nil^);
AICredits := #0;
end;
procedure EditMap(const Map: string; Newlx, Newly, NewLandMass: integer);
var
p1, Loc1: integer;
Game: TNewGameData;
begin
Notify(ntStartDone);
Notify(ntInitLocalHuman);
MapFileName := Map;
lx := Newlx;
ly := Newly;
MapSize := lx * ly;
LandMass := NewLandMass;
bix[0] := BrainTerm;
Difficulty[0] := 0;
InitBrain(BrainTerm);
DelphiRandomize;
GAlive := 0;
GWatching := 1;
if not LoadMap(MapFileName) then
for Loc1 := 0 to MapSize - 1 do
RealMap[Loc1] := fOcean or ($F shl 27);
CL := nil;
InitMapEditor;
RW[0].Data := nil;
RW[0].BorderHelper := nil;
RW[0].Alive := 0;
Game.lx := lx;
Game.ly := ly;
Game.RO[0] := @RW[0];
Game.Difficulty[0] := 0;
for p1 := 1 to nPl - 1 do begin
Game.RO[p1] := nil;
Game.Difficulty[p1] := -1;
end;
BrainTerm.Client(cNewMap, -1, Game);
DiscoverAll(0, lObserveSuper);
Notify(ntEndInfo);
bix[0].Client(cShowGame, 0, nil^);
Notify(ntBackOff);
ChangeClientWhenDone(cEditMap, 0, nil^, 0);
end;
procedure DestroySpacePort_TellPlayers(p, pCapturer: integer);
var
cix, i, p1: integer;
ShowShipChange: TShowShipChange;
begin
// stop ship part production
for cix := 0 to RW[p].nCity - 1 do
with RW[p].City[cix] do
if (Loc >= 0) and (Project and cpImp <> 0) and
((Project and cpIndex = woMIR) or
(Imp[Project and cpIndex].Kind = ikShipPart)) then
begin
inc(RW[p].Money, Prod0);
Prod := 0;
Prod0 := 0;
Project := cpImp + imTrGoods;
Project0 := cpImp + imTrGoods;
end;
// destroy ship
with GShip[p] do
if Parts[0] + Parts[1] + Parts[2] > 0 then
begin
for i := 0 to nShipPart - 1 do
begin
ShowShipChange.Ship1Change[i] := -Parts[i];
if pCapturer >= 0 then
begin
ShowShipChange.Ship2Change[i] := Parts[i];
inc(GShip[pCapturer].Parts[i], Parts[i]);
end;
Parts[i] := 0;
end;
if Mode >= moMovie then
begin
if pCapturer >= 0 then
ShowShipChange.Reason := scrCapture
else
ShowShipChange.Reason := scrDestruction;
ShowShipChange.Ship1Owner := p;
ShowShipChange.Ship2Owner := pCapturer;
for p1 := 0 to nPl - 1 do
if 1 shl p1 and (GAlive or GWatching) <> 0 then
begin
move(GShip, RW[p1].Ship, SizeOf(GShip));
if 1 shl p1 and GWatching <> 0 then
CallPlayer(cShowShipChange, p1, ShowShipChange);
end;
end;
end;
end;
procedure DestroyCity_TellPlayers(p, cix: integer; SaveUnits: boolean);
begin
if RW[p].City[cix].built[imSpacePort] > 0 then
DestroySpacePort_TellPlayers(p, -1);
DestroyCity(p, cix, SaveUnits);
end;
procedure ChangeCityOwner_TellPlayers(pOld, cixOld, pNew: integer);
begin
if RW[pOld].City[cixOld].built[imSpacePort] > 0 then
if RW[pNew].NatBuilt[imSpacePort] > 0 then
DestroySpacePort_TellPlayers(pOld, pNew)
else
DestroySpacePort_TellPlayers(pOld, -1);
ChangeCityOwner(pOld, cixOld, pNew);
end;
procedure CheckWin(p: integer);
var
i: integer;
ShipComplete: boolean;
begin
ShipComplete := true;
for i := 0 to nShipPart - 1 do
if GShip[p].Parts[i] < ShipNeed[i] then
ShipComplete := false;
if ShipComplete then
GWinner := GWinner or 1 shl p; // game won!
end;
procedure BeforeTurn;
var
i, p1, uix, cix, V21, Loc1, Cost, Job0, nAlive, nAppliers, ad, OldLoc,
SiegedTiles, nUpdateLoc: integer;
UpdateLoc: array [0 .. numax - 1] of integer;
Radius: TVicinity21Loc;
ShowShipChange: TShowShipChange;
TribeExtinct, JobDone, MirBuilt: boolean;
begin
{$IFOPT O-}assert(1 shl pTurn and InvalidTreatyMap = 0); {$ENDIF}
assert(1 shl pTurn and (GAlive or GWatching) <> 0);
if (1 shl pTurn and GAlive = 0) and (Difficulty[pTurn] > 0) then
exit;
for I := 0 to numax - 1 do
UpdateLoc[I] := -1 ; // Initalise to invalid value;
if (GWonder[woGrLibrary].EffectiveOwner = pTurn) and (GWinner = 0) then
begin // check great library effect
nAlive := 0;
for p1 := 0 to nPl - 1 do
if 1 shl p1 and GAlive <> 0 then
inc(nAlive);
ad := 0;
while ad <= (nAdv - 5) do begin
if RW[pTurn].Tech[ad] < tsSeen then
begin
nAppliers := 0;
for p1 := 0 to nPl - 1 do
if (p1 <> pTurn) and (1 shl p1 and GAlive <> 0) and
(RW[p1].Tech[ad] >= tsApplicable) then
inc(nAppliers);
if nAppliers * 2 > nAlive then
begin
SeeTech(pTurn, ad);
inc(nTech[pTurn]);
if Mode >= moMovie then
CallPlayer(cShowGreatLibTech, pTurn, ad);
// do not call CallPlayer(pTurn) while map is invalid
end;
end;
Inc(ad);
end;
end;
MaskD(ObserveLevel, MapSize, not Cardinal(3 shl (2 * pTurn)));
if Mode > moLoading_Fast then
MaskD(RW[pTurn].Map^, MapSize, Cardinal(not Cardinal(fUnit or fHiddenUnit or
fStealthUnit or fObserved or fSpiedOut or fOwned or fOwnZoCUnit or
fInEnemyZoC)));
RW[pTurn].nEnemyUn := 0;
MirBuilt := false;
if (Difficulty[pTurn] > 0) and (GWinner = 0) then
with RW[pTurn] do
begin
if nCity > 0 then
for p1 := 0 to nPl - 1 do
if GTurn = EvaStart[p1] + PeaceEvaTurns then
begin // peace contract -- remove all units from p1's territory
Loc1 := City[0].Loc; // search destination for homeless units
for cix := 1 to nCity - 1 do
if (City[cix].Loc >= 0) and
((Loc1 < 0) or (City[cix].built[imPalace] > 0)) then
Loc1 := City[cix].Loc;
for uix := 0 to nUn - 1 do
with Un[uix] do
if (Loc >= 0) and (Model[mix].Kind <> mkDiplomat) and
((Home >= 0) or (Loc1 >= 0)) and
(RealMap[Loc] shr 27 = Cardinal(p1)) then
begin
OldLoc := Loc;
if Master >= 0 then
begin // transport unload
if Model[mix].Domain = dAir then
dec(Un[Master].AirLoad)
else
dec(Un[Master].TroopLoad);
Master := -1;
end
else
FreeUnit(pTurn, uix);
if Home >= 0 then
Loc := City[Home].Loc
else
Loc := Loc1;
PlaceUnit(pTurn, uix);
UpdateUnitMap(OldLoc);
UpdateUnitMap(Loc);
Flags := Flags or unWithdrawn;
Happened := Happened or phPeaceEvacuation;
end;
end;
if Mode >= moMovie then
FillChar(ShowShipChange, SizeOf(ShowShipChange), 0);
TribeExtinct := true;
nUpdateLoc := 0;
for cix := 0 to nCity - 1 do
with City[cix] do
if Loc >= 0 then
begin { next turn for all cities - city loop 1 }
// if ServerVersion[pTurn]>=$000EF0 then
// Flags:=Flags and (chFounded or chCaptured or chProductionSabotaged or chDisorder)
// else Flags:=Flags and (chCaptured or chProductionSabotaged or chDisorder);
// check for siege
SiegedTiles := 0;
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));
p1 := RealMap[Loc1] shr 27;
if (RealMap[Loc1] and fCity <> 0) or (p1 < nPl) and
(p1 <> pTurn) and (RW[pTurn].Treaty[p1] >= trPeace) or
(ZoCMap[Loc1] > 0) and (Occupant[Loc1] <> pTurn) and
(Treaty[Occupant[Loc1]] < trPeace) then
begin
Tiles := Tiles and not(1 shl V21);
UsedByCity[Loc1] := -1;
Flags := Flags or chSiege;
inc(SiegedTiles);
end;
end;
while SiegedTiles > 0 do // replace sieged tiles
begin
if not AddBestCityTile(pTurn, cix) then
Break;
dec(SiegedTiles);
end;
if Flags and chFounded = 0 then
begin
// CollectCityResources(pTurn,cix); // old style
if CityTurn(pTurn, cix) then
TribeExtinct := false
else
begin // city is erased
RemoveDomainUnits(dSea, pTurn, Loc);
RemoveDomainUnits(dAir, pTurn, Loc);
Map[Loc] := Map[Loc] and not fCity; // !!! do this in inner core
UpdateLoc[nUpdateLoc] := Loc;
inc(nUpdateLoc);
DestroyCity_TellPlayers(pTurn, cix, true);
end;
if (Flags and chProduction <> 0) and (Project0 and cpImp <> 0)
then
begin
if Project0 and cpIndex = woMIR then // MIR completed
MirBuilt := true
else if Project0 and cpIndex = woManhattan then
GColdWarStart := GTurn
else if Imp[Project0 and cpIndex].Kind = ikShipPart
then { ship parts produced }
inc(ShowShipChange.Ship1Change[Project0 and cpIndex -
imShipComp]);
end;
end;
end; { city loop 1 }
if nUpdateLoc > 0 then
begin
CheckBorders(-1, pTurn);
for i := 0 to nUpdateLoc - 1 do
UpdateUnitMap(UpdateLoc[i], true);
if Mode >= moMovie then
for p1 := 0 to nPl - 1 do
if (1 shl p1 and GWatching <> 0) and (p1 <> pTurn) then
for i := 0 to nUpdateLoc - 1 do
if ObserveLevel[UpdateLoc[i]] shr (2 * p1) and 3 >= lObserveUnhidden
then
CallPlayer(cShowCityChanged, p1, UpdateLoc[i]);
end;
for uix := 0 to nUn - 1 do
with Un[uix] do
if Loc >= 0 then
begin // unit loop 2
if Health < 100 then
Recover(pTurn, uix);
if Flags and unMountainDelay <> 0 then
begin
Movement := 0;
Flags := Flags and not unMountainDelay;
end
else
Movement := UnitSpeed(pTurn, mix, Health); { refresh movement }
assert(Loc >= 0);
if Model[mix].Kind <> mkDiplomat then
begin // check treaty violation
p1 := RealMap[Loc] shr 27;
if (p1 < nPl) and (p1 <> pTurn) and (Treaty[p1] >= trPeace) then
begin
if (Job in [jCity, jPillage, jClear, jAfforest, jTrans]) or
(Job in [jIrr, jMine, jFort, jBase]) and
(RealMap[Loc] and fTerImp <> 0) then
Job := jNone;
if (GTurn > EvaStart[p1] + PeaceEvaTurns) and
(Treaty[p1] <> trAlliance) then
begin
EvaStart[p1] := GTurn;
Happened := Happened or phPeaceViolation;
if Mode >= moMovie then
CallPlayer(cShowPeaceViolation, p1, pTurn);
end;
end;
end;
if ServerVersion[pTurn] >= $000EF0 then
begin
if (Health <= 0) or TribeExtinct then
RemoveUnit_UpdateMap(pTurn, uix);
end;
end;
if ServerVersion[pTurn] < $000EF0 then
for uix := 0 to nUn - 1 do
with Un[uix] do
if Loc >= 0 then
begin // unit loop 3
Loc1 := Loc;
Job0 := Job;
if Job <> jNone then
JobDone := Work(pTurn, uix)
else
JobDone := False;
{ settlers do terrain improvement jobs }
if (Health <= 0) or TribeExtinct then
RemoveUnit_UpdateMap(pTurn, uix);
if (Job0 = jCity) and JobDone then // new city
begin
AddBestCityTile(pTurn, RW[pTurn].nCity - 1);
UpdateUnitMap(Loc1, true);
if Mode >= moMovie then // tell enemies
for p1 := 0 to nPl - 1 do
if (1 shl p1 and GWatching <> 0) and (p1 <> pTurn) and
(ObserveLevel[Loc1] and (3 shl (2 * p1)) > 0) then
CallPlayer(cShowCityChanged, p1, Loc1);
end;
end;
{ pollution - city loop 3 }
for cix := 0 to nCity - 1 do
with City[cix] do
if (Loc >= 0) and (Pollution >= MaxPollution) then
Pollute(pTurn, cix);
CompactLists(pTurn);
if (nUn = 0) and (nCity = 0) then
begin // nation made extinct
Happened := Happened or phExtinct;
GAlive := GAlive and not(1 shl pTurn);
Stat[stPop, pTurn, GTurn] := 0;
Stat[stMil, pTurn, GTurn] := 0;
Stat[stScience, pTurn, GTurn] := 0;
Stat[stExplore, pTurn, GTurn] := 0;
Stat[stTerritory, pTurn, GTurn] := 0;
Stat[stWork, pTurn, GTurn] := 0;
for p1 := 0 to nPl - 1 do
if 1 shl p1 and (GAlive or GWatching) <> 0 then
begin
if p1 <> pTurn then
begin
GiveCivilReport(p1, pTurn);
if (GTestFlags and tfUncover <> 0) or (Difficulty[p1] = 0) or
(RW[p1].Treaty[pTurn] = trAlliance) then
GiveMilReport(p1, pTurn);
end;
with RW[p1] do
begin
Alive := GAlive;
for Loc1 := 0 to MapSize - 1 do
if Territory[Loc1] = pTurn then
// remove territory of extinct nation from player maps
begin
Territory[Loc1] := -1;
Map[Loc1] := Map[Loc1] and not fPeace;
end;
end;
end;
Exit;
end;
// check research
Cost := TechCost(pTurn);
if GTestFlags and tfImmAdvance <> 0 then
Research := Cost;
if (Happened and phTech = 0) and (Research >= Cost) then
begin
if ResearchTech = adMilitary then
EnableDevModel(pTurn) { new Unit class initiated }
else if ResearchTech >= 0 then
DiscoverTech(pTurn, ResearchTech);
dec(Research, Cost);
Happened := Happened or phTech;
ResearchTech := -1;
end
else if (ResearchTech = -2) and (nCity > 0) then
begin
Happened := Happened or phTech;
ResearchTech := -1;
end;
if Credibility < MaxCredibility then
for p1 := 0 to nPl - 1 do
if (p1 <> pTurn) and (1 shl p1 and GAlive <> 0) and
(Treaty[p1] >= trPeace) then
begin
inc(Credibility);
Break;
end;
if GWinner = 0 then
CheckWin(pTurn);
if (Mode >= moMovie) and (GWinner = 0) and
((ShowShipChange.Ship1Change[0] > 0) or
(ShowShipChange.Ship1Change[1] > 0) or
(ShowShipChange.Ship1Change[2] > 0)) then
begin
ShowShipChange.Reason := scrProduction;
ShowShipChange.Ship1Owner := pTurn;
ShowShipChange.Ship2Owner := -1;
for p1 := 0 to nPl - 1 do
if (p1 <> pTurn) and (1 shl p1 and (GAlive or GWatching) <> 0) then
begin
move(GShip, RW[p1].Ship, SizeOf(GShip));
if 1 shl p1 and GWatching <> 0 then
CallPlayer(cShowShipChange, p1, ShowShipChange);
end;
end;
if WinOnAlone and (GAlive and not(1 shl pTurn or 1) = 0) then
GWinner := 1 shl pTurn; // break if only one nation left
if GTurn = AnarchyStart + AnarchyTurns then
begin
AnarchyStart := -AnarchyTurns - 1;
Government := gDespotism;
for p1 := 0 to nPl - 1 do
if (p1 <> pTurn) and ((GAlive or GWatching) and (1 shl p1) <> 0) then
RW[p1].EnemyReport[pTurn].Government := gDespotism;
inc(Happened, phChangeGov);
end;
end; // if Difficulty[pTurn]>0
if (pTurn = 0) and (GWinner > 0) then
begin // game over, give world map and all reports to player 0
DiscoverAll(pTurn, lObserveSuper);
for p1 := 1 to nPl - 1 do
if 1 shl p1 and GAlive <> 0 then
begin
if RW[pTurn].Treaty[p1] < trNone then
begin
RW[pTurn].Treaty[p1] := trNone;
RW[p1].Treaty[pTurn] := trNone;
end;
GiveCivilReport(pTurn, p1);
GiveMilReport(pTurn, p1);
end;
end
else
begin
// show observed areas
if (GTestFlags and tfUncover <> 0) or (Difficulty[pTurn] = 0)
then { supervisor - all tiles visible }
begin
if (bix[pTurn].Kind <> btNoTerm) and
((Difficulty[pTurn] > 0) or (Mode > moLoading_Fast)) then
DiscoverAll(pTurn, lObserveSuper);
end
else
begin
DiscoverViewAreas(pTurn);
if MirBuilt then
DiscoverAll(pTurn, lObserveUnhidden);
end;
end;
// CheckContact;
end;
procedure AfterTurn;
var
cix, uix, p1, Loc1, Job0: integer;
JobDone: boolean;
begin
with RW[pTurn] do
begin
for cix := 0 to nCity - 1 do
if City[cix].Loc >= 0 then
begin
// City[cix].Flags:=City[cix].Flags and not chProductionSabotaged;
City[cix].Flags := City[cix].Flags and (chCaptured or chDisorder);
CollectCityResources(pTurn, cix); // new style
end;
inc(Money, OracleIncome);
OracleIncome := 0;
if GWonder[woOracle].EffectiveOwner = pTurn then
begin
for p1 := 0 to nPl - 1 do
if (1 shl p1 and GAlive <> 0) and
((p1 = pTurn) or (RW[pTurn].Treaty[p1] > trNoContact)) then
for cix := 0 to RW[p1].nCity - 1 do
if (RW[p1].City[cix].Loc >= 0) and
(RW[p1].City[cix].built[imTemple] > 0) then
inc(OracleIncome);
end;
if (GTestFlags and tfImmImprove = 0) and (Government <> gAnarchy) then
for cix := 0 to nCity - 1 do
if (City[cix].Loc >= 0) and (City[cix].Flags and chCaptured = 0) then
PayCityMaintenance(pTurn, cix);
if ServerVersion[pTurn] >= $000EF0 then
begin // let settlers work
for cix := 0 to nCity - 1 do
City[cix].Flags := City[cix].Flags and not chFounded;
for uix := 0 to nUn - 1 do
with Un[uix] do
if Loc >= 0 then
begin
Loc1 := Loc;
Job0 := Job;
if Job <> jNone then
JobDone := Work(pTurn, uix)
else
JobDone := False;
{ settlers do terrain improvement jobs }
if Health <= 0 then
RemoveUnit_UpdateMap(pTurn, uix);
if (Job0 = jCity) and JobDone then // new city
begin
AddBestCityTile(pTurn, RW[pTurn].nCity - 1);
UpdateUnitMap(Loc1, true);
if Mode >= moMovie then // tell enemies
for p1 := 0 to nPl - 1 do
if (1 shl p1 and GWatching <> 0) and (p1 <> pTurn) and
(ObserveLevel[Loc1] and (3 shl (2 * p1)) > 0) then
CallPlayer(cShowCityChanged, p1, Loc1);
end;
end;
end;
for uix := 0 to nUn - 1 do
with Un[uix] do
if Loc >= 0 then
begin { next turn for all units }
if Model[mix].Domain = dAir then
if (Master >= 0) or (RealMap[Loc] and fCity <> 0) or
(RealMap[Loc] and fTerImp = tiBase) then
begin
Fuel := Model[mix].Cap[mcFuel];
Flags := Flags or unBombsLoaded;
end
else if Model[mix].Kind = mkSpecial_Glider then { glider }
begin
if RealMap[Loc] and fTerrain < fGrass then
begin
RemoveUnit_UpdateMap(pTurn, uix); // unit lost
Happened := Happened or phGliderLost;
end;
end
else
begin
dec(Fuel);
if Fuel < 0 then
begin
RemoveUnit_UpdateMap(pTurn, uix); // unit lost
Happened := Happened or phPlaneLost;
end
end
else if (Master < 0) and (Movement > 0) then // check HostileDamage
begin
Health := Health - HostileDamage(pTurn, mix, Loc, Movement);
if Health < 0 then
RemoveUnit_UpdateMap(pTurn, uix);
end;
end; { unit loop 1 }
for uix := 0 to nUn - 1 do
with Un[uix] do
begin
Flags := Flags and not unWithdrawn;
if (Loc >= 0) and (Model[mix].Domain = dGround) and (Master < 0) and
((integer(Movement) = Model[mix].Speed) or
(Model[mix].Cap[mcAcademy] > 0) and (Movement * 2 >= Model[mix].Speed))
then
Flags := Flags or unFortified; // fortify unmoved units
end;
if (GTestFlags and tfUncover = 0) and (Difficulty[pTurn] > 0) then
begin // restrict view area to current positions
MaskD(ObserveLevel, MapSize, not Cardinal(3 shl (2 * pTurn)));
if Mode > moLoading_Fast then
MaskD(RW[pTurn].Map^, MapSize, Cardinal(not Cardinal(fUnit or fHiddenUnit or
fStealthUnit or fObserved or fSpiedOut or fOwned or fOwnZoCUnit or
fInEnemyZoC)));
RW[pTurn].nEnemyUn := 0;
DiscoverViewAreas(pTurn);
end;
if GWinner = 0 then
for p1 := 0 to nPl - 1 do
if 1 shl p1 and GAlive <> 0 then
CheckWin(p1);
end;
end;
procedure NextPlayer;
begin
if GTurn = 0 then
BeforeTurn0
else
BeforeTurn;
NoLogCityTileChanges;
GenerateStat(pTurn);
Inform(pTurn);
ChangeClient;
end;
function ExecuteMove(p, uix, ToLoc: integer; var MoveInfo: TMoveInfo;
ShowMove: TShowMove): integer;
var
i, p1, FromLoc, uix1, nUpdateLoc: integer;
MinLevel, MissionResult: Cardinal;
PModel: ^TModel;
UpdateLoc: array [0 .. numax - 1] of integer;
SeeFrom, SeeTo, ExtDiscover: boolean;
begin
for I := 0 to numax - 1 do
UpdateLoc[I] := -1 ; // Initalise to invalid value;
result := 0;
with RW[p], Un[uix] do
begin
PModel := @Model[mix];
FromLoc := Loc;
if Master < 0 then
FreeUnit(p, uix);
if (MoveInfo.MoveType in [mtMove, mtCapture]) and MoveInfo.MountainDelay
then
begin
Flags := Flags or unMountainDelay;
end;
Loc := -2;
if TroopLoad + AirLoad > 0 then
for i := 0 to nUn - 1 do
if (Un[i].Loc >= 0) and (Un[i].Master = uix) then
Un[i].Loc := -2;
UpdateUnitMap(FromLoc);
if Mode >= moMovie then { show move in interface modules }
begin
ShowMove.EndHealth := MoveInfo.EndHealth;
ShowMove.EndHealthDef := -1;
if Master >= 0 then
if Model[Un[Master].mix].Domain = dAir then
ShowMove.Flags := ShowMove.Flags or umPlaneUnloading
else
ShowMove.Flags := ShowMove.Flags or umShipUnloading;
if MoveInfo.ToMaster >= 0 then
if Model[Un[MoveInfo.ToMaster].mix].Domain = dAir then
ShowMove.Flags := ShowMove.Flags or umPlaneLoading
else
ShowMove.Flags := ShowMove.Flags or umShipLoading;
for p1 := 0 to nPl - 1 do
if (1 shl p1 and GWatching <> 0) and ((p1 <> p) or (bix[p1].Kind = btTerm))
then
begin
if PModel.Cap[mcStealth] > 0 then
MinLevel := lObserveSuper
else if PModel.Cap[mcSub] > 0 then
MinLevel := lObserveAll
else
MinLevel := lObserveUnhidden;
SeeFrom := (p1 = p) or (ObserveLevel[FromLoc] shr (2 * p1) and
3 >= MinLevel);
SeeTo := (p1 = p) or (ObserveLevel[ToLoc] shr (2 * p1) and
3 >= MinLevel);
if SeeFrom and SeeTo then
begin
TellAboutModel(p1, p, mix);
if p1 = p then
ShowMove.emix := -1
else
ShowMove.emix := emixSafe(p1, p, mix);
if MoveInfo.MoveType = mtCapture then
CallPlayer(cShowCapturing, p1, ShowMove)
else
CallPlayer(cShowMoving, p1, ShowMove);
end
else if SeeFrom then
CallPlayer(cShowUnitChanged, p1, FromLoc);
end;
end;
if MoveInfo.MoveType <> mtSpyMission then
Loc := ToLoc;
if TroopLoad + AirLoad > 0 then
for i := 0 to nUn - 1 do
if Un[i].Loc = -2 then
Un[i].Loc := ToLoc;
ExtDiscover := false;
nUpdateLoc := 0;
if MoveInfo.MoveType = mtCapture then
begin
assert(Occupant[ToLoc] < 0);
for uix1 := 0 to RW[MoveInfo.Defender].nUn - 1 do
with RW[MoveInfo.Defender].Un[uix1] do
if (Loc >= 0) and (Home = MoveInfo.Dcix) then
begin
UpdateLoc[nUpdateLoc] := Loc;
inc(nUpdateLoc);
end;
// unit will be removed -- remember position and update for all players
if (RW[MoveInfo.Defender].City[MoveInfo.Dcix].Size > 2) and (nCity < ncmax)
then
begin // city captured
ChangeCityOwner_TellPlayers(MoveInfo.Defender, MoveInfo.Dcix, p);
City[nCity - 1].Flags := CaptureTurns shl 16;
CityShrink(p, nCity - 1);
if Mode = moPlaying then
with RW[p].City[nCity - 1] do
begin
// SavedResourceWeights[nCity-1]:=ResourceWeights;
SavedTiles[nCity - 1] := Tiles;
end;
ExtDiscover := true;
// Temple of Zeus effect
if GWonder[woZeus].EffectiveOwner = p then
begin
GiveCivilReport(p, MoveInfo.Defender);
for i := 0 to nAdv - 1 do
if not(i in FutureTech) and (RW[p].Tech[i] < tsSeen) and
(RW[MoveInfo.Defender].Tech[i] >= tsApplicable) then
begin
Happened := Happened or phStealTech;
GStealFrom := MoveInfo.Defender;
Break;
end;
end;
if Mode = moPlaying then
LogCheckBorders(p, nCity - 1, MoveInfo.Defender);
{$IFOPT O-} if Mode < moPlaying then
InvalidTreatyMap := not(1 shl p); {$ENDIF}
// territory should not be considered for the rest of the command
// execution, because during loading a game it's incorrect before
// subsequent sIntExpandTerritory is processed
end
else // city destroyed
begin
DestroyCity_TellPlayers(MoveInfo.Defender, MoveInfo.Dcix, false);
CheckBorders(ToLoc, MoveInfo.Defender);
end;
RecalcPeaceMap(p);
if Mode >= moMovie then
move(GWonder, Wonder, SizeOf(GWonder));
end; { if MoveInfo.MoveType=mtCapture }
if MoveInfo.MoveType = mtSpyMission then
begin
MissionResult := DoSpyMission(p, MoveInfo.Defender, MoveInfo.Dcix,
SpyMission);
if (Mode = moPlaying) and (SpyMission = smStealForeignReports) then
CallPlayer(cShowMissionResult, p, MissionResult);
end;
Health := MoveInfo.EndHealth;
dec(Movement, MoveInfo.Cost);
// transport unload
if Master >= 0 then
begin
if PModel.Domain = dAir then
dec(Un[Master].AirLoad)
else
begin
dec(Un[Master].TroopLoad);
assert(Movement <= 0);
end;
Master := -1;
end;
if (Health <= 0) or (MoveInfo.MoveType = mtSpyMission) then
RemoveUnit(p, uix) // spy mission or victim of HostileDamage
else
begin // transport load
Master := MoveInfo.ToMaster;
if MoveInfo.ToMaster >= 0 then
begin
if PModel.Domain = dAir then
inc(Un[MoveInfo.ToMaster].AirLoad)
else
inc(Un[MoveInfo.ToMaster].TroopLoad);
end
else
PlaceUnit(p, uix);
end;
if (MoveInfo.MoveType = mtCapture) and (nUpdateLoc > 0) then
RecalcMapZoC(p);
UpdateUnitMap(ToLoc, MoveInfo.MoveType = mtCapture);
for i := 0 to nUpdateLoc - 1 do
UpdateUnitMap(UpdateLoc[i]);
// tell about lost units of defender
if (MoveInfo.MoveType <> mtSpyMission) and (Master < 0) then
begin
if (PModel.Kind = mkDiplomat) or (PModel.Domain = dAir) or
(PModel.Cap[mcRadar] + PModel.Cap[mcAcademy] >
0) or (RealMap[ToLoc] and fTerrain = fMountains) or
(RealMap[ToLoc] and fTerImp = tiFort) or
(RealMap[ToLoc] and fTerImp = tiBase) then
ExtDiscover := true;
if (PModel.Kind = mkDiplomat) or (PModel.Cap[mcSpy] > 0) then
i := lObserveSuper
else if (PModel.Domain = dAir) or
(PModel.Cap[mcRadar] > 0) then
i := lObserveAll
else
i := lObserveUnhidden;
if ExtDiscover then
begin
if Discover21(ToLoc, p, i, true, PModel.Domain = dGround) then
result := result or rEnemySpotted;
end
else
begin
if Discover9(ToLoc, p, i, true, PModel.Domain = dGround) then
result := result or rEnemySpotted;
end;
end;
if Mode >= moMovie then { show after-move in interface modules }
for p1 := 0 to nPl - 1 do
if (1 shl p1 and GWatching <> 0) and ((p1 <> p) or (bix[p1].Kind = btTerm))
then
begin
if PModel.Cap[mcStealth] > 0 then
MinLevel := lObserveSuper
else if PModel.Cap[mcSub] > 0 then
MinLevel := lObserveAll
else
MinLevel := lObserveUnhidden;
SeeFrom := (p1 = p) or (ObserveLevel[FromLoc] shr (2 * p1) and
3 >= MinLevel);
SeeTo := (p1 = p) or (ObserveLevel[ToLoc] shr (2 * p1) and
3 >= MinLevel);
if SeeTo and (MoveInfo.MoveType = mtCapture) then
CallPlayer(cShowCityChanged, p1, ToLoc);
if SeeFrom and SeeTo then
CallPlayer(cShowAfterMove, p1, ToLoc)
else if (MoveInfo.MoveType <> mtSpyMission) and SeeTo then
CallPlayer(cShowUnitChanged, p1, ToLoc);
for i := 0 to nUpdateLoc - 1 do
if ObserveLevel[UpdateLoc[i]] shr (2 * p1) and 3 >= lObserveUnhidden
then
CallPlayer(cShowUnitChanged, p1, UpdateLoc[i]);
end;
end;
end;
function ExecuteAttack(p, uix, ToLoc: integer; var MoveInfo: TMoveInfo;
ShowMove: TShowMove): integer;
procedure WriteBattleHistory(ToLoc, FromLoc, Attacker, Defender, mixAttacker,
mixDefender: integer; AttackerLost, DefenderLost: boolean);
var
AttackerBattle, DefenderBattle: ^TBattle;
begin
with RW[Attacker] do
begin
if nBattleHistory = 0 then
ReallocMem(BattleHistory, 16 * SizeOf(TBattle))
else if (nBattleHistory >= 16) and
(nBattleHistory and (nBattleHistory - 1) = 0) then
ReallocMem(BattleHistory, nBattleHistory * (2 * SizeOf(TBattle)));
AttackerBattle := @BattleHistory[nBattleHistory];
inc(nBattleHistory);
end;
with RW[Defender] do
begin
if nBattleHistory = 0 then
ReallocMem(BattleHistory, 16 * SizeOf(TBattle))
else if (nBattleHistory >= 16) and
(nBattleHistory and (nBattleHistory - 1) = 0) then
ReallocMem(BattleHistory, nBattleHistory * (2 * SizeOf(TBattle)));
DefenderBattle := @BattleHistory[nBattleHistory];
inc(nBattleHistory);
end;
AttackerBattle.Enemy := Defender;
AttackerBattle.Flags := 0;
AttackerBattle.Turn := GTurn;
AttackerBattle.mix := mixAttacker;
AttackerBattle.mixEnemy := mixDefender;
AttackerBattle.ToLoc := ToLoc;
AttackerBattle.FromLoc := FromLoc;
DefenderBattle.Enemy := Attacker;
DefenderBattle.Flags := bhEnemyAttack;
DefenderBattle.Turn := GTurn;
DefenderBattle.mix := mixDefender;
DefenderBattle.mixEnemy := mixAttacker;
DefenderBattle.ToLoc := ToLoc;
DefenderBattle.FromLoc := FromLoc;
if AttackerLost then
begin
AttackerBattle.Flags := AttackerBattle.Flags or bhMyUnitLost;
DefenderBattle.Flags := DefenderBattle.Flags or bhEnemyUnitLost;
end;
if DefenderLost then
begin
AttackerBattle.Flags := AttackerBattle.Flags or bhEnemyUnitLost;
DefenderBattle.Flags := DefenderBattle.Flags or bhMyUnitLost;
end;
end;
var
i, p1, FromLoc, uix1, nUpdateLoc, ExpGain, ExpelToLoc, cix1: integer;
PModel: ^TModel;
UpdateLoc: array [0 .. numax - 1] of integer;
LoseCityPop, CityDestroyed, SeeFrom, SeeTo, ZoCDefenderDestroyed: boolean;
begin
result := 0;
for I := 0 to numax - 1 do
UpdateLoc[I] := -1 ; // Initalise to invalid value;
with RW[p].Un[uix] do
begin
PModel := @RW[p].Model[mix];
FromLoc := Loc;
ShowMove.EndHealth := MoveInfo.EndHealth;
ShowMove.EndHealthDef := MoveInfo.EndHealthDef;
if MoveInfo.MoveType = mtAttack then
WriteBattleHistory(ToLoc, FromLoc, p, MoveInfo.Defender, mix,
RW[MoveInfo.Defender].Un[MoveInfo.Duix].mix, MoveInfo.EndHealth <= 0,
MoveInfo.EndHealthDef <= 0);
{ if RW[p].Treaty[MoveInfo.Defender]=trCeaseFire then
begin
if Mode>=moMovie then
CallPlayer(cShowCancelTreaty,MoveInfo.Defender,p);
CancelTreaty(p,MoveInfo.Defender)
end; }
if Mode >= moMovie then { show attack in interface modules }
for p1 := 0 to nPl - 1 do
if (1 shl p1 and GWatching <> 0) and ((p1 <> p) or (bix[p1].Kind = btTerm))
then
begin
SeeFrom := ObserveLevel[FromLoc] shr (2 * p1) and
3 >= lObserveUnhidden;
SeeTo := ObserveLevel[ToLoc] shr (2 * p1) and 3 >= lObserveUnhidden;
if SeeFrom and SeeTo then
begin
TellAboutModel(p1, p, mix);
if p1 = p then
ShowMove.emix := -1
else
ShowMove.emix := emixSafe(p1, p, mix);
CallPlayer(cShowAttacking, p1, ShowMove);
end;
end;
LoseCityPop := false;
if (RealMap[ToLoc] and fCity <> 0) and
((MoveInfo.MoveType = mtAttack) and (MoveInfo.EndHealthDef <= 0) or
(MoveInfo.MoveType = mtBombard) and (BombardmentDestroysCity or
(RW[MoveInfo.Defender].City[MoveInfo.Dcix].Size > 2))) then
case PModel.Domain of
dGround:
LoseCityPop := (PModel.Cap[mcArtillery] > 0) or
(RW[MoveInfo.Defender].City[MoveInfo.Dcix].built[imWalls] = 0) and
(Continent[ToLoc] <> GrWallContinent[MoveInfo.Defender]);
dSea:
LoseCityPop := RW[MoveInfo.Defender].City[MoveInfo.Dcix].built
[imCoastalFort] = 0;
dAir:
LoseCityPop := RW[MoveInfo.Defender].City[MoveInfo.Dcix].built
[imMissileBat] = 0;
end;
CityDestroyed := LoseCityPop and
(RW[MoveInfo.Defender].City[MoveInfo.Dcix].Size <= 2);
if MoveInfo.MoveType = mtBombard then
begin
assert(Movement >= 100);
if PModel.Attack = 0 then
Flags := Flags and not unBombsLoaded;
dec(Movement, 100);
end
else if MoveInfo.MoveType = mtExpel then
begin
assert(Movement >= 100);
Job := jNone;
Flags := Flags and not unFortified;
dec(Movement, 100);
end
else
begin
assert(MoveInfo.MoveType = mtAttack);
if MoveInfo.EndHealth = 0 then
RemoveUnit(p, uix, MoveInfo.Defender) // destroy attacker
else
begin // update attacker
ExpGain := (Health - MoveInfo.EndHealth + 1) shr 1;
if Exp + ExpGain > (nExp - 1) * ExpCost then
Exp := (nExp - 1) * ExpCost
else
inc(Exp, ExpGain);
Health := MoveInfo.EndHealth;
Job := jNone;
if RW[MoveInfo.Defender].Model[RW[MoveInfo.Defender].Un[MoveInfo.Duix]
.mix].Domain < dAir then
Flags := Flags and not unBombsLoaded;
Flags := Flags and not unFortified;
if Movement > 100 then
dec(Movement, 100)
else
Movement := 0;
end;
end;
ZoCDefenderDestroyed := false;
nUpdateLoc := 0;
ExpelToLoc := -1;
if MoveInfo.MoveType = mtExpel then
with RW[MoveInfo.Defender], Un[MoveInfo.Duix] do
begin // expel friendly unit
if Home >= 0 then
ExpelToLoc := City[Home].Loc
else
begin
ExpelToLoc := City[0].Loc; // search destination for homeless units
for cix1 := 1 to nCity - 1 do
if (City[cix1].Loc >= 0) and
((ExpelToLoc < 0) or (City[cix1].built[imPalace] > 0)) then
ExpelToLoc := City[cix1].Loc;
end;
if ExpelToLoc >= 0 then
begin
FreeUnit(MoveInfo.Defender, MoveInfo.Duix);
Loc := ExpelToLoc;
PlaceUnit(MoveInfo.Defender, MoveInfo.Duix);
UpdateLoc[nUpdateLoc] := Loc;
inc(nUpdateLoc);
Flags := Flags or unWithdrawn;
end;
end
else if (MoveInfo.MoveType = mtAttack) and (MoveInfo.EndHealthDef > 0) then
with RW[MoveInfo.Defender].Un[MoveInfo.Duix] do
begin // update defender
ExpGain := (Health - MoveInfo.EndHealthDef + 1) shr 1;
if Exp + ExpGain > (nExp - 1) * ExpCost then
Exp := (nExp - 1) * ExpCost
else
inc(Exp, ExpGain);
Health := MoveInfo.EndHealthDef;
end
else
begin // destroy defenders
if MoveInfo.MoveType <> mtBombard then
begin
ZoCDefenderDestroyed := RW[MoveInfo.Defender].Model
[RW[MoveInfo.Defender].Un[MoveInfo.Duix].mix].Flags and mdZOC <> 0;
if ((RealMap[ToLoc] and fCity = 0) and
(RealMap[ToLoc] and fTerImp <> tiBase) and
(RealMap[ToLoc] and fTerImp <> tiFort)) or LoseCityPop and
(RW[MoveInfo.Defender].City[MoveInfo.Dcix].Size = 2) then
RemoveAllUnits(MoveInfo.Defender, ToLoc, p)
{ no city, base or fortress }
else
RemoveUnit(MoveInfo.Defender, MoveInfo.Duix, p);
end;
if LoseCityPop then // city defender defeated -- shrink city
if not CityDestroyed then
CityShrink(MoveInfo.Defender, MoveInfo.Dcix)
else
begin
for uix1 := 0 to RW[MoveInfo.Defender].nUn - 1 do
with RW[MoveInfo.Defender].Un[uix1] do
if (Loc >= 0) and (Home = MoveInfo.Dcix) then
begin
UpdateLoc[nUpdateLoc] := Loc;
inc(nUpdateLoc);
end;
// unit will be removed -- remember position and update for all players
DestroyCity_TellPlayers(MoveInfo.Defender, MoveInfo.Dcix, false);
CheckBorders(ToLoc, MoveInfo.Defender);
RecalcPeaceMap(p);
end;
end;
if CityDestroyed and (nUpdateLoc > 0) then
RecalcMapZoC(p)
else if ZoCDefenderDestroyed then
RecalcV8ZoC(p, ToLoc);
UpdateUnitMap(FromLoc);
UpdateUnitMap(ToLoc, LoseCityPop);
for i := 0 to nUpdateLoc - 1 do
UpdateUnitMap(UpdateLoc[i]);
// tell about lost units of defender
if Mode >= moMovie then
begin
for i := 0 to RW[p].nEnemyModel - 1 do
with RW[p].EnemyModel[i] do
Lost := Destroyed[p, Owner, mix];
for p1 := 0 to nPl - 1 do { show after-attack in interface modules }
if (1 shl p1 and GWatching <> 0) and ((p1 <> p) or (bix[p1].Kind = btTerm))
then
begin
SeeFrom := ObserveLevel[FromLoc] shr (2 * p1) and
3 >= lObserveUnhidden;
SeeTo := ObserveLevel[ToLoc] shr (2 * p1) and 3 >= lObserveUnhidden;
if SeeTo and CityDestroyed then
CallPlayer(cShowCityChanged, p1, ToLoc); // city was destroyed
if SeeFrom and SeeTo then
begin
CallPlayer(cShowAfterAttack, p1, ToLoc);
CallPlayer(cShowAfterAttack, p1, FromLoc);
end
else
begin
if SeeTo then
CallPlayer(cShowUnitChanged, p1, ToLoc);
if SeeFrom then
CallPlayer(cShowUnitChanged, p1, FromLoc);
end;
if SeeTo and (MoveInfo.MoveType = mtExpel) and (ExpelToLoc >= 0) then
CallPlayer(cShowUnitChanged, p1, ExpelToLoc);
end;
end;
end;
end;
function MoveUnit(p, uix, dx, dy: integer; TestOnly: boolean): integer;
var
ToLoc: integer;
MoveInfo: TMoveInfo;
ShowMove: TShowMove;
begin
{$IFOPT O-}assert(1 shl p and InvalidTreatyMap = 0); {$ENDIF}
with RW[p].Un[uix] do
begin
ToLoc := dLoc(Loc, dx, dy);
if (ToLoc < 0) or (ToLoc >= MapSize) then
begin
result := eInvalid;
exit;
end;
result := CalculateMove(p, uix, ToLoc, 3 - dy and 1, TestOnly, MoveInfo);
if result = eZOC_EnemySpotted then
ZOCTile := ToLoc;
if (result >= rExecuted) and not TestOnly then
begin
ShowMove.dx := dx;
ShowMove.dy := dy;
ShowMove.FromLoc := Loc;
ShowMove.mix := mix;
ShowMove.Health := Health;
ShowMove.Fuel := Fuel;
ShowMove.Exp := Exp;
ShowMove.Load := TroopLoad + AirLoad;
ShowMove.Owner := p;
if (TroopLoad > 0) or (AirLoad > 0) then
ShowMove.Flags := unMulti
else
ShowMove.Flags := 0;
case MoveInfo.MoveType of
mtCapture:
ShowMove.Flags := ShowMove.Flags or umCapturing;
mtSpyMission:
ShowMove.Flags := ShowMove.Flags or umSpyMission;
mtBombard:
ShowMove.Flags := ShowMove.Flags or umBombarding;
mtExpel:
ShowMove.Flags := ShowMove.Flags or umExpelling;
end;
case MoveInfo.MoveType of
mtMove, mtCapture, mtSpyMission:
result := ExecuteMove(p, uix, ToLoc, MoveInfo, ShowMove) or result;
mtAttack, mtBombard, mtExpel:
result := ExecuteAttack(p, uix, ToLoc, MoveInfo, ShowMove) or result;
end;
end;
end;
end;
function Server(Command, Player, Subject: integer; var Data): integer; stdcall;
function CountPrice(const Offer: TOffer; PriceType: integer): integer;
var
i: integer;
begin
result := 0;
for i := 0 to Offer.nDeliver + Offer.nCost - 1 do
if Offer.Price[i] and $FFFF0000 = Cardinal(PriceType) then
inc(result);
end;
{ procedure UpdateBorderHelper;
var
x, y, Loc, Loc1, dx, dy, ObserveMask: integer;
begin
ObserveMask:=3 shl (2*pTurn);
for x:=0 to lx-1 do for y:=0 to ly shr 1-1 do
begin
Loc:=lx*(y*2)+x;
if ObserveLevel[Loc] and ObserveMask<>0 then
begin
for dy:=0 to 1 do for dx:=0 to 1 do
begin
Loc1:=(Loc+dx-1+lx) mod lx +lx*((y+dy)*2-1);
if (Loc1>=0) and (Loc1<MapSize)
and (ObserveLevel[Loc1] and ObserveMask<>0) then
if RealMap[Loc1] and $78000000=RealMap[Loc] and $78000000 then
begin
RW[pTurn].BorderHelper[Loc]:=RW[pTurn].BorderHelper[Loc] and not (1 shl (dy*2+dx));
RW[pTurn].BorderHelper[Loc1]:=RW[pTurn].BorderHelper[Loc1] and not (8 shr (dy*2+dx))
end
else
begin
RW[pTurn].BorderHelper[Loc]:=RW[pTurn].BorderHelper[Loc] or (1 shl (dy*2+dx));
RW[pTurn].BorderHelper[Loc1]:=RW[pTurn].BorderHelper[Loc1] or (8 shr (dy*2+dx));
end
end
end
end
end; }
const
ptSelect = 0;
ptTrGoods = 1;
ptUn = 2;
ptCaravan = 3;
ptImp = 4;
ptWonder = 6;
ptShip = 7;
ptInvalid = 8;
function ProjectType(Project: integer): integer;
begin
if Project and cpCompleted <> 0 then
result := ptSelect
else if Project and (cpImp + cpIndex) = cpImp + imTrGoods then
result := ptTrGoods
else if Project and cpImp = 0 then
if RW[Player].Model[Project and cpIndex].Kind = mkCaravan then
result := ptCaravan
else
result := ptUn
else if Project and cpIndex >= nImp then
result := ptInvalid
else if Imp[Project and cpIndex].Kind = ikWonder then
result := ptWonder
else if Imp[Project and cpIndex].Kind = ikShipPart then
result := ptShip
else
result := ptImp;
end;
var
d, i, j, p1, p2, pt0, pt1, uix1, cix1, Loc0, Loc1, dx, dy, NewCap, MinCap,
MaxCap, CapWeight, Cost, NextProd, Preq, TotalFood, TotalProd, CheckSum,
StopTurn, FutureMCost, NewProject, OldImp, mix, V8, V21, AStr, DStr,
ABaseDamage, DBaseDamage: integer;
CityReport: TCityReport;
FormerCLState: TCmdListState;
Adjacent: TVicinity8Loc;
Radius: TVicinity21Loc;
ShowShipChange: TShowShipChange;
ShowNegoData: TShowNegoData;
logged, ok, HasShipChanged, AllHumansDead, OfferFullySupported: Boolean;
begin
if Command = sTurn then
begin
p2 := -1;
for p1 := 0 to nPl - 1 do
if (p1 <> Player) and (1 shl p1 and GWatching <> 0) then
CallPlayer(cShowTurnChange, p1, p2);
end;
assert(MapSize = lx * ly);
assert(Command and (sctMask or sExecute) <> sctInternal or sExecute);
// not for internal commands
if (Command < 0) or (Command >= $10000) then
begin
result := eUnknown;
exit;
end;
if (Player < 0) or (Player >= nPl) or
((Command and (sctMask or sExecute) <> sctInfo) and
((Subject < 0) or (Subject >= $1000))) then
begin
result := eInvalid;
exit;
end;
if (1 shl Player and (GAlive or GWatching) = 0) and
not((Command = sTurn) or (Command = sBreak) or (Command = sResign) or
(Command = sGetAIInfo) or (Command = sGetAICredits) or
(Command = sGetVersion) or (Command and $FF0F = sGetChart)) then
begin
PutMessage(1 shl 16 + 1, Format('NOT Alive: %d', [Player]));
result := eNoTurn;
exit;
end;
result := eOK;
// check if command allowed now
if (Mode = moPlaying) and not((Command >= cClientEx) or (Command = sMessage)
or (Command = sSetDebugMap) or (Command = sGetDebugMap) or
(Command = sGetAIInfo) or (Command = sGetAICredits) or
(Command = sGetVersion) or (Command = sGetTechCost) or
(Command = sGetDefender) or (Command = sGetUnitReport) or
(Command = sGetCityReport) or (Command = sGetCityTileInfo) or
(Command = sGetCity) or (Command = sGetEnemyCityReport) or
(Command = sGetEnemyCityAreaInfo) or (Command = sGetCityReportNew) or
(Command and $FF0F = sGetChart) or (Command and $FF0F = sSetAttitude))
// commands always allowed
and not((Player = pTurn) and (Command < $1000))
// info request always allowed for pTurn
and ((pDipActive < 0) and (Player <> pTurn) // not his turn
or (pDipActive >= 0) and (Player <> pDipActive)
// not active in negotiation mode
or (pDipActive >= 0) and (Command and sctMask <> sctEndClient)) then
// no nego command
begin
PutMessage(1 shl 16 + 1, Format('No Turn: %d calls %x',
[Player, Command shr 4]));
result := eNoTurn;
exit;
end;
// do not use EXIT hereafter!
{$IFOPT O-}
HandoverStack[nHandoverStack] := Player + $1000;
HandoverStack[nHandoverStack + 1] := Command;
inc(nHandoverStack, 2);
InvalidTreatyMap := 0;
// new command, sIntExpandTerritory of previous command was processed
{$ENDIF}
if (Mode = moPlaying) and (Command >= sExecute) and
((Command and sctMask <> sctEndClient) or (Command = sTurn)) and
(Command and sctMask <> sctModel) and (Command <> sCancelTreaty) and
(Command <> sSetCityTiles) and (Command <> sBuyCityProject) and
((Command < cClientEx) or ProcessClientData[Player]) then
begin { log command }
FormerCLState := CL.State;
CL.Put(Command, Player, Subject, @Data);
logged := true;
end
else
begin
FormerCLState := default (TCmdListState);
logged := false;
end;
case Command of
{
Info Request Commands
____________________________________________________________________
}
sMessage:
bix[0].Client(cDebugMessage, Subject, Data);
sSetDebugMap:
DebugMap[Player] := @Data;
sGetDebugMap:
pointer(Data) := DebugMap[Subject];
{ sChangeSuperView:
if Difficulty[Player]=0 then
begin
for i:=0 to nBrain-1 do if Brain[i].Initialized then
CallClient(i, cShowSuperView, Subject)
end
else result:=eInvalid; }
sRefreshDebugMap:
bix[0].Client(cRefreshDebugMap, -1, Player);
sGetChart .. sGetChart + (nStat - 1) shl 4:
if (Subject >= 0) and (Subject < nPl) and Assigned(bix[Subject]) then
begin
StopTurn := 0;
if (Difficulty[Player] = 0) or (GTestFlags and tfUncover <> 0)
// supervisor
or (Subject = Player) // own chart
or (GWinner > 0) // game end chart
or (1 shl Subject and GAlive = 0) then // chart of extinct nation
if Subject > Player then
StopTurn := GTurn
else
StopTurn := GTurn + 1
else if RW[Player].Treaty[Subject] > trNoContact then
if Command shr 4 and $F = stMil then
StopTurn := RW[Player].EnemyReport[Subject].TurnOfMilReport + 1
else
StopTurn := RW[Player].EnemyReport[Subject].TurnOfCivilReport + 1;
move(Stat[Command shr 4 and $F, Subject]^, Data,
StopTurn * SizeOf(integer));
FillChar(TChart(Data)[StopTurn], (GTurn - StopTurn) *
SizeOf(integer), 0);
end
else
result := eInvalid;
sGetTechCost:
integer(Data) := TechCost(Player);
sGetAIInfo:
if AIInfo[Subject] = '' then
pchar(Data) := nil
else
pchar(Data) := @AIInfo[Subject][1];
sGetAICredits:
if AICredits = '' then
pchar(Data) := nil
else
pchar(Data) := @AICredits[1];
sGetVersion:
integer(Data) := CevoVersion;
sGetGameChanged:
if Player <> 0 then
result := eInvalid
else if (CL <> nil) and (CL.State.nLog = nLogOpened) and
(CL.State.MoveCode = 0) and not HasCityTileChanges and
not HasChanges(Player) then
result := eNotChanged;
sGetTileInfo:
if (Subject >= 0) and (Subject < MapSize) then
result := GetTileInfo(Player, -2, Subject, TTileInfo(Data))
else
result := eInvalid;
sGetCityTileInfo:
if (Subject >= 0) and (Subject < MapSize) then
result := GetTileInfo(Player, -1, Subject, TTileInfo(Data))
else
result := eInvalid;
sGetHypoCityTileInfo:
if (Subject >= 0) and (Subject < MapSize) then
begin
if (TTileInfo(Data).ExplCity < 0) or
(TTileInfo(Data).ExplCity >= RW[Player].nCity) then
result := eInvalid
else if ObserveLevel[Subject] shr (Player * 2) and 3 = 0 then
result := eNoPreq
else
result := GetTileInfo(Player, TTileInfo(Data).ExplCity, Subject,
TTileInfo(Data));
end
else
result := eInvalid;
sGetJobProgress:
if (Subject >= 0) and (Subject < MapSize) then
begin
if ObserveLevel[Subject] shr (Player * 2) and 3 = 0 then
result := eNoPreq
else
result := GetJobProgress(Player, Subject, TJobProgressData(Data));
end
else
result := eInvalid;
sGetModels:
if (GTestFlags and tfUncover <> 0) or (Difficulty[Player] = 0)
then { supervisor only command }
begin
for p1 := 0 to nPl - 1 do
if (p1 <> Player) and (1 shl p1 and GAlive <> 0) then
for mix := 0 to RW[p1].nModel - 1 do
TellAboutModel(Player, p1, mix);
end
else
result := eInvalid;
sGetUnits:
if (Subject >= 0) and (Subject < MapSize) and
(ObserveLevel[Subject] shr (Player * 2) and 3 = lObserveSuper) then
integer(Data) := GetUnitStack(Player, Subject)
else
result := eNoPreq;
sGetDefender:
if (Subject >= 0) and (Subject < MapSize) and (Occupant[Subject] = Player)
then
Strongest(Subject, integer(Data), d, i, j)
else
result := eInvalid;
sGetBattleForecast, sGetBattleForecastEx:
if (Subject >= 0) and (Subject < MapSize) and
(ObserveLevel[Subject] and (3 shl (Player * 2)) > 0) then
with TBattleForecast(Data) do
if (1 shl pAtt and GAlive <> 0) and (mixAtt >= 0) and
(mixAtt < RW[pAtt].nModel) and
((pAtt = Player) or (RWemix[Player, pAtt, mixAtt] >= 0)) then
begin
result := GetBattleForecast(Subject, TBattleForecast(Data), uix1,
cix1, AStr, DStr, ABaseDamage, DBaseDamage);
if Command = sGetBattleForecastEx then
begin
TBattleForecastEx(Data).AStr := (AStr + 200) div 400;
TBattleForecastEx(Data).DStr := (DStr + 200) div 400;
TBattleForecastEx(Data).ABaseDamage := ABaseDamage;
TBattleForecastEx(Data).DBaseDamage := DBaseDamage;
end;
if result = eOK then
result := eInvalid; // no enemy unit there!
end
else
result := eInvalid
else
result := eInvalid;
sGetUnitReport:
if (Subject < 0) or (Subject >= RW[Player].nUn) or
(RW[Player].Un[Subject].Loc < 0) then
result := eInvalid
else
GetUnitReport(Player, Subject, TUnitReport(Data));
sGetMoveAdvice:
if (Subject < 0) or (Subject >= RW[Player].nUn) or
(RW[Player].Un[Subject].Loc < 0) then
result := eInvalid
else
result := GetMoveAdvice(Player, Subject, TMoveAdviceData(Data));
sGetPlaneReturn:
if (Subject < 0) or (Subject >= RW[Player].nUn) or
(RW[Player].Un[Subject].Loc < 0) or
(RW[Player].Model[RW[Player].Un[Subject].mix].Domain <> dAir) then
result := eInvalid
else
begin
if CanPlaneReturn(Player, Subject, TPlaneReturnData(Data)) then
result := eOK
else
result := eNoWay;
end;
sGetCity:
if (Subject >= 0) and (Subject < MapSize) and
(ObserveLevel[Subject] shr (Player * 2) and 3 = lObserveSuper) and
(RealMap[Subject] and fCity <> 0) then
with TGetCityData(Data) do
begin
Owner := Player;
SearchCity(Subject, Owner, cix1);
c := RW[Owner].City[cix1];
if (Owner <> Player) and (c.Project and cpImp = 0) then
TellAboutModel(Player, Owner, c.Project and cpIndex);
end
else
result := eInvalid;
sGetCityReport:
if (Subject < 0) or (Subject >= RW[Player].nCity) or
(RW[Player].City[Subject].Loc < 0) then
result := eInvalid
else
result := GetCityReport(Player, Subject, TCityReport(Data));
sGetCityReportNew:
if (Subject < 0) or (Subject >= RW[Player].nCity) or
(RW[Player].City[Subject].Loc < 0) then
result := eInvalid
else
GetCityReportNew(Player, Subject, TCityReportNew(Data));
sGetCityAreaInfo:
if (Subject < 0) or (Subject >= RW[Player].nCity) or
(RW[Player].City[Subject].Loc < 0) then
result := eInvalid
else
GetCityAreaInfo(Player, RW[Player].City[Subject].Loc,
TCityAreaInfo(Data));
sGetEnemyCityReport:
if (Subject >= 0) and (Subject < MapSize) and
(ObserveLevel[Subject] shr (Player * 2) and 3 = lObserveSuper) and
(RealMap[Subject] and fCity <> 0) then
begin
p1 := Occupant[Subject];
if p1 < 0 then
p1 := 1;
SearchCity(Subject, p1, cix1);
TCityReport(Data).HypoTiles := -1;
TCityReport(Data).HypoTax := -1;
TCityReport(Data).HypoLux := -1;
GetCityReport(p1, cix1, TCityReport(Data));
end
else
result := eInvalid;
sGetEnemyCityReportNew:
if (Subject >= 0) and (Subject < MapSize) and
(ObserveLevel[Subject] shr (Player * 2) and 3 = lObserveSuper) and
(RealMap[Subject] and fCity <> 0) then
begin
p1 := Occupant[Subject];
if p1 < 0 then
p1 := 1;
SearchCity(Subject, p1, cix1);
TCityReport(Data).HypoTiles := -1;
TCityReport(Data).HypoTax := -1;
TCityReport(Data).HypoLux := -1;
GetCityReportNew(p1, cix1, TCityReportNew(Data));
end
else
result := eInvalid;
sGetEnemyCityAreaInfo:
if (Subject >= 0) and (Subject < MapSize) and
(ObserveLevel[Subject] shr (Player * 2) and 3 = lObserveSuper) and
(RealMap[Subject] and fCity <> 0) then
begin
p1 := Occupant[Subject];
if p1 < 0 then
p1 := 1;
SearchCity(Subject, p1, cix1);
GetCityAreaInfo(p1, Subject, TCityAreaInfo(Data));
end
else
result := eInvalid;
sGetCityTileAdvice:
if (Subject < 0) or (Subject >= RW[Player].nCity) or
(RW[Player].City[Subject].Loc < 0) then
result := eInvalid
else
GetCityTileAdvice(Player, Subject, TCityTileAdviceData(Data));
{
Map Editor Commands
____________________________________________________________________
}
sEditTile:
if Player = 0 then
with TEditTileData(Data) do
EditTile(Loc, NewTile)
else
result := eInvalid;
sRandomMap:
if (Player = 0) and MapGeneratorAvailable then
begin
CreateElevation;
PreviewElevation := false;
CreateMap(false);
FillChar(ObserveLevel, MapSize * 4, 0);
DiscoverAll(Player, lObserveSuper);
end
else
result := eInvalid;
sMapGeneratorRequest:
if not MapGeneratorAvailable then
result := eInvalid;
{
Client Deactivation Commands
____________________________________________________________________
}
sTurn, sTurn - sExecute:
begin
AllHumansDead := true;
for p1 := 0 to nPl - 1 do
if (1 shl p1 and GAlive <> 0) and (bix[p1].Kind = btTerm) then
AllHumansDead := false;
if (pDipActive >= 0) // still in negotiation mode
or (pTurn = 0) and ((GWinner > 0) or (GTurn = MaxTurn) or
(Difficulty[0] > 0) and AllHumansDead) then // game end reached
result := eViolation
else if Command >= sExecute then
begin
if Mode = moPlaying then
begin
CL.State := FormerCLState;
LogCityTileChanges;
{$IFNDEF SCR}
if pTurn = 0 then
begin
LogChanges;
SaveGame('~' + LogFileName, true);
end;
{$ENDIF}
end
else if (Mode = moMovie) and (pTurn = 0) then
CallPlayer(cMovieEndTurn, 0, nil^);
GWatching := GWatching and GAlive or 1;
RW[pTurn].Happened := 0;
uixSelectedTransport := -1;
SpyMission := smSabotageProd;
if 1 shl pTurn and GAlive <> 0 then
begin
// calculate checksum
TotalFood := 0;
TotalProd := 0;
for i := 0 to RW[pTurn].nCity - 1 do
if RW[pTurn].City[i].Loc >= 0 then
begin
inc(TotalFood, RW[pTurn].City[i].Food);
inc(TotalProd, RW[pTurn].City[i].Prod);
end;
CheckSum := TotalFood and 7 + TotalProd and 7 shl 3 +
RW[pTurn].Money and 7 shl 6 + Worked[pTurn] div 100 and 7 shl 9;
end
else
CheckSum := 0;
if Mode < moPlaying then // check checksum
begin
if CheckSum <> Subject then
LoadOK := false;
end
else // save checksum
CL.Put(Command, Player, CheckSum, @Data);
{$IFDEF TEXTLOG}
CmdInfo := '';
if CheckSum and 7 <> Subject and 7 then
CmdInfo := Format('***ERROR (Food %d) ',
[(CheckSum and 7 - Subject and 7 + 12) mod 8 - 4]) + CmdInfo;
if CheckSum shr 3 and 7 <> Subject shr 3 and 7 then
CmdInfo := '***ERROR (Prod) ' + CmdInfo;
if CheckSum shr 6 and 7 <> Subject shr 6 and 7 then
CmdInfo := '***ERROR (Research) ' + CmdInfo;
if CheckSum shr 9 and 7 <> Subject shr 9 and 7 then
CmdInfo := '***ERROR (Work) ' + CmdInfo;
{$ENDIF}
if 1 shl pTurn and GAlive <> 0 then
begin
AfterTurn;
if Mode < moPlaying then
InsertTerritoryUpdateCommands;
// if bix[pTurn]=bixTerm then UpdateBorderHelper;
end;
repeat
pTurn := (pTurn + 1) mod nPl;
if pTurn = 0 then
inc(GTurn);
if Assigned(bix[pTurn]) and ((1 shl pTurn) and GAlive = 0) then
begin // already made extinct -- continue statistics
Stat[stExplore, pTurn, GTurn] := 0;
Stat[stPop, pTurn, GTurn] := 0;
Stat[stTerritory, pTurn, GTurn] := 0;
Stat[stScience, pTurn, GTurn] := 0;
Stat[stWork, pTurn, GTurn] := 0;
Stat[stMil, pTurn, GTurn] := 0;
end;
until (pTurn = 0) or ((1 shl pTurn and (GAlive or GWatching) <> 0) and
(GWinner = 0));
if (Mode = moLoading_Fast) and
((GTurn = LoadTurn) or (GTurn = LoadTurn - 1) and (pTurn > 0)) then
Mode := moLoading;
if Mode = moPlaying then
begin
CCCommand := cTurn;
CCPlayer := pTurn;
Notify(ntNextPlayer);
end
else
begin
if GTurn = 0 then
BeforeTurn0
else
BeforeTurn;
if (Mode = moMovie) and (pTurn = 0) then
begin
Inform(pTurn);
CallPlayer(cMovieTurn, 0, nil^);
end;
end;
{$IFDEF TEXTLOG}CmdInfo := CmdInfo + Format('---Turn %d P%d---', [GTurn, pTurn]); {$ENDIF}
end;
end; // sTurn
sBreak, sResign, sNextRound, sReload:
if Mode = moMovie then
MovieStopped := true
else
begin
if Command = sReload then
begin
ok := (Difficulty[0] = 0) and (bix[0].Kind <> btNoTerm) and
(integer(Data) >= 0) and (integer(Data) < GTurn);
for p1 := 1 to nPl - 1 do
if bix[p1].Kind = btTerm then
ok := false;
// allow reload in AI-only games only
end
else
ok := Player = 0;
if ok then
begin
if (Command = sBreak) or (Command = sResign) then
Notify(ntBackOn);
for i := 0 to Brains.Count - 1 do
if Brains[i].Initialized then
begin
if Brains[i].Kind = btAI then
Notify(ntDeinitModule, i);
CallClient(i, cBreakGame, nil^);
end;
Notify(ntEndInfo);
if (Command = sBreak) or (Command = sReload) then
begin
LogCityTileChanges;
LogChanges;
SaveGame(LogFileName, false);
end;
// DeleteFile(SavePath + '~' + LogFileName);
EndGame;
case Command of
sBreak:
Notify(ntStartGoRefresh);
sResign:
Notify(ntStartGo);
sNextRound:
StartNewGame(SavePath, LogFileName, MapFileName, lx, ly,
LandMass, MaxTurn);
sReload:
LoadGame(SavePath, LogFileName, integer(Data), false);
end;
end
else
result := eInvalid;
end;
sAbandonMap, sSaveMap:
if Player = 0 then
begin
if Command = sSaveMap then
SaveMap(MapFileName);
Notify(ntBackOn);
BrainTerm.Client(cBreakGame, -1, nil^);
ReleaseMapEditor;
if Command = sSaveMap then
Notify(ntStartGoRefreshMaps)
else
Notify(ntStartGo);
end
else
result := eInvalid;
scContact .. scContact + (nPl - 1) shl 4, scContact - sExecute .. scContact
- sExecute + (nPl - 1) shl 4:
if (pDipActive >= 0) or (1 shl (Command shr 4 and $F) and GAlive = 0) then
result := eInvalid
else if GWinner > 0 then
result := eViolation // game end reached
else if RW[Player].Treaty[Command shr 4 and $F] = trNoContact then
result := eNoPreq
else if GTurn < GColdWarStart + ColdWarTurns then
result := eColdWar
else if RW[Player].Government = gAnarchy then
result := eAnarchy
else if RW[Command shr 4 and $F].Government = gAnarchy then
begin
result := eAnarchy;
LastEndClientCommand := scReject; // enable cancel treaty
pContacted := Command shr 4 and $F;
end
else if Command >= sExecute then
begin // contact request
pContacted := Command shr 4 and $F;
pDipActive := pContacted;
assert(Mode = moPlaying);
Inform(pDipActive);
ChangeClientWhenDone(scContact, pDipActive, pTurn, 4);
end;
scReject, scReject - sExecute:
if LastEndClientCommand and $FF0F = scContact then
begin
if Command >= sExecute then
begin // contact requested and not accepted yet
pDipActive := -1;
assert(Mode = moPlaying);
ChangeClientWhenDone(cContinue, pTurn, nil^, 0);
end;
end
else
result := eInvalid;
scDipStart, scDipStart - sExecute:
if LastEndClientCommand and $FF0F = scContact then
begin
if Command >= sExecute then
begin // accept contact
pContacted := pDipActive;
RW[pContacted].EnemyReport[pTurn].Credibility :=
RW[pTurn].Credibility;
pDipActive := pTurn;
assert(Mode = moPlaying);
IntServer(sIntHaveContact, pTurn, pContacted, nil^);
ChangeClientWhenDone(scDipStart, pDipActive, nil^, 0);
end;
end
else
result := eInvalid;
scDipNotice, scDipAccept, scDipCancelTreaty, scDipBreak,
scDipNotice - sExecute, scDipAccept - sExecute,
scDipCancelTreaty - sExecute, scDipBreak - sExecute:
if pDipActive >= 0 then
begin
assert(Mode = moPlaying);
if pDipActive = pTurn then
p1 := pContacted
else
p1 := pTurn;
if (Command and not sExecute = scDipBreak and not sExecute) and
(LastEndClientCommand <> scDipBreak) then // ok
else if (Command and not sExecute = scDipNotice and not sExecute) and
((LastEndClientCommand = scDipCancelTreaty) or
(LastEndClientCommand = scDipBreak)) then // ok
else if (Command and not sExecute = scDipAccept and not sExecute) and
(LastEndClientCommand = scDipOffer) then
with LastOffer do
begin
// check if offer can be accepted
if nDeliver + nCost = 0 then
result := eOfferNotAcceptable;
for i := 0 to nDeliver + nCost - 1 do
if Price[i] = opChoose then
result := eOfferNotAcceptable;
for i := 0 to nCost - 1 do
if not PayPrice(pDipActive, p1, Price[nDeliver + i], false) then
result := eOfferNotAcceptable;
if (Command >= sExecute) and (result >= rExecuted) then
begin
IntServer(sIntPayPrices + nDeliver + nCost, p1, pDipActive,
LastOffer);
// CheckContact;
// tell other players about ship part trades
HasShipChanged := false;
FillChar(ShowShipChange, SizeOf(ShowShipChange), 0);
for i := 0 to nDeliver + nCost - 1 do
if Price[i] and opMask = opShipParts then
begin
HasShipChanged := true;
if i >= nDeliver then
begin // p1 has demanded from pDipActive
ShowShipChange.Ship1Change[Price[i] shr 16 and 3] :=
+integer(Price[i] and $FFFF);
ShowShipChange.Ship2Change[Price[i] shr 16 and 3] :=
-integer(Price[i] and $FFFF);
end
else
begin // p1 has delivered to pDipActive
ShowShipChange.Ship1Change[Price[i] shr 16 and 3] :=
-integer(Price[i] and $FFFF);
ShowShipChange.Ship2Change[Price[i] shr 16 and 3] :=
+integer(Price[i] and $FFFF);
end;
end;
if HasShipChanged then
begin
ShowShipChange.Reason := scrTrade;
ShowShipChange.Ship1Owner := p1;
ShowShipChange.Ship2Owner := pDipActive;
for p2 := 0 to nPl - 1 do
if (p2 <> p1) and (p2 <> pDipActive) and
(1 shl p2 and (GAlive or GWatching) <> 0) then
begin
move(GShip, RW[p2].Ship, SizeOf(GShip));
if 1 shl p2 and GWatching <> 0 then
CallPlayer(cShowShipChange, p2, ShowShipChange);
end;
end;
end;
end
else if (Command and not sExecute = scDipCancelTreaty and not sExecute)
and (RW[pDipActive].Treaty[p1] >= trPeace) then
begin
if (ServerVersion[pDipActive] >= $010100) and
(GTurn < RW[pDipActive].LastCancelTreaty[p1] + CancelTreatyTurns)
then
result := eCancelTreatyRush
else if Command >= sExecute then
begin
IntServer(sIntCancelTreaty, pDipActive, p1, nil^);
for p2 := 0 to nPl - 1 do
if (p2 <> p1) and (1 shl p2 and PeaceEnded <> 0) then
begin
i := p1 shl 4 + pDipActive;
CallPlayer(cShowSupportAllianceAgainst, p2, i);
end;
for p2 := 0 to nPl - 1 do
if (p2 <> p1) and (1 shl p2 and PeaceEnded <> 0) then
begin
i := p2;
CallPlayer(cShowCancelTreatyByAlliance, pDipActive, i);
end;
end;
end
else
result := eInvalid;
if (Command >= sExecute) and (result >= rExecuted) then
if LastEndClientCommand = scDipBreak then
begin // break negotiation
pDipActive := -1;
CallPlayer(cShowEndContact, pContacted, nil^);
ChangeClientWhenDone(cContinue, pTurn, nil^, 0);
end
else
begin
if (GTestFlags and tfUncover <> 0) or (Difficulty[0] = 0) then
with ShowNegoData do
begin // display negotiation in log window
pSender := pDipActive;
pTarget := p1;
Action := Command;
bix[0].Client(cShowNego, 1 shl 16 + 3, ShowNegoData);
end;
pDipActive := p1;
ChangeClientWhenDone(Command, pDipActive, nil^, 0);
end;
end
else
result := eInvalid;
scDipOffer, scDipOffer - sExecute:
if (pDipActive >= 0) and (LastEndClientCommand <> scDipCancelTreaty) and
(LastEndClientCommand <> scDipBreak) then
if (LastEndClientCommand = scDipOffer) and
(LastOffer.nDeliver + LastOffer.nCost + TOffer(Data).nDeliver +
TOffer(Data).nCost = 0) then
begin
if Command >= sExecute then
begin // agreed discussion end
pDipActive := -1;
CallPlayer(cShowEndContact, pContacted, nil^);
assert(Mode = moPlaying);
ChangeClientWhenDone(cContinue, pTurn, nil^, 0);
end;
end
else
begin
// check if offer can be made
if pDipActive = pTurn then
p1 := pContacted
else
p1 := pTurn;
if RW[pDipActive].Treaty[p1] < trPeace then
begin // no tribute allowed!
for i := 0 to TOffer(Data).nDeliver + TOffer(Data).nCost - 1 do
if (TOffer(Data).Price[i] and opMask = opTribute) then
result := eInvalidOffer;
for i := 0 to TOffer(Data).nDeliver + TOffer(Data).nCost - 1 do
if (TOffer(Data).Price[i] = opTreaty + trPeace) then
result := eOK;
end;
for i := 0 to TOffer(Data).nDeliver - 1 do
if (TOffer(Data).Price[i] <> opChoose) and
not PayPrice(pDipActive, p1, TOffer(Data).Price[i], false) then
result := eInvalidOffer;
if CountPrice(TOffer(Data), opTreaty) > 1 then
result := eInvalidOffer;
for i := 0 to nShipPart - 1 do
if CountPrice(TOffer(Data), opShipParts + i shl 16) > 1 then
result := eInvalidOffer;
if CountPrice(TOffer(Data), opMoney) > 1 then
result := eInvalidOffer;
if CountPrice(TOffer(Data), opTribute) > 1 then
result := eInvalidOffer;
case CountPrice(TOffer(Data), opChoose) of
0:
;
1:
if (TOffer(Data).nCost = 0) or (TOffer(Data).nDeliver = 0) then
result := eInvalidOffer;
else
result := eInvalidOffer;
end;
// !!! check here if cost can be demanded
if (Command >= sExecute) and (result >= rExecuted) then
begin
OfferFullySupported := (TOffer(Data).nDeliver <= 2) and
(TOffer(Data).nCost <= 2); // >2 no more allowed
for i := 0 to TOffer(Data).nDeliver + TOffer(Data).nCost - 1 do
begin
if TOffer(Data).Price[i] and opMask = opTribute then
OfferFullySupported := false;
// tribute no more part of the game
if (TOffer(Data).Price[i] and opMask = opTreaty) and
(TOffer(Data).Price[i] - opTreaty <= RW[pDipActive].Treaty[p1])
then
OfferFullySupported := false;
// agreed treaty end no more part of the game
if TOffer(Data).Price[i] = opTreaty + trCeaseFire then
OfferFullySupported := false;
// ceasefire no more part of the game
end;
if not OfferFullySupported then
begin
// some elements have been removed from the game -
// automatically respond will null-offer
LastOffer.nDeliver := 0;
LastOffer.nCost := 0;
ChangeClientWhenDone(scDipOffer, pDipActive, LastOffer,
SizeOf(LastOffer));
end
else
begin
if (GTestFlags and tfUncover <> 0) or (Difficulty[0] = 0) then
with ShowNegoData do
begin // display negotiation in log window
pSender := pDipActive;
pTarget := p1;
Action := Command;
Offer := TOffer(Data);
bix[0].Client(cShowNego, 1 shl 16 + 3, ShowNegoData);
end;
LastOffer := TOffer(Data);
// show offered things to receiver
for i := 0 to LastOffer.nDeliver - 1 do
ShowPrice(pDipActive, p1, LastOffer.Price[i]);
pDipActive := p1;
assert(Mode = moPlaying);
ChangeClientWhenDone(scDipOffer, pDipActive, LastOffer,
SizeOf(LastOffer));
end;
end;
end
else
result := eInvalid;
{
General Commands
____________________________________________________________________
}
sClearTestFlag:
if Player = 0 then
begin
{$IFDEF TEXTLOG}CmdInfo := Format('ClearTestFlag %x', [Subject]); {$ENDIF}
ClearTestFlags(Subject);
end
else
result := eInvalid;
sSetTestFlag:
if Player = 0 then
begin
{$IFDEF TEXTLOG}CmdInfo := Format('SetTestFlag %x', [Subject]); {$ENDIF}
SetTestFlags(Player, Subject);
// CheckContact;
end
else
result := eInvalid;
sSetGovernment, sSetGovernment - sExecute:
begin
{$IFDEF TEXTLOG}CmdInfo := Format('SetGovernment P%d: %d', [Player, Subject]); {$ENDIF}
if RW[Player].Happened and phChangeGov = 0 then
result := eViolation
else if RW[Player].Government = Subject then
result := eNotChanged
else if (Subject >= nGov) then
result := eInvalid
else if (Subject >= gMonarchy) and
(RW[Player].Tech[GovPreq[Subject]] < tsApplicable) then
result := eNoPreq
else if Command >= sExecute then
begin
RW[Player].Government := Subject;
for p1 := 0 to nPl - 1 do
if (p1 <> Player) and ((GAlive or GWatching) and (1 shl p1) <> 0)
then
RW[p1].EnemyReport[Player].Government := Subject;
end;
end;
sSetRates, sSetRates - sExecute:
begin
{$IFDEF TEXTLOG}CmdInfo := Format('SetRates P%d: %d/%d', [Player, Subject and $F * 10, Subject shr 4 * 10]); {$ENDIF}
if Subject and $F + Subject shr 4 > 10 then
result := eInvalid
else if (RW[Player].TaxRate = Subject and $F * 10) and
(RW[Player].LuxRate = Subject shr 4 * 10) then
result := eNotChanged
else if Command >= sExecute then
begin
RW[Player].TaxRate := Subject and $F * 10;
RW[Player].LuxRate := Subject shr 4 * 10;
end;
end;
sRevolution:
begin
{$IFDEF TEXTLOG}CmdInfo := Format('Revolution P%d', [Player]); {$ENDIF}
if RW[Player].Government = gAnarchy then
result := eInvalid
else
begin
RW[Player].Government := gAnarchy;
for p1 := 0 to nPl - 1 do
if (p1 <> Player) and ((GAlive or GWatching) and (1 shl p1) <> 0)
then
RW[p1].EnemyReport[Player].Government := gAnarchy;
RW[Player].AnarchyStart := GTurn;
end;
end;
sSetResearch, sSetResearch - sExecute:
with RW[Player] do
begin
{$IFDEF TEXTLOG}CmdInfo := Format('SetResearch P%d: %d', [Player, Subject]);
{$ENDIF}
if (Happened and phTech <> 0) and
((Subject < nAdv) or (Subject = adMilitary)) then
begin
if (Mode = moPlaying) and (Subject = adMilitary) and
(DevModelTurn[Player] <> GTurn) then
result := eNoModel
else if Subject <> adMilitary then
begin
if Subject = futComputingTechnology then
begin
if Tech[Subject] >= MaxFutureTech_Computing then
result := eInvalid;
end
else if Subject in FutureTech then
begin
if Tech[Subject] >= MaxFutureTech then
result := eInvalid;
end
else if Tech[Subject] >= tsApplicable then
result := eInvalid; // already discovered
if Tech[Subject] <> tsSeen then // look if preqs met
if AdvPreq[Subject, 2] <> preNone then
begin // 2 of 3 required
i := 0;
for j := 0 to 2 do
if Tech[AdvPreq[Subject, j]] >= tsApplicable then
inc(i);
if i < 2 then
result := eNoPreq;
end
else if (AdvPreq[Subject, 0] <> preNone) and
(Tech[AdvPreq[Subject, 0]] < tsApplicable) or
(AdvPreq[Subject, 1] <> preNone) and
(Tech[AdvPreq[Subject, 1]] < tsApplicable) then
result := eNoPreq;
end;
if (result = eOK) and (Command >= sExecute) then
begin
if (Mode = moPlaying) and (Subject = adMilitary) then
IntServer(sIntSetDevModel, Player, 0, DevModel.Kind);
// save DevModel, because sctModel commands are not logged
ResearchTech := Subject;
end;
end
else
result := eViolation;
end;
sStealTech, sStealTech - sExecute:
begin
{$IFDEF TEXTLOG}CmdInfo := Format('StealTech P%d: %d', [Player, Subject]);
{$ENDIF}
if RW[Player].Happened and phStealTech = 0 then
result := eInvalid
else if (Subject >= nAdv) or (Subject in FutureTech) or
(RW[Player].Tech[Subject] >= tsSeen) or
(RW[GStealFrom].Tech[Subject] < tsApplicable) then
result := eInvalid
else if Command >= sExecute then
begin
SeeTech(Player, Subject);
dec(RW[Player].Happened, phStealTech);
end;
end;
sSetAttitude .. sSetAttitude + (nPl - 1) shl 4,
sSetAttitude - sExecute .. sSetAttitude - sExecute + (nPl - 1) shl 4:
begin
p1 := Command shr 4 and $F;
{$IFDEF TEXTLOG}CmdInfo := Format('SetAttitude P%d to P%d: %d', [Player, p1, Subject]); {$ENDIF}
if (Subject >= nAttitude) or (p1 >= nPl) or
(RW[Player].EnemyReport[p1] = nil) then
result := eInvalid
else if RW[Player].Treaty[p1] = trNoContact then
result := eNoPreq
else if RW[Player].Attitude[p1] = Subject then
result := eNotChanged
else if Command >= sExecute then
begin
RW[Player].Attitude[p1] := Subject;
RW[p1].EnemyReport[Player].Attitude := Subject;
end;
end;
sCancelTreaty, sCancelTreaty - sExecute:
if (LastEndClientCommand <> scReject) or
(RW[Player].Treaty[pContacted] < trPeace) then
result := eInvalid
else if (ServerVersion[Player] >= $010100) and
(GTurn < RW[Player].LastCancelTreaty[pContacted] + CancelTreatyTurns)
then
result := eCancelTreatyRush
else if Command >= sExecute then
begin
CallPlayer(cShowCancelTreaty, pContacted, Player);
IntServer(sIntCancelTreaty, Player, pContacted, nil^);
for p2 := 0 to nPl - 1 do
if (p2 <> pContacted) and (1 shl p2 and PeaceEnded <> 0) then
begin
i := pContacted shl 4 + Player;
CallPlayer(cShowSupportAllianceAgainst, p2, i);
end;
for p2 := 0 to nPl - 1 do
if (p2 <> pContacted) and (1 shl p2 and PeaceEnded <> 0) then
begin
i := p2;
CallPlayer(cShowCancelTreatyByAlliance, Player, i);
end;
LastEndClientCommand := sTurn;
end;
{
Model Related Commands
____________________________________________________________________
}
sCreateDevModel, sCreateDevModel - sExecute:
begin
{$IFDEF TEXTLOG}CmdInfo := Format('CreateDevModel P%d', [Player]); {$ENDIF}
if Subject >= 4 then
result := eInvalid
else if (upgrade[Subject, 0].Preq <> preNone) and
(RW[Player].Tech[upgrade[Subject, 0].Preq] < tsApplicable) then
result := eNoPreq
else if Command >= sExecute then
begin
with RW[Player].DevModel do
begin
Domain := Subject;
MStrength := 0;
MTrans := 0;
MCost := 0;
Upgrades := 0;
FutureMCost := 0;
for i := 0 to nUpgrade - 1 do
with upgrade[Domain, i] do
if (Preq = preNone) or (Preq >= 0) and
((RW[Player].Tech[Preq] >= tsApplicable) or
(Preq in FutureTech) and (RW[Player].Tech[Preq] >= 0)) then
begin
if Preq in FutureTech then
begin
j := RW[Player].Tech[Preq];
inc(FutureMCost, j * Cost);
end
else
begin
j := 1;
if Cost > MCost then
MCost := Cost;
end;
inc(Upgrades, 1 shl i);
inc(MStrength, j * Strength);
inc(MTrans, j * Trans);
end;
inc(MCost, FutureMCost);
FillChar(Cap, SizeOf(Cap), 0);
Cap[mcOffense] := 2;
Cap[mcDefense] := 1;
for i := 0 to nFeature - 1 do
with Feature[i] do
if (1 shl Domain and Domains <> 0) and
((Preq = preNone) or (Preq = preSun) and
(GWonder[woSun].EffectiveOwner = Player) or (Preq >= 0) and
(RW[Player].Tech[Preq] >= tsApplicable)) and (i in AutoFeature)
then
Cap[i] := 1;
MaxWeight := 5;
if (WeightPreq7[Domain] <> preNA) and
(RW[Player].Tech[WeightPreq7[Domain]] >= tsApplicable) then
MaxWeight := 7;
if (WeightPreq10[Domain] <> preNA) and
(RW[Player].Tech[WeightPreq10[Domain]] >= tsApplicable) then
if Domain = dSea then
MaxWeight := 9
else
MaxWeight := 10;
end;
CalculateModel(RW[Player].DevModel);
DevModelTurn[Player] := GTurn;
end
end;
sSetDevModelCap .. sSetDevModelCap + $3F0,
sSetDevModelCap - sExecute .. sSetDevModelCap - sExecute + $3F0:
begin
{$IFDEF TEXTLOG}CmdInfo := Format('SetDevModelCap P%d', [Player]); {$ENDIF}
if Subject >= nFeature then
result := eInvalid
else if DevModelTurn[Player] = GTurn then
begin
NewCap := Command shr 4 and $3F; { new value }
with RW[Player].DevModel do
if 1 shl Domain and Feature[Subject].Domains = 0 then
result := eDomainMismatch
else if not((Feature[Subject].Preq = preNone) or
(Feature[Subject].Preq = preSun) and
(GWonder[woSun].EffectiveOwner = Player) or
(Feature[Subject].Preq >= 0) and
(RW[Player].Tech[Feature[Subject].Preq] >= tsApplicable)) then
result := eNoPreq
else
begin
if (Subject in AutoFeature) or (Subject = mcDefense) then
MinCap := 1
else
MinCap := 0; { MinCap - minimum use of feature }
if Subject >= mcFirstNonCap then
MaxCap := 1
else if Subject = mcDefense then
begin
if Domain = dGround then
MaxCap := 2
else
MaxCap := 3;
if RW[Player].Tech[adSteel] >= tsApplicable then
inc(MaxCap);
end
else
MaxCap := 8; { MaxCap - maximum use of this feature }
if (Domain = dGround) and (Subject = mcDefense) then
CapWeight := 2
else
CapWeight := Feature[Subject].Weight;
if (NewCap < MinCap) or (NewCap > MaxCap) or
(Weight + (NewCap - Cap[Subject]) * CapWeight > MaxWeight) then
result := eViolation
else if Command >= sExecute then
begin
Cap[Subject] := NewCap;
// mutual feature exclusion
case Subject of
mcSub:
begin
if ServerVersion[Player] >= $010103 then
Cap[mcSeaTrans] := 0;
Cap[mcArtillery] := 0;
Cap[mcCarrier] := 0;
if Cap[mcDefense] > 2 then
Cap[mcDefense] := 2;
end;
mcSeaTrans:
begin
if ServerVersion[Player] >= $010103 then
Cap[mcSub] := 0;
end;
mcCarrier:
Cap[mcSub] := 0;
mcArtillery:
Cap[mcSub] := 0;
mcAlpine:
begin
Cap[mcOver] := 0;
Cap[mcMob] := 0;
end;
mcOver:
Cap[mcAlpine] := 0;
mcMob:
begin
Cap[mcAlpine] := 0;
end;
end;
CalculateModel(RW[Player].DevModel);
end;
end;
end
else
result := eNoModel;
end;
{
Unit Related Commands
____________________________________________________________________
}
sRemoveUnit, sRemoveUnit - sExecute:
begin
{$IFDEF TEXTLOG}CmdInfo := Format('RemoveUnit P%d Mod%d Loc%d', [Player, RW[Player].Un[Subject].mix, RW[Player].Un[Subject].Loc]); {$ENDIF}
if (Subject >= RW[Player].nUn) or (RW[Player].Un[Subject].Loc < 0) then
result := eInvalid
else
begin
result := eRemoved;
Loc0 := RW[Player].Un[Subject].Loc;
if RealMap[Loc0] and fCity <> 0 then { check utilize }
begin
SearchCity(Loc0, Player, cix1);
with RW[Player].City[cix1] do
begin
if (RW[Player].Model[RW[Player].Un[Subject].mix].Kind = mkCaravan)
and ((Project and cpImp = 0) or
(Imp[Project and cpIndex].Kind <> ikShipPart)) or
(Project and cpImp = 0) and
(RW[Player].Model[Project and cpIndex].Kind <> mkCaravan) then
result := eUtilized;
if Command >= sExecute then
begin
if result = eUtilized then
begin
with RW[Player].Un[Subject] do
begin
Cost := integer(RW[Player].Model[mix].Cost) * Health *
BuildCostMod[Difficulty[Player]] div 1200;
if RW[Player].Model[mix].Cap[mcLine] > 0 then
Cost := Cost div 2;
end;
if Project and (cpImp + cpIndex) = cpImp + imTrGoods then
inc(RW[Player].Money, Cost)
else
begin
inc(Prod, Cost * 2 div 3);
Project0 := Project0 and not cpCompleted;
if Project0 and not cpAuto <> Project and not cpAuto then
Project0 := Project;
Prod0 := Prod;
end
end;
RemoveUnit_UpdateMap(Player, Subject);
end;
end;
end
else if Command >= sExecute then
RemoveUnit_UpdateMap(Player, Subject);
end
end;
sSetUnitHome, sSetUnitHome - sExecute:
begin
{$IFDEF TEXTLOG}CmdInfo := Format('SetUnitHome P%d Mod%d Loc%d', [Player, RW[Player].Un[Subject].mix, RW[Player].Un[Subject].Loc]); {$ENDIF}
if (Subject >= RW[Player].nUn) or (RW[Player].Un[Subject].Loc < 0) then
result := eInvalid
else
begin
Loc0 := RW[Player].Un[Subject].Loc;
if RealMap[Loc0] and fCity = 0 then
result := eInvalid
else
begin
SearchCity(Loc0, Player, cix1);
if RW[Player].City[cix1].Flags and chCaptured <> 0 then
result := eViolation
else if Command >= sExecute then
RW[Player].Un[Subject].Home := cix1;
end;
end;
end;
sSetSpyMission .. sSetSpyMission + (nSpyMission - 1) shl 4,
sSetSpyMission - sExecute .. sSetSpyMission - sExecute +
(nSpyMission - 1) shl 4:
if Command >= sExecute then
SpyMission := Command shr 4 and $F;
sLoadUnit, sLoadUnit - sExecute:
begin
{$IFDEF TEXTLOG}CmdInfo := Format('LoadUnit P%d Mod%d Loc%d', [Player, RW[Player].Un[Subject].mix, RW[Player].Un[Subject].Loc]); {$ENDIF}
if (Subject >= RW[Player].nUn) or (RW[Player].Un[Subject].Loc < 0) then
result := eInvalid
else
result := LoadUnit(Player, Subject, Command < sExecute);
end;
sUnloadUnit, sUnloadUnit - sExecute:
begin
{$IFDEF TEXTLOG}CmdInfo := Format('UnloadUnit P%d Mod%d Loc%d', [Player, RW[Player].Un[Subject].mix, RW[Player].Un[Subject].Loc]); {$ENDIF}
if (Subject >= RW[Player].nUn) or (RW[Player].Un[Subject].Loc < 0) then
result := eInvalid
else
result := UnloadUnit(Player, Subject, Command < sExecute);
end;
sSelectTransport, sSelectTransport - sExecute:
if (Subject >= RW[Player].nUn) or (RW[Player].Un[Subject].Loc < 0) then
result := eInvalid
else
with RW[Player].Model[RW[Player].Un[Subject].mix] do
begin
if Cap[mcSeaTrans] + Cap[mcAirTrans] + Cap[mcCarrier] = 0 then
result := eInvalid
else if Command >= sExecute then
uixSelectedTransport := Subject;
end;
sCreateUnit .. sCreateUnit + (nPl - 1) shl 4,
sCreateUnit - sExecute .. sCreateUnit - sExecute + (nPl - 1) shl 4:
if (GTestFlags and tfUncover <> 0) or (Difficulty[Player] = 0)
then { supervisor only command }
begin
p1 := Command shr 4 and $F;
Loc1 := integer(Data);
if (Occupant[Loc1] >= 0) and (p1 <> Occupant[Loc1]) or
(RealMap[Loc1] and fCity <> 0) and
(RealMap[Loc1] shr 27 <> Cardinal(p1)) or
(RW[p1].Model[Subject].Domain < dAir) and
((RW[p1].Model[Subject].Domain = dSea) <> (RealMap[integer(Data)] and
fTerrain < fGrass)) then
result := eViolation
else if Command >= sExecute then
begin
CreateUnit(p1, Subject);
RW[p1].Un[RW[p1].nUn - 1].Loc := integer(Data);
PlaceUnit(p1, RW[p1].nUn - 1);
UpdateUnitMap(integer(Data));
end;
end
else
result := eInvalid;
sMoveUnit + (0 + 6 * 8) * 16, sMoveUnit + (1 + 7 * 8) * 16,
sMoveUnit + (2 + 0 * 8) * 16, sMoveUnit + (1 + 1 * 8) * 16,
sMoveUnit + (0 + 2 * 8) * 16, sMoveUnit + (7 + 1 * 8) * 16,
sMoveUnit + (6 + 0 * 8) * 16, sMoveUnit + (7 + 7 * 8) * 16,
sMoveUnit - sExecute + (0 + 6 * 8) * 16, sMoveUnit - sExecute +
(1 + 7 * 8) * 16, sMoveUnit - sExecute + (2 + 0 * 8) * 16,
sMoveUnit - sExecute + (1 + 1 * 8) * 16, sMoveUnit - sExecute +
(0 + 2 * 8) * 16, sMoveUnit - sExecute + (7 + 1 * 8) * 16,
sMoveUnit - sExecute + (6 + 0 * 8) * 16, sMoveUnit - sExecute +
(7 + 7 * 8) * 16:
begin
dx := (Command shr 4 + 4) and 7 - 4;
dy := (Command shr 7 + 4) and 7 - 4;
{$IFDEF TEXTLOG}CmdInfo := Format('MoveUnit P%d I%d Mod%d Loc%d (%d,%d)', [Player, Subject, RW[Player].Un[Subject].mix, RW[Player].Un[Subject].Loc, dx, dy]); {$ENDIF}
if (Subject >= RW[Player].nUn) or (RW[Player].Un[Subject].Loc < 0) then
result := eInvalid
else
result := MoveUnit(Player, Subject, dx, dy, Command < sExecute);
end;
{
Settlers Related Commands
____________________________________________________________________
}
sAddToCity, sAddToCity - sExecute:
begin
{$IFDEF TEXTLOG}CmdInfo := Format('AddToCity P%d Mod%d Loc%d', [Player, RW[Player].Un[Subject].mix, RW[Player].Un[Subject].Loc]); {$ENDIF}
if (Subject >= RW[Player].nUn) or (RW[Player].Un[Subject].Loc < 0) then
result := eInvalid
else if not(RW[Player].Model[RW[Player].Un[Subject].mix].Kind
in [mkSettler, mkSlaves]) and
(RW[Player].Un[Subject].Flags and unConscripts = 0) then
result := eViolation
else
begin
Loc0 := RW[Player].Un[Subject].Loc;
if RealMap[Loc0] and fCity = 0 then
result := eInvalid
else
begin
SearchCity(Loc0, Player, cix1);
with RW[Player].City[cix1] do
if not CanCityGrow(Player, cix1) then
result := eMaxSize
else if Command >= sExecute then
begin { add to city }
if Mode = moPlaying then
SavedTiles[cix1] := 0; // save in every case
if CanCityGrow(Player, cix1) then
CityGrowth(Player, cix1);
if (RW[Player].Model[RW[Player].Un[Subject].mix]
.Kind = mkSettler) and CanCityGrow(Player, cix1) then
CityGrowth(Player, cix1);
RemoveUnit_UpdateMap(Player, Subject);
end;
end;
end;
end;
sStartJob .. sStartJob + $3F0, sStartJob - sExecute .. sStartJob + $3F0
- sExecute:
begin
Loc0 := RW[Player].Un[Subject].Loc;
i := Command shr 4 and $3F; // new job
{$IFDEF TEXTLOG}CmdInfo := Format('StartJob P%d Mod%d Loc%d: %d', [Player, RW[Player].Un[Subject].mix, Loc0, i]); {$ENDIF}
if (Subject >= RW[Player].nUn) or (Loc0 < 0) then
result := eInvalid
else if i >= nJob then
result := eInvalid
else
begin
result := StartJob(Player, Subject, i, Command < sExecute);
if result = eCity then
begin // new city
cix1 := RW[Player].nCity - 1;
AddBestCityTile(Player, cix1);
if Mode = moPlaying then
with RW[Player].City[cix1] do
begin
// SavedResourceWeights[cix1]:=ResourceWeights;
SavedTiles[cix1] := 0; // save in every case
end;
if Mode >= moMovie then { show new city in interface modules }
for p1 := 0 to nPl - 1 do
if (1 shl p1 and GWatching <> 0) and (p1 <> Player) and
(ObserveLevel[Loc0] and (3 shl (2 * p1)) > 0) then
CallPlayer(cShowCityChanged, p1, Loc0);
end;
end;
end;
{
City Related Commands
____________________________________________________________________
}
sSetCityProject, sSetCityProject - sExecute:
begin
NewProject := integer(Data) and not cpAuto;
{$IFDEF TEXTLOG}CmdInfo := Format('SetCityProject P%d Loc%d: %d', [Player, RW[Player].City[Subject].Loc, NewProject]); {$ENDIF}
if (Subject >= RW[Player].nCity) or (RW[Player].City[Subject].Loc < 0)
then
result := eInvalid
else
with RW[Player].City[Subject] do
begin
if NewProject = Project then
result := eNotChanged
else
begin
pt0 := ProjectType(Project0);
pt1 := ProjectType(NewProject);
if NewProject and cpImp = 0 then
begin
if NewProject and cpIndex >= RW[Player].nModel then
result := eInvalid
else if (NewProject and cpConscripts <> 0) and
not((RW[Player].Tech[adConscription] >= tsApplicable) and
(RW[Player].Model[NewProject and cpIndex].Domain = dGround)
and (RW[Player].Model[NewProject and cpIndex].Kind < mkScout))
then
result := eViolation
// else if (RW[Player].Model[NewProject and cpIndex].Kind=mkSlaves)
// and (GWonder[woPyramids].EffectiveOwner<>Player) then
// result:=eNoPreq
end
else if NewProject and cpIndex >= nImp then
result := eInvalid
else
begin
Preq := Imp[NewProject and cpIndex].Preq;
for i := 0 to nImpReplacement - 1 do
if (ImpReplacement[i].OldImp = NewProject and cpIndex) and
(built[ImpReplacement[i].NewImp] > 0) then
result := eObsolete;
if result = eObsolete then
else if Preq = preNA then
result := eInvalid
else if (Preq >= 0) and (RW[Player].Tech[Preq] < tsApplicable)
then
result := eNoPreq
else if built[NewProject and cpIndex] > 0 then
result := eInvalid
else if (NewProject and cpIndex < nWonder) and
(GWonder[NewProject and cpIndex].CityID <> WonderNotBuiltYet) then
result := eViolation // wonder already exists
else if (NewProject and cpIndex = imSpacePort) and
(RW[Player].NatBuilt[imSpacePort] > 0) then
result := eViolation // space port already exists
else if (NewProject = cpImp + imBank) and (built[imMarket] = 0)
or (NewProject = cpImp + imUniversity) and
(built[imLibrary] = 0) or (NewProject = cpImp + imResLab) and
(built[imUniversity] = 0) or (NewProject = cpImp + imMfgPlant)
and (built[imFactory] = 0) then
result := eNoPreq;
case NewProject - cpImp of
woLighthouse, woMagellan, imCoastalFort, imHarbor, imPlatform:
begin { city at ocean? }
Preq := 0;
V8_to_Loc(Loc, Adjacent);
for V8 := 0 to 7 do
begin
Loc1 := Adjacent[V8];
if (Loc1 >= 0) and (Loc1 < MapSize) and
(RealMap[Loc1] and fTerrain = fShore) then
inc(Preq);
end;
if Preq = 0 then
result := eNoPreq;
end;
woHoover, imHydro:
begin { city at river or mountains? }
Preq := 0;
V8_to_Loc(Loc, Adjacent);
for V8 := 0 to 7 do
begin
Loc1 := Adjacent[V8];
if (Loc1 >= 0) and (Loc1 < MapSize) and
((RealMap[Loc1] and fTerrain = fMountains) or
(RealMap[Loc1] and fRiver <> 0)) then
inc(Preq);
end;
if Preq = 0 then
result := eNoPreq;
end;
woMIR, imShipComp, imShipPow, imShipHab:
if RW[Player].NatBuilt[imSpacePort] = 0 then
result := eNoPreq;
end;
if (GTestFlags and tfNoRareNeed = 0) and
(Imp[NewProject and cpIndex].Kind = ikShipPart) then
if RW[Player].Tech[adMassProduction] < tsApplicable then
result := eNoPreq
else
begin // check for rare resources
if NewProject and cpIndex = imShipComp then
j := 1
else if NewProject and cpIndex = imShipPow then
j := 2
else { if NewProject and cpIndex=imShipHab then }
j := 3;
// j = rare resource required
Preq := 0;
V21_to_Loc(Loc, Radius);
for V21 := 1 to 26 do
begin
Loc1 := Radius[V21];
if (Loc1 >= 0) and (Loc1 < MapSize) and
(RealMap[Loc1] shr 25 and 3 = Cardinal(j)) then
inc(Preq);
end;
if Preq = 0 then
result := eNoPreq;
end;
end;
if (Command >= sExecute) and (result >= rExecuted) then
begin
if pt0 <> ptSelect then
if NewProject and (cpImp or cpIndex) = Project0 and
(cpImp or cpIndex) then
Prod := Prod0
else if (pt1 = ptTrGoods) or (pt1 = ptShip) or (pt1 <> pt0)
and (pt0 <> ptCaravan) then
begin
inc(RW[Player].Money, Prod0);
Prod := 0;
Prod0 := 0;
Project0 := cpImp + imTrGoods;
end
else
Prod := Prod0 * 2 div 3;
Project := NewProject
end;
end;
end;
end;
sBuyCityProject, sBuyCityProject - sExecute:
begin
{$IFDEF TEXTLOG}CmdInfo := Format('BuyCityProject P%d Loc%d', [Player, RW[Player].City[Subject].Loc]); {$ENDIF}
if (Subject >= RW[Player].nCity) or (RW[Player].City[Subject].Loc < 0)
then
result := eInvalid
else
with RW[Player].City[Subject] do
if (RW[Player].Government = gAnarchy) or (Flags and chCaptured <> 0)
then
result := eOutOfControl
else if (Project and cpImp <> 0) and
((Project and cpIndex = imTrGoods) or
(Imp[Project and cpIndex].Kind = ikShipPart)) then
result := eInvalid // don't buy colony ship
else
begin
CityReport.HypoTiles := -1;
CityReport.HypoTax := -1;
CityReport.HypoLux := -1;
GetCityReport(Player, Subject, CityReport);
Cost := CityReport.ProdCost;
NextProd := CityReport.ProdRep - CityReport.Support;
if (CityReport.Working - CityReport.Happy > Size shr 1) or
(NextProd < 0) then // !!! change to new style disorder
NextProd := 0;
Cost := Cost - Prod - NextProd;
if (GWonder[woMich].EffectiveOwner = Player) and
(Project and cpImp <> 0) then
Cost := Cost * 2
else
Cost := Cost * 4;
if Cost <= 0 then
result := eNotChanged
else if Cost > RW[Player].Money then
result := eViolation
else if Command >= sExecute then
IntServer(sIntBuyMaterial, Player, Subject, Cost);
// need to save material/cost because city tiles are not correct
// when loading
end;
end;
sSellCityProject, sSellCityProject - sExecute:
begin
{$IFDEF TEXTLOG}CmdInfo := Format('SellCityProject P%d Loc%d', [Player, RW[Player].City[Subject].Loc]); {$ENDIF}
if (Subject >= RW[Player].nCity) or (RW[Player].City[Subject].Loc < 0)
then
result := eInvalid
else if Command >= sExecute then
with RW[Player].City[Subject] do
begin
inc(RW[Player].Money, Prod0);
Prod := 0;
Prod0 := 0;
end;
end;
sSellCityImprovement, sSellCityImprovement - sExecute:
begin
{$IFDEF TEXTLOG}CmdInfo := Format('SellCityImprovement P%d Loc%d: %d', [Player, RW[Player].City[Subject].Loc, integer(Data)]); {$ENDIF}
if (Subject >= RW[Player].nCity) or (RW[Player].City[Subject].Loc < 0)
then
result := eInvalid
else
with RW[Player].City[Subject] do
if built[integer(Data)] = 0 then
result := eInvalid
else if (RW[Player].Government = gAnarchy) or
(Flags and chCaptured <> 0) then
result := eOutOfControl
else if Flags and chImprovementSold <> 0 then
result := eOnlyOnce
else if Command >= sExecute then
begin
inc(RW[Player].Money, Imp[integer(Data)].Cost * BuildCostMod
[Difficulty[Player]] div 12);
built[integer(Data)] := 0;
if Imp[integer(Data)].Kind in [ikNatLocal, ikNatGlobal] then
begin
RW[Player].NatBuilt[integer(Data)] := 0;
case integer(Data) of
imGrWall:
GrWallContinent[Player] := -1;
imSpacePort:
DestroySpacePort_TellPlayers(Player, -1);
end;
end;
inc(Flags, chImprovementSold);
end;
end;
sRebuildCityImprovement, sRebuildCityImprovement - sExecute:
begin
OldImp := integer(Data);
{$IFDEF TEXTLOG}CmdInfo := Format('RebuildCityImprovement P%d Loc%d: %d', [Player, RW[Player].City[Subject].Loc, OldImp]); {$ENDIF}
if (Subject >= RW[Player].nCity) or (RW[Player].City[Subject].Loc < 0)
then
result := eInvalid
else
begin
if (OldImp < 0) or (OldImp >= nImp) or
not(Imp[OldImp].Kind in [ikCommon, ikNatLocal, ikNatGlobal]) then
result := eInvalid
else
with RW[Player].City[Subject] do
if (built[OldImp] = 0) or (Project and cpImp = 0) or
not(Imp[Project and cpIndex].Kind in [ikCommon, ikNatLocal,
ikNatGlobal]) then
result := eInvalid
else if (RW[Player].Government = gAnarchy) or
(Flags and chCaptured <> 0) then
result := eOutOfControl
else if Flags and chImprovementSold <> 0 then
result := eOnlyOnce
else if Command >= sExecute then
begin
inc(Prod, Imp[OldImp].Cost * BuildCostMod[Difficulty[Player]]
div 12 * 2 div 3);
Project0 := Project0 and not cpCompleted;
if Project0 and not cpAuto <> Project and not cpAuto then
Project0 := Project;
Prod0 := Prod;
built[OldImp] := 0;
if Imp[OldImp].Kind in [ikNatLocal, ikNatGlobal] then
begin // nat. project lost
RW[Player].NatBuilt[OldImp] := 0;
case OldImp of
imGrWall:
GrWallContinent[Player] := -1;
imSpacePort:
DestroySpacePort_TellPlayers(Player, -1);
end;
end;
inc(Flags, chImprovementSold);
end;
end;
end;
sSetCityTiles, sSetCityTiles - sExecute:
begin
{$IFDEF TEXTLOG}CmdInfo := Format('SetCityTiles P%d Loc%d: %x', [Player, RW[Player].City[Subject].Loc, integer(Data)]); {$ENDIF}
if (Subject >= RW[Player].nCity) or (RW[Player].City[Subject].Loc < 0)
then
result := eInvalid
else
result := SetCityTiles(Player, Subject, integer(Data),
Command < sExecute);
end;
{
Client Exclusive Commands
____________________________________________________________________
}
else
if Command >= cClientEx then
begin
{$IFDEF TEXTLOG}CmdInfo := Format('ClientEx%x P%d', [Command, Player]);
{$ENDIF}
if ProcessClientData[Player] or (Mode = moPlaying) then
CallPlayer(Command, Player, Data)
end
else
result := eUnknown;
end; { case command }
// do not log invalid and non-relevant commands
if result = eZOC_EnemySpotted then
begin
assert(Mode = moPlaying);
CL.State := FormerCLState;
IntServer(sIntDiscoverZOC, Player, 0, ZOCTile);
end
else if result and rEffective = 0 then
if Mode < moPlaying then
begin
{$IFDEF TEXTLOG}CmdInfo := Format('***ERROR (%x) ', [result]) + CmdInfo;
{$ENDIF}
LoadOK := false;
end
else
begin
if logged then
CL.State := FormerCLState;
if (result < rExecuted) and (Command >= sExecute) then
PutMessage(1 shl 16 + 1, Format('INVALID: %d calls %x (%d)',
[Player, Command, Subject]));
end;
if (Command and (cClientEx or sExecute or sctMask) = sExecute or sctEndClient)
and (result >= rExecuted) then
LastEndClientCommand := Command;
{$IFOPT O-}dec(nHandoverStack, 2); {$ENDIF}
end;
initialization
FindFirst(ParamStr(0), $21, ExeInfo);
FindClose(ExeInfo);
{$IFOPT O-}nHandoverStack := 0; {$ENDIF}
end.
|