1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858
|
#include "MapWnd.h"
#include <deque>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>
#include <boost/graph/graph_concepts.hpp>
#include <boost/optional/optional.hpp>
#include <boost/unordered_set.hpp>
#include <GG/Layout.h>
#include <GG/MultiEdit.h>
#include <GG/PtRect.h>
#include <GG/WndEvent.h>
#include "CensusBrowseWnd.h"
#include "ChatWnd.h"
#include "ClientUI.h"
#include "CombatReport/CombatReportWnd.h"
#include "CUIControls.h"
#include "CUIDrawUtil.h"
#include "DesignWnd.h"
#include "EncyclopediaDetailPanel.h"
#include "FieldIcon.h"
#include "FleetButton.h"
#include "FleetWnd.h"
#include "GovernmentWnd.h"
#include "Hotkeys.h"
#include "InGameMenu.h"
#include "ModeratorActionsWnd.h"
#include "ObjectListWnd.h"
#include "PlayerListWnd.h"
#include "ProductionWnd.h"
#include "ResearchWnd.h"
#include "ResourceBrowseWnd.h"
#include "ShaderProgram.h"
#include "SidePanel.h"
#include "SitRepPanel.h"
#include "Sound.h"
#include "SystemIcon.h"
#include "TextBrowseWnd.h"
#include "../client/ClientNetworking.h"
#include "../client/human/GGHumanClientApp.h"
#include "../Empire/Empire.h"
#include "../network/Message.h"
#include "../universe/Field.h"
#include "../universe/Fleet.h"
#include "../universe/Pathfinder.h"
#include "../universe/Planet.h"
#include "../universe/ShipDesign.h"
#include "../universe/Ship.h"
#include "../universe/Species.h"
#include "../universe/System.h"
#include "../universe/UniverseObjectVisitors.h"
#include "../universe/UniverseObject.h"
#include "../universe/Universe.h"
#include "../util/GameRules.h"
#include "../util/i18n.h"
#include "../util/Logger.h"
#include "../util/ModeratorAction.h"
#include "../util/OptionsDB.h"
#include "../util/Order.h"
#include "../util/Random.h"
#include "../util/ranges.h"
#include "../util/ScopedTimer.h"
namespace {
consteval double Pow(double base, int exp) {
double retval = 1.0;
bool invert = exp < 0;
std::size_t abs_exp = exp >= 0 ? exp : -exp;
while (abs_exp--)
retval *= base;
return invert ? (1.0 / retval) : retval;
}
// "Babylonian Method" of finding square roots...
consteval double SqrtIterative2(double a, double c) {
double g = 0.5 * (c + a/c);
return (g == c) ? g : SqrtIterative2(a, g);
}
consteval double Sqrt(double a)
{ return SqrtIterative2(a, a); }
constexpr double ZOOM_STEP_SIZE = Sqrt(Sqrt(2.0));
constexpr double ZOOM_IN_MAX_STEPS = 12.0;
constexpr double ZOOM_IN_MIN_STEPS = -10.0; // negative zoom steps indicates zooming out
constexpr double ZOOM_MAX = Pow(ZOOM_STEP_SIZE, ZOOM_IN_MAX_STEPS);
constexpr double ZOOM_MIN = Pow(ZOOM_STEP_SIZE, ZOOM_IN_MIN_STEPS);
constexpr GG::X SITREP_PANEL_WIDTH{400};
constexpr GG::Y SITREP_PANEL_HEIGHT{200};
const std::string SITREP_WND_NAME = "map.sitrep";
const std::string MAP_PEDIA_WND_NAME = "map.pedia";
const std::string OBJECT_WND_NAME = "map.object-list";
const std::string MODERATOR_WND_NAME = "map.moderator";
const std::string COMBAT_REPORT_WND_NAME = "combat.summary";
const std::string MAP_SIDEPANEL_WND_NAME = "map.sidepanel";
const std::string GOVERNMENT_WND_NAME = "map.government";
constexpr GG::Y ZOOM_SLIDER_HEIGHT{200};
constexpr GG::Y SCALE_LINE_HEIGHT{20};
constexpr GG::X SCALE_LINE_MAX_WIDTH{240};
constexpr int MIN_SYSTEM_NAME_SIZE = 10;
constexpr int LAYOUT_MARGIN = 5;
constexpr GG::Y TOOLBAR_HEIGHT{32};
constexpr double TWO_PI = 2.0*3.1415926536;
constexpr GG::X ICON_SINGLE_WIDTH{40};
constexpr GG::X ICON_DUAL_WIDTH{64};
constexpr GG::X ICON_WIDTH{24};
constexpr GG::Pt ICON_SIZE{GG::X{24}, GG::Y{24}};
constexpr GG::Pt MENU_ICON_SIZE{GG::X{32}, GG::Y{32}};
DeclareThreadSafeLogger(effects);
double ZoomScaleFactor(double steps_in) {
if (steps_in > ZOOM_IN_MAX_STEPS) {
ErrorLogger() << "ZoomScaleFactor passed steps in (" << steps_in << ") higher than max (" << ZOOM_IN_MAX_STEPS << "), so using max";
steps_in = ZOOM_IN_MAX_STEPS;
} else if (steps_in < ZOOM_IN_MIN_STEPS) {
ErrorLogger() << "ZoomScaleFactor passed steps in (" << steps_in << ") lower than minimum (" << ZOOM_IN_MIN_STEPS << "), so using min";
steps_in = ZOOM_IN_MIN_STEPS;
}
return std::pow(ZOOM_STEP_SIZE, steps_in);
}
void AddOptions(OptionsDB& db) {
db.Add("ui.map.background.gas.shown", UserStringNop("OPTIONS_DB_GALAXY_MAP_GAS"), true, Validator<bool>());
db.Add("ui.map.background.starfields.shown", UserStringNop("OPTIONS_DB_GALAXY_MAP_STARFIELDS"), true, Validator<bool>());
db.Add("ui.map.background.starfields.scale", UserStringNop("OPTIONS_DB_GALAXY_MAP_STARFIELDS_SCALE"), 1.0, RangedStepValidator<double>(0.2, 0.2, 4.0));
db.Add("ui.map.scale.legend.shown", UserStringNop("OPTIONS_DB_GALAXY_MAP_SCALE_LINE"), true, Validator<bool>());
db.Add("ui.map.scale.circle.shown", UserStringNop("OPTIONS_DB_GALAXY_MAP_SCALE_CIRCLE"), false, Validator<bool>());
db.Add("ui.map.zoom.slider.shown", UserStringNop("OPTIONS_DB_GALAXY_MAP_ZOOM_SLIDER"), false, Validator<bool>());
db.Add("ui.map.starlane.thickness", UserStringNop("OPTIONS_DB_STARLANE_THICKNESS"), 2.0, RangedStepValidator<double>(0.2, 0.2, 10.0));
db.Add("ui.map.starlane.thickness.factor", UserStringNop("OPTIONS_DB_STARLANE_CORE"), 2.0, RangedStepValidator<double>(1.0, 1.0, 10.0));
db.Add("ui.map.starlane.empire.color.shown", UserStringNop("OPTIONS_DB_RESOURCE_STARLANE_COLOURING"), true, Validator<bool>());
db.Add("ui.map.fleet.supply.shown", UserStringNop("OPTIONS_DB_FLEET_SUPPLY_LINES"), true, Validator<bool>());
db.Add("ui.map.fleet.supply.width", UserStringNop("OPTIONS_DB_FLEET_SUPPLY_LINE_WIDTH"), 3.0, RangedStepValidator<double>(0.2, 0.2, 10.0));
db.Add("ui.map.fleet.supply.dot.spacing", UserStringNop("OPTIONS_DB_FLEET_SUPPLY_LINE_DOT_SPACING"), 20, RangedStepValidator<int>(1, 3, 40));
db.Add("ui.map.fleet.supply.dot.rate", UserStringNop("OPTIONS_DB_FLEET_SUPPLY_LINE_DOT_RATE"), 0.02, RangedStepValidator<double>(0.01, 0.01, 0.1));
db.Add("ui.fleet.explore.hostile.ignored", UserStringNop("OPTIONS_DB_FLEET_EXPLORE_IGNORE_HOSTILE"), false, Validator<bool>());
db.Add("ui.fleet.explore.system.route.limit", UserStringNop("OPTIONS_DB_FLEET_EXPLORE_SYSTEM_ROUTE_LIMIT"), 25, StepValidator<int>(1, -1));
db.Add("ui.fleet.explore.system.known.multiplier", UserStringNop("OPTIONS_DB_FLEET_EXPLORE_SYSTEM_KNOWN_MULTIPLIER"), 10.0f, Validator<float>());
db.Add("ui.map.starlane.color", UserStringNop("OPTIONS_DB_UNOWNED_STARLANE_COLOUR"), GG::Clr(72, 72, 72, 255), Validator<GG::Clr>());
db.Add("ui.map.detection.range.shown", UserStringNop("OPTIONS_DB_GALAXY_MAP_DETECTION_RANGE"), true, Validator<bool>());
db.Add("ui.map.detection.range.future.shown", UserStringNop("OPTIONS_DB_GALAXY_MAP_DETECTION_RANGE_FUTURE"), true, Validator<bool>());
db.Add("ui.map.scanlines.shown", UserStringNop("OPTIONS_DB_UI_SYSTEM_FOG"), true, Validator<bool>());
db.Add("ui.map.system.scanlines.spacing", UserStringNop("OPTIONS_DB_UI_SYSTEM_FOG_SPACING"), 4.0, RangedStepValidator<double>(0.2, 1.4, 8.0));
db.Add("ui.map.system.scanlines.color", UserStringNop("OPTIONS_DB_UI_SYSTEM_FOG_CLR"), GG::Clr(36, 36, 36, 192), Validator<GG::Clr>());
db.Add("ui.map.field.scanlines.color", UserStringNop("OPTIONS_DB_UI_FIELD_FOG_CLR"), GG::Clr(0, 0, 0, 64), Validator<GG::Clr>());
db.Add("ui.map.system.icon.size", UserStringNop("OPTIONS_DB_UI_SYSTEM_ICON_SIZE"), 14, RangedValidator<int>(8, 50));
db.Add("ui.map.system.circle.shown", UserStringNop("OPTIONS_DB_UI_SYSTEM_CIRCLES"), true, Validator<bool>());
db.Add("ui.map.system.circle.size", UserStringNop("OPTIONS_DB_UI_SYSTEM_CIRCLE_SIZE"), 1.5, RangedStepValidator<double>(0.1, 1.0, 2.5));
db.Add("ui.map.system.circle.inner.width", UserStringNop("OPTIONS_DB_UI_SYSTEM_INNER_CIRCLE_WIDTH"), 2.0, RangedStepValidator<double>(0.5, 1.0, 8.0));
db.Add("ui.map.system.circle.outer.width", UserStringNop("OPTIONS_DB_UI_SYSTEM_OUTER_CIRCLE_WIDTH"), 2.0, RangedStepValidator<double>(0.5, 1.0, 8.0));
db.Add("ui.map.system.circle.inner.max.width", UserStringNop("OPTIONS_DB_UI_SYSTEM_INNER_CIRCLE_MAX_WIDTH"), 5.0, RangedStepValidator<double>(0.5, 1.0, 12.0));
db.Add("ui.map.system.circle.distance", UserStringNop("OPTIONS_DB_UI_SYSTEM_CIRCLE_DISTANCE"), 2.0, RangedStepValidator<double>(0.5, 1.0, 8.0));
db.Add("ui.map.system.unexplored.rollover.enabled", UserStringNop("OPTIONS_DB_UI_SYSTEM_UNEXPLORED_OVERLAY"), true, Validator<bool>());
db.Add("ui.map.system.icon.tiny.threshold", UserStringNop("OPTIONS_DB_UI_SYSTEM_TINY_ICON_SIZE_THRESHOLD"), 10, RangedValidator<int>(1, 16));
db.Add("ui.map.system.select.indicator.size", UserStringNop("OPTIONS_DB_UI_SYSTEM_SELECTION_INDICATOR_SIZE"), 1.6, RangedStepValidator<double>(0.1, 0.5, 5));
db.Add("ui.map.system.select.indicator.rpm", UserStringNop("OPTIONS_DB_UI_SYSTEM_SELECTION_INDICATOR_FPS"), 12, RangedValidator<int>(1, 60));
db.Add("ui.map.system.unowned.name.color", UserStringNop("OPTIONS_DB_UI_SYSTEM_NAME_UNOWNED_COLOR"), GG::Clr(160, 160, 160, 255), Validator<GG::Clr>());
db.Add("ui.map.fleet.button.tiny.zoom.threshold", UserStringNop("OPTIONS_DB_UI_TINY_FLEET_BUTTON_MIN_ZOOM"), 0.8, RangedStepValidator<double>(0.1, 0.1, 4.0));
db.Add("ui.map.fleet.button.small.zoom.threshold", UserStringNop("OPTIONS_DB_UI_SMALL_FLEET_BUTTON_MIN_ZOOM"), 1.50, RangedStepValidator<double>(0.1, 0.1, 4.0));
db.Add("ui.map.fleet.button.medium.zoom.threshold", UserStringNop("OPTIONS_DB_UI_MEDIUM_FLEET_BUTTON_MIN_ZOOM"), 3.00, RangedStepValidator<double>(0.1, 0.1, 8.0));
db.Add("ui.map.fleet.button.big.zoom.threshold", UserStringNop("OPTIONS_DB_UI_BIG_FLEET_BUTTON_MIN_ZOOM"), 6.00, RangedStepValidator<double>(0.1, 0.1, 8.0));
db.Add("ui.map.detection.range.opacity", UserStringNop("OPTIONS_DB_GALAXY_MAP_DETECTION_RANGE_OPACITY"), 3, RangedValidator<int>(0, 8));
db.Add("ui.map.lock", UserStringNop("OPTIONS_DB_UI_GALAXY_MAP_LOCK"), false, Validator<bool>());
db.Add("ui.map.menu.enabled", UserStringNop("OPTIONS_DB_UI_GALAXY_MAP_POPUP"), false, Validator<bool>());
db.Add("ui.production.mappanels.removed", UserStringNop("OPTIONS_DB_UI_HIDE_MAP_PANELS"), false, Validator<bool>());
db.Add("ui.map.sidepanel.width", UserStringNop("OPTIONS_DB_UI_SIDEPANEL_WIDTH"), 512, Validator<int>());
db.Add("ui.map.sidepanel.meter-refresh", UserStringNop("OPTIONS_DB_UI_SIDEPANEL_OPEN_METER_UPDATE"), true, Validator<bool>());
db.Add("ui.map.object-changed.meter-refresh", UserStringNop("OPTIONS_DB_UI_OBJECT_CHANGED_METER_UPDATE"), true, Validator<bool>());
// Register hotkey names/default values for the context "map".
Hotkey::AddHotkey("ui.map.open", UserStringNop("HOTKEY_MAP_RETURN_TO_MAP"), GG::Key::GGK_ESCAPE);
Hotkey::AddHotkey("ui.turn.end", UserStringNop("HOTKEY_MAP_END_TURN"), GG::Key::GGK_RETURN, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.map.sitrep", UserStringNop("HOTKEY_MAP_SIT_REP"), GG::Key::GGK_n, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.research", UserStringNop("HOTKEY_MAP_RESEARCH"), GG::Key::GGK_r, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.production", UserStringNop("HOTKEY_MAP_PRODUCTION"), GG::Key::GGK_p, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.government", UserStringNop("HOTKEY_MAP_GOVERNMENT"), GG::Key::GGK_i, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.design", UserStringNop("HOTKEY_MAP_DESIGN"), GG::Key::GGK_d, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.map.objects", UserStringNop("HOTKEY_MAP_OBJECTS"), GG::Key::GGK_o, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.map.messages", UserStringNop("HOTKEY_MAP_MESSAGES"), GG::Key::GGK_t, GG::MOD_KEY_ALT);
Hotkey::AddHotkey("ui.map.empires", UserStringNop("HOTKEY_MAP_EMPIRES"), GG::Key::GGK_e, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.pedia", UserStringNop("HOTKEY_MAP_PEDIA"), GG::Key::GGK_F1);
Hotkey::AddHotkey("ui.map.graphs", UserStringNop("HOTKEY_MAP_GRAPHS"), GG::Key::GGK_NONE);
Hotkey::AddHotkey("ui.gamemenu", UserStringNop("HOTKEY_MAP_MENU"), GG::Key::GGK_F10);
Hotkey::AddHotkey("ui.zoom.in", UserStringNop("HOTKEY_MAP_ZOOM_IN"), GG::Key::GGK_z, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.zoom.in.alt", UserStringNop("HOTKEY_MAP_ZOOM_IN_ALT"), GG::Key::GGK_KP_PLUS, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.zoom.out", UserStringNop("HOTKEY_MAP_ZOOM_OUT"), GG::Key::GGK_x, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.zoom.out.alt", UserStringNop("HOTKEY_MAP_ZOOM_OUT_ALT"), GG::Key::GGK_KP_MINUS, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.map.system.zoom.home", UserStringNop("HOTKEY_MAP_ZOOM_HOME_SYSTEM"), GG::Key::GGK_h, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.map.system.zoom.prev", UserStringNop("HOTKEY_MAP_ZOOM_PREV_SYSTEM"), GG::Key::GGK_COMMA, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.map.system.zoom.next", UserStringNop("HOTKEY_MAP_ZOOM_NEXT_SYSTEM"), GG::Key::GGK_PERIOD, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.map.system.owned.zoom.prev", UserStringNop("HOTKEY_MAP_ZOOM_PREV_OWNED_SYSTEM"), GG::Key::GGK_COMMA, GG::MOD_KEY_CTRL | GG::MOD_KEY_SHIFT);
Hotkey::AddHotkey("ui.map.system.owned.zoom.next", UserStringNop("HOTKEY_MAP_ZOOM_NEXT_OWNED_SYSTEM"), GG::Key::GGK_PERIOD, GG::MOD_KEY_CTRL | GG::MOD_KEY_SHIFT);
Hotkey::AddHotkey("ui.map.fleet.zoom.prev", UserStringNop("HOTKEY_MAP_ZOOM_PREV_FLEET"), GG::Key::GGK_f, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.map.fleet.zoom.next", UserStringNop("HOTKEY_MAP_ZOOM_NEXT_FLEET"), GG::Key::GGK_g, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.map.fleet.idle.zoom.prev", UserStringNop("HOTKEY_MAP_ZOOM_PREV_IDLE_FLEET"), GG::Key::GGK_f, GG::MOD_KEY_ALT);
Hotkey::AddHotkey("ui.map.fleet.idle.zoom.next", UserStringNop("HOTKEY_MAP_ZOOM_NEXT_IDLE_FLEET"), GG::Key::GGK_g, GG::MOD_KEY_ALT);
Hotkey::AddHotkey("ui.pan.right", UserStringNop("HOTKEY_MAP_PAN_RIGHT"), GG::Key::GGK_RIGHT, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.pan.left", UserStringNop("HOTKEY_MAP_PAN_LEFT"), GG::Key::GGK_LEFT, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.pan.up", UserStringNop("HOTKEY_MAP_PAN_UP"), GG::Key::GGK_UP, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.pan.down", UserStringNop("HOTKEY_MAP_PAN_DOWN"), GG::Key::GGK_DOWN, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.map.scale.legend", UserStringNop("HOTKEY_MAP_TOGGLE_SCALE_LINE"), GG::Key::GGK_l, GG::MOD_KEY_ALT);
Hotkey::AddHotkey("ui.map.scale.circle", UserStringNop("HOTKEY_MAP_TOGGLE_SCALE_CIRCLE"), GG::Key::GGK_c, GG::MOD_KEY_ALT);
Hotkey::AddHotkey("ui.cut", UserStringNop("HOTKEY_CUT"), GG::Key::GGK_x, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.copy", UserStringNop("HOTKEY_COPY"), GG::Key::GGK_c, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.paste", UserStringNop("HOTKEY_PASTE"), GG::Key::GGK_v, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.select.all", UserStringNop("HOTKEY_SELECT_ALL"), GG::Key::GGK_a, GG::MOD_KEY_CTRL);
Hotkey::AddHotkey("ui.select.none", UserStringNop("HOTKEY_DESELECT"), GG::Key::GGK_d, GG::MOD_KEY_CTRL);
// stepping through UI controls doesn't really work as of this writing, so I'm commenting out these hotkey commands
//Hotkey::AddHotkey("ui.focus.prev", UserStringNop("HOTKEY_FOCUS_PREV_WND"), GG::Key::GGK_NONE);
//Hotkey::AddHotkey("ui.focus.next", UserStringNop("HOTKEY_FOCUS_NEXT_WND"), GG::Key::GGK_NONE);
}
bool temp_bool = RegisterOptions(&AddOptions);
/* Returns fractional distance along line segment between two points that a
* third point between them is.assumes the "mid" point is between the
* "start" and "end" points, in which case the returned fraction is between
* 0.0 and 1.0 */
double FractionalDistanceBetweenPoints(double startX, double startY, double midX,
double midY, double endX, double endY)
{
// get magnitudes of vectors
double full_deltaX = endX - startX, full_deltaY = endY - startY;
double mid_deltaX = midX - startX, mid_deltaY = midY - startY;
double full_length = std::sqrt(full_deltaX*full_deltaX + full_deltaY*full_deltaY);
if (full_length == 0.0) // safety check
full_length = 1.0;
double mid_length = std::sqrt(mid_deltaX*mid_deltaX + mid_deltaY*mid_deltaY);
return mid_length / full_length;
}
/* Returns point that is dist ditance away from (X1, Y1) in the direction
* of (X2, Y2) */
constexpr auto PositionFractionalAtDistanceBetweenPoints(
double X1, double Y1, double X2, double Y2, double dist)
{
double newX = X1 + (X2 - X1) * dist;
double newY = Y1 + (Y2 - Y1) * dist;
return std::pair<double, double>{newX, newY};
}
/* Returns apparent map X and Y position of an object at universe position
* \a X and \a Y for an object that is located on a starlane between
* systems with ids \a lane_start_sys_id and \a lane_end_sys_id
* The apparent position of an object depends on its actual position, the
* actual positions of the systems at the ends of the lane, and the
* apparent positions of the ends of the lanes. The apparent position of
* objects on the lane is compressed into the space between the apparent
* ends of the lane, but is proportional to the distance of the actual
* position along the lane. */
boost::optional<std::pair<double, double>> ScreenPosOnStarlane(
double X, double Y, int lane_start_sys_id, int lane_end_sys_id,
LaneEndpoints screen_lane_endpoints, const ScriptingContext& context)
{
// get endpoints of lane in universe. may be different because on-
// screen lanes are drawn between system circles, not system centres
auto prev = context.ContextObjects().get(lane_start_sys_id);
auto next = context.ContextObjects().get(lane_end_sys_id);
if (!next || !prev) {
ErrorLogger() << "ScreenPosOnStarlane couldn't find next system " << lane_start_sys_id
<< " or prev system " << lane_end_sys_id;
return boost::none;
}
// get fractional distance along lane that fleet's universe position is
double dist_along_lane = FractionalDistanceBetweenPoints(
prev->X(), prev->Y(), X, Y, next->X(), next->Y());
return PositionFractionalAtDistanceBetweenPoints(screen_lane_endpoints.X1, screen_lane_endpoints.Y1,
screen_lane_endpoints.X2, screen_lane_endpoints.Y2,
dist_along_lane);
}
bool InRect(GG::X left, GG::Y top, GG::X right, GG::Y bottom, const GG::Pt pt)
{ return pt.x >= left && pt.y >= top && pt.x < right && pt.y < bottom; } //pt >= ul && pt < lr;
auto InWndRect(const GG::Wnd* top_wnd) {
return [top_wnd](const GG::Wnd* wnd, const GG::Pt pt) -> bool {
return wnd && InRect(wnd->Left(), top_wnd ? top_wnd->Top() : wnd->Top(),
wnd->Right(), wnd->Bottom(),
pt);
};
}
GG::X AppWidth() noexcept {
if (const auto* app = GGHumanClientApp::GetApp())
return app->AppWidth();
return GG::X0;
}
GG::Y AppHeight() noexcept {
if (GGHumanClientApp* app = GGHumanClientApp::GetApp())
return app->AppHeight();
return GG::Y0;
}
bool ClientPlayerIsModerator()
{ return GGHumanClientApp::GetApp()->GetClientType() == Networking::ClientType::CLIENT_TYPE_HUMAN_MODERATOR; }
void PlayTurnButtonClickSound()
{ Sound::GetSound().PlaySound(GetOptionsDB().Get<std::string>("ui.button.turn.press.sound.path"), true); }
bool ToggleBoolOption(const std::string& option_name) {
const bool initially_enabled = GetOptionsDB().Get<bool>(option_name);
GetOptionsDB().Set(option_name, !initially_enabled);
return !initially_enabled;
}
void ShowTurnButtonPopup(std::shared_ptr<GG::Button> turn_btn,
std::function<void()> end_turn_action,
std::function<void()> revoke_orders_action)
{
const auto* app = ClientApp::GetApp();
if (!app)
return;
const auto order_count = app->Orders().size();
const auto turn = app->CurrentTurn();
// create popup menu with a commands in it
const GG::Pt pt = turn_btn ? turn_btn->LowerRight() : GG::Pt{GG::X1, GG::Y1};
auto popup = GG::Wnd::Create<CUIPopupMenu>(pt.x, pt.y);
//popup->AddMenuItem(GG::MenuItem(true)); // separator
const bool disable_end_turn = !turn_btn || turn_btn->Disabled();
auto start_label = boost::io::str(FlexibleFormat(UserString("MAP_BTN_TURN_SEND_AND_START")) % turn % order_count);
popup->AddMenuItem(GG::MenuItem(std::move(start_label), disable_end_turn, false, end_turn_action));
const bool disable_revoke = disable_end_turn || order_count < 1;
auto revoke_label = boost::io::str(FlexibleFormat(UserString("MAP_BTN_TURN_REVOKE")) % turn % order_count);
popup->AddMenuItem(GG::MenuItem(std::move(revoke_label), disable_revoke, false, revoke_orders_action));
popup->Run();
}
const std::string FLEET_DETAIL_SHIP_COUNT{UserStringNop("MAP_FLEET_SHIP_COUNT")};
const std::string FLEET_DETAIL_ARMED_COUNT{UserStringNop("MAP_FLEET_ARMED_COUNT")};
const std::string FLEET_DETAIL_SLOT_COUNT{UserStringNop("MAP_FLEET_SLOT_COUNT")};
const std::string FLEET_DETAIL_PART_COUNT{UserStringNop("MAP_FLEET_PART_COUNT")};
const std::string FLEET_DETAIL_UNARMED_COUNT{UserStringNop("MAP_FLEET_UNARMED_COUNT")};
const std::string FLEET_DETAIL_COLONY_COUNT{UserStringNop("MAP_FLEET_COLONY_COUNT")};
const std::string FLEET_DETAIL_CARRIER_COUNT{UserStringNop("MAP_FLEET_CARRIER_COUNT")};
const std::string FLEET_DETAIL_TROOP_COUNT{UserStringNop("MAP_FLEET_TROOP_COUNT")};
/** BrowseInfoWnd for the fleet icon tooltip */
class FleetDetailBrowseWnd : public GG::BrowseInfoWnd {
public:
FleetDetailBrowseWnd(int empire_id, GG::X width) :
GG::BrowseInfoWnd(GG::X0, GG::Y0, width, GG::Y(ClientUI::Pts())),
m_empire_id(empire_id),
m_margin(LAYOUT_MARGIN)
{
GG::X value_col_width{(m_margin * 3) + (ClientUI::Pts() * 3)};
m_col_widths = {width - value_col_width, value_col_width};
RequirePreRender();
}
void PreRender() override {
SetChildClippingMode(ChildClippingMode::ClipToClient);
NewLabelValue(FLEET_DETAIL_SHIP_COUNT, true);
NewLabelValue(FLEET_DETAIL_ARMED_COUNT);
NewLabelValue(FLEET_DETAIL_SLOT_COUNT);
NewLabelValue(FLEET_DETAIL_PART_COUNT);
NewLabelValue(FLEET_DETAIL_UNARMED_COUNT);
NewLabelValue(FLEET_DETAIL_COLONY_COUNT);
NewLabelValue(FLEET_DETAIL_CARRIER_COUNT);
NewLabelValue(FLEET_DETAIL_TROOP_COUNT);
UpdateLabels();
ResetShipDesignLabels();
DoLayout();
}
typedef std::pair<std::shared_ptr<CUILabel>,
std::shared_ptr<CUILabel>> LabelValueType;
bool WndHasBrowseInfo(const Wnd* wnd, std::size_t mode) const override {
assert(mode <= wnd->BrowseModes().size());
return true;
}
void Render() override {
const GG::Y row_height{ClientUI::Pts() + (m_margin * 2)};
static constexpr GG::Y offset{32};
const GG::Clr BG_CLR = ClientUI::WndColor();
const GG::Clr BORDER_CLR = ClientUI::WndOuterBorderColor();
const GG::Pt UL = GG::Pt(UpperLeft().x, UpperLeft().y + offset);
const GG::Pt LR = LowerRight();
// main background
GG::FlatRectangle(UL, LR, BG_CLR, BORDER_CLR);
// summary text background
GG::FlatRectangle(UL, GG::Pt(LR.x, row_height + offset), BORDER_CLR, BORDER_CLR);
// seperation line between armed/unarmed and utility ships
GG::Y line_ht(UL.y + (row_height * 2) + (row_height * 5 / 4));
GG::Pt line_ul(UL.x + (m_margin * 2), line_ht);
GG::Pt line_lr(LR.x - (m_margin * 2), line_ht);
GG::Line(line_ul, line_lr, BORDER_CLR);
// seperation line between ships and parts
line_ht = {UL.y + (row_height * 5) + (row_height * 6 / 4)};
line_ul = {UL.x + (m_margin * 2), line_ht};
line_lr = {LR.x - (m_margin * 2), line_ht};
GG::Line(line_ul, line_lr, BORDER_CLR);
// seperation line between parts and designs
line_ht = { UL.y + (row_height * 7) + (row_height * 7 / 4) };
line_ul = { UL.x + (m_margin * 2), line_ht };
line_lr = { LR.x - (m_margin * 2), line_ht };
GG::Line(line_ul, line_lr, BORDER_CLR);
}
void DoLayout() {
const GG::Y row_height{ClientUI::Pts()};
static constexpr GG::Y offset{32};
const GG::X descr_width{m_col_widths.at(0) - (m_margin * 2)};
const GG::X value_width{m_col_widths.at(1) - (m_margin * 3)};
GG::Pt descr_ul{GG::X{m_margin}, offset + m_margin};
GG::Pt descr_lr{descr_ul.x + descr_width, offset + row_height};
GG::Pt value_ul{descr_lr.x + m_margin, descr_ul.y};
GG::Pt value_lr{value_ul.x + value_width, descr_lr.y};
const GG::Pt next_row{GG::X0, row_height + (m_margin * 2)};
const GG::Pt space_row{next_row * 5 / 4};
LayoutRow(FLEET_DETAIL_SHIP_COUNT,
descr_ul, descr_lr, value_ul, value_lr, space_row);
LayoutRow(FLEET_DETAIL_ARMED_COUNT,
descr_ul, descr_lr, value_ul, value_lr, next_row);
LayoutRow(FLEET_DETAIL_UNARMED_COUNT,
descr_ul, descr_lr, value_ul, value_lr, space_row);
LayoutRow(FLEET_DETAIL_CARRIER_COUNT,
descr_ul, descr_lr, value_ul, value_lr, next_row);
LayoutRow(FLEET_DETAIL_TROOP_COUNT,
descr_ul, descr_lr, value_ul, value_lr, next_row);
LayoutRow(FLEET_DETAIL_COLONY_COUNT,
descr_ul, descr_lr, value_ul, value_lr, space_row);
LayoutRow(FLEET_DETAIL_PART_COUNT,
descr_ul, descr_lr, value_ul, value_lr, next_row);
LayoutRow(FLEET_DETAIL_SLOT_COUNT,
descr_ul, descr_lr, value_ul, value_lr,
m_ship_design_labels.empty() ? GG::Pt0 : space_row);
for (auto it = m_ship_design_labels.begin(); it != m_ship_design_labels.end(); ++it) {
LayoutRow(*it, descr_ul, descr_lr, value_ul, value_lr,
std::next(it) == m_ship_design_labels.end()? GG::Pt0 : next_row);
}
Resize(GG::Pt(value_lr.x + (m_margin * 3), value_lr.y + (m_margin * 3)));
}
/** Constructs and attaches new description and value labels
* for the given description row @p descr. */
void NewLabelValue(const std::string& descr, bool title = false) {
if (m_labels.contains(descr))
return;
GG::Y height{ClientUI::Pts()};
// center format for title label
m_labels.emplace(descr, std::pair(
GG::Wnd::Create<CUILabel>(UserString(descr),
title ? GG::FORMAT_CENTER : GG::FORMAT_RIGHT,
GG::NO_WND_FLAGS, GG::X0, GG::Y0,
m_col_widths.at(0) - (m_margin * 2), height
),
GG::Wnd::Create<CUILabel>("0", GG::FORMAT_RIGHT,
GG::NO_WND_FLAGS, GG::X0, GG::Y0,
m_col_widths.at(1) - (m_margin * 2), height
)
));
if (title) { // utilize bold font for title label and value
m_labels.at(descr).first->SetFont(ClientUI::GetBoldFont());
m_labels.at(descr).second->SetFont(ClientUI::GetBoldFont());
}
AttachChild(m_labels.at(descr).first);
AttachChild(m_labels.at(descr).second);
}
/** Updates the text displayed for the value of each label */
void UpdateLabels() {
UpdateValues();
for (const auto& value : m_values) {
auto label_it = m_labels.find(value.first);
if (label_it == m_labels.end())
continue;
label_it->second.second->ChangeTemplatedText(std::to_string(value.second), 0);
}
}
protected:
/** Updates the value for display with each label */
void UpdateValues() {
m_values.clear();
m_values[FLEET_DETAIL_SHIP_COUNT] = 0;
m_ship_design_counts.clear();
if (m_empire_id == ALL_EMPIRES)
return;
const ScriptingContext context;
const Universe& universe = context.ContextUniverse();
const ObjectMap& objects = context.ContextObjects();
const SpeciesManager& sm = context.species;
const auto& destroyed_objects = universe.EmpireKnownDestroyedObjectIDs(m_empire_id);
for (auto* ship : objects.allRaw<Ship>()) {
if (!ship->OwnedBy(m_empire_id) || destroyed_objects.contains(ship->ID()))
continue;
m_values[FLEET_DETAIL_SHIP_COUNT]++;
if (ship->IsArmed(context))
m_values[FLEET_DETAIL_ARMED_COUNT]++;
else
m_values[FLEET_DETAIL_UNARMED_COUNT]++;
if (ship->CanColonize(universe, sm))
m_values[FLEET_DETAIL_COLONY_COUNT]++;
if (ship->HasTroops(universe))
m_values[FLEET_DETAIL_TROOP_COUNT]++;
if (ship->HasFighters(universe))
m_values[FLEET_DETAIL_CARRIER_COUNT]++;
const ShipDesign* design = universe.GetShipDesign(ship->DesignID());
if (!design)
continue;
m_ship_design_counts[design->ID()]++;
for (const std::string& part : design->Parts()) {
m_values[FLEET_DETAIL_SLOT_COUNT]++;
if (!part.empty())
m_values[FLEET_DETAIL_PART_COUNT]++;
}
}
}
/** Resize/Move controls for row @p descr
* and then advance sizes by @p row_advance */
void LayoutRow(const std::string& descr,
GG::Pt& descr_ul, GG::Pt& descr_lr,
GG::Pt& value_ul, GG::Pt& value_lr,
const GG::Pt row_advance)
{
if (!m_labels.contains(descr)) {
ErrorLogger() << "Unable to find expected label key " << descr;
return;
}
LayoutRow(m_labels.at(descr), descr_ul, descr_lr, value_ul, value_lr, row_advance);
}
void LayoutRow(LabelValueType& row, GG::Pt& descr_ul, GG::Pt& descr_lr,
GG::Pt& value_ul, GG::Pt& value_lr, const GG::Pt row_advance)
{
row.first->SizeMove(descr_ul, descr_lr);
if (row.second)
row.second->SizeMove(value_ul, value_lr);
descr_ul += row_advance;
descr_lr += row_advance;
value_ul += row_advance;
value_lr += row_advance;
}
/** Remove the old labels for ship design counts, and add the new ones.
The stats themselves are updated in UpdateValues. */
void ResetShipDesignLabels() {
for (auto& labels : m_ship_design_labels) {
DetachChild(labels.first);
DetachChild(labels.second);
}
m_ship_design_labels.clear();
for (const auto& entry : m_ship_design_counts) {
GG::Y height{ ClientUI::Pts() };
// center format for title label
m_ship_design_labels.emplace_back(
GG::Wnd::Create<CUILabel>(
GetUniverse().GetShipDesign(entry.first)->Name(),
GG::FORMAT_RIGHT,
GG::NO_WND_FLAGS, GG::X0, GG::Y0,
m_col_widths.at(0) - (m_margin * 2), height),
GG::Wnd::Create<CUILabel>(
std::to_string(entry.second),
GG::FORMAT_RIGHT,
GG::NO_WND_FLAGS, GG::X0, GG::Y0,
m_col_widths.at(1) - (m_margin * 2), height)
);
}
std::sort(m_ship_design_labels.begin(), m_ship_design_labels.end(),
[](LabelValueType a, LabelValueType b)
{ return a.first->Text() < b.first->Text(); }
);
for (auto& labels : m_ship_design_labels) {
AttachChild(labels.first);
AttachChild(labels.second);
}
}
private:
void UpdateImpl(std::size_t mode, const Wnd* target) override {
UpdateLabels();
ResetShipDesignLabels();
DoLayout();
}
std::unordered_map<std::string, int> m_values; ///< Internal value for display with a description
std::unordered_map<std::string, LabelValueType> m_labels; ///< Label controls mapped to the description key
std::unordered_map<int, int> m_ship_design_counts; ///< Map of ship design ids to the number of ships with that id
std::vector<LabelValueType> m_ship_design_labels; ///< Label controls for ship designs, sorted by disply name
int m_empire_id; ///< ID of the viewing empire
std::vector<GG::X> m_col_widths; ///< widths of each column
int m_margin; ///< margin between controls
};
}
////////////////////////////////////////////////////////////
// MapWnd::MapScaleLine
////////////////////////////////////////////////////////////
/** Displays a notched line with number labels to indicate Universe distance on the map. */
class MapWnd::MapScaleLine : public GG::Control {
public:
MapScaleLine(GG::X x, GG::Y y, GG::X w, GG::Y h) :
GG::Control(x, y, w, h, GG::ONTOP)
{
m_label = GG::Wnd::Create<GG::TextControl>(GG::X0, GG::Y0, GG::X1, GG::Y1, "", ClientUI::GetFont(), ClientUI::TextColor());
}
void CompleteConstruction() override {
GG::Control::CompleteConstruction();
AttachChild(m_label);
std::set<int> dummy = std::set<int>();
Update(1.0, dummy, INVALID_OBJECT_ID);
m_legend_show_connection = GetOptionsDB().OptionChangedSignal("ui.map.scale.legend.shown").connect(
[this]() { UpdateEnabled(); });
UpdateEnabled();
}
virtual ~MapScaleLine()
{}
void Render() override {
if (!m_enabled)
return;
// use GL to draw line and ticks and labels to indicte a length on the map
GG::Pt ul = UpperLeft();
GG::Pt lr = ul + GG::Pt(m_line_length, Height());
GG::GL2DVertexBuffer verts;
verts.reserve(6);
// left border
verts.store(Value(ul.x), Value(ul.y));
verts.store(Value(ul.x), Value(lr.y));
// right border
verts.store(Value(lr.x), Value(ul.y));
verts.store(Value(lr.x), Value(lr.y));
// bottom line
verts.store(Value(ul.x), Value(lr.y));
verts.store(Value(lr.x), Value(lr.y));
glColor(GG::CLR_WHITE);
glLineWidth(2.0);
glPushAttrib(GL_ENABLE_BIT | GL_LINE_WIDTH);
glDisable(GL_TEXTURE_2D);
glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
glEnableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
verts.activate();
glDrawArrays(GL_LINES, 0, verts.size());
glPopClientAttrib();
glPopAttrib();
}
GG::X GetLength() const
{ return m_line_length; }
double GetScaleFactor() const
{ return m_scale_factor; }
void Update(double zoom_factor, std::set<int>& fleet_ids, int sel_system_id) {
// The uu length of the map scale line is generally adjusted in this routine up or down by factors of two or five as
// the zoom_factor changes, so that it's pixel length on the screen is kept to a reasonable distance. We also add
// additional stopping points for the map scale to augment the usefulness of the linked map scale circle (whose
// radius is the same as the map scale length). These additional stopping points include the speeds and detection
// ranges of any selected fleets, and the detection ranges of all planets in the currently selected system,
// provided such values are at least 20 uu.
// get selected fleet speeds and detection ranges
std::set<double> fixed_distances;
for (const auto* fleet : Objects().findRaw<Fleet>(fleet_ids)) {
if (!fleet)
continue;
if (fleet->Speed(Objects()) > 20)
fixed_distances.insert(fleet->Speed(Objects()));
for (const auto* ship : Objects().findRaw<Ship>(fleet->ShipIDs())) {
if (!ship)
continue;
const float ship_range = ship->GetMeter(MeterType::METER_DETECTION)->Initial();
if (ship_range > 20)
fixed_distances.insert(ship_range);
const float ship_speed = ship->Speed();
if (ship_speed > 20)
fixed_distances.insert(ship_speed);
}
}
// get detection ranges for planets in the selected system (if any)
if (const auto* system = Objects().getRaw<System>(sel_system_id)) {
for (const auto* planet : Objects().findRaw<Planet>(system->PlanetIDs())) {
if (!planet)
continue;
const float planet_range = planet->GetMeter(MeterType::METER_DETECTION)->Initial();
if (planet_range > 20)
fixed_distances.insert(planet_range);
}
}
zoom_factor = std::min(std::max(zoom_factor, ZOOM_MIN), ZOOM_MAX); // sanity range limits to prevent divide by zero
m_scale_factor = zoom_factor;
// determine length of line to draw and how long that is in universe units
double AVAILABLE_WIDTH = static_cast<double>(std::max(Value(Width()), 1));
// length in universe units that could be shown if full AVAILABLE_WIDTH was used
double max_shown_length = AVAILABLE_WIDTH / m_scale_factor;
// select an actual shown length in universe units by reducing max_shown_length to a nice round number,
// where nice round numbers are numbers beginning with 1, 2 or 5
// We start by getting the highest power of 10 that is less than the max_shown_length,
// and then we step that initial value up, either by a factor of 2 or 5 if that
// will still fit, or to one of the values taken from fleets and planets
// if they are at least as reat as the standard value but not larger than the max_shown_length
// get appropriate power of 10
double pow10 = floor(log10(max_shown_length));
double init_length = std::pow(10.0, pow10);
double shown_length = init_length;
// determin a preliminary shown length
// see if next higher multiples of 5 or 2 can be used
if (shown_length * 5.0 <= max_shown_length)
shown_length *= 5.0;
else if (shown_length * 2.0 <= max_shown_length)
shown_length *= 2.0;
// DebugLogger() << "MapScaleLine::Update maxLen: " << max_shown_length
// << "; init_length: " << init_length
// << "; shown_length: " << shown_length;
// for (double distance : fixed_distances) {
// DebugLogger() << " MapScaleLine::Update fleet speed: " << distance;
// }
// if there are additional scale steps to check (from fleets & planets)
if (!fixed_distances.empty()) {
// check if the set of fixed distances includes a value that is in between the max_shown_length
// and one zoom step smaller than the max shown length. If so, use that for the shown_length;
// otherwise, check if the set of fixed distances includes a value that is greater than the
// shown_length determined above but still less than the max_shown_length, and if so then use that value.
// Note: if the ZOOM_STEP_SIZE is too large, then potential values in the set of fixed distances
// might get skipped over.
auto distance_ub = fixed_distances.upper_bound(max_shown_length/ZOOM_STEP_SIZE);
if (distance_ub != fixed_distances.end() && *distance_ub <= max_shown_length) {
TraceLogger() << " MapScaleLine::Update distance_ub: " << *distance_ub;
shown_length = *distance_ub;
} else {
distance_ub = fixed_distances.upper_bound(shown_length);
if (distance_ub != fixed_distances.end() && *distance_ub <= max_shown_length) {
TraceLogger() << " MapScaleLine::Update distance_ub: " << *distance_ub;
shown_length = *distance_ub;
}
}
}
// determine end of drawn scale line
m_line_length = GG::X(static_cast<int>(shown_length * m_scale_factor));
// update text
auto label_text = boost::io::str(FlexibleFormat(UserString("MAP_SCALE_INDICATOR")) %
std::to_string(static_cast<int>(shown_length)));
m_label->SetText(std::move(label_text));
m_label->Resize(GG::Pt(GG::X(m_line_length), Height()));
}
private:
void UpdateEnabled() {
m_enabled = GetOptionsDB().Get<bool>("ui.map.scale.legend.shown");
if (m_enabled)
AttachChild(m_label);
else
DetachChild(m_label);
}
double m_scale_factor = 1.0;
GG::X m_line_length = GG::X1;
std::shared_ptr<GG::TextControl> m_label;
boost::signals2::scoped_connection m_legend_show_connection;
bool m_enabled = false;
};
////////////////////////////////////////////////////////////
// MapWndPopup
////////////////////////////////////////////////////////////
MapWndPopup::MapWndPopup(std::string t, GG::X default_x, GG::Y default_y, GG::X default_w, GG::Y default_h,
GG::Flags<GG::WndFlag> flags, std::string_view config_name) :
CUIWnd(std::move(t), default_x, default_y, default_w, default_h, flags, config_name)
{}
MapWndPopup::MapWndPopup(std::string t, GG::Flags<GG::WndFlag> flags, std::string_view config_name) :
CUIWnd(std::move(t), flags, config_name)
{}
void MapWndPopup::CompleteConstruction() {
CUIWnd::CompleteConstruction();
// MapWndPopupWnd is registered as a top level window, the same as ClientUI and MapWnd.
// Consequently, when the GUI shutsdown either could be destroyed before this Wnd
if (auto client = ClientUI::GetClientUI())
if (auto mapwnd = client->GetMapWnd())
mapwnd->RegisterPopup(std::static_pointer_cast<MapWndPopup>(shared_from_this()));
}
MapWndPopup::~MapWndPopup() {
if (!Visible()) {
// Make sure it doesn't save visible = 0 to the config (doesn't
// make sense for windows that are created/destroyed repeatedly),
// try/catch because this is called from a dtor and OptionsDB
// functions can throw.
try {
Show();
} catch (const std::exception& e) {
ErrorLogger() << "~MapWndPopup() : caught exception cleaning up a popup: " << e.what();
}
}
// MapWndPopupWnd is registered as a top level window, the same as ClientUI and MapWnd.
// Consequently, when the GUI shutsdown either could be destroyed before this Wnd
if (auto client = ClientUI::GetClientUI())
if (auto mapwnd = client->GetMapWnd())
mapwnd->RemovePopup(this);
}
void MapWndPopup::CloseClicked()
{ CUIWnd::CloseClicked(); }
void MapWndPopup::Close()
{ CloseClicked(); }
////////////////////////////////////////////////
// MapWnd::MovementLineData
////////////////////////////////////////////////
MapWnd::MovementLineData::MovementLineData(const std::vector<MovePathNode>& path_,
const std::map<std::pair<int, int>, LaneEndpoints>& lane_end_points_map,
GG::Clr colour_, int empireID) :
path(path_),
colour(colour_)
{
// generate screen position vertices
if (path.empty() || path.size() == 1)
return; // nothing to draw. need at least two nodes at different locations to draw a line
//DebugLogger() << "move_line path: ";
//for (const MovePathNode& node : path)
// DebugLogger() << " ... " << node.object_id << " (" << node.x << ", " << node.y << ") eta: " << node.eta << " turn_end: " << node.turn_end;
// draw lines connecting points of interest along path. only draw a line if the previous and
// current positions of the ends of the line are known.
const MovePathNode& first_node = path.front();
double prev_node_x = first_node.x;
double prev_node_y = first_node.y;
int prev_sys_id = first_node.object_id;
int next_sys_id = INVALID_OBJECT_ID;
auto prev_eta = first_node.eta;
const ScriptingContext context;
const Empire* empire = GetEmpire(empireID);
std::set<int> unobstructed;
bool s_flag = false;
bool calc_s_flag = false;
if (empire) {
unobstructed = empire->SupplyUnobstructedSystems();
calc_s_flag = true;
//s_flag = ((first_node.object_id != INVALID_OBJECT_ID) && !unobstructed.contains(first_node.object_id));
}
for (const MovePathNode& node : path) {
// stop rendering if end of path is indicated
if (node.eta == Fleet::ETA_UNKNOWN || node.eta == Fleet::ETA_NEVER || node.eta == Fleet::ETA_OUT_OF_RANGE)
break;
// 1) Get systems at ends of lane on which current node is located.
if (node.object_id == INVALID_OBJECT_ID) {
// node is in open space.
// node should have valid prev_sys_id and node.lane_end_id to get info about starlane this node is on
prev_sys_id = node.lane_start_id;
next_sys_id = node.lane_end_id;
} else {
// node is at a system.
// node should / may not have a valid lane_end_id, but this system's id is the end of a lane approaching it
next_sys_id = node.object_id;
// if this is the first node of the path, prev_sys_id should be set to node.object_id from pre-loop initialization.
// if this node is later in the path, prev_sys_id should have been set in previous loop iteration
}
// skip invalid line segments
if (prev_sys_id == next_sys_id || next_sys_id == INVALID_OBJECT_ID || prev_sys_id == INVALID_OBJECT_ID)
continue;
// 2) Get apparent universe positions of nodes, which depend on endpoints of lane and actual universe position of nodes
// get lane end points
auto ends_it = lane_end_points_map.find({prev_sys_id, next_sys_id});
if (ends_it == lane_end_points_map.end()) {
ErrorLogger() << "couldn't get endpoints of lane for move line";
break;
}
const LaneEndpoints& lane_endpoints = ends_it->second;
// get on-screen positions of nodes shifted to fit on starlane
auto start_xy = ScreenPosOnStarlane(prev_node_x, prev_node_y, prev_sys_id, next_sys_id, lane_endpoints, context);
auto end_xy = ScreenPosOnStarlane(node.x, node.y, prev_sys_id, next_sys_id, lane_endpoints, context);
if (!start_xy) {
ErrorLogger() << "System " << prev_sys_id << " has invalid screen coordinates.";
continue;
}
if (!end_xy) {
ErrorLogger() << "System " << next_sys_id << " has invalid screen coordinates.";
continue;
}
// 3) Add points for line segment to list of Vertices
bool b_flag = node.post_blockade;
s_flag = s_flag || (calc_s_flag &&
((node.object_id != INVALID_OBJECT_ID) && !unobstructed.contains(node.object_id)));
vertices.emplace_back(start_xy->first, start_xy->second, prev_eta, false, b_flag, s_flag);
vertices.emplace_back(end_xy->first, end_xy->second, node.eta, node.turn_end, b_flag, s_flag);
// 4) prep for next iteration
prev_node_x = node.x;
prev_node_y = node.y;
prev_eta = node.eta;
if (node.object_id != INVALID_OBJECT_ID) { // only need to update previous and next sys ids if current node is at a system
prev_sys_id = node.object_id; // to be used in next iteration
next_sys_id = INVALID_OBJECT_ID; // to be set in next iteration
}
}
}
///////////////////////////
// MapWnd
///////////////////////////
MapWnd::MapWnd() :
GG::Wnd(-AppWidth(), -AppHeight(),
static_cast<GG::X>(GetUniverse().UniverseWidth() * ZOOM_MAX + AppWidth() * 1.5),
static_cast<GG::Y>(GetUniverse().UniverseWidth() * ZOOM_MAX + AppHeight() * 1.5),
GG::INTERACTIVE | GG::DRAGABLE)
{}
void MapWnd::CompleteConstruction() {
GG::Wnd::CompleteConstruction();
SetName("MapWnd");
using boost::placeholders::_1;
using boost::placeholders::_2;
ScriptingContext context;
m_obj_delete_connection = context.ContextUniverse().UniverseObjectDeleteSignal.connect(
[this](std::shared_ptr<const UniverseObject> obj) { UniverseObjectDeleted(obj); });
// toolbar
m_toolbar = GG::Wnd::Create<CUIToolBar>();
m_toolbar->SetName("MapWnd Toolbar");
GG::GUI::GetGUI()->Register(m_toolbar);
m_toolbar->Hide();
//////////////////////////////
// Toolbar buttons and icons
//////////////////////////////
// turn button
// determine size from the text that will go into the button, using a test year string
std::string unready_longest_reasonable = boost::io::str(FlexibleFormat(UserString("MAP_BTN_TURN_UNREADY")) % "99999");
m_btn_turn = Wnd::Create<CUIButton>(unready_longest_reasonable);
m_btn_turn->Resize(m_btn_turn->MinUsableSize());
m_btn_turn->LeftClickedSignal.connect([this]() { EndTurn(); });
m_btn_turn->LeftClickedSignal.connect(&PlayTurnButtonClickSound);
m_btn_turn->RightClickedSignal.connect(
[this]() { ShowTurnButtonPopup(m_btn_turn, [this]() { EndTurn(); }, [this]() { RevertOrders(); }); });
RefreshTurnButtonTooltip();
boost::filesystem::path button_texture_dir = ClientUI::ArtDir() / "icons" / "buttons";
// auto turn button
m_btn_auto_turn = Wnd::Create<CUIButton>(
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "manual_turn.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "auto_turn.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "manual_turn_mouseover.png")));
m_btn_auto_turn->LeftClickedSignal.connect([this]() { ToggleAutoEndTurn(); });
m_btn_auto_turn->Resize(ICON_SIZE);
m_btn_auto_turn->SetMinSize(ICON_SIZE);
ToggleAutoEndTurn(); // toggle twice to set textures without changing default setting state
ToggleAutoEndTurn();
// timeout remain label
// determine size from the text that will go into label, using a test time string
std::string timeout_longest_reasonable =
boost::io::str(FlexibleFormat(UserString("MAP_TIMEOUT_HRS_MINS")) % 999 % 59); // minutes part never exceeds 59, turn interval hopefully doesn't exceed month
m_timeout_remain = Wnd::Create<CUILabel>(std::move(timeout_longest_reasonable));
m_timeout_remain->Resize(m_timeout_remain->MinUsableSize());
// FPS indicator
m_FPS = GG::Wnd::Create<FPSIndicator>();
m_FPS->Hide();
// Menu button
m_btn_menu = Wnd::Create<SettableInWindowCUIButton>(
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "menu.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "menu_clicked.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "menu_mouseover.png")),
InWndRect(m_toolbar.get()));
m_btn_menu->SetMinSize(MENU_ICON_SIZE);
m_btn_menu->LeftClickedSignal.connect([this]() { ShowMenu(); });
m_btn_menu->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_btn_menu->SetBrowseInfoWnd(GG::Wnd::Create<TextBrowseWnd>(
UserString("MAP_BTN_MENU"), UserString("MAP_BTN_MENU_DESC")));
// Encyclo"pedia" button
m_btn_pedia = Wnd::Create<SettableInWindowCUIButton>(
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "pedia.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "pedia_clicked.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "pedia_mouseover.png")),
InWndRect(m_toolbar.get()));
m_btn_pedia->SetMinSize(MENU_ICON_SIZE);
m_btn_pedia->LeftClickedSignal.connect([this]() { TogglePedia(); });
m_btn_pedia->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_btn_pedia->SetBrowseInfoWnd(GG::Wnd::Create<TextBrowseWnd>(
UserString("MAP_BTN_PEDIA"), UserString("MAP_BTN_PEDIA_DESC")));
// Graphs button
m_btn_graphs = Wnd::Create<SettableInWindowCUIButton>(
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "charts.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "charts_clicked.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "charts_mouseover.png")),
InWndRect(m_toolbar.get()));
m_btn_graphs->SetMinSize(MENU_ICON_SIZE);
m_btn_graphs->LeftClickedSignal.connect([this]() { ShowGraphs(); });
m_btn_graphs->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_btn_graphs->SetBrowseInfoWnd(GG::Wnd::Create<TextBrowseWnd>(
UserString("MAP_BTN_GRAPH"), UserString("MAP_BTN_GRAPH_DESC")));
// Design button
m_btn_design = Wnd::Create<SettableInWindowCUIButton>(
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "design.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "design_clicked.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "design_mouseover.png")),
InWndRect(m_toolbar.get()));
m_btn_design->SetMinSize(MENU_ICON_SIZE);
m_btn_design->LeftClickedSignal.connect([this]() { ToggleDesign(); });
m_btn_design->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_btn_design->SetBrowseInfoWnd(GG::Wnd::Create<TextBrowseWnd>(
UserString("MAP_BTN_DESIGN"), UserString("MAP_BTN_DESIGN_DESC")));
// Government button
m_btn_government = Wnd::Create<SettableInWindowCUIButton>(
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "government.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "government_clicked.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "government_mouseover.png")),
InWndRect(m_toolbar.get()));
m_btn_government->SetMinSize(MENU_ICON_SIZE);
m_btn_government->LeftClickedSignal.connect([this]() { ToggleGovernment(); });
m_btn_government->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_btn_government->SetBrowseInfoWnd(GG::Wnd::Create<TextBrowseWnd>(
UserString("MAP_BTN_GOVERNMENT"), UserString("MAP_BTN_GOVERNMENT_DESC")));
// Production button
m_btn_production = Wnd::Create<SettableInWindowCUIButton>(
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "production.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "production_clicked.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "production_mouseover.png")),
InWndRect(m_toolbar.get()));
m_btn_production->SetMinSize(MENU_ICON_SIZE);
m_btn_production->LeftClickedSignal.connect([this]() { ToggleProduction(); });
m_btn_production->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_btn_production->SetBrowseInfoWnd(GG::Wnd::Create<TextBrowseWnd>(
UserString("MAP_BTN_PRODUCTION"), UserString("MAP_BTN_PRODUCTION_DESC")));
// Research button
m_btn_research = Wnd::Create<SettableInWindowCUIButton>(
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "research.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "research_clicked.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "research_mouseover.png")),
InWndRect(m_toolbar.get()));
m_btn_research->SetMinSize(MENU_ICON_SIZE);
m_btn_research->LeftClickedSignal.connect(
[this]() {
const ScriptingContext context;
ToggleResearch(context);
});
m_btn_research->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_btn_research->SetBrowseInfoWnd(GG::Wnd::Create<TextBrowseWnd>(
UserString("MAP_BTN_RESEARCH"), UserString("MAP_BTN_RESEARCH_DESC")));
// Objects button
m_btn_objects = Wnd::Create<SettableInWindowCUIButton>(
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "objects.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "objects_clicked.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "objects_mouseover.png")),
InWndRect(m_toolbar.get()));
m_btn_objects->SetMinSize(MENU_ICON_SIZE);
m_btn_objects->LeftClickedSignal.connect([this]() { ToggleObjects(); });
m_btn_objects->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_btn_objects->SetBrowseInfoWnd(GG::Wnd::Create<TextBrowseWnd>(
UserString("MAP_BTN_OBJECTS"), UserString("MAP_BTN_OBJECTS_DESC")));
// Empires button
m_btn_empires = Wnd::Create<SettableInWindowCUIButton>(
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "empires.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "empires_clicked.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "empires_mouseover.png")),
InWndRect(m_toolbar.get()));
m_btn_empires->SetMinSize(MENU_ICON_SIZE);
m_btn_empires->LeftClickedSignal.connect([this]() { ToggleEmpires(); });
m_btn_empires->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_btn_empires->SetBrowseInfoWnd(GG::Wnd::Create<TextBrowseWnd>(
UserString("MAP_BTN_EMPIRES"), UserString("MAP_BTN_EMPIRES_DESC")));
// SitRep button
m_btn_siterep = Wnd::Create<SettableInWindowCUIButton>(
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "sitrep.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "sitrep_clicked.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "sitrep_mouseover.png")),
InWndRect(m_toolbar.get()));
m_btn_siterep->SetMinSize(MENU_ICON_SIZE);
m_btn_siterep->LeftClickedSignal.connect([this]() { ToggleSitRep(); });
m_btn_siterep->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_btn_siterep->SetBrowseInfoWnd(GG::Wnd::Create<TextBrowseWnd>(
UserString("MAP_BTN_SITREP"), UserString("MAP_BTN_SITREP_DESC")));
// Messages button
m_btn_messages = Wnd::Create<SettableInWindowCUIButton>(
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "messages.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "messages_clicked.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "messages_mouseover.png")),
InWndRect(m_toolbar.get()));
m_btn_messages->SetMinSize(MENU_ICON_SIZE);
m_btn_messages->LeftClickedSignal.connect([this]() { ToggleMessages(); });
m_btn_messages->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_btn_messages->SetBrowseInfoWnd(GG::Wnd::Create<TextBrowseWnd>(
UserString("MAP_BTN_MESSAGES"), UserString("MAP_BTN_MESSAGES_DESC")));
// Moderator button
m_btn_moderator = Wnd::Create<SettableInWindowCUIButton>(
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "moderator.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "moderator_clicked.png")),
GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "moderator_mouseover.png")),
InWndRect(m_toolbar.get()));
m_btn_moderator->SetMinSize(MENU_ICON_SIZE);
m_btn_moderator->LeftClickedSignal.connect([this]() { ToggleModeratorActions(); });
m_btn_moderator->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_btn_moderator->SetBrowseInfoWnd(GG::Wnd::Create<TextBrowseWnd>(
UserString("MAP_BTN_MODERATOR"), UserString("MAP_BTN_MODERATOR_DESC")));
// resources
m_population = GG::Wnd::Create<StatisticIcon>(ClientUI::MeterIcon(MeterType::METER_POPULATION),
0, 3, false, ICON_SINGLE_WIDTH, m_btn_turn->Height());
m_population->SetName("Population StatisticIcon");
m_industry = GG::Wnd::Create<StatisticIcon>(ClientUI::MeterIcon(MeterType::METER_INDUSTRY),
0, 3, false, ICON_SINGLE_WIDTH, m_btn_turn->Height());
m_industry->SetName("Industry StatisticIcon");
m_industry->LeftClickedSignal.connect([this](auto) { ToggleProduction(); });
m_stockpile = GG::Wnd::Create<StatisticIcon>(ClientUI::MeterIcon(MeterType::METER_STOCKPILE),
0, 3, false, ICON_DUAL_WIDTH, m_btn_turn->Height());
m_stockpile->SetName("Stockpile StatisticIcon");
m_research = GG::Wnd::Create<StatisticIcon>(ClientUI::MeterIcon(MeterType::METER_RESEARCH),
0, 3, false, ICON_SINGLE_WIDTH, m_btn_turn->Height());
m_research->SetName("Research StatisticIcon");
m_research->LeftClickedSignal.connect([this](auto) { ToggleResearch(ScriptingContext{}); });
m_influence = GG::Wnd::Create<StatisticIcon>(ClientUI::MeterIcon(MeterType::METER_INFLUENCE),
0, 3, false, ICON_DUAL_WIDTH, m_btn_turn->Height());
m_influence->SetName("Influence StatisticIcon");
m_influence->LeftClickedSignal.connect([this](auto) { ToggleGovernment(); });
m_fleet = GG::Wnd::Create<StatisticIcon>(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "sitrep" / "fleet_arrived.png"),
0, 3, false, ICON_SINGLE_WIDTH, m_btn_turn->Height());
m_fleet->SetName("Fleet StatisticIcon");
m_detection = GG::Wnd::Create<StatisticIcon>(ClientUI::MeterIcon(MeterType::METER_DETECTION),
0, 3, false, ICON_SINGLE_WIDTH, m_btn_turn->Height());
m_detection->SetName("Detection StatisticIcon");
GG::SubTexture wasted_ressource_subtexture = GG::SubTexture(
ClientUI::GetTexture(button_texture_dir / "wasted_resource.png", false));
GG::SubTexture wasted_ressource_mouseover_subtexture = GG::SubTexture(
ClientUI::GetTexture(button_texture_dir / "wasted_resource_mouseover.png", false));
GG::SubTexture wasted_ressource_clicked_subtexture = GG::SubTexture(
ClientUI::GetTexture(button_texture_dir / "wasted_resource_clicked.png", false));
m_industry_wasted = Wnd::Create<CUIButton>(
wasted_ressource_subtexture,
wasted_ressource_clicked_subtexture,
wasted_ressource_mouseover_subtexture);
m_research_wasted = Wnd::Create<CUIButton>(
wasted_ressource_subtexture,
wasted_ressource_clicked_subtexture,
wasted_ressource_mouseover_subtexture);
m_industry_wasted->Resize(ICON_SIZE);
m_industry_wasted->SetMinSize(ICON_SIZE);
m_research_wasted->Resize(ICON_SIZE);
m_research_wasted->SetMinSize(ICON_SIZE);
m_industry_wasted->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_research_wasted->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_industry_wasted->LeftClickedSignal.connect([this]() { ZoomToSystemWithWastedPP(); });
m_research_wasted->LeftClickedSignal.connect([this]() { ToggleResearch(ScriptingContext{}); });
//Set the correct tooltips
RefreshIndustryResourceIndicator();
RefreshResearchResourceIndicator();
/////////////////////////////////////
// place buttons / icons on toolbar
/////////////////////////////////////
std::vector<GG::X> widths{
m_btn_turn->Width(), ICON_WIDTH, ICON_WIDTH,
ICON_WIDTH, ICON_WIDTH, ICON_SINGLE_WIDTH,
ICON_DUAL_WIDTH, ICON_WIDTH, ICON_SINGLE_WIDTH,
ICON_DUAL_WIDTH, ICON_SINGLE_WIDTH, ICON_SINGLE_WIDTH,
ICON_SINGLE_WIDTH, MENU_ICON_SIZE.x, MENU_ICON_SIZE.x,
MENU_ICON_SIZE.x, MENU_ICON_SIZE.x, MENU_ICON_SIZE.x,
MENU_ICON_SIZE.x, MENU_ICON_SIZE.x, MENU_ICON_SIZE.x,
MENU_ICON_SIZE.x, MENU_ICON_SIZE.x, MENU_ICON_SIZE.x,
MENU_ICON_SIZE.x};
std::vector<float> stretches{
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f,
2.0f, 0.0f, 1.0f,
2.0f, 1.0f, 1.0f,
1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f};
auto layout = GG::Wnd::Create<GG::Layout>(m_toolbar->ClientUpperLeft().x, m_toolbar->ClientUpperLeft().y,
m_toolbar->ClientWidth(), m_toolbar->ClientHeight(),
1, widths.size());
layout->SetName("Toolbar Layout");
layout->SetCellMargin(5);
layout->SetBorderMargin(5);
layout->SetMinimumRowHeight(0, ICON_SIZE.y);
//layout->RenderOutline(true);
layout->SetColumnStretches(stretches);
layout->SetMinimumColumnWidths(widths);
int layout_column{0};
layout->Add(m_btn_turn, 0, layout_column++, GG::ALIGN_CENTER | GG::ALIGN_VCENTER); // 0
layout->Add(m_btn_auto_turn, 0, layout_column++, GG::ALIGN_CENTER | GG::ALIGN_VCENTER);
layout->Add(m_timeout_remain, 0, layout_column++, GG::ALIGN_CENTER | GG::ALIGN_VCENTER);
layout->Add(m_FPS, 0, layout_column++, GG::ALIGN_CENTER | GG::ALIGN_VCENTER);
layout->Add(m_industry_wasted, 0, layout_column++, GG::ALIGN_RIGHT | GG::ALIGN_VCENTER);
layout->Add(m_industry, 0, layout_column++, GG::ALIGN_LEFT | GG::ALIGN_VCENTER); // 5
layout->Add(m_stockpile, 0, layout_column++, GG::ALIGN_LEFT | GG::ALIGN_VCENTER);
layout->Add(m_research_wasted, 0, layout_column++, GG::ALIGN_RIGHT | GG::ALIGN_VCENTER);
layout->Add(m_research, 0, layout_column++, GG::ALIGN_LEFT | GG::ALIGN_VCENTER);
layout->Add(m_influence, 0, layout_column++, GG::ALIGN_LEFT | GG::ALIGN_VCENTER);
layout->Add(m_fleet, 0, layout_column++, GG::ALIGN_LEFT | GG::ALIGN_VCENTER); // 10
layout->Add(m_population, 0, layout_column++, GG::ALIGN_LEFT | GG::ALIGN_VCENTER);
layout->Add(m_detection, 0, layout_column++, GG::ALIGN_LEFT | GG::ALIGN_VCENTER);
layout->Add(m_btn_moderator, 0, layout_column++, GG::ALIGN_CENTER | GG::ALIGN_VCENTER);
layout->Add(m_btn_messages, 0, layout_column++, GG::ALIGN_CENTER | GG::ALIGN_VCENTER);
layout->Add(m_btn_siterep, 0, layout_column++, GG::ALIGN_CENTER | GG::ALIGN_VCENTER); // 15
layout->Add(m_btn_empires, 0, layout_column++, GG::ALIGN_CENTER | GG::ALIGN_VCENTER);
layout->Add(m_btn_objects, 0, layout_column++, GG::ALIGN_CENTER | GG::ALIGN_VCENTER);
layout->Add(m_btn_research, 0, layout_column++, GG::ALIGN_CENTER | GG::ALIGN_VCENTER);
layout->Add(m_btn_production, 0, layout_column++, GG::ALIGN_CENTER | GG::ALIGN_VCENTER);
layout->Add(m_btn_design, 0, layout_column++, GG::ALIGN_CENTER | GG::ALIGN_VCENTER); // 20
layout->Add(m_btn_government, 0, layout_column++, GG::ALIGN_CENTER | GG::ALIGN_VCENTER);
layout->Add(m_btn_graphs, 0, layout_column++, GG::ALIGN_CENTER | GG::ALIGN_VCENTER);
layout->Add(m_btn_pedia, 0, layout_column++, GG::ALIGN_CENTER | GG::ALIGN_VCENTER);
layout->Add(m_btn_menu, 0, layout_column++, GG::ALIGN_CENTER | GG::ALIGN_VCENTER);
m_toolbar->SetLayout(layout);
///////////////////
// Misc widgets on map screen
///////////////////
// scale line
m_scale_line = GG::Wnd::Create<MapScaleLine>(GG::X(LAYOUT_MARGIN), GG::Y(LAYOUT_MARGIN) + m_toolbar->Height(),
SCALE_LINE_MAX_WIDTH, SCALE_LINE_HEIGHT);
GG::GUI::GetGUI()->Register(m_scale_line);
int sel_system_id = SidePanel::SystemID();
m_scale_line->Update(ZoomFactor(), m_selected_fleet_ids, sel_system_id);
m_scale_line->Hide();
// Zoom slider
static constexpr int ZOOM_SLIDER_MIN = static_cast<int>(ZOOM_IN_MIN_STEPS),
ZOOM_SLIDER_MAX = static_cast<int>(ZOOM_IN_MAX_STEPS);
m_zoom_slider = GG::Wnd::Create<CUISlider<double>>(ZOOM_SLIDER_MIN, ZOOM_SLIDER_MAX,
GG::Orientation::VERTICAL,
GG::INTERACTIVE | GG::ONTOP);
m_zoom_slider->MoveTo(GG::Pt(m_btn_turn->Left(), m_scale_line->Bottom() + GG::Y(LAYOUT_MARGIN)));
m_zoom_slider->Resize(GG::Pt(GG::X(ClientUI::ScrollWidth()), ZOOM_SLIDER_HEIGHT));
m_zoom_slider->SlideTo(m_zoom_steps_in);
GG::GUI::GetGUI()->Register(m_zoom_slider);
m_zoom_slider->Hide();
m_zoom_slider->SlidSignal.connect([this](double pos, double, double) { SetZoom(pos, false); });
m_slider_show_connection = GetOptionsDB().OptionChangedSignal("ui.map.zoom.slider.shown").connect(
[this]() { RefreshSliders(); });
///////////////////
// Map sub-windows
///////////////////
// system-view side panel
m_side_panel = GG::Wnd::Create<SidePanel>(MAP_SIDEPANEL_WND_NAME);
GG::GUI::GetGUI()->Register(m_side_panel);
SidePanel::SystemSelectedSignal.connect( boost::bind(&MapWnd::SelectSystem, this, _1));
SidePanel::PlanetSelectedSignal.connect( boost::bind(&MapWnd::SelectPlanet, this, _1));
SidePanel::PlanetDoubleClickedSignal.connect( boost::bind(&MapWnd::PlanetDoubleClicked, this, _1));
SidePanel::PlanetRightClickedSignal.connect( boost::bind(&MapWnd::PlanetRightClicked, this, _1));
SidePanel::BuildingRightClickedSignal.connect( boost::bind(&MapWnd::BuildingRightClicked, this, _1));
// not strictly necessary, as in principle whenever any Planet
// changes, all meter estimates and resource pools should / could be
// updated. however, this is a convenience to limit the updates to
// what is actually being shown in the sidepanel right now, which is
// useful since most Planet changes will be due to focus
// changes on the sidepanel, and most differences in meter estimates
// and resource pools due to this will be in the same system
SidePanel::ResourceCenterChangedSignal.connect([this](){
if (GetOptionsDB().Get<bool>("ui.map.object-changed.meter-refresh")) {
ScriptingContext context;
context.ContextUniverse().UpdateMeterEstimates(context);
UpdateEmpireResourcePools();
}
});
// situation report window
m_sitrep_panel = GG::Wnd::Create<SitRepPanel>(SITREP_WND_NAME);
// Wnd is manually closed by user
m_sitrep_panel->ClosingSignal.connect(boost::bind(&MapWnd::HideSitRep, this));
if (m_sitrep_panel->Visible()) {
PushWndStack(m_sitrep_panel);
m_btn_siterep->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "sitrep_mouseover.png")));
m_btn_siterep->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "sitrep.png")));
}
// encyclopedia panel
m_pedia_panel = GG::Wnd::Create<EncyclopediaDetailPanel>(GG::ONTOP | GG::INTERACTIVE | GG::DRAGABLE | GG::RESIZABLE | CLOSABLE | PINABLE, MAP_PEDIA_WND_NAME);
// Wnd is manually closed by user
m_pedia_panel->ClosingSignal.connect(boost::bind(&MapWnd::HidePedia, this));
if (m_pedia_panel->Visible()) {
PushWndStack(m_pedia_panel);
m_btn_pedia->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "pedia_mouseover.png")));
m_btn_pedia->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "pedia.png")));
}
// objects list
m_object_list_wnd = GG::Wnd::Create<ObjectListWnd>(OBJECT_WND_NAME);
// Wnd is manually closed by user
m_object_list_wnd->ClosingSignal.connect(boost::bind(&MapWnd::HideObjects, this));
m_object_list_wnd->ObjectDumpSignal.connect(boost::bind(&ClientUI::DumpObject, ClientUI::GetClientUI(), _1));
if (m_object_list_wnd->Visible()) {
PushWndStack(m_object_list_wnd);
m_btn_objects->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "objects_mouseover.png")));
m_btn_objects->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "objects.png")));
}
// moderator actions
m_moderator_wnd = GG::Wnd::Create<ModeratorActionsWnd>(MODERATOR_WND_NAME);
m_moderator_wnd->ClosingSignal.connect(boost::bind(&MapWnd::HideModeratorActions, this));
if (m_moderator_wnd->Visible()) {
PushWndStack(m_moderator_wnd);
m_btn_moderator->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "moderator_mouseover.png")));
m_btn_moderator->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "moderator.png")));
}
// Combat report
m_combat_report_wnd = GG::Wnd::Create<CombatReportWnd>(COMBAT_REPORT_WND_NAME);
// government window
m_government_wnd = GG::Wnd::Create<GovernmentWnd>(GOVERNMENT_WND_NAME);
// Wnd is manually closed by user
m_government_wnd->ClosingSignal.connect(
boost::bind(&MapWnd::HideGovernment, this));
if (m_government_wnd->Visible()) {
PushWndStack(m_government_wnd);
m_btn_government->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "government_mouseover.png")));
m_btn_government->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "government.png")));
}
// position CUIWnds owned by the MapWnd
InitializeWindows();
// messages and empires windows
if (ClientUI* cui = ClientUI::GetClientUI()) {
if (const auto& msg_wnd = cui->GetMessageWnd()) {
// Wnd is manually closed by user
msg_wnd->ClosingSignal.connect(boost::bind(&MapWnd::HideMessages, this));
if (msg_wnd->Visible()) {
PushWndStack(msg_wnd);
m_btn_messages->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "messages_mouseover.png")));
m_btn_messages->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "messages.png")));
}
}
if (const auto& plr_wnd = cui->GetPlayerListWnd()) {
// Wnd is manually closed by user
plr_wnd->ClosingSignal.connect(boost::bind(&MapWnd::HideEmpires, this));
if (plr_wnd->Visible()) {
PushWndStack(plr_wnd);
m_btn_empires->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "empires_mouseover.png")));
m_btn_empires->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "empires.png")));
}
}
}
GGHumanClientApp::GetApp()->RepositionWindowsSignal.connect(
boost::bind(&MapWnd::InitializeWindows, this));
// research window
m_research_wnd = GG::Wnd::Create<ResearchWnd>(AppWidth(), AppHeight() - m_toolbar->Height());
m_research_wnd->MoveTo(GG::Pt(GG::X0, m_toolbar->Height()));
GG::GUI::GetGUI()->Register(m_research_wnd);
m_research_wnd->Hide();
// production window
m_production_wnd = GG::Wnd::Create<ProductionWnd>(AppWidth(), AppHeight() - m_toolbar->Height());
m_production_wnd->MoveTo(GG::Pt(GG::X0, m_toolbar->Height()));
GG::GUI::GetGUI()->Register(m_production_wnd);
m_production_wnd->Hide();
m_production_wnd->SystemSelectedSignal.connect(boost::bind(&MapWnd::SelectSystem, this, _1));
m_production_wnd->PlanetSelectedSignal.connect(boost::bind(&MapWnd::SelectPlanet, this, _1));
// design window
m_design_wnd = GG::Wnd::Create<DesignWnd>(AppWidth(), AppHeight() - m_toolbar->Height());
m_design_wnd->MoveTo(GG::Pt(GG::X0, m_toolbar->Height()));
GG::GUI::GetGUI()->Register(m_design_wnd);
m_design_wnd->Hide();
//////////////////
// General Gamestate response signals
//////////////////
FleetUIManager& fm = FleetUIManager::GetFleetUIManager();
fm.ActiveFleetWndChangedSignal.connect(boost::bind(&MapWnd::SelectedFleetsChanged, this));
fm.ActiveFleetWndSelectedFleetsChangedSignal.connect(boost::bind(&MapWnd::SelectedFleetsChanged, this));
fm.ActiveFleetWndSelectedShipsChangedSignal.connect(boost::bind(&MapWnd::SelectedShipsChanged, this));
fm.FleetRightClickedSignal.connect(boost::bind(&MapWnd::FleetRightClicked, this, _1));
fm.ShipRightClickedSignal.connect(boost::bind(&MapWnd::ShipRightClicked, this, _1));
DoLayout();
// Connect keyboard accelerators for map
ConnectKeyboardAcceleratorSignals();
m_timeout_clock.Stop();
m_timeout_clock.Connect(this);
}
void MapWnd::DoLayout() {
m_toolbar->Resize(GG::Pt(AppWidth(), TOOLBAR_HEIGHT));
m_research_wnd->Resize(GG::Pt(AppWidth(), AppHeight() - m_toolbar->Height()));
m_production_wnd->Resize(GG::Pt(AppWidth(), AppHeight() - m_toolbar->Height()));
m_design_wnd->Resize(GG::Pt(AppWidth(), AppHeight() - m_toolbar->Height()));
m_government_wnd->ValidatePosition();
m_sitrep_panel->ValidatePosition();
m_object_list_wnd->ValidatePosition();
m_pedia_panel->ValidatePosition();
m_side_panel->ValidatePosition();
m_combat_report_wnd->ValidatePosition();
m_moderator_wnd->ValidatePosition();
if (ClientUI* cui = ClientUI::GetClientUI()) {
if (const auto& msg_wnd = cui->GetMessageWnd())
msg_wnd->ValidatePosition();
if (const auto& plr_wnd = cui->GetPlayerListWnd())
plr_wnd->ValidatePosition();
}
FleetUIManager::GetFleetUIManager().CullEmptyWnds();
for (auto& fwnd : FleetUIManager::GetFleetUIManager()) {
if (auto wnd = fwnd.lock()) {
wnd->ValidatePosition();
} else {
ErrorLogger() << "MapWnd::DoLayout(): null FleetWnd* found in the FleetUIManager::iterator.";
}
}
}
void MapWnd::InitializeWindows() {
const GG::X SIDEPANEL_WIDTH(GetOptionsDB().Get<GG::X>("ui.map.sidepanel.width"));
// system-view side panel
const GG::Pt sidepanel_ul(AppWidth() - SIDEPANEL_WIDTH, m_toolbar->Bottom());
const GG::Pt sidepanel_wh(SIDEPANEL_WIDTH, AppHeight() - m_toolbar->Height());
// situation report window
const GG::Pt sitrep_ul(SCALE_LINE_MAX_WIDTH + LAYOUT_MARGIN, m_toolbar->Bottom());
static constexpr GG::Pt sitrep_wh(SITREP_PANEL_WIDTH, SITREP_PANEL_HEIGHT);
// encyclopedia panel
const GG::Pt pedia_ul(SCALE_LINE_MAX_WIDTH + LAYOUT_MARGIN, m_toolbar->Bottom() + SITREP_PANEL_HEIGHT);
static constexpr GG::Pt pedia_wh(SITREP_PANEL_WIDTH*3/2, SITREP_PANEL_HEIGHT*3);
// objects list
const GG::Pt object_list_ul(SCALE_LINE_MAX_WIDTH/2, m_scale_line->Bottom() + GG::Y(LAYOUT_MARGIN));
static constexpr GG::Pt object_list_wh(SITREP_PANEL_WIDTH*2, SITREP_PANEL_HEIGHT*3);
// moderator actions
const GG::Pt moderator_ul(GG::X0, m_scale_line->Bottom() + GG::Y(LAYOUT_MARGIN));
static constexpr GG::Pt moderator_wh(SITREP_PANEL_WIDTH, SITREP_PANEL_HEIGHT);
// Combat report
static constexpr GG::Pt combat_log_ul(GG::X(150), GG::Y(50));
static constexpr GG::Pt combat_log_wh(GG::X(400), GG::Y(300));
// government window
const GG::Pt gov_ul(GG::X0, m_scale_line->Bottom() + m_scale_line->Height() + GG::Y(LAYOUT_MARGIN*2));
static constexpr GG::Pt gov_wh(SITREP_PANEL_WIDTH*2, SITREP_PANEL_HEIGHT*3);
m_side_panel-> InitSizeMove(sidepanel_ul, sidepanel_ul + sidepanel_wh);
m_sitrep_panel-> InitSizeMove(sitrep_ul, sitrep_ul + sitrep_wh);
m_pedia_panel-> InitSizeMove(pedia_ul, pedia_ul + pedia_wh);
m_object_list_wnd-> InitSizeMove(object_list_ul, object_list_ul + object_list_wh);
m_moderator_wnd-> InitSizeMove(moderator_ul, moderator_ul + moderator_wh);
m_combat_report_wnd->InitSizeMove(combat_log_ul, combat_log_ul + combat_log_wh);
m_government_wnd-> InitSizeMove(gov_ul, gov_ul + gov_wh);
}
GG::Pt MapWnd::ClientUpperLeft() const noexcept
{ return UpperLeft() + GG::Pt(AppWidth(), AppHeight()); }
double MapWnd::ZoomFactor() const
{ return ZoomScaleFactor(m_zoom_steps_in); }
GG::Pt MapWnd::ScreenCoordsFromUniversePosition(double universe_x, double universe_y) const {
GG::Pt cl_ul = ClientUpperLeft();
GG::X x(GG::ToX(universe_x * ZoomFactor()) + cl_ul.x);
GG::Y y(GG::ToY(universe_y * ZoomFactor()) + cl_ul.y);
return GG::Pt(x, y);
}
std::pair<double, double> MapWnd::UniversePositionFromScreenCoords(GG::Pt screen_coords) const {
GG::Pt cl_ul = ClientUpperLeft();
double x = (screen_coords - cl_ul).x / ZoomFactor();
double y = (screen_coords - cl_ul).y / ZoomFactor();
return {x, y};
}
int MapWnd::SelectedSystemID() const
{ return SidePanel::SystemID(); }
int MapWnd::SelectedPlanetID() const
{ return m_production_wnd->SelectedPlanetID(); }
int MapWnd::SelectedFleetID() const {
if (!m_selected_fleet_ids.empty())
return *m_selected_fleet_ids.begin();
return INVALID_OBJECT_ID;
}
int MapWnd::SelectedShipID() const {
if (!m_selected_ship_ids.empty())
return *m_selected_ship_ids.begin();
return INVALID_OBJECT_ID;
}
void MapWnd::GetSaveGameUIData(SaveGameUIData& data) const {
data.map_left = Value(Left());
data.map_top = Value(Top());
data.map_zoom_steps_in = m_zoom_steps_in;
data.fleets_exploring = m_fleets_exploring;
}
bool MapWnd::InProductionViewMode() const
{ return m_in_production_view_mode; }
bool MapWnd::InResearchViewMode() const
{ return m_research_wnd->Visible(); }
bool MapWnd::InDesignViewMode() const
{ return m_design_wnd->Visible(); }
ModeratorActionSetting MapWnd::GetModeratorActionSetting() const
{ return m_moderator_wnd->SelectedAction(); }
bool MapWnd::AutoEndTurnEnabled() const
{ return m_auto_end_turn; }
void MapWnd::PreRender() {
// Save CPU / GPU activity by skipping rendering when it's not needed
// As of this writing, the design and research screens have fully opaque backgrounds.
if (m_design_wnd->Visible())
return;
if (m_research_wnd->Visible())
return;
GG::Wnd::PreRender();
DeferredRefreshFleetButtons();
}
void MapWnd::Render() {
// HACK! This is placed here so we can be sure it is executed frequently
// (every time we render), and before we render any of the
// FleetWnds. It doesn't necessarily belong in MapWnd at all.
FleetUIManager::GetFleetUIManager().CullEmptyWnds();
// Save CPU / GPU activity by skipping rendering when it's not needed
// As of this writing, the design and research screens have fully opaque backgrounds.
if (m_design_wnd->Visible())
return;
if (m_research_wnd->Visible())
return;
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
GG::Pt origin_offset = UpperLeft() + GG::Pt(AppWidth(), AppHeight());
glTranslatef(Value(origin_offset.x), Value(origin_offset.y), 0.0f);
glScalef(ZoomFactor(), ZoomFactor(), 1.0f);
glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
RenderStarfields();
RenderGalaxyGas();
RenderVisibilityRadii();
RenderFields();
RenderSystemOverlays();
RenderStarlanes();
RenderSystems();
RenderFleetMovementLines();
glPopClientAttrib();
glPopMatrix();
}
void MapWnd::RenderStarfields() {
if (!GetOptionsDB().Get<bool>("ui.map.background.starfields.shown"))
return;
double starfield_width = GetUniverse().UniverseWidth();
if (m_starfield_verts.empty()) {
ClearStarfieldRenderingBuffers();
Seed(static_cast<int>(starfield_width));
m_starfield_colours.clear();
std::size_t NUM_STARS = std::pow(2.0, 12.0);
m_starfield_verts.reserve(NUM_STARS);
m_starfield_colours.reserve(NUM_STARS);
for (std::size_t i = 0; i < NUM_STARS; ++i) {
float x = RandGaussian(starfield_width/2, starfield_width/3); // RandDouble(0, starfield_width);
float y = RandGaussian(starfield_width/2, starfield_width/3); // RandDouble(0, starfield_width);
float r2 = (x - starfield_width/2)*(x - starfield_width/2) + (y-starfield_width/2)*(y-starfield_width/2);
float z = RandDouble(-100, 100)*std::exp(-r2/(starfield_width*starfield_width/4));
m_starfield_verts.store(x, y, z);
const float brightness = 1.0f - std::pow(RandZeroToOne(), 2);
m_starfield_colours.store(GG::BlendClr(GG::CLR_WHITE, GG::CLR_ZERO, brightness));
}
m_starfield_verts.createServerBuffer();
m_starfield_colours.createServerBuffer();
}
GLfloat window_width = Value(AppWidth());
GLfloat window_height = std::max(1, Value(AppHeight()));
glViewport(0, 0, window_width, window_height);
bool perspective_starfield = true;
GLfloat zpos_1to1 = 0.0f;
if (perspective_starfield) {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
GLfloat aspect_ratio = window_width / window_height;
GLfloat zclip_near = window_height/4;
GLfloat zclip_far = 3*zclip_near;
GLfloat fov_height = zclip_near;
GLfloat fov_width = fov_height * aspect_ratio;
zpos_1to1 = -2*zclip_near; // stars rendered at -viewport_size units should be 1:1 with ortho projected stuff
glFrustum(-fov_width, fov_width,
fov_height, -fov_height,
zclip_near, zclip_far);
glTranslatef(-window_width/2, -window_height/2, 0.0f);
}
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// last: standard map panning
GG::Pt origin_offset = UpperLeft() + GG::Pt(AppWidth(), AppHeight());
glTranslatef(Value(origin_offset.x), Value(origin_offset.y), 0.0f);
glScalef(ZoomFactor(), ZoomFactor(), 1.0f);
glTranslatef(0.0, 0.0, zpos_1to1);
glScalef(1.0f, 1.0f, ZoomFactor());
// first: starfield manipulations
bool rotate_starfield = false;
if (rotate_starfield) {
float ticks = GG::GUI::GetGUI()->Ticks();
glTranslatef(starfield_width/2, starfield_width/2, 0.0f); // move back to original position
glRotatef(ticks/10, 0.0f, 0.0f, 1.0f); // rotate about centre of starfield
glTranslatef(-starfield_width/2, -starfield_width/2, 0.0f); // move centre of starfield to origin
}
float point_star_scale = GetOptionsDB().Get<double>("ui.map.background.starfields.scale");
glPointSize(point_star_scale * std::max(1.0, std::sqrt(ZoomFactor())));
glEnable(GL_POINT_SMOOTH);
glDisable(GL_TEXTURE_2D);
glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
m_starfield_verts.activate();
m_starfield_colours.activate();
glDrawArrays(GL_POINTS, 0, m_starfield_verts.size());
glPopClientAttrib();
glEnable(GL_TEXTURE_2D);
glPointSize(1.0f);
if (perspective_starfield) {
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glViewport(0, 0, window_width, window_height);
}
void MapWnd::RenderFields() {
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
glEnableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// render not visible fields
for (auto& [field_texture, buffer] : m_field_vertices_not_visible) {
glBindTexture(GL_TEXTURE_2D, field_texture->OpenGLId());
buffer.activate();
m_field_texture_coords.activate();
glDrawArrays(GL_QUADS, 0, buffer.size());
}
// if any, render scanlines over not-visible fields
if (!m_field_scanline_circles.empty()
&& GGHumanClientApp::GetApp()->EmpireID() != ALL_EMPIRES
&& GetOptionsDB().Get<bool>("ui.map.scanlines.shown"))
{
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
m_field_scanline_circles.activate();
glBindTexture(GL_TEXTURE_2D, 0);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
m_scanline_shader.SetColor(GetOptionsDB().Get<GG::Clr>("ui.map.field.scanlines.color"));
m_scanline_shader.StartUsing();
glDrawArrays(GL_TRIANGLES, 0, m_field_scanline_circles.size());
m_scanline_shader.StopUsing();
/*glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);*/
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
// render visible fields
for (auto& [field_texture, buffer] : m_field_vertices_visible) {
glBindTexture(GL_TEXTURE_2D, field_texture->OpenGLId());
buffer.activate();
m_field_texture_coords.activate();
glDrawArrays(GL_QUADS, 0, buffer.size());
}
glPopClientAttrib();
}
namespace {
std::shared_ptr<GG::Texture> GetGasTexture() {
static std::shared_ptr<GG::Texture> gas_texture;
if (!gas_texture) {
gas_texture = ClientUI::GetClientUI()->GetTexture(ClientUI::ArtDir() / "galaxy_decoration" / "gaseous_array.png");
gas_texture->SetFilters(GL_NEAREST, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, gas_texture->OpenGLId());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
}
return gas_texture;
}
}
void MapWnd::RenderGalaxyGas() {
if (!GetOptionsDB().Get<bool>("ui.map.background.gas.shown"))
return;
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
if (m_galaxy_gas_quad_vertices.empty())
return;
glEnable(GL_TEXTURE_2D);
glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
glEnableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
m_galaxy_gas_quad_vertices.activate();
m_galaxy_gas_texture_coords.activate();
glBindTexture(GL_TEXTURE_2D, GetGasTexture()->OpenGLId());
glDrawArrays(GL_QUADS, 0, m_galaxy_gas_quad_vertices.size());
glPopClientAttrib();
}
void MapWnd::RenderSystemOverlays() {
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glPushMatrix();
glLoadIdentity();
for (auto& system_icon : m_system_icons)
{ system_icon.second->RenderOverlay(ZoomFactor()); }
glPopMatrix();
}
void MapWnd::RenderSystems() {
glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
glEnableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
const float HALO_SCALE_FACTOR = static_cast<float>(SystemHaloScaleFactor());
if (0.5f < HALO_SCALE_FACTOR && m_star_texture_coords.size()) {
glMatrixMode(GL_TEXTURE);
glTranslatef(0.5f, 0.5f, 0.0f);
glScalef(1.0f / HALO_SCALE_FACTOR, 1.0f / HALO_SCALE_FACTOR, 1.0f);
glTranslatef(-0.5f, -0.5f, 0.0f);
m_star_texture_coords.activate();
for (auto& star_halo_buffer : m_star_halo_quad_vertices) {
if (!star_halo_buffer.second.size())
continue;
glBindTexture(GL_TEXTURE_2D, star_halo_buffer.first->OpenGLId());
star_halo_buffer.second.activate();
glDrawArrays(GL_QUADS, 0, star_halo_buffer.second.size());
}
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
}
if (m_star_texture_coords.size() &&
ClientUI::SystemTinyIconSizeThreshold() < ZoomFactor() * ClientUI::SystemIconSize())
{
m_star_texture_coords.activate();
for (auto& star_core_buffer : m_star_core_quad_vertices) {
if (!star_core_buffer.second.size())
continue;
glBindTexture(GL_TEXTURE_2D, star_core_buffer.first->OpenGLId());
star_core_buffer.second.activate();
glDrawArrays(GL_QUADS, 0, star_core_buffer.second.size());
}
}
// circles around system icons and fog over unexplored systems
bool circles = GetOptionsDB().Get<bool>("ui.map.system.circle.shown");
bool fog_scanlines = false;
const ScriptingContext context;
const Universe& universe = context.ContextUniverse();
const ObjectMap& objects = context.ContextObjects();
const EmpireManager& empires = context.Empires();
const SupplyManager& supply = context.supply;
int empire_id = GGHumanClientApp::GetApp()->EmpireID();
if (empire_id != ALL_EMPIRES && GetOptionsDB().Get<bool>("ui.map.scanlines.shown"))
fog_scanlines = true;
RenderScaleCircle();
if (!fog_scanlines && !circles) {
glPopClientAttrib();
return;
}
auto& known_destroyed_object_ids = universe.EmpireKnownDestroyedObjectIDs(empire_id);
// distance between inner and outer system circle
const double circle_distance = GetOptionsDB().Get<double>("ui.map.system.circle.distance");
// width of outer...
//const double outer_circle_width = GetOptionsDB().Get<double>("ui.map.system.circle.outer.width");
// ... and inner circle line at close zoom
const double inner_circle_width = GetOptionsDB().Get<double>("ui.map.system.circle.inner.width");
// width of inner circle line when map is zoomed out
const double max_inner_circle_width = GetOptionsDB().Get<double>("ui.map.system.circle.inner.max.width");
// thick
const float line_thick = std::max(std::min(2 / ZoomFactor(), max_inner_circle_width), inner_circle_width);
std::vector<std::pair<int, int>> colony_count_by_empire_id;
colony_count_by_empire_id.reserve(static_cast<std::size_t>(empires.NumEmpires()) + 1u);
auto increment_empire_colony_count = [&colony_count_by_empire_id](int col_empire_id) {
auto it = std::find_if(colony_count_by_empire_id.begin(), colony_count_by_empire_id.end(),
[col_empire_id](const auto& e) { return e.first == col_empire_id; });
if (it != colony_count_by_empire_id.end())
it->second++;
else
colony_count_by_empire_id.emplace_back(col_empire_id, 1);
};
std::vector<std::pair<int, GG::Clr>> empire_colours;
empire_colours.reserve(colony_count_by_empire_id.size());
std::transform(empires.begin(), empires.end(), std::back_inserter(empire_colours),
[](const auto& e) { return std::pair{e.first, e.second->Color()}; });
auto get_empire_colour = [&empire_colours,
neutral_colour{GetOptionsDB().Get<GG::Clr>("ui.map.starlane.color")}]
(int empire_id)
{
auto it = std::find_if(empire_colours.begin(), empire_colours.end(),
[empire_id](const auto& id_clr) { return empire_id == id_clr.first; });
if (it != empire_colours.end())
return it->second;
return neutral_colour;
};
m_scanline_circle_vertices.clear();
m_system_circle_vertices.clear();
m_system_circle_colours.clear();
m_scanline_circle_vertices.reserve(120*m_system_icons.size());
m_system_circle_vertices.reserve(120*m_system_icons.size());
m_system_circle_colours.reserve(120*m_system_icons.size());
for (const auto& [system_id, icon] : m_system_icons) {
GG::Pt icon_size = icon->LowerRight() - icon->UpperLeft();
GG::Pt icon_middle = icon->UpperLeft() + (icon_size / 2);
GG::Pt circle_size = GG::Pt(static_cast<GG::X>(icon->EnclosingCircleDiameter()),
static_cast<GG::Y>(icon->EnclosingCircleDiameter()));
GG::Pt circle_ul = icon_middle - (circle_size / 2);
GG::Pt circle_lr = circle_ul + circle_size;
// prep scanlines
if (fog_scanlines &&
empire_id != ALL_EMPIRES &&
universe.GetObjectVisibilityByEmpire(system_id, empire_id) <= Visibility::VIS_BASIC_VISIBILITY)
{
BufferStoreCircleArcVertices(m_scanline_circle_vertices, circle_ul, circle_lr,
0, TWO_PI, true, 24, false);
}
if (!circles)
continue;
auto* system = objects.getRaw<const System>(system_id);
if (!system || system->NumStarlanes() < 1)
continue;
// prep circles around systems that have at least one starlane, if they are enabled
const GG::Pt circle_distance_pt = GG::Pt(GG::X1, GG::Y1) * circle_distance;
const GG::Pt inner_circle_ul = circle_ul + (circle_distance_pt * ZoomFactor());
const GG::Pt inner_circle_lr = circle_lr - (circle_distance_pt * ZoomFactor());
bool has_empire_planet = false;
bool has_neutrals = false;
colony_count_by_empire_id.clear();
for (const auto* planet : objects.findRaw<const Planet>(system->PlanetIDs())) {
if (known_destroyed_object_ids.contains(planet->ID()))
continue;
// remember if this system has a player-owned planet, count # of colonies for each empire
if (!planet->Unowned()) {
has_empire_planet = true;
increment_empire_colony_count(planet->Owner());
}
// remember if this system has neutrals
if (planet->Unowned() && !planet->SpeciesName().empty() &&
planet->GetMeter(MeterType::METER_POPULATION)->Initial() > 0.0f)
{
has_neutrals = true;
increment_empire_colony_count(ALL_EMPIRES);
}
}
// outer circle in color of supplying empire
const int supply_empire_id = supply.EmpireThatCanSupplyAt(system_id);
const auto pre_sz1 = m_system_circle_vertices.size();
BufferStoreCircleArcVertices(m_system_circle_vertices, circle_ul, circle_lr,
0.0, TWO_PI, false, 0, false);
{
const std::size_t count1 = m_system_circle_vertices.size() - pre_sz1;
const auto clr_e = get_empire_colour(supply_empire_id);
for (std::size_t n = 0; n < count1; ++n)
m_system_circle_colours.store(clr_e);
}
// systems with neutrals and no empire have a segmented inner circle
if (has_neutrals && !has_empire_planet) {
static constexpr std::size_t segments = 24;
static constexpr double segment_arc = TWO_PI / segments;
const auto pre_sz2 = m_system_circle_vertices.size();
for (std::size_t n = 0; n < segments; n = n + 2) {
const auto theta1 = n * segment_arc;
const auto theta2 = (n+1) * segment_arc;
BufferStoreCircleArcVertices(m_system_circle_vertices, inner_circle_ul, inner_circle_lr,
theta1, theta2, false, 48, false);
}
const std::size_t count2 = m_system_circle_vertices.size() - pre_sz2;
const auto clr_txt = ClientUI::TextColor();
for (std::size_t n = 0; n < count2; ++n)
m_system_circle_colours.store(clr_txt);
}
// systems with empire planets have an unbroken inner circle,
// with different-color segments for each empire present
if (!has_empire_planet)
continue;
const int colonized_planets = std::transform_reduce(
colony_count_by_empire_id.begin(), colony_count_by_empire_id.end(),
0, std::plus<>(), [](const auto& e) { return e.second; });
const std::size_t segments = std::max(colonized_planets, 1);
const double segment_arc = TWO_PI / segments;
std::size_t n = 0;
for (const auto& [colony_empire_id, colony_count] : colony_count_by_empire_id) {
const auto pre_sz3 = m_system_circle_vertices.size();
const auto theta1 = n*segment_arc;
const auto theta2 = (n + colony_count)*segment_arc;
BufferStoreCircleArcVertices(m_system_circle_vertices, inner_circle_ul, inner_circle_lr,
theta1, theta2, false, 30, false);
const std::size_t count3 = m_system_circle_vertices.size() - pre_sz3;
n += colony_count;
const auto clr_e2 = (colony_empire_id == ALL_EMPIRES) ?
ClientUI::TextColor() : get_empire_colour(colony_empire_id);
for (std::size_t n2 = 0; n2 < count3; ++n2)
m_system_circle_colours.store(clr_e2);
}
}
glPushMatrix();
glLoadIdentity();
glDisable(GL_TEXTURE_2D);
glEnable(GL_LINE_SMOOTH);
//glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS); // already pushed above
glEnableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
// render scanlines
m_scanline_shader.SetColor(GetOptionsDB().Get<GG::Clr>("ui.map.system.scanlines.color"));
m_scanline_shader.StartUsing();
m_scanline_circle_vertices.activate();
glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(m_scanline_circle_vertices.size()));
m_scanline_shader.StopUsing();
// render system circles
glEnableClientState(GL_COLOR_ARRAY);
glLineWidth(line_thick);
m_system_circle_vertices.activate();
m_system_circle_colours.activate();
glDrawArrays(GL_LINES, 0, static_cast<GLsizei>(m_system_circle_vertices.size()));
glPopClientAttrib();
glDisable(GL_LINE_SMOOTH);
glEnable(GL_TEXTURE_2D);
glPopMatrix();
glLineWidth(1.0f);
}
void MapWnd::RenderStarlanes() {
// if lanes everywhere, don't render the lanes
if (GetGameRules().Get<bool>("RULE_STARLANES_EVERYWHERE"))
return;
bool coloured = GetOptionsDB().Get<bool>("ui.map.starlane.empire.color.shown");
float core_multiplier = static_cast<float>(GetOptionsDB().Get<double>("ui.map.starlane.thickness.factor"));
RenderStarlanes(m_RC_starlane_vertices, m_RC_starlane_colors, core_multiplier * ZoomFactor(), coloured, false);
RenderStarlanes(m_starlane_vertices, m_starlane_colors, 1.0, coloured, true);
}
void MapWnd::RenderStarlanes(GG::GL2DVertexBuffer& vertices, GG::GLRGBAColorBuffer& colours,
double thickness, bool coloured, bool do_base_render)
{
if (vertices.size() && (colours.size() || !coloured) && (coloured || do_base_render)) {
// render starlanes with vertex buffer (and possibly colour buffer)
const GG::Clr UNOWNED_LANE_COLOUR = GetOptionsDB().Get<GG::Clr>("ui.map.starlane.color");
glDisable(GL_TEXTURE_2D);
glEnable(GL_LINE_SMOOTH);
glLineWidth(static_cast<GLfloat>(thickness * GetOptionsDB().Get<double>("ui.map.starlane.thickness")));
glPushAttrib(GL_COLOR_BUFFER_BIT);
glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
glEnableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
if (coloured) {
glEnableClientState(GL_COLOR_ARRAY);
colours.activate();
} else {
glDisableClientState(GL_COLOR_ARRAY);
glColor(UNOWNED_LANE_COLOUR);
}
vertices.activate();
glDrawArrays(GL_LINES, 0, vertices.size());
glPopClientAttrib();
glPopAttrib();
glEnable(GL_TEXTURE_2D);
glDisable(GL_LINE_SMOOTH);
}
glLineWidth(1.0);
}
namespace {
auto MoveLineDotTexture()
{ return ClientUI::GetTexture(ClientUI::ArtDir() / "misc" / "move_line_dot.png"); }
}
void MapWnd::BufferAddMoveLineVertices(GG::GL2DVertexBuffer& dot_verts_buf,
GG::GLRGBAColorBuffer& dot_colours_buf,
GG::GLTexCoordBuffer& dot_star_texture_coords_buf,
float offset, float dot_size, int dot_spacing,
const MapWnd::MovementLineData& move_line,
GG::Clr colour_override) const
{
const float dot_half_sz = dot_size / 2.0f;
const auto colour = colour_override == GG::CLR_ZERO ? move_line.colour : colour_override;
std::vector<std::pair<int, int>> vert_screen_coords;
vert_screen_coords.reserve(move_line.vertices.size());
std::transform(move_line.vertices.begin(), move_line.vertices.end(),
std::back_inserter(vert_screen_coords),
[left{Value(ClientUpperLeft().x)},
top{Value(ClientUpperLeft().y)},
zoom{ZoomFactor()}] (const auto& vert)
{
int x = (vert.x * zoom) + left;
int y = (vert.y * zoom) + top;
return std::pair{x, y};
});
const auto vert_coord_end = vert_screen_coords.end();
for (auto vert_coord_it = vert_screen_coords.begin(); vert_coord_it != vert_coord_end;) {
// get next two vertices screen positions
const auto& [x1, y1] = *vert_coord_it;
++vert_coord_it;
if (vert_coord_it == vert_coord_end)
break;
const auto& [x2, y2] = *vert_coord_it;
// get unit vector along line connecting vertices
const float deltaX = x2 - x1;
const float deltaY = y2 - y1;
const float length = std::sqrt(deltaX*deltaX + deltaY*deltaY);
if (!std::isnormal(length)) // safety check
continue;
const float uVecX = deltaX / length;
const float uVecY = deltaY / length;
// increment along line, adding dots to buffers, until end of line segment is passed
while (offset < length) {
// don't know why the dot needs to be shifted half a dot size down/right and
// rendered 2 x dot size on each axis, but apparently it does...
// find position of dot from initial vertex position, offset length and unit vectors
const auto left = x1 + offset * uVecX + dot_half_sz;
const auto top = y1 + offset * uVecY + dot_half_sz;
dot_verts_buf.store(left - dot_size, top - dot_size);
dot_verts_buf.store(left - dot_size, top + dot_size);
dot_verts_buf.store(left + dot_size, top + dot_size);
dot_verts_buf.store(left + dot_size, top - dot_size);
// move offset to that for next dot
offset += dot_spacing;
dot_colours_buf.store(colour);
dot_colours_buf.store(colour);
dot_colours_buf.store(colour);
dot_colours_buf.store(colour);
dot_star_texture_coords_buf.store(0.0f, 0.0f);
dot_star_texture_coords_buf.store(0.0f, 1.0f);
dot_star_texture_coords_buf.store(1.0f, 1.0f);
dot_star_texture_coords_buf.store(1.0f, 0.0f);
}
offset -= length; // so next segment's dots meld smoothly into this segment's
}
}
void MapWnd::RenderFleetMovementLines() {
if (ZoomFactor() < ClientUI::TinyFleetButtonZoomThreshold())
return;
// determine animation shift for move lines
const int dot_spacing = GetOptionsDB().Get<int>("ui.map.fleet.supply.dot.spacing");
const float rate = static_cast<float>(GetOptionsDB().Get<double>("ui.map.fleet.supply.dot.rate"));
const int ticks = GG::GUI::GetGUI()->Ticks();
/* Updated each frame to shift rendered posistion of dots that are drawn to
* show fleet move lines. */
const float move_line_animation_shift = static_cast<int>(ticks * rate) % dot_spacing;
// texture for dots
const auto move_line_dot_texture = MoveLineDotTexture();
const float dot_size = Value(move_line_dot_texture->DefaultWidth());
// dots rendered same size for all zoom levels, so do positioning in screen
// space instead of universe space
glPushMatrix();
glLoadIdentity();
// render movement lines for all fleets
glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, move_line_dot_texture->OpenGLId());
const auto sz = (m_fleet_lines.size() + m_projected_fleet_lines.size()) * 4;
m_fleet_move_dot_vertices.clear();
m_fleet_move_dot_vertices.reserve(sz);
m_fleet_move_dot_colours.clear();
m_fleet_move_dot_colours.reserve(sz);
m_fleet_move_dot_star_texture_coords.clear();
m_fleet_move_dot_star_texture_coords.reserve(sz);
for (const auto& move_line : m_fleet_lines | range_values) {
if (move_line.vertices.empty() || move_line.vertices.size() % 2 == 1)
continue;
BufferAddMoveLineVertices(m_fleet_move_dot_vertices, m_fleet_move_dot_colours,
m_fleet_move_dot_star_texture_coords, move_line_animation_shift,
dot_size, dot_spacing, move_line);
}
m_projected_move_dots_start_index = m_fleet_move_dot_vertices.size();
for (const auto& proj_line : m_projected_fleet_lines | range_values) {
if (proj_line.vertices.empty() || proj_line.vertices.size() % 2 == 1)
continue;
BufferAddMoveLineVertices(m_fleet_move_dot_vertices, m_fleet_move_dot_colours,
m_fleet_move_dot_star_texture_coords, move_line_animation_shift,
dot_size, dot_spacing, proj_line, GG::CLR_WHITE);
}
// re-render selected fleets' movement lines in white
for (int fleet_id : m_selected_fleet_ids) {
auto line_it = m_fleet_lines.find(fleet_id);
if (line_it != m_fleet_lines.end()) {
const auto& move_line = line_it->second;
if (!move_line.vertices.empty() && move_line.vertices.size() % 2 == 0) {
BufferAddMoveLineVertices(m_fleet_move_dot_vertices, m_fleet_move_dot_colours,
m_fleet_move_dot_star_texture_coords,
move_line_animation_shift,
dot_size, dot_spacing, move_line,
GG::CLR_WHITE);
}
}
}
// after adding all dots to buffer, render general fleet move dots in one call
m_fleet_move_dot_vertices.activate();
m_fleet_move_dot_colours.activate();
m_fleet_move_dot_star_texture_coords.activate();
glDrawArrays(GL_QUADS, 0, m_projected_move_dots_start_index);
glDisableClientState(GL_COLOR_ARRAY);
// render move line ETA indicators for selected fleets
for (int fleet_id : m_selected_fleet_ids) {
auto line_it = m_fleet_lines.find(fleet_id);
if (line_it != m_fleet_lines.end())
RenderMovementLineETAIndicators(line_it->second);
}
// render projected move lines, starting from offset index
glBindTexture(GL_TEXTURE_2D, move_line_dot_texture->OpenGLId());
m_fleet_move_dot_vertices.activate();
m_fleet_move_dot_colours.activate();
m_fleet_move_dot_star_texture_coords.activate();
glDrawArrays(GL_QUADS, m_projected_move_dots_start_index,
m_fleet_move_dot_vertices.size() - m_projected_move_dots_start_index);
// render projected move line ETA indicators
for (const auto& eta_indicator : m_projected_fleet_lines)
RenderMovementLineETAIndicators(eta_indicator.second, GG::CLR_WHITE);
glPopClientAttrib();
glPopMatrix();
}
void MapWnd::RenderMovementLineETAIndicators(const MapWnd::MovementLineData& move_line, GG::Clr clr) {
const auto& vertices = move_line.vertices;
if (vertices.empty())
return; // nothing to draw.
static constexpr GG::Pt MARKER_HALF_SIZE_PT{GG::X{9}, GG::Y{9}};
const int MARKER_PTS = ClientUI::Pts();
auto font = ClientUI::GetBoldFont(MARKER_PTS);
auto flags = GG::FORMAT_CENTER | GG::FORMAT_VCENTER;
glPushMatrix();
glLoadIdentity();
static constexpr int flag_border = 5;
for (const auto& vert : vertices) {
if (!vert.show_eta)
continue;
// draw background disc in empire colour, or passed-in colour
GG::Pt marker_centre = ScreenCoordsFromUniversePosition(vert.x, vert.y);
GG::Pt ul = marker_centre - MARKER_HALF_SIZE_PT;
GG::Pt lr = marker_centre + MARKER_HALF_SIZE_PT;
glDisable(GL_TEXTURE_2D);
// segmented circle of wedges to indicate blockades
if (vert.flag_blockade) {
static constexpr float wedge = static_cast<float>(TWO_PI)/12.0f;
for (int n = 0; n < 12; n = n + 2) {
glColor(GG::CLR_BLACK);
CircleArc(ul + GG::Pt(-flag_border*GG::X1, -flag_border*GG::Y1), lr + GG::Pt(flag_border*GG::X1, flag_border*GG::Y1), n*wedge, (n+1)*wedge, true);
glColor(GG::CLR_RED);
CircleArc(ul + GG::Pt(-(flag_border)*GG::X1, -(flag_border)*GG::Y1), lr + GG::Pt((flag_border)*GG::X1, (flag_border)*GG::Y1), (n+1)*wedge, (n+2)*wedge, true);
}
} else if (vert.flag_supply_block) {
static constexpr float wedge = static_cast<float>(TWO_PI)/12.0f;
for (int n = 0; n < 12; n = n + 2) {
glColor(GG::CLR_BLACK);
CircleArc(ul + GG::Pt(-flag_border*GG::X1, -flag_border*GG::Y1), lr + GG::Pt(flag_border*GG::X1, flag_border*GG::Y1), n*wedge, (n+1)*wedge, true);
glColor(GG::CLR_YELLOW);
CircleArc(ul + GG::Pt(-(flag_border)*GG::X1, -(flag_border)*GG::Y1), lr + GG::Pt((flag_border)*GG::X1, (flag_border)*GG::Y1), (n+1)*wedge, (n+2)*wedge, true);
}
}
// empire-coloured central fill within wedged outer ring
glColor(clr == GG::CLR_ZERO ? move_line.colour : clr);
CircleArc(ul, lr, 0.0, TWO_PI, true);
glEnable(GL_TEXTURE_2D);
// render ETA number in white with black shadows
const std::string text = "<s>" + std::to_string(vert.eta) + "</s>"; // TODO: use to_chars and reused string?
GG::Font::RenderState rs{GG::CLR_WHITE};
// TODO cache the text_elements
const auto text_elements = font->ExpensiveParseFromTextToTextElements(text, flags);
const auto lines = font->DetermineLines(text, flags, lr.x - ul.x, text_elements);
font->RenderText(ul, lr, text, flags, lines, rs);
}
glPopMatrix();
}
namespace {
constexpr GG::Pt BORDER_INSET{GG::X1, GG::Y1};
// Reimplementation of the boost::hash_range function, embedding
// boost::hash_combine and using std::hash instead of boost::hash
struct hash_clr {
std::size_t operator()(const GG::Clr clr) const noexcept {
static constexpr std::hash<uint32_t> hasher;
return hasher(uint32_t(clr));
}
};
auto GetFleetFutureTurnDetectionRangeCircles(const ScriptingContext& context,
const std::set<int>& fleet_ids)
{
std::unordered_map<GG::Clr, std::vector<std::pair<GG::Pt, GG::Pt>>, hash_clr> retval;
for (const auto* fleet : context.ContextObjects().findRaw<Fleet>(fleet_ids)) {
float fleet_detection_range = 0.0f;
for (const auto* ship : context.ContextObjects().findRaw<Ship>(fleet->ShipIDs())) {
if (const Meter* detection_meter = ship->GetMeter(MeterType::METER_DETECTION))
fleet_detection_range = std::max(fleet_detection_range, detection_meter->Current());
}
// skip fleets with no detection range
if (fleet_detection_range <= 0.0f)
continue;
const int radius = static_cast<int>(fleet_detection_range);
const GG::Pt rad_pt{GG::X{radius}, GG::Y{radius}};
// get colour... empire, monster, or neutral
auto empire = context.GetEmpire(fleet->Owner());
const GG::Clr empire_colour = empire ? empire->Color() :
fleet->HasMonsters(context.ContextUniverse()) ? GG::CLR_RED : GG::CLR_WHITE;
// get all current and future positions of fleet
for (const auto& node : fleet->MovePath(false, context)) {
// only show detection circles at turn-end positions
if (!node.turn_end)
continue;
// if out of system detection not allowed, skip fleets not expected to be in systems
if (!GetGameRules().Get<bool>("RULE_EXTRASOLAR_SHIP_DETECTION") &&
node.object_id == INVALID_OBJECT_ID)
{ continue; }
GG::Pt circle_centre = GG::Pt{GG::X(node.x), GG::Y(node.y)};
retval[empire_colour].emplace_back(circle_centre - rad_pt, circle_centre + rad_pt);
}
}
return retval;
}
}
void MapWnd::RenderVisibilityRadii() {
if (!GetOptionsDB().Get<bool>("ui.map.detection.range.shown"))
return;
glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glPushAttrib(GL_ENABLE_BIT | GL_STENCIL_BUFFER_BIT);
glEnable(GL_STENCIL_TEST);
glEnable(GL_LINE_SMOOTH);
glDisable(GL_TEXTURE_2D);
glLineWidth(1.5f);
// render each colour's radii separately, so they can consistently blend
// when overlapping other colours, but be stenciled to avoid blending
// when overlapping within a colour
for (const auto& [radii_start_run, border_start_run] : m_radii_radii_vertices_indices_runs) {
glClear(GL_STENCIL_BUFFER_BIT);
glStencilOp(GL_INCR, GL_INCR, GL_INCR);
glStencilFunc(GL_EQUAL, 0x0, 0xff);
m_visibility_radii_vertices.activate();
m_visibility_radii_colors.activate();
glDrawArrays(GL_TRIANGLES, radii_start_run.first, radii_start_run.second);
glStencilFunc(GL_GREATER, 0x2, 0xff);
glStencilOp(GL_DECR, GL_KEEP, GL_KEEP);
m_visibility_radii_border_vertices.activate();
m_visibility_radii_border_colors.activate();
glDrawArrays(GL_LINES, border_start_run.first, border_start_run.second);
}
if (GetOptionsDB().Get<bool>("ui.map.detection.range.future.shown")) {
glDisable(GL_STENCIL_TEST);
// future position ranges for selected fleets
ScriptingContext context;
auto future_turn_circles = GetFleetFutureTurnDetectionRangeCircles(context, m_selected_fleet_ids);
GG::GL2DVertexBuffer verts;
verts.reserve(120);
GG::GLRGBAColorBuffer vert_colours;
vert_colours.reserve(120);
for (const auto& [circle_colour, ul_lrs] : future_turn_circles) {
// get empire colour and calculate brighter radii outline colour
for (const auto& [ul, lr] : ul_lrs) {
// store line segments for border lines of radii
verts.clear();
vert_colours.clear();
BufferStoreCircleArcVertices(verts, ul + BORDER_INSET, lr - BORDER_INSET,
0.0, TWO_PI, false, 72, false);
// store colours for line segments
vert_colours.store(verts.size(), circle_colour);
verts.activate();
vert_colours.activate();
glDrawArrays(GL_LINES, 0, verts.size());
}
}
}
glEnable(GL_TEXTURE_2D);
glLineWidth(1.0f);
glPopAttrib();
glPopClientAttrib();
}
void MapWnd::RenderScaleCircle() {
if (SidePanel::SystemID() == INVALID_OBJECT_ID)
return;
if (!GetOptionsDB().Get<bool>("ui.map.scale.legend.shown") || !GetOptionsDB().Get<bool>("ui.map.scale.circle.shown"))
return;
if (m_scale_circle_vertices.empty())
InitScaleCircleRenderingBuffer();
glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
glEnableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_LINE_SMOOTH);
glDisable(GL_TEXTURE_2D);
glLineWidth(2.0f);
const GG::Clr circle_colour = []() { auto retval = GG::CLR_WHITE; retval.a = 128; return retval; }();
glColor(circle_colour);
m_scale_circle_vertices.activate();
glDrawArrays(GL_LINE_STRIP, 0, m_scale_circle_vertices.size());
glEnable(GL_TEXTURE_2D);
glDisable(GL_LINE_SMOOTH);
glLineWidth(1.0f);
glPopClientAttrib();
}
void MapWnd::RegisterWindows() {
// TODO: move these wnds into a GG::Wnd and call parent_wnd->Show(false) to
// hide all windows instead of unregistering them all.
// Actually register these CUIWnds so that the Visible() ones are rendered.
if (GGHumanClientApp* app = GGHumanClientApp::GetApp()) {
app->Register(m_sitrep_panel);
app->Register(m_object_list_wnd);
app->Register(m_pedia_panel);
app->Register(m_side_panel);
app->Register(m_combat_report_wnd);
app->Register(m_moderator_wnd);
app->Register(m_government_wnd);
// message and player list wnds are managed by the HumanClientFSM
}
}
void MapWnd::RemoveWindows() {
// Hide windows by unregistering them which works regardless of their
// m_visible attribute.
if (GGHumanClientApp* app = GGHumanClientApp::GetApp()) {
app->Remove(m_sitrep_panel);
app->Remove(m_object_list_wnd);
app->Remove(m_pedia_panel);
app->Remove(m_side_panel);
app->Remove(m_combat_report_wnd);
app->Remove(m_moderator_wnd);
app->Remove(m_government_wnd);
// message and player list wnds are managed by the HumanClientFSM
}
}
void MapWnd::Pan(const GG::Pt delta) {
GG::Pt move_to_pt = ClientUpperLeft() + delta;
CorrectMapPosition(move_to_pt);
MoveTo(move_to_pt - GG::Pt(AppWidth(), AppHeight()));
}
bool MapWnd::PanX(GG::X x) {
Pan(GG::Pt(x, GG::Y0));
return true;
}
bool MapWnd::PanY(GG::Y y) {
Pan(GG::Pt(GG::X0, y));
return true;
}
void MapWnd::LButtonDown(GG::Pt pt, GG::Flags<GG::ModKey> mod_keys)
{ m_drag_offset = pt - ClientUpperLeft(); }
void MapWnd::LDrag(GG::Pt pt, GG::Pt move, GG::Flags<GG::ModKey> mod_keys) {
if (GetOptionsDB().Get<bool>("ui.map.lock"))
return;
GG::Pt move_to_pt = pt - m_drag_offset;
CorrectMapPosition(move_to_pt);
MoveTo(move_to_pt - GG::Pt(AppWidth(), AppHeight()));
m_dragged = true;
}
void MapWnd::LButtonUp(GG::Pt pt, GG::Flags<GG::ModKey> mod_keys) {
m_drag_offset = GG::Pt(-GG::X1, -GG::Y1);
m_dragged = false;
}
void MapWnd::LClick(GG::Pt pt, GG::Flags<GG::ModKey> mod_keys) {
m_drag_offset = GG::Pt(-GG::X1, -GG::Y1);
FleetUIManager& manager = FleetUIManager::GetFleetUIManager();
const auto fleet_wnd = manager.ActiveFleetWnd();
bool quick_close_wnds = GetOptionsDB().Get<bool>("ui.quickclose.enabled");
// if a fleet window is visible, hide it and deselect fleet; if not, hide sidepanel
if (!m_dragged && !m_in_production_view_mode && fleet_wnd && quick_close_wnds) {
manager.CloseAll();
} else if (!m_dragged && !m_in_production_view_mode) {
SelectSystem(INVALID_OBJECT_ID);
m_side_panel->Hide();
}
m_dragged = false;
}
void MapWnd::RClick(GG::Pt pt, GG::Flags<GG::ModKey> mod_keys) {
// if in moderator mode, treat as moderator action click
if (ClientPlayerIsModerator()) {
// only supported action on empty map location at present is creating a system
if (m_moderator_wnd->SelectedAction() == ModeratorActionSetting::MAS_CreateSystem) {
ClientNetworking& net = GGHumanClientApp::GetApp()->Networking();
auto u_pos = this->UniversePositionFromScreenCoords(pt);
StarType star_type = m_moderator_wnd->SelectedStarType();
net.SendMessage(ModeratorActionMessage(
Moderator::CreateSystem(u_pos.first, u_pos.second, star_type)));
return;
}
}
if (GetOptionsDB().Get<bool>("ui.map.menu.enabled")) {
// create popup menu with map options in it.
bool fps = GetOptionsDB().Get<bool>("video.fps.shown");
bool showPlanets = GetOptionsDB().Get<bool>("ui.map.sidepanel.planet.shown");
bool systemCircles = GetOptionsDB().Get<bool>("ui.map.system.circle.shown");
bool resourceColor = GetOptionsDB().Get<bool>("ui.map.starlane.empire.color.shown");
bool fleetSupply = GetOptionsDB().Get<bool>("ui.map.fleet.supply.shown");
bool gas = GetOptionsDB().Get<bool>("ui.map.background.gas.shown");
bool starfields = GetOptionsDB().Get<bool>("ui.map.background.starfields.shown");
bool scale = GetOptionsDB().Get<bool>("ui.map.scale.legend.shown");
bool scaleCircle = GetOptionsDB().Get<bool>("ui.map.scale.circle.shown");
bool zoomSlider = GetOptionsDB().Get<bool>("ui.map.zoom.slider.shown");
bool detectionRange = GetOptionsDB().Get<bool>("ui.map.detection.range.shown");
auto show_fps_action = [&fps]() { GetOptionsDB().Set<bool>("video.fps.shown", !fps); };
auto show_planets_action = [&showPlanets]() { GetOptionsDB().Set<bool>("ui.map.sidepanel.planet.shown", !showPlanets); };
auto system_circles_action = [&systemCircles]() { GetOptionsDB().Set<bool>("ui.map.system.circle.shown", !systemCircles); };
auto resource_color_action = [&resourceColor]() { GetOptionsDB().Set<bool>("ui.map.starlane.empire.color.shown", !resourceColor); };
auto fleet_supply_action = [&fleetSupply]() { GetOptionsDB().Set<bool>("ui.map.fleet.supply.shown", !fleetSupply); };
auto gas_action = [&gas]() { GetOptionsDB().Set<bool>("ui.map.background.gas.shown", !gas); };
auto starfield_action = [&starfields]() { GetOptionsDB().Set<bool>("ui.map.background.starfields.shown", !starfields); };
auto map_scale_action = [&scale]() { GetOptionsDB().Set<bool>("ui.map.scale.legend.shown", !scale); };
auto scale_circle_action = [&scaleCircle]() { GetOptionsDB().Set<bool>("ui.map.scale.circle.shown", !scaleCircle); };
auto zoom_slider_action = [&zoomSlider]() { GetOptionsDB().Set<bool>("ui.map.zoom.slider.shown", !zoomSlider); };
auto detection_range_action = [&detectionRange]() { GetOptionsDB().Set<bool>("ui.map.detection.range.shown", !detectionRange); };
auto popup = GG::Wnd::Create<CUIPopupMenu>(pt.x, pt.y);
popup->AddMenuItem(GG::MenuItem(UserString("OPTIONS_SHOW_FPS"), false, fps, show_fps_action));
popup->AddMenuItem(GG::MenuItem(UserString("OPTIONS_SHOW_SIDEPANEL_PLANETS"), false, showPlanets, show_planets_action));
popup->AddMenuItem(GG::MenuItem(UserString("OPTIONS_UI_SYSTEM_CIRCLES"), false, systemCircles, system_circles_action));
popup->AddMenuItem(GG::MenuItem(UserString("OPTIONS_RESOURCE_STARLANE_COLOURING"), false, resourceColor, resource_color_action));
popup->AddMenuItem(GG::MenuItem(UserString("OPTIONS_FLEET_SUPPLY_LINES"), false, fleetSupply, fleet_supply_action));
popup->AddMenuItem(GG::MenuItem(UserString("OPTIONS_GALAXY_MAP_GAS"), false, gas, gas_action));
popup->AddMenuItem(GG::MenuItem(UserString("OPTIONS_GALAXY_MAP_STARFIELDS"), false, starfields, starfield_action));
popup->AddMenuItem(GG::MenuItem(UserString("OPTIONS_GALAXY_MAP_SCALE_LINE"), false, scale, map_scale_action));
popup->AddMenuItem(GG::MenuItem(UserString("OPTIONS_GALAXY_MAP_SCALE_CIRCLE"), false, scaleCircle, scale_circle_action));
popup->AddMenuItem(GG::MenuItem(UserString("OPTIONS_GALAXY_MAP_ZOOM_SLIDER"), false, zoomSlider, zoom_slider_action));
popup->AddMenuItem(GG::MenuItem(UserString("OPTIONS_GALAXY_MAP_DETECTION_RANGE"), false, detectionRange, detection_range_action));
// display popup menu
popup->Run();
}
}
void MapWnd::MouseWheel(GG::Pt pt, int move, GG::Flags<GG::ModKey> mod_keys) {
if (move)
Zoom(move, pt);
}
void MapWnd::KeyPress(GG::Key key, uint32_t key_code_point, GG::Flags<GG::ModKey> mod_keys) {
if (key == GG::Key::GGK_LSHIFT || key == GG::Key::GGK_RSHIFT)
ReplotProjectedFleetMovement(mod_keys & GG::MOD_KEY_SHIFT);
}
void MapWnd::KeyRelease(GG::Key key, uint32_t key_code_point, GG::Flags<GG::ModKey> mod_keys) {
if (key == GG::Key::GGK_LSHIFT || key == GG::Key::GGK_RSHIFT)
ReplotProjectedFleetMovement(mod_keys & GG::MOD_KEY_SHIFT);
}
void MapWnd::EnableOrderIssuing(bool enable) {
// disallow order enabling if this client does not have an empire
// and is not a moderator
const auto* app = GGHumanClientApp::GetApp();
bool moderator = false;
bool observer = false;
m_btn_turn->Disable(GGHumanClientApp::GetApp()->SinglePlayerGame() && !enable);
if (!app) {
enable = false;
m_btn_turn->Disable(true);
} else {
bool have_empire = (app->EmpireID() != ALL_EMPIRES);
moderator = (app->GetClientType() == Networking::ClientType::CLIENT_TYPE_HUMAN_MODERATOR);
if (!have_empire && !moderator) {
enable = false;
m_btn_turn->Disable(true);
}
observer = (app->GetClientType() == Networking::ClientType::CLIENT_TYPE_HUMAN_OBSERVER);
}
m_moderator_wnd->EnableActions(enable && moderator);
m_ready_turn = !enable;
const auto& button_label = (!app) ? UserString("ERROR") :
(!moderator && !observer && m_ready_turn && !app->SinglePlayerGame()) ?
UserString("MAP_BTN_TURN_UNREADY") : UserString("MAP_BTN_TURN_UPDATE");
m_btn_turn->SetText(boost::io::str(FlexibleFormat(button_label) %
std::to_string(app ? app->CurrentTurn() : 0)));
RefreshTurnButtonTooltip();
m_side_panel->EnableOrderIssuing(enable);
m_production_wnd->EnableOrderIssuing(enable);
m_research_wnd->EnableOrderIssuing(enable);
m_design_wnd->EnableOrderIssuing(enable);
m_government_wnd->EnableOrderIssuing(enable);
FleetUIManager::GetFleetUIManager().EnableOrderIssuing(enable);
}
void MapWnd::InitTurn(ScriptingContext& context) {
DebugLogger() << "Initializing turn " << context.current_turn;
SectionedScopedTimer timer("MapWnd::InitTurn");
timer.EnterSection("init");
//DebugLogger() << GetSupplyManager().Dump();
Universe& universe = context.ContextUniverse();
ObjectMap& objects = context.ContextObjects();
TraceLogger(effects) << "MapWnd::InitTurn initial:";
for (auto obj : objects.allRaw())
TraceLogger(effects) << obj->Dump();
timer.EnterSection("meter estimates");
// update effect accounting and meter estimates
universe.InitMeterEstimatesAndDiscrepancies(context);
timer.EnterSection("orders");
// if we've just loaded the game there may be some unexecuted orders, we
// should reapply them now, so they are reflected in the UI, but do not
// influence current meters or their discrepancies for this turn
GGHumanClientApp::GetApp()->Orders().ApplyOrders(context);
timer.EnterSection("meter estimates");
universe.UpdateMeterEstimates(context);
universe.ApplyAppearanceEffects(context);
timer.EnterSection("init rendering");
// set up system icons, starlanes, galaxy gas rendering
InitTurnRendering();
timer.EnterSection("fleet signals");
// connect system fleet add and remove signals
for (auto system : objects.allRaw<System>()) {
m_system_fleet_insert_remove_signals[system->ID()].emplace_back(system->FleetsInsertedSignal.connect(
[this](std::vector<int>&& fleet_ids, const ObjectMap& objects) {
RefreshFleetButtons(true);
for (const auto* fleet : objects.findRaw<Fleet>(std::move(fleet_ids))) {
m_fleet_state_change_signals.try_emplace(
fleet->ID(),
fleet->StateChangedSignal.connect(boost::bind(&MapWnd::RefreshFleetButtons, this, true)));
}
}));
m_system_fleet_insert_remove_signals[system->ID()].emplace_back(system->FleetsRemovedSignal.connect(
[this](std::vector<int>&& fleets) {
RefreshFleetButtons(true);
for (const auto fleet_id : fleets)
m_fleet_state_change_signals.erase(fleet_id);
}));
}
m_fleet_state_change_signals.clear(); // should disconnect scoped signals
// connect fleet change signals to update fleet movement lines, so that ordering
// fleets to move updates their displayed path and rearranges fleet buttons (if necessary)
for (const auto fleet : objects.allRaw<Fleet>()) {
m_fleet_state_change_signals[fleet->ID()] = fleet->StateChangedSignal.connect(
boost::bind(&MapWnd::RefreshFleetButtons, this, true));
}
// set turn button to current turn
m_btn_turn->SetText(boost::io::str(FlexibleFormat(UserString("MAP_BTN_TURN_UPDATE")) %
std::to_string(context.current_turn)));
RefreshTurnButtonTooltip();
m_ready_turn = false;
MoveChildUp(m_btn_turn);
timer.EnterSection("sitreps");
// are there any sitreps to show?
bool show_intro_sitreps = context.current_turn == 1 &&
GetOptionsDB().Get<Aggression>("setup.ai.aggression") <= Aggression::TYPICAL;
DebugLogger() << "showing intro sitreps : " << show_intro_sitreps;
if (show_intro_sitreps || m_sitrep_panel->NumVisibleSitrepsThisTurn() > 0) {
m_sitrep_panel->ShowSitRepsForTurn(context.current_turn);
if (!m_design_wnd->Visible() && !m_research_wnd->Visible()
&& !m_production_wnd->Visible())
{ ShowSitRep(); }
}
if (m_sitrep_panel->Visible()) {
// Ensure that the panel is at least updated if it's visible because it
// can now set itself to be visible (from the config) before a game is
// loaded, and it can be visible while the production/research/design
// windows are open.
m_sitrep_panel->Update();
}
m_combat_report_wnd->Hide();
if (m_object_list_wnd->Visible())
m_object_list_wnd->Refresh();
m_moderator_wnd->Refresh();
m_pedia_panel->Refresh();
// show or hide system names, depending on zoom. replicates code in MapWnd::Zoom
if (ZoomFactor() * ClientUI::Pts() < MIN_SYSTEM_NAME_SIZE)
HideSystemNames();
else
ShowSystemNames();
// empire is recreated each turn based on turn update from server, so
// connections of signals emitted from the empire must be remade each turn
// (unlike connections to signals from the sidepanel)
auto this_client_empire = context.GetEmpire(GGHumanClientApp::GetApp()->EmpireID());
if (this_client_empire) {
this_client_empire->GetInfluencePool().ChangedSignal.connect(
boost::bind(&MapWnd::RefreshInfluenceResourceIndicator, this));
this_client_empire->GetResearchPool().ChangedSignal.connect(
boost::bind(&MapWnd::RefreshResearchResourceIndicator, this));
this_client_empire->GetIndustryPool().ChangedSignal.connect(
boost::bind(&MapWnd::RefreshIndustryResourceIndicator, this));
this_client_empire->GetPopulationPool().ChangedSignal.connect(
boost::bind(&MapWnd::RefreshPopulationIndicator, this));
this_client_empire->GetProductionQueue().ProductionQueueChangedSignal.connect(
boost::bind(&MapWnd::RefreshIndustryResourceIndicator, this));
// so lane colouring to indicate wasted PP is updated
this_client_empire->GetProductionQueue().ProductionQueueChangedSignal.connect(
boost::bind(&MapWnd::InitStarlaneRenderingBuffers, this));
this_client_empire->GetResearchQueue().ResearchQueueChangedSignal.connect(
boost::bind(&MapWnd::RefreshResearchResourceIndicator, this));
}
m_toolbar->Show();
m_FPS->Show();
m_scale_line->Show();
RefreshSliders();
timer.EnterSection("update resource pools");
for (auto& empire : context.Empires() | range_values) {
empire->UpdateResourcePools(context,
empire->TechCostsTimes(context),
empire->PlanetAnnexationCosts(context),
empire->PolicyAdoptionCosts(context),
empire->ProductionCostsTimes(context));
}
timer.EnterSection("refresh government");
m_government_wnd->Refresh();
timer.EnterSection("refresh research");
m_research_wnd->Refresh(context);
timer.EnterSection("refresh sidepanel");
SidePanel::Refresh(); // recreate contents of all SidePanels. ensures previous turn's objects and signals are disposed of
timer.EnterSection("refresh production wnd");
m_production_wnd->Refresh(context);
if (context.current_turn == 1 && this_client_empire) {
// start first turn with player's system selected
if (const auto obj = objects.getRaw(this_client_empire->CapitalID())) {
SelectSystem(obj->SystemID());
CenterOnMapCoord(obj->X(), obj->Y());
}
// default the tech tree to be centred on something interesting
m_research_wnd->Reset(context);
} else if (context.current_turn == 1 && !this_client_empire) {
CenterOnMapCoord(0.0, 0.0);
}
timer.EnterSection("refresh indicators");
RefreshIndustryResourceIndicator();
RefreshResearchResourceIndicator();
RefreshInfluenceResourceIndicator();
RefreshFleetResourceIndicator();
RefreshPopulationIndicator();
RefreshDetectionIndicator();
timer.EnterSection("dispatch exploring");
FleetUIManager::GetFleetUIManager().RefreshAll();
DispatchFleetsExploring();
timer.EnterSection("enable observers");
GGHumanClientApp* app = GGHumanClientApp::GetApp();
if (app->GetClientType() == Networking::ClientType::CLIENT_TYPE_HUMAN_MODERATOR) {
// this client is a moderator
m_btn_moderator->Disable(false);
m_btn_moderator->Show();
} else {
HideModeratorActions();
m_btn_moderator->Disable();
m_btn_moderator->Hide();
}
if (app->GetClientType() == Networking::ClientType::CLIENT_TYPE_HUMAN_OBSERVER) {
m_btn_auto_turn->Disable();
m_btn_auto_turn->Hide();
} else {
m_btn_auto_turn->Disable(false);
m_btn_auto_turn->Show();
}
if (GetOptionsDB().Get<bool>("ui.turn.start.sound.enabled"))
Sound::GetSound().PlaySound(GetOptionsDB().Get<std::string>("ui.turn.start.sound.path"), true);
}
void MapWnd::MidTurnUpdate() {
DebugLogger() << "MapWnd::MidTurnUpdate";
ScopedTimer timer("MapWnd::MidTurnUpdate");
GetUniverse().InitializeSystemGraph(Empires(), Objects());
GetUniverse().UpdateEmpireVisibilityFilteredSystemGraphsWithMainObjectMap(Empires());
// set up system icons, starlanes, galaxy gas rendering
InitTurnRendering();
FleetUIManager::GetFleetUIManager().RefreshAll();
SidePanel::Refresh();
// show or hide system names, depending on zoom. replicates code in MapWnd::Zoom
if (ZoomFactor() * ClientUI::Pts() < MIN_SYSTEM_NAME_SIZE)
HideSystemNames();
else
ShowSystemNames();
}
void MapWnd::InitTurnRendering() {
DebugLogger() << "MapWnd::InitTurnRendering";
ScopedTimer timer("MapWnd::InitTurnRendering");
using boost::placeholders::_1;
using boost::placeholders::_2;
// adjust size of map window for universe and application size
Resize(GG::Pt(static_cast<GG::X>(GetUniverse().UniverseWidth() * ZOOM_MAX + AppWidth() * 1.5),
static_cast<GG::Y>(GetUniverse().UniverseWidth() * ZOOM_MAX + AppHeight() * 1.5)));
// remove any existing fleet movement lines or projected movement lines. this gets cleared
// here instead of with the movement line stuff because that would clear some movement lines
// that come from the SystemIcons
m_fleet_lines.clear();
ClearProjectedFleetMovementLines();
int client_empire_id = GGHumanClientApp::GetApp()->EmpireID();
const Universe& universe = GetUniverse();
const auto& this_client_known_destroyed_objects = universe.EmpireKnownDestroyedObjectIDs(client_empire_id);
const auto& this_client_stale_object_info = universe.EmpireStaleKnowledgeObjectIDs(client_empire_id);
const ObjectMap& objects = universe.Objects();
// remove old system icons
for (const auto& system_icon : m_system_icons)
DetachChild(system_icon.second);
m_system_icons.clear();
// create system icons
for (auto* sys : objects.allRaw<System>()) {
const int sys_id = sys->ID();
// skip known destroyed objects
if (this_client_known_destroyed_objects.contains(sys_id))
continue;
// create new system icon
auto icon = GG::Wnd::Create<SystemIcon>(GG::X0, GG::Y0, GG::X(10), sys_id);
m_system_icons[sys_id] = icon;
icon->InstallEventFilter(shared_from_this());
if (SidePanel::SystemID() == sys_id)
icon->SetSelected(true);
AttachChild(icon);
// connect UI response signals. TODO: Make these configurable in GUI?
icon->LeftClickedSignal.connect(boost::bind(&MapWnd::SystemLeftClicked, this, _1));
icon->RightClickedSignal.connect(boost::bind(
static_cast<void (MapWnd::*)(int, GG::Flags<GG::ModKey>)>(&MapWnd::SystemRightClicked), this, _1, _2));
icon->LeftDoubleClickedSignal.connect(boost::bind(&MapWnd::SystemDoubleClicked, this, _1));
icon->MouseEnteringSignal.connect(boost::bind(&MapWnd::MouseEnteringSystem, this, _1, _2));
icon->MouseLeavingSignal.connect(boost::bind(&MapWnd::MouseLeavingSystem, this, _1));
}
// temp: reset starfield each turn
m_starfield_verts.clear();
m_starfield_colours.clear();
// end temp
// create buffers for system icon and galaxy gas rendering, and starlane rendering
InitSystemRenderingBuffers();
InitStarlaneRenderingBuffers();
// position system icons
DoSystemIconsLayout();
// remove old field icons
for (const auto& field_icon : m_field_icons)
DetachChild(field_icon);
m_field_icons.clear();
// create field icons
std::vector<std::pair<int, float>> field_ids_by_size;
for (auto* field : objects.allRaw<Field>()) {
field_ids_by_size.emplace_back(field->ID(), field->GetMeter(MeterType::METER_SIZE)->Initial());
}
std::sort(field_ids_by_size.begin(), field_ids_by_size.end(), [](const auto& lhs, const auto& rhs) { return lhs.second < rhs.second; });
for (const auto& [fld_id, field_size] : field_ids_by_size) {
// skip known destroyed and stale fields
if (this_client_known_destroyed_objects.contains(fld_id))
continue;
if (this_client_stale_object_info.contains(fld_id))
continue;
// don't skip not visible but not stale fields; still expect these to be where last seen, or near there
//if (field->GetVisibility(client_empire_id, universe) <= Visibility::VIS_NO_VISIBILITY)
// continue;
// create new system icon
auto icon = GG::Wnd::Create<FieldIcon>(fld_id);
m_field_icons.push_back(icon);
icon->InstallEventFilter(shared_from_this());
AttachChild(icon);
icon->RightClickedSignal.connect(boost::bind(
static_cast<void (MapWnd::*)(int)>(&MapWnd::FieldRightClicked), this, _1));
}
// position field icons
DoFieldIconsLayout();
InitFieldRenderingBuffers();
InitVisibilityRadiiRenderingBuffers();
// create fleet buttons and move lines. needs to be after InitStarlaneRenderingBuffers so that m_starlane_endpoints is populated
RefreshFleetButtons(true);
// move field icons to bottom of child stack so that other icons can be moused over with a field
for (const auto& field_icon : m_field_icons)
MoveChildDown(field_icon);
}
void MapWnd::InitSystemRenderingBuffers() {
DebugLogger() << "MapWnd::InitSystemRenderingBuffers";
ScopedTimer timer("MapWnd::InitSystemRenderingBuffers");
// clear out all the old buffers
ClearSystemRenderingBuffers();
// Generate texture coordinates to be used for subsequent vertex buffer creation.
// Note these coordinates assume the texture is twice as large as it should
// be. This allows us to use one set of texture coords for everything, even
// though the star-halo textures must be rendered at sizes as much as twice
// as large as the star-disc textures.
static constexpr std::array<GLfloat, 4> tex_coords{-0.5, -0.5, 1.5, 1.5};
for (std::size_t i = 0; i < m_system_icons.size(); ++i)
GG::Texture::InitBuffer(m_star_texture_coords, tex_coords);
for (const auto& system_icon : m_system_icons) {
const auto& icon = system_icon.second;
int system_id = system_icon.first;
auto* system = Objects().getRaw<const System>(system_id);
if (!system) {
ErrorLogger() << "MapWnd::InitSystemRenderingBuffers couldn't get system with id " << system_id;
continue;
}
// Add disc and halo textures for system icon
// See note above texture coords for why we're making coordinate sets that are 2x too big.
double icon_size = ClientUI::SystemIconSize();
float icon_ul_x = static_cast<float>(system->X() - icon_size);
float icon_ul_y = static_cast<float>(system->Y() - icon_size);
float icon_lr_x = static_cast<float>(system->X() + icon_size);
float icon_lr_y = static_cast<float>(system->Y() + icon_size);
if (icon->DiscTexture()) {
auto& core_vertices = m_star_core_quad_vertices[icon->DiscTexture()];
GG::Texture::InitBuffer(core_vertices, icon_ul_x, icon_ul_y, icon_lr_x, icon_lr_y);
}
if (icon->HaloTexture()) {
auto& halo_vertices = m_star_halo_quad_vertices[icon->HaloTexture()];
GG::Texture::InitBuffer(halo_vertices, icon_ul_x, icon_ul_y, icon_lr_x, icon_lr_y);
}
// add (rotated) gaseous substance around system
if (auto gas_texture = GetGasTexture()) {
const float GAS_SIZE = ClientUI::SystemIconSize() * 6.0;
const float ROTATION = system_id * 27.0; // arbitrary rotation in radians ("27.0" is just a number that produces pleasing results)
const float COS_THETA = std::cos(ROTATION);
const float SIN_THETA = std::sin(ROTATION);
// Components of corner points of a quad
const float X1 = GAS_SIZE, Y1 = GAS_SIZE; // upper right corner (X1, Y1)
const float X2 = -GAS_SIZE, Y2 = GAS_SIZE; // upper left corner (X2, Y2)
const float X3 = -GAS_SIZE, Y3 = -GAS_SIZE; // lower left corner (X3, Y3)
const float X4 = GAS_SIZE, Y4 = -GAS_SIZE; // lower right corner (X4, Y4)
// Calculate rotated corner point components after CCW ROTATION radians around origin.
const float X1r = COS_THETA*X1 + SIN_THETA*Y1;
const float Y1r = -SIN_THETA*X1 + COS_THETA*Y1;
const float X2r = COS_THETA*X2 + SIN_THETA*Y2;
const float Y2r = -SIN_THETA*X2 + COS_THETA*Y2;
const float X3r = COS_THETA*X3 + SIN_THETA*Y3;
const float Y3r = -SIN_THETA*X3 + COS_THETA*Y3;
const float X4r = COS_THETA*X4 + SIN_THETA*Y4;
const float Y4r = -SIN_THETA*X4 + COS_THETA*Y4;
// See note above texture coords for why we're making coordinate sets that are 2x too big.
// add to system position to get translated scaled rotated quad corner
const float GAS_X1 = system->X() + X1r;
const float GAS_Y1 = system->Y() + Y1r;
const float GAS_X2 = system->X() + X2r;
const float GAS_Y2 = system->Y() + Y2r;
const float GAS_X3 = system->X() + X3r;
const float GAS_Y3 = system->Y() + Y3r;
const float GAS_X4 = system->X() + X4r;
const float GAS_Y4 = system->Y() + Y4r;
m_galaxy_gas_quad_vertices.store(GAS_X1,GAS_Y1); // rotated upper right
m_galaxy_gas_quad_vertices.store(GAS_X2,GAS_Y2); // rotated upper left
m_galaxy_gas_quad_vertices.store(GAS_X3,GAS_Y3); // rotated lower left
m_galaxy_gas_quad_vertices.store(GAS_X4,GAS_Y4); // rotated lower right
unsigned int subtexture_index = system_id % 12; // 0 1 2 3 4 5 6 7 8 9 10 11
unsigned int subtexture_x_index = subtexture_index / 3; // 0 0 0 0 1 1 1 1 2 2 2 2
unsigned int subtexture_y_index = subtexture_index - 4*subtexture_x_index; // 0 1 2 3 0 1 2 3 0 1 2 3
const auto [tex_coord_min_x, tex_coord_min_y, tex_coord_max_x, tex_coord_max_y] =
gas_texture->DefaultTexCoords();
// gas texture is expected to be a 4 wide by 3 high grid
// also add a bit of padding to hopefully avoid artifacts of texture edges
const GLfloat tx_low_x = tex_coord_min_x + (subtexture_x_index + 0)*(tex_coord_max_x - tex_coord_min_x)/4;
const GLfloat tx_high_x = tex_coord_min_x + (subtexture_x_index + 1)*(tex_coord_max_x - tex_coord_min_x)/4;
const GLfloat tx_low_y = tex_coord_min_y + (subtexture_y_index + 0)*(tex_coord_max_y - tex_coord_min_y)/3;
const GLfloat tx_high_y = tex_coord_min_y + (subtexture_y_index + 1)*(tex_coord_max_y - tex_coord_min_y)/3;
const std::array<GLfloat, 4> rot_tex_coords{tx_low_x, tx_low_y, tx_high_x, tx_high_y};
GG::Texture::InitBuffer(m_galaxy_gas_texture_coords, rot_tex_coords);
}
}
// create new buffers
// star cores
for (auto& star_core_buffer : m_star_core_quad_vertices) {
glBindTexture(GL_TEXTURE_2D, star_core_buffer.first->OpenGLId());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
star_core_buffer.second.createServerBuffer();
}
// star halos
for (auto& star_halo_buffer : m_star_halo_quad_vertices) {
glBindTexture(GL_TEXTURE_2D, star_halo_buffer.first->OpenGLId());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
star_halo_buffer.second.createServerBuffer();
}
m_star_texture_coords.createServerBuffer();
m_galaxy_gas_quad_vertices.createServerBuffer();
m_galaxy_gas_texture_coords.createServerBuffer();
}
void MapWnd::ClearSystemRenderingBuffers() {
m_star_core_quad_vertices.clear();
m_star_halo_quad_vertices.clear();
m_galaxy_gas_quad_vertices.clear();
m_galaxy_gas_texture_coords.clear();
m_star_texture_coords.clear();
m_star_circle_vertices.clear();
}
namespace GetPathsThroughSupplyLanes {
// SupplyLaneMap map keyed by system containing all systems
// corresponding to valid supply lane destinations
typedef std::unordered_multimap<int,int> SupplyLaneMMap;
// PathInfo stores the \p ids of systems one hop back on a path
// toward an \p o originating terminal system.
struct PathInfo {
PathInfo(int a, int o) : one_hop_back(1, a), single_origin(o) {}
PathInfo(int o) : one_hop_back(), single_origin(o) {}
// system(s) one hop back on the path.
// The terminal point has no preceding system.
// Merged paths are indicated with multiple preceding systems.
std::vector<int> one_hop_back;
// The originating terminal point.
// If single origin is boost::none then two paths with at least
// two different terminal points merged.
boost::optional<int> single_origin;
};
struct PrevCurrInfo {
PrevCurrInfo(int p, int n, int o) : prev(p), curr(n), origin(o) {}
int prev, curr, origin;
};
/**
GetPathsThroughSupplyLanes starts with:
\p terminal_points are system ids of systems that contain either
a resource source or a resource sink.
\p supply_lanes are pairs of system ids at the end of supply
lanes.
GetPathsThroughSupplyLanes returns a good path.
The \p good_path is all system ids of systems connecting any \p
terminal_point to any other reachable \p terminal_point along the
\p supply_lanes. The \p good_path is all systems on a path that
could transport a resource from a source to a sink along a
starlane that is part of the starlanes through which supply can
flow (See Empire/Supply.h for details.). The \p good_path
includes the terminal point system ids that are part of the
path. The \p good_path will exclude terminal_points not
connected to a supply lane, islands of supply lane not connected
to at least two terminal points, and dead-end lengths of supply
lane that don't connect between two terminal points.
Algorithm Descrition:
The algorithm starts with terminal points and supply lanes. It
finds all paths from any terminal point to any other terminal
point connected only by supply lanes.
The algorithm finds and returns all system ids on the \p
good_path in two steps:
1) find mid points on paths along supply lanes between terminal points,
2) return the system ids collected by tracing the paths from
mid points to terminal points of the found paths.
In the first part, it starts a breadth first search from every
terminal point at once. It tracks which terminal point each path
started from.
When two paths from different terminal points meet it records
both points that met as mid points on a good path between
terminal points.
When two paths meet from the same terminal points it merges them
into one path.
In the second part, it starts from the mid points and works its
way back to the terminal points, recording every system along the
path as part of the good path. It stops when it reaches a system
already in the good path.
The algorithm is fast because neither the first nor the second
part visits any system more than once.
The first part uses visited to track already visited systems.
The second part stops back tracking along paths when it reaches
systems already on the good path.
*/
boost::container::flat_set<int> GetPathsThroughSupplyLanes(
const std::unordered_set<int> & terminal_points, const SupplyLaneMMap& supply_lanes)
{
boost::container::flat_set<int> good_path;
// No terminal points, so all paths lead nowhere.
if (terminal_points.empty())
return good_path;
// Part One: Find all reachable mid points between two different
// terminal points.
// try_next holds systems reached in the breadth first search
// that have not had the supply lanes leaving them explored.
std::deque<PrevCurrInfo> try_next;
// visited holds systems already reached by the breadth first search.
boost::unordered_map<int, PathInfo> visited;
// reachable_midpoints holds all systems reachable from at least
// two different terminal points.
std::vector<int> reachable_midpoints;
// Initialize with all the terminal points, by adding all
// terminal points to the queue, and to visited.
for (int terminal_point : terminal_points) {
try_next.emplace_back(terminal_point, terminal_point, terminal_point);
visited.emplace(terminal_point, PathInfo(terminal_point));
}
// Find all reachable midpoints where paths from two different
// terminal points meet.
while (!try_next.empty() ) {
// Try the next system from the queue.
const PrevCurrInfo& curr = try_next.front();
// Check each supply lane that exits this sytem.
auto supplylane_endpoints = supply_lanes.equal_range(curr.curr);
for (auto sup_it = supplylane_endpoints.first;
sup_it != supplylane_endpoints.second; ++sup_it)
{
int next = sup_it->second;
// Skip the system if it back tracks.
if (next == curr.prev)
continue;
auto previous = visited.find(next);
// next has no previous so it is an unvisited
// system. Create a new previous from curr->next with
// the same originating terminal point as the current
// system.
if (previous == visited.end()) {
visited.emplace(next, PathInfo(curr.curr, curr.origin));
try_next.emplace_back(curr.curr, next, curr.origin);
// next has an ancester so it was visited. Modify the
// older previous to merge paths/create mid points.
} else {
// curr and the previous have the same origin so add
// curr to the systems one hop back along the path
// to previous.
if (previous->second.single_origin
&& previous->second.single_origin == curr.origin)
{
previous->second.one_hop_back.push_back(curr.curr);
// curr and the previous have different origins so
// mark both as reachable midpoints along a good path.
} else if (previous->second.single_origin
&& previous->second.single_origin != curr.origin)
{
// Single origin becomes multi-origin and these
// points are both marked as midpoints.
previous->second.single_origin = boost::none;
reachable_midpoints.push_back(curr.curr);
reachable_midpoints.push_back(next);
// previous is multi-origin so it is already a mid
// point on a good path to multiple terminal
// points. Add curr to the systems one hop back
// along the path to previous.
} else
previous->second.one_hop_back.push_back(curr.curr);
}
}
try_next.pop_front();
}
// Queue is exhausted.
// Part Two: Starting from the mid points find all systems on
// good paths between terminal points.
// No terminal point has a path to any other terminal point.
if (reachable_midpoints.empty())
return good_path;
// Return all systems on any path back to a terminal point.
// Start from every mid point and back track along all paths
// from that mid point adding each system to the good path.
// Stop back tracking when you hit a system already on the good
// path.
// All visited systems on the path(s) from this midpoint not yet processed.
boost::unordered_set<int> unprocessed;
for (int reachable_midpoint : reachable_midpoints) {
boost::unordered_map<int, PathInfo>::const_iterator previous_ii_sys;
int ii_sys;
// Add the mid point to unprocessed, and while there
// are more unprocessed keep checking if the next system is
// in the good_path.
unprocessed.insert(reachable_midpoint);
while (!unprocessed.empty()) {
ii_sys = *unprocessed.begin();
unprocessed.erase(unprocessed.begin());
// If ii_sys is not in the good_path, then add it to the
// good_path and add all of its visited to the unprocessed.
if ((previous_ii_sys = visited.find(ii_sys)) != visited.end() && (!good_path.contains(ii_sys))) {
good_path.insert(ii_sys);
unprocessed.insert(previous_ii_sys->second.one_hop_back.begin(),
previous_ii_sys->second.one_hop_back.end());
}
}
}
return good_path;
}
}
namespace {
// Reimplementation of the boost::hash_range function, embedding
// boost::hash_combine and using std::hash instead of boost::hash
struct hash_set {
static constexpr std::hash<int> hasher{};
static constexpr bool is_noexcept =
noexcept((std::size_t{} ^ hasher(42)) + 0x9e3779b9 + (13<<6) + (13>>2));
std::size_t operator()(const std::set<int>& set) const noexcept(is_noexcept) {
std::size_t seed(2283351); // arbitrary number
for (auto element : set)
seed ^= hasher(element) + 0x9e3779b9 + (seed<<6) + (seed>>2);
return seed;
}
};
/* Takes X and Y coordinates of a pair of systems and moves these points inwards along the vector
* between them by the radius of a system on screen (at zoom 1.0) and return result */
LaneEndpoints StarlaneEndPointsFromSystemPositions(double X1, double Y1, double X2, double Y2) {
// get unit vector
double deltaX = X2 - X1, deltaY = Y2 - Y1;
double mag = std::sqrt(deltaX*deltaX + deltaY*deltaY);
double ring_radius = ClientUI::SystemCircleSize() / 2.0 + 0.5;
// safety check. don't modify original coordinates if they're too close togther
if (mag > 2*ring_radius) {
// rescale vector to length of ring radius
double offsetX = deltaX / mag * ring_radius;
double offsetY = deltaY / mag * ring_radius;
// move start and end points inwards by rescaled vector
X1 += offsetX;
Y1 += offsetY;
X2 -= offsetX;
Y2 -= offsetY;
}
LaneEndpoints retval(static_cast<float>(X1), static_cast<float>(Y1), static_cast<float>(X2), static_cast<float>(Y2));
return retval;
}
using flat_int_set = boost::container::flat_set<int>;
using flat_map_int_sets = boost::container::flat_map<flat_int_set, flat_int_set>;
using flat_map_int_int_set = boost::container::flat_map<int, flat_int_set>;
auto GetResPoolLaneInfo(const ObjectMap& objects, const Empire& empire, const SupplyManager& supply) {
flat_map_int_sets res_pool_systems;
flat_map_int_sets res_group_cores;
flat_int_set res_group_core_members;
flat_map_int_int_set member_to_core;
flat_int_set under_alloc_res_grp_core_members;
const ProductionQueue& queue = empire.GetProductionQueue();
auto& allocated_pp(queue.AllocatedPP());
auto& available_pp(empire.GetIndustryPool().Output());
// For each industry set,
// add all planet's systems to res_pool_systems[industry set]
for (const auto& available_pp_group : available_pp) {
float group_pp = available_pp_group.second;
if (group_pp < 1e-4f)
continue;
// std::string this_pool = "( ";
for (int object_id : available_pp_group.first) {
// this_pool += std::to_string(object_id) +", ";
auto* planet = objects.getRaw<Planet>(object_id);
if (!planet)
continue;
//DebugLogger() << "Empire " << empire_id << "; Planet (" << object_id << ") is named " << planet->Name();
int system_id = planet->SystemID();
auto* system = objects.getRaw<System>(system_id);
if (!system)
continue;
res_pool_systems[available_pp_group.first].insert(system_id);
}
// this_pool += ")";
//DebugLogger() << "Empire " << empire_id << "; ResourcePool[RE_INDUSTRY] resourceGroup (" << this_pool << ") has (" << available_pp_group.second << " PP available";
//DebugLogger() << "Empire " << empire_id << "; ResourcePool[RE_INDUSTRY] resourceGroup (" << this_pool << ") has (" << allocated_pp[available_pp_group.first] << " PP allocated";
}
// Convert supply starlanes to non-directional. This saves half of the lookups.
GetPathsThroughSupplyLanes::SupplyLaneMMap resource_supply_lanes_undirected;
const auto& resource_supply_lanes_directed = supply.SupplyStarlaneTraversals(empire.EmpireID());
for (const auto& supply_lane : resource_supply_lanes_directed) {
resource_supply_lanes_undirected.emplace(supply_lane.first, supply_lane.second);
resource_supply_lanes_undirected.emplace(supply_lane.second, supply_lane.first);
}
// For each pool of resources find all paths available through the supply network.
for (auto& [group_core, group_systems] : res_pool_systems) {
// All individual resource system are included in the network on their own.
group_core.insert(group_systems.begin(), group_systems.end());
res_group_core_members.insert(group_systems.begin(), group_systems.end());
// Convert res_pool_system.second from set<int> to
// unordered_set<int> to improve lookup speed.
std::unordered_set<int> terminal_points{group_systems.begin(), group_systems.end()};
const auto paths = GetPathsThroughSupplyLanes::GetPathsThroughSupplyLanes(
terminal_points, resource_supply_lanes_undirected);
// All systems on the paths are valid end points so they are
// added to the core group of systems that will be rendered
// with thick lines.
for (int waypoint : paths) {
group_core.insert(waypoint);
res_group_core_members.insert(waypoint);
member_to_core[waypoint] = group_core;
}
}
// Take note of all systems of under-allocated resource groups.
for (const auto& available_pp_group : available_pp) {
float group_pp = available_pp_group.second;
if (group_pp < 0.01f)
continue;
auto allocated_it = allocated_pp.find(available_pp_group.first);
if (allocated_it == allocated_pp.end() || (group_pp > allocated_it->second + 0.05)) {
auto group_core_it = res_group_cores.find(available_pp_group.first);
if (group_core_it != res_group_cores.end()) {
under_alloc_res_grp_core_members.insert(group_core_it->second.begin(),
group_core_it->second.end());
}
}
}
return std::tuple{res_pool_systems, res_group_cores, res_group_core_members,
member_to_core, under_alloc_res_grp_core_members};
}
void PrepFullLanesToRender(const std::unordered_map<int, std::shared_ptr<SystemIcon>>& sys_icons,
GG::GL2DVertexBuffer& starlane_vertices,
GG::GLRGBAColorBuffer& starlane_colors)
{
const auto& this_client_known_destroyed_objects =
GetUniverse().EmpireKnownDestroyedObjectIDs(GGHumanClientApp::GetApp()->EmpireID());
const auto& empires = Empires();
const auto& sm = GetSupplyManager();
const auto& o = Objects();
const GG::Clr UNOWNED_LANE_COLOUR = GetOptionsDB().Get<GG::Clr>("ui.map.starlane.color");
std::set<std::pair<int, int>> already_rendered_full_lanes;
for (const auto& id_icon : sys_icons) {
int system_id = id_icon.first;
// skip systems that don't actually exist
if (this_client_known_destroyed_objects.contains(system_id))
continue;
auto start_system = o.get<System>(system_id);
if (!start_system) {
ErrorLogger() << "GetFullLanesToRender couldn't get system with id " << system_id;
continue;
}
// add system's starlanes
for (const auto lane_end_sys_id : start_system->Starlanes()) {
// skip lanes to systems that don't actually exist
if (this_client_known_destroyed_objects.contains(lane_end_sys_id))
continue;
auto* dest_system = o.getRaw<const System>(lane_end_sys_id);
if (!dest_system)
continue;
// check that this lane isn't already in map / being rendered.
if (already_rendered_full_lanes.contains({start_system->ID(), dest_system->ID()}))
continue;
already_rendered_full_lanes.emplace(start_system->ID(), dest_system->ID());
already_rendered_full_lanes.emplace(dest_system->ID(), start_system->ID());
// add vertices for this full-length starlane
LaneEndpoints lane_endpoints = StarlaneEndPointsFromSystemPositions(start_system->X(), start_system->Y(), dest_system->X(), dest_system->Y());
starlane_vertices.store(lane_endpoints.X1, lane_endpoints.Y1);
starlane_vertices.store(lane_endpoints.X2, lane_endpoints.Y2);
// determine colour(s) for lane based on which empire(s) can transfer resources along the lane.
// todo: multiple rendered lanes (one for each empire) when multiple empires use the same lane.
GG::Clr lane_colour = UNOWNED_LANE_COLOUR; // default colour if no empires transfer resources along starlane
for (const auto& [empire_id, empire] : empires) {
const auto& resource_supply_lanes = sm.SupplyStarlaneTraversals(empire_id);
std::pair<int, int> lane_forward{start_system->ID(), dest_system->ID()};
std::pair<int, int> lane_backward{dest_system->ID(), start_system->ID()};
// see if this lane exists in this empire's supply propagation lanes set. either direction accepted.
if (resource_supply_lanes.contains(lane_forward) ||
resource_supply_lanes.contains(lane_backward))
{
lane_colour = empire->Color();
break;
}
}
// vertex colours for starlane
starlane_colors.store(lane_colour);
starlane_colors.store(lane_colour);
}
}
}
void PrepResourceConnectionLanesToRender(const std::unordered_map<int, std::shared_ptr<SystemIcon>>& sys_icons,
int empire_id,
std::set<std::pair<int, int>>& rendered_half_starlanes,
GG::GL2DVertexBuffer& rc_starlane_vertices,
GG::GLRGBAColorBuffer& rc_starlane_colors)
{
rendered_half_starlanes.clear();
const ScriptingContext context;
auto empire = context.GetEmpire(empire_id);
if (!empire)
return;
const auto lane_colour = empire->Color();
auto [res_pool_systems, // map keyed by ResourcePool (set of objects) to the corresponding set of system ids
res_group_cores, // map keyed by ResourcePool to the set of systems considered the core of the corresponding ResGroup
res_group_core_members,
member_to_core,
under_alloc_res_grp_core_members] =
GetResPoolLaneInfo(context.ContextObjects(), *empire, context.supply);
const auto& this_client_known_destroyed_objects =
context.ContextUniverse().EmpireKnownDestroyedObjectIDs(GGHumanClientApp::GetApp()->EmpireID());
//unused variable const GG::Clr UNOWNED_LANE_COLOUR = GetOptionsDB().Get<GG::Clr>("ui.map.starlane.color");
for (const auto& id_icon : sys_icons) {
const int system_id = id_icon.first;
// skip systems that don't actually exist
if (this_client_known_destroyed_objects.contains(system_id))
continue;
const auto* const start_system = context.ContextObjects().getRaw<System>(system_id);
if (!start_system) {
ErrorLogger() << "GetFullLanesToRender couldn't get system with id " << system_id;
continue;
}
// add system's starlanes
for (const auto lane_end_sys_id : start_system->Starlanes()) {
// skip lanes to systems that don't actually exist
if (this_client_known_destroyed_objects.contains(lane_end_sys_id))
continue;
const auto* const dest_system = context.ContextObjects().getRaw<const System>(lane_end_sys_id);
if (!dest_system)
continue;
//std::cout << "colouring lanes between " << start_system->Name() << " and " << dest_system->Name() << std::endl;
// check that this lane isn't already going to be rendered. skip it if it is.
if (rendered_half_starlanes.contains({start_system->ID(), dest_system->ID()}))
continue;
// add resource connection highlight lanes
//std::pair<int, int> lane_forward{start_system->ID(), dest_system->ID()};
//std::pair<int, int> lane_backward{dest_system->ID(), start_system->ID()};
LaneEndpoints lane_endpoints = StarlaneEndPointsFromSystemPositions(start_system->X(), start_system->Y(), dest_system->X(), dest_system->Y());
if (!res_group_core_members.contains(start_system->ID()))
continue;
//start system is a res Grp core member for empire -- highlight
float indicator_extent = 0.5f;
GG::Clr lane_colour_to_use{lane_colour};
if (under_alloc_res_grp_core_members.contains(start_system->ID()))
lane_colour_to_use = GG::DarkenClr(GG::InvertClr(lane_colour));
const auto start_core = member_to_core.find(start_system->ID());
const auto dest_core = member_to_core.find(dest_system->ID());
if (start_core != member_to_core.end() && dest_core != member_to_core.end()
&& (start_core->second != dest_core->second))
{ indicator_extent = 0.2f; }
rc_starlane_vertices.store(lane_endpoints.X1, lane_endpoints.Y1);
rc_starlane_vertices.store((lane_endpoints.X2 - lane_endpoints.X1) * indicator_extent + lane_endpoints.X1, // part way along starlane
(lane_endpoints.Y2 - lane_endpoints.Y1) * indicator_extent + lane_endpoints.Y1);
rc_starlane_colors.store(lane_colour_to_use);
rc_starlane_colors.store(lane_colour_to_use);
}
}
}
void PrepObstructedLaneTraversalsToRender(const auto& sys_icons, int empire_id,
std::set<std::pair<int, int>>& rendered_half_starlanes, // TODO: pass as better container...
GG::GL2DVertexBuffer& starlane_vertices,
GG::GLRGBAColorBuffer& starlane_colors)
{
static_assert(std::is_same_v<int, std::decay_t<decltype(sys_icons.begin()->first)>>);
static_assert(std::is_same_v<SystemIcon, std::decay_t<decltype(*sys_icons.begin()->second)>>);
auto this_empire = GetEmpire(empire_id);
if (!this_empire)
return;
const auto& this_client_known_destroyed_objects =
GetUniverse().EmpireKnownDestroyedObjectIDs(GGHumanClientApp::GetApp()->EmpireID());
for (const auto& id_icon : sys_icons) {
int system_id = id_icon.first;
// skip systems that don't actually exist
if (this_client_known_destroyed_objects.contains(system_id))
continue;
// skip systems that don't actually exist
if (this_client_known_destroyed_objects.contains(system_id))
continue;
auto start_system = Objects().get<System>(system_id);
if (!start_system) {
ErrorLogger() << "MapWnd::InitStarlaneRenderingBuffers couldn't get system with id " << system_id;
continue;
}
// add system's starlanes
for (const auto lane_end_sys_id : start_system->Starlanes()) {
// skip lanes to systems that don't actually exist
if (this_client_known_destroyed_objects.contains(lane_end_sys_id))
continue;
auto dest_system = Objects().get<System>(lane_end_sys_id);
if (!dest_system)
continue;
//std::cout << "colouring lanes between " << start_system->Name() << " and " << dest_system->Name() << std::endl;
// check that this lane isn't already going to be rendered. skip it if it is.
if (rendered_half_starlanes.contains({start_system->ID(), dest_system->ID()}))
continue;
// add obstructed lane traversals as half lanes
for (const auto& [loop_empire_id, loop_empire] : Empires()) {
const auto& resource_obstructed_supply_lanes =
GetSupplyManager().SupplyObstructedStarlaneTraversals(loop_empire_id);
// see if this lane exists in this empire's obstructed supply propagation lanes set. either direction accepted.
if (!resource_obstructed_supply_lanes.contains({start_system->ID(), dest_system->ID()}))
continue;
// found an empire that has a half lane here, so add it.
rendered_half_starlanes.emplace(start_system->ID(), dest_system->ID()); // inserted as ordered pair, so both directions can have different half-lanes
LaneEndpoints lane_endpoints = StarlaneEndPointsFromSystemPositions(
start_system->X(), start_system->Y(), dest_system->X(), dest_system->Y());
starlane_vertices.store(lane_endpoints.X1, lane_endpoints.Y1);
starlane_vertices.store((lane_endpoints.X1 + lane_endpoints.X2) * 0.5f, // half way along starlane
(lane_endpoints.Y1 + lane_endpoints.Y2) * 0.5f);
GG::Clr lane_colour = loop_empire->Color();
starlane_colors.store(lane_colour);
starlane_colors.store(lane_colour);
//std::cout << "Adding half lane between " << start_system->Name() << " to " << dest_system->Name() << " with colour of empire " << empire->Name() << std::endl;
break;
}
}
}
}
auto CalculateStarlaneEndpoints(const std::unordered_map<int, std::shared_ptr<SystemIcon>>& sys_icons) {
std::map<std::pair<int, int>, LaneEndpoints> retval;
const auto& this_client_known_destroyed_objects =
GetUniverse().EmpireKnownDestroyedObjectIDs(GGHumanClientApp::GetApp()->EmpireID());
for (auto const system_id : sys_icons | range_keys) {
// skip systems that don't actually exist
if (this_client_known_destroyed_objects.contains(system_id))
continue;
auto start_system = Objects().get<System>(system_id);
if (!start_system) {
ErrorLogger() << "GetFullLanesToRender couldn't get system with id " << system_id;
continue;
}
// add system's starlanes
for (const auto lane_end_sys_id : start_system->Starlanes()) {
// skip lanes to systems that don't actually exist
if (this_client_known_destroyed_objects.contains(lane_end_sys_id))
continue;
auto dest_system = Objects().get<System>(lane_end_sys_id);
if (!dest_system)
continue;
retval[{system_id, lane_end_sys_id}] =
StarlaneEndPointsFromSystemPositions(start_system->X(), start_system->Y(),
dest_system->X(), dest_system->Y());
retval[{lane_end_sys_id, system_id}] =
StarlaneEndPointsFromSystemPositions(dest_system->X(), dest_system->Y(),
start_system->X(), start_system->Y());
}
}
return retval;
}
}
void MapWnd::InitStarlaneRenderingBuffers() {
DebugLogger() << "MapWnd::InitStarlaneRenderingBuffers";
ScopedTimer timer("MapWnd::InitStarlaneRenderingBuffers", true);
ClearStarlaneRenderingBuffers();
// todo: move this somewhere better... fill in starlane endpoint cache
m_starlane_endpoints = CalculateStarlaneEndpoints(m_system_icons);
// temp storage
std::set<std::pair<int, int>> rendered_half_starlanes; // stored as unaltered pairs, so that a each direction of traversal can be shown separately
// add vertices and colours to lane rendering buffers
PrepFullLanesToRender(m_system_icons, m_starlane_vertices, m_starlane_colors);
PrepResourceConnectionLanesToRender(m_system_icons, GGHumanClientApp::GetApp()->EmpireID(),
rendered_half_starlanes,
m_RC_starlane_vertices, m_RC_starlane_colors);
PrepObstructedLaneTraversalsToRender(m_system_icons, GGHumanClientApp::GetApp()->EmpireID(),
rendered_half_starlanes,
m_starlane_vertices, m_starlane_colors);
// fill new buffers
m_starlane_vertices.createServerBuffer();
m_starlane_colors.createServerBuffer();
m_starlane_vertices.harmonizeBufferType(m_starlane_colors);
m_RC_starlane_vertices.createServerBuffer();
m_RC_starlane_colors.createServerBuffer();
m_RC_starlane_vertices.harmonizeBufferType(m_RC_starlane_colors);
}
void MapWnd::ClearStarlaneRenderingBuffers() {
m_starlane_vertices.clear();
m_starlane_colors.clear();
m_RC_starlane_vertices.clear();
m_RC_starlane_colors.clear();
}
void MapWnd::InitFieldRenderingBuffers() {
DebugLogger() << "MapWnd::InitFieldRenderingBuffers";
ScopedTimer timer("MapWnd::InitFieldRenderingBuffers", true);
ClearFieldRenderingBuffers();
const Universe& universe = GetUniverse();
const auto* app = GGHumanClientApp::GetApp();
const auto empire_id = app->EmpireID();
const auto current_turn = app->CurrentTurn();
// reverse size processing so large fields are painted first and smaller ones on top of larger ones
for (auto& field_icon : m_field_icons | range_reverse) {
bool current_field_visible =
(empire_id == ALL_EMPIRES) ||
universe.GetObjectVisibilityByEmpire(field_icon->FieldID(), empire_id) > Visibility::VIS_BASIC_VISIBILITY;
auto field = universe.Objects().get<Field>(field_icon->FieldID());
if (!field)
continue;
const float FIELD_SIZE = field->GetMeter(MeterType::METER_SIZE)->Initial(); // field size is its radius
if (FIELD_SIZE <= 0)
continue;
const auto& field_texture = field_icon->FieldTexture();
if (!field_texture)
continue;
// group by texture as much as possible for fewer GL calls, but generally paint fields one by one according to size
// so smaller ones get painted over larger ones, including across different textures
// -> if field_vertices is empty (initial conditions), or the last considered texture is not the same as the current field_texture, we create new buffer;
// otherwise, the field type/texture did not change, so we keep adding vertices to the old buffer, and end up with fewer gl calls during rendering
auto& field_vertices = current_field_visible ? m_field_vertices_visible : m_field_vertices_not_visible;
const bool should_create_new_buffer = (field_vertices.empty() || field_vertices.back().first != field_texture);
GG::GL2DVertexBuffer& current_field_vertex_buffer =
should_create_new_buffer ?
field_vertices.emplace_back(field_texture, GG::GL2DVertexBuffer()).second :
field_vertices.back().second;
// determine field rotation angle...
float rotation_angle = field->ID() * 27.0f; // arbitrary rotation in radians ("27.0" is just a number that produces pleasing results)
// per-turn rotation of texture. TODO: make depend on something scriptable
float rotation_speed = 0.03f; // arbitrary rotation rate in radians
if (rotation_speed != 0.0f)
rotation_angle += current_turn * rotation_speed;
const float COS_THETA = std::cos(rotation_angle);
const float SIN_THETA = std::sin(rotation_angle);
// Components of corner points of a quad
const float X1 = FIELD_SIZE, Y1 = FIELD_SIZE; // upper right corner (X1, Y1)
const float X2 = -FIELD_SIZE, Y2 = FIELD_SIZE; // upper left corner (X2, Y2)
const float X3 = -FIELD_SIZE, Y3 = -FIELD_SIZE; // lower left corner (X3, Y3)
const float X4 = FIELD_SIZE, Y4 = -FIELD_SIZE; // lower right corner (X4, Y4)
// Calculate rotated corner point components after CCW ROTATION radians around origin.
const float X1r = COS_THETA*X1 + SIN_THETA*Y1;
const float Y1r = -SIN_THETA*X1 + COS_THETA*Y1;
const float X2r = COS_THETA*X2 + SIN_THETA*Y2;
const float Y2r = -SIN_THETA*X2 + COS_THETA*Y2;
const float X3r = COS_THETA*X3 + SIN_THETA*Y3;
const float Y3r = -SIN_THETA*X3 + COS_THETA*Y3;
const float X4r = COS_THETA*X4 + SIN_THETA*Y4;
const float Y4r = -SIN_THETA*X4 + COS_THETA*Y4;
// add to system position to get translated scaled rotated quad corners
const float FIELD_X1 = field->X() + X1r;
const float FIELD_Y1 = field->Y() + Y1r;
const float FIELD_X2 = field->X() + X2r;
const float FIELD_Y2 = field->Y() + Y2r;
const float FIELD_X3 = field->X() + X3r;
const float FIELD_Y3 = field->Y() + Y3r;
const float FIELD_X4 = field->X() + X4r;
const float FIELD_Y4 = field->Y() + Y4r;
current_field_vertex_buffer.store(FIELD_X1, FIELD_Y1); // rotated upper right
current_field_vertex_buffer.store(FIELD_X2, FIELD_Y2); // rotated upper left
current_field_vertex_buffer.store(FIELD_X3, FIELD_Y3); // rotated lower left
current_field_vertex_buffer.store(FIELD_X4, FIELD_Y4); // rotated lower right
// also add circles to render scanlines for not-visible fields
if (!current_field_visible) {
GG::Pt circle_ul = GG::Pt(GG::X(field->X() - FIELD_SIZE), GG::Y(field->Y() - FIELD_SIZE));
GG::Pt circle_lr = GG::Pt(GG::X(field->X() + FIELD_SIZE), GG::Y(field->Y() + FIELD_SIZE));
BufferStoreCircleArcVertices(m_field_scanline_circles, circle_ul, circle_lr, 0, TWO_PI, true, 0, false);
}
}
m_field_scanline_circles.createServerBuffer();
std::size_t max_buffer_size = 0;
static constexpr auto field_texture_exists = [](const auto& field_vertices_pair) { return field_vertices_pair.first.get(); };
for (auto& field_vertices : { std::ref(m_field_vertices_not_visible), std::ref(m_field_vertices_visible) }) {
for (auto& [field_texture, buffer] : field_vertices.get() | range_filter(field_texture_exists)) {
// TODO: why the binding here?
glBindTexture(GL_TEXTURE_2D, field_texture->OpenGLId());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
buffer.createServerBuffer();
max_buffer_size = std::max(max_buffer_size, buffer.size());
}
}
glBindTexture(GL_TEXTURE_2D, 0);
// make texture coords buffer's size proportional to largest number of fields rendered
// in one GL call; proxy for that is max_buffer_size computed earlier, and since those
// vertex buffers store 4 vertices per field, we divide by 4 to get field count
const std::size_t max_fields_per_texture = max_buffer_size / 4;
for (std::size_t i = 0; i < max_fields_per_texture; ++i) {
m_field_texture_coords.store(1.0f, 0.0f);
m_field_texture_coords.store(0.0f, 0.0f);
m_field_texture_coords.store(0.0f, 1.0f);
m_field_texture_coords.store(1.0f, 1.0f);
}
m_field_texture_coords.createServerBuffer();
}
void MapWnd::ClearFieldRenderingBuffers() {
m_field_vertices_not_visible.clear();
m_field_vertices_visible.clear();
m_field_texture_coords.clear();
m_field_scanline_circles.clear();
}
void MapWnd::InitVisibilityRadiiRenderingBuffers() {
DebugLogger() << "MapWnd::InitVisibilityRadiiRenderingBuffers";
//std::cout << "InitVisibilityRadiiRenderingBuffers" << std::endl;
ScopedTimer timer("MapWnd::InitVisibilityRadiiRenderingBuffers", true);
ClearVisibilityRadiiRenderingBuffers();
ScriptingContext context;
const Universe& universe = context.ContextUniverse();
const ObjectMap& objects = context.ContextObjects();
int client_empire_id = GGHumanClientApp::GetApp()->EmpireID();
const auto& stale_object_ids = universe.EmpireStaleKnowledgeObjectIDs(client_empire_id);
auto empire_position_max_detection_ranges = universe.GetEmpiresAndNeutralPositionDetectionRanges(objects, stale_object_ids);
//auto empire_position_max_detection_ranges = universe.GetEmpiresPositionNextTurnFleetDetectionRanges(context);
std::unordered_map<GG::Clr, std::vector<std::pair<GG::Pt, GG::Pt>>, hash_clr> circles;
for (const auto& [empire_id, detection_circles] : empire_position_max_detection_ranges) {
if (empire_id == ALL_EMPIRES)
continue;
auto empire = context.GetEmpire(empire_id);
if (!empire) {
ErrorLogger() << "InitVisibilityRadiiRenderingBuffers couldn't find empire with id: " << empire_id;
continue;
}
for (const auto& [centre, radius] : detection_circles) {
if (radius < 5.0f || radius > 2048.0f) // hide uselessly small and ridiculously large circles. the latter so super-testers don't have an empire-coloured haze over the whole map.
continue;
const auto& [X, Y] = centre;
GG::Clr circle_colour = empire->Color();
circle_colour.a = 8*GetOptionsDB().Get<int>("ui.map.detection.range.opacity");
const GG::Pt circle_centre = GG::Pt{GG::X(X), GG::Y(Y)};
const GG::Pt rad_pt{GG::X(radius), GG::Y(radius)};
const GG::Pt ul = circle_centre - rad_pt;
const GG::Pt lr = circle_centre + rad_pt;
circles[circle_colour].emplace_back(ul, lr);
}
//std::cout << "adding radii circle at: " << circle_centre << " for empire: " << it->first.first << std::endl;
}
// loop over colours / empires, adding a batch of triangles to buffers for
// each's visibilty circles and outlines
for (const auto& [circle_colour, ul_lrs] : circles) {
// get empire colour and calculate brighter radii outline colour
GG::Clr border_colour = AdjustBrightness(circle_colour, 2.0, true);
border_colour.a = std::min(255, border_colour.a + 80);
const std::size_t radii_start_index = m_visibility_radii_vertices.size();
const std::size_t border_start_index = m_visibility_radii_border_vertices.size();
for (const auto& [ul, lr] : ul_lrs) {
static constexpr std::size_t verts_per_circle = 36;
static constexpr std::size_t vert_per_cricle_edge = 72;
unsigned int initial_size = m_visibility_radii_vertices.size();
// store triangles for filled / transparent part of radii
BufferStoreCircleArcVertices(m_visibility_radii_vertices, ul, lr,
0.0, TWO_PI, true, verts_per_circle, false);
// store colours for triangles
unsigned int size_increment = m_visibility_radii_vertices.size() - initial_size;
for (unsigned int count = 0; count < size_increment; ++count)
m_visibility_radii_colors.store(circle_colour);
// store line segments for border lines of radii
initial_size = m_visibility_radii_border_vertices.size();
BufferStoreCircleArcVertices(m_visibility_radii_border_vertices, ul + BORDER_INSET, lr - BORDER_INSET,
0.0, TWO_PI, false, vert_per_cricle_edge, false);
// store colours for line segments
size_increment = m_visibility_radii_border_vertices.size() - initial_size;
for (unsigned int count = 0; count < size_increment; ++count)
m_visibility_radii_border_colors.store(border_colour);
}
// store how many vertices to render for this colour
std::size_t radii_end_index = m_visibility_radii_vertices.size();
std::size_t border_end_index = m_visibility_radii_border_vertices.size();
m_radii_radii_vertices_indices_runs.emplace_back(
std::pair{radii_start_index, radii_end_index - radii_start_index},
std::pair{border_start_index, border_end_index - border_start_index});
}
m_visibility_radii_border_vertices.createServerBuffer();
m_visibility_radii_border_colors.createServerBuffer();
m_visibility_radii_border_vertices.harmonizeBufferType(m_visibility_radii_border_colors);
m_visibility_radii_vertices.createServerBuffer();
m_visibility_radii_colors.createServerBuffer();
m_visibility_radii_vertices.harmonizeBufferType(m_visibility_radii_colors);
}
void MapWnd::ClearVisibilityRadiiRenderingBuffers() {
m_visibility_radii_vertices.clear();
m_visibility_radii_colors.clear();
m_visibility_radii_border_vertices.clear();
m_visibility_radii_border_colors.clear();
m_radii_radii_vertices_indices_runs.clear();
}
void MapWnd::InitScaleCircleRenderingBuffer() {
ClearScaleCircleRenderingBuffer();
if (!m_scale_line)
return;
int radius = static_cast<int>(Value(m_scale_line->GetLength()) / std::max(0.001, m_scale_line->GetScaleFactor()));
if (radius < 5)
return;
auto selected_system = Objects().get<System>(SidePanel::SystemID());
if (!selected_system)
return;
GG::Pt circle_centre = GG::Pt(GG::X(selected_system->X()), GG::Y(selected_system->Y()));
GG::Pt ul = circle_centre - GG::Pt(GG::X(radius), GG::Y(radius));
GG::Pt lr = circle_centre + GG::Pt(GG::X(radius), GG::Y(radius));
BufferStoreCircleArcVertices(m_scale_circle_vertices, ul, lr, 0, TWO_PI, false, 0, true);
m_scale_circle_vertices.createServerBuffer();
}
void MapWnd::ClearScaleCircleRenderingBuffer()
{ m_scale_circle_vertices.clear(); }
void MapWnd::ClearStarfieldRenderingBuffers() {
m_starfield_verts.clear();
m_starfield_colours.clear();
}
void MapWnd::RestoreFromSaveData(const SaveGameUIData& data) {
m_zoom_steps_in = data.map_zoom_steps_in;
GG::Pt ul = UpperLeft();
GG::Pt map_ul = GG::Pt(GG::X(data.map_left), GG::Y(data.map_top));
GG::Pt map_move = map_ul - ul;
OffsetMove(map_move);
// this correction ensures that zooming in doesn't leave too large a margin to the side
GG::Pt move_to_pt = ul = ClientUpperLeft();
CorrectMapPosition(move_to_pt);
//GG::Pt final_move = move_to_pt - ul;
MoveTo(move_to_pt - GG::Pt(AppWidth(), AppHeight()));
m_fleets_exploring = data.fleets_exploring;
}
void MapWnd::ShowSystemNames() {
for (auto& system_icon : m_system_icons)
system_icon.second->ShowName();
}
void MapWnd::HideSystemNames() {
for (auto& system_icon : m_system_icons)
system_icon.second->HideName();
}
void MapWnd::CenterOnMapCoord(double x, double y) {
if (GetOptionsDB().Get<bool>("ui.map.lock"))
return;
const GG::Pt ul = ClientUpperLeft();
const auto zf = ZoomFactor();
const double current_x = (AppWidth()/2 - ul.x) / zf;
const double current_y = (AppHeight()/2 - ul.y) / zf;
GG::Pt map_move = GG::Pt(static_cast<GG::X>((current_x - x) * zf),
static_cast<GG::Y>((current_y - y) * zf));
OffsetMove(map_move);
// this correction ensures that the centering doesn't leave too large a margin to the side
GG::Pt move_to_pt = ClientUpperLeft();
CorrectMapPosition(move_to_pt);
MoveTo(move_to_pt - GG::Pt(AppWidth(), AppHeight()));
}
void MapWnd::ShowPlanet(int planet_id) {
if (!m_in_production_view_mode) {
ShowPedia();
m_pedia_panel->SetPlanet(planet_id);
}
if (m_in_production_view_mode) {
m_production_wnd->ShowPedia();
m_production_wnd->ShowPlanetInEncyclopedia(planet_id);
}
}
void MapWnd::ShowCombatLog(int log_id) {
m_combat_report_wnd->SetLog(log_id);
m_combat_report_wnd->Show();
GG::GUI::GetGUI()->MoveUp(m_combat_report_wnd);
PushWndStack(m_combat_report_wnd);
}
void MapWnd::ShowTech(std::string tech_name) {
if (m_research_wnd->Visible())
m_research_wnd->ShowTech(tech_name);
if (m_in_production_view_mode) {
m_production_wnd->ShowPedia();
m_production_wnd->ShowTechInEncyclopedia(std::move(tech_name));
} else {
if (!m_pedia_panel->Visible())
TogglePedia();
m_pedia_panel->SetTech(std::move(tech_name));
}
}
void MapWnd::ShowPolicy(std::string policy_name) {
if (m_production_wnd->Visible()) {
m_production_wnd->ShowPedia();
m_production_wnd->ShowPolicyInEncyclopedia(std::move(policy_name));
} else {
if (!m_pedia_panel->Visible())
TogglePedia();
m_pedia_panel->SetPolicy(std::move(policy_name));
}
}
void MapWnd::ShowBuildingType(std::string building_type_name) {
if (m_production_wnd->Visible()) {
m_production_wnd->ShowPedia();
m_production_wnd->ShowBuildingTypeInEncyclopedia(std::move(building_type_name));
} else {
if (!m_pedia_panel->Visible())
TogglePedia();
m_pedia_panel->SetBuildingType(std::move(building_type_name));
}
}
void MapWnd::ShowShipPart(std::string ship_part_name) {
if (m_design_wnd->Visible())
m_design_wnd->ShowShipPartInEncyclopedia(ship_part_name);
if (m_in_production_view_mode) {
m_production_wnd->ShowPedia();
m_production_wnd->ShowShipPartInEncyclopedia(std::move(ship_part_name));
} else {
if (!m_pedia_panel->Visible())
TogglePedia();
m_pedia_panel->SetShipPart(std::move(ship_part_name));
}
}
void MapWnd::ShowShipHull(std::string ship_hull_name) {
if (m_design_wnd->Visible()) {
m_design_wnd->ShowShipHullInEncyclopedia(std::move(ship_hull_name));
} else {
if (!m_pedia_panel->Visible())
TogglePedia();
m_pedia_panel->SetShipHull(std::move(ship_hull_name));
}
}
void MapWnd::ShowShipDesign(int design_id) {
if (m_production_wnd->Visible()) {
m_production_wnd->ShowPedia();
m_production_wnd->ShowShipDesignInEncyclopedia(design_id);
} else {
if (!m_pedia_panel->Visible())
TogglePedia();
m_pedia_panel->SetDesign(design_id);
}
}
void MapWnd::ShowSpecial(std::string special_name) {
if (m_production_wnd->Visible()) {
m_production_wnd->ShowPedia();
m_production_wnd->ShowSpecialInEncyclopedia(std::move(special_name));
} else {
if (!m_pedia_panel->Visible())
TogglePedia();
m_pedia_panel->SetSpecial(std::move(special_name));
}
}
void MapWnd::ShowSpecies(std::string species_name) {
if (m_production_wnd->Visible()) {
m_production_wnd->ShowPedia();
m_production_wnd->ShowSpeciesInEncyclopedia(std::move(species_name));
} else {
if (!m_pedia_panel->Visible())
TogglePedia();
m_pedia_panel->SetSpecies(std::move(species_name));
}
}
void MapWnd::ShowFieldType(std::string field_type_name) {
if (m_production_wnd->Visible()) {
m_production_wnd->ShowPedia();
m_production_wnd->ShowFieldTypeInEncyclopedia(std::move(field_type_name));
} else {
if (!m_pedia_panel->Visible())
TogglePedia();
m_pedia_panel->SetFieldType(std::move(field_type_name));
}
}
void MapWnd::ShowEmpire(int empire_id) {
if (m_in_production_view_mode) {
m_production_wnd->ShowPedia();
m_production_wnd->ShowEmpireInEncyclopedia(empire_id);
}
else {
if (!m_pedia_panel->Visible())
TogglePedia();
m_pedia_panel->SetEmpire(empire_id);
}
}
void MapWnd::ShowMeterTypeArticle(std::string meter_string) {
ShowPedia();
m_pedia_panel->SetMeterType(std::move(meter_string));
}
void MapWnd::ShowMeterTypeArticle(MeterType meter_type) {
ShowPedia();
m_pedia_panel->SetMeterType(meter_type);
}
void MapWnd::ShowEncyclopediaEntry(std::string str) {
if (!m_pedia_panel->Visible())
TogglePedia();
m_pedia_panel->SetEncyclopediaArticle(std::move(str));
}
void MapWnd::CenterOnObject(int id) {
if (auto obj = Objects().get(id))
CenterOnMapCoord(obj->X(), obj->Y());
}
void MapWnd::ReselectLastSystem() {
if (SidePanel::SystemID() != INVALID_OBJECT_ID)
SelectSystem(SidePanel::SystemID());
}
void MapWnd::SelectSystem(int system_id) {
auto system = Objects().get<System>(system_id);
if (!system && system_id != INVALID_OBJECT_ID) {
ErrorLogger() << "MapWnd::SelectSystem couldn't find system with id " << system_id << " so is selected no system instead";
system_id = INVALID_OBJECT_ID;
}
if (system && GetOptionsDB().Get<bool>("ui.map.sidepanel.meter-refresh")) {
// ensure meter estimates are up to date, particularly for which ship is selected
ScriptingContext context;
context.ContextUniverse().UpdateMeterEstimates(context, true);
}
if (SidePanel::SystemID() != system_id) {
// remove map selection indicator from previously selected system
if (SidePanel::SystemID() != INVALID_OBJECT_ID) {
const auto it = m_system_icons.find(SidePanel::SystemID());
if (it != m_system_icons.end())
it->second->SetSelected(false);
}
// set selected system on sidepanel and production screen, as appropriate
if (m_in_production_view_mode)
m_production_wnd->SelectSystem(system_id); // calls SidePanel::SetSystem
else
SidePanel::SetSystem(system_id);
// place map selection indicator on newly selected system
if (SidePanel::SystemID() != INVALID_OBJECT_ID) {
const auto it = m_system_icons.find(SidePanel::SystemID());
if (it != m_system_icons.end())
it->second->SetSelected(true);
}
}
InitScaleCircleRenderingBuffer();
if (m_in_production_view_mode) {
// don't need to do anything to ensure this->m_side_panel is visible,
// since it should be hidden if in production view mode.
return;
}
// even if selected system hasn't changed, it may be nessary to show or
// hide this mapwnd's sidepanel, in case it was hidden at some point and
// should be visible, or is visible and should be hidden.
if (SidePanel::SystemID() == INVALID_OBJECT_ID) {
// no selected system. hide sidepanel.
m_side_panel->Hide();
RemoveFromWndStack(m_side_panel);
} else {
// selected a valid system, show sidepanel
m_side_panel->Show();
GG::GUI::GetGUI()->MoveUp(m_side_panel);
PushWndStack(m_side_panel);
}
}
void MapWnd::ReselectLastFleet() {
//// DEBUG
//std::cout << "MapWnd::ReselectLastFleet m_selected_fleet_ids: " << std::endl;
//for (int fleet_id : m_selected_fleet_ids) {
// auto obj = GetUniverse().Object(fleet_id);
// if (obj)
// std::cout << " " << obj->Name() << "(" << fleet_id << ")" << std::endl;
// else
// std::cout << " [missing object] (" << fleet_id << ")" << std::endl;
//}
const ObjectMap& objects = GetUniverse().Objects();
// search through stored selected fleets' ids and remove ids of missing fleets
std::set<int> missing_fleets;
for (const auto* fleet : objects.findRaw<Fleet>(m_selected_fleet_ids)) {
if (fleet)
missing_fleets.insert(fleet->ID());
}
for (int fleet_id : missing_fleets)
m_selected_fleet_ids.erase(fleet_id);
// select a not-missing fleet, if any
for (int fleet_id : m_selected_fleet_ids) {
SelectFleet(fleet_id);
break; // abort after first fleet selected... don't need to do more
}
}
void MapWnd::SelectPlanet(int planetID, const ScriptingContext& context)
{ m_production_wnd->SelectPlanet(planetID, context); }
void MapWnd::SelectPlanet(int planetID) {
const ScriptingContext context;
m_production_wnd->SelectPlanet(planetID, context);
}
void MapWnd::SelectFleet(int fleet_id)
{ SelectFleet(Objects().get<Fleet>(fleet_id)); }
void MapWnd::SelectFleet(const std::shared_ptr<Fleet>& fleet) {
FleetUIManager& manager = FleetUIManager::GetFleetUIManager();
if (!fleet) {
//std::cout << "MapWnd::SelectFleet selecting no fleet: deselecting all selected fleets." << std::endl;
// first deselect any selected fleets in non-active fleet wnd. this should
// not emit any signals about the active fleet wnd's fleets changing
const auto& active_fleet_wnd = manager.ActiveFleetWnd();
for (const auto& fwnd : manager) {
auto wnd = fwnd.lock();
if (wnd && wnd.get() != active_fleet_wnd)
wnd->DeselectAllFleets();
}
// and finally deselect active fleet wnd fleets. this might emit a signal
// which will update this->m_selected_Fleets
if (active_fleet_wnd)
active_fleet_wnd->DeselectAllFleets();
return;
}
//std::cout << "MapWnd::SelectFleet " << fleet->ID() << std::endl;
// find if there is a FleetWnd for this fleet already open.
auto fleet_wnd = manager.WndForFleetID(fleet->ID());
// if there isn't a FleetWnd for this fleet open, need to open one
if (!fleet_wnd) {
// Add any overlapping fleet buttons for moving or offroad fleets.
auto wnd_fleet_ids = FleetIDsOfFleetButtonsOverlapping(fleet->ID());
if (wnd_fleet_ids.empty())
wnd_fleet_ids.push_back(fleet->ID());
// A leeway, scaled to the button size, around a group of moving fleets so the fleetwnd
// tracks moving fleets together
const auto& any_fleet_button = m_fleet_buttons.empty() ? nullptr : m_fleet_buttons.begin()->second;
double leeway_around_moving_fleets = any_fleet_button ?
2.0 * (Value(any_fleet_button->Width()) + Value(any_fleet_button->Height())) / ZoomFactor() : 0.0;
// create new fleetwnd in which to show selected fleet
fleet_wnd = manager.NewFleetWnd(wnd_fleet_ids, leeway_around_moving_fleets);
// opened a new FleetWnd, so play sound
FleetButton::PlayFleetButtonOpenSound();
}
// make sure selected fleet's FleetWnd is active
manager.SetActiveFleetWnd(fleet_wnd);
GG::GUI::GetGUI()->MoveUp(fleet_wnd);
PushWndStack(fleet_wnd);
// if indicated fleet is already the only selected fleet in the active FleetWnd, nothing to do.
if (m_selected_fleet_ids.size() == 1 && m_selected_fleet_ids.contains(fleet->ID()))
return;
// select fleet in FleetWnd. this deselects all other fleets in the FleetWnd.
// this->m_selected_fleet_ids will be updated by ActiveFleetWndSelectedFleetsChanged or ActiveFleetWndChanged
// signals being emitted and connected to MapWnd::SelectedFleetsChanged
fleet_wnd->SelectFleet(fleet->ID());
}
void MapWnd::RemoveFleet(int fleet_id) {
m_fleet_lines.erase(fleet_id);
m_projected_fleet_lines.erase(fleet_id);
m_selected_fleet_ids.erase(fleet_id);
RefreshFleetButtons(true);
}
void MapWnd::SetFleetMovementLine(int fleet_id) {
if (fleet_id == INVALID_OBJECT_ID)
return;
auto fleet = Objects().get<Fleet>(fleet_id);
if (!fleet) {
ErrorLogger() << "MapWnd::SetFleetMovementLine was passed invalid fleet id " << fleet_id;
return;
}
//std::cout << "creating fleet movement line for fleet at (" << fleet->X() << ", " << fleet->Y() << ")" << std::endl;
const ScriptingContext context;
// get colour: empire colour, or white if no single empire applicable
GG::Clr line_colour = GG::CLR_WHITE;
const auto empire = context.GetEmpire(fleet->Owner());
if (empire)
line_colour = empire->Color();
else if (fleet->Unowned() && fleet->HasMonsters(GetUniverse()))
line_colour = GG::CLR_RED;
// create and store line
auto route(fleet->TravelRoute());
auto path = fleet->MovePath(route, true, context);
auto route_it = route.begin();
if (!route.empty() && (++route_it) != route.end()) {
//DebugLogger() << "MapWnd::SetFleetMovementLine fleet id " << fleet_id<<" checking for blockade at system "<< route.front() <<
// " with m_arrival_lane "<< fleet->ArrivalStarlane()<<" and next destination "<<*route_it;
if (fleet->SystemID() == route.front() &&
fleet->BlockadedAtSystem(route.front(), *route_it, context))
{
//adjust ETAs if necessary
//if (!route.empty() && fleet->SystemID()==route.front() && (++(path.begin()))->post_blockade) {
//DebugLogger() << "MapWnd::SetFleetMovementLine fleet id " << fleet_id<<" blockaded at system "<< route.front() <<
// " with m_arrival_lane "<< fleet->ArrivalStarlane()<<" and next destination "<<*route_it;
if (route_it != route.end() && !( (*route_it == fleet->ArrivalStarlane()) ||
(empire && empire->PreservedLaneTravel(fleet->SystemID(), *route_it)) ) )
{
for (MovePathNode& node : path) {
//DebugLogger() << "MapWnd::SetFleetMovementLine fleet id " << fleet_id<<" node obj " << node.object_id <<
// ", node lane end " << node.lane_end_id << ", is post-blockade (" << node.post_blockade << ")";
if (node.eta >= 250)
node.eta = Fleet::ETA_NEVER;
else
node.eta++;
}
} else {
//DebugLogger() << "MapWnd::SetFleetMovementLine fleet id " << fleet_id<<" slips through second block check";
}
}
}
m_fleet_lines[fleet_id] = MovementLineData(path, m_starlane_endpoints, line_colour, fleet->Owner());
}
void MapWnd::SetProjectedFleetMovementLine(int fleet_id, const std::vector<int>& travel_route) {
if (fleet_id == INVALID_OBJECT_ID)
return;
const ScriptingContext context;
// ensure passed fleet exists
auto fleet = context.ContextObjects().get<Fleet>(fleet_id);
if (!fleet) {
ErrorLogger() << "MapWnd::SetProjectedFleetMovementLine was passed invalid fleet id " << fleet_id;
return;
}
//std::cout << "creating projected fleet movement line for fleet at (" << fleet->X() << ", " << fleet->Y() << ")" << std::endl;
auto empire = context.GetEmpire(fleet->Owner());
// get move path to show. if there isn't one, show nothing
auto path = fleet->MovePath(travel_route, true, context);
// We need the route to contain the current system
// even when it is empty to switch between non appending
// and appending projections on shift changes
if (path.empty())
path.emplace_back(fleet->X(), fleet->Y(), true, 0, fleet->SystemID(),
INVALID_OBJECT_ID, INVALID_OBJECT_ID, false, false);
auto route_it = travel_route.begin();
if (!travel_route.empty() && (++route_it) != travel_route.end()) {
if (fleet->SystemID() == travel_route.front() &&
fleet->BlockadedAtSystem(travel_route.front(), *route_it, context))
{
//adjust ETAs if necessary
//if (!route.empty() && fleet->SystemID()==route.front() && (++(path.begin()))->post_blockade) {
//DebugLogger() << "MapWnd::SetFleetMovementLine fleet id " << fleet_id<<" blockaded at system "<< route.front() <<
//" with m_arrival_lane "<< fleet->ArrivalStarlane()<<" and next destination "<<*route_it;
if (route_it != travel_route.end() && !((*route_it == fleet->ArrivalStarlane()) ||
(empire && empire->PreservedLaneTravel(fleet->SystemID(), *route_it))))
{
for (MovePathNode& node : path) {
//DebugLogger() << "MapWnd::SetFleetMovementLine fleet id " << fleet_id << " node obj " << node.object_id <<
// ", node lane end " << node.lane_end_id << ", is post-blockade (" << node.post_blockade << ")";
if (node.eta >= 250)
node.eta = Fleet::ETA_NEVER;
else
node.eta++;
}
}
}
}
// get colour: empire colour, or white if no single empire applicable
const auto line_colour = empire ? empire->Color() : GG::CLR_WHITE;
// create and store line
m_projected_fleet_lines[fleet_id] = MovementLineData(path, m_starlane_endpoints,
line_colour, fleet->Owner());
}
void MapWnd::SetProjectedFleetMovementLines(const std::vector<int>& fleet_ids,
const std::vector<int>& travel_route)
{
for (int fleet_id : fleet_ids)
SetProjectedFleetMovementLine(fleet_id, travel_route);
}
void MapWnd::ClearProjectedFleetMovementLines()
{ m_projected_fleet_lines.clear(); }
void MapWnd::ForgetObject(int id) {
// Remove visibility information for this object from
// the empire's visibility table.
// The object is usually a ghost ship or fleet.
// Server changes cause a permanent effect
// Tell the server to change what the empire wants to know
// in future so that the server doesn't keep resending this
// object information.
ScriptingContext context;
ObjectMap& objects{context.ContextObjects()};
Universe& universe{context.ContextUniverse()};
auto obj = objects.get(id);
if (!obj)
return;
// If there is only 1 ship in a fleet, forget the fleet
const Ship* ship = nullptr;
if (obj->ObjectType() == UniverseObjectType::OBJ_SHIP) {
ship = static_cast<const Ship*>(obj.get());
if (auto ship_s_fleet = objects.get<const Fleet>(ship->FleetID())) {
bool only_ship_in_fleet = ship_s_fleet->NumShips() == 1;
if (only_ship_in_fleet)
return ForgetObject(ship->FleetID());
}
}
int client_empire_id = GGHumanClientApp::GetApp()->EmpireID();
GGHumanClientApp::GetApp()->Orders().IssueOrder(
std::make_shared<ForgetOrder>(client_empire_id, obj->ID()),
context);
// Client changes for immediate effect
// Force the client to change immediately.
universe.ForgetKnownObject(ALL_EMPIRES, obj->ID());
// Force a refresh
RequirePreRender();
// Update fleet wnd if needed
if (obj->ObjectType() == UniverseObjectType::OBJ_FLEET) {
auto fleet = static_cast<Fleet*>(obj.get());
RemoveFleet(fleet->ID());
fleet->StateChangedSignal();
}
if (ship)
ship->StateChangedSignal();
}
void MapWnd::DoSystemIconsLayout() {
// position and resize system icons and gaseous substance
const int SYSTEM_ICON_SIZE = SystemIconSize();
for (auto& system_icon : m_system_icons) {
auto system = Objects().get<System>(system_icon.first);
if (!system) {
ErrorLogger() << "MapWnd::DoSystemIconsLayout couldn't get system with id " << system_icon.first;
continue;
}
GG::Pt icon_ul(GG::X(static_cast<int>(system->X()*ZoomFactor() - SYSTEM_ICON_SIZE / 2.0)),
GG::Y(static_cast<int>(system->Y()*ZoomFactor() - SYSTEM_ICON_SIZE / 2.0)));
system_icon.second->SizeMove(icon_ul, icon_ul + GG::Pt(GG::X(SYSTEM_ICON_SIZE),
GG::Y(SYSTEM_ICON_SIZE)));
}
}
void MapWnd::DoFieldIconsLayout() {
// position and resize field icons
const double zoom_factor = ZoomFactor();
std::for_each(m_field_icons.cbegin(), m_field_icons.cend(), [zoom_factor](const auto& field_icon) {
auto field = Objects().get<Field>(field_icon->FieldID());
if (!field) {
ErrorLogger() << "MapWnd::DoFieldIconsLayout couldn't get field with id " << field_icon->FieldID();
} else {
double RADIUS = zoom_factor * field->GetMeter(MeterType::METER_SIZE)->Initial(); // Field's MeterType::METER_SIZE gives the radius of the field
GG::Pt icon_ul(GG::X(static_cast<int>(field->X() * zoom_factor - RADIUS)),
GG::Y(static_cast<int>(field->Y() * zoom_factor - RADIUS)));
field_icon->SizeMove(icon_ul, icon_ul + GG::Pt(GG::X(2 * RADIUS), GG::Y(2 * RADIUS)));
}
});
}
void MapWnd::DoFleetButtonsLayout() {
const ObjectMap& objects = GetUniverse().Objects();
auto place_system_fleet_btn = [this](const std::unordered_map<int, std::unordered_set<std::shared_ptr<FleetButton>>>::value_type& system_and_btns, bool is_departing) {
// calculate system icon position
auto system = Objects().get<System>(system_and_btns.first);
if (!system) {
ErrorLogger() << "MapWnd::DoFleetButtonsLayout couldn't find system with id " << system_and_btns.first;
return;
}
const int SYSTEM_ICON_SIZE = SystemIconSize();
GG::Pt icon_ul(GG::X(static_cast<int>(system->X()*ZoomFactor() - SYSTEM_ICON_SIZE / 2.0)),
GG::Y(static_cast<int>(system->Y()*ZoomFactor() - SYSTEM_ICON_SIZE / 2.0)));
// get system icon itself. can't use the system icon's UpperLeft to position fleet button due to weirdness that results that I don't want to figure out
const auto& sys_it = m_system_icons.find(system->ID());
if (sys_it == m_system_icons.end()) {
ErrorLogger() << "couldn't find system icon for fleet button in DoFleetButtonsLayout";
return;
}
const auto& system_icon = sys_it->second;
// place all buttons
int n = 1;
for (auto& button : system_and_btns.second) {
GG::Pt ul = system_icon->NthFleetButtonUpperLeft(n, is_departing);
++n;
button->MoveTo(ul + icon_ul);
}
};
// position departing fleet buttons
for (const auto& system_and_btns : m_departing_fleet_buttons)
place_system_fleet_btn(system_and_btns, true);
// position stationary fleet buttons
for (const auto& system_and_btns : m_stationary_fleet_buttons)
place_system_fleet_btn(system_and_btns, false);
// position moving fleet buttons
for (auto& lane_and_fbs : m_moving_fleet_buttons) {
for (auto& fb : lane_and_fbs.second) {
const GG::Pt FLEET_BUTTON_SIZE = fb->Size();
std::shared_ptr<const Fleet> fleet;
// skip button if it has no fleets (somehow...?) or if the first fleet in the button is 0
if (fb->Fleets().empty() || !(fleet = objects.get<Fleet>(fb->Fleets().front()))) {
ErrorLogger() << "DoFleetButtonsLayout couldn't get first fleet for button";
continue;
}
const auto button_pos = MovingFleetMapPositionOnLane(fleet);
if (!button_pos)
continue;
// position button
GG::Pt button_ul(GG::ToX(button_pos->first * ZoomFactor() - FLEET_BUTTON_SIZE.x / 2.0),
GG::ToY(button_pos->second * ZoomFactor() - FLEET_BUTTON_SIZE.y / 2.0));
fb->MoveTo(button_ul);
}
}
// position offroad fleet buttons
for (auto& [button_pos, buttons] : m_offroad_fleet_buttons) {
for (auto& fb : buttons) {
const GG::Pt FLEET_BUTTON_SIZE = fb->Size();
const auto& fb_fleets = fb->Fleets();
// skip button if it has no fleets (somehow...?) or if the first fleet in the button is 0
if (fb_fleets.empty() || !objects.getRaw<Fleet>(fb_fleets.front())) {
ErrorLogger() << "DoFleetButtonsLayout couldn't get first fleet for button";
continue;
}
// position button
GG::Pt button_ul(GG::ToX(button_pos.first * ZoomFactor() - FLEET_BUTTON_SIZE.x / 2.0),
GG::ToY(button_pos.second * ZoomFactor() - FLEET_BUTTON_SIZE.y / 2.0));
fb->MoveTo(button_ul);
}
}
}
boost::optional<std::pair<double, double>> MapWnd::MovingFleetMapPositionOnLane(
std::shared_ptr<const Fleet> fleet) const
{
if (!fleet)
return boost::none;
// get endpoints of lane on screen
const int sys1_id = fleet->PreviousSystemID();
const int sys2_id = fleet->NextSystemID();
// get apparent positions of endpoints for this lane that have been pre-calculated
auto endpoints_it = m_starlane_endpoints.find({sys1_id, sys2_id});
if (endpoints_it == m_starlane_endpoints.end()) {
// couldn't find an entry for the lane this fleet is one, so just
// return actual position of fleet on starlane - ignore the distance
// away from the star centre at which starlane endpoints should appear
return std::pair(fleet->X(), fleet->Y());
}
const ScriptingContext context;
// return apparent position of fleet on starlane
const LaneEndpoints& screen_lane_endpoints = endpoints_it->second;
return ScreenPosOnStarlane(fleet->X(), fleet->Y(), sys1_id, sys2_id,
screen_lane_endpoints, context);
}
namespace {
template <typename Key>
using KeyToFleetsMap = std::unordered_map<Key, std::vector<int>, boost::hash<Key>>;
using SystemXEmpireToFleetsMap = KeyToFleetsMap<std::pair<int, int>>;
using LocationXEmpireToFleetsMap = KeyToFleetsMap<std::pair<std::pair<double, double>, int>>;
using StarlaneToFleetsMap = KeyToFleetsMap<std::pair<int, int>>;
/** Return fleet if \p obj is not destroyed, not stale, a fleet and not empty.*/
template <typename IntSet>
std::shared_ptr<const Fleet> IsQualifiedFleet(const std::shared_ptr<const UniverseObject>& obj,
int empire_id,
const IntSet& known_destroyed_objects,
const IntSet& stale_object_info)
{
const int object_id = obj->ID();
if (obj->ObjectType() != UniverseObjectType::OBJ_FLEET)
return nullptr;
auto fleet = std::static_pointer_cast<const Fleet>(obj);
if (fleet
&& !fleet->Empty()
&& !known_destroyed_objects.contains(object_id)
&& !stale_object_info.contains(object_id))
{ return fleet; }
return nullptr;
}
/** If the \p fleet has orders and is departing from a valid system, return the system*/
std::shared_ptr<const System> IsDepartingFromSystem(const std::shared_ptr<const Fleet>& fleet) {
if (fleet->FinalDestinationID() != INVALID_OBJECT_ID
&& !fleet->TravelRoute().empty()
&& fleet->SystemID() != INVALID_OBJECT_ID)
{
auto system = Objects().get<System>(fleet->SystemID());
if (system)
return system;
ErrorLogger() << "Couldn't get system with id " << fleet->SystemID()
<< " of a departing fleet named " << fleet->Name();
}
return nullptr;
}
/** If the \p fleet is stationary in a valid system, return the system*/
std::shared_ptr<const System> IsStationaryInSystem(const std::shared_ptr<const Fleet>& fleet) {
if ((fleet->FinalDestinationID() == INVALID_OBJECT_ID
|| fleet->TravelRoute().empty())
&& fleet->SystemID() != INVALID_OBJECT_ID)
{
auto system = Objects().get<System>(fleet->SystemID());
if (system)
return system;
ErrorLogger() << "Couldn't get system with id " << fleet->SystemID()
<< " of a stationary fleet named " << fleet->Name();
}
return nullptr;
}
/** If the \p fleet has a valid destination and it not at a system, return true*/
bool IsMoving(const std::shared_ptr<const Fleet>& fleet) {
return (fleet->FinalDestinationID() != INVALID_OBJECT_ID
&& fleet->SystemID() == INVALID_OBJECT_ID);
}
/** Return the starlane's endpoints if the \p fleet is on a starlane. */
boost::optional<std::pair<int, int>> IsOnStarlane(const std::shared_ptr<const Fleet>& fleet) {
// get endpoints of lane on screen
int sys1_id = fleet->PreviousSystemID();
int sys2_id = fleet->NextSystemID();
auto sys1 = Objects().get<System>(sys1_id);
if (!sys1)
return boost::none;
if (sys1->HasStarlaneTo(sys2_id))
return std::pair(std::min(sys1_id, sys2_id), std::max(sys1_id, sys2_id));
return boost::none;
}
/** If the \p fleet has a valid destination, is not at a system and is
on a starlane, return the starlane's endpoint system ids */
boost::optional<std::pair<int, int>> IsMovingOnStarlane(const std::shared_ptr<const Fleet>& fleet) {
if (!IsMoving(fleet))
return boost::none;
return IsOnStarlane(fleet);
}
/** If the \p fleet has a valid destination and it not on a starlane, return true*/
bool IsOffRoad(const std::shared_ptr<const Fleet>& fleet)
{ return (fleet->SystemID() == INVALID_OBJECT_ID && !IsOnStarlane(fleet)); }
}
void MapWnd::RefreshFleetButtons(bool recreate) {
RequirePreRender();
m_deferred_refresh_fleet_buttons |= !recreate;
m_deferred_recreate_fleet_buttons |= recreate;
}
void MapWnd::DeferredRefreshFleetButtons() {
if (!m_deferred_recreate_fleet_buttons && !m_deferred_refresh_fleet_buttons) {
DoFleetButtonsLayout();
return;
} else if (!m_deferred_recreate_fleet_buttons) {
// just do refresh
m_deferred_refresh_fleet_buttons = false;
ScopedTimer timer("RefreshFleetButtons( just refresh )", true);
for (auto& fleet_button : m_fleet_buttons)
fleet_button.second->Refresh(FleetButtonSizeType());
DoFleetButtonsLayout();
return;
} // else do recreate
m_deferred_recreate_fleet_buttons = false;
m_deferred_refresh_fleet_buttons = false;
ScopedTimer timer("RefreshFleetButtons( full recreate )", true);
// determine fleets that need buttons so that fleets at the same location can
// be grouped by empire owner and buttons created
int client_empire_id = GGHumanClientApp::GetApp()->EmpireID();
const auto& this_client_known_destroyed_objects =
GetUniverse().EmpireKnownDestroyedObjectIDs(client_empire_id);
const auto& this_client_stale_object_info =
GetUniverse().EmpireStaleKnowledgeObjectIDs(client_empire_id);
SystemXEmpireToFleetsMap departing_fleets;
SystemXEmpireToFleetsMap stationary_fleets;
m_moving_fleets.clear();
LocationXEmpireToFleetsMap moving_fleets;
LocationXEmpireToFleetsMap offroad_fleets;
for (const auto& entry : Objects().allExisting<Fleet>()) {
auto fleet = IsQualifiedFleet(entry.second, client_empire_id,
this_client_known_destroyed_objects,
this_client_stale_object_info);
if (!fleet)
continue;
// Collect fleets with a travel route just departing.
if (auto departure_system = IsDepartingFromSystem(fleet)) {
departing_fleets[{departure_system->ID(), fleet->Owner()}].push_back(fleet->ID());
// Collect stationary fleets by system.
} else if (auto stationary_system = IsStationaryInSystem(fleet)) {
// DebugLogger() << fleet->Name() << " is Stationary." ;
stationary_fleets[{stationary_system->ID(), fleet->Owner()}].push_back(fleet->ID());
// Collect traveling fleets between systems by which starlane they
// are on (ignoring location on lane and direction of travel)
} else if (auto starlane_end_systems = IsMovingOnStarlane(fleet)) {
moving_fleets[{*starlane_end_systems, fleet->Owner()}].push_back(fleet->ID());
m_moving_fleets[*starlane_end_systems].push_back(fleet->ID());
} else if (IsOffRoad(fleet)) {
offroad_fleets[{{fleet->X(), fleet->Y()}, fleet->Owner()}].push_back(fleet->ID());
} else {
ErrorLogger() << "Fleet "<< fleet->Name() <<"(" << fleet->ID()
<< ") is not stationary, departing from a system or in transit."
<< " final dest id is " << fleet->FinalDestinationID()
<< " travel routes is of length = " << fleet->TravelRoute().size()
<< " system id is " << fleet->SystemID()
<< " location is (" << fleet->X() << "," <<fleet->Y() << ")";
}
}
DeleteFleetButtons();
// create new fleet buttons for fleets...
const auto FLEETBUTTON_SIZE = FleetButtonSizeType();
CreateFleetButtonsOfType(m_departing_fleet_buttons, departing_fleets, FLEETBUTTON_SIZE);
CreateFleetButtonsOfType(m_stationary_fleet_buttons, stationary_fleets, FLEETBUTTON_SIZE);
CreateFleetButtonsOfType(m_moving_fleet_buttons, moving_fleets, FLEETBUTTON_SIZE);
CreateFleetButtonsOfType(m_offroad_fleet_buttons, offroad_fleets, FLEETBUTTON_SIZE);
// position fleetbuttons
DoFleetButtonsLayout();
// add selection indicators to fleetbuttons
RefreshFleetButtonSelectionIndicators();
// create movement lines (after positioning buttons, so lines will originate from button location)
for (const auto& fleet_button : m_fleet_buttons)
SetFleetMovementLine(fleet_button.first);
}
template <typename FleetButtonMap, typename FleetsMap>
void MapWnd::CreateFleetButtonsOfType(FleetButtonMap& type_fleet_buttons,
const FleetsMap& fleets_map,
const FleetButton::SizeType& fleet_button_size)
{
for (const auto& fleets : fleets_map) {
const auto& key = fleets.first.first;
// buttons need fleet IDs
const auto& fleet_IDs = fleets.second;
if (fleet_IDs.empty())
continue;
// sort fleets by position
std::map<std::pair<double, double>, std::vector<int>> fleet_positions_ids;
for (const auto* fleet : Objects().findRaw<Fleet>(fleet_IDs)) {
if (fleet)
fleet_positions_ids[{fleet->X(), fleet->Y()}].emplace_back(fleet->ID());
}
// create separate FleetButton for each cluster of fleets
for (auto& cluster : fleet_positions_ids) {
auto& ids_in_cluster = cluster.second;
// create new fleetbutton for this cluster of fleets
auto fb = GG::Wnd::Create<FleetButton>(std::move(ids_in_cluster), fleet_button_size);
// store per type of fleet button.
using FBM_key_t = typename FleetButtonMap::key_type;
type_fleet_buttons[static_cast<FBM_key_t>(key)].insert(fb);
// store FleetButton for fleets in current cluster
for (int fleet_id : fb->Fleets())
m_fleet_buttons[fleet_id] = fb;
fb->LeftClickedSignal.connect(boost::bind(&MapWnd::FleetButtonLeftClicked, this, fb.get()));
fb->RightClickedSignal.connect(boost::bind(&MapWnd::FleetButtonRightClicked, this, fb.get()));
AttachChild(std::move(fb));
}
}
}
void MapWnd::DeleteFleetButtons() {
for (auto& id_and_fb : m_fleet_buttons)
DetachChild(id_and_fb.second);
m_fleet_buttons.clear();
// The pointers in the following containers duplicate those in
// m_fleet_buttons and therefore don't need to be detached.
m_stationary_fleet_buttons.clear();
m_departing_fleet_buttons.clear();
m_moving_fleet_buttons.clear();
m_offroad_fleet_buttons.clear();
}
void MapWnd::RefreshSliders() {
if (m_zoom_slider) {
if (GetOptionsDB().Get<bool>("ui.map.zoom.slider.shown") && Visible())
m_zoom_slider->Show();
else
m_zoom_slider->Hide();
}
}
int MapWnd::SystemIconSize() const
{ return static_cast<int>(ClientUI::SystemIconSize() * ZoomFactor()); }
int MapWnd::SystemNamePts() const {
static constexpr int SYSTEM_NAME_MINIMUM_PTS = 6; // limit to absolute minimum point size
static constexpr double MAX_NAME_ZOOM_FACTOR = 1.5; // limit to relative max above standard UI font size
const double NAME_ZOOM_FACTOR = std::min(MAX_NAME_ZOOM_FACTOR, ZoomFactor());
const int ZOOMED_PTS = static_cast<int>(ClientUI::Pts() * NAME_ZOOM_FACTOR);
return std::max(ZOOMED_PTS, SYSTEM_NAME_MINIMUM_PTS);
}
double MapWnd::SystemHaloScaleFactor() const
{ return 1.0 + log10(ZoomFactor()); }
FleetButton::SizeType MapWnd::FleetButtonSizeType() const {
// no SizeType::LARGE as these icons are too big for the map. (they can be used in the FleetWnd, however)
if (ZoomFactor() > ClientUI::BigFleetButtonZoomThreshold())
return FleetButton::SizeType::LARGE;
else if (ZoomFactor() > ClientUI::MediumFleetButtonZoomThreshold())
return FleetButton::SizeType::MEDIUM;
else if (ZoomFactor() > ClientUI::SmallFleetButtonZoomThreshold())
return FleetButton::SizeType::SMALL;
else if (ZoomFactor() > ClientUI::TinyFleetButtonZoomThreshold())
return FleetButton::SizeType::TINY;
else
return FleetButton::SizeType::NONE;
}
void MapWnd::Zoom(int delta) {
GG::Pt center = GG::Pt(AppWidth()/2, AppHeight()/2);
Zoom(delta, center);
}
void MapWnd::Zoom(int delta, const GG::Pt position) {
if (delta == 0)
return;
// increment zoom steps in by delta steps
double new_zoom_steps_in = m_zoom_steps_in + static_cast<double>(delta);
SetZoom(new_zoom_steps_in, true, position);
}
void MapWnd::SetZoom(double steps_in, bool update_slide) {
GG::Pt center = GG::Pt(AppWidth()/2, AppHeight()/2);
SetZoom(steps_in, update_slide, center);
}
void MapWnd::SetZoom(double steps_in, bool update_slide, const GG::Pt position) {
if (GetOptionsDB().Get<bool>("ui.map.lock"))
return;
ScopedTimer timer("MapWnd::SetZoom(steps_in=" + std::to_string(steps_in) +
", update_slide=" + std::to_string(update_slide) +
", position=" + std::string{position}, true);
// impose range limits on zoom steps
double new_steps_in = std::max(std::min(steps_in, ZOOM_IN_MAX_STEPS), ZOOM_IN_MIN_STEPS);
// abort if no change
if (new_steps_in == m_zoom_steps_in)
return;
// save position offsets and old zoom factors
GG::Pt ul = ClientUpperLeft();
GG::X center_x = GG::ToX(AppWidth() / 2.0);
GG::Y center_y = GG::ToY(AppHeight() / 2.0);
GG::X ul_offset_x = ul.x - center_x;
GG::Y ul_offset_y = ul.y - center_y;
const double OLD_ZOOM = ZoomFactor();
const FleetButton::SizeType OLD_FLEETBUTTON_SIZE = FleetButtonSizeType();
// set new zoom level
m_zoom_steps_in = new_steps_in;
// keeps position the same after zooming
// used to keep the mouse at the same position when doing mouse wheel zoom
const GG::Pt position_center_delta = GG::Pt(position.x - center_x, position.y - center_y);
ul_offset_x -= position_center_delta.x;
ul_offset_y -= position_center_delta.y;
// correct map offsets for zoom changes
ul_offset_x *= (ZoomFactor() / OLD_ZOOM);
ul_offset_y *= (ZoomFactor() / OLD_ZOOM);
// now add the zoom position offset at the new zoom level
ul_offset_x += position_center_delta.x;
ul_offset_y += position_center_delta.y;
// show or hide system names, depending on zoom. replicates code in MapWnd::Zoom
if (ZoomFactor() * ClientUI::Pts() < MIN_SYSTEM_NAME_SIZE)
HideSystemNames();
else
ShowSystemNames();
DoSystemIconsLayout();
DoFieldIconsLayout();
// if fleet buttons need to change size, need to fully refresh them (clear
// and recreate). If they are the same size as before the zoom, then can
// just reposition them without recreating
const FleetButton::SizeType NEW_FLEETBUTTON_SIZE = FleetButtonSizeType();
if (OLD_FLEETBUTTON_SIZE != NEW_FLEETBUTTON_SIZE)
RefreshFleetButtons(false);
else
DoFleetButtonsLayout();
// move field icons to bottom of child stack so that other icons can be moused over with a field
for (const auto& field_icon : m_field_icons)
MoveChildDown(field_icon);
// translate map and UI widgets to account for the change in upper left due to zooming
GG::Pt map_move(center_x + ul_offset_x - ul.x, center_y + ul_offset_y - ul.y);
OffsetMove(map_move);
// this correction ensures that zooming in doesn't leave too large a margin to the side
GG::Pt move_to_pt = ul = ClientUpperLeft();
CorrectMapPosition(move_to_pt);
MoveTo(move_to_pt - GG::Pt(AppWidth(), AppHeight()));
int sel_system_id = SidePanel::SystemID();
if (m_scale_line)
m_scale_line->Update(ZoomFactor(), m_selected_fleet_ids, sel_system_id);
if (update_slide && m_zoom_slider)
m_zoom_slider->SlideTo(m_zoom_steps_in);
InitScaleCircleRenderingBuffer();
ZoomedSignal(ZoomFactor());
}
void MapWnd::CorrectMapPosition(GG::Pt& move_to_pt) {
GG::X contents_width(GG::ToX(ZoomFactor() * GetUniverse().UniverseWidth()));
GG::X app_width = AppWidth();
GG::Y app_height = AppHeight();
GG::X map_margin_width(app_width);
//std::cout << "MapWnd::CorrectMapPosition appwidth: " << Value(app_width) << " appheight: " << Value(app_height)
// << " to_x: " << Value(move_to_pt.x) << " to_y: " << Value(move_to_pt.y) << std::endl;;
// restrict map positions to prevent map from being dragged too far off screen.
// add extra padding to restrictions when universe to be shown is larger than
// the screen area in which to show it.
if (app_width - map_margin_width < contents_width || GG::X{Value(app_height)} - map_margin_width < contents_width) {
if (map_margin_width < move_to_pt.x)
move_to_pt.x = map_margin_width;
if (move_to_pt.x + contents_width < app_width - map_margin_width)
move_to_pt.x = app_width - map_margin_width - contents_width;
if (Value(map_margin_width) < Value(move_to_pt.y))
move_to_pt.y = GG::Y{Value(map_margin_width)};
if (Value(move_to_pt.y) + contents_width < GG::X{Value(app_height)} - map_margin_width)
move_to_pt.y = app_height - Value(map_margin_width) - Value(contents_width);
} else {
if (move_to_pt.x < GG::X0)
move_to_pt.x = GG::X0;
if (app_width < move_to_pt.x + contents_width)
move_to_pt.x = app_width - contents_width;
if (move_to_pt.y < GG::Y0)
move_to_pt.y = GG::Y0;
if (app_height < move_to_pt.y + Value(contents_width))
move_to_pt.y = app_height - Value(contents_width);
}
}
void MapWnd::FieldRightClicked(int field_id) {
if (ClientPlayerIsModerator()) {
ModeratorActionSetting mas = m_moderator_wnd->SelectedAction();
ClientNetworking& net = GGHumanClientApp::GetApp()->Networking();
if (mas == ModeratorActionSetting::MAS_Destroy)
net.SendMessage(ModeratorActionMessage(Moderator::DestroyUniverseObject(field_id)));
return;
}
}
void MapWnd::SystemDoubleClicked(int system_id) {
if (!m_in_production_view_mode) {
if (!m_production_wnd->Visible())
ToggleProduction();
CenterOnObject(system_id);
m_production_wnd->SelectSystem(system_id);
}
}
void MapWnd::SystemLeftClicked(int system_id) {
SelectSystem(system_id);
SystemLeftClickedSignal(system_id);
}
void MapWnd::SystemRightClicked(int system_id, GG::Flags<GG::ModKey> mod_keys) {
if (ClientPlayerIsModerator()) {
ModeratorActionSetting mas = m_moderator_wnd->SelectedAction();
ClientNetworking& net = GGHumanClientApp::GetApp()->Networking();
if (mas == ModeratorActionSetting::MAS_Destroy) {
net.SendMessage(ModeratorActionMessage(
Moderator::DestroyUniverseObject(system_id)));
} else if (mas == ModeratorActionSetting::MAS_CreatePlanet) {
net.SendMessage(ModeratorActionMessage(
Moderator::CreatePlanet(system_id, m_moderator_wnd->SelectedPlanetType(),
m_moderator_wnd->SelectedPlanetSize())));
} else if (mas == ModeratorActionSetting::MAS_AddStarlane) {
int selected_system_id = SidePanel::SystemID();
if (Objects().get<System>(selected_system_id)) {
net.SendMessage(ModeratorActionMessage(
Moderator::AddStarlane(system_id, selected_system_id)));
}
} else if (mas == ModeratorActionSetting::MAS_RemoveStarlane) {
int selected_system_id = SidePanel::SystemID();
if (Objects().get<System>(selected_system_id)) {
net.SendMessage(ModeratorActionMessage(
Moderator::RemoveStarlane(system_id, selected_system_id)));
}
} else if (mas == ModeratorActionSetting::MAS_SetOwner) {
int empire_id = m_moderator_wnd->SelectedEmpire();
auto system = Objects().get<System>(system_id);
if (!system)
return;
for (auto* obj : Objects().findRaw<const UniverseObject>(system->ContainedObjectIDs())) {
UniverseObjectType obj_type = obj->ObjectType();
if (obj_type >= UniverseObjectType::OBJ_BUILDING && obj_type < UniverseObjectType::OBJ_SYSTEM) {
net.SendMessage(ModeratorActionMessage(
Moderator::SetOwner(obj->ID(), empire_id)));
}
}
}
}
if (!m_in_production_view_mode && FleetUIManager::GetFleetUIManager().ActiveFleetWnd()) {
if (system_id == INVALID_OBJECT_ID)
ClearProjectedFleetMovementLines();
else
PlotFleetMovement(system_id, !m_ready_turn, mod_keys & GG::MOD_KEY_SHIFT);
SystemRightClickedSignal(system_id);
}
}
void MapWnd::MouseEnteringSystem(int system_id, GG::Flags<GG::ModKey> mod_keys) {
if (ClientPlayerIsModerator()) {
//
} else {
if (!m_in_production_view_mode)
PlotFleetMovement(system_id, false, mod_keys & GG::MOD_KEY_SHIFT);
}
SystemBrowsedSignal(system_id);
}
void MapWnd::MouseLeavingSystem(int system_id)
{ MouseEnteringSystem(INVALID_OBJECT_ID, GG::Flags<GG::ModKey>()); }
void MapWnd::PlanetDoubleClicked(int planet_id) {
if (planet_id == INVALID_OBJECT_ID)
return;
const ScriptingContext context;
// retrieve system_id from planet_id
auto planet = context.ContextObjects().get<Planet>(planet_id);
if (!planet)
return;
// open production screen
if (!m_in_production_view_mode) {
if (!m_production_wnd->Visible())
ToggleProduction();
CenterOnObject(planet->SystemID());
m_production_wnd->SelectSystem(planet->SystemID());
m_production_wnd->SelectPlanet(planet_id, context);
}
}
void MapWnd::PlanetRightClicked(int planet_id) {
if (planet_id == INVALID_OBJECT_ID)
return;
if (!ClientPlayerIsModerator())
return;
ModeratorActionSetting mas = m_moderator_wnd->SelectedAction();
ClientNetworking& net = GGHumanClientApp::GetApp()->Networking();
if (mas == ModeratorActionSetting::MAS_Destroy) {
net.SendMessage(ModeratorActionMessage(
Moderator::DestroyUniverseObject(planet_id)));
} else if (mas == ModeratorActionSetting::MAS_SetOwner) {
int empire_id = m_moderator_wnd->SelectedEmpire();
net.SendMessage(ModeratorActionMessage(
Moderator::SetOwner(planet_id, empire_id)));
}
}
void MapWnd::BuildingRightClicked(int building_id) {
if (building_id == INVALID_OBJECT_ID)
return;
if (!ClientPlayerIsModerator())
return;
ModeratorActionSetting mas = m_moderator_wnd->SelectedAction();
ClientNetworking& net = GGHumanClientApp::GetApp()->Networking();
if (mas == ModeratorActionSetting::MAS_Destroy) {
net.SendMessage(ModeratorActionMessage(
Moderator::DestroyUniverseObject(building_id)));
} else if (mas == ModeratorActionSetting::MAS_SetOwner) {
int empire_id = m_moderator_wnd->SelectedEmpire();
net.SendMessage(ModeratorActionMessage(
Moderator::SetOwner(building_id, empire_id)));
}
}
void MapWnd::ReplotProjectedFleetMovement(bool append) {
TraceLogger() << "MapWnd::ReplotProjectedFleetMovement" << (append?" append":"");
for (const auto& fleet_line : m_projected_fleet_lines) {
const MovementLineData& data = fleet_line.second;
if (!data.path.empty()) {
int target = data.path.back().object_id;
if (target != INVALID_OBJECT_ID) {
PlotFleetMovement(target, false, append);
}
}
}
}
void MapWnd::PlotFleetMovement(int system_id, bool execute_move, bool append) {
if (!FleetUIManager::GetFleetUIManager().ActiveFleetWnd())
return;
if (execute_move || append)
DebugLogger() << "PlotFleetMovement " << (execute_move?" execute":"") << (append?" append":"");
else
TraceLogger() << "PlotfleetMovement";
int empire_id = GGHumanClientApp::GetApp()->EmpireID();
auto fleet_ids = FleetUIManager::GetFleetUIManager().ActiveFleetWnd()->SelectedFleetIDs();
ScriptingContext context;
const ObjectMap& objects{context.ContextObjects()};
const Universe& universe{context.ContextUniverse()};
// apply to all selected this-player-owned fleets in currently-active FleetWnd
for (const auto* fleet : objects.findRaw<Fleet>(fleet_ids)) {
if (!fleet)
continue;
// only give orders / plot prospective move paths of fleets owned by player
if (!(fleet->OwnedBy(empire_id)) || !(fleet->NumShips()))
continue;
// plot empty move pathes if destination is not a known system
if (system_id == INVALID_OBJECT_ID) {
m_projected_fleet_lines.erase(fleet->ID());
continue;
}
int fleet_sys_id = fleet->SystemID();
if (append && !fleet->TravelRoute().empty()) {
fleet_sys_id = fleet->TravelRoute().back();
}
int start_system = fleet_sys_id;
if (fleet_sys_id == INVALID_OBJECT_ID)
start_system = fleet->NextSystemID();
// get path to destination...
auto route = universe.GetPathfinder()->ShortestPath(
start_system, system_id, empire_id, objects).first;
// Prepend a non-empty old_route to the beginning of route.
if (append && !fleet->TravelRoute().empty()) {
auto old_route(fleet->TravelRoute());
old_route.erase(--old_route.end()); //end of old is begin of new
route.insert(route.begin(), old_route.begin(), old_route.end()); // route.splice(route.begin(), old_route);
}
// disallow "offroad" (direct non-starlane non-wormhole) travel
if (route.size() == 2 && route.front() != route.back()) {
int begin_id = route.front();
auto begin_sys = objects.get<System>(begin_id);
int end_id = route.back();
auto end_sys = objects.get<System>(end_id);
if (!begin_sys->HasStarlaneTo(end_id) && !end_sys->HasStarlaneTo(begin_id))
continue;
}
// if actually ordering fleet movement, not just prospectively previewing, ... do so
if (execute_move && !route.empty()){
GGHumanClientApp::GetApp()->Orders().IssueOrder(
std::make_shared<FleetMoveOrder>(empire_id, fleet->ID(), system_id, append, context),
context);
StopFleetExploring(fleet->ID());
}
// show route on map
SetProjectedFleetMovementLine(fleet->ID(), route);
}
}
std::vector<int> MapWnd::FleetIDsOfFleetButtonsOverlapping(int fleet_id) const {
std::vector<int> fleet_ids;
const auto fleet = Objects().get<Fleet>(fleet_id);
if (!fleet) {
ErrorLogger() << "MapWnd::FleetIDsOfFleetButtonsOverlapping: Fleet id "
<< fleet_id << " does not exist.";
return fleet_ids;
}
const auto it = m_fleet_buttons.find(fleet_id);
if (it == m_fleet_buttons.end()) {
// Log that a FleetButton could not be found for the requested fleet, and include when the fleet was last seen
const int empire_id = GGHumanClientApp::GetApp()->EmpireID();
const auto& vis_turn_map = GetUniverse().GetObjectVisibilityTurnMapByEmpire(fleet_id, empire_id);
const auto vis_it = vis_turn_map.find(Visibility::VIS_BASIC_VISIBILITY);
const int vis_turn = (vis_it != vis_turn_map.end()) ? vis_it->second : -1;
ErrorLogger() << "Couldn't find a FleetButton for fleet " << fleet_id
<< " with last basic vis turn " << vis_turn;
return fleet_ids;
}
const auto& fleet_btn = it->second;
// A check if a button overlaps the fleet button
auto overlaps_fleet_btn = [&fleet_btn](const FleetButton& test_fb) {
GG::Pt center = GG::Pt((fleet_btn->Left() + fleet_btn->Right()) / 2,
(fleet_btn->Top() + fleet_btn->Bottom()) /2);
bool retval = test_fb.InWindow(center)
|| test_fb.InWindow(fleet_btn->UpperLeft())
|| test_fb.InWindow(GG::Pt(fleet_btn->Right(), fleet_btn->Top()))
|| test_fb.InWindow(GG::Pt(fleet_btn->Left(), fleet_btn->Bottom()))
|| test_fb.InWindow(fleet_btn->LowerRight());
return retval;
};
// There are 4 types of fleet buttons: moving on a starlane, offroad,
// and stationary or departing from a system.
// Moving fleet buttons only overlap with fleet buttons on the same starlane
if (const auto starlane_end_systems = IsMovingOnStarlane(fleet)) {
const auto& lane_btns_it = m_moving_fleet_buttons.find(*starlane_end_systems);
if (lane_btns_it == m_moving_fleet_buttons.end())
return fleet_ids;
// Add all fleets for each overlapping button on the starlane
for (const auto& test_fb : lane_btns_it->second)
if (overlaps_fleet_btn(*test_fb))
std::copy(test_fb->Fleets().begin(), test_fb->Fleets().end(),
std::back_inserter(fleet_ids));
return fleet_ids;
}
// Offroad fleet buttons only overlap other offroad fleet buttons.
if (IsOffRoad(fleet)) {
// This scales poorly (linearly) with increasing universe size if
// offroading is common.
for (const auto& pos_and_fbs: m_offroad_fleet_buttons) {
const auto& fbs = pos_and_fbs.second;
if (fbs.empty())
continue;
// Since all buttons are at the same position, only check if the first
// button overlaps fleet_btn
if (!overlaps_fleet_btn(**fbs.begin()))
continue;
// Add all fleets for all fleet buttons to btn_fleet
for (const auto& overlapped_fb: fbs)
fleet_ids.insert(fleet_ids.end(), overlapped_fb->Fleets().begin(),
overlapped_fb->Fleets().end());
}
return fleet_ids;
}
// Stationary and departing fleet buttons should not overlap with each
// other because of their offset placement for each empire.
return fleet_btn->Fleets();
}
std::vector<int> MapWnd::FleetIDsOfFleetButtonsOverlapping(const FleetButton& fleet_btn) const {
// get possible fleets to select from, and a pointer to one of those fleets
if (fleet_btn.Fleets().empty()) {
ErrorLogger() << "Clicked FleetButton contained no fleets!";
return std::vector<int>();
}
// Add any overlapping fleet buttons for moving or offroad fleets.
const auto overlapped_fleets = FleetIDsOfFleetButtonsOverlapping(fleet_btn.Fleets()[0]);
if (overlapped_fleets.empty())
ErrorLogger() << "Clicked FleetButton and overlapping buttons contained no fleets!";
return overlapped_fleets;
}
void MapWnd::FleetButtonLeftClicked(const FleetButton* fleet_btn) {
if (!fleet_btn)
return;
// allow switching to fleetView even when in production mode
if (m_in_production_view_mode) {
HideProduction();
RestoreSidePanel();
}
// Add any overlapping fleet buttons for moving or offroad fleets.
const auto fleet_ids_to_include_in_fleet_wnd = FleetIDsOfFleetButtonsOverlapping(*fleet_btn);
if (fleet_ids_to_include_in_fleet_wnd.empty())
return;
int already_selected_fleet_id = INVALID_OBJECT_ID;
// Find if a FleetWnd for these fleet(s) is already open, and if so, if there
// is a single selected fleet in the window, and if so, what fleet that is
// Note: The shared_ptr<FleetWnd> scope is confined to this if block, so that
// SelectFleet below can delete the FleetWnd and re-use the CUIWnd config from
// OptionsDB if needed.
if (const auto& wnd_for_button = FleetUIManager::GetFleetUIManager().WndForFleetIDs(fleet_ids_to_include_in_fleet_wnd)) {
// check which fleet(s) is/are selected in the button's FleetWnd
auto selected_fleet_ids = wnd_for_button->SelectedFleetIDs();
// record selected fleet if just one fleet is selected. otherwise, keep default
// INVALID_OBJECT_ID to indicate that no single fleet is selected
if (selected_fleet_ids.size() == 1)
already_selected_fleet_id = *(selected_fleet_ids.begin());
}
// pick fleet to select from fleets represented by the clicked FleetButton.
int fleet_to_select_id = INVALID_OBJECT_ID;
// fleet_ids are the ids of the clicked and nearby buttons, but when
// clicking a button, only the fleets in that button should be cycled
// through.
const auto& selectable_fleet_ids = fleet_btn->Fleets();
if (already_selected_fleet_id == INVALID_OBJECT_ID || selectable_fleet_ids.size() == 1) {
// no (single) fleet is already selected, or there is only one selectable fleet,
// so select first fleet in button
fleet_to_select_id = *selectable_fleet_ids.begin();
} else {
// select next fleet after already-selected fleet, or first fleet if already-selected
// fleet is the last fleet in the button.
// to do this, scan through button's fleets to find already_selected_fleet
bool found_already_selected_fleet = false;
for (auto it = selectable_fleet_ids.begin(); it != selectable_fleet_ids.end(); ++it) {
if (*it == already_selected_fleet_id) {
// found already selected fleet. get NEXT fleet. don't need to worry about
// there not being enough fleets to do this because if above checks for case
// of there being only one fleet in this button
++it;
// if next fleet iterator is past end of fleets, loop around to first fleet
if (it == selectable_fleet_ids.end())
it = selectable_fleet_ids.begin();
// get fleet to select out of iterator
fleet_to_select_id = *it;
found_already_selected_fleet = true;
break;
}
}
if (!found_already_selected_fleet) {
// didn't find already-selected fleet. the selected fleet might have been moving when the
// click button was for stationary fleets, or vice versa. regardless, just default back
// to selecting the first fleet for this button
fleet_to_select_id = *selectable_fleet_ids.begin();
}
}
// select chosen fleet
if (fleet_to_select_id != INVALID_OBJECT_ID)
SelectFleet(fleet_to_select_id);
}
void MapWnd::FleetButtonRightClicked(const FleetButton* fleet_btn) {
if (!fleet_btn)
return;
// Add any overlapping fleet buttons for moving or offroad fleets.
const auto fleet_ids = FleetIDsOfFleetButtonsOverlapping(*fleet_btn);
if (fleet_ids.empty())
return;
// if fleetbutton holds currently not visible fleets, offer to dismiss them
int empire_id = GGHumanClientApp::GetApp()->EmpireID();
std::vector<int> sensor_ghosts;
// find sensor ghosts
for (const auto* fleet : Objects().findRaw<Fleet>(fleet_ids)) {
if (empire_id == ALL_EMPIRES || !fleet || fleet->OwnedBy(empire_id))
continue;
if (GetUniverse().GetObjectVisibilityByEmpire(fleet->ID(), empire_id) >= Visibility::VIS_BASIC_VISIBILITY)
continue;
sensor_ghosts.push_back(fleet->ID());
}
// should there be sensor ghosts, offer to dismiss them
if (sensor_ghosts.size() > 0) {
auto popup = GG::Wnd::Create<CUIPopupMenu>(fleet_btn->LowerRight().x, fleet_btn->LowerRight().y);
auto forget_fleet_actions = [this, sensor_ghosts]() {
for (auto fleet_id : sensor_ghosts) {
ForgetObject(fleet_id);
}
};
popup->AddMenuItem(GG::MenuItem(UserString("FW_ORDER_DISMISS_SENSOR_GHOST_ALL"), false, false, forget_fleet_actions));
popup->Run();
// Force a redraw
RequirePreRender();
auto fleet_wnd = FleetUIManager::GetFleetUIManager().ActiveFleetWnd();
if (fleet_wnd)
fleet_wnd->RequirePreRender();
}
FleetsRightClicked(fleet_ids);
}
void MapWnd::FleetRightClicked(int fleet_id) {
if (fleet_id == INVALID_OBJECT_ID)
return;
std::vector<int> fleet_ids;
fleet_ids.push_back(fleet_id);
FleetsRightClicked(fleet_ids);
}
void MapWnd::FleetsRightClicked(const std::vector<int>& fleet_ids) {
if (fleet_ids.empty())
return;
if (!ClientPlayerIsModerator())
return;
ModeratorActionSetting mas = m_moderator_wnd->SelectedAction();
ClientNetworking& net = GGHumanClientApp::GetApp()->Networking();
if (mas == ModeratorActionSetting::MAS_Destroy) {
for (int fleet_id : fleet_ids) {
net.SendMessage(ModeratorActionMessage(
Moderator::DestroyUniverseObject(fleet_id)));
}
} else if (mas == ModeratorActionSetting::MAS_SetOwner) {
int empire_id = m_moderator_wnd->SelectedEmpire();
for (int fleet_id : fleet_ids) {
net.SendMessage(ModeratorActionMessage(
Moderator::SetOwner(fleet_id, empire_id)));
}
}
}
void MapWnd::ShipRightClicked(int ship_id) {
if (ship_id == INVALID_OBJECT_ID)
return;
std::vector<int> ship_ids;
ship_ids.push_back(ship_id);
ShipsRightClicked(ship_ids);
}
void MapWnd::ShipsRightClicked(const std::vector<int>& ship_ids) {
if (ship_ids.empty())
return;
if (!ClientPlayerIsModerator())
return;
ModeratorActionSetting mas = m_moderator_wnd->SelectedAction();
ClientNetworking& net = GGHumanClientApp::GetApp()->Networking();
if (mas == ModeratorActionSetting::MAS_Destroy) {
for (int ship_id : ship_ids) {
net.SendMessage(ModeratorActionMessage(
Moderator::DestroyUniverseObject(ship_id)));
}
} else if (mas == ModeratorActionSetting::MAS_SetOwner) {
int empire_id = m_moderator_wnd->SelectedEmpire();
for (int ship_id : ship_ids) {
net.SendMessage(ModeratorActionMessage(
Moderator::SetOwner(ship_id, empire_id)));
}
}
}
void MapWnd::SelectedFleetsChanged() {
// get selected fleets
std::set<int> selected_fleet_ids;
if (const FleetWnd* fleet_wnd = FleetUIManager::GetFleetUIManager().ActiveFleetWnd())
selected_fleet_ids = fleet_wnd->SelectedFleetIDs();
// if old and new sets of selected fleets are the same, don't need to change anything
if (selected_fleet_ids == m_selected_fleet_ids)
return;
// set new selected fleets
m_selected_fleet_ids = selected_fleet_ids;
// update fleetbutton selection indicators
RefreshFleetButtonSelectionIndicators();
}
void MapWnd::SelectedShipsChanged() {
ScopedTimer timer("MapWnd::SelectedShipsChanged", true);
// get selected ships
std::set<int> selected_ship_ids;
if (const FleetWnd* fleet_wnd = FleetUIManager::GetFleetUIManager().ActiveFleetWnd())
selected_ship_ids = fleet_wnd->SelectedShipIDs();
// if old and new sets of selected fleets are the same, don't need to change anything
if (selected_ship_ids == m_selected_ship_ids)
return;
// set new selected fleets
m_selected_ship_ids = selected_ship_ids;
// refresh meters of planets in currently selected system, as changing selected fleets
// may have changed which species a planet should have population estimates shown for
SidePanel::Update();
}
void MapWnd::RefreshFleetButtonSelectionIndicators() {
// clear old selection indicators
for (auto& stationary_fleet_button : m_stationary_fleet_buttons) {
for (auto& button : stationary_fleet_button.second)
button->SetSelected(false);
}
for (auto& departing_fleet_button : m_departing_fleet_buttons) {
for (auto& button : departing_fleet_button.second)
button->SetSelected(false);
}
for (auto& moving_fleet_button : m_moving_fleet_buttons) {
for (auto& button : moving_fleet_button.second)
button->SetSelected(false);
}
for (auto& offroad_fleet_button : m_offroad_fleet_buttons) {
for (auto& button : offroad_fleet_button.second)
button->SetSelected(false);
}
// add new selection indicators
for (int fleet_id : m_selected_fleet_ids) {
const auto& button_it = m_fleet_buttons.find(fleet_id);
if (button_it != m_fleet_buttons.end())
button_it->second->SetSelected(true);
}
}
void MapWnd::UniverseObjectDeleted(const std::shared_ptr<const UniverseObject>& obj) {
if (obj)
DebugLogger() << "MapWnd::UniverseObjectDeleted: " << obj->ID();
else
DebugLogger() << "MapWnd::UniverseObjectDeleted: NO OBJECT";
if (obj && obj->ObjectType() == UniverseObjectType::OBJ_FLEET)
RemoveFleet(obj->ID());
}
void MapWnd::RegisterPopup(std::shared_ptr<MapWndPopup>&& popup) {
if (popup)
m_popups.emplace_back(std::move(popup));
}
void MapWnd::RemovePopup(MapWndPopup* popup) {
if (!popup)
return;
auto it = std::find_if(m_popups.begin(), m_popups.end(),
[popup](const auto& xx){ return xx.lock().get() == popup;});
if (it != m_popups.end())
m_popups.erase(it);
}
void MapWnd::ResetEmpireShown() {
const ScriptingContext context;
m_production_wnd->SetEmpireShown(GGHumanClientApp::GetApp()->EmpireID(), context);
m_research_wnd->SetEmpireShown(GGHumanClientApp::GetApp()->EmpireID(), context);
// TODO: Design?
}
void MapWnd::Sanitize() {
ShowAllPopups(); // make sure popups don't save visible = 0 to the config
CloseAllPopups();
HideResearch();
HideProduction();
HideDesign();
HideGovernment();
RemoveWindows();
m_pedia_panel->ClearItems(); // deletes all pedia items in the memory
m_toolbar->Hide();
m_FPS->Hide();
m_scale_line->Hide();
m_zoom_slider->Hide();
m_combat_report_wnd->Hide();
m_sitrep_panel->ShowSitRepsForTurn(INVALID_GAME_TURN);
if (m_auto_end_turn)
ToggleAutoEndTurn();
ResetEmpireShown();
SelectSystem(INVALID_OBJECT_ID);
// temp
m_starfield_verts.clear();
m_starfield_colours.clear();
// end temp
ClearSystemRenderingBuffers();
ClearStarlaneRenderingBuffers();
ClearFieldRenderingBuffers();
ClearVisibilityRadiiRenderingBuffers();
ClearScaleCircleRenderingBuffer();
ClearStarfieldRenderingBuffers();
if (ClientUI* cui = ClientUI::GetClientUI()) {
// clearing of message window commented out because scrollbar has quirks
// after doing so until enough messages are added
//if (MessageWnd* msg_wnd = cui->GetMessageWnd())
// msg_wnd->Clear();
if (const auto& plr_wnd = cui->GetPlayerListWnd())
plr_wnd->Clear();
}
MoveTo(GG::Pt(-AppWidth(), -AppHeight()));
m_zoom_steps_in = 1.0;
const ScriptingContext context;
m_research_wnd->Sanitize();
m_production_wnd->Sanitize(context.ContextObjects());
m_design_wnd->Sanitize();
m_government_wnd->Sanitize();
m_selected_fleet_ids.clear();
m_selected_ship_ids.clear();
m_starlane_endpoints.clear();
DeleteFleetButtons();
m_fleet_state_change_signals.clear(); // should disconnect scoped signals
m_system_fleet_insert_remove_signals.clear(); // should disconnect scoped signals
m_fleet_lines.clear();
m_projected_fleet_lines.clear();
m_system_icons.clear();
m_fleets_exploring.clear();
DetachChildren();
}
void MapWnd::ResetTimeoutClock(int timeout) {
m_timeout_time = timeout <= 0 ?
std::chrono::time_point<std::chrono::high_resolution_clock>() :
std::chrono::high_resolution_clock::now() + std::chrono::high_resolution_clock::duration(std::chrono::seconds(timeout));
TimerFiring(0, &m_timeout_clock);
}
void MapWnd::TimerFiring(unsigned int ticks, GG::Timer* timer) {
const auto remaining = m_timeout_time - std::chrono::high_resolution_clock::now();
const auto remaining_sec = std::chrono::duration_cast<std::chrono::seconds>(remaining);
if (remaining_sec.count() <= 0) {
m_timeout_clock.Stop();
m_timeout_remain->SetText("");
return;
}
int sec_part = remaining_sec.count() % 60;
int min_part = remaining_sec.count() / 60 % 60;
int hour_part = remaining_sec.count() / 3600;
if (hour_part == 0) {
if (min_part == 0) {
m_timeout_remain->SetText(boost::io::str(FlexibleFormat(UserString("MAP_TIMEOUT_SECONDS")) % sec_part));
} else {
m_timeout_remain->SetText(boost::io::str(FlexibleFormat(UserString("MAP_TIMEOUT_MINS_SECS")) % min_part % sec_part));
}
} else {
m_timeout_remain->SetText(boost::io::str(FlexibleFormat(UserString("MAP_TIMEOUT_HRS_MINS")) % hour_part % min_part));
}
if (!m_timeout_clock.Running()) {
m_timeout_clock.Reset(GG::GUI::GetGUI()->Ticks());
m_timeout_clock.Start();
}
}
void MapWnd::PushWndStack(std::shared_ptr<GG::Wnd> wnd) {
if (!wnd)
return;
// First remove it from its current location in the stack (if any), to prevent it from being
// present in two locations at once.
RemoveFromWndStack(wnd);
m_wnd_stack.push_back(std::move(wnd));
}
void MapWnd::RemoveFromWndStack(std::shared_ptr<GG::Wnd> wnd) {
// Recreate the stack, but without the Wnd to be removed or any null/expired weak_ptrs
std::vector<std::weak_ptr<GG::Wnd>> new_stack;
for (auto& weak_wnd : m_wnd_stack) {
// skip adding to the new stack if it's null/expired
if (auto shared_wnd = weak_wnd.lock()) {
// skip adding to the new stack if it's the one to be removed
if (shared_wnd != wnd) {
// Swap them to avoid another reference count check
new_stack.emplace_back();
new_stack.back().swap(weak_wnd);
}
}
}
m_wnd_stack.swap(new_stack);
}
bool MapWnd::ReturnToMap() {
std::shared_ptr<GG::Wnd> wnd;
// Pop the top Wnd from the stack, and repeat until we find a non-null and visible one (or
// until the stack runs out).
// Need to check that it's visible, in case it was closed without being removed from the stack;
// if we didn't reject such a Wnd, we might close no window, or even open a window.
// Either way, the Wnd is removed from the stack, since it is no longer of any use.
while (!m_wnd_stack.empty() && !(wnd && wnd->Visible())) {
wnd = m_wnd_stack.back().lock();
m_wnd_stack.pop_back();
}
// If no non-null and visible Wnd was found, then there's nothing to do.
if (!(wnd && wnd->Visible())) {
return true;
}
auto cui = ClientUI::GetClientUI();
if (wnd == m_sitrep_panel) {
ToggleSitRep();
} else if (wnd == m_research_wnd) {
ScriptingContext context;
ToggleResearch(context);
} else if (wnd == m_design_wnd) {
ToggleDesign();
} else if (wnd == m_production_wnd) {
ToggleProduction();
} else if (wnd == m_pedia_panel) {
TogglePedia();
} else if (wnd == m_object_list_wnd) {
ToggleObjects();
} else if (wnd == m_moderator_wnd) {
ToggleModeratorActions();
} else if (wnd == m_combat_report_wnd) {
m_combat_report_wnd->Hide();
} else if (wnd == m_side_panel) {
SelectSystem(INVALID_OBJECT_ID);
} else if (dynamic_cast<FleetWnd*>(wnd.get())) {
// if it is any fleet window at all, go ahead and close all fleet windows.
FleetUIManager::GetFleetUIManager().CloseAll();
} else if (cui && wnd == cui->GetPlayerListWnd()) {
HideEmpires();
} else if (cui && wnd == cui->GetMessageWnd()) {
HideMessages();
} else if (wnd == m_government_wnd) {
HideGovernment();
} else {
ErrorLogger() << "Unknown GG::Wnd " << wnd->Name() << " found in MapWnd::m_wnd_stack";
}
return true;
}
bool MapWnd::RevertOrders() {
ClientUI* cui = ClientUI::GetClientUI();
if (!cui) {
ErrorLogger() << "MapWnd::RevertOrders: No client UI available";
return false;
}
GGHumanClientApp::GetApp()->RevertOrders();
return true;
}
bool MapWnd::EndTurn() {
if (m_ready_turn) {
GGHumanClientApp::GetApp()->UnreadyTurn();
} else {
ClientUI* cui = ClientUI::GetClientUI();
if (!cui) {
ErrorLogger() << "MapWnd::EndTurn: No client UI available";
return false;
}
SaveGameUIData ui_data;
cui->GetSaveGameUIData(ui_data);
GGHumanClientApp::GetApp()->StartTurn(ui_data);
}
return true;
}
void MapWnd::ToggleAutoEndTurn() {
if (!m_btn_auto_turn)
return;
if (m_auto_end_turn) {
m_auto_end_turn = false;
m_btn_auto_turn->SetUnpressedGraphic( GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "manual_turn.png")));
m_btn_auto_turn->SetPressedGraphic( GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "auto_turn.png")));
m_btn_auto_turn->SetRolloverGraphic( GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "manual_turn_mouseover.png")));
m_btn_auto_turn->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_btn_auto_turn->SetBrowseInfoWnd(GG::Wnd::Create<TextBrowseWnd>(
UserString("MAP_BTN_MANUAL_TURN_ADVANCE"),
UserString("MAP_BTN_MANUAL_TURN_ADVANCE_DESC")
));
} else {
m_auto_end_turn = true;
m_btn_auto_turn->SetUnpressedGraphic( GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "auto_turn.png")));
m_btn_auto_turn->SetPressedGraphic( GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "manual_turn.png")));
m_btn_auto_turn->SetRolloverGraphic( GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "auto_turn_mouseover.png")));
m_btn_auto_turn->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_btn_auto_turn->SetBrowseInfoWnd(GG::Wnd::Create<TextBrowseWnd>(
UserString("MAP_BTN_AUTO_ADVANCE_ENABLED"),
UserString("MAP_BTN_AUTO_ADVANCE_ENABLED_DESC")
));
}
}
void MapWnd::ShowModeratorActions() {
// hide other "competing" windows
HideResearch();
HideProduction();
HideDesign();
HideGovernment();
RestoreSidePanel();
// update moderator window
m_moderator_wnd->Refresh();
// show the moderator window
m_moderator_wnd->Show();
GG::GUI::GetGUI()->MoveUp(m_moderator_wnd);
PushWndStack(m_moderator_wnd);
m_btn_moderator->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "moderator_mouseover.png")));
m_btn_moderator->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "moderator.png")));
}
void MapWnd::HideModeratorActions() {
m_moderator_wnd->Hide();
RemoveFromWndStack(m_moderator_wnd);
m_btn_moderator->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "moderator.png")));
m_btn_moderator->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "moderator_mouseover.png")));
}
bool MapWnd::ToggleModeratorActions() {
if (!m_moderator_wnd->Visible() || m_production_wnd->Visible() ||
m_research_wnd->Visible() || m_design_wnd->Visible())
{
ShowModeratorActions();
} else {
HideModeratorActions();
}
return true;
}
void MapWnd::ShowObjects() {
ClearProjectedFleetMovementLines();
// hide other "competing" windows
HideResearch();
HideProduction();
HideDesign();
RestoreSidePanel();
// update objects window
m_object_list_wnd->Refresh();
// show the objects window
m_object_list_wnd->Show();
GG::GUI::GetGUI()->MoveUp(m_object_list_wnd);
PushWndStack(m_object_list_wnd);
// indicate selection on button
m_btn_objects->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "objects_mouseover.png")));
m_btn_objects->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "objects.png")));
}
void MapWnd::HideObjects() {
m_object_list_wnd->Hide(); // necessary so it won't be visible when next toggled
RemoveFromWndStack(m_object_list_wnd);
m_btn_objects->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "objects.png")));
m_btn_objects->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "objects_mouseover.png")));
}
bool MapWnd::ToggleObjects() {
if (!m_object_list_wnd->Visible() || m_production_wnd->Visible() ||
m_research_wnd->Visible() || m_design_wnd->Visible())
{
ShowObjects();
} else {
HideObjects();
}
return true;
}
void MapWnd::ShowSitRep() {
ClearProjectedFleetMovementLines();
// hide other "competing" windows
HideResearch();
HideProduction();
HideDesign();
RestoreSidePanel();
// show the sitrep window
m_sitrep_panel->Show();
GG::GUI::GetGUI()->MoveUp(m_sitrep_panel);
PushWndStack(m_sitrep_panel);
// indicate selection on button
m_btn_siterep->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "sitrep_mouseover.png")));
m_btn_siterep->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "sitrep.png")));
}
void MapWnd::HideSitRep() {
m_sitrep_panel->Hide(); // necessary so it won't be visible when next toggled
RemoveFromWndStack(m_sitrep_panel);
m_btn_siterep->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "sitrep.png")));
m_btn_siterep->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "sitrep_mouseover.png")));
}
bool MapWnd::ToggleSitRep() {
if (!m_sitrep_panel->Visible() || m_production_wnd->Visible() ||
m_research_wnd->Visible() || m_design_wnd->Visible())
{
ShowSitRep();
} else {
HideSitRep();
}
return true;
}
void MapWnd::ShowMessages() {
// hide other "competing" windows
HideResearch();
HideProduction();
HideDesign();
RestoreSidePanel();
ClientUI* cui = ClientUI::GetClientUI();
if (!cui)
return;
const auto& msg_wnd = cui->GetMessageWnd();
if (!msg_wnd)
return;
GG::GUI* gui = GG::GUI::GetGUI();
if (!gui)
return;
msg_wnd->Show();
msg_wnd->OpenForInput();
gui->MoveUp(msg_wnd);
PushWndStack(msg_wnd);
// indicate selection on button
m_btn_messages->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "messages_mouseover.png")));
m_btn_messages->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "messages.png")));
}
void MapWnd::HideMessages() {
if (ClientUI* cui = ClientUI::GetClientUI()) {
cui->GetMessageWnd()->Hide();
RemoveFromWndStack(cui->GetMessageWnd());
}
m_btn_messages->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "messages.png")));
m_btn_messages->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "messages_mouseover.png")));
}
bool MapWnd::ToggleMessages() {
ClientUI* cui = ClientUI::GetClientUI();
if (!cui)
return false;
const auto& msg_wnd = cui->GetMessageWnd();
if (!msg_wnd)
return false;
if (!msg_wnd->Visible() || m_production_wnd->Visible() ||
m_research_wnd->Visible() || m_design_wnd->Visible())
{
ShowMessages();
} else {
HideMessages();
}
return true;
}
void MapWnd::ShowEmpires() {
// hide other "competing" windows
HideResearch();
HideProduction();
HideDesign();
RestoreSidePanel();
ClientUI* cui = ClientUI::GetClientUI();
if (!cui)
return;
const auto& plr_wnd = cui->GetPlayerListWnd();
if (!plr_wnd)
return;
GG::GUI* gui = GG::GUI::GetGUI();
if (!gui)
return;
plr_wnd->Show();
gui->MoveUp(plr_wnd);
PushWndStack(plr_wnd);
// indicate selection on button
m_btn_empires->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "empires_mouseover.png")));
m_btn_empires->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "empires.png")));
}
void MapWnd::HideEmpires() {
if (ClientUI* cui = ClientUI::GetClientUI()) {
cui->GetPlayerListWnd()->Hide();
RemoveFromWndStack(cui->GetPlayerListWnd());
}
m_btn_empires->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "empires.png")));
m_btn_empires->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "empires_mouseover.png")));
}
bool MapWnd::ToggleEmpires() {
ClientUI* cui = ClientUI::GetClientUI();
if (!cui)
return false;
const auto& plr_wnd = cui->GetPlayerListWnd();
if (!plr_wnd)
return false;
if (!plr_wnd->Visible() || m_production_wnd->Visible() ||
m_research_wnd->Visible() || m_design_wnd->Visible())
{
ShowEmpires();
} else {
HideEmpires();
}
return true;
}
void MapWnd::ShowPedia() {
// if production screen is visible, toggle the production screen's pedia, not the one of the map screen
if (m_in_production_view_mode) {
m_production_wnd->TogglePedia();
return;
}
// same for research
if (m_research_wnd->Visible()) {
m_research_wnd->TogglePedia();
return;
}
// design screen already has a pedia in it...
if (m_design_wnd->Visible())
return;
ClearProjectedFleetMovementLines();
if (m_pedia_panel->GetItemsSize() == 0)
m_pedia_panel->SetIndex();
// update pedia window
m_pedia_panel->Refresh();
// show the pedia window
m_pedia_panel->Show();
GG::GUI::GetGUI()->MoveUp(m_pedia_panel);
PushWndStack(m_pedia_panel);
// indicate selection on button
m_btn_pedia->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "pedia_mouseover.png")));
m_btn_pedia->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "pedia.png")));
}
void MapWnd::HidePedia() {
m_pedia_panel->Hide();
RemoveFromWndStack(m_pedia_panel);
m_btn_pedia->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "pedia.png")));
m_btn_pedia->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "pedia_mouseover.png")));
}
bool MapWnd::TogglePedia() {
if (!m_pedia_panel->Visible() || m_production_wnd->Visible() ||
m_research_wnd->Visible() || m_design_wnd->Visible())
{
ShowPedia();
} else {
HidePedia();
}
return true;
}
bool MapWnd::ShowGraphs() {
ShowPedia();
m_pedia_panel->AddItem(TextLinker::ENCYCLOPEDIA_TAG, "ENC_GRAPH");
return true;
}
void MapWnd::HideSidePanelAndRememberIfItWasVisible() {
// a kludge, so the sidepanel will reappear after opening and closing a full screen wnd
m_sidepanel_open_before_showing_other = m_side_panel->Visible();
m_side_panel->Hide();
}
void MapWnd::RestoreSidePanel() {
if (m_sidepanel_open_before_showing_other)
ReselectLastSystem();
}
void MapWnd::ShowResearch(const ScriptingContext& context) {
ClearProjectedFleetMovementLines();
// hide other "competing" windows
HideProduction();
HideDesign();
HideSidePanelAndRememberIfItWasVisible();
// show the research window
m_research_wnd->Show();
GG::GUI::GetGUI()->MoveUp(m_research_wnd);
PushWndStack(m_research_wnd);
m_research_wnd->Update(context);
// hide pedia again if it is supposed to be hidden persistently
if (GetOptionsDB().Get<bool>("ui.research.pedia.hidden.enabled"))
m_research_wnd->HidePedia();
// indicate selection on button
m_btn_research->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "research_mouseover.png")));
m_btn_research->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "research.png")));
}
void MapWnd::HideResearch() {
GGHumanClientApp::GetApp()->SendPartialOrders();
m_research_wnd->Hide();
RemoveFromWndStack(m_research_wnd);
m_btn_research->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "research.png")));
m_btn_research->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "research_mouseover.png")));
}
bool MapWnd::ToggleResearch(const ScriptingContext& context) {
if (m_research_wnd->Visible()) {
HideResearch();
RestoreSidePanel();
} else {
ShowResearch(context);
}
return true;
}
void MapWnd::ShowProduction() {
ClearProjectedFleetMovementLines();
// hide other "competing" windows
HideResearch();
HideDesign();
HideSidePanelAndRememberIfItWasVisible();
HidePedia();
if (GetOptionsDB().Get<bool>("ui.production.mappanels.removed")) {
RemoveWindows();
GG::GUI::GetGUI()->Remove(ClientUI::GetClientUI()->GetMessageWnd());
GG::GUI::GetGUI()->Remove(ClientUI::GetClientUI()->GetPlayerListWnd());
}
m_in_production_view_mode = true;
HideAllPopups();
GG::GUI::GetGUI()->MoveUp(m_production_wnd);
PushWndStack(m_production_wnd);
// indicate selection on button
m_btn_production->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "production_mouseover.png")));
m_btn_production->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "production.png")));
const ScriptingContext context;
// if no system is currently shown in sidepanel, default to this empire's
// home system (ie. where the capital is)
if (SidePanel::SystemID() == INVALID_OBJECT_ID) {
if (auto empire = context.GetEmpire(GGHumanClientApp::GetApp()->EmpireID()))
if (auto obj = context.ContextObjects().get(empire->CapitalID()))
SelectSystem(obj->SystemID());
} else {
// if a system is already shown, make sure a planet gets selected by
// default when the production screen opens up
m_production_wnd->SelectDefaultPlanet(context.ContextObjects());
}
m_production_wnd->Update(context);
m_production_wnd->Show();
// hide pedia again if it is supposed to be hidden persistently
if (GetOptionsDB().Get<bool>("ui.production.pedia.hidden.enabled"))
m_production_wnd->TogglePedia();
}
void MapWnd::HideProduction() {
GGHumanClientApp::GetApp()->SendPartialOrders();
m_production_wnd->Hide();
RemoveFromWndStack(m_production_wnd);
m_in_production_view_mode = false;
m_btn_production->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "production.png")));
m_btn_production->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "production_mouseover.png")));
// Don't check ui.production.mappanels.removed to avoid a
// situation where the option is changed and the panels aren't re-registered.
RegisterWindows();
GG::GUI::GetGUI()->Register(ClientUI::GetClientUI()->GetMessageWnd());
GG::GUI::GetGUI()->Register(ClientUI::GetClientUI()->GetPlayerListWnd());
ShowAllPopups();
}
bool MapWnd::ToggleProduction() {
if (m_in_production_view_mode) {
HideProduction();
RestoreSidePanel();
} else {
ShowProduction();
}
// make info panels in production/map window's side panel update their expand-collapse state
m_side_panel->Update();
return true;
}
void MapWnd::ShowDesign() {
ClearProjectedFleetMovementLines();
// hide other "competing" windows
HideResearch();
HideProduction();
HideSidePanelAndRememberIfItWasVisible();
// show the design window
m_design_wnd->Show();
GG::GUI::GetGUI()->MoveUp(m_design_wnd);
PushWndStack(m_design_wnd);
m_design_wnd->Reset();
// indicate selection on button
m_btn_design->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "design_mouseover.png")));
m_btn_design->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "design.png")));
}
void MapWnd::HideDesign() {
GGHumanClientApp::GetApp()->SendPartialOrders();
m_design_wnd->Hide();
RemoveFromWndStack(m_design_wnd);
m_btn_design->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "design.png")));
m_btn_design->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "design_mouseover.png")));
}
bool MapWnd::ToggleDesign() {
if (m_design_wnd->Visible()) {
HideDesign();
RestoreSidePanel();
} else {
ShowDesign();
}
return true;
}
void MapWnd::ShowGovernment() {
ClearProjectedFleetMovementLines();
// hide other "competing" windows
HideResearch();
HideProduction();
HideDesign();
RestoreSidePanel();
// show the government window
m_government_wnd->Show();
GG::GUI::GetGUI()->MoveUp(m_government_wnd);
PushWndStack(m_government_wnd);
m_government_wnd->Reset();
// indicate selection on button
m_btn_government->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "government_mouseover.png")));
m_btn_government->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "government.png")));
}
void MapWnd::HideGovernment() {
GGHumanClientApp::GetApp()->SendPartialOrders();
m_government_wnd->Hide();
RemoveFromWndStack(m_government_wnd);
m_btn_government->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "government.png")));
m_btn_government->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "government_mouseover.png")));
}
bool MapWnd::ToggleGovernment() {
if (!m_government_wnd->Visible() || m_production_wnd->Visible() ||
m_research_wnd->Visible() || m_design_wnd->Visible())
{
ShowGovernment();
} else {
HideGovernment();
}
return true;
}
bool MapWnd::ShowMenu() {
if (m_menu_showing)
return true;
ClearProjectedFleetMovementLines();
m_menu_showing = true;
m_btn_menu->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "menu_mouseover.png")));
m_btn_menu->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "menu.png")));
auto menu = GG::Wnd::Create<InGameMenu>();
menu->Run();
m_menu_showing = false;
m_btn_menu->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "menu.png")));
m_btn_menu->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(ClientUI::ArtDir() / "icons" / "buttons" / "menu_mouseover.png")));
return true;
}
bool MapWnd::CloseSystemView() {
GGHumanClientApp::GetApp()->SendPartialOrders();
SelectSystem(INVALID_OBJECT_ID);
m_side_panel->Hide(); // redundant, but safer to keep in case the behavior of SelectSystem changes
return true;
}
bool MapWnd::KeyboardZoomIn() {
Zoom(1);
return true;
}
bool MapWnd::KeyboardZoomOut() {
Zoom(-1);
return true;
}
void MapWnd::RefreshTurnButtonTooltip() {
auto app = GGHumanClientApp::GetApp();
std::string btn_turn_tooltip;
if (!m_ready_turn) {
if (app->SinglePlayerGame())
btn_turn_tooltip = UserString("MAP_BTN_TURN_TOOLTIP_DESC_SP");
else
btn_turn_tooltip = UserString("MAP_BTN_TURN_TOOLTIP_DESC_MP");
if (app->GetClientType() == Networking::ClientType::CLIENT_TYPE_HUMAN_MODERATOR)
btn_turn_tooltip = UserString("MAP_BTN_TURN_TOOLTIP_DESC_MOD");
}
if (m_ready_turn && !app->SinglePlayerGame())
btn_turn_tooltip = UserString("MAP_BTN_TURN_TOOLTIP_DESC_WAIT");
if (app->GetClientType() == Networking::ClientType::CLIENT_TYPE_HUMAN_OBSERVER)
btn_turn_tooltip = UserString("MAP_BTN_TURN_TOOLTIP_DESC_OBS");
m_btn_turn->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_btn_turn->SetBrowseInfoWnd(GG::Wnd::Create<TextBrowseWnd>(
UserString("MAP_BTN_TURN_TOOLTIP"), btn_turn_tooltip));
}
void MapWnd::RefreshInfluenceResourceIndicator() {
const Empire* empire = GetEmpire(GGHumanClientApp::GetApp()->EmpireID());
if (!empire) {
m_influence->SetValue(0.0);
return;
}
const double total_IP_spent = empire->GetInfluenceQueue().TotalIPsSpent();
const double total_IP_output = empire->GetInfluencePool().TotalOutput();
const double total_IP_target_output = empire->GetInfluencePool().TargetOutput();
const float stockpile = empire->GetInfluencePool().Stockpile();
const float stockpile_used = empire->GetInfluenceQueue().AllocatedStockpileIP();
const float expected_stockpile = empire->GetInfluenceQueue().ExpectedNewStockpileAmount();
const float stockpile_plusminus_next_turn = expected_stockpile - stockpile;
m_influence->SetValue(stockpile);
m_influence->SetValue(stockpile_plusminus_next_turn, 1);
DebugLogger() << "MapWnd::RefreshInfluenceResourceIndicator stockpile: " << stockpile
<< " plusminus: " << stockpile_plusminus_next_turn;
m_influence->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_influence->SetBrowseInfoWnd(GG::Wnd::Create<ResourceBrowseWnd>(
UserString("MAP_INFLUENCE_TITLE"), UserString("GOVERNMENT_INFO_IP"),
total_IP_spent, total_IP_output, total_IP_target_output,
true, stockpile_used, stockpile, expected_stockpile));
}
void MapWnd::RefreshFleetResourceIndicator() {
int empire_id = GGHumanClientApp::GetApp()->EmpireID();
Empire* empire = GetEmpire(empire_id);
if (!empire) {
m_fleet->SetValue(0.0);
return;
}
const auto& this_client_known_destroyed_objects = GetUniverse().EmpireKnownDestroyedObjectIDs(empire_id);
int total_fleet_count = 0;
for (auto* ship : Objects().allRaw<Ship>()) {
if (ship->OwnedBy(empire_id) && !this_client_known_destroyed_objects.contains(ship->ID()))
total_fleet_count++;
}
m_fleet->SetValue(total_fleet_count);
m_fleet->ClearBrowseInfoWnd();
m_fleet->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_fleet->SetBrowseInfoWnd(GG::Wnd::Create<FleetDetailBrowseWnd>(
empire_id, GG::X(FontBasedUpscale(250))));
}
void MapWnd::RefreshResearchResourceIndicator() {
const Empire* empire = GetEmpire(GGHumanClientApp::GetApp()->EmpireID());
if (!empire) {
m_research->SetValue(0.0);
m_research_wasted->Hide();
return;
}
m_research->SetValue(empire->ResourceOutput(ResourceType::RE_RESEARCH));
m_research->ClearBrowseInfoWnd();
m_research->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
float total_RP_spent = empire->GetResearchQueue().TotalRPsSpent();
float total_RP_output = empire->GetResearchPool().TotalOutput();
float total_RP_wasted = total_RP_output - total_RP_spent;
float total_RP_target_output = empire->GetResearchPool().TargetOutput();
m_research->SetBrowseInfoWnd(GG::Wnd::Create<ResourceBrowseWnd>(
UserString("MAP_RESEARCH_TITLE"), UserString("RESEARCH_INFO_RP"),
total_RP_spent, total_RP_output, total_RP_target_output
));
if (total_RP_wasted > 0.05) {
DebugLogger() << "MapWnd::RefreshResearchResourceIndicator: Showing Research Wasted Icon with RP spent: "
<< total_RP_spent << " and RP Production: " << total_RP_output << ", wasting " << total_RP_wasted;
m_research_wasted->Show();
m_research_wasted->ClearBrowseInfoWnd();
m_research_wasted->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_research_wasted->SetBrowseInfoWnd(GG::Wnd::Create<WastedStockpiledResourceBrowseWnd>(
UserString("MAP_RESEARCH_WASTED_TITLE"), UserString("RESEARCH_INFO_RP"),
total_RP_output, total_RP_wasted, false, 0.0f, 0.0f, total_RP_wasted,
UserString("MAP_RES_CLICK_TO_OPEN")));
} else {
m_research_wasted->Hide();
}
}
void MapWnd::RefreshDetectionIndicator() {
const Empire* empire = GetEmpire(GGHumanClientApp::GetApp()->EmpireID());
if (!empire)
return;
m_detection->SetValue(empire->GetMeter("METER_DETECTION_STRENGTH")->Current());
m_detection->ClearBrowseInfoWnd();
m_detection->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_detection->SetBrowseInfoWnd(GG::Wnd::Create<TextBrowseWnd>(
UserString("MAP_DETECTION_TITLE"), UserString("MAP_DETECTION_TEXT")));
}
void MapWnd::RefreshIndustryResourceIndicator() {
ScriptingContext context;
auto empire = context.GetEmpire(GGHumanClientApp::GetApp()->EmpireID());
if (!empire) {
m_industry->SetValue(0.0);
m_industry_wasted->Hide();
m_stockpile->SetValue(0.0);
return;
}
m_industry->SetValue(empire->ResourceOutput(ResourceType::RE_INDUSTRY));
m_industry->ClearBrowseInfoWnd();
m_industry->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
double total_PP_spent = empire->GetProductionQueue().TotalPPsSpent();
double total_PP_output = empire->GetIndustryPool().TotalOutput();
double total_PP_target_output = empire->GetIndustryPool().TargetOutput();
float stockpile = empire->GetIndustryPool().Stockpile();
const auto stockpile_values = empire->GetProductionQueue().AllocatedStockpilePP() | range_values;
float stockpile_used = std::accumulate(stockpile_values.begin(), stockpile_values.end(), 0.0f);
float stockpile_use_capacity = empire->GetProductionQueue().StockpileCapacity(context.ContextObjects());
float expected_stockpile = empire->GetProductionQueue().ExpectedNewStockpileAmount();
float stockpile_plusminus_next_turn = expected_stockpile - stockpile;
double total_PP_for_stockpile_projects = empire->GetProductionQueue().ExpectedProjectTransferToStockpile();
double total_PP_to_stockpile = expected_stockpile - stockpile + stockpile_used;
double total_PP_excess = total_PP_output - total_PP_spent;
double total_PP_wasted = total_PP_output - total_PP_spent - total_PP_to_stockpile + total_PP_for_stockpile_projects;
m_industry->SetBrowseInfoWnd(GG::Wnd::Create<ResourceBrowseWnd>(
UserString("MAP_PRODUCTION_TITLE"), UserString("PRODUCTION_INFO_PP"),
total_PP_spent, total_PP_output, total_PP_target_output,
true, stockpile_used, stockpile, expected_stockpile));
m_stockpile->SetValue(stockpile);
m_stockpile->SetValue(stockpile_plusminus_next_turn, 1);
m_stockpile->ClearBrowseInfoWnd();
m_stockpile->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_stockpile->SetBrowseInfoWnd(GG::Wnd::Create<ResourceBrowseWnd>(
UserString("MAP_STOCKPILE_TITLE"), UserString("PRODUCTION_INFO_PP"),
-1.0f, -1.0f, -1.0f,
true, stockpile_used, stockpile, expected_stockpile,
true, stockpile_use_capacity));
// red "waste" icon if the non-project transfer to IS is more than either 3x per-turn use or 80% total output
// else yellow icon if the non-project transfer to IS is more than 20% total output, or if there is any transfer
// to IS and the IS is expected to be above 10x per-turn use.
if (total_PP_wasted > 0.05 || (total_PP_excess > std::min(3.0 * stockpile_use_capacity, 0.8 * total_PP_output))) {
DebugLogger() << "MapWnd::RefreshIndustryResourceIndicator: Showing Industry Wasted Icon with Industry spent: "
<< total_PP_spent << " and Industry Production: " << total_PP_output << ", wasting " << total_PP_wasted;
boost::filesystem::path button_texture_dir = ClientUI::ArtDir() / "icons" / "buttons";
m_industry_wasted->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(button_texture_dir /
"wasted_resource.png", false)));
m_industry_wasted->SetPressedGraphic(GG::SubTexture(ClientUI::GetTexture(button_texture_dir /
"wasted_resource_clicked.png", false)));
m_industry_wasted->SetRolloverGraphic(GG::SubTexture(ClientUI::GetTexture(button_texture_dir /
"wasted_resource_mouseover.png", false)));
m_industry_wasted->Show();
m_industry_wasted->ClearBrowseInfoWnd();
m_industry_wasted->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_industry_wasted->SetBrowseInfoWnd(GG::Wnd::Create<WastedStockpiledResourceBrowseWnd>(
UserString("MAP_PRODUCTION_WASTED_TITLE"), UserString("PRODUCTION_INFO_PP"),
total_PP_output, total_PP_excess,
true, stockpile_use_capacity, total_PP_to_stockpile, total_PP_wasted,
UserString("MAP_PROD_CLICK_TO_OPEN")));
} else if (total_PP_to_stockpile > 0.05 && (expected_stockpile > (10 * stockpile_use_capacity) ||
total_PP_excess > 0.2 * total_PP_output)) {
boost::filesystem::path button_texture_dir = ClientUI::ArtDir() / "icons" / "buttons";
m_industry_wasted->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(button_texture_dir /
"warned_resource.png", false)));
m_industry_wasted->SetPressedGraphic(GG::SubTexture(ClientUI::GetTexture(button_texture_dir /
"warned_resource_clicked.png", false)));
m_industry_wasted->SetRolloverGraphic(GG::SubTexture(ClientUI::GetTexture(button_texture_dir /
"warned_resource_mouseover.png", false)));
m_industry_wasted->Show();
m_industry_wasted->ClearBrowseInfoWnd();
m_industry_wasted->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_industry_wasted->SetBrowseInfoWnd(GG::Wnd::Create<WastedStockpiledResourceBrowseWnd>(
UserString("MAP_PRODUCTION_WASTED_TITLE"), UserString("PRODUCTION_INFO_PP"),
total_PP_output, total_PP_excess,
true, stockpile_use_capacity, total_PP_to_stockpile, total_PP_wasted,
UserString("MAP_PROD_CLICK_TO_OPEN")));
} else {
m_industry_wasted->Hide();
}
}
void MapWnd::RefreshPopulationIndicator() {
// TODO: make a ScriptingContext and use instead of free GetEmpire, Objects, etc.
float target_population = 0.0f;
Empire* empire = GetEmpire(GGHumanClientApp::GetApp()->EmpireID());
if (!empire) {
m_population->SetValue(0.0);
return;
} else {
target_population = empire->GetPopulationPool().Population();
}
m_population->SetValue(target_population);
m_population->ClearBrowseInfoWnd();
const auto& pop_center_ids = empire->GetPopulationPool().PopCenterIDs();
std::map<std::string, float> population_counts;
std::map<std::string, int> population_worlds;
std::map<std::string, float> tag_counts;
std::map<std::string, int> tag_worlds;
const ObjectMap& objects = Objects();
const SpeciesManager& sm = GetSpeciesManager();
//tally up all species population counts
for (const auto* pc : objects.findRaw<Planet>(pop_center_ids)) {
if (!pc)
continue;
const auto& species_name = pc->SpeciesName();
if (species_name.empty())
continue;
const float this_pop = pc->UniverseObject::GetMeter(MeterType::METER_POPULATION)->Initial();
population_counts[species_name] += this_pop;
population_worlds[species_name] += 1;
if (const Species* species = sm.GetSpecies(species_name) ) {
for (auto tag : species->Tags()) {
tag_counts[std::string{tag}] += this_pop;
tag_worlds[std::string{tag}] += 1;
}
}
}
m_population->SetBrowseModeTime(GetOptionsDB().Get<int>("ui.tooltip.delay"));
m_population->SetBrowseInfoWnd(GG::Wnd::Create<CensusBrowseWnd>(
UserString("MAP_POPULATION_DISTRIBUTION"),
target_population,
std::move(population_counts),std::move(population_worlds),
std::move(tag_counts), std::move(tag_worlds), GetSpeciesManager().census_order()
));
}
void MapWnd::UpdateEmpireResourcePools() {
ScriptingContext context;
auto empire = context.GetEmpire(GGHumanClientApp::GetApp()->EmpireID());
if (!empire)
return;
/* Recalculate stockpile, available, production, predicted change of
* resources. When resource pools update, they emit ChangeSignal, which is
* connected to MapWnd::Refresh???ResourceIndicator, which updates the
* empire resource pool indicators of the MapWnd. */
empire->UpdateResourcePools(context, empire->TechCostsTimes(context),
empire->PlanetAnnexationCosts(context),
empire->PolicyAdoptionCosts(context),
empire->ProductionCostsTimes(context));
// Update indicators on sidepanel, which are not directly connected to from the ResourcePool ChangedSignal
SidePanel::Update();
}
bool MapWnd::ZoomToHomeSystem() {
const Empire* empire = GetEmpire(GGHumanClientApp::GetApp()->EmpireID());
if (!empire)
return false;
int home_id = empire->CapitalID();
if (home_id != INVALID_OBJECT_ID) {
auto object = Objects().get(home_id);
if (!object)
return false;
CenterOnObject(object->SystemID());
SelectSystem(object->SystemID());
}
return true;
}
namespace {
struct AllTrue { constexpr bool operator()(const auto*) { return true; } };
template <typename T, typename P = AllTrue>
requires std::is_base_of_v<UniverseObject, T> &&
requires(P p, const T* t) { {p(t)} -> std::same_as<bool>; }
auto IDsSortedByName(P&& pred = P{}) {
constexpr auto to_id_name = [](const auto* obj) -> std::pair<int, std::string_view> { return {obj->ID(), obj->Name()}; };
const auto objs = [pred]() -> decltype(auto) {
if constexpr (std::is_same_v<std::decay_t<decltype(pred)>, AllTrue>)
return Objects().allExistingRaw<const T>();
else
return Objects().findExistingRaw<const T>(pred);
}();
// collect ids and corresponding names
std::vector<std::pair<int, std::string_view>> ids_names;
ids_names.reserve(Objects().size<T>());
range_copy(objs | range_transform(to_id_name), std::back_inserter(ids_names));
// alphabetize, with empty names at end
auto not_empty_it = std::partition(ids_names.begin(), ids_names.end(),
[](const auto& id_name) { return !id_name.second.empty(); });
std::sort(ids_names.begin(), not_empty_it,
[](const auto& lhs, const auto& rhs) { return lhs.second < rhs.second; });
// extract ordered ids
std::vector<int> retval;
retval.reserve(ids_names.size());
range_copy(ids_names | range_keys, std::back_inserter(retval));
return retval;
}
template <typename It>
constexpr auto LoopNext(const It begin_it, const It from_it, const It end_it) {
if (from_it != end_it) {
auto it = std::next(from_it);
if (it != end_it)
return it;
}
return begin_it;
};
constexpr std::array<int, 10> test_nums = {1,2,3,4,5,6,7,8,9,10};
constexpr auto tnsb = test_nums.begin(), tnsrt = std::next(test_nums.begin(), 5), tnsnd = test_nums.end();
static_assert(*tnsrt == 6 && *LoopNext(tnsb, tnsrt, tnsnd) == 7);
static_assert(LoopNext(tnsb, tnsnd, tnsnd) == tnsb);
enum class SearchDir : bool { FORWARD, REVERSE };
// gets id of loop-next id in \a ids
int GetNext(const auto& ids, const int start_from_id, SearchDir dir = SearchDir::FORWARD) {
if (ids.empty()) [[unlikely]]
return INVALID_OBJECT_ID;
if (ids.size() == 1u) [[unlikely]]
return *ids.begin(); // forward and backwards are the same in this case...
if (dir == SearchDir::FORWARD)
return *LoopNext(ids.begin(), std::find(ids.begin(), ids.end(), start_from_id), ids.end());
else
return *LoopNext(ids.rbegin(), std::find(ids.rbegin(), ids.rend(), start_from_id), ids.rend());
}
bool ZoomToPrevOrNextOwnedSystem(SearchDir dir, MapWnd& mw) {
const auto contains_owned_by_empire = [empire_id{GGHumanClientApp::GetApp()->EmpireID()}](const System* sys) {
auto is_owned_and_contained = [empire_id, sys_id{sys->ID()}](const Planet* obj)
{ return obj && obj->ContainedBy(sys_id) && obj->OwnedBy(empire_id); };
return Objects().check_if_any<Planet, decltype(is_owned_and_contained), false>(is_owned_and_contained);
};
const auto next_sys_id = GetNext(IDsSortedByName<System>(contains_owned_by_empire), SidePanel::SystemID(), dir);
if (next_sys_id == INVALID_OBJECT_ID)
return false;
mw.CenterOnObject(next_sys_id);
mw.SelectSystem(next_sys_id);
return true;
}
bool ZoomToPrevOrNextSystem(SearchDir dir, MapWnd& mw) {
const auto next_sys_id = GetNext(IDsSortedByName<System>(), SidePanel::SystemID(), dir);
if (next_sys_id == INVALID_OBJECT_ID)
return false;
mw.CenterOnObject(next_sys_id);
mw.SelectSystem(next_sys_id);
return true;
}
bool ZoomToPrevOrNextOwnedFleet(SearchDir dir, MapWnd& mw) {
const auto is_owned_fleet = [client_empire_id{GGHumanClientApp::GetApp()->EmpireID()}](const Fleet* fleet)
{ return client_empire_id == ALL_EMPIRES || fleet->OwnedBy(client_empire_id); };
const auto next_fleet_id = GetNext(IDsSortedByName<Fleet>(is_owned_fleet), mw.SelectedFleetID(), dir);
if (next_fleet_id == INVALID_OBJECT_ID)
return false;
mw.CenterOnObject(next_fleet_id);
mw.SelectFleet(next_fleet_id);
return true;
}
bool ZoomToPrevOrNextIdleFleet(SearchDir dir, MapWnd& mw) {
auto is_stationary_owned_fleet = [client_empire_id{GGHumanClientApp::GetApp()->EmpireID()}](const Fleet* fleet) {
return (fleet->FinalDestinationID() == INVALID_OBJECT_ID || fleet->TravelRoute().empty()) &&
(client_empire_id == ALL_EMPIRES ||
(!fleet->Unowned() && fleet->Owner() == client_empire_id));
};
const auto next_fleet_id = GetNext(IDsSortedByName<Fleet>(is_stationary_owned_fleet), mw.SelectedFleetID(), dir);
if (next_fleet_id == INVALID_OBJECT_ID)
return false;
mw.CenterOnObject(next_fleet_id);
mw.SelectFleet(next_fleet_id);
return true;
}
}
bool MapWnd::ZoomToPrevOwnedSystem()
{ return ZoomToPrevOrNextOwnedSystem(SearchDir::REVERSE, *this); }
bool MapWnd::ZoomToNextOwnedSystem()
{ return ZoomToPrevOrNextOwnedSystem(SearchDir::FORWARD, *this); }
bool MapWnd::ZoomToPrevSystem()
{ return ZoomToPrevOrNextSystem(SearchDir::REVERSE, *this); }
bool MapWnd::ZoomToNextSystem()
{ return ZoomToPrevOrNextSystem(SearchDir::FORWARD, *this); }
bool MapWnd::ZoomToPrevIdleFleet()
{ return ZoomToPrevOrNextIdleFleet(SearchDir::REVERSE, *this); }
bool MapWnd::ZoomToNextIdleFleet()
{ return ZoomToPrevOrNextIdleFleet(SearchDir::FORWARD, *this); }
bool MapWnd::ZoomToPrevFleet()
{ return ZoomToPrevOrNextOwnedFleet(SearchDir::REVERSE, *this); }
bool MapWnd::ZoomToNextFleet()
{ return ZoomToPrevOrNextOwnedFleet(SearchDir::FORWARD, *this); }
bool MapWnd::ZoomToSystemWithWastedPP() {
const ScriptingContext context;
const Empire* empire = GetEmpire(GGHumanClientApp::GetApp()->EmpireID());
if (!empire)
return false;
const ProductionQueue& queue = empire->GetProductionQueue();
const auto& pool = empire->GetIndustryPool();
auto wasted_PP_objects(queue.ObjectsWithWastedPP(pool));
if (wasted_PP_objects.empty())
return false;
for (const auto& obj_ids : wasted_PP_objects) {
for (auto id : obj_ids) {
const auto* obj = context.ContextObjects().getRaw(id);
if (!obj)
continue;
const auto sys_id = obj->SystemID();
if (sys_id == INVALID_OBJECT_ID)
continue;
// found object with wasted PP that is in a system. zoom there.
CenterOnObject(sys_id);
SelectSystem(sys_id);
ShowProduction();
return true;
}
}
return false;
}
namespace {
/// On when the MapWnd window is visible and not covered by one of the full screen covering windows
struct NotCoveredMapWndCondition {
NotCoveredMapWndCondition(const MapWnd& tg) : target(tg) {}
bool operator()() const
{ return target.Visible() && !target.InResearchViewMode() && !target.InDesignViewMode(); };
const MapWnd& target;
};
}
void MapWnd::ConnectKeyboardAcceleratorSignals() {
HotkeyManager& hkm = HotkeyManager::GetManager();
hkm.Connect(boost::bind(&MapWnd::ReturnToMap, this), "ui.map.open",
AndCondition(VisibleWindowCondition(this), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::EndTurn, this), "ui.turn.end",
AndCondition(VisibleWindowCondition(this), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::ToggleSitRep, this), "ui.map.sitrep",
AndCondition(VisibleWindowCondition(this), NoModalWndsOpenCondition));
hkm.Connect([this]() { return ToggleResearch(ScriptingContext{}); }, "ui.research",
AndCondition(VisibleWindowCondition(this), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::ToggleProduction, this), "ui.production",
AndCondition(VisibleWindowCondition(this), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::ToggleGovernment, this), "ui.government",
AndCondition(VisibleWindowCondition(this), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::ToggleDesign, this), "ui.design",
AndCondition(VisibleWindowCondition(this), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::ToggleObjects, this), "ui.map.objects",
AndCondition(VisibleWindowCondition(this), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::ToggleMessages, this), "ui.map.messages",
AndCondition(VisibleWindowCondition(this), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::ToggleEmpires, this), "ui.map.empires",
AndCondition(VisibleWindowCondition(this), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::TogglePedia, this), "ui.pedia",
AndCondition(VisibleWindowCondition(this), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::ShowGraphs, this), "ui.map.graphs",
AndCondition(VisibleWindowCondition(this), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::ShowMenu, this), "ui.gamemenu",
AndCondition(VisibleWindowCondition(this), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::KeyboardZoomIn, this), "ui.zoom.in",
AndCondition(NotCoveredMapWndCondition(*this), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::KeyboardZoomIn, this), "ui.zoom.in.alt",
AndCondition(NotCoveredMapWndCondition(*this), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::KeyboardZoomOut, this), "ui.zoom.out",
AndCondition(NotCoveredMapWndCondition(*this), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::KeyboardZoomOut, this), "ui.zoom.out.alt",
AndCondition(NotCoveredMapWndCondition(*this), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::ZoomToHomeSystem, this), "ui.map.system.zoom.home",
AndCondition(NotCoveredMapWndCondition(*this), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::ZoomToPrevSystem, this), "ui.map.system.zoom.prev",
AndCondition(NotCoveredMapWndCondition(*this), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::ZoomToNextSystem, this), "ui.map.system.zoom.next",
AndCondition(NotCoveredMapWndCondition(*this), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::ZoomToPrevOwnedSystem, this), "ui.map.system.owned.zoom.prev",
AndCondition(NotCoveredMapWndCondition(*this), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::ZoomToNextOwnedSystem, this), "ui.map.system.owned.zoom.next",
AndCondition(NotCoveredMapWndCondition(*this), NoModalWndsOpenCondition));
// the list of windows for which the fleet shortcuts are blacklisted.
std::array<const GG::Wnd*, 3> bl = {m_research_wnd.get(), m_production_wnd.get(), m_design_wnd.get()};
hkm.Connect(boost::bind(&MapWnd::ZoomToPrevFleet, this), "ui.map.fleet.zoom.prev",
AndCondition(OrCondition(InvisibleWindowCondition(bl), VisibleWindowCondition(this)), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::ZoomToNextFleet, this), "ui.map.fleet.zoom.next",
AndCondition(OrCondition(InvisibleWindowCondition(bl), VisibleWindowCondition(this)), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::ZoomToPrevIdleFleet, this), "ui.map.fleet.idle.zoom.prev",
AndCondition(OrCondition(InvisibleWindowCondition(bl), VisibleWindowCondition(this)), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::ZoomToNextIdleFleet, this), "ui.map.fleet.idle.zoom.next",
AndCondition(OrCondition(InvisibleWindowCondition(bl), VisibleWindowCondition(this)), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::PanX, this, GG::X(50)), "ui.pan.right",
AndCondition(OrCondition(InvisibleWindowCondition(bl), VisibleWindowCondition(this)), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::PanX, this, GG::X(-50)), "ui.pan.left",
AndCondition(OrCondition(InvisibleWindowCondition(bl), VisibleWindowCondition(this)), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::PanY, this, GG::Y(50)), "ui.pan.down",
AndCondition(OrCondition(InvisibleWindowCondition(bl), VisibleWindowCondition(this)), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&MapWnd::PanY, this, GG::Y(-50)), "ui.pan.up",
AndCondition(OrCondition(InvisibleWindowCondition(bl), VisibleWindowCondition(this)), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&ToggleBoolOption, "ui.map.scale.legend.shown"), "ui.map.scale.legend",
AndCondition(OrCondition(InvisibleWindowCondition(bl), VisibleWindowCondition(this)), NoModalWndsOpenCondition));
hkm.Connect(boost::bind(&ToggleBoolOption, "ui.map.scale.circle.shown"), "ui.map.scale.circle",
AndCondition(OrCondition(InvisibleWindowCondition(bl), VisibleWindowCondition(this)), NoModalWndsOpenCondition));
// these are general-use hotkeys, only connected here as a convenient location to do so once.
hkm.Connect(boost::bind(&GG::GUI::CutFocusWndText, GG::GUI::GetGUI()), "ui.cut");
hkm.Connect(boost::bind(&GG::GUI::CopyFocusWndText, GG::GUI::GetGUI()), "ui.copy");
hkm.Connect(boost::bind(&GG::GUI::PasteFocusWndClipboardText, GG::GUI::GetGUI()), "ui.paste");
hkm.Connect(boost::bind(&GG::GUI::FocusWndSelectAll, GG::GUI::GetGUI()), "ui.select.all");
hkm.Connect(boost::bind(&GG::GUI::FocusWndDeselect, GG::GUI::GetGUI()), "ui.select.none");
//hkm.Connect(boost::bind(&GG::GUI::SetPrevFocusWndInCycle, GG::GUI::GetGUI()), "ui.focus.prev",
// NoModalWndsOpenCondition);
//hkm.Connect(boost::bind(&GG::GUI::SetNextFocusWndInCycle, GG::GUI::GetGUI()), "ui.focus.next",
// NoModalWndsOpenCondition);
hkm.RebuildShortcuts();
}
void MapWnd::CloseAllPopups()
{ GG::ProcessThenRemoveExpiredPtrs(m_popups, [](auto& wnd) { wnd->Close(); }); }
void MapWnd::HideAllPopups()
{ GG::ProcessThenRemoveExpiredPtrs(m_popups, [](auto& wnd) { wnd->Hide(); }); }
void MapWnd::SetFleetExploring(const int fleet_id) {
if (!std::count(m_fleets_exploring.begin(), m_fleets_exploring.end(), fleet_id)) {
m_fleets_exploring.insert(fleet_id);
DispatchFleetsExploring();
}
}
void MapWnd::StopFleetExploring(const int fleet_id) {
auto it = m_fleets_exploring.find(fleet_id);
if (it == m_fleets_exploring.end())
return;
m_fleets_exploring.erase(it);
DispatchFleetsExploring();
// force UI update. Removing a fleet from the UI's list of exploring fleets
// doesn't actually change the Fleet object's state in any way, so the UI
// would otherwise still show the fleet as "exploring"
if (auto fleet = Objects().get<Fleet>(fleet_id))
fleet->StateChangedSignal();
}
bool MapWnd::IsFleetExploring(const int fleet_id)
{ return std::count(m_fleets_exploring.begin(), m_fleets_exploring.end(), fleet_id); }
namespace {
typedef std::vector<int> RouteListType;
typedef std::pair<double, RouteListType> OrderedRouteType;
typedef std::pair<int, RouteListType> FleetRouteType;
typedef std::pair<double, FleetRouteType> OrderedFleetRouteType;
typedef std::unordered_map<int, int> SystemFleetMap;
/** Number of jumps in a given route */
int JumpsForRoute(const RouteListType& route) {
int count = static_cast<int>(route.size());
if (count > 0) // dont count source system
-- count;
return count;
}
/** If @p fleet can determine an eta for @p route */
bool FleetRouteInRange(const Fleet* fleet, const RouteListType& route,
const ScriptingContext& context)
{
const auto eta_final_turns = fleet->ETA(fleet->MovePath(route, false, context)).first;
return (eta_final_turns != Fleet::ETA_NEVER &&
eta_final_turns != Fleet::ETA_UNKNOWN &&
eta_final_turns != Fleet::ETA_OUT_OF_RANGE);
}
// helper function for DispatchFleetsExploring
// return systems ID with a starlane connecting them to a system in \a system_ids
boost::container::flat_set<int> NeighbourSystemsOf(const Empire* empire, const Universe& universe,
const auto& system_ids)
{
const auto starlanes{empire->KnownStarlanes(universe)};
using starlanes_t = std::decay_t<decltype(starlanes)>;
static_assert(std::is_same_v<starlanes_t, boost::container::flat_set<Empire::LaneEndpoints>>,
"make sure starlanes is sorted for efficient insertion into flat_set below");
const auto lane_starts_in_system_in_ids = [&system_ids](const auto lane) {
return std::any_of(system_ids.begin(), system_ids.end(),
[lane](const auto sys_id) { return lane.start == sys_id; });
};
auto rng = starlanes | range_filter(lane_starts_in_system_in_ids)
| range_transform([](const auto lane) { return lane.end; });
boost::container::flat_set<int> retval;
retval.reserve(starlanes.size());
retval.insert(rng.begin(), rng.end());
return retval;
}
/** Get the shortest suitable route from @p start_id to @p destination_id as known to @p empire_id */
OrderedRouteType GetShortestRoute(int empire_id, int start_id, int destination_id) {
const Universe& universe = GetUniverse();
const ObjectMap& objects = universe.Objects();
const EmpireManager& empires = Empires();
const auto start_system = objects.getRaw<System>(start_id);
const auto dest_system = objects.getRaw<System>(destination_id);
if (!start_system || !dest_system) {
WarnLogger() << "GetShortestRoute: couldn't find start or destination systems";
return {};
}
const auto ignore_hostile = GetOptionsDB().Get<bool>("ui.fleet.explore.hostile.ignored");
const auto fleet_pred = std::make_shared<HostileVisitor>(empire_id, empires);
auto [system_list, path_length] = ignore_hostile ?
universe.GetPathfinder()->ShortestPath(start_id, destination_id, empire_id, objects) :
universe.GetPathfinder()->ShortestPath(start_id, destination_id, empire_id, fleet_pred, empires, objects);
if (!system_list.empty() && path_length > 0.0)
return {path_length, std::move(system_list)};
return {};
}
/** Route from @p fleet current location to @p destination */
OrderedFleetRouteType GetOrderedFleetRoute(const Fleet* fleet,
const System* destination,
const ScriptingContext& context)
{
const ObjectMap& objects{context.ContextObjects()};
if (!fleet || !destination) {
WarnLogger() << "GetOrderedFleetRoute: null fleet or system";
return {};
}
if ((fleet->Fuel(objects) < 1.0f) || !fleet->MovePath(false, context).empty()) {
WarnLogger() << "GetOrderedFleetRoute: no fuel or non-empty move path";
return {};
}
auto [route_length, route_ids] = GetShortestRoute(fleet->Owner(), fleet->SystemID(), destination->ID());
if (route_length <= 0.0) {
TraceLogger() << "No suitable route from system " << fleet->SystemID() << " to " << destination->ID()
<< " (" << route_ids.size() << ">" << route_length << ")";
return {};
}
if (!FleetRouteInRange(fleet, route_ids, context)) {
TraceLogger() << "Fleet " << std::to_string(fleet->ID())
<< " has no ETA for route to " << std::to_string(route_ids.back());
return {};
}
// decrease priority of system if previously viewed but not yet explored
if (!destination->Name().empty()) {
route_length *= GetOptionsDB().Get<float>("ui.fleet.explore.system.known.multiplier");
TraceLogger() << "Deferred priority for system " << destination->Name() << " (" << destination->ID() << ")";
}
return std::pair{route_length, std::pair{fleet->ID(), std::move(route_ids)}};
}
/** Shortest route not exceeding @p max_jumps from @p dest_id to a system with supply as known to @p empire */
OrderedRouteType GetNearestSupplyRoute(const std::shared_ptr<const Empire>& empire,
int dest_id, int max_jumps,
const ScriptingContext& context)
{
OrderedRouteType retval;
if (!empire) {
WarnLogger() << "Invalid empire";
return retval;
}
auto supplyable_systems = context.supply.FleetSupplyableSystemIDs(
empire->EmpireID(), true, context);
if (!supplyable_systems.empty()) {
TraceLogger() << [&supplyable_systems]() {
std::string msg = "Supplyable systems:";
for (auto sys : supplyable_systems)
msg.append(" " + std::to_string(sys));
return msg;
}();
}
OrderedRouteType shortest_route;
for (auto supply_system_id : supplyable_systems) {
shortest_route = GetShortestRoute(empire->EmpireID(), dest_id, supply_system_id);
TraceLogger() << [&shortest_route, dest_id]() {
std::string msg = "Checking supply route from " + std::to_string(dest_id) +
" dist:" + std::to_string(shortest_route.first) + " systems:";
for (auto node : shortest_route.second)
msg.append(" " + std::to_string(node));
return msg;
}();
auto route_jumps = JumpsForRoute(shortest_route.second);
if (max_jumps > -1 && route_jumps > max_jumps) {
TraceLogger() << "Rejecting route to " << std::to_string(*shortest_route.second.rbegin())
<< " jumps " << std::to_string(route_jumps) << " exceed max " << std::to_string(max_jumps);
continue;
}
if (shortest_route.first <= 0.0 || shortest_route.second.empty()) {
TraceLogger() << "Invalid route";
continue;
}
if (retval.first <= 0.0 || shortest_route.first < retval.first) {
TraceLogger() << "Setting " << std::to_string(*shortest_route.second.rbegin()) << " as shortest route";
retval = std::move(shortest_route);
}
}
return retval;
}
/** If @p fleet would be able to reach a system with supply after completing @p route */
bool CanResupplyAfterDestination(const Fleet* fleet, const RouteListType& route,
const ScriptingContext& context)
{
const ObjectMap& objects{context.ContextObjects()};
if (!fleet || route.empty()) {
WarnLogger() << "Invalid fleet or empty route";
return false;
}
auto empire = context.GetEmpire(fleet->Owner());
if (!empire) {
WarnLogger() << "Invalid empire";
return false;
}
int max_jumps = std::trunc(fleet->Fuel(objects));
if (max_jumps < 1) {
TraceLogger() << "Not enough fuel " << std::to_string(max_jumps)
<< " to move fleet " << std::to_string(fleet->ID());
return false;
}
auto dest_nearest_supply = GetNearestSupplyRoute(
empire, route.back(), max_jumps, context);
auto dest_nearest_supply_jumps = JumpsForRoute(dest_nearest_supply.second);
auto dest_jumps = JumpsForRoute(route);
int total_jumps = dest_jumps + dest_nearest_supply_jumps;
if (total_jumps > max_jumps) {
TraceLogger() << "Not enough fuel " << std::to_string(max_jumps)
<< " for fleet " << std::to_string(fleet->ID())
<< " to resupply after destination " << std::to_string(total_jumps);
return false;
}
return true;
}
/** Route from current system of @p fleet to nearest system with supply as determined by owning empire of @p fleet */
OrderedRouteType ExploringFleetResupplyRoute(const Fleet* fleet,
const ScriptingContext& context)
{
auto empire = context.GetEmpire(fleet->Owner());
if (!empire) {
WarnLogger() << "Invalid empire for id " << fleet->Owner();
return {};
}
auto nearest_supply = GetNearestSupplyRoute(empire, fleet->SystemID(),
std::trunc(fleet->Fuel(context.ContextObjects())),
context);
if (nearest_supply.first > 0.0 && FleetRouteInRange(fleet, nearest_supply.second, context))
return nearest_supply;
return {};
}
/** Issue an order for @p fleet to move to nearest system with supply */
bool IssueFleetResupplyOrder(const Fleet* fleet, ScriptingContext& context) {
if (!fleet) {
WarnLogger() << "Invalid fleet";
return false;
}
auto route = ExploringFleetResupplyRoute(fleet, context);
// Attempt move order if route is not empty and fleet has enough fuel to reach it
if (route.second.empty()) {
TraceLogger() << "Empty route for resupply of exploring fleet " << fleet->ID();
return false;
}
auto num_jumps_resupply = JumpsForRoute(route.second);
int max_fleet_jumps = std::trunc(fleet->Fuel(context.ContextObjects()));
if (num_jumps_resupply <= max_fleet_jumps) {
GGHumanClientApp::GetApp()->Orders().IssueOrder(
std::make_shared<FleetMoveOrder>(
fleet->Owner(), fleet->ID(), *route.second.crbegin(), false, context),
context);
} else {
TraceLogger() << "Not enough fuel for fleet " << fleet->ID()
<< " to resupply at system " << *route.second.crbegin();
return false;
}
if (fleet->FinalDestinationID() == *route.second.crbegin()) {
TraceLogger() << "Sending fleet " << fleet->ID()
<< " to refuel at system " << *route.second.crbegin();
return true;
} else {
TraceLogger() << "Fleet move order failed fleet:" << fleet->ID() << " route:"
<< [&route]() {
std::string retval;
for (auto node : route.second)
retval += " " + std::to_string(node);
return retval;
}();
}
return false;
}
/** Issue order for @p fleet to move using @p route */
bool IssueFleetExploreOrder(const Fleet* fleet, const RouteListType& route,
ScriptingContext& context)
{
if (!fleet || route.empty()) {
WarnLogger() << "Invalid fleet or empty route";
return false;
}
if (!FleetRouteInRange(fleet, route, context)) {
TraceLogger() << "Fleet " << std::to_string(fleet->ID())
<< " has no eta for route to " << std::to_string(route.back());
return false;
}
GGHumanClientApp::GetApp()->Orders().IssueOrder(
std::make_shared<FleetMoveOrder>(fleet->Owner(), fleet->ID(),
route.back(), false, context),
context);
if (fleet->FinalDestinationID() == route.back()) {
TraceLogger() << "Sending fleet " << fleet->ID() << " to explore system " << route.back();
return true;
}
TraceLogger() << "Fleet move order failed fleet:" << fleet->ID() << " dest:" << route.back();
return false;
}
/** Determine and issue move order for fleet and route @p fleet_route */
void IssueExploringFleetOrders(boost::unordered_set<int>& idle_fleets,
SystemFleetMap& systems_being_explored,
const FleetRouteType& fleet_route,
ScriptingContext& context)
{
ObjectMap& objects{context.ContextObjects()};
const auto& route = fleet_route.second;
if (route.empty()) { // no route
WarnLogger() << "Attempted to issue move order with empty route";
return;
}
if (idle_fleets.empty()) { // no more fleets to issue orders to
TraceLogger() << "No idle fleets";
return;
}
if (systems_being_explored.contains(route.back())) {
TraceLogger() << "System " << std::to_string(route.back()) << " already being explored";
return;
}
auto fleet_id = fleet_route.first;
auto idle_fleet_it = idle_fleets.find(fleet_id);
if (idle_fleet_it == idle_fleets.end()) { // fleet no longer idle
TraceLogger() << "Fleet " << std::to_string(fleet_id) << " not idle";
return;
}
auto fleet = objects.getRaw<Fleet>(fleet_id);
if (!fleet) {
ErrorLogger() << "No valid fleet with id " << fleet_id;
idle_fleets.erase(idle_fleet_it);
return;
}
if (std::trunc(fleet->Fuel(objects)) < 1) { // wait for fuel
TraceLogger() << "Not enough fuel to move fleet " << std::to_string(fleet->ID());
return;
}
// Determine if fleet should refuel
if (fleet->Fuel(objects) < fleet->MaxFuel(objects) &&
!CanResupplyAfterDestination(fleet, route, context))
{
if (IssueFleetResupplyOrder(fleet, context)) {
idle_fleets.erase(idle_fleet_it);
return;
}
TraceLogger() << "Fleet " << std::to_string(fleet->ID()) << " can not reach resupply";
}
if (IssueFleetExploreOrder(fleet, route, context)) {
idle_fleets.erase(idle_fleet_it);
systems_being_explored.emplace(*route.rbegin(), fleet->ID());
}
}
};
void MapWnd::DispatchFleetsExploring() {
ScriptingContext context;
const auto& universe{context.ContextUniverse()};
const auto& objects{context.ContextObjects()};
int empire_id = GGHumanClientApp::GetApp()->EmpireID();
auto empire = context.GetEmpire(empire_id);
if (!empire) {
WarnLogger() << "Invalid empire";
return;
}
int max_routes_per_system = GetOptionsDB().Get<int>("ui.fleet.explore.system.route.limit");
const auto& destroyed_objects{universe.EmpireKnownDestroyedObjectIDs(empire_id)};
boost::unordered_set<int> idle_fleets;
/** all systems ID for which an exploring fleet is in route and the fleet assigned */
SystemFleetMap systems_being_explored;
// clean the fleet list by removing non-existing fleet, and extract the
// fleets waiting for orders
for (const auto* fleet : objects.findRaw<Fleet>(m_fleets_exploring)) {
if (!fleet)
continue;
if (destroyed_objects.contains(fleet->ID())) {
m_fleets_exploring.erase(fleet->ID()); //this fleet can't explore anymore
} else {
if (fleet->MovePath(false, context).empty())
idle_fleets.insert(fleet->ID());
else
systems_being_explored.emplace(fleet->FinalDestinationID(), fleet->ID());
}
}
if (idle_fleets.empty())
return;
TraceLogger() << [&idle_fleets]() {
std::string retval = "MapWnd::DispatchFleetsExploring Idle Exploring Fleet IDs:";
for (auto fleet_id : idle_fleets)
retval += " " + std::to_string(fleet_id);
return retval;
}();
//list all unexplored systems by taking the neighboors of explored systems because ObjectMap does not list them all.
auto explored_systems{empire->ExploredSystems()};
auto candidates_unknown_systems{NeighbourSystemsOf(empire.get(), universe, explored_systems)};
candidates_unknown_systems.merge(NeighbourSystemsOf(empire.get(), universe, candidates_unknown_systems));
// Populate list of unexplored systems
boost::unordered_set<int> unexplored_systems;
for (const auto* system : objects.findRaw<System>(candidates_unknown_systems)) {
if (!system)
continue;
if (!empire->HasExploredSystem(system->ID()) &&
!systems_being_explored.contains(system->ID()))
{ unexplored_systems.emplace(system->ID()); }
}
if (unexplored_systems.empty()) {
TraceLogger() << "No unknown systems to explore";
return;
}
TraceLogger() << [&unexplored_systems]() {
std::string retval = "MapWnd::DispatchFleetsExploring Unknown System IDs:";
for (auto system_id : unexplored_systems)
retval += " " + std::to_string(system_id);
return retval;
}();
std::multimap<double, FleetRouteType> fleet_routes; // priority, (fleet, route)
// Determine fleet routes for each unexplored system
std::unordered_map<int, int> fleet_route_count;
for (const auto* unexplored_system : objects.findRaw<System>(unexplored_systems)) {
if (!unexplored_system)
continue;
for (const auto& fleet_id : idle_fleets) {
if (max_routes_per_system > 0 &&
fleet_route_count[unexplored_system->ID()] > max_routes_per_system)
{ break; }
auto fleet = objects.getRaw<Fleet>(fleet_id);
if (!fleet) {
WarnLogger() << "Invalid fleet " << fleet_id;
continue;
}
if (fleet->Fuel(objects) < 1.0f)
continue;
auto route = GetOrderedFleetRoute(fleet, unexplored_system, context);
if (route.first > 0.0) {
++fleet_route_count[unexplored_system->ID()];
fleet_routes.insert(std::move(route));
}
}
}
if (!fleet_routes.empty()) {
TraceLogger() << [&fleet_routes]() {
std::string retval = "MapWnd::DispatchFleetsExploring Explorable Systems:\n\t Priority\tFleet\tDestination";
for (const auto& route : fleet_routes) {
retval.append("\n\t" + std::to_string(route.first) + "\t" +
std::to_string(route.second.first) + "\t " +
std::to_string(route.second.second.empty() ? -1 : *route.second.second.rbegin()));
}
return retval;
}();
}
// Issue fleet orders
for (const auto& fleet_route : fleet_routes)
IssueExploringFleetOrders(idle_fleets, systems_being_explored, fleet_route.second, context);
// verify fleets have expected destination
for (const auto& [dest_sys_id, fleet_id] : systems_being_explored) {
auto fleet = objects.get<Fleet>(fleet_id);
if (!fleet)
continue;
auto dest_id = fleet->FinalDestinationID();
if (dest_id == dest_sys_id)
continue;
WarnLogger() << "Non idle exploring fleet "<< fleet_id << " has differing destination:"
<< fleet->FinalDestinationID() << " expected:" << dest_sys_id;
idle_fleets.emplace(fleet_id);
// systems_being_explored.erase(system_fleet_it);
}
if (!idle_fleets.empty()) {
DebugLogger() << [&idle_fleets]() {
std::string retval = "MapWnd::DispatchFleetsExploring Idle exploring fleets after orders:";
retval.reserve(retval.size() + 10*idle_fleets.size()); // rough guesstimate
for (auto fleet_id : idle_fleets)
retval.append(" ").append(std::to_string(fleet_id));
return retval;
}();
}
}
void MapWnd::ShowAllPopups()
{ GG::ProcessThenRemoveExpiredPtrs(m_popups, [](auto& wnd) { wnd->Show(); }); }
|