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
|
/***********************************************************************
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 <fc_config.h>
#endif
/* utility */
#include "bitvector.h"
#include "fcintl.h"
#include "log.h"
#include "mem.h"
#include "rand.h"
#include "support.h"
/* common */
#include "ai.h"
#include "base.h"
#include "borders.h"
#include "events.h"
#include "game.h"
#include "map.h"
#include "movement.h"
#include "nation.h"
#include "packets.h"
#include "player.h"
#include "road.h"
#include "specialist.h"
#include "unit.h"
#include "unitlist.h"
#include "vision.h"
/* server */
#include "citytools.h"
#include "cityturn.h"
#include "notify.h"
#include "plrhand.h"
#include "sanitycheck.h"
#include "sernet.h"
#include "srv_main.h"
#include "unithand.h"
#include "unittools.h"
/* server/advisors */
#include "advdata.h"
/* server/generator */
#include "mapgen_utils.h"
#include "maphand.h"
#define MAXIMUM_CLAIMED_OCEAN_SIZE (20)
/* Suppress send_tile_info() during game_load() */
static bool send_tile_suppressed = FALSE;
static void player_tile_init(struct tile *ptile, struct player *pplayer);
static void player_tile_free(struct tile *ptile, struct player *pplayer);
static bool give_tile_info_from_player_to_player(struct player *pfrom,
struct player *pdest,
struct tile *ptile);
static void shared_vision_change_seen(struct player *pplayer,
struct tile *ptile,
const v_radius_t change,
bool can_reveal_tiles);
static void map_change_seen(struct player *pplayer,
struct tile *ptile,
const v_radius_t change,
bool can_reveal_tiles);
static void map_change_own_seen(struct player *pplayer,
struct tile *ptile,
const v_radius_t change);
static inline int map_get_seen(const struct player *pplayer,
const struct tile *ptile,
enum vision_layer vlayer);
static inline bool map_is_also_seen(const struct tile *ptile,
const struct player *pplayer,
enum vision_layer vlayer);
static bool is_claimable_ocean(struct tile *ptile, struct tile *source,
struct player *pplayer);
/**********************************************************************//**
Used only in global_warming() and nuclear_winter() below.
**************************************************************************/
static bool is_terrain_ecologically_wet(struct tile *ptile)
{
return (is_terrain_class_near_tile(&(wld.map), ptile, TC_OCEAN)
|| tile_has_river(ptile)
|| count_river_near_tile(&(wld.map), ptile, NULL) > 0);
}
/**********************************************************************//**
Wrapper for climate_change().
**************************************************************************/
void global_warming(int effect)
{
climate_change(TRUE, effect);
notify_player(NULL, NULL, E_GLOBAL_ECO, ftc_server,
_("Global warming has occurred!"));
notify_player(NULL, NULL, E_GLOBAL_ECO, ftc_server,
_("Coastlines have been flooded and vast "
"ranges of grassland have become deserts."));
}
/**********************************************************************//**
Wrapper for climate_change().
**************************************************************************/
void nuclear_winter(int effect)
{
climate_change(FALSE, effect);
notify_player(NULL, NULL, E_GLOBAL_ECO, ftc_server,
_("Nuclear winter has occurred!"));
notify_player(NULL, NULL, E_GLOBAL_ECO, ftc_server,
_("Wetlands have dried up and vast "
"ranges of grassland have become tundra."));
}
/**********************************************************************//**
Do a climate change. Global warming occurred if 'warming' is TRUE, else
there is a nuclear winter.
**************************************************************************/
void climate_change(bool warming, int effect)
{
int k = map_num_tiles();
bool used[k];
const struct civ_map *nmap = &(wld.map);
memset(used, 0, sizeof(used));
log_verbose("Climate change: %s (%d)",
warming ? "Global warming" : "Nuclear winter", effect);
while (effect > 0 && (k--) > 0) {
struct terrain *old, *candidates[2], *new;
struct tile *ptile;
int i;
do {
/* We want to transform a tile at most once due to a climate change. */
ptile = rand_map_pos(&(wld.map));
} while (used[tile_index(ptile)]);
used[tile_index(ptile)] = TRUE;
old = tile_terrain(ptile);
/* Prefer the transformation that's appropriate to the ambient moisture,
* but be prepared to fall back in exceptional circumstances */
{
struct terrain *wetter, *drier;
wetter = warming ? old->warmer_wetter_result : old->cooler_wetter_result;
drier = warming ? old->warmer_drier_result : old->cooler_drier_result;
if (is_terrain_ecologically_wet(ptile)) {
candidates[0] = wetter;
candidates[1] = drier;
} else {
candidates[0] = drier;
candidates[1] = wetter;
}
}
/* If the preferred transformation is ruled out for some exceptional reason
* specific to this tile, fall back to the other, rather than letting this
* tile be immune to change. */
for (i = 0; i < 2; i++) {
new = candidates[i];
/* If the preferred transformation simply hasn't been specified
* for this terrain at all, don't fall back to the other. */
if (new == T_NONE) {
break;
}
if (tile_city(ptile) != NULL && terrain_has_flag(new, TER_NO_CITIES)) {
/* do not change to a terrain with the flag TER_NO_CITIES if the tile
* has a city */
continue;
}
/* Only change between water and land at coastlines, and between
* frozen and unfrozen at ice margin */
if (!terrain_surroundings_allow_change(nmap, ptile, new)) {
continue;
}
/* OK! */
break;
}
if (i == 2) {
/* Neither transformation was permitted. Give up. */
continue;
}
if (new != T_NONE && old != new) {
effect--;
/* Check terrain changing activities.
These would not be caught by the check if unit can continue activity
after the terrain change has taken place, as the activity itself is still
legal, but would be towards different terrain, and terrain types are not
activity targets (target is NULL) */
unit_list_iterate(ptile->units, punit) {
if (punit->activity_target == NULL) {
/* Target is always NULL for terrain changing activities. */
if (punit->activity == ACTIVITY_CULTIVATE
|| punit->activity == ACTIVITY_PLANT
|| punit->activity == ACTIVITY_TRANSFORM) {
unit_activities_cancel(punit);
}
}
} unit_list_iterate_end;
/* Really change the terrain. */
tile_change_terrain(ptile, new);
check_terrain_change(ptile, old);
update_tile_knowledge(ptile);
tile_change_side_effects(ptile, FALSE);
} else if (old == new) {
/* This counts toward a climate change although nothing is changed. */
effect--;
}
}
}
/**********************************************************************//**
Check city for extra upgrade. Returns whether anything was done.
*gained will be set if there's exactly one kind of extra added.
**************************************************************************/
bool upgrade_city_extras(struct city *pcity, struct extra_type **gained)
{
struct tile *ptile = pcity->tile;
struct player *pplayer = city_owner(pcity);
bool upgradet = FALSE;
extra_type_iterate(pextra) {
if (!tile_has_extra(ptile, pextra)) {
if (extra_has_flag(pextra, EF_ALWAYS_ON_CITY_CENTER)
|| (extra_has_flag(pextra, EF_AUTO_ON_CITY_CENTER)
&& player_can_build_extra(pextra, pplayer, ptile)
&& !tile_has_conflicting_extra(ptile, pextra))) {
tile_add_extra(pcity->tile, pextra);
if (gained != NULL) {
if (upgradet) {
*gained = NULL;
} else {
*gained = pextra;
}
}
upgradet = TRUE;
}
}
} extra_type_iterate_end;
return upgradet;
}
/**********************************************************************//**
To be called when a player gains some better extra building tech
for the first time. Sends a message, and upgrades all city
squares to new extras. "discovery" just affects the message: set to
1 if the tech is a "discovery",
0 if otherwise acquired (conquer/trade/GLib). --dwp
**************************************************************************/
void upgrade_all_city_extras(struct player *pplayer, bool discovery)
{
int cities_upgradet = 0;
struct extra_type *upgradet = NULL;
bool multiple_types = FALSE;
int cities_total = city_list_size(pplayer->cities);
int percent;
conn_list_do_buffer(pplayer->connections);
city_list_iterate(pplayer->cities, pcity) {
struct extra_type *new_upgrade;
if (upgrade_city_extras(pcity, &new_upgrade)) {
update_tile_knowledge(pcity->tile);
cities_upgradet++;
if (new_upgrade == NULL) {
/* This single city alone had multiple types */
multiple_types = TRUE;
} else if (upgradet == NULL) {
/* First gained */
upgradet = new_upgrade;
} else if (upgradet != new_upgrade) {
/* Different type from what another city got. */
multiple_types = TRUE;
}
}
} city_list_iterate_end;
if (cities_total > 0) {
percent = cities_upgradet * 100 / cities_total;
} else {
percent = 0;
}
if (cities_upgradet > 0) {
if (discovery) {
if (percent >= 75) {
notify_player(pplayer, NULL, E_TECH_GAIN, ftc_server,
_("New hope sweeps like fire through the country as "
"the discovery of new infrastructure building technology "
"is announced."));
}
} else {
if (percent >= 75) {
notify_player(pplayer, NULL, E_TECH_GAIN, ftc_server,
_("The people are pleased to hear that your "
"scientists finally know about new infrastructure building "
"technology."));
}
}
if (multiple_types) {
notify_player(pplayer, NULL, E_TECH_GAIN, ftc_server,
_("Workers spontaneously gather and upgrade all "
"possible cities with better infrastructure."));
} else {
notify_player(pplayer, NULL, E_TECH_GAIN, ftc_server,
_("Workers spontaneously gather and upgrade all "
"possible cities with %s."), extra_name_translation(upgradet));
}
}
conn_list_do_unbuffer(pplayer->connections);
}
/**********************************************************************//**
Return TRUE iff the player me really gives shared vision to player them.
**************************************************************************/
bool really_gives_vision(struct player *me, struct player *them)
{
return BV_ISSET(me->server.really_gives_vision, player_index(them));
}
/**********************************************************************//**
Start buffering shared vision
**************************************************************************/
static void buffer_shared_vision(struct player *pplayer)
{
players_iterate(pplayer2) {
if (really_gives_vision(pplayer, pplayer2)) {
conn_list_compression_freeze(pplayer2->connections);
conn_list_do_buffer(pplayer2->connections);
}
} players_iterate_end;
conn_list_compression_freeze(pplayer->connections);
conn_list_do_buffer(pplayer->connections);
}
/**********************************************************************//**
Stop buffering shared vision
**************************************************************************/
static void unbuffer_shared_vision(struct player *pplayer)
{
players_iterate(pplayer2) {
if (really_gives_vision(pplayer, pplayer2)) {
conn_list_do_unbuffer(pplayer2->connections);
conn_list_compression_thaw(pplayer2->connections);
}
} players_iterate_end;
conn_list_do_unbuffer(pplayer->connections);
conn_list_compression_thaw(pplayer->connections);
}
/**********************************************************************//**
Give information about whole map (all tiles) from player to player.
Takes care of shared vision chains.
**************************************************************************/
void give_map_from_player_to_player(struct player *pfrom, struct player *pdest)
{
buffer_shared_vision(pdest);
whole_map_iterate(&(wld.map), ptile) {
give_tile_info_from_player_to_player(pfrom, pdest, ptile);
} whole_map_iterate_end;
unbuffer_shared_vision(pdest);
city_thaw_workers_queue();
sync_cities();
}
/**********************************************************************//**
Give information about all oceanic tiles from player to player
**************************************************************************/
void give_seamap_from_player_to_player(struct player *pfrom, struct player *pdest)
{
buffer_shared_vision(pdest);
whole_map_iterate(&(wld.map), ptile) {
if (is_ocean_tile(ptile)) {
give_tile_info_from_player_to_player(pfrom, pdest, ptile);
}
} whole_map_iterate_end;
unbuffer_shared_vision(pdest);
city_thaw_workers_queue();
sync_cities();
}
/**********************************************************************//**
Give information about tiles within city radius from player to player
**************************************************************************/
void give_citymap_from_player_to_player(struct city *pcity,
struct player *pfrom, struct player *pdest)
{
struct tile *pcenter = city_tile(pcity);
const struct civ_map *nmap = &(wld.map);
buffer_shared_vision(pdest);
city_tile_iterate(nmap, city_map_radius_sq_get(pcity), pcenter, ptile) {
give_tile_info_from_player_to_player(pfrom, pdest, ptile);
} city_tile_iterate_end;
unbuffer_shared_vision(pdest);
city_thaw_workers_queue();
sync_cities();
}
/**********************************************************************//**
Send all tiles known to specified clients.
If dest is NULL means game.est_connections.
Note for multiple connections this may change "sent" multiple times
for single player. This is ok, because "sent" data is just optimised
calculations, so it will be correct before this, for each connection
during this, and at end.
**************************************************************************/
void send_all_known_tiles(struct conn_list *dest)
{
int tiles_sent;
if (!dest) {
dest = game.est_connections;
}
/* send whole map piece by piece to each player to balance the load
of the send buffers better */
tiles_sent = 0;
conn_list_do_buffer(dest);
whole_map_iterate(&(wld.map), ptile) {
tiles_sent++;
if ((tiles_sent % wld.map.xsize) == 0) {
conn_list_do_unbuffer(dest);
flush_packets();
conn_list_do_buffer(dest);
}
send_tile_info(dest, ptile, FALSE);
} whole_map_iterate_end;
conn_list_do_unbuffer(dest);
flush_packets();
}
/**********************************************************************//**
Suppress send_tile_info() during game_load()
**************************************************************************/
bool send_tile_suppression(bool now)
{
bool formerly = send_tile_suppressed;
send_tile_suppressed = now;
return formerly;
}
/**********************************************************************//**
Send tile information to all the clients in dest which know and see
the tile. If dest is NULL, sends to all clients (game.est_connections)
which know and see tile.
Note that this function does not update the playermap. For that call
update_tile_knowledge().
**************************************************************************/
void send_tile_info(struct conn_list *dest, struct tile *ptile,
bool send_unknown)
{
struct packet_tile_info info;
const struct player *owner;
const struct player *eowner;
if (dest == NULL) {
CALL_FUNC_EACH_AI(tile_info, ptile);
}
if (send_tile_suppressed) {
return;
}
if (!dest) {
dest = game.est_connections;
}
info.tile = tile_index(ptile);
if (ptile->spec_sprite) {
sz_strlcpy(info.spec_sprite, ptile->spec_sprite);
} else {
info.spec_sprite[0] = '\0';
}
conn_list_iterate(dest, pconn) {
struct player *pplayer = pconn->playing;
bool known;
if (NULL == pplayer && !pconn->observer) {
continue;
}
if (pplayer != NULL) {
known = map_is_known(ptile, pplayer);
}
if (pplayer == NULL || (known && map_is_also_seen(ptile, pplayer, V_MAIN))) {
struct extra_type *resource;
info.known = TILE_KNOWN_SEEN;
info.continent = tile_continent(ptile);
owner = tile_owner(ptile);
eowner = extra_owner(ptile);
info.owner = (owner ? player_number(owner) : MAP_TILE_OWNER_NULL);
info.extras_owner = (eowner ? player_number(eowner) : MAP_TILE_OWNER_NULL);
info.worked = (NULL != tile_worked(ptile))
? tile_worked(ptile)->id
: IDENTITY_NUMBER_ZERO;
info.terrain = (NULL != tile_terrain(ptile))
? terrain_number(tile_terrain(ptile))
: terrain_count();
resource = tile_resource(ptile);
if (resource != NULL
&& (pplayer == NULL
|| player_knows_extra_exist(pplayer, resource, ptile))) {
info.resource = extra_number(resource);
} else {
info.resource = MAX_EXTRA_TYPES;
}
info.placing = (NULL != ptile->placing)
? extra_number(ptile->placing)
: -1;
info.place_turn = (NULL != ptile->placing)
? game.info.turn + ptile->infra_turns
: 0;
if (pplayer != NULL) {
dbv_to_bv(info.extras.vec, &(map_get_player_tile(ptile, pplayer)->extras));
} else {
info.extras = ptile->extras;
}
if (ptile->label != NULL) {
/* Always leave final '\0' in place */
strncpy(info.label, ptile->label, sizeof(info.label) - 1);
} else {
info.label[0] = '\0';
}
send_packet_tile_info(pconn, &info);
} else if (pplayer != NULL && known) {
struct player_tile *plrtile = map_get_player_tile(ptile, pplayer);
struct vision_site *psite = map_get_playermap_site(plrtile);
info.known = TILE_KNOWN_UNSEEN;
info.continent = tile_continent(ptile);
owner = (game.server.foggedborders
? plrtile->owner
: tile_owner(ptile));
eowner = plrtile->extras_owner;
info.owner = (owner ? player_number(owner) : MAP_TILE_OWNER_NULL);
info.extras_owner = (eowner ? player_number(eowner) : MAP_TILE_OWNER_NULL);
info.worked = (NULL != psite)
? psite->identity
: IDENTITY_NUMBER_ZERO;
info.terrain = (NULL != plrtile->terrain)
? terrain_number(plrtile->terrain)
: terrain_count();
info.resource = (NULL != plrtile->resource)
? extra_number(plrtile->resource)
: MAX_EXTRA_TYPES;
info.placing = -1;
info.place_turn = 0;
dbv_to_bv(info.extras.vec, &(plrtile->extras));
/* Labels never change, so they are not subject to fog of war */
if (ptile->label != NULL) {
sz_strlcpy(info.label, ptile->label);
} else {
info.label[0] = '\0';
}
send_packet_tile_info(pconn, &info);
} else if (send_unknown) {
info.known = TILE_UNKNOWN;
info.continent = 0;
info.owner = MAP_TILE_OWNER_NULL;
info.extras_owner = MAP_TILE_OWNER_NULL;
info.worked = IDENTITY_NUMBER_ZERO;
info.terrain = terrain_count();
info.resource = MAX_EXTRA_TYPES;
info.placing = -1;
info.place_turn = 0;
BV_CLR_ALL(info.extras);
info.label[0] = '\0';
send_packet_tile_info(pconn, &info);
}
}
conn_list_iterate_end;
}
/**********************************************************************//**
Return whether unit is on this particular layer.
Callers assume that each unit is in just one layer, i.e.,
though all units can be seen on V_MAIN, this returns FALSE
for layer V_MAIN for units that are visible ALSO in other layers.
**************************************************************************/
static bool unit_is_on_layer(const struct unit *punit,
enum vision_layer vlayer)
{
return unit_type_get(punit)->vlayer == vlayer;
}
/**********************************************************************//**
Send basic map information: map size, topology, and is_earth.
**************************************************************************/
void send_map_info(struct conn_list *dest)
{
struct packet_map_info minfo;
minfo.xsize = wld.map.xsize;
minfo.ysize = wld.map.ysize;
minfo.topology_id = wld.map.topology_id;
minfo.wrap_id = wld.map.wrap_id;
minfo.north_latitude = wld.map.north_latitude;
minfo.south_latitude = wld.map.south_latitude;
lsend_packet_map_info(dest, &minfo);
}
/**********************************************************************//**
Change the seen count of a tile for a pplayer. It will automatically
handle the shared visions.
**************************************************************************/
static void shared_vision_change_seen(struct player *pplayer,
struct tile *ptile,
const v_radius_t change,
bool can_reveal_tiles)
{
map_change_own_seen(pplayer, ptile, change);
map_change_seen(pplayer, ptile, change, can_reveal_tiles);
players_iterate(pplayer2) {
if (really_gives_vision(pplayer, pplayer2)) {
map_change_seen(pplayer2, ptile, change, can_reveal_tiles);
}
} players_iterate_end;
}
/**********************************************************************//**
There doesn't have to be a city.
**************************************************************************/
void map_vision_update(struct player *pplayer, struct tile *ptile,
const v_radius_t old_radius_sq,
const v_radius_t new_radius_sq,
bool can_reveal_tiles)
{
v_radius_t change;
int max_radius;
if (old_radius_sq[V_MAIN] == new_radius_sq[V_MAIN]
&& old_radius_sq[V_INVIS] == new_radius_sq[V_INVIS]
&& old_radius_sq[V_SUBSURFACE] == new_radius_sq[V_SUBSURFACE]) {
return;
}
/* Determines 'max_radius' value. */
max_radius = 0;
vision_layer_iterate(v) {
if (max_radius < old_radius_sq[v]) {
max_radius = old_radius_sq[v];
}
if (max_radius < new_radius_sq[v]) {
max_radius = new_radius_sq[v];
}
} vision_layer_iterate_end;
#ifdef FREECIV_DEBUG
log_debug("Updating vision at (%d, %d) in a radius of %d.",
TILE_XY(ptile), max_radius);
vision_layer_iterate(v) {
log_debug(" vision layer %d is changing from %d to %d.",
v, old_radius_sq[v], new_radius_sq[v]);
} vision_layer_iterate_end;
#endif /* FREECIV_DEBUG */
buffer_shared_vision(pplayer);
circle_dxyr_iterate(&(wld.map), ptile, max_radius, tile1, dx, dy, dr) {
vision_layer_iterate(v) {
if (dr > old_radius_sq[v] && dr <= new_radius_sq[v]) {
change[v] = 1;
} else if (dr > new_radius_sq[v] && dr <= old_radius_sq[v]) {
change[v] = -1;
} else {
change[v] = 0;
}
} vision_layer_iterate_end;
shared_vision_change_seen(pplayer, tile1, change, can_reveal_tiles);
} circle_dxyr_iterate_end;
unbuffer_shared_vision(pplayer);
}
/**********************************************************************//**
Turn a player's ability to see inside their borders on or off.
It is safe to set the current value.
**************************************************************************/
void map_set_border_vision(struct player *pplayer,
const bool is_enabled)
{
const v_radius_t radius_sq = V_RADIUS(is_enabled ? 1 : -1, 0, 0);
if (pplayer->server.border_vision == is_enabled) {
/* No change. Changing the seen count beyond what already exists would
* be a bug. */
return;
}
/* Set the new border seer value. */
pplayer->server.border_vision = is_enabled;
whole_map_iterate(&(wld.map), ptile) {
if (pplayer == ptile->owner) {
/* The tile is within the player's borders. */
shared_vision_change_seen(pplayer, ptile, radius_sq, TRUE);
}
} whole_map_iterate_end;
}
/**********************************************************************//**
Shows the area to the player. Unless the tile is "seen", it will remain
fogged and units will be hidden.
Callers may wish to buffer_shared_vision() before calling this function.
**************************************************************************/
void map_show_tile(struct player *src_player, struct tile *ptile)
{
static int recurse = 0;
log_debug("Showing %i,%i to %s", TILE_XY(ptile), player_name(src_player));
fc_assert(recurse == 0);
recurse++;
players_iterate(pplayer) {
if (pplayer == src_player || really_gives_vision(src_player, pplayer)) {
struct city *pcity;
if (!map_is_known_and_seen(ptile, pplayer, V_MAIN)) {
map_set_known(ptile, pplayer);
/* As the tile may be fogged send_tile_info won't always do this for us */
update_player_tile_knowledge(pplayer, ptile);
update_player_tile_last_seen(pplayer, ptile);
send_tile_info(pplayer->connections, ptile, FALSE);
/* Remove old cities that exist no more */
reality_check_city(pplayer, ptile);
if ((pcity = tile_city(ptile))) {
/* As the tile may be fogged send_city_info won't do this for us */
update_dumb_city(pplayer, pcity);
send_city_info(pplayer, pcity);
}
vision_layer_iterate(v) {
if (0 < map_get_seen(pplayer, ptile, v)) {
unit_list_iterate(ptile->units, punit) {
if (unit_is_on_layer(punit, v)) {
send_unit_info(pplayer->connections, punit);
}
} unit_list_iterate_end;
}
} vision_layer_iterate_end;
}
}
} players_iterate_end;
recurse--;
}
/**********************************************************************//**
Hides the area to the player.
Callers may wish to buffer_shared_vision() before calling this function.
**************************************************************************/
void map_hide_tile(struct player *src_player, struct tile *ptile)
{
static int recurse = 0;
log_debug("Hiding %d,%d to %s", TILE_XY(ptile), player_name(src_player));
fc_assert(recurse == 0);
recurse++;
players_iterate(pplayer) {
if (pplayer == src_player || really_gives_vision(src_player, pplayer)) {
if (map_is_known(ptile, pplayer)) {
if (0 < map_get_seen(pplayer, ptile, V_MAIN)) {
update_player_tile_last_seen(pplayer, ptile);
}
/* Remove city. */
remove_dumb_city(pplayer, ptile);
/* Remove units. */
vision_layer_iterate(v) {
if (0 < map_get_seen(pplayer, ptile, v)) {
unit_list_iterate(ptile->units, punit) {
if (unit_is_on_layer(punit, v)) {
unit_goes_out_of_sight(pplayer, punit);
}
} unit_list_iterate_end;
}
} vision_layer_iterate_end;
}
map_clear_known(ptile, pplayer);
send_tile_info(pplayer->connections, ptile, TRUE);
}
} players_iterate_end;
recurse--;
}
/**********************************************************************//**
Shows the area to the player. Unless the tile is "seen", it will remain
fogged and units will be hidden.
**************************************************************************/
void map_show_circle(struct player *pplayer, struct tile *ptile, int radius_sq)
{
buffer_shared_vision(pplayer);
circle_iterate(&(wld.map), ptile, radius_sq, tile1) {
map_show_tile(pplayer, tile1);
} circle_iterate_end;
unbuffer_shared_vision(pplayer);
}
/**********************************************************************//**
Shows the area to the player. Unless the tile is "seen", it will remain
fogged and units will be hidden.
**************************************************************************/
void map_show_all(struct player *pplayer)
{
buffer_shared_vision(pplayer);
whole_map_iterate(&(wld.map), ptile) {
map_show_tile(pplayer, ptile);
} whole_map_iterate_end;
unbuffer_shared_vision(pplayer);
}
/**********************************************************************//**
Return whether the player knows the tile. Knowing a tile means you've
seen it once (as opposed to seeing a tile which means you can see it now).
**************************************************************************/
bool map_is_known(const struct tile *ptile, const struct player *pplayer)
{
if (pplayer->tile_known.vec == NULL) {
/* Player map not initialized yet */
return FALSE;
}
return dbv_isset(&pplayer->tile_known, tile_index(ptile));
}
/**********************************************************************//**
Returns whether the layer 'vlayer' of the tile 'ptile' is seen
by the player 'pplayer'. Only call this when you already know
the tile to be known.
**************************************************************************/
static inline bool map_is_also_seen(const struct tile *ptile,
const struct player *pplayer,
enum vision_layer vlayer)
{
return map_get_seen(pplayer, ptile, vlayer) > 0;
}
/**********************************************************************//**
Returns whether the layer 'vlayer' of the tile 'ptile' is known and seen
by the player 'pplayer'.
**************************************************************************/
bool map_is_known_and_seen(const struct tile *ptile,
const struct player *pplayer,
enum vision_layer vlayer)
{
return map_is_known(ptile, pplayer) && map_is_also_seen(ptile, pplayer, vlayer);
}
/**********************************************************************//**
Return whether the player can see the tile. Seeing a tile means you have
vision of it now (as opposed to knowing a tile which means you've seen it
before). Note that a tile can be seen but not known (currently this only
happens when a city is founded with some unknown tiles in its radius); in
this case the tile is unknown (but map_get_seen() will still return TRUE).
**************************************************************************/
static inline int map_get_seen(const struct player *pplayer,
const struct tile *ptile,
enum vision_layer vlayer)
{
return map_get_player_tile(ptile, pplayer)->seen_count[vlayer];
}
/**********************************************************************//**
This function changes the seen state of one player for all vision layers
of a tile. It reveals the tiles if needed and controls the fog of war.
See also map_change_own_seen(), shared_vision_change_seen().
**************************************************************************/
void map_change_seen(struct player *pplayer,
struct tile *ptile,
const v_radius_t change,
bool can_reveal_tiles)
{
struct player_tile *plrtile = map_get_player_tile(ptile, pplayer);
bool revealing_tile = FALSE;
#ifdef FREECIV_DEBUG
log_debug("%s() for player %s (nb %d) at (%d, %d).",
__FUNCTION__, player_name(pplayer), player_number(pplayer),
TILE_XY(ptile));
vision_layer_iterate(v) {
log_debug(" vision layer %d is changing from %d to %d.",
v, plrtile->seen_count[v], plrtile->seen_count[v] + change[v]);
} vision_layer_iterate_end;
#endif /* FREECIV_DEBUG */
/* Removes units out of vision. First, check invisible layers because
* we must remove all units before fog of war because clients expect
* the tile is empty when it is fogged. */
if (0 > change[V_INVIS]
&& plrtile->seen_count[V_INVIS] == -change[V_INVIS]) {
log_debug("(%d, %d): hiding invisible units to player %s (nb %d).",
TILE_XY(ptile), player_name(pplayer), player_number(pplayer));
unit_list_iterate(ptile->units, punit) {
if (unit_is_on_layer(punit, V_INVIS)
&& can_player_see_unit(pplayer, punit)
&& (plrtile->seen_count[V_MAIN] + change[V_MAIN] <= 0
|| !pplayers_allied(pplayer, unit_owner(punit)))) {
/* Allied units on seen tiles (V_MAIN) are always seen.
* That's how can_player_see_unit_at() works. */
unit_goes_out_of_sight(pplayer, punit);
}
} unit_list_iterate_end;
}
if (0 > change[V_SUBSURFACE]
&& plrtile->seen_count[V_SUBSURFACE] == -change[V_SUBSURFACE]) {
log_debug("(%d, %d): hiding subsurface units to player %s (nb %d).",
TILE_XY(ptile), player_name(pplayer), player_number(pplayer));
unit_list_iterate(ptile->units, punit) {
if (unit_is_on_layer(punit, V_SUBSURFACE)
&& can_player_see_unit(pplayer, punit)) {
unit_goes_out_of_sight(pplayer, punit);
}
} unit_list_iterate_end;
}
if (0 > change[V_MAIN]
&& plrtile->seen_count[V_MAIN] == -change[V_MAIN]) {
log_debug("(%d, %d): hiding visible units to player %s (nb %d).",
TILE_XY(ptile), player_name(pplayer), player_number(pplayer));
unit_list_iterate(ptile->units, punit) {
if (unit_is_on_layer(punit, V_MAIN)
&& can_player_see_unit(pplayer, punit)) {
unit_goes_out_of_sight(pplayer, punit);
}
} unit_list_iterate_end;
}
vision_layer_iterate(v) {
/* Avoid underflow. */
fc_assert(0 <= change[v] || -change[v] <= plrtile->seen_count[v]);
plrtile->seen_count[v] += change[v];
} vision_layer_iterate_end;
/* V_MAIN vision ranges must always be more than invisible ranges
* (see comment in common/vision.h), so we assume that the V_MAIN
* seen count cannot be inferior to V_INVIS or V_SUBSURFACE seen count.
* Moreover, when the fog of war is disabled, V_MAIN has an extra
* seen count point. */
fc_assert(plrtile->seen_count[V_INVIS] + !game.info.fogofwar
<= plrtile->seen_count[V_MAIN]);
fc_assert(plrtile->seen_count[V_SUBSURFACE] + !game.info.fogofwar
<= plrtile->seen_count[V_MAIN]);
if (!map_is_known(ptile, pplayer)) {
if (0 < plrtile->seen_count[V_MAIN] && can_reveal_tiles) {
log_debug("(%d, %d): revealing tile to player %s (nb %d).",
TILE_XY(ptile), player_name(pplayer),
player_number(pplayer));
map_set_known(ptile, pplayer);
revealing_tile = TRUE;
} else {
return;
}
}
/* Fog the tile. */
if (0 > change[V_MAIN] && 0 == plrtile->seen_count[V_MAIN]) {
struct city *pcity;
log_debug("(%d, %d): fogging tile for player %s (nb %d).",
TILE_XY(ptile), player_name(pplayer), player_number(pplayer));
pcity = ptile->worked;
if (pcity != NULL && city_owner(pcity) == pplayer) {
city_map_update_empty(pcity, ptile);
pcity->specialists[DEFAULT_SPECIALIST]++;
if (pcity->server.needs_arrange == CNA_NOT) {
pcity->server.needs_arrange = CNA_NORMAL;
}
}
update_player_tile_last_seen(pplayer, ptile);
if (game.server.foggedborders) {
plrtile->owner = tile_owner(ptile);
}
plrtile->extras_owner = extra_owner(ptile);
send_tile_info(pplayer->connections, ptile, FALSE);
}
if ((revealing_tile && 0 < plrtile->seen_count[V_MAIN])
|| (0 < change[V_MAIN]
/* plrtile->seen_count[V_MAIN] Always set to 1
* when the fog of war is disabled. */
&& (change[V_MAIN] + !game.info.fogofwar
== (plrtile->seen_count[V_MAIN])))) {
struct city *pcity;
log_debug("(%d, %d): unfogging tile for player %s (nb %d).",
TILE_XY(ptile), player_name(pplayer), player_number(pplayer));
/* Send info about the tile itself.
* It has to be sent first because the client needs correct
* continent number before it can handle following packets
*/
update_player_tile_knowledge(pplayer, ptile);
send_tile_info(pplayer->connections, ptile, FALSE);
/* Discover units. */
unit_list_iterate(ptile->units, punit) {
/* Be sure not to revive dead unit on client when it's not yet
* removed from the tile. This could happen when "unit_lost" lua script
* somehow causes tile of the dead unit to unfog again. */
if (unit_is_on_layer(punit, V_MAIN)
&& !punit->server.dying) {
send_unit_info(pplayer->connections, punit);
}
} unit_list_iterate_end;
/* Discover cities. */
reality_check_city(pplayer, ptile);
if (NULL != (pcity = tile_city(ptile))) {
send_city_info(pplayer, pcity);
}
}
if ((revealing_tile && 0 < plrtile->seen_count[V_INVIS])
|| (0 < change[V_INVIS]
&& change[V_INVIS] == plrtile->seen_count[V_INVIS])) {
log_debug("(%d, %d): revealing invisible units to player %s (nb %d).",
TILE_XY(ptile), player_name(pplayer),
player_number(pplayer));
/* Discover units. */
unit_list_iterate(ptile->units, punit) {
if (unit_is_on_layer(punit, V_INVIS)) {
send_unit_info(pplayer->connections, punit);
}
} unit_list_iterate_end;
}
if ((revealing_tile && 0 < plrtile->seen_count[V_SUBSURFACE])
|| (0 < change[V_SUBSURFACE]
&& change[V_SUBSURFACE] == plrtile->seen_count[V_SUBSURFACE])) {
log_debug("(%d, %d): revealing subsurface units to player %s (nb %d).",
TILE_XY(ptile), player_name(pplayer),
player_number(pplayer));
/* Discover units. */
unit_list_iterate(ptile->units, punit) {
if (unit_is_on_layer(punit, V_SUBSURFACE)) {
send_unit_info(pplayer->connections, punit);
}
} unit_list_iterate_end;
}
}
/**********************************************************************//**
Get own seen count from player tile. Doesn't count shared vision.
@param plrtile Player tile to check own seen count from
@param vlayer Vision layer which we want the count for
@return Own seen count
**************************************************************************/
static inline int player_tile_own_seen(const struct player_tile *plrtile,
enum vision_layer vlayer)
{
return plrtile->own_seen[vlayer];
}
/**********************************************************************//**
Changes the own seen count of a tile for a player.
**************************************************************************/
static void map_change_own_seen(struct player *pplayer,
struct tile *ptile,
const v_radius_t change)
{
struct player_tile *plrtile = map_get_player_tile(ptile, pplayer);
vision_layer_iterate(v) {
plrtile->own_seen[v] += change[v];
} vision_layer_iterate_end;
}
/**********************************************************************//**
Changes site information for player tile.
**************************************************************************/
void change_playertile_site(struct player_tile *ptile,
struct vision_site *new_site)
{
if (ptile->site == new_site) {
/* Do nothing. */
return;
}
if (ptile->site != NULL) {
/* Releasing old site from tile */
vision_site_destroy(ptile->site);
}
ptile->site = new_site;
}
/**********************************************************************//**
Set known status of the tile.
**************************************************************************/
void map_set_known(struct tile *ptile, struct player *pplayer)
{
dbv_set(&pplayer->tile_known, tile_index(ptile));
}
/**********************************************************************//**
Clear known status of the tile.
**************************************************************************/
void map_clear_known(struct tile *ptile, struct player *pplayer)
{
dbv_clr(&pplayer->tile_known, tile_index(ptile));
}
/**********************************************************************//**
Call this function to unfog all tiles. This should only be called when
a player dies or at the end of the game as it will result in permanent
vision of the whole map.
**************************************************************************/
void map_know_and_see_all(struct player *pplayer)
{
const v_radius_t radius_sq = V_RADIUS(1, 1, 1);
buffer_shared_vision(pplayer);
whole_map_iterate(&(wld.map), ptile) {
map_change_seen(pplayer, ptile, radius_sq, TRUE);
} whole_map_iterate_end;
unbuffer_shared_vision(pplayer);
}
/**********************************************************************//**
Unfogs all tiles for all players. See map_know_and_see_all.
**************************************************************************/
void show_map_to_all(void)
{
players_iterate(pplayer) {
map_know_and_see_all(pplayer);
} players_iterate_end;
}
/**********************************************************************//**
Allocate space for map, and initialise the tiles.
Uses current map.xsize and map.ysize.
**************************************************************************/
void player_map_init(struct player *pplayer)
{
pplayer->server.private_map
= fc_realloc(pplayer->server.private_map,
MAP_INDEX_SIZE * sizeof(*pplayer->server.private_map));
whole_map_iterate(&(wld.map), ptile) {
player_tile_init(ptile, pplayer);
} whole_map_iterate_end;
dbv_init(&pplayer->tile_known, MAP_INDEX_SIZE);
}
/**********************************************************************//**
Free a player's private map.
**************************************************************************/
void player_map_free(struct player *pplayer)
{
if (!pplayer->server.private_map) {
return;
}
whole_map_iterate(&(wld.map), ptile) {
player_tile_free(ptile, pplayer);
} whole_map_iterate_end;
free(pplayer->server.private_map);
pplayer->server.private_map = NULL;
dbv_free(&pplayer->tile_known);
}
/**********************************************************************//**
Remove all knowledge of a player from main map and other players'
private maps, and send updates to connected clients.
Frees all vision_sites associated with that player.
**************************************************************************/
void remove_player_from_maps(struct player *pplayer)
{
/* only after removing borders! */
conn_list_do_buffer(game.est_connections);
whole_map_iterate(&(wld.map), ptile) {
/* Clear all players' knowledge about the removed player, and free
* data structures (including those in removed player's player map). */
bool reality_changed = FALSE;
players_iterate(aplayer) {
struct player_tile *aplrtile;
bool changed = FALSE;
if (!aplayer->server.private_map) {
continue;
}
aplrtile = map_get_player_tile(ptile, aplayer);
/* Free vision sites (cities) for removed and other players */
if (aplrtile && aplrtile->site
&& vision_site_owner(aplrtile->site) == pplayer) {
change_playertile_site(aplrtile, NULL);
changed = TRUE;
}
/* Remove references to player from others' maps */
if (aplrtile->owner == pplayer) {
aplrtile->owner = NULL;
changed = TRUE;
}
if (aplrtile->extras_owner == pplayer) {
aplrtile->extras_owner = NULL;
changed = TRUE;
}
/* Must ensure references to dying player are gone from clients
* before player is destroyed */
if (changed) {
/* This will use player tile if fogged */
send_tile_info(pplayer->connections, ptile, FALSE);
}
} players_iterate_end;
/* Clear removed player's knowledge */
if (pplayer->tile_known.vec) {
map_clear_known(ptile, pplayer);
}
/* Free all claimed tiles. */
if (tile_owner(ptile) == pplayer) {
tile_set_owner(ptile, NULL, NULL);
reality_changed = TRUE;
}
if (extra_owner(ptile) == pplayer) {
ptile->extras_owner = NULL;
reality_changed = TRUE;
}
if (reality_changed) {
/* Update anyone who can see the tile (e.g. global observers) */
send_tile_info(NULL, ptile, FALSE);
}
} whole_map_iterate_end;
conn_list_do_unbuffer(game.est_connections);
}
/**********************************************************************//**
We need to use fogofwar_old here, so the player's tiles get
in the same state as the other players' tiles.
**************************************************************************/
static void player_tile_init(struct tile *ptile, struct player *pplayer)
{
struct player_tile *plrtile = map_get_player_tile(ptile, pplayer);
plrtile->terrain = T_UNKNOWN;
plrtile->resource = NULL;
plrtile->owner = NULL;
plrtile->extras_owner = NULL;
plrtile->site = NULL;
dbv_init(&(plrtile->extras), extra_count());
if (!game.server.last_updated_year) {
plrtile->last_updated = game.info.turn;
} else {
plrtile->last_updated = game.info.year;
}
plrtile->seen_count[V_MAIN] = !game.server.fogofwar_old;
plrtile->seen_count[V_INVIS] = 0;
plrtile->seen_count[V_SUBSURFACE] = 0;
memcpy(plrtile->own_seen, plrtile->seen_count, sizeof(v_radius_t));
}
/**********************************************************************//**
Free the memory stored into the player tile.
**************************************************************************/
static void player_tile_free(struct tile *ptile, struct player *pplayer)
{
struct player_tile *plrtile = map_get_player_tile(ptile, pplayer);
if (plrtile->site != NULL) {
vision_site_destroy(plrtile->site);
}
dbv_free(&(plrtile->extras));
}
/**********************************************************************//**
Returns city located at given tile from player map.
**************************************************************************/
struct vision_site *map_get_player_city(const struct tile *ptile,
const struct player *pplayer)
{
struct vision_site *psite = map_get_player_site(ptile, pplayer);
fc_assert_ret_val(psite == NULL || psite->location == ptile, NULL);
return psite;
}
/**********************************************************************//**
Players' information of tiles is tracked so that fogged area can be kept
consistent even when the client disconnects. This function returns the
player tile information for the given tile and player.
**************************************************************************/
struct player_tile *map_get_player_tile(const struct tile *ptile,
const struct player *pplayer)
{
fc_assert_ret_val(pplayer->server.private_map, NULL);
return pplayer->server.private_map + tile_index(ptile);
}
/**********************************************************************//**
Give pplayer the correct knowledge about tile; return TRUE iff
knowledge changed.
Note that unlike update_tile_knowledge, this function will not send any
packets to the client. Callers may want to call send_tile_info() if this
function returns TRUE.
**************************************************************************/
bool update_player_tile_knowledge(struct player *pplayer, struct tile *ptile)
{
struct player_tile *plrtile = map_get_player_tile(ptile, pplayer);
if (plrtile->terrain != ptile->terrain
|| !bv_match_dbv(&(plrtile->extras), ptile->extras.vec)
|| plrtile->resource != ptile->resource
|| plrtile->owner != tile_owner(ptile)
|| plrtile->extras_owner != extra_owner(ptile)) {
plrtile->terrain = ptile->terrain;
extra_type_iterate(pextra) {
if (player_knows_extra_exist(pplayer, pextra, ptile)) {
dbv_set(&(plrtile->extras), extra_number(pextra));
} else {
dbv_clr(&(plrtile->extras), extra_number(pextra));
}
} extra_type_iterate_end;
if (ptile->resource != NULL
&& player_knows_extra_exist(pplayer, ptile->resource, ptile)) {
plrtile->resource = ptile->resource;
} else {
plrtile->resource = NULL;
}
plrtile->owner = tile_owner(ptile);
plrtile->extras_owner = extra_owner(ptile);
return TRUE;
}
return FALSE;
}
/**********************************************************************//**
Update playermap knowledge for everybody who sees the tile, and send a
packet to everyone whose info is changed.
Note this only checks for changing of the terrain, special, or resource
for the tile, since these are the only values held in the playermap.
A tile's owner always can see terrain changes in their territory.
**************************************************************************/
void update_tile_knowledge(struct tile *ptile)
{
if (server_state() == S_S_INITIAL) {
return;
}
/* Players */
players_iterate(pplayer) {
if (map_is_known_and_seen(ptile, pplayer, V_MAIN)) {
if (update_player_tile_knowledge(pplayer, ptile)) {
send_tile_info(pplayer->connections, ptile, FALSE);
}
}
} players_iterate_end;
/* Global observers */
conn_list_iterate(game.est_connections, pconn) {
struct player *pplayer = pconn->playing;
if (NULL == pplayer && pconn->observer) {
send_tile_info(pconn->self, ptile, FALSE);
}
} conn_list_iterate_end;
}
/**********************************************************************//**
Remember that tile was last seen this year.
**************************************************************************/
void update_player_tile_last_seen(struct player *pplayer,
struct tile *ptile)
{
if (!game.server.last_updated_year) {
map_get_player_tile(ptile, pplayer)->last_updated = game.info.turn;
} else {
map_get_player_tile(ptile, pplayer)->last_updated = game.info.year;
}
}
/**********************************************************************//**
Give tile information from one player to one player.
@param pfrom Who gives the information
@param pdest Who receives the information
@param ptile Tile to give info about
@return Whether there was any new info to give
**************************************************************************/
static bool really_give_tile_info_from_player_to_player(struct player *pfrom,
struct player *pdest,
struct tile *ptile)
{
if (!map_is_known_and_seen(ptile, pdest, V_MAIN)) {
/* I can just hear people scream as they try to comprehend this if :).
* Let me try in words:
* 1) if the tile is seen by pfrom the info is sent to pdest
* OR
* 2) if the tile is known by pfrom AND (they have more recent info
* OR it is not known by pdest)
*/
if (map_is_known_and_seen(ptile, pfrom, V_MAIN)
|| (map_is_known(ptile, pfrom)
&& (((map_get_player_tile(ptile, pfrom)->last_updated
> map_get_player_tile(ptile, pdest)->last_updated))
|| !map_is_known(ptile, pdest)))) {
struct player_tile *from_tile, *dest_tile;
from_tile = map_get_player_tile(ptile, pfrom);
dest_tile = map_get_player_tile(ptile, pdest);
/* Update and send tile knowledge */
map_set_known(ptile, pdest);
dest_tile->terrain = from_tile->terrain;
dbv_copy(&(dest_tile->extras), &(from_tile->extras));
dest_tile->resource = from_tile->resource;
dest_tile->owner = from_tile->owner;
dest_tile->extras_owner = from_tile->extras_owner;
dest_tile->last_updated = from_tile->last_updated;
send_tile_info(pdest->connections, ptile, FALSE);
/* Update and send city knowledge */
/* Remove outdated cities */
if (dest_tile->site) {
if (!from_tile->site) {
/* As the city was gone on the newer from_tile
it will be removed by this function */
reality_check_city(pdest, ptile);
} else { /* We have a dest_city. update */
if (from_tile->site->identity
!= dest_tile->site->identity) {
/* As the city was gone on the newer from_tile
it will be removed by this function */
reality_check_city(pdest, ptile);
}
}
}
/* Set and send new city info */
if (from_tile->site) {
if (!dest_tile->site) {
/* We cannot assign new vision site with change_playertile_site(),
* since location is not yet set up for new site */
dest_tile->site = vision_site_copy(from_tile->site);
}
/* Note that we don't care if receiver knows vision source city
* or not. */
send_city_info_at_tile(pdest, pdest->connections, NULL, ptile);
}
city_map_update_tile_frozen(ptile);
return TRUE;
}
}
return FALSE;
}
/**********************************************************************//**
Give information about whole map (all tiles) from player to player.
Does not take care of shared vision; caller is assumed to do that.
**************************************************************************/
static void really_give_map_from_player_to_player(struct player *pfrom,
struct player *pdest)
{
whole_map_iterate(&(wld.map), ptile) {
really_give_tile_info_from_player_to_player(pfrom, pdest, ptile);
} whole_map_iterate_end;
city_thaw_workers_queue();
sync_cities();
}
/**********************************************************************//**
Give tile information from player to player. Handles chains of
shared vision so that receiver may give information forward.
@param pfrom Who gives the information
@param pdest Who receives the information
@param ptile Tile to give info about
@return Whether there was any new info to give
**************************************************************************/
static bool give_tile_info_from_player_to_player(struct player *pfrom,
struct player *pdest,
struct tile *ptile)
{
bool updt = really_give_tile_info_from_player_to_player(pfrom, pdest, ptile);
players_iterate(pplayer2) {
if (really_gives_vision(pdest, pplayer2)) {
updt |= really_give_tile_info_from_player_to_player(pfrom, pplayer2, ptile);
}
} players_iterate_end;
return updt;
}
/**********************************************************************//**
This updates all players' really_gives_vision field.
If p1 gives p2 shared vision and p2 gives p3 shared vision p1
should also give p3 shared vision.
**************************************************************************/
static void create_vision_dependencies(void)
{
int added;
players_iterate(pplayer) {
pplayer->server.really_gives_vision = pplayer->gives_shared_vision;
} players_iterate_end;
/* In words: This terminates when it has run a round without adding
a dependency. One loop only propagates dependencies one level deep,
which is why we keep doing it as long as changes occur. */
do {
added = 0;
players_iterate(pplayer) {
players_iterate(pplayer2) {
if (really_gives_vision(pplayer, pplayer2)
&& pplayer != pplayer2) {
players_iterate(pplayer3) {
if (really_gives_vision(pplayer2, pplayer3)
&& !really_gives_vision(pplayer, pplayer3)
&& pplayer != pplayer3) {
BV_SET(pplayer->server.really_gives_vision, player_index(pplayer3));
added++;
}
} players_iterate_end;
}
} players_iterate_end;
} players_iterate_end;
} while (added > 0);
}
/**********************************************************************//**
Starts shared vision between two players.
**************************************************************************/
void give_shared_vision(struct player *pfrom, struct player *pto)
{
bv_player save_vision[player_slot_count()];
if (pfrom == pto) {
return;
}
if (gives_shared_vision(pfrom, pto)) {
log_error("Trying to give shared vision from %s to %s, "
"but that vision is already given!",
player_name(pfrom), player_name(pto));
return;
}
players_iterate(pplayer) {
save_vision[player_index(pplayer)] = pplayer->server.really_gives_vision;
} players_iterate_end;
BV_SET(pfrom->gives_shared_vision, player_index(pto));
create_vision_dependencies();
log_debug("giving shared vision from %s to %s",
player_name(pfrom), player_name(pto));
players_iterate(pplayer) {
buffer_shared_vision(pplayer);
players_iterate(pplayer2) {
if (really_gives_vision(pplayer, pplayer2)
&& !BV_ISSET(save_vision[player_index(pplayer)],
player_index(pplayer2))) {
log_debug("really giving shared vision from %s to %s",
player_name(pplayer), player_name(pplayer2));
whole_map_iterate(&(wld.map), ptile) {
const struct player_tile *plrtile = map_get_player_tile(ptile, pplayer);
const v_radius_t change =
V_RADIUS(player_tile_own_seen(plrtile, V_MAIN),
player_tile_own_seen(plrtile, V_INVIS),
player_tile_own_seen(plrtile, V_SUBSURFACE));
if (0 < change[V_MAIN] || 0 < change[V_INVIS]) {
map_change_seen(pplayer2, ptile, change,
map_is_known(ptile, pplayer));
}
} whole_map_iterate_end;
/* Squares that are not seen, but which pfrom may have more recent
knowledge of */
really_give_map_from_player_to_player(pplayer, pplayer2);
}
} players_iterate_end;
unbuffer_shared_vision(pplayer);
} players_iterate_end;
if (S_S_RUNNING == server_state()) {
send_player_info_c(pfrom, NULL);
}
}
/**********************************************************************//**
Removes shared vision from between two players.
**************************************************************************/
void remove_shared_vision(struct player *pfrom, struct player *pto)
{
bv_player save_vision[player_slot_count()];
fc_assert_ret(pfrom != pto);
if (!gives_shared_vision(pfrom, pto)) {
log_error("Tried removing the shared vision from %s to %s, "
"but it did not exist in the first place!",
player_name(pfrom), player_name(pto));
return;
}
players_iterate(pplayer) {
save_vision[player_index(pplayer)] = pplayer->server.really_gives_vision;
} players_iterate_end;
log_debug("removing shared vision from %s to %s",
player_name(pfrom), player_name(pto));
BV_CLR(pfrom->gives_shared_vision, player_index(pto));
create_vision_dependencies();
players_iterate(pplayer) {
buffer_shared_vision(pplayer);
players_iterate(pplayer2) {
if (!really_gives_vision(pplayer, pplayer2)
&& BV_ISSET(save_vision[player_index(pplayer)],
player_index(pplayer2))) {
log_debug("really removing shared vision from %s to %s",
player_name(pplayer), player_name(pplayer2));
whole_map_iterate(&(wld.map), ptile) {
const struct player_tile *plrtile = map_get_player_tile(ptile, pplayer);
const v_radius_t change =
V_RADIUS(-player_tile_own_seen(plrtile, V_MAIN),
-player_tile_own_seen(plrtile, V_INVIS),
-player_tile_own_seen(plrtile, V_SUBSURFACE));
if (0 > change[V_MAIN] || 0 > change[V_INVIS]) {
map_change_seen(pplayer2, ptile, change, FALSE);
}
} whole_map_iterate_end;
}
} players_iterate_end;
unbuffer_shared_vision(pplayer);
} players_iterate_end;
if (S_S_RUNNING == server_state()) {
send_player_info_c(pfrom, NULL);
}
}
/**********************************************************************//**
Turns FoW on for player
**************************************************************************/
void enable_fog_of_war_player(struct player *pplayer)
{
const v_radius_t radius_sq = V_RADIUS(-1, 0, 0);
dlsend_packet_edit_fogofwar_state(pplayer->connections, TRUE);
buffer_shared_vision(pplayer);
whole_map_iterate(&(wld.map), ptile) {
map_change_seen(pplayer, ptile, radius_sq, FALSE);
} whole_map_iterate_end;
unbuffer_shared_vision(pplayer);
}
/**********************************************************************//**
Turns FoW on for everyone.
**************************************************************************/
void enable_fog_of_war(void)
{
players_iterate(pplayer) {
enable_fog_of_war_player(pplayer);
} players_iterate_end;
}
/**********************************************************************//**
Turns FoW off for player
**************************************************************************/
void disable_fog_of_war_player(struct player *pplayer)
{
const v_radius_t radius_sq = V_RADIUS(1, 0, 0);
dlsend_packet_edit_fogofwar_state(pplayer->connections, FALSE);
buffer_shared_vision(pplayer);
whole_map_iterate(&(wld.map), ptile) {
map_change_seen(pplayer, ptile, radius_sq, FALSE);
} whole_map_iterate_end;
unbuffer_shared_vision(pplayer);
}
/**********************************************************************//**
Turns FoW off for everyone
**************************************************************************/
void disable_fog_of_war(void)
{
players_iterate(pplayer) {
disable_fog_of_war_player(pplayer);
} players_iterate_end;
}
/**********************************************************************//**
Set the tile to be a river if required.
It's required if one of the tiles nearby would otherwise be part of a
river to nowhere.
(Note that rivers-to-nowhere can still occur if a single-tile lake is
transformed away, but this is relatively unlikely.)
For simplicity, I'm assuming that this is the only exit of the river,
so I don't need to trace it across the continent. --CJM
**************************************************************************/
static void ocean_to_land_fix_rivers(struct tile *ptile)
{
cardinal_adjc_iterate(&(wld.map), ptile, tile1) {
bool ocean_near = FALSE;
cardinal_adjc_iterate(&(wld.map), tile1, tile2) {
if (is_ocean_tile(tile2))
ocean_near = TRUE;
} cardinal_adjc_iterate_end;
if (!ocean_near) {
/* If ruleset has several river types defined, this
* may cause same tile to contain more than one river. */
extra_type_by_cause_iterate(EC_ROAD, priver) {
if (tile_has_extra(tile1, priver)
&& road_has_flag(extra_road_get(priver), RF_RIVER)) {
tile_add_extra(ptile, priver);
}
} extra_type_by_cause_iterate_end;
}
} cardinal_adjc_iterate_end;
}
/**********************************************************************//**
Bounce one unit from tile on terrain change.
**************************************************************************/
static void terrain_change_bounce_single_unit(struct unit *punit,
struct tile *from)
{
bool unit_alive = TRUE;
/* Look for a nearby safe tile */
adjc_iterate(&(wld.map), from, ptile2) {
if (can_unit_exist_at_tile(&(wld.map), punit, ptile2)
&& !is_non_allied_unit_tile(ptile2, unit_owner(punit))
&& !is_non_allied_city_tile(ptile2, unit_owner(punit))) {
log_verbose("Moved %s %s due to changing terrain at (%d,%d).",
nation_rule_name(nation_of_unit(punit)),
unit_rule_name(punit), TILE_XY(unit_tile(punit)));
notify_player(unit_owner(punit), unit_tile(punit),
E_UNIT_RELOCATED, ftc_server,
_("Moved your %s due to changing terrain."),
unit_link(punit));
/* TODO: should a unit be able to bounce to a transport like is
* done below? What if the unit can't legally enter the transport,
* say because the transport is Unreachable and the unit doesn't
* have it in its embarks field or because "Transport Embark"
* isn't enabled? Kept like it was to preserve the old rules for
* now. -- Sveinung */
unit_alive = unit_move(punit, ptile2, 0,
NULL, TRUE, FALSE, FALSE, FALSE, FALSE);
if (unit_alive && punit->activity == ACTIVITY_SENTRY) {
unit_activity_handling(punit, ACTIVITY_IDLE);
}
break;
}
} adjc_iterate_end;
if (unit_alive && unit_tile(punit) == from) {
/* If we get here we could not move punit. */
/* Try to bounce transported units. */
if (0 < get_transporter_occupancy(punit)) {
struct unit_list *pcargo_units;
pcargo_units = unit_transport_cargo(punit);
unit_list_iterate_safe(pcargo_units, pcargo) {
terrain_change_bounce_single_unit(pcargo, from);
} unit_list_iterate_safe_end;
}
}
}
/**********************************************************************//**
Helper function for bounce_units_on_terrain_change() that checks units
on a single tile.
@param ptile Tile where the units to check are located
**************************************************************************/
static void check_units_single_tile(struct tile *ptile)
{
const struct civ_map *nmap = &(wld.map);
unit_list_iterate_safe(ptile->units, punit) {
int id = punit->id;
/* Top-level transports only. Each handle their own cargo */
if (!unit_transported(punit)
&& !can_unit_exist_at_tile(nmap, punit, ptile)) {
terrain_change_bounce_single_unit(punit, ptile);
if (unit_is_alive(id) && unit_tile(punit) == ptile) {
log_verbose("Disbanded %s %s due to changing terrain "
"at (%d, %d).",
nation_rule_name(nation_of_unit(punit)),
unit_rule_name(punit), TILE_XY(ptile));
notify_player(unit_owner(punit), ptile,
E_UNIT_LOST_MISC, ftc_server,
_("Disbanded your %s due to changing terrain."),
unit_tile_link(punit));
wipe_unit(punit, ULR_NONNATIVE_TERR, NULL);
}
}
} unit_list_iterate_safe_end;
}
/**********************************************************************//**
Check ptile and nearby tiles to see if all units can remain at their
current locations, and move or disband any that cannot. Call this after
terrain or specials change on ptile.
@param ptile Tile where the terrain may have changed
**************************************************************************/
void bounce_units_on_terrain_change(struct tile *ptile)
{
const struct civ_map *nmap = &(wld.map);
/* Check this tile for direct effect on its units */
check_units_single_tile(ptile);
/* We have to check adjacent tiles too, in case units in cities are now
* illegal (e.g., boat in a city that has become landlocked),
* and in case of CoastStrict units losing their adjacent coast. */
adjc_iterate(nmap, ptile, ptile2) {
check_units_single_tile(ptile2);
} adjc_iterate_end;
}
/**********************************************************************//**
Returns TRUE if the terrain change from 'oldter' to 'newter' may require
expensive reassignment of continents.
**************************************************************************/
bool need_to_reassign_continents(const struct terrain *oldter,
const struct terrain *newter)
{
bool old_is_ocean, new_is_ocean;
if (!oldter || !newter) {
return FALSE;
}
old_is_ocean = is_ocean(oldter);
new_is_ocean = is_ocean(newter);
return (old_is_ocean && !new_is_ocean)
|| (!old_is_ocean && new_is_ocean);
}
/**********************************************************************//**
Handle local side effects for a terrain change.
**************************************************************************/
void terrain_changed(struct tile *ptile)
{
struct city *pcity = tile_city(ptile);
if (pcity != NULL) {
/* Tile is city center and new terrain may support better extras. */
upgrade_city_extras(pcity, NULL);
}
bounce_units_on_terrain_change(ptile);
}
/**********************************************************************//**
Handles local side effects for a terrain change (tile and its
surroundings). Does *not* handle global side effects (such as reassigning
continents).
For in-game terrain changes 'extend_rivers' should be TRUE; for edits it
should be FALSE.
**************************************************************************/
void fix_tile_on_terrain_change(struct tile *ptile,
struct terrain *oldter,
bool extend_rivers)
{
if (is_ocean(oldter) && !is_ocean_tile(ptile)) {
if (extend_rivers) {
ocean_to_land_fix_rivers(ptile);
}
city_landlocked_sell_coastal_improvements(ptile);
}
terrain_changed(ptile);
}
/**********************************************************************//**
Handles local and global side effects for a terrain change for a single
tile.
Call this in the server immediately after calling tile_change_terrain().
Assumes an in-game terrain change (e.g., by workers/engineers).
**************************************************************************/
void check_terrain_change(struct tile *ptile, struct terrain *oldter)
{
struct terrain *newter = tile_terrain(ptile);
struct tile *claimer;
bool cont_reassigned = FALSE;
/* Check if new terrain is a freshwater terrain next to non-freshwater.
* In that case, the new terrain is *changed*. */
if (is_ocean(newter) && terrain_has_flag(newter, TER_FRESHWATER)) {
bool nonfresh = FALSE;
adjc_iterate(&(wld.map), ptile, atile) {
if (is_ocean(tile_terrain(atile))
&& !terrain_has_flag(tile_terrain(atile), TER_FRESHWATER)) {
nonfresh = TRUE;
break;
}
} adjc_iterate_end;
if (nonfresh) {
/* Need to pick a new, non-freshwater ocean type for this tile.
* We don't want e.g. Deep Ocean to be propagated to this tile
* and then to a whole lake by the flooding below, so we pick
* the shallowest non-fresh oceanic type.
* Prefer terrain that matches the frozenness of the target. */
newter = most_shallow_ocean(terrain_has_flag(newter, TER_FROZEN));
tile_change_terrain(ptile, newter);
}
}
if (need_to_reassign_continents(oldter, newter)) {
assign_continent_numbers();
cont_reassigned = TRUE;
phase_players_iterate(pplayer) {
if (is_adv_data_phase_open(pplayer)) {
/* Player is using continent numbers that they would assume to remain accurate.
* Force refresh:
* 1) Close the phase, so that it can be opened
* 2) Open the phase, recalculating
*/
adv_data_phase_done(pplayer);
adv_data_phase_init(pplayer, FALSE);
}
} phase_players_iterate_end;
}
fix_tile_on_terrain_change(ptile, oldter, TRUE);
/* Check for saltwater filling freshwater lake */
if (game.scenario.lake_flooding
&& is_ocean(newter) && !terrain_has_flag(newter, TER_FRESHWATER)) {
adjc_iterate(&(wld.map), ptile, atile) {
if (terrain_has_flag(tile_terrain(atile), TER_FRESHWATER)) {
struct terrain *aold = tile_terrain(atile);
tile_change_terrain(atile,
most_shallow_ocean(terrain_has_flag(aold,
TER_FROZEN)));
/* Recursive, but as lakes are of limited size, this
* won't recurse so much as to cause stack problems. */
check_terrain_change(atile, aold);
update_tile_knowledge(atile);
}
} adjc_iterate_end;
}
if (cont_reassigned) {
send_all_known_tiles(NULL);
}
claimer = tile_claimer(ptile);
if (claimer != NULL) {
/* Make sure map_claim_border() conditions are still satisfied */
if (is_ocean_tile(ptile)) {
/* Only certain water tiles are claimable */
if (!is_claimable_ocean(ptile, claimer, tile_owner(ptile))) {
map_clear_border(ptile);
}
} else {
/* Only land tiles on the same island as the border source
* are claimable */
if (tile_continent(ptile) != tile_continent(claimer)) {
map_clear_border(ptile);
}
}
}
sanity_check_tile(ptile);
}
/**********************************************************************//**
Ocean tile can be claimed iff one of the following conditions stands:
a) it is an inland lake not larger than MAXIMUM_OCEAN_SIZE
b) it is adjacent to only one continent and not more than two ocean tiles
c) It is one tile away from a border source
d) Player knows tech with Claim_Ocean flag
e) Source itself is Oceanic tile and player knows tech with Claim_Ocean_Limited flag
The source which claims the ocean has to be placed on the correct continent.
in case a) The continent which surrounds the inland lake
in case b) The only continent which is adjacent to the tile
**************************************************************************/
static bool is_claimable_ocean(struct tile *ptile, struct tile *source,
struct player *pplayer)
{
Continent_id cont = tile_continent(ptile);
Continent_id source_cont = tile_continent(source);
int ocean_tiles;
bool other_continent;
if (get_ocean_size(-cont) <= MAXIMUM_CLAIMED_OCEAN_SIZE
&& get_lake_surrounders(cont) == source_cont) {
return TRUE;
}
if (ptile == source) {
/* Source itself is always claimable. */
return TRUE;
}
if (num_known_tech_with_flag(pplayer, TF_CLAIM_OCEAN) > 0
|| (source_cont < 0 && num_known_tech_with_flag(pplayer, TF_CLAIM_OCEAN_LIMITED) > 0)) {
return TRUE;
}
ocean_tiles = 0;
other_continent = FALSE;
adjc_iterate(&(wld.map), ptile, adj_tile) {
Continent_id adj_cont = tile_continent(adj_tile);
if (adj_tile == source) {
/* Water next to border source is always claimable */
return TRUE;
}
if (adj_cont == cont) {
ocean_tiles++;
} else if (adj_cont != source_cont) {
/* This water is adjacent to a continent different from the one
* the border source is on */
other_continent = TRUE;
}
} adjc_iterate_end;
if (!other_continent && ocean_tiles <= 2) {
return TRUE;
} else {
return FALSE;
}
}
/**********************************************************************//**
For each unit at the tile, queue any unique home city.
**************************************************************************/
static void map_unit_homecity_enqueue(struct tile *ptile)
{
unit_list_iterate(ptile->units, punit) {
struct city *phome = game_city_by_number(punit->homecity);
if (NULL == phome) {
continue;
}
city_refresh_queue_add(phome);
} unit_list_iterate_end;
}
/**********************************************************************//**
Claim ownership of a single tile.
**************************************************************************/
static void map_claim_border_ownership(struct tile *ptile,
struct player *powner,
struct tile *psource)
{
struct player *ploser = tile_owner(ptile);
if ((ploser != powner && ploser != NULL)
&& (BORDERS_SEE_INSIDE == game.info.borders
|| BORDERS_EXPAND == game.info.borders
|| ploser->server.border_vision)) {
const v_radius_t radius_sq = V_RADIUS(-1, 0, 0);
shared_vision_change_seen(ploser, ptile, radius_sq, FALSE);
}
if (powner != NULL
&& (BORDERS_SEE_INSIDE == game.info.borders
|| BORDERS_EXPAND == game.info.borders
|| powner->server.border_vision)) {
const v_radius_t radius_sq = V_RADIUS(1, 0, 0);
shared_vision_change_seen(powner, ptile, radius_sq, TRUE);
}
tile_set_owner(ptile, powner, psource);
/* Needed only when foggedborders enabled, but we do it unconditionally
* in case foggedborders ever gets enabled later. Better to have correct
* information in player map just in case. */
update_tile_knowledge(ptile);
if (ploser != powner) {
if (S_S_RUNNING == server_state() && game.info.happyborders != HB_DISABLED) {
map_unit_homecity_enqueue(ptile);
}
if (!city_map_update_tile_frozen(ptile)) {
send_tile_info(NULL, ptile, FALSE);
}
}
}
/**********************************************************************//**
Claim ownership of a single tile.
**************************************************************************/
void map_claim_ownership(struct tile *ptile, struct player *powner,
struct tile *psource, bool claim_bases)
{
map_claim_border_ownership(ptile, powner, psource);
if (claim_bases) {
tile_claim_bases(ptile, powner);
}
}
/**********************************************************************//**
Claim ownership of bases on single tile.
**************************************************************************/
void tile_claim_bases(struct tile *ptile, struct player *powner)
{
struct player *base_loser = extra_owner(ptile);
/* This MUST be before potentially recursive call to map_claim_base(),
* so that the recursive call will get new owner == base_loser and
* abort recursion. */
ptile->extras_owner = powner;
extra_type_by_cause_iterate(EC_BASE, pextra) {
map_claim_base(ptile, pextra, powner, base_loser);
} extra_type_by_cause_iterate_end;
}
/**********************************************************************//**
Remove border for this source.
**************************************************************************/
void map_clear_border(struct tile *ptile)
{
int radius_sq = tile_border_source_radius_sq(ptile);
circle_dxyr_iterate(&(wld.map), ptile, radius_sq, dtile, dx, dy, dr) {
struct tile *claimer = tile_claimer(dtile);
if (claimer == ptile) {
map_claim_ownership(dtile, NULL, NULL, FALSE);
}
} circle_dxyr_iterate_end;
}
/**********************************************************************//**
Update borders for this source. Changes the radius without temporary
clearing.
**************************************************************************/
void map_update_border(struct tile *ptile, struct player *owner,
int old_radius_sq, int new_radius_sq)
{
if (old_radius_sq == new_radius_sq) {
/* No change */
return;
}
if (BORDERS_DISABLED == game.info.borders) {
return;
}
if (old_radius_sq < new_radius_sq) {
map_claim_border(ptile, owner, new_radius_sq);
} else {
circle_dxyr_iterate(&(wld.map), ptile, old_radius_sq, dtile, dx, dy, dr) {
if (dr > new_radius_sq) {
struct tile *claimer = tile_claimer(dtile);
if (claimer == ptile) {
map_claim_ownership(dtile, NULL, NULL, FALSE);
}
}
} circle_dxyr_iterate_end;
}
}
/**********************************************************************//**
Update borders for this source. Call this for each new source.
If radius_sq is -1, get value from the border source on tile.
**************************************************************************/
void map_claim_border(struct tile *ptile, struct player *owner,
int radius_sq)
{
if (BORDERS_DISABLED == game.info.borders) {
return;
}
if (owner == NULL) {
/* Clear the border instead of claiming. Code below this block
* cannot handle NULL owner. */
map_clear_border(ptile);
return;
}
if (radius_sq < 0) {
radius_sq = tile_border_source_radius_sq(ptile);
}
circle_dxyr_iterate(&(wld.map), ptile, radius_sq, dtile, dx, dy, dr) {
struct tile *dclaimer = tile_claimer(dtile);
if (dclaimer == ptile) {
/* Already claimed by the ptile */
continue;
}
if (dr != 0 && is_border_source(dtile)) {
/* Do not claim border sources other than self */
/* Note that this is extremely important at the moment for
* base claiming to work correctly in case there's two
* fortresses near each other. There could be infinite
* recursion in them claiming each other. */
continue;
}
if (!map_is_known(dtile, owner) && game.info.borders < BORDERS_EXPAND) {
continue;
}
/* Always claim source itself (distance, dr, to it 0) */
if (dr != 0 && NULL != dclaimer && dclaimer != ptile) {
struct city *ccity = tile_city(dclaimer);
int strength_old, strength_new;
if (ccity != NULL) {
/* Previously claimed by city */
int city_x, city_y;
map_distance_vector(&city_x, &city_y, ccity->tile, dtile);
if (map_vector_to_sq_distance(city_x, city_y)
<= city_map_radius_sq_get(ccity)
+ game.info.border_city_permanent_radius_sq) {
/* Tile is within region permanently claimed by city */
continue;
}
}
strength_old = tile_border_strength(dtile, dclaimer);
strength_new = tile_border_strength(dtile, ptile);
if (strength_new <= strength_old) {
/* Stronger shall prevail,
* in case of equal strength older shall prevail */
continue;
}
}
if (is_ocean_tile(dtile)) {
/* Only certain water tiles are claimable */
if (is_claimable_ocean(dtile, ptile, owner)) {
map_claim_ownership(dtile, owner, ptile, dr == 0);
}
} else {
/* Only land tiles on the same island as the border source
* are claimable */
if (tile_continent(dtile) == tile_continent(ptile)) {
map_claim_ownership(dtile, owner, ptile, dr == 0);
}
}
} circle_dxyr_iterate_end;
}
/**********************************************************************//**
Update borders for all sources. Call this on turn end.
**************************************************************************/
void map_calculate_borders(void)
{
if (BORDERS_DISABLED == game.info.borders) {
return;
}
if (wld.map.tiles == NULL) {
/* Map not yet initialized */
return;
}
log_verbose("map_calculate_borders()");
whole_map_iterate(&(wld.map), ptile) {
if (is_border_source(ptile)) {
map_claim_border(ptile, ptile->owner, -1);
}
} whole_map_iterate_end;
log_verbose("map_calculate_borders() workers");
city_thaw_workers_queue();
city_refresh_queue_processing();
}
/**********************************************************************//**
Claim base to player's ownership.
**************************************************************************/
void map_claim_base(struct tile *ptile, struct extra_type *pextra,
struct player *powner, struct player *ploser)
{
struct base_type *pbase;
bv_player *could_see_unit = NULL;
int units_num = 0;
int ul_size;
if (!tile_has_extra(ptile, pextra)) {
return;
}
if (pextra->eus != EUS_NORMAL) {
ul_size = unit_list_size(ptile->units);
} else {
ul_size = 0;
}
int stored_units[ul_size + 1];
if (ul_size > 0) {
int i;
could_see_unit = fc_malloc(sizeof(*could_see_unit) * ul_size);
unit_list_iterate(ptile->units, aunit) {
stored_units[units_num++] = aunit->id;
} unit_list_iterate_end;
fc_assert(units_num == ul_size);
for (i = 0; i < units_num; i++) {
struct unit *aunit = game_unit_by_number(stored_units[i]);
BV_CLR_ALL(could_see_unit[i]);
players_iterate(aplayer) {
if (can_player_see_unit(aplayer, aunit)) {
BV_SET(could_see_unit[i], player_index(aplayer));
}
} players_iterate_end;
}
}
pbase = extra_base_get(pextra);
fc_assert_ret(pbase != NULL);
/* Transfer base provided vision to new owner */
if (powner != NULL) {
const v_radius_t old_radius_sq = V_RADIUS(-1, -1, -1);
const v_radius_t new_radius_sq = V_RADIUS(pbase->vision_main_sq,
pbase->vision_invis_sq,
pbase->vision_subs_sq);
map_vision_update(powner, ptile, old_radius_sq, new_radius_sq,
game.server.vision_reveal_tiles);
}
if (ploser != NULL) {
const v_radius_t old_radius_sq = V_RADIUS(pbase->vision_main_sq,
pbase->vision_invis_sq,
pbase->vision_subs_sq);
const v_radius_t new_radius_sq = V_RADIUS(-1, -1, -1);
map_vision_update(ploser, ptile, old_radius_sq, new_radius_sq,
game.server.vision_reveal_tiles);
}
if (BORDERS_DISABLED != game.info.borders
&& territory_claiming_base(pbase) && powner != ploser) {
/* Clear borders from old owner. New owner may not know all those
* tiles and thus does not claim them when borders mode is less
* than EXPAND. */
if (ploser != NULL) {
/* Set this particular tile owner by NULL so in recursion
* both loser and owner will be NULL. */
map_claim_border_ownership(ptile, NULL, ptile);
map_clear_border(ptile);
}
/* We here first claim this tile ownership -> now on extra_owner()
* will return new owner. Then we claim border, which will recursively
* lead to this tile and base being claimed. But at that point
* ploser == powner and above check will abort the recursion. */
if (powner != NULL) {
map_claim_border_ownership(ptile, powner, ptile);
map_claim_border(ptile, powner, -1);
}
city_thaw_workers_queue();
city_refresh_queue_processing();
}
if (units_num > 0) {
int i;
for (i = 0; i < units_num; i++) {
struct unit *aunit = game_unit_by_number(stored_units[i]);
players_iterate(aplayer) {
if (can_player_see_unit(aplayer, aunit)) {
if (!BV_ISSET(could_see_unit[i], player_index(aplayer))) {
send_unit_info(aplayer->connections, aunit);
}
} else {
if (BV_ISSET(could_see_unit[i], player_index(aplayer))) {
unit_goes_out_of_sight(aplayer, aunit);
}
}
} players_iterate_end;
}
free(could_see_unit);
}
}
/**********************************************************************//**
Change the sight points for the vision source, fogging or unfogging tiles
as needed.
See documentation in vision.h.
**************************************************************************/
void vision_change_sight(struct vision *vision, const v_radius_t radius_sq)
{
map_vision_update(vision->player, vision->tile, vision->radius_sq,
radius_sq, vision->can_reveal_tiles);
memcpy(vision->radius_sq, radius_sq, sizeof(v_radius_t));
}
/**********************************************************************//**
Clear all sight points from this vision source.
See documentation in vision.h.
**************************************************************************/
void vision_clear_sight(struct vision *vision)
{
const v_radius_t vision_radius_sq = V_RADIUS(-1, -1, -1);
vision_change_sight(vision, vision_radius_sq);
/* Owner of some city might have lost vision of a tile previously worked */
players_iterate(pplayer) {
city_list_iterate(pplayer->cities, pcity) {
/* We are not interested about CNA_BROADCAST_PENDING, as the vision loss has
* not set it, and whatever set it should take care of it. */
if (pcity->server.needs_arrange == CNA_NORMAL) {
city_refresh(pcity);
auto_arrange_workers(pcity);
pcity->server.needs_arrange = CNA_NOT;
}
} city_list_iterate_end;
} players_iterate_end;
}
/**********************************************************************//**
Create extra to tile.
**************************************************************************/
void create_extra(struct tile *ptile, struct extra_type *pextra,
struct player *pplayer)
{
bool extras_removed = FALSE;
extra_type_iterate(old_extra) {
if (tile_has_extra(ptile, old_extra)
&& !can_extras_coexist(old_extra, pextra)) {
destroy_extra(ptile, old_extra);
extras_removed = TRUE;
}
} extra_type_iterate_end;
if (pextra->eus != EUS_NORMAL) {
unit_list_iterate(ptile->units, aunit) {
if (is_native_extra_to_utype(pextra, unit_type_get(aunit))) {
players_iterate(aplayer) {
if (!pplayers_allied(pplayer, aplayer)
&& can_player_see_unit(aplayer, aunit)) {
unit_goes_out_of_sight(aplayer, aunit);
}
} players_iterate_end;
}
} unit_list_iterate_end;
}
tile_add_extra(ptile, pextra);
/* Watchtower might become effective. */
unit_list_refresh_vision(ptile->units);
if (pextra->data.base != NULL) {
/* Claim bases on tile */
if (pplayer) {
struct player *old_owner = extra_owner(ptile);
/* Created base from NULL -> pplayer */
map_claim_base(ptile, pextra, pplayer, NULL);
if (old_owner != pplayer) {
/* Existing bases from old_owner -> pplayer */
extra_type_by_cause_iterate(EC_BASE, oldbase) {
if (oldbase != pextra) {
map_claim_base(ptile, oldbase, pplayer, old_owner);
}
} extra_type_by_cause_iterate_end;
ptile->extras_owner = pplayer;
}
} else {
/* Player who already owns bases on tile claims new base */
map_claim_base(ptile, pextra, extra_owner(ptile), NULL);
}
}
if (extras_removed) {
/* Maybe conflicting extra that was removed was the only thing
* making tile native to some unit. */
bounce_units_on_terrain_change(ptile);
}
}
/**********************************************************************//**
Remove extra from tile.
**************************************************************************/
void destroy_extra(struct tile *ptile, struct extra_type *pextra)
{
bv_player base_seen;
bool real = tile_map_check(&(wld.map), ptile);
/* Remember what players were able to see the base. */
if (real) {
BV_CLR_ALL(base_seen);
players_iterate(pplayer) {
if (map_is_known_and_seen(ptile, pplayer, V_MAIN)) {
BV_SET(base_seen, player_index(pplayer));
}
} players_iterate_end;
}
if (real && is_extra_caused_by(pextra, EC_BASE)) {
struct base_type *pbase = extra_base_get(pextra);
struct player *owner = extra_owner(ptile);
if (territory_claiming_base(pbase)) {
map_clear_border(ptile);
}
if (NULL != owner
&& (0 <= pbase->vision_main_sq || 0 <= pbase->vision_invis_sq)) {
/* Base provides vision, but no borders. */
const v_radius_t old_radius_sq =
V_RADIUS(0 <= pbase->vision_main_sq ? pbase->vision_main_sq : -1,
0 <= pbase->vision_invis_sq ? pbase->vision_invis_sq : -1,
0 <= pbase->vision_subs_sq ? pbase->vision_subs_sq : -1);
const v_radius_t new_radius_sq = V_RADIUS(-1, -1, -1);
map_vision_update(owner, ptile, old_radius_sq, new_radius_sq,
game.server.vision_reveal_tiles);
}
}
tile_remove_extra(ptile, pextra);
if (real) {
/* Remove base from vision of players which were able to see the base. */
players_iterate(pplayer) {
if (BV_ISSET(base_seen, player_index(pplayer))
&& update_player_tile_knowledge(pplayer, ptile)) {
send_tile_info(pplayer->connections, ptile, FALSE);
}
} players_iterate_end;
if (pextra->eus != EUS_NORMAL) {
struct player *eowner = extra_owner(ptile);
unit_list_iterate(ptile->units, aunit) {
if (is_native_extra_to_utype(pextra, unit_type_get(aunit))) {
players_iterate(aplayer) {
if (can_player_see_unit(aplayer, aunit)
&& !pplayers_allied(aplayer, eowner)) {
send_unit_info(aplayer->connections, aunit);
}
} players_iterate_end;
}
} unit_list_iterate_end;
}
}
}
/**********************************************************************//**
Transfer (random parts of) player pfrom's world map to pto.
@param pfrom player that is the source of the map
@param pto player that receives the map
@param prob probability for the transfer each known tile
@param reveal_cities if the map of all known cities should be transferred
@return Whether there any new info was given
**************************************************************************/
bool give_distorted_map(struct player *pfrom, struct player *pto,
int prob, bool reveal_cities)
{
bool updt = FALSE;
buffer_shared_vision(pto);
whole_map_iterate(&(wld.map), ptile) {
if (fc_rand(100) < prob) {
updt |= give_tile_info_from_player_to_player(pfrom, pto, ptile);
} else if (reveal_cities && NULL != tile_city(ptile)) {
updt|= give_tile_info_from_player_to_player(pfrom, pto, ptile);
}
} whole_map_iterate_end;
unbuffer_shared_vision(pto);
return updt;
}
/**********************************************************************//**
Handle various side effects of the change on tile.
If a city was working the tile, that city might need refresh
after this call.
@param ptile tile that has changed
@param refresh_city whether city working the tile should be refreshed
**************************************************************************/
void tile_change_side_effects(struct tile *ptile, bool refresh_city)
{
struct city *pcity = ptile->worked;
/* Check the unit activities. */
unit_activities_cancel_all_illegal_area(ptile);
if (pcity != NULL && !is_free_worked(pcity, ptile)
&& get_city_tile_output_bonus(pcity, ptile, NULL, EFT_TILE_WORKABLE) <= 0) {
city_map_update_empty(pcity, ptile);
pcity->specialists[DEFAULT_SPECIALIST]++;
if (refresh_city) {
auto_arrange_workers(pcity);
send_city_info(NULL, pcity);
}
}
}
|