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
|
/**********************************************************************
Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
***********************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fcintl.h"
#include "log.h"
#include "mem.h"
#include "rand.h"
#include "registry.h"
#include "shared.h"
#include "support.h"
#include "capability.h"
#include "city.h"
#include "game.h"
#include "government.h"
#include "idex.h"
#include "map.h"
#include "movement.h"
#include "unit.h"
#include "unitlist.h"
#include "version.h"
#include "aicity.h"
#include "aidata.h"
#include "aiunit.h"
#include "script.h"
#include "barbarian.h"
#include "citytools.h"
#include "cityturn.h"
#include "diplhand.h"
#include "mapgen.h"
#include "maphand.h"
#include "meta.h"
#include "plrhand.h"
#include "ruleset.h"
#include "savegame.h"
#include "score.h"
#include "spacerace.h"
#include "srv_main.h"
#include "stdinhand.h"
#include "techtools.h"
#include "unittools.h"
#define TOKEN_SIZE 10
/*
* This loops over the entire map to save data. It collects all the data of
* a line using GET_XY_CHAR and then executes the macro SECFILE_INSERT_LINE.
* Internal variables map_x, map_y, nat_x, nat_y, and line are allocated
* within the macro but definable by the caller of the macro.
*
* Parameters:
* line: buffer variable to hold a line of chars
* map_x, map_y: variables for internal map coordinates
* nat_x, nat_y: variables for output/native coordinates
* GET_XY_CHAR: macro returning the map character for each position
* SECFILE_INSERT_LINE: macro to output each processed line (line nat_y)
*
* Note: don't use this macro DIRECTLY, use
* SAVE_NORMAL_MAP_DATA or SAVE_PLAYER_MAP_DATA instead.
*/
#define SAVE_MAP_DATA(ptile, line, \
GET_XY_CHAR, SECFILE_INSERT_LINE) \
{ \
char line[map.xsize + 1]; \
int _nat_x, _nat_y; \
\
for (_nat_y = 0; _nat_y < map.ysize; _nat_y++) { \
for (_nat_x = 0; _nat_x < map.xsize; _nat_x++) { \
struct tile *ptile = native_pos_to_tile(_nat_x, _nat_y); \
assert(ptile != NULL); \
line[_nat_x] = (GET_XY_CHAR); \
if (!my_isprint(line[_nat_x] & 0x7f)) { \
die("Trying to write invalid map " \
"data: '%c' %d", line[_nat_x], line[_nat_x]); \
} \
} \
line[map.xsize] = '\0'; \
(SECFILE_INSERT_LINE); \
} \
}
/*
* Wrappers for SAVE_MAP_DATA.
*
* SAVE_NORMAL_MAP_DATA saves a standard line of map data.
*
* SAVE_PLAYER_MAP_DATA saves a line of map data from a playermap.
*/
#define SAVE_NORMAL_MAP_DATA(ptile, secfile, secname, GET_XY_CHAR) \
SAVE_MAP_DATA(ptile, _line, GET_XY_CHAR, \
secfile_insert_str(secfile, _line, secname, _nat_y))
#define SAVE_PLAYER_MAP_DATA(ptile, secfile, secname, plrno, \
GET_XY_CHAR) \
SAVE_MAP_DATA(ptile, _line, GET_XY_CHAR, \
secfile_insert_str(secfile, _line, secname, plrno, _nat_y))
/*
* This loops over the entire map to load data. It inputs a line of data
* using the macro SECFILE_LOOKUP_LINE and then loops using the macro
* SET_XY_CHAR to load each char into the map at (map_x, map_y). Internal
* variables ch, map_x, map_y, nat_x, and nat_y are allocated within the
* macro but definable by the caller.
*
* Parameters:
* ch: a variable to hold a char (data for a single position)
* map_x, map_y: variables for internal map coordinates
* nat_x, nat_y: variables for output/native coordinates
* SET_XY_CHAR: macro to load the map character at each (map_x, map_y)
* SECFILE_LOOKUP_LINE: macro to input the nat_y line for processing
*
* Note: some (but not all) of the code this is replacing used to
* skip over lines that did not exist. This allowed for
* backward-compatibility. We could add another parameter that
* specified whether it was OK to skip the data, but there's not
* really much advantage to exiting early in this case. Instead,
* we let any map data type to be empty, and just print an
* informative warning message about it.
*/
#define LOAD_MAP_DATA(ch, nat_y, ptile, \
SECFILE_LOOKUP_LINE, SET_XY_CHAR) \
{ \
int _nat_x, _nat_y; \
bool _printed_warning = FALSE; \
for (_nat_y = 0; _nat_y < map.ysize; _nat_y++) { \
const int nat_y = _nat_y; \
const char *_line = (SECFILE_LOOKUP_LINE); \
if (NULL == _line) { \
freelog(LOG_VERBOSE, "Line not found='%s'", \
#SECFILE_LOOKUP_LINE); \
_printed_warning = TRUE; \
continue; \
} else if (strlen(_line) != map.xsize) { \
freelog(LOG_VERBOSE, "Line too short (expected %d got %lu)='%s'", \
map.xsize, (unsigned long) strlen(_line), \
#SECFILE_LOOKUP_LINE); \
_printed_warning = TRUE; \
continue; \
} \
for (_nat_x = 0; _nat_x < map.xsize; _nat_x++) { \
const char ch = _line[_nat_x]; \
struct tile *ptile = native_pos_to_tile(_nat_x, _nat_y); \
\
(SET_XY_CHAR); \
} \
} \
if (_printed_warning) { \
/* TRANS: Minor error message. */ \
freelog(LOG_ERROR, \
_("Saved game contains incomplete map data. This can" \
" happen with old saved games, or it may indicate an" \
" invalid saved game file. Proceed at your own risk.")); \
} \
}
/* Iterate on the specials half-bytes */
#define special_halfbyte_iterate(s) \
{ \
enum tile_special_type s; \
\
for(s = 0; 4 * s < S_LAST; s++) {
#define special_halfbyte_iterate_end \
} \
}
/* The following should be removed when compatibility with
pre-1.13.0 savegames is broken: startoptions, spacerace2
and rulesets */
#define SAVEFILE_OPTIONS "startoptions spacerace2 rulesets" \
" diplchance_percent map_editor known32fix turn " \
"attributes watchtower rulesetdir client_worklists orders " \
"startunits turn_last_built improvement_order technology_order embassies " \
"new_owner_map resources"
static const char hex_chars[] = "0123456789abcdef";
static void set_savegame_special(bv_special *specials,
char ch, const enum tile_special_type *index);
/***************************************************************
This returns an ascii hex value of the given half-byte of the binary
integer. See ascii_hex2bin().
example: bin2ascii_hex(0xa00, 2) == 'a'
***************************************************************/
#define bin2ascii_hex(value, halfbyte_wanted) \
hex_chars[((value) >> ((halfbyte_wanted) * 4)) & 0xf]
/***************************************************************
This returns a binary integer value of the ascii hex char, offset by
the given number of half-bytes. See bin2ascii_hex().
example: ascii_hex2bin('a', 2) == 0xa00
This is only used in loading games, and it requires some error
checking so it's done as a function.
***************************************************************/
static int ascii_hex2bin(char ch, int halfbyte)
{
char *pch;
if (ch == ' ') {
/*
* Sane value. It is unknow if there are savegames out there which
* need this fix. Savegame.c doesn't write such savegames
* (anymore) since the inclusion into CVS (2000-08-25).
*/
return 0;
}
pch = strchr(hex_chars, ch);
if (!pch || ch == '\0') {
die("Unknown hex value: '%c' %d", ch, ch);
}
return (pch - hex_chars) << (halfbyte * 4);
}
/****************************************************************************
Dereferences the terrain character. See terrains[].identifier
example: char2terrain('a') => T_ARCTIC
****************************************************************************/
static struct terrain *char2terrain(char ch)
{
/* terrain_by_identifier plus fatal error */
if (ch == UNKNOWN_TERRAIN_IDENTIFIER) {
return T_UNKNOWN;
}
terrain_type_iterate(pterrain) {
if (pterrain->identifier == ch) {
return pterrain;
}
} terrain_type_iterate_end;
freelog(LOG_FATAL, "Unknown terrain identifier '%c' in savegame.", ch);
exit(EXIT_FAILURE);
}
/****************************************************************************
References the terrain character. See terrains[].identifier
example: terrain2char(T_ARCTIC) => 'a'
****************************************************************************/
static char terrain2char(const struct terrain *pterrain)
{
if (pterrain == T_UNKNOWN) {
return UNKNOWN_TERRAIN_IDENTIFIER;
} else {
return pterrain->identifier;
}
}
/****************************************************************************
Returns an order for a character identifier. See also order2char.
****************************************************************************/
static enum unit_orders char2order(char order)
{
switch (order) {
case 'm':
case 'M':
return ORDER_MOVE;
case 'w':
case 'W':
return ORDER_FULL_MP;
case 'b':
case 'B':
return ORDER_BUILD_CITY;
case 'a':
case 'A':
return ORDER_ACTIVITY;
case 'd':
case 'D':
return ORDER_DISBAND;
case 'u':
case 'U':
return ORDER_BUILD_WONDER;
case 't':
case 'T':
return ORDER_TRADEROUTE;
case 'h':
case 'H':
return ORDER_HOMECITY;
}
/* This can happen if the savegame is invalid. */
return ORDER_LAST;
}
/****************************************************************************
Returns a character identifier for an order. See also char2order.
****************************************************************************/
static char order2char(enum unit_orders order)
{
switch (order) {
case ORDER_MOVE:
return 'm';
case ORDER_FULL_MP:
return 'w';
case ORDER_ACTIVITY:
return 'a';
case ORDER_BUILD_CITY:
return 'b';
case ORDER_DISBAND:
return 'd';
case ORDER_BUILD_WONDER:
return 'u';
case ORDER_TRADEROUTE:
return 't';
case ORDER_HOMECITY:
return 'h';
case ORDER_LAST:
break;
}
assert(0);
return '?';
}
/****************************************************************************
Returns a direction for a character identifier. See also dir2char.
****************************************************************************/
static enum direction8 char2dir(char dir)
{
/* Numberpad values for the directions. */
switch (dir) {
case '1':
return DIR8_SOUTHWEST;
case '2':
return DIR8_SOUTH;
case '3':
return DIR8_SOUTHEAST;
case '4':
return DIR8_WEST;
case '6':
return DIR8_EAST;
case '7':
return DIR8_NORTHWEST;
case '8':
return DIR8_NORTH;
case '9':
return DIR8_NORTHEAST;
}
/* This can happen if the savegame is invalid. */
return DIR8_LAST;
}
/****************************************************************************
Returns a character identifier for a direction. See also char2dir.
****************************************************************************/
static char dir2char(enum direction8 dir)
{
/* Numberpad values for the directions. */
switch (dir) {
case DIR8_NORTH:
return '8';
case DIR8_SOUTH:
return '2';
case DIR8_EAST:
return '6';
case DIR8_WEST:
return '4';
case DIR8_NORTHEAST:
return '9';
case DIR8_NORTHWEST:
return '7';
case DIR8_SOUTHEAST:
return '3';
case DIR8_SOUTHWEST:
return '1';
}
assert(0);
return '?';
}
/****************************************************************************
Returns a character identifier for an activity. See also char2activity.
****************************************************************************/
static char activity2char(enum unit_activity activity)
{
switch (activity) {
case ACTIVITY_IDLE:
return 'w';
case ACTIVITY_POLLUTION:
return 'p';
case ACTIVITY_ROAD:
return 'r';
case ACTIVITY_MINE:
return 'm';
case ACTIVITY_IRRIGATE:
return 'i';
case ACTIVITY_FORTIFIED:
return 'f';
case ACTIVITY_FORTRESS:
return 't';
case ACTIVITY_SENTRY:
return 's';
case ACTIVITY_RAILROAD:
return 'l';
case ACTIVITY_PILLAGE:
return 'e';
case ACTIVITY_GOTO:
return 'g';
case ACTIVITY_EXPLORE:
return 'x';
case ACTIVITY_TRANSFORM:
return 'o';
case ACTIVITY_AIRBASE:
return 'a';
case ACTIVITY_FORTIFYING:
return 'y';
case ACTIVITY_FALLOUT:
return 'u';
case ACTIVITY_UNKNOWN:
case ACTIVITY_PATROL_UNUSED:
return '?';
case ACTIVITY_LAST:
break;
}
assert(0);
return '?';
}
/****************************************************************************
Returns an activity for a character identifier. See also activity2char.
****************************************************************************/
static enum unit_activity char2activity(char activity)
{
enum unit_activity a;
for (a = 0; a < ACTIVITY_LAST; a++) {
char achar = activity2char(a);
if (activity == achar || activity == my_toupper(achar)) {
return a;
}
}
/* This can happen if the savegame is invalid. */
return ACTIVITY_LAST;
}
/***************************************************************
Quote the memory block denoted by data and length so it consists only
of " a-f0-9:". The returned string has to be freed by the caller using
free().
***************************************************************/
static char *quote_block(const void *const data, int length)
{
char *buffer = fc_malloc(length * 3 + 10);
size_t offset;
int i;
sprintf(buffer, "%d:", length);
offset = strlen(buffer);
for (i = 0; i < length; i++) {
sprintf(buffer + offset, "%02x ", ((unsigned char *) data)[i]);
offset += 3;
}
return buffer;
}
/***************************************************************
Unquote a string. The unquoted data is written into dest. If the
unqoted data will be largern than dest_length the function aborts. It
returns the actual length of the unquoted block.
***************************************************************/
static int unquote_block(const char *const quoted_, void *dest,
int dest_length)
{
int i, length, parsed, tmp;
char *endptr;
const char *quoted = quoted_;
parsed = sscanf(quoted, "%d", &length);
assert(parsed == 1);
assert(length <= dest_length);
quoted = strchr(quoted, ':');
assert(quoted != NULL);
quoted++;
for (i = 0; i < length; i++) {
tmp = strtol(quoted, &endptr, 16);
assert((endptr - quoted) == 2);
assert(*endptr == ' ');
assert((tmp & 0xff) == tmp);
((unsigned char *) dest)[i] = tmp;
quoted += 3;
}
return length;
}
/***************************************************************
load starting positions for the players from a savegame file
Now we don't know how many start positions there are nor how many
should be because rulesets are loaded later. So try to load as
many as they are; there should be at least enough for every
player. This could be changed/improved in future.
***************************************************************/
static void map_startpos_load(struct section_file *file)
{
int savegame_start_positions;
int i, j;
int nat_x, nat_y;
for (savegame_start_positions = 0;
secfile_lookup_int_default(file, -1, "map.r%dsx",
savegame_start_positions) != -1;
savegame_start_positions++) {
/* Nothing. */
}
{
struct start_position start_positions[savegame_start_positions];
for (i = j = 0; i < savegame_start_positions; i++) {
char *nation_name = secfile_lookup_str_default(file, NULL,
"map.r%dsnation", i);
struct nation_type *pnation;
if (!nation_name) {
/* Starting positions in normal games are saved without nation.
Just ignore it */
continue;
}
pnation = find_nation_by_rule_name(nation_name);
if (pnation == NO_NATION_SELECTED) {
freelog(LOG_ERROR,
"Warning: Unknown nation %s for starting position %d",
nation_name, i);
continue;
}
nat_x = secfile_lookup_int(file, "map.r%dsx", i);
nat_y = secfile_lookup_int(file, "map.r%dsy", i);
start_positions[j].tile = native_pos_to_tile(nat_x, nat_y);
start_positions[j].nation = pnation;
j++;
}
map.num_start_positions = j;
if (map.num_start_positions > 0) {
map.start_positions = fc_realloc(map.start_positions,
map.num_start_positions
* sizeof(*map.start_positions));
for (i = 0; i < j; i++) {
map.start_positions[i] = start_positions[i];
}
}
}
if (map.num_start_positions
&& map.num_start_positions < game.info.max_players) {
freelog(LOG_VERBOSE,
"Number of starts (%d) are lower than rules.max_players (%d),"
" lowering rules.max_players.",
map.num_start_positions, game.info.max_players);
game.info.max_players = map.num_start_positions;
}
}
/***************************************************************
load the tile map from a savegame file
***************************************************************/
static void map_tiles_load(struct section_file *file)
{
map.topology_id = secfile_lookup_int_default(file, MAP_ORIGINAL_TOPO,
"map.topology_id");
/* In some cases we read these before, but not always, and
* its safe to read them again:
*/
map.xsize=secfile_lookup_int(file, "map.width");
map.ysize=secfile_lookup_int(file, "map.height");
/* With a FALSE parameter [xy]size are not changed by this call. */
map_init_topology(FALSE);
map_allocate();
/* get the terrain type */
LOAD_MAP_DATA(ch, line, ptile,
secfile_lookup_str(file, "map.t%03d", line),
ptile->terrain = char2terrain(ch));
assign_continent_numbers(FALSE);
whole_map_iterate(ptile) {
ptile->spec_sprite = secfile_lookup_str_default(file, NULL,
"map.spec_sprite_%d_%d",
ptile->nat_x, ptile->nat_y);
if (ptile->spec_sprite) {
ptile->spec_sprite = mystrdup(ptile->spec_sprite);
}
} whole_map_iterate_end;
}
/* The order of the special bits, so that we can rebuild the
* bitvector from old savegames (where the order of the bits is
* unspecified). Here S_LAST means an unused bit (but see also resource
* recovery). */
static const enum tile_special_type default_specials[] = {
S_LAST, S_ROAD, S_IRRIGATION, S_RAILROAD,
S_MINE, S_POLLUTION, S_HUT, S_FORTRESS,
S_LAST, S_RIVER, S_FARMLAND, S_AIRBASE,
S_FALLOUT, S_LAST, S_LAST, S_LAST
};
/****************************************************************************
Load the rivers overlay map from a savegame file.
A scenario may define the terrain of the map but not list the specials on
it (thus allowing users to control the placement of specials). However
rivers are a special case and must be included in the map along with the
scenario. Thus in those cases this function should be called to load
the river information separate from any other special data.
This does not need to be called from map_load(), because map_load() loads
the rivers overlay along with the rest of the specials. Call this only
if you've already called map_tiles_load(), and want to load only the
rivers overlay but no other specials. Scenarios that encode things this
way should have the "riversoverlay" capability.
****************************************************************************/
static void map_rivers_overlay_load(struct section_file *file,
const enum tile_special_type *special_order)
{
/* used by set_savegame_special */
map.have_rivers_overlay = TRUE;
if (special_order) {
special_halfbyte_iterate(j) {
char buf[16]; /* enough for sprintf() below */
sprintf (buf, "map.spe%02d%%03d", j);
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, buf, nat_y),
set_savegame_special(&ptile->special, ch, special_order + 4 * j));
} special_halfbyte_iterate_end;
} else {
/* Get the bits of the special flags which contain the river special
and extract the rivers overlay from them. */
assert(S_LAST <= 32);
LOAD_MAP_DATA(ch, line, ptile,
secfile_lookup_str_default(file, NULL, "map.n%03d", line),
set_savegame_special(&ptile->special, ch, default_specials + 8));
}
}
/****************************************************************************
Complicated helper function for loading specials from a savegame.
'ch' gives the character loaded from the savegame. Specials are packed
in four to a character in hex notation. 'index' is a mapping of
savegame bit -> special bit. S_LAST is used to mark unused savegame bits.
****************************************************************************/
static void set_savegame_special(bv_special *specials,
char ch,
const enum tile_special_type *index)
{
int i, bin;
char *pch = strchr(hex_chars, ch);
if (!pch || ch == '\0') {
freelog(LOG_ERROR, "Unknown hex value: '%c' (%d)", ch, ch);
bin = 0;
} else {
bin = pch - hex_chars;
}
for (i = 0; i < 4; i++) {
enum tile_special_type sp = index[i];
if (sp >= S_LAST) {
continue;
}
if (map.have_rivers_overlay && sp != S_RIVER) {
continue;
}
if (bin & (1 << i)) {
set_special(specials, sp);
}
}
}
/****************************************************************************
Complicated helper function for saving specials into a savegame.
Specials are packed in four to a character in hex notation. 'index'
specifies which set of specials are included in this character.
****************************************************************************/
static char get_savegame_special(bv_special specials,
const enum tile_special_type *index)
{
int i, bin = 0;
for (i = 0; i < 4; i++) {
enum tile_special_type sp = index[i];
if (sp >= S_LAST) {
break;
}
if (contains_special(specials, sp)) {
bin |= (1 << i);
}
}
return hex_chars[bin];
}
/****************************************************************************
Complicated helper function for reading resources from a savegame.
This reads resources saved in the specials bitvector.
ch is the character read from the map, and n is the number of the special
(0 for special_1, 1 for special_2).
****************************************************************************/
static void set_savegame_old_resource(struct resource **r,
const struct terrain *terrain,
char ch, int n)
{
assert (n == 0 || n == 1);
/* If resource is already set to non-NULL or there is no resource found
* in this half-byte, then abort */
if (*r || !(ascii_hex2bin (ch, 0) & 0x1) || !terrain->resources[0]) {
return;
}
/* Note that we must handle the case of special_2 set when there is
* only one resource defined for the terrain (example, Shields on
* Grassland terrain where both resources are identical) */
if (n == 0 || !terrain->resources[1]) {
*r = terrain->resources[0];
} else {
*r = terrain->resources[1];
}
}
/****************************************************************************
Return the resource for the given identifier.
****************************************************************************/
static struct resource *identifier_to_resource(char c)
{
if (c == RESOURCE_NULL_IDENTIFIER) {
return NULL;
}
resource_type_iterate (r) {
if (r->identifier == c) {
return r;
}
} resource_type_iterate_end;
return NULL;
}
/****************************************************************************
Return the identifier for the given resource.
****************************************************************************/
static char resource_to_identifier(const struct resource *presource)
{
return presource ? presource->identifier : RESOURCE_NULL_IDENTIFIER;
}
/***************************************************************
load a complete map from a savegame file
***************************************************************/
static void map_load(struct section_file *file,
const enum tile_special_type *special_order)
{
char *savefile_options = secfile_lookup_str(file, "savefile.options");
/* map_init();
* This is already called in game_init(), and calling it
* here stomps on map.huts etc. --dwp
*/
map_tiles_load(file);
if (secfile_lookup_bool_default(file, TRUE, "game.save_starts")) {
map_startpos_load(file);
} else {
map.num_start_positions = 0;
}
if (special_order) {
/* First load the resources. This is straightforward. */
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, "map.res%03d", nat_y),
ptile->resource = identifier_to_resource(ch));
special_halfbyte_iterate(j) {
char buf[16]; /* enough for sprintf() below */
sprintf (buf, "map.spe%02d_%%03d", j);
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, buf, nat_y),
set_savegame_special(&ptile->special, ch, special_order + 4 * j));
} special_halfbyte_iterate_end;
} else {
/* get 4-bit segments of 16-bit "special" field. */
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, "map.l%03d", nat_y),
set_savegame_special(&ptile->special, ch, default_specials + 0));
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, "map.u%03d", nat_y),
set_savegame_special(&ptile->special, ch, default_specials + 4));
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str_default(file, NULL, "map.n%03d", nat_y),
set_savegame_special(&ptile->special, ch, default_specials + 8));
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str_default(file, NULL, "map.f%03d", nat_y),
set_savegame_special(&ptile->special, ch, default_specials + 12));
/* Setup resources (from half-bytes 1 and 3 of old savegames) */
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, "map.l%03d", nat_y),
set_savegame_old_resource(&ptile->resource, ptile->terrain, ch, 0));
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, "map.n%03d", nat_y),
set_savegame_old_resource(&ptile->resource, ptile->terrain, ch, 1));
}
/* after the resources are loaded, indicate those currently valid */
whole_map_iterate(ptile) {
if (NULL != ptile->terrain
&& NULL != ptile->resource
&& terrain_has_resource(ptile->terrain, ptile->resource)) {
/* cannot use set_special() for internal values */
BV_SET(ptile->special, S_RESOURCE_VALID);
}
} whole_map_iterate_end;
/* Owner and ownership source are stored as plain numbers */
if (has_capability("new_owner_map", savefile_options)) {
int x, y;
for (y = 0; y < map.ysize; y++) {
char *buffer =
secfile_lookup_str_default(file, NULL, "map.owner%03d", y);
char *ptr = buffer;
if (buffer == NULL) {
die("Savegame corrupt - map line %d not found.", y);
}
for (x = 0; x < map.xsize; x++) {
char token[TOKEN_SIZE];
int number;
struct tile *ptile = native_pos_to_tile(x, y);
scanin(&ptr, ",", token, sizeof(token));
if (token[0] == '\0') {
die("Savegame corrupt - map size not correct.");
}
if (strcmp(token, "-") == 0) {
ptile->owner = NULL;
} else {
if (sscanf(token, "%d", &number)) {
ptile->owner = get_player(number);
} else {
die("Savegame corrupt - got map owner %s in (%d, %d).",
token, x, y);
}
}
}
}
for (y = 0; y < map.ysize; y++) {
char *buffer2 =
secfile_lookup_str_default(file, NULL, "map.source%03d", y);
char *ptr2 = buffer2;
if (buffer2 == NULL) {
die("Savegame corrupt - map line %d not found.", y);
}
for (x = 0; x < map.xsize; x++) {
char token2[TOKEN_SIZE];
int number;
struct tile *ptile = native_pos_to_tile(x, y);
scanin(&ptr2, ",", token2, sizeof(token2));
if (token2[0] == '\0') {
die("Savegame corrupt - map size not correct.");
}
if (strcmp(token2, "-") == 0) {
ptile->owner_source = NULL;
} else {
if (sscanf(token2, "%d", &number)) {
ptile->owner_source = index_to_tile(number);
} else {
die("Savegame corrupt - got map source %s in (%d, %d).",
token2, x, y);
}
}
}
}
}
if (secfile_lookup_bool_default(file, TRUE, "game.save_known")) {
int known[MAP_INDEX_SIZE];
/* get 4-bit segments of the first half of the 32-bit "known" field */
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, "map.a%03d", nat_y),
known[ptile->index] = ascii_hex2bin(ch, 0));
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, "map.b%03d", nat_y),
known[ptile->index] |= ascii_hex2bin(ch, 1));
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, "map.c%03d", nat_y),
known[ptile->index] |= ascii_hex2bin(ch, 2));
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, "map.d%03d", nat_y),
known[ptile->index] |= ascii_hex2bin(ch, 3));
if (has_capability("known32fix", savefile_options)) {
/* get 4-bit segments of the second half of the 32-bit "known" field */
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, "map.e%03d", nat_y),
known[ptile->index] |= ascii_hex2bin(ch, 4));
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, "map.g%03d", nat_y),
known[ptile->index] |= ascii_hex2bin(ch, 5));
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, "map.h%03d", nat_y),
known[ptile->index] |= ascii_hex2bin(ch, 6));
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, "map.i%03d", nat_y),
known[ptile->index] |= ascii_hex2bin(ch, 7));
}
/* HACK: we read the known data from hex into a 32-bit integer, and
* now we convert it to bv_player. */
whole_map_iterate(ptile) {
BV_CLR_ALL(ptile->tile_known);
players_iterate(pplayer) {
if (known[ptile->index] & (1u << pplayer->player_no)) {
BV_SET(ptile->tile_known, pplayer->player_no);
}
} players_iterate_end;
} whole_map_iterate_end;
}
map.have_resources = TRUE;
}
/***************************************************************
...
***************************************************************/
static void map_save(struct section_file *file)
{
int i;
/* map.xsize and map.ysize (saved as map.width and map.height)
* are now always saved in game_save()
*/
/* Old freecivs expect map.is_earth to be present in the savegame. */
secfile_insert_bool(file, FALSE, "map.is_earth");
secfile_insert_bool(file, game.save_options.save_starts, "game.save_starts");
if (game.save_options.save_starts) {
for (i=0; i<map.num_start_positions; i++) {
struct tile *ptile = map.start_positions[i].tile;
secfile_insert_int(file, ptile->nat_x, "map.r%dsx", i);
secfile_insert_int(file, ptile->nat_y, "map.r%dsy", i);
if (map.start_positions[i].nation != NO_NATION_SELECTED) {
secfile_insert_str(file, nation_rule_name(map.start_positions[i].nation),
"map.r%dsnation", i);
}
}
}
whole_map_iterate(ptile) {
if (ptile->spec_sprite) {
secfile_insert_str(file, ptile->spec_sprite, "map.spec_sprite_%d_%d",
ptile->nat_x, ptile->nat_y);
}
} whole_map_iterate_end;
/* put the terrain type */
SAVE_NORMAL_MAP_DATA(ptile, file, "map.t%03d",
terrain2char(ptile->terrain));
if (!map.have_resources) {
if (map.have_rivers_overlay) {
/*
* Save the rivers overlay map; this is a special case to allow
* re-saving scenarios which have rivers overlay data. This only
* applies if don't have rest of specials.
*/
special_halfbyte_iterate(j) {
char buf[16]; /* enough for sprintf() below */
enum tile_special_type mod[4];
int l;
for (l = 0; l < 4; l++) {
mod[l] = (4 * j + l == S_RIVER) ? S_RIVER : S_LAST;
}
sprintf (buf, "map.spe%02d_%%03d", j);
SAVE_NORMAL_MAP_DATA(ptile, file, buf,
get_savegame_special(ptile->special, mod));
} special_halfbyte_iterate_end;
}
return;
}
SAVE_NORMAL_MAP_DATA(ptile, file, "map.res%03d",
resource_to_identifier(ptile->resource));
special_halfbyte_iterate(j) {
char buf[16]; /* enough for sprintf() below */
enum tile_special_type mod[4];
int l;
for (l = 0; l < 4; l++) {
mod[l] = MIN(4 * j + l, S_LAST);
}
sprintf (buf, "map.spe%02d_%%03d", j);
SAVE_NORMAL_MAP_DATA(ptile, file, buf,
get_savegame_special(ptile->special, mod));
} special_halfbyte_iterate_end;
/* Store owner and ownership source as plain numbers */
{
int x, y;
for (y = 0; y < map.ysize; y++) {
char line[map.xsize * TOKEN_SIZE];
line[0] = '\0';
for (x = 0; x < map.xsize; x++) {
char token[TOKEN_SIZE];
struct tile *ptile = native_pos_to_tile(x, y);
if (tile_owner(ptile) == NULL) {
strcpy(token, "-");
} else {
my_snprintf(token, sizeof(token), "%d", tile_owner(ptile)->player_no);
}
strcat(line, token);
if (x + 1 < map.xsize) {
strcat(line, ",");
}
}
secfile_insert_str(file, line, "map.owner%03d", y);
}
for (y = 0; y < map.ysize; y++) {
char line[map.xsize * TOKEN_SIZE];
line[0] = '\0';
for (x = 0; x < map.xsize; x++) {
char token[TOKEN_SIZE];
struct tile *ptile = native_pos_to_tile(x, y);
if (ptile->owner_source == NULL) {
strcpy(token, "-");
} else {
my_snprintf(token, sizeof(token), "%d", ptile->owner_source->index);
}
strcat(line, token);
if (x + 1 < map.xsize) {
strcat(line, ",");
}
}
secfile_insert_str(file, line, "map.source%03d", y);
}
}
secfile_insert_bool(file, game.save_options.save_known, "game.save_known");
if (game.save_options.save_known) {
int known[MAP_INDEX_SIZE];
/* HACK: we convert the data into a 32-bit integer, and then save it as
* hex. */
memset(known, 0, sizeof(known));
whole_map_iterate(ptile) {
players_iterate(pplayer) {
if (map_is_known(ptile, pplayer)) {
known[ptile->index] |= (1u << pplayer->player_no);
}
} players_iterate_end;
} whole_map_iterate_end;
/* put 4-bit segments of the 32-bit "known" field */
SAVE_NORMAL_MAP_DATA(ptile, file, "map.a%03d",
bin2ascii_hex(known[ptile->index], 0));
SAVE_NORMAL_MAP_DATA(ptile, file, "map.b%03d",
bin2ascii_hex(known[ptile->index], 1));
SAVE_NORMAL_MAP_DATA(ptile, file, "map.c%03d",
bin2ascii_hex(known[ptile->index], 2));
SAVE_NORMAL_MAP_DATA(ptile, file, "map.d%03d",
bin2ascii_hex(known[ptile->index], 3));
SAVE_NORMAL_MAP_DATA(ptile, file, "map.e%03d",
bin2ascii_hex(known[ptile->index], 4));
SAVE_NORMAL_MAP_DATA(ptile, file, "map.g%03d",
bin2ascii_hex(known[ptile->index], 5));
SAVE_NORMAL_MAP_DATA(ptile, file, "map.h%03d",
bin2ascii_hex(known[ptile->index], 6));
SAVE_NORMAL_MAP_DATA(ptile, file, "map.i%03d",
bin2ascii_hex(known[ptile->index], 7));
}
}
/*
* Previously (with 1.14.1 and earlier) units had their type saved by ID.
* This meant any time a unit was added (unless it was added at the end)
* savegame compatibility would be broken. Sometime after 1.14.1 this
* method was changed so the type is saved by name. However to preserve
* backwards compatibility we have here a list of unit names from before
* the change was made. When loading an old savegame (one that doesn't
* have the type string) we need to lookup the type into this array
* to get the "proper" type string. And when saving a new savegame we
* insert the "old" type index into the array so that old servers can
* load the savegame.
*
* Note that this list includes the AWACS, which was not in 1.14.1.
*/
/* old (~1.14.1) unit order in default/civ2/history ruleset */
static const char* old_default_unit_types[] = {
"Settlers", "Engineers", "Warriors", "Phalanx",
"Archers", "Legion", "Pikemen", "Musketeers",
"Fanatics", "Partisan", "Alpine Troops","Riflemen",
"Marines", "Paratroopers", "Mech. Inf.", "Horsemen",
"Chariot", "Elephants", "Crusaders", "Knights",
"Dragoons", "Cavalry", "Armor", "Catapult",
"Cannon", "Artillery", "Howitzer", "Fighter",
"Bomber", "Helicopter", "Stealth Fighter", "Stealth Bomber",
"Trireme", "Caravel", "Galleon", "Frigate",
"Ironclad", "Destroyer", "Cruiser", "AEGIS Cruiser",
"Battleship", "Submarine", "Carrier", "Transport",
"Cruise Missile", "Nuclear", "Diplomat", "Spy",
"Caravan", "Freight", "Explorer", "Barbarian Leader",
"AWACS"
};
/* old (~1.14.1) unit order in civ1 ruleset */
static const char* old_civ1_unit_types[] = {
"Settlers", "Engineers", "Militia", "Phalanx",
"Archers", "Legion", "Pikemen", "Musketeers",
"Fanatics", "Partisan", "Alpine Troops","Riflemen",
"Marines", "Paratroopers", "Mech. Inf.", "Cavalry",
"Chariot", "Elephants", "Crusaders", "Knights",
"Dragoons", "Civ2-Cavalry", "Armor", "Catapult",
"Cannon", "Civ2-Artillery","Artillery", "Fighter",
"Bomber", "Helicopter", "Stealth Fighter", "Stealth Bomber",
"Trireme", "Sail", "Galleon", "Frigate",
"Ironclad", "Destroyer", "Cruiser", "AEGIS Cruiser",
"Battleship", "Submarine", "Carrier", "Transport",
"Cruise Missile", "Nuclear", "Diplomat", "Spy",
"Caravan", "Freight", "Explorer", "Barbarian Leader"
};
/* old (1.14.1) improvement order in default ruleset */
const char* old_impr_types[] =
{
"Airport", "Aqueduct", "Bank",
"Barracks", "Barracks II", "Barracks III",
"Cathedral", "City Walls", "Coastal Defense",
"Colosseum", "Courthouse", "Factory",
"Granary", "Harbour", "Hydro Plant",
"Library", "Marketplace", "Mass Transit",
"Mfg. Plant", "Nuclear Plant", "Offshore Platform",
"Palace", "Police Station", "Port Facility",
"Power Plant", "Recycling Center", "Research Lab",
"SAM Battery", "SDI Defense", "Sewer System",
"Solar Plant", "Space Component", "Space Module",
"Space Structural", "Stock Exchange", "Super Highways",
"Supermarket", "Temple", "University",
"Apollo Program", "A.Smith's Trading Co.","Colossus",
"Copernicus' Observatory", "Cure For Cancer", "Darwin's Voyage",
"Eiffel Tower", "Great Library", "Great Wall",
"Hanging Gardens", "Hoover Dam", "Isaac Newton's College",
"J.S. Bach's Cathedral","King Richard's Crusade", "Leonardo's Workshop",
"Lighthouse", "Magellan's Expedition","Manhattan Project",
"Marco Polo's Embassy","Michelangelo's Chapel","Oracle",
"Pyramids", "SETI Program", "Shakespeare's Theatre",
"Statue of Liberty", "Sun Tzu's War Academy","United Nations",
"Women's Suffrage", "Coinage"
};
/* old (~1.14.1) techs order in civ2/default ruleset.
*
* Note that Theology is called Religion in civ1 ruleset; this is handled
* as a special case in the code.
*
* Nowadays we save A_FUTURE as "A_FUTURE", A_NONE as "A_NONE".
* A_UNSET as "A_UNSET" - they used to be saved as 198, 0 or -1, 0.
*/
const char* old_default_techs[] =
{
"A_NONE",
"Advanced Flight", "Alphabet", "Amphibious Warfare",
"Astronomy", "Atomic Theory", "Automobile",
"Banking", "Bridge Building", "Bronze Working",
"Ceremonial Burial", "Chemistry", "Chivalry",
"Code of Laws", "Combined Arms", "Combustion",
"Communism", "Computers", "Conscription",
"Construction", "Currency", "Democracy",
"Economics", "Electricity", "Electronics",
"Engineering", "Environmentalism", "Espionage",
"Explosives", "Feudalism", "Flight",
"Fundamentalism", "Fusion Power", "Genetic Engineering",
"Guerilla Warfare", "Gunpowder", "Horseback Riding",
"Industrialization", "Invention", "Iron Working",
"Labor Union", "Laser", "Leadership",
"Literacy", "Machine Tools", "Magnetism",
"Map Making", "Masonry", "Mass Production",
"Mathematics", "Medicine", "Metallurgy",
"Miniaturization", "Mobile Warfare", "Monarchy",
"Monotheism", "Mysticism", "Navigation",
"Nuclear Fission", "Nuclear Power", "Philosophy",
"Physics", "Plastics", "Polytheism",
"Pottery", "Radio", "Railroad",
"Recycling", "Refining", "Refrigeration",
"Robotics", "Rocketry", "Sanitation",
"Seafaring", "Space Flight", "Stealth",
"Steam Engine", "Steel", "Superconductors",
"Tactics", "The Corporation", "The Republic",
"The Wheel", "Theology", "Theory of Gravity",
"Trade", "University", "Warrior Code",
"Writing"
};
/* old (~1.14.1) government order in default, civ1, and history rulesets */
const char* old_default_governments[] =
{
"Anarchy", "Despotism", "Monarchy", "Communism", "Republic", "Democracy"
};
/* old (~1.14.1) government order in the civ2 ruleset */
const char* old_civ2_governments[] =
{
"Anarchy", "Despotism", "Monarchy", "Communism", "Fundamentalism",
"Republic", "Democracy"
};
/****************************************************************************
Nowadays unit types are saved by name, but old servers need the
unit_type_id. This function tries to find the correct _old_ id for the
unit's type. It is used when the unit is saved.
****************************************************************************/
static Unit_type_id old_unit_type_id(const struct unit_type *type)
{
const char** types;
int num_types, i;
if (strcmp(game.rulesetdir, "civ1") == 0) {
types = old_civ1_unit_types;
num_types = ARRAY_SIZE(old_civ1_unit_types);
} else {
types = old_default_unit_types;
num_types = ARRAY_SIZE(old_default_unit_types);
}
for (i = 0; i < num_types; i++) {
if (mystrcasecmp(utype_rule_name(type), types[i]) == 0) {
return i;
}
}
/* It's a new unit. Savegame cannot be forward compatible so we can
* return anything */
return type->index;
}
/****************************************************************************
Convert an old-style unit type id into a unit type name.
****************************************************************************/
static const char* old_unit_type_name(int id)
{
/* before 1.15.0 unit types used to be saved by id */
if (id < 0) {
freelog(LOG_FATAL, "Wrong unit type id value (%d)", id);
exit(EXIT_FAILURE);
}
/* Different rulesets had different unit names. */
if (strcmp(game.rulesetdir, "civ1") == 0) {
if (id >= ARRAY_SIZE(old_civ1_unit_types)) {
freelog(LOG_FATAL, "Wrong unit type id value (%d)", id);
exit(EXIT_FAILURE);
}
return old_civ1_unit_types[id];
} else {
if (id >= ARRAY_SIZE(old_default_unit_types)) {
freelog(LOG_FATAL, "Wrong unit type id value (%d)", id);
exit(EXIT_FAILURE);
}
return old_default_unit_types[id];
}
}
/****************************************************************************
Nowadays improvement types are saved by name, but old servers need the
Impr_type_id. This function tries to find the correct _old_ id for the
improvements's type. It is used when the improvement is saved.
****************************************************************************/
static int old_impr_type_id(Impr_type_id type)
{
int i;
for (i = 0; i < ARRAY_SIZE(old_impr_types); i++) {
if (mystrcasecmp(improvement_rule_name(type),
old_impr_types[i]) == 0) {
return i;
}
}
/* It's a new improvement. Savegame cannot be forward compatible so we can
* return anything */
return type;
}
/***************************************************************
Convert old-style improvement type id into improvement type name
***************************************************************/
static const char* old_impr_type_name(int id)
{
/* before 1.15.0 improvement types used to be saved by id */
if (id < 0 || id >= ARRAY_SIZE(old_impr_types)) {
freelog(LOG_FATAL, "Wrong improvement type id value (%d)", id);
exit(EXIT_FAILURE);
}
return old_impr_types[id];
}
/****************************************************************************
Initialize the old-style improvement bitvector so that all improvements
are marked as not present.
****************************************************************************/
static void init_old_improvement_bitvector(char* bitvector)
{
int i;
for (i = 0; i < ARRAY_SIZE(old_impr_types); i++) {
bitvector[i] = '0';
}
bitvector[ARRAY_SIZE(old_impr_types)] = '\0';
}
/****************************************************************************
Insert improvement into old-style bitvector
Improvement lists in cities and destroyed_wonders are saved as a
bitvector in a string array. New bitvectors do not depend on ruleset
order. However, we want to create savegames which can be read by
1.14.x and earlier servers. This function adds an improvement into the
bitvector string according to the 1.14.1 improvement ordering.
****************************************************************************/
static void add_improvement_into_old_bitvector(char* bitvector,
Impr_type_id id)
{
int old_id;
old_id = old_impr_type_id(id);
if (old_id < 0 || old_id >= ARRAY_SIZE(old_impr_types)) {
return;
}
bitvector[old_id] = '1';
}
/****************************************************************************
Nowadays techs are saved by name, but old servers need numbers
This function tries to find the correct _old_ id for the
technology. It is used when the technology is saved.
****************************************************************************/
static int old_tech_id(Tech_type_id tech)
{
const char* technology_name;
int i;
/* old (1.14.1) servers used to save it as 0 and interpret it from context */
if (is_future_tech(tech)) {
return 0;
}
/* old (1.14.1) servers used to save it as 0 and interpret it from context */
if (tech == A_UNSET) {
return 0;
}
technology_name = advance_rule_name(tech);
/* this is the only place where civ1 was different from 1.14.1 defaults */
if (strcmp(game.rulesetdir, "civ1") == 0
&& mystrcasecmp(technology_name, "Religion") == 0) {
return 83;
}
for (i = 0; i < ARRAY_SIZE(old_default_techs); i++) {
if (mystrcasecmp(technology_name, old_default_techs[i]) == 0) {
return i;
}
}
/* It's a new technology. Savegame cannot be forward compatible so we can
* return anything */
return tech;
}
/****************************************************************************
Convert an old-style technology id into a tech name.
****************************************************************************/
static const char* old_tech_name(int id)
{
/* This was 1.14.1 value for A_FUTURE */
if (id == 198) {
return "A_FUTURE";
}
if (id == -1 || id == 0) {
return "A_NONE";
}
if (id == A_UNSET) {
return "A_UNSET";
}
if (id < 0 || id >= ARRAY_SIZE(old_default_techs)) {
freelog(LOG_FATAL, "Wrong tech type id value (%d)", id);
exit(EXIT_FAILURE);
}
if (strcmp(game.rulesetdir, "civ1") == 0 && id == 83) {
return "Religion";
}
return old_default_techs[id];
}
/****************************************************************************
Initialize the old-style technology bitvector so that all advances
are marked as not present.
****************************************************************************/
static void init_old_technology_bitvector(char* bitvector)
{
int i;
for (i = 0; i < ARRAY_SIZE(old_default_techs); i++) {
bitvector[i] = '0';
}
bitvector[ARRAY_SIZE(old_default_techs)] = '\0';
}
/****************************************************************************
Insert technology into old-style bitvector
New bitvectors do not depend on ruleset order. However, we want to create
savegames which can be read by 1.14.x and earlier servers.
This function adds a technology into the bitvector string according
to the 1.14.1 technology ordering.
****************************************************************************/
static void add_technology_into_old_bitvector(char* bitvector,
Tech_type_id id)
{
int old_id;
old_id = old_tech_id(id);
if (old_id < 0 || old_id >= ARRAY_SIZE(old_default_techs)) {
return;
}
bitvector[old_id] = '1';
}
/*****************************************************************************
Load technology from path_name and if doesn't exist (because savegame
is too old) load from path.
*****************************************************************************/
static Tech_type_id load_technology(struct section_file *file,
const char* path, int plrno)
{
char path_with_name[128];
const char* name;
int id;
my_snprintf(path_with_name, sizeof(path_with_name),
"%s_name", path);
name = secfile_lookup_str_default(file, NULL, path_with_name, plrno);
if (!name) {
id = secfile_lookup_int_default(file, -1, path, plrno);
name = old_tech_name(id);
}
if (mystrcasecmp(name, "A_FUTURE") == 0) {
return A_FUTURE;
}
if (mystrcasecmp(name, "A_NONE") == 0) {
return A_NONE;
}
if (mystrcasecmp(name, "A_UNSET") == 0) {
return A_UNSET;
}
if (name[0] == '\0') {
/* it is used by changed_from */
return -1;
}
id = find_advance_by_rule_name(name);
if (id == A_LAST) {
freelog(LOG_FATAL, "Unknown technology \"%s\".", name);
exit(EXIT_FAILURE);
}
return id;
}
/*****************************************************************************
Save technology in secfile entry called path_name and for forward
compatibility in path(by number).
*****************************************************************************/
static void save_technology(struct section_file *file,
const char* path, int plrno, Tech_type_id tech)
{
char path_with_name[128];
const char* name;
my_snprintf(path_with_name, sizeof(path_with_name),
"%s_name", path);
switch (tech) {
case -1: /* used in changed_from */
name = "";
break;
case A_NONE:
name = "A_NONE";
break;
case A_UNSET:
name = "A_UNSET";
break;
case A_FUTURE:
name = "A_FUTURE";
break;
default:
name = advance_rule_name(tech);
break;
}
secfile_insert_str(file, name, path_with_name, plrno);
secfile_insert_int(file, old_tech_id(tech), path, plrno);
}
/****************************************************************************
Nowadays governments are saved by name, but old servers need the
index. This function tries to find the correct _old_ id for the
government. It is used when the government is saved.
****************************************************************************/
static int old_government_id(struct government *gov)
{
const char** names;
int num_names, i;
if (strcmp(game.rulesetdir, "civ2") == 0) {
names = old_civ2_governments;
num_names = ARRAY_SIZE(old_civ2_governments);
} else {
names = old_default_governments;
num_names = ARRAY_SIZE(old_default_governments);
}
for (i = 0; i < num_names; i++) {
if (mystrcasecmp(government_rule_name(gov), names[i]) == 0) {
return i;
}
}
/* It's a new government. Savegame cannot be forward compatible so we can
* return anything */
return gov->index;
}
/****************************************************************************
Convert an old-style government index into a government name.
****************************************************************************/
static const char* old_government_name(int id)
{
/* before 1.15.0 governments used to be saved by index */
if (id < 0) {
freelog(LOG_FATAL, "Wrong government type id value (%d)", id);
exit(EXIT_FAILURE);
}
/* Different rulesets had different governments. */
if (strcmp(game.rulesetdir, "civ2") == 0) {
if (id >= ARRAY_SIZE(old_civ2_governments)) {
freelog(LOG_FATAL, "Wrong government type id value (%d)", id);
exit(EXIT_FAILURE);
}
return old_civ2_governments[id];
} else {
if (id >= ARRAY_SIZE(old_default_governments)) {
freelog(LOG_FATAL, "Wrong government type id value (%d)", id);
exit(EXIT_FAILURE);
}
return old_default_governments[id];
}
}
/****************************************************************************
Loads the units for the given player.
****************************************************************************/
static void load_player_units(struct player *plr, int plrno,
struct section_file *file)
{
int nunits, i, j;
enum unit_activity activity;
char *savefile_options = secfile_lookup_str(file, "savefile.options");
plr->units = unit_list_new();
nunits = secfile_lookup_int(file, "player%d.nunits", plrno);
if (!plr->is_alive && nunits > 0) {
nunits = 0; /* Some old savegames may be buggy. */
}
for (i = 0; i < nunits; i++) {
struct unit *punit;
struct city *pcity;
int nat_x, nat_y;
const char* type_name;
struct unit_type *type;
type_name = secfile_lookup_str_default(file, NULL,
"player%d.u%d.type_by_name",
plrno, i);
if (!type_name) {
/* before 1.15.0 unit types used to be saved by id. */
int t = secfile_lookup_int(file, "player%d.u%d.type",
plrno, i);
if (t < 0) {
freelog(LOG_FATAL, "Wrong player%d.u%d.type value (%d)",
plrno, i, t);
exit(EXIT_FAILURE);
}
type_name = old_unit_type_name(t);
}
type = find_unit_type_by_rule_name(type_name);
if (!type) {
freelog(LOG_FATAL, "Unknown unit type '%s' in player%d section",
type_name, plrno);
exit(EXIT_FAILURE);
}
punit = create_unit_virtual(plr, NULL, type,
secfile_lookup_int(file, "player%d.u%d.veteran", plrno, i));
punit->id = secfile_lookup_int(file, "player%d.u%d.id", plrno, i);
alloc_id(punit->id);
idex_register_unit(punit);
nat_x = secfile_lookup_int(file, "player%d.u%d.x", plrno, i);
nat_y = secfile_lookup_int(file, "player%d.u%d.y", plrno, i);
punit->tile = native_pos_to_tile(nat_x, nat_y);
/* Avoid warning when loading pre-2.1 saves containing foul status */
secfile_lookup_bool_default(file, FALSE, "player%d.u%d.foul",
plrno, i);
punit->homecity = secfile_lookup_int(file, "player%d.u%d.homecity",
plrno, i);
if ((pcity = game_find_city_by_number(punit->homecity))) {
unit_list_prepend(pcity->units_supported, punit);
}
punit->moves_left
= secfile_lookup_int(file, "player%d.u%d.moves", plrno, i);
punit->fuel = secfile_lookup_int(file, "player%d.u%d.fuel", plrno, i);
activity = secfile_lookup_int(file, "player%d.u%d.activity", plrno, i);
if (activity == ACTIVITY_PATROL_UNUSED) {
/* Previously ACTIVITY_PATROL and ACTIVITY_GOTO were used for
* client-side goto. Now client-side goto is handled by setting
* a special flag, and units with orders generally have ACTIVITY_IDLE.
* Old orders are lost. Old client-side goto units will still have
* ACTIVITY_GOTO and will goto the correct position via server goto.
* Old client-side patrol units lose their patrol routes and are put
* into idle mode. */
activity = ACTIVITY_IDLE;
}
set_unit_activity(punit, activity);
/* need to do this to assign/deassign settlers correctly -- Syela
*
* was punit->activity=secfile_lookup_int(file,
* "player%d.u%d.activity",plrno, i); */
punit->activity_count = secfile_lookup_int(file,
"player%d.u%d.activity_count",
plrno, i);
punit->activity_target
= secfile_lookup_int_default(file, S_LAST,
"player%d.u%d.activity_target", plrno, i);
punit->done_moving = secfile_lookup_bool_default(file,
(punit->moves_left == 0), "player%d.u%d.done_moving", plrno, i);
punit->battlegroup
= secfile_lookup_int_default(file, BATTLEGROUP_NONE,
"player%d.u%d.battlegroup", plrno, i);
/* Load the goto information. Older savegames will not have the
* "go" field, so we just load the goto destination by default. */
if (secfile_lookup_bool_default(file, TRUE,
"player%d.u%d.go", plrno, i)) {
int nat_x = secfile_lookup_int(file, "player%d.u%d.goto_x", plrno, i);
int nat_y = secfile_lookup_int(file, "player%d.u%d.goto_y", plrno, i);
punit->goto_tile = native_pos_to_tile(nat_x, nat_y);
} else {
punit->goto_tile = NULL;
}
punit->ai.passenger
= secfile_lookup_int_default(file, 0, "player%d.u%d.passenger", plrno, i);
punit->ai.ferryboat
= secfile_lookup_int_default(file, 0, "player%d.u%d.ferryboat", plrno, i);
punit->ai.charge
= secfile_lookup_int_default(file, 0, "player%d.u%d.charge", plrno, i);
punit->ai.bodyguard
= secfile_lookup_int_default(file, 0, "player%d.u%d.bodyguard", plrno, i);
punit->ai.control
= secfile_lookup_bool(file, "player%d.u%d.ai", plrno, i);
punit->hp = secfile_lookup_int(file, "player%d.u%d.hp", plrno, i);
punit->ord_map
= secfile_lookup_int_default(file, 0,
"player%d.u%d.ord_map", plrno, i);
punit->ord_city
= secfile_lookup_int_default(file, 0,
"player%d.u%d.ord_city", plrno, i);
punit->moved
= secfile_lookup_bool_default(file, FALSE,
"player%d.u%d.moved", plrno, i);
punit->paradropped
= secfile_lookup_bool_default(file, FALSE,
"player%d.u%d.paradropped", plrno, i);
punit->transported_by
= secfile_lookup_int_default(file, -1, "player%d.u%d.transported_by",
plrno, i);
/* Initialize upkeep values: these are hopefully initialized
elsewhere before use (specifically, in city_support(); but
fixme: check whether always correctly initialized?).
Below is mainly for units which don't have homecity --
otherwise these don't get initialized (and AI calculations
etc may use junk values).
*/
/* load the unit orders */
if (has_capability("orders", savefile_options)) {
int len = secfile_lookup_int_default(file, 0,
"player%d.u%d.orders_length", plrno, i);
if (len > 0) {
char *orders_buf, *dir_buf, *act_buf;
punit->orders.list = fc_malloc(len * sizeof(*(punit->orders.list)));
punit->orders.length = len;
punit->orders.index = secfile_lookup_int_default(file, 0,
"player%d.u%d.orders_index", plrno, i);
punit->orders.repeat = secfile_lookup_bool_default(file, FALSE,
"player%d.u%d.orders_repeat", plrno, i);
punit->orders.vigilant = secfile_lookup_bool_default(file, FALSE,
"player%d.u%d.orders_vigilant", plrno, i);
orders_buf = secfile_lookup_str_default(file, "",
"player%d.u%d.orders_list", plrno, i);
dir_buf = secfile_lookup_str_default(file, "",
"player%d.u%d.dir_list", plrno, i);
act_buf = secfile_lookup_str_default(file, "",
"player%d.u%d.activity_list", plrno, i);
punit->has_orders = TRUE;
for (j = 0; j < len; j++) {
struct unit_order *order = &punit->orders.list[j];
if (orders_buf[j] == '\0' || dir_buf[j] == '\0'
|| act_buf[j] == '\0') {
freelog(LOG_ERROR, "Invalid unit orders.");
free_unit_orders(punit);
break;
}
order->order = char2order(orders_buf[j]);
order->dir = char2dir(dir_buf[j]);
order->activity = char2activity(act_buf[j]);
if (order->order == ORDER_LAST
|| (order->order == ORDER_MOVE && order->dir == DIR8_LAST)
|| (order->order == ORDER_ACTIVITY
&& order->activity == ACTIVITY_LAST)) {
/* An invalid order. Just drop the orders for this unit. */
free(punit->orders.list);
punit->orders.list = NULL;
punit->has_orders = FALSE;
break;
}
}
} else {
punit->has_orders = FALSE;
punit->orders.list = NULL;
}
} else {
/* Old-style goto routes get discarded. */
punit->has_orders = FALSE;
punit->orders.list = NULL;
}
/* allocate the unit's contribution to fog of war */
punit->server.vision = vision_new(unit_owner(punit), punit->tile, TRUE);
unit_refresh_vision(punit);
/* NOTE: There used to be some map_set_known calls here. These were
* unneeded since unfogging the tile when the unit sees it will
* automatically reveal that tile. */
unit_list_append(plr->units, punit);
unit_list_prepend(punit->tile->units, punit);
}
}
/****************************************************************************
Load all information about player "plrno" into the structure pointed to
by "plr".
****************************************************************************/
static void player_load(struct player *plr, int plrno,
struct section_file *file,
char** improvement_order,
int improvement_order_size,
char** technology_order,
int technology_order_size)
{
int i, j, k, x, y, ncities, c_s;
const char *p;
const char *name;
char *savefile_options = secfile_lookup_str(file, "savefile.options");
struct ai_data *ai;
struct government *gov;
int id;
struct team *pteam;
struct player_research *research;
struct nation_type *pnation;
/* not all players have teams */
id = secfile_lookup_int_default(file, -1, "player%d.team_no", plrno);
pteam = team_get_by_id(id);
if (pteam == NULL) {
pteam = find_empty_team();
}
team_add_player(plr, pteam);
research = get_player_research(plr);
server_player_init(plr, TRUE, FALSE);
ai = ai_data_get(plr);
plr->ai.barbarian_type = secfile_lookup_int_default(file, 0, "player%d.ai.is_barbarian",
plrno);
if (is_barbarian(plr)) game.info.nbarbarians++;
sz_strlcpy(plr->name, secfile_lookup_str(file, "player%d.name", plrno));
sz_strlcpy(plr->username,
secfile_lookup_str_default(file, "", "player%d.username", plrno));
sz_strlcpy(plr->ranked_username,
secfile_lookup_str_default(file, "", "player%d.ranked_username",
plrno));
/* 1.15 and later versions store nations by name. Try that first. */
p = secfile_lookup_str_default(file, NULL, "player%d.nation", plrno);
if (!p) {
/*
* Otherwise read as a pre-1.15 savefile with numeric nation indexes.
* This random-looking order is from the old nations/ruleset file.
* Use it to convert old-style nation indices to name strings.
* The idea is not to be dependent on the order in which nations
* get read into the registry.
*/
const char *name_order[] = {
"roman", "babylonian", "german", "egyptian", "american", "greek",
"indian", "russian", "zulu", "french", "aztec", "chinese", "english",
"mongol", "turk", "spanish", "persian", "arab", "carthaginian", "inca",
"viking", "polish", "hungarian", "danish", "dutch", "swedish",
"japanese", "portuguese", "finnish", "sioux", "czech", "australian",
"welsh", "korean", "scottish", "israeli", "argentine", "canadian",
"ukrainian", "lithuanian", "kenyan", "dunedain", "vietnamese", "thai",
"mordor", "bavarian", "brazilian", "irish", "cornish", "italian",
"filipino", "estonian", "latvian", "boer", "silesian", "singaporean",
"chilean", "catalan", "croatian", "slovenian", "serbian", "barbarian",
};
int index = secfile_lookup_int(file, "player%d.race", plrno);
if (index >= 0 && index < ARRAY_SIZE(name_order)) {
p = name_order[index];
} else {
p = "";
}
}
pnation = find_nation_by_rule_name(p);
if (pnation != NO_NATION_SELECTED) {
player_set_nation(plr, pnation);
} else {
plr->nation = NO_NATION_SELECTED;
}
/* Nation may be unselected at this point; we check for this later and
* reassign nations to players who don't have them. */
/* Add techs from game and nation, but ignore game.info.tech. */
init_tech(plr);
/* We used to call give_initial_techs here, but that shouldn't be
* necessary. The savegame should already mark those techs as known.
* give_initial_techs will crash if the nation is unset. */
if (is_barbarian(plr) && plr->nation == NO_NATION_SELECTED) {
pnation = pick_barbarian_nation();
player_set_nation(plr, pnation);
}
/* government */
name = secfile_lookup_str_default(file, NULL, "player%d.government_name",
plrno);
if (!name) {
/* old servers used to save government by id */
id = secfile_lookup_int(file, "player%d.government", plrno);
name = old_government_name(id);
}
gov = find_government_by_rule_name(name);
if (gov == NULL) {
freelog(LOG_FATAL, "Player%d: unsupported government \"%s\".",
plrno,
name);
exit(EXIT_FAILURE);
}
plr->government = gov;
/* Target government */
name = secfile_lookup_str_default(file, NULL,
"player%d.target_government_name",
plrno);
if (name) {
gov = find_government_by_rule_name(name);
} else {
gov = NULL;
}
if (gov) {
plr->target_government = gov;
} else {
/* Old servers didn't have this value. */
plr->target_government = government_of_player(plr);
}
BV_CLR_ALL(plr->embassy);
if (has_capability("embassies", savefile_options)) {
players_iterate(pother) {
if (secfile_lookup_bool(file, "player%d.embassy%d",
plrno, pother->player_no)) {
BV_SET(plr->embassy, pother->player_no);
}
} players_iterate_end;
} else {
/* Required for 2.0 and earlier savegames. Remove eventually and make
* the cap check mandatory. */
int embassy = secfile_lookup_int(file, "player%d.embassy", plrno);
players_iterate(pother) {
if (embassy & (1 << pother->player_no)) {
BV_SET(plr->embassy, pother->player_no);
}
} players_iterate_end;
}
p = secfile_lookup_str_default(file, NULL, "player%d.city_style_by_name",
plrno);
if (!p) {
char* old_order[4] = {"European", "Classical", "Tropical", "Asian"};
c_s = secfile_lookup_int_default(file, 0, "player%d.city_style", plrno);
if (c_s < 0 || c_s > 3) {
freelog(LOG_ERROR, "Player%d: unsupported city_style %d."
" Changed to \"%s\".",
plrno,
c_s,
old_order[0]);
c_s = 0;
}
p = old_order[c_s];
}
c_s = find_city_style_by_rule_name(p);
if (c_s < 0) {
freelog(LOG_ERROR, "Player%d: unsupported city_style_name \"%s\"."
" Changed to \"%s\".",
plrno,
p,
city_style_rule_name(0));
c_s = 0;
}
plr->city_style = c_s;
plr->nturns_idle=0;
plr->is_male=secfile_lookup_bool_default(file, TRUE, "player%d.is_male", plrno);
plr->is_alive=secfile_lookup_bool(file, "player%d.is_alive", plrno);
/* "Old" observer players will still be loaded but are considered dead. */
plr->ai.control = secfile_lookup_bool(file, "player%d.ai.control", plrno);
for (i = 0; i < MAX_NUM_PLAYERS; i++) {
plr->ai.love[i]
= secfile_lookup_int_default(file, 1, "player%d.ai%d.love", plrno, i);
ai->diplomacy.player_intel[i].spam
= secfile_lookup_int_default(file, 0, "player%d.ai%d.spam", plrno, i);
ai->diplomacy.player_intel[i].countdown
= secfile_lookup_int_default(file, -1, "player%d.ai%d.countdown", plrno, i);
ai->diplomacy.player_intel[i].war_reason
= secfile_lookup_int_default(file, 0, "player%d.ai%d.war_reason", plrno, i);
ai->diplomacy.player_intel[i].ally_patience
= secfile_lookup_int_default(file, 0, "player%d.ai%d.patience", plrno, i);
ai->diplomacy.player_intel[i].warned_about_space
= secfile_lookup_int_default(file, 0, "player%d.ai%d.warn_space", plrno, i);
ai->diplomacy.player_intel[i].asked_about_peace
= secfile_lookup_int_default(file, 0, "player%d.ai%d.ask_peace", plrno, i);
ai->diplomacy.player_intel[i].asked_about_alliance
= secfile_lookup_int_default(file, 0, "player%d.ai%d.ask_alliance", plrno, i);
ai->diplomacy.player_intel[i].asked_about_ceasefire
= secfile_lookup_int_default(file, 0, "player%d.ai%d.ask_ceasefire", plrno, i);
}
/* Backwards-compatibility: the tech goal value is still stored in the
* "ai" section even though it was moved into the research struct. */
research->tech_goal
= load_technology(file, "player%d.ai.tech_goal", plrno);
if (research->tech_goal == A_NONE) {
/* Old servers (1.14.1) saved both A_UNSET and A_NONE by 0
* Here 0 means A_UNSET
*/
research->tech_goal = A_UNSET;
}
/* Some sane defaults */
plr->ai.handicap = 0; /* set later */
plr->ai.fuzzy = 0; /* set later */
plr->ai.expand = 100; /* set later */
plr->ai.science_cost = 100; /* set later */
plr->ai.skill_level =
secfile_lookup_int_default(file, game.info.skill_level,
"player%d.ai.skill_level", plrno);
if (plr->ai.control && plr->ai.skill_level==0) {
plr->ai.skill_level = GAME_OLD_DEFAULT_SKILL_LEVEL;
}
if (plr->ai.control) {
/* Set AI parameters */
set_ai_level_directer(plr, plr->ai.skill_level);
}
plr->economic.gold=secfile_lookup_int(file, "player%d.gold", plrno);
plr->economic.tax=secfile_lookup_int(file, "player%d.tax", plrno);
plr->economic.science=secfile_lookup_int(file, "player%d.science", plrno);
plr->economic.luxury=secfile_lookup_int(file, "player%d.luxury", plrno);
plr->bulbs_last_turn =
secfile_lookup_int_default(file, 0,
"player%d.bulbs_last_turn",
plrno);
/* how many future techs were researched already by player */
research->future_tech
= secfile_lookup_int(file, "player%d.futuretech", plrno);
/* We use default values for bulbs_researched_before, changed_from
* and got_tech to preserve backwards-compatibility with save files
* that didn't store this information. */
research->bulbs_researched
= secfile_lookup_int(file, "player%d.researched", plrno);
research->bulbs_researched_before =
secfile_lookup_int_default(file, 0,
"player%d.researched_before", plrno);
research->changed_from =
load_technology(file, "player%d.research_changed_from", plrno);
research->got_tech = secfile_lookup_bool_default(file, FALSE,
"player%d.research_got_tech",
plrno);
research->techs_researched
= secfile_lookup_int(file, "player%d.researchpoints", plrno);
research->researching
= load_technology(file, "player%d.researching", plrno);
if (research->researching == A_NONE) {
/* Old servers (1.14.1) used to save A_FUTURE by 0
* This has to be interpreted from context because A_NONE was also
* saved by 0
*/
research->researching = A_FUTURE;
}
p = secfile_lookup_str_default(file, NULL, "player%d.invs_new", plrno);
if (!p) {
/* old savegames */
p = secfile_lookup_str(file, "player%d.invs", plrno);
for (k = 0; p[k]; k++) {
if (p[k] == '1') {
name = old_tech_name(k);
id = find_advance_by_rule_name(name);
if (id != A_LAST) {
set_invention(plr, id, TECH_KNOWN);
}
}
}
} else {
for (k = 0; k < technology_order_size && p[k]; k++) {
if (p[k] == '1') {
id = find_advance_by_rule_name(technology_order[k]);
if (id != A_LAST) {
set_invention(plr, id, TECH_KNOWN);
}
}
}
}
plr->capital = secfile_lookup_bool(file, "player%d.capital", plrno);
{
/* The old-style "revolution" value indicates the number of turns until
* the revolution is complete, or 0 if there is no revolution. The
* new-style "revolution_finishes" value indicates the turn in which
* the revolution will complete (which may be less than the current
* turn) or -1 if there is no revolution. */
int revolution = secfile_lookup_int_default(file, 0, "player%d.revolution",
plrno);
if (revolution == 0) {
if (government_of_player(plr) != game.government_when_anarchy) {
revolution = -1;
} else {
/* some old savegames may be buggy */
revolution = game.info.turn + 1;
}
} else {
revolution = game.info.turn + revolution;
}
plr->revolution_finishes
= secfile_lookup_int_default(file, revolution,
"player%d.revolution_finishes", plrno);
}
update_research(plr);
for (i = 0; i < game.info.nplayers; i++) {
plr->diplstates[i].type =
secfile_lookup_int_default(file, DS_WAR,
"player%d.diplstate%d.type", plrno, i);
plr->diplstates[i].max_state =
secfile_lookup_int_default(file, DS_WAR,
"player%d.diplstate%d.max_state", plrno, i);
plr->diplstates[i].first_contact_turn =
secfile_lookup_int_default(file, 0,
"player%d.diplstate%d.first_contact_turn", plrno, i);
plr->diplstates[i].turns_left =
secfile_lookup_int_default(file, -2,
"player%d.diplstate%d.turns_left", plrno, i);
plr->diplstates[i].has_reason_to_cancel =
secfile_lookup_int_default(file, 0,
"player%d.diplstate%d.has_reason_to_cancel",
plrno, i);
plr->diplstates[i].contact_turns_left =
secfile_lookup_int_default(file, 0,
"player%d.diplstate%d.contact_turns_left", plrno, i);
}
/* We don't need this info, but savegames carry it anyway.
To avoid getting "unused" warnings we touch the values like this. */
for (i = game.info.nplayers; i < MAX_NUM_PLAYERS + MAX_NUM_BARBARIANS; i++) {
secfile_lookup_int_default(file, DS_WAR,
"player%d.diplstate%d.type", plrno, i);
secfile_lookup_int_default(file, 0,
"player%d.diplstate%d.turns_left", plrno, i);
secfile_lookup_int_default(file, 0,
"player%d.diplstate%d.has_reason_to_cancel",
plrno, i);
secfile_lookup_int_default(file, 0,
"player%d.diplstate%d.contact_turns_left", plrno, i);
}
{ /* spacerace */
struct player_spaceship *ship = &plr->spaceship;
char prefix[32];
char *st;
my_snprintf(prefix, sizeof(prefix), "player%d.spaceship", plrno);
spaceship_init(ship);
ship->state = secfile_lookup_int(file, "%s.state", prefix);
if (ship->state != SSHIP_NONE) {
ship->structurals = secfile_lookup_int(file, "%s.structurals", prefix);
ship->components = secfile_lookup_int(file, "%s.components", prefix);
ship->modules = secfile_lookup_int(file, "%s.modules", prefix);
ship->fuel = secfile_lookup_int(file, "%s.fuel", prefix);
ship->propulsion = secfile_lookup_int(file, "%s.propulsion", prefix);
ship->habitation = secfile_lookup_int(file, "%s.habitation", prefix);
ship->life_support = secfile_lookup_int(file, "%s.life_support", prefix);
ship->solar_panels = secfile_lookup_int(file, "%s.solar_panels", prefix);
st = secfile_lookup_str(file, "%s.structure", prefix);
for (i = 0; i < NUM_SS_STRUCTURALS; i++) {
if (st[i] == '0') {
ship->structure[i] = FALSE;
} else if (st[i] == '1') {
ship->structure[i] = TRUE;
} else {
freelog(LOG_ERROR, "invalid spaceship structure '%c' (%d)", st[i],
st[i]);
ship->structure[i] = FALSE;
}
}
if (ship->state >= SSHIP_LAUNCHED) {
ship->launch_year = secfile_lookup_int(file, "%s.launch_year", prefix);
}
spaceship_calc_derived(ship);
}
}
plr->cities = city_list_new();
ncities=secfile_lookup_int(file, "player%d.ncities", plrno);
if (!plr->is_alive && ncities > 0) {
ncities = 0; /* Some old savegames may be buggy. */
}
for (i = 0; i < ncities; i++) { /* read the cities */
struct city *pcity;
const char *name;
int id, k;
int citizens = 0;
int nat_x = secfile_lookup_int(file, "player%d.c%d.x", plrno, i);
int nat_y = secfile_lookup_int(file, "player%d.c%d.y", plrno, i);
struct tile *ptile = native_pos_to_tile(nat_x, nat_y);
pcity = create_city_virtual(plr, ptile,
secfile_lookup_str(file, "player%d.c%d.name", plrno, i));
ptile->owner_source = pcity->tile;
tile_set_owner(ptile, plr);
pcity->id=secfile_lookup_int(file, "player%d.c%d.id", plrno, i);
alloc_id(pcity->id);
idex_register_city(pcity);
id = secfile_lookup_int_default(file, -1,
"player%d.c%d.original", plrno, i);
if (id >= 0 && id < game.info.nplayers) {
pcity->original = get_player(id);
} else {
pcity->original = get_player(plrno);
}
pcity->size=secfile_lookup_int(file, "player%d.c%d.size", plrno, i);
pcity->steal=secfile_lookup_int(file, "player%d.c%d.steal", plrno, i);
specialist_type_iterate(sp) {
citizens +=
pcity->specialists[sp]
= secfile_lookup_int(file, "player%d.c%d.n%s", plrno, i,
get_specialist(sp)->name);
} specialist_type_iterate_end;
for (j = 0; j < NUM_TRADEROUTES; j++)
pcity->trade[j]=secfile_lookup_int(file, "player%d.c%d.traderoute%d",
plrno, i, j);
pcity->food_stock = secfile_lookup_int(file, "player%d.c%d.food_stock",
plrno, i);
pcity->shield_stock = secfile_lookup_int(file,
"player%d.c%d.shield_stock",
plrno, i);
pcity->anarchy = secfile_lookup_int(file, "player%d.c%d.anarchy",
plrno, i);
pcity->rapture = secfile_lookup_int_default(file, 0,
"player%d.c%d.rapture",
plrno, i);
pcity->was_happy = secfile_lookup_bool(file,
"player%d.c%d.was_happy",
plrno, i);
pcity->production.is_unit =
secfile_lookup_bool(file,
"player%d.c%d.is_building_unit", plrno, i);
name = secfile_lookup_str_default(file, NULL,
"player%d.c%d.currently_building_name",
plrno, i);
if (pcity->production.is_unit) {
if (!name) {
id = secfile_lookup_int(file, "player%d.c%d.currently_building",
plrno, i);
name = old_unit_type_name(id);
}
pcity->production.value = find_unit_type_by_rule_name(name)->index;
} else {
if (!name) {
id = secfile_lookup_int(file, "player%d.c%d.currently_building",
plrno, i);
name = old_impr_type_name(id);
}
pcity->production.value = find_improvement_by_rule_name(name);
}
if (has_capability("turn_last_built", savefile_options)) {
pcity->turn_last_built = secfile_lookup_int(file,
"player%d.c%d.turn_last_built", plrno, i);
} else {
/* Before, turn_last_built was stored as a year. There is no easy
* way to convert this into a turn value. */
pcity->turn_last_built = 0;
}
pcity->changed_from.is_unit =
secfile_lookup_bool_default(file, pcity->production.is_unit,
"player%d.c%d.changed_from_is_unit",
plrno, i);
name = secfile_lookup_str_default(file, NULL,
"player%d.c%d.changed_from_name",
plrno, i);
if (pcity->changed_from.is_unit) {
if (!name) {
id = secfile_lookup_int(file, "player%d.c%d.changed_from_id",
plrno, i);
name = old_unit_type_name(id);
}
pcity->changed_from.value = find_unit_type_by_rule_name(name)->index;
} else {
if (!name) {
id = secfile_lookup_int(file, "player%d.c%d.changed_from_id",
plrno, i);
name = old_impr_type_name(id);
}
pcity->changed_from.value = find_improvement_by_rule_name(name);
}
pcity->before_change_shields=
secfile_lookup_int_default(file, pcity->shield_stock,
"player%d.c%d.before_change_shields", plrno, i);
pcity->disbanded_shields=
secfile_lookup_int_default(file, 0,
"player%d.c%d.disbanded_shields", plrno, i);
pcity->caravan_shields=
secfile_lookup_int_default(file, 0,
"player%d.c%d.caravan_shields", plrno, i);
pcity->last_turns_shield_surplus =
secfile_lookup_int_default(file, 0,
"player%d.c%d.last_turns_shield_surplus",
plrno, i);
pcity->synced = FALSE; /* must re-sync with clients */
pcity->turn_founded =
secfile_lookup_int_default(file, -2, "player%d.c%d.turn_founded",
plrno, i);
j = secfile_lookup_int(file, "player%d.c%d.did_buy", plrno, i);
pcity->did_buy = (j != 0);
if (j == -1 && pcity->turn_founded == -2) {
pcity->turn_founded = game.info.turn;
}
pcity->did_sell =
secfile_lookup_bool_default(file, FALSE, "player%d.c%d.did_sell", plrno,i);
pcity->airlift = secfile_lookup_bool_default(file, FALSE,
"player%d.c%d.airlift", plrno,i);
/* New city-options form as of freeciv 2.1. Old (<= 2.0) city options
* are lost on upgrade. */
BV_CLR_ALL(pcity->city_options);
for (j = 0; j < CITYO_LAST; j++) {
if (secfile_lookup_bool_default(file, FALSE,
"player%d.c%d.option%d",
plrno, i, j)) {
BV_SET(pcity->city_options, j);
}
}
/* Fix for old buggy savegames. */
if (!has_capability("known32fix", savefile_options)
&& plrno >= 16) {
map_city_radius_iterate(pcity->tile, tile1) {
map_set_known(tile1, plr);
} map_city_radius_iterate_end;
}
/* adding the cities contribution to fog-of-war */
pcity->server.vision = vision_new(city_owner(pcity), pcity->tile, FALSE);
city_refresh_vision(pcity);
pcity->units_supported = unit_list_new();
/* Initialize pcity->city_map[][], using set_worker_city() so that
ptile->worked gets initialized correctly. The pre-initialisation
to C_TILE_EMPTY is necessary because set_worker_city() accesses
the existing value to possibly adjust ptile->worked, so need to
initialize a non-worked value so ptile->worked (possibly already
set from neighbouring city) does not get unset for C_TILE_EMPTY
or C_TILE_UNAVAILABLE here. -- dwp
*/
p=secfile_lookup_str(file, "player%d.c%d.workers", plrno, i);
for(y=0; y<CITY_MAP_SIZE; y++) {
for(x=0; x<CITY_MAP_SIZE; x++) {
bool valid = is_valid_city_coords(x, y);
pcity->city_map[x][y] = valid ? C_TILE_EMPTY : C_TILE_UNAVAILABLE;
if (*p == '0') {
if (!valid) {
/* oops, inconsistent savegame; minimal fix: */
freelog(LOG_VERBOSE, "Invalid workers '%c' for %s (%d,%d), "
"ignoring", *p, city_name(pcity), x, y);
} else {
set_worker_city(pcity, x, y,
NULL != city_map_to_map(pcity, x, y)
? C_TILE_EMPTY : C_TILE_UNAVAILABLE);
}
} else if (*p=='1') {
struct tile *ptile;
if (!valid) {
/* oops, inconsistent savegame; minimal fix: */
freelog(LOG_VERBOSE, "Invalid workers '%c' for %s (%d,%d), "
"ignoring", *p, city_name(pcity), x, y);
} else if (NULL == (ptile = city_map_to_map(pcity, x, y))
|| ptile->worked) {
/* oops, inconsistent savegame; minimal fix: */
freelog(LOG_VERBOSE, "Inconsistent worked for %s (%d,%d), "
"converting to specialist", city_name(pcity), x, y);
pcity->specialists[DEFAULT_SPECIALIST]++;
set_worker_city(pcity, x, y, C_TILE_UNAVAILABLE);
citizens++;
} else {
set_worker_city(pcity, x, y, C_TILE_WORKER);
citizens++;
}
} else if (*p == '2') {
if (valid) {
set_worker_city(pcity, x, y, C_TILE_UNAVAILABLE);
}
assert(pcity->city_map[x][y] == C_TILE_UNAVAILABLE);
} else {
freelog(LOG_VERBOSE, "Invalid workers '%c' for %s (%d,%d), "
"ignoring", *p, city_name(pcity), x, y);
}
p++;
}
}
/* center tile worker isn't counted in city size */
if (citizens > 1) {
citizens--;
}
if (citizens != pcity->size) {
freelog(LOG_ERROR, "player_load()"
" %d citizens not equal %d city size in \"%s\".",
citizens,
pcity->size,
city_name(pcity));
}
/* Initialise list of improvements with City- and Building-wide
equiv_ranges */
for (k = 0; k < ARRAY_SIZE(pcity->improvements); k++) {
pcity->improvements[k] = I_NONE;
}
p = secfile_lookup_str_default(file, NULL,
"player%d.c%d.improvements_new",
plrno, i);
if (!p) {
/* old savegames */
p = secfile_lookup_str(file, "player%d.c%d.improvements", plrno, i);
for (k = 0; p[k]; k++) {
if (p[k] == '1') {
name = old_impr_type_name(k);
id = find_improvement_by_rule_name(name);
if (id != -1) {
city_add_improvement(pcity, id);
}
}
}
} else {
for (k = 0; k < improvement_order_size && p[k]; k++) {
if (p[k] == '1') {
id = find_improvement_by_rule_name(improvement_order[k]);
if (id != -1) {
city_add_improvement(pcity, id);
}
}
}
}
init_worklist(&pcity->worklist);
worklist_load(file, &pcity->worklist, "player%d.c%d", plrno, i);
/* FIXME: remove this when the urgency is properly recalculated. */
pcity->ai.urgency = secfile_lookup_int_default(file, 0,
"player%d.c%d.ai.urgency", plrno, i);
/* avoid myrand recalculations on subsequent reload. */
pcity->ai.next_recalc = secfile_lookup_int_default(file, 0,
"player%d.c%d.ai.building_turn", plrno, i);
/* avoid myrand and expensive recalculations on subsequent reload. */
pcity->ai.next_founder_want_recalc = secfile_lookup_int_default(file, 0,
"player%d.c%d.ai.founder_turn", plrno, i);
pcity->ai.founder_want = secfile_lookup_int_default(file, 0,
"player%d.c%d.ai.founder_want", plrno, i);
pcity->ai.founder_boat = secfile_lookup_bool_default(file,
(pcity->ai.founder_want < 0),
"player%d.c%d.ai.founder_boat", plrno, i);
/* do after all the set_worker_city() are done. */
tile_set_city(pcity->tile, pcity);
city_list_append(plr->cities, pcity);
}
load_player_units(plr, plrno, file);
/* Toss any existing attribute_block (should not exist) */
if (plr->attribute_block.data) {
free(plr->attribute_block.data);
plr->attribute_block.data = NULL;
}
/* This is a big heap of opaque data for the client, check everything! */
plr->attribute_block.length = secfile_lookup_int_default(
file, 0, "player%d.attribute_v2_block_length", plrno);
if (0 > plr->attribute_block.length) {
freelog(LOG_ERROR, "player%d.attribute_v2_block_length=%d too small",
plrno,
plr->attribute_block.length);
plr->attribute_block.length = 0;
} else if (MAX_ATTRIBUTE_BLOCK < plr->attribute_block.length) {
freelog(LOG_ERROR, "player%d.attribute_v2_block_length=%d too big (max %d)",
plrno,
plr->attribute_block.length,
MAX_ATTRIBUTE_BLOCK);
plr->attribute_block.length = 0;
} else if (0 < plr->attribute_block.length) {
int part_nr, parts;
size_t actual_length;
size_t quoted_length;
char *quoted;
plr->attribute_block.data = fc_malloc(plr->attribute_block.length);
quoted_length = secfile_lookup_int
(file, "player%d.attribute_v2_block_length_quoted", plrno);
quoted = fc_malloc(quoted_length + 1);
quoted[0] = '\0';
parts =
secfile_lookup_int(file, "player%d.attribute_v2_block_parts", plrno);
for (part_nr = 0; part_nr < parts; part_nr++) {
char *current = secfile_lookup_str(file,
"player%d.attribute_v2_block_data.part%d",
plrno, part_nr);
if (!current) {
freelog(LOG_ERROR, "attribute_v2_block_parts=%d actual=%d",
parts,
part_nr);
break;
}
freelog(LOG_DEBUG, "attribute_v2_block_length_quoted=%lu have=%lu part=%lu",
(unsigned long) quoted_length,
(unsigned long) strlen(quoted),
(unsigned long) strlen(current));
assert(strlen(quoted) + strlen(current) <= quoted_length);
strcat(quoted, current);
}
if (quoted_length != strlen(quoted)) {
freelog(LOG_FATAL, "attribute_v2_block_length_quoted=%lu actual=%lu",
(unsigned long) quoted_length,
(unsigned long) strlen(quoted));
assert(0);
}
actual_length =
unquote_block(quoted,
plr->attribute_block.data,
plr->attribute_block.length);
assert(actual_length == plr->attribute_block.length);
free(quoted);
}
}
/**********************************************************************
The private map for fog of war
***********************************************************************/
static void player_map_load(struct player *plr, int plrno,
struct section_file *file,
char** improvement_order, int improvement_order_size,
const enum tile_special_type *special_order)
{
int i;
if (!plr->is_alive)
map_know_and_see_all(plr);
/* load map if:
1) it from a fog of war build
2) fog of war was on (otherwise the private map wasn't saved)
3) is not from a "unit only" fog of war save file
*/
if (secfile_lookup_int_default(file, -1, "game.fogofwar") != -1
&& game.info.fogofwar == TRUE
&& secfile_lookup_int_default(file, -1,"player%d.total_ncities", plrno) != -1
&& secfile_lookup_bool_default(file, TRUE, "game.save_private_map")) {
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, "player%d.map_t%03d",
plrno, nat_y),
map_get_player_tile(ptile, plr)->terrain =
char2terrain(ch));
if (special_order) {
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, "player%d.map_res%03d", plrno, nat_y),
map_get_player_tile(ptile, plr)->resource
= identifier_to_resource(ch));
special_halfbyte_iterate(j) {
char buf[32]; /* enough for sprintf() below */
sprintf (buf, "player%d.map_spe%02d_%%03d", plrno, j);
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, buf, nat_y),
set_savegame_special(&map_get_player_tile(ptile, plr)->special,
ch, special_order + 4 * j));
} special_halfbyte_iterate_end;
} else {
/* get 4-bit segments of 12-bit "special" field. */
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, "player%d.map_l%03d", plrno, nat_y),
set_savegame_special(&map_get_player_tile(ptile, plr)->special,
ch, default_specials + 0));
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, "player%d.map_u%03d", plrno, nat_y),
set_savegame_special(&map_get_player_tile(ptile, plr)->special,
ch, default_specials + 4));
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str_default (file, NULL, "player%d.map_n%03d",
plrno, nat_y),
set_savegame_special(&map_get_player_tile(ptile, plr)->special,
ch, default_specials + 8));
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, "map.l%03d", nat_y),
set_savegame_old_resource(&map_get_player_tile(ptile, plr)->resource,
ptile->terrain, ch, 0));
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str(file, "map.n%03d", nat_y),
set_savegame_old_resource(&map_get_player_tile(ptile, plr)->resource,
ptile->terrain, ch, 1));
}
/* get 4-bit segments of 16-bit "updated" field */
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str
(file, "player%d.map_ua%03d", plrno, nat_y),
map_get_player_tile(ptile, plr)->last_updated =
ascii_hex2bin(ch, 0));
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str
(file, "player%d.map_ub%03d", plrno, nat_y),
map_get_player_tile(ptile, plr)->last_updated |=
ascii_hex2bin(ch, 1));
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str
(file, "player%d.map_uc%03d", plrno, nat_y),
map_get_player_tile(ptile, plr)->last_updated |=
ascii_hex2bin(ch, 2));
LOAD_MAP_DATA(ch, nat_y, ptile,
secfile_lookup_str
(file, "player%d.map_ud%03d", plrno, nat_y),
map_get_player_tile(ptile, plr)->last_updated |=
ascii_hex2bin(ch, 3));
{
int j;
i = secfile_lookup_int(file, "player%d.total_ncities", plrno);
for (j = 0; j < i; j++) {
int nat_x, nat_y;
int k, id;
const char *p;
struct tile *ptile;
struct vision_base *pdcity = fc_calloc(1, sizeof(*pdcity));
pdcity->identity = secfile_lookup_int(file, "player%d.dc%d.id", plrno, j);
if (VISION_BASE_RUIN >= pdcity->identity) {
freelog(LOG_ERROR, "[player%d] dc%d has invalid id (%d); skipping.",
plrno, j, pdcity->identity);
free(pdcity);
continue;
}
nat_x = secfile_lookup_int(file, "player%d.dc%d.x", plrno, j);
nat_y = secfile_lookup_int(file, "player%d.dc%d.y", plrno, j);
ptile = native_pos_to_tile(nat_x, nat_y);
pdcity->location = ptile;
sz_strlcpy(pdcity->name, secfile_lookup_str(file, "player%d.dc%d.name", plrno, j));
pdcity->size = secfile_lookup_int(file, "player%d.dc%d.size", plrno, j);
pdcity->occupied = secfile_lookup_bool_default(file, FALSE,
"player%d.dc%d.occupied", plrno, j);
pdcity->walls = secfile_lookup_bool_default(file, FALSE,
"player%d.dc%d.walls", plrno, j);
pdcity->happy = secfile_lookup_bool_default(file, FALSE,
"player%d.dc%d.happy", plrno, j);
pdcity->unhappy = secfile_lookup_bool_default(file, FALSE,
"player%d.dc%d.unhappy", plrno, j);
id = secfile_lookup_int(file, "player%d.dc%d.owner", plrno, j);
pdcity->owner = get_player(id);
if (NULL == vision_owner(pdcity)) {
freelog(LOG_ERROR, "[player%d] dc%d has invalid owner (%d); skipping.",
plrno, j, id);
free(pdcity);
continue;
}
/* Initialise list of improvements */
BV_CLR_ALL(pdcity->improvements);
p = secfile_lookup_str_default(file, NULL,
"player%d.dc%d.improvements", plrno, j);
if (!p) {
/* old savegames */
} else {
for (k = 0; k < improvement_order_size && p[k]; k++) {
if (p[k] == '1') {
id = find_improvement_by_rule_name(improvement_order[k]);
if (id != -1) {
BV_SET(pdcity->improvements, id);
}
}
}
}
map_get_player_tile(ptile, plr)->vision_source = pdcity;
alloc_id(pdcity->identity);
}
}
/* This shouldn't be neccesary if the savegame was consistent, but there
is a bug in some pre-1.11 savegames. Anyway, it can't hurt */
whole_map_iterate(ptile) {
if (map_is_known_and_seen(ptile, plr, V_MAIN)) {
update_player_tile_knowledge(plr, ptile);
reality_check_city(plr, ptile);
if (tile_get_city(ptile)) {
update_dumb_city(plr, tile_get_city(ptile));
}
}
} whole_map_iterate_end;
} else {
/* We have an old savegame or fog of war was turned off; the
players private knowledge is set to be what he could see
without fog of war */
whole_map_iterate(ptile) {
if (map_is_known(ptile, plr)) {
struct city *pcity = tile_get_city(ptile);
update_player_tile_last_seen(plr, ptile);
update_player_tile_knowledge(plr, ptile);
if (pcity)
update_dumb_city(plr, pcity);
}
} whole_map_iterate_end;
}
}
/***************************************************************
...
***************************************************************/
static void player_save(struct player *plr, int plrno,
struct section_file *file)
{
int i;
char invs[A_LAST+1];
struct player_spaceship *ship = &plr->spaceship;
struct ai_data *ai = ai_data_get(plr);
int wlist_max_length = 0;
secfile_insert_str(file, player_name(plr), "player%d.name", plrno);
secfile_insert_str(file, plr->username, "player%d.username", plrno);
secfile_insert_str(file, plr->ranked_username, "player%d.ranked_username",
plrno);
secfile_insert_str(file, nation_rule_name(nation_of_player(plr)),
"player%d.nation", plrno);
/* 1.15 and later won't use the race field, they key on the nation string
* This field is kept only for forward compatibility
* Nations can't be saved correctly because race must be < 62 */
secfile_insert_int(file, plrno, "player%d.race", plrno);
secfile_insert_int(file, plr->team ? plr->team->index : -1,
"player%d.team_no", plrno);
secfile_insert_str(file, government_rule_name(government_of_player(plr)),
"player%d.government_name", plrno);
/* 1.15 and later won't use "government" field; it's kept for forward
* compatibility */
secfile_insert_int(file, old_government_id(government_of_player(plr)),
"player%d.government", plrno);
if (plr->target_government) {
secfile_insert_str(file, government_rule_name(government_of_player(plr)),
"player%d.target_government_name", plrno);
}
players_iterate(pother) {
secfile_insert_bool(file, BV_ISSET(plr->embassy, pother->player_no),
"player%d.embassy%d", plrno, pother->player_no);
} players_iterate_end;
/* Required for 2.0 and earlier servers. Remove eventually. */
secfile_insert_int(file, 0, "player%d.embassy", plrno);
/* This field won't be used; it's kept only for forward compatibility.
* City styles are specified by name since CVS 12/01-04. */
secfile_insert_int(file, 0, "player%d.city_style", plrno);
/* This is the new city style field to be used */
secfile_insert_str(file, city_style_rule_name(plr->city_style),
"player%d.city_style_by_name", plrno);
secfile_insert_bool(file, plr->is_male, "player%d.is_male", plrno);
secfile_insert_bool(file, plr->is_alive, "player%d.is_alive", plrno);
secfile_insert_bool(file, plr->ai.control, "player%d.ai.control", plrno);
for (i = 0; i < MAX_NUM_PLAYERS; i++) {
secfile_insert_int(file, plr->ai.love[i],
"player%d.ai%d.love", plrno, i);
secfile_insert_int(file, ai->diplomacy.player_intel[i].spam,
"player%d.ai%d.spam", plrno, i);
secfile_insert_int(file, ai->diplomacy.player_intel[i].countdown,
"player%d.ai%d.countdown", plrno, i);
secfile_insert_int(file, ai->diplomacy.player_intel[i].war_reason,
"player%d.ai%d.war_reason", plrno, i);
secfile_insert_int(file, ai->diplomacy.player_intel[i].ally_patience,
"player%d.ai%d.patience", plrno, i);
secfile_insert_int(file, ai->diplomacy.player_intel[i].warned_about_space,
"player%d.ai%d.warn_space", plrno, i);
secfile_insert_int(file, ai->diplomacy.player_intel[i].asked_about_peace,
"player%d.ai%d.ask_peace", plrno, i);
secfile_insert_int(file, ai->diplomacy.player_intel[i].asked_about_alliance,
"player%d.ai%d.ask_alliance", plrno, i);
secfile_insert_int(file, ai->diplomacy.player_intel[i].asked_about_ceasefire,
"player%d.ai%d.ask_ceasefire", plrno, i);
}
save_technology(file, "player%d.ai.tech_goal",
plrno, get_player_research(plr)->tech_goal);
secfile_insert_int(file, plr->ai.skill_level,
"player%d.ai.skill_level", plrno);
secfile_insert_int(file, plr->ai.barbarian_type, "player%d.ai.is_barbarian", plrno);
secfile_insert_int(file, plr->economic.gold, "player%d.gold", plrno);
secfile_insert_int(file, plr->economic.tax, "player%d.tax", plrno);
secfile_insert_int(file, plr->economic.science, "player%d.science", plrno);
secfile_insert_int(file, plr->economic.luxury, "player%d.luxury", plrno);
secfile_insert_int(file, plr->bulbs_last_turn, "player%d.bulbs_last_turn",
plrno);
secfile_insert_int(file, get_player_research(plr)->future_tech,
"player%d.futuretech", plrno);
secfile_insert_int(file, get_player_research(plr)->bulbs_researched,
"player%d.researched", plrno);
secfile_insert_int(file, get_player_research(plr)->bulbs_researched_before,
"player%d.researched_before", plrno);
secfile_insert_bool(file, get_player_research(plr)->got_tech,
"player%d.research_got_tech", plrno);
save_technology(file, "player%d.research_changed_from", plrno,
get_player_research(plr)->changed_from);
secfile_insert_int(file, get_player_research(plr)->techs_researched,
"player%d.researchpoints", plrno);
save_technology(file, "player%d.researching", plrno,
get_player_research(plr)->researching);
secfile_insert_bool(file, plr->capital, "player%d.capital", plrno);
secfile_insert_int(file, plr->revolution_finishes,
"player%d.revolution_finishes", plrno);
{
/* Insert the old-style "revolution" value, for forward compatibility.
* See the loading code for more explanation. */
int revolution;
if (plr->revolution_finishes < 0) {
/* No revolution. */
revolution = 0;
} else if (plr->revolution_finishes <= game.info.turn) {
revolution = 1; /* Approximation. */
} else {
revolution = plr->revolution_finishes - game.info.turn;
}
secfile_insert_int(file, revolution, "player%d.revolution", plrno);
}
/* 1.14 servers depend on technology order in ruleset. Here we are trying
* to simulate 1.14.1 default order */
init_old_technology_bitvector(invs);
tech_type_iterate(tech_id) {
if (get_invention(plr, tech_id) == TECH_KNOWN) {
add_technology_into_old_bitvector(invs, tech_id);
}
} tech_type_iterate_end;
secfile_insert_str(file, invs, "player%d.invs", plrno);
/* Save technology lists as bitvector. Note that technology order is
* saved in savefile.technology_order */
tech_type_iterate(tech_id) {
invs[tech_id] = (get_invention(plr, tech_id) == TECH_KNOWN) ? '1' : '0';
} tech_type_iterate_end;
invs[game.control.num_tech_types] = '\0';
secfile_insert_str(file, invs, "player%d.invs_new", plrno);
for (i = 0; i < MAX_NUM_PLAYERS+MAX_NUM_BARBARIANS; i++) {
secfile_insert_int(file, plr->diplstates[i].type,
"player%d.diplstate%d.type", plrno, i);
secfile_insert_int(file, plr->diplstates[i].max_state,
"player%d.diplstate%d.max_state", plrno, i);
secfile_insert_int(file, plr->diplstates[i].first_contact_turn,
"player%d.diplstate%d.first_contact_turn", plrno, i);
secfile_insert_int(file, plr->diplstates[i].turns_left,
"player%d.diplstate%d.turns_left", plrno, i);
secfile_insert_int(file, plr->diplstates[i].has_reason_to_cancel,
"player%d.diplstate%d.has_reason_to_cancel", plrno, i);
secfile_insert_int(file, plr->diplstates[i].contact_turns_left,
"player%d.diplstate%d.contact_turns_left", plrno, i);
}
{
char vision[MAX_NUM_PLAYERS+MAX_NUM_BARBARIANS+1];
for (i=0; i < MAX_NUM_PLAYERS+MAX_NUM_BARBARIANS; i++)
vision[i] = gives_shared_vision(plr, get_player(i)) ? '1' : '0';
vision[i] = '\0';
secfile_insert_str(file, vision, "player%d.gives_shared_vision", plrno);
}
secfile_insert_int(file, ship->state, "player%d.spaceship.state", plrno);
if (ship->state != SSHIP_NONE) {
char prefix[32];
char st[NUM_SS_STRUCTURALS+1];
int i;
my_snprintf(prefix, sizeof(prefix), "player%d.spaceship", plrno);
secfile_insert_int(file, ship->structurals, "%s.structurals", prefix);
secfile_insert_int(file, ship->components, "%s.components", prefix);
secfile_insert_int(file, ship->modules, "%s.modules", prefix);
secfile_insert_int(file, ship->fuel, "%s.fuel", prefix);
secfile_insert_int(file, ship->propulsion, "%s.propulsion", prefix);
secfile_insert_int(file, ship->habitation, "%s.habitation", prefix);
secfile_insert_int(file, ship->life_support, "%s.life_support", prefix);
secfile_insert_int(file, ship->solar_panels, "%s.solar_panels", prefix);
for(i=0; i<NUM_SS_STRUCTURALS; i++) {
st[i] = (ship->structure[i]) ? '1' : '0';
}
st[i] = '\0';
secfile_insert_str(file, st, "%s.structure", prefix);
if (ship->state >= SSHIP_LAUNCHED) {
secfile_insert_int(file, ship->launch_year, "%s.launch_year", prefix);
}
}
secfile_insert_int(file, unit_list_size(plr->units), "player%d.nunits",
plrno);
secfile_insert_int(file, city_list_size(plr->cities), "player%d.ncities",
plrno);
i = -1;
unit_list_iterate(plr->units, punit) {
i++;
secfile_insert_int(file, punit->id, "player%d.u%d.id", plrno, i);
secfile_insert_int(file, punit->tile->nat_x, "player%d.u%d.x", plrno, i);
secfile_insert_int(file, punit->tile->nat_y, "player%d.u%d.y", plrno, i);
secfile_insert_int(file, punit->veteran, "player%d.u%d.veteran",
plrno, i);
secfile_insert_int(file, punit->hp, "player%d.u%d.hp", plrno, i);
secfile_insert_int(file, punit->homecity, "player%d.u%d.homecity",
plrno, i);
/* .type is actually kept only for forward compatibility */
secfile_insert_int(file, old_unit_type_id(unit_type(punit)),
"player%d.u%d.type",
plrno, i);
secfile_insert_str(file, unit_rule_name(punit),
"player%d.u%d.type_by_name",
plrno, i);
secfile_insert_int(file, punit->activity, "player%d.u%d.activity",
plrno, i);
secfile_insert_int(file, punit->activity_count,
"player%d.u%d.activity_count",
plrno, i);
secfile_insert_int(file, punit->activity_target,
"player%d.u%d.activity_target",
plrno, i);
secfile_insert_bool(file, punit->done_moving,
"player%d.u%d.done_moving", plrno, i);
secfile_insert_int(file, punit->moves_left, "player%d.u%d.moves",
plrno, i);
secfile_insert_int(file, punit->fuel, "player%d.u%d.fuel",
plrno, i);
secfile_insert_int(file, punit->battlegroup,
"player%d.u%d.battlegroup", plrno, i);
if (punit->goto_tile) {
secfile_insert_bool(file, TRUE, "player%d.u%d.go", plrno, i);
secfile_insert_int(file, punit->goto_tile->nat_x,
"player%d.u%d.goto_x", plrno, i);
secfile_insert_int(file, punit->goto_tile->nat_y,
"player%d.u%d.goto_y", plrno, i);
} else {
secfile_insert_bool(file, FALSE, "player%d.u%d.go", plrno, i);
/* for compatibility with older servers */
secfile_insert_int(file, 0, "player%d.u%d.goto_x", plrno, i);
secfile_insert_int(file, 0, "player%d.u%d.goto_y", plrno, i);
}
secfile_insert_bool(file, punit->ai.control, "player%d.u%d.ai", plrno, i);
secfile_insert_int(file, punit->ai.passenger, "player%d.u%d.passenger",
plrno, i);
secfile_insert_int(file, punit->ai.ferryboat, "player%d.u%d.ferryboat",
plrno, i);
secfile_insert_int(file, punit->ai.charge, "player%d.u%d.charge", plrno, i);
secfile_insert_int(file, punit->ai.bodyguard, "player%d.u%d.bodyguard",
plrno, i);
secfile_insert_int(file, punit->ord_map, "player%d.u%d.ord_map", plrno, i);
secfile_insert_int(file, punit->ord_city, "player%d.u%d.ord_city", plrno, i);
secfile_insert_bool(file, punit->moved, "player%d.u%d.moved", plrno, i);
secfile_insert_bool(file, punit->paradropped, "player%d.u%d.paradropped", plrno, i);
secfile_insert_int(file, punit->transported_by,
"player%d.u%d.transported_by", plrno, i);
if (punit->has_orders) {
int len = punit->orders.length, j;
char orders_buf[len + 1], dir_buf[len + 1], act_buf[len + 1];
secfile_insert_int(file, len, "player%d.u%d.orders_length", plrno, i);
secfile_insert_int(file, punit->orders.index,
"player%d.u%d.orders_index", plrno, i);
secfile_insert_bool(file, punit->orders.repeat,
"player%d.u%d.orders_repeat", plrno, i);
secfile_insert_bool(file, punit->orders.vigilant,
"player%d.u%d.orders_vigilant", plrno, i);
for (j = 0; j < len; j++) {
orders_buf[j] = order2char(punit->orders.list[j].order);
dir_buf[j] = '?';
act_buf[j] = '?';
switch (punit->orders.list[j].order) {
case ORDER_MOVE:
dir_buf[j] = dir2char(punit->orders.list[j].dir);
break;
case ORDER_ACTIVITY:
act_buf[j] = activity2char(punit->orders.list[j].activity);
break;
case ORDER_FULL_MP:
case ORDER_BUILD_CITY:
case ORDER_DISBAND:
case ORDER_BUILD_WONDER:
case ORDER_TRADEROUTE:
case ORDER_HOMECITY:
case ORDER_LAST:
break;
}
}
orders_buf[len] = dir_buf[len] = act_buf[len] = '\0';
secfile_insert_str(file, orders_buf,
"player%d.u%d.orders_list", plrno, i);
secfile_insert_str(file, dir_buf,
"player%d.u%d.dir_list", plrno, i);
secfile_insert_str(file, act_buf,
"player%d.u%d.activity_list", plrno, i);
} else {
/* Put all the same fields into the savegame - otherwise the
* registry code can't correctly use a tabular format and the
* savegame will be bigger. */
secfile_insert_int(file, 0, "player%d.u%d.orders_length", plrno, i);
secfile_insert_int(file, 0, "player%d.u%d.orders_index", plrno, i);
secfile_insert_bool(file, FALSE,
"player%d.u%d.orders_repeat", plrno, i);
secfile_insert_bool(file, FALSE,
"player%d.u%d.orders_vigilant", plrno, i);
secfile_insert_str(file, "-",
"player%d.u%d.orders_list", plrno, i);
secfile_insert_str(file, "-",
"player%d.u%d.dir_list", plrno, i);
secfile_insert_str(file, "-",
"player%d.u%d.activity_list", plrno, i);
}
}
unit_list_iterate_end;
i = -1;
/* First determine lenght of longest worklist */
city_list_iterate(plr->cities, pcity) {
if (pcity->worklist.length > wlist_max_length) {
wlist_max_length = pcity->worklist.length;
}
} city_list_iterate_end;
city_list_iterate(plr->cities, pcity) {
int j, x, y;
char citymap_buf[CITY_MAP_SIZE * CITY_MAP_SIZE + 1];
char impr_buf[MAX_NUM_ITEMS + 1];
i++;
secfile_insert_int(file, pcity->id, "player%d.c%d.id", plrno, i);
secfile_insert_int(file, pcity->tile->nat_x, "player%d.c%d.x", plrno, i);
secfile_insert_int(file, pcity->tile->nat_y, "player%d.c%d.y", plrno, i);
secfile_insert_str(file, city_name(pcity), "player%d.c%d.name", plrno, i);
secfile_insert_int(file, pcity->original->player_no,
"player%d.c%d.original", plrno, i);
secfile_insert_int(file, pcity->size, "player%d.c%d.size", plrno, i);
secfile_insert_int(file, pcity->steal, "player%d.c%d.steal", plrno, i);
specialist_type_iterate(sp) {
secfile_insert_int(file, pcity->specialists[sp],
"player%d.c%d.n%s", plrno, i,
get_specialist(sp)->name);
} specialist_type_iterate_end;
for (j = 0; j < NUM_TRADEROUTES; j++)
secfile_insert_int(file, pcity->trade[j], "player%d.c%d.traderoute%d",
plrno, i, j);
secfile_insert_int(file, pcity->food_stock, "player%d.c%d.food_stock",
plrno, i);
secfile_insert_int(file, pcity->shield_stock, "player%d.c%d.shield_stock",
plrno, i);
secfile_insert_int(file, pcity->turn_last_built,
"player%d.c%d.turn_last_built", plrno, i);
secfile_insert_bool(file, pcity->changed_from.is_unit,
"player%d.c%d.changed_from_is_unit", plrno, i);
if (pcity->changed_from.is_unit) {
struct unit_type *punittype = utype_by_number(pcity->changed_from.value);
secfile_insert_int(file, old_unit_type_id(punittype),
"player%d.c%d.changed_from_id", plrno, i);
secfile_insert_str(file, utype_rule_name(punittype),
"player%d.c%d.changed_from_name", plrno, i);
} else {
secfile_insert_int(file, old_impr_type_id(pcity->changed_from.value),
"player%d.c%d.changed_from_id", plrno, i);
secfile_insert_str(file, improvement_rule_name(
pcity->changed_from.value),
"player%d.c%d.changed_from_name", plrno, i);
}
secfile_insert_int(file, pcity->before_change_shields,
"player%d.c%d.before_change_shields", plrno, i);
secfile_insert_int(file, pcity->disbanded_shields,
"player%d.c%d.disbanded_shields", plrno, i);
secfile_insert_int(file, pcity->caravan_shields,
"player%d.c%d.caravan_shields", plrno, i);
secfile_insert_int(file, pcity->last_turns_shield_surplus,
"player%d.c%d.last_turns_shield_surplus", plrno, i);
secfile_insert_int(file, pcity->anarchy, "player%d.c%d.anarchy", plrno,i);
secfile_insert_int(file, pcity->rapture, "player%d.c%d.rapture", plrno,i);
secfile_insert_bool(file, pcity->was_happy, "player%d.c%d.was_happy", plrno,i);
if (pcity->turn_founded == game.info.turn) {
j = -1;
} else {
assert(pcity->did_buy == TRUE || pcity->did_buy == FALSE);
j = pcity->did_buy ? 1 : 0;
}
secfile_insert_int(file, j, "player%d.c%d.did_buy", plrno, i);
secfile_insert_int(file, pcity->turn_founded,
"player%d.c%d.turn_founded", plrno, i);
secfile_insert_bool(file, pcity->did_sell, "player%d.c%d.did_sell", plrno,i);
secfile_insert_bool(file, pcity->airlift, "player%d.c%d.airlift", plrno,i);
/* New options format as of Freeciv 2.1. */
for (j = 0; j < CITYO_LAST; j++) {
secfile_insert_bool(file, BV_ISSET(pcity->city_options, j),
"player%d.c%d.option%d", plrno, i, j);
}
j=0;
for(y=0; y<CITY_MAP_SIZE; y++) {
for(x=0; x<CITY_MAP_SIZE; x++) {
switch (get_worker_city(pcity, x, y)) {
case C_TILE_EMPTY:
citymap_buf[j++] = '0';
break;
case C_TILE_WORKER:
citymap_buf[j++] = '1';
break;
case C_TILE_UNAVAILABLE:
citymap_buf[j++] = '2';
break;
default:
citymap_buf[j++] = '?';
assert(0);
break;
}
}
}
citymap_buf[j]='\0';
assert(j < ARRAY_SIZE(citymap_buf));
secfile_insert_str(file, citymap_buf, "player%d.c%d.workers", plrno, i);
secfile_insert_bool(file, pcity->production.is_unit,
"player%d.c%d.is_building_unit", plrno, i);
if (pcity->production.is_unit) {
struct unit_type *punittype = utype_by_number(pcity->production.value);
secfile_insert_int(file, old_unit_type_id(punittype),
"player%d.c%d.currently_building", plrno, i);
secfile_insert_str(file, utype_rule_name(punittype),
"player%d.c%d.currently_building_name", plrno, i);
} else {
secfile_insert_int(file, old_impr_type_id(pcity->production.value),
"player%d.c%d.currently_building", plrno, i);
secfile_insert_str(file, improvement_rule_name(
pcity->production.value),
"player%d.c%d.currently_building_name", plrno, i);
}
/* 1.14 servers depend on improvement order in ruleset. Here we
* are trying to simulate 1.14.1 default order
*/
init_old_improvement_bitvector(impr_buf);
impr_type_iterate(id) {
if (pcity->improvements[id] != I_NONE) {
add_improvement_into_old_bitvector(impr_buf, id);
}
} impr_type_iterate_end;
assert(strlen(impr_buf) < sizeof(impr_buf));
secfile_insert_str(file, impr_buf, "player%d.c%d.improvements", plrno, i);
/* Save improvement list as bitvector. Note that improvement order
* is saved in savefile.improvement_order.
*/
impr_type_iterate(id) {
impr_buf[id] = (pcity->improvements[id] != I_NONE) ? '1' : '0';
} impr_type_iterate_end;
impr_buf[game.control.num_impr_types] = '\0';
assert(strlen(impr_buf) < sizeof(impr_buf));
secfile_insert_str(file, impr_buf,
"player%d.c%d.improvements_new", plrno, i);
worklist_save(file, &pcity->worklist, wlist_max_length,
"player%d.c%d", plrno, i);
/* FIXME: remove this when the urgency is properly recalculated. */
secfile_insert_int(file, pcity->ai.urgency,
"player%d.c%d.ai.urgency", plrno, i);
/* avoid myrand recalculations on subsequent reload. */
secfile_insert_int(file, pcity->ai.next_recalc,
"player%d.c%d.ai.building_turn", plrno, i);
/* avoid myrand and expensive recalculations on subsequent reload. */
secfile_insert_int(file, pcity->ai.next_founder_want_recalc,
"player%d.c%d.ai.founder_turn", plrno, i);
secfile_insert_int(file, pcity->ai.founder_want,
"player%d.c%d.ai.founder_want", plrno, i);
secfile_insert_bool(file, pcity->ai.founder_boat,
"player%d.c%d.ai.founder_boat", plrno, i);
}
city_list_iterate_end;
/********** Put the players private map **********/
/* Otherwise the player can see all, and there's no reason to save the private map. */
if (game.info.fogofwar
&& game.save_options.save_private_map) {
/* put the terrain type */
SAVE_PLAYER_MAP_DATA(ptile, file,"player%d.map_t%03d", plrno,
terrain2char(map_get_player_tile
(ptile, plr)->terrain));
SAVE_PLAYER_MAP_DATA(ptile, file, "player%d.map_res%03d", plrno,
resource_to_identifier(map_get_player_tile(ptile, plr)->resource));
special_halfbyte_iterate(j) {
char buf[32]; /* enough for sprintf() below */
enum tile_special_type mod[4];
int l;
for (l = 0; l < 4; l++) {
mod[l] = MIN(4 * j + l, S_LAST);
}
sprintf (buf, "player%%d.map_spe%02d_%%03d", j);
SAVE_PLAYER_MAP_DATA(ptile, file, buf, plrno,
get_savegame_special(map_get_player_tile(ptile, plr)->special, mod));
} special_halfbyte_iterate_end;
/* put 4-bit segments of 16-bit "updated" field */
SAVE_PLAYER_MAP_DATA(ptile, file,"player%d.map_ua%03d", plrno,
bin2ascii_hex(map_get_player_tile
(ptile, plr)->last_updated, 0));
SAVE_PLAYER_MAP_DATA(ptile, file, "player%d.map_ub%03d", plrno,
bin2ascii_hex(map_get_player_tile
(ptile, plr)->last_updated, 1));
SAVE_PLAYER_MAP_DATA(ptile, file,"player%d.map_uc%03d", plrno,
bin2ascii_hex(map_get_player_tile
(ptile, plr)->last_updated, 2));
SAVE_PLAYER_MAP_DATA(ptile, file, "player%d.map_ud%03d", plrno,
bin2ascii_hex(map_get_player_tile
(ptile, plr)->last_updated, 3));
{
struct vision_base *pdcity;
char impr_buf[MAX_NUM_ITEMS + 1];
i = 0;
whole_map_iterate(ptile) {
if (NULL != (pdcity = map_get_player_base(ptile, plr))
&& VISION_BASE_RUIN < pdcity->identity) {
secfile_insert_int(file, pdcity->identity, "player%d.dc%d.id",
plrno, i);
secfile_insert_int(file, ptile->nat_x,
"player%d.dc%d.x", plrno, i);
secfile_insert_int(file, ptile->nat_y,
"player%d.dc%d.y", plrno, i);
secfile_insert_str(file, pdcity->name, "player%d.dc%d.name",
plrno, i);
secfile_insert_int(file, pdcity->size, "player%d.dc%d.size",
plrno, i);
secfile_insert_bool(file, FALSE,
"player%d.dc%d.has_walls", plrno, i);
secfile_insert_bool(file, pdcity->occupied,
"player%d.dc%d.occupied", plrno, i);
secfile_insert_bool(file, pdcity->walls,
"player%d.dc%d.walls", plrno, i);
secfile_insert_bool(file, pdcity->happy,
"player%d.dc%d.happy", plrno, i);
secfile_insert_bool(file, pdcity->unhappy,
"player%d.dc%d.unhappy", plrno, i);
secfile_insert_int(file, vision_owner(pdcity)->player_no,
"player%d.dc%d.owner", plrno, i);
/* Save improvement list as bitvector. Note that improvement order
* is saved in savefile.improvement_order.
*/
impr_type_iterate(id) {
impr_buf[id] = BV_ISSET(pdcity->improvements, id) ? '1' : '0';
} impr_type_iterate_end;
impr_buf[game.control.num_impr_types] = '\0';
assert(strlen(impr_buf) < sizeof(impr_buf));
secfile_insert_str(file, impr_buf,
"player%d.dc%d.improvements", plrno, i);
i++;
}
} whole_map_iterate_end;
}
secfile_insert_int(file, i, "player%d.total_ncities", plrno);
}
/* This is a big heap of opaque data from the client. Although the binary
* format is not user editable, keep the lines short enough for debugging,
* and hope that data compression will keep the file a reasonable size.
* Note that the "quoted" format is a multiple of 3.
*/
#define PART_SIZE (3*256)
#define PART_ADJUST (3)
if (plr->attribute_block.data) {
char part[PART_SIZE + PART_ADJUST];
int parts;
int current_part_nr;
char *quoted = quote_block(plr->attribute_block.data,
plr->attribute_block.length);
char *quoted_at = strchr(quoted, ':');
size_t bytes_left = strlen(quoted);
size_t bytes_at_colon = 1 + (quoted_at - quoted);
size_t bytes_adjust = bytes_at_colon % PART_ADJUST;
secfile_insert_int(file, plr->attribute_block.length,
"player%d.attribute_v2_block_length", plrno);
secfile_insert_int(file, bytes_left,
"player%d.attribute_v2_block_length_quoted", plrno);
/* Try to wring some compression efficiencies out of the "quoted" format.
* The first line has a variable length decimal, mis-aligning triples.
*/
if ((bytes_left - bytes_adjust) > PART_SIZE) {
/* first line can be longer */
parts = 1 + (bytes_left - bytes_adjust - 1) / PART_SIZE;
} else {
parts = 1;
}
secfile_insert_int(file, parts,
"player%d.attribute_v2_block_parts", plrno);
if (parts > 1) {
size_t size_of_current_part = PART_SIZE + bytes_adjust;
/* first line can be longer */
memcpy(part, quoted, size_of_current_part);
part[size_of_current_part] = '\0';
secfile_insert_str(file, part,
"player%d.attribute_v2_block_data.part%d",
plrno,
0);
bytes_left -= size_of_current_part;
quoted_at = "ed[size_of_current_part];
current_part_nr = 1;
} else {
quoted_at = quoted;
current_part_nr = 0;
}
for (; current_part_nr < parts; current_part_nr++) {
size_t size_of_current_part = MIN(bytes_left, PART_SIZE);
memcpy(part, quoted_at, size_of_current_part);
part[size_of_current_part] = '\0';
secfile_insert_str(file, part,
"player%d.attribute_v2_block_data.part%d",
plrno,
current_part_nr);
bytes_left -= size_of_current_part;
quoted_at = "ed_at[size_of_current_part];
}
assert(bytes_left == 0);
free(quoted);
}
#undef PART_ADJUST
#undef PART_SIZE
}
/***************************************************************
Assign values to ord_city and ord_map for each unit, so the
values can be saved.
***************************************************************/
static void calc_unit_ordering(void)
{
int j;
players_iterate(pplayer) {
/* to avoid junk values for unsupported units: */
unit_list_iterate(pplayer->units, punit) {
punit->ord_city = 0;
} unit_list_iterate_end;
city_list_iterate(pplayer->cities, pcity) {
j = 0;
unit_list_iterate(pcity->units_supported, punit) {
punit->ord_city = j++;
} unit_list_iterate_end;
} city_list_iterate_end;
} players_iterate_end;
whole_map_iterate(ptile) {
j = 0;
unit_list_iterate(ptile->units, punit) {
punit->ord_map = j++;
} unit_list_iterate_end;
} whole_map_iterate_end;
}
/***************************************************************
For each city and tile, sort unit lists according to
ord_city and ord_map values.
***************************************************************/
static void apply_unit_ordering(void)
{
players_iterate(pplayer) {
city_list_iterate(pplayer->cities, pcity) {
unit_list_sort_ord_city(pcity->units_supported);
}
city_list_iterate_end;
} players_iterate_end;
whole_map_iterate(ptile) {
unit_list_sort_ord_map(ptile->units);
} whole_map_iterate_end;
}
/***************************************************************
Old savegames have defects...
***************************************************************/
static void check_city(struct city *pcity)
{
city_map_iterate(x, y) {
bool res = city_can_work_tile(pcity, x, y);
switch (pcity->city_map[x][y]) {
case C_TILE_EMPTY:
if (!res) {
set_worker_city(pcity, x, y, C_TILE_UNAVAILABLE);
freelog(LOG_VERBOSE, "Unavailable tile marked as empty"
" for %s (%d,%d)",
city_name(pcity), x, y);
}
break;
case C_TILE_WORKER:
if (!res) {
struct tile *ptile= city_map_to_map(pcity, x, y);
pcity->specialists[DEFAULT_SPECIALIST]++;
set_worker_city(pcity, x, y, C_TILE_UNAVAILABLE);
freelog(LOG_VERBOSE, "Worked tile was unavailable"
" for %s (%d,%d)",
city_name(pcity), x, y);
map_city_radius_iterate(ptile, tile2) {
struct city *pcity2 = tile_get_city(tile2);
if (pcity2)
check_city(pcity2);
} map_city_radius_iterate_end;
}
break;
case C_TILE_UNAVAILABLE:
if (res) {
set_worker_city(pcity, x, y, C_TILE_EMPTY);
freelog(LOG_VERBOSE, "Empty tile marked as unavailable"
" for %s (%d,%d)",
city_name(pcity), x, y);
}
break;
}
} city_map_iterate_end;
city_refresh(pcity);
}
/***************************************************************
Called only in server/stdinhand.c load_command()
Entire ruleset is always sent afterward.
***************************************************************/
void game_load(struct section_file *file)
{
int i, k, id;
enum server_states tmp_server_state;
RANDOM_STATE rstate;
char *savefile_options;
const char *string;
char** improvement_order = NULL;
int improvement_order_size = 0;
char** technology_order = NULL;
enum tile_special_type *special_order = NULL;
int technology_order_size = 0;
const char* name;
int civstyle = 0;
/* [savefile] */
savefile_options = secfile_lookup_str(file, "savefile.options");
/* We don't need savefile.reason, but read it anyway to avoid
* warnings about unread secfile entries. */
secfile_lookup_str_default(file, "None", "savefile.reason");
if (has_capability("improvement_order", savefile_options)) {
improvement_order = secfile_lookup_str_vec(file, &improvement_order_size,
"savefile.improvement_order");
}
if (has_capability("technology_order", savefile_options)) {
technology_order = secfile_lookup_str_vec(file, &technology_order_size,
"savefile.technology_order");
}
if (has_capability("resources", savefile_options)) {
char **modname;
int nmod;
enum tile_special_type j;
modname = secfile_lookup_str_vec(file, &nmod,
"savefile.specials");
/* make sure that the size of the array is divisible by 4 */
special_order = fc_calloc(nmod + (4 - (nmod % 4)),
sizeof(*special_order));
for (j = 0; j < nmod; j++) {
special_order[j] = find_special_by_rule_name(modname[j]);
}
free(modname);
for (; j < S_LAST + (4 - (S_LAST % 4)); j++) {
special_order[j] = S_LAST;
}
}
/* [game] */
game.version = secfile_lookup_int_default(file, 0, "game.version");
/* we require at least version 1.9.0 */
if (10900 > game.version) {
/* TRANS: Fatal error message. */
freelog(LOG_FATAL,
_("Saved game is too old, at least version 1.9.0 required."));
exit(EXIT_FAILURE);
}
tmp_server_state = (enum server_states)
secfile_lookup_int_default(file, S_S_RUNNING, "game.server_state");
{
set_meta_patches_string(secfile_lookup_str_default(file,
default_meta_patches_string(),
"game.metapatches"));
game.meta_info.user_message_set = secfile_lookup_bool_default(file,
FALSE,
"game.user_metamessage");
if (game.meta_info.user_message_set) {
set_user_meta_message_string(secfile_lookup_str_default(file,
default_meta_message_string(),
"game.metamessage"));
} else {
/* To avoid warnings when loading pre-2.1 savegames */
secfile_lookup_str_default(file, "", "game.metamessage");
}
sz_strlcpy(srvarg.metaserver_addr,
secfile_lookup_str_default(file, DEFAULT_META_SERVER_ADDR,
"game.metaserver"));
game.info.gold = secfile_lookup_int(file, "game.gold");
game.info.tech = secfile_lookup_int(file, "game.tech");
game.info.skill_level = secfile_lookup_int(file, "game.skill_level");
if (game.info.skill_level==0)
game.info.skill_level = GAME_OLD_DEFAULT_SKILL_LEVEL;
game.info.timeout = secfile_lookup_int(file, "game.timeout");
game.timeoutint = secfile_lookup_int_default(file,
GAME_DEFAULT_TIMEOUTINT,
"game.timeoutint");
game.timeoutintinc =
secfile_lookup_int_default(file, GAME_DEFAULT_TIMEOUTINTINC,
"game.timeoutintinc");
game.timeoutinc =
secfile_lookup_int_default(file, GAME_DEFAULT_TIMEOUTINC,
"game.timeoutinc");
game.timeoutincmult =
secfile_lookup_int_default(file, GAME_DEFAULT_TIMEOUTINCMULT,
"game.timeoutincmult");
game.timeoutcounter =
secfile_lookup_int_default(file, 1, "game.timeoutcounter");
game.timeoutaddenemymove
= secfile_lookup_int_default(file, game.timeoutaddenemymove,
"game.timeoutaddenemymove");
game.info.end_year = secfile_lookup_int(file, "game.end_year");
game.info.shieldbox
= secfile_lookup_int_default(file, GAME_DEFAULT_SHIELDBOX,
"game.box_shield");
game.info.sciencebox
= secfile_lookup_int_default(file, 0, "game.box_science");
if (game.info.sciencebox == 0) {
/* Researchcost was used for 2.0 and earlier servers. */
game.info.sciencebox
= 5 * secfile_lookup_int_default(file, 0, "game.researchcost");
if (game.info.sciencebox == 0) {
/* With even earlier servers (?) techlevel was used for this info. */
game.info.sciencebox = 5 * secfile_lookup_int(file, "game.techlevel");
}
}
game.info.year = secfile_lookup_int(file, "game.year");
if (has_capability("turn", savefile_options)) {
game.info.turn = secfile_lookup_int(file, "game.turn");
} else {
game.info.turn = -2;
}
game.info.simultaneous_phases
= secfile_lookup_bool_default(file, TRUE,
"game.simultaneous_phases_now");
game.simultaneous_phases_stored
= secfile_lookup_bool_default(file, TRUE,
"game.simultaneous_phases_stored");
game.info.min_players = secfile_lookup_int(file, "game.min_players");
game.info.max_players = secfile_lookup_int(file, "game.max_players");
game.info.heating = secfile_lookup_int_default(file, 0, "game.heating");
game.info.globalwarming = secfile_lookup_int(file, "game.globalwarming");
game.info.warminglevel = secfile_lookup_int(file, "game.warminglevel");
game.info.cooling = secfile_lookup_int_default(file, 0, "game.cooling");
game.info.nuclearwinter = secfile_lookup_int_default(file, 0, "game.nuclearwinter");
game.info.coolinglevel = secfile_lookup_int_default(file, 8, "game.coolinglevel");
game.info.notradesize =
secfile_lookup_int_default(file, GAME_DEFAULT_NOTRADESIZE, "game.notradesize");
game.info.fulltradesize =
secfile_lookup_int_default(file, GAME_DEFAULT_FULLTRADESIZE, "game.fulltradesize");
game.info.unhappysize =
secfile_lookup_int_default(file, GAME_DEFAULT_UNHAPPYSIZE, "game.unhappysize");
game.info.angrycitizen =
secfile_lookup_bool_default(file, GAME_DEFAULT_ANGRYCITIZEN, "game.angrycitizen");
game.info.cityfactor =
secfile_lookup_int_default(file, GAME_DEFAULT_CITYFACTOR, "game.cityfactor");
game.info.diplcost =
secfile_lookup_int_default(file, GAME_DEFAULT_DIPLCOST, "game.diplcost");
game.info.freecost =
secfile_lookup_int_default(file, GAME_DEFAULT_FREECOST, "game.freecost");
game.info.conquercost =
secfile_lookup_int_default(file, GAME_DEFAULT_CONQUERCOST, "game.conquercost");
game.info.foodbox = secfile_lookup_int_default(file, 0, "game.box_food");
if (game.info.foodbox == 0) {
/* foodbox was used for 2.0 and earlier servers. */
game.info.foodbox = 10 * secfile_lookup_int_default(file, 100, "game.foodbox");
}
game.info.techpenalty =
secfile_lookup_int_default(file, GAME_DEFAULT_TECHPENALTY, "game.techpenalty");
game.info.razechance =
secfile_lookup_int_default(file, GAME_DEFAULT_RAZECHANCE, "game.razechance");
game.info.save_nturns =
secfile_lookup_int_default(file, GAME_DEFAULT_SAVETURNS, "game.save_nturns");
/* suppress warnings about unused entries in old savegames: */
(void) section_file_lookup(file, "game.rail_food");
(void) section_file_lookup(file, "game.rail_prod");
(void) section_file_lookup(file, "game.rail_trade");
(void) section_file_lookup(file, "game.farmfood");
civstyle = secfile_lookup_int_default(file, 2, "game.civstyle");
game.info.citymindist = secfile_lookup_int_default(file,
GAME_DEFAULT_CITYMINDIST, "game.citymindist");
game.info.rapturedelay = secfile_lookup_int_default(file,
GAME_DEFAULT_RAPTUREDELAY, "game.rapturedelay");
/* National borders setting. */
game.info.borders = secfile_lookup_int_default(file, 0, "game.borders");
game.info.happyborders = secfile_lookup_bool_default(file, FALSE,
"game.happyborders");
/* Diplomacy. */
game.info.diplomacy = secfile_lookup_int_default(file, GAME_DEFAULT_DIPLOMACY,
"game.diplomacy");
sz_strlcpy(game.save_name,
secfile_lookup_str_default(file, GAME_DEFAULT_SAVE_NAME,
"game.save_name"));
game.info.aifill = secfile_lookup_int_default(file, 0, "game.aifill");
game.scorelog = secfile_lookup_bool_default(file, FALSE, "game.scorelog");
game.scoreturn =
secfile_lookup_int_default(file, game.info.turn + GAME_DEFAULT_SCORETURN,
"game.scoreturn");
sz_strlcpy(game.id, secfile_lookup_str_default(file, "", "game.id"));
game.info.fogofwar = secfile_lookup_bool_default(file, FALSE, "game.fogofwar");
game.fogofwar_old = game.info.fogofwar;
game.info.civilwarsize =
secfile_lookup_int_default(file, GAME_DEFAULT_CIVILWARSIZE,
"game.civilwarsize");
game.info.contactturns =
secfile_lookup_int_default(file, GAME_DEFAULT_CONTACTTURNS,
"game.contactturns");
if(has_capability("diplchance_percent", savefile_options)) {
game.info.diplchance = secfile_lookup_int_default(file, game.info.diplchance,
"game.diplchance");
} else {
game.info.diplchance = secfile_lookup_int_default(file, 3, /* old default */
"game.diplchance");
if (game.info.diplchance < 2) {
game.info.diplchance = GAME_MAX_DIPLCHANCE;
} else if (game.info.diplchance > 10) {
game.info.diplchance = GAME_MIN_DIPLCHANCE;
} else {
game.info.diplchance = 100 - (10 * (game.info.diplchance - 1));
}
}
game.info.aqueductloss = secfile_lookup_int_default(file, game.info.aqueductloss,
"game.aqueductloss");
game.info.killcitizen = secfile_lookup_int_default(file, game.info.killcitizen,
"game.killcitizen");
game.info.savepalace = secfile_lookup_bool_default(file, game.info.savepalace,
"game.savepalace");
game.info.turnblock = secfile_lookup_bool_default(file, game.info.turnblock,
"game.turnblock");
game.info.fixedlength = secfile_lookup_bool_default(file, game.info.fixedlength,
"game.fixedlength");
game.info.barbarianrate = secfile_lookup_int_default(file, game.info.barbarianrate,
"game.barbarians");
game.info.onsetbarbarian = secfile_lookup_int_default(file, game.info.onsetbarbarian,
"game.onsetbarbs");
game.info.revolution_length
= secfile_lookup_int_default(file, game.info.revolution_length,
"game.revolen");
game.info.nbarbarians = 0; /* counted in player_load for compatibility with
1.10.0 */
game.info.occupychance = secfile_lookup_int_default(file, game.info.occupychance,
"game.occupychance");
game.info.autoattack = secfile_lookup_bool_default(file,
GAME_DEFAULT_AUTOATTACK,
"game.autoattack");
game.seed = secfile_lookup_int_default(file, game.seed,
"game.randseed");
game.info.allowed_city_names =
secfile_lookup_int_default(file, game.info.allowed_city_names,
"game.allowed_city_names");
if(civstyle == 1) {
string = "civ1";
} else {
string = "default";
}
if (!has_capability("rulesetdir", savefile_options)) {
char *str2, *str =
secfile_lookup_str_default(file, "default", "game.info.t.techs");
if (strcmp("classic",
secfile_lookup_str_default(file, "default",
"game.info.t.terrain")) == 0) {
/* TRANS: Fatal error message. */
freelog(LOG_FATAL, _("Saved game uses the \"classic\" terrain"
" ruleset, and is no longer supported."));
exit(EXIT_FAILURE);
}
#define T(x) \
str2 = secfile_lookup_str_default(file, "default", x); \
if (strcmp(str, str2) != 0) { \
freelog(LOG_NORMAL, _("Warning: Different rulesetdirs " \
"('%s' and '%s') are no longer supported. " \
"Using '%s'."), \
str, str2, str); \
}
T("game.info.t.units");
T("game.info.t.buildings");
T("game.info.t.terrain");
T("game.info.t.governments");
T("game.info.t.nations");
T("game.info.t.cities");
T("game.info.t.game");
#undef T
sz_strlcpy(game.rulesetdir, str);
} else {
sz_strlcpy(game.rulesetdir,
secfile_lookup_str_default(file, string,
"game.rulesetdir"));
}
sz_strlcpy(game.demography,
secfile_lookup_str_default(file, GAME_DEFAULT_DEMOGRAPHY,
"game.demography"));
sz_strlcpy(game.allow_take,
secfile_lookup_str_default(file, GAME_DEFAULT_ALLOW_TAKE,
"game.allow_take"));
game.info.spacerace = secfile_lookup_bool_default(file, game.info.spacerace,
"game.spacerace");
game.info.auto_ai_toggle =
secfile_lookup_bool_default(file, game.info.auto_ai_toggle,
"game.auto_ai_toggle");
load_rulesets();
}
/* Free all players from teams, and teams from players
* This must be done while players_iterate() still iterates
* to the previous number of players. */
players_iterate(pplayer) {
team_remove_player(pplayer);
} players_iterate_end;
game.info.nplayers = secfile_lookup_int(file, "game.nplayers");
script_state_load(file);
{
{
{
if (!has_capability("startunits", savefile_options)) {
int settlers = secfile_lookup_int(file, "game.settlers");
int explorer = secfile_lookup_int(file, "game.explorer");
int i;
for (i = 0; settlers > 0 && i < (MAX_LEN_STARTUNIT - 1) ; i++, settlers--) {
game.info.start_units[i] = 'c';
}
for (; explorer > 0 && i < (MAX_LEN_STARTUNIT - 1) ; i++, explorer--) {
game.info.start_units[i] = 'x';
}
game.info.start_units[i] = '\0';
} else {
sz_strlcpy(game.info.start_units,
secfile_lookup_str_default(file,
GAME_DEFAULT_START_UNITS,
"game.start_units"));
}
game.info.dispersion =
secfile_lookup_int_default(file, GAME_DEFAULT_DISPERSION,
"game.dispersion");
}
map.topology_id = secfile_lookup_int_default(file, MAP_ORIGINAL_TOPO,
"map.topology_id");
map.size = secfile_lookup_int_default(file, MAP_DEFAULT_SIZE,
"map.size");
map.riches = secfile_lookup_int(file, "map.riches");
map.huts = secfile_lookup_int(file, "map.huts");
map.generator = secfile_lookup_int(file, "map.generator");
map.seed = secfile_lookup_int(file, "map.seed");
map.landpercent = secfile_lookup_int(file, "map.landpercent");
map.wetness = secfile_lookup_int_default(file, MAP_DEFAULT_WETNESS,
"map.wetness");
map.steepness = secfile_lookup_int_default(file, MAP_DEFAULT_STEEPNESS,
"map.steepness");
map.have_huts = secfile_lookup_bool_default(file, TRUE,
"map.have_huts");
map.temperature =
secfile_lookup_int_default(file,
MAP_DEFAULT_TEMPERATURE, "map.temperature");
map.alltemperate
= secfile_lookup_bool_default(file, MAP_DEFAULT_ALLTEMPERATE,
"map.alltemperate");
map.tinyisles
= secfile_lookup_bool_default(file, MAP_DEFAULT_TINYISLES,
"map.tinyisles");
map.separatepoles
= secfile_lookup_bool_default(file, MAP_DEFAULT_SEPARATE_POLES,
"map.separatepoles");
if (has_capability("startoptions", savefile_options)) {
map.xsize = secfile_lookup_int(file, "map.width");
map.ysize = secfile_lookup_int(file, "map.height");
} else {
/* old versions saved with these names in S_S_INITIAL: */
map.xsize = secfile_lookup_int(file, "map.xsize");
map.ysize = secfile_lookup_int(file, "map.ysize");
}
if (S_S_INITIAL == tmp_server_state && 0 == map.generator) {
/* generator 0 = map done with map editor */
/* aka a "scenario" */
if (has_capability("specials",savefile_options)) {
map_load(file, special_order);
return;
}
map_tiles_load(file);
if (has_capability("riversoverlay",savefile_options)) {
map_rivers_overlay_load(file, special_order);
}
if (has_capability("startpos",savefile_options)) {
map_startpos_load(file);
return;
}
return;
}
}
if (S_S_INITIAL == tmp_server_state) {
return;
}
}
/* We check
1) if the block exists at all.
2) if it is saved. */
if (section_file_lookup(file, "random.index_J")
&& secfile_lookup_bool_default(file, TRUE, "game.save_random")) {
rstate.j = secfile_lookup_int(file,"random.index_J");
rstate.k = secfile_lookup_int(file,"random.index_K");
rstate.x = secfile_lookup_int(file,"random.index_X");
for(i=0;i<8;i++) {
char name[20];
my_snprintf(name, sizeof(name), "random.table%d",i);
string=secfile_lookup_str(file,name);
sscanf(string,"%8x %8x %8x %8x %8x %8x %8x", &rstate.v[7*i],
&rstate.v[7*i+1], &rstate.v[7*i+2], &rstate.v[7*i+3],
&rstate.v[7*i+4], &rstate.v[7*i+5], &rstate.v[7*i+6]);
}
rstate.is_init = TRUE;
set_myrand_state(rstate);
} else {
/* mark it */
(void) secfile_lookup_bool_default(file, TRUE, "game.save_random");
/* We're loading a running game without a seed (which is okay, if it's
* a scenario). We need to generate the game seed now because it will
* be needed later during the load. */
if (S_S_RUNNING == tmp_server_state) {
init_game_seed();
rstate = get_myrand_state();
}
}
game.info.is_new_game = !secfile_lookup_bool_default(file, TRUE,
"game.save_players");
map_load(file, special_order);
if (game.info.is_new_game) {
/* override previous load */
game.info.nplayers = 0;
} else {
/* destroyed wonders: */
string = secfile_lookup_str_default(file, NULL,
"game.destroyed_wonders_new");
if (!string) {
/* old savegames */
string = secfile_lookup_str_default(file, "",
"game.destroyed_wonders");
for (k = 0; string[k]; k++) {
if (string[k] == '1') {
name = old_impr_type_name(k);
id = find_improvement_by_rule_name(name);
if (id != -1) {
game.info.great_wonders[id] = -1;
}
}
}
} else {
for (k = 0; k < improvement_order_size && string[k]; k++) {
if (string[k] == '1') {
id = find_improvement_by_rule_name(improvement_order[k]);
if (id != -1) {
game.info.great_wonders[id] = -1;
}
}
}
}
/* Initialize nations we loaded from rulesets. This has to be after
* map loading and before we seek nations for players */
init_available_nations();
/* Now, load the players. */
players_iterate(pplayer) {
player_load(pplayer, pplayer->player_no, file, improvement_order,
improvement_order_size, technology_order,
technology_order_size);
} players_iterate_end;
/* Some players may have invalid nations in the ruleset. Once all
* players are loaded, pick one of the remaining nations for them.
*/
players_iterate(pplayer) {
if (pplayer->nation == NO_NATION_SELECTED) {
player_set_nation(pplayer, pick_a_nation(NULL, FALSE, TRUE));
/* TRANS: Minor error message: <Leader> ... <Poles>. */
freelog(LOG_ERROR, _("%s had invalid nation; changing to %s."),
player_name(pplayer),
nation_plural_for_player(pplayer));
}
} players_iterate_end;
/* Assign players with no team listed onto an empty team. */
players_iterate(pplayer) {
if (!pplayer->team) {
team_add_player(pplayer, find_empty_team());
}
} players_iterate_end;
/* Sanity check alliances, prevent allied-with-ally-of-enemy */
players_iterate(plr) {
players_iterate(aplayer) {
if (plr->is_alive
&& aplayer->is_alive
&& pplayers_allied(plr, aplayer)
&& pplayer_can_make_treaty(plr, aplayer, DS_ALLIANCE)
== DIPL_ALLIANCE_PROBLEM) {
/* TRANS: ... <Danish> alliance to <Poles>.... */
freelog(LOG_ERROR, _("Illegal alliance structure detected: "
"%s alliance to %s reduced to peace treaty."),
nation_adjective_for_player(plr),
nation_plural_for_player(aplayer));
plr->diplstates[aplayer->player_no].type = DS_PEACE;
aplayer->diplstates[plr->player_no].type = DS_PEACE;
}
} players_iterate_end;
} players_iterate_end;
/* Update all city information. This must come after all cities are
* loaded (in player_load) but before player (dumb) cities are loaded
* (in player_map_load).
*
* This is a bit ugly since generic_city_refresh assumes a city that
* has already been refreshed at some point (even if it's out of date).
* Here we follow the simplest method of just refreshing all cities,
* and updating trade routes at the same time. This could lead to memory
* issues because the refresh might look at some data of another city
* that hasn't itself been refreshed yet. However this shouldn't cause
* any problems because in the end all cities are refreshed once (or
* more) and recursive dependencies are all taken care of. */
cities_iterate(pcity) {
generic_city_refresh(pcity, TRUE, NULL);
} cities_iterate_end;
/* Since the cities must be placed on the map to put them on the
player map we do this afterwards */
for(i = 0; i < game.info.nplayers; i++) {
player_map_load(&game.players[i], i, file, improvement_order,
improvement_order_size, special_order);
}
/* We do this here since if the did it in player_load, player 1
would try to unfog (unloaded) player 2's map when player 1's units
were loaded */
players_iterate(pplayer) {
pplayer->really_gives_vision = 0;
pplayer->gives_shared_vision = 0;
} players_iterate_end;
players_iterate(pplayer) {
char *vision;
int plrno = pplayer->player_no;
vision = secfile_lookup_str_default(file, NULL,
"player%d.gives_shared_vision",
plrno);
if (vision) {
players_iterate(pplayer2) {
if (vision[pplayer2->player_no] == '1') {
give_shared_vision(pplayer, pplayer2);
}
} players_iterate_end;
}
} players_iterate_end;
initialize_globals();
apply_unit_ordering();
/* Make sure everything is consistent. */
players_iterate(pplayer) {
unit_list_iterate(pplayer->units, punit) {
if (!can_unit_continue_current_activity(punit)) {
freelog(LOG_ERROR, "Unit doing illegal activity in savegame!");
punit->activity = ACTIVITY_IDLE;
}
} unit_list_iterate_end;
city_list_iterate(pplayer->cities, pcity) {
check_city(pcity);
} city_list_iterate_end;
} players_iterate_end;
}
if (secfile_lookup_int_default(file, -1,
"game.shuffled_player_%d", 0) >= 0) {
int shuffled_players[game.info.nplayers];
for (i = 0; i < game.info.nplayers; i++) {
shuffled_players[i]
= secfile_lookup_int(file, "game.shuffled_player_%d", i);
}
set_shuffled_players(shuffled_players);
} else {
/* No shuffled players included, so shuffle them (this may include
* scenarios). */
shuffle_players();
}
game.info.player_idx = 0;
game.player_ptr=&game.players[0];
/* Fix ferrying sanity */
players_iterate(pplayer) {
unit_list_iterate_safe(pplayer->units, punit) {
struct unit *ferry = game_find_unit_by_number(punit->transported_by);
if (!ferry && !can_unit_exist_at_tile(punit, punit->tile)) {
freelog(LOG_ERROR, "Removing %s unferried %s in %s at (%d, %d)",
nation_rule_name(nation_of_player(pplayer)),
unit_rule_name(punit),
terrain_rule_name(punit->tile->terrain),
TILE_XY(punit->tile));
bounce_unit(punit, TRUE);
}
} unit_list_iterate_safe_end;
} players_iterate_end;
/* Fix stacking issues. We don't rely on the savegame preserving
* alliance invariants (old savegames often did not) so if there are any
* unallied units on the same tile we just bounce them. */
players_iterate(pplayer) {
players_iterate(aplayer) {
resolve_unit_stacks(pplayer, aplayer, TRUE);
} players_iterate_end;
} players_iterate_end;
players_iterate(pplayer) {
calc_civ_score(pplayer);
} players_iterate_end;
/* Recalculate the potential buildings for each city.
* Has caused some problems with game random state. */
players_iterate(pplayer) {
bool saved_ai_control = pplayer->ai.control;
/* Recalculate for all players. */
pplayer->ai.control = FALSE;
ai_manage_buildings(pplayer);
pplayer->ai.control = saved_ai_control;
} players_iterate_end;
/* Restore game random state, just in case various initialization code
* inexplicably altered the previously existing state. */
if (!game.info.is_new_game) {
set_myrand_state(rstate);
}
}
/***************************************************************
...
***************************************************************/
void game_save(struct section_file *file, const char *save_reason)
{
int i;
int version;
char options[512];
char temp[B_LAST+1];
const char *user_message;
version = MAJOR_VERSION *10000 + MINOR_VERSION *100 + PATCH_VERSION;
secfile_insert_int(file, version, "game.version");
/* Game state: once the game is no longer a new game (ie, has been
* started the first time), it should always be considered a running
* game for savegame purposes:
*/
secfile_insert_int(file, (int) (game.info.is_new_game ? server_state() :
S_S_RUNNING), "game.server_state");
secfile_insert_str(file, get_meta_patches_string(), "game.metapatches");
secfile_insert_bool(file, game.meta_info.user_message_set,
"game.user_metamessage");
user_message = get_user_meta_message_string();
if (user_message != NULL) {
secfile_insert_str(file, user_message, "game.metamessage");
}
secfile_insert_str(file, meta_addr_port(), "game.metaserver");
sz_strlcpy(options, SAVEFILE_OPTIONS);
if (game.info.is_new_game) {
if (map.num_start_positions>0) {
sz_strlcat(options, " startpos");
}
if (map.have_resources) {
sz_strlcat(options, " specials");
}
if (map.have_rivers_overlay && !map.have_resources) {
sz_strlcat(options, " riversoverlay");
}
}
secfile_insert_str(file, options, "savefile.options");
secfile_insert_str(file, save_reason, "savefile.reason");
/* Save improvement order in savegame, so we are not dependent on
* ruleset order.
* If the game isn't started improvements aren't loaded
* so we can not save the order.
*/
if (game.control.num_impr_types > 0) {
const char* buf[game.control.num_impr_types];
impr_type_iterate(id) {
buf[id] = improvement_rule_name(id);
} impr_type_iterate_end;
secfile_insert_str_vec(file, buf, game.control.num_impr_types,
"savefile.improvement_order");
}
/* Save technology order in savegame, so we are not dependent on ruleset
* order. If the game isn't started advances aren't loaded
* so we can not save the order. */
if (game.control.num_tech_types > 0) {
const char* buf[game.control.num_tech_types];
tech_type_iterate(tech) {
if (tech == A_NONE) {
buf[tech] = "A_NONE";
} else {
buf[tech] = advance_rule_name(tech);
}
} tech_type_iterate_end;
secfile_insert_str_vec(file, buf, game.control.num_tech_types,
"savefile.technology_order");
}
{
const char **modname;
/* Save specials order */
modname = fc_calloc(S_LAST, sizeof(*modname));
tile_special_type_iterate(j) {
modname[j] = special_rule_name(j);
} tile_special_type_iterate_end;
secfile_insert_str_vec(file, modname, S_LAST,
"savefile.specials");
free(modname);
}
secfile_insert_int(file, game.info.gold, "game.gold");
secfile_insert_int(file, game.info.tech, "game.tech");
secfile_insert_int(file, game.info.skill_level, "game.skill_level");
secfile_insert_int(file, game.info.timeout, "game.timeout");
secfile_insert_int(file, game.timeoutint, "game.timeoutint");
secfile_insert_int(file, game.timeoutintinc, "game.timeoutintinc");
secfile_insert_int(file, game.timeoutinc, "game.timeoutinc");
secfile_insert_int(file, game.timeoutincmult, "game.timeoutincmult");
secfile_insert_int(file, game.timeoutcounter, "game.timeoutcounter");
secfile_insert_int(file, game.timeoutaddenemymove,
"game.info.timeoutaddenemymove");
secfile_insert_int(file, game.info.end_year, "game.end_year");
secfile_insert_int(file, game.info.year, "game.year");
secfile_insert_int(file, game.info.turn, "game.turn");
secfile_insert_bool(file, game.info.simultaneous_phases,
"game.simultaneous_phases_now");
secfile_insert_bool(file, game.simultaneous_phases_stored,
"game.simultaneous_phases_stored");
secfile_insert_int(file, game.info.min_players, "game.min_players");
secfile_insert_int(file, game.info.max_players, "game.max_players");
secfile_insert_int(file, game.info.nplayers, "game.nplayers");
secfile_insert_int(file, game.info.heating, "game.heating");
secfile_insert_int(file, game.info.globalwarming, "game.globalwarming");
secfile_insert_int(file, game.info.warminglevel, "game.warminglevel");
secfile_insert_int(file, game.info.nuclearwinter, "game.nuclearwinter");
secfile_insert_int(file, game.info.cooling, "game.cooling");
secfile_insert_int(file, game.info.coolinglevel, "game.coolinglevel");
secfile_insert_int(file, game.info.notradesize, "game.notradesize");
secfile_insert_int(file, game.info.fulltradesize, "game.fulltradesize");
secfile_insert_int(file, game.info.unhappysize, "game.unhappysize");
secfile_insert_bool(file, game.info.angrycitizen, "game.angrycitizen");
secfile_insert_int(file, game.info.cityfactor, "game.cityfactor");
secfile_insert_int(file, game.info.citymindist, "game.citymindist");
secfile_insert_int(file, game.info.civilwarsize, "game.civilwarsize");
secfile_insert_int(file, game.info.contactturns, "game.contactturns");
secfile_insert_int(file, game.info.rapturedelay, "game.rapturedelay");
secfile_insert_int(file, game.info.diplcost, "game.diplcost");
secfile_insert_int(file, game.info.freecost, "game.freecost");
secfile_insert_int(file, game.info.conquercost, "game.conquercost");
secfile_insert_int(file, game.info.foodbox, "game.box_food");
secfile_insert_int(file, game.info.shieldbox, "game.box_shield");
secfile_insert_int(file, game.info.sciencebox, "game.box_science");
{
/* These values are for compatibility with 2.0 and previous servers. */
secfile_insert_int(file, game.info.sciencebox / 5, "game.researchcost");
secfile_insert_int(file, game.info.foodbox / 10, "game.foodbox");
}
secfile_insert_int(file, game.info.techpenalty, "game.techpenalty");
secfile_insert_int(file, game.info.razechance, "game.razechance");
/* Write civstyle for compatibility with old servers */
secfile_insert_int(file, 2, "game.civstyle");
secfile_insert_int(file, game.info.save_nturns, "game.save_nturns");
secfile_insert_str(file, game.save_name, "game.save_name");
secfile_insert_int(file, game.info.aifill, "game.aifill");
secfile_insert_bool(file, game.scorelog, "game.scorelog");
secfile_insert_int(file, game.scoreturn, "game.scoreturn");
secfile_insert_str(file, game.id, "game.id");
secfile_insert_bool(file, game.info.fogofwar, "game.fogofwar");
secfile_insert_bool(file, game.info.spacerace, "game.spacerace");
secfile_insert_bool(file, game.info.auto_ai_toggle, "game.auto_ai_toggle");
secfile_insert_int(file, game.info.diplchance, "game.diplchance");
secfile_insert_int(file, game.info.aqueductloss, "game.aqueductloss");
secfile_insert_int(file, game.info.killcitizen, "game.killcitizen");
secfile_insert_bool(file, game.info.turnblock, "game.turnblock");
secfile_insert_bool(file, game.info.savepalace, "game.savepalace");
secfile_insert_bool(file, game.info.fixedlength, "game.fixedlength");
secfile_insert_int(file, game.info.barbarianrate, "game.barbarians");
secfile_insert_int(file, game.info.onsetbarbarian, "game.onsetbarbs");
secfile_insert_int(file, game.info.revolution_length, "game.revolen");
secfile_insert_int(file, game.info.occupychance, "game.occupychance");
secfile_insert_bool(file, game.info.autoattack, "game.autoattack");
secfile_insert_str(file, game.demography, "game.demography");
secfile_insert_str(file, game.allow_take, "game.allow_take");
secfile_insert_int(file, game.info.borders, "game.borders");
secfile_insert_bool(file, game.info.happyborders, "game.happyborders");
secfile_insert_int(file, game.info.diplomacy, "game.diplomacy");
secfile_insert_int(file, game.info.allowed_city_names, "game.allowed_city_names");
/* old (1.14.1) servers need to have these server variables. The values
* don't matter, though. */
secfile_insert_int(file, 2, "game.settlers");
secfile_insert_int(file, 1, "game.explorer");
secfile_insert_int(file, 30, "map.mountains");
secfile_insert_int(file, 35, "map.grass");
secfile_insert_int(file, 5, "map.swampsize");
secfile_insert_int(file, 5, "map.deserts");
secfile_insert_int(file, 5, "map.riverlength");
secfile_insert_int(file, 20, "map.forestsize");
if (TRUE) {
/* Now always save these, so the server options reflect the
* actual values used at the start of the game.
* The first two used to be saved as "map.xsize" and "map.ysize"
* when S_S_INITIAL, but I'm standardizing on width,height --dwp
*/
secfile_insert_int(file, map.topology_id, "map.topology_id");
secfile_insert_int(file, map.size, "map.size");
secfile_insert_int(file, map.xsize, "map.width");
secfile_insert_int(file, map.ysize, "map.height");
secfile_insert_str(file, game.info.start_units, "game.start_units");
secfile_insert_int(file, game.info.dispersion, "game.dispersion");
secfile_insert_int(file, map.seed, "map.seed");
secfile_insert_int(file, map.landpercent, "map.landpercent");
secfile_insert_int(file, map.riches, "map.riches");
secfile_insert_int(file, map.wetness, "map.wetness");
secfile_insert_int(file, map.steepness, "map.steepness");
secfile_insert_int(file, map.huts, "map.huts");
secfile_insert_int(file, map.generator, "map.generator");
secfile_insert_bool(file, map.have_huts, "map.have_huts");
secfile_insert_int(file, map.temperature, "map.temperature");
secfile_insert_bool(file, map.alltemperate, "map.alltemperate");
secfile_insert_bool(file, map.tinyisles, "map.tinyisles");
secfile_insert_bool(file, map.separatepoles, "map.separatepoles");
}
secfile_insert_int(file, game.seed, "game.randseed");
if (myrand_is_init() && game.save_options.save_random) {
RANDOM_STATE rstate = get_myrand_state();
secfile_insert_int(file, 1, "game.save_random");
assert(rstate.is_init);
secfile_insert_int(file, rstate.j, "random.index_J");
secfile_insert_int(file, rstate.k, "random.index_K");
secfile_insert_int(file, rstate.x, "random.index_X");
for (i = 0; i < 8; i++) {
char name[20], vec[100];
my_snprintf(name, sizeof(name), "random.table%d", i);
my_snprintf(vec, sizeof(vec),
"%8x %8x %8x %8x %8x %8x %8x", rstate.v[7 * i],
rstate.v[7 * i + 1], rstate.v[7 * i + 2],
rstate.v[7 * i + 3], rstate.v[7 * i + 4],
rstate.v[7 * i + 5], rstate.v[7 * i + 6]);
secfile_insert_str(file, vec, name);
}
} else {
secfile_insert_int(file, 0, "game.save_random");
}
secfile_insert_str(file, game.rulesetdir, "game.rulesetdir");
if (!map_is_empty()) {
map_save(file);
}
script_state_save(file);
if (game.info.is_new_game && (S_S_INITIAL == server_state())) {
return; /* want to save scenarios as well */
}
secfile_insert_bool(file, game.save_options.save_players,
"game.save_players");
if (game.save_options.save_players) {
/* 1.14 servers depend on improvement order in ruleset. Here we
* are trying to simulate 1.14.1 default order
*/
init_old_improvement_bitvector(temp);
impr_type_iterate(id) {
if (is_great_wonder(id) && great_wonder_was_built(id)
&& !find_city_from_great_wonder(id)) {
add_improvement_into_old_bitvector(temp, id);
}
} impr_type_iterate_end;
secfile_insert_str(file, temp, "game.destroyed_wonders");
/* Save destroyed wonders as bitvector. Note that improvement order
* is saved in savefile.improvement_order
*/
impr_type_iterate(id) {
if (is_great_wonder(id) && great_wonder_was_built(id)
&& !find_city_from_great_wonder(id)) {
temp[id] = '1';
} else {
temp[id] = '0';
}
} impr_type_iterate_end;
temp[game.control.num_impr_types] = '\0';
secfile_insert_str(file, temp, "game.destroyed_wonders_new");
calc_unit_ordering();
players_iterate(pplayer) {
player_save(pplayer, pplayer->player_no, file);
} players_iterate_end;
for (i = 0; i < game.info.nplayers; i++) {
secfile_insert_int(file, shuffled_player(i)->player_no,
"game.shuffled_player_%d", i);
}
}
}
|